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 static int _crypto_initialized = 0;
00021 
00022 /*@unchecked@*/ /*@null@*/
00023 static pgpDig _dig = NULL;
00024 
00025 /*@unchecked@*/ /*@null@*/
00026 static pgpDigParams _digp = NULL;
00027 
00028 struct pgpValTbl_s pgpSigTypeTbl[] = {
00029     { PGPSIGTYPE_BINARY,        "Binary document signature" },
00030     { PGPSIGTYPE_TEXT,          "Text document signature" },
00031     { PGPSIGTYPE_STANDALONE,    "Standalone signature" },
00032     { PGPSIGTYPE_GENERIC_CERT,  "Generic certification of a User ID and Public Key" },
00033     { PGPSIGTYPE_PERSONA_CERT,  "Persona certification of a User ID and Public Key" },
00034     { PGPSIGTYPE_CASUAL_CERT,   "Casual certification of a User ID and Public Key" },
00035     { PGPSIGTYPE_POSITIVE_CERT, "Positive certification of a User ID and Public Key" },
00036     { PGPSIGTYPE_SUBKEY_BINDING,"Subkey Binding Signature" },
00037     { PGPSIGTYPE_SIGNED_KEY,    "Signature directly on a key" },
00038     { PGPSIGTYPE_KEY_REVOKE,    "Key revocation signature" },
00039     { PGPSIGTYPE_SUBKEY_REVOKE, "Subkey revocation signature" },
00040     { PGPSIGTYPE_CERT_REVOKE,   "Certification revocation signature" },
00041     { PGPSIGTYPE_TIMESTAMP,     "Timestamp signature" },
00042     { -1,                       "Unknown signature type" },
00043 };
00044 
00045 struct pgpValTbl_s pgpPubkeyTbl[] = {
00046     { PGPPUBKEYALGO_RSA,        "RSA" },
00047     { PGPPUBKEYALGO_RSA_ENCRYPT,"RSA(Encrypt-Only)" },
00048     { PGPPUBKEYALGO_RSA_SIGN,   "RSA(Sign-Only)" },
00049     { PGPPUBKEYALGO_ELGAMAL_ENCRYPT,"Elgamal(Encrypt-Only)" },
00050     { PGPPUBKEYALGO_DSA,        "DSA" },
00051     { PGPPUBKEYALGO_EC,         "Elliptic Curve" },
00052     { PGPPUBKEYALGO_ECDSA,      "ECDSA" },
00053     { PGPPUBKEYALGO_ELGAMAL,    "Elgamal" },
00054     { PGPPUBKEYALGO_DH,         "Diffie-Hellman (X9.42)" },
00055     { -1,                       "Unknown public key algorithm" },
00056 };
00057 
00058 struct pgpValTbl_s pgpSymkeyTbl[] = {
00059     { PGPSYMKEYALGO_PLAINTEXT,  "Plaintext" },
00060     { PGPSYMKEYALGO_IDEA,       "IDEA" },
00061     { PGPSYMKEYALGO_TRIPLE_DES, "3DES" },
00062     { PGPSYMKEYALGO_CAST5,      "CAST5" },
00063     { PGPSYMKEYALGO_BLOWFISH,   "BLOWFISH" },
00064     { PGPSYMKEYALGO_SAFER,      "SAFER" },
00065     { PGPSYMKEYALGO_DES_SK,     "DES/SK" },
00066     { PGPSYMKEYALGO_AES_128,    "AES(128-bit key)" },
00067     { PGPSYMKEYALGO_AES_192,    "AES(192-bit key)" },
00068     { PGPSYMKEYALGO_AES_256,    "AES(256-bit key)" },
00069     { PGPSYMKEYALGO_TWOFISH,    "TWOFISH(256-bit key)" },
00070     { PGPSYMKEYALGO_NOENCRYPT,  "no encryption" },
00071     { -1,                       "Unknown symmetric key algorithm" },
00072 };
00073 
00074 struct pgpValTbl_s pgpCompressionTbl[] = {
00075     { PGPCOMPRESSALGO_NONE,     "Uncompressed" },
00076     { PGPCOMPRESSALGO_ZIP,      "ZIP" },
00077     { PGPCOMPRESSALGO_ZLIB,     "ZLIB" },
00078     { PGPCOMPRESSALGO_BZIP2,    "BZIP2" },
00079     { -1,                       "Unknown compression algorithm" },
00080 };
00081 
00082 struct pgpValTbl_s pgpHashTbl[] = {
00083     { PGPHASHALGO_MD5,          "MD5" },
00084     { PGPHASHALGO_SHA1,         "SHA1" },
00085     { PGPHASHALGO_RIPEMD160,    "RIPEMD160" },
00086     { PGPHASHALGO_MD2,          "MD2" },
00087     { PGPHASHALGO_TIGER192,     "TIGER192" },
00088     { PGPHASHALGO_HAVAL_5_160,  "HAVAL-5-160" },
00089     { PGPHASHALGO_SHA256,       "SHA256" },
00090     { PGPHASHALGO_SHA384,       "SHA384" },
00091     { PGPHASHALGO_SHA512,       "SHA512" },
00092     { -1,                       "Unknown hash algorithm" },
00093 };
00094 
00095 /*@-exportlocal -exportheadervar@*/
00096 /*@observer@*/ /*@unchecked@*/
00097 struct pgpValTbl_s pgpKeyServerPrefsTbl[] = {
00098     { 0x80,                     "No-modify" },
00099     { -1,                       "Unknown key server preference" },
00100 };
00101 /*@=exportlocal =exportheadervar@*/
00102 
00103 struct pgpValTbl_s pgpSubTypeTbl[] = {
00104     { PGPSUBTYPE_SIG_CREATE_TIME,"signature creation time" },
00105     { PGPSUBTYPE_SIG_EXPIRE_TIME,"signature expiration time" },
00106     { PGPSUBTYPE_EXPORTABLE_CERT,"exportable certification" },
00107     { PGPSUBTYPE_TRUST_SIG,     "trust signature" },
00108     { PGPSUBTYPE_REGEX,         "regular expression" },
00109     { PGPSUBTYPE_REVOCABLE,     "revocable" },
00110     { PGPSUBTYPE_KEY_EXPIRE_TIME,"key expiration time" },
00111     { PGPSUBTYPE_ARR,           "additional recipient request" },
00112     { PGPSUBTYPE_PREFER_SYMKEY, "preferred symmetric algorithms" },
00113     { PGPSUBTYPE_REVOKE_KEY,    "revocation key" },
00114     { PGPSUBTYPE_ISSUER_KEYID,  "issuer key ID" },
00115     { PGPSUBTYPE_NOTATION,      "notation data" },
00116     { PGPSUBTYPE_PREFER_HASH,   "preferred hash algorithms" },
00117     { PGPSUBTYPE_PREFER_COMPRESS,"preferred compression algorithms" },
00118     { PGPSUBTYPE_KEYSERVER_PREFERS,"key server preferences" },
00119     { PGPSUBTYPE_PREFER_KEYSERVER,"preferred key server" },
00120     { PGPSUBTYPE_PRIMARY_USERID,"primary user id" },
00121     { PGPSUBTYPE_POLICY_URL,    "policy URL" },
00122     { PGPSUBTYPE_KEY_FLAGS,     "key flags" },
00123     { PGPSUBTYPE_SIGNER_USERID, "signer's user id" },
00124     { PGPSUBTYPE_REVOKE_REASON, "reason for revocation" },
00125     { PGPSUBTYPE_FEATURES,      "features" },
00126     { PGPSUBTYPE_EMBEDDED_SIG,  "embedded signature" },
00127 
00128     { PGPSUBTYPE_INTERNAL_100,  "internal subpkt type 100" },
00129     { PGPSUBTYPE_INTERNAL_101,  "internal subpkt type 101" },
00130     { PGPSUBTYPE_INTERNAL_102,  "internal subpkt type 102" },
00131     { PGPSUBTYPE_INTERNAL_103,  "internal subpkt type 103" },
00132     { PGPSUBTYPE_INTERNAL_104,  "internal subpkt type 104" },
00133     { PGPSUBTYPE_INTERNAL_105,  "internal subpkt type 105" },
00134     { PGPSUBTYPE_INTERNAL_106,  "internal subpkt type 106" },
00135     { PGPSUBTYPE_INTERNAL_107,  "internal subpkt type 107" },
00136     { PGPSUBTYPE_INTERNAL_108,  "internal subpkt type 108" },
00137     { PGPSUBTYPE_INTERNAL_109,  "internal subpkt type 109" },
00138     { PGPSUBTYPE_INTERNAL_110,  "internal subpkt type 110" },
00139     { -1,                       "Unknown signature subkey type" },
00140 };
00141 
00142 struct pgpValTbl_s pgpTagTbl[] = {
00143     { PGPTAG_PUBLIC_SESSION_KEY,"Public-Key Encrypted Session Key" },
00144     { PGPTAG_SIGNATURE,         "Signature" },
00145     { PGPTAG_SYMMETRIC_SESSION_KEY,"Symmetric-Key Encrypted Session Key" },
00146     { PGPTAG_ONEPASS_SIGNATURE, "One-Pass Signature" },
00147     { PGPTAG_SECRET_KEY,        "Secret Key" },
00148     { PGPTAG_PUBLIC_KEY,        "Public Key" },
00149     { PGPTAG_SECRET_SUBKEY,     "Secret Subkey" },
00150     { PGPTAG_COMPRESSED_DATA,   "Compressed Data" },
00151     { PGPTAG_SYMMETRIC_DATA,    "Symmetrically Encrypted Data" },
00152     { PGPTAG_MARKER,            "Marker" },
00153     { PGPTAG_LITERAL_DATA,      "Literal Data" },
00154     { PGPTAG_TRUST,             "Trust" },
00155     { PGPTAG_USER_ID,           "User ID" },
00156     { PGPTAG_PUBLIC_SUBKEY,     "Public Subkey" },
00157     { PGPTAG_COMMENT_OLD,       "Comment (from OpenPGP draft)" },
00158     { PGPTAG_PHOTOID,           "PGP's photo ID" },
00159     { PGPTAG_ENCRYPTED_MDC,     "Integrity protected encrypted data" },
00160     { PGPTAG_MDC,               "Manipulaion detection code packet" },
00161     { PGPTAG_PRIVATE_60,        "Private #60" },
00162     { PGPTAG_COMMENT,           "Comment" },
00163     { PGPTAG_PRIVATE_62,        "Private #62" },
00164     { PGPTAG_CONTROL,           "Control (GPG)" },
00165     { -1,                       "Unknown packet tag" },
00166 };
00167 
00168 struct pgpValTbl_s pgpArmorTbl[] = {
00169     { PGPARMOR_MESSAGE,         "MESSAGE" },
00170     { PGPARMOR_PUBKEY,          "PUBLIC KEY BLOCK" },
00171     { PGPARMOR_SIGNATURE,       "SIGNATURE" },
00172     { PGPARMOR_SIGNED_MESSAGE,  "SIGNED MESSAGE" },
00173     { PGPARMOR_FILE,            "ARMORED FILE" },
00174     { PGPARMOR_PRIVKEY,         "PRIVATE KEY BLOCK" },
00175     { PGPARMOR_SECKEY,          "SECRET KEY BLOCK" },
00176     { -1,                       "Unknown armor block" }
00177 };
00178 
00179 struct pgpValTbl_s pgpArmorKeyTbl[] = {
00180     { PGPARMORKEY_VERSION,      "Version: " },
00181     { PGPARMORKEY_COMMENT,      "Comment: " },
00182     { PGPARMORKEY_MESSAGEID,    "MessageID: " },
00183     { PGPARMORKEY_HASH,         "Hash: " },
00184     { PGPARMORKEY_CHARSET,      "Charset: " },
00185     { -1,                       "Unknown armor key" }
00186 };
00187 
00193 /*@unused@*/ static inline /*@null@*/ void *
00194 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p)
00195         /*@modifies p @*/
00196 {
00197     if (p != NULL)      free((void *)p);
00198     return NULL;
00199 }
00200 
00201 static void pgpPrtNL(void)
00202         /*@globals fileSystem @*/
00203         /*@modifies fileSystem @*/
00204 {
00205     if (!_print) return;
00206     fprintf(stderr, "\n");
00207 }
00208 
00209 static void pgpPrtInt(const char *pre, int i)
00210         /*@globals fileSystem @*/
00211         /*@modifies fileSystem @*/
00212 {
00213     if (!_print) return;
00214     if (pre && *pre)
00215         fprintf(stderr, "%s", pre);
00216     fprintf(stderr, " %d", i);
00217 }
00218 
00219 static void pgpPrtStr(const char *pre, const char *s)
00220         /*@globals fileSystem @*/
00221         /*@modifies fileSystem @*/
00222 {
00223     if (!_print) return;
00224     if (pre && *pre)
00225         fprintf(stderr, "%s", pre);
00226     fprintf(stderr, " %s", s);
00227 }
00228 
00229 static void pgpPrtHex(const char *pre, const byte *p, unsigned int plen)
00230         /*@globals fileSystem @*/
00231         /*@modifies fileSystem @*/
00232 {
00233     if (!_print) return;
00234     if (pre && *pre)
00235         fprintf(stderr, "%s", pre);
00236     fprintf(stderr, " %s", pgpHexStr(p, plen));
00237 }
00238 
00239 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
00240         /*@globals fileSystem @*/
00241         /*@modifies fileSystem @*/
00242 {
00243     if (!_print) return;
00244     if (pre && *pre)
00245         fprintf(stderr, "%s", pre);
00246     fprintf(stderr, "%s(%u)", pgpValStr(vs, val), (unsigned)val);
00247 }
00248 
00251 /*@unused@*/ static /*@observer@*/
00252 const char * pgpMpiHex(const byte *p)
00253         /*@*/
00254 {
00255     static char prbuf[2048];
00256     char *t = prbuf;
00257     t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
00258     return prbuf;
00259 }
00260 
00261 /*@-boundswrite@*/
00265 static int pgpMpiSet(const char * pre, int lbits,
00266                 /*@out@*/ void *dest, const byte * p, const byte * pend)
00267         /*@globals fileSystem @*/
00268         /*@modifies dest, fileSystem @*/
00269 {
00270     unsigned int mbits = pgpMpiBits(p);
00271     unsigned int nbits;
00272     unsigned int nbytes;
00273     char *t = dest;
00274     unsigned int ix;
00275 
00276     if ((p + ((mbits+7) >> 3)) > pend)
00277         return 1;
00278 
00279     if (mbits > lbits)
00280         return 1;
00281 
00282     nbits = (lbits > mbits ? lbits : mbits);
00283     nbytes = ((nbits + 7) >> 3);
00284     ix = (nbits - mbits) >> 3;
00285 
00286 if (_debug)
00287 fprintf(stderr, "*** mbits %u nbits %u nbytes %u ix %u\n", mbits, nbits, nbytes, ix);
00288     if (ix > 0) memset(t, '\0', ix);
00289     memcpy(t+ix, p+2, nbytes-ix);
00290 if (_debug)
00291 fprintf(stderr, "*** %s %s\n", pre, pgpHexStr(dest, nbytes));
00292 
00293     return 0;
00294 }
00295 
00299 static SECItem *pgpMpiItem(PRArenaPool *arena, /*@out@*/ SECItem *item, const byte *p)
00300         /*@globals fileSystem @*/
00301         /*@modifies dest, fileSystem @*/
00302 {
00303     unsigned int nbytes = pgpMpiLen(p)-2;
00304 
00305     if (item == NULL) {
00306         if ((item=SECITEM_AllocItem(arena, item, nbytes)) == NULL)
00307             return item;
00308     } else {
00309         if (arena != NULL)
00310             item->data = PORT_ArenaGrow(arena, item->data, item->len, nbytes);
00311         else
00312             item->data = PORT_Realloc(item->data, nbytes);
00313         
00314         if (item->data == NULL) {
00315             if (arena == NULL)
00316                 SECITEM_FreeItem(item, PR_TRUE);
00317             return NULL;
00318         }
00319     }
00320 
00321     memcpy(item->data, p+2, nbytes);
00322     item->len = nbytes;
00323     return item;
00324 }
00325 /*@=boundswrite@*/
00326 
00327 static SECKEYPublicKey *pgpNewPublicKey(KeyType type)
00328 {
00329     PRArenaPool *arena;
00330     SECKEYPublicKey *key;
00331     
00332     arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
00333     if (arena == NULL)
00334         return NULL;
00335     
00336     key = PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey));
00337     
00338     if (key == NULL) {
00339         PORT_FreeArena(arena, PR_FALSE);
00340         return NULL;
00341     }
00342     
00343     key->keyType = type;
00344     key->pkcs11ID = CK_INVALID_HANDLE;
00345     key->pkcs11Slot = NULL;
00346     key->arena = arena;
00347     return key;
00348 }
00349 
00350 static SECKEYPublicKey *pgpNewRSAKey(void)
00351 {
00352     return pgpNewPublicKey(rsaKey);
00353 }
00354 
00355 static SECKEYPublicKey *pgpNewDSAKey(void)
00356 {
00357     return pgpNewPublicKey(dsaKey);
00358 }
00359 
00360 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
00361 {
00362     const byte *p = h;
00363     unsigned plen;
00364     int i;
00365 
00366     while (hlen > 0) {
00367         i = pgpLen(p, &plen);
00368         p += i;
00369         hlen -= i;
00370 
00371         pgpPrtVal("    ", pgpSubTypeTbl, (p[0]&(~PGPSUBTYPE_CRITICAL)));
00372         if (p[0] & PGPSUBTYPE_CRITICAL)
00373             if (_print)
00374                 fprintf(stderr, " *CRITICAL*");
00375         switch (*p) {
00376         case PGPSUBTYPE_PREFER_SYMKEY:  /* preferred symmetric algorithms */
00377             for (i = 1; i < plen; i++)
00378                 pgpPrtVal(" ", pgpSymkeyTbl, p[i]);
00379             /*@switchbreak@*/ break;
00380         case PGPSUBTYPE_PREFER_HASH:    /* preferred hash algorithms */
00381             for (i = 1; i < plen; i++)
00382                 pgpPrtVal(" ", pgpHashTbl, p[i]);
00383             /*@switchbreak@*/ break;
00384         case PGPSUBTYPE_PREFER_COMPRESS:/* preferred compression algorithms */
00385             for (i = 1; i < plen; i++)
00386                 pgpPrtVal(" ", pgpCompressionTbl, p[i]);
00387             /*@switchbreak@*/ break;
00388         case PGPSUBTYPE_KEYSERVER_PREFERS:/* key server preferences */
00389             for (i = 1; i < plen; i++)
00390                 pgpPrtVal(" ", pgpKeyServerPrefsTbl, p[i]);
00391             /*@switchbreak@*/ break;
00392         case PGPSUBTYPE_SIG_CREATE_TIME:
00393 /*@-mods -mayaliasunique @*/
00394             if (_digp && !(_digp->saved & PGPDIG_SAVED_TIME) &&
00395                 (sigtype == PGPSIGTYPE_POSITIVE_CERT || sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT || sigtype == PGPSIGTYPE_STANDALONE))
00396             {
00397                 _digp->saved |= PGPDIG_SAVED_TIME;
00398                 memcpy(_digp->time, p+1, sizeof(_digp->time));
00399             }
00400 /*@=mods =mayaliasunique @*/
00401             /*@fallthrough@*/
00402         case PGPSUBTYPE_SIG_EXPIRE_TIME:
00403         case PGPSUBTYPE_KEY_EXPIRE_TIME:
00404             if ((plen - 1) == 4) {
00405                 time_t t = pgpGrab(p+1, plen-1);
00406                 if (_print)
00407                    fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00408             } else
00409                 pgpPrtHex("", p+1, plen-1);
00410             /*@switchbreak@*/ break;
00411 
00412         case PGPSUBTYPE_ISSUER_KEYID:   /* issuer key ID */
00413 /*@-mods -mayaliasunique @*/
00414             if (_digp && !(_digp->saved & PGPDIG_SAVED_ID) &&
00415                 (sigtype == PGPSIGTYPE_POSITIVE_CERT || sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT || sigtype == PGPSIGTYPE_STANDALONE))
00416             {
00417                 _digp->saved |= PGPDIG_SAVED_ID;
00418                 memcpy(_digp->signid, p+1, sizeof(_digp->signid));
00419             }
00420 /*@=mods =mayaliasunique @*/
00421             /*@fallthrough@*/
00422         case PGPSUBTYPE_EXPORTABLE_CERT:
00423         case PGPSUBTYPE_TRUST_SIG:
00424         case PGPSUBTYPE_REGEX:
00425         case PGPSUBTYPE_REVOCABLE:
00426         case PGPSUBTYPE_ARR:
00427         case PGPSUBTYPE_REVOKE_KEY:
00428         case PGPSUBTYPE_NOTATION:
00429         case PGPSUBTYPE_PREFER_KEYSERVER:
00430         case PGPSUBTYPE_PRIMARY_USERID:
00431         case PGPSUBTYPE_POLICY_URL:
00432         case PGPSUBTYPE_KEY_FLAGS:
00433         case PGPSUBTYPE_SIGNER_USERID:
00434         case PGPSUBTYPE_REVOKE_REASON:
00435         case PGPSUBTYPE_FEATURES:
00436         case PGPSUBTYPE_EMBEDDED_SIG:
00437         case PGPSUBTYPE_INTERNAL_100:
00438         case PGPSUBTYPE_INTERNAL_101:
00439         case PGPSUBTYPE_INTERNAL_102:
00440         case PGPSUBTYPE_INTERNAL_103:
00441         case PGPSUBTYPE_INTERNAL_104:
00442         case PGPSUBTYPE_INTERNAL_105:
00443         case PGPSUBTYPE_INTERNAL_106:
00444         case PGPSUBTYPE_INTERNAL_107:
00445         case PGPSUBTYPE_INTERNAL_108:
00446         case PGPSUBTYPE_INTERNAL_109:
00447         case PGPSUBTYPE_INTERNAL_110:
00448         default:
00449             pgpPrtHex("", p+1, plen-1);
00450             /*@switchbreak@*/ break;
00451         }
00452         pgpPrtNL();
00453         p += plen;
00454         hlen -= plen;
00455     }
00456     return 0;
00457 }
00458 
00459 /*@-varuse =readonlytrans @*/
00460 /*@observer@*/ /*@unchecked@*/
00461 static const char * pgpSigRSA[] = {
00462     " m**d =",
00463     NULL,
00464 };
00465 
00466 /*@observer@*/ /*@unchecked@*/
00467 static const char * pgpSigDSA[] = {
00468     "    r =",
00469     "    s =",
00470     NULL,
00471 };
00472 /*@=varuse =readonlytrans @*/
00473 
00474 #ifndef DSA_SUBPRIME_LEN
00475 #define DSA_SUBPRIME_LEN 20
00476 #endif
00477 
00478 static int pgpPrtSigParams(/*@unused@*/ pgpTag tag, byte pubkey_algo, byte sigtype,
00479                 const byte *p, const byte *h, unsigned int hlen)
00480         /*@globals fileSystem @*/
00481         /*@modifies fileSystem @*/
00482 {
00483     const byte * pend = h + hlen;
00484     int i;
00485     SECItem dsaraw;
00486     unsigned char dsabuf[2*DSA_SUBPRIME_LEN];
00487 
00488     dsaraw.type = 0;
00489     dsaraw.data = dsabuf;
00490     dsaraw.len = sizeof(dsabuf);
00491     
00492     for (i = 0; p < pend; i++, p += pgpMpiLen(p)) {
00493         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00494             if (i >= 1) break;
00495             if (_dig &&
00496         (sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT))
00497             {
00498                 switch (i) {
00499                 case 0:         /* m**d */
00500                     _dig->rsasig = pgpMpiItem(NULL, _dig->rsasig, p);
00501                     if (_dig->rsasig == NULL)
00502                         return 1;
00503                     /*@switchbreak@*/ break;
00504                 default:
00505                     /*@switchbreak@*/ break;
00506                 }
00507             }
00508             pgpPrtStr("", pgpSigRSA[i]);
00509         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00510             if (i >= 2) break;
00511             if (_dig &&
00512         (sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT))
00513             {
00514                 int xx;
00515                 xx = 0;
00516                 switch (i) {
00517                 case 0:
00518                     memset(dsaraw.data, '\0', 2*DSA_SUBPRIME_LEN);
00519                                 /* r */
00520                     xx = pgpMpiSet(pgpSigDSA[i], DSA_SUBPRIME_LEN*8, dsaraw.data, p, pend);
00521                     /*@switchbreak@*/ break;
00522                 case 1:         /* s */
00523                     xx = pgpMpiSet(pgpSigDSA[i], DSA_SUBPRIME_LEN*8, dsaraw.data + DSA_SUBPRIME_LEN, p, pend);
00524                     if (_dig->dsasig != NULL)
00525                         SECITEM_FreeItem(_dig->dsasig, PR_FALSE);
00526                     else if ((_dig->dsasig=SECITEM_AllocItem(NULL, NULL, 0)) == NULL) {
00527                         xx = 1;
00528                         /*@switchbreak@*/ break;
00529                     }
00530                     if (DSAU_EncodeDerSig(_dig->dsasig, &dsaraw) != SECSuccess)
00531                         xx = 1;
00532                     /*@switchbreak@*/ break;
00533                 default:
00534                     xx = 1;
00535                     /*@switchbreak@*/ break;
00536                 }
00537                 if (xx) return xx;
00538             }
00539             pgpPrtStr("", pgpSigDSA[i]);
00540         } else {
00541             if (_print)
00542                 fprintf(stderr, "%7d", i);
00543         }
00544         pgpPrtStr("", pgpMpiStr(p));
00545         pgpPrtNL();
00546     }
00547 
00548     return 0;
00549 }
00550 
00551 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
00552         /*@globals _digp @*/
00553         /*@modifies *_digp @*/
00554 {
00555     byte version = h[0];
00556     byte * p;
00557     unsigned plen;
00558     int rc;
00559 
00560     switch (version) {
00561     case 3:
00562     {   pgpPktSigV3 v = (pgpPktSigV3)h;
00563         time_t t;
00564 
00565         if (v->hashlen != 5)
00566             return 1;
00567 
00568         pgpPrtVal("V3 ", pgpTagTbl, tag);
00569         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00570         pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
00571         pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
00572         pgpPrtNL();
00573         t = pgpGrab(v->time, sizeof(v->time));
00574         if (_print)
00575             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00576         pgpPrtNL();
00577         pgpPrtHex(" signer keyid", v->signid, sizeof(v->signid));
00578         plen = pgpGrab(v->signhash16, sizeof(v->signhash16));
00579         pgpPrtHex(" signhash16", v->signhash16, sizeof(v->signhash16));
00580         pgpPrtNL();
00581 
00582         if (_digp && _digp->pubkey_algo == 0) {
00583             _digp->version = v->version;
00584             _digp->hashlen = v->hashlen;
00585             _digp->sigtype = v->sigtype;
00586             _digp->hash = memcpy(xmalloc(v->hashlen), &v->sigtype, v->hashlen);
00587             memcpy(_digp->time, v->time, sizeof(_digp->time));
00588             memcpy(_digp->signid, v->signid, sizeof(_digp->signid));
00589             _digp->pubkey_algo = v->pubkey_algo;
00590             _digp->hash_algo = v->hash_algo;
00591             memcpy(_digp->signhash16, v->signhash16, sizeof(_digp->signhash16));
00592         }
00593 
00594         p = ((byte *)v) + sizeof(*v);
00595         rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen);
00596     }   break;
00597     case 4:
00598     {   pgpPktSigV4 v = (pgpPktSigV4)h;
00599 
00600         pgpPrtVal("V4 ", pgpTagTbl, tag);
00601         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00602         pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
00603         pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
00604         pgpPrtNL();
00605 
00606         p = &v->hashlen[0];
00607         plen = pgpGrab(v->hashlen, sizeof(v->hashlen));
00608         p += sizeof(v->hashlen);
00609 
00610         if ((p + plen) > (h + hlen))
00611             return 1;
00612 
00613 if (_debug && _print)
00614 fprintf(stderr, "   hash[%u] -- %s\n", plen, pgpHexStr(p, plen));
00615         if (_digp && _digp->pubkey_algo == 0) {
00616             _digp->hashlen = sizeof(*v) + plen;
00617             _digp->hash = memcpy(xmalloc(_digp->hashlen), v, _digp->hashlen);
00618         }
00619         (void) pgpPrtSubType(p, plen, v->sigtype);
00620         p += plen;
00621 
00622         plen = pgpGrab(p,2);
00623         p += 2;
00624 
00625         if ((p + plen) > (h + hlen))
00626             return 1;
00627 
00628 if (_debug && _print)
00629 fprintf(stderr, " unhash[%u] -- %s\n", plen, pgpHexStr(p, plen));
00630         (void) pgpPrtSubType(p, plen, v->sigtype);
00631         p += plen;
00632 
00633         plen = pgpGrab(p,2);
00634         pgpPrtHex(" signhash16", p, 2);
00635         pgpPrtNL();
00636 
00637         if (_digp && _digp->pubkey_algo == 0) {
00638             _digp->version = v->version;
00639             _digp->sigtype = v->sigtype;
00640             _digp->pubkey_algo = v->pubkey_algo;
00641             _digp->hash_algo = v->hash_algo;
00642             memcpy(_digp->signhash16, p, sizeof(_digp->signhash16));
00643         }
00644 
00645         p += 2;
00646         if (p > (h + hlen))
00647             return 1;
00648 
00649         rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen);
00650     }   break;
00651     default:
00652         rc = 1;
00653         break;
00654     }
00655     return rc;
00656 }
00657 
00658 /*@-varuse =readonlytrans @*/
00659 /*@observer@*/ /*@unchecked@*/
00660 static const char * pgpPublicRSA[] = {
00661     "    n =",
00662     "    e =",
00663     NULL,
00664 };
00665 
00666 /*@observer@*/ /*@unchecked@*/
00667 static const char * pgpSecretRSA[] = {
00668     "    d =",
00669     "    p =",
00670     "    q =",
00671     "    u =",
00672     NULL,
00673 };
00674 
00675 /*@observer@*/ /*@unchecked@*/
00676 static const char * pgpPublicDSA[] = {
00677     "    p =",
00678     "    q =",
00679     "    g =",
00680     "    y =",
00681     NULL,
00682 };
00683 
00684 /*@observer@*/ /*@unchecked@*/
00685 static const char * pgpSecretDSA[] = {
00686     "    x =",
00687     NULL,
00688 };
00689 
00690 /*@observer@*/ /*@unchecked@*/
00691 static const char * pgpPublicELGAMAL[] = {
00692     "    p =",
00693     "    g =",
00694     "    y =",
00695     NULL,
00696 };
00697 
00698 /*@observer@*/ /*@unchecked@*/
00699 static const char * pgpSecretELGAMAL[] = {
00700     "    x =",
00701     NULL,
00702 };
00703 /*@=varuse =readonlytrans @*/
00704 
00705 static const byte * pgpPrtPubkeyParams(byte pubkey_algo,
00706                 /*@returned@*/ const byte *p, const byte *h, unsigned int hlen)
00707         /*@globals fileSystem, internalState @*/
00708         /*@modifies fileSystem, internalState @*/
00709 {
00710     int i;
00711 
00712     for (i = 0; p < &h[hlen]; i++, p += pgpMpiLen(p)) {
00713         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00714             if (i >= 2) break;
00715             if (_dig) {
00716                 if (_dig->rsa == NULL) {
00717                     _dig->rsa = pgpNewRSAKey();
00718                     if (_dig->rsa == NULL)
00719                         break; /* error abort? */
00720                 }
00721                 switch (i) {
00722                 case 0:         /* n */
00723                     pgpMpiItem(_dig->rsa->arena, &_dig->rsa->u.rsa.modulus, p);
00724                     /*@switchbreak@*/ break;
00725                 case 1:         /* e */
00726                     pgpMpiItem(_dig->rsa->arena, &_dig->rsa->u.rsa.publicExponent, p);
00727                     /*@switchbreak@*/ break;
00728                 default:
00729                     /*@switchbreak@*/ break;
00730                 }
00731             }
00732             pgpPrtStr("", pgpPublicRSA[i]);
00733         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00734             if (i >= 4) break;
00735             if (_dig) {
00736                 if (_dig->dsa == NULL) {
00737                     _dig->dsa = pgpNewDSAKey();
00738                     if (_dig->dsa == NULL)
00739                         break; /* error abort? */
00740                 }
00741                 switch (i) {
00742                 case 0:         /* p */
00743                     pgpMpiItem(_dig->dsa->arena, &_dig->dsa->u.dsa.params.prime, p);
00744                     /*@switchbreak@*/ break;
00745                 case 1:         /* q */
00746                     pgpMpiItem(_dig->dsa->arena, &_dig->dsa->u.dsa.params.subPrime, p);
00747                     /*@switchbreak@*/ break;
00748                 case 2:         /* g */
00749                     pgpMpiItem(_dig->dsa->arena, &_dig->dsa->u.dsa.params.base, p);
00750                     /*@switchbreak@*/ break;
00751                 case 3:         /* y */
00752                     pgpMpiItem(_dig->dsa->arena, &_dig->dsa->u.dsa.publicValue, p);
00753                     /*@switchbreak@*/ break;
00754                 default:
00755                     /*@switchbreak@*/ break;
00756                 }
00757             }
00758             pgpPrtStr("", pgpPublicDSA[i]);
00759         } else if (pubkey_algo == PGPPUBKEYALGO_ELGAMAL_ENCRYPT) {
00760             if (i >= 3) break;
00761             pgpPrtStr("", pgpPublicELGAMAL[i]);
00762         } else {
00763             if (_print)
00764                 fprintf(stderr, "%7d", i);
00765         }
00766         pgpPrtStr("", pgpMpiStr(p));
00767         pgpPrtNL();
00768     }
00769 
00770     return p;
00771 }
00772 
00773 static const byte * pgpPrtSeckeyParams(/*@unused@*/ byte pubkey_algo,
00774                 /*@returned@*/ const byte *p, const byte *h, unsigned int hlen)
00775         /*@globals fileSystem @*/
00776         /*@modifies fileSystem @*/
00777 {
00778     int i;
00779 
00780     switch (*p) {
00781     case 0:
00782         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00783         break;
00784     case 255:
00785         p++;
00786         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00787         switch (p[1]) {
00788         case 0x00:
00789             pgpPrtVal(" simple ", pgpHashTbl, p[2]);
00790             p += 2;
00791             /*@innerbreak@*/ break;
00792         case 0x01:
00793             pgpPrtVal(" salted ", pgpHashTbl, p[2]);
00794             pgpPrtHex("", p+3, 8);
00795             p += 10;
00796             /*@innerbreak@*/ break;
00797         case 0x03:
00798             pgpPrtVal(" iterated/salted ", pgpHashTbl, p[2]);
00799             /*@-shiftnegative -shiftimplementation @*/ /* FIX: unsigned cast */
00800             i = (16 + (p[11] & 0xf)) << ((p[11] >> 4) + 6);
00801             /*@=shiftnegative =shiftimplementation @*/
00802             pgpPrtHex("", p+3, 8);
00803             pgpPrtInt(" iter", i);
00804             p += 11;
00805             /*@innerbreak@*/ break;
00806         }
00807         break;
00808     default:
00809         pgpPrtVal(" ", pgpSymkeyTbl, *p);
00810         pgpPrtHex(" IV", p+1, 8);
00811         p += 8;
00812         break;
00813     }
00814     pgpPrtNL();
00815 
00816     p++;
00817 
00818 #ifdef  NOTYET  /* XXX encrypted MPI's need to be handled. */
00819     for (i = 0; p < &h[hlen]; i++, p += pgpMpiLen(p)) {
00820         if (pubkey_algo == PGPPUBKEYALGO_RSA) {
00821             if (pgpSecretRSA[i] == NULL) break;
00822             pgpPrtStr("", pgpSecretRSA[i]);
00823         } else if (pubkey_algo == PGPPUBKEYALGO_DSA) {
00824             if (pgpSecretDSA[i] == NULL) break;
00825             pgpPrtStr("", pgpSecretDSA[i]);
00826         } else if (pubkey_algo == PGPPUBKEYALGO_ELGAMAL_ENCRYPT) {
00827             if (pgpSecretELGAMAL[i] == NULL) break;
00828             pgpPrtStr("", pgpSecretELGAMAL[i]);
00829         } else {
00830             if (_print)
00831                 fprintf(stderr, "%7d", i);
00832         }
00833         pgpPrtStr("", pgpMpiStr(p));
00834         pgpPrtNL();
00835     }
00836 #else
00837     pgpPrtHex(" secret", p, (hlen - (p - h) - 2));
00838     pgpPrtNL();
00839     p += (hlen - (p - h) - 2);
00840 #endif
00841     pgpPrtHex(" checksum", p, 2);
00842     pgpPrtNL();
00843 
00844     return p;
00845 }
00846 
00847 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
00848         /*@globals _digp @*/
00849         /*@modifies *_digp @*/
00850 {
00851     byte version = *h;
00852     const byte * p;
00853     unsigned plen;
00854     time_t t;
00855     int rc;
00856 
00857     switch (version) {
00858     case 3:
00859     {   pgpPktKeyV3 v = (pgpPktKeyV3)h;
00860         pgpPrtVal("V3 ", pgpTagTbl, tag);
00861         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00862         t = pgpGrab(v->time, sizeof(v->time));
00863         if (_print)
00864             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00865         plen = pgpGrab(v->valid, sizeof(v->valid));
00866         if (plen != 0)
00867             fprintf(stderr, " valid %u days", plen);
00868         pgpPrtNL();
00869 
00870         if (_digp && _digp->tag == tag) {
00871             _digp->version = v->version;
00872             memcpy(_digp->time, v->time, sizeof(_digp->time));
00873             _digp->pubkey_algo = v->pubkey_algo;
00874         }
00875 
00876         p = ((byte *)v) + sizeof(*v);
00877         p = pgpPrtPubkeyParams(v->pubkey_algo, p, h, hlen);
00878         rc = 0;
00879     }   break;
00880     case 4:
00881     {   pgpPktKeyV4 v = (pgpPktKeyV4)h;
00882         pgpPrtVal("V4 ", pgpTagTbl, tag);
00883         pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
00884         t = pgpGrab(v->time, sizeof(v->time));
00885         if (_print)
00886             fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
00887         pgpPrtNL();
00888 
00889         if (_digp && _digp->tag == tag) {
00890             _digp->version = v->version;
00891             memcpy(_digp->time, v->time, sizeof(_digp->time));
00892             _digp->pubkey_algo = v->pubkey_algo;
00893         }
00894 
00895         p = ((byte *)v) + sizeof(*v);
00896         p = pgpPrtPubkeyParams(v->pubkey_algo, p, h, hlen);
00897         if (!(tag == PGPTAG_PUBLIC_KEY || tag == PGPTAG_PUBLIC_SUBKEY))
00898             p = pgpPrtSeckeyParams(v->pubkey_algo, p, h, hlen);
00899         rc = 0;
00900     }   break;
00901     default:
00902         rc = 1;
00903         break;
00904     }
00905     return rc;
00906 }
00907 
00908 /*@-boundswrite@*/
00909 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
00910         /*@globals _digp @*/
00911         /*@modifies *_digp @*/
00912 {
00913     pgpPrtVal("", pgpTagTbl, tag);
00914     if (_print)
00915         fprintf(stderr, " \"%.*s\"", (int)hlen, (const char *)h);
00916     pgpPrtNL();
00917     if (_digp) {
00918         char * t;
00919         _digp->userid = t = memcpy(xmalloc(hlen+1), h, hlen);
00920         t[hlen] = '\0';
00921     }
00922     return 0;
00923 }
00924 /*@=boundswrite@*/
00925 
00926 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
00927 {
00928     int i = hlen;
00929 
00930     pgpPrtVal("", pgpTagTbl, tag);
00931     if (_print)
00932         fprintf(stderr, " ");
00933     while (i > 0) {
00934         int j;
00935         if (*h >= ' ' && *h <= 'z') {
00936             if (_print)
00937                 fprintf(stderr, "%s", (const char *)h);
00938             j = strlen(h);
00939             while (h[j] == '\0')
00940                 j++;
00941         } else {
00942             pgpPrtHex("", h, i);
00943             j = i;
00944         }
00945         i -= j;
00946         h += j;
00947     }
00948     pgpPrtNL();
00949     return 0;
00950 }
00951 
00952 int pgpPubkeyFingerprint(const byte * pkt, /*@unused@*/ unsigned int pktlen,
00953                 byte * keyid)
00954 {
00955     unsigned int val = *pkt;
00956     unsigned int plen, hlen;
00957     pgpTag tag;
00958     const uint8_t *se, *h;
00959     DIGEST_CTX ctx;
00960     int rc = -1;        /* assume failure. */
00961 
00962     if (!(val & 0x80))
00963         return rc;
00964 
00965     if (val & 0x40) {
00966         tag = (val & 0x3f);
00967         plen = pgpLen(pkt+1, &hlen);
00968     } else {
00969         tag = (val >> 2) & 0xf;
00970         plen = (1 << (val & 0x3));
00971         hlen = pgpGrab(pkt+1, plen);
00972     }
00973     if (pktlen > 0 && 1 + plen + hlen > pktlen)
00974         return rc;
00975     
00976     h = pkt + 1 + plen;
00977 
00978     switch (h[0]) {
00979     case 3:
00980       { pgpPktKeyV3 v = (pgpPktKeyV3) (h);
00981         se = (uint8_t *)(v + 1);
00982         switch (v->pubkey_algo) {
00983         case PGPPUBKEYALGO_RSA:
00984             se += pgpMpiLen(se);
00985             memmove(keyid, (se-8), 8);
00986             rc = 0;
00987             break;
00988         default:        /* TODO: md5 of mpi bodies (i.e. no length) */
00989             break;
00990         }
00991       } break;
00992     case 4:
00993       { pgpPktKeyV4 v = (pgpPktKeyV4) (h);
00994         uint8_t * d = NULL;
00995         uint8_t in[3];
00996         size_t dlen;
00997         int i;
00998 
00999         se = (uint8_t *)(v + 1);
01000         switch (v->pubkey_algo) {
01001         case PGPPUBKEYALGO_RSA:
01002             for (i = 0; i < 2; i++)
01003                 se += pgpMpiLen(se);
01004             break;
01005         case PGPPUBKEYALGO_DSA:
01006             for (i = 0; i < 4; i++)
01007                 se += pgpMpiLen(se);
01008             break;
01009         }
01010 
01011         ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
01012         i = se - h;
01013         in[0] = 0x99;
01014         in[1] = i >> 8;
01015         in[2] = i;
01016         (void) rpmDigestUpdate(ctx, in, 3);
01017         (void) rpmDigestUpdate(ctx, h, i);
01018         (void) rpmDigestFinal(ctx, (void **)&d, &dlen, 0);
01019 
01020         memmove(keyid, (d + (dlen-8)), 8);
01021         if (d) free(d);
01022         rc = 0;
01023 
01024       } break;
01025     }
01026     return rc;
01027 }
01028 
01029 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01030 {
01031     unsigned int val = *pkt;
01032     unsigned int pktlen;
01033     pgpTag tag;
01034     unsigned int plen;
01035     const byte *h;
01036     unsigned int hlen = 0;
01037     int rc = 0;
01038 
01039     /* XXX can't deal with these. */
01040     if (!(val & 0x80))
01041         return -1;
01042 
01043     if (val & 0x40) {
01044         tag = (val & 0x3f);
01045         plen = pgpLen(pkt+1, &hlen);
01046     } else {
01047         tag = (val >> 2) & 0xf;
01048         plen = (1 << (val & 0x3));
01049         hlen = pgpGrab(pkt+1, plen);
01050     }
01051 
01052     pktlen = 1 + plen + hlen;
01053     if (pktlen > pleft)
01054         return -1;
01055 
01056     h = pkt + 1 + plen;
01057     switch (tag) {
01058     case PGPTAG_SIGNATURE:
01059         rc = pgpPrtSig(tag, h, hlen);
01060         break;
01061     case PGPTAG_PUBLIC_KEY:
01062         /* Get the public key fingerprint. */
01063         if (_digp) {
01064 /*@-mods@*/
01065             if (!pgpPubkeyFingerprint(pkt, pktlen, _digp->signid))
01066                 _digp->saved |= PGPDIG_SAVED_ID;
01067             else
01068                 memset(_digp->signid, 0, sizeof(_digp->signid));
01069 /*@=mods@*/
01070         }
01071         /*@fallthrough@*/
01072     case PGPTAG_PUBLIC_SUBKEY:
01073         rc = pgpPrtKey(tag, h, hlen);
01074         break;
01075     case PGPTAG_SECRET_KEY:
01076     case PGPTAG_SECRET_SUBKEY:
01077         rc = pgpPrtKey(tag, h, hlen);
01078         break;
01079     case PGPTAG_USER_ID:
01080         rc = pgpPrtUserID(tag, h, hlen);
01081         break;
01082     case PGPTAG_COMMENT:
01083     case PGPTAG_COMMENT_OLD:
01084         rc = pgpPrtComment(tag, h, hlen);
01085         break;
01086 
01087     case PGPTAG_RESERVED:
01088     case PGPTAG_PUBLIC_SESSION_KEY:
01089     case PGPTAG_SYMMETRIC_SESSION_KEY:
01090     case PGPTAG_COMPRESSED_DATA:
01091     case PGPTAG_SYMMETRIC_DATA:
01092     case PGPTAG_MARKER:
01093     case PGPTAG_LITERAL_DATA:
01094     case PGPTAG_TRUST:
01095     case PGPTAG_PHOTOID:
01096     case PGPTAG_ENCRYPTED_MDC:
01097     case PGPTAG_MDC:
01098     case PGPTAG_PRIVATE_60:
01099     case PGPTAG_PRIVATE_62:
01100     case PGPTAG_CONTROL:
01101     default:
01102         pgpPrtVal("", pgpTagTbl, tag);
01103         pgpPrtHex("", h, hlen);
01104         pgpPrtNL();
01105         break;
01106     }
01107 
01108     return (rc ? -1 : pktlen);
01109 }
01110 
01111 pgpDig pgpNewDig(void)
01112 {
01113     pgpDig dig = xcalloc(1, sizeof(*dig));
01114 
01115     return dig;
01116 }
01117 
01118 /*@-boundswrite@*/
01119 void pgpCleanDig(pgpDig dig)
01120 {
01121     if (dig != NULL) {
01122         int i;
01123         dig->signature.userid = _free(dig->signature.userid);
01124         dig->pubkey.userid = _free(dig->pubkey.userid);
01125         dig->signature.hash = _free(dig->signature.hash);
01126         dig->pubkey.hash = _free(dig->pubkey.hash);
01127         /*@-unqualifiedtrans@*/ /* FIX: double indirection */
01128         for (i = 0; i < 4; i++) {
01129             dig->signature.params[i] = _free(dig->signature.params[i]);
01130             dig->pubkey.params[i] = _free(dig->pubkey.params[i]);
01131         }
01132         /*@=unqualifiedtrans@*/
01133 
01134         memset(&dig->signature, 0, sizeof(dig->signature));
01135         memset(&dig->pubkey, 0, sizeof(dig->pubkey));
01136 
01137         dig->md5 = _free(dig->md5);
01138         dig->sha1 = _free(dig->sha1);
01139 
01140         if (dig->dsa != NULL) {
01141             SECKEY_DestroyPublicKey(dig->dsa);
01142             dig->dsa = NULL;
01143         }
01144 
01145         if (dig->dsasig != NULL) {
01146             SECITEM_ZfreeItem(dig->dsasig, PR_TRUE);
01147             dig->dsasig = NULL;
01148         }
01149 
01150         if (dig->rsa != NULL) {
01151             SECKEY_DestroyPublicKey(dig->rsa);
01152             dig->rsa = NULL;
01153         }
01154 
01155         if (dig->rsasig != NULL) {
01156             SECITEM_ZfreeItem(dig->rsasig, PR_TRUE);
01157             dig->rsasig = NULL;
01158         }
01159 
01160     }
01161 /*@-nullstate@*/
01162     return;
01163 /*@=nullstate@*/
01164 }
01165 /*@=boundswrite@*/
01166 
01167 pgpDig pgpFreeDig(/*@only@*/ /*@null@*/ pgpDig dig)
01168         /*@modifies dig @*/
01169 {
01170     if (dig != NULL) {
01171 
01172         /* DUmp the signature/pubkey data. */
01173         pgpCleanDig(dig);
01174 
01175         /*@-branchstate@*/
01176         if (dig->hdrsha1ctx != NULL)
01177             (void) rpmDigestFinal(dig->hdrsha1ctx, NULL, NULL, 0);
01178         /*@=branchstate@*/
01179         dig->hdrsha1ctx = NULL;
01180 
01181         /*@-branchstate@*/
01182         if (dig->sha1ctx != NULL)
01183             (void) rpmDigestFinal(dig->sha1ctx, NULL, NULL, 0);
01184         /*@=branchstate@*/
01185         dig->sha1ctx = NULL;
01186 
01187 #ifdef  NOTYET
01188         /*@-branchstate@*/
01189         if (dig->hdrmd5ctx != NULL)
01190             (void) rpmDigestFinal(dig->hdrmd5ctx, NULL, NULL, 0);
01191         /*@=branchstate@*/
01192         dig->hdrmd5ctx = NULL;
01193 #endif
01194 
01195         /*@-branchstate@*/
01196         if (dig->md5ctx != NULL)
01197             (void) rpmDigestFinal(dig->md5ctx, NULL, NULL, 0);
01198         /*@=branchstate@*/
01199         dig->md5ctx = NULL;
01200 
01201         dig = _free(dig);
01202     }
01203     return dig;
01204 }
01205 
01206 int pgpPrtPkts(const byte * pkts, unsigned int pktlen, pgpDig dig, int printing)
01207         /*@globals _dig, _digp, _print @*/
01208         /*@modifies _dig, _digp, *_digp, _print @*/
01209 {
01210     unsigned int val = *pkts;
01211     const byte *p;
01212     unsigned int pleft;
01213     int len;
01214 
01215     _print = printing;
01216     _dig = dig;
01217     if (dig != NULL && (val & 0x80)) {
01218         pgpTag tag = (val & 0x40) ? (val & 0x3f) : ((val >> 2) & 0xf);
01219         _digp = (tag == PGPTAG_SIGNATURE) ? &_dig->signature : &_dig->pubkey;
01220         _digp->tag = tag;
01221     } else
01222         _digp = NULL;
01223 
01224     for (p = pkts, pleft = pktlen; p < (pkts + pktlen); p += len, pleft -= len) {
01225         len = pgpPrtPkt(p, pleft);
01226         if (len <= 0)
01227             return len;
01228         if (len > pleft)        /* XXX shouldn't happen */
01229             break;
01230     }
01231     return 0;
01232 }
01233 
01234 /*@-boundswrite@*/
01235 pgpArmor pgpReadPkts(const char * fn, const byte ** pkt, size_t * pktlen)
01236 {
01237     const byte * b = NULL;
01238     ssize_t blen;
01239     const char * enc = NULL;
01240     const char * crcenc = NULL;
01241     byte * dec;
01242     byte * crcdec;
01243     size_t declen;
01244     size_t crclen;
01245     uint32_t crcpkt, crc;
01246     const char * armortype = NULL;
01247     char * t, * te;
01248     int pstate = 0;
01249     pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP;    /* XXX assume failure */
01250     int rc;
01251 
01252     rc = rpmioSlurp(fn, &b, &blen);
01253     if (rc || b == NULL || blen <= 0) {
01254         goto exit;
01255     }
01256 
01257     if (pgpIsPkt(b)) {
01258 #ifdef NOTYET   /* XXX ASCII Pubkeys only, please. */
01259         ec = 0; /* XXX fish out pkt type. */
01260 #endif
01261         goto exit;
01262     }
01263 
01264 #define TOKEQ(_s, _tok) (!strncmp((_s), (_tok), sizeof(_tok)-1))
01265 
01266     for (t = (char *)b; t && *t; t = te) {
01267         if ((te = strchr(t, '\n')) == NULL)
01268             te = t + strlen(t);
01269         else
01270             te++;
01271 
01272         switch (pstate) {
01273         case 0:
01274             armortype = NULL;
01275             if (!TOKEQ(t, "-----BEGIN PGP "))
01276                 continue;
01277             t += sizeof("-----BEGIN PGP ")-1;
01278 
01279             rc = pgpValTok(pgpArmorTbl, t, te);
01280             if (rc < 0) {
01281                 ec = PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE;
01282                 goto exit;
01283             }
01284             if (rc != PGPARMOR_PUBKEY)  /* XXX ASCII Pubkeys only, please. */
01285                 continue;
01286             armortype = t;
01287 
01288             t = te - (sizeof("-----\n")-1);
01289             if (!TOKEQ(t, "-----\n"))
01290                 continue;
01291             *t = '\0';
01292             pstate++;
01293             /*@switchbreak@*/ break;
01294         case 1:
01295             enc = NULL;
01296             rc = pgpValTok(pgpArmorKeyTbl, t, te);
01297             if (rc >= 0)
01298                 continue;
01299             if (*t != '\n') {
01300                 pstate = 0;
01301                 continue;
01302             }
01303             enc = te;           /* Start of encoded packets */
01304             pstate++;
01305             /*@switchbreak@*/ break;
01306         case 2:
01307             crcenc = NULL;
01308             if (*t != '=')
01309                 continue;
01310             *t++ = '\0';        /* Terminate encoded packets */
01311             crcenc = t;         /* Start of encoded crc */
01312             pstate++;
01313             /*@switchbreak@*/ break;
01314         case 3:
01315             pstate = 0;
01316             if (!TOKEQ(t, "-----END PGP ")) {
01317                 ec = PGPARMOR_ERR_NO_END_PGP;
01318                 goto exit;
01319             }
01320             *t = '\0';          /* Terminate encoded crc */
01321             t += sizeof("-----END PGP ")-1;
01322             if (t >= te) continue;
01323 
01324             if (armortype == NULL) /* XXX can't happen */
01325                 continue;
01326             rc = strncmp(t, armortype, strlen(armortype));
01327             if (rc)
01328                 continue;
01329 
01330             t += strlen(armortype);
01331             if (t >= te) continue;
01332 
01333             if (!TOKEQ(t, "-----")) {
01334                 ec = PGPARMOR_ERR_NO_END_PGP;
01335                 goto exit;
01336             }
01337             t += (sizeof("-----")-1);
01338             if (t >= te) continue;
01339             /* XXX permitting \r here is not RFC-2440 compliant <shrug> */
01340             if (!(*t == '\n' || *t == '\r')) continue;
01341 
01342             crcdec = NULL;
01343             crclen = 0;
01344             if (b64decode(crcenc, (void **)&crcdec, &crclen) != 0) {
01345                 ec = PGPARMOR_ERR_CRC_DECODE;
01346                 goto exit;
01347             }
01348             crcpkt = pgpGrab(crcdec, crclen);
01349             crcdec = _free(crcdec);
01350             dec = NULL;
01351             declen = 0;
01352             if (b64decode(enc, (void **)&dec, &declen) != 0) {
01353                 ec = PGPARMOR_ERR_BODY_DECODE;
01354                 goto exit;
01355             }
01356             crc = pgpCRC(dec, declen);
01357             if (crcpkt != crc) {
01358                 ec = PGPARMOR_ERR_CRC_CHECK;
01359                 goto exit;
01360             }
01361             b = _free(b);
01362             b = dec;
01363             blen = declen;
01364             ec = PGPARMOR_PUBKEY;       /* XXX ASCII Pubkeys only, please. */
01365             goto exit;
01366             /*@notreached@*/ /*@switchbreak@*/ break;
01367         }
01368     }
01369     ec = PGPARMOR_NONE;
01370 
01371 exit:
01372     if (ec > PGPARMOR_NONE && pkt)
01373         *pkt = b;
01374     else if (b != NULL)
01375         b = _free(b);
01376     if (pktlen)
01377         *pktlen = blen;
01378     return ec;
01379 }
01380 /*@=boundswrite@*/
01381 
01382 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01383 {
01384     const char * enc;
01385     char * t;
01386     size_t nt = 0;
01387     char * val;
01388 
01389     enc = b64encode(s, ns, -1);
01390     if (enc != NULL) {
01391         nt = strlen(enc);
01392     }
01393 
01394     nt += 512;  /* XXX slop for armor and crc */
01395 
01396 /*@-boundswrite@*/
01397     val = t = xmalloc(nt + 1);
01398     *t = '\0';
01399     t = stpcpy(t, "-----BEGIN PGP ");
01400     t = stpcpy(t, pgpValStr(pgpArmorTbl, atype));
01401     /*@-globs@*/
01402     t = stpcpy( stpcpy(t, "-----\nVersion: rpm-"), VERSION);
01403     /*@=globs@*/
01404     t = stpcpy(t, " (NSS-3)\n\n");
01405 
01406     if (enc != NULL) {
01407         t = stpcpy(t, enc);
01408         enc = _free(enc);
01409         if ((enc = b64crc(s, ns)) != NULL) {
01410             *t++ = '=';
01411             t = stpcpy(t, enc);
01412             enc = _free(enc);
01413         }
01414     }
01415         
01416     t = stpcpy(t, "-----END PGP ");
01417     t = stpcpy(t, pgpValStr(pgpArmorTbl, atype));
01418     t = stpcpy(t, "-----\n");
01419 /*@=boundswrite@*/
01420 
01421     return val;
01422 }
01423 /*@=boundsread@*/
01424 
01425 int rpmInitCrypto(void) {
01426     int rc = 0;
01427 
01428     if (!_crypto_initialized && NSS_NoDB_Init(NULL) != SECSuccess) {
01429         rc = -1;
01430     } else {
01431         _crypto_initialized = 1;
01432     }
01433 
01434     return rc;
01435 }
01436 
01437 

Generated on Fri Apr 2 18:06:17 2010 for rpm by  doxygen 1.4.7