root/openpgpsdk/trunk/tests/test_rsa_signature.c

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

Doxygen changes.

Line 
1 /*
2  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
3  * All rights reserved.
4  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
5  * their moral rights under the UK Copyright Design and Patents Act 1988 to
6  * be recorded as the authors of this copyright work.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License.
10  *
11  * You may obtain a copy of the License at
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include "CUnit/Basic.h"
23
24 #include <openpgpsdk/types.h>
25 #include "openpgpsdk/keyring.h"
26 #include <openpgpsdk/armour.h>
27 #include <openpgpsdk/create.h>
28 #include "openpgpsdk/packet.h"
29 #include "openpgpsdk/packet-parse.h"
30 #include "openpgpsdk/packet-show.h"
31 #include "openpgpsdk/util.h"
32 #include "openpgpsdk/std_print.h"
33 #include "openpgpsdk/readerwriter.h"
34 #include "openpgpsdk/validate.h"
35
36 // \todo change this once we know it works
37 #include "../src/lib/parse_local.h"
38
39 #include "tests.h"
40
41 static int debug=0;
42
43 static char *filename_rsa_large_noarmour_nopassphrase="ops_rsa_signed_large_noarmour_nopassphrase.txt";
44 static char *filename_rsa_large_armour_nopassphrase="ops_rsa_signed_large_armour_nopassphrase.txt";
45 static char *filename_rsa_noarmour_nopassphrase="ops_rsa_signed_noarmour_nopassphrase.txt";
46 static char *filename_rsa_noarmour_passphrase="ops_rsa_signed_noarmour_passphrase.txt";
47 static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt";
48 static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt";
49 static char *filename_rsa_clearsign_file_nopassphrase="ops_rsa_signed_clearsign_file_nopassphrase.txt";
50 static char *filename_rsa_clearsign_file_passphrase="ops_rsa_signed_clearsign_file_passphrase.txt";
51 static char *filename_rsa_clearsign_buf_nopassphrase="ops_rsa_signed_clearsign_buf_nopassphrase.txt";
52 static char *filename_rsa_clearsign_buf_passphrase="ops_rsa_signed_clearsign_buf_passphrase.txt";
53
54 /* Signature suite initialization.
55  * Create temporary directory.
56  * Create temporary test files.
57  */
58
59 int init_suite_rsa_signature(void)
60     {
61     // Create test files
62
63     create_small_testfile(filename_rsa_noarmour_nopassphrase);
64     create_small_testfile(filename_rsa_noarmour_passphrase);
65     create_small_testfile(filename_rsa_armour_nopassphrase);
66     create_small_testfile(filename_rsa_armour_passphrase);
67     create_small_testfile(filename_rsa_clearsign_file_nopassphrase);
68     create_small_testfile(filename_rsa_clearsign_file_passphrase);
69     create_small_testfile(filename_rsa_clearsign_buf_nopassphrase);
70     create_small_testfile(filename_rsa_clearsign_buf_passphrase);
71
72     create_large_testfile(filename_rsa_large_noarmour_nopassphrase);
73     create_large_testfile(filename_rsa_large_armour_nopassphrase);
74
75     // Return success
76     return 0;
77     }
78
79 int clean_suite_rsa_signature(void)
80     {
81     ops_finish();
82
83     reset_vars();
84
85     return 0;
86     }
87
88 static void test_rsa_signature_clearsign_file(const char *filename, const ops_secret_key_t *skey)
89     {
90     char cmd[MAXBUF+1];
91     char myfile[MAXBUF+1];
92     char signed_file[MAXBUF+1];
93     int rtn=0;
94     ops_boolean_t overwrite;
95
96     // setup filenames
97     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
98     snprintf(signed_file,sizeof signed_file,"%s.asc",myfile);
99
100     // sign file
101     overwrite=ops_true;
102     ops_sign_file_as_cleartext(myfile, NULL, skey, overwrite);
103
104     /*
105      * Validate output
106      */
107
108     // Check with OPS
109
110     {
111     int fd=0;
112     ops_parse_info_t *pinfo=NULL;
113     validate_data_cb_arg_t validate_arg;
114     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));
115     int rtn=0;
116    
117     if (debug)
118         {
119         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
120         }
121    
122     // open signed file
123 #ifdef WIN32
124     fd=open(signed_file,O_RDONLY | O_BINARY);
125 #else
126     fd=open(signed_file,O_RDONLY);
127 #endif
128     if(fd < 0)
129         {
130         perror(signed_file);
131         exit(2);
132         }
133    
134     // Set verification reader and handling options
135     
136     pinfo=ops_parse_info_new();
137    
138     memset(&validate_arg,'\0',sizeof validate_arg);
139     validate_arg.result=result;
140     validate_arg.keyring=&pub_keyring;
141     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
142    
143     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
144     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
145     ops_reader_set_fd(pinfo,fd);
146     pinfo->rinfo.accumulate=ops_true;
147    
148     // Must de-armour because it's clearsigned
149     
150     ops_reader_push_dearmour(pinfo);
151    
152     // Do the verification
153     
154     rtn=ops_parse(pinfo);
155     ops_print_errors(ops_parse_info_get_errors(pinfo));
156     CU_ASSERT(rtn==1);
157    
158     // Tidy up
159     //    if (has_armour)
160         ops_reader_pop_dearmour(pinfo);
161    
162     ops_parse_info_delete(pinfo);
163    
164     close(fd);
165     ops_validate_result_free(result);
166     }
167
168     // Check signature with GPG
169     {
170
171     //snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
172     snprintf(cmd,sizeof cmd,"cat %s | %s --verify", signed_file, gpgcmd);
173     rtn=system(cmd);
174     CU_ASSERT(rtn==0);
175     }
176     }
177
178 static void test_rsa_signature_clearsign_buf(const char *filename, const ops_secret_key_t *skey)
179     {
180     char cmd[MAXBUF+1];
181     char myfile[MAXBUF+1];
182     char signed_file[MAXBUF+1];
183     int rtn=0;
184     ops_memory_t *input=NULL;
185     ops_memory_t *output=NULL;
186     ops_boolean_t overwrite;
187     int errnum=0;
188
189     // setup filenames
190     // (we are testing the function which signs a buf, but still want
191     // to read/write the buffers from/to files for external viewing
192
193     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
194     snprintf(signed_file,sizeof signed_file,"%s.asc",myfile);
195
196     // read file contents
197     input=ops_write_mem_from_file(myfile,&errnum);
198     CU_ASSERT(errnum==0);
199
200     // sign file
201     ops_sign_buf_as_cleartext((const char *)ops_memory_get_data(input),ops_memory_get_length(input),&output,skey);
202
203     // write to file
204     overwrite=ops_true;
205     ops_write_file_from_buf(signed_file, (const char*)ops_memory_get_data(output),ops_memory_get_length(output),overwrite);
206
207     /*
208      * Validate output
209      */
210
211     // Check with OPS
212
213     {
214     int fd=0;
215     ops_parse_info_t *pinfo=NULL;
216     validate_data_cb_arg_t validate_arg;
217     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));
218
219     int rtn=0;
220    
221     if (debug)
222         {
223         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
224         }
225    
226     // open signed file
227 #ifdef WIN32
228     fd=open(signed_file,O_RDONLY | O_BINARY);
229 #else
230     fd=open(signed_file,O_RDONLY);
231 #endif
232     if(fd < 0)
233         {
234         perror(signed_file);
235         exit(2);
236         }
237    
238     // Set verification reader and handling options
239     
240     pinfo=ops_parse_info_new();
241    
242     memset(&validate_arg,'\0',sizeof validate_arg);
243     validate_arg.result=result;
244     validate_arg.keyring=&pub_keyring;
245     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
246    
247     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
248     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
249     ops_reader_set_fd(pinfo,fd);
250     pinfo->rinfo.accumulate=ops_true;
251    
252     // Must de-armour because it's clearsigned
253     
254     ops_reader_push_dearmour(pinfo);
255    
256     // Do the verification
257     
258     rtn=ops_parse(pinfo);
259     ops_print_errors(ops_parse_info_get_errors(pinfo));
260     CU_ASSERT(rtn==1);
261    
262     // Tidy up
263     //    if (has_armour)
264         ops_reader_pop_dearmour(pinfo);
265    
266     ops_parse_info_delete(pinfo);
267    
268     close(fd);
269     ops_validate_result_free(result);
270     }
271
272     // Check signature with GPG
273     {
274
275     snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
276     rtn=system(cmd);
277     CU_ASSERT(rtn==0);
278     }
279     }
280
281 static void test_rsa_signature_sign(const int use_armour, const char *filename, const ops_secret_key_t *skey)
282     {
283     char cmd[MAXBUF+1];
284     char myfile[MAXBUF+1];
285     char signed_file[MAXBUF+1];
286     char *suffix= use_armour ? "asc" : "gpg";
287     int rtn=0;
288     ops_boolean_t overwrite=ops_true;
289
290     // filenames
291     snprintf(myfile,sizeof myfile,"%s/%s",dir,filename);
292     snprintf(signed_file,sizeof signed_file,"%s.%s",myfile,suffix);
293
294     ops_sign_file(myfile, signed_file, skey, use_armour, overwrite);
295     //ops_sign_file(myfile, NULL, skey, use_armour, overwrite);
296
297     /*
298      * Validate output
299      */
300
301     // Check with OPS
302
303     {
304     int fd=0;
305     ops_parse_info_t *pinfo=NULL;
306     validate_data_cb_arg_t validate_arg;
307     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));;
308     int rtn=0;
309    
310     if (debug)
311         {
312         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
313         }
314    
315     // open signed file
316 #ifdef WIN32
317     fd=open(signed_file,O_RDONLY | O_BINARY);
318 #else
319     fd=open(signed_file,O_RDONLY);
320 #endif
321     if(fd < 0)
322         {
323         perror(signed_file);
324         exit(2);
325         }
326    
327     // Set verification reader and handling options
328     
329     pinfo=ops_parse_info_new();
330    
331     memset(&validate_arg,'\0',sizeof validate_arg);
332     validate_arg.result=result;
333     validate_arg.keyring=&pub_keyring;
334     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
335    
336     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
337     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
338     ops_reader_set_fd(pinfo,fd);
339     pinfo->rinfo.accumulate=ops_true;
340    
341     // Set up armour/passphrase options
342     
343     if (use_armour)
344         ops_reader_push_dearmour(pinfo);
345    
346     // Do the verification
347     
348     rtn=ops_parse_and_print_errors(pinfo);
349     CU_ASSERT(rtn==1);
350    
351     // Tidy up
352     if (use_armour)
353         ops_reader_pop_dearmour(pinfo);
354    
355     ops_parse_info_delete(pinfo);
356    
357     close(fd);
358     ops_validate_result_free(result);
359     }
360
361     // Check signature with GPG
362     {
363
364     snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file);
365     rtn=system(cmd);
366     CU_ASSERT(rtn==0);
367     }
368     }
369
370 static void test_rsa_signature_sign_memory(const int use_armour, const void* input, const int input_len, const ops_secret_key_t *skey)
371     {
372     int rtn=0;
373     ops_memory_t* mem=NULL;
374     ops_parse_info_t *pinfo=NULL;
375     validate_data_cb_arg_t validate_arg;
376     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));
377    
378
379     // filenames
380
381     mem=ops_sign_buf(input, input_len, OPS_SIG_TEXT, skey, use_armour);
382
383     /*
384      * Validate output
385      */
386
387     if (debug)
388         {
389         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
390         }
391    
392     ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem), ops_memory_get_length(mem),ops_true);
393
394     // Set verification reader and handling options
395     
396     ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify, ops_true);
397    
398     memset(&validate_arg,'\0',sizeof validate_arg);
399     validate_arg.result=result;
400     validate_arg.keyring=&pub_keyring;
401     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
402    
403     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
404     pinfo->rinfo.accumulate=ops_true;
405    
406     // Set up armour/passphrase options
407     
408     if (use_armour)
409         ops_reader_push_dearmour(pinfo);
410    
411     // Do the verification
412     
413     rtn=ops_parse_and_print_errors(pinfo);
414     CU_ASSERT(rtn==1);
415    
416     // Tidy up
417     if (use_armour)
418         ops_reader_pop_dearmour(pinfo);
419    
420     ops_parse_info_delete(pinfo);
421     ops_memory_free(mem);
422     ops_validate_result_free(result);
423     }
424
425 static void test_rsa_signature_large_noarmour_nopassphrase(void)
426     {
427     int armour=0;
428     assert(pub_keyring.nkeys);
429     test_rsa_signature_sign(armour,filename_rsa_large_noarmour_nopassphrase, alpha_skey);
430     }
431
432 static void test_rsa_signature_large_armour_nopassphrase(void)
433     {
434     int armour=1;
435     assert(pub_keyring.nkeys);
436     test_rsa_signature_sign(armour,filename_rsa_large_armour_nopassphrase, alpha_skey);
437     }
438
439 static void test_rsa_signature_noarmour_nopassphrase(void)
440     {
441     unsigned char testdata[MAXBUF];
442     int armour=0;
443     assert(pub_keyring.nkeys);
444     test_rsa_signature_sign(armour,filename_rsa_noarmour_nopassphrase, alpha_skey);
445     create_testdata("test_rsa_signature_noarmour_nopassphrase",testdata, MAXBUF);
446     test_rsa_signature_sign_memory(armour,testdata,MAXBUF, alpha_skey);
447     }
448
449 static void test_rsa_signature_noarmour_passphrase(void)
450     {
451     unsigned char testdata[MAXBUF];
452     int armour=0;
453     assert(pub_keyring.nkeys);
454     test_rsa_signature_sign(armour,filename_rsa_noarmour_passphrase, bravo_skey);
455
456     create_testdata("test_rsa_signature_noarmour_passphrase",testdata, MAXBUF);
457     test_rsa_signature_sign_memory(armour,testdata,MAXBUF, bravo_skey);
458     }
459
460 static void test_rsa_signature_armour_nopassphrase(void)
461     {
462     unsigned char testdata[MAXBUF];
463     int armour=1;
464     assert(pub_keyring.nkeys);
465     test_rsa_signature_sign(armour,filename_rsa_armour_nopassphrase, alpha_skey);
466
467     create_testdata("test_rsa_signature_armour_nopassphrase",testdata, MAXBUF);
468     test_rsa_signature_sign_memory(armour,testdata,MAXBUF, alpha_skey);
469     }
470
471 static void test_rsa_signature_armour_passphrase(void)
472     {
473     unsigned char testdata[MAXBUF];
474
475     int armour=1;
476     assert(pub_keyring.nkeys);
477     test_rsa_signature_sign(armour,filename_rsa_armour_passphrase, bravo_skey);
478
479     create_testdata("test_rsa_signature_armour_passphrase",testdata, MAXBUF);
480     test_rsa_signature_sign_memory(armour,testdata,MAXBUF, bravo_skey);
481     }
482
483 static void test_rsa_signature_clearsign_file_nopassphrase(void)
484     {
485     assert(pub_keyring.nkeys);
486     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase, alpha_skey);
487     }
488
489 static void test_rsa_signature_clearsign_file_passphrase(void)
490     {
491     assert(pub_keyring.nkeys);
492     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase, bravo_skey);
493     }
494
495 static void test_rsa_signature_clearsign_buf_nopassphrase(void)
496     {
497     assert(pub_keyring.nkeys);
498     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase, alpha_skey);
499     }
500
501 static void test_rsa_signature_clearsign_buf_passphrase(void)
502     {
503     assert(pub_keyring.nkeys);
504     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase, bravo_skey);
505     }
506
507 /*
508 static void test_todo(void)
509     {
510     CU_FAIL("Test FUTURE: Use other hash algorithms");
511     CU_FAIL("Test FUTURE: Check for key expiry");
512     CU_FAIL("Test FUTURE: Check for key revocation");
513     CU_FAIL("Test FUTURE: Check for signature expiry");
514     CU_FAIL("Test FUTURE: Check for signature revocation");
515     }
516 */
517
518 static int add_tests(CU_pSuite suite)
519     {
520     // add tests to suite
521     
522     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
523             return 0;
524    
525     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
526             return 0;
527    
528     if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase", test_rsa_signature_clearsign_file_nopassphrase))
529             return 0;
530    
531     if (NULL == CU_add_test(suite, "Clearsigned file, passphrase", test_rsa_signature_clearsign_file_passphrase))
532             return 0;
533
534     if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase", test_rsa_signature_clearsign_buf_nopassphrase))
535             return 0;
536    
537     if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase", test_rsa_signature_clearsign_buf_passphrase))
538             return 0;
539
540     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
541             return 0;
542    
543     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
544             return 0;
545    
546     if (NULL == CU_add_test(suite, "Large, no armour, no passphrase", test_rsa_signature_large_noarmour_nopassphrase))
547             return 0;
548    
549     if (NULL == CU_add_test(suite, "Large, armour, no passphrase", test_rsa_signature_large_armour_nopassphrase))
550             return 0;
551    
552     /*
553     if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo))
554             return 0;
555     */
556     return 1;
557 }
558
559 CU_pSuite suite_rsa_signature()
560 {
561     CU_pSuite suite = NULL;
562
563     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
564     if (!suite)
565             return NULL;
566
567     if (!add_tests(suite))
568         return NULL;
569
570     return suite;
571     }
572
573
574 // EOF
575
Note: See TracBrowser for help on using the browser.