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 pubkeys stored in the rpmdb database
15 #include <sys/types.h>
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
30 #include <rpm/header.h>
32 #include <rpm/rpmdb.h>
40 #include "repo_rpmdb.h"
42 /* FIXME: dedup with repo_rpmdb.c */
44 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
46 const unsigned char *cp;
49 unsigned char *buf = 0, *bp;
51 /* check if it's already utf8, code taken from screen ;-) */
52 cp = (const unsigned char *)str;
53 while ((c = *cp++) != 0)
57 if ((c & 0xc0) != 0x80)
58 break; /* encoding error */
59 c = (c & 0x3f) | (state << 6);
60 if (!(state & 0x40000000))
62 /* check for overlong sequences */
63 if ((c & 0x820823e0) == 0x80000000)
65 else if ((c & 0x020821f0) == 0x02000000)
67 else if ((c & 0x000820f8) == 0x00080000)
69 else if ((c & 0x0000207c) == 0x00002000)
79 c = (c & 0x01) | 0xbffffffc; /* 5 bytes to follow */
81 c = (c & 0x03) | 0xbfffff00; /* 4 */
83 c = (c & 0x07) | 0xbfffc000; /* 3 */
85 c = (c & 0x0f) | 0xbff00000; /* 2 */
87 c = (c & 0x1f) | 0xfc000000; /* 1 */
91 state = (c & 0x80000000) ? c : 0;
95 /* not utf8, assume latin1 */
96 buf = solv_malloc(2 * strlen(str) + 1);
97 cp = (const unsigned char *)str;
100 while ((c = *cp++) != 0)
113 repodata_set_str(repodata, handle, tag, str);
119 r64dec1(char *p, unsigned int *vp, int *eofp)
129 if (x >= 'A' && x <= 'Z')
131 else if (x >= 'a' && x <= 'z')
133 else if (x >= '0' && x <= '9')
160 crc24(unsigned char *p, int len)
162 unsigned int crc = 0xb704ceL;
168 for (i = 0; i < 8; i++)
169 if ((crc <<= 1) & 0x1000000)
172 return crc & 0xffffffL;
175 static unsigned char *
176 unarmor(char *pubkey, int *pktlp)
180 unsigned char *buf, *bp;
184 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
186 pubkey = strchr(pubkey, '\n');
191 pubkey = strchr(pubkey, '\n');
194 /* skip header lines */
197 while (*pubkey == ' ' || *pubkey == '\t')
201 pubkey = strchr(pubkey, '\n');
206 p = strchr(pubkey, '=');
210 bp = buf = solv_malloc(l * 3 / 4 + 4);
214 pubkey = r64dec1(pubkey, &v, &eof);
224 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
227 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
232 if (v != crc24(buf, bp - buf))
237 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
239 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
249 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
252 unsigned char keyid[8];
253 unsigned int kcr = 0, maxex = 0;
254 unsigned char *pubkey = 0;
255 unsigned char *userid = 0;
261 for (; pl; p += l, pl -= l)
265 if (!(x & 128) || pl <= 0)
270 tag = (x & 0x3c) >> 2;
292 else if (x >= 192 && x < 224)
296 l = ((x - 192) << 8) + *p++ + 192;
301 /* sanity: p[0] must be zero */
302 if (pl <= 4 || p[0] != 0)
304 l = p[1] << 16 | p[2] << 8 | p[3];
315 pubkey = solv_realloc(pubkey, l);
317 memcpy(pubkey, p, l);
326 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
330 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
335 if (p[7] == 1) /* RSA */
338 unsigned char fp[16];
342 ql = ((p[8] << 8 | p[9]) + 7) / 8;
343 memcpy(keyid, p + 10 + ql - 8, 8);
344 h = solv_chksum_create(REPOKEY_TYPE_MD5);
345 solv_chksum_add(h, p + 10, ql);
347 ql = ((q[0] << 8 | q[1]) + 7) / 8;
348 solv_chksum_add(h, q + 2, ql);
349 solv_chksum_free(h, fp);
350 for (i = 0; i < 16; i++)
351 sprintf(fpx + i * 2, "%02x", fp[i]);
352 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
359 unsigned char hdr[3];
360 unsigned char fp[20];
363 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
367 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
368 solv_chksum_add(h, hdr, 3);
369 solv_chksum_add(h, p, l);
370 solv_chksum_free(h, fp);
371 for (i = 0; i < 20; i++)
372 sprintf(fpx + i * 2, "%02x", fp[i]);
373 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
374 memcpy(keyid, fp + 12, 8);
379 if (p[0] == 3 && p[1] == 5)
384 /* printf("V3 signature packet\n"); */
387 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
389 if (!memcmp(keyid, p + 6, 8))
391 /* printf("SELF SIG\n"); */
395 /* printf("OTHER SIG\n"); */
399 htype = REPOKEY_TYPE_MD5;
401 htype = REPOKEY_TYPE_SHA1;
403 htype = REPOKEY_TYPE_SHA256;
406 void *h = solv_chksum_create(htype);
407 unsigned char b[3], *cs;
412 solv_chksum_add(h, b, 3);
413 solv_chksum_add(h, pubkey, pubkeyl);
414 if (p[2] >= 0x10 && p[2] <= 0x13)
415 solv_chksum_add(h, userid, useridl);
416 solv_chksum_add(h, p + 2, 5);
417 cs = solv_chksum_get(h, 0);
418 solv_chksum_free(h, 0);
424 int j, ql, haveissuer;
428 unsigned int scr = 0;
430 unsigned char issuer[8];
432 /* printf("V4 signature packet\n"); */
435 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
440 for (j = 0; q && j < 2; j++)
447 ql = q[0] << 8 | q[1];
463 if (ql < 4 || q[0] != 0)
468 sl = q[1] << 16 | q[2] << 8 | q[3];
479 sl = ((x - 192) << 8) + *q++ + 192;
488 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
489 if (x == 16 && sl == 9 && !haveissuer)
491 memcpy(issuer, q + 1, 8);
495 if (x == 2 && j == 0)
496 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
498 if (x == 9 && j == 0)
499 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
511 htype = REPOKEY_TYPE_MD5;
513 htype = REPOKEY_TYPE_SHA1;
515 htype = REPOKEY_TYPE_SHA256;
516 if (htype && pubkeyl)
518 void *h = solv_chksum_create(htype);
519 unsigned char b[6], *cs;
525 solv_chksum_add(h, b, 3);
526 solv_chksum_add(h, pubkey, pubkeyl);
527 if (p[1] >= 0x10 && p[1] <= 0x13)
530 b[1] = useridl >> 24;
531 b[2] = useridl >> 16;
534 solv_chksum_add(h, b, 5);
535 solv_chksum_add(h, userid, useridl);
537 hl = 6 + (p[4] << 8 | p[5]);
538 solv_chksum_add(h, p, hl);
545 solv_chksum_add(h, b, 6);
546 cs = solv_chksum_get(h, 0);
547 solv_chksum_free(h, 0);
550 if (!memcmp(keyid, issuer, 8))
552 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
558 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
565 userid = solv_realloc(userid, l);
567 memcpy(userid, p, l);
574 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
579 /* this is private to rpm, but rpm lacks an interface to retrieve
580 * the values. Sigh. */
581 struct pgpDigParams_s {
583 const unsigned char * hash;
584 #ifndef HAVE_PGPDIGGETPARAMS
585 const char * params[4];
588 unsigned char version; /*!< version number. */
589 unsigned char time[4]; /*!< time that the key was created. */
590 unsigned char pubkey_algo; /*!< public key algorithm. */
591 unsigned char hash_algo;
592 unsigned char sigtype;
593 unsigned char hashlen;
594 unsigned char signhash16[2];
595 unsigned char signid[8];
599 #ifndef HAVE_PGPDIGGETPARAMS
601 struct pgpDigParams_s signature;
602 struct pgpDigParams_s pubkey;
607 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
609 Pool *pool = s->repo->pool;
615 char evrbuf[8 + 1 + 8 + 1];
616 struct pgpDigParams_s *digpubkey;
618 pkts = unarmor(pubkey, &pktsl);
621 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
622 parsekeydata(s, data, pkts, pktsl);
623 /* only rpm knows how to do the release calculation, we don't dare
624 * to recreate all the bugs */
628 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
630 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
632 #ifdef HAVE_PGPDIGGETPARAMS
633 digpubkey = pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY);
635 digpubkey = &dig->pubkey;
637 btime = digpubkey->time[0] << 24 | digpubkey->time[1] << 16 | digpubkey->time[2] << 8 | digpubkey->signid[3];
638 sprintf(evrbuf, "%02x%02x%02x%02x-%02x%02x%02x%02x", digpubkey->signid[4], digpubkey->signid[5], digpubkey->signid[6], digpubkey->signid[7], digpubkey->time[0], digpubkey->time[1], digpubkey->time[2], digpubkey->time[3]);
640 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
642 s->name = pool_str2id(pool, "gpg-pubkey", 1);
643 s->evr = pool_str2id(pool, evrbuf, 1);
645 for (i = 0; i < 8; i++)
646 sprintf(keyid + 2 * i, "%02x", digpubkey->signid[i]);
647 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
648 if (digpubkey->userid)
649 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, digpubkey->userid);
651 (void)pgpFreeDig(dig);
653 (void)pgpDigFree(dig);
655 solv_free((void *)pkts);
660 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
662 Pool *pool = repo->pool;
668 const char *rootdir = 0;
671 data = repo_add_repodata(repo, flags);
672 if (flags & REPO_USE_ROOTDIR)
673 rootdir = pool_get_rootdir(pool);
674 state = rpm_state_create(repo->pool, rootdir);
676 rpm_installedrpmdbids(state, "Name", "gpg-pubkey", &q);
677 for (i = 0; i < q.count; i++)
680 unsigned long long itime;
682 handle = rpm_byrpmdbid(state, q.elements[i]);
685 str = rpm_query(handle, SOLVABLE_DESCRIPTION);
688 s = pool_id2solvable(pool, repo_add_solvable(repo));
689 pubkey2solvable(s, data, str);
691 itime = rpm_query_num(handle, SOLVABLE_INSTALLTIME, 0);
693 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, itime);
695 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
696 repo->rpmdbid[s - pool->solvables - repo->start] = q.elements[i];
699 rpm_state_free(state);
700 if (!(flags & REPO_NO_INTERNALIZE))
701 repodata_internalize(data);
706 repo_add_pubkey(Repo *repo, const char *key, int flags)
708 Pool *pool = repo->pool;
715 data = repo_add_repodata(repo, flags);
718 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
720 pool_error(pool, -1, "%s: %s", key, strerror(errno));
728 buf = solv_realloc(buf, bufl);
730 ll = fread(buf, 1, bufl - l, fp);
734 pool_error(pool, -1, "%s: %s", key, strerror(errno));
743 s = pool_id2solvable(pool, repo_add_solvable(repo));
744 if (!pubkey2solvable(s, data, buf))
746 repo_free_solvable(repo, s - pool->solvables, 1);
751 if (!(flags & REPO_NO_INTERNALIZE))
752 repodata_internalize(data);
753 return s - pool->solvables;