root/openpgpsdk/trunk/tests/test_rsa_signature.c

Revision 518 (checked in by rachel, 6 years ago)

Added some placeholder tests for ones to be added

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 #include "tests.h"
15
16 static int do_gpgtest=0;
17
18 static char *filename_rsa_noarmour_nopassphrase="ops_rsa_signed_noarmour_nopassphrase.txt";
19 static char *filename_rsa_noarmour_passphrase="ops_rsa_signed_noarmour_passphrase.txt";
20 static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt";
21 static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt";
22
23 static ops_parse_cb_return_t
24 callback_verify(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo);
25
26 /* Signature suite initialization.
27  * Create temporary directory.
28  * Create temporary test files.
29  */
30
31 int init_suite_rsa_signature(void)
32     {
33     do_gpgtest=0;
34
35     // Create test files
36
37     create_testfile(filename_rsa_noarmour_nopassphrase);
38     create_testfile(filename_rsa_noarmour_passphrase);
39     create_testfile(filename_rsa_armour_nopassphrase);
40     create_testfile(filename_rsa_armour_passphrase);
41
42     // Return success
43     return 0;
44     }
45
46 int init_suite_rsa_signature_gpgtest(void)
47     {
48     init_suite_rsa_signature();
49
50     do_gpgtest=1;
51
52     return 0;
53     }
54
55 int clean_suite_rsa_signature(void)
56     {
57     ops_finish();
58
59     reset_vars();
60
61     return 0;
62     }
63
64 static void test_rsa_signature(const int has_armour, const char *filename, const ops_secret_key_t *skey, ops_hash_algorithm_t hash_alg)
65     {
66     unsigned char keyid[OPS_KEY_ID_SIZE];
67     ops_create_signature_t *sig=NULL;
68
69     char cmd[MAXBUF+1];
70     char myfile[MAXBUF+1];
71     char signed_file[MAXBUF+1];
72     char *suffix= has_armour ? "asc" : "gpg";
73     int fd_in=0;
74     int fd_out=0;
75     int rtn=0;
76     ops_create_info_t *cinfo=NULL;
77    
78     // open file to sign
79     snprintf(myfile,MAXBUF,"%s/%s",dir,filename);
80 #ifdef WIN32
81     fd_in=open(myfile,O_RDONLY | O_BINARY);
82 #else
83     fd_in=open(myfile,O_RDONLY);
84 #endif
85     if(fd_in < 0)
86         {
87         perror(myfile);
88         exit(2);
89         }
90    
91     snprintf(signed_file,MAXBUF,"%s/%s_%s.%s",dir,filename,ops_show_hash_algorithm(hash_alg),suffix);
92 #ifdef WIN32
93     fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
94 #else
95     fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL, 0600);
96 #endif
97     if(fd_out < 0)
98         {
99         perror(signed_file);
100         exit(2);
101         }
102    
103     // Set up armour/passphrase options
104     // OPS code armours signatures by default
105
106     assert(has_armour);
107    
108     // set up signature
109     sig=ops_create_signature_new();
110     ops_signature_start_plaintext_signature(sig,(ops_secret_key_t *)skey,hash_alg,OPS_SIG_BINARY);
111
112     // set up output file
113     cinfo=ops_create_info_new();
114     ops_writer_set_fd(cinfo,fd_out);
115     ops_writer_push_dash_escaped(cinfo,sig);
116
117     // Do the signing
118
119     for (;;)
120         {
121         unsigned char buf[MAXBUF];
122         int n=0;
123    
124         n=read(fd_in,buf,sizeof(buf));
125         if (!n)
126             break;
127         assert(n>=0);
128         ops_write(buf,n,cinfo);
129         }
130     close(fd_in);
131
132     // add signature
133
134     ops_writer_switch_to_signature(cinfo);
135     ops_signature_add_creation_time(sig,time(NULL));
136     ops_keyid(keyid,&skey->public_key);
137     ops_signature_add_issuer_key_id(sig,keyid);
138     ops_signature_hashed_subpackets_end(sig);
139     ops_write_signature(sig,(ops_public_key_t *)&skey->public_key,(ops_secret_key_t *)skey,cinfo);
140     ops_writer_close(cinfo);
141     close(fd_out);
142
143     // Check
144
145     if (!do_gpgtest)
146         {
147         int fd=0;
148         ops_parse_info_t *pinfo=NULL;
149         validate_cb_arg_t validate_arg;
150         ops_validate_result_t result;
151         int rtn=0;
152
153     // open signed file
154 #ifdef WIN32
155         fd=open(signed_file,O_RDONLY | O_BINARY);
156 #else
157         fd=open(signed_file,O_RDONLY);
158 #endif
159         if(fd < 0)
160             {
161             perror(signed_file);
162             exit(2);
163             }
164        
165         // Set verification reader and handling options
166         
167         pinfo=ops_parse_info_new();
168         ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
169         ops_reader_set_fd(pinfo,fd);
170        
171         memset(&validate_arg,'\0',sizeof validate_arg);
172         validate_arg.result=&result;
173         validate_arg.keyring=&pub_keyring;
174         validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
175        
176         // Set up armour/passphrase options
177         
178         if (has_armour)
179             ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
180         //    current_passphrase=has_passphrase ? passphrase : nopassphrase;
181         
182         // Do the verification
183         
184         rtn=ops_parse(pinfo);
185         ops_print_errors(ops_parse_info_get_errors(pinfo));
186         CU_ASSERT(rtn==1);
187        
188         // Tidy up
189         if (has_armour)
190             ops_reader_pop_dearmour(pinfo);
191        
192         ops_public_key_free(&validate_arg.pkey);
193         if (validate_arg.subkey.version)
194             ops_public_key_free(&validate_arg.subkey);
195         ops_user_id_free(&validate_arg.user_id);
196         ops_user_attribute_free(&validate_arg.user_attribute);
197         ops_parse_info_delete(pinfo);
198        
199         close(fd);
200         }
201     else
202         {
203         // Check signature with GPG
204
205         snprintf(cmd,MAXBUF,"gpg --verify --quiet --homedir %s %s", dir, signed_file);
206         rtn=system(cmd);
207         CU_ASSERT(rtn==0);
208         }
209     }
210
211 void test_rsa_signature_noarmour_nopassphrase(void)
212     {
213     int armour=0;
214     assert(pub_keyring.nkeys);
215     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA1);
216 #ifdef TODO
217     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_MD5);
218     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_RIPEMD);
219     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA256);
220     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA384);
221     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA512);
222 #endif
223     }
224
225 void test_rsa_signature_noarmour_passphrase(void)
226     {
227     int armour=0;
228     assert(pub_keyring.nkeys);
229     test_rsa_signature(armour,filename_rsa_noarmour_passphrase, bravo_skey, OPS_HASH_SHA1);
230     }
231
232 void test_rsa_signature_armour_nopassphrase(void)
233     {
234     int armour=1;
235     assert(pub_keyring.nkeys);
236     test_rsa_signature(armour,filename_rsa_armour_nopassphrase, alpha_skey, OPS_HASH_SHA1);
237     }
238
239 void test_rsa_signature_armour_passphrase(void)
240     {
241     int armour=1;
242     assert(pub_keyring.nkeys);
243     test_rsa_signature(armour,filename_rsa_armour_passphrase, bravo_skey, OPS_HASH_SHA1);
244     }
245
246 CU_pSuite suite_rsa_signature()
247 {
248     CU_pSuite suite = NULL;
249
250     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
251     if (!suite)
252             return NULL;
253
254     // add tests to suite
255     
256 #ifdef TBD
257     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
258             return NULL;
259    
260     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
261             return NULL;
262 #endif /*TBD*/
263    
264     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
265             return NULL;
266    
267     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
268             return NULL;
269    
270    
271     return suite;
272 }
273
274 CU_pSuite suite_rsa_signature_GPGtest()
275 {
276     CU_pSuite suite = NULL;
277
278     suite = CU_add_suite("RSA Signature Suite (GPG interop)", init_suite_rsa_signature_gpgtest, clean_suite_rsa_signature);
279
280     if (!suite)
281             return NULL;
282
283     // add tests to suite
284     
285 #ifdef TBD
286     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
287             return NULL;
288    
289     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
290             return NULL;
291 #endif /*TBD*/
292    
293     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
294             return NULL;
295    
296     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
297             return NULL;
298    
299    
300     return suite;
301 }
302
303 static ops_parse_cb_return_t
304 callback_verify(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
305     {
306     //    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
307
308     //        ops_print_packet(content_);
309
310     switch(content_->tag)
311         {
312     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
313         break;
314
315     case OPS_PTAG_CT_LITERAL_DATA_BODY:
316         return callback_literal_data(content_,cbinfo);
317         break;
318
319     case OPS_PTAG_CT_ONE_PASS_SIGNATURE:
320  case OPS_PTAG_CT_SIGNATURE:
321  case OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER:
322  case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY:
323  case OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER:
324  case OPS_PTAG_CT_ARMOUR_HEADER:
325  case OPS_PTAG_CT_ARMOUR_TRAILER:
326         break;
327
328     case OPS_PTAG_CT_SIGNATURE_HEADER:
329     case OPS_PTAG_CT_SIGNATURE_FOOTER:
330         return callback_signature(content_, cbinfo);
331
332         /*
333     case OPS_PTAG_CT_UNARMOURED_TEXT:
334         printf("OPS_PTAG_CT_UNARMOURED_TEXT\n");
335         if(!skipping)
336             {
337             puts("Skipping...");
338             skipping=ops_true;
339             }
340         fwrite(content->unarmoured_text.data,1,
341                content->unarmoured_text.length,stdout);
342         break;
343
344     case OPS_PTAG_CT_PK_SESSION_KEY:
345         return callback_pk_session_key(content_,cbinfo);
346
347     case OPS_PARSER_CMD_GET_SECRET_KEY:
348         return callback_cmd_get_secret_key(content_,cbinfo);
349
350     case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
351         return callback_cmd_get_secret_key_passphrase(content_,cbinfo);
352
353     case OPS_PTAG_CT_LITERAL_DATA_BODY:
354         return callback_literal_data(content_,cbinfo);
355         //      text=ops_mallocz(content->literal_data_body.length+1);
356         //      memcpy(text,content->literal_data_body.data,content->literal_data_body.length);
357         //              break;
358
359     case OPS_PARSER_PTAG:
360     case OPS_PTAG_CT_ARMOUR_HEADER:
361     case OPS_PTAG_CT_ARMOUR_TRAILER:
362     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
363     case OPS_PTAG_CT_COMPRESSED:
364     case OPS_PTAG_CT_SE_IP_DATA_BODY:
365     case OPS_PTAG_CT_SE_IP_DATA_HEADER:
366         // Ignore these packets
367         // They're handled in ops_parse_one_packet()
368         // and nothing else needs to be done
369         break;
370 */
371
372     default:
373         return callback_general(content_,cbinfo);
374         }
375
376     return OPS_RELEASE_MEMORY;
377     }
378
379 // EOF
380
Note: See TracBrowser for help on using the browser.