root/openpgpsdk/trunk/tests/test_rsa_verify.c

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

Signature/verification now interoperates with GPG

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 static int do_gpgtest=0;
19
20 #ifndef ATTRIBUTE_UNUSED
21
22 #ifndef WIN32
23 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
24 #else
25 #define ATTRIBUTE_UNUSED
26 #endif // #ifndef WIN32
27
28 #endif /* ATTRIBUTE_UNUSED */
29
30 static char *filename_rsa_noarmour_nopassphrase="gpg_signed_noarmour_nopassphrase.txt";
31 static char *filename_rsa_armour_nopassphrase="gpg_signed_armour_nopassphrase.txt";
32 static char *filename_rsa_noarmour_passphrase="gpg_signed_armour_nopassphrase.txt";
33 static char *filename_rsa_armour_passphrase="gpg_signed_armour_passphrase.txt";
34
35 static char *filename_rsa_clearsign_armour_nopassphrase="gpg_clearsigned_armour_nopassphrase.txt";
36
37 /* Signature verification suite initialization.
38  * Create temporary test files.
39  */
40
41 int init_suite_rsa_verify(void)
42     {
43     char cmd[MAXBUF+1];
44
45     do_gpgtest=0;
46
47     // Create SIGNED test files
48
49     create_testfile(filename_rsa_noarmour_nopassphrase);
50     create_testfile(filename_rsa_armour_nopassphrase);
51     create_testfile(filename_rsa_noarmour_passphrase);
52     create_testfile(filename_rsa_armour_passphrase);
53
54     // Now sign the test files with GPG
55
56     snprintf(cmd,MAXBUF,"%s --openpgp --compress-level 0 --sign --local-user %s %s/%s",
57              gpgcmd, alpha_name, dir, filename_rsa_noarmour_nopassphrase);
58     if (system(cmd))
59         { return 1; }
60
61     snprintf(cmd,MAXBUF,"%s --compress-level 0 --sign --local-user %s --armor %s/%s",
62              gpgcmd, alpha_name, dir, filename_rsa_armour_nopassphrase);
63     if (system(cmd))
64         { return 1; }
65
66     snprintf(cmd,MAXBUF,"%s --compress-level 0 --sign --local-user %s --passphrase %s %s/%s",
67              gpgcmd, bravo_name, bravo_passphrase, dir, filename_rsa_noarmour_passphrase);
68     if (system(cmd))
69         { return 1; }
70
71     snprintf(cmd,MAXBUF,"%s --compress-level 0 --sign --local-user %s --passphrase %s --armor %s/%s",
72              gpgcmd, bravo_name, bravo_passphrase, dir, filename_rsa_armour_passphrase);
73     if (system(cmd))
74         { return 1; }
75
76     /*
77      * Create CLEARSIGNED test files
78      */
79
80     create_testfile(filename_rsa_clearsign_armour_nopassphrase);
81
82     // and sign them
83
84     snprintf(cmd,MAXBUF,"%s --openpgp --compress-level 0 --clearsign --local-user %s --armor %s/%s",
85              gpgcmd, alpha_name, dir, filename_rsa_clearsign_armour_nopassphrase);
86     if (system(cmd))
87         { return 1; }
88
89     // Return success
90     return 0;
91     }
92
93 int init_suite_rsa_verify_gpgtest(void)
94     {
95     init_suite_rsa_verify();
96
97     do_gpgtest=1;
98
99     return 0;
100     }
101
102 int clean_suite_rsa_verify(void)
103     {
104     ops_finish();
105
106     reset_vars();
107
108     return 0;
109     }
110
111 static void test_rsa_verify(const int has_armour, const int has_passphrase ATTRIBUTE_UNUSED, const char *filename, const char* protocol)
112     {
113     char signedfile[MAXBUF+1];
114     char *suffix= has_armour ? "asc" : "gpg";
115     int fd=0;
116     ops_parse_info_t *pinfo=NULL;
117     validate_data_cb_arg_t validate_arg;
118     ops_validate_result_t result;
119     int rtn=0;
120    
121     // open signed file
122     snprintf(signedfile,MAXBUF,"%s/%s%s%s.%s",
123              dir, filename,
124              protocol==NULL ? "" : "_",
125              protocol==NULL ? "" : protocol,
126              suffix);
127 #ifdef WIN32
128     fd=open(signedfile,O_RDONLY | O_BINARY);
129 #else
130     fd=open(signedfile,O_RDONLY);
131 #endif
132     if(fd < 0)
133         {
134         perror(signedfile);
135         exit(2);
136         }
137    
138     // Set verification reader and handling options
139
140     pinfo=ops_parse_info_new();
141
142     memset(&validate_arg,'\0',sizeof validate_arg);
143     validate_arg.result=&result;
144     validate_arg.keyring=&pub_keyring;
145     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo);
146
147     ops_parse_cb_set(pinfo,callback_verify,&validate_arg);
148     ops_reader_set_fd(pinfo,fd);
149     pinfo->rinfo.accumulate=ops_true;
150
151     // Set up armour/passphrase options
152
153     if (has_armour)
154         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
155     //    current_passphrase=has_passphrase ? passphrase : nopassphrase;
156     
157     // Do the verification
158
159     rtn=ops_parse(pinfo);
160     ops_print_errors(ops_parse_info_get_errors(pinfo));
161     CU_ASSERT(rtn==1);
162
163     // Tidy up
164     if (has_armour)
165         ops_reader_pop_dearmour(pinfo);
166
167     ops_parse_info_delete(pinfo);
168
169     close(fd);
170    
171 #ifdef NEEDED
172     // File contents should match
173     create_testtext(filename,&testtext[0],MAXBUF);
174     CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0);
175 #endif
176     }
177
178 void test_rsa_verify_noarmour_nopassphrase(void)
179     {
180     //    int clearsign=0;
181     int armour=0;
182     int passphrase=0;
183     assert(pub_keyring.nkeys);
184     //    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id);
185     //    assert(pub_key);
186     test_rsa_verify(armour,passphrase,filename_rsa_noarmour_nopassphrase,NULL);
187     }
188
189 void test_rsa_verify_clearsign_armour_nopassphrase(void)
190     {
191     //    int clearsign=1;
192     int armour=1;
193     int passphrase=0;
194     assert(pub_keyring.nkeys);
195
196     test_rsa_verify(armour,passphrase,filename_rsa_clearsign_armour_nopassphrase,NULL);
197     }
198
199 #ifdef TBD
200 void test_rsa_encrypt_armour_singlekey(void)
201     {
202     int armour=1;
203     char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>";
204     const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id);
205     assert(pub_key);
206     test_rsa_encrypt(armour,pub_key,filename_rsa_armour_singlekey);
207     }
208
209 void test_rsa_encrypt_noarmour_passphrase(void)
210     {
211     int armour=0;
212     int passphrase=1;
213     test_rsa_encrypt(armour,passphrase,filename_rsa_noarmour_passphrase);
214     }
215
216 void test_rsa_encrypt_armour_passphrase(void)
217     {
218     int armour=1;
219     int passphrase=1;
220     test_rsa_encrypt(armour,passphrase,filename_rsa_armour_passphrase);
221     }
222 #endif /*TBD*/
223
224 CU_pSuite suite_rsa_verify()
225 {
226     CU_pSuite suite = NULL;
227
228     suite = CU_add_suite("RSA Verification Suite", init_suite_rsa_verify, clean_suite_rsa_verify);
229     if (!suite)
230             return NULL;
231
232     // add tests to suite
233     
234     if (NULL == CU_add_test(suite, "Clearsigned, armoured, no passphrase", test_rsa_verify_clearsign_armour_nopassphrase))
235             return NULL;
236    
237     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_verify_noarmour_nopassphrase))
238             return NULL;
239    
240     /*
241     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_verify_noarmour_passphrase))
242             return NULL;
243     */
244     return suite;
245 }
246
247 CU_pSuite suite_rsa_verify_GPGtest()
248 {
249     CU_pSuite suite = NULL;
250
251     suite = CU_add_suite("RSA Verification Suite (GPG interop)", init_suite_rsa_verify_gpgtest, clean_suite_rsa_verify);
252     if (!suite)
253             return NULL;
254
255     // add tests to suite
256     
257     if (NULL == CU_add_test(suite, "Clearsigned, armoured, no passphrase", test_rsa_verify_clearsign_armour_nopassphrase))
258             return NULL;
259    
260     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_verify_noarmour_nopassphrase))
261             return NULL;
262
263     /*
264     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_verify_noarmour_passphrase))
265             return NULL;
266     */
267     return suite;
268 }
269
270 // EOF
271
Note: See TracBrowser for help on using the browser.