root/openpgpsdk/trunk/tests/test_rsa_verify.c

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

Added OPS_E_UNIMPLEMENTED error
Changed cbinfo struct to support stacked errors in callbacks
Changed RSA encryption test *NOT* to enforce writing of multiple packets
Changed RSA signature test to use OPS validation

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