root/openpgpsdk/trunk/tests/test_rsa_decrypt.c

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

Doxygen changes

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 #include "tests.h"
23
24 #include "CUnit/Basic.h"
25
26 #include <openpgpsdk/types.h>
27 #include "openpgpsdk/keyring.h"
28 #include <openpgpsdk/armour.h>
29 #include "openpgpsdk/packet.h"
30 #include "openpgpsdk/packet-parse.h"
31 #include "openpgpsdk/readerwriter.h"
32 #include "openpgpsdk/util.h"
33 #include "openpgpsdk/std_print.h"
34 #include "../src/lib/parse_local.h"
35
36 static char *compress_algos[]={ "zip", "zlib", "bzip2" };
37 static int n_compress_algos=3;
38
39 static ops_parse_cb_return_t
40 callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
41     {
42     ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
43     static ops_boolean_t skipping;
44
45     OPS_USED(cbinfo);
46
47 //    ops_print_packet(content_);
48
49     if(content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT && skipping)
50         {
51         puts("...end of skip");
52         skipping=ops_false;
53         }
54
55     switch(content_->tag)
56         {
57     case OPS_PTAG_CT_UNARMOURED_TEXT:
58         printf("OPS_PTAG_CT_UNARMOURED_TEXT\n");
59         if(!skipping)
60             {
61             puts("Skipping...");
62             skipping=ops_true;
63             }
64         fwrite(content->unarmoured_text.data,1,
65                content->unarmoured_text.length,stdout);
66         break;
67
68     case OPS_PTAG_CT_PK_SESSION_KEY:
69         return callback_pk_session_key(content_,cbinfo);
70         break;
71
72     case OPS_PARSER_CMD_GET_SECRET_KEY:
73         return callback_cmd_get_secret_key(content_,cbinfo);
74         break;
75
76     case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
77         return test_cb_get_passphrase(content_,cbinfo);
78         break;
79
80     case OPS_PTAG_CT_LITERAL_DATA_BODY:
81         return callback_literal_data(content_,cbinfo);
82                 break;
83
84     case OPS_PTAG_CT_ARMOUR_HEADER:
85     case OPS_PTAG_CT_ARMOUR_TRAILER:
86     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
87     case OPS_PTAG_CT_COMPRESSED:
88     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
89     case OPS_PTAG_CT_SE_IP_DATA_BODY:
90     case OPS_PTAG_CT_SE_IP_DATA_HEADER:
91     case OPS_PTAG_CT_SE_DATA_BODY:
92     case OPS_PTAG_CT_SE_DATA_HEADER:
93
94         // Ignore these packets
95         // They're handled in ops_parse_one_packet()
96         // and nothing else needs to be done
97         break;
98
99     default:
100         return callback_general(content_,cbinfo);
101         //      fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
102         //              content_->tag);
103         //      assert(0);
104         }
105
106     return OPS_RELEASE_MEMORY;
107     }
108
109 /* Decryption suite initialization.
110  * Create temporary directory.
111  * Create temporary test files.
112  */
113
114 int init_suite_rsa_decrypt(void)
115     {
116     // Return success
117     return 0;
118     }
119
120 int clean_suite_rsa_decrypt(void)
121     {
122        
123     reset_vars();
124
125     return 0;
126     }
127
128 static void test_rsa_decrypt(const int has_armour, const char *filename)
129     {
130     char encfile[MAXBUF+1];
131     char* testtext=NULL;
132     char *suffix= has_armour ? "asc" : "gpg";
133     int fd=0;
134     ops_parse_info_t *pinfo=NULL;
135     ops_memory_t* mem_out=NULL;
136     int rtn=0;
137     int repeats=10;
138
139     // open encrypted file
140     snprintf(encfile,sizeof encfile,"%s/%s.%s",dir,
141              filename,suffix);
142
143     // setup for reading from given input file
144     ops_setup_file_read(&pinfo, encfile,
145                         NULL, /* arg */
146                         callback,
147                         ops_false /* accumulate */
148                         );
149
150     // setup keyring and passphrase callback
151     pinfo->cbinfo.cryptinfo.keyring=&sec_keyring;
152     pinfo->cbinfo.cryptinfo.cb_get_passphrase=test_cb_get_passphrase;
153
154     // Set up armour/passphrase options
155
156     if (has_armour)
157         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false);
158    
159     // setup for writing parsed data to mem_out
160     ops_setup_memory_write(&pinfo->cbinfo.cinfo, &mem_out, 128);
161
162     // do it
163     rtn=ops_parse_and_print_errors(pinfo);
164     CU_ASSERT(rtn==1);
165
166     // Tidy up
167     if (has_armour)
168         ops_reader_pop_dearmour(pinfo);
169
170     close(fd);
171    
172     // File contents should match
173     testtext=create_testtext(filename,repeats);
174     CU_ASSERT(strlen(testtext)==ops_memory_get_length(mem_out));
175     CU_ASSERT(memcmp(ops_memory_get_data(mem_out),
176                      testtext,
177                      ops_memory_get_length(mem_out))==0);
178     }
179
180 static void create_filename(char* buf, int maxbuf, int armour, int passphrase, char * sym_alg, char * compress_alg, int compress_level)
181     {
182     snprintf(buf,maxbuf,"gpg_enc_rsa_%s_%s_%s_%s_%d.txt",
183         armour ? "arm" : "noarm",
184         passphrase ? "pp" : "nopp",
185         sym_alg,
186         compress_alg, compress_level);
187     }
188
189 static void test_rsa_decrypt_generic(char* sym_alg)
190     {
191     char filename[MAXBUF+1];
192     char cmd[MAXBUF+1];
193     int armour=0;
194     int passphrase=0;
195     int compress_alg=0;
196     int compress_lvl=0;
197
198     for (compress_alg=0; compress_alg<n_compress_algos; compress_alg++)
199         {
200         for (compress_lvl=0; compress_lvl<=MAX_COMPRESS_LEVEL; compress_lvl++)
201             {
202             /* only need to check every compression level if we're debugging */
203             if (compress_lvl>0 && compress_lvl < MAX_COMPRESS_LEVEL)
204                 continue;
205             for (armour=0; armour<=1; armour++)
206                 {
207                 char *armour_cmd= armour ? "--armor " : "";
208                 char *suffix= armour ? "asc" : "gpg";
209                
210                 for (passphrase=0; passphrase<=1; passphrase++)
211                     {
212                     char *rcpt= passphrase ? "Bravo" : "Alpha";
213
214                     // Create filename matching these params
215                     create_filename(&filename[0],sizeof filename,
216                                     armour, passphrase,
217                                     sym_alg,
218                                     compress_algos[compress_alg], compress_lvl);
219                    
220                     // Create file with unique text matching this test
221                     create_small_testfile(filename);
222                    
223                     // Encrypt file using GPG
224                     snprintf(cmd,sizeof cmd,"gpg --quiet --no-tty --homedir=%s --cipher-algo \"%s\" --compress-algo \"%s\" --compress-level %d --output=%s/%s.%s  --force-mdc --encrypt --recipient %s %s %s/%s",
225                              dir, //homedir
226                              sym_alg,
227                              compress_algos[compress_alg],
228                              compress_lvl,
229                              dir, filename, suffix, // for output file
230                              rcpt,
231                              armour_cmd,
232                              dir, filename);
233                     if (system(cmd))
234                         {
235                         fprintf(stderr,"Err: cmd is %s\n", cmd);
236                         //                        return 1;
237                         }
238                    
239                     // Decrypt using OPS
240                     test_rsa_decrypt(armour,filename);
241                     }
242                 }
243             }
244         }
245     }
246
247 static void test_rsa_decrypt_cast5(void)
248     {
249     return test_rsa_decrypt_generic("cast5");
250     }
251
252 static void test_rsa_decrypt_aes128(void)
253     {
254     return test_rsa_decrypt_generic("aes");
255     }
256
257 static void test_rsa_decrypt_aes256(void)
258     {
259     return test_rsa_decrypt_generic("aes256");
260     }
261
262 static void test_rsa_decrypt_3des(void)
263     {
264     return test_rsa_decrypt_generic("3des");
265     }
266
267 //
268
269 /*
270 static void test_todo(void)
271     {
272     CU_FAIL("Test FUTURE: Decryption with multiple keys in same keyring");
273     CU_FAIL("Test FUTURE: Decryption with multiple keys where some are not in my keyring");
274     CU_FAIL("Test FUTURE: Decryption with multiple keys where my key is (a) first key in list; (b) last key in list; (c) in the middle of the list");
275     }
276 */
277
278 static int add_tests(CU_pSuite suite)
279     {
280     // add tests to suite
281     
282     if (NULL == CU_add_test(suite, "CAST5", test_rsa_decrypt_cast5))
283             return 0;
284
285     if (NULL == CU_add_test(suite, "AES128", test_rsa_decrypt_aes128))
286             return 0;
287
288     if (NULL == CU_add_test(suite, "AES256", test_rsa_decrypt_aes256))
289             return 0;
290
291     if (NULL == CU_add_test(suite, "3DES", test_rsa_decrypt_3des))
292             return 0;
293
294     /*
295     if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo))
296             return 0;
297     */
298     return 1;
299     }
300
301 CU_pSuite suite_rsa_decrypt()
302 {
303     CU_pSuite suite = NULL;
304
305     suite = CU_add_suite("RSA Decryption Suite", init_suite_rsa_decrypt, clean_suite_rsa_decrypt);
306     if (!suite)
307             return NULL;
308
309     if (!add_tests(suite))
310         return NULL;
311
312     return suite;
313 }
314
Note: See TracBrowser for help on using the browser.