| 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/lib/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_file_nopassphrase="ops_rsa_signed_clearsign_file_nopassphrase.txt"; |
|---|
| 26 |
static char *filename_rsa_clearsign_file_passphrase="ops_rsa_signed_clearsign_file_passphrase.txt"; |
|---|
| 27 |
static char *filename_rsa_clearsign_buf_nopassphrase="ops_rsa_signed_clearsign_buf_nopassphrase.txt"; |
|---|
| 28 |
static char *filename_rsa_clearsign_buf_passphrase="ops_rsa_signed_clearsign_buf_passphrase.txt"; |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
int init_suite_rsa_signature(void) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
create_testfile(filename_rsa_noarmour_nopassphrase); |
|---|
| 40 |
create_testfile(filename_rsa_noarmour_passphrase); |
|---|
| 41 |
create_testfile(filename_rsa_armour_nopassphrase); |
|---|
| 42 |
create_testfile(filename_rsa_armour_passphrase); |
|---|
| 43 |
create_testfile(filename_rsa_clearsign_file_nopassphrase); |
|---|
| 44 |
create_testfile(filename_rsa_clearsign_file_passphrase); |
|---|
| 45 |
create_testfile(filename_rsa_clearsign_buf_nopassphrase); |
|---|
| 46 |
create_testfile(filename_rsa_clearsign_buf_passphrase); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
return 0; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
int clean_suite_rsa_signature(void) |
|---|
| 53 |
{ |
|---|
| 54 |
ops_finish(); |
|---|
| 55 |
|
|---|
| 56 |
reset_vars(); |
|---|
| 57 |
|
|---|
| 58 |
return 0; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
static void test_rsa_signature_clearsign_file(const char *filename, const ops_secret_key_t *skey) |
|---|
| 62 |
{ |
|---|
| 63 |
char cmd[MAXBUF+1]; |
|---|
| 64 |
char myfile[MAXBUF+1]; |
|---|
| 65 |
char signed_file[MAXBUF+1]; |
|---|
| 66 |
int rtn=0; |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
snprintf(myfile,sizeof myfile,"%s/%s",dir,filename); |
|---|
| 70 |
snprintf(signed_file,sizeof signed_file,"%s.asc",myfile); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
ops_sign_file_as_cleartext(myfile,skey); |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
{ |
|---|
| 82 |
int fd=0; |
|---|
| 83 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 84 |
validate_data_cb_arg_t validate_arg; |
|---|
| 85 |
ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); |
|---|
| 86 |
int rtn=0; |
|---|
| 87 |
|
|---|
| 88 |
if (debug) |
|---|
| 89 |
{ |
|---|
| 90 |
fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
#ifdef WIN32 |
|---|
| 95 |
fd=open(signed_file,O_RDONLY | O_BINARY); |
|---|
| 96 |
#else |
|---|
| 97 |
fd=open(signed_file,O_RDONLY); |
|---|
| 98 |
#endif |
|---|
| 99 |
if(fd < 0) |
|---|
| 100 |
{ |
|---|
| 101 |
perror(signed_file); |
|---|
| 102 |
exit(2); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
pinfo=ops_parse_info_new(); |
|---|
| 108 |
|
|---|
| 109 |
memset(&validate_arg,'\0',sizeof validate_arg); |
|---|
| 110 |
validate_arg.result=result; |
|---|
| 111 |
validate_arg.keyring=&pub_keyring; |
|---|
| 112 |
validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); |
|---|
| 113 |
|
|---|
| 114 |
ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); |
|---|
| 115 |
ops_parse_cb_set(pinfo,callback_verify,&validate_arg); |
|---|
| 116 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 117 |
pinfo->rinfo.accumulate=ops_true; |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
rtn=ops_parse(pinfo); |
|---|
| 126 |
ops_print_errors(ops_parse_info_get_errors(pinfo)); |
|---|
| 127 |
CU_ASSERT(rtn==1); |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 132 |
|
|---|
| 133 |
ops_parse_info_delete(pinfo); |
|---|
| 134 |
|
|---|
| 135 |
close(fd); |
|---|
| 136 |
ops_validate_result_free(result); |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
{ |
|---|
| 141 |
|
|---|
| 142 |
snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file); |
|---|
| 143 |
rtn=system(cmd); |
|---|
| 144 |
CU_ASSERT(rtn==0); |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
static void test_rsa_signature_clearsign_buf(const char *filename, const ops_secret_key_t *skey) |
|---|
| 149 |
{ |
|---|
| 150 |
char cmd[MAXBUF+1]; |
|---|
| 151 |
char myfile[MAXBUF+1]; |
|---|
| 152 |
char signed_file[MAXBUF+1]; |
|---|
| 153 |
int rtn=0; |
|---|
| 154 |
ops_memory_t *input=NULL; |
|---|
| 155 |
ops_memory_t *output=NULL; |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
snprintf(myfile,sizeof myfile,"%s/%s",dir,filename); |
|---|
| 162 |
snprintf(signed_file,sizeof signed_file,"%s.asc",myfile); |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
input=ops_write_buf_from_file(myfile); |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
ops_sign_buf_as_cleartext((const char *)ops_memory_get_data(input),ops_memory_get_length(input),&output,skey); |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
ops_write_file_from_buf(signed_file, (const char*)ops_memory_get_data(output),ops_memory_get_length(output)); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
{ |
|---|
| 180 |
int fd=0; |
|---|
| 181 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 182 |
validate_data_cb_arg_t validate_arg; |
|---|
| 183 |
ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); |
|---|
| 184 |
|
|---|
| 185 |
int rtn=0; |
|---|
| 186 |
|
|---|
| 187 |
if (debug) |
|---|
| 188 |
{ |
|---|
| 189 |
fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
#ifdef WIN32 |
|---|
| 194 |
fd=open(signed_file,O_RDONLY | O_BINARY); |
|---|
| 195 |
#else |
|---|
| 196 |
fd=open(signed_file,O_RDONLY); |
|---|
| 197 |
#endif |
|---|
| 198 |
if(fd < 0) |
|---|
| 199 |
{ |
|---|
| 200 |
perror(signed_file); |
|---|
| 201 |
exit(2); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
pinfo=ops_parse_info_new(); |
|---|
| 207 |
|
|---|
| 208 |
memset(&validate_arg,'\0',sizeof validate_arg); |
|---|
| 209 |
validate_arg.result=result; |
|---|
| 210 |
validate_arg.keyring=&pub_keyring; |
|---|
| 211 |
validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); |
|---|
| 212 |
|
|---|
| 213 |
ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); |
|---|
| 214 |
ops_parse_cb_set(pinfo,callback_verify,&validate_arg); |
|---|
| 215 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 216 |
pinfo->rinfo.accumulate=ops_true; |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
rtn=ops_parse(pinfo); |
|---|
| 225 |
ops_print_errors(ops_parse_info_get_errors(pinfo)); |
|---|
| 226 |
CU_ASSERT(rtn==1); |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 231 |
|
|---|
| 232 |
ops_parse_info_delete(pinfo); |
|---|
| 233 |
|
|---|
| 234 |
close(fd); |
|---|
| 235 |
ops_validate_result_free(result); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
{ |
|---|
| 240 |
|
|---|
| 241 |
snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file); |
|---|
| 242 |
rtn=system(cmd); |
|---|
| 243 |
CU_ASSERT(rtn==0); |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
static void test_rsa_signature_sign(const int use_armour, const char *filename, const ops_secret_key_t *skey) |
|---|
| 248 |
{ |
|---|
| 249 |
char cmd[MAXBUF+1]; |
|---|
| 250 |
char myfile[MAXBUF+1]; |
|---|
| 251 |
char signed_file[MAXBUF+1]; |
|---|
| 252 |
char *suffix= use_armour ? "asc" : "ops"; |
|---|
| 253 |
int rtn=0; |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
snprintf(myfile,sizeof myfile,"%s/%s",dir,filename); |
|---|
| 257 |
snprintf(signed_file,sizeof signed_file,"%s.%s",myfile,suffix); |
|---|
| 258 |
|
|---|
| 259 |
ops_sign_file(myfile, signed_file, skey, use_armour); |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
{ |
|---|
| 268 |
int fd=0; |
|---|
| 269 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 270 |
validate_data_cb_arg_t validate_arg; |
|---|
| 271 |
ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));; |
|---|
| 272 |
int rtn=0; |
|---|
| 273 |
|
|---|
| 274 |
if (debug) |
|---|
| 275 |
{ |
|---|
| 276 |
fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
#ifdef WIN32 |
|---|
| 281 |
fd=open(signed_file,O_RDONLY | O_BINARY); |
|---|
| 282 |
#else |
|---|
| 283 |
fd=open(signed_file,O_RDONLY); |
|---|
| 284 |
#endif |
|---|
| 285 |
if(fd < 0) |
|---|
| 286 |
{ |
|---|
| 287 |
perror(signed_file); |
|---|
| 288 |
exit(2); |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
pinfo=ops_parse_info_new(); |
|---|
| 294 |
|
|---|
| 295 |
memset(&validate_arg,'\0',sizeof validate_arg); |
|---|
| 296 |
validate_arg.result=result; |
|---|
| 297 |
validate_arg.keyring=&pub_keyring; |
|---|
| 298 |
validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); |
|---|
| 299 |
|
|---|
| 300 |
ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); |
|---|
| 301 |
ops_parse_cb_set(pinfo,callback_verify,&validate_arg); |
|---|
| 302 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 303 |
pinfo->rinfo.accumulate=ops_true; |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
if (use_armour) |
|---|
| 308 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
rtn=ops_parse_and_print_errors(pinfo); |
|---|
| 313 |
CU_ASSERT(rtn==1); |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
if (use_armour) |
|---|
| 317 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 318 |
|
|---|
| 319 |
ops_parse_info_delete(pinfo); |
|---|
| 320 |
|
|---|
| 321 |
close(fd); |
|---|
| 322 |
ops_validate_result_free(result); |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
{ |
|---|
| 327 |
|
|---|
| 328 |
snprintf(cmd,sizeof cmd,"%s --verify %s", gpgcmd, signed_file); |
|---|
| 329 |
rtn=system(cmd); |
|---|
| 330 |
CU_ASSERT(rtn==0); |
|---|
| 331 |
} |
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
static void test_rsa_signature_noarmour_nopassphrase(void) |
|---|
| 335 |
{ |
|---|
| 336 |
int armour=0; |
|---|
| 337 |
assert(pub_keyring.nkeys); |
|---|
| 338 |
test_rsa_signature_sign(armour,filename_rsa_noarmour_nopassphrase, alpha_skey); |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
static void test_rsa_signature_noarmour_passphrase(void) |
|---|
| 342 |
{ |
|---|
| 343 |
int armour=0; |
|---|
| 344 |
assert(pub_keyring.nkeys); |
|---|
| 345 |
test_rsa_signature_sign(armour,filename_rsa_noarmour_passphrase, bravo_skey); |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
static void test_rsa_signature_armour_nopassphrase(void) |
|---|
| 349 |
{ |
|---|
| 350 |
int armour=1; |
|---|
| 351 |
assert(pub_keyring.nkeys); |
|---|
| 352 |
test_rsa_signature_sign(armour,filename_rsa_armour_nopassphrase, alpha_skey); |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
static void test_rsa_signature_armour_passphrase(void) |
|---|
| 356 |
{ |
|---|
| 357 |
int armour=1; |
|---|
| 358 |
assert(pub_keyring.nkeys); |
|---|
| 359 |
test_rsa_signature_sign(armour,filename_rsa_armour_passphrase, bravo_skey); |
|---|
| 360 |
} |
|---|
| 361 |
|
|---|
| 362 |
static void test_rsa_signature_clearsign_file_nopassphrase(void) |
|---|
| 363 |
{ |
|---|
| 364 |
assert(pub_keyring.nkeys); |
|---|
| 365 |
test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase, alpha_skey); |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
static void test_rsa_signature_clearsign_file_passphrase(void) |
|---|
| 369 |
{ |
|---|
| 370 |
assert(pub_keyring.nkeys); |
|---|
| 371 |
test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase, bravo_skey); |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
static void test_rsa_signature_clearsign_buf_nopassphrase(void) |
|---|
| 375 |
{ |
|---|
| 376 |
assert(pub_keyring.nkeys); |
|---|
| 377 |
test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase, alpha_skey); |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
static void test_rsa_signature_clearsign_buf_passphrase(void) |
|---|
| 381 |
{ |
|---|
| 382 |
assert(pub_keyring.nkeys); |
|---|
| 383 |
test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase, bravo_skey); |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
static void test_todo(void) |
|---|
| 387 |
{ |
|---|
| 388 |
CU_FAIL("Test TODO: Test large files"); |
|---|
| 389 |
CU_FAIL("Test TODO: Sign with V3 signature?"); |
|---|
| 390 |
CU_FAIL("Test TODO: Use other hash algorithms?"); |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
static int add_tests(CU_pSuite suite) |
|---|
| 394 |
{ |
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_signature_noarmour_nopassphrase)) |
|---|
| 398 |
return 0; |
|---|
| 399 |
|
|---|
| 400 |
if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_signature_noarmour_passphrase)) |
|---|
| 401 |
return 0; |
|---|
| 402 |
|
|---|
| 403 |
if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase", test_rsa_signature_clearsign_file_nopassphrase)) |
|---|
| 404 |
return 0; |
|---|
| 405 |
|
|---|
| 406 |
if (NULL == CU_add_test(suite, "Clearsigned file, passphrase", test_rsa_signature_clearsign_file_passphrase)) |
|---|
| 407 |
return 0; |
|---|
| 408 |
|
|---|
| 409 |
if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase", test_rsa_signature_clearsign_buf_nopassphrase)) |
|---|
| 410 |
return 0; |
|---|
| 411 |
|
|---|
| 412 |
if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase", test_rsa_signature_clearsign_buf_passphrase)) |
|---|
| 413 |
return 0; |
|---|
| 414 |
|
|---|
| 415 |
if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_signature_armour_nopassphrase)) |
|---|
| 416 |
return 0; |
|---|
| 417 |
|
|---|
| 418 |
if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_signature_armour_passphrase)) |
|---|
| 419 |
return 0; |
|---|
| 420 |
|
|---|
| 421 |
if (NULL == CU_add_test(suite, "Tests to be implemented", test_todo)) |
|---|
| 422 |
return 0; |
|---|
| 423 |
|
|---|
| 424 |
return 1; |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
CU_pSuite suite_rsa_signature() |
|---|
| 428 |
{ |
|---|
| 429 |
CU_pSuite suite = NULL; |
|---|
| 430 |
|
|---|
| 431 |
suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, clean_suite_rsa_signature); |
|---|
| 432 |
if (!suite) |
|---|
| 433 |
return NULL; |
|---|
| 434 |
|
|---|
| 435 |
if (!add_tests(suite)) |
|---|
| 436 |
return NULL; |
|---|
| 437 |
|
|---|
| 438 |
return suite; |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|