root/openpgpsdk/trunk/tests/test_rsa_verify.c

Revision 521 (checked in by rachel, 5 years ago)

Initial implementation of Validation of Document Signatures.
(Note: Does not yet interoperate with GPG)
Renamed validate_cb to more accurate name of validate_key_cb.
Implemented validate_data_cb to handle validation of documents & signed cleartext.
Added in error-handling when validation fails.
Turned off unwanted debug output.

Line 
1 #include "CUnit/Basic.h"
2
3 #include <openpgpsdk/types.h>
4 #include "openpgpsdk/keyring.h"
5 #include <openpgpsdk/armour.h>
6 #include "openpgpsdk/packet.h"
7 #include "openpgpsdk/packet-parse.h"
8 #include "openpgpsdk/util.h"
9 #include "openpgpsdk/std_print.h"
10 #include "openpgpsdk/readerwriter.h"
11 #include "openpgpsdk/validate.h"
12
13 // \todo change this once we know it works
14 #include "../src/advanced/parse_local.h"
15
16 #include "tests.h"
17
18 #ifndef ATTRIBUTE_UNUSED
19
20 #ifndef WIN32
21 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
22 #else
23 #define ATTRIBUTE_UNUSED
24 #endif // #ifndef WIN32
25
26 #endif /* ATTRIBUTE_UNUSED */
27
28 static char *filename_rsa_noarmour_nopassphrase="gpg_signed_noarmour_nopassphrase.txt";
29 static char *filename_rsa_armour_nopassphrase="gpg_signed_armour_nopassphrase.txt";
30 static char *filename_rsa_noarmour_passphrase="gpg_signed_armour_nopassphrase.txt";
31 static char *filename_rsa_armour_passphrase="gpg_signed_armour_passphrase.txt";
32
33 static ops_parse_cb_return_t
34 callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
35     {
36     //    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
37
38     //        ops_print_packet(content_);
39
40     switch(content_->tag)
41         {
42         /*
43     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
44         break;
45
46     case OPS_PTAG_CT_LITERAL_DATA_BODY:
47         return callback_literal_data(content_,cbinfo);
48         break;
49         */
50
51     case OPS_PTAG_CT_ONE_PASS_SIGNATURE:
52         break;
53
54     case OPS_PTAG_CT_SIGNATURE:
55     case OPS_PTAG_CT_SIGNATURE_HEADER:
56     case OPS_PTAG_CT_SIGNATURE_FOOTER:
57     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
58     case OPS_PTAG_CT_LITERAL_DATA_BODY:
59         return callback_data_signature(content_, cbinfo);
60
61         /*
62     case OPS_PTAG_CT_UNARMOURED_TEXT:
63         printf("OPS_PTAG_CT_UNARMOURED_TEXT\n");
64         if(!skipping)
65             {
66             puts("Skipping...");
67             skipping=ops_true;
68             }
69         fwrite(content->unarmoured_text.data,1,
70                content->unarmoured_text.length,stdout);
71         break;
72
73     case OPS_PTAG_CT_PK_SESSION_KEY:
74         return callback_pk_session_key(content_,cbinfo);
75
76     case OPS_PARSER_CMD_GET_SECRET_KEY:
77         return callback_cmd_get_secret_key(content_,cbinfo);
78
79     case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
80         return callback_cmd_get_secret_key_passphrase(content_,cbinfo);
81
82     case OPS_PTAG_CT_LITERAL_DATA_BODY:
83         return callback_literal_data(content_,cbinfo);
84         //      text=ops_mallocz(content->literal_data_body.length+1);
85         //      memcpy(text,content->literal_data_body.data,content->literal_data_body.length);
86         //              break;
87
88     case OPS_PARSER_PTAG:
89     case OPS_PTAG_CT_ARMOUR_HEADER:
90     case OPS_PTAG_CT_ARMOUR_TRAILER:
91     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
92     case OPS_PTAG_CT_COMPRESSED:
93     case OPS_PTAG_CT_SE_IP_DATA_BODY:
94     case OPS_PTAG_CT_SE_IP_DATA_HEADER:
95         // Ignore these packets
96         // They're handled in ops_parse_one_packet()
97         // and nothing else needs to be done
98         break;
99 */
100
101     default:
102         return callback_general(content_,cbinfo);
103         }
104
105     return OPS_RELEASE_MEMORY;
106     }
107
108 /* Signature verification suite initialization.
109  * Create temporary test files.
110  */
111
112 int init_suite_rsa_verify(void)
113     {
114     char cmd[MAXBUF+1];
115
116     // Create test files
117
118     create_testfile(filename_rsa_noarmour_nopassphrase);
119     create_testfile(filename_rsa_armour_nopassphrase);
120     create_testfile(filename_rsa_noarmour_passphrase);
121     create_testfile(filename_rsa_armour_passphrase);
122
123     // Now sign the test files with GPG
124
125     snprintf(cmd,MAXBUF,"gpg --quiet --no-tty --homedir=%s --openpgp --compress-level 0 --sign --local-user %s %s/%s",
126              dir, alpha_name, dir, filename_rsa_noarmour_nopassphrase);
127     if (system(cmd))
128         { return 1; }
129
130     snprintf(cmd,MAXBUF,"gpg --quiet --no-tty --homedir=%s --compress-level 0 --sign --armour --local-user %s %s/%s",
131              dir, alpha_name, dir, filename_rsa_armour_nopassphrase);
132     if (system(cmd))
133         { return 1; }
134
135     snprintf(cmd,MAXBUF,"gpg --quiet --no-tty --homedir=%s --compress-level 0 --sign --local-user %s --passphrase %s %s/%s",
136              dir, bravo_name, bravo_passphrase, dir, filename_rsa_noarmour_passphrase);
137     if (system(cmd))
138         { return 1; }
139
140     snprintf(cmd,MAXBUF,"gpg --quiet --no-tty  --homedir=%s --compress-level 0 --sign --armour --local-user %s --passphrase %s %s/%s",
141              dir, bravo_name, bravo_passphrase, dir, filename_rsa_armour_passphrase);
142     if (system(cmd))
143         { return 1; }
144
145     // Return success
146     return 0;
147     }
148
149 int clean_suite_rsa_verify(void)
150     {
151     ops_finish();
152
153     reset_vars();
154
155     return 0;
156     }
157
158 static void test_rsa_verify(const int has_armour, const int has_passphrase ATTRIBUTE_UNUSED, const char *filename, const char* protocol)
159     {
160     char signedfile[MAXBUF+1];
161     //    char testtext[MAXBUF+1];
162     char *suffix= has_armour ? "asc" : "gpg";
163     int fd=0;
164     ops_parse_info_t *pinfo=NULL;
165     validate_data_cb_arg_t validate_arg;
166     ops_validate_result_t result;
167     int rtn=0;
168    
169     // open signed file
170     snprintf(signedfile,MAXBUF,"%s/%s%s%s.%s",dir,
171              protocol==NULL ? "" : protocol,
172              protocol==NULL ? "" : "_",
173              filename,suffix);
174 #ifdef WIN32
175     fd=open(signedfile,O_RDONLY | O_BINARY);
176 #else
177     fd=open(signedfile,O_RDONLY);
178 #endif
179     if(fd < 0)
180         {
181         perror(signedfile);
182         exit(2);
183         }
184    
185     // Set verification reader and handling options
186
187     pinfo=ops_parse_info_new();
188
189     memset(&validate_arg,'\0',sizeof validate_arg);
190     validate_arg.result=&result;
191     validate_arg.keyring=&pub_keyring;
192     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
193
194     ops_parse_cb_set(pinfo,callback,&validate_arg);
195     ops_reader_set_fd(pinfo,fd);
196     pinfo->rinfo.accumulate=ops_true;
197
198     // Set up armour/passphrase options
199
200     if (has_armour)
201         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
202     //    current_passphrase=has_passphrase ? passphrase : nopassphrase;
203     
204     // Do the verification
205
206     rtn=ops_parse(pinfo);
207     ops_print_errors(ops_parse_info_get_errors(pinfo));
208     CU_ASSERT(rtn==1);
209
210     // Tidy up
211     if (has_armour)
212         ops_reader_pop_dearmour(pinfo);
213
214     ops_parse_info_delete(pinfo);
215
216     close(fd);
217    
218 #ifdef NEEDED
219     // File contents should match
220     create_testtext(filename,&testtext[0],MAXBUF);
221     CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0);
222 #endif
223     }
224
225 void test_rsa_verify_noarmour_nopassphrase(void)
226     {
227     int armour=0;
228     int passphrase=0;
229     assert(pub_keyring.nkeys);
230     //    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id);
231     //    assert(pub_key);
232     test_rsa_verify(armour,passphrase,filename_rsa_noarmour_nopassphrase,NULL);
233     }
234
235 #ifdef TBD
236 void test_rsa_encrypt_armour_singlekey(void)
237     {
238     int armour=1;
239     char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>";
240     const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id);
241     assert(pub_key);
242     test_rsa_encrypt(armour,pub_key,filename_rsa_armour_singlekey);
243     }
244
245 void test_rsa_encrypt_noarmour_passphrase(void)
246     {
247     int armour=0;
248     int passphrase=1;
249     test_rsa_encrypt(armour,passphrase,filename_rsa_noarmour_passphrase);
250     }
251
252 void test_rsa_encrypt_armour_passphrase(void)
253     {
254     int armour=1;
255     int passphrase=1;
256     test_rsa_encrypt(armour,passphrase,filename_rsa_armour_passphrase);
257     }
258 #endif /*TBD*/
259
260 CU_pSuite suite_rsa_verify()
261 {
262     CU_pSuite suite = NULL;
263
264     suite = CU_add_suite("RSA Verification Suite", init_suite_rsa_verify, clean_suite_rsa_verify);
265     if (!suite)
266             return NULL;
267
268     // add tests to suite
269     
270     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_verify_noarmour_nopassphrase))
271             return NULL;
272    
273     /*
274     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_verify_noarmour_passphrase))
275             return NULL;
276     */
277     return suite;
278 }
279
Note: See TracBrowser for help on using the browser.