root/openpgpsdk/trunk/tests/test_rsa_signature.c

Revision 707 (checked in by ben, 1 year ago)

Fixes from Cyril Soler.

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, ops_true);
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, ops_true);
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, use_armour);
161     }
162
163 static void test_rsa_signature_sign_stream(const int use_armour,
164                                            const char *filename,
165                                            const ops_secret_key_t *skey)
166     {
167     char myfile[MAXBUF];
168     char signed_file[MAXBUF];
169     char buffer[MAXBUF];
170     char *suffix= use_armour ? "streamed.asc" : "streamed.gpg";
171     ops_boolean_t overwrite=ops_true;
172
173     set_up_file_names(myfile, signed_file, filename, suffix);
174
175     ops_create_info_t *info;
176     ops_memory_t *tmp;
177     ops_setup_memory_write(&info, &tmp, MAXBUF);
178     ops_writer_push_signed(info, OPS_SIG_BINARY, skey);
179
180     int input_fd = open(myfile, O_RDONLY | O_BINARY);
181     CU_ASSERT(input_fd >= 0);
182     for (;;)
183         {
184         ssize_t n = read(input_fd, buffer, MAXBUF);
185         CU_ASSERT(n >= 0);
186         if (n == 0)
187             break;
188         ops_write(buffer, n, info);
189         }
190     close(input_fd);
191     ops_writer_close(info);
192     ops_memory_t *signed_memory = copy_partial_packet(tmp);
193     ops_write_file_from_buf(signed_file, ops_memory_get_data(signed_memory),
194                             ops_memory_get_length(signed_memory), overwrite);
195     ops_teardown_memory_write(info, tmp);
196     ops_memory_free(signed_memory);
197     check_sig(signed_file, use_armour);
198     }
199
200 static void test_rsa_signature_sign_memory(const int use_armour,
201                                            const void* input,
202                                            const int input_len,
203                                            const ops_secret_key_t *skey)
204     {
205     ops_memory_t* mem=NULL;
206     ops_parse_info_t *pinfo=NULL;
207     validate_data_cb_arg_t validate_arg;
208
209     mem=ops_sign_buf(input, input_len, OPS_SIG_TEXT, skey, use_armour,
210                      ops_true);
211
212     /*
213      * Validate output
214      */
215
216     if (debug)
217         {
218         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n");
219         }
220    
221     ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem),
222                             ops_memory_get_length(mem), ops_true);
223
224     ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify,
225                           ops_true);
226
227     check_sig_with_ops_core(pinfo, use_armour, &validate_arg);
228
229     ops_memory_free(mem);
230     }
231
232 static void test_rsa_signature_large_noarmour_nopassphrase(void)
233     {
234     assert(pub_keyring.nkeys);
235     test_rsa_signature_sign(OPS_UNARMOURED,
236                             filename_rsa_large_noarmour_nopassphrase,
237                             alpha_skey);
238
239     test_rsa_signature_sign_stream(OPS_UNARMOURED,
240                                    filename_rsa_large_noarmour_nopassphrase,
241                                    alpha_skey);
242     }
243
244 static void test_rsa_signature_large_armour_nopassphrase(void)
245     {
246     assert(pub_keyring.nkeys);
247     test_rsa_signature_sign(OPS_ARMOURED,
248                             filename_rsa_large_armour_nopassphrase, alpha_skey);
249
250     test_rsa_signature_sign_stream(OPS_ARMOURED,
251                                    filename_rsa_large_armour_nopassphrase,
252                                    alpha_skey);
253     }
254
255 static void test_rsa_signature_noarmour_nopassphrase(void)
256     {
257     unsigned char testdata[MAXBUF];
258     assert(pub_keyring.nkeys);
259     test_rsa_signature_sign(OPS_UNARMOURED, filename_rsa_noarmour_nopassphrase,
260                             alpha_skey);
261     create_testdata("test_rsa_signature_noarmour_nopassphrase", testdata,
262                     MAXBUF);
263     test_rsa_signature_sign_memory(OPS_UNARMOURED, testdata, MAXBUF,
264                                    alpha_skey);
265     }
266 static void test_rsa_signature_noarmour_passphrase(void)
267     {
268     unsigned char testdata[MAXBUF];
269     assert(pub_keyring.nkeys);
270     test_rsa_signature_sign(OPS_UNARMOURED, filename_rsa_noarmour_passphrase,
271                             bravo_skey);
272
273     test_rsa_signature_sign_stream(OPS_UNARMOURED, filename_rsa_noarmour_passphrase,
274                                    bravo_skey);
275
276     create_testdata("test_rsa_signature_noarmour_passphrase", testdata, MAXBUF);
277     test_rsa_signature_sign_memory(OPS_UNARMOURED, testdata, MAXBUF, bravo_skey);
278     }
279
280 static void test_rsa_signature_armour_nopassphrase(void)
281     {
282     unsigned char testdata[MAXBUF];
283     assert(pub_keyring.nkeys);
284     test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_nopassphrase,
285                             alpha_skey);
286     test_rsa_signature_sign_stream(OPS_ARMOURED, filename_rsa_armour_nopassphrase,
287                                    alpha_skey);
288
289     create_testdata("test_rsa_signature_armour_nopassphrase", testdata, MAXBUF);
290     test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, alpha_skey);
291     }
292
293 static void test_rsa_signature_armour_passphrase(void)
294     {
295     unsigned char testdata[MAXBUF];
296
297     assert(pub_keyring.nkeys);
298     test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_passphrase,
299                             bravo_skey);
300     test_rsa_signature_sign_stream(OPS_ARMOURED, filename_rsa_armour_passphrase,
301                                    bravo_skey);
302
303     create_testdata("test_rsa_signature_armour_passphrase", testdata, MAXBUF);
304     test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, bravo_skey);
305     }
306
307 static void test_rsa_signature_clearsign_file_nopassphrase(void)
308     {
309     assert(pub_keyring.nkeys);
310     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase,
311                                       alpha_skey);
312     }
313
314 static void test_rsa_signature_clearsign_file_passphrase(void)
315     {
316     assert(pub_keyring.nkeys);
317     test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase,
318                                       bravo_skey);
319     }
320
321 static void test_rsa_signature_clearsign_buf_nopassphrase(void)
322     {
323     assert(pub_keyring.nkeys);
324     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase,
325                                      alpha_skey);
326     }
327
328 static void test_rsa_signature_clearsign_buf_passphrase(void)
329     {
330     assert(pub_keyring.nkeys);
331     test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase,
332                                      bravo_skey);
333     }
334 /*
335 static void test_todo(void)
336     {
337     CU_FAIL("Test FUTURE: Use other hash algorithms");
338     CU_FAIL("Test FUTURE: Check for key expiry");
339     CU_FAIL("Test FUTURE: Check for key revocation");
340     CU_FAIL("Test FUTURE: Check for signature expiry");
341     CU_FAIL("Test FUTURE: Check for signature revocation");
342     }
343 */
344
345 static int add_tests(CU_pSuite suite)
346     {
347     // add tests to suite
348     
349     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase",
350                             test_rsa_signature_noarmour_nopassphrase))
351             return 0;
352    
353     if (NULL == CU_add_test(suite, "Unarmoured, passphrase",
354                             test_rsa_signature_noarmour_passphrase))
355             return 0;
356     if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase",
357                             test_rsa_signature_clearsign_file_nopassphrase))
358             return 0;
359
360     if (NULL == CU_add_test(suite, "Clearsigned file, passphrase",
361                             test_rsa_signature_clearsign_file_passphrase))
362             return 0;
363
364     if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase",
365                             test_rsa_signature_clearsign_buf_nopassphrase))
366             return 0;
367    
368     if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase",
369                             test_rsa_signature_clearsign_buf_passphrase))
370             return 0;
371
372     if (NULL == CU_add_test(suite, "Armoured, no passphrase",
373                             test_rsa_signature_armour_nopassphrase))
374             return 0;
375    
376     if (NULL == CU_add_test(suite, "Armoured, passphrase",
377                             test_rsa_signature_armour_passphrase))
378             return 0;
379    
380     if (NULL == CU_add_test(suite, "Large, no armour, no passphrase",
381                             test_rsa_signature_large_noarmour_nopassphrase))
382             return 0;
383    
384     if (NULL == CU_add_test(suite, "Large, armour, no passphrase",
385                             test_rsa_signature_large_armour_nopassphrase))
386             return 0;
387     /*
388     if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo))
389             return 0;
390     */
391     return 1;
392     }
393
394 CU_pSuite suite_rsa_signature()
395     {
396     CU_pSuite suite = NULL;
397
398     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature,
399                          clean_suite_rsa_signature);
400     if (!suite)
401             return NULL;
402
403     if (!add_tests(suite))
404         return NULL;
405
406     return suite;
407     }
408
409 // EOF
410
Note: See TracBrowser for help on using the browser.