2 * \file lib/signature.c
7 #include "rpmio_internal.h"
9 #include <rpmmacro.h> /* XXX for rpmGetPath() */
14 #include "misc.h" /* XXX for dosetenv() and makeTempFile() */
15 #include "legacy.h" /* XXX for mdbinfile() */
17 #include "signature.h"
18 #include "header_internal.h"
21 /*@access FD_t@*/ /* XXX ufdio->read arg1 is void ptr */
22 /*@access Header@*/ /* XXX compared with NULL */
23 /*@access entryInfo @*/ /* XXX rpmReadSignature */
24 /*@access indexEntry @*/ /* XXX rpmReadSignature */
25 /*@access DIGEST_CTX@*/ /* XXX compared with NULL */
27 /*@access pgpDigParams@*/
29 #if !defined(__GLIBC__)
30 char ** environ = NULL;
33 int rpmLookupSignatureType(int action)
36 static int disabled = 0;
40 case RPMLOOKUPSIG_DISABLE:
43 case RPMLOOKUPSIG_ENABLE:
46 case RPMLOOKUPSIG_QUERY:
50 { const char *name = rpmExpand("%{?_signature}", NULL);
51 if (!(name && *name != '\0'))
53 else if (!xstrcasecmp(name, "none"))
55 else if (!xstrcasecmp(name, "pgp"))
57 else if (!xstrcasecmp(name, "pgp5")) /* XXX legacy */
59 else if (!xstrcasecmp(name, "gpg"))
62 rc = -1; /* Invalid %_signature spec in macro file */
70 /* rpmDetectPGPVersion() returns the absolute path to the "pgp" */
71 /* executable of the requested version, or NULL when none found. */
73 const char * rpmDetectPGPVersion(pgpVersion * pgpVer)
75 /* Actually this should support having more then one pgp version. */
76 /* At the moment only one version is possible since we only */
77 /* have one %_pgpbin and one %_pgp_path. */
79 static pgpVersion saved_pgp_version = PGP_UNKNOWN;
80 const char *pgpbin = rpmGetPath("%{?_pgpbin}", NULL);
82 if (saved_pgp_version == PGP_UNKNOWN) {
87 if (!(pgpbin && pgpbin[0] != '\0')) {
88 pgpbin = _free(pgpbin);
89 saved_pgp_version = -1;
94 pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
95 (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
98 if (stat(pgpvbin, &st) == 0)
99 saved_pgp_version = PGP_5;
100 else if (stat(pgpbin, &st) == 0)
101 saved_pgp_version = PGP_2;
103 saved_pgp_version = PGP_NOTDETECTED;
107 if (pgpVer && pgpbin)
108 *pgpVer = saved_pgp_version;
114 * Print package size.
115 * @todo rpmio: use fdSize rather than fstat(2) to get file size.
116 * @param fd package file handle
117 * @param siglen signature header size
118 * @param pad signature padding
119 * @param datalen length of header+payload
120 * @return rpmRC return code
122 static inline rpmRC printSize(FD_t fd, int siglen, int pad, int datalen)
123 /*@globals fileSystem @*/
124 /*@modifies fileSystem @*/
128 if (fstat(Fileno(fd), &st) < 0)
132 rpmMessage(RPMMESS_DEBUG,
133 _("Expected size: %12d = lead(%d)+sigs(%d)+pad(%d)+data(%d)\n"),
134 (int)sizeof(struct rpmlead)+siglen+pad+datalen,
135 (int)sizeof(struct rpmlead), siglen, pad, datalen);
137 rpmMessage(RPMMESS_DEBUG,
138 _(" Actual size: %12d\n"), (int)st.st_size);
144 static unsigned char header_magic[8] = {
145 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
148 rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type,
159 indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
160 entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
161 unsigned char * dataStart;
162 unsigned char * dataEnd = NULL;
164 rpmRC rc = RPMRC_FAIL; /* assume failure */
175 if (sig_type != RPMSIGTYPE_HEADERSIG)
178 memset(block, 0, sizeof(block));
179 if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
180 (void) snprintf(buf, sizeof(buf),
181 _("sigh size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
184 if (memcmp(block, header_magic, sizeof(header_magic))) {
185 (void) snprintf(buf, sizeof(buf),
186 _("sigh magic: BAD\n"));
190 il = ntohl(block[2]);
192 if (il < 0 || il > 32) {
193 (void) snprintf(buf, sizeof(buf),
194 _("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
198 dl = ntohl(block[3]);
200 if (dl < 0 || dl > 8192) {
201 (void) snprintf(buf, sizeof(buf),
202 _("sigh data: BAD, no. of bytes(%d) out of range\n"), dl);
207 nb = (il * sizeof(struct entryInfo_s)) + dl;
209 ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
213 pe = (entryInfo) &ei[2];
215 dataStart = (unsigned char *) (pe + il);
216 if ((xx = timedRead(fd, (char *)pe, nb)) != nb) {
217 (void) snprintf(buf, sizeof(buf),
218 _("sigh blob(%d): BAD, read returned %d\n"), (int)nb, xx);
222 /* Check (and convert) the 1st tag element. */
223 xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
225 (void) snprintf(buf, sizeof(buf),
226 _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
227 0, entry->info.tag, entry->info.type,
228 entry->info.offset, entry->info.count);
232 /* Is there an immutable header region tag? */
234 if (entry->info.tag == RPMTAG_HEADERSIGNATURES
235 && entry->info.type == RPM_BIN_TYPE
236 && entry->info.count == REGION_TAG_COUNT)
240 if (entry->info.offset >= dl) {
241 (void) snprintf(buf, sizeof(buf),
242 _("region offset: BAD, tag %d type %d offset %d count %d\n"),
243 entry->info.tag, entry->info.type,
244 entry->info.offset, entry->info.count);
248 /* Is there an immutable header region tag trailer? */
249 dataEnd = dataStart + entry->info.offset;
252 (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
254 dataEnd += REGION_TAG_COUNT;
256 xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
258 !(entry->info.tag == RPMTAG_HEADERSIGNATURES
259 && entry->info.type == RPM_BIN_TYPE
260 && entry->info.count == REGION_TAG_COUNT))
262 (void) snprintf(buf, sizeof(buf),
263 _("region trailer: BAD, tag %d type %d offset %d count %d\n"),
264 entry->info.tag, entry->info.type,
265 entry->info.offset, entry->info.count);
270 memset(info, 0, sizeof(*info));
273 /* Is the no. of tags in the region less than the total no. of tags? */
274 ril = entry->info.offset/sizeof(*pe);
275 if ((entry->info.offset % sizeof(*pe)) || ril > il) {
276 (void) snprintf(buf, sizeof(buf),
277 _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
282 /* Sanity check signature tags */
284 memset(info, 0, sizeof(*info));
286 for (i = 1; i < il; i++) {
287 xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
289 (void) snprintf(buf, sizeof(buf),
290 _("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
291 i, entry->info.tag, entry->info.type,
292 entry->info.offset, entry->info.count);
297 /* OK, blob looks sane, load the header. */
298 sigh = headerLoad(ei);
300 (void) snprintf(buf, sizeof(buf), _("sigh load: BAD\n"));
303 sigh->flags |= HEADERFLAG_ALLOCATED;
305 { int sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
306 int pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
307 int_32 * archSize = NULL;
309 /* Position at beginning of header. */
310 if (pad && (xx = timedRead(fd, (char *)block, pad)) != pad) {
311 (void) snprintf(buf, sizeof(buf),
312 _("sigh pad(%d): BAD, read %d bytes\n"), pad, xx);
316 /* Print package component sizes. */
317 if (headerGetEntry(sigh, RPMSIGTAG_SIZE, NULL,(void **)&archSize, NULL))
318 rc = printSize(fd, sigSize, pad, *archSize);
323 if (sighp && sigh && rc == RPMRC_OK)
324 *sighp = headerLink(sigh);
325 sigh = headerFree(sigh);
328 buf[sizeof(buf)-1] = '\0';
336 int rpmWriteSignature(FD_t fd, Header h)
338 static byte buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
342 rc = headerWrite(fd, h, HEADER_MAGIC_YES);
346 sigSize = headerSizeof(h, HEADER_MAGIC_YES);
347 pad = (8 - (sigSize % 8)) % 8;
350 if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
354 rpmMessage(RPMMESS_DEBUG, _("Signature: size(%d)+pad(%d)\n"), sigSize, pad);
358 Header rpmNewSignature(void)
360 Header h = headerNew();
364 Header rpmFreeSignature(Header h)
366 return headerFree(h);
370 * Generate PGP (aka RSA/MD5) signature(s) for a header+payload file.
371 * @param file header+payload file name
372 * @retval pkt signature packet(s)
373 * @retval pktlen signature packet(s) length
374 * @param passPhrase private key pass phrase
375 * @return 0 on success, 1 on failure
377 static int makePGPSignature(const char * file, /*@out@*/ byte ** pkt,
378 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
379 /*@globals errno, rpmGlobalMacroContext,
380 fileSystem, internalState @*/
381 /*@modifies errno, *pkt, *pktlen, rpmGlobalMacroContext,
382 fileSystem, internalState @*/
384 char * sigfile = alloca(1024);
393 (void) stpcpy( stpcpy(sigfile, file), ".sig");
396 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
397 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
399 inpipe[0] = inpipe[1] = 0;
404 if (!(pid = fork())) {
405 const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
409 (void) close(STDIN_FILENO);
410 (void) dup2(inpipe[0], 3);
411 (void) close(inpipe[1]);
413 (void) dosetenv("PGPPASSFD", "3", 1);
415 if (pgp_path && *pgp_path != '\0')
416 (void) dosetenv("PGPPATH", pgp_path, 1);
419 /* dosetenv("PGPPASS", passPhrase, 1); */
421 unsetenv("MALLOC_CHECK_");
422 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
425 cmd = rpmExpand("%{?__pgp_sign_cmd}", NULL);
426 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
429 rc = execve(av[0], av+1, environ);
433 cmd = rpmExpand("%{?__pgp5_sign_cmd}", NULL);
434 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
437 rc = execve(av[0], av+1, environ);
441 case PGP_NOTDETECTED:
446 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
451 delMacro(NULL, "__plaintext_filename");
452 delMacro(NULL, "__signature_filename");
454 (void) close(inpipe[0]);
456 (void) write(inpipe[1], passPhrase, strlen(passPhrase));
457 (void) write(inpipe[1], "\n", 1);
458 (void) close(inpipe[1]);
460 (void)waitpid(pid, &status, 0);
461 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
462 rpmError(RPMERR_SIGGEN, _("pgp failed\n"));
466 if (stat(sigfile, &st)) {
467 /* PGP failed to write signature */
468 if (sigfile) (void) unlink(sigfile); /* Just in case */
469 rpmError(RPMERR_SIGGEN, _("pgp failed to write signature\n"));
474 *pktlen = st.st_size;
475 rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *pktlen);
476 *pkt = xmalloc(*pktlen);
483 fd = Fopen(sigfile, "r.fdio");
484 if (fd != NULL && !Ferror(fd)) {
485 rc = timedRead(fd, *pkt, *pktlen);
486 if (sigfile) (void) unlink(sigfile);
493 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
498 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *pktlen);
505 * Generate GPG (aka DSA) signature(s) for a header+payload file.
506 * @param file header+payload file name
507 * @retval pkt signature packet(s)
508 * @retval pktlen signature packet(s) length
509 * @param passPhrase private key pass phrase
510 * @return 0 on success, 1 on failure
512 static int makeGPGSignature(const char * file, /*@out@*/ byte ** pkt,
513 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
514 /*@globals rpmGlobalMacroContext,
515 fileSystem, internalState @*/
516 /*@modifies *pkt, *pktlen, rpmGlobalMacroContext,
517 fileSystem, internalState @*/
519 char * sigfile = alloca(1024);
529 (void) stpcpy( stpcpy(sigfile, file), ".sig");
532 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
533 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
535 inpipe[0] = inpipe[1] = 0;
540 if (!(pid = fork())) {
541 const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
543 (void) close(STDIN_FILENO);
544 (void) dup2(inpipe[0], 3);
545 (void) close(inpipe[1]);
548 if (gpg_path && *gpg_path != '\0')
549 (void) dosetenv("GNUPGHOME", gpg_path, 1);
552 unsetenv("MALLOC_CHECK_");
553 cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
554 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
557 rc = execve(av[0], av+1, environ);
560 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
565 delMacro(NULL, "__plaintext_filename");
566 delMacro(NULL, "__signature_filename");
568 fpipe = fdopen(inpipe[1], "w");
569 (void) close(inpipe[0]);
571 fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
572 (void) fclose(fpipe);
575 (void) waitpid(pid, &status, 0);
576 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
577 rpmError(RPMERR_SIGGEN, _("gpg failed\n"));
581 if (stat(sigfile, &st)) {
582 /* GPG failed to write signature */
583 if (sigfile) (void) unlink(sigfile); /* Just in case */
584 rpmError(RPMERR_SIGGEN, _("gpg failed to write signature\n"));
589 *pktlen = st.st_size;
590 rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *pktlen);
591 *pkt = xmalloc(*pktlen);
598 fd = Fopen(sigfile, "r.fdio");
599 if (fd != NULL && !Ferror(fd)) {
600 rc = timedRead(fd, *pkt, *pktlen);
601 if (sigfile) (void) unlink(sigfile);
608 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
613 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *pktlen);
620 * Generate header only signature(s) from a header+payload file.
621 * @param sig signature header
622 * @param file header+payload file name
623 * @param sigTag type of signature(s) to add
624 * @param passPhrase private key pass phrase
625 * @return 0 on success, -1 on failure
627 static int makeHDRSignature(Header sig, const char * file, int_32 sigTag,
628 /*@null@*/ const char * passPhrase)
629 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
630 /*@modifies sig, rpmGlobalMacroContext, fileSystem, internalState @*/
636 const char * fn = NULL;
637 const char * SHA1 = NULL;
638 int ret = -1; /* assume failure. */
643 case RPMSIGTAG_PGP5: /* XXX legacy */
647 /*@notreached@*/ break;
649 fd = Fopen(file, "r.fdio");
650 if (fd == NULL || Ferror(fd))
652 h = headerRead(fd, HEADER_MAGIC_YES);
655 (void) Fclose(fd); fd = NULL;
657 if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
662 if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)
668 ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
669 (void) rpmDigestUpdate(ctx, header_magic, sizeof(header_magic));
670 (void) rpmDigestUpdate(ctx, uh, uhc);
671 (void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
672 uh = headerFreeData(uh, uht);
678 if (!headerAddEntry(sig, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
683 fd = Fopen(file, "r.fdio");
684 if (fd == NULL || Ferror(fd))
686 h = headerRead(fd, HEADER_MAGIC_YES);
689 (void) Fclose(fd); fd = NULL;
690 if (makeTempFile(NULL, &fn, &fd))
692 if (headerWrite(fd, h, HEADER_MAGIC_YES))
694 (void) Fclose(fd); fd = NULL;
695 if (makeGPGSignature(fn, &pkt, &pktlen, passPhrase)
696 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
701 fd = Fopen(file, "r.fdio");
702 if (fd == NULL || Ferror(fd))
704 h = headerRead(fd, HEADER_MAGIC_YES);
707 (void) Fclose(fd); fd = NULL;
708 if (makeTempFile(NULL, &fn, &fd))
710 if (headerWrite(fd, h, HEADER_MAGIC_YES))
712 (void) Fclose(fd); fd = NULL;
713 if (makePGPSignature(fn, &pkt, &pktlen, passPhrase)
714 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
727 if (fd != NULL) (void) Fclose(fd);
731 int rpmAddSignature(Header sig, const char * file, int_32 sigTag,
732 const char * passPhrase)
737 int ret = -1; /* assume failure. */
741 if (stat(file, &st) != 0)
744 if (!headerAddEntry(sig, sigTag, RPM_INT32_TYPE, &pktlen, 1))
750 pkt = xcalloc(1, pktlen);
751 if (domd5(file, pkt, 0, NULL)
752 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
756 case RPMSIGTAG_PGP5: /* XXX legacy */
758 if (makePGPSignature(file, &pkt, &pktlen, passPhrase)
759 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
761 #ifdef NOTYET /* XXX needs hdrmd5ctx, like hdrsha1ctx. */
762 /* XXX Piggyback a header-only RSA signature as well. */
763 ret = makeHDRSignature(sig, file, RPMSIGTAG_RSA, passPhrase);
768 if (makeGPGSignature(file, &pkt, &pktlen, passPhrase)
769 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
771 /* XXX Piggyback a header-only DSA signature as well. */
772 ret = makeHDRSignature(sig, file, RPMSIGTAG_DSA, passPhrase);
777 ret = makeHDRSignature(sig, file, sigTag, passPhrase);
784 static int checkPassPhrase(const char * passPhrase, const int sigTag)
785 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
786 /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
788 int passPhrasePipe[2];
793 passPhrasePipe[0] = passPhrasePipe[1] = 0;
795 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);
824 if (gpg_path && *gpg_path != '\0')
825 (void) dosetenv("GNUPGHOME", gpg_path, 1);
828 cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
829 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
832 rc = execve(av[0], av+1, environ);
835 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
837 } /*@notreached@*/ break;
839 case RPMSIGTAG_PGP5: /* XXX legacy */
841 { const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
845 (void) dosetenv("PGPPASSFD", "3", 1);
847 if (pgp_path && *pgp_path != '\0')
848 xx = dosetenv("PGPPATH", pgp_path, 1);
851 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
854 cmd = rpmExpand("%{?__pgp_check_password_cmd}", NULL);
855 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
858 rc = execve(av[0], av+1, environ);
860 /*@innerbreak@*/ break;
861 case PGP_5: /* XXX legacy */
862 cmd = rpmExpand("%{?__pgp5_check_password_cmd}", NULL);
863 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
866 rc = execve(av[0], av+1, environ);
868 /*@innerbreak@*/ break;
870 case PGP_NOTDETECTED:
871 /*@innerbreak@*/ break;
874 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
877 } /*@notreached@*/ break;
878 default: /* This case should have been screened out long ago. */
879 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
880 _exit(RPMERR_SIGGEN);
881 /*@notreached@*/ break;
885 xx = close(passPhrasePipe[0]);
886 xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
887 xx = write(passPhrasePipe[1], "\n", 1);
888 xx = close(passPhrasePipe[1]);
890 (void) waitpid(pid, &status, 0);
892 return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
895 char * rpmGetPassPhrase(const char * prompt, const int sigTag)
904 { const char *name = rpmExpand("%{?_gpg_name}", NULL);
905 aok = (name && *name != '\0');
910 rpmError(RPMERR_SIGGEN,
911 _("You must set \"%%_gpg_name\" in your macro file\n"));
916 case RPMSIGTAG_PGP5: /* XXX legacy */
919 { const char *name = rpmExpand("%{?_pgp_name}", NULL);
920 aok = (name && *name != '\0');
925 rpmError(RPMERR_SIGGEN,
926 _("You must set \"%%_pgp_name\" in your macro file\n"));
931 /* Currently the calling function (rpm.c:main) is checking this and
932 * doing a better job. This section should never be accessed.
934 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
936 /*@notreached@*/ break;
939 /*@-moduncon -nullpass @*/
940 pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
941 /*@=moduncon -nullpass @*/
943 if (checkPassPhrase(pass, sigTag))
949 static /*@observer@*/ const char * rpmSigString(rpmRC res)
954 case RPMRC_OK: str = "OK"; break;
955 case RPMRC_FAIL: str = "BAD"; break;
956 case RPMRC_NOKEY: str = "NOKEY"; break;
957 case RPMRC_NOTTRUSTED: str = "NOTRUSTED"; break;
959 case RPMRC_NOTFOUND: str = "UNKNOWN"; break;
966 verifySizeSignature(const rpmts ts, /*@out@*/ char * t)
969 const void * sig = rpmtsSig(ts);
970 pgpDig dig = rpmtsDig(ts);
972 int_32 size = 0x7fffffff;
975 t = stpcpy(t, _("Header+Payload size: "));
977 if (sig == NULL || dig == NULL || dig->nbytes == 0) {
979 t = stpcpy(t, rpmSigString(res));
983 memcpy(&size, sig, sizeof(size));
985 if (size != dig->nbytes) {
987 t = stpcpy(t, rpmSigString(res));
988 sprintf(t, " Expected(%d) != (%d)\n", (int)size, (int)dig->nbytes);
991 t = stpcpy(t, rpmSigString(res));
992 sprintf(t, " (%d)", (int)dig->nbytes);
1003 verifyMD5Signature(const rpmts ts, /*@out@*/ char * t,
1004 /*@null@*/ DIGEST_CTX md5ctx)
1005 /*@globals internalState @*/
1006 /*@modifies *t, internalState @*/
1008 const void * sig = rpmtsSig(ts);
1009 int_32 siglen = rpmtsSiglen(ts);
1010 pgpDig dig = rpmtsDig(ts);
1012 byte * md5sum = NULL;
1016 t = stpcpy(t, _("MD5 digest: "));
1018 if (md5ctx == NULL || sig == NULL || dig == NULL) {
1020 t = stpcpy(t, rpmSigString(res));
1024 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1025 (void) rpmDigestFinal(rpmDigestDup(md5ctx),
1026 (void **)&md5sum, &md5len, 0);
1027 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1029 if (md5len != siglen || memcmp(md5sum, sig, md5len)) {
1031 t = stpcpy(t, rpmSigString(res));
1032 t = stpcpy(t, " Expected(");
1033 (void) pgpHexCvt(t, sig, siglen);
1035 t = stpcpy(t, ") != (");
1038 t = stpcpy(t, rpmSigString(res));
1039 t = stpcpy(t, " (");
1041 (void) pgpHexCvt(t, md5sum, md5len);
1046 md5sum = _free(md5sum);
1047 t = stpcpy(t, "\n");
1054 * Verify header immutable region SHA1 digest.
1055 * @param ts transaction set
1056 * @retval t verbose success/failure text
1058 * @return RPMRC_OK on success
1061 verifySHA1Signature(const rpmts ts, /*@out@*/ char * t,
1062 /*@null@*/ DIGEST_CTX sha1ctx)
1063 /*@globals internalState @*/
1064 /*@modifies *t, internalState @*/
1066 const void * sig = rpmtsSig(ts);
1068 int_32 siglen = rpmtsSiglen(ts);
1070 pgpDig dig = rpmtsDig(ts);
1072 const char * SHA1 = NULL;
1075 t = stpcpy(t, _("Header SHA1 digest: "));
1077 if (sha1ctx == NULL || sig == NULL || dig == NULL) {
1079 t = stpcpy(t, rpmSigString(res));
1083 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1084 (void) rpmDigestFinal(rpmDigestDup(sha1ctx),
1085 (void **)&SHA1, NULL, 1);
1086 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1088 if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
1090 t = stpcpy(t, rpmSigString(res));
1091 t = stpcpy(t, " Expected(");
1093 t = stpcpy(t, ") != (");
1096 t = stpcpy(t, rpmSigString(res));
1097 t = stpcpy(t, " (");
1100 t = stpcpy(t, SHA1);
1105 t = stpcpy(t, "\n");
1111 * Convert hex to binary nibble.
1112 * @param c hex character
1113 * @return binary nibble
1115 static inline unsigned char nibble(char c)
1118 if (c >= '0' && c <= '9')
1120 if (c >= 'A' && c <= 'F')
1121 return (c - 'A') + 10;
1122 if (c >= 'a' && c <= 'f')
1123 return (c - 'a') + 10;
1129 * Verify PGP (aka RSA/MD5) signature.
1130 * @param ts transaction set
1131 * @retval t verbose success/failure text
1133 * @return RPMRC_OK on success
1136 verifyPGPSignature(rpmts ts, /*@out@*/ char * t,
1137 /*@null@*/ DIGEST_CTX md5ctx)
1138 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1139 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1141 const void * sig = rpmtsSig(ts);
1143 int_32 siglen = rpmtsSiglen(ts);
1145 int_32 sigtag = rpmtsSigtag(ts);
1146 pgpDig dig = rpmtsDig(ts);
1147 pgpDigParams sigp = rpmtsSignature(ts);
1152 t = stpcpy(t, _("V3 RSA/MD5 signature: "));
1154 if (md5ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1159 /* XXX sanity check on sigtag and signature agreement. */
1160 if (!(sigtag == RPMSIGTAG_PGP
1161 && sigp->pubkey_algo == PGPPUBKEYALGO_RSA
1162 && sigp->hash_algo == PGPHASHALGO_MD5))
1168 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1169 { DIGEST_CTX ctx = rpmDigestDup(md5ctx);
1173 if (sigp->hash != NULL)
1174 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1176 #ifdef NOTYET /* XXX not for binary/text document signatures. */
1177 if (sigp->sigtype == 4) {
1178 int nb = dig->nbytes + sigp->hashlen;
1183 memcpy(trailer+2, &nb, sizeof(nb));
1184 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1188 xx = rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 1);
1189 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
1191 /* Compare leading 16 bits of digest for quick check. */
1193 signhash16[0] = (nibble(s[0]) << 4) | nibble(s[1]);
1194 signhash16[1] = (nibble(s[2]) << 4) | nibble(s[3]);
1195 if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
1202 { const char * prefix = "3020300c06082a864886f70d020505000410";
1203 unsigned int nbits = 1024;
1204 unsigned int nb = (nbits + 7) >> 3;
1205 const char * hexstr;
1208 hexstr = tt = xmalloc(2 * nb + 1);
1209 memset(tt, 'f', (2 * nb));
1210 tt[0] = '0'; tt[1] = '0';
1211 tt[2] = '0'; tt[3] = '1';
1212 tt += (2 * nb) - strlen(prefix) - strlen(dig->md5) - 2;
1213 *tt++ = '0'; *tt++ = '0';
1214 tt = stpcpy(tt, prefix);
1215 tt = stpcpy(tt, dig->md5);
1217 mp32nzero(&dig->rsahm); mp32nsethex(&dig->rsahm, hexstr);
1219 hexstr = _free(hexstr);
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 (rsavrfy(&dig->rsa_pk, &dig->rsahm, &dig->c))
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");
1248 * Verify GPG (aka DSA) signature.
1249 * @param ts transaction set
1250 * @retval t verbose success/failure text
1252 * @return RPMRC_OK on success
1256 verifyGPGSignature(rpmts ts, /*@out@*/ char * t,
1257 /*@null@*/ DIGEST_CTX sha1ctx)
1258 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1259 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1261 const void * sig = rpmtsSig(ts);
1263 int_32 siglen = rpmtsSiglen(ts);
1265 int_32 sigtag = rpmtsSigtag(ts);
1266 pgpDig dig = rpmtsDig(ts);
1267 pgpDigParams sigp = rpmtsSignature(ts);
1272 if (dig != NULL && dig->hdrsha1ctx == sha1ctx)
1273 t = stpcpy(t, _("Header "));
1274 t = stpcpy(t, _("V3 DSA signature: "));
1276 if (sha1ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1281 /* XXX sanity check on sigtag and signature agreement. */
1282 if (!((sigtag == RPMSIGTAG_GPG || sigtag == RPMSIGTAG_DSA)
1283 && sigp->pubkey_algo == PGPPUBKEYALGO_DSA
1284 && sigp->hash_algo == PGPHASHALGO_SHA1))
1290 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
1291 { DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
1294 if (sigp->hash != NULL)
1295 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1297 #ifdef NOTYET /* XXX not for binary/text document signatures. */
1298 if (sigp->sigtype == 4) {
1299 int nb = dig->nbytes + sigp->hashlen;
1304 memcpy(trailer+2, &nb, sizeof(nb));
1305 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1308 xx = rpmDigestFinal(ctx, (void **)&dig->sha1, &dig->sha1len, 1);
1309 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
1311 mp32nzero(&dig->hm); mp32nsethex(&dig->hm, dig->sha1);
1313 /* Compare leading 16 bits of digest for quick check. */
1314 signhash16[0] = (*dig->hm.data >> 24) & 0xff;
1315 signhash16[1] = (*dig->hm.data >> 16) & 0xff;
1316 if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
1322 /* Retrieve the matching public key. */
1323 res = rpmtsFindPubkey(ts);
1324 if (res != RPMRC_OK)
1327 (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1328 if (dsavrfy(&dig->p, &dig->q, &dig->g,
1329 &dig->hm, &dig->y, &dig->r, &dig->s))
1333 (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
1336 t = stpcpy(t, rpmSigString(res));
1338 t = stpcpy(t, ", key ID ");
1339 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1342 t = stpcpy(t, "\n");
1348 rpmVerifySignature(const rpmts ts, char * result)
1350 const void * sig = rpmtsSig(ts);
1351 int_32 siglen = rpmtsSiglen(ts);
1352 int_32 sigtag = rpmtsSigtag(ts);
1353 pgpDig dig = rpmtsDig(ts);
1356 if (sig == NULL || siglen <= 0 || dig == NULL) {
1357 sprintf(result, _("Verify signature: BAD PARAMETERS\n"));
1358 return RPMRC_NOTFOUND;
1362 case RPMSIGTAG_SIZE:
1363 res = verifySizeSignature(ts, result);
1366 res = verifyMD5Signature(ts, result, dig->md5ctx);
1368 case RPMSIGTAG_SHA1:
1369 res = verifySHA1Signature(ts, result, dig->hdrsha1ctx);
1372 case RPMSIGTAG_PGP5: /* XXX legacy */
1374 res = verifyPGPSignature(ts, result, dig->md5ctx);
1377 res = verifyGPGSignature(ts, result, dig->hdrsha1ctx);
1380 res = verifyGPGSignature(ts, result, dig->sha1ctx);
1382 case RPMSIGTAG_LEMD5_1:
1383 case RPMSIGTAG_LEMD5_2:
1384 sprintf(result, _("Broken MD5 digest: UNSUPPORTED\n"));
1385 res = RPMRC_NOTFOUND;
1388 sprintf(result, _("Signature: UNKNOWN (%d)\n"), sigtag);
1389 res = RPMRC_NOTFOUND;