2 * \file lib/signature.c
9 #include <rpm/rpmtag.h>
10 #include <rpm/rpmlib.h> /* XXX RPMSIGTAG* & related */
11 #include <rpm/rpmmacro.h> /* XXX for rpmGetPath() */
12 #include <rpm/rpmdb.h>
13 #include <rpm/rpmstring.h>
14 #include <rpm/rpmfileutil.h>
15 #include <rpm/rpmlog.h>
16 #include <rpm/rpmts.h>
18 #include "rpmio/digest.h"
19 #include "lib/misc.h" /* XXX for dosetenv() */
20 #include "lib/rpmlead.h"
21 #include "lib/signature.h"
22 #include "rpmdb/header_internal.h"
26 #if !defined(__GLIBC__) && !defined(__APPLE__)
27 char ** environ = NULL;
30 int rpmLookupSignatureType(int action)
32 static int disabled = 0;
36 case RPMLOOKUPSIG_DISABLE:
39 case RPMLOOKUPSIG_ENABLE:
41 case RPMLOOKUPSIG_QUERY:
44 { char *name = rpmExpand("%{?_signature}", NULL);
45 if (!(name && *name != '\0'))
47 else if (!xstrcasecmp(name, "none"))
49 else if (!xstrcasecmp(name, "pgp"))
51 else if (!xstrcasecmp(name, "pgp5")) /* XXX legacy */
53 else if (!xstrcasecmp(name, "gpg"))
56 rc = -1; /* Invalid %_signature spec in macro file */
63 /* rpmDetectPGPVersion() returns the absolute path to the "pgp" */
64 /* executable of the requested version, or NULL when none found. */
66 const char * rpmDetectPGPVersion(pgpVersion * pgpVer)
68 /* Actually this should support having more then one pgp version. */
69 /* At the moment only one version is possible since we only */
70 /* have one %_pgpbin and one %_pgp_path. */
72 static pgpVersion saved_pgp_version = PGP_UNKNOWN;
73 char *pgpbin = rpmGetPath("%{?_pgpbin}", NULL);
75 if (saved_pgp_version == PGP_UNKNOWN) {
79 if (!(pgpbin && pgpbin[0] != '\0')) {
80 pgpbin = _free(pgpbin);
81 saved_pgp_version = -1;
84 pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
85 (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
87 if (stat(pgpvbin, &st) == 0)
88 saved_pgp_version = PGP_5;
89 else if (stat(pgpbin, &st) == 0)
90 saved_pgp_version = PGP_2;
92 saved_pgp_version = PGP_NOTDETECTED;
96 *pgpVer = saved_pgp_version;
101 * Print package size.
102 * @todo rpmio: use fdSize rather than fstat(2) to get file size.
103 * @param fd package file handle
104 * @param siglen signature header size
105 * @param pad signature padding
106 * @param datalen length of header+payload
107 * @return rpmRC return code
109 static inline rpmRC printSize(FD_t fd, size_t siglen, size_t pad, rpm_off_t datalen)
112 int fdno = Fileno(fd);
114 if (fstat(fdno, &st) < 0)
118 _("Expected size: %12zd = lead(%d)+sigs(%zd)+pad(%zd)+data(%d)\n"),
119 RPMLEAD_SIZE+siglen+pad+datalen,
120 RPMLEAD_SIZE, siglen, pad, datalen);
122 _(" Actual size: %12d\n"), (int)st.st_size);
127 static unsigned char header_magic[8] = {
128 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
131 rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type, char ** msg)
141 indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
142 entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
143 unsigned char * dataStart;
144 unsigned char * dataEnd = NULL;
146 rpmRC rc = RPMRC_FAIL; /* assume failure */
155 if (sig_type != RPMSIGTYPE_HEADERSIG)
158 memset(block, 0, sizeof(block));
159 if ((xx = timedRead(fd, (void *)block, sizeof(block))) != sizeof(block)) {
160 (void) snprintf(buf, sizeof(buf),
161 _("sigh size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
164 if (memcmp(block, header_magic, sizeof(header_magic))) {
165 (void) snprintf(buf, sizeof(buf),
166 _("sigh magic: BAD\n"));
169 il = ntohl(block[2]);
170 if (il < 0 || il > 32) {
171 (void) snprintf(buf, sizeof(buf),
172 _("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
175 dl = ntohl(block[3]);
176 if (dl < 0 || dl > 8192) {
177 (void) snprintf(buf, sizeof(buf),
178 _("sigh data: BAD, no. of bytes(%d) out of range\n"), dl);
182 nb = (il * sizeof(struct entryInfo_s)) + dl;
183 ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
186 pe = (entryInfo) &ei[2];
187 dataStart = (unsigned char *) (pe + il);
188 if ((xx = timedRead(fd, (void *)pe, nb)) != nb) {
189 (void) snprintf(buf, sizeof(buf),
190 _("sigh blob(%d): BAD, read returned %d\n"), (int)nb, xx);
194 /* Check (and convert) the 1st tag element. */
195 xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
197 (void) snprintf(buf, sizeof(buf),
198 _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
199 0, entry->info.tag, entry->info.type,
200 entry->info.offset, entry->info.count);
204 /* Is there an immutable header region tag? */
205 if (entry->info.tag == RPMTAG_HEADERSIGNATURES
206 && entry->info.type == RPM_BIN_TYPE
207 && entry->info.count == REGION_TAG_COUNT)
210 if (entry->info.offset >= dl) {
211 (void) snprintf(buf, sizeof(buf),
212 _("region offset: BAD, tag %d type %d offset %d count %d\n"),
213 entry->info.tag, entry->info.type,
214 entry->info.offset, entry->info.count);
218 /* Is there an immutable header region tag trailer? */
219 dataEnd = dataStart + entry->info.offset;
220 (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
221 /* XXX Really old packages have HEADER_IMAGE, not HEADER_SIGNATURES. */
222 if (info->tag == htonl(RPMTAG_HEADERIMAGE)) {
223 rpmSigTag stag = htonl(RPMTAG_HEADERSIGNATURES);
225 memcpy(dataEnd, &stag, sizeof(stag));
227 dataEnd += REGION_TAG_COUNT;
229 xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
231 !((entry->info.tag == RPMTAG_HEADERSIGNATURES || entry->info.tag == RPMTAG_HEADERIMAGE)
232 && entry->info.type == RPM_BIN_TYPE
233 && entry->info.count == REGION_TAG_COUNT))
235 (void) snprintf(buf, sizeof(buf),
236 _("region trailer: BAD, tag %d type %d offset %d count %d\n"),
237 entry->info.tag, entry->info.type,
238 entry->info.offset, entry->info.count);
241 memset(info, 0, sizeof(*info));
243 /* Is the no. of tags in the region less than the total no. of tags? */
244 ril = entry->info.offset/sizeof(*pe);
245 if ((entry->info.offset % sizeof(*pe)) || ril > il) {
246 (void) snprintf(buf, sizeof(buf),
247 _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
252 /* Sanity check signature tags */
253 memset(info, 0, sizeof(*info));
254 for (i = 1; i < il; i++) {
255 xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
257 (void) snprintf(buf, sizeof(buf),
258 _("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
259 i, entry->info.tag, entry->info.type,
260 entry->info.offset, entry->info.count);
265 /* OK, blob looks sane, load the header. */
266 sigh = headerLoad(ei);
268 (void) snprintf(buf, sizeof(buf), _("sigh load: BAD\n"));
271 sigh->flags |= HEADERFLAG_ALLOCATED;
273 { size_t sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
274 size_t pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
276 rpm_off_t * archSize = NULL;
278 /* Position at beginning of header. */
279 if (pad && (trc = timedRead(fd, (void *)block, pad)) != pad) {
280 (void) snprintf(buf, sizeof(buf),
281 _("sigh pad(%zd): BAD, read %zd bytes\n"), pad, trc);
285 /* Print package component sizes. */
286 if (headerGetEntry(sigh, RPMSIGTAG_SIZE, NULL,(rpm_data_t *)&archSize, NULL)) {
287 rc = printSize(fd, sigSize, pad, *archSize);
289 (void) snprintf(buf, sizeof(buf),
290 _("sigh sigSize(%zd): BAD, fstat(2) failed\n"), sigSize);
295 if (sighp && sigh && rc == RPMRC_OK)
296 *sighp = headerLink(sigh);
297 sigh = headerFree(sigh);
300 buf[sizeof(buf)-1] = '\0';
307 int rpmWriteSignature(FD_t fd, Header sigh)
309 static uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
313 rc = headerWrite(fd, sigh, HEADER_MAGIC_YES);
317 sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
318 pad = (8 - (sigSize % 8)) % 8;
320 if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
323 rpmlog(RPMLOG_DEBUG, _("Signature: size(%d)+pad(%d)\n"), sigSize, pad);
327 Header rpmNewSignature(void)
329 Header sigh = headerNew();
333 Header rpmFreeSignature(Header sigh)
335 return headerFree(sigh);
339 * Generate PGP signature(s) for a header+payload file.
340 * @param file header+payload file name
341 * @retval *sigTagp signature tag
342 * @retval *pktp signature packet(s)
343 * @retval *pktlenp signature packet(s) length
344 * @param passPhrase private key pass phrase
345 * @return 0 on success, 1 on failure
347 static int makePGPSignature(const char * file, rpmSigTag * sigTagp,
348 uint8_t ** pktp, size_t * pktlenp,
349 const char * passPhrase)
351 char * sigfile = alloca(1024);
359 pgpDigParams sigp = NULL;
363 (void) stpcpy( stpcpy(sigfile, file), ".sig");
365 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
366 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
368 inpipe[0] = inpipe[1] = 0;
371 if (!(pid = fork())) {
372 const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
376 (void) dup2(inpipe[0], 3);
377 (void) close(inpipe[1]);
379 (void) dosetenv("PGPPASSFD", "3", 1);
380 if (pgp_path && *pgp_path != '\0')
381 (void) dosetenv("PGPPATH", pgp_path, 1);
383 /* dosetenv("PGPPASS", passPhrase, 1); */
385 unsetenv("MALLOC_CHECK_");
386 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
389 cmd = rpmExpand("%{?__pgp_sign_cmd}", NULL);
390 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
392 rc = execve(av[0], av+1, environ);
395 cmd = rpmExpand("%{?__pgp5_sign_cmd}", NULL);
396 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
398 rc = execve(av[0], av+1, environ);
401 case PGP_NOTDETECTED:
406 rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "pgp",
411 delMacro(NULL, "__plaintext_filename");
412 delMacro(NULL, "__signature_filename");
414 (void) close(inpipe[0]);
416 (void) write(inpipe[1], passPhrase, strlen(passPhrase));
417 (void) write(inpipe[1], "\n", 1);
418 (void) close(inpipe[1]);
420 (void)waitpid(pid, &status, 0);
421 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
422 rpmlog(RPMLOG_ERR, _("pgp failed\n"));
426 if (stat(sigfile, &st)) {
427 /* PGP failed to write signature */
428 if (sigfile) (void) unlink(sigfile); /* Just in case */
429 rpmlog(RPMLOG_ERR, _("pgp failed to write signature\n"));
433 *pktlenp = st.st_size;
434 rpmlog(RPMLOG_DEBUG, _("PGP sig size: %zd\n"), *pktlenp);
435 *pktp = xmalloc(*pktlenp);
440 fd = Fopen(sigfile, "r.fdio");
441 if (fd != NULL && !Ferror(fd)) {
442 rc = timedRead(fd, (void *)*pktp, *pktlenp);
443 if (sigfile) (void) unlink(sigfile);
446 if (rc != *pktlenp) {
447 *pktp = _free(*pktp);
448 rpmlog(RPMLOG_ERR, _("unable to read the signature\n"));
453 rpmlog(RPMLOG_DEBUG, _("Got %zd bytes of PGP sig\n"), *pktlenp);
456 /* Parse the signature, change signature tag as appropriate. */
459 (void) pgpPrtPkts(*pktp, *pktlenp, dig, 0);
460 sigp = &dig->signature;
462 dig = pgpFreeDig(dig);
469 * Generate GPG signature(s) for a header+payload file.
470 * @param file header+payload file name
471 * @retval *sigTagp signature tag
472 * @retval *pktp signature packet(s)
473 * @retval *pktlenp signature packet(s) length
474 * @param passPhrase private key pass phrase
475 * @return 0 on success, 1 on failure
477 static int makeGPGSignature(const char * file, rpmSigTag * sigTagp,
478 uint8_t ** pktp, size_t * pktlenp,
479 const char * passPhrase)
481 char * sigfile = alloca(strlen(file)+sizeof(".sig"));
489 pgpDigParams sigp = NULL;
492 (void) stpcpy( stpcpy(sigfile, file), ".sig");
494 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
495 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
497 inpipe[0] = inpipe[1] = 0;
500 if (!(pid = fork())) {
501 const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
503 (void) dup2(inpipe[0], 3);
504 (void) close(inpipe[1]);
506 if (gpg_path && *gpg_path != '\0')
507 (void) dosetenv("GNUPGHOME", gpg_path, 1);
508 (void) dosetenv("LC_ALL", "C", 1);
510 unsetenv("MALLOC_CHECK_");
511 cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
512 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
514 rc = execve(av[0], av+1, environ);
516 rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "gpg",
521 delMacro(NULL, "__plaintext_filename");
522 delMacro(NULL, "__signature_filename");
524 fpipe = fdopen(inpipe[1], "w");
525 (void) close(inpipe[0]);
527 fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
528 (void) fclose(fpipe);
531 (void) waitpid(pid, &status, 0);
532 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
533 rpmlog(RPMLOG_ERR, _("gpg exec failed (%d)\n"), WEXITSTATUS(status));
537 if (stat(sigfile, &st)) {
538 /* GPG failed to write signature */
539 if (sigfile) (void) unlink(sigfile); /* Just in case */
540 rpmlog(RPMLOG_ERR, _("gpg failed to write signature\n"));
544 *pktlenp = st.st_size;
545 rpmlog(RPMLOG_DEBUG, _("GPG sig size: %zd\n"), *pktlenp);
546 *pktp = xmalloc(*pktlenp);
551 fd = Fopen(sigfile, "r.fdio");
552 if (fd != NULL && !Ferror(fd)) {
553 rc = timedRead(fd, (void *)*pktp, *pktlenp);
554 if (sigfile) (void) unlink(sigfile);
557 if (rc != *pktlenp) {
558 *pktp = _free(*pktp);
559 rpmlog(RPMLOG_ERR, _("unable to read the signature\n"));
564 rpmlog(RPMLOG_DEBUG, _("Got %zd bytes of GPG sig\n"), *pktlenp);
566 /* Parse the signature, change signature tag as appropriate. */
569 (void) pgpPrtPkts(*pktp, *pktlenp, dig, 0);
570 sigp = &dig->signature;
578 /* XXX check MD5 hash too? */
579 if (sigp->pubkey_algo == PGPPUBKEYALGO_RSA)
580 *sigTagp = RPMSIGTAG_PGP;
582 case RPMSIGTAG_PGP5: /* XXX legacy */
584 if (sigp->pubkey_algo == PGPPUBKEYALGO_DSA)
585 *sigTagp = RPMSIGTAG_GPG;
588 /* XXX check MD5 hash too? */
589 if (sigp->pubkey_algo == PGPPUBKEYALGO_RSA)
590 *sigTagp = RPMSIGTAG_RSA;
593 if (sigp->pubkey_algo == PGPPUBKEYALGO_DSA)
594 *sigTagp = RPMSIGTAG_DSA;
597 case RPMSIGTAG_LEMD5_1:
598 case RPMSIGTAG_LEMD5_2:
599 case RPMSIGTAG_BADSHA1_1:
600 case RPMSIGTAG_BADSHA1_2:
601 case RPMSIGTAG_PAYLOADSIZE:
605 dig = pgpFreeDig(dig);
611 * Generate header only signature(s) from a header+payload file.
612 * @param sigh signature header
613 * @param file header+payload file name
614 * @param sigTag type of signature(s) to add
615 * @param passPhrase private key pass phrase
616 * @return 0 on success, -1 on failure
618 static int makeHDRSignature(Header sigh, const char * file, rpmSigTag sigTag,
619 const char * passPhrase)
627 int ret = -1; /* assume failure. */
632 case RPMSIGTAG_PGP5: /* XXX legacy */
638 fd = Fopen(file, "r.fdio");
639 if (fd == NULL || Ferror(fd))
641 h = headerRead(fd, HEADER_MAGIC_YES);
644 (void) Fclose(fd); fd = NULL;
646 if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
652 if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)
658 ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
659 (void) rpmDigestUpdate(ctx, header_magic, sizeof(header_magic));
660 (void) rpmDigestUpdate(ctx, uh, uhc);
661 (void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
662 uh = headerFreeData(uh, uht);
668 if (!headerAddEntry(sigh, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
673 fd = Fopen(file, "r.fdio");
674 if (fd == NULL || Ferror(fd))
676 h = headerRead(fd, HEADER_MAGIC_YES);
679 (void) Fclose(fd); fd = NULL;
680 if (rpmMkTempFile(NULL, &fn, &fd))
682 if (headerWrite(fd, h, HEADER_MAGIC_YES))
684 (void) Fclose(fd); fd = NULL;
685 if (makeGPGSignature(fn, &sigTag, &pkt, &pktlen, passPhrase)
686 || !headerAddEntry(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
691 fd = Fopen(file, "r.fdio");
692 if (fd == NULL || Ferror(fd))
694 h = headerRead(fd, HEADER_MAGIC_YES);
697 (void) Fclose(fd); fd = NULL;
698 if (rpmMkTempFile(NULL, &fn, &fd))
700 if (headerWrite(fd, h, HEADER_MAGIC_YES))
702 (void) Fclose(fd); fd = NULL;
703 if (makePGPSignature(fn, &sigTag, &pkt, &pktlen, passPhrase)
704 || !headerAddEntry(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
709 case RPMSIGTAG_LEMD5_1:
710 case RPMSIGTAG_LEMD5_2:
711 case RPMSIGTAG_BADSHA1_1:
712 case RPMSIGTAG_BADSHA1_2:
713 case RPMSIGTAG_PAYLOADSIZE:
724 if (fd != NULL) (void) Fclose(fd);
728 int rpmAddSignature(Header sigh, const char * file, rpmSigTag sigTag,
729 const char * passPhrase)
734 int ret = -1; /* assume failure. */
738 if (stat(file, &st) != 0)
741 if (!headerAddEntry(sigh, sigTag, RPM_INT32_TYPE, &pktlen, 1))
747 pkt = memset(alloca(pktlen), 0, pktlen);
748 if (rpmDoDigest(PGPHASHALGO_MD5, file, 0, pkt, NULL)
749 || !headerAddEntry(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
753 case RPMSIGTAG_PGP5: /* XXX legacy */
755 if (makePGPSignature(file, &sigTag, &pkt, &pktlen, passPhrase)
756 || !headerAddEntry(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
758 #ifdef NOTYET /* XXX needs hdrmd5ctx, like hdrsha1ctx. */
759 /* XXX Piggyback a header-only RSA signature as well. */
760 ret = makeHDRSignature(sigh, file, RPMSIGTAG_RSA, passPhrase);
765 if (makeGPGSignature(file, &sigTag, &pkt, &pktlen, passPhrase)
766 || !headerAddEntry(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
768 /* XXX Piggyback a header-only DSA signature as well. */
769 ret = makeHDRSignature(sigh, file, RPMSIGTAG_DSA, passPhrase);
774 ret = makeHDRSignature(sigh, file, sigTag, passPhrase);
777 case RPMSIGTAG_LEMD5_1:
778 case RPMSIGTAG_LEMD5_2:
779 case RPMSIGTAG_BADSHA1_1:
780 case RPMSIGTAG_BADSHA1_2:
781 case RPMSIGTAG_PAYLOADSIZE:
788 static int checkPassPhrase(const char * passPhrase, const rpmSigTag sigTag)
790 int passPhrasePipe[2];
795 passPhrasePipe[0] = passPhrasePipe[1] = 0;
796 xx = pipe(passPhrasePipe);
797 if (!(pid = fork())) {
802 xx = close(STDIN_FILENO);
803 xx = close(STDOUT_FILENO);
804 xx = close(passPhrasePipe[1]);
805 if (! rpmIsVerbose())
806 xx = close(STDERR_FILENO);
807 if ((fdno = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
808 xx = dup2(fdno, STDIN_FILENO);
811 if ((fdno = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
812 xx = dup2(fdno, STDOUT_FILENO);
815 xx = dup2(passPhrasePipe[0], 3);
817 unsetenv("MALLOC_CHECK_");
821 { const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
823 if (gpg_path && *gpg_path != '\0')
824 (void) dosetenv("GNUPGHOME", gpg_path, 1);
826 cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
827 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
829 rc = execve(av[0], av+1, environ);
831 rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "gpg",
835 case RPMSIGTAG_PGP5: /* XXX legacy */
837 { const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
841 (void) dosetenv("PGPPASSFD", "3", 1);
842 if (pgp_path && *pgp_path != '\0')
843 xx = dosetenv("PGPPATH", pgp_path, 1);
845 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
848 cmd = rpmExpand("%{?__pgp_check_password_cmd}", NULL);
849 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
851 rc = execve(av[0], av+1, environ);
853 case PGP_5: /* XXX legacy */
854 cmd = rpmExpand("%{?__pgp5_check_password_cmd}", NULL);
855 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
857 rc = execve(av[0], av+1, environ);
860 case PGP_NOTDETECTED:
864 rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "pgp",
868 default: /* This case should have been screened out long ago. */
869 rpmlog(RPMLOG_ERR, _("Invalid %%_signature spec in macro file\n"));
875 xx = close(passPhrasePipe[0]);
876 xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
877 xx = write(passPhrasePipe[1], "\n", 1);
878 xx = close(passPhrasePipe[1]);
880 (void) waitpid(pid, &status, 0);
882 return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
885 char * rpmGetPassPhrase(const char * prompt, const rpmSigTag sigTag)
893 { char *name = rpmExpand("%{?_gpg_name}", NULL);
894 aok = (name && *name != '\0');
900 _("You must set \"%%_gpg_name\" in your macro file\n"));
903 case RPMSIGTAG_PGP5: /* XXX legacy */
905 { char *name = rpmExpand("%{?_pgp_name}", NULL);
906 aok = (name && *name != '\0');
912 _("You must set \"%%_pgp_name\" in your macro file\n"));
915 /* Currently the calling function (rpm.c:main) is checking this and
916 * doing a better job. This section should never be accessed.
918 rpmlog(RPMLOG_ERR, _("Invalid %%_signature spec in macro file\n"));
923 pass = getpass( (prompt ? prompt : "") );
925 if (checkPassPhrase(pass, sigTag))
932 static const char * rpmSigString(rpmRC res)
936 case RPMRC_OK: str = "OK"; break;
937 case RPMRC_FAIL: str = "BAD"; break;
938 case RPMRC_NOKEY: str = "NOKEY"; break;
939 case RPMRC_NOTTRUSTED: str = "NOTRUSTED"; break;
941 case RPMRC_NOTFOUND: str = "UNKNOWN"; break;
947 verifySizeSignature(const rpmts ts, char * t)
949 rpm_constdata_t sig = rpmtsSig(ts);
950 pgpDig dig = rpmtsDig(ts);
952 size_t size = 0x7fffffff;
955 t = stpcpy(t, _("Header+Payload size: "));
957 if (sig == NULL || dig == NULL || dig->nbytes == 0) {
959 t = stpcpy(t, rpmSigString(res));
963 memcpy(&size, sig, sizeof(size));
965 if (size != dig->nbytes) {
967 t = stpcpy(t, rpmSigString(res));
968 sprintf(t, " Expected(%zd) != (%zd)\n", size, dig->nbytes);
971 t = stpcpy(t, rpmSigString(res));
972 sprintf(t, " (%d)", (int)dig->nbytes);
981 verifyMD5Signature(const rpmts ts, char * t,
984 rpm_constdata_t sig = rpmtsSig(ts);
985 size_t siglen = rpmtsSiglen(ts);
986 pgpDig dig = rpmtsDig(ts);
988 uint8_t * md5sum = NULL;
992 t = stpcpy(t, _("MD5 digest: "));
994 if (md5ctx == NULL || sig == NULL || dig == NULL) {
996 t = stpcpy(t, rpmSigString(res));
1000 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1001 (void) rpmDigestFinal(rpmDigestDup(md5ctx),
1002 (void **)&md5sum, &md5len, 0);
1003 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1004 rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
1006 if (md5len != siglen || memcmp(md5sum, sig, md5len)) {
1008 t = stpcpy(t, rpmSigString(res));
1009 t = stpcpy(t, " Expected(");
1010 (void) pgpHexCvt(t, sig, siglen);
1012 t = stpcpy(t, ") != (");
1015 t = stpcpy(t, rpmSigString(res));
1016 t = stpcpy(t, " (");
1018 (void) pgpHexCvt(t, md5sum, md5len);
1023 md5sum = _free(md5sum);
1024 t = stpcpy(t, "\n");
1029 * Verify header immutable region SHA1 digest.
1030 * @param ts transaction set
1031 * @retval t verbose success/failure text
1033 * @return RPMRC_OK on success
1036 verifySHA1Signature(const rpmts ts, char * t,
1039 rpm_constdata_t sig = rpmtsSig(ts);
1041 size_t siglen = rpmtsSiglen(ts);
1043 pgpDig dig = rpmtsDig(ts);
1048 t = stpcpy(t, _("Header SHA1 digest: "));
1050 if (sha1ctx == NULL || sig == NULL || dig == NULL) {
1052 t = stpcpy(t, rpmSigString(res));
1056 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1057 (void) rpmDigestFinal(rpmDigestDup(sha1ctx),
1058 (void **)&SHA1, NULL, 1);
1059 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1061 if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
1063 t = stpcpy(t, rpmSigString(res));
1064 t = stpcpy(t, " Expected(");
1066 t = stpcpy(t, ") != (");
1069 t = stpcpy(t, rpmSigString(res));
1070 t = stpcpy(t, " (");
1073 t = stpcpy(t, SHA1);
1078 t = stpcpy(t, "\n");
1083 * Convert hex to binary nibble.
1084 * @param c hex character
1085 * @return binary nibble
1087 static inline unsigned char nibble(char c)
1089 if (c >= '0' && c <= '9')
1091 if (c >= 'A' && c <= 'F')
1092 return (c - 'A') + 10;
1093 if (c >= 'a' && c <= 'f')
1094 return (c - 'a') + 10;
1099 * Verify RSA signature.
1100 * @param ts transaction set
1101 * @retval t verbose success/failure text
1103 * @return RPMRC_OK on success
1106 verifyRSASignature(rpmts ts, char * t,
1109 rpm_constdata_t sig = rpmtsSig(ts);
1111 size_t siglen = rpmtsSiglen(ts);
1113 rpmSigTag sigtag = rpmtsSigtag(ts);
1114 pgpDig dig = rpmtsDig(ts);
1115 pgpDigParams sigp = rpmtsSignature(ts);
1117 rpmRC res = RPMRC_OK;
1122 if (dig != NULL && dig->hdrmd5ctx == md5ctx)
1123 t = stpcpy(t, _("Header "));
1125 switch (sigp->version) {
1126 case 3: *t++ = '3'; break;
1127 case 4: *t++ = '4'; break;
1130 if (md5ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1134 /* Verify the desired signature match. */
1135 switch (sigp->pubkey_algo) {
1136 case PGPPUBKEYALGO_RSA:
1137 if (sigtag == RPMSIGTAG_PGP || sigtag == RPMSIGTAG_PGP5 || sigtag == RPMSIGTAG_RSA)
1144 /* Verify the desired hash match. */
1145 /* XXX Values from PKCS#1 v2.1 (aka RFC-3447) */
1146 switch (sigp->hash_algo) {
1147 case PGPHASHALGO_MD5:
1148 t = stpcpy(t, " RSA/MD5");
1149 sigalg = SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION;
1151 case PGPHASHALGO_SHA1:
1152 t = stpcpy(t, " RSA/SHA1");
1153 sigalg = SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION;
1155 case PGPHASHALGO_RIPEMD160:
1158 case PGPHASHALGO_MD2:
1159 t = stpcpy(t, " RSA/MD2");
1160 sigalg = SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION;
1162 case PGPHASHALGO_TIGER192:
1165 case PGPHASHALGO_HAVAL_5_160:
1168 case PGPHASHALGO_SHA256:
1169 t = stpcpy(t, " RSA/SHA256");
1170 sigalg = SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION;
1172 case PGPHASHALGO_SHA384:
1173 t = stpcpy(t, " RSA/SHA384");
1174 sigalg = SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION;
1176 case PGPHASHALGO_SHA512:
1177 t = stpcpy(t, " RSA/SHA512");
1178 sigalg = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION;
1182 sigalg = SEC_OID_UNKNOWN;
1186 t = stpcpy(t, _(" signature: "));
1187 if (res != RPMRC_OK) {
1191 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1192 { DIGEST_CTX ctx = rpmDigestDup(md5ctx);
1194 if (sigp->hash != NULL)
1195 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1197 #ifdef NOTYET /* XXX not for binary/text signatures as in packages. */
1198 if (!(sigp->sigtype == PGPSIGTYPE_BINARY || sigp->sigtype == PGP_SIGTYPE_TEXT)) {
1199 size_t nb = dig->nbytes + sigp->hashlen;
1204 memcpy(trailer+2, &nb, sizeof(nb));
1205 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1209 xx = rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 0);
1210 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
1211 rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
1213 /* Compare leading 16 bits of digest for quick check. */
1214 if (memcmp(dig->md5, sigp->signhash16, 2)) {
1218 digest.type = siBuffer;
1219 digest.data = dig->md5;
1220 digest.len = dig->md5len;
1223 /* Retrieve the matching public key. */
1224 res = rpmtsFindPubkey(ts);
1225 if (res != RPMRC_OK)
1228 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1229 if (VFY_VerifyDigest(&digest, dig->rsa, dig->rsasig, sigalg, NULL) == SECSuccess)
1233 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1236 t = stpcpy(t, rpmSigString(res));
1238 t = stpcpy(t, ", key ID ");
1239 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1242 t = stpcpy(t, "\n");
1247 * Verify DSA signature.
1248 * @param ts transaction set
1249 * @retval t verbose success/failure text
1251 * @return RPMRC_OK on success
1254 verifyDSASignature(rpmts ts, char * t,
1257 rpm_constdata_t sig = rpmtsSig(ts);
1259 size_t siglen = rpmtsSiglen(ts);
1261 rpmSigTag sigtag = rpmtsSigtag(ts);
1262 pgpDig dig = rpmtsDig(ts);
1263 pgpDigParams sigp = rpmtsSignature(ts);
1269 if (dig != NULL && dig->hdrsha1ctx == sha1ctx)
1270 t = stpcpy(t, _("Header "));
1272 switch (sigp->version) {
1273 case 3: *t++ = '3'; break;
1274 case 4: *t++ = '4'; break;
1276 t = stpcpy(t, _(" DSA signature: "));
1278 if (sha1ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1283 /* XXX sanity check on sigtag and signature agreement. */
1284 if (!((sigtag == RPMSIGTAG_GPG || sigtag == RPMSIGTAG_DSA)
1285 && sigp->pubkey_algo == PGPPUBKEYALGO_DSA
1286 && sigp->hash_algo == PGPHASHALGO_SHA1))
1292 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1293 { DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
1295 if (sigp->hash != NULL)
1296 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1298 if (sigp->version == 4) {
1299 size_t nb = sigp->hashlen;
1302 trailer[0] = sigp->version;
1304 memcpy(trailer+2, &nb, sizeof(nb));
1305 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1307 xx = rpmDigestFinal(ctx, (void **)&dig->sha1, &dig->sha1len, 0);
1308 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
1309 rpmtsOp(ts, RPMTS_OP_DIGEST)->count--; /* XXX one too many */
1311 /* Compare leading 16 bits of digest for quick check. */
1312 if (memcmp(dig->sha1, sigp->signhash16, 2)) {
1316 digest.type = siBuffer;
1317 digest.data = dig->sha1;
1318 digest.len = dig->sha1len;
1321 /* Retrieve the matching public key. */
1322 res = rpmtsFindPubkey(ts);
1323 if (res != RPMRC_OK)
1326 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1327 if (VFY_VerifyDigest(&digest, dig->dsa, dig->dsasig,
1328 SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST, NULL) == SECSuccess)
1332 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1335 t = stpcpy(t, rpmSigString(res));
1337 t = stpcpy(t, ", key ID ");
1338 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1341 t = stpcpy(t, "\n");
1346 rpmVerifySignature(const rpmts ts, char * result)
1348 rpm_constdata_t sig = rpmtsSig(ts);
1349 size_t siglen = rpmtsSiglen(ts);
1350 rpmSigTag sigtag = rpmtsSigtag(ts);
1351 pgpDig dig = rpmtsDig(ts);
1354 if (sig == NULL || siglen <= 0 || dig == NULL) {
1355 sprintf(result, _("Verify signature: BAD PARAMETERS\n"));
1356 return RPMRC_NOTFOUND;
1360 case RPMSIGTAG_SIZE:
1361 res = verifySizeSignature(ts, result);
1364 res = verifyMD5Signature(ts, result, dig->md5ctx);
1366 case RPMSIGTAG_SHA1:
1367 res = verifySHA1Signature(ts, result, dig->hdrsha1ctx);
1370 res = verifyRSASignature(ts, result, dig->hdrmd5ctx);
1372 case RPMSIGTAG_PGP5: /* XXX legacy */
1374 res = verifyRSASignature(ts, result,
1375 ((dig->signature.hash_algo == PGPHASHALGO_MD5)
1376 ? dig->md5ctx : dig->sha1ctx));
1379 res = verifyDSASignature(ts, result, dig->hdrsha1ctx);
1382 res = verifyDSASignature(ts, result, dig->sha1ctx);
1384 case RPMSIGTAG_LEMD5_1:
1385 case RPMSIGTAG_LEMD5_2:
1386 sprintf(result, _("Broken MD5 digest: UNSUPPORTED\n"));
1387 res = RPMRC_NOTFOUND;
1390 sprintf(result, _("Signature: UNKNOWN (%d)\n"), sigtag);
1391 res = RPMRC_NOTFOUND;