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
|
/** \ingroup rpmcli
* \file lib/rpmchecksig.c
* Verify the signature of a package.
*/
#include "system.h"
#include "rpmio_internal.h"
#include <rpmcli.h>
#include "rpmpgp.h"
#include "rpmlead.h"
#include "signature.h"
#include "misc.h" /* XXX for makeTempFile() */
#include "debug.h"
/*@access Header@*/ /* XXX compared with NULL */
/*@access FD_t@*/ /* XXX compared with NULL */
/*@access rpmDigest@*/
static int manageFile(FD_t *fdp, const char **fnp, int flags,
/*@unused@*/ int rc)
/*@globals fileSystem, internalState @*/
/*@modifies *fdp, *fnp, fileSystem, internalState @*/
{
const char *fn;
FD_t fd;
if (fdp == NULL) { /* programmer error */
return 1;
}
/* close and reset *fdp to NULL */
if (*fdp && (fnp == NULL || *fnp == NULL)) {
(void) Fclose(*fdp);
*fdp = NULL;
return 0;
}
/* open a file and set *fdp */
if (*fdp == NULL && fnp && *fnp) {
fd = Fopen(*fnp, ((flags & O_WRONLY) ? "w.ufdio" : "r.ufdio"));
if (fd == NULL || Ferror(fd)) {
rpmError(RPMERR_OPEN, _("%s: open failed: %s\n"), *fnp,
Fstrerror(fd));
return 1;
}
*fdp = fd;
return 0;
}
/* open a temp file */
if (*fdp == NULL && (fnp == NULL || *fnp == NULL)) {
fn = NULL;
if (makeTempFile(NULL, (fnp ? &fn : NULL), &fd)) {
rpmError(RPMERR_MAKETEMP, _("makeTempFile failed\n"));
return 1;
}
if (fnp)
*fnp = fn;
*fdp = fdLink(fd, "manageFile return");
fdFree(fd, "manageFile return");
return 0;
}
/* no operation */
if (*fdp && fnp && *fnp) {
return 0;
}
/* XXX never reached */
return 1;
}
static int copyFile(FD_t *sfdp, const char **sfnp,
FD_t *tfdp, const char **tfnp, rpmDigest dig)
/*@globals fileSystem, internalState @*/
/*@modifies *sfdp, *sfnp, *tfdp, *tfnp, *dig, fileSystem, internalState @*/
{
unsigned char buffer[BUFSIZ];
ssize_t count;
int rc = 1;
if (manageFile(sfdp, sfnp, O_RDONLY, 0))
goto exit;
if (manageFile(tfdp, tfnp, O_WRONLY|O_CREAT|O_TRUNC, 0))
goto exit;
if (dig != NULL) {
dig->md5ctx = rpmDigestInit(RPMDIGEST_MD5);
(void) fdInitSHA1(*sfdp, 0);
}
while ((count = Fread(buffer, sizeof(buffer[0]), sizeof(buffer), *sfdp)) > 0) {
if (dig)
rpmDigestUpdate(dig->md5ctx, buffer, count);
if (Fwrite(buffer, sizeof(buffer[0]), count, *tfdp) != count) {
rpmError(RPMERR_FWRITE, _("%s: Fwrite failed: %s\n"), *tfnp,
Fstrerror(*tfdp));
goto exit;
}
}
if (count < 0) {
rpmError(RPMERR_FREAD, _("%s: Fread failed: %s\n"), *sfnp, Fstrerror(*sfdp));
goto exit;
}
if (dig != NULL) {
dig->sha1ctx = _free(dig->sha1ctx);
dig->sha1ctx = (*sfdp)->digest;
(*sfdp)->digest = NULL;
}
rc = 0;
exit:
if (*sfdp) (void) manageFile(sfdp, NULL, 0, rc);
if (*tfdp) (void) manageFile(tfdp, NULL, 0, rc);
return rc;
}
int rpmReSign(rpmResignFlags flags, char * passPhrase, const char ** argv)
{
FD_t fd = NULL;
FD_t ofd = NULL;
struct rpmlead lead, *l = &lead;
int sigtype;
const char *rpm, *trpm;
const char *sigtarget = NULL;
char tmprpm[1024+1];
Header sig = NULL;
int res = EXIT_FAILURE;
rpmRC rc;
tmprpm[0] = '\0';
/*@-branchstate@*/
if (argv)
while ((rpm = *argv++) != NULL) {
fprintf(stdout, "%s:\n", rpm);
if (manageFile(&fd, &rpm, O_RDONLY, 0))
goto exit;
memset(l, 0, sizeof(*l));
if (readLead(fd, l)) {
rpmError(RPMERR_READLEAD, _("%s: readLead failed\n"), rpm);
goto exit;
}
switch (l->major) {
case 1:
rpmError(RPMERR_BADSIGTYPE, _("%s: Can't sign v1.0 RPM\n"), rpm);
goto exit;
/*@notreached@*/ /*@switchbreak@*/ break;
case 2:
rpmError(RPMERR_BADSIGTYPE, _("%s: Can't re-sign v2.0 RPM\n"), rpm);
goto exit;
/*@notreached@*/ /*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
rc = rpmReadSignature(fd, &sig, l->signature_type);
if (!(rc == RPMRC_OK || rc == RPMRC_BADSIZE)) {
rpmError(RPMERR_SIGGEN, _("%s: rpmReadSignature failed\n"), rpm);
goto exit;
}
if (sig == NULL) {
rpmError(RPMERR_SIGGEN, _("%s: No signature available\n"), rpm);
goto exit;
}
/* Write the header and archive to a temp file */
/* ASSERT: ofd == NULL && sigtarget == NULL */
if (copyFile(&fd, &rpm, &ofd, &sigtarget, NULL))
goto exit;
/* Both fd and ofd are now closed. sigtarget contains tempfile name. */
/* ASSERT: fd == NULL && ofd == NULL */
/* Generate the new signatures */
if (flags != RESIGN_ADD_SIGNATURE) {
sig = rpmFreeSignature(sig);
sig = rpmNewSignature();
(void) rpmAddSignature(sig, sigtarget, RPMSIGTAG_SIZE, passPhrase);
(void) rpmAddSignature(sig, sigtarget, RPMSIGTAG_MD5, passPhrase);
}
/*@-globs@*/ /* FIX: rpmGlobalMacroContext not in <rpmlib.h> */
if ((sigtype = rpmLookupSignatureType(RPMLOOKUPSIG_QUERY)) > 0)
(void) rpmAddSignature(sig, sigtarget, sigtype, passPhrase);
/*@=globs@*/
/* Write the lead/signature of the output rpm */
strcpy(tmprpm, rpm);
strcat(tmprpm, ".XXXXXX");
(void) mktemp(tmprpm);
trpm = tmprpm;
if (manageFile(&ofd, &trpm, O_WRONLY|O_CREAT|O_TRUNC, 0))
goto exit;
l->signature_type = RPMSIGTYPE_HEADERSIG;
if (writeLead(ofd, l)) {
rpmError(RPMERR_WRITELEAD, _("%s: writeLead failed: %s\n"), trpm,
Fstrerror(ofd));
goto exit;
}
if (rpmWriteSignature(ofd, sig)) {
rpmError(RPMERR_SIGGEN, _("%s: rpmWriteSignature failed: %s\n"), trpm,
Fstrerror(ofd));
goto exit;
}
/* Append the header and archive from the temp file */
/* ASSERT: fd == NULL && ofd != NULL */
if (copyFile(&fd, &sigtarget, &ofd, &trpm, NULL))
goto exit;
/* Both fd and ofd are now closed. */
/* ASSERT: fd == NULL && ofd == NULL */
/* Clean up intermediate target */
(void) unlink(sigtarget);
sigtarget = _free(sigtarget);
/* Move final target into place. */
(void) unlink(rpm);
(void) rename(trpm, rpm);
tmprpm[0] = '\0';
}
/*@=branchstate@*/
res = 0;
exit:
if (fd) (void) manageFile(&fd, NULL, 0, res);
if (ofd) (void) manageFile(&ofd, NULL, 0, res);
sig = rpmFreeSignature(sig);
if (sigtarget) {
(void) unlink(sigtarget);
sigtarget = _free(sigtarget);
}
if (tmprpm[0] != '\0') {
(void) unlink(tmprpm);
tmprpm[0] = '\0';
}
return res;
}
int rpmCheckSig(rpmCheckSigFlags flags, const char ** argv)
{
FD_t fd = NULL;
FD_t ofd = NULL;
int res2, res3;
struct rpmlead lead, *l = &lead;
const char *pkgfn = NULL;
char result[1024];
const char * sigtarget = NULL;
unsigned char buffer[8192];
unsigned char missingKeys[7164];
unsigned char untrustedKeys[7164];
Header sig;
HeaderIterator hi;
int_32 tag, type, count;
const void * ptr;
int res = 0;
const char * gpgpk = NULL;
unsigned int gpgpklen = 0;
const char * pgppk = NULL;
unsigned int pgppklen = 0;
rpmDigest dig = alloca(sizeof(*dig));
rpmRC rc;
if (argv == NULL) return res;
memset(dig, 0, sizeof(*dig));
/*@-branchstate@*/
while ((pkgfn = *argv++) != NULL) {
if (manageFile(&fd, &pkgfn, O_RDONLY, 0)) {
res++;
goto bottom;
}
memset(l, 0, sizeof(*l));
if (readLead(fd, l)) {
rpmError(RPMERR_READLEAD, _("%s: readLead failed\n"), pkgfn);
res++;
goto bottom;
}
switch (l->major) {
case 1:
rpmError(RPMERR_BADSIGTYPE, _("%s: No signature available (v1.0 RPM)\n"), pkgfn);
res++;
goto bottom;
/*@notreached@*/ /*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
rc = rpmReadSignature(fd, &sig, l->signature_type);
if (!(rc == RPMRC_OK || rc == RPMRC_BADSIZE)) {
rpmError(RPMERR_SIGGEN, _("%s: rpmReadSignature failed\n"), pkgfn);
res++;
goto bottom;
}
if (sig == NULL) {
rpmError(RPMERR_SIGGEN, _("%s: No signature available\n"), pkgfn);
res++;
goto bottom;
}
/* Write the header and archive to a temp file */
/* ASSERT: ofd == NULL && sigtarget == NULL */
if (copyFile(&fd, &pkgfn, &ofd, &sigtarget, dig)) {
res++;
goto bottom;
}
/* Both fd and ofd are now closed. sigtarget contains tempfile name. */
/* ASSERT: fd == NULL && ofd == NULL */
res2 = 0;
missingKeys[0] = '\0';
untrustedKeys[0] = '\0';
sprintf(buffer, "%s:%c", pkgfn, (rpmIsVerbose() ? '\n' : ' ') );
for (hi = headerInitIterator(sig);
headerNextIterator(hi, &tag, &type, &ptr, &count);
ptr = headerFreeData(ptr, type))
{
if (ptr == NULL) /* XXX can't happen */
/*@innercontinue@*/ continue;
switch (tag) {
case RPMSIGTAG_PGP5: /* XXX legacy */
case RPMSIGTAG_PGP:
if (!(flags & CHECKSIG_PGP))
/*@innercontinue@*/ continue;
if (rpmIsVerbose())
fprintf(stderr, "========================= Package RSA Signature\n");
(void) pgpPrtPkts(ptr, count, dig, rpmIsVerbose());
{ DIGEST_CTX ctx = rpmDigestDup(dig->md5ctx);
rpmDigestUpdate(ctx, &dig->sig.v3.sigtype, dig->sig.v3.hashlen);
rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 1);
/* XXX compare leading 16 bits of digest for quick check. */
}
/* XXX retrieve by keyid from signature. */
if (pgppk == NULL) {
(void) b64decode(redhatPubKeyRSA, (void **)&pgppk, &pgppklen);
if (rpmIsVerbose())
fprintf(stderr, "========================= Red Hat RSA Public Key\n");
(void) pgpPrtPkts(pgppk, pgppklen, NULL, rpmIsVerbose());
}
(void) pgpPrtPkts(pgppk, pgppklen, dig, 0);
{ const char * prefix = "3020300c06082a864886f70d020505000410";
unsigned int nbits = 1024;
unsigned int nb = (nbits + 7) >> 3;
const char * hexstr;
char * t;
hexstr = t = xmalloc(2 * nb + 1);
memset(t, 'f', (2 * nb));
t[0] = '0'; t[1] = '0';
t[2] = '0'; t[3] = '1';
t += (2 * nb) - strlen(prefix) - strlen(dig->md5) - 2;
*t++ = '0'; *t++ = '0';
t = stpcpy(t, prefix);
t = stpcpy(t, dig->md5);
mp32nzero(&dig->rsahm); mp32nsethex(&dig->rsahm, hexstr);
hexstr = _free(hexstr);
}
/*@switchbreak@*/ break;
case RPMSIGTAG_GPG:
if (!(flags & CHECKSIG_GPG))
/*@innercontinue@*/ continue;
if (rpmIsVerbose())
fprintf(stderr, "========================= Package DSA Signature\n");
(void) pgpPrtPkts(ptr, count, dig, rpmIsVerbose());
{ DIGEST_CTX ctx = rpmDigestDup(dig->sha1ctx);
rpmDigestUpdate(ctx, &dig->sig.v3.sigtype, dig->sig.v3.hashlen);
rpmDigestFinal(ctx, (void **)&dig->sha1, &dig->sha1len, 1);
mp32nzero(&dig->hm); mp32nsethex(&dig->hm, dig->sha1);
}
/* XXX retrieve by keyid from signature. */
if (gpgpk == NULL) {
(void) b64decode(redhatPubKeyDSA, (void **)&gpgpk, &gpgpklen);
if (rpmIsVerbose())
fprintf(stderr, "========================= Red Hat DSA Public Key\n");
(void) pgpPrtPkts(gpgpk, gpgpklen, NULL, rpmIsVerbose());
}
(void) pgpPrtPkts(gpgpk, gpgpklen, dig, 0);
/*@switchbreak@*/ break;
case RPMSIGTAG_LEMD5_2:
case RPMSIGTAG_LEMD5_1:
case RPMSIGTAG_MD5:
if (!(flags & CHECKSIG_MD5))
/*@innercontinue@*/ continue;
/*@switchbreak@*/ break;
default:
/*@innercontinue@*/ continue;
/*@notreached@*/ /*@switchbreak@*/ break;
}
if (ptr == NULL) /* XXX can't happen */
/*@innercontinue@*/ continue;
if ((res3 = rpmVerifySignature(sigtarget, tag, ptr, count,
dig, result))) {
if (rpmIsVerbose()) {
strcat(buffer, result);
res2 = 1;
} else {
char *tempKey;
switch (tag) {
case RPMSIGTAG_SIZE:
strcat(buffer, "SIZE ");
res2 = 1;
/*@switchbreak@*/ break;
case RPMSIGTAG_LEMD5_2:
case RPMSIGTAG_LEMD5_1:
case RPMSIGTAG_MD5:
strcat(buffer, "MD5 ");
res2 = 1;
/*@switchbreak@*/ break;
case RPMSIGTAG_PGP5: /* XXX legacy */
case RPMSIGTAG_PGP:
switch (res3) {
/* Do not consider these a failure */
case RPMSIG_NOKEY:
case RPMSIG_NOTTRUSTED:
{ int offset = 7;
strcat(buffer, "(PGP) ");
tempKey = strstr(result, "Key ID");
if (tempKey == NULL) {
tempKey = strstr(result, "keyid:");
offset = 9;
}
if (tempKey) {
if (res3 == RPMSIG_NOKEY) {
strcat(missingKeys, " PGP#");
strncat(missingKeys, tempKey + offset, 8);
} else {
strcat(untrustedKeys, " PGP#");
strncat(untrustedKeys, tempKey + offset, 8);
}
}
} /*@innerbreak@*/ break;
default:
strcat(buffer, "PGP ");
res2 = 1;
/*@innerbreak@*/ break;
}
/*@switchbreak@*/ break;
case RPMSIGTAG_GPG:
/* Do not consider this a failure */
switch (res3) {
case RPMSIG_NOKEY:
strcat(buffer, "(GPG) ");
strcat(missingKeys, " GPG#");
tempKey = strstr(result, "key ID");
if (tempKey)
strncat(missingKeys, tempKey+7, 8);
/*@innerbreak@*/ break;
default:
strcat(buffer, "GPG ");
res2 = 1;
/*@innerbreak@*/ break;
}
/*@switchbreak@*/ break;
default:
strcat(buffer, "?UnknownSignatureType? ");
res2 = 1;
/*@switchbreak@*/ break;
}
}
} else {
if (rpmIsVerbose()) {
strcat(buffer, result);
} else {
switch (tag) {
case RPMSIGTAG_SIZE:
strcat(buffer, "size ");
/*@switchbreak@*/ break;
case RPMSIGTAG_LEMD5_2:
case RPMSIGTAG_LEMD5_1:
case RPMSIGTAG_MD5:
strcat(buffer, "md5 ");
/*@switchbreak@*/ break;
case RPMSIGTAG_PGP5: /* XXX legacy */
case RPMSIGTAG_PGP:
strcat(buffer, "pgp ");
/*@switchbreak@*/ break;
case RPMSIGTAG_GPG:
strcat(buffer, "gpg ");
/*@switchbreak@*/ break;
default:
strcat(buffer, "??? ");
/*@switchbreak@*/ break;
}
}
}
}
hi = headerFreeIterator(hi);
res += res2;
(void) unlink(sigtarget);
sigtarget = _free(sigtarget);
if (res2) {
if (rpmIsVerbose()) {
rpmError(RPMERR_SIGVFY, "%s", (char *)buffer);
} else {
rpmError(RPMERR_SIGVFY, "%s%s%s%s%s%s%s%s\n", (char *)buffer,
_("NOT OK"),
(missingKeys[0] != '\0') ? _(" (MISSING KEYS:") : "",
(char *)missingKeys,
(missingKeys[0] != '\0') ? _(") ") : "",
(untrustedKeys[0] != '\0') ? _(" (UNTRUSTED KEYS:") : "",
(char *)untrustedKeys,
(untrustedKeys[0] != '\0') ? _(")") : "");
}
} else {
if (rpmIsVerbose()) {
rpmError(RPMERR_SIGVFY, "%s", (char *)buffer);
} else {
rpmError(RPMERR_SIGVFY, "%s%s%s%s%s%s%s%s\n", (char *)buffer,
_("OK"),
(missingKeys[0] != '\0') ? _(" (MISSING KEYS:") : "",
(char *)missingKeys,
(missingKeys[0] != '\0') ? _(") ") : "",
(untrustedKeys[0] != '\0') ? _(" (UNTRUSTED KEYS:") : "",
(char *)untrustedKeys,
(untrustedKeys[0] != '\0') ? _(")") : "");
}
}
bottom:
if (fd) (void) manageFile(&fd, NULL, 0, 0);
if (ofd) (void) manageFile(&ofd, NULL, 0, 0);
if (sigtarget) {
(void) unlink(sigtarget);
sigtarget = _free(sigtarget);
}
dig->sha1ctx = _free(dig->sha1ctx);
dig->sha1 = _free(dig->sha1);
dig->md5ctx = _free(dig->md5ctx);
dig->md5 = _free(dig->md5);
dig->hash_data = _free(dig->hash_data);
mp32nfree(&dig->hm);
mp32nfree(&dig->r);
mp32nfree(&dig->s);
(void) rsapkFree(&dig->rsa_pk);
mp32nfree(&dig->m);
mp32nfree(&dig->c);
mp32nfree(&dig->rsahm);
}
/*@=branchstate@*/
dig->sha1ctx = _free(dig->sha1ctx);
dig->sha1 = _free(dig->sha1);
dig->md5ctx = _free(dig->md5ctx);
dig->md5 = _free(dig->md5);
dig->hash_data = _free(dig->hash_data);
mp32bfree(&dig->p);
mp32bfree(&dig->q);
mp32nfree(&dig->g);
mp32nfree(&dig->y);
mp32nfree(&dig->hm);
mp32nfree(&dig->r);
mp32nfree(&dig->s);
(void) rsapkFree(&dig->rsa_pk);
mp32nfree(&dig->m);
mp32nfree(&dig->c);
mp32nfree(&dig->rsahm);
return res;
}
|