root/openpgpsdk/trunk/tests/test_rsa_signature.c

Revision 683 (checked in by ben, 4 years ago)

More refactoring. Note that the tests now get lots of

parse error: Format error (ptag bit not set)

yet still pass. Not a healthy sign.

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 // FIXME: now that these tests print errors during parse, they are
23 // blatantly broken, but still pass.
24
25 #include "CUnit/Basic.h"
26
27 #include <openpgpsdk/defs.h>
28 #include <openpgpsdk/types.h>
29 #include "openpgpsdk/keyring.h"
30 #include <openpgpsdk/armour.h>
31 #include <openpgpsdk/create.h>
32 #include "openpgpsdk/packet.h"
33 #include "openpgpsdk/packet-parse.h"
34 #include "openpgpsdk/packet-show.h"
35 #include "openpgpsdk/util.h"
36 #include "openpgpsdk/std_print.h"
37 #include "openpgpsdk/readerwriter.h"
38 #include "openpgpsdk/validate.h"
39
40 // \todo change this once we know it works
41 #include "../src/lib/parse_local.h"
42
43 #include "tests.h"
44
45 static int debug=0;
46
47 static const char filename_rsa_large_noarmour_nopassphrase[]
48   ="ops_rsa_signed_large_noarmour_nopassphrase.txt";
49 static const char filename_rsa_large_armour_nopassphrase[]
50   ="ops_rsa_signed_large_armour_nopassphrase.txt";
51 static const char filename_rsa_noarmour_nopassphrase[]
52   ="ops_rsa_signed_noarmour_nopassphrase.txt";
53 static const char filename_rsa_noarmour_passphrase[]
54   ="ops_rsa_signed_noarmour_passphrase.txt";
55 static const char filename_rsa_armour_nopassphrase[]
56   ="ops_rsa_signed_armour_nopassphrase.txt";
57 static const char filename_rsa_armour_passphrase[]
58   ="ops_rsa_signed_armour_passphrase.txt";
59 static const char filename_rsa_clearsign_file_nopassphrase[]
60   ="ops_rsa_signed_clearsign_file_nopassphrase.txt";
61 static const char filename_rsa_clearsign_file_passphrase[]
62   ="ops_rsa_signed_clearsign_file_passphrase.txt";
63 static const char filename_rsa_clearsign_buf_nopassphrase[]
64   ="ops_rsa_signed_clearsign_buf_nopassphrase.txt";
65 static const char filename_rsa_clearsign_buf_passphrase[]
66   ="ops_rsa_signed_clearsign_buf_passphrase.txt";
67
68 /* Signature suite initialization.
69  * Create temporary directory.
70  * Create temporary test files.
71  */
72
73 int init_suite_rsa_signature(void)
74     {
75     // Create test files
76
77     create_small_testfile(filename_rsa_noarmour_nopassphrase);
78     create_small_testfile(filename_rsa_noarmour_passphrase);
79     create_small_testfile(filename_rsa_armour_nopassphrase);
80     create_small_testfile(filename_rsa_armour_passphrase);
81     create_small_testfile(filename_rsa_clearsign_file_nopassphrase);
82     create_small_testfile(filename_rsa_clearsign_file_passphrase);
83     create_small_testfile(filename_rsa_clearsign_buf_nopassphrase);
84     create_small_testfile(filename_rsa_clearsign_buf_passphrase);
85
86     create_large_testfile(filename_rsa_large_noarmour_nopassphrase);
87     create_large_testfile(filename_rsa_large_armour_nopassphrase);
88
89     // Return success
90     return 0;
91     }
92
93 int clean_suite_rsa_signature(void)
94     {
95     ops_finish();
96
97     reset_vars();
98
99     return 0;
100     }
101
102 static void test_rsa_signature_clearsign_file(const char *filename,
103                                               const ops_secret_key_t *skey)
104     {
105     char myfile[MAXBUF];
106     char signed_file[MAXBUF];
107     ops_boolean_t overwrite;
108
109     set_up_file_names(myfile, signed_file, filename, "asc");
110
111     // sign file
112     overwrite=ops_true;
113     ops_sign_file_as_cleartext(myfile, NULL, skey, overwrite);
114
115     check_sig(signed_file);
116     }
117
118 static void test_rsa_signature_clearsign_buf(const char *filename,
119                                              const ops_secret_key_t *skey)
120     {
121     char myfile[MAXBUF];
122     char signed_file[MAXBUF];
123     ops_memory_t *input=NULL;
124     ops_memory_t *output=NULL;
125     ops_boolean_t overwrite;
126     int errnum=0;
127
128     // (we are testing the function which signs a buf, but still want
129     // to read/write the buffers from/to files for external viewing
130     set_up_file_names(myfile, signed_file, filename, "asc");
131
132     // read file contents
133     input=ops_write_mem_from_file(myfile, &errnum);
134     CU_ASSERT(errnum==0);
135
136     // sign file
137     ops_sign_buf_as_cleartext(ops_memory_get_data(input),
138                               ops_memory_get_length(input), &output,skey);
139
140     // write to file
141     overwrite=ops_true;
142     ops_write_file_from_buf(signed_file, ops_memory_get_data(output),
143                             ops_memory_get_length(output), overwrite);
144
145     check_sig(signed_file);
146     }
147
148 static void test_rsa_signature_sign(const int use_armour, const char *filename,
149                                     const ops_secret_key_t *skey)
150     {
151     char myfile[MAXBUF];
152     char signed_file[MAXBUF];
153     char *suffix= use_armour ? "asc" : "gpg";
154     ops_boolean_t overwrite=ops_true;
155
156     set_up_file_names(myfile, signed_file, filename, suffix);
157
158     ops_sign_file(myfile, signed_file, skey, use_armour, overwrite);
159
160     check_sig(signed_file);
161     }
162
163 static void test_rsa_signature_sign_memory(const int use_armour,
164                                            const void* input,
165                                            const int input_len,
166                                            const ops_secret_key_t *skey)
167     {
168     ops_memory_t* mem=NULL;
169     ops_parse_info_t *pinfo=NULL;
170     validate_data_cb_arg_t validate_arg;
171
172     mem=ops_sign_buf(input, input_len, OPS_SIG_TEXT, skey, use_armour);
173
174     /*
175      * Validate output
176      */
177
178     if (debug)
179         {
180         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
181         }
182    
183     ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem),
184                             ops_memory_get_length(mem), ops_true);
185
186     ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify,
187                           ops_true);
188
189     check_sig_with_ops_core(pinfo, use_armour, &validate_arg);
190
191     ops_memory_free(mem);
192     }
193
194 static void test_rsa_signature_large_noarmour_nopassphrase(void)
195     {
196     assert(pub_keyring.nkeys);
197     test_rsa_signature_sign(OPS_UNARMOURED,
198                             filename_rsa_large_noarmour_nopassphrase,
199                             alpha_skey);
200     }
201
202 static void test_rsa_signature_large_armour_nopassphrase(void)
203     {
204     assert(pub_keyring.nkeys);
205     test_rsa_signature_sign(OPS_ARMOURED,
206                             filename_rsa_large_armour_nopassphrase, alpha_skey);
207     }
208
209 static void test_rsa_signature_noarmour_nopassphrase(void)
210     {
211     unsigned char testdata[MAXBUF];
212     assert(pub_keyring.nkeys);
213     test_rsa_signature_sign(OPS_UNARMOURED, filename_rsa_noarmour_nopassphrase,
214                             alpha_skey);
215     create_testdata("test_rsa_signature_noarmour_nopassphrase", testdata,
216                     MAXBUF);
217     test_rsa_signature_sign_memory(OPS_UNARMOURED, testdata, MAXBUF,
218                                    alpha_skey);
219     }
220
221 static void test_rsa_signature_noarmour_passphrase(void)
222     {
223     unsigned char testdata[MAXBUF];
224     assert(pub_keyring.nkeys);
225     test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_noarmour_passphrase,
226                             bravo_skey);
227
228     create_testdata("test_rsa_signature_noarmour_passphrase", testdata, MAXBUF);
229     test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, bravo_skey);
230     }
231
232 static void test_rsa_signature_armour_nopassphrase(void)
233     {
234     unsigned char testdata[MAXBUF];
235     assert(pub_keyring.nkeys);
236     test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_nopassphrase,
237                             alpha_skey);
238
239     create_testdata("test_rsa_signature_armour_nopassphrase", testdata, MAXBUF);
240     test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, alpha_skey);
241     }
242
243 static void test_rsa_signature_armour_passphrase(void)
244     {
245     unsigned char testdata[MAXBUF];
246
247     assert(pub_keyring.nkeys);
248     test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_passphrase,
249                             bravo_skey);
250
251     create_testdata("test_rsa_signature_armour_passphrase", testdata, MAXBUF);
252     test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, bravo_skey);
253     }
254
255 static void test_rsa_signature_clearsign_file_nopassphrase(void)
256     {
257     assert(pub_keyring.nkeys);
258     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase,
259                                       alpha_skey);
260     }
261
262 static void test_rsa_signature_clearsign_file_passphrase(void)
263     {
264     assert(pub_keyring.nkeys);
265     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase,
266                                       bravo_skey);
267     }
268
269 static void test_rsa_signature_clearsign_buf_nopassphrase(void)
270     {
271     assert(pub_keyring.nkeys);
272     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase,
273                                      alpha_skey);
274     }
275
276 static void test_rsa_signature_clearsign_buf_passphrase(void)
277     {
278     assert(pub_keyring.nkeys);
279     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase,
280                                      bravo_skey);
281     }
282
283 /*
284 static void test_todo(void)
285     {
286     CU_FAIL("Test FUTURE: Use other hash algorithms");
287     CU_FAIL("Test FUTURE: Check for key expiry");
288     CU_FAIL("Test FUTURE: Check for key revocation");
289     CU_FAIL("Test FUTURE: Check for signature expiry");
290     CU_FAIL("Test FUTURE: Check for signature revocation");
291     }
292 */
293
294 static int add_tests(CU_pSuite suite)
295     {
296     // add tests to suite
297     
298     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase",
299                             test_rsa_signature_noarmour_nopassphrase))
300             return 0;
301    
302     if (NULL == CU_add_test(suite, "Unarmoured, passphrase",
303                             test_rsa_signature_noarmour_passphrase))
304             return 0;
305    
306     if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase",
307                             test_rsa_signature_clearsign_file_nopassphrase))
308             return 0;
309    
310     if (NULL == CU_add_test(suite, "Clearsigned file, passphrase",
311                             test_rsa_signature_clearsign_file_passphrase))
312             return 0;
313
314     if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase",
315                             test_rsa_signature_clearsign_buf_nopassphrase))
316             return 0;
317    
318     if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase",
319                             test_rsa_signature_clearsign_buf_passphrase))
320             return 0;
321
322     if (NULL == CU_add_test(suite, "Armoured, no passphrase",
323                             test_rsa_signature_armour_nopassphrase))
324             return 0;
325    
326     if (NULL == CU_add_test(suite, "Armoured, passphrase",
327                             test_rsa_signature_armour_passphrase))
328             return 0;
329    
330     if (NULL == CU_add_test(suite, "Large, no armour, no passphrase",
331                             test_rsa_signature_large_noarmour_nopassphrase))
332             return 0;
333    
334     if (NULL == CU_add_test(suite, "Large, armour, no passphrase",
335                             test_rsa_signature_large_armour_nopassphrase))
336             return 0;
337    
338     /*
339     if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo))
340             return 0;
341     */
342     return 1;
343     }
344
345 CU_pSuite suite_rsa_signature()
346     {
347     CU_pSuite suite = NULL;
348
349     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature,
350                          clean_suite_rsa_signature);
351     if (!suite)
352             return NULL;
353
354     if (!add_tests(suite))
355         return NULL;
356
357     return suite;
358     }
359
360 // EOF
361
Note: See TracBrowser for help on using the browser.