Changeset 34
- Timestamp:
- 02/22/05 15:55:54
- Files:
-
- openpgpsdk/trunk/src/packet-parse.c (modified) (38 diffs)
- openpgpsdk/trunk/src/packet.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/src/packet-parse.c
r33 r34 12 12 #include <string.h> 13 13 14 typedef struct ops_region 15 { 16 struct ops_region *parent; 17 unsigned length; 18 unsigned length_read; 19 } ops_region_t; 20 21 static void init_subregion(ops_region_t *subregion,ops_region_t *region) 22 { 23 memset(subregion,'\0',sizeof *subregion); 24 subregion->parent=region; 25 } 26 14 27 #define CB(t,pc) do { (pc)->tag=(t); cb(pc); } while(0) 15 28 #define C content.content … … 88 101 */ 89 102 static int limited_read(unsigned char *dest,unsigned length, 90 ops_ ptag_t *ptag,ops_packet_reader_t *reader,103 ops_region_t *region,ops_packet_reader_t *reader, 91 104 ops_packet_parse_callback_t *cb) 92 105 { 93 106 ops_parser_content_t content; 94 107 95 if( ptag->length_read+length > ptag->length)108 if(region->length_read+length > region->length) 96 109 ERR("Not enough data left"); 97 110 … … 99 112 ERR("Read failed"); 100 113 101 ptag->length_read+=length; 114 do 115 { 116 region->length_read+=length; 117 assert(!region->parent || region->length <= region->parent->length); 118 } 119 while((region=region->parent)); 102 120 103 121 return 1; … … 116 134 * \return 1 on success, 0 on error (calls the cb with #OPS_PARSER_ERROR in #limited_read). 117 135 */ 118 static int limited_skip(unsigned length,ops_ ptag_t *ptag,136 static int limited_skip(unsigned length,ops_region_t *region, 119 137 ops_packet_reader_t *reader, 120 138 ops_packet_parse_callback_t *cb) … … 125 143 { 126 144 int n=length%8192; 127 if(!limited_read(buf,n, ptag,reader,cb))145 if(!limited_read(buf,n,region,reader,cb)) 128 146 return 0; 129 147 length-=n; … … 149 167 */ 150 168 static int limited_read_scalar(unsigned *dest,unsigned length, 151 ops_ptag_t *ptag,ops_packet_reader_t *reader, 169 ops_region_t *region, 170 ops_packet_reader_t *reader, 152 171 ops_packet_parse_callback_t *cb) 153 172 { … … 156 175 int n; 157 176 158 if(!limited_read(c,length, ptag,reader,cb))177 if(!limited_read(c,length,region,reader,cb)) 159 178 return 0; 160 179 … … 183 202 * \see RFC2440bis-12 3.5 184 203 */ 185 static int limited_read_time(time_t *dest,ops_ ptag_t *ptag,204 static int limited_read_time(time_t *dest,ops_region_t *region, 186 205 ops_packet_reader_t *reader, 187 206 ops_packet_parse_callback_t *cb) 188 207 { 189 return limited_read_scalar((unsigned *)dest,4, ptag,reader,cb);208 return limited_read_scalar((unsigned *)dest,4,region,reader,cb); 190 209 } 191 210 … … 212 231 * \see RFC2440bis-12 3.2 213 232 */ 214 static int limited_read_mpi(BIGNUM **pbn,ops_ ptag_t *ptag,233 static int limited_read_mpi(BIGNUM **pbn,ops_region_t *region, 215 234 ops_packet_reader_t *reader, 216 235 ops_packet_parse_callback_t *cb) … … 223 242 ops_parser_content_t content; 224 243 225 if(!limited_read_scalar(&length,2, ptag,reader,cb))244 if(!limited_read_scalar(&length,2,region,reader,cb)) 226 245 return 0; 227 246 … … 232 251 233 252 assert(length <= 8192); 234 if(!limited_read(buf,length, ptag,reader,cb))253 if(!limited_read(buf,length,region,reader,cb)) 235 254 return 0; 236 255 … … 259 278 * \see ops_ptag_t 260 279 */ 261 static int limited_read_new_length(unsigned *length,ops_ ptag_t *ptag,280 static int limited_read_new_length(unsigned *length,ops_region_t *region, 262 281 ops_packet_reader_t *reader, 263 282 ops_packet_parse_callback_t *cb) … … 265 284 unsigned char c[1]; 266 285 267 if(!limited_read(c,1, ptag,reader,cb))286 if(!limited_read(c,1,region,reader,cb)) 268 287 return 0; 269 288 if(c[0] < 192) … … 276 295 unsigned t=(c[0]-192) << 8; 277 296 278 if(!limited_read(c,1, ptag,reader,cb))297 if(!limited_read(c,1,region,reader,cb)) 279 298 return 0; 280 299 *length=t+c[1]+192; 281 300 return 1; 282 301 } 283 return limited_read_scalar(length,4, ptag,reader,cb);302 return limited_read_scalar(length,4,region,reader,cb); 284 303 } 285 304 … … 297 316 * \see RFC2440bis-12 5.5.2 298 317 */ 299 static int parse_public_key(ops_ptag_t *ptag,ops_packet_reader_t *reader, 300 ops_packet_parse_callback_t *cb) 318 static int parse_public_key(ops_content_tag_t tag,ops_region_t *region, 319 ops_packet_reader_t *reader, 320 ops_packet_parse_callback_t *cb) 301 321 { 302 322 ops_parser_content_t content; 303 323 unsigned char c[1]; 304 324 305 assert ( ptag->length_read == 0); /* We should not have read anything so far */306 307 if(!limited_read(c,1, ptag,reader,cb))325 assert (region->length_read == 0); /* We should not have read anything so far */ 326 327 if(!limited_read(c,1,region,reader,cb)) 308 328 return 0; 309 329 C.public_key.version=c[0]; … … 315 335 ERR1("Bad public key version (0x%02x)",C.public_key.version); 316 336 317 if(!limited_read_time(&C.public_key.creation_time, ptag,reader,cb))337 if(!limited_read_time(&C.public_key.creation_time,region,reader,cb)) 318 338 return 0; 319 339 320 340 C.public_key.days_valid=0; 321 341 if(C.public_key.version == 3 322 && !limited_read_scalar(&C.public_key.days_valid,2, ptag,reader,342 && !limited_read_scalar(&C.public_key.days_valid,2,region,reader, 323 343 cb)) 324 344 return 0; 325 345 326 if(!limited_read(c,1, ptag,reader,cb))346 if(!limited_read(c,1,region,reader,cb)) 327 347 return 0; 328 348 … … 332 352 { 333 353 case OPS_PKA_DSA: 334 if(!limited_read_mpi(&C.public_key.key.dsa.p, ptag,reader,cb)335 || !limited_read_mpi(&C.public_key.key.dsa.q, ptag,reader,cb)336 || !limited_read_mpi(&C.public_key.key.dsa.g, ptag,reader,cb)337 || !limited_read_mpi(&C.public_key.key.dsa.y, ptag,reader,cb))354 if(!limited_read_mpi(&C.public_key.key.dsa.p,region,reader,cb) 355 || !limited_read_mpi(&C.public_key.key.dsa.q,region,reader,cb) 356 || !limited_read_mpi(&C.public_key.key.dsa.g,region,reader,cb) 357 || !limited_read_mpi(&C.public_key.key.dsa.y,region,reader,cb)) 338 358 return 0; 339 359 break; … … 342 362 case OPS_PKA_RSA_ENCRYPT_ONLY: 343 363 case OPS_PKA_RSA_SIGN_ONLY: 344 if(!limited_read_mpi(&C.public_key.key.rsa.n, ptag,reader,cb)345 || !limited_read_mpi(&C.public_key.key.rsa.e, ptag,reader,cb))364 if(!limited_read_mpi(&C.public_key.key.rsa.n,region,reader,cb) 365 || !limited_read_mpi(&C.public_key.key.rsa.e,region,reader,cb)) 346 366 return 0; 347 367 break; 348 368 349 369 case OPS_PKA_ELGAMAL: 350 if(!limited_read_mpi(&C.public_key.key.elgamel.p, ptag,reader,cb)351 || !limited_read_mpi(&C.public_key.key.elgamel.g, ptag,reader,cb)352 || !limited_read_mpi(&C.public_key.key.elgamel.y, ptag,reader,cb))370 if(!limited_read_mpi(&C.public_key.key.elgamel.p,region,reader,cb) 371 || !limited_read_mpi(&C.public_key.key.elgamel.g,region,reader,cb) 372 || !limited_read_mpi(&C.public_key.key.elgamel.y,region,reader,cb)) 353 373 return 0; 354 374 break; … … 358 378 } 359 379 360 if( ptag->length_read != ptag->length)361 ERR1("Unconsumed data (%d)", ptag->length-ptag->length_read);362 363 CB( ptag->content_tag,&content);380 if(region->length_read != region->length) 381 ERR1("Unconsumed data (%d)", region->length-region->length_read); 382 383 CB(tag,&content); 364 384 365 385 return 1; … … 383 403 * \see RFC2440bis-12 5.11 384 404 */ 385 static int parse_user_id(ops_ ptag_t *ptag,ops_packet_reader_t *reader,405 static int parse_user_id(ops_region_t *region,ops_packet_reader_t *reader, 386 406 ops_packet_parse_callback_t *cb) 387 407 { 388 408 ops_parser_content_t content; 389 409 390 assert ( ptag->length_read == 0); /* We should not have read anything so far */391 392 assert( ptag->length);393 C.user_id.user_id=malloc( ptag->length+1); /* XXX should we not like check malloc's return value? */394 if(!limited_read(C.user_id.user_id, ptag->length,ptag,reader,cb))395 return 0; 396 C.user_id.user_id[ ptag->length] = 0; /* terminate the string */410 assert (region->length_read == 0); /* We should not have read anything so far */ 411 412 assert(region->length); 413 C.user_id.user_id=malloc(region->length+1); /* XXX should we not like check malloc's return value? */ 414 if(!limited_read(C.user_id.user_id,region->length,region,reader,cb)) 415 return 0; 416 C.user_id.user_id[region->length] = 0; /* terminate the string */ 397 417 398 418 CB(OPS_PTAG_CT_USER_ID,&content); … … 414 434 * \see RFC2440bis-12 5.2.2 415 435 */ 416 static int parse_v3_signature(ops_ ptag_t *ptag,ops_packet_reader_t *reader,436 static int parse_v3_signature(ops_region_t *region,ops_packet_reader_t *reader, 417 437 ops_packet_parse_callback_t *cb) 418 438 { … … 420 440 ops_parser_content_t content; 421 441 422 assert (ptag->length_read == 0); /* We should not have read anything so far */442 assert(region->length_read == 0); /* We should not have read anything so far */ 423 443 424 444 C.signature.version=OPS_SIG_V3; 425 445 426 446 /* hash info length */ 427 if(!limited_read(c,1, ptag,reader,cb))447 if(!limited_read(c,1,region,reader,cb)) 428 448 return 0; 429 449 if(c[0] != 5) 430 450 ERR("bad hash info length"); 431 451 432 if(!limited_read(c,1, ptag,reader,cb))452 if(!limited_read(c,1,region,reader,cb)) 433 453 return 0; 434 454 C.signature.type=c[0]; 435 455 /* XXX: check signature type */ 436 456 437 if(!limited_read_time(&C.signature.creation_time, ptag,reader,cb))438 return 0; 439 440 if(!limited_read(C.signature.signer_id,8, ptag,reader,cb))441 return 0; 442 443 if(!limited_read(c,1, ptag,reader,cb))457 if(!limited_read_time(&C.signature.creation_time,region,reader,cb)) 458 return 0; 459 460 if(!limited_read(C.signature.signer_id,8,region,reader,cb)) 461 return 0; 462 463 if(!limited_read(c,1,region,reader,cb)) 444 464 return 0; 445 465 C.signature.key_algorithm=c[0]; 446 466 /* XXX: check algorithm */ 447 467 448 if(!limited_read(c,1, ptag,reader,cb))468 if(!limited_read(c,1,region,reader,cb)) 449 469 return 0; 450 470 C.signature.hash_algorithm=c[0]; 451 471 /* XXX: check algorithm */ 452 472 453 if(!limited_read(C.signature.hash2,2, ptag,reader,cb))473 if(!limited_read(C.signature.hash2,2,region,reader,cb)) 454 474 return 0; 455 475 … … 457 477 { 458 478 case OPS_PKA_RSA: 459 if(!limited_read_mpi(&C.signature.signature.rsa.sig, ptag,reader,cb))479 if(!limited_read_mpi(&C.signature.signature.rsa.sig,region,reader,cb)) 460 480 return 0; 461 481 break; 462 482 463 483 case OPS_PKA_DSA: 464 if(!limited_read_mpi(&C.signature.signature.dsa.r, ptag,reader,cb)465 || !limited_read_mpi(&C.signature.signature.dsa.s, ptag,reader,cb))484 if(!limited_read_mpi(&C.signature.signature.dsa.r,region,reader,cb) 485 || !limited_read_mpi(&C.signature.signature.dsa.s,region,reader,cb)) 466 486 return 0; 467 487 break; … … 471 491 } 472 492 473 if( ptag->length_read != ptag->length)474 ERR1("Unconsumed data (%d)", ptag->length-ptag->length_read);493 if(region->length_read != region->length) 494 ERR1("Unconsumed data (%d)",region->length-region->length_read); 475 495 476 496 CB(OPS_PTAG_CT_SIGNATURE,&content); … … 495 515 * \see RFC2440bis-12 5.2.3 496 516 */ 497 static int parse_one_signature_subpacket(ops_ ptag_t *ptag,517 static int parse_one_signature_subpacket(ops_region_t *region, 498 518 ops_packet_reader_t *reader, 499 519 ops_packet_parse_callback_t *cb, 500 520 ops_parse_packet_options_t *opt) 501 521 { 502 ops_ ptag_t subptag;522 ops_region_t subregion; 503 523 char c[1]; 504 524 ops_parser_content_t content; 505 525 unsigned t8,t7; 506 526 507 memset(&subptag,'\0',sizeof subptag);508 if(!limited_read_new_length(&sub ptag.length,ptag,reader,cb))509 return 0; 510 511 if(!limited_read(c,1,&sub ptag,reader,cb))527 init_subregion(&subregion,region); 528 if(!limited_read_new_length(&subregion.length,region,reader,cb)) 529 return 0; 530 531 if(!limited_read(c,1,&subregion,reader,cb)) 512 532 return 0; 513 533 … … 517 537 content.critical=c[0] >> 7; 518 538 content.tag=OPS_PTAG_SIGNATURE_SUBPACKET_BASE+(c[0]&0x7f); 539 540 /* Application wants it delivered raw */ 519 541 if(opt->ss_raw[t8]&t7) 520 542 { 521 543 C.ss_raw.tag=content.tag; 522 C.ss_raw.length=sub ptag.length-1;544 C.ss_raw.length=subregion.length-1; 523 545 C.ss_raw.raw=malloc(C.ss_raw.length); 524 if(!limited_read(C.ss_raw.raw,C.ss_raw.length,&subptag,reader,cb)) 525 return 0; 526 ptag->length_read+=subptag.length; 546 if(!limited_read(C.ss_raw.raw,C.ss_raw.length,&subregion,reader,cb)) 547 return 0; 527 548 CB(OPS_PTAG_RAW_SS,&content); 528 549 return 1; 529 550 } 551 552 /* Application doesn't want it delivered parsed */ 530 553 if(!(opt->ss_parsed[t8]&t7)) 531 554 { 532 555 if(content.critical) 533 556 ERR1("Critical signature subpacket ignored (%d)",c[0]&0x7f); 534 if(!limited_skip(subptag.length-1,&subptag,reader,cb)) 535 return 0; 536 printf("skipped %d length %d\n",c[0]&0x7f,subptag.length); 537 ptag->length_read+=subptag.length; 557 if(!limited_skip(subregion.length-1,&subregion,reader,cb)) 558 return 0; 559 printf("skipped %d length %d\n",c[0]&0x7f,subregion.length); 538 560 return 1; 539 561 } … … 543 565 case OPS_PTAG_SS_CREATION_TIME: 544 566 case OPS_PTAG_SS_EXPIRATION_TIME: 545 if(!limited_read_time(&C.ss_time.time,&sub ptag,reader,cb))567 if(!limited_read_time(&C.ss_time.time,&subregion,reader,cb)) 546 568 return 0; 547 569 break; 548 570 549 571 case OPS_PTAG_SS_TRUST: 550 if(!limited_read(&C.ss_trust.level,1,&sub ptag,reader,cb)551 || !limited_read(&C.ss_trust.level,1,&sub ptag,reader,cb))572 if(!limited_read(&C.ss_trust.level,1,&subregion,reader,cb) 573 || !limited_read(&C.ss_trust.level,1,&subregion,reader,cb)) 552 574 return 0; 553 575 break; … … 557 579 } 558 580 559 if(sub ptag.length_read != subptag.length)560 ERR1("Unconsumed data (%d)", sub ptag.length-subptag.length_read);581 if(subregion.length_read != subregion.length) 582 ERR1("Unconsumed data (%d)", subregion.length-subregion.length_read); 561 583 562 ptag->length_read+=subptag.length;563 584 cb(&content); 564 585 … … 581 602 * \see RFC2440bis-12 5.2.3 582 603 */ 583 static int parse_signature_subpackets(ops_ ptag_t *ptag,604 static int parse_signature_subpackets(ops_region_t *region, 584 605 ops_packet_reader_t *reader, 585 606 ops_packet_parse_callback_t *cb, 586 607 ops_parse_packet_options_t *opt) 587 608 { 588 ops_ptag_t subptag; 589 590 memset(&subptag,'\0',sizeof subptag); 591 if(!limited_read_scalar(&subptag.length,2,ptag,reader,cb)) 592 return 0; 593 594 while(subptag.length_read < subptag.length) 595 if(!parse_one_signature_subpacket(&subptag,reader,cb,opt)) 596 { 597 ptag->length_read+=subptag.length_read; 598 return 0; 599 } 600 601 assert(subptag.length_read == subptag.length); /* XXX: this should not be an assert but a parse error. It's not 609 ops_region_t subregion; 610 611 init_subregion(&subregion,region); 612 if(!limited_read_scalar(&subregion.length,2,region,reader,cb)) 613 return 0; 614 615 while(subregion.length_read < subregion.length) 616 if(!parse_one_signature_subpacket(&subregion,reader,cb,opt)) 617 return 0; 618 619 assert(subregion.length_read == subregion.length); /* XXX: this should not be an assert but a parse error. It's not 602 620 our fault if the packet is inconsistent with itself. */ 603 621 604 ptag->length_read+=subptag.length_read;605 606 622 return 1; 607 623 } … … 620 636 * \see RFC2440bis-12 5.2.3 621 637 */ 622 static int parse_v4_signature(ops_ ptag_t *ptag,ops_packet_reader_t *reader,638 static int parse_v4_signature(ops_region_t *region,ops_packet_reader_t *reader, 623 639 ops_packet_parse_callback_t *cb, 624 640 ops_parse_packet_options_t *opt) … … 629 645 C.signature.version=OPS_SIG_V4; 630 646 631 if(!limited_read(c,1, ptag,reader,cb))647 if(!limited_read(c,1,region,reader,cb)) 632 648 return 0; 633 649 C.signature.type=c[0]; 634 650 /* XXX: check signature type */ 635 651 636 if(!limited_read(c,1, ptag,reader,cb))652 if(!limited_read(c,1,region,reader,cb)) 637 653 return 0; 638 654 C.signature.key_algorithm=c[0]; 639 655 /* XXX: check algorithm */ 640 656 641 if(!limited_read(c,1, ptag,reader,cb))657 if(!limited_read(c,1,region,reader,cb)) 642 658 return 0; 643 659 C.signature.hash_algorithm=c[0]; 644 660 /* XXX: check algorithm */ 645 661 646 if(!parse_signature_subpackets(ptag,reader,cb,opt)) 647 return 0; 648 649 if(!parse_signature_subpackets(ptag,reader,cb,opt)) 650 return 0; 651 652 653 654 if(!limited_read(C.signature.hash2,2,ptag,reader,cb)) 662 if(!parse_signature_subpackets(region,reader,cb,opt)) 663 return 0; 664 665 if(!parse_signature_subpackets(region,reader,cb,opt)) 666 return 0; 667 668 if(!limited_read(C.signature.hash2,2,region,reader,cb)) 655 669 return 0; 656 670 … … 658 672 { 659 673 case OPS_PKA_RSA: 660 if(!limited_read_mpi(&C.signature.signature.rsa.sig, ptag,reader,cb))674 if(!limited_read_mpi(&C.signature.signature.rsa.sig,region,reader,cb)) 661 675 return 0; 662 676 break; 663 677 664 678 case OPS_PKA_DSA: 665 if(!limited_read_mpi(&C.signature.signature.dsa.r, ptag,reader,cb)666 || !limited_read_mpi(&C.signature.signature.dsa.s, ptag,reader,cb))679 if(!limited_read_mpi(&C.signature.signature.dsa.r,region,reader,cb) 680 || !limited_read_mpi(&C.signature.signature.dsa.s,region,reader,cb)) 667 681 return 0; 668 682 break; … … 672 686 } 673 687 674 if( ptag->length_read != ptag->length)675 ERR1("Unconsumed data (%d)", ptag->length-ptag->length_read);688 if(region->length_read != region->length) 689 ERR1("Unconsumed data (%d)",region->length-region->length_read); 676 690 677 691 CB(OPS_PTAG_CT_SIGNATURE,&content); … … 691 705 * \return 1 on success, 0 on error 692 706 */ 693 static int parse_signature(ops_ ptag_t *ptag,ops_packet_reader_t *reader,707 static int parse_signature(ops_region_t *region,ops_packet_reader_t *reader, 694 708 ops_packet_parse_callback_t *cb, 695 709 ops_parse_packet_options_t *opt) … … 698 712 ops_parser_content_t content; 699 713 700 if(!limited_read(c,1, ptag,reader,cb))714 if(!limited_read(c,1,region,reader,cb)) 701 715 return 0; 702 716 … … 704 718 /* XXX: are there v2 signatures? - Peter */ 705 719 if(c[0] == 2 || c[0] == 3) 706 return parse_v3_signature( ptag,reader,cb);720 return parse_v3_signature(region,reader,cb); 707 721 else if(c[0] == 4) 708 return parse_v4_signature( ptag,reader,cb,opt);722 return parse_v4_signature(region,reader,cb,opt); 709 723 ERR1("Bad signature version (%d)",c[0]); 710 724 } … … 728 742 ops_parser_content_t content; 729 743 int r; 744 ops_region_t region; 730 745 731 746 ret=reader(ptag,1); … … 773 788 } 774 789 775 C.ptag.length_read=0;776 790 CB(OPS_PARSER_PTAG,&content); 777 791 792 init_subregion(®ion,NULL); 793 region.length=C.ptag.length; 778 794 switch(C.ptag.content_tag) 779 795 { 780 796 case OPS_PTAG_CT_SIGNATURE: 781 r=parse_signature(& C.ptag,reader,cb,opt);797 r=parse_signature(®ion,reader,cb,opt); 782 798 break; 783 799 784 800 case OPS_PTAG_CT_PUBLIC_KEY: 785 801 case OPS_PTAG_CT_PUBLIC_SUBKEY: 786 r=parse_public_key( &C.ptag,reader,cb);802 r=parse_public_key(C.ptag.content_tag,®ion,reader,cb); 787 803 break; 788 804 789 805 case OPS_PTAG_CT_USER_ID: 790 r=parse_user_id(& C.ptag,reader,cb);806 r=parse_user_id(®ion,reader,cb); 791 807 break; 792 808 openpgpsdk/trunk/src/packet.h
r33 r34 158 158 length information, not at the same moment we create the packet tag structure. 159 159 Only defined if #length_read is set. */ /* XXX: Ben, is this correct? */ 160 unsigned length_read; /*!< How much bytes of this packet we have read so far - for internal use161 only. */160 // unsigned length_read; /*!< How much bytes of this packet we have read so far - for internal use 161 // only. */ 162 162 } ops_ptag_t; 163 163
