Changeset 335
- Timestamp:
- 01/22/06 12:23:40
- Files:
-
- openpgpsdk/trunk/examples/decrypt.c (modified) (5 diffs)
- openpgpsdk/trunk/examples/packet-dump.c (modified) (3 diffs)
- openpgpsdk/trunk/include/openpgpsdk/packet-parse.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/packet.h (modified) (5 diffs)
- openpgpsdk/trunk/src/accumulate.c (modified) (1 diff)
- openpgpsdk/trunk/src/packet-parse.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/decrypt.c
r334 r335 1 1 #include <openpgpsdk/packet-parse.h> 2 2 #include <openpgpsdk/util.h> 3 #include <openpgpsdk/keyring.h> 4 #include <openpgpsdk/accumulate.h> 5 #include <openpgpsdk/armour.h> 6 3 7 #include <unistd.h> 4 8 #include <string.h> 5 #include <openpgpsdk/keyring.h>6 9 #include <fcntl.h> 7 #include <openpgpsdk/accumulate.h> 8 #include <openpgpsdk/armour.h> 10 #include <assert.h> 9 11 10 12 static char *pname; … … 14 16 cb_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 15 17 { 16 const ops_parser_content_union_t *content=&content_->content;17 char buffer[1024];18 size_t n;18 // const ops_parser_content_union_t *content=&content_->content; 19 // char buffer[1024]; 20 // size_t n; 19 21 20 22 OPS_USED(cbinfo); … … 23 25 { 24 26 case OPS_PARSER_PTAG: 27 case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: // we get these because we didn't prompt 28 case OPS_PARSER_ERROR_UNKNOWN_TAG: 29 case OPS_PARSER_ERROR_PACKET_CONSUMED: // only happens after another error we've deemed to be OK 30 case OPS_PTAG_CT_SIGNATURE_HEADER: 31 case OPS_PTAG_CT_SIGNATURE_FOOTER: 32 case OPS_PTAG_CT_SIGNATURE: 33 case OPS_PTAG_CT_TRUST: 25 34 break; 26 35 27 36 case OPS_PTAG_CMD_GET_PASSPHRASE: 37 /* 28 38 printf("Passphrase: "); 29 39 fgets(buffer,sizeof buffer,stdin); … … 34 44 strcpy(*content->passphrase,buffer); 35 45 return OPS_KEEP_MEMORY; 46 */ 47 // we don't want to prompt when reading the keyring 48 break; 36 49 37 50 default: 38 51 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 39 52 content_->tag); 53 assert(0); 40 54 exit(1); 41 55 } … … 55 69 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 56 70 content_->tag); 57 exit(1);71 assert(0); 58 72 } 59 73 openpgpsdk/trunk/examples/packet-dump.c
r332 r335 35 35 print_indent(); 36 36 printf("%s=",name); 37 BN_print_fp(stdout,bn); 37 if(bn) 38 BN_print_fp(stdout,bn); 39 else 40 puts("(unset)"); 38 41 printf("\n"); 39 42 } … … 780 783 break; 781 784 785 case OPS_PTAG_CMD_GET_PASSPHRASE: 786 printf(">>> ASKED FOR PASSPHRASE <<<\n"); 787 break; 788 782 789 case OPS_PTAG_CT_SECRET_KEY: 790 case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 783 791 // XXX: fix me 784 print_tagname("SECRET_KEY"); 792 if(content_->tag == OPS_PTAG_CT_SECRET_KEY) 793 print_tagname("SECRET_KEY"); 794 else 795 print_tagname("ENCRYPTED_SECRET_KEY"); 785 796 print_public_key(&content->secret_key.public_key); 786 797 … … 792 803 print_bn("q",content->secret_key.key.rsa.q); 793 804 print_bn("u",content->secret_key.key.rsa.u); 805 break; 806 807 case OPS_PKA_DSA: 808 print_bn("x",content->secret_key.key.dsa.x); 794 809 break; 795 810 openpgpsdk/trunk/include/openpgpsdk/packet-parse.h
r334 r335 92 92 OPS_PARSE_RAW, /*!< Callback Raw */ 93 93 OPS_PARSE_PARSED, /*!< Callback Parsed */ 94 OPS_PARSE_IGNORE /*!< Don't callback */94 OPS_PARSE_IGNORE, /*!< Don't callback */ 95 95 }; 96 96 openpgpsdk/trunk/include/openpgpsdk/packet.h
r334 r335 140 140 OPS_PTAG_CT_MDC =19, /*!< Modification Detection Code Packet */ 141 141 142 OPS_PARSER_ERROR =0x100, /*!< Internal Use: Parser Error */ 143 OPS_PARSER_PTAG =0x101, /*!< Internal Use: The packet is the "Packet Tag" itself - used when 142 OPS_PARSER_PTAG =0x100, /*!< Internal Use: The packet is the "Packet Tag" itself - used when 144 143 callback sends back the PTag. */ 145 OPS_PTAG_RAW_SS =0x102, /*!< Internal Use: content is raw sig subtag */ 146 OPS_PTAG_SS_ALL =0x103, /*!< Internal Use: select all subtags */ 147 OPS_PARSER_PACKET_END =0x104, 148 OPS_PARSER_ERRCODE =0x105, /*! < Internal Use: Parser Error with errcode returned */ 144 OPS_PTAG_RAW_SS =0x101, /*!< Internal Use: content is raw sig subtag */ 145 OPS_PTAG_SS_ALL =0x102, /*!< Internal Use: select all subtags */ 146 OPS_PARSER_PACKET_END =0x103, 149 147 150 148 /* signature subpackets (0x200-2ff) (type+0x200) */ … … 199 197 OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER=0x300+8, 200 198 OPS_PTAG_CT_UNARMOURED_TEXT =0x300+9, 199 OPS_PTAG_CT_ENCRYPTED_SECRET_KEY =0x300+10, // In this case the algorithm specific fields will not be initialised 201 200 202 201 /* commands to the callback */ 203 202 OPS_PTAG_CMD_GET_PASSPHRASE =0x400, 203 204 205 /* Errors */ 206 OPS_PARSER_ERROR =0x500, /*!< Internal Use: Parser Error */ 207 OPS_PARSER_ERRCODE =0x500+1, /*! < Internal Use: Parser Error with errcode returned */ 208 OPS_PARSER_ERROR_UNKNOWN_TAG =0x500+2, 209 OPS_PARSER_ERROR_PACKET_CONSUMED =0x500+3, 204 210 }; 205 211 … … 336 342 } ops_rsa_secret_key_t; 337 343 344 typedef struct 345 { 346 BIGNUM *x; 347 } ops_dsa_secret_key_t; 348 338 349 /** ops_secret_key_union_t 339 350 */ … … 341 352 { 342 353 ops_rsa_secret_key_t rsa; 354 ops_dsa_secret_key_t dsa; 343 355 } ops_secret_key_union_t; 344 356 … … 349 361 OPS_S2KU_NONE=0, 350 362 OPS_S2KU_ENCRYPTED_AND_HASHED=254, 351 OPS_S2KU_ENCRYPTED=255 363 OPS_S2KU_ENCRYPTED=255, 364 OPS_S2KU_NOT_SET=256 352 365 } ops_s2k_usage_t; 353 366 openpgpsdk/trunk/src/accumulate.c
r320 r335 32 32 { 33 33 case OPS_PTAG_CT_PUBLIC_KEY: 34 case OPS_PTAG_CT_SECRET_KEY: 35 case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 34 36 // printf("New key\n"); 35 37 ++keyring->nkeys; openpgpsdk/trunk/src/packet-parse.c
r334 r335 119 119 #define WARN(warn) do { C.error.error=warn; CB(OPS_PARSER_ERROR,&content);; } while(0) 120 120 #define WARNP(info,warn) do { C.error.error=warn; CBP(info,OPS_PARSER_ERROR,&content); } while(0) 121 #define SWARNP(errtype,info,warn) do { C.error.error=warn; CBP(info,errtype,&content); } while(0) 121 122 /*! \todo descr ERR1 macro */ 122 123 #define ERR1P(info,fmt,x) do { format_error(&content,(fmt),(x)); CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0) … … 593 594 free(trailer->hash); 594 595 trailer->hash=NULL; 596 } 597 598 void ops_cmd_get_passphrase_free(char **passphrase) 599 { 600 free(*passphrase); 601 *passphrase=NULL; 595 602 } 596 603 … … 725 732 case OPS_PARSER_ERROR: 726 733 case OPS_PARSER_ERRCODE: 734 case OPS_PARSER_ERROR_UNKNOWN_TAG: 735 case OPS_PARSER_ERROR_PACKET_CONSUMED: 727 736 break; 728 737 729 738 case OPS_PTAG_CT_SECRET_KEY: 739 case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 730 740 ops_secret_key_free(&c->content.secret_key); 731 741 break; … … 733 743 case OPS_PTAG_CT_PK_SESSION_KEY: 734 744 ops_pk_session_key_free(&c->content.pk_session_key); 745 break; 746 747 case OPS_PTAG_CMD_GET_PASSPHRASE: 748 ops_cmd_get_passphrase_free(c->content.passphrase); 735 749 break; 736 750 … … 1736 1750 break; 1737 1751 1752 case OPS_PKA_DSA: 1753 free_BN(&key->key.dsa.x); 1754 break; 1755 1738 1756 default: 1757 fprintf(stderr,"Unknown algorithm: %d\n",key->public_key.algorithm); 1739 1758 assert(0); 1740 1759 } 1741 1760 1742 1761 ops_public_key_free(&key->public_key); 1762 } 1763 1764 static int consume_packet(ops_region_t *region,ops_parse_info_t *parse_info, 1765 ops_boolean_t warn) 1766 { 1767 ops_data_t remainder; 1768 ops_parser_content_t content; 1769 1770 if(read_data(&remainder,region,parse_info)) 1771 { 1772 /* now throw it away */ 1773 data_free(&remainder); 1774 if(warn) 1775 SWARNP(OPS_PARSER_ERROR_PACKET_CONSUMED,parse_info, 1776 "Remainder of packet consumed and discarded."); 1777 } 1778 else if(warn) 1779 WARNP(parse_info,"Problem consuming remainder of error packet."); 1780 else 1781 return 0; 1782 1783 return 1; 1743 1784 } 1744 1785 … … 1748 1789 unsigned char c[1]; 1749 1790 1791 memset(&content,'\0',sizeof content); 1750 1792 if(!parse_public_key_data(&C.secret_key.public_key,region,parse_info)) 1751 1793 return 0; … … 1753 1795 return 0; 1754 1796 C.secret_key.s2k_usage=c[0]; 1797 1798 if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED 1799 || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 1800 { 1801 if(!limited_read(c,1,region,parse_info)) 1802 return 0; 1803 C.secret_key.algorithm=c[0]; 1804 1805 if(!limited_read(c,1,region,parse_info)) 1806 return 0; 1807 C.secret_key.s2k_specifier=c[0]; 1808 } 1809 else if(C.secret_key.s2k_usage != OPS_S2KU_NONE) 1810 { 1811 C.secret_key.algorithm=C.secret_key.s2k_usage; 1812 C.secret_key.s2k_usage=OPS_S2KU_NOT_SET; 1813 } 1814 1755 1815 if(C.secret_key.s2k_usage != OPS_S2KU_NONE) 1756 1816 { … … 1759 1819 char *passphrase; 1760 1820 1761 assert(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED1762 || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED);1763 1764 if(!limited_read(c,1,region,parse_info))1765 return 0;1766 C.secret_key.algorithm=c[0];1767 1768 if(!limited_read(c,1,region,parse_info))1769 return 0;1770 C.secret_key.s2k_specifier=c[0];1771 1772 1821 n=ops_block_size(C.secret_key.algorithm); 1773 1822 assert(n > 0 && n <= OPS_MAX_BLOCK_SIZE); … … 1776 1825 return 0; 1777 1826 1827 passphrase=NULL; 1778 1828 pc.content.passphrase=&passphrase; 1779 1829 CBP(parse_info,OPS_PTAG_CMD_GET_PASSPHRASE,&pc); 1830 if(!passphrase) 1831 { 1832 if(!consume_packet(region,parse_info,ops_false)) 1833 return 0; 1834 CBP(parse_info,OPS_PTAG_CT_ENCRYPTED_SECRET_KEY,&content); 1835 return 1; 1836 } 1780 1837 } 1781 1838 … … 1855 1912 return 1; 1856 1913 } 1857 1858 1914 1859 1915 /** Parse one packet. … … 1981 2037 format_error(&content,"Format error (unknown content tag %d)", 1982 2038 C.ptag.content_tag); 1983 CBP(parse_info,OPS_PARSER_ERROR ,&content);2039 CBP(parse_info,OPS_PARSER_ERROR_UNKNOWN_TAG,&content); 1984 2040 r=0; 1985 2041 } … … 1988 2044 1989 2045 if(region.length != region.length_read) 1990 { 1991 ops_data_t remainder; 1992 1993 if (read_data(&remainder,®ion,parse_info)) 1994 { 1995 /* now throw it away */ 1996 data_free(&remainder); 1997 WARNP(parse_info,"Remainder of packet consumed and discarded."); 1998 } 1999 else 2000 WARNP(parse_info,"Problem consuming remainder of error packet."); 2001 } 2046 consume_packet(®ion,parse_info,ops_true); 2002 2047 2003 2048 /* set pktlen */
