1 /** \ingroup rpmio signature
3 * Routines to handle RFC-2440 detached signatures.
9 #include <netinet/in.h>
10 #include <rpm/rpmstring.h>
11 #include <rpm/rpmlog.h>
13 #include "rpmio/digest.h"
14 #include "rpmio/rpmio_internal.h" /* XXX rpmioSlurp */
18 static int _print = 0;
21 * Container for values parsed from an OpenPGP signature and public key.
24 struct pgpDigParams_s * signature;
25 struct pgpDigParams_s * pubkey;
28 typedef const struct pgpValTbl_s {
30 char const * const str;
33 static struct pgpValTbl_s const pgpSigTypeTbl[] = {
34 { PGPSIGTYPE_BINARY, "Binary document signature" },
35 { PGPSIGTYPE_TEXT, "Text document signature" },
36 { PGPSIGTYPE_STANDALONE, "Standalone signature" },
37 { PGPSIGTYPE_GENERIC_CERT, "Generic certification of a User ID and Public Key" },
38 { PGPSIGTYPE_PERSONA_CERT, "Persona certification of a User ID and Public Key" },
39 { PGPSIGTYPE_CASUAL_CERT, "Casual certification of a User ID and Public Key" },
40 { PGPSIGTYPE_POSITIVE_CERT, "Positive certification of a User ID and Public Key" },
41 { PGPSIGTYPE_SUBKEY_BINDING,"Subkey Binding Signature" },
42 { PGPSIGTYPE_SIGNED_KEY, "Signature directly on a key" },
43 { PGPSIGTYPE_KEY_REVOKE, "Key revocation signature" },
44 { PGPSIGTYPE_SUBKEY_REVOKE, "Subkey revocation signature" },
45 { PGPSIGTYPE_CERT_REVOKE, "Certification revocation signature" },
46 { PGPSIGTYPE_TIMESTAMP, "Timestamp signature" },
47 { -1, "Unknown signature type" },
50 static struct pgpValTbl_s const pgpPubkeyTbl[] = {
51 { PGPPUBKEYALGO_RSA, "RSA" },
52 { PGPPUBKEYALGO_RSA_ENCRYPT,"RSA(Encrypt-Only)" },
53 { PGPPUBKEYALGO_RSA_SIGN, "RSA(Sign-Only)" },
54 { PGPPUBKEYALGO_ELGAMAL_ENCRYPT,"Elgamal(Encrypt-Only)" },
55 { PGPPUBKEYALGO_DSA, "DSA" },
56 { PGPPUBKEYALGO_EC, "Elliptic Curve" },
57 { PGPPUBKEYALGO_ECDSA, "ECDSA" },
58 { PGPPUBKEYALGO_ELGAMAL, "Elgamal" },
59 { PGPPUBKEYALGO_DH, "Diffie-Hellman (X9.42)" },
60 { -1, "Unknown public key algorithm" },
63 static struct pgpValTbl_s const pgpSymkeyTbl[] = {
64 { PGPSYMKEYALGO_PLAINTEXT, "Plaintext" },
65 { PGPSYMKEYALGO_IDEA, "IDEA" },
66 { PGPSYMKEYALGO_TRIPLE_DES, "3DES" },
67 { PGPSYMKEYALGO_CAST5, "CAST5" },
68 { PGPSYMKEYALGO_BLOWFISH, "BLOWFISH" },
69 { PGPSYMKEYALGO_SAFER, "SAFER" },
70 { PGPSYMKEYALGO_DES_SK, "DES/SK" },
71 { PGPSYMKEYALGO_AES_128, "AES(128-bit key)" },
72 { PGPSYMKEYALGO_AES_192, "AES(192-bit key)" },
73 { PGPSYMKEYALGO_AES_256, "AES(256-bit key)" },
74 { PGPSYMKEYALGO_TWOFISH, "TWOFISH(256-bit key)" },
75 { PGPSYMKEYALGO_NOENCRYPT, "no encryption" },
76 { -1, "Unknown symmetric key algorithm" },
79 static struct pgpValTbl_s const pgpCompressionTbl[] = {
80 { PGPCOMPRESSALGO_NONE, "Uncompressed" },
81 { PGPCOMPRESSALGO_ZIP, "ZIP" },
82 { PGPCOMPRESSALGO_ZLIB, "ZLIB" },
83 { PGPCOMPRESSALGO_BZIP2, "BZIP2" },
84 { -1, "Unknown compression algorithm" },
87 static struct pgpValTbl_s const pgpHashTbl[] = {
88 { PGPHASHALGO_MD5, "MD5" },
89 { PGPHASHALGO_SHA1, "SHA1" },
90 { PGPHASHALGO_RIPEMD160, "RIPEMD160" },
91 { PGPHASHALGO_MD2, "MD2" },
92 { PGPHASHALGO_TIGER192, "TIGER192" },
93 { PGPHASHALGO_HAVAL_5_160, "HAVAL-5-160" },
94 { PGPHASHALGO_SHA256, "SHA256" },
95 { PGPHASHALGO_SHA384, "SHA384" },
96 { PGPHASHALGO_SHA512, "SHA512" },
97 { PGPHASHALGO_SHA224, "SHA224" },
98 { -1, "Unknown hash algorithm" },
101 static struct pgpValTbl_s const pgpKeyServerPrefsTbl[] = {
102 { 0x80, "No-modify" },
103 { -1, "Unknown key server preference" },
106 static struct pgpValTbl_s const pgpSubTypeTbl[] = {
107 { PGPSUBTYPE_SIG_CREATE_TIME,"signature creation time" },
108 { PGPSUBTYPE_SIG_EXPIRE_TIME,"signature expiration time" },
109 { PGPSUBTYPE_EXPORTABLE_CERT,"exportable certification" },
110 { PGPSUBTYPE_TRUST_SIG, "trust signature" },
111 { PGPSUBTYPE_REGEX, "regular expression" },
112 { PGPSUBTYPE_REVOCABLE, "revocable" },
113 { PGPSUBTYPE_KEY_EXPIRE_TIME,"key expiration time" },
114 { PGPSUBTYPE_ARR, "additional recipient request" },
115 { PGPSUBTYPE_PREFER_SYMKEY, "preferred symmetric algorithms" },
116 { PGPSUBTYPE_REVOKE_KEY, "revocation key" },
117 { PGPSUBTYPE_ISSUER_KEYID, "issuer key ID" },
118 { PGPSUBTYPE_NOTATION, "notation data" },
119 { PGPSUBTYPE_PREFER_HASH, "preferred hash algorithms" },
120 { PGPSUBTYPE_PREFER_COMPRESS,"preferred compression algorithms" },
121 { PGPSUBTYPE_KEYSERVER_PREFERS,"key server preferences" },
122 { PGPSUBTYPE_PREFER_KEYSERVER,"preferred key server" },
123 { PGPSUBTYPE_PRIMARY_USERID,"primary user id" },
124 { PGPSUBTYPE_POLICY_URL, "policy URL" },
125 { PGPSUBTYPE_KEY_FLAGS, "key flags" },
126 { PGPSUBTYPE_SIGNER_USERID, "signer's user id" },
127 { PGPSUBTYPE_REVOKE_REASON, "reason for revocation" },
128 { PGPSUBTYPE_FEATURES, "features" },
129 { PGPSUBTYPE_EMBEDDED_SIG, "embedded signature" },
131 { PGPSUBTYPE_INTERNAL_100, "internal subpkt type 100" },
132 { PGPSUBTYPE_INTERNAL_101, "internal subpkt type 101" },
133 { PGPSUBTYPE_INTERNAL_102, "internal subpkt type 102" },
134 { PGPSUBTYPE_INTERNAL_103, "internal subpkt type 103" },
135 { PGPSUBTYPE_INTERNAL_104, "internal subpkt type 104" },
136 { PGPSUBTYPE_INTERNAL_105, "internal subpkt type 105" },
137 { PGPSUBTYPE_INTERNAL_106, "internal subpkt type 106" },
138 { PGPSUBTYPE_INTERNAL_107, "internal subpkt type 107" },
139 { PGPSUBTYPE_INTERNAL_108, "internal subpkt type 108" },
140 { PGPSUBTYPE_INTERNAL_109, "internal subpkt type 109" },
141 { PGPSUBTYPE_INTERNAL_110, "internal subpkt type 110" },
142 { -1, "Unknown signature subkey type" },
145 static struct pgpValTbl_s const pgpTagTbl[] = {
146 { PGPTAG_PUBLIC_SESSION_KEY,"Public-Key Encrypted Session Key" },
147 { PGPTAG_SIGNATURE, "Signature" },
148 { PGPTAG_SYMMETRIC_SESSION_KEY,"Symmetric-Key Encrypted Session Key" },
149 { PGPTAG_ONEPASS_SIGNATURE, "One-Pass Signature" },
150 { PGPTAG_SECRET_KEY, "Secret Key" },
151 { PGPTAG_PUBLIC_KEY, "Public Key" },
152 { PGPTAG_SECRET_SUBKEY, "Secret Subkey" },
153 { PGPTAG_COMPRESSED_DATA, "Compressed Data" },
154 { PGPTAG_SYMMETRIC_DATA, "Symmetrically Encrypted Data" },
155 { PGPTAG_MARKER, "Marker" },
156 { PGPTAG_LITERAL_DATA, "Literal Data" },
157 { PGPTAG_TRUST, "Trust" },
158 { PGPTAG_USER_ID, "User ID" },
159 { PGPTAG_PUBLIC_SUBKEY, "Public Subkey" },
160 { PGPTAG_COMMENT_OLD, "Comment (from OpenPGP draft)" },
161 { PGPTAG_PHOTOID, "PGP's photo ID" },
162 { PGPTAG_ENCRYPTED_MDC, "Integrity protected encrypted data" },
163 { PGPTAG_MDC, "Manipulaion detection code packet" },
164 { PGPTAG_PRIVATE_60, "Private #60" },
165 { PGPTAG_COMMENT, "Comment" },
166 { PGPTAG_PRIVATE_62, "Private #62" },
167 { PGPTAG_CONTROL, "Control (GPG)" },
168 { -1, "Unknown packet tag" },
171 static struct pgpValTbl_s const pgpArmorTbl[] = {
172 { PGPARMOR_MESSAGE, "MESSAGE" },
173 { PGPARMOR_PUBKEY, "PUBLIC KEY BLOCK" },
174 { PGPARMOR_SIGNATURE, "SIGNATURE" },
175 { PGPARMOR_SIGNED_MESSAGE, "SIGNED MESSAGE" },
176 { PGPARMOR_FILE, "ARMORED FILE" },
177 { PGPARMOR_PRIVKEY, "PRIVATE KEY BLOCK" },
178 { PGPARMOR_SECKEY, "SECRET KEY BLOCK" },
179 { -1, "Unknown armor block" }
182 static struct pgpValTbl_s const pgpArmorKeyTbl[] = {
183 { PGPARMORKEY_VERSION, "Version: " },
184 { PGPARMORKEY_COMMENT, "Comment: " },
185 { PGPARMORKEY_MESSAGEID, "MessageID: " },
186 { PGPARMORKEY_HASH, "Hash: " },
187 { PGPARMORKEY_CHARSET, "Charset: " },
188 { -1, "Unknown armor key" }
191 static void pgpPrtNL(void)
194 fprintf(stderr, "\n");
197 static const char * pgpValStr(pgpValTbl vs, uint8_t val)
202 } while ((++vs)->val != -1);
206 static pgpValTbl pgpValTable(pgpValType type)
209 case PGPVAL_TAG: return pgpTagTbl;
210 case PGPVAL_ARMORBLOCK: return pgpArmorTbl;
211 case PGPVAL_ARMORKEY: return pgpArmorKeyTbl;
212 case PGPVAL_SIGTYPE: return pgpSigTypeTbl;
213 case PGPVAL_SUBTYPE: return pgpSubTypeTbl;
214 case PGPVAL_PUBKEYALGO: return pgpPubkeyTbl;
215 case PGPVAL_SYMKEYALGO: return pgpSymkeyTbl;
216 case PGPVAL_COMPRESSALGO: return pgpCompressionTbl;
217 case PGPVAL_HASHALGO: return pgpHashTbl;
218 case PGPVAL_SERVERPREFS: return pgpKeyServerPrefsTbl;
225 const char * pgpValString(pgpValType type, uint8_t val)
227 pgpValTbl tbl = pgpValTable(type);
228 return (tbl != NULL) ? pgpValStr(tbl, val) : NULL;
231 static void pgpPrtHex(const char *pre, const uint8_t *p, size_t plen)
236 fprintf(stderr, "%s", pre);
237 hex = pgpHexStr(p, plen);
238 fprintf(stderr, " %s", hex);
242 static void pgpPrtVal(const char * pre, pgpValTbl vs, uint8_t val)
246 fprintf(stderr, "%s", pre);
247 fprintf(stderr, "%s(%u)", pgpValStr(vs, val), (unsigned)val);
251 * Return hex formatted representation of a multiprecision integer.
253 * @return hex formatted string (malloc'ed)
256 char * pgpMpiStr(const uint8_t *p)
259 char *hex = pgpHexStr(p+2, pgpMpiLen(p)-2);
260 rasprintf(&str, "[%4u]: %s", pgpGrab(p, (size_t) 2), hex);
266 * Return value of an OpenPGP string.
267 * @param vs table of (string,value) pairs
268 * @param s string token to lookup
269 * @param se end-of-string address
273 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
276 size_t vlen = strlen(vs->str);
277 if (vlen <= (se-s) && rstreqn(s, vs->str, vlen))
279 } while ((++vs)->val != -1);
284 * Decode length from 1, 2, or 5 octet body length encoding, used in
285 * new format packet headers and V4 signature subpackets.
286 * @param s pointer to length encoding buffer
287 * @param slen buffer size
288 * @retval *lenp decoded length
289 * @return no. of bytes used to encode the length, 0 on error
292 size_t pgpLen(const uint8_t *s, size_t slen, size_t * lenp)
298 * Callers can only ensure we'll always have the first byte, beyond
299 * that the required size is not known until we decode it so we need
300 * to check if we have enough bytes to read the size as we go.
305 } else if (*s < 255 && slen > 2) {
307 dlen = (((s[0]) - 192) << 8) + s[1] + 192;
308 } else if (slen > 5) {
310 dlen = pgpGrab(s+1, 4);
320 uint8_t tag; /* decoded PGP tag */
321 const uint8_t *head; /* pointer to start of packet (header) */
322 const uint8_t *body; /* pointer to packet body */
323 size_t blen; /* length of body in bytes */
326 static int decodePkt(const uint8_t *p, size_t plen, struct pgpPkt *pkt)
328 int rc = -1; /* assume failure */
330 /* Valid PGP packet header must always have two or more bytes in it */
331 if (p && plen >= 2 && p[0] & 0x80) {
336 /* New format packet, body length encoding in second byte */
337 lenlen = pgpLen(p+1, plen-1, &pkt->blen);
338 pkt->tag = (p[0] & 0x3f);
340 /* Old format packet, body length encoding in tag byte */
341 lenlen = (1 << (p[0] & 0x3));
343 pkt->blen = pgpGrab(p+1, lenlen);
345 pkt->tag = (p[0] >> 2) & 0xf;
349 /* Does the packet header and its body fit in our boundaries? */
350 if (lenlen && (hlen + pkt->blen <= plen)) {
352 pkt->body = pkt->head + hlen;
360 #define CRC24_INIT 0xb704ce
361 #define CRC24_POLY 0x1864cfb
364 * Return CRC of a buffer.
365 * @param octets bytes
366 * @param len no. of bytes
367 * @return crc of buffer
370 unsigned int pgpCRC(const uint8_t *octets, size_t len)
372 unsigned int crc = CRC24_INIT;
376 crc ^= (*octets++) << 16;
377 for (i = 0; i < 8; i++) {
383 return crc & 0xffffff;
386 static int pgpPrtSubType(const uint8_t *h, size_t hlen, pgpSigType sigtype,
389 const uint8_t *p = h;
393 i = pgpLen(p, hlen, &plen);
394 if (i == 0 || i + plen > hlen)
400 pgpPrtVal(" ", pgpSubTypeTbl, (p[0]&(~PGPSUBTYPE_CRITICAL)));
401 if (p[0] & PGPSUBTYPE_CRITICAL)
403 fprintf(stderr, " *CRITICAL*");
405 case PGPSUBTYPE_PREFER_SYMKEY: /* preferred symmetric algorithms */
406 for (i = 1; i < plen; i++)
407 pgpPrtVal(" ", pgpSymkeyTbl, p[i]);
409 case PGPSUBTYPE_PREFER_HASH: /* preferred hash algorithms */
410 for (i = 1; i < plen; i++)
411 pgpPrtVal(" ", pgpHashTbl, p[i]);
413 case PGPSUBTYPE_PREFER_COMPRESS:/* preferred compression algorithms */
414 for (i = 1; i < plen; i++)
415 pgpPrtVal(" ", pgpCompressionTbl, p[i]);
417 case PGPSUBTYPE_KEYSERVER_PREFERS:/* key server preferences */
418 for (i = 1; i < plen; i++)
419 pgpPrtVal(" ", pgpKeyServerPrefsTbl, p[i]);
421 case PGPSUBTYPE_SIG_CREATE_TIME:
422 if (!(_digp->saved & PGPDIG_SAVED_TIME) &&
423 (sigtype == PGPSIGTYPE_POSITIVE_CERT || sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT || sigtype == PGPSIGTYPE_STANDALONE))
425 _digp->saved |= PGPDIG_SAVED_TIME;
426 memcpy(_digp->time, p+1, sizeof(_digp->time));
428 case PGPSUBTYPE_SIG_EXPIRE_TIME:
429 case PGPSUBTYPE_KEY_EXPIRE_TIME:
430 if ((plen - 1) == 4) {
431 time_t t = pgpGrab(p+1, plen-1);
433 fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
435 pgpPrtHex("", p+1, plen-1);
438 case PGPSUBTYPE_ISSUER_KEYID: /* issuer key ID */
439 if (!(_digp->saved & PGPDIG_SAVED_ID) &&
440 (sigtype == PGPSIGTYPE_POSITIVE_CERT || sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT || sigtype == PGPSIGTYPE_STANDALONE))
442 _digp->saved |= PGPDIG_SAVED_ID;
443 memcpy(_digp->signid, p+1, sizeof(_digp->signid));
445 case PGPSUBTYPE_EXPORTABLE_CERT:
446 case PGPSUBTYPE_TRUST_SIG:
447 case PGPSUBTYPE_REGEX:
448 case PGPSUBTYPE_REVOCABLE:
450 case PGPSUBTYPE_REVOKE_KEY:
451 case PGPSUBTYPE_NOTATION:
452 case PGPSUBTYPE_PREFER_KEYSERVER:
453 case PGPSUBTYPE_PRIMARY_USERID:
454 case PGPSUBTYPE_POLICY_URL:
455 case PGPSUBTYPE_KEY_FLAGS:
456 case PGPSUBTYPE_SIGNER_USERID:
457 case PGPSUBTYPE_REVOKE_REASON:
458 case PGPSUBTYPE_FEATURES:
459 case PGPSUBTYPE_EMBEDDED_SIG:
460 case PGPSUBTYPE_INTERNAL_100:
461 case PGPSUBTYPE_INTERNAL_101:
462 case PGPSUBTYPE_INTERNAL_102:
463 case PGPSUBTYPE_INTERNAL_103:
464 case PGPSUBTYPE_INTERNAL_104:
465 case PGPSUBTYPE_INTERNAL_105:
466 case PGPSUBTYPE_INTERNAL_106:
467 case PGPSUBTYPE_INTERNAL_107:
468 case PGPSUBTYPE_INTERNAL_108:
469 case PGPSUBTYPE_INTERNAL_109:
470 case PGPSUBTYPE_INTERNAL_110:
472 pgpPrtHex("", p+1, plen-1);
479 return (hlen != 0); /* non-zero hlen is an error */
482 pgpDigAlg pgpDigAlgFree(pgpDigAlg alg)
492 static int pgpPrtSigParams(pgpTag tag, uint8_t pubkey_algo, uint8_t sigtype,
493 const uint8_t *p, const uint8_t *h, size_t hlen,
496 int rc = 1; /* assume failure */
497 const uint8_t * pend = h + hlen;
499 pgpDigAlg sigalg = pgpSignatureNew(pubkey_algo);
501 for (i = 0; p < pend && i < sigalg->mpis; i++, p += pgpMpiLen(p)) {
502 if (sigtype == PGPSIGTYPE_BINARY || sigtype == PGPSIGTYPE_TEXT) {
503 if (sigalg->setmpi(sigalg, i, p, pend))
508 /* Does the size and number of MPI's match our expectations? */
509 if (p == pend && i == sigalg->mpis)
512 /* We can't handle more than one sig at a time */
513 if (rc == 0 && sigp->alg == NULL && sigp->tag == PGPTAG_SIGNATURE)
516 pgpDigAlgFree(sigalg);
521 static int pgpPrtSig(pgpTag tag, const uint8_t *h, size_t hlen,
524 uint8_t version = h[0];
531 { pgpPktSigV3 v = (pgpPktSigV3)h;
534 if (hlen <= sizeof(*v) || v->hashlen != 5)
537 pgpPrtVal("V3 ", pgpTagTbl, tag);
538 pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
539 pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
540 pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
542 t = pgpGrab(v->time, sizeof(v->time));
544 fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
546 pgpPrtHex(" signer keyid", v->signid, sizeof(v->signid));
547 plen = pgpGrab(v->signhash16, sizeof(v->signhash16));
548 pgpPrtHex(" signhash16", v->signhash16, sizeof(v->signhash16));
551 if (_digp->pubkey_algo == 0) {
552 _digp->version = v->version;
553 _digp->hashlen = v->hashlen;
554 _digp->sigtype = v->sigtype;
555 _digp->hash = memcpy(xmalloc(v->hashlen), &v->sigtype, v->hashlen);
556 memcpy(_digp->time, v->time, sizeof(_digp->time));
557 memcpy(_digp->signid, v->signid, sizeof(_digp->signid));
558 _digp->pubkey_algo = v->pubkey_algo;
559 _digp->hash_algo = v->hash_algo;
560 memcpy(_digp->signhash16, v->signhash16, sizeof(_digp->signhash16));
563 p = ((uint8_t *)v) + sizeof(*v);
564 rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen, _digp);
567 { pgpPktSigV4 v = (pgpPktSigV4)h;
569 if (hlen <= sizeof(*v))
572 pgpPrtVal("V4 ", pgpTagTbl, tag);
573 pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
574 pgpPrtVal(" ", pgpHashTbl, v->hash_algo);
575 pgpPrtVal(" ", pgpSigTypeTbl, v->sigtype);
579 plen = pgpGrab(v->hashlen, sizeof(v->hashlen));
580 p += sizeof(v->hashlen);
582 if ((p + plen) > (h + hlen))
585 if (_digp->pubkey_algo == 0) {
586 _digp->hashlen = sizeof(*v) + plen;
587 _digp->hash = memcpy(xmalloc(_digp->hashlen), v, _digp->hashlen);
589 if (pgpPrtSubType(p, plen, v->sigtype, _digp))
596 if ((p + plen) > (h + hlen))
599 if (pgpPrtSubType(p, plen, v->sigtype, _digp))
604 pgpPrtHex(" signhash16", p, 2);
607 if (_digp->pubkey_algo == 0) {
608 _digp->version = v->version;
609 _digp->sigtype = v->sigtype;
610 _digp->pubkey_algo = v->pubkey_algo;
611 _digp->hash_algo = v->hash_algo;
612 memcpy(_digp->signhash16, p, sizeof(_digp->signhash16));
619 rc = pgpPrtSigParams(tag, v->pubkey_algo, v->sigtype, p, h, hlen, _digp);
628 char * pgpHexStr(const uint8_t *p, size_t plen)
631 str = t = xmalloc(plen * 2 + 1);
632 static char const hex[] = "0123456789abcdef";
636 *t++ = hex[ (i >> 4) & 0xf ];
637 *t++ = hex[ (i ) & 0xf ];
643 static int pgpPrtPubkeyParams(uint8_t pubkey_algo,
644 const uint8_t *p, const uint8_t *h, size_t hlen,
648 const uint8_t *pend = h + hlen;
650 pgpDigAlg keyalg = pgpPubkeyNew(pubkey_algo);
652 for (i = 0; p < pend && i < keyalg->mpis; i++, p += pgpMpiLen(p)) {
653 if (keyalg->setmpi(keyalg, i, p, pend))
657 /* Does the size and number of MPI's match our expectations? */
658 if (p == pend && i == keyalg->mpis)
661 /* We can't handle more than one key at a time */
662 if (rc == 0 && keyp->alg == NULL && keyp->tag == PGPTAG_PUBLIC_KEY)
665 pgpDigAlgFree(keyalg);
670 static int pgpPrtKey(pgpTag tag, const uint8_t *h, size_t hlen,
673 uint8_t version = *h;
674 const uint8_t * p = NULL;
678 /* We only permit V4 keys, V3 keys are long long since deprecated */
681 { pgpPktKeyV4 v = (pgpPktKeyV4)h;
683 if (hlen > sizeof(*v)) {
684 pgpPrtVal("V4 ", pgpTagTbl, tag);
685 pgpPrtVal(" ", pgpPubkeyTbl, v->pubkey_algo);
686 t = pgpGrab(v->time, sizeof(v->time));
688 fprintf(stderr, " %-24.24s(0x%08x)", ctime(&t), (unsigned)t);
691 if (_digp->tag == tag) {
692 _digp->version = v->version;
693 memcpy(_digp->time, v->time, sizeof(_digp->time));
694 _digp->pubkey_algo = v->pubkey_algo;
697 p = ((uint8_t *)v) + sizeof(*v);
698 rc = pgpPrtPubkeyParams(v->pubkey_algo, p, h, hlen, _digp);
705 static int pgpPrtUserID(pgpTag tag, const uint8_t *h, size_t hlen,
708 pgpPrtVal("", pgpTagTbl, tag);
710 fprintf(stderr, " \"%.*s\"", (int)hlen, (const char *)h);
713 _digp->userid = memcpy(xmalloc(hlen+1), h, hlen);
714 _digp->userid[hlen] = '\0';
718 static int getFingerprint(const uint8_t *h, size_t hlen, pgpKeyID_t keyid)
720 int rc = -1; /* assume failure */
722 const uint8_t *pend = h + hlen;
724 /* We only permit V4 keys, V3 keys are long long since deprecated */
727 { pgpPktKeyV4 v = (pgpPktKeyV4) (h);
730 /* Packet must be larger than v to have room for the required MPIs */
731 if (hlen > sizeof(*v)) {
732 switch (v->pubkey_algo) {
733 case PGPPUBKEYALGO_RSA:
736 case PGPPUBKEYALGO_DSA:
742 se = (uint8_t *)(v + 1);
743 while (se < pend && mpis-- > 0)
746 /* Does the size and number of MPI's match our expectations? */
747 if (se == pend && mpis == 0) {
748 DIGEST_CTX ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
752 uint8_t in[3] = { 0x99, (i >> 8), i };
754 (void) rpmDigestUpdate(ctx, in, 3);
755 (void) rpmDigestUpdate(ctx, h, i);
756 (void) rpmDigestFinal(ctx, (void **)&d, &dlen, 0);
759 memcpy(keyid, (d + (dlen-sizeof(keyid))), sizeof(keyid));
770 int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, pgpKeyID_t keyid)
774 if (decodePkt(pkt, pktlen, &p))
777 return getFingerprint(p.body, p.blen, keyid);
780 int pgpExtractPubkeyFingerprint(const char * b64pkt, pgpKeyID_t keyid)
784 int rc = -1; /* assume failure */
786 if (rpmBase64Decode(b64pkt, (void **)&pkt, &pktlen) == 0) {
787 if (pgpPubkeyFingerprint(pkt, pktlen, keyid) == 0) {
788 /* if there ever was a bizarre return code for success... */
796 static int pgpPrtPkt(struct pgpPkt *p, pgpDigParams _digp)
801 case PGPTAG_SIGNATURE:
802 rc = pgpPrtSig(p->tag, p->body, p->blen, _digp);
804 case PGPTAG_PUBLIC_KEY:
805 /* Get the public key fingerprint. */
806 if (!getFingerprint(p->body, p->blen, _digp->signid))
807 _digp->saved |= PGPDIG_SAVED_ID;
809 memset(_digp->signid, 0, sizeof(_digp->signid));
810 rc = pgpPrtKey(p->tag, p->body, p->blen, _digp);
813 rc = pgpPrtUserID(p->tag, p->body, p->blen, _digp);
816 case PGPTAG_COMMENT_OLD:
817 case PGPTAG_PUBLIC_SUBKEY:
818 case PGPTAG_SECRET_KEY:
819 case PGPTAG_SECRET_SUBKEY:
820 case PGPTAG_RESERVED:
821 case PGPTAG_PUBLIC_SESSION_KEY:
822 case PGPTAG_SYMMETRIC_SESSION_KEY:
823 case PGPTAG_COMPRESSED_DATA:
824 case PGPTAG_SYMMETRIC_DATA:
826 case PGPTAG_LITERAL_DATA:
829 case PGPTAG_ENCRYPTED_MDC:
831 case PGPTAG_PRIVATE_60:
832 case PGPTAG_PRIVATE_62:
835 pgpPrtVal("", pgpTagTbl, p->tag);
836 pgpPrtHex("", p->body, p->blen);
844 pgpDig pgpNewDig(void)
846 pgpDig dig = xcalloc(1, sizeof(*dig));
851 pgpDigParams pgpDigParamsFree(pgpDigParams digp)
854 pgpDigAlgFree(digp->alg);
857 memset(digp, 0, sizeof(*digp));
863 void pgpCleanDig(pgpDig dig)
866 pgpDigParamsFree(dig->signature);
867 pgpDigParamsFree(dig->pubkey);
868 memset(dig, 0, sizeof(*dig));
873 pgpDig pgpFreeDig(pgpDig dig)
877 /* DUmp the signature/pubkey data. */
884 pgpDigParams pgpDigGetParams(pgpDig dig, unsigned int pkttype)
886 pgpDigParams params = NULL;
889 case PGPTAG_SIGNATURE:
890 params = dig->signature;
892 case PGPTAG_PUBLIC_KEY:
893 params = dig->pubkey;
900 int pgpDigParamsCmp(pgpDigParams p1, pgpDigParams p2)
902 int rc = 1; /* assume different, eg if either is NULL */
904 /* XXX Should we compare something else too? */
905 if (p1->hash_algo != p2->hash_algo)
907 if (p1->pubkey_algo != p2->pubkey_algo)
909 if (p1->version != p2->version)
911 if (p1->sigtype != p2->sigtype)
913 if (memcmp(p1->signid, p2->signid, sizeof(p1->signid)) != 0)
916 /* Parameters match ... at least for our purposes */
923 unsigned int pgpDigParamsAlgo(pgpDigParams digp, unsigned int algotype)
925 unsigned int algo = 0; /* assume failure */
928 case PGPVAL_PUBKEYALGO:
929 algo = digp->pubkey_algo;
931 case PGPVAL_HASHALGO:
932 algo = digp->hash_algo;
939 int pgpPrtParams(const uint8_t * pkts, size_t pktlen, unsigned int pkttype,
942 const uint8_t *p = pkts;
943 const uint8_t *pend = pkts + pktlen;
944 pgpDigParams digp = NULL;
946 int rc = -1; /* assume failure */
949 if (decodePkt(p, (pend - p), &pkt))
953 if (pkttype && pkt.tag != pkttype) {
956 digp = xcalloc(1, sizeof(*digp));
961 if (pgpPrtPkt(&pkt, digp))
964 p += (pkt.body - pkt.head) + pkt.blen;
967 rc = (digp && (p == pend)) ? 0 : -1;
969 if (ret && rc == 0) {
972 pgpDigParamsFree(digp);
977 int pgpPrtPkts(const uint8_t * pkts, size_t pktlen, pgpDig dig, int printing)
980 pgpDigParams digp = NULL;
984 rc = pgpPrtParams(pkts, pktlen, 0, &digp);
986 if (dig && rc == 0) {
987 if (digp->tag == PGPTAG_SIGNATURE) {
988 pgpDigParamsFree(dig->signature);
989 dig->signature = digp;
991 pgpDigParamsFree(dig->pubkey);
995 pgpDigParamsFree(digp);
1001 char *pgpIdentItem(pgpDigParams digp)
1006 char *signid = pgpHexStr(digp->signid+4, sizeof(digp->signid)-4);
1007 rasprintf(&id, _("V%d %s/%s %s, key ID %s"),
1009 pgpValStr(pgpPubkeyTbl, digp->pubkey_algo),
1010 pgpValStr(pgpHashTbl, digp->hash_algo),
1011 pgpValStr(pgpTagTbl, digp->tag),
1015 id = xstrdup(_("(none)"));
1020 rpmRC pgpVerifySignature(pgpDigParams key, pgpDigParams sig, DIGEST_CTX hashctx)
1022 DIGEST_CTX ctx = rpmDigestDup(hashctx);
1023 uint8_t *hash = NULL;
1025 rpmRC res = RPMRC_FAIL; /* assume failure */
1027 if (sig == NULL || ctx == NULL)
1030 if (sig->hash != NULL)
1031 rpmDigestUpdate(ctx, sig->hash, sig->hashlen);
1033 if (sig->version == 4) {
1034 /* V4 trailer is six octets long (rfc4880) */
1036 uint32_t nb = sig->hashlen;
1038 trailer[0] = sig->version;
1040 memcpy(trailer+2, &nb, 4);
1041 rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1044 rpmDigestFinal(ctx, (void **)&hash, &hashlen, 0);
1046 /* Compare leading 16 bits of digest for quick check. */
1047 if (hash == NULL || memcmp(hash, sig->signhash16, 2) != 0)
1051 * If we have a key, verify the signature for real. Otherwise we've
1052 * done all we can, return NOKEY to indicate "looks okay but dunno."
1054 if (key && key->alg) {
1055 pgpDigAlg sa = sig->alg;
1056 pgpDigAlg ka = key->alg;
1057 if (sa && sa->verify) {
1058 if (sa->verify(ka, sa, hash, hashlen, sig->hash_algo) == 0) {
1072 rpmRC pgpVerifySig(pgpDig dig, DIGEST_CTX hashctx)
1074 if (dig == NULL || hashctx == NULL)
1077 return pgpVerifySignature(pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY),
1078 pgpDigGetParams(dig, PGPTAG_SIGNATURE), hashctx);
1081 static pgpArmor decodePkts(uint8_t *b, uint8_t **pkt, size_t *pktlen)
1083 const char * enc = NULL;
1084 const char * crcenc = NULL;
1089 uint32_t crcpkt, crc;
1090 const char * armortype = NULL;
1093 pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP; /* XXX assume failure */
1095 #define TOKEQ(_s, _tok) (rstreqn((_s), (_tok), sizeof(_tok)-1))
1097 for (t = (char *)b; t && *t; t = te) {
1099 if ((te = strchr(t, '\n')) == NULL)
1107 if (!TOKEQ(t, "-----BEGIN PGP "))
1109 t += sizeof("-----BEGIN PGP ")-1;
1111 rc = pgpValTok(pgpArmorTbl, t, te);
1113 ec = PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE;
1116 if (rc != PGPARMOR_PUBKEY) /* XXX ASCII Pubkeys only, please. */
1119 armortype = pgpValStr(pgpArmorTbl, rc);
1120 t += strlen(armortype);
1121 if (!TOKEQ(t, "-----"))
1123 t += sizeof("-----")-1;
1124 if (*t != '\n' && *t != '\r')
1131 rc = pgpValTok(pgpArmorKeyTbl, t, te);
1134 if (*t != '\n' && *t != '\r') {
1138 enc = te; /* Start of encoded packets */
1145 *t++ = '\0'; /* Terminate encoded packets */
1146 crcenc = t; /* Start of encoded crc */
1151 if (!TOKEQ(t, "-----END PGP ")) {
1152 ec = PGPARMOR_ERR_NO_END_PGP;
1155 *t = '\0'; /* Terminate encoded crc */
1156 t += sizeof("-----END PGP ")-1;
1157 if (t >= te) continue;
1159 if (armortype == NULL) /* XXX can't happen */
1161 if (!rstreqn(t, armortype, strlen(armortype)))
1164 t += strlen(armortype);
1165 if (t >= te) continue;
1167 if (!TOKEQ(t, "-----")) {
1168 ec = PGPARMOR_ERR_NO_END_PGP;
1171 t += (sizeof("-----")-1);
1172 if (t >= te) continue;
1173 /* XXX permitting \r here is not RFC-2440 compliant <shrug> */
1174 if (!(*t == '\n' || *t == '\r')) continue;
1178 if (rpmBase64Decode(crcenc, (void **)&crcdec, &crclen) != 0) {
1179 ec = PGPARMOR_ERR_CRC_DECODE;
1182 crcpkt = pgpGrab(crcdec, crclen);
1183 crcdec = _free(crcdec);
1186 if (rpmBase64Decode(enc, (void **)&dec, &declen) != 0) {
1187 ec = PGPARMOR_ERR_BODY_DECODE;
1190 crc = pgpCRC(dec, declen);
1191 if (crcpkt != crc) {
1192 ec = PGPARMOR_ERR_CRC_CHECK;
1195 if (pkt) *pkt = dec;
1196 if (pktlen) *pktlen = declen;
1197 ec = PGPARMOR_PUBKEY; /* XXX ASCII Pubkeys only, please. */
1208 pgpArmor pgpReadPkts(const char * fn, uint8_t ** pkt, size_t * pktlen)
1212 pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP; /* XXX assume failure */
1213 int rc = rpmioSlurp(fn, &b, &blen);
1214 if (rc == 0 && b != NULL && blen > 0) {
1215 ec = decodePkts(b, pkt, pktlen);
1221 pgpArmor pgpParsePkts(const char *armor, uint8_t ** pkt, size_t * pktlen)
1223 pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP; /* XXX assume failure */
1224 if (armor && strlen(armor) > 0) {
1225 uint8_t *b = (uint8_t*) xstrdup(armor);
1226 ec = decodePkts(b, pkt, pktlen);
1232 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
1234 char *buf = NULL, *val = NULL;
1235 char *enc = rpmBase64Encode(s, ns, -1);
1236 char *crc = rpmBase64CRC(s, ns);
1237 const char *valstr = pgpValStr(pgpArmorTbl, atype);
1239 if (crc != NULL && enc != NULL) {
1240 rasprintf(&buf, "%s=%s", enc, crc);
1245 rasprintf(&val, "-----BEGIN PGP %s-----\nVersion: rpm-" VERSION " (NSS-3)\n\n"
1246 "%s\n-----END PGP %s-----\n",
1247 valstr, buf != NULL ? buf : "", valstr);