root/openpgpsdk/trunk/tests/test_rsa_signature.c

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

Armoured signatures now working

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/advanced/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_nopassphrase="ops_rsa_signed_clearsign_nopassphrase.txt";
26 static char *filename_rsa_clearsign_passphrase="ops_rsa_signed_clearsign_passphrase.txt";
27
28 /* Signature suite initialization.
29  * Create temporary directory.
30  * Create temporary test files.
31  */
32
33 int init_suite_rsa_signature(void)
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     create_testfile(filename_rsa_clearsign_nopassphrase);
42     create_testfile(filename_rsa_clearsign_passphrase);
43
44     // Return success
45     return 0;
46     }
47
48 int clean_suite_rsa_signature(void)
49     {
50     ops_finish();
51
52     reset_vars();
53
54     return 0;
55     }
56
57 static void test_rsa_signature_clearsign(const char *filename, const ops_secret_key_t *skey)
58     {
59     char cmd[MAXBUF+1];
60     char myfile[MAXBUF+1];
61     char signed_file[MAXBUF+1];
62     int rtn=0;
63
64     // setup filenames
65     snprintf(myfile,MAXBUF,"%s/%s",dir,filename);
66     snprintf(signed_file,MAXBUF,"%s.asc",myfile);
67
68     // sign file
69     ops_sign_file_as_cleartext(myfile,skey);
70
71     /*
72      * Validate output
73      */
74
75     // Check with OPS
76
77     {
78     int fd=0;
79     ops_parse_info_t *pinfo=NULL;
80     validate_data_cb_arg_t validate_arg;
81     ops_validate_result_t result;
82     int rtn=0;
83    
84     if (debug)
85         {
86         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
87         }
88    
89     // open signed file
90 #ifdef WIN32
91     fd=open(signed_file,O_RDONLY | O_BINARY);
92 #else
93     fd=open(signed_file,O_RDONLY);
94 #endif
95     if(fd < 0)
96         {
97         perror(signed_file);
98         exit(2);
99         }
100    
101     // Set verification reader and handling options
102     
103     pinfo=ops_parse_info_new();
104    
105     memset(&validate_arg,'\0',sizeof validate_arg);
106     validate_arg.result=&result;
107     validate_arg.keyring=&pub_keyring;
108     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
109    
110     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
111     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
112     ops_reader_set_fd(pinfo,fd);
113     pinfo->rinfo.accumulate=ops_true;
114    
115     // Must de-armour because it's clearsigned
116     
117     ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
118    
119     // Do the verification
120     
121     rtn=ops_parse(pinfo);
122     ops_print_errors(ops_parse_info_get_errors(pinfo));
123     CU_ASSERT(rtn==1);
124    
125     // Tidy up
126     //    if (has_armour)
127         ops_reader_pop_dearmour(pinfo);
128    
129     ops_parse_info_delete(pinfo);
130    
131     close(fd);
132     }
133
134     // Check signature with GPG
135     {
136
137     snprintf(cmd,MAXBUF,"%s --verify %s", gpgcmd, signed_file);
138     rtn=system(cmd);
139     CU_ASSERT(rtn==0);
140     }
141     }
142
143 static void test_rsa_signature_sign(const int use_armour, const char *filename, const ops_secret_key_t *skey)
144     {
145     char cmd[MAXBUF+1];
146     char myfile[MAXBUF+1];
147     char signed_file[MAXBUF+1];
148     char *suffix= use_armour ? "asc" : "ops";
149     int rtn=0;
150
151     // filenames
152     snprintf(myfile,MAXBUF,"%s/%s",dir,filename);
153     snprintf(signed_file,MAXBUF,"%s.%s",myfile,suffix);
154
155     ops_sign_file(myfile, signed_file, skey, use_armour);
156
157     /*
158      * Validate output
159      */
160
161     // Check with OPS
162
163     {
164     int fd=0;
165     ops_parse_info_t *pinfo=NULL;
166     validate_data_cb_arg_t validate_arg;
167     ops_validate_result_t result;
168     int rtn=0;
169    
170     if (debug)
171         {
172         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
173         }
174    
175     // open signed file
176 #ifdef WIN32
177     fd=open(signed_file,O_RDONLY | O_BINARY);
178 #else
179     fd=open(signed_file,O_RDONLY);
180 #endif
181     if(fd < 0)
182         {
183         perror(signed_file);
184         exit(2);
185         }
186    
187     // Set verification reader and handling options
188     
189     pinfo=ops_parse_info_new();
190    
191     memset(&validate_arg,'\0',sizeof validate_arg);
192     validate_arg.result=&result;
193     validate_arg.keyring=&pub_keyring;
194     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
195    
196     ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED);
197     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
198     ops_reader_set_fd(pinfo,fd);
199     pinfo->rinfo.accumulate=ops_true;
200    
201     // Set up armour/passphrase options
202     
203     if (use_armour)
204         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
205    
206     // Do the verification
207     
208     rtn=ops_parse(pinfo);
209     ops_print_errors(ops_parse_info_get_errors(pinfo));
210     CU_ASSERT(rtn==1);
211    
212     // Tidy up
213     if (use_armour)
214         ops_reader_pop_dearmour(pinfo);
215    
216     ops_parse_info_delete(pinfo);
217    
218     close(fd);
219     }
220
221     // Check signature with GPG
222     {
223
224     snprintf(cmd,MAXBUF,"%s --verify %s", gpgcmd, signed_file);
225     rtn=system(cmd);
226     CU_ASSERT(rtn==0);
227     }
228     }
229
230 static void test_rsa_signature_noarmour_nopassphrase(void)
231     {
232     int armour=0;
233     assert(pub_keyring.nkeys);
234     test_rsa_signature_sign(armour,filename_rsa_noarmour_nopassphrase, alpha_skey);
235     }
236
237 static void test_rsa_signature_noarmour_passphrase(void)
238     {
239     int armour=0;
240     assert(pub_keyring.nkeys);
241     test_rsa_signature_sign(armour,filename_rsa_noarmour_passphrase, bravo_skey);
242     }
243
244 static void test_rsa_signature_armour_nopassphrase(void)
245     {
246     int armour=1;
247     assert(pub_keyring.nkeys);
248     test_rsa_signature_sign(armour,filename_rsa_armour_nopassphrase, alpha_skey);
249     }
250
251 static void test_rsa_signature_armour_passphrase(void)
252     {
253     int armour=1;
254     assert(pub_keyring.nkeys);
255     test_rsa_signature_sign(armour,filename_rsa_armour_passphrase, bravo_skey);
256     }
257
258 static void test_rsa_signature_clearsign_nopassphrase(void)
259     {
260     assert(pub_keyring.nkeys);
261     test_rsa_signature_clearsign(filename_rsa_clearsign_nopassphrase, alpha_skey);
262     }
263
264 static void test_rsa_signature_clearsign_passphrase(void)
265     {
266     assert(pub_keyring.nkeys);
267     test_rsa_signature_clearsign(filename_rsa_clearsign_passphrase, bravo_skey);
268     }
269
270 CU_pSuite suite_rsa_signature()
271 {
272     CU_pSuite suite = NULL;
273
274     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
275     if (!suite)
276             return NULL;
277
278     // add tests to suite
279     
280     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
281             return NULL;
282    
283     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
284             return NULL;
285    
286     if (NULL == CU_add_test(suite, "Clearsigned, no passphrase", test_rsa_signature_clearsign_nopassphrase))
287             return NULL;
288    
289     if (NULL == CU_add_test(suite, "Clearsigned, passphrase", test_rsa_signature_clearsign_passphrase))
290             return NULL;
291
292     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
293             return NULL;
294    
295     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
296             return NULL;
297    
298     return suite;
299 }
300
301 // EOF
302
Note: See TracBrowser for help on using the browser.