| 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 |
#include "openpgpsdk/validate.h" |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
#include "../src/advanced/parse_local.h" |
|---|
| 16 |
|
|---|
| 17 |
#include "tests.h" |
|---|
| 18 |
|
|---|
| 19 |
static int debug=0; |
|---|
| 20 |
|
|---|
| 21 |
static char *filename_rsa_noarmour_nopassphrase="ops_rsa_signed_noarmour_nopassphrase.txt"; |
|---|
| 22 |
static char *filename_rsa_noarmour_passphrase="ops_rsa_signed_noarmour_passphrase.txt"; |
|---|
| 23 |
static char *filename_rsa_armour_nopassphrase="ops_rsa_signed_armour_nopassphrase.txt"; |
|---|
| 24 |
static char *filename_rsa_armour_passphrase="ops_rsa_signed_armour_passphrase.txt"; |
|---|
| 25 |
static char *filename_rsa_clearsign_nopassphrase="ops_rsa_signed_clearsign_nopassphrase.txt"; |
|---|
| 26 |
static char *filename_rsa_clearsign_passphrase="ops_rsa_signed_clearsign_passphrase.txt"; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
int init_suite_rsa_signature(void) |
|---|
| 34 |
{ |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
create_testfile(filename_rsa_noarmour_nopassphrase); |
|---|
| 38 |
create_testfile(filename_rsa_noarmour_passphrase); |
|---|
| 39 |
create_testfile(filename_rsa_armour_nopassphrase); |
|---|
| 40 |
create_testfile(filename_rsa_armour_passphrase); |
|---|
| 41 |
create_testfile(filename_rsa_clearsign_nopassphrase); |
|---|
| 42 |
create_testfile(filename_rsa_clearsign_passphrase); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
return 0; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
int clean_suite_rsa_signature(void) |
|---|
| 49 |
{ |
|---|
| 50 |
ops_finish(); |
|---|
| 51 |
|
|---|
| 52 |
reset_vars(); |
|---|
| 53 |
|
|---|
| 54 |
return 0; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
static void test_rsa_signature_clearsign(const char *filename, const ops_secret_key_t *skey, ops_hash_algorithm_t hash_alg) |
|---|
| 58 |
{ |
|---|
| 59 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 60 |
ops_create_signature_t *sig=NULL; |
|---|
| 61 |
|
|---|
| 62 |
char cmd[MAXBUF+1]; |
|---|
| 63 |
char myfile[MAXBUF+1]; |
|---|
| 64 |
char signed_file[MAXBUF+1]; |
|---|
| 65 |
|
|---|
| 66 |
char *suffix= "asc"; |
|---|
| 67 |
int fd_in=0; |
|---|
| 68 |
int fd_out=0; |
|---|
| 69 |
int rtn=0; |
|---|
| 70 |
ops_create_info_t *cinfo=NULL; |
|---|
| 71 |
unsigned char buf[MAXBUF]; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
snprintf(myfile,MAXBUF,"%s/%s",dir,filename); |
|---|
| 75 |
#ifdef WIN32 |
|---|
| 76 |
fd_in=open(myfile,O_RDONLY | O_BINARY); |
|---|
| 77 |
#else |
|---|
| 78 |
fd_in=open(myfile,O_RDONLY); |
|---|
| 79 |
#endif |
|---|
| 80 |
if(fd_in < 0) |
|---|
| 81 |
{ |
|---|
| 82 |
perror(myfile); |
|---|
| 83 |
exit(2); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
snprintf(signed_file,MAXBUF,"%s/%s_%s.%s",dir,filename,ops_show_hash_algorithm(hash_alg),suffix); |
|---|
| 87 |
#ifdef WIN32 |
|---|
| 88 |
fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600); |
|---|
| 89 |
#else |
|---|
| 90 |
fd_out=open(signed_file,O_WRONLY | O_CREAT | O_EXCL, 0600); |
|---|
| 91 |
#endif |
|---|
| 92 |
if(fd_out < 0) |
|---|
| 93 |
{ |
|---|
| 94 |
perror(signed_file); |
|---|
| 95 |
exit(2); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
sig=ops_create_signature_new(); |
|---|
| 105 |
ops_signature_start_plaintext_signature(sig,(ops_secret_key_t *)skey,hash_alg,OPS_SIG_BINARY); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
cinfo=ops_create_info_new(); |
|---|
| 109 |
ops_writer_set_fd(cinfo,fd_out); |
|---|
| 110 |
ops_writer_push_dash_escaped(cinfo,sig); |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
for (;;) |
|---|
| 115 |
{ |
|---|
| 116 |
int n=0; |
|---|
| 117 |
|
|---|
| 118 |
n=read(fd_in,buf,sizeof(buf)); |
|---|
| 119 |
if (!n) |
|---|
| 120 |
break; |
|---|
| 121 |
assert(n>=0); |
|---|
| 122 |
ops_write(buf,n,cinfo); |
|---|
| 123 |
} |
|---|
| 124 |
close(fd_in); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
ops_writer_switch_to_signature(cinfo); |
|---|
| 129 |
ops_signature_add_creation_time(sig,time(NULL)); |
|---|
| 130 |
ops_keyid(keyid,&skey->public_key); |
|---|
| 131 |
ops_signature_add_issuer_key_id(sig,keyid); |
|---|
| 132 |
|
|---|
| 133 |
ops_signature_hashed_subpackets_end(sig); |
|---|
| 134 |
ops_write_signature(sig,(ops_public_key_t *)&skey->public_key,(ops_secret_key_t *)skey,cinfo); |
|---|
| 135 |
ops_writer_close(cinfo); |
|---|
| 136 |
close(fd_out); |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
{ |
|---|
| 145 |
int fd=0; |
|---|
| 146 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 147 |
validate_data_cb_arg_t validate_arg; |
|---|
| 148 |
ops_validate_result_t result; |
|---|
| 149 |
int rtn=0; |
|---|
| 150 |
|
|---|
| 151 |
if (debug) |
|---|
| 152 |
{ |
|---|
| 153 |
fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
#ifdef WIN32 |
|---|
| 158 |
fd=open(signed_file,O_RDONLY | O_BINARY); |
|---|
| 159 |
#else |
|---|
| 160 |
fd=open(signed_file,O_RDONLY); |
|---|
| 161 |
#endif |
|---|
| 162 |
if(fd < 0) |
|---|
| 163 |
{ |
|---|
| 164 |
perror(signed_file); |
|---|
| 165 |
exit(2); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
pinfo=ops_parse_info_new(); |
|---|
| 171 |
|
|---|
| 172 |
memset(&validate_arg,'\0',sizeof validate_arg); |
|---|
| 173 |
validate_arg.result=&result; |
|---|
| 174 |
validate_arg.keyring=&pub_keyring; |
|---|
| 175 |
validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); |
|---|
| 176 |
|
|---|
| 177 |
ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); |
|---|
| 178 |
ops_parse_cb_set(pinfo,callback_verify,&validate_arg); |
|---|
| 179 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 180 |
pinfo->rinfo.accumulate=ops_true; |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
rtn=ops_parse(pinfo); |
|---|
| 191 |
ops_print_errors(ops_parse_info_get_errors(pinfo)); |
|---|
| 192 |
CU_ASSERT(rtn==1); |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 197 |
|
|---|
| 198 |
ops_parse_info_delete(pinfo); |
|---|
| 199 |
|
|---|
| 200 |
close(fd); |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
{ |
|---|
| 205 |
|
|---|
| 206 |
snprintf(cmd,MAXBUF,"%s --verify %s", gpgcmd, signed_file); |
|---|
| 207 |
rtn=system(cmd); |
|---|
| 208 |
CU_ASSERT(rtn==0); |
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
static void test_rsa_signature_noarmour_nopassphrase(void) |
|---|
| 213 |
{ |
|---|
| 214 |
CU_FAIL("Test TODO: Sign file with no armour and no passphrase"); |
|---|
| 215 |
#ifdef TBD |
|---|
| 216 |
|
|---|
| 217 |
int armour=0; |
|---|
| 218 |
assert(pub_keyring.nkeys); |
|---|
| 219 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA1); |
|---|
| 220 |
#ifdef TODO |
|---|
| 221 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_MD5); |
|---|
| 222 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_RIPEMD); |
|---|
| 223 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA256); |
|---|
| 224 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA384); |
|---|
| 225 |
test_rsa_signature(armour,filename_rsa_noarmour_nopassphrase, alpha_skey, OPS_HASH_SHA512); |
|---|
| 226 |
#endif |
|---|
| 227 |
#endif |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
static void test_rsa_signature_noarmour_passphrase(void) |
|---|
| 231 |
{ |
|---|
| 232 |
CU_FAIL("Test TODO: Sign file with no armour and passphrase"); |
|---|
| 233 |
#ifdef TBD |
|---|
| 234 |
int armour=0; |
|---|
| 235 |
assert(pub_keyring.nkeys); |
|---|
| 236 |
test_rsa_signature(armour,filename_rsa_noarmour_passphrase, bravo_skey, OPS_HASH_SHA1); |
|---|
| 237 |
#endif |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
static void test_rsa_signature_armour_nopassphrase(void) |
|---|
| 241 |
{ |
|---|
| 242 |
CU_FAIL("Test TODO: Sign file with armour and no passphrase"); |
|---|
| 243 |
#ifdef TBD |
|---|
| 244 |
int armour=1; |
|---|
| 245 |
assert(pub_keyring.nkeys); |
|---|
| 246 |
test_rsa_signature(armour,filename_rsa_armour_nopassphrase, alpha_skey, OPS_HASH_SHA1); |
|---|
| 247 |
#endif |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
static void test_rsa_signature_armour_passphrase(void) |
|---|
| 251 |
{ |
|---|
| 252 |
CU_FAIL("Test TODO: Sign file with armour and passphrase"); |
|---|
| 253 |
#ifdef TBD |
|---|
| 254 |
int armour=1; |
|---|
| 255 |
assert(pub_keyring.nkeys); |
|---|
| 256 |
test_rsa_signature(armour,filename_rsa_armour_passphrase, bravo_skey, OPS_HASH_SHA1); |
|---|
| 257 |
#endif |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
static void test_rsa_signature_clearsign_nopassphrase(void) |
|---|
| 261 |
{ |
|---|
| 262 |
assert(pub_keyring.nkeys); |
|---|
| 263 |
test_rsa_signature_clearsign(filename_rsa_armour_nopassphrase, alpha_skey, OPS_HASH_SHA1); |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
static void test_rsa_signature_clearsign_passphrase(void) |
|---|
| 267 |
{ |
|---|
| 268 |
assert(pub_keyring.nkeys); |
|---|
| 269 |
test_rsa_signature_clearsign(filename_rsa_armour_passphrase, bravo_skey, OPS_HASH_SHA1); |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
CU_pSuite suite_rsa_signature() |
|---|
| 273 |
{ |
|---|
| 274 |
CU_pSuite suite = NULL; |
|---|
| 275 |
|
|---|
| 276 |
suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature); |
|---|
| 277 |
if (!suite) |
|---|
| 278 |
return NULL; |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase)) |
|---|
| 283 |
return NULL; |
|---|
| 284 |
|
|---|
| 285 |
if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase)) |
|---|
| 286 |
return NULL; |
|---|
| 287 |
|
|---|
| 288 |
if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase)) |
|---|
| 289 |
return NULL; |
|---|
| 290 |
|
|---|
| 291 |
if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase)) |
|---|
| 292 |
return NULL; |
|---|
| 293 |
|
|---|
| 294 |
if (NULL == CU_add_test(suite, "Clearsigned, no passphrase", test_rsa_signature_clearsign_nopassphrase)) |
|---|
| 295 |
return NULL; |
|---|
| 296 |
|
|---|
| 297 |
if (NULL == CU_add_test(suite, "Clearsigned, passphrase", test_rsa_signature_clearsign_passphrase)) |
|---|
| 298 |
return NULL; |
|---|
| 299 |
|
|---|
| 300 |
return suite; |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|