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"
43 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
45 if (str[solv_validutf8(str)])
47 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
48 repodata_set_str(repodata, handle, tag, ustr);
52 repodata_set_str(repodata, handle, tag, str);
56 r64dec1(char *p, unsigned int *vp, int *eofp)
66 if (x >= 'A' && x <= 'Z')
68 else if (x >= 'a' && x <= 'z')
70 else if (x >= '0' && x <= '9')
97 crc24(unsigned char *p, int len)
99 unsigned int crc = 0xb704ceL;
105 for (i = 0; i < 8; i++)
106 if ((crc <<= 1) & 0x1000000)
109 return crc & 0xffffffL;
112 static unsigned char *
113 unarmor(char *pubkey, int *pktlp)
117 unsigned char *buf, *bp;
121 while (strncmp(pubkey, "-----BEGIN PGP PUBLIC KEY BLOCK-----", 36) != 0)
123 pubkey = strchr(pubkey, '\n');
128 pubkey = strchr(pubkey, '\n');
131 /* skip header lines */
134 while (*pubkey == ' ' || *pubkey == '\t')
138 pubkey = strchr(pubkey, '\n');
143 p = strchr(pubkey, '=');
147 bp = buf = solv_malloc(l * 3 / 4 + 4);
151 pubkey = r64dec1(pubkey, &v, &eof);
161 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
164 if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
169 if (v != crc24(buf, bp - buf))
174 while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
176 if (strncmp(pubkey, "-----END PGP PUBLIC KEY BLOCK-----", 34) != 0)
186 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
189 unsigned char keyid[8];
190 unsigned int kcr = 0, maxex = 0;
192 unsigned char *pubkey = 0;
193 unsigned char *userid = 0;
198 for (; pl; p += l, pl -= l)
202 if (!(x & 128) || pl <= 0)
207 tag = (x & 0x3c) >> 2;
229 else if (x >= 192 && x < 224)
233 l = ((x - 192) << 8) + *p++ + 192;
238 /* sanity: p[0] must be zero */
239 if (pl <= 4 || p[0] != 0)
241 l = p[1] << 16 | p[2] << 8 | p[3];
253 pubkey = solv_realloc(pubkey, l);
255 memcpy(pubkey, p, l);
259 if (p[0] == 3 && l >= 10)
263 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
267 ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
272 if (p[7] == 1) /* RSA */
275 unsigned char fp[16];
279 ql = ((p[8] << 8 | p[9]) + 7) / 8; /* length of public modulus */
280 if (ql >= 8 && 10 + ql + 2 <= l)
282 memcpy(keyid, p + 10 + ql - 8, 8); /* keyid is last 64 bits of public modulus */
284 ql2 = ((q[0] << 8 | q[1]) + 7) / 8; /* length of encryption exponent */
285 if (10 + ql + 2 + ql2 <= l)
287 /* fingerprint is the md5 over the two MPI bodies */
288 h = solv_chksum_create(REPOKEY_TYPE_MD5);
289 solv_chksum_add(h, p + 10, ql);
290 solv_chksum_add(h, q + 2, ql2);
291 solv_chksum_free(h, fp);
292 for (i = 0; i < 16; i++)
293 sprintf(fpx + i * 2, "%02x", fp[i]);
294 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
299 else if (p[0] == 4 && l >= 6)
303 unsigned char hdr[3];
304 unsigned char fp[20];
307 kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
311 /* fingerprint is the sha1 over the packet */
312 h = solv_chksum_create(REPOKEY_TYPE_SHA1);
313 solv_chksum_add(h, hdr, 3);
314 solv_chksum_add(h, p, l);
315 solv_chksum_free(h, fp);
316 for (i = 0; i < 20; i++)
317 sprintf(fpx + i * 2, "%02x", fp[i]);
318 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
319 memcpy(keyid, fp + 12, 8); /* keyid is last 64 bits of fingerprint */
324 if (p[0] == 3 && p[1] == 5)
329 /* printf("V3 signature packet\n"); */
332 if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
334 if (!memcmp(keyid, p + 6, 8))
336 /* printf("SELF SIG\n"); */
340 /* printf("OTHER SIG\n"); */
344 htype = REPOKEY_TYPE_MD5;
346 htype = REPOKEY_TYPE_SHA1;
348 htype = REPOKEY_TYPE_SHA256;
351 void *h = solv_chksum_create(htype);
352 unsigned char b[3], *cs;
357 solv_chksum_add(h, b, 3);
358 solv_chksum_add(h, pubkey, pubkeyl);
359 if (p[2] >= 0x10 && p[2] <= 0x13)
360 solv_chksum_add(h, userid, useridl);
361 solv_chksum_add(h, p + 2, 5);
362 cs = solv_chksum_get(h, 0);
363 solv_chksum_free(h, 0);
369 int j, ql, haveissuer;
373 unsigned int scr = 0;
375 unsigned char issuer[8];
377 /* printf("V4 signature packet\n"); */
380 if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
385 for (j = 0; q && j < 2; j++)
392 ql = q[0] << 8 | q[1];
402 /* decode sub-packet length */
409 if (ql < 4 || q[0] != 0)
414 sl = q[1] << 16 | q[2] << 8 | q[3];
425 sl = ((x - 192) << 8) + *q++ + 192;
434 /* printf("%d SIGSUB %d %d\n", j, x, sl); */
435 if (x == 16 && sl == 9 && !haveissuer)
437 memcpy(issuer, q + 1, 8);
441 if (x == 2 && j == 0)
442 scr = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
444 if (x == 9 && j == 0)
445 ex = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
457 htype = REPOKEY_TYPE_MD5;
459 htype = REPOKEY_TYPE_SHA1;
461 htype = REPOKEY_TYPE_SHA256;
462 if (htype && pubkeyl)
464 void *h = solv_chksum_create(htype);
465 unsigned char b[6], *cs;
471 solv_chksum_add(h, b, 3);
472 solv_chksum_add(h, pubkey, pubkeyl);
473 if (p[1] >= 0x10 && p[1] <= 0x13)
476 b[1] = useridl >> 24;
477 b[2] = useridl >> 16;
480 solv_chksum_add(h, b, 5);
481 solv_chksum_add(h, userid, useridl);
483 hl = 6 + (p[4] << 8 | p[5]);
484 solv_chksum_add(h, p, hl);
491 solv_chksum_add(h, b, 6);
492 cs = solv_chksum_get(h, 0);
493 solv_chksum_free(h, 0);
496 if (!memcmp(keyid, issuer, 8))
498 /* printf("SELF SIG cr %d ex %d\n", cr, ex); */
504 /* printf("OTHER SIG cr %d ex %d\n", cr, ex); */
512 userid = solv_realloc(userid, l);
514 memcpy(userid, p, l);
520 repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
527 /* this is private to rpm, but rpm lacks an interface to retrieve
528 * the values. Sigh. */
529 struct pgpDigParams_s {
531 const unsigned char * hash;
532 #ifndef HAVE_PGPDIGGETPARAMS
533 const char * params[4];
536 unsigned char version; /*!< version number. */
537 unsigned char time[4]; /*!< time that the key was created. */
538 unsigned char pubkey_algo; /*!< public key algorithm. */
539 unsigned char hash_algo;
540 unsigned char sigtype;
541 unsigned char hashlen;
542 unsigned char signhash16[2];
543 unsigned char signid[8];
547 #ifndef HAVE_PGPDIGGETPARAMS
549 struct pgpDigParams_s signature;
550 struct pgpDigParams_s pubkey;
555 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
557 Pool *pool = s->repo->pool;
563 char evrbuf[8 + 1 + 8 + 1];
564 struct pgpDigParams_s *digpubkey;
566 pkts = unarmor(pubkey, &pktsl);
569 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
570 parsekeydata(s, data, pkts, pktsl);
571 /* only rpm knows how to do the release calculation, we don't dare
572 * to recreate all the bugs */
576 dig = pgpDigNew(RPMVSF_DEFAULT, 0);
578 (void) pgpPrtPkts(pkts, pktsl, dig, 0);
580 #ifdef HAVE_PGPDIGGETPARAMS
581 digpubkey = pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY);
583 digpubkey = &dig->pubkey;
585 btime = digpubkey->time[0] << 24 | digpubkey->time[1] << 16 | digpubkey->time[2] << 8 | digpubkey->time[3];
586 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]);
588 repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
590 s->name = pool_str2id(pool, "gpg-pubkey", 1);
591 s->evr = pool_str2id(pool, evrbuf, 1);
593 for (i = 0; i < 8; i++)
594 sprintf(keyid + 2 * i, "%02x", digpubkey->signid[i]);
595 repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
596 if (digpubkey->userid)
597 setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, digpubkey->userid);
599 (void)pgpFreeDig(dig);
601 (void)pgpDigFree(dig);
603 solv_free((void *)pkts);
608 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
610 Pool *pool = repo->pool;
616 const char *rootdir = 0;
619 data = repo_add_repodata(repo, flags);
620 if (flags & REPO_USE_ROOTDIR)
621 rootdir = pool_get_rootdir(pool);
622 state = rpm_state_create(repo->pool, rootdir);
624 rpm_installedrpmdbids(state, "Name", "gpg-pubkey", &q);
625 for (i = 0; i < q.count; i++)
628 unsigned long long itime;
630 handle = rpm_byrpmdbid(state, q.elements[i]);
633 str = rpm_query(handle, SOLVABLE_DESCRIPTION);
636 s = pool_id2solvable(pool, repo_add_solvable(repo));
637 pubkey2solvable(s, data, str);
639 itime = rpm_query_num(handle, SOLVABLE_INSTALLTIME, 0);
641 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, itime);
643 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
644 repo->rpmdbid[s - pool->solvables - repo->start] = q.elements[i];
647 rpm_state_free(state);
648 if (!(flags & REPO_NO_INTERNALIZE))
649 repodata_internalize(data);
654 repo_add_pubkey(Repo *repo, const char *key, int flags)
656 Pool *pool = repo->pool;
663 data = repo_add_repodata(repo, flags);
666 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
668 pool_error(pool, -1, "%s: %s", key, strerror(errno));
676 buf = solv_realloc(buf, bufl);
678 ll = fread(buf, 1, bufl - l, fp);
682 pool_error(pool, -1, "%s: %s", key, strerror(errno));
691 s = pool_id2solvable(pool, repo_add_solvable(repo));
692 if (!pubkey2solvable(s, data, buf))
694 repo_free_solvable(repo, s - pool->solvables, 1);
699 if (!(flags & REPO_NO_INTERNALIZE))
700 repodata_internalize(data);
701 return s - pool->solvables;