root/openpgpsdk/trunk/tests/test_rsa_signature.c

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

Implemented basic transferable public & secret keys (binary format).
Implemented key signing.
Renamed key_data struct to keydata.
Added tests for key generation and signing.

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/packet-show.h"
9 #include "openpgpsdk/util.h"
10 #include "openpgpsdk/std_print.h"
11 #include "openpgpsdk/readerwriter.h"
12 #include "openpgpsdk/validate.h"
13
14 // \todo change this once we know it works
15 #include "../src/lib/parse_local.h"
16
17 #include "tests.h"
18
19 static int debug=0;
20
21 static char *filename_rsa_noarmour_nopassphrase="ops_rsa_signed_noarmour_nopassphrase.txt";
22 static char *filename_rsa_noarmour_passphrase="ops_rsa_signed_noarmour_passphrase.txt";
23 static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt";
24 static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt";
25 static char *filename_rsa_clearsign_file_nopassphrase="ops_rsa_signed_clearsign_file_nopassphrase.txt";
26 static char *filename_rsa_clearsign_file_passphrase="ops_rsa_signed_clearsign_file_passphrase.txt";
27 static char *filename_rsa_clearsign_buf_nopassphrase="ops_rsa_signed_clearsign_buf_nopassphrase.txt";
28 static char *filename_rsa_clearsign_buf_passphrase="ops_rsa_signed_clearsign_buf_passphrase.txt";
29
30 /* Signature suite initialization.
31  * Create temporary directory.
32  * Create temporary test files.
33  */
34
35 int init_suite_rsa_signature(void)
36     {
37     // Create test files
38
39     create_testfile(filename_rsa_noarmour_nopassphrase);
40     create_testfile(filename_rsa_noarmour_passphrase);
41     create_testfile(filename_rsa_armour_nopassphrase);
42     create_testfile(filename_rsa_armour_passphrase);
43     create_testfile(filename_rsa_clearsign_file_nopassphrase);
44     create_testfile(filename_rsa_clearsign_file_passphrase);
45     create_testfile(filename_rsa_clearsign_buf_nopassphrase);
46     create_testfile(filename_rsa_clearsign_buf_passphrase);
47
48     // Return success
49     return 0;
50     }
51
52 int clean_suite_rsa_signature(void)
53     {
54     ops_finish();
55
56     reset_vars();
57
58     return 0;
59     }
60
61 static void test_rsa_signature_clearsign_file(const char *filename, const ops_secret_key_t *skey)
62     {
63     char cmd[MAXBUF+1];
64     char myfile[MAXBUF+1];
65     char signed_file[MAXBUF+1];
66     int rtn=0;
67
68     // setup filenames
69     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
70     snprintf(signed_file,sizeof signed_file,"%s.asc",myfile);
71
72     // sign file
73     ops_sign_file_as_cleartext(myfile,skey);
74
75     /*
76      * Validate output
77      */
78
79     // Check with OPS
80
81     {
82     int fd=0;
83     ops_parse_info_t *pinfo=NULL;
84     validate_data_cb_arg_t validate_arg;
85     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));
86     int rtn=0;
87    
88     if (debug)
89         {
90         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
91         }
92    
93     // open signed file
94 #ifdef WIN32
95     fd=open(signed_file,O_RDONLY | O_BINARY);
96 #else
97     fd=open(signed_file,O_RDONLY);
98 #endif
99     if(fd < 0)
100         {
101         perror(signed_file);
102         exit(2);
103         }
104    
105     // Set verification reader and handling options
106     
107     pinfo=ops_parse_info_new();
108    
109     memset(&validate_arg,'\0',sizeof validate_arg);
110     validate_arg.result=result;
111     validate_arg.keyring=&pub_keyring;
112     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
113    
114     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
115     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
116     ops_reader_set_fd(pinfo,fd);
117     pinfo->rinfo.accumulate=ops_true;
118    
119     // Must de-armour because it's clearsigned
120     
121     ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
122    
123     // Do the verification
124     
125     rtn=ops_parse(pinfo);
126     ops_print_errors(ops_parse_info_get_errors(pinfo));
127     CU_ASSERT(rtn==1);
128    
129     // Tidy up
130     //    if (has_armour)
131         ops_reader_pop_dearmour(pinfo);
132    
133     ops_parse_info_delete(pinfo);
134    
135     close(fd);
136     ops_validate_result_free(result);
137     }
138
139     // Check signature with GPG
140     {
141
142     snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
143     rtn=system(cmd);
144     CU_ASSERT(rtn==0);
145     }
146     }
147
148 static void test_rsa_signature_clearsign_buf(const char *filename, const ops_secret_key_t *skey)
149     {
150     char cmd[MAXBUF+1];
151     char myfile[MAXBUF+1];
152     char signed_file[MAXBUF+1];
153     int rtn=0;
154     ops_memory_t *input=NULL;
155     ops_memory_t *output=NULL;
156
157     // setup filenames
158     // (we are testing the function which signs a buf, but still want
159     // to read/write the buffers from/to files for external viewing
160
161     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
162     snprintf(signed_file,sizeof signed_file,"%s.asc",myfile);
163
164     // read file contents
165     input=ops_write_buf_from_file(myfile);
166
167     // sign file
168     ops_sign_buf_as_cleartext((const char *)ops_memory_get_data(input),ops_memory_get_length(input),&output,skey);
169
170     // write to file
171     ops_write_file_from_buf(signed_file, (const char*)ops_memory_get_data(output),ops_memory_get_length(output));
172
173     /*
174      * Validate output
175      */
176
177     // Check with OPS
178
179     {
180     int fd=0;
181     ops_parse_info_t *pinfo=NULL;
182     validate_data_cb_arg_t validate_arg;
183     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));
184
185     int rtn=0;
186    
187     if (debug)
188         {
189         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
190         }
191    
192     // open signed file
193 #ifdef WIN32
194     fd=open(signed_file,O_RDONLY | O_BINARY);
195 #else
196     fd=open(signed_file,O_RDONLY);
197 #endif
198     if(fd < 0)
199         {
200         perror(signed_file);
201         exit(2);
202         }
203    
204     // Set verification reader and handling options
205     
206     pinfo=ops_parse_info_new();
207    
208     memset(&validate_arg,'\0',sizeof validate_arg);
209     validate_arg.result=result;
210     validate_arg.keyring=&pub_keyring;
211     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
212    
213     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
214     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
215     ops_reader_set_fd(pinfo,fd);
216     pinfo->rinfo.accumulate=ops_true;
217    
218     // Must de-armour because it's clearsigned
219     
220     ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
221    
222     // Do the verification
223     
224     rtn=ops_parse(pinfo);
225     ops_print_errors(ops_parse_info_get_errors(pinfo));
226     CU_ASSERT(rtn==1);
227    
228     // Tidy up
229     //    if (has_armour)
230         ops_reader_pop_dearmour(pinfo);
231    
232     ops_parse_info_delete(pinfo);
233    
234     close(fd);
235     ops_validate_result_free(result);
236     }
237
238     // Check signature with GPG
239     {
240
241     snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
242     rtn=system(cmd);
243     CU_ASSERT(rtn==0);
244     }
245     }
246
247 static void test_rsa_signature_sign(const int use_armour, const char *filename, const ops_secret_key_t *skey)
248     {
249     char cmd[MAXBUF+1];
250     char myfile[MAXBUF+1];
251     char signed_file[MAXBUF+1];
252     char *suffix= use_armour ? "asc" : "ops";
253     int rtn=0;
254
255     // filenames
256     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
257     snprintf(signed_file,sizeof signed_file,"%s.%s",myfile,suffix);
258
259     ops_sign_file(myfile, signed_file, skey, use_armour);
260
261     /*
262      * Validate output
263      */
264
265     // Check with OPS
266
267     {
268     int fd=0;
269     ops_parse_info_t *pinfo=NULL;
270     validate_data_cb_arg_t validate_arg;
271     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));;
272     int rtn=0;
273    
274     if (debug)
275         {
276         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
277         }
278    
279     // open signed file
280 #ifdef WIN32
281     fd=open(signed_file,O_RDONLY | O_BINARY);
282 #else
283     fd=open(signed_file,O_RDONLY);
284 #endif
285     if(fd < 0)
286         {
287         perror(signed_file);
288         exit(2);
289         }
290    
291     // Set verification reader and handling options
292     
293     pinfo=ops_parse_info_new();
294    
295     memset(&validate_arg,'\0',sizeof validate_arg);
296     validate_arg.result=result;
297     validate_arg.keyring=&pub_keyring;
298     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
299    
300     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
301     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
302     ops_reader_set_fd(pinfo,fd);
303     pinfo->rinfo.accumulate=ops_true;
304    
305     // Set up armour/passphrase options
306     
307     if (use_armour)
308         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
309    
310     // Do the verification
311     
312     rtn=ops_parse_and_print_errors(pinfo);
313     CU_ASSERT(rtn==1);
314    
315     // Tidy up
316     if (use_armour)
317         ops_reader_pop_dearmour(pinfo);
318    
319     ops_parse_info_delete(pinfo);
320    
321     close(fd);
322     ops_validate_result_free(result);
323     }
324
325     // Check signature with GPG
326     {
327
328     snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
329     rtn=system(cmd);
330     CU_ASSERT(rtn==0);
331     }
332     }
333
334 static void test_rsa_signature_noarmour_nopassphrase(void)
335     {
336     int armour=0;
337     assert(pub_keyring.nkeys);
338     test_rsa_signature_sign(armour,filename_rsa_noarmour_nopassphrase, alpha_skey);
339     }
340
341 static void test_rsa_signature_noarmour_passphrase(void)
342     {
343     int armour=0;
344     assert(pub_keyring.nkeys);
345     test_rsa_signature_sign(armour,filename_rsa_noarmour_passphrase, bravo_skey);
346     }
347
348 static void test_rsa_signature_armour_nopassphrase(void)
349     {
350     int armour=1;
351     assert(pub_keyring.nkeys);
352     test_rsa_signature_sign(armour,filename_rsa_armour_nopassphrase, alpha_skey);
353     }
354
355 static void test_rsa_signature_armour_passphrase(void)
356     {
357     int armour=1;
358     assert(pub_keyring.nkeys);
359     test_rsa_signature_sign(armour,filename_rsa_armour_passphrase, bravo_skey);
360     }
361
362 static void test_rsa_signature_clearsign_file_nopassphrase(void)
363     {
364     assert(pub_keyring.nkeys);
365     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase, alpha_skey);
366     }
367
368 static void test_rsa_signature_clearsign_file_passphrase(void)
369     {
370     assert(pub_keyring.nkeys);
371     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase, bravo_skey);
372     }
373
374 static void test_rsa_signature_clearsign_buf_nopassphrase(void)
375     {
376     assert(pub_keyring.nkeys);
377     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase, alpha_skey);
378     }
379
380 static void test_rsa_signature_clearsign_buf_passphrase(void)
381     {
382     assert(pub_keyring.nkeys);
383     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase, bravo_skey);
384     }
385
386 static void test_todo(void)
387     {
388     CU_FAIL("Test TODO: Test large files");
389     CU_FAIL("Test TODO: Sign with V3 signature?");
390     CU_FAIL("Test TODO: Use other hash algorithms?");
391     CU_FAIL("Test TODO: Check for key/signature expiry");
392     CU_FAIL("Test TODO: Check for key/signature revocation");
393     }
394
395 static int add_tests(CU_pSuite suite)
396     {
397     // add tests to suite
398     
399     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
400             return 0;
401    
402     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
403             return 0;
404    
405     if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase", test_rsa_signature_clearsign_file_nopassphrase))
406             return 0;
407    
408     if (NULL == CU_add_test(suite, "Clearsigned file, passphrase", test_rsa_signature_clearsign_file_passphrase))
409             return 0;
410
411     if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase", test_rsa_signature_clearsign_buf_nopassphrase))
412             return 0;
413    
414     if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase", test_rsa_signature_clearsign_buf_passphrase))
415             return 0;
416
417     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
418             return 0;
419    
420     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
421             return 0;
422    
423     if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo))
424             return 0;
425    
426     return 1;
427 }
428
429 CU_pSuite suite_rsa_signature()
430 {
431     CU_pSuite suite = NULL;
432
433     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
434     if (!suite)
435             return NULL;
436
437     if (!add_tests(suite))
438         return NULL;
439
440     return suite;
441     }
442
443
444 // EOF
445
Note: See TracBrowser for help on using the browser.