root/openpgpsdk/trunk/tests/test_rsa_signature.c

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

New RSA verify test

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/packet-show.h"
9 #include "openpgpsdk/util.h"
10 #include "openpgpsdk/std_print.h"
11 #include "openpgpsdk/readerwriter.h"
12
13 #include "tests.h"
14
15 static char *filename_rsa_noarmour_nopassphrase="ops_rsa_signed_noarmour_nopassphrase.txt";
16 static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt";
17 static char *filename_rsa_noarmour_passphrase="ops_rsa_signed_armour_nopassphrase.txt";
18 static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt";
19
20 #ifdef XXX
21 static ops_parse_cb_return_t
22 callback_ops_decrypt(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
23     {
24     ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
25     static ops_boolean_t skipping;
26     //    static const ops_key_data_t *decrypter;
27     //    const ops_key_data_t *keydata=NULL;
28     //    const ops_secret_key_t *secret;
29
30     OPS_USED(cbinfo);
31
32 //    ops_print_packet(content_);
33
34     if(content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT && skipping)
35         {
36         puts("...end of skip");
37         skipping=ops_false;
38         }
39
40     switch(content_->tag)
41         {
42     case OPS_PTAG_CT_UNARMOURED_TEXT:
43         printf("OPS_PTAG_CT_UNARMOURED_TEXT\n");
44         if(!skipping)
45             {
46             puts("Skipping...");
47             skipping=ops_true;
48             }
49         fwrite(content->unarmoured_text.data,1,
50                content->unarmoured_text.length,stdout);
51         break;
52
53     case OPS_PTAG_CT_PK_SESSION_KEY:
54         return callback_pk_session_key(content_,cbinfo);
55
56     case OPS_PARSER_CMD_GET_SECRET_KEY:
57         return callback_cmd_get_secret_key(content_,cbinfo);
58
59     case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
60         return callback_cmd_get_secret_key_passphrase(content_,cbinfo);
61
62     case OPS_PTAG_CT_LITERAL_DATA_BODY:
63         return callback_literal_data(content_,cbinfo);
64         //      text=ops_mallocz(content->literal_data_body.length+1);
65         //      memcpy(text,content->literal_data_body.data,content->literal_data_body.length);
66         //              break;
67
68     case OPS_PARSER_PTAG:
69     case OPS_PTAG_CT_ARMOUR_HEADER:
70     case OPS_PTAG_CT_ARMOUR_TRAILER:
71     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
72     case OPS_PTAG_CT_COMPRESSED:
73     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
74     case OPS_PTAG_CT_SE_IP_DATA_BODY:
75     case OPS_PTAG_CT_SE_IP_DATA_HEADER:
76         // Ignore these packets
77         // They're handled in ops_parse_one_packet()
78         // and nothing else needs to be done
79         break;
80
81     default:
82         fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
83                 content_->tag);
84         assert(0);
85         }
86
87     return OPS_RELEASE_MEMORY;
88     }
89 #endif
90
91 /* Signature suite initialization.
92  * Create temporary directory.
93  * Create temporary test files.
94  */
95
96 int init_suite_rsa_signature(void)
97     {
98     // Create test files
99
100     create_testfile(filename_rsa_noarmour_nopassphrase);
101     create_testfile(filename_rsa_armour_nopassphrase);
102     create_testfile(filename_rsa_noarmour_passphrase);
103     create_testfile(filename_rsa_armour_passphrase);
104
105     // Return success
106     return 0;
107     }
108
109 int clean_suite_rsa_signature(void)
110     {
111     ops_finish();
112
113     reset_vars();
114
115     return 0;
116     }
117
118 static void test_rsa_signature(const int has_armour, const char *filename, const ops_secret_key_t *skey, ops_hash_algorithm_t hash_alg)
119     {
120     unsigned char keyid[OPS_KEY_ID_SIZE];
121     ops_create_signature_t *sig=NULL;
122
123     char cmd[MAXBUF+1];
124     char myfile[MAXBUF+1];
125     char signed_file[MAXBUF+1];
126     char *suffix= has_armour ? "asc" : "gpg";
127     int fd_in=0;
128     int fd_out=0;
129     int rtn=0;
130    
131     // open file to sign
132     snprintf(myfile,MAXBUF,"%s/%s",dir,filename);
133     fd_in=open(myfile,O_RDONLY);
134     if(fd_in < 0)
135         {
136         perror(myfile);
137         exit(2);
138         }
139    
140     snprintf(signed_file,MAXBUF,"%s/%s_%s.%s",dir,filename,ops_show_hash_algorithm(hash_alg),suffix);
141     fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL, 0600);
142     if(fd_out < 0)
143         {
144         perror(signed_file);
145         exit(2);
146         }
147    
148     // Set up armour/passphrase options
149
150     /*
151     if (has_armour)
152         ops_writer_push_armour(cinfo,ops_false,ops_false,ops_false);
153         */
154     // current_passphrase=has_passphrase ? passphrase : nopassphrase;
155     
156     // set up signature
157     sig=ops_create_signature_new();
158     ops_signature_start_plaintext_signature(sig,(ops_secret_key_t *)skey,hash_alg,OPS_SIG_BINARY);
159
160     // set up output file
161     ops_create_info_t *cinfo;
162     cinfo=ops_create_info_new();
163     ops_writer_set_fd(cinfo,fd_out);
164     ops_writer_push_dash_escaped(cinfo,sig);
165
166     // Do the signing
167
168     for (;;)
169         {
170         unsigned char buf[MAXBUF];
171         int n=0;
172    
173         n=read(fd_in,buf,sizeof(buf));
174         if (!n)
175             break;
176         assert(n>=0);
177         ops_write(buf,n,cinfo);
178         }
179     close(fd_in);
180
181     // add signature
182
183     ops_writer_switch_to_signature(cinfo);
184     ops_signature_add_creation_time(sig,time(NULL));
185     ops_keyid(keyid,&skey->public_key);
186     ops_signature_add_issuer_key_id(sig,keyid);
187     ops_signature_hashed_subpackets_end(sig);
188     ops_write_signature(sig,(ops_public_key_t *)&skey->public_key,(ops_secret_key_t *)skey,cinfo);
189     ops_writer_close(cinfo);
190
191 #ifdef TODO
192      // Check signature with OPS
193 #endif
194
195     // Check signature with GPG
196
197     snprintf(cmd,MAXBUF,"gpg --verify --quiet --homedir %s %s", dir, signed_file);
198     rtn=system(cmd);
199     CU_ASSERT(rtn==0);
200     }
201
202 void test_rsa_signature_noarmour_nopassphrase(void)
203     {
204     int armour=0;
205     assert(pub_keyring.nkeys);
206     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA1);
207 #ifdef TODO
208     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_MD5);
209     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_RIPEMD);
210     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA256);
211     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA384);
212     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA512);
213 #endif
214     }
215
216 void test_rsa_signature_noarmour_passphrase(void)
217     {
218     int armour=0;
219     assert(pub_keyring.nkeys);
220     test_rsa_signature(armour,filename_rsa_noarmour_passphrase, bravo_skey, OPS_HASH_SHA1);
221     }
222
223 CU_pSuite suite_rsa_signature()
224 {
225     CU_pSuite suite = NULL;
226
227     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
228     if (!suite)
229             return NULL;
230
231     // add tests to suite
232     
233     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
234             return NULL;
235    
236     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
237             return NULL;
238    
239 #ifdef TBD
240     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
241             return NULL;
242    
243     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
244             return NULL;
245    
246 #endif /*TBD*/
247    
248     return suite;
249 }
250
Note: See TracBrowser for help on using the browser.