2 * \file lib/signature.c
7 #include "rpmio_internal.h"
9 #include <rpmmacro.h> /* XXX for rpmGetPath() */
12 #define _RPMTS_INTERNAL
15 #include "misc.h" /* XXX for dosetenv() and makeTempFile() */
16 #include "legacy.h" /* XXX for mdbinfile() */
18 #include "signature.h"
19 #include "header_internal.h"
22 /*@access FD_t@*/ /* XXX ufdio->read arg1 is void ptr */
23 /*@access Header@*/ /* XXX compared with NULL */
24 /*@access entryInfo @*/ /* XXX rpmReadSignature */
25 /*@access indexEntry @*/ /* XXX rpmReadSignature */
26 /*@access DIGEST_CTX@*/ /* XXX compared with NULL */
28 /*@access pgpDigParams@*/
30 #if !defined(__GLIBC__)
31 char ** environ = NULL;
34 int rpmLookupSignatureType(int action)
37 static int disabled = 0;
41 case RPMLOOKUPSIG_DISABLE:
44 case RPMLOOKUPSIG_ENABLE:
47 case RPMLOOKUPSIG_QUERY:
51 { const char *name = rpmExpand("%{?_signature}", NULL);
52 if (!(name && *name != '\0'))
54 else if (!xstrcasecmp(name, "none"))
56 else if (!xstrcasecmp(name, "pgp"))
58 else if (!xstrcasecmp(name, "pgp5")) /* XXX legacy */
60 else if (!xstrcasecmp(name, "gpg"))
63 rc = -1; /* Invalid %_signature spec in macro file */
71 /* rpmDetectPGPVersion() returns the absolute path to the "pgp" */
72 /* executable of the requested version, or NULL when none found. */
74 const char * rpmDetectPGPVersion(pgpVersion * pgpVer)
76 /* Actually this should support having more then one pgp version. */
77 /* At the moment only one version is possible since we only */
78 /* have one %_pgpbin and one %_pgp_path. */
80 static pgpVersion saved_pgp_version = PGP_UNKNOWN;
81 const char *pgpbin = rpmGetPath("%{?_pgpbin}", NULL);
83 if (saved_pgp_version == PGP_UNKNOWN) {
88 if (!(pgpbin && pgpbin[0] != '\0')) {
89 pgpbin = _free(pgpbin);
90 saved_pgp_version = -1;
95 pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
96 (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
99 if (stat(pgpvbin, &st) == 0)
100 saved_pgp_version = PGP_5;
101 else if (stat(pgpbin, &st) == 0)
102 saved_pgp_version = PGP_2;
104 saved_pgp_version = PGP_NOTDETECTED;
108 if (pgpVer && pgpbin)
109 *pgpVer = saved_pgp_version;
115 * Print package size.
116 * @todo rpmio: use fdSize rather than fstat(2) to get file size.
117 * @param fd package file handle
118 * @param siglen signature header size
119 * @param pad signature padding
120 * @param datalen length of header+payload
121 * @return rpmRC return code
123 static inline rpmRC printSize(FD_t fd, int siglen, int pad, int datalen)
124 /*@globals fileSystem @*/
125 /*@modifies fileSystem @*/
129 if (fstat(Fileno(fd), &st) < 0)
133 rpmMessage(RPMMESS_DEBUG,
134 _("Expected size: %12d = lead(%d)+sigs(%d)+pad(%d)+data(%d)\n"),
135 (int)sizeof(struct rpmlead)+siglen+pad+datalen,
136 (int)sizeof(struct rpmlead), siglen, pad, datalen);
138 rpmMessage(RPMMESS_DEBUG,
139 _(" Actual size: %12d\n"), (int)st.st_size);
145 static unsigned char header_magic[8] = {
146 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
149 rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type,
160 indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
161 entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
162 unsigned char * dataStart;
163 unsigned char * dataEnd = NULL;
165 rpmRC rc = RPMRC_FAIL; /* assume failure */
176 if (sig_type != RPMSIGTYPE_HEADERSIG)
179 memset(block, 0, sizeof(block));
180 if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
181 (void) snprintf(buf, sizeof(buf),
182 _("sigh size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
185 if (memcmp(block, header_magic, sizeof(header_magic))) {
186 (void) snprintf(buf, sizeof(buf),
187 _("sigh magic: BAD\n"));
191 il = ntohl(block[2]);
193 if (il < 0 || il > 32) {
194 (void) snprintf(buf, sizeof(buf),
195 _("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
199 dl = ntohl(block[3]);
201 if (dl < 0 || dl > 8192) {
202 (void) snprintf(buf, sizeof(buf),
203 _("sigh data: BAD, no. of bytes(%d) out of range\n"), dl);
208 nb = (il * sizeof(struct entryInfo_s)) + dl;
210 ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
214 pe = (entryInfo) &ei[2];
216 dataStart = (unsigned char *) (pe + il);
217 if ((xx = timedRead(fd, (char *)pe, nb)) != nb) {
218 (void) snprintf(buf, sizeof(buf),
219 _("sigh blob(%d): BAD, read returned %d\n"), (int)nb, xx);
223 /* Check (and convert) the 1st tag element. */
224 xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
226 (void) snprintf(buf, sizeof(buf),
227 _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
228 0, entry->info.tag, entry->info.type,
229 entry->info.offset, entry->info.count);
233 /* Is there an immutable header region tag? */
235 if (entry->info.tag == RPMTAG_HEADERSIGNATURES
236 && entry->info.type == RPM_BIN_TYPE
237 && entry->info.count == REGION_TAG_COUNT)
241 if (entry->info.offset >= dl) {
242 (void) snprintf(buf, sizeof(buf),
243 _("region offset: BAD, tag %d type %d offset %d count %d\n"),
244 entry->info.tag, entry->info.type,
245 entry->info.offset, entry->info.count);
249 /* Is there an immutable header region tag trailer? */
250 dataEnd = dataStart + entry->info.offset;
253 (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
255 dataEnd += REGION_TAG_COUNT;
257 xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
259 !(entry->info.tag == RPMTAG_HEADERSIGNATURES
260 && entry->info.type == RPM_BIN_TYPE
261 && entry->info.count == REGION_TAG_COUNT))
263 (void) snprintf(buf, sizeof(buf),
264 _("region trailer: BAD, tag %d type %d offset %d count %d\n"),
265 entry->info.tag, entry->info.type,
266 entry->info.offset, entry->info.count);
271 memset(info, 0, sizeof(*info));
274 /* Is the no. of tags in the region less than the total no. of tags? */
275 ril = entry->info.offset/sizeof(*pe);
276 if ((entry->info.offset % sizeof(*pe)) || ril > il) {
277 (void) snprintf(buf, sizeof(buf),
278 _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
283 /* Sanity check signature tags */
285 memset(info, 0, sizeof(*info));
287 for (i = 1; i < il; i++) {
288 xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
290 (void) snprintf(buf, sizeof(buf),
291 _("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
292 i, entry->info.tag, entry->info.type,
293 entry->info.offset, entry->info.count);
298 /* OK, blob looks sane, load the header. */
299 sigh = headerLoad(ei);
301 (void) snprintf(buf, sizeof(buf), _("sigh load: BAD\n"));
304 sigh->flags |= HEADERFLAG_ALLOCATED;
306 { int sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
307 int pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
308 int_32 * archSize = NULL;
310 /* Position at beginning of header. */
311 if (pad && (xx = timedRead(fd, (char *)block, pad)) != pad) {
312 (void) snprintf(buf, sizeof(buf),
313 _("sigh pad(%d): BAD, read %d bytes\n"), pad, xx);
317 /* Print package component sizes. */
318 if (headerGetEntry(sigh, RPMSIGTAG_SIZE, NULL,(void **)&archSize, NULL))
319 rc = printSize(fd, sigSize, pad, *archSize);
324 if (sighp && sigh && rc == RPMRC_OK)
325 *sighp = headerLink(sigh);
326 sigh = headerFree(sigh);
329 buf[sizeof(buf)-1] = '\0';
337 int rpmWriteSignature(FD_t fd, Header h)
339 static byte buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
343 rc = headerWrite(fd, h, HEADER_MAGIC_YES);
347 sigSize = headerSizeof(h, HEADER_MAGIC_YES);
348 pad = (8 - (sigSize % 8)) % 8;
351 if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
355 rpmMessage(RPMMESS_DEBUG, _("Signature: size(%d)+pad(%d)\n"), sigSize, pad);
359 Header rpmNewSignature(void)
361 Header h = headerNew();
365 Header rpmFreeSignature(Header h)
367 return headerFree(h);
371 * Generate PGP (aka RSA/MD5) signature(s) for a header+payload file.
372 * @param file header+payload file name
373 * @retval pkt signature packet(s)
374 * @retval pktlen signature packet(s) length
375 * @param passPhrase private key pass phrase
376 * @return 0 on success, 1 on failure
378 static int makePGPSignature(const char * file, /*@out@*/ byte ** pkt,
379 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
380 /*@globals errno, rpmGlobalMacroContext,
381 fileSystem, internalState @*/
382 /*@modifies errno, *pkt, *pktlen, rpmGlobalMacroContext,
383 fileSystem, internalState @*/
385 char * sigfile = alloca(1024);
394 (void) stpcpy( stpcpy(sigfile, file), ".sig");
397 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
398 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
400 inpipe[0] = inpipe[1] = 0;
405 if (!(pid = fork())) {
406 const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
410 (void) close(STDIN_FILENO);
411 (void) dup2(inpipe[0], 3);
412 (void) close(inpipe[1]);
414 (void) dosetenv("PGPPASSFD", "3", 1);
416 if (pgp_path && *pgp_path != '\0')
417 (void) dosetenv("PGPPATH", pgp_path, 1);
420 /* dosetenv("PGPPASS", passPhrase, 1); */
422 unsetenv("MALLOC_CHECK_");
423 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
426 cmd = rpmExpand("%{?__pgp_sign_cmd}", NULL);
427 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
430 rc = execve(av[0], av+1, environ);
434 cmd = rpmExpand("%{?__pgp5_sign_cmd}", NULL);
435 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
438 rc = execve(av[0], av+1, environ);
442 case PGP_NOTDETECTED:
447 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
452 delMacro(NULL, "__plaintext_filename");
453 delMacro(NULL, "__signature_filename");
455 (void) close(inpipe[0]);
457 (void) write(inpipe[1], passPhrase, strlen(passPhrase));
458 (void) write(inpipe[1], "\n", 1);
459 (void) close(inpipe[1]);
461 (void)waitpid(pid, &status, 0);
462 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
463 rpmError(RPMERR_SIGGEN, _("pgp failed\n"));
467 if (stat(sigfile, &st)) {
468 /* PGP failed to write signature */
469 if (sigfile) (void) unlink(sigfile); /* Just in case */
470 rpmError(RPMERR_SIGGEN, _("pgp failed to write signature\n"));
475 *pktlen = st.st_size;
476 rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *pktlen);
477 *pkt = xmalloc(*pktlen);
484 fd = Fopen(sigfile, "r.fdio");
485 if (fd != NULL && !Ferror(fd)) {
486 rc = timedRead(fd, *pkt, *pktlen);
487 if (sigfile) (void) unlink(sigfile);
494 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
499 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *pktlen);
506 * Generate GPG (aka DSA) signature(s) for a header+payload file.
507 * @param file header+payload file name
508 * @retval pkt signature packet(s)
509 * @retval pktlen signature packet(s) length
510 * @param passPhrase private key pass phrase
511 * @return 0 on success, 1 on failure
513 static int makeGPGSignature(const char * file, /*@out@*/ byte ** pkt,
514 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
515 /*@globals rpmGlobalMacroContext,
516 fileSystem, internalState @*/
517 /*@modifies *pkt, *pktlen, rpmGlobalMacroContext,
518 fileSystem, internalState @*/
520 char * sigfile = alloca(1024);
530 (void) stpcpy( stpcpy(sigfile, file), ".sig");
533 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
534 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
536 inpipe[0] = inpipe[1] = 0;
541 if (!(pid = fork())) {
542 const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
544 (void) close(STDIN_FILENO);
545 (void) dup2(inpipe[0], 3);
546 (void) close(inpipe[1]);
549 if (gpg_path && *gpg_path != '\0')
550 (void) dosetenv("GNUPGHOME", gpg_path, 1);
553 unsetenv("MALLOC_CHECK_");
554 cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
555 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
558 rc = execve(av[0], av+1, environ);
561 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
566 delMacro(NULL, "__plaintext_filename");
567 delMacro(NULL, "__signature_filename");
569 fpipe = fdopen(inpipe[1], "w");
570 (void) close(inpipe[0]);
572 fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
573 (void) fclose(fpipe);
576 (void) waitpid(pid, &status, 0);
577 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
578 rpmError(RPMERR_SIGGEN, _("gpg failed\n"));
582 if (stat(sigfile, &st)) {
583 /* GPG failed to write signature */
584 if (sigfile) (void) unlink(sigfile); /* Just in case */
585 rpmError(RPMERR_SIGGEN, _("gpg failed to write signature\n"));
590 *pktlen = st.st_size;
591 rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *pktlen);
592 *pkt = xmalloc(*pktlen);
599 fd = Fopen(sigfile, "r.fdio");
600 if (fd != NULL && !Ferror(fd)) {
601 rc = timedRead(fd, *pkt, *pktlen);
602 if (sigfile) (void) unlink(sigfile);
609 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
614 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *pktlen);
621 * Generate header only signature(s) from a header+payload file.
622 * @param sig signature header
623 * @param file header+payload file name
624 * @param sigTag type of signature(s) to add
625 * @param passPhrase private key pass phrase
626 * @return 0 on success, -1 on failure
628 static int makeHDRSignature(Header sig, const char * file, int_32 sigTag,
629 /*@null@*/ const char * passPhrase)
630 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
631 /*@modifies sig, rpmGlobalMacroContext, fileSystem, internalState @*/
637 const char * fn = NULL;
638 const char * SHA1 = NULL;
639 int ret = -1; /* assume failure. */
644 case RPMSIGTAG_PGP5: /* XXX legacy */
648 /*@notreached@*/ break;
650 fd = Fopen(file, "r.fdio");
651 if (fd == NULL || Ferror(fd))
653 h = headerRead(fd, HEADER_MAGIC_YES);
656 (void) Fclose(fd); fd = NULL;
658 if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
663 if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)
669 ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
670 (void) rpmDigestUpdate(ctx, header_magic, sizeof(header_magic));
671 (void) rpmDigestUpdate(ctx, uh, uhc);
672 (void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
673 uh = headerFreeData(uh, uht);
679 if (!headerAddEntry(sig, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
684 fd = Fopen(file, "r.fdio");
685 if (fd == NULL || Ferror(fd))
687 h = headerRead(fd, HEADER_MAGIC_YES);
690 (void) Fclose(fd); fd = NULL;
691 if (makeTempFile(NULL, &fn, &fd))
693 if (headerWrite(fd, h, HEADER_MAGIC_YES))
695 (void) Fclose(fd); fd = NULL;
696 if (makeGPGSignature(fn, &pkt, &pktlen, passPhrase)
697 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
702 fd = Fopen(file, "r.fdio");
703 if (fd == NULL || Ferror(fd))
705 h = headerRead(fd, HEADER_MAGIC_YES);
708 (void) Fclose(fd); fd = NULL;
709 if (makeTempFile(NULL, &fn, &fd))
711 if (headerWrite(fd, h, HEADER_MAGIC_YES))
713 (void) Fclose(fd); fd = NULL;
714 if (makePGPSignature(fn, &pkt, &pktlen, passPhrase)
715 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
728 if (fd != NULL) (void) Fclose(fd);
732 int rpmAddSignature(Header sig, const char * file, int_32 sigTag,
733 const char * passPhrase)
738 int ret = -1; /* assume failure. */
742 if (stat(file, &st) != 0)
745 if (!headerAddEntry(sig, sigTag, RPM_INT32_TYPE, &pktlen, 1))
751 pkt = xcalloc(1, pktlen);
752 if (domd5(file, pkt, 0, NULL)
753 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
757 case RPMSIGTAG_PGP5: /* XXX legacy */
759 if (makePGPSignature(file, &pkt, &pktlen, passPhrase)
760 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
762 #ifdef NOTYET /* XXX needs hdrmd5ctx, like hdrsha1ctx. */
763 /* XXX Piggyback a header-only RSA signature as well. */
764 ret = makeHDRSignature(sig, file, RPMSIGTAG_RSA, passPhrase);
769 if (makeGPGSignature(file, &pkt, &pktlen, passPhrase)
770 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
772 /* XXX Piggyback a header-only DSA signature as well. */
773 ret = makeHDRSignature(sig, file, RPMSIGTAG_DSA, passPhrase);
778 ret = makeHDRSignature(sig, file, sigTag, passPhrase);
785 static int checkPassPhrase(const char * passPhrase, const int sigTag)
786 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
787 /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
789 int passPhrasePipe[2];
794 passPhrasePipe[0] = passPhrasePipe[1] = 0;
796 xx = pipe(passPhrasePipe);
798 if (!(pid = fork())) {
803 xx = close(STDIN_FILENO);
804 xx = close(STDOUT_FILENO);
805 xx = close(passPhrasePipe[1]);
806 if (! rpmIsVerbose())
807 xx = close(STDERR_FILENO);
808 if ((fdno = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
809 xx = dup2(fdno, STDIN_FILENO);
812 if ((fdno = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
813 xx = dup2(fdno, STDOUT_FILENO);
816 xx = dup2(passPhrasePipe[0], 3);
818 unsetenv("MALLOC_CHECK_");
822 { const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
825 if (gpg_path && *gpg_path != '\0')
826 (void) dosetenv("GNUPGHOME", gpg_path, 1);
829 cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
830 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
833 rc = execve(av[0], av+1, environ);
836 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
838 } /*@notreached@*/ break;
840 case RPMSIGTAG_PGP5: /* XXX legacy */
842 { const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
846 (void) dosetenv("PGPPASSFD", "3", 1);
848 if (pgp_path && *pgp_path != '\0')
849 xx = dosetenv("PGPPATH", pgp_path, 1);
852 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
855 cmd = rpmExpand("%{?__pgp_check_password_cmd}", NULL);
856 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
859 rc = execve(av[0], av+1, environ);
861 /*@innerbreak@*/ break;
862 case PGP_5: /* XXX legacy */
863 cmd = rpmExpand("%{?__pgp5_check_password_cmd}", NULL);
864 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
867 rc = execve(av[0], av+1, environ);
869 /*@innerbreak@*/ break;
871 case PGP_NOTDETECTED:
872 /*@innerbreak@*/ break;
875 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
878 } /*@notreached@*/ break;
879 default: /* This case should have been screened out long ago. */
880 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
881 _exit(RPMERR_SIGGEN);
882 /*@notreached@*/ break;
886 xx = close(passPhrasePipe[0]);
887 xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
888 xx = write(passPhrasePipe[1], "\n", 1);
889 xx = close(passPhrasePipe[1]);
891 (void) waitpid(pid, &status, 0);
893 return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
896 char * rpmGetPassPhrase(const char * prompt, const int sigTag)
905 { const char *name = rpmExpand("%{?_gpg_name}", NULL);
906 aok = (name && *name != '\0');
911 rpmError(RPMERR_SIGGEN,
912 _("You must set \"%%_gpg_name\" in your macro file\n"));
917 case RPMSIGTAG_PGP5: /* XXX legacy */
920 { const char *name = rpmExpand("%{?_pgp_name}", NULL);
921 aok = (name && *name != '\0');
926 rpmError(RPMERR_SIGGEN,
927 _("You must set \"%%_pgp_name\" in your macro file\n"));
932 /* Currently the calling function (rpm.c:main) is checking this and
933 * doing a better job. This section should never be accessed.
935 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
937 /*@notreached@*/ break;
940 /*@-moduncon -nullpass @*/
941 pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
942 /*@=moduncon -nullpass @*/
944 if (checkPassPhrase(pass, sigTag))
950 static /*@observer@*/ const char * rpmSigString(rpmRC res)
955 case RPMRC_OK: str = "OK"; break;
956 case RPMRC_FAIL: str = "BAD"; break;
957 case RPMRC_NOKEY: str = "NOKEY"; break;
958 case RPMRC_NOTTRUSTED: str = "NOTRUSTED"; break;
960 case RPMRC_NOTFOUND: str = "UNKNOWN"; break;
967 verifySizeSignature(const rpmts ts, /*@out@*/ char * t)
970 const void * sig = rpmtsSig(ts);
971 pgpDig dig = rpmtsDig(ts);
973 int_32 size = 0x7fffffff;
976 t = stpcpy(t, _("Header+Payload size: "));
978 if (sig == NULL || dig == NULL || dig->nbytes == 0) {
980 t = stpcpy(t, rpmSigString(res));
984 memcpy(&size, sig, sizeof(size));
986 if (size != dig->nbytes) {
988 t = stpcpy(t, rpmSigString(res));
989 sprintf(t, " Expected(%d) != (%d)\n", (int)size, (int)dig->nbytes);
992 t = stpcpy(t, rpmSigString(res));
993 sprintf(t, " (%d)", (int)dig->nbytes);
1004 verifyMD5Signature(const rpmts ts, /*@out@*/ char * t,
1005 /*@null@*/ DIGEST_CTX md5ctx)
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(&ts->op_digest, 0);
1025 (void) rpmDigestFinal(rpmDigestDup(md5ctx),
1026 (void **)&md5sum, &md5len, 0);
1027 (void) rpmswExit(&ts->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)
1065 const void * sig = rpmtsSig(ts);
1067 int_32 siglen = rpmtsSiglen(ts);
1069 pgpDig dig = rpmtsDig(ts);
1071 const char * SHA1 = NULL;
1074 t = stpcpy(t, _("Header SHA1 digest: "));
1076 if (sha1ctx == NULL || sig == NULL || dig == NULL) {
1078 t = stpcpy(t, rpmSigString(res));
1082 (void) rpmswEnter(&ts->op_digest, 0);
1083 (void) rpmDigestFinal(rpmDigestDup(sha1ctx),
1084 (void **)&SHA1, NULL, 1);
1085 (void) rpmswExit(&ts->op_digest, 0);
1087 if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
1089 t = stpcpy(t, rpmSigString(res));
1090 t = stpcpy(t, " Expected(");
1092 t = stpcpy(t, ") != (");
1095 t = stpcpy(t, rpmSigString(res));
1096 t = stpcpy(t, " (");
1099 t = stpcpy(t, SHA1);
1104 t = stpcpy(t, "\n");
1110 * Convert hex to binary nibble.
1111 * @param c hex character
1112 * @return binary nibble
1114 static inline unsigned char nibble(char c)
1117 if (c >= '0' && c <= '9')
1119 if (c >= 'A' && c <= 'F')
1120 return (c - 'A') + 10;
1121 if (c >= 'a' && c <= 'f')
1122 return (c - 'a') + 10;
1128 * Verify PGP (aka RSA/MD5) signature.
1129 * @param ts transaction set
1130 * @retval t verbose success/failure text
1132 * @return RPMRC_OK on success
1135 verifyPGPSignature(rpmts ts, /*@out@*/ char * t,
1136 /*@null@*/ DIGEST_CTX md5ctx)
1137 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1138 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1140 const void * sig = rpmtsSig(ts);
1142 int_32 siglen = rpmtsSiglen(ts);
1144 int_32 sigtag = rpmtsSigtag(ts);
1145 pgpDig dig = rpmtsDig(ts);
1146 pgpDigParams sigp = rpmtsSignature(ts);
1151 t = stpcpy(t, _("V3 RSA/MD5 signature: "));
1153 if (md5ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1158 /* XXX sanity check on sigtag and signature agreement. */
1159 if (!(sigtag == RPMSIGTAG_PGP
1160 && sigp->pubkey_algo == PGPPUBKEYALGO_RSA
1161 && sigp->hash_algo == PGPHASHALGO_MD5))
1167 (void) rpmswEnter(&ts->op_digest, 0);
1168 { DIGEST_CTX ctx = rpmDigestDup(md5ctx);
1172 if (sigp->hash != NULL)
1173 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1175 #ifdef NOTYET /* XXX not for binary/text document signatures. */
1176 if (sigp->sigtype == 4) {
1177 int nb = dig->nbytes + sigp->hashlen;
1182 memcpy(trailer+2, &nb, sizeof(nb));
1183 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1187 xx = rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 1);
1188 (void) rpmswExit(&ts->op_digest, sigp->hashlen);
1190 /* Compare leading 16 bits of digest for quick check. */
1192 signhash16[0] = (nibble(s[0]) << 4) | nibble(s[1]);
1193 signhash16[1] = (nibble(s[2]) << 4) | nibble(s[3]);
1194 if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
1201 { const char * prefix = "3020300c06082a864886f70d020505000410";
1202 unsigned int nbits = 1024;
1203 unsigned int nb = (nbits + 7) >> 3;
1204 const char * hexstr;
1207 hexstr = tt = xmalloc(2 * nb + 1);
1208 memset(tt, 'f', (2 * nb));
1209 tt[0] = '0'; tt[1] = '0';
1210 tt[2] = '0'; tt[3] = '1';
1211 tt += (2 * nb) - strlen(prefix) - strlen(dig->md5) - 2;
1212 *tt++ = '0'; *tt++ = '0';
1213 tt = stpcpy(tt, prefix);
1214 tt = stpcpy(tt, dig->md5);
1216 mp32nzero(&dig->rsahm); mp32nsethex(&dig->rsahm, hexstr);
1218 hexstr = _free(hexstr);
1222 /* Retrieve the matching public key. */
1223 res = rpmtsFindPubkey(ts);
1224 if (res != RPMRC_OK)
1227 (void) rpmswEnter(&ts->op_signature, 0);
1228 if (rsavrfy(&dig->rsa_pk, &dig->rsahm, &dig->c))
1232 (void) rpmswExit(&ts->op_signature, 0);
1235 t = stpcpy(t, rpmSigString(res));
1237 t = stpcpy(t, ", key ID ");
1238 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1241 t = stpcpy(t, "\n");
1247 * Verify GPG (aka DSA) signature.
1248 * @param ts transaction set
1249 * @retval t verbose success/failure text
1251 * @return RPMRC_OK on success
1255 verifyGPGSignature(rpmts ts, /*@out@*/ char * t,
1256 /*@null@*/ DIGEST_CTX sha1ctx)
1257 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1258 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1260 const void * sig = rpmtsSig(ts);
1262 int_32 siglen = rpmtsSiglen(ts);
1264 int_32 sigtag = rpmtsSigtag(ts);
1265 pgpDig dig = rpmtsDig(ts);
1266 pgpDigParams sigp = rpmtsSignature(ts);
1271 if (dig != NULL && dig->hdrsha1ctx == sha1ctx)
1272 t = stpcpy(t, _("Header "));
1273 t = stpcpy(t, _("V3 DSA signature: "));
1275 if (sha1ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1280 /* XXX sanity check on sigtag and signature agreement. */
1281 if (!((sigtag == RPMSIGTAG_GPG || sigtag == RPMSIGTAG_DSA)
1282 && sigp->pubkey_algo == PGPPUBKEYALGO_DSA
1283 && sigp->hash_algo == PGPHASHALGO_SHA1))
1289 (void) rpmswEnter(&ts->op_digest, 0);
1290 { DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
1293 (void) rpmswEnter(&ts->op_digest, 0);
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(&ts->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(&ts->op_signature, 0);
1328 if (dsavrfy(&dig->p, &dig->q, &dig->g,
1329 &dig->hm, &dig->y, &dig->r, &dig->s))
1333 (void) rpmswExit(&ts->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;