refactor a bit
[platform/upstream/libsolv.git] / ext / repo_pubkey.c
1 /*
2  * Copyright (c) 2007-2013, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_pubkey
10  *
11  * support for pubkey reading
12  *
13  */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <stdint.h>
25 #include <errno.h>
26
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
29 #ifndef RPM5
30 #include <rpm/header.h>
31 #endif
32 #include <rpm/rpmdb.h>
33
34 #include "pool.h"
35 #include "repo.h"
36 #include "hash.h"
37 #include "util.h"
38 #include "queue.h"
39 #include "chksum.h"
40 #include "repo_rpmdb.h"
41 #ifdef ENABLE_PGPVRFY
42 #include "solv_pgpvrfy.h"
43 #endif
44
45 static void 
46 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
47 {
48   if (str[solv_validutf8(str)])
49     {    
50       char *ustr = solv_latin1toutf8(str);      /* not utf8, assume latin1 */
51       repodata_set_str(repodata, handle, tag, ustr);
52       solv_free(ustr);
53     }    
54   else 
55     repodata_set_str(repodata, handle, tag, str);
56 }
57
58 static char *
59 r64dec1(char *p, unsigned int *vp, int *eofp)
60 {
61   int i, x;
62   unsigned int v = 0;
63
64   for (i = 0; i < 4; )
65     {
66       x = *p++;
67       if (!x)
68         return 0;
69       if (x >= 'A' && x <= 'Z')
70         x -= 'A';
71       else if (x >= 'a' && x <= 'z')
72         x -= 'a' - 26;
73       else if (x >= '0' && x <= '9')
74         x -= '0' - 52;
75       else if (x == '+')
76         x = 62;
77       else if (x == '/')
78         x = 63;
79       else if (x == '=')
80         {
81           x = 0;
82           if (i == 0)
83             {
84               *eofp = 3;
85               *vp = 0;
86               return p - 1;
87             }
88           *eofp += 1;
89         }
90       else
91         continue;
92       v = v << 6 | x;
93       i++;
94     }
95   *vp = v;
96   return p;
97 }
98
99 static unsigned int
100 crc24(unsigned char *p, int len)
101 {
102   unsigned int crc = 0xb704ceL;
103   int i;
104
105   while (len--)
106     {
107       crc ^= (*p++) << 16;
108       for (i = 0; i < 8; i++)
109         if ((crc <<= 1) & 0x1000000)
110           crc ^= 0x1864cfbL;
111     }
112   return crc & 0xffffffL;
113 }
114
115 static unsigned char *
116 unarmor(char *pubkey, int *pktlp, char *startstr, char *endstr)
117 {
118   char *p;
119   int l, eof;
120   unsigned char *buf, *bp;
121   unsigned int v;
122
123   *pktlp = 0;
124   l = strlen(startstr);
125   while (strncmp(pubkey, startstr, l) != 0)
126     {
127       pubkey = strchr(pubkey, '\n');
128       if (!pubkey)
129         return 0;
130       pubkey++;
131     }
132   pubkey = strchr(pubkey, '\n');
133   if (!pubkey++)
134     return 0;
135   /* skip header lines */
136   for (;;)
137     {
138       while (*pubkey == ' ' || *pubkey == '\t')
139         pubkey++;
140       if (*pubkey == '\n')
141         break;
142       pubkey = strchr(pubkey, '\n');
143       if (!pubkey++)
144         return 0;
145     }
146   pubkey++;
147   p = strchr(pubkey, '=');
148   if (!p)
149     return 0;
150   l = p - pubkey;
151   bp = buf = solv_malloc(l * 3 / 4 + 4);
152   eof = 0;
153   while (!eof)
154     {
155       pubkey = r64dec1(pubkey, &v, &eof);
156       if (!pubkey)
157         {
158           solv_free(buf);
159           return 0;
160         }
161       *bp++ = v >> 16;
162       *bp++ = v >> 8;
163       *bp++ = v;
164     }
165   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
166     pubkey++;
167   bp -= eof;
168   if (*pubkey != '=' || (pubkey = r64dec1(pubkey + 1, &v, &eof)) == 0)
169     {
170       solv_free(buf);
171       return 0;
172     }
173   if (v != crc24(buf, bp - buf))
174     {
175       solv_free(buf);
176       return 0;
177     }
178   while (*pubkey == ' ' || *pubkey == '\t' || *pubkey == '\n' || *pubkey == '\r')
179     pubkey++;
180   if (strncmp(pubkey, endstr, strlen(endstr)) != 0)
181     {
182       solv_free(buf);
183       return 0;
184     }
185   *pktlp = bp - buf;
186   return buf;
187 }
188
189 struct gpgsig {
190   int type;
191   unsigned char issuer[8];
192   int haveissuer;
193   unsigned int created;
194   unsigned int expires;
195   unsigned int keyexpires;
196   unsigned char *sigdata;
197   int sigdatal;
198 };
199
200 static Id
201 pgphashalgo2type(int algo)
202 {
203   if (algo == 1)
204     return REPOKEY_TYPE_MD5;
205   if (algo == 2)
206     return REPOKEY_TYPE_SHA1;
207   if (algo == 8)
208     return REPOKEY_TYPE_SHA256;
209   return 0;
210 }
211
212 static void
213 parsesigpacket(struct gpgsig *sig, unsigned char *p, int l, unsigned char *pubkey, int pubkeyl, unsigned char *userid, int useridl)
214 {
215   sig->type = -1;
216   if (p[0] == 3)
217     {
218       Id htype = 0;
219       /* printf("V3 signature packet\n"); */
220       if (l <= 19 || p[1] != 5)
221         return;
222       if (p[2] != 0x10 && p[2] != 0x11 && p[2] != 0x12 && p[2] != 0x13 && p[2] != 0x1f)
223         return;
224       sig->type = p[2];
225       sig->haveissuer = 1;
226       memcpy(sig->issuer, p + 7, 8);
227       sig->created = p[3] << 24 | p[4] << 16 | p[5] << 8 | p[6];
228       htype = pgphashalgo2type(p[16]);
229       if (htype && pubkey)
230         {
231           void *h = solv_chksum_create(htype);
232           const unsigned char *cs;
233           unsigned char b[3];
234           int csl;
235
236           if ((p[2] >= 0x10 && p[2] <= 0x13) || p[2] == 0x1f || p[2] == 0x18 || p[2] == 0x20 || p[2] == 0x28)
237             {
238               b[0] = 0x99;
239               b[1] = pubkeyl >> 8;
240               b[2] = pubkeyl;
241               solv_chksum_add(h, b, 3);
242               solv_chksum_add(h, pubkey, pubkeyl);
243             }
244           if (p[2] >= 0x10 && p[2] <= 0x13)
245             solv_chksum_add(h, userid, useridl);
246           solv_chksum_add(h, p + 2, 5);
247           cs = solv_chksum_get(h, &csl);
248           if (cs[0] == p[17] && cs[1] == p[18])
249             {
250               sig->sigdata = solv_malloc(2 + csl + l - 19);
251               sig->sigdatal = 2 + csl + l - 19;
252               sig->sigdata[0] = p[15];
253               sig->sigdata[1] = p[16];
254               memcpy(sig->sigdata + 2, cs, csl);
255               memcpy(sig->sigdata + 2 + csl, p + 19, l - 19);
256             }
257           solv_chksum_free(h, 0);
258         }
259     }
260   else if (p[0] == 4)
261     {
262       int j, ql, x;
263       unsigned char *q;
264       Id htype = 0;
265
266       /* printf("V4 signature packet\n"); */
267       if (l < 6)
268         return;
269       if (p[1] != 0x10 && p[1] != 0x11 && p[1] != 0x12 && p[1] != 0x13 && p[1] != 0x1f)
270         return;
271       sig->type = p[1];
272       q = p + 4;
273       sig->keyexpires = -1;
274       for (j = 0; q && j < 2; j++)
275         {
276           if (q + 2 > p + l)
277             {
278               q = 0;
279               break;
280             }
281           ql = q[0] << 8 | q[1];
282           q += 2;
283           if (q + ql > p + l)
284             {
285               q = 0;
286               break;
287             }
288           while (ql)
289             {
290               int sl;
291               /* decode sub-packet length */
292               x = *q++;
293               ql--;
294               if (x < 192)
295                 sl = x;
296               else if (x == 255)
297                 {
298                   if (ql < 4 || q[0] != 0)
299                     {
300                       q = 0;
301                       break;
302                     }
303                   sl = q[1] << 16 | q[2] << 8 | q[3];
304                   q += 4;
305                   ql -= 4;
306                 }
307               else
308                 {
309                   if (ql < 1)
310                     {
311                       q = 0;
312                       break;
313                     }
314                   sl = ((x - 192) << 8) + *q++ + 192;
315                   ql--;
316                 }
317               if (ql < sl)
318                 {
319                   q = 0;
320                   break;
321                 }
322               x = q[0] & 127;
323               /* printf("%d SIGSUB %d %d\n", j, x, sl); */
324               if (x == 16 && sl == 9 && !sig->haveissuer)
325                 {
326                   sig->haveissuer = 1;
327                   memcpy(sig->issuer, q + 1, 8);
328                 }
329               if (x == 2 && j == 0)
330                 sig->created = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
331               if (x == 3 && j == 0)
332                 sig->expires = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
333               if (x == 9 && j == 0)
334                 sig->keyexpires = q[1] << 24 | q[2] << 16 | q[3] << 8 | q[4];
335               q += sl;
336               ql -= sl;
337             }
338         }
339       htype = pgphashalgo2type(p[3]);
340       if (sig->haveissuer && htype && pubkey && q && q - p + 2 < l)
341         {
342           void *h = solv_chksum_create(htype);
343           unsigned char b[6];
344           const unsigned char *cs;
345           unsigned int hl;
346           int csl, ml = l - (q - p + 2);
347
348           if ((p[1] >= 0x10 && p[1] <= 0x13) || p[1] == 0x1f || p[1] == 0x18 || p[1] == 0x20 || p[1] == 0x28)
349             {
350               b[0] = 0x99;
351               b[1] = pubkeyl >> 8;
352               b[2] = pubkeyl;
353               solv_chksum_add(h, b, 3);
354               solv_chksum_add(h, pubkey, pubkeyl);
355             }
356           if (p[1] >= 0x10 && p[1] <= 0x13)
357             {
358               b[0] = 0xb4;
359               b[1] = useridl >> 24;
360               b[2] = useridl >> 16;
361               b[3] = useridl >> 8;
362               b[4] = useridl;
363               solv_chksum_add(h, b, 5);
364               solv_chksum_add(h, userid, useridl);
365             }
366           hl = 6 + (p[4] << 8 | p[5]);
367           solv_chksum_add(h, p, hl);
368           b[0] = 4;
369           b[1] = 0xff;
370           b[2] = hl >> 24;
371           b[3] = hl >> 16;
372           b[4] = hl >> 8;
373           b[5] = hl;
374           solv_chksum_add(h, b, 6);
375           cs = solv_chksum_get(h, &csl);
376           if (cs[0] == q[0] && cs[1] == q[1])
377             {
378               sig->sigdata = solv_malloc(2 + csl + ml);
379               sig->sigdatal = 2 + csl + ml;
380               sig->sigdata[0] = p[2];
381               sig->sigdata[1] = p[3];
382               memcpy(sig->sigdata + 2, cs, csl);
383               memcpy(sig->sigdata + 2 + csl, q + 2, ml);
384             }
385           solv_chksum_free(h, 0);
386         }
387     }
388 }
389
390 static void
391 parsekeydata(Solvable *s, Repodata *data, unsigned char *p, int pl)
392 {
393   int x, tag, l;
394   unsigned char keyid[8];
395   unsigned int kcr = 0, maxex = 0, maxsigcr = 0;
396   unsigned char *pubkey = 0;
397   int pubkeyl = 0;
398   unsigned char *userid = 0;
399   int useridl = 0;
400   unsigned char *pubdata = 0;
401   int pubdatal = 0;
402
403   for (; pl; p += l, pl -= l)
404     {
405       x = *p++;
406       pl--;
407       if (!(x & 128) || pl <= 0)
408         return;
409       if ((x & 64) == 0)
410         {
411           /* old format */
412           tag = (x & 0x3c) >> 2;
413           x &= 3;
414           if (x == 3)
415             return;
416           l = 1 << x;
417           if (pl < l)
418             return;
419           x = 0;
420           while (l--)
421             {
422               x = x << 8 | *p++;
423               pl--;
424             }
425           l = x;
426         }
427       else
428         {
429           tag = (x & 0x3f);
430           x = *p++;
431           pl--;
432           if (x < 192)
433             l = x;
434           else if (x >= 192 && x < 224)
435             {
436               if (pl <= 0)
437                 return;
438               l = ((x - 192) << 8) + *p++ + 192;
439               pl--;
440             }
441           else if (x == 255)
442             {
443               /* sanity: p[0] must be zero */
444               if (pl <= 4 || p[0] != 0)
445                 return;
446               l = p[1] << 16 | p[2] << 8 | p[3];
447               p += 4;
448               pl -= 4;
449             }
450           else
451             return;
452         }
453       if (pl < l)
454         return;
455       if (tag == 6)
456         {
457           if (pubkey)
458             break;      /* one key at a time, please */
459           pubkey = solv_malloc(l);
460           if (l)
461             memcpy(pubkey, p, l);
462           pubkeyl = l;
463           if (p[0] == 3 && l >= 10)
464             {
465               unsigned int ex;
466               void *h;
467               maxsigcr = kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
468               ex = 0;
469               if (p[5] || p[6])
470                 {
471                   ex = kcr + 24*3600 * (p[5] << 8 | p[6]);
472                   if (ex > maxex)
473                     maxex = ex;
474                 }
475               memset(keyid, 0, 8);
476               if (p[7] == 1)    /* RSA */
477                 {
478                   int ql, ql2;
479                   unsigned char fp[16];
480                   char fpx[32 + 1];
481                   unsigned char *q;
482
483                   ql = ((p[8] << 8 | p[9]) + 7) / 8;            /* length of public modulus */
484                   if (ql >= 8 && 10 + ql + 2 <= l)
485                     {
486                       memcpy(keyid, p + 10 + ql - 8, 8);        /* keyid is last 64 bits of public modulus */
487                       q = p + 10 + ql;
488                       ql2 = ((q[0] << 8 | q[1]) + 7) / 8;       /* length of encryption exponent */
489                       if (10 + ql + 2 + ql2 <= l)
490                         {
491                           /* fingerprint is the md5 over the two MPI bodies */
492                           h = solv_chksum_create(REPOKEY_TYPE_MD5);
493                           solv_chksum_add(h, p + 10, ql);
494                           solv_chksum_add(h, q + 2, ql2);
495                           solv_chksum_free(h, fp);
496                           solv_bin2hex(fp, 16, fpx);
497                           repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
498                         }
499                     }
500                   pubdata = p + 7;
501                   pubdatal = l - 7;
502                 }
503             }
504           else if (p[0] == 4 && l >= 6)
505             {
506               void *h;
507               unsigned char hdr[3];
508               unsigned char fp[20];
509               char fpx[40 + 1];
510
511               maxsigcr = kcr = p[1] << 24 | p[2] << 16 | p[3] << 8 | p[4];
512               hdr[0] = 0x99;
513               hdr[1] = l >> 8;
514               hdr[2] = l;
515               /* fingerprint is the sha1 over the packet */
516               h = solv_chksum_create(REPOKEY_TYPE_SHA1);
517               solv_chksum_add(h, hdr, 3);
518               solv_chksum_add(h, p, l);
519               solv_chksum_free(h, fp);
520               solv_bin2hex(fp, 20, fpx);
521               repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_FINGERPRINT, fpx);
522               memcpy(keyid, fp + 12, 8);        /* keyid is last 64 bits of fingerprint */
523               pubdata = p + 5;
524               pubdatal = l - 5;
525             }
526         }
527       if (tag == 2)
528         {
529           struct gpgsig sig;
530           if (!pubdata)
531             continue;
532           memset(&sig, 0, sizeof(sig));
533           parsesigpacket(&sig, p, l, pubkey, pubkeyl, userid, useridl);
534           if (!sig.haveissuer || !((sig.type >= 0x10 && sig.type <= 0x13) || sig.type == 0x1f))
535             {
536               solv_free(sig.sigdata);
537               continue;
538             }
539           if (!memcmp(keyid, sig.issuer, 8))
540             {
541 #ifdef ENABLE_PGPVRFY
542               /* found self sig, verify */
543               if (solv_pgpvrfy(pubdata, pubdatal, sig.sigdata, sig.sigdatal))
544 #endif
545                 {
546                   if (sig.keyexpires && maxex != -1)
547                     {
548                       if (sig.keyexpires == -1)
549                         maxex = -1;
550                       else if (sig.keyexpires + kcr > maxex)
551                         maxex = sig.keyexpires + kcr;
552                     }
553                   if (sig.created > maxsigcr)
554                     maxsigcr = sig.created;
555                 }
556             }
557           else
558             {
559               char issuerstr[17];
560               Id shandle = repodata_new_handle(data);
561               solv_bin2hex(sig.issuer, 8, issuerstr);
562               repodata_set_str(data, shandle, SIGNATURE_ISSUER, issuerstr);
563               if (sig.created)
564                 repodata_set_num(data, shandle, SIGNATURE_TIME, sig.created);
565               if (sig.expires)
566                 repodata_set_num(data, shandle, SIGNATURE_EXPIRES, sig.created + sig.expires);
567               if (sig.sigdata)
568                 repodata_set_binary(data, shandle, SIGNATURE_DATA, sig.sigdata, sig.sigdatal);
569               repodata_add_flexarray(data, s - s->repo->pool->solvables, PUBKEY_SIGNATURES, shandle);
570             }
571           solv_free(sig.sigdata);
572         }
573       if (tag == 13)
574         {
575           userid = solv_realloc(userid, l);
576           if (l)
577             memcpy(userid, p, l);
578           useridl = l;
579         }
580     }
581   if (kcr)
582     repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, kcr);
583   if (maxex && maxex != -1)
584     repodata_set_num(data, s - s->repo->pool->solvables, PUBKEY_EXPIRES, maxex);
585   s->name = pool_str2id(s->repo->pool, "gpg-pubkey", 1);
586   s->evr = 1;
587   s->arch = 1;
588   if (userid && useridl)
589     {
590       char *useridstr = solv_malloc(useridl + 1);
591       memcpy(useridstr, userid, useridl);
592       useridstr[useridl] = 0;
593       setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, useridstr);
594       free(useridstr);
595     }
596   if (pubdata)
597     {
598       char keyidstr[17];
599       solv_bin2hex(keyid, 8, keyidstr);
600       repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyidstr);
601     }
602   if (pubdata)
603     {
604       /* build rpm-style evr */
605       char evr[8 + 1 + 8 + 1];
606       solv_bin2hex(keyid + 4, 4, evr);
607       sprintf(evr + 8, "-%08x", maxsigcr);
608       s->evr = pool_str2id(s->repo->pool, evr, 1);
609       /* set data blob */
610       repodata_set_binary(data, s - s->repo->pool->solvables, PUBKEY_DATA, pubdata, pubdatal);
611     }
612   solv_free(pubkey);
613   solv_free(userid);
614 }
615
616
617 #ifdef ENABLE_RPMDB
618
619 /* this is private to rpm, but rpm lacks an interface to retrieve
620  * the values. Sigh. */
621 struct pgpDigParams_s {
622     const char * userid;
623     const unsigned char * hash;
624 #ifndef HAVE_PGPDIGGETPARAMS
625     const char * params[4];
626 #endif
627     unsigned char tag;
628     unsigned char version;               /*!< version number. */
629     unsigned char time[4];               /*!< time that the key was created. */
630     unsigned char pubkey_algo;           /*!< public key algorithm. */
631     unsigned char hash_algo;
632     unsigned char sigtype;
633     unsigned char hashlen;
634     unsigned char signhash16[2];
635     unsigned char signid[8];
636     unsigned char saved;
637 };
638
639 #ifndef HAVE_PGPDIGGETPARAMS
640 struct pgpDig_s {
641     struct pgpDigParams_s signature;
642     struct pgpDigParams_s pubkey;
643 };
644 #endif
645
646
647 /* only rpm knows how to do the release calculation, we don't dare
648  * to recreate all the bugs in libsolv */
649 static void
650 parsekeydata_rpm(Solvable *s, Repodata *data, unsigned char *pkts, int pktsl)
651 {
652   Pool *pool = s->repo->pool;
653   struct pgpDigParams_s *digpubkey;
654   pgpDig dig = 0;
655   char keyid[16 + 1];
656   char evrbuf[8 + 1 + 8 + 1];
657   unsigned int btime;
658
659 #ifndef RPM5
660   dig = pgpNewDig();
661 #else
662   dig = pgpDigNew(RPMVSF_DEFAULT, 0);
663 #endif
664   (void) pgpPrtPkts(pkts, pktsl, dig, 0);
665 #ifdef HAVE_PGPDIGGETPARAMS
666   digpubkey = pgpDigGetParams(dig, PGPTAG_PUBLIC_KEY);
667 #else
668   digpubkey = &dig->pubkey;
669 #endif
670   if (digpubkey)
671     {
672       btime = digpubkey->time[0] << 24 | digpubkey->time[1] << 16 | digpubkey->time[2] << 8 | digpubkey->time[3];
673       solv_bin2hex(digpubkey->signid, 8, keyid);
674       solv_bin2hex(digpubkey->signid + 4, 4, evrbuf);
675       evrbuf[8] = '-';
676       solv_bin2hex(digpubkey->time, 4, evrbuf + 9);
677       s->evr = pool_str2id(pool, evrbuf, 1);
678       repodata_set_str(data, s - s->repo->pool->solvables, PUBKEY_KEYID, keyid);
679       if (digpubkey->userid)
680         setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_SUMMARY, digpubkey->userid);
681       if (btime)
682         repodata_set_num(data, s - s->repo->pool->solvables, SOLVABLE_BUILDTIME, btime);
683     }
684 #ifndef RPM5
685   (void)pgpFreeDig(dig);
686 #else
687   (void)pgpDigFree(dig);
688 #endif
689 }
690
691 #endif  /* ENABLE_RPMDB */
692
693 static int
694 pubkey2solvable(Solvable *s, Repodata *data, char *pubkey)
695 {
696   unsigned char *pkts;
697   int pktsl;
698
699   pkts = unarmor(pubkey, &pktsl, "-----BEGIN PGP PUBLIC KEY BLOCK-----", "-----END PGP PUBLIC KEY BLOCK-----");
700   if (!pkts)
701     {
702       pool_error(s->repo->pool, 0, "unarmor failure");
703       return 0;
704     }
705   setutf8string(data, s - s->repo->pool->solvables, SOLVABLE_DESCRIPTION, pubkey);
706   parsekeydata(s, data, pkts, pktsl);
707 #ifdef ENABLE_RPMDB
708   parsekeydata_rpm(s, data, pkts, pktsl);
709 #endif
710   solv_free((void *)pkts);
711   return 1;
712 }
713
714 #ifdef ENABLE_RPMDB
715
716 int
717 repo_add_rpmdb_pubkeys(Repo *repo, int flags)
718 {
719   Pool *pool = repo->pool;
720   Queue q;
721   int i;
722   char *str;
723   Repodata *data;
724   Solvable *s;
725   const char *rootdir = 0;
726   void *state;
727
728   data = repo_add_repodata(repo, flags);
729   if (flags & REPO_USE_ROOTDIR)
730     rootdir = pool_get_rootdir(pool);
731   state = rpm_state_create(repo->pool, rootdir);
732   queue_init(&q);
733   rpm_installedrpmdbids(state, "Name", "gpg-pubkey", &q);
734   for (i = 0; i < q.count; i++)
735     {
736       void *handle;
737       unsigned long long itime;
738
739       handle = rpm_byrpmdbid(state, q.elements[i]);
740       if (!handle)
741         continue;
742       str = rpm_query(handle, SOLVABLE_DESCRIPTION);
743       if (!str)
744         continue;
745       s = pool_id2solvable(pool, repo_add_solvable(repo));
746       pubkey2solvable(s, data, str);
747       solv_free(str);
748       itime = rpm_query_num(handle, SOLVABLE_INSTALLTIME, 0);
749       if (itime)
750         repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLTIME, itime);
751       if (!repo->rpmdbid)
752         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
753       repo->rpmdbid[s - pool->solvables - repo->start] = q.elements[i];
754     }
755   queue_free(&q);
756   rpm_state_free(state);
757   if (!(flags & REPO_NO_INTERNALIZE))
758     repodata_internalize(data);
759   return 0;
760 }
761
762 #endif
763
764 Id
765 repo_add_pubkey(Repo *repo, const char *key, int flags)
766 {
767   Pool *pool = repo->pool;
768   Repodata *data;
769   Solvable *s;
770   char *buf;
771   int bufl, l, ll;
772   FILE *fp;
773
774   data = repo_add_repodata(repo, flags);
775   buf = 0;
776   bufl = 0;
777   if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, key) : key, "r")) == 0)
778     {
779       pool_error(pool, -1, "%s: %s", key, strerror(errno));
780       return 0;
781     }
782   for (l = 0; ;)
783     {
784       if (bufl - l < 4096)
785         {
786           bufl += 4096;
787           buf = solv_realloc(buf, bufl);
788         }
789       ll = fread(buf + l, 1, bufl - l, fp);
790       if (ll < 0)
791         {
792           fclose(fp);
793           pool_error(pool, -1, "%s: %s", key, strerror(errno));
794           return 0;
795         }
796       if (ll == 0)
797         break;
798       l += ll;
799     }
800   buf[l] = 0;
801   fclose(fp);
802   s = pool_id2solvable(pool, repo_add_solvable(repo));
803   if (!pubkey2solvable(s, data, buf))
804     {
805       repo_free_solvable(repo, s - pool->solvables, 1);
806       solv_free(buf);
807       return 0;
808     }
809   solv_free(buf);
810   if (!(flags & REPO_NO_INTERNALIZE))
811     repodata_internalize(data);
812   return s - pool->solvables;
813 }
814