root/openpgpsdk/trunk/tests/test_rsa_signature.c

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

WIN32 changes provided by Alexey Simak

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_noarmour_passphrase="ops_rsa_signed_noarmour_passphrase.txt";
17 static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt";
18 static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt";
19
20 /* Signature suite initialization.
21  * Create temporary directory.
22  * Create temporary test files.
23  */
24
25 int init_suite_rsa_signature(void)
26     {
27     // Create test files
28
29     create_testfile(filename_rsa_noarmour_nopassphrase);
30     create_testfile(filename_rsa_noarmour_passphrase);
31     create_testfile(filename_rsa_armour_nopassphrase);
32     create_testfile(filename_rsa_armour_passphrase);
33
34     // Return success
35     return 0;
36     }
37
38 int clean_suite_rsa_signature(void)
39     {
40     ops_finish();
41
42     reset_vars();
43
44     return 0;
45     }
46
47 static void test_rsa_signature(const int has_armour, const char *filename, const ops_secret_key_t *skey, ops_hash_algorithm_t hash_alg)
48     {
49     unsigned char keyid[OPS_KEY_ID_SIZE];
50     ops_create_signature_t *sig=NULL;
51
52     char cmd[MAXBUF+1];
53     char myfile[MAXBUF+1];
54     char signed_file[MAXBUF+1];
55     char *suffix= has_armour ? "asc" : "gpg";
56     int fd_in=0;
57     int fd_out=0;
58     int rtn=0;
59     ops_create_info_t *cinfo=NULL;
60    
61     // open file to sign
62     snprintf(myfile,MAXBUF,"%s/%s",dir,filename);
63 #ifdef WIN32
64     fd_in=open(myfile,O_RDONLY | O_BINARY);
65 #else
66     fd_in=open(myfile,O_RDONLY);
67 #endif
68     if(fd_in < 0)
69         {
70         perror(myfile);
71         exit(2);
72         }
73    
74     snprintf(signed_file,MAXBUF,"%s/%s_%s.%s",dir,filename,ops_show_hash_algorithm(hash_alg),suffix);
75 #ifdef WIN32
76     fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600);
77 #else
78     fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL, 0600);
79 #endif
80     if(fd_out < 0)
81         {
82         perror(signed_file);
83         exit(2);
84         }
85    
86     // Set up armour/passphrase options
87     // OPS code armours signatures by default
88
89     assert(has_armour);
90    
91     // set up signature
92     sig=ops_create_signature_new();
93     ops_signature_start_plaintext_signature(sig,(ops_secret_key_t *)skey,hash_alg,OPS_SIG_BINARY);
94
95     // set up output file
96     cinfo=ops_create_info_new();
97     ops_writer_set_fd(cinfo,fd_out);
98     ops_writer_push_dash_escaped(cinfo,sig);
99
100     // Do the signing
101
102     for (;;)
103         {
104         unsigned char buf[MAXBUF];
105         int n=0;
106    
107         n=read(fd_in,buf,sizeof(buf));
108         if (!n)
109             break;
110         assert(n>=0);
111         ops_write(buf,n,cinfo);
112         }
113     close(fd_in);
114
115     // add signature
116
117     ops_writer_switch_to_signature(cinfo);
118     ops_signature_add_creation_time(sig,time(NULL));
119     ops_keyid(keyid,&skey->public_key);
120     ops_signature_add_issuer_key_id(sig,keyid);
121     ops_signature_hashed_subpackets_end(sig);
122     ops_write_signature(sig,(ops_public_key_t *)&skey->public_key,(ops_secret_key_t *)skey,cinfo);
123     ops_writer_close(cinfo);
124     close(fd_out);
125
126 #ifdef TODO
127      // Check signature with OPS
128 #endif
129
130     // Check signature with GPG
131
132     snprintf(cmd,MAXBUF,"gpg --verify --quiet --homedir %s %s", dir, signed_file);
133     rtn=system(cmd);
134     CU_ASSERT(rtn==0);
135     }
136
137 void test_rsa_signature_noarmour_nopassphrase(void)
138     {
139     int armour=0;
140     assert(pub_keyring.nkeys);
141     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA1);
142 #ifdef TODO
143     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_MD5);
144     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_RIPEMD);
145     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA256);
146     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA384);
147     test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA512);
148 #endif
149     }
150
151 void test_rsa_signature_noarmour_passphrase(void)
152     {
153     int armour=0;
154     assert(pub_keyring.nkeys);
155     test_rsa_signature(armour,filename_rsa_noarmour_passphrase, bravo_skey, OPS_HASH_SHA1);
156     }
157
158 void test_rsa_signature_armour_nopassphrase(void)
159     {
160     int armour=1;
161     assert(pub_keyring.nkeys);
162     test_rsa_signature(armour,filename_rsa_armour_nopassphrase, alpha_skey, OPS_HASH_SHA1);
163     }
164
165 void test_rsa_signature_armour_passphrase(void)
166     {
167     int armour=1;
168     assert(pub_keyring.nkeys);
169     test_rsa_signature(armour,filename_rsa_armour_passphrase, bravo_skey, OPS_HASH_SHA1);
170     }
171
172 CU_pSuite suite_rsa_signature()
173 {
174     CU_pSuite suite = NULL;
175
176     suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature);
177     if (!suite)
178             return NULL;
179
180     // add tests to suite
181     
182 #ifdef TBD
183     if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase))
184             return NULL;
185    
186     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase))
187             return NULL;
188 #endif /*TBD*/
189    
190     if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase))
191             return NULL;
192    
193     if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase))
194             return NULL;
195    
196    
197     return suite;
198 }
199
Note: See TracBrowser for help on using the browser.