Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

rpmio/rpmpgp.c

Go to the documentation of this file.
00001 /*@-boundsread@*/
00007 #include "system.h"
00008 #include "rpmio_internal.h"
00009 #include "debug.h"
00010 
00011 /*@access pgpDig @*/
00012 /*@access pgpDigParams @*/
00013 
00014 /*@unchecked@*/
00015 static int _debug = 0;
00016 
00017 /*@unchecked@*/
00018 static int _print = 0;
00019 
00020 /*@unchecked@*/ /*@null@*/
00021 static pgpDig _dig = NULL;
00022 
00023 /*@unchecked@*/ /*@null@*/
00024 static pgpDigParams _digp = NULL;
00025 
00026 #ifdef  DYING
00027 /* This is the unarmored RPM-GPG-KEY public key. */
00028 const char * redhatPubKeyDSA = "\
00029 mQGiBDfqVDgRBADBKr3Bl6PO8BQ0H8sJoD6p9U7Yyl7pjtZqioviPwXP+DCWd4u8\n\
00030 HQzcxAZ57m8ssA1LK1Fx93coJhDzM130+p5BG9mYSWShLabR3N1KXdXQYYcowTOM\n\
00031 GxdwYRGr1Spw8QydLhjVfU1VSl4xt6bupPbWJbyjkg5Z3P7BlUOUJmrx3wCgobNV\n\
00032 EDGaWYJcch5z5B1of/41G8kEAKii6q7Gu/vhXXnLS6m15oNnPVybyngiw/23dKjS\n\
00033 ZVG7rKANEK2mxg1VB+vc/uUc4k49UxJJfCZg1gu1sPFV3GSa+Y/7jsiLktQvCiLP\n\
00034 lncQt1dV+ENmHR5BdIDPWDzKBVbgWnSDnqQ6KrZ7T6AlZ74VMpjGxxkWU6vV2xsW\n\
00035 XCLPA/9P/vtImA8CZN3jxGgtK5GGtDNJ/cMhhuv5tnfwFg4b/VGo2Jr8mhLUqoIb\n\
00036 E6zeGAmZbUpdckDco8D5fiFmqTf5+++pCEpJLJkkzel/32N2w4qzPrcRMCiBURES\n\
00037 PjCLd4Y5rPoU8E4kOHc/4BuHN903tiCsCPloCrWsQZ7UdxfQ5LQiUmVkIEhhdCwg\n\
00038 SW5jIDxzZWN1cml0eUByZWRoYXQuY29tPohVBBMRAgAVBQI36lQ4AwsKAwMVAwID\n\
00039 FgIBAheAAAoJECGRgM3bQqYOsBQAnRVtg7B25Hm11PHcpa8FpeddKiq2AJ9aO8sB\n\
00040 XmLDmPOEFI75mpTrKYHF6rkCDQQ36lRyEAgAokgI2xJ+3bZsk8jRA8ORIX8DH05U\n\
00041 lMH27qFYzLbT6npXwXYIOtVn0K2/iMDj+oEB1Aa2au4OnddYaLWp06v3d+XyS0t+\n\
00042 5ab2ZfIQzdh7wCwxqRkzR+/H5TLYbMG+hvtTdylfqIX0WEfoOXMtWEGSVwyUsnM3\n\
00043 Jy3LOi48rQQSCKtCAUdV20FoIGWhwnb/gHU1BnmES6UdQujFBE6EANqPhp0coYoI\n\
00044 hHJ2oIO8ujQItvvNaU88j/s/izQv5e7MXOgVSjKe/WX3s2JtB/tW7utpy12wh1J+\n\
00045 JsFdbLV/t8CozUTpJgx5mVA3RKlxjTA+On+1IEUWioB+iVfT7Ov/0kcAzwADBQf9\n\
00046 E4SKCWRand8K0XloMYgmipxMhJNnWDMLkokvbMNTUoNpSfRoQJ9EheXDxwMpTPwK\n\
00047 ti/PYrrL2J11P2ed0x7zm8v3gLrY0cue1iSba+8glY+p31ZPOr5ogaJw7ZARgoS8\n\
00048 BwjyRymXQp+8Dete0TELKOL2/itDOPGHW07SsVWOR6cmX4VlRRcWB5KejaNvdrE5\n\
00049 4XFtOd04NMgWI63uqZc4zkRa+kwEZtmbz3tHSdRCCE+Y7YVP6IUf/w6YPQFQriWY\n\
00050 FiA6fD10eB+BlIUqIw80VgjsBKmCwvKkn4jg8kibXgj4/TzQSx77uYokw1EqQ2wk\n\
00051 OZoaEtcubsNMquuLCMWijYhGBBgRAgAGBQI36lRyAAoJECGRgM3bQqYOhyYAnj7h\n\
00052 VDY/FJAGqmtZpwVp9IlitW5tAJ4xQApr/jNFZCTksnI+4O1765F7tA==\n\
00053 ";
00054 
00055 /* This is the unarmored RPM-PGP-KEY public key. */
00056 const char * redhatPubKeyRSA = "\
00057 mQCNAzEpXjUAAAEEAKG4/V9oUSiDc9wIge6Bmg6erDGCLzmFyioAho8kDIJSrcmi\n\
00058 F9qTdPq+fj726pgW1iSb0Y7syZn9Y2lgQm5HkPODfNi8eWyTFSxbr8ygosLRClTP\n\
00059 xqHVhtInGrfZNLoSpv1LdWOme0yOpOQJnghdOMzKXpgf5g84vaUg6PHLopv5AAUR\n\
00060 tCpSZWQgSGF0IFNvZnR3YXJlLCBJbmMuIDxyZWRoYXRAcmVkaGF0LmNvbT6JAJUD\n\
00061 BRAyA5tUoyDApfg4JKEBAUzSA/9QdcVsu955vVyZDk8uvOXWV0X3voT9B3aYMFvj\n\
00062 UNHUD6F1VFruwQHVKbGJEq1o5MOA6OXKR3vJZStXEMF47TWXJfQaflgl8ywZTH5W\n\
00063 +eMlKau6Nr0labUV3lmsAE4Vsgu8NCkzIrp2wNVbeW2ZAXtrKswV+refLquUhp7l\n\
00064 wMpH9IkAdQMFEDGttkRNdXhbO1TgGQEBAGoC/j6C22PqXIyqZc6fG6J6Jl/T5kFG\n\
00065 xH1pKIzua5WCDDugAgnuOJgywa4pegT4UqwEZiMTAlwT6dmG1CXgKB+5V7lnCjDc\n\
00066 JZLni0iztoe08ig6fJrjNGXljf7KYXzgwBftQokAlQMFEDMQzo2MRVM9rfPulQEB\n\
00067 pLoD/1/MWv3u0Paiu14XRvDrBaJ7BmG2/48bA5vKOzpvvoNRO95YS7ZEtqErXA7Y\n\
00068 DRO8+C8f6PAILMk7kCk4lNMscS/ZRzu5+J8cv4ejsFvxgJBBU3Zgp8AWdWOpvZ0I\n\
00069 wW//HoDUGhOxlEtymljIMFBkj4SysHWhCBUfA9Xy86kouTJQiQCVAwUQMxDOQ50a\n\
00070 feTWLUSJAQFnYQQAkt9nhMTeioREB1DvJt+vsFyOj//o3ThqK5ySEP3dgj62iaQp\n\
00071 JrBmAe5XZPw25C/TXAf+x27H8h2QbKgq49VtsElFexc6wO+uq85fAPDdyE+2XyNE\n\
00072 njGZkY/TP2F/jTB0sAwJO+xFCHmSYkcBjzxK/2LMD+O7rwp2UCUhhl9QhhqJAJUD\n\
00073 BRAx5na6pSDo8cuim/kBARmjA/4lDVnV2h9KiNabp9oE38wmGgu5m5XgUHW8L6du\n\
00074 iQDnwO5IgXN2vDpKGxbgtwv6iYYmGd8IRQ66uJvOsxSv3OR7J7LkCHuI2b/s0AZn\n\
00075 c79DZaJ2ChUCZlbNQBMeEdrFWif9NopY+d5+2tby1onu9XOFMMvomxL3NhctElYR\n\
00076 HC8Xw4kAlQMFEDHmdTtURTdEKY1MpQEBEtEEAMZbp1ZFrjiHkj2aLFC1S8dGRbSH\n\
00077 GUdnLP9qLPFgmWekp9E0o8ZztALGVdqPfPF3N/JJ+AL4IMrfojd7+eZKw36Mdvtg\n\
00078 dPI+Oz4sxHDbDynZ2qspD9Om5yYuxuz/Xq+9nO2IlsAnEYw3ag3cxat0kvxpOPRe\n\
00079 Yy+vFpgfDNizr3MgiQBVAwUQMXNMXCjtrosVMemRAQEDnwH7BsJrnnh91nI54LAK\n\
00080 Gcq3pr8ld0PAtWJmNRGQvUlpEMXUSnu59j2P1ogPNjL3PqKdVxk5Jqgcr8TPQMf3\n\
00081 V4fqXokAlQMFEDFy+8YiEmsRQ3LyzQEB+TwD/03QDslXLg5F3zj4zf0yI6ikT0be\n\
00082 5OhZv2pnkb80qgdHzFRxBOYmSoueRKdQJASd8F9ue4b3bmf/Y7ikiY0DblvxcXB2\n\
00083 sz1Pu8i2Zn9u8SKuxNIoVvM8/STRVkgPfvL5QjAWMHT9Wvg81XcI2yXJzrt/2f2g\n\
00084 mNpWIvVOOT85rVPIiQCVAwUQMVPRlBlzviMjNHElAQG1nwP/fpVX6nKRWJCSFeB7\n\
00085 leZ4lb+y1uMsMVv0n7agjJVw13SXaA267y7VWCBlnhsCemxEugqEIkI4lu/1mgtw\n\
00086 WPWSE0BOIVjj0AA8zp2T0H3ZCCMbiFAFJ1P2Gq2rKr8QrOb/08oH1lEzyz0j/jKh\n\
00087 qiXAxdlB1wojQB6yLbHvTIe3rZGJAHUDBRAxKetfzauiKSJ6LJEBAed/AvsEiGgj\n\
00088 TQzhsZcUuRNrQpV0cDGH9Mpril7P7K7yFIzju8biB+Cu6nEknSOHlMLl8usObVlk\n\
00089 d8Wf14soHC7SjItiGSKtI8JhauzBJPl6fDDeyHGsJKo9f9adKeBMCipCFOuJAJUD\n\
00090 BRAxKeqWRHFTaIK/x+0BAY6eA/4m5X4gs1UwOUIRnljo9a0cVs6ITL554J9vSCYH\n\
00091 Zzd87kFwdf5W1Vd82HIkRzcr6cp33E3IDkRzaQCMVw2me7HePP7+4Ry2q3EeZMbm\n\
00092 NE++VzkxjikzpRb2+F5nGB2UdsElkgbXinswebiuOwOrocLbz6JFdDsJPcT5gVfi\n\
00093 z15FuA==\n\
00094 ";
00095 #endif  /* DYING */
00096 
00097 struct pgpValTbl_s pgpSigTypeTbl[] = {
00098     { PGPSIGTYPE_BINARY,        "Binary document signature" },
00099     { PGPSIGTYPE_TEXT,          "Text document signature" },
00100     { PGPSIGTYPE_STANDALONE,    "Standalone signature" },
00101     { PGPSIGTYPE_GENERIC_CERT,  "Generic certification of a User ID and Public Key" },
00102     { PGPSIGTYPE_PERSONA_CERT,  "Persona certification of a User ID and Public Key" },
00103     { PGPSIGTYPE_CASUAL_CERT,   "Casual certification of a User ID and Public Key" },
00104     { PGPSIGTYPE_POSITIVE_CERT, "Positive certification of a User ID and Public Key" },
00105     { PGPSIGTYPE_SUBKEY_BINDING,"Subkey Binding Signature" },
00106     { PGPSIGTYPE_SIGNED_KEY,    "Signature directly on a key" },
00107     { PGPSIGTYPE_KEY_REVOKE,    "Key revocation signature" },
00108     { PGPSIGTYPE_SUBKEY_REVOKE, "Subkey revocation signature" },
00109     { PGPSIGTYPE_CERT_REVOKE,   "Certification revocation signature" },
00110     { PGPSIGTYPE_TIMESTAMP,     "Timestamp signature" },
00111     { -1,                       "Unknown signature type" },
00112 };
00113 
00114 struct pgpValTbl_s pgpPubkeyTbl[] = {
00115     { PGPPUBKEYALGO_RSA,        "RSA" },
00116     { PGPPUBKEYALGO_RSA_ENCRYPT,"RSA(Encrypt-Only)" },
00117     { PGPPUBKEYALGO_RSA_SIGN,   "RSA(Sign-Only)" },
00118     { PGPPUBKEYALGO_ELGAMAL_ENCRYPT,"Elgamal(Encrypt-Only)" },
00119     { PGPPUBKEYALGO_DSA,        "DSA" },
00120     { PGPPUBKEYALGO_EC,         "Elliptic Curve" },
00121     { PGPPUBKEYALGO_ECDSA,      "ECDSA" },
00122     { PGPPUBKEYALGO_ELGAMAL,    "Elgamal" },
00123     { PGPPUBKEYALGO_DH,         "Diffie-Hellman (X9.42)" },
00124     { -1,                       "Unknown public key algorithm" },
00125 };
00126 
00127 struct pgpValTbl_s pgpSymkeyTbl[] = {
00128     { PGPSYMKEYALGO_PLAINTEXT,  "Plaintext" },
00129     { PGPSYMKEYALGO_IDEA,       "IDEA" },
00130     { PGPSYMKEYALGO_TRIPLE_DES, "3DES" },
00131     { PGPSYMKEYALGO_CAST5,      "CAST5" },
00132     { PGPSYMKEYALGO_BLOWFISH,   "BLOWFISH" },
00133     { PGPSYMKEYALGO_SAFER,      "SAFER" },
00134     { PGPSYMKEYALGO_DES_SK,     "DES/SK" },
00135     { PGPSYMKEYALGO_AES_128,    "AES(128-bit key)" },
00136     { PGPSYMKEYALGO_AES_192,    "AES(192-bit key)" },
00137     { PGPSYMKEYALGO_AES_256,    "AES(256-bit key)" },
00138     { PGPSYMKEYALGO_TWOFISH,    "TWOFISH" },
00139     { -1,                       "Unknown symmetric key algorithm" },
00140 };
00141 
00142 struct pgpValTbl_s pgpCompressionTbl[] = {
00143     { PGPCOMPRESSALGO_NONE,     "Uncompressed" },
00144     { PGPCOMPRESSALGO_ZIP,      "ZIP" },
00145     { PGPCOMPRESSALGO_ZLIB,     "ZLIB" },
00146     { -1,                       "Unknown compression algorithm" },
00147 };
00148 
00149 struct pgpValTbl_s pgpHashTbl[] = {
00150     { PGPHASHALGO_MD5,          "MD5" },
00151     { PGPHASHALGO_SHA1,         "SHA1" },
00152     { PGPHASHALGO_RIPEMD160,    "RIPEMD160" },
00153     { PGPHASHALGO_MD2,          "MD2" },
00154     { PGPHASHALGO_TIGER192,     "TIGER192" },
00155     { PGPHASHALGO_HAVAL_5_160,  "HAVAL-5-160" },
00156     { -1,                       "Unknown hash algorithm" },
00157 };
00158 
00159 /*@-exportlocal -exportheadervar@*/
00160 /*@observer@*/ /*@unchecked@*/
00161 struct pgpValTbl_s pgpKeyServerPrefsTbl[] = {
00162     { 0x80,                     "No-modify" },
00163     { -1,                       "Unknown key server preference" },
00164 };
00165 /*@=exportlocal =exportheadervar@*/
00166 
00167 struct pgpValTbl_s pgpSubTypeTbl[] = {
00168     { PGPSUBTYPE_SIG_CREATE_TIME,"signature creation time" },
00169     { PGPSUBTYPE_SIG_EXPIRE_TIME,"signature expiration time" },
00170     { PGPSUBTYPE_EXPORTABLE_CERT,"exportable certification" },
00171     { PGPSUBTYPE_TRUST_SIG,     "trust signature" },
00172     { PGPSUBTYPE_REGEX,         "regular expression" },
00173     { PGPSUBTYPE_REVOCABLE,     "revocable" },
00174     { PGPSUBTYPE_KEY_EXPIRE_TIME,"key expiration time" },
00175     { PGPSUBTYPE_BACKWARD_COMPAT,"placeholder for backward compatibility" },
00176     { PGPSUBTYPE_PREFER_SYMKEY, "preferred symmetric algorithms" },
00177     { PGPSUBTYPE_REVOKE_KEY,    "revocation key" },
00178     { PGPSUBTYPE_ISSUER_KEYID,  "issuer key ID" },
00179     { PGPSUBTYPE_NOTATION,      "notation data" },
00180     { PGPSUBTYPE_PREFER_HASH,   "preferred hash algorithms" },
00181     { PGPSUBTYPE_PREFER_COMPRESS,"preferred compression algorithms" },
00182     { PGPSUBTYPE_KEYSERVER_PREFERS,"key server preferences" },
00183     { PGPSUBTYPE_PREFER_KEYSERVER,"preferred key server" },
00184     { PGPSUBTYPE_PRIMARY_USERID,"primary user id" },
00185     { PGPSUBTYPE_POLICY_URL,    "policy URL" },
00186     { PGPSUBTYPE_KEY_FLAGS,     "key flags" },
00187     { PGPSUBTYPE_SIGNER_USERID, "signer's user id" },
00188     { PGPSUBTYPE_REVOKE_REASON, "reason for revocation" },
00189     { PGPSUBTYPE_INTERNAL_100,  "internal subpkt type 100" },
00190     { PGPSUBTYPE_INTERNAL_101,  "internal subpkt type 101" },
00191     { PGPSUBTYPE_INTERNAL_102,  "internal subpkt type 102" },
00192     { PGPSUBTYPE_INTERNAL_103,  "internal subpkt type 103" },
00193     { PGPSUBTYPE_INTERNAL_104,  "internal subpkt type 104" },
00194     { PGPSUBTYPE_INTERNAL_105,  "internal subpkt type 105" },
00195     { PGPSUBTYPE_INTERNAL_106,  "internal subpkt type 106" },
00196     { PGPSUBTYPE_INTERNAL_107,  "internal subpkt type 107" },
00197     { PGPSUBTYPE_INTERNAL_108,  "internal subpkt type 108" },
00198     { PGPSUBTYPE_INTERNAL_109,  "internal subpkt type 109" },
00199     { PGPSUBTYPE_INTERNAL_110,  "internal subpkt type 110" },
00200     { -1,                       "Unknown signature subkey type" },
00201 };
00202 
00203 struct pgpValTbl_s pgpTagTbl[] = {
00204     { PGPTAG_PUBLIC_SESSION_KEY,"Public-Key Encrypted Session Key" },
00205     { PGPTAG_SIGNATURE,         "Signature" },
00206     { PGPTAG_SYMMETRIC_SESSION_KEY,"Symmetric-Key Encrypted Session Key" },
00207     { PGPTAG_ONEPASS_SIGNATURE, "One-Pass Signature" },
00208     { PGPTAG_SECRET_KEY,        "Secret Key" },
00209     { PGPTAG_PUBLIC_KEY,        "Public Key" },
00210     { PGPTAG_SECRET_SUBKEY,     "Secret Subkey" },
00211     { PGPTAG_COMPRESSED_DATA,   "Compressed Data" },
00212     { PGPTAG_SYMMETRIC_DATA,    "Symmetrically Encrypted Data" },
00213     { PGPTAG_MARKER,            "Marker" },
00214     { PGPTAG_LITERAL_DATA,      "Literal Data" },
00215     { PGPTAG_TRUST,             "Trust" },
00216     { PGPTAG_USER_ID,           "User ID" },
00217     { PGPTAG_PUBLIC_SUBKEY,     "Public Subkey" },
00218     { PGPTAG_COMMENT_OLD,       "Comment (from OpenPGP draft)" },
00219     { PGPTAG_PHOTOID,           "PGP's photo ID" },
00220     { PGPTAG_ENCRYPTED_MDC,     "Integrity protected encrypted data" },
00221     { PGPTAG_MDC,               "Manipulaion detection code packet" },
00222     { PGPTAG_PRIVATE_60,        "Private #60" },
00223     { PGPTAG_COMMENT,           "Comment" },
00224     { PGPTAG_PRIVATE_62,        "Private #62" },
00225     { PGPTAG_CONTROL,           "Control (GPG)" },
00226     { -1,                       "Unknown packet tag" },
00227 };
00228 
00229 struct pgpValTbl_s pgpArmorTbl[] = {
00230     { PGPARMOR_MESSAGE,         "MESSAGE" },
00231     { PGPARMOR_PUBKEY,          "PUBLIC KEY BLOCK" },
00232     { PGPARMOR_SIGNATURE,       "SIGNATURE" },
00233     { PGPARMOR_SIGNED_MESSAGE,  "SIGNED MESSAGE" },
00234     { PGPARMOR_FILE,            "ARMORED FILE" },
00235     { PGPARMOR_PRIVKEY,         "PRIVATE KEY BLOCK" },
00236     { PGPARMOR_SECKEY,          "SECRET KEY BLOCK" },
00237     { -1,                       "Unknown armor block" }
00238 };
00239 
00240 struct pgpValTbl_s pgpArmorKeyTbl[] = {
00241     { PGPARMORKEY_VERSION,      "Version: " },
00242     { PGPARMORKEY_COMMENT,      "Comment: " },
00243     { PGPARMORKEY_MESSAGEID,    "MessageID: " },
00244     { PGPARMORKEY_HASH,         "Hash: " },
00245     { PGPARMORKEY_CHARSET,      "Charset: " },
00246     { -1,                       "Unknown armor key" }
00247 };
00248 
00254 /*@unused@*/ static inline /*@null@*/ void *
00255 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p)
00256         /*@modifies p @*/
00257 {
00258     if (p != NULL)      free((void *)p);
00259     return NULL;
00260 }
00261 
00262 static void pgpPrtNL(void)
00263         /*@globals fileSystem @*/
00264         /*@modifies fileSystem @*/
00265 {
00266     if (!_print) return;
00267     fprintf(stderr, "\n");
00268 }
00269 
00270 static void pgpPrtInt(const char *pre, int i)
00271         /*@globals fileSystem @*/
00272         /*@modifies fileSystem @*/
00273 {
00274     if (!_print) return;
00275     if (pre && *pre)
00276         fprintf(stderr, "%s", pre);
00277     fprintf(stderr, " %d", i);
00278 }
00279 
00280 static void pgpPrtStr(const char *pre, const char *s)
00281         /*@globals fileSystem @*/
00282         /*@modifies fileSystem @*/
00283 {
00284     if (!_print) return;
00285     if (pre && *pre)
00286         fprintf(stderr, "%s", pre);
00287     fprintf(stderr, " %s", s);
00288 }
00289 
00290 static void pgpPrtHex(const char *pre, const byte *p, unsigned int plen)
00291         /*@globals fileSystem @*/
00292         /*@modifies fileSystem @*/
00293 {
00294     if (!_print) return;
00295     if (pre && *pre)
00296         fprintf(stderr, "%s", pre);
00297     fprintf(stderr, " %s", pgpHexStr(p, plen));
00298 }
00299 
00300 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
00301         /*@globals fileSystem @*/
00302         /*@modifies fileSystem @*/
00303 {
00304     if (!_print) return;
00305     if (pre && *pre)
00306         fprintf(stderr, "%s", pre);
00307     fprintf(stderr, "%s(%u)", pgpValStr(vs, val), (unsigned)val);
00308 }
00309 
00312 /*@unused@*/ static /*@observer@*/
00313 const char * pgpMpiHex(const byte *p)
00314         /*@*/
00315 {
00316     static char prbuf[2048];
00317     char *t = prbuf;
00318     t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
00319     return prbuf;
00320 }
00321 
00322 /*@-boundswrite@*/
00326 static int pgpHexSet(const char * pre, int lbits,
00327                 /*@out@*/ mpnumber * mpn, const byte * p, const byte * pend)
00328         /*@globals fileSystem @*/
00329         /*@modifies *mpn, fileSystem @*/
00330 {
00331     unsigned int mbits = pgpMpiBits(p);
00332     unsigned int nbits;
00333     unsigned int nbytes;
00334     char * t;
00335     unsigned int ix;
00336 
00337     if ((p + ((mbits+7) >> 3)) > pend)
00338         return 1;
00339 
00340     nbits = (lbits > mbits ? lbits : mbits);
00341     nbytes = ((nbits + 7) >> 3);
00342     t = xmalloc(2*nbytes+1);
00343     ix = 2 * ((nbits - mbits) >> 3);
00344 
00345 if (_debug)
00346 fprintf(stderr, "*** mbits %u nbits %u nbytes %u t %p[%d] ix %u\n", mbits, nbits, nbytes, t, (2*nbytes+1), ix);
00347     if (ix > 0) memset(t, (int)'0', ix);
00348     strcpy(t+ix, pgpMpiHex(p));
00349 if (_debug)
00350 fprintf(stderr, "*** %s %s\n", pre, t);
00351     mpnsethex(mpn, t);
00352     t = _free(t);
00353 if (_debug && _print)
00354 fprintf(stderr, "\t %s ", pre), mpfprintln(stderr, mpn->size, mpn->data);
00355     return 0;
00356 }
00357 /*@=boundswrite@*/
00358 
00359 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
00360 {
00361     const byte *p = h;
00362     unsigned plen;
00363     int i;
00364 
00365     while (hlen > 0) {
00366         i = pgpLen(p, &plen);
00367         p += i;
00368         hlen -= i;
00369 
00370         pgpPrtVal("    ", pgpSubTypeTbl, p[0]);
00371         switch (*p) {
00372         case PGPSUBTYPE_PREFER_SYMKEY:  /* preferred symmetric algorithms */
00373             for (i = 1; i < plen; i++)
00374                 pgpPrtVal(" ", pgpSymkeyTbl, p[i]);
00375             /*@switchbreak@*/ break;
00376         case PGPSUBTYPE_PREFER_HASH:    /* preferred hash algorithms */
00377             for (i = 1; i < plen; i++)
00378                 pgpPrtVal(" ", pgpHashTbl, p[i]);
00379             /*@switchbreak@*/ break;
00380         case PGPSUBTYPE_PREFER_COMPRESS:/* preferred compression algorithms */
00381             for (i = 1; i < plen; i++)
00382                 pgpPrtVal(" ", pgpCompressionTbl, p[i]);
00383             /*@switchbreak@*/ break;
00384         case PGPSUBTYPE_KEYSERVER_PREFERS:/* key server preferences */
00385             for (i = 1; i < plen; i++)
00386                 pgpPrtVal(" ", pgpKeyServerPrefsTbl, p[i]);
00387             /*@switchbreak@*/ break;
00388         case PGPSUBTYPE_SIG_CREATE_TIME:
00389 /*@-mods -mayaliasunique @*/
00390             if (_digp && !(_digp->saved & PGPDIG_SAVED_TIME) &&
00391                 sigtype == PGPSIGTYPE_POSITIVE_CERT)
00392             {
00393                 _digp->saved |= PGPDIG_SAVED_TIME;
00394                 memcpy(_digp->time, p+1, sizeof(_digp->time));
00395             }
00396 /*@=mods =mayaliasunique @*/
00397             /*@fallthrough@*/
00398         case PGPSUBTYPE_SIG_EXPIRE_TIME:
00399         case PGPSUBTYPE_KEY_EXPIRE_TIME:
00400             if ((plen - 1) == 4) {
00401                 time_t t = pgpGrab(p+1, plen-1);
00402                 if (_print)
00403                    fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00404             } else
00405                 pgpPrtHex("", p+1, plen-1);
00406             /*@switchbreak@*/ break;
00407 
00408         case PGPSUBTYPE_ISSUER_KEYID:   /* issuer key ID */
00409 /*@-mods -mayaliasunique @*/
00410             if (_digp && !(_digp->saved & PGPDIG_SAVED_ID) &&
00411                 sigtype == PGPSIGTYPE_POSITIVE_CERT)
00412             {
00413                 _digp->saved |= PGPDIG_SAVED_ID;
00414                 memcpy(_digp->signid, p+1, sizeof(_digp->signid));
00415             }
00416 /*@=mods =mayaliasunique @*/
00417             /*@fallthrough@*/
00418         case PGPSUBTYPE_EXPORTABLE_CERT:
00419         case PGPSUBTYPE_TRUST_SIG:
00420         case PGPSUBTYPE_REGEX:
00421         case PGPSUBTYPE_REVOCABLE:
00422         case PGPSUBTYPE_BACKWARD_COMPAT:
00423         case PGPSUBTYPE_REVOKE_KEY:
00424         case PGPSUBTYPE_NOTATION:
00425         case PGPSUBTYPE_PREFER_KEYSERVER:
00426         case PGPSUBTYPE_PRIMARY_USERID:
00427         case PGPSUBTYPE_POLICY_URL:
00428         case PGPSUBTYPE_KEY_FLAGS:
00429         case PGPSUBTYPE_SIGNER_USERID:
00430         case PGPSUBTYPE_REVOKE_REASON:
00431         case PGPSUBTYPE_INTERNAL_100:
00432         case PGPSUBTYPE_INTERNAL_101:
00433         case PGPSUBTYPE_INTERNAL_102:
00434         case PGPSUBTYPE_INTERNAL_103:
00435         case PGPSUBTYPE_INTERNAL_104:
00436         case PGPSUBTYPE_INTERNAL_105:
00437         case PGPSUBTYPE_INTERNAL_106:
00438         case PGPSUBTYPE_INTERNAL_107:
00439         case PGPSUBTYPE_INTERNAL_108:
00440         case PGPSUBTYPE_INTERNAL_109:
00441         case PGPSUBTYPE_INTERNAL_110:
00442         default:
00443             pgpPrtHex("", p+1, plen-1);
00444             /*@switchbreak@*/ break;
00445         }
00446         pgpPrtNL();
00447         p += plen;
00448         hlen -= plen;
00449     }
00450     return 0;
00451 }
00452 
00453 /*@-varuse =readonlytrans @*/
00454 /*@observer@*/ /*@unchecked@*/
00455 static const char * pgpSigRSA[] = {
00456     " m**d =",
00457     NULL,
00458 };
00459 
00460 /*@observer@*/ /*@unchecked@*/
00461 static const char * pgpSigDSA[] = {
00462     "    r =",
00463     "    s =",
00464     NULL,
00465 };
00466 /*@=varuse =readonlytrans @*/
00467 
00468 static int pgpPrtSigParams(/*@unused@*/ pgpTag tag, byte pubkey_algo, byte sigtype,
00469                 const byte *p, const byte *h, unsigned int hlen)
00470         /*@globals fileSystem @*/
00471         /*@modifies fileSystem @*/
00472 {
00473     const byte * pend = h + hlen;
00474     int i;
00475 
00476     for (i = 0; p < pend; i++, p += pgpMpiLen(p)) {
00477         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00478             if (i >= 1) break;
00479             if (_dig &&
00480         (sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT))
00481             {
00482                 switch (i) {
00483                 case 0:         /* m**d */
00484                     mpnsethex(&_dig->c, pgpMpiHex(p));
00485 if (_debug && _print)
00486 fprintf(stderr, "\t  m**d = "),  mpfprintln(stderr, _dig->c.size, _dig->c.data);
00487                     /*@switchbreak@*/ break;
00488                 default:
00489                     /*@switchbreak@*/ break;
00490                 }
00491             }
00492             pgpPrtStr("", pgpSigRSA[i]);
00493         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00494             if (i >= 2) break;
00495             if (_dig &&
00496         (sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT))
00497             {
00498                 int xx;
00499                 xx = 0;
00500                 switch (i) {
00501                 case 0:         /* r */
00502                     xx = pgpHexSet(pgpSigDSA[i], 160, &_dig->r, p, pend);
00503                     /*@switchbreak@*/ break;
00504                 case 1:         /* s */
00505                     xx = pgpHexSet(pgpSigDSA[i], 160, &_dig->s, p, pend);
00506                     /*@switchbreak@*/ break;
00507                 default:
00508                     xx = 1;
00509                     /*@switchbreak@*/ break;
00510                 }
00511                 if (xx) return xx;
00512             }
00513             pgpPrtStr("", pgpSigDSA[i]);
00514         } else {
00515             if (_print)
00516                 fprintf(stderr, "%7d", i);
00517         }
00518         pgpPrtStr("", pgpMpiStr(p));
00519         pgpPrtNL();
00520     }
00521 
00522     return 0;
00523 }
00524 
00525 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
00526         /*@globals _digp @*/
00527         /*@modifies *_digp @*/
00528 {
00529     byte version = h[0];
00530     byte * p;
00531     unsigned plen;
00532     int rc;
00533 
00534     switch (version) {
00535     case 3:
00536     {   pgpPktSigV3 v = (pgpPktSigV3)h;
00537         time_t t;
00538 
00539         if (v->hashlen != 5)
00540             return 1;
00541 
00542         pgpPrtVal("V3 ", pgpTagTbl, tag);
00543         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00544         pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
00545         pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
00546         pgpPrtNL();
00547         t = pgpGrab(v->time, sizeof(v->time));
00548         if (_print)
00549             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00550         pgpPrtNL();
00551         pgpPrtHex(" signer keyid", v->signid, sizeof(v->signid));
00552         plen = pgpGrab(v->signhash16, sizeof(v->signhash16));
00553         pgpPrtHex(" signhash16", v->signhash16, sizeof(v->signhash16));
00554         pgpPrtNL();
00555 
00556         if (_digp && _digp->pubkey_algo == 0) {
00557             _digp->version = v->version;
00558             _digp->hashlen = v->hashlen;
00559             _digp->sigtype = v->sigtype;
00560             _digp->hash = memcpy(xmalloc(v->hashlen), &v->sigtype, v->hashlen);
00561             memcpy(_digp->time, v->time, sizeof(_digp->time));
00562             memcpy(_digp->signid, v->signid, sizeof(_digp->signid));
00563             _digp->pubkey_algo = v->pubkey_algo;
00564             _digp->hash_algo = v->hash_algo;
00565             memcpy(_digp->signhash16, v->signhash16, sizeof(_digp->signhash16));
00566         }
00567 
00568         p = ((byte *)v) + sizeof(*v);
00569         rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen);
00570     }   break;
00571     case 4:
00572     {   pgpPktSigV4 v = (pgpPktSigV4)h;
00573 
00574         pgpPrtVal("V4 ", pgpTagTbl, tag);
00575         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00576         pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
00577         pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
00578         pgpPrtNL();
00579 
00580         p = &v->hashlen[0];
00581         plen = pgpGrab(v->hashlen, sizeof(v->hashlen));
00582         p += sizeof(v->hashlen);
00583 
00584         if ((p + plen) > (h + hlen))
00585             return 1;
00586 
00587 if (_debug && _print)
00588 fprintf(stderr, "   hash[%u] -- %s\n", plen, pgpHexStr(p, plen));
00589         if (_digp && _digp->pubkey_algo == 0) {
00590             _digp->hashlen = sizeof(*v) + plen;
00591             _digp->hash = memcpy(xmalloc(_digp->hashlen), v, _digp->hashlen);
00592         }
00593         (void) pgpPrtSubType(p, plen, v->sigtype);
00594         p += plen;
00595 
00596         plen = pgpGrab(p,2);
00597         p += 2;
00598 
00599         if ((p + plen) > (h + hlen))
00600             return 1;
00601 
00602 if (_debug && _print)
00603 fprintf(stderr, " unhash[%u] -- %s\n", plen, pgpHexStr(p, plen));
00604         (void) pgpPrtSubType(p, plen, v->sigtype);
00605         p += plen;
00606 
00607         plen = pgpGrab(p,2);
00608         pgpPrtHex(" signhash16", p, 2);
00609         pgpPrtNL();
00610 
00611         if (_digp && _digp->pubkey_algo == 0) {
00612             _digp->version = v->version;
00613             _digp->sigtype = v->sigtype;
00614             _digp->pubkey_algo = v->pubkey_algo;
00615             _digp->hash_algo = v->hash_algo;
00616             memcpy(_digp->signhash16, p, sizeof(_digp->signhash16));
00617         }
00618 
00619         p += 2;
00620         if (p > (h + hlen))
00621             return 1;
00622 
00623         rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen);
00624     }   break;
00625     default:
00626         rc = 1;
00627         break;
00628     }
00629     return rc;
00630 }
00631 
00632 /*@-varuse =readonlytrans @*/
00633 /*@observer@*/ /*@unchecked@*/
00634 static const char * pgpPublicRSA[] = {
00635     "    n =",
00636     "    e =",
00637     NULL,
00638 };
00639 
00640 /*@observer@*/ /*@unchecked@*/
00641 static const char * pgpSecretRSA[] = {
00642     "    d =",
00643     "    p =",
00644     "    q =",
00645     "    u =",
00646     NULL,
00647 };
00648 
00649 /*@observer@*/ /*@unchecked@*/
00650 static const char * pgpPublicDSA[] = {
00651     "    p =",
00652     "    q =",
00653     "    g =",
00654     "    y =",
00655     NULL,
00656 };
00657 
00658 /*@observer@*/ /*@unchecked@*/
00659 static const char * pgpSecretDSA[] = {
00660     "    x =",
00661     NULL,
00662 };
00663 
00664 /*@observer@*/ /*@unchecked@*/
00665 static const char * pgpPublicELGAMAL[] = {
00666     "    p =",
00667     "    g =",
00668     "    y =",
00669     NULL,
00670 };
00671 
00672 /*@observer@*/ /*@unchecked@*/
00673 static const char * pgpSecretELGAMAL[] = {
00674     "    x =",
00675     NULL,
00676 };
00677 /*@=varuse =readonlytrans @*/
00678 
00679 static const byte * pgpPrtPubkeyParams(byte pubkey_algo,
00680                 /*@returned@*/ const byte *p, const byte *h, unsigned int hlen)
00681         /*@globals fileSystem, internalState @*/
00682         /*@modifies fileSystem, internalState @*/
00683 {
00684     int i;
00685 
00686     for (i = 0; p < &h[hlen]; i++, p += pgpMpiLen(p)) {
00687         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00688             if (i >= 2) break;
00689             if (_dig) {
00690                 switch (i) {
00691                 case 0:         /* n */
00692                     mpbsethex(&_dig->rsa_pk.n, pgpMpiHex(p));
00693                     /* Get the keyid */
00694                     if (_digp) {
00695                         uint32_t* np = _dig->rsa_pk.n.modl;
00696                         size_t nsize = _dig->rsa_pk.n.size;
00697                         uint32_t keyid[2];
00698                         #if WORDS_BIGENDIAN
00699                         keyid[0] = np[nsize-2];
00700                         keyid[1] = np[nsize-1];
00701                         #else
00702                         keyid[0] = swapu32(np[nsize-2]);
00703                         keyid[1] = swapu32(np[nsize-1]);
00704                         #endif
00705                         memcpy(_digp->signid, keyid, sizeof(_digp->signid));
00706                     }
00707 if (_debug && _print)
00708 fprintf(stderr, "\t     n = "),  mpfprintln(stderr, _dig->rsa_pk.n.size, _dig->rsa_pk.n.modl);
00709                     /*@switchbreak@*/ break;
00710                 case 1:         /* e */
00711                     mpnsethex(&_dig->rsa_pk.e, pgpMpiHex(p));
00712 if (_debug && _print)
00713 fprintf(stderr, "\t     e = "),  mpfprintln(stderr, _dig->rsa_pk.e.size, _dig->rsa_pk.e.data);
00714                     /*@switchbreak@*/ break;
00715                 default:
00716                     /*@switchbreak@*/ break;
00717                 }
00718             }
00719             pgpPrtStr("", pgpPublicRSA[i]);
00720         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00721             if (i >= 4) break;
00722             if (_dig) {
00723                 switch (i) {
00724                 case 0:         /* p */
00725                     mpbsethex(&_dig->p, pgpMpiHex(p));
00726 if (_debug && _print)
00727 fprintf(stderr, "\t     p = "),  mpfprintln(stderr, _dig->p.size, _dig->p.modl);
00728                     /*@switchbreak@*/ break;
00729                 case 1:         /* q */
00730                     mpbsethex(&_dig->q, pgpMpiHex(p));
00731 if (_debug && _print)
00732 fprintf(stderr, "\t     q = "),  mpfprintln(stderr, _dig->q.size, _dig->q.modl);
00733                     /*@switchbreak@*/ break;
00734                 case 2:         /* g */
00735                     mpnsethex(&_dig->g, pgpMpiHex(p));
00736 if (_debug && _print)
00737 fprintf(stderr, "\t     g = "),  mpfprintln(stderr, _dig->g.size, _dig->g.data);
00738                     /*@switchbreak@*/ break;
00739                 case 3:         /* y */
00740                     mpnsethex(&_dig->y, pgpMpiHex(p));
00741 if (_debug && _print)
00742 fprintf(stderr, "\t     y = "),  mpfprintln(stderr, _dig->y.size, _dig->y.data);
00743                     /*@switchbreak@*/ break;
00744                 default:
00745                     /*@switchbreak@*/ break;
00746                 }
00747             }
00748             pgpPrtStr("", pgpPublicDSA[i]);
00749         } else if (pubkey_algo == PGPPUBKEYALGO_ELGAMAL_ENCRYPT) {
00750             if (i >= 3) break;
00751             pgpPrtStr("", pgpPublicELGAMAL[i]);
00752         } else {
00753             if (_print)
00754                 fprintf(stderr, "%7d", i);
00755         }
00756         pgpPrtStr("", pgpMpiStr(p));
00757         pgpPrtNL();
00758     }
00759 
00760     return p;
00761 }
00762 
00763 static const byte * pgpPrtSeckeyParams(/*@unused@*/ byte pubkey_algo,
00764                 /*@returned@*/ const byte *p, const byte *h, unsigned int hlen)
00765         /*@globals fileSystem @*/
00766         /*@modifies fileSystem @*/
00767 {
00768     int i;
00769 
00770     switch (*p) {
00771     case 0:
00772         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00773         break;
00774     case 255:
00775         p++;
00776         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00777         switch (p[1]) {
00778         case 0x00:
00779             pgpPrtVal(" simple ", pgpHashTbl, p[2]);
00780             p += 2;
00781             /*@innerbreak@*/ break;
00782         case 0x01:
00783             pgpPrtVal(" salted ", pgpHashTbl, p[2]);
00784             pgpPrtHex("", p+3, 8);
00785             p += 10;
00786             /*@innerbreak@*/ break;
00787         case 0x03:
00788             pgpPrtVal(" iterated/salted ", pgpHashTbl, p[2]);
00789             /*@-shiftnegative -shiftimplementation @*/ /* FIX: unsigned cast */
00790             i = (16 + (p[11] & 0xf)) << ((p[11] >> 4) + 6);
00791             /*@=shiftnegative =shiftimplementation @*/
00792             pgpPrtHex("", p+3, 8);
00793             pgpPrtInt(" iter", i);
00794             p += 11;
00795             /*@innerbreak@*/ break;
00796         }
00797         break;
00798     default:
00799         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00800         pgpPrtHex(" IV", p+1, 8);
00801         p += 8;
00802         break;
00803     }
00804     pgpPrtNL();
00805 
00806     p++;
00807 
00808 #ifdef  NOTYET  /* XXX encrypted MPI's need to be handled. */
00809     for (i = 0; p < &h[hlen]; i++, p += pgpMpiLen(p)) {
00810         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00811             if (pgpSecretRSA[i] == NULL) break;
00812             pgpPrtStr("", pgpSecretRSA[i]);
00813         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00814             if (pgpSecretDSA[i] == NULL) break;
00815             pgpPrtStr("", pgpSecretDSA[i]);
00816         } else if (pubkey_algo == PGPPUBKEYALGO_ELGAMAL_ENCRYPT) {
00817             if (pgpSecretELGAMAL[i] == NULL) break;
00818             pgpPrtStr("", pgpSecretELGAMAL[i]);
00819         } else {
00820             if (_print)
00821                 fprintf(stderr, "%7d", i);
00822         }
00823         pgpPrtStr("", pgpMpiStr(p));
00824         pgpPrtNL();
00825     }
00826 #else
00827     pgpPrtHex(" secret", p, (hlen - (p - h) - 2));
00828     pgpPrtNL();
00829     p += (hlen - (p - h) - 2);
00830 #endif
00831     pgpPrtHex(" checksum", p, 2);
00832     pgpPrtNL();
00833 
00834     return p;
00835 }
00836 
00837 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
00838         /*@globals _digp @*/
00839         /*@modifies *_digp @*/
00840 {
00841     byte version = *h;
00842     const byte * p;
00843     unsigned plen;
00844     time_t t;
00845     int rc;
00846 
00847     switch (version) {
00848     case 3:
00849     {   pgpPktKeyV3 v = (pgpPktKeyV3)h;
00850         pgpPrtVal("V3 ", pgpTagTbl, tag);
00851         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00852         t = pgpGrab(v->time, sizeof(v->time));
00853         if (_print)
00854             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00855         plen = pgpGrab(v->valid, sizeof(v->valid));
00856         if (plen != 0)
00857             fprintf(stderr, " valid %u days", plen);
00858         pgpPrtNL();
00859 
00860         if (_digp && _digp->tag == tag) {
00861             _digp->version = v->version;
00862             memcpy(_digp->time, v->time, sizeof(_digp->time));
00863             _digp->pubkey_algo = v->pubkey_algo;
00864         }
00865 
00866         p = ((byte *)v) + sizeof(*v);
00867         p = pgpPrtPubkeyParams(v->pubkey_algo, p, h, hlen);
00868         rc = 0;
00869     }   break;
00870     case 4:
00871     {   pgpPktKeyV4 v = (pgpPktKeyV4)h;
00872         pgpPrtVal("V4 ", pgpTagTbl, tag);
00873         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00874         t = pgpGrab(v->time, sizeof(v->time));
00875         if (_print)
00876             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00877         pgpPrtNL();
00878 
00879         if (_digp && _digp->tag == tag) {
00880             _digp->version = v->version;
00881             memcpy(_digp->time, v->time, sizeof(_digp->time));
00882             _digp->pubkey_algo = v->pubkey_algo;
00883         }
00884 
00885         p = ((byte *)v) + sizeof(*v);
00886         p = pgpPrtPubkeyParams(v->pubkey_algo, p, h, hlen);
00887         if (!(tag == PGPTAG_PUBLIC_KEY || tag == PGPTAG_PUBLIC_SUBKEY))
00888             p = pgpPrtSeckeyParams(v->pubkey_algo, p, h, hlen);
00889         rc = 0;
00890     }   break;
00891     default:
00892         rc = 1;
00893         break;
00894     }
00895     return rc;
00896 }
00897 
00898 /*@-boundswrite@*/
00899 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
00900         /*@globals _digp @*/
00901         /*@modifies *_digp @*/
00902 {
00903     pgpPrtVal("", pgpTagTbl, tag);
00904     if (_print)
00905         fprintf(stderr, " \"%.*s\"", (int)hlen, (const char *)h);
00906     pgpPrtNL();
00907     if (_digp) {
00908         char * t;
00909         _digp->userid = t = memcpy(xmalloc(hlen+1), h, hlen);
00910         t[hlen] = '\0';
00911     }
00912     return 0;
00913 }
00914 /*@=boundswrite@*/
00915 
00916 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
00917 {
00918     int i = hlen;
00919 
00920     pgpPrtVal("", pgpTagTbl, tag);
00921     if (_print)
00922         fprintf(stderr, " ");
00923     while (i > 0) {
00924         int j;
00925         if (*h >= ' ' && *h <= 'z') {
00926             if (_print)
00927                 fprintf(stderr, "%s", (const char *)h);
00928             j = strlen(h);
00929             while (h[j] == '\0')
00930                 j++;
00931         } else {
00932             pgpPrtHex("", h, i);
00933             j = i;
00934         }
00935         i -= j;
00936         h += j;
00937     }
00938     pgpPrtNL();
00939     return 0;
00940 }
00941 
00942 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
00943 {
00944     unsigned int val = *pkt;
00945     unsigned int pktlen;
00946     pgpTag tag;
00947     unsigned int plen;
00948     const byte *h;
00949     unsigned int hlen = 0;
00950     int rc = 0;
00951 
00952     /* XXX can't deal with these. */
00953     if (!(val & 0x80))
00954         return -1;
00955 
00956     if (val & 0x40) {
00957         tag = (val & 0x3f);
00958         plen = pgpLen(pkt+1, &hlen);
00959     } else {
00960         tag = (val >> 2) & 0xf;
00961         plen = (1 << (val & 0x3));
00962         hlen = pgpGrab(pkt+1, plen);
00963     }
00964 
00965     pktlen = 1 + plen + hlen;
00966     if (pktlen > pleft)
00967         return -1;
00968 
00969     h = pkt + 1 + plen;
00970     switch (tag) {
00971     case PGPTAG_SIGNATURE:
00972         rc = pgpPrtSig(tag, h, hlen);
00973         break;
00974     case PGPTAG_PUBLIC_KEY:
00975     case PGPTAG_PUBLIC_SUBKEY:
00976         rc = pgpPrtKey(tag, h, hlen);
00977         break;
00978     case PGPTAG_SECRET_KEY:
00979     case PGPTAG_SECRET_SUBKEY:
00980         rc = pgpPrtKey(tag, h, hlen);
00981         break;
00982     case PGPTAG_USER_ID:
00983         rc = pgpPrtUserID(tag, h, hlen);
00984         break;
00985     case PGPTAG_COMMENT:
00986     case PGPTAG_COMMENT_OLD:
00987         rc = pgpPrtComment(tag, h, hlen);
00988         break;
00989 
00990     case PGPTAG_RESERVED:
00991     case PGPTAG_PUBLIC_SESSION_KEY:
00992     case PGPTAG_SYMMETRIC_SESSION_KEY:
00993     case PGPTAG_COMPRESSED_DATA:
00994     case PGPTAG_SYMMETRIC_DATA:
00995     case PGPTAG_MARKER:
00996     case PGPTAG_LITERAL_DATA:
00997     case PGPTAG_TRUST:
00998     case PGPTAG_PHOTOID:
00999     case PGPTAG_ENCRYPTED_MDC:
01000     case PGPTAG_MDC:
01001     case PGPTAG_PRIVATE_60:
01002     case PGPTAG_PRIVATE_62:
01003     case PGPTAG_CONTROL:
01004     default:
01005         pgpPrtVal("", pgpTagTbl, tag);
01006         pgpPrtHex("", h, hlen);
01007         pgpPrtNL();
01008         break;
01009     }
01010 
01011     return (rc ? -1 : pktlen);
01012 }
01013 
01014 pgpDig pgpNewDig(void)
01015 {
01016     pgpDig dig = xcalloc(1, sizeof(*dig));
01017     return dig;
01018 }
01019 
01020 /*@-boundswrite@*/
01021 void pgpCleanDig(pgpDig dig)
01022 {
01023     if (dig != NULL) {
01024         int i;
01025         dig->signature.userid = _free(dig->signature.userid);
01026         dig->pubkey.userid = _free(dig->pubkey.userid);
01027         dig->signature.hash = _free(dig->signature.hash);
01028         dig->pubkey.hash = _free(dig->pubkey.hash);
01029         /*@-unqualifiedtrans@*/ /* FIX: double indirection */
01030         for (i = 0; i < 4; i++) {
01031             dig->signature.params[i] = _free(dig->signature.params[i]);
01032             dig->pubkey.params[i] = _free(dig->pubkey.params[i]);
01033         }
01034         /*@=unqualifiedtrans@*/
01035 
01036         memset(&dig->signature, 0, sizeof(dig->signature));
01037         memset(&dig->pubkey, 0, sizeof(dig->pubkey));
01038 
01039         dig->md5 = _free(dig->md5);
01040         dig->sha1 = _free(dig->sha1);
01041         mpnfree(&dig->hm);
01042         mpnfree(&dig->r);
01043         mpnfree(&dig->s);
01044 
01045         (void) rsapkFree(&dig->rsa_pk);
01046         mpnfree(&dig->m);
01047         mpnfree(&dig->c);
01048         mpnfree(&dig->rsahm);
01049     }
01050 /*@-nullstate@*/
01051     return;
01052 /*@=nullstate@*/
01053 }
01054 /*@=boundswrite@*/
01055 
01056 pgpDig pgpFreeDig(/*@only@*/ /*@null@*/ pgpDig dig)
01057         /*@modifies dig @*/
01058 {
01059     if (dig != NULL) {
01060 
01061         /* DUmp the signature/pubkey data. */
01062         pgpCleanDig(dig);
01063 
01064         /*@-branchstate@*/
01065         if (dig->hdrsha1ctx != NULL)
01066             (void) rpmDigestFinal(dig->hdrsha1ctx, NULL, NULL, 0);
01067         /*@=branchstate@*/
01068         dig->hdrsha1ctx = NULL;
01069 
01070         /*@-branchstate@*/
01071         if (dig->sha1ctx != NULL)
01072             (void) rpmDigestFinal(dig->sha1ctx, NULL, NULL, 0);
01073         /*@=branchstate@*/
01074         dig->sha1ctx = NULL;
01075 
01076         mpbfree(&dig->p);
01077         mpbfree(&dig->q);
01078         mpnfree(&dig->g);
01079         mpnfree(&dig->y);
01080         mpnfree(&dig->hm);
01081         mpnfree(&dig->r);
01082         mpnfree(&dig->s);
01083 
01084 #ifdef  NOTYET
01085         /*@-branchstate@*/
01086         if (dig->hdrmd5ctx != NULL)
01087             (void) rpmDigestFinal(dig->hdrmd5ctx, NULL, NULL, 0);
01088         /*@=branchstate@*/
01089         dig->hdrmd5ctx = NULL;
01090 #endif
01091 
01092         /*@-branchstate@*/
01093         if (dig->md5ctx != NULL)
01094             (void) rpmDigestFinal(dig->md5ctx, NULL, NULL, 0);
01095         /*@=branchstate@*/
01096         dig->md5ctx = NULL;
01097 
01098         mpbfree(&dig->rsa_pk.n);
01099         mpnfree(&dig->rsa_pk.e);
01100         mpnfree(&dig->m);
01101         mpnfree(&dig->c);
01102         mpnfree(&dig->hm);
01103 
01104         dig = _free(dig);
01105     }
01106     return dig;
01107 }
01108 
01109 int pgpPrtPkts(const byte * pkts, unsigned int pktlen, pgpDig dig, int printing)
01110         /*@globals _dig, _digp, _print @*/
01111         /*@modifies _dig, _digp, *_digp, _print @*/
01112 {
01113     unsigned int val = *pkts;
01114     const byte *p;
01115     unsigned int pleft;
01116     int len;
01117 
01118     _print = printing;
01119     _dig = dig;
01120     if (dig != NULL && (val & 0x80)) {
01121         pgpTag tag = (val & 0x40) ? (val & 0x3f) : ((val >> 2) & 0xf);
01122         _digp = (tag == PGPTAG_SIGNATURE) ? &_dig->signature : &_dig->pubkey;
01123         _digp->tag = tag;
01124     } else
01125         _digp = NULL;
01126 
01127     for (p = pkts, pleft = pktlen; p < (pkts + pktlen); p += len, pleft -= len) {
01128         len = pgpPrtPkt(p, pleft);
01129         if (len <= 0)
01130             return len;
01131         if (len > pleft)        /* XXX shouldn't happen */
01132             break;
01133     }
01134     return 0;
01135 }
01136 
01137 /*@-boundswrite@*/
01138 pgpArmor pgpReadPkts(const char * fn, const byte ** pkt, size_t * pktlen)
01139 {
01140     const byte * b = NULL;
01141     ssize_t blen;
01142     const char * enc = NULL;
01143     const char * crcenc = NULL;
01144     byte * dec;
01145     byte * crcdec;
01146     size_t declen;
01147     size_t crclen;
01148     uint32_t crcpkt, crc;
01149     const char * armortype = NULL;
01150     char * t, * te;
01151     int pstate = 0;
01152     pgpArmor ec = PGPARMOR_ERROR;       /* XXX assume failure */
01153     int rc;
01154 
01155     rc = rpmioSlurp(fn, &b, &blen);
01156     if (rc || b == NULL || blen <= 0) {
01157         goto exit;
01158     }
01159 
01160     if (pgpIsPkt(b)) {
01161 #ifdef NOTYET   /* XXX ASCII Pubkeys only, please. */
01162         ec = 0; /* XXX fish out pkt type. */
01163 #endif
01164         goto exit;
01165     }
01166 
01167 #define TOKEQ(_s, _tok) (!strncmp((_s), (_tok), sizeof(_tok)-1))
01168 
01169     for (t = (char *)b; t && *t; t = te) {
01170         if ((te = strchr(t, '\n')) == NULL)
01171             te = t + strlen(t);
01172         else
01173             te++;
01174 
01175         switch (pstate) {
01176         case 0:
01177             armortype = NULL;
01178             if (!TOKEQ(t, "-----BEGIN PGP "))
01179                 continue;
01180             t += sizeof("-----BEGIN PGP ")-1;
01181 
01182             rc = pgpValTok(pgpArmorTbl, t, te);
01183             if (rc < 0)
01184                 goto exit;
01185             if (rc != PGPARMOR_PUBKEY)  /* XXX ASCII Pubkeys only, please. */
01186                 continue;
01187             armortype = t;
01188 
01189             t = te - (sizeof("-----\n")-1);
01190             if (!TOKEQ(t, "-----\n"))
01191                 continue;
01192             *t = '\0';
01193             pstate++;
01194             /*@switchbreak@*/ break;
01195         case 1:
01196             enc = NULL;
01197             rc = pgpValTok(pgpArmorKeyTbl, t, te);
01198             if (rc >= 0)
01199                 continue;
01200             if (*t != '\n') {
01201                 pstate = 0;
01202                 continue;
01203             }
01204             enc = te;           /* Start of encoded packets */
01205             pstate++;
01206             /*@switchbreak@*/ break;
01207         case 2:
01208             crcenc = NULL;
01209             if (*t != '=')
01210                 continue;
01211             *t++ = '\0';        /* Terminate encoded packets */
01212             crcenc = t;         /* Start of encoded crc */
01213             pstate++;
01214             /*@switchbreak@*/ break;
01215         case 3:
01216             pstate = 0;
01217             if (!TOKEQ(t, "-----END PGP "))
01218                 goto exit;
01219             *t = '\0';          /* Terminate encoded crc */
01220             t += sizeof("-----END PGP ")-1;
01221 
01222             if (armortype == NULL) /* XXX can't happen */
01223                 continue;
01224             rc = strncmp(t, armortype, strlen(armortype));
01225             if (rc)
01226                 continue;
01227 
01228             t = te - (sizeof("-----\n")-1);
01229             if (!TOKEQ(t, "-----\n"))
01230                 goto exit;
01231 
01232             if (b64decode(crcenc, (void **)&crcdec, &crclen) != 0)
01233                 continue;
01234             crcpkt = pgpGrab(crcdec, crclen);
01235             crcdec = _free(crcdec);
01236             if (b64decode(enc, (void **)&dec, &declen) != 0)
01237                 goto exit;
01238             crc = pgpCRC(dec, declen);
01239             if (crcpkt != crc)
01240                 goto exit;
01241             b = _free(b);
01242             b = dec;
01243             blen = declen;
01244             ec = PGPARMOR_PUBKEY;       /* XXX ASCII Pubkeys only, please. */
01245             goto exit;
01246             /*@notreached@*/ /*@switchbreak@*/ break;
01247         }
01248     }
01249     ec = PGPARMOR_NONE;
01250 
01251 exit:
01252     if (ec > PGPARMOR_NONE && pkt)
01253         *pkt = b;
01254     else if (b != NULL)
01255         b = _free(b);
01256     if (pktlen)
01257         *pktlen = blen;
01258     return ec;
01259 }
01260 /*@=boundswrite@*/
01261 
01262 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01263 {
01264     const char * enc;
01265     char * t;
01266     size_t nt;
01267     char * val;
01268     int lc;
01269 
01270     nt = ((ns + 2) / 3) * 4;
01271     /*@-globs@*/
01272     /* Add additional bytes necessary for eol string(s). */
01273     if (b64encode_chars_per_line > 0 && b64encode_eolstr != NULL) {
01274         lc = (nt + b64encode_chars_per_line - 1) / b64encode_chars_per_line;
01275        if (((nt + b64encode_chars_per_line - 1) % b64encode_chars_per_line) != 0)
01276         ++lc;
01277         nt += lc * strlen(b64encode_eolstr);
01278     }
01279     /*@=globs@*/
01280 
01281     nt += 512;  /* XXX slop for armor and crc */
01282 
01283 /*@-boundswrite@*/
01284     val = t = xmalloc(nt + 1);
01285     *t = '\0';
01286     t = stpcpy(t, "-----BEGIN PGP ");
01287     t = stpcpy(t, pgpValStr(pgpArmorTbl, atype));
01288     /*@-globs@*/
01289     t = stpcpy( stpcpy(t, "-----\nVersion: rpm-"), VERSION);
01290     /*@=globs@*/
01291     t = stpcpy(t, " (beecrypt-3.0.0)\n\n");
01292 
01293     if ((enc = b64encode(s, ns)) != NULL) {
01294         t = stpcpy(t, enc);
01295         enc = _free(enc);
01296         if ((enc = b64crc(s, ns)) != NULL) {
01297             *t++ = '=';
01298             t = stpcpy(t, enc);
01299             enc = _free(enc);
01300         }
01301     }
01302         
01303     t = stpcpy(t, "-----END PGP ");
01304     t = stpcpy(t, pgpValStr(pgpArmorTbl, atype));
01305     t = stpcpy(t, "-----\n");
01306 /*@=boundswrite@*/
01307 
01308     return val;
01309 }
01310 
01311 /*@=boundsread@*/

Generated on Wed Jul 21 16:07:00 2004 for rpm by doxygen1.2.18