1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
/** \ingroup header
* \file lib/package.c
*/
#include "system.h"
#include <netinet/in.h>
#include <rpmio_internal.h>
#include <rpmlib.h>
#include "rpmts.h"
#include "misc.h" /* XXX stripTrailingChar() */
#include "legacy.h" /* XXX providePackageNVR() and compressFileList() */
#include "rpmlead.h"
#include "header_internal.h" /* XXX headerCheck */
#include "signature.h"
#include "debug.h"
#define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
/*@access pgpDig @*/
/*@access pgpDigParams @*/
/*@access rpmts @*/
/*@access Header @*/ /* XXX compared with NULL */
/*@access entryInfo @*/ /* XXX headerCheck */
/*@access indexEntry @*/ /* XXX headerCheck */
/*@access FD_t @*/ /* XXX stealing digests */
/*@unchecked@*/
static int _print_pkts = 0;
/*@unchecked@*/
static int nkeyids = 0;
/*@unchecked@*/ /*@only@*/ /*@null@*/
static int * keyids;
/*@unchecked@*/
static unsigned char header_magic[8] = {
0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
};
/**
* Alignment needs (and sizeof scalars types) for internal rpm data types.
*/
/*@observer@*/ /*@unchecked@*/
static int typeAlign[16] = {
1, /*!< RPM_NULL_TYPE */
1, /*!< RPM_CHAR_TYPE */
1, /*!< RPM_INT8_TYPE */
2, /*!< RPM_INT16_TYPE */
4, /*!< RPM_INT32_TYPE */
8, /*!< RPM_INT64_TYPE */
1, /*!< RPM_STRING_TYPE */
1, /*!< RPM_BIN_TYPE */
1, /*!< RPM_STRING_ARRAY_TYPE */
1, /*!< RPM_I18NSTRING_TYPE */
0,
0,
0,
0,
0,
0
};
/**
* Sanity check on no. of tags.
* This check imposes a limit of 65K tags, more than enough.
*/
#define hdrchkTags(_ntags) ((_ntags) & 0xffff0000)
/**
* Sanity check on type values.
*/
#define hdrchkType(_type) ((_type) < RPM_MIN_TYPE || (_type) > RPM_MAX_TYPE)
/**
* Sanity check on data size and/or offset.
* This check imposes a limit of 16 MB, more than enough.
*/
#define hdrchkData(_nbytes) ((_nbytes) & 0xff000000)
/**
* Sanity check on data alignment for data type.
*/
#define hdrchkAlign(_type, _off) ((_off) & (typeAlign[_type]-1))
/**
* Sanity check on range of data offset.
*/
#define hdrchkRange(_dl, _off) ((_off) < 0 || (_off) > (_dl))
void headerMergeLegacySigs(Header h, const Header sig)
{
HFD_t hfd = (HFD_t) headerFreeData;
HAE_t hae = (HAE_t) headerAddEntry;
HeaderIterator hi;
int_32 tag, type, count;
const void * ptr;
int xx;
for (hi = headerInitIterator(sig);
headerNextIterator(hi, &tag, &type, &ptr, &count);
ptr = hfd(ptr, type))
{
switch (tag) {
/* XXX Translate legacy signature tag values. */
case RPMSIGTAG_SIZE:
tag = RPMTAG_SIGSIZE;
/*@switchbreak@*/ break;
case RPMSIGTAG_LEMD5_1:
tag = RPMTAG_SIGLEMD5_1;
/*@switchbreak@*/ break;
case RPMSIGTAG_PGP:
tag = RPMTAG_SIGPGP;
/*@switchbreak@*/ break;
case RPMSIGTAG_LEMD5_2:
tag = RPMTAG_SIGLEMD5_2;
/*@switchbreak@*/ break;
case RPMSIGTAG_MD5:
tag = RPMTAG_SIGMD5;
/*@switchbreak@*/ break;
case RPMSIGTAG_GPG:
tag = RPMTAG_SIGGPG;
/*@switchbreak@*/ break;
case RPMSIGTAG_PGP5:
tag = RPMTAG_SIGPGP5;
/*@switchbreak@*/ break;
case RPMSIGTAG_PAYLOADSIZE:
tag = RPMTAG_ARCHIVESIZE;
/*@switchbreak@*/ break;
case RPMSIGTAG_SHA1:
case RPMSIGTAG_DSA:
case RPMSIGTAG_RSA:
default:
if (!(tag >= HEADER_SIGBASE && tag < HEADER_TAGBASE))
continue;
/*@switchbreak@*/ break;
}
if (ptr == NULL) continue; /* XXX can't happen */
if (!headerIsEntry(h, tag))
xx = hae(h, tag, type, ptr, count);
}
hi = headerFreeIterator(hi);
}
Header headerRegenSigHeader(const Header h, int noArchiveSize)
{
HFD_t hfd = (HFD_t) headerFreeData;
Header sig = rpmNewSignature();
HeaderIterator hi;
int_32 tag, stag, type, count;
const void * ptr;
int xx;
for (hi = headerInitIterator(h);
headerNextIterator(hi, &tag, &type, &ptr, &count);
ptr = hfd(ptr, type))
{
switch (tag) {
/* XXX Translate legacy signature tag values. */
case RPMTAG_SIGSIZE:
stag = RPMSIGTAG_SIZE;
/*@switchbreak@*/ break;
case RPMTAG_SIGLEMD5_1:
stag = RPMSIGTAG_LEMD5_1;
/*@switchbreak@*/ break;
case RPMTAG_SIGPGP:
stag = RPMSIGTAG_PGP;
/*@switchbreak@*/ break;
case RPMTAG_SIGLEMD5_2:
stag = RPMSIGTAG_LEMD5_2;
/*@switchbreak@*/ break;
case RPMTAG_SIGMD5:
stag = RPMSIGTAG_MD5;
/*@switchbreak@*/ break;
case RPMTAG_SIGGPG:
stag = RPMSIGTAG_GPG;
/*@switchbreak@*/ break;
case RPMTAG_SIGPGP5:
stag = RPMSIGTAG_PGP5;
/*@switchbreak@*/ break;
case RPMTAG_ARCHIVESIZE:
/* XXX rpm-4.1 and later has archive size in signature header. */
if (noArchiveSize)
continue;
stag = RPMSIGTAG_PAYLOADSIZE;
/*@switchbreak@*/ break;
case RPMTAG_SHA1HEADER:
case RPMTAG_DSAHEADER:
case RPMTAG_RSAHEADER:
default:
if (!(tag >= HEADER_SIGBASE && tag < HEADER_TAGBASE))
continue;
stag = tag;
/*@switchbreak@*/ break;
}
if (ptr == NULL) continue; /* XXX can't happen */
if (!headerIsEntry(sig, stag))
xx = headerAddEntry(sig, stag, type, ptr, count);
}
hi = headerFreeIterator(hi);
return sig;
}
/**
* Remember current key id.
* @param ts transaction set
* @return 0 if new keyid, otherwise 1
*/
static int rpmtsStashKeyid(rpmts ts)
/*@globals nkeyids, keyids @*/
/*@modifies nkeyids, keyids @*/
{
const void * sig = rpmtsSig(ts);
pgpDig dig = rpmtsDig(ts);
pgpDigParams sigp = rpmtsSignature(ts);
unsigned int keyid;
int i;
if (sig == NULL || dig == NULL || sigp == NULL)
return 0;
keyid = pgpGrab(sigp->signid+4, 4);
if (keyid == 0)
return 0;
if (keyids != NULL)
for (i = 0; i < nkeyids; i++) {
/*@-boundsread@*/
if (keyid == keyids[i])
return 1;
/*@=boundsread@*/
}
keyids = xrealloc(keyids, (nkeyids + 1) * sizeof(*keyids));
/*@-boundswrite@*/
keyids[nkeyids] = keyid;
/*@=boundswrite@*/
nkeyids++;
return 0;
}
/**
* Perform simple sanity and range checks on header tag(s).
* @param il no. of tags in header
* @param dl no. of bytes in header data.
* @param pe 1st element in tag array, big-endian
* @param info failing (or last) tag element, host-endian
* @param negate negative offset expected?
* @return -1 on success, otherwise failing tag element index
*/
static int headerVerifyInfo(int il, int dl, entryInfo pe, entryInfo info,
int negate)
/*@modifies info @*/
{
int i;
/*@-boundsread@*/
for (i = 0; i < il; i++) {
info->tag = ntohl(pe[i].tag);
info->type = ntohl(pe[i].type);
info->offset = ntohl(pe[i].offset);
if (negate)
info->offset = -info->offset;
info->count = ntohl(pe[i].count);
if (hdrchkType(info->type) || hdrchkAlign(info->type, info->offset) ||
hdrchkRange(dl, info->offset) || hdrchkData(info->count))
return i;
}
/*@=boundsread@*/
return -1;
}
/**
* Check header consistency, performing headerGetEntry() the hard way.
*
* Sanity checks on the header are performed while looking for a
* header-only digest or signature to verify the blob. If found,
* the digest or signature is verified.
*
* @param ts transaction set
* @param uh unloaded header blob
* @param uc no. of bytes in blob (or 0 to disable)
* @retval *msg signature verification msg
* @return RPMRC_OK/RPMRC_NOTFOUND/RPMRC_FAIL
*/
rpmRC headerCheck(rpmts ts, const void * uh, size_t uc, const char ** msg)
{
pgpDig dig;
unsigned char buf[8*BUFSIZ];
int_32 * ei = (int_32 *) uh;
/*@-boundsread@*/
int_32 il = ntohl(ei[0]);
int_32 dl = ntohl(ei[1]);
/*@-castexpose@*/
entryInfo pe = (entryInfo) &ei[2];
/*@=castexpose@*/
/*@=boundsread@*/
int_32 ildl[2];
int_32 pvlen = sizeof(ildl) + (il * sizeof(*pe)) + dl;
unsigned char * dataStart = (char *) (pe + il);
indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
const void * sig = NULL;
const char * b;
int vsflags = rpmtsVerifySigFlags(ts);
int siglen = 0;
int blen;
size_t nb;
int_32 ril = 0;
unsigned char * end = NULL;
rpmRC rc = RPMRC_FAIL; /* assume failure */
int xx;
int i;
/* Is the blob the right size? */
if (uc > 0 && pvlen != uc)
goto exit;
/* Check (and convert) the 1st tag element. */
if (headerVerifyInfo(1, dl, pe, &entry->info, 0) != -1)
goto exit;
/* Is there an immutable header region tag? */
/*@-sizeoftype@*/
if (!(entry->info.tag == RPMTAG_HEADERIMMUTABLE
&& entry->info.type == RPM_BIN_TYPE
&& entry->info.count == REGION_TAG_COUNT))
{
rc = RPMRC_NOTFOUND;
goto exit;
}
/*@=sizeoftype@*/
/* Is there an immutable header region tag trailer? */
end = dataStart + entry->info.offset;
/*@-bounds@*/
(void) memcpy(info, end, entry->info.count);
/*@=bounds@*/
end += entry->info.count;
/*@-sizeoftype@*/
if (headerVerifyInfo(1, dl, info, &entry->info, 1) != -1
|| !(entry->info.tag == RPMTAG_HEADERIMMUTABLE
&& entry->info.type == RPM_BIN_TYPE
&& entry->info.count == REGION_TAG_COUNT))
goto exit;
/*@=sizeoftype@*/
/*@-boundswrite@*/
memset(info, 0, sizeof(*info));
/*@=boundswrite@*/
/* Is the no. of tags in the region less than the total no. of tags? */
ril = entry->info.offset/sizeof(*pe);
if (ril > il)
goto exit;
/* Find a header-only digest/signature tag. */
for (i = ril; i < il; i++) {
if (headerVerifyInfo(1, dl, pe+i, &entry->info, 0) != -1)
goto exit;
switch (entry->info.tag) {
case RPMTAG_SHA1HEADER:
if (vsflags & _RPMTS_VSF_NODIGESTS)
/*@switchbreak@*/ break;
blen = 0;
/*@-boundsread@*/
for (b = dataStart + entry->info.offset; *b != '\0'; b++) {
if (strchr("0123456789abcdefABCDEF", *b) == NULL)
/*@innerbreak@*/ break;
blen++;
}
if (entry->info.type != RPM_STRING_TYPE || *b != '\0' || blen != 40)
goto exit;
/*@=boundsread@*/
if (info->tag == 0) {
/*@-boundswrite@*/
*info = entry->info; /* structure assignment */
/*@=boundswrite@*/
siglen = blen + 1;
}
/*@switchbreak@*/ break;
#ifdef NOTYET
case RPMTAG_RSAHEADER:
#endif
case RPMTAG_DSAHEADER:
if (vsflags & _RPMTS_VSF_NOSIGNATURES)
/*@switchbreak@*/ break;
if (entry->info.type != RPM_BIN_TYPE)
goto exit;
/*@-boundswrite@*/
*info = entry->info; /* structure assignment */
/*@=boundswrite@*/
siglen = info->count;
/*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
}
rc = RPMRC_NOTFOUND;
exit:
/* Return determined RPMRC_OK/RPMRC_FAIL conditions. */
if (rc != RPMRC_NOTFOUND)
return rc;
/* If no header-only digest/signature, then do simple sanity check. */
if (info->tag == 0) {
verifyinfo_exit:
if (headerVerifyInfo(ril-1, dl, pe+1, &entry->info, 0) != -1)
rc = RPMRC_FAIL;
else
rc = RPMRC_OK;
return rc;
}
/* Verify header-only digest/signature. */
dig = rpmtsDig(ts);
if (dig == NULL)
goto verifyinfo_exit;
dig->nbytes = 0;
/*@-boundsread@*/
sig = memcpy(xmalloc(siglen), dataStart + info->offset, siglen);
/*@=boundsread@*/
(void) rpmtsSetSig(ts, info->tag, info->type, sig, info->count);
switch (info->tag) {
#ifdef NOTYET
case RPMTAG_RSAHEADER:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, info->count, dig, (_print_pkts & rpmIsDebug()));
/* XXX only V3 signatures for now. */
if (dig->signature.version != 3) {
rpmMessage(RPMMESS_WARNING,
_("only V3 signatures can be verified, skipping V%u signature"),
dig->signature.version);
rpmtsCleanDig(ts);
goto verifyinfo_exit;
}
ildl[0] = htonl(ril);
ildl[1] = (end - dataStart);
ildl[1] = htonl(ildl[1]);
dig->hdrmd5ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
b = (unsigned char *) header_magic;
nb = sizeof(header_magic);
(void) rpmDigestUpdate(dig->hdrmd5ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) ildl;
nb = sizeof(ildl);
(void) rpmDigestUpdate(dig->hdrmd5ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) pe;
nb = (htonl(ildl[0]) * sizeof(*pe));
(void) rpmDigestUpdate(dig->hdrmd5ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) dataStart;
nb = htonl(ildl[1]);
(void) rpmDigestUpdate(dig->hdrmd5ctx, b, nb);
dig->nbytes += nb;
break;
#endif
case RPMTAG_DSAHEADER:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, info->count, dig, (_print_pkts & rpmIsDebug()));
/* XXX only V3 signatures for now. */
if (dig->signature.version != 3) {
rpmMessage(RPMMESS_WARNING,
_("only V3 signatures can be verified, skipping V%u signature"),
dig->signature.version);
rpmtsCleanDig(ts);
goto verifyinfo_exit;
}
/*@fallthrough@*/
case RPMTAG_SHA1HEADER:
/*@-boundswrite@*/
ildl[0] = htonl(ril);
ildl[1] = (end - dataStart);
ildl[1] = htonl(ildl[1]);
/*@=boundswrite@*/
dig->hdrsha1ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
b = (unsigned char *) header_magic;
nb = sizeof(header_magic);
(void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) ildl;
nb = sizeof(ildl);
(void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) pe;
nb = (htonl(ildl[0]) * sizeof(*pe));
(void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
dig->nbytes += nb;
b = (unsigned char *) dataStart;
nb = htonl(ildl[1]);
(void) rpmDigestUpdate(dig->hdrsha1ctx, b, nb);
dig->nbytes += nb;
break;
default:
break;
}
/** @todo Implement disable/enable/warn/error/anal policy. */
/*@-boundswrite@*/
buf[0] = '\0';
/*@=boundswrite@*/
switch (rpmVerifySignature(ts, buf)) {
case RPMSIG_OK: /* Signature is OK. */
rc = RPMRC_OK;
break;
case RPMSIG_NOTTRUSTED: /* Signature is OK, but key is not trusted. */
case RPMSIG_NOKEY: /* Key is unavailable. */
rc = RPMRC_OK;
break;
case RPMSIG_UNKNOWN: /* Signature is unknown type. */
rc = RPMRC_OK;
break;
default:
case RPMSIG_BAD: /* Signature does not verify. */
rc = RPMRC_FAIL;
break;
}
/*@-boundswrite@*/
if (msg != NULL)
*msg = xstrdup(buf);
/*@=boundswrite@*/
rpmtsCleanDig(ts);
return rc;
}
int rpmReadPackageFile(rpmts ts, FD_t fd,
const char * fn, Header * hdrp)
{
pgpDig dig;
byte buf[8*BUFSIZ];
ssize_t count;
struct rpmlead * l = alloca(sizeof(*l));
Header sigh = NULL;
int_32 sigtag;
int_32 sigtype;
const void * sig;
int_32 siglen;
Header h = NULL;
int hmagic;
int vsflags;
rpmRC rc = RPMRC_FAIL; /* assume failure */
int xx;
int i;
{ struct stat st;
/*@-boundswrite@*/
memset(&st, 0, sizeof(st));
/*@=boundswrite@*/
(void) fstat(Fileno(fd), &st);
/* if fd points to a socket, pipe, etc, st.st_size is *always* zero */
if (S_ISREG(st.st_mode) && st.st_size < sizeof(*l))
goto exit;
}
memset(l, 0, sizeof(*l));
if (readLead(fd, l)) {
rc = RPMRC_NOTFOUND;
goto exit;
}
switch (l->major) {
case 1:
rpmError(RPMERR_NEWPACKAGE,
_("packaging version 1 is not supported by this version of RPM\n"));
goto exit;
/*@notreached@*/ break;
case 2:
case 3:
case 4:
break;
default:
rpmError(RPMERR_NEWPACKAGE, _("only packaging with major numbers <= 4 "
"is supported by this version of RPM\n"));
goto exit;
/*@notreached@*/ break;
}
/* Read the signature header. */
rc = rpmReadSignature(fd, &sigh, l->signature_type);
if (!(rc == RPMRC_OK || rc == RPMRC_BADSIZE)) {
rpmError(RPMERR_SIGGEN, _("%s: rpmReadSignature failed\n"), fn);
goto exit;
}
if (sigh == NULL) {
rpmError(RPMERR_SIGGEN, _("%s: No signature available\n"), fn);
rc = RPMRC_FAIL;
goto exit;
}
/* Figger the most effective available signature. */
sigtag = 0;
vsflags = rpmtsVerifySigFlags(ts);
if (vsflags & _RPMTS_VSF_VERIFY_LEGACY) {
if (sigtag == 0 && !(vsflags & _RPMTS_VSF_NOSIGNATURES)) {
if (headerIsEntry(sigh, RPMSIGTAG_DSA))
sigtag = RPMSIGTAG_DSA;
else if (headerIsEntry(sigh, RPMSIGTAG_RSA))
sigtag = RPMSIGTAG_RSA;
else if (headerIsEntry(sigh, RPMSIGTAG_GPG)) {
sigtag = RPMSIGTAG_GPG;
fdInitDigest(fd, PGPHASHALGO_SHA1, 0);
} else if (headerIsEntry(sigh, RPMSIGTAG_PGP)) {
sigtag = RPMSIGTAG_PGP;
fdInitDigest(fd, PGPHASHALGO_MD5, 0);
}
}
if (sigtag == 0 && !(vsflags & _RPMTS_VSF_NODIGESTS)) {
if (headerIsEntry(sigh, RPMSIGTAG_SHA1))
sigtag = RPMSIGTAG_SHA1;
else if (headerIsEntry(sigh, RPMSIGTAG_MD5)) {
sigtag = RPMSIGTAG_MD5;
fdInitDigest(fd, PGPHASHALGO_MD5, 0);
}
}
} else {
if (sigtag == 0 && !(vsflags & _RPMTS_VSF_NOSIGNATURES)) {
if (headerIsEntry(sigh, RPMSIGTAG_DSA))
sigtag = RPMSIGTAG_DSA;
else if (headerIsEntry(sigh, RPMSIGTAG_RSA))
sigtag = RPMSIGTAG_RSA;
}
if (sigtag == 0 && !(vsflags & _RPMTS_VSF_NODIGESTS)) {
if (headerIsEntry(sigh, RPMSIGTAG_SHA1))
sigtag = RPMSIGTAG_SHA1;
}
}
/* Read the metadata, computing digest(s) on the fly. */
hmagic = ((l->major >= 3) ? HEADER_MAGIC_YES : HEADER_MAGIC_NO);
h = headerRead(fd, hmagic);
if (h == NULL) {
rpmError(RPMERR_FREAD, _("%s: headerRead failed\n"), fn);
rc = RPMRC_FAIL;
goto exit;
}
/* Any signatures to check? */
if (sigtag == 0) {
rc = RPMRC_OK;
goto exit;
}
dig = rpmtsDig(ts);
if (dig == NULL) {
rc = RPMRC_FAIL;
goto exit;
}
dig->nbytes = 0;
/* Retrieve the tag parameters from the signature header. */
sig = NULL;
xx = headerGetEntry(sigh, sigtag, &sigtype, (void **) &sig, &siglen);
if (sig == NULL) {
rc = RPMRC_FAIL;
goto exit;
}
(void) rpmtsSetSig(ts, sigtag, sigtype, sig, siglen);
switch (sigtag) {
case RPMSIGTAG_RSA:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, siglen, dig, (_print_pkts & rpmIsDebug()));
/* XXX only V3 signatures for now. */
if (dig->signature.version != 3) {
rpmMessage(RPMMESS_WARNING,
_("only V3 signatures can be verified, skipping V%u signature"),
dig->signature.version);
rc = RPMRC_OK;
goto exit;
}
{ void * uh = NULL;
int_32 uht;
int_32 uhc;
if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc))
break;
dig->md5ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
(void) rpmDigestUpdate(dig->md5ctx, header_magic, sizeof(header_magic));
dig->nbytes += sizeof(header_magic);
(void) rpmDigestUpdate(dig->md5ctx, uh, uhc);
dig->nbytes += uhc;
uh = headerFreeData(uh, uht);
} break;
case RPMSIGTAG_DSA:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, siglen, dig, (_print_pkts & rpmIsDebug()));
/* XXX only V3 signatures for now. */
if (dig->signature.version != 3) {
rpmMessage(RPMMESS_WARNING,
_("only V3 signatures can be verified, skipping V%u signature"),
dig->signature.version);
rc = RPMRC_OK;
goto exit;
}
/*@fallthrough@*/
case RPMSIGTAG_SHA1:
{ void * uh = NULL;
int_32 uht;
int_32 uhc;
if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc))
break;
dig->hdrsha1ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
(void) rpmDigestUpdate(dig->hdrsha1ctx, header_magic, sizeof(header_magic));
dig->nbytes += sizeof(header_magic);
(void) rpmDigestUpdate(dig->hdrsha1ctx, uh, uhc);
dig->nbytes += uhc;
uh = headerFreeData(uh, uht);
} break;
case RPMSIGTAG_GPG:
case RPMSIGTAG_PGP5: /* XXX legacy */
case RPMSIGTAG_PGP:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, siglen, dig,
(_print_pkts & rpmIsDebug()));
/* XXX only V3 signatures for now. */
if (dig->signature.version != 3) {
rpmMessage(RPMMESS_WARNING,
_("only V3 signatures can be verified, skipping V%u signature"),
dig->signature.version);
rc = RPMRC_OK;
goto exit;
}
/*@fallthrough@*/
case RPMSIGTAG_MD5:
/* Legacy signatures need the compressed payload in the digest too. */
dig->nbytes += headerSizeof(h, hmagic);
while ((count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
dig->nbytes += count;
if (count < 0) {
rpmError(RPMERR_FREAD, _("%s: Fread failed: %s\n"),
fn, Fstrerror(fd));
rc = RPMRC_FAIL;
goto exit;
}
dig->nbytes += count;
/* XXX Steal the digest-in-progress from the file handle. */
for (i = fd->ndigests - 1; i >= 0; i--) {
FDDIGEST_t fddig = fd->digests + i;
if (fddig->hashctx == NULL)
continue;
if (fddig->hashalgo == PGPHASHALGO_MD5) {
dig->md5ctx = fddig->hashctx;
fddig->hashctx = NULL;
continue;
}
if (fddig->hashalgo == PGPHASHALGO_SHA1) {
dig->sha1ctx = fddig->hashctx;
fddig->hashctx = NULL;
continue;
}
}
break;
}
/** @todo Implement disable/enable/warn/error/anal policy. */
/*@-boundswrite@*/
buf[0] = '\0';
/*@=boundswrite@*/
switch (rpmVerifySignature(ts, buf)) {
case RPMSIG_OK: /* Signature is OK. */
rpmMessage(RPMMESS_DEBUG, "%s: %s", fn, buf);
rc = RPMRC_OK;
break;
case RPMSIG_NOTTRUSTED: /* Signature is OK, but key is not trusted. */
case RPMSIG_NOKEY: /* Key is unavailable. */
/* XXX Print NOKEY/NOTTRUSTED warning only once. */
{ int lvl = (rpmtsStashKeyid(ts) ? RPMMESS_DEBUG : RPMMESS_WARNING);
rpmMessage(lvl, "%s: %s", fn, buf);
rc = RPMRC_OK;
} break;
case RPMSIG_UNKNOWN: /* Signature is unknown type. */
rpmMessage(RPMMESS_WARNING, "%s: %s", fn, buf);
rc = RPMRC_OK;
break;
default:
case RPMSIG_BAD: /* Signature does not verify. */
rpmMessage(RPMMESS_ERROR, "%s: %s", fn, buf);
rc = RPMRC_FAIL;
break;
}
exit:
if (rc == RPMRC_OK && hdrp != NULL) {
/* Convert legacy headers on the fly ... */
legacyRetrofit(h, l);
/* Append (and remap) signature tags to the metadata. */
headerMergeLegacySigs(h, sigh);
/* Bump reference count for return. */
/*@-boundswrite@*/
*hdrp = headerLink(h);
/*@=boundswrite@*/
}
h = headerFree(h);
rpmtsCleanDig(ts);
sigh = rpmFreeSignature(sigh);
return rc;
}
|