2 * Copyright (c) 2007-2013, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * support for pubkey reading
15 #include <sys/types.h>
28 #include <rpm/rpmio.h>
29 #include <rpm/rpmpgp.h>
31 #include <rpm/header.h>
33 #include <rpm/rpmdb.h>
41 #include "repo_rpmdb.h"
42 #include "repo_pubkey.h"
44 #include "solv_pgpvrfy.h"
48 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
50 if (str[solv_validutf8(str)])
52 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
53 repodata_set_str(repodata, handle, tag, ustr);
57 repodata_set_str(repodata, handle, tag, str);
61 r64dec1(char *p, unsigned int *vp, int *eofp)
71 if (x >= 'A' && x <= 'Z')
73 else if (x >= 'a' && x <= 'z')
75 else if (x >= '0' && x <= '9')
102 crc24(unsigned char *p, int len)
104 unsigned int crc = 0xb704ce;
110 for (i = 0; i < 8; i++)
111 if ((crc <<= 1) & 0x1000000)
114 return crc & 0xffffff;
118 unarmor(char *pubkey, unsigned char **pktp, int *pktlp, const char *startstr, const char *endstr)
120 char *p, *pubkeystart = pubkey;
122 unsigned char *buf, *bp;
129 l = strlen(startstr);
130 while (strncmp(pubkey, startstr, l) != 0)
132 pubkey = strchr(pubkey, '\n');
137 pubkey = strchr(pubkey, '\n');
140 /* skip header lines */
143 while (*pubkey == ' ' || *pubkey == '\t')
147 pubkey = strchr(pubkey, '\n');
152 p = strchr(pubkey, '=');
156 bp = buf = solv_malloc(l * 3 / 4 + 4);
160 pubkey = r64dec1(pubkey, &v, &eof);
170 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
173 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
178 if (v != crc24(buf, bp - buf))
183 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
185 if (strncmp(pubkey, endstr, strlen(endstr)) != 0)
190 p = strchr(pubkey, '\n');
192 p = pubkey + strlen(pubkey);
195 return (p ? p + 1 : pubkey + strlen(pubkey)) - pubkeystart;
198 #define ARMOR_NLAFTER 16
201 armor(unsigned char *pkt, int pktl, const char *startstr, const char *endstr, const char *version)
203 static const char bintoasc[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
204 char *str = solv_malloc(strlen(startstr) + strlen(endstr) + strlen(version) + (pktl / 3) * 4 + (pktl / (ARMOR_NLAFTER * 3)) + 30);
209 v = crc24(pkt, pktl);
210 sprintf(p, "%s\nVersion: %s\n\n", startstr, version);
212 for (i = -1; pktl > 0; pktl -= 3)
214 if (++i == ARMOR_NLAFTER)
220 b = pktl > 1 ? *pkt++ : 0;
221 c = pktl > 2 ? *pkt++ : 0;
222 *p++ = bintoasc[a >> 2];
223 *p++ = bintoasc[(a & 3) << 4 | b >> 4];
224 *p++ = pktl > 1 ? bintoasc[(b & 15) << 2 | c >> 6] : '=';
225 *p++ = pktl > 2 ? bintoasc[c & 63] : '=';
229 *p++ = bintoasc[v >> 18 & 0x3f];
230 *p++ = bintoasc[v >> 12 & 0x3f];
231 *p++ = bintoasc[v >> 6 & 0x3f];
232 *p++ = bintoasc[v & 0x3f];
233 sprintf(p, "\n%s\n", endstr);
237 /* internal representation of a signature */
241 unsigned char issuer[8];
243 unsigned int created;
244 unsigned int expires;
245 unsigned int keyexpires;
246 unsigned char *sigdata;
252 pgphashalgo2type(int algo)
255 return REPOKEY_TYPE_MD5;
257 return REPOKEY_TYPE_SHA1;
259 return REPOKEY_TYPE_SHA256;
263 /* hash the pubkey/userid data for self-sig verification
264 * hash the final trailer
265 * create a "sigdata" block suitable for a call to solv_pgpverify */
267 pgpsig_makesigdata(struct pgpsig *sig, unsigned char *p, int l, unsigned char *pubkey, int pubkeyl, unsigned char *userid, int useridl, Chksum *h)
269 int type = sig->type;
271 const unsigned char *cs;
274 if (!h || sig->mpioff < 2 || l <= sig->mpioff)
276 if ((type >= 0x10 && type <= 0x13) || type == 0x1f || type == 0x18 || type == 0x20 || type == 0x28)
281 solv_chksum_add(h, b, 3);
282 solv_chksum_add(h, pubkey, pubkeyl);
284 if ((type >= 0x10 && type <= 0x13))
289 b[1] = useridl >> 24;
290 b[2] = useridl >> 16;
293 solv_chksum_add(h, b, 5);
295 solv_chksum_add(h, userid, useridl);
299 solv_chksum_add(h, p + 2, 5);
302 int hl = 6 + (p[4] << 8 | p[5]);
303 solv_chksum_add(h, p, hl);
310 solv_chksum_add(h, b, 6);
312 cs = solv_chksum_get(h, &csl);
313 if (cs[0] == p[sig->mpioff - 2] && cs[1] == p[sig->mpioff - 1])
315 int ml = l - sig->mpioff;
316 sig->sigdata = solv_malloc(2 + csl + ml);
317 sig->sigdatal = 2 + csl + ml;
318 sig->sigdata[0] = p[0] == 3 ? p[15] : p[2];
319 sig->sigdata[1] = p[0] == 3 ? p[16] : p[3];
320 memcpy(sig->sigdata + 2, cs, csl);
321 memcpy(sig->sigdata + 2 + csl, p + sig->mpioff, ml);
325 /* parse the header of a subpacket contained in a signature packet
326 * returns: length of the packet header, 0 if there was an error
327 * *pktlp is set to the packet length, the tag is the first byte.
330 parsesubpkglength(unsigned char *q, int ql, int *pktlp)
333 /* decode sub-packet length, ql must be > 0 */
342 if (ql < 5 || q[0] != 0)
344 sl = q[1] << 16 | q[2] << 8 | q[3];
351 sl = ((x - 192) << 8) + q[0] + 192;
354 if (!sl || ql < sl + hl) /* sub pkg tag is included in length, i.e. sl must not be zero */
360 /* parse a signature packet, initializing the pgpsig struct */
362 pgpsig_init(struct pgpsig *sig, unsigned char *p, int l)
364 memset(sig, 0, sizeof(*sig));
368 /* printf("V3 signature packet\n"); */
369 if (l <= 19 || p[1] != 5)
373 memcpy(sig->issuer, p + 7, 8);
374 sig->created = p[3] << 24 | p[4] << 16 | p[5] << 8 | p[6];
375 sig->hashalgo = p[16];
383 /* printf("V4 signature packet\n"); */
387 sig->hashalgo = p[3];
389 sig->keyexpires = -1;
390 for (j = 0; q && j < 2; j++)
397 ql = q[0] << 8 | q[1];
407 hl = parsesubpkglength(q, ql, &sl);
415 x = q[0] & 127; /* strip critical bit */
416 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
417 if (x == 16 && sl == 9 && !sig->haveissuer)
420 memcpy(sig->issuer, q + 1, 8);
422 if (x == 2 && j == 0)
423 sig->created = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
424 if (x == 3 && j == 0)
425 sig->expires = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
426 if (x == 9 && j == 0)
427 sig->keyexpires = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
432 if (q && q - p + 2 < l)
433 sig->mpioff = q - p + 2;
437 /* parse a pgp packet header
438 * returns: length of the packet header, 0 if there was an error
439 * *tagp and *pktlp is set to the packet tag and the packet length
442 parsepkgheader(unsigned char *p, int pl, int *tagp, int *pktlp)
444 unsigned char *op = p;
451 if (!(x & 128) || pl <= 0)
455 *tagp = (x & 0x3c) >> 2; /* old format */
457 if (x > 4 || pl < x || (x == 4 && p[0]))
465 *tagp = (x & 0x3f); /* new format */
470 else if (x >= 192 && x < 224)
474 l = ((x - 192) << 8) + *p++ + 192;
479 if (pl <= 4 || p[0] != 0) /* sanity: p[0] must be zero */
481 l = p[1] << 16 | p[2] << 8 | p[3];
494 /* parse the first pubkey (possible creating new packages for the subkeys)
495 * returns the number of parsed bytes.
496 * if flags contains ADD_WITH_SUBKEYS, all subkeys will be added as new
497 * solvables as well */
499 parsepubkey(Solvable *s, Repodata *data, unsigned char *p, int pl, int flags)
501 Repo *repo = s->repo;
502 unsigned char *pstart = p;
504 unsigned char keyid[8];
505 char subkeyofstr[17];
506 unsigned int kcr = 0, maxex = 0, maxsigcr = 0;
507 unsigned char *pubkey = 0;
510 unsigned char *userid = 0;
512 unsigned char *pubdata = 0;
516 for (; ; p += l, pl -= l)
518 int hl = parsepkgheader(p, pl, &tag, &l);
519 if (!hl || (pubkey && (tag == 6 || tag == 14)))
523 repodata_set_num(data, s - repo->pool->solvables, SOLVABLE_BUILDTIME, kcr);
524 if (maxex && maxex != -1)
525 repodata_set_num(data, s - repo->pool->solvables, PUBKEY_EXPIRES, maxex);
526 s->name = pool_str2id(s->repo->pool, insubkey ? "gpg-subkey" : "gpg-pubkey", 1);
529 if (userid && useridl)
531 char *useridstr = solv_malloc(useridl + 1);
532 memcpy(useridstr, userid, useridl);
533 useridstr[useridl] = 0;
534 setutf8string(data, s - repo->pool->solvables, SOLVABLE_SUMMARY, useridstr);
540 char evr[8 + 1 + 8 + 1];
541 solv_bin2hex(keyid, 8, keyidstr);
542 repodata_set_str(data, s - repo->pool->solvables, PUBKEY_KEYID, keyidstr);
543 /* build rpm-style evr */
544 strcpy(evr, keyidstr + 8);
545 sprintf(evr + 8, "-%08x", maxsigcr);
546 s->evr = pool_str2id(repo->pool, evr, 1);
548 if (insubkey && *subkeyofstr)
549 repodata_set_str(data, s - repo->pool->solvables, PUBKEY_SUBKEYOF, subkeyofstr);
550 if (pubdata) /* set data blob */
551 repodata_set_binary(data, s - repo->pool->solvables, PUBKEY_DATA, pubdata, pubdatal);
556 p = 0; /* parse error */
559 if (tag == 6 || (tag == 14 && !(flags & ADD_WITH_SUBKEYS)))
561 if (tag == 14 && pubdata && !insubkey)
562 solv_bin2hex(keyid, 8, subkeyofstr);
563 /* create new solvable for subkey */
564 s = pool_id2solvable(repo->pool, repo_add_solvable(repo));
568 if (!pubkey && tag != 6)
570 if (tag == 6 || (tag == 14 && (flags & ADD_WITH_SUBKEYS) != 0)) /* Public-Key Packet */
574 pubkey = solv_memdup(p, l);
581 if (p[0] == 3 && l >= 10)
585 maxsigcr = kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
589 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
594 if (p[7] == 1) /* RSA */
597 unsigned char fp[16];
601 ql = ((p[8] << 8 | p[9]) + 7) / 8; /* length of public modulus */
602 if (ql >= 8 && 10 + ql + 2 <= l)
604 memcpy(keyid, p + 10 + ql - 8, 8); /* keyid is last 64 bits of public modulus */
606 ql2 = ((q[0] << 8 | q[1]) + 7) / 8; /* length of encryption exponent */
607 if (10 + ql + 2 + ql2 <= l)
609 /* fingerprint is the md5 over the two MPI bodies */
610 h = solv_chksum_create(REPOKEY_TYPE_MD5);
611 solv_chksum_add(h, p + 10, ql);
612 solv_chksum_add(h, q + 2, ql2);
613 solv_chksum_free(h, fp);
614 solv_bin2hex(fp, 16, fpx);
615 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
622 else if (p[0] == 4 && l >= 6)
625 unsigned char hdr[3];
626 unsigned char fp[20];
629 maxsigcr = kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
633 /* fingerprint is the sha1 over the packet */
634 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
635 solv_chksum_add(h, hdr, 3);
636 solv_chksum_add(h, p, l);
637 solv_chksum_free(h, fp);
638 solv_bin2hex(fp, 20, fpx);
639 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
640 memcpy(keyid, fp + 12, 8); /* keyid is last 64 bits of fingerprint */
645 if (tag == 2) /* Signature Packet */
651 pgpsig_init(&sig, p, l);
652 if (!sig.haveissuer || !((sig.type >= 0x10 && sig.type <= 0x13) || sig.type == 0x1f))
654 if (sig.type >= 0x10 && sig.type <= 0x13 && !userid)
656 htype = pgphashalgo2type(sig.hashalgo);
657 if (htype && sig.mpioff)
659 Chksum *h = solv_chksum_create(htype);
660 pgpsig_makesigdata(&sig, p, l, pubkey, pubkeyl, userid, useridl, h);
661 solv_chksum_free(h, 0);
663 if (!memcmp(keyid, sig.issuer, 8))
665 #ifdef ENABLE_PGPVRFY
666 /* found self sig, verify */
667 if (solv_pgpvrfy(pubdata, pubdatal, sig.sigdata, sig.sigdatal))
670 if (sig.keyexpires && maxex != -1)
672 if (sig.keyexpires == -1)
674 else if (sig.keyexpires + kcr > maxex)
675 maxex = sig.keyexpires + kcr;
677 if (sig.created > maxsigcr)
678 maxsigcr = sig.created;
681 else if (flags & ADD_WITH_KEYSIGNATURES)
684 Id shandle = repodata_new_handle(data);
685 solv_bin2hex(sig.issuer, 8, issuerstr);
686 repodata_set_str(data, shandle, SIGNATURE_ISSUER, issuerstr);
688 repodata_set_num(data, shandle, SIGNATURE_TIME, sig.created);
690 repodata_set_num(data, shandle, SIGNATURE_EXPIRES, sig.created + sig.expires);
692 repodata_set_binary(data, shandle, SIGNATURE_DATA, sig.sigdata, sig.sigdatal);
693 repodata_add_flexarray(data, s - s->repo->pool->solvables, PUBKEY_SIGNATURES, shandle);
695 solv_free(sig.sigdata);
697 if (tag == 13 && !insubkey) /* User ID Packet */
699 userid = solv_realloc(userid, l);
701 memcpy(userid, p, l);
707 return p ? p - pstart : 0;
713 /* this is private to rpm, but rpm lacks an interface to retrieve
714 * the values. Sigh. */
715 struct pgpDigParams_s {
717 const unsigned char * hash;
718 #ifndef HAVE_PGPDIGGETPARAMS
719 const char * params[4];
722 unsigned char version; /*!< version number. */
723 unsigned char time[4]; /*!< time that the key was created. */
724 unsigned char pubkey_algo; /*!< public key algorithm. */
725 unsigned char hash_algo;
726 unsigned char sigtype;
727 unsigned char hashlen;
728 unsigned char signhash16[2];
729 unsigned char signid[8];
733 #ifndef HAVE_PGPDIGGETPARAMS
735 struct pgpDigParams_s signature;
736 struct pgpDigParams_s pubkey;
741 /* only rpm knows how to do the release calculation, we don't dare
742 * to recreate all the bugs in libsolv */
744 parsepubkey_rpm(Solvable *s, Repodata *data, unsigned char *pkts, int pktsl)
746 Pool *pool = s->repo->pool;
747 struct pgpDigParams_s *digpubkey;
750 char evrbuf[8 + 1 + 8 + 1];
756 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
758 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
759 #ifdef HAVE_PGPDIGGETPARAMS
760 digpubkey = pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY);
762 digpubkey = &dig->pubkey;
766 btime = digpubkey->time[0] << 24 | digpubkey->time[1] << 16 | digpubkey->time[2] << 8 | digpubkey->time[3];
767 solv_bin2hex(digpubkey->signid, 8, keyid);
768 solv_bin2hex(digpubkey->signid + 4, 4, evrbuf);
770 solv_bin2hex(digpubkey->time, 4, evrbuf + 9);
771 s->evr = pool_str2id(pool, evrbuf, 1);
772 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
773 if (digpubkey->userid)
774 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, digpubkey->userid);
776 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
779 (void)pgpFreeDig(dig);
781 (void)pgpDigFree(dig);
785 #endif /* ENABLE_RPMDB */
787 /* parse an ascii armored pubkey
788 * adds multiple pubkeys if ADD_MULTIPLE_PUBKEYS is set */
790 pubkey2solvable(Pool *pool, Id p, Repodata *data, char *pubkey, int flags)
792 unsigned char *pkts, *pkts_orig;
793 int pktsl, pl = 0, tag, l, hl;
795 if (!unarmor(pubkey, &pkts, &pktsl, "-----BEGIN PGP PUBLIC KEY BLOCK-----", "-----END PGP PUBLIC KEY BLOCK-----"))
797 pool_error(pool, 0, "unarmor failure");
806 setutf8string(data, p, SOLVABLE_DESCRIPTION, pubkey);
807 pl = parsepubkey(pool->solvables + p, data, pkts, pktsl, flags);
809 parsepubkey_rpm(pool->solvables + p, data, pkts, pktsl);
811 if (!pl || !(flags & ADD_MULTIPLE_PUBKEYS))
816 hl = parsepkgheader(pkts, pktsl, &tag, &l);
821 p = repo_add_solvable(pool->solvables[p].repo);
823 solv_free((void *)pkts_orig);
830 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
832 Pool *pool = repo->pool;
837 const char *rootdir = 0;
840 data = repo_add_repodata(repo, flags);
841 if (flags & REPO_USE_ROOTDIR)
842 rootdir = pool_get_rootdir(pool);
843 state = rpm_state_create(repo->pool, rootdir);
845 rpm_installedrpmdbids(state, "Name", "gpg-pubkey", &q);
846 for (i = 0; i < q.count; i++)
850 unsigned long long itime;
852 handle = rpm_byrpmdbid(state, q.elements[i]);
855 str = rpm_query(handle, SOLVABLE_DESCRIPTION);
858 p = repo_add_solvable(repo);
859 if (!pubkey2solvable(pool, p, data, str, flags))
862 repo_free_solvable(repo, p, 1);
866 itime = rpm_query_num(handle, SOLVABLE_INSTALLTIME, 0);
867 for (p2 = p; p2 < pool->nsolvables; p2++)
870 repodata_set_num(data, p2, SOLVABLE_INSTALLTIME, itime);
872 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
873 repo->rpmdbid[p2 - repo->start] = q.elements[i];
877 rpm_state_free(state);
878 if (!(flags & REPO_NO_INTERNALIZE))
879 repodata_internalize(data);
886 solv_slurp(FILE *fp, int *lenp)
892 for (l = 0; ; l += ll)
897 buf = solv_realloc(buf, bufl);
899 ll = fread(buf + l, 1, bufl - l, fp);
902 buf = solv_free(buf);
908 buf[l] = 0; /* always zero-terminate */
918 repo_add_pubkey(Repo *repo, const char *keyfile, int flags)
920 Pool *pool = repo->pool;
926 data = repo_add_repodata(repo, flags);
928 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, keyfile) : keyfile, "r")) == 0)
930 pool_error(pool, -1, "%s: %s", keyfile, strerror(errno));
933 if ((buf = solv_slurp(fp, 0)) == 0)
935 pool_error(pool, -1, "%s: %s", keyfile, strerror(errno));
940 p = repo_add_solvable(repo);
941 if (!pubkey2solvable(pool, p, data, buf, flags))
943 repo_free_solvable(repo, p, 1);
947 if (!(flags & REPO_NO_LOCATION))
950 for (p2 = p; p2 < pool->nsolvables; p2++)
951 repodata_set_location(data, p2, 0, 0, keyfile);
954 if (!(flags & REPO_NO_INTERNALIZE))
955 repodata_internalize(data);
960 is_sig_packet(unsigned char *sig, int sigl)
964 if ((sig[0] & 0x80) == 0 || (sig[0] & 0x40 ? sig[0] & 0x3f : sig[0] >> 2 & 0x0f) != 2)
970 is_pubkey_packet(unsigned char *pkt, int pktl)
974 if ((pkt[0] & 0x80) == 0 || (pkt[0] & 0x40 ? pkt[0] & 0x3f : pkt[0] >> 2 & 0x0f) != 6)
980 add_one_pubkey(Pool *pool, Repo *repo, Repodata *data, unsigned char *pbuf, int pbufl, int flags)
982 Id p = repo_add_solvable(repo);
983 char *solvversion = pool_tmpjoin(pool, "libsolv-", LIBSOLV_VERSION_STRING, 0);
984 char *descr = armor(pbuf, pbufl, "-----BEGIN PGP PUBLIC KEY BLOCK-----", "-----END PGP PUBLIC KEY BLOCK-----", solvversion);
985 setutf8string(data, p, SOLVABLE_DESCRIPTION, descr);
986 parsepubkey(pool->solvables + p, data, pbuf, pbufl, flags);
988 parsepubkey_rpm(pool->solvables + p, data, pbuf, pbufl);
993 repo_add_keyring(Repo *repo, FILE *fp, int flags)
995 Pool *pool = repo->pool;
997 unsigned char *buf, *p, *pbuf;
998 int bufl, l, pl, pbufl;
1000 data = repo_add_repodata(repo, flags);
1001 buf = (unsigned char *)solv_slurp(fp, &bufl);
1002 if (buf && !is_pubkey_packet(buf, bufl))
1004 /* assume ascii armored */
1005 unsigned char *nbuf = 0, *ubuf;
1008 for (l = 0; (nl = unarmor((char *)buf + l, &ubuf, &ubufl, "-----BEGIN PGP PUBLIC KEY BLOCK-----", "-----END PGP PUBLIC KEY BLOCK-----")) != 0; l += nl)
1010 /* found another block. concat. */
1011 nbuf = solv_realloc(nbuf, bufl + ubufl);
1013 memcpy(nbuf + bufl, ubuf, ubufl);
1020 /* now split into pubkey parts, ignoring the packets we don't know */
1023 for (p = buf; bufl; p += pl, bufl -= pl)
1026 int hl = parsepkgheader(p, bufl, &tag, &pl);
1032 /* found new pubkey! flush old */
1035 add_one_pubkey(pool, repo, data, pbuf, pbufl, flags);
1036 pbuf = solv_free(pbuf);
1040 if (tag != 6 && !pbufl)
1042 if (tag != 6 && tag != 2 && tag != 13 && tag != 14 && tag != 17)
1044 /* we want that packet. concat. */
1045 pbuf = solv_realloc(pbuf, pbufl + pl);
1046 memcpy(pbuf + pbufl, p, pl);
1050 add_one_pubkey(pool, repo, data, pbuf, pbufl, flags);
1053 if (!(flags & REPO_NO_INTERNALIZE))
1054 repodata_internalize(data);
1059 repo_add_keydir(Repo *repo, const char *keydir, const char *suffix, int flags)
1061 Pool *pool = repo->pool;
1064 struct dirent **namelist;
1067 data = repo_add_repodata(repo, flags);
1068 nent = scandir(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, keydir) : keydir, &namelist, 0, alphasort);
1070 return pool_error(pool, -1, "%s: %s", keydir, strerror(errno));
1071 rkeydir = pool_prepend_rootdir(pool, keydir);
1072 sl = suffix ? strlen(suffix) : 0;
1073 for (i = 0; i < nent; i++)
1075 const char *dn = namelist[i]->d_name;
1077 if (*dn == '.' && !(flags & ADD_KEYDIR_WITH_DOTFILES))
1080 if (sl && (l < sl || strcmp(dn + l - sl, suffix) != 0))
1082 repo_add_pubkey(repo, pool_tmpjoin(pool, rkeydir, "/", dn), flags | REPO_REUSE_REPODATA);
1085 for (i = 0; i < nent; i++)
1086 solv_free(namelist[i]);
1087 solv_free(namelist);
1088 if (!(flags & REPO_NO_INTERNALIZE))
1089 repodata_internalize(data);
1094 solvsig_create(FILE *fp)
1098 int sigl, hl, tag, pktl;
1099 struct pgpsig pgpsig;
1101 if ((sig = (unsigned char *)solv_slurp(fp, &sigl)) == 0)
1103 if (!is_sig_packet(sig, sigl))
1105 /* not a raw sig, check armored */
1106 unsigned char *nsig;
1107 if (!unarmor((char *)sig, &nsig, &sigl, "-----BEGIN PGP SIGNATURE-----", "-----END PGP SIGNATURE-----"))
1114 if (!is_sig_packet(sig, sigl))
1120 hl = parsepkgheader(sig, sigl, &tag, &pktl);
1121 if (!hl || tag != 2 || !pktl)
1126 pgpsig_init(&pgpsig, sig + hl, pktl);
1127 if (pgpsig.type != 0 || !pgpsig.haveissuer)
1132 ss = solv_calloc(1, sizeof(*ss));
1133 ss->sigpkt = solv_memdup(sig + hl, pktl);
1136 solv_bin2hex(pgpsig.issuer, 8, ss->keyid);
1137 ss->htype = pgphashalgo2type(pgpsig.hashalgo);
1138 ss->created = pgpsig.created;
1139 ss->expires = pgpsig.expires;
1144 solvsig_free(Solvsig *ss)
1146 solv_free(ss->sigpkt);
1151 repo_find_all_pubkeys_cmp(const void *va, const void *vb, void *dp)
1156 /* cannot use evrcmp, as rpm says '0' > 'a' */
1157 return strcmp(pool_id2str(pool, pool->solvables[b].evr), pool_id2str(pool, pool->solvables[a].evr));
1161 repo_find_all_pubkeys(Repo *repo, const char *keyid, Queue *q)
1170 FOR_REPO_SOLVABLES(repo, p, s)
1172 const char *kidstr, *evr = pool_id2str(s->repo->pool, s->evr);
1174 if (!evr || strncmp(evr, keyid + 8, 8) != 0)
1176 kidstr = solvable_lookup_str(s, PUBKEY_KEYID);
1177 if (kidstr && !strcmp(kidstr, keyid))
1181 solv_sort(q->elements, q->count, sizeof(Id), repo_find_all_pubkeys_cmp, repo->pool);
1185 repo_find_pubkey(Repo *repo, const char *keyid)
1190 repo_find_all_pubkeys(repo, keyid, &q);
1191 p = q.count ? q.elements[0] : 0;
1196 #ifdef ENABLE_PGPVRFY
1198 /* warning: does not check key expiry/revokation, same as with gpgv or rpm */
1199 /* returns the Id of the pubkey that verified the signature */
1201 repo_verify_sigdata(Repo *repo, unsigned char *sigdata, int sigdatal, const char *keyid)
1207 if (!sigdata || !keyid)
1210 repo_find_all_pubkeys(repo, keyid, &q);
1211 for (i = 0; i < q.count; i++)
1214 const unsigned char *pubdata = repo_lookup_binary(repo, q.elements[i], PUBKEY_DATA, &pubdatal);
1215 if (pubdata && solv_pgpvrfy(pubdata, pubdatal, sigdata, sigdatal))
1218 p = i < q.count? q.elements[i] : 0;
1224 solvsig_verify(Solvsig *ss, Repo *repo, Chksum *chk)
1226 struct pgpsig pgpsig;
1230 if (!chk || solv_chksum_isfinished(chk))
1232 pgpsig_init(&pgpsig, ss->sigpkt, ss->sigpktl);
1233 chk2 = solv_chksum_create_clone(chk);
1234 pgpsig_makesigdata(&pgpsig, ss->sigpkt, ss->sigpktl, 0, 0, 0, 0, chk2);
1235 solv_chksum_free(chk2, 0);
1236 if (!pgpsig.sigdata)
1238 p = repo_verify_sigdata(repo, pgpsig.sigdata, pgpsig.sigdatal, ss->keyid);
1239 solv_free(pgpsig.sigdata);