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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
/** \ingroup header
* \file lib/package.c
*/
#include "system.h"
#include <netinet/in.h>
#include "rpmio/digest.h"
#include "rpmio/rpmio_internal.h" /* fd*Digest(), fd stats */
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include "lib/legacy.h" /* XXX legacyRetrofit() */
#include "lib/rpmlead.h"
#include <rpm/rpmlog.h>
#include "rpmdb/header_internal.h" /* XXX headerCheck */
#include "lib/signature.h"
#include "debug.h"
#define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
static int _print_pkts = 0;
static unsigned int nkeyids_max = 256;
static unsigned int nkeyids = 0;
static unsigned int nextkeyid = 0;
static unsigned int * keyids;
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.
*/
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 and/or count.
* 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 sigh)
{
HFD_t hfd = (HFD_t) headerFreeData;
HAE_t hae = (HAE_t) headerAddEntry;
HeaderIterator hi;
rpm_tagtype_t type;
rpm_tag_t tag;
rpm_count_t count;
rpm_data_t ptr;
int xx;
for (hi = headerInitIterator(sigh);
headerNextIterator(hi, &tag, &type, &ptr, &count);
ptr = hfd(ptr, type))
{
switch (tag) {
/* XXX Translate legacy signature tag values. */
case RPMSIGTAG_SIZE:
tag = RPMTAG_SIGSIZE;
break;
case RPMSIGTAG_LEMD5_1:
tag = RPMTAG_SIGLEMD5_1;
break;
case RPMSIGTAG_PGP:
tag = RPMTAG_SIGPGP;
break;
case RPMSIGTAG_LEMD5_2:
tag = RPMTAG_SIGLEMD5_2;
break;
case RPMSIGTAG_MD5:
tag = RPMTAG_SIGMD5;
break;
case RPMSIGTAG_GPG:
tag = RPMTAG_SIGGPG;
break;
case RPMSIGTAG_PGP5:
tag = RPMTAG_SIGPGP5;
break;
case RPMSIGTAG_PAYLOADSIZE:
tag = RPMTAG_ARCHIVESIZE;
break;
case RPMSIGTAG_SHA1:
case RPMSIGTAG_DSA:
case RPMSIGTAG_RSA:
default:
if (!(tag >= HEADER_SIGBASE && tag < HEADER_TAGBASE))
continue;
break;
}
if (ptr == NULL) continue; /* XXX can't happen */
if (!headerIsEntry(h, tag)) {
if (hdrchkType(type))
continue;
if (count < 0 || hdrchkData(count))
continue;
switch(type) {
case RPM_NULL_TYPE:
continue;
break;
case RPM_CHAR_TYPE:
case RPM_INT8_TYPE:
case RPM_INT16_TYPE:
case RPM_INT32_TYPE:
if (count != 1)
continue;
break;
case RPM_STRING_TYPE:
case RPM_BIN_TYPE:
if (count >= 16*1024)
continue;
break;
case RPM_STRING_ARRAY_TYPE:
case RPM_I18NSTRING_TYPE:
continue;
break;
}
xx = hae(h, tag, type, ptr, count);
}
}
hi = headerFreeIterator(hi);
}
Header headerRegenSigHeader(const Header h, int noArchiveSize)
{
HFD_t hfd = (HFD_t) headerFreeData;
Header sigh = rpmNewSignature();
HeaderIterator hi;
rpm_count_t count;
rpm_tag_t tag, stag;
rpm_tagtype_t type;
rpm_data_t 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;
break;
case RPMTAG_SIGLEMD5_1:
stag = RPMSIGTAG_LEMD5_1;
break;
case RPMTAG_SIGPGP:
stag = RPMSIGTAG_PGP;
break;
case RPMTAG_SIGLEMD5_2:
stag = RPMSIGTAG_LEMD5_2;
break;
case RPMTAG_SIGMD5:
stag = RPMSIGTAG_MD5;
break;
case RPMTAG_SIGGPG:
stag = RPMSIGTAG_GPG;
break;
case RPMTAG_SIGPGP5:
stag = RPMSIGTAG_PGP5;
break;
case RPMTAG_ARCHIVESIZE:
/* XXX rpm-4.1 and later has archive size in signature header. */
if (noArchiveSize)
continue;
stag = RPMSIGTAG_PAYLOADSIZE;
break;
case RPMTAG_SHA1HEADER:
case RPMTAG_DSAHEADER:
case RPMTAG_RSAHEADER:
default:
if (!(tag >= HEADER_SIGBASE && tag < HEADER_TAGBASE))
continue;
stag = tag;
break;
}
if (ptr == NULL) continue; /* XXX can't happen */
if (!headerIsEntry(sigh, stag))
xx = headerAddEntry(sigh, stag, type, ptr, count);
}
hi = headerFreeIterator(hi);
return sigh;
}
/**
* Remember current key id.
* @param ts transaction set
* @return 0 if new keyid, otherwise 1
*/
static int rpmtsStashKeyid(rpmts ts)
{
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++) {
if (keyid == keyids[i])
return 1;
}
if (nkeyids < nkeyids_max) {
nkeyids++;
keyids = xrealloc(keyids, nkeyids * sizeof(*keyids));
}
if (keyids) /* XXX can't happen */
keyids[nextkeyid] = keyid;
nextkeyid++;
nextkeyid %= nkeyids_max;
return 0;
}
int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
{
entryInfo pe = (entryInfo) pev;
entryInfo info = iv;
int i;
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))
return i;
if (hdrchkAlign(info->type, info->offset))
return i;
if (!negate && hdrchkRange(dl, info->offset))
return i;
if (hdrchkData(info->count))
return i;
}
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, char ** msg)
{
pgpDig dig;
char buf[8*BUFSIZ];
int32_t * ei = (int32_t *) uh;
int32_t il = ntohl(ei[0]);
int32_t dl = ntohl(ei[1]);
entryInfo pe = (entryInfo) &ei[2];
int32_t ildl[2];
int32_t pvlen = sizeof(ildl) + (il * sizeof(*pe)) + dl;
unsigned char * dataStart = (unsigned char *) (pe + il);
indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
void * sig = NULL;
unsigned const char * b;
rpmVSFlags vsflags = rpmtsVSFlags(ts);
size_t siglen = 0;
size_t blen;
size_t nb;
int32_t ril = 0;
unsigned char * regionEnd = NULL;
rpmRC rc = RPMRC_FAIL; /* assume failure */
int xx;
int i;
static int hclvl;
hclvl++;
buf[0] = '\0';
/* Is the blob the right size? */
if (uc > 0 && pvlen != uc) {
(void) snprintf(buf, sizeof(buf),
_("blob size(%d): BAD, 8 + 16 * il(%d) + dl(%d)\n"),
(int)uc, (int)il, (int)dl);
goto exit;
}
/* Check (and convert) the 1st tag element. */
xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
if (xx != -1) {
(void) snprintf(buf, sizeof(buf),
_("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
0, entry->info.tag, entry->info.type,
entry->info.offset, entry->info.count);
goto exit;
}
/* Is there an immutable header region tag? */
if (!(entry->info.tag == RPMTAG_HEADERIMMUTABLE
&& entry->info.type == RPM_BIN_TYPE
&& entry->info.count == REGION_TAG_COUNT))
{
rc = RPMRC_NOTFOUND;
goto exit;
}
/* Is the offset within the data area? */
if (entry->info.offset >= dl) {
(void) snprintf(buf, sizeof(buf),
_("region offset: BAD, tag %d type %d offset %d count %d\n"),
entry->info.tag, entry->info.type,
entry->info.offset, entry->info.count);
goto exit;
}
/* Is there an immutable header region tag trailer? */
regionEnd = dataStart + entry->info.offset;
(void) memcpy(info, regionEnd, REGION_TAG_COUNT);
regionEnd += REGION_TAG_COUNT;
xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
if (xx != -1 ||
!(entry->info.tag == RPMTAG_HEADERIMMUTABLE
&& entry->info.type == RPM_BIN_TYPE
&& entry->info.count == REGION_TAG_COUNT))
{
(void) snprintf(buf, sizeof(buf),
_("region trailer: BAD, tag %d type %d offset %d count %d\n"),
entry->info.tag, entry->info.type,
entry->info.offset, entry->info.count);
goto exit;
}
memset(info, 0, sizeof(*info));
/* Is the no. of tags in the region less than the total no. of tags? */
ril = entry->info.offset/sizeof(*pe);
if ((entry->info.offset % sizeof(*pe)) || ril > il) {
(void) snprintf(buf, sizeof(buf),
_("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
goto exit;
}
/* Find a header-only digest/signature tag. */
for (i = ril; i < il; i++) {
xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
if (xx != -1) {
(void) snprintf(buf, sizeof(buf),
_("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
i, entry->info.tag, entry->info.type,
entry->info.offset, entry->info.count);
goto exit;
}
switch (entry->info.tag) {
case RPMTAG_SHA1HEADER:
if (vsflags & RPMVSF_NOSHA1HEADER)
break;
blen = 0;
for (b = dataStart + entry->info.offset; *b != '\0'; b++) {
if (strchr("0123456789abcdefABCDEF", *b) == NULL)
break;
blen++;
}
if (entry->info.type != RPM_STRING_TYPE || *b != '\0' || blen != 40)
{
(void) snprintf(buf, sizeof(buf), _("hdr SHA1: BAD, not hex\n"));
goto exit;
}
if (info->tag == 0) {
*info = entry->info; /* structure assignment */
siglen = blen + 1;
}
break;
case RPMTAG_RSAHEADER:
if (vsflags & RPMVSF_NORSAHEADER)
break;
if (entry->info.type != RPM_BIN_TYPE) {
(void) snprintf(buf, sizeof(buf), _("hdr RSA: BAD, not binary\n"));
goto exit;
}
*info = entry->info; /* structure assignment */
siglen = info->count;
break;
case RPMTAG_DSAHEADER:
if (vsflags & RPMVSF_NODSAHEADER)
break;
if (entry->info.type != RPM_BIN_TYPE) {
(void) snprintf(buf, sizeof(buf), _("hdr DSA: BAD, not binary\n"));
goto exit;
}
*info = entry->info; /* structure assignment */
siglen = info->count;
break;
default:
break;
}
}
rc = RPMRC_NOTFOUND;
exit:
/* Return determined RPMRC_OK/RPMRC_FAIL conditions. */
if (rc != RPMRC_NOTFOUND) {
buf[sizeof(buf)-1] = '\0';
if (msg) *msg = xstrdup(buf);
hclvl--;
return rc;
}
/* If no header-only digest/signature, then do simple sanity check. */
if (info->tag == 0) {
verifyinfo_exit:
xx = headerVerifyInfo(ril-1, dl, pe+1, &entry->info, 0);
if (xx != -1) {
(void) snprintf(buf, sizeof(buf),
_("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
xx+1, entry->info.tag, entry->info.type,
entry->info.offset, entry->info.count);
rc = RPMRC_FAIL;
} else {
(void) snprintf(buf, sizeof(buf), "Header sanity check: OK\n");
rc = RPMRC_OK;
}
buf[sizeof(buf)-1] = '\0';
if (msg) *msg = xstrdup(buf);
hclvl--;
return rc;
}
/* Verify header-only digest/signature. */
dig = rpmtsDig(ts);
if (dig == NULL)
goto verifyinfo_exit;
dig->nbytes = 0;
sig = memcpy(xmalloc(siglen), dataStart + info->offset, siglen);
(void) rpmtsSetSig(ts, info->tag, info->type, sig, (size_t) info->count);
switch (info->tag) {
case RPMTAG_RSAHEADER:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, info->count, dig, (_print_pkts & rpmIsDebug()));
if (dig->signature.version != 3 && dig->signature.version != 4) {
rpmlog(RPMLOG_ERR,
_("skipping header with unverifiable V%u signature\n"),
dig->signature.version);
rpmtsCleanDig(ts);
rc = RPMRC_FAIL;
goto exit;
}
ildl[0] = htonl(ril);
ildl[1] = (regionEnd - dataStart);
ildl[1] = htonl(ildl[1]);
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
dig->hdrmd5ctx = rpmDigestInit(dig->signature.hash_algo, 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;
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), dig->nbytes);
break;
case RPMTAG_DSAHEADER:
/* Parse the parameters from the OpenPGP packets that will be needed. */
xx = pgpPrtPkts(sig, info->count, dig, (_print_pkts & rpmIsDebug()));
if (dig->signature.version != 3 && dig->signature.version != 4) {
rpmlog(RPMLOG_ERR,
_("skipping header with unverifiable V%u signature\n"),
dig->signature.version);
rpmtsCleanDig(ts);
rc = RPMRC_FAIL;
goto exit;
}
case RPMTAG_SHA1HEADER:
ildl[0] = htonl(ril);
ildl[1] = (regionEnd - dataStart);
ildl[1] = htonl(ildl[1]);
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
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;
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), dig->nbytes);
break;
default:
sig = _free(sig);
break;
}
buf[0] = '\0';
rc = rpmVerifySignature(ts, buf);
buf[sizeof(buf)-1] = '\0';
if (msg) *msg = xstrdup(buf);
/* XXX headerCheck can recurse, free info only at top level. */
if (hclvl == 1)
rpmtsCleanDig(ts);
if (info->tag == RPMTAG_SHA1HEADER)
sig = _free(sig);
hclvl--;
return rc;
}
rpmRC rpmReadHeader(rpmts ts, FD_t fd, Header *hdrp, char ** msg)
{
char buf[BUFSIZ];
int32_t block[4];
int32_t il;
int32_t dl;
int32_t * ei = NULL;
size_t uc;
size_t nb;
Header h = NULL;
rpmRC rc = RPMRC_FAIL; /* assume failure */
int xx;
buf[0] = '\0';
if (hdrp)
*hdrp = NULL;
if (msg)
*msg = NULL;
memset(block, 0, sizeof(block));
if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
(void) snprintf(buf, sizeof(buf),
_("hdr size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
goto exit;
}
if (memcmp(block, header_magic, sizeof(header_magic))) {
(void) snprintf(buf, sizeof(buf), _("hdr magic: BAD\n"));
goto exit;
}
il = ntohl(block[2]);
if (hdrchkTags(il)) {
(void) snprintf(buf, sizeof(buf),
_("hdr tags: BAD, no. of tags(%d) out of range\n"), il);
goto exit;
}
dl = ntohl(block[3]);
if (hdrchkData(dl)) {
(void) snprintf(buf, sizeof(buf),
_("hdr data: BAD, no. of bytes(%d) out of range\n"), dl);
goto exit;
}
nb = (il * sizeof(struct entryInfo_s)) + dl;
uc = sizeof(il) + sizeof(dl) + nb;
ei = xmalloc(uc);
ei[0] = block[2];
ei[1] = block[3];
if ((xx = timedRead(fd, (char *)&ei[2], nb)) != nb) {
(void) snprintf(buf, sizeof(buf),
_("hdr blob(%zd): BAD, read returned %d\n"), nb, xx);
goto exit;
}
/* Sanity check header tags */
rc = headerCheck(ts, ei, uc, msg);
if (rc != RPMRC_OK)
goto exit;
/* OK, blob looks sane, load the header. */
h = headerLoad(ei);
if (h == NULL) {
(void) snprintf(buf, sizeof(buf), _("hdr load: BAD\n"));
goto exit;
}
h->flags |= HEADERFLAG_ALLOCATED;
ei = NULL; /* XXX will be freed with header */
exit:
if (hdrp && h && rc == RPMRC_OK)
*hdrp = headerLink(h);
ei = _free(ei);
h = headerFree(h);
if (msg != NULL && *msg == NULL && buf[0] != '\0') {
buf[sizeof(buf)-1] = '\0';
*msg = xstrdup(buf);
}
return rc;
}
rpmRC rpmReadPackageFile(rpmts ts, FD_t fd, const char * fn, Header * hdrp)
{
pgpDig dig;
char buf[8*BUFSIZ];
ssize_t count;
rpmlead l = NULL;
Header sigh = NULL;
rpm_tag_t sigtag;
rpm_tagtype_t sigtype;
rpm_data_t sig;
rpm_count_t siglen;
rpmtsOpX opx;
size_t nb;
Header h = NULL;
char * msg;
rpmVSFlags vsflags;
rpmRC rc = RPMRC_FAIL; /* assume failure */
int xx;
if (hdrp) *hdrp = NULL;
l = rpmLeadNew();
if ((rc = rpmLeadRead(fd, l)) == RPMRC_OK) {
rc = rpmLeadCheck(l, fn);
}
l = rpmLeadFree(l);
if (rc != RPMRC_OK)
goto exit;
/* Read the signature header. */
msg = NULL;
rc = rpmReadSignature(fd, &sigh, RPMSIGTYPE_HEADERSIG, &msg);
switch (rc) {
default:
rpmlog(RPMLOG_ERR, _("%s: rpmReadSignature failed: %s"), fn,
(msg && *msg ? msg : "\n"));
msg = _free(msg);
goto exit;
break;
case RPMRC_OK:
if (sigh == NULL) {
rpmlog(RPMLOG_ERR, _("%s: No signature available\n"), fn);
rc = RPMRC_FAIL;
goto exit;
}
break;
}
msg = _free(msg);
#define _chk(_mask) (sigtag == 0 && !(vsflags & (_mask)))
/*
* Figger the most effective available signature.
* Prefer signatures over digests, then header-only over header+payload.
* DSA will be preferred over RSA if both exist because tested first.
* Note that NEEDPAYLOAD prevents header+payload signatures and digests.
*/
sigtag = 0;
opx = 0;
vsflags = rpmtsVSFlags(ts);
if (_chk(RPMVSF_NODSAHEADER) && headerIsEntry(sigh, RPMSIGTAG_DSA)) {
sigtag = RPMSIGTAG_DSA;
} else
if (_chk(RPMVSF_NORSAHEADER) && headerIsEntry(sigh, RPMSIGTAG_RSA)) {
sigtag = RPMSIGTAG_RSA;
} else
if (_chk(RPMVSF_NODSA|RPMVSF_NEEDPAYLOAD) &&
headerIsEntry(sigh, RPMSIGTAG_GPG))
{
sigtag = RPMSIGTAG_GPG;
fdInitDigest(fd, PGPHASHALGO_SHA1, 0);
opx = RPMTS_OP_SIGNATURE;
} else
if (_chk(RPMVSF_NORSA|RPMVSF_NEEDPAYLOAD) &&
headerIsEntry(sigh, RPMSIGTAG_PGP))
{
sigtag = RPMSIGTAG_PGP;
fdInitDigest(fd, PGPHASHALGO_MD5, 0);
opx = RPMTS_OP_SIGNATURE;
} else
if (_chk(RPMVSF_NOSHA1HEADER) && headerIsEntry(sigh, RPMSIGTAG_SHA1)) {
sigtag = RPMSIGTAG_SHA1;
} else
if (_chk(RPMVSF_NOMD5|RPMVSF_NEEDPAYLOAD) &&
headerIsEntry(sigh, RPMSIGTAG_MD5))
{
sigtag = RPMSIGTAG_MD5;
fdInitDigest(fd, PGPHASHALGO_MD5, 0);
opx = RPMTS_OP_DIGEST;
}
/* Read the metadata, computing digest(s) on the fly. */
h = NULL;
msg = NULL;
/* XXX stats will include header i/o and setup overhead. */
/* XXX repackaged packages have appended tags, legacy dig/sig check fails */
if (opx > 0)
(void) rpmswEnter(rpmtsOp(ts, opx), 0);
nb = -fd->stats->ops[FDSTAT_READ].bytes;
rc = rpmReadHeader(ts, fd, &h, &msg);
nb += fd->stats->ops[FDSTAT_READ].bytes;
if (opx > 0)
(void) rpmswExit(rpmtsOp(ts, opx), nb);
if (rc != RPMRC_OK || h == NULL) {
rpmlog(RPMLOG_ERR, _("%s: headerRead failed: %s"), fn,
(msg && *msg ? msg : "\n"));
msg = _free(msg);
goto exit;
}
msg = _free(msg);
/* Any digests or 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, &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()));
if (dig->signature.version != 3 && dig->signature.version != 4) {
rpmlog(RPMLOG_ERR,
_("skipping package %s with unverifiable V%u signature\n"),
fn, dig->signature.version);
rc = RPMRC_FAIL;
goto exit;
}
{ void * uh = NULL;
rpm_tagtype_t uht;
rpm_count_t uhc;
if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc))
break;
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
dig->hdrmd5ctx = rpmDigestInit(dig->signature.hash_algo, RPMDIGEST_NONE);
(void) rpmDigestUpdate(dig->hdrmd5ctx, header_magic, sizeof(header_magic));
dig->nbytes += sizeof(header_magic);
(void) rpmDigestUpdate(dig->hdrmd5ctx, uh, uhc);
dig->nbytes += uhc;
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), dig->nbytes);
rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
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()));
if (dig->signature.version != 3 && dig->signature.version != 4) {
rpmlog(RPMLOG_ERR,
_("skipping package %s with unverifiable V%u signature\n"),
fn, dig->signature.version);
rc = RPMRC_FAIL;
goto exit;
}
case RPMSIGTAG_SHA1:
{ void * uh = NULL;
rpm_tagtype_t uht;
rpm_count_t uhc;
if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc))
break;
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
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;
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), dig->nbytes);
if (sigtag == RPMSIGTAG_SHA1)
rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
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()));
if (dig->signature.version != 3 && dig->signature.version != 4) {
rpmlog(RPMLOG_ERR,
_("skipping package %s with unverifiable V%u signature\n"),
fn, dig->signature.version);
rc = RPMRC_FAIL;
goto exit;
}
case RPMSIGTAG_MD5:
/* Legacy signatures need the compressed payload in the digest too. */
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
while ((count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
dig->nbytes += count;
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), dig->nbytes);
rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
dig->nbytes += nb; /* XXX include size of header blob. */
if (count < 0) {
rpmlog(RPMLOG_ERR, _("%s: Fread failed: %s\n"),
fn, Fstrerror(fd));
rc = RPMRC_FAIL;
goto exit;
}
fdStealDigest(fd, dig);
break;
}
/** @todo Implement disable/enable/warn/error/anal policy. */
buf[0] = '\0';
rc = rpmVerifySignature(ts, buf);
switch (rc) {
case RPMRC_OK: /* Signature is OK. */
rpmlog(RPMLOG_DEBUG, "%s: %s", fn, buf);
break;
case RPMRC_NOTTRUSTED: /* Signature is OK, but key is not trusted. */
case RPMRC_NOKEY: /* Public key is unavailable. */
/* XXX Print NOKEY/NOTTRUSTED warning only once. */
{ int lvl = (rpmtsStashKeyid(ts) ? RPMLOG_DEBUG : RPMLOG_WARNING);
rpmlog(lvl, "%s: %s", fn, buf);
} break;
case RPMRC_NOTFOUND: /* Signature is unknown type. */
rpmlog(RPMLOG_WARNING, "%s: %s", fn, buf);
break;
default:
case RPMRC_FAIL: /* Signature does not verify. */
rpmlog(RPMLOG_ERR, "%s: %s", fn, buf);
break;
}
exit:
if (rc != RPMRC_FAIL && h != NULL && hdrp != NULL) {
/*
* Convert legacy headers on the fly. Not having "new" style compressed
* filenames is close enough estimate for legacy indication...
*/
if (!headerIsEntry(h, RPMTAG_DIRNAMES)) {
legacyRetrofit(h);
}
/* Append (and remap) signature tags to the metadata. */
headerMergeLegacySigs(h, sigh);
/* Bump reference count for return. */
*hdrp = headerLink(h);
}
h = headerFree(h);
rpmtsCleanDig(ts);
sigh = rpmFreeSignature(sigh);
return rc;
}
/**
* Check for supported payload format in header.
* @param h header to check
* @return RPMRC_OK if supported, RPMRC_FAIL otherwise
*/
rpmRC headerCheckPayloadFormat(Header h) {
rpmRC rc = RPMRC_FAIL;
int xx;
const char *payloadfmt = NULL;
xx = headerGetEntry(h, RPMTAG_PAYLOADFORMAT, NULL,
(rpm_data_t *)&payloadfmt, NULL);
/*
* XXX Ugh, rpm 3.x packages don't have payload format tag. Instead
* of blinly allowing, should check somehow (HDRID existence or... ?)
*/
if (!payloadfmt)
return RPMRC_OK;
if (payloadfmt && strncmp(payloadfmt, "cpio", strlen("cpio")) == 0) {
rc = RPMRC_OK;
} else {
char *nevra = headerGetNEVRA(h, NULL);
if (payloadfmt && strncmp(payloadfmt, "drpm", strlen("drpm")) == 0) {
rpmlog(RPMLOG_ERR,
_("%s is a Delta RPM and cannot be directly installed\n"),
nevra);
} else {
rpmlog(RPMLOG_ERR,
_("Unsupported payload (%s) in package %s\n"),
payloadfmt ? payloadfmt : "none", nevra);
}
nevra = _free(nevra);
}
return rc;
}
|