Changeset 607
- Timestamp:
- 08/28/08 17:07:03
- Files:
-
- openpgpsdk/trunk/src/lib/reader_armoured.c (modified) (20 diffs)
- openpgpsdk/trunk/tests/test_rsa_decrypt.c (modified) (1 diff)
- openpgpsdk/trunk/tests/test_rsa_verify.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/src/lib/reader_armoured.c
r574 r607 24 24 */ 25 25 26 #include <openpgpsdk/errors.h> 26 27 #include <openpgpsdk/callback.h> 27 28 #include <openpgpsdk/configure.h> … … 251 252 { 252 253 free(hash); 253 ERR(cbinfo,"Unknown hash algorithm",OPS_E_R_BAD_FORMAT);254 OPS_ERROR_1(errors,OPS_E_R_BAD_FORMAT,"Unknown hash algorithm '%s'",hashstr); 254 255 } 255 256 ops_hash_any(hash,alg); … … 277 278 /* then this had better be a trailer! */ 278 279 if(c != '-') 279 ERR(cbinfo,"Bad dash-escaping",OPS_E_R_BAD_FORMAT);280 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad dash-escaping"); 280 281 for(count=2 ; count < 5 ; ++count) 281 282 { … … 283 284 return -1; 284 285 if(c != '-') 285 ERR(cbinfo,"Bad dash-escaping (2)",OPS_E_R_BAD_FORMAT);286 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad dash-escaping (2)"); 286 287 } 287 288 arg->state=AT_TRAILER_NAME; … … 332 333 } 333 334 335 /* \todo what does a return value of 0 indicate? 1 is good, -1 is bad */ 334 336 static int parse_headers(dearmour_arg_t *arg,ops_error_t **errors, 335 337 ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo) 336 338 { 339 int rtn=1; 337 340 char *buf; 338 341 unsigned nbuf; 339 342 unsigned size; 340 343 ops_boolean_t first=ops_true; 341 ops_parser_content_t content;344 //ops_parser_content_t content; 342 345 343 346 buf=NULL; … … 349 352 350 353 if((c=read_char(arg,errors,rinfo,cbinfo,ops_true)) < 0) 351 return -1; 354 { 355 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Unexpected EOF"); 356 rtn=-1; 357 break; 358 } 352 359 353 360 if(c == '\n') … … 364 371 if(!s) 365 372 if(!first && !arg->allow_headers_without_gap) 373 { 366 374 // then we have seriously malformed armour 367 ERR(cbinfo,"No colon in armour header",OPS_E_R_BAD_FORMAT); 375 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No colon in armour header"); 376 rtn=-1; 377 break; 378 } 368 379 else 369 380 { 370 381 if(first && 371 382 !(arg->allow_headers_without_gap || arg->allow_no_gap)) 372 ERR(cbinfo,"No colon in armour header (2)", 373 OPS_E_R_BAD_FORMAT); 374 // then we have a nasty armoured block with no 375 // headers, not even a blank line. 376 buf[nbuf]='\n'; 377 push_back(arg,(unsigned char *)buf,nbuf+1); 378 break; 383 { 384 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No colon in armour header (2)"); 385 // then we have a nasty armoured block with no 386 // headers, not even a blank line. 387 buf[nbuf]='\n'; 388 push_back(arg,(unsigned char *)buf,nbuf+1); 389 rtn=-1; 390 break; 391 } 379 392 } 380 393 else … … 382 395 *s='\0'; 383 396 if(s[1] != ' ') 384 ERR(cbinfo,"No space in armour header",OPS_E_R_BAD_FORMAT);397 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No space in armour header"); 385 398 add_header(arg,buf,s+2); 386 399 nbuf=0; … … 401 414 free(buf); 402 415 403 return 1;416 return rtn; 404 417 } 405 418 … … 468 481 int n2; 469 482 unsigned long l; 470 ops_parser_content_t content;483 //ops_parser_content_t content; 471 484 int c; 472 485 int ret; … … 476 489 ret=read4(arg,errors,rinfo,cbinfo,&c,&n,&l); 477 490 if(ret < 0) 478 ERR(cbinfo,"Badly formed base64",OPS_E_R_BAD_FORMAT);491 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly formed base64"); 479 492 480 493 if(n == 3) 481 494 { 482 495 if(c != '=') 483 ERR(cbinfo,"Badly terminated base64 (2)",OPS_E_R_BAD_FORMAT);496 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (2)"); 484 497 arg->buffered=2; 485 498 arg->eof64=ops_true; … … 489 502 { 490 503 if(c != '=') 491 ERR(cbinfo,"Badly terminated base64 (3)",OPS_E_R_BAD_FORMAT);504 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (3)"); 492 505 arg->buffered=1; 493 506 arg->eof64=ops_true; … … 495 508 c=read_char(arg,errors,rinfo,cbinfo,ops_false); 496 509 if(c != '=') 497 ERR(cbinfo,"Badly terminated base64",OPS_E_R_BAD_FORMAT);510 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64"); 498 511 } 499 512 else if(n == 0) 500 513 { 501 514 if(!arg->prev_nl || c != '=') 502 ERR(cbinfo,"Badly terminated base64 (4)",OPS_E_R_BAD_FORMAT);515 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (4)"); 503 516 arg->buffered=0; 504 517 } … … 516 529 c=read_and_eat_whitespace(arg,errors,rinfo,cbinfo,ops_true); 517 530 if(c != '\n') 518 ERR(cbinfo,"No newline at base64 end",OPS_E_R_BAD_FORMAT);531 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No newline at base64 end"); 519 532 c=read_char(arg,errors,rinfo,cbinfo,ops_false); 520 533 if(c != '=') 521 ERR(cbinfo,"No checksum at base64 end",OPS_E_R_BAD_FORMAT);534 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No checksum at base64 end"); 522 535 } 523 536 … … 527 540 ret=read4(arg,errors,rinfo,cbinfo,&c,&n,&arg->read_checksum); 528 541 if(ret < 0 || n != 4) 529 ERR(cbinfo,"Error in checksum",OPS_E_R_BAD_FORMAT);542 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Error in checksum"); 530 543 c=read_char(arg,errors,rinfo,cbinfo,ops_true); 531 544 if(arg->allow_trailing_whitespace) 532 545 c=eat_whitespace(c,arg,errors,rinfo,cbinfo,ops_true); 533 546 if(c != '\n') 534 ERR(cbinfo,"Badly terminated checksum",OPS_E_R_BAD_FORMAT);547 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated checksum"); 535 548 c=read_char(arg,errors,rinfo,cbinfo,ops_false); 536 549 if(c != '-') 537 ERR(cbinfo,"Bad base64 trailer (2)",OPS_E_R_BAD_FORMAT);550 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad base64 trailer (2)"); 538 551 } 539 552 … … 542 555 for(n=0 ; n < 4 ; ++n) 543 556 if(read_char(arg,errors,rinfo,cbinfo,ops_false) != '-') 544 ERR(cbinfo,"Bad base64 trailer",OPS_E_R_BAD_FORMAT);557 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad base64 trailer"); 545 558 arg->eof64=ops_true; 546 559 } … … 558 571 559 572 if(arg->eof64 && arg->read_checksum != arg->checksum) 560 ERR(cbinfo,"Checksum mismatch",OPS_E_R_BAD_FORMAT);573 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Checksum mismatch"); 561 574 562 575 return 1; … … 726 739 } 727 740 /* then I guess this wasn't a proper trailer */ 728 ERR(cbinfo,"Bad ASCII armour trailer",OPS_E_R_BAD_FORMAT);741 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer"); 729 742 break; 730 743 … … 739 752 if(c != '-') 740 753 /* wasn't a trailer after all */ 741 ERR(cbinfo,"Bad ASCII armour trailer (2)", 742 OPS_E_R_BAD_FORMAT); 754 OPS_ERROR(errors, OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer (2)"); 743 755 } 744 756 … … 752 764 if(c != '\n') 753 765 /* wasn't a trailer line after all */ 754 ERR(cbinfo,"Bad ASCII armour trailer (3)",OPS_E_R_BAD_FORMAT);766 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer (3)"); 755 767 756 768 if(!strncmp(buf,"BEGIN ",6)) openpgpsdk/trunk/tests/test_rsa_decrypt.c
r574 r607 199 199 for (compress_alg=0; compress_alg<n_compress_algos; compress_alg++) 200 200 { 201 for (compress_lvl=0; compress_lvl< MAX_COMPRESS_LEVEL; compress_lvl++)201 for (compress_lvl=0; compress_lvl<=MAX_COMPRESS_LEVEL; compress_lvl++) 202 202 { 203 /* only need to check every compression level if we're debugging */ 204 if (compress_lvl>0 && compress_lvl < MAX_COMPRESS_LEVEL) 205 continue; 203 206 for (armour=0; armour<=1; armour++) 204 207 { openpgpsdk/trunk/tests/test_rsa_verify.c
r572 r607 49 49 static char *filename_rsa_clearsign_passphrase="gpg_rsa_clearsign_passphrase.txt"; 50 50 static char *filename_rsa_clearsign_fail_bad_sig="gpg_rsa_clearsign_fail_bad_sig.txt"; 51 51 52 static char *filename_rsa_noarmour_compress_base="gpg_rsa_sign_noarmour_compress"; 52 53 static char *filename_rsa_armour_compress_base="gpg_rsa_sign_armour_compress"; … … 55 56 56 57 static char *filename_rsa_hash_md5="gpg_rsa_hash_md5.txt"; 58 59 static int num_malformed=0; 57 60 58 61 typedef ops_parse_cb_return_t (*ops_callback)(const ops_parser_content_t *, ops_parse_cb_info_t *); … … 62 65 */ 63 66 67 static void make_filename_malformed(char* filename, int maxlen, const int i) 68 { 69 snprintf(filename,maxlen,"malformed_%d.txt",i); 70 } 71 72 static void create_malformed_testfiles() 73 { 74 int i=0; 75 int fd=0; 76 char * malformed[]={ 77 // no signature 78 "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\n-----END PGP SIGNATURE-----\n", 79 // no signature and early EOF 80 "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\n-----END PGP SIGNATURE-----", 81 // early EOF 82 "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\n", 83 // no signature 84 "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: -----END PGP SIGNATURE-----GnuPG v1.4.6 (GNU/Linux)\n" 85 }; 86 num_malformed=sizeof (malformed)/sizeof(char *); 87 for (i=0; i<num_malformed; i++) 88 { 89 char fullname[MAXBUF]; 90 char filename[MAXBUF]; 91 make_filename_malformed(filename,MAXBUF,i); 92 snprintf(fullname,MAXBUF,"%s/%s.asc",dir,filename); 93 if ((fd=open(fullname,O_WRONLY | O_CREAT, 0600)) < 0) 94 { 95 fprintf(stderr,"create_malformed_testfiles: cannot open file %s for writing\n", fullname); 96 return; 97 } 98 write(fd,malformed[i],strlen(malformed[i])); 99 close(fd); 100 } 101 } 102 64 103 int init_suite_rsa_verify(void) 65 104 { … … 77 116 create_small_testfile(filename_rsa_noarmour_passphrase); 78 117 create_small_testfile(filename_rsa_noarmour_fail_bad_sig); 118 119 create_malformed_testfiles(); 79 120 80 121 // Now sign the test files with GPG … … 441 482 } 442 483 484 static void test_rsa_verify_clearsign_fail_malformed_msg(void) 485 { 486 int i=0; 487 int armour=1; 488 assert(pub_keyring.nkeys); 489 490 for (i=0; i<num_malformed; i++) 491 { 492 char filename[MAXBUF]; 493 make_filename_malformed(filename,MAXBUF,i); 494 test_rsa_verify_fail(armour,filename,NULL,OPS_E_R_BAD_FORMAT); 495 } 496 } 497 443 498 CU_pSuite suite_rsa_verify() 444 499 { … … 480 535 return NULL; 481 536 537 if (NULL == CU_add_test(suite, "Clearsign: should fail on malformed message", test_rsa_verify_clearsign_fail_malformed_msg)) 538 return NULL; 539 482 540 return suite; 483 541 }
