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 Header@*/ /* XXX compared with NULL */
22 /*@access FD_t@*/ /* XXX compared with NULL */
23 /*@access DIGEST_CTX@*/ /* XXX compared with NULL */
25 /*@access pgpDigParams@*/
27 #if !defined(__GLIBC__)
28 char ** environ = NULL;
31 int rpmLookupSignatureType(int action)
34 static int disabled = 0;
38 case RPMLOOKUPSIG_DISABLE:
41 case RPMLOOKUPSIG_ENABLE:
44 case RPMLOOKUPSIG_QUERY:
48 { const char *name = rpmExpand("%{?_signature}", NULL);
49 if (!(name && *name != '\0'))
51 else if (!xstrcasecmp(name, "none"))
53 else if (!xstrcasecmp(name, "pgp"))
55 else if (!xstrcasecmp(name, "pgp5")) /* XXX legacy */
57 else if (!xstrcasecmp(name, "gpg"))
60 rc = -1; /* Invalid %_signature spec in macro file */
68 /* rpmDetectPGPVersion() returns the absolute path to the "pgp" */
69 /* executable of the requested version, or NULL when none found. */
71 const char * rpmDetectPGPVersion(pgpVersion * pgpVer)
73 /* Actually this should support having more then one pgp version. */
74 /* At the moment only one version is possible since we only */
75 /* have one %_pgpbin and one %_pgp_path. */
77 static pgpVersion saved_pgp_version = PGP_UNKNOWN;
78 const char *pgpbin = rpmGetPath("%{?_pgpbin}", NULL);
80 if (saved_pgp_version == PGP_UNKNOWN) {
85 if (!(pgpbin && pgpbin[0] != '\0')) {
86 pgpbin = _free(pgpbin);
87 saved_pgp_version = -1;
92 pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
93 (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
96 if (stat(pgpvbin, &st) == 0)
97 saved_pgp_version = PGP_5;
98 else if (stat(pgpbin, &st) == 0)
99 saved_pgp_version = PGP_2;
101 saved_pgp_version = PGP_NOTDETECTED;
105 if (pgpVer && pgpbin)
106 *pgpVer = saved_pgp_version;
112 * Print package size.
113 * @todo rpmio: use fdSize rather than fstat(2) to get file size.
114 * @param fd package file handle
115 * @param siglen signature header size
116 * @param pad signature padding
117 * @param datalen length of header+payload
118 * @return rpmRC return code
120 static inline rpmRC printSize(FD_t fd, int siglen, int pad, int datalen)
121 /*@globals fileSystem @*/
122 /*@modifies fileSystem @*/
126 if (fstat(Fileno(fd), &st) < 0)
130 rpmMessage(RPMMESS_DEBUG,
131 _("Expected size: %12d = lead(%d)+sigs(%d)+pad(%d)+data(%d)\n"),
132 (int)sizeof(struct rpmlead)+siglen+pad+datalen,
133 (int)sizeof(struct rpmlead), siglen, pad, datalen);
135 rpmMessage(RPMMESS_DEBUG,
136 _(" Actual size: %12d\n"), (int)st.st_size);
142 static unsigned char header_magic[8] = {
143 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
146 rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type,
157 indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
158 entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
159 unsigned char * dataStart;
160 unsigned char * dataEnd = NULL;
162 rpmRC rc = RPMRC_FAIL; /* assume failure */
171 if (sig_type != RPMSIGTYPE_HEADERSIG)
174 if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
175 snprintf(buf, sizeof(buf),
176 _("sigh size(%d): BAD, read returned %d\n"), sizeof(block), xx);
179 if (memcmp(block, header_magic, sizeof(header_magic))) {
180 snprintf(buf, sizeof(buf),
181 _("sigh magic: BAD\n"));
184 il = ntohl(block[2]);
185 if (il < 0 || il > 32) {
186 snprintf(buf, sizeof(buf),
187 _("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
190 dl = ntohl(block[3]);
191 if (dl < 0 || dl > 8192) {
192 snprintf(buf, sizeof(buf),
193 _("sigh data: BAD, no. of bytes(%d) out of range\n"), dl);
197 nb = (il * sizeof(struct entryInfo_s)) + dl;
198 ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
201 pe = (entryInfo) &ei[2];
202 dataStart = (unsigned char *) (pe + il);
203 if ((xx = timedRead(fd, (char *)pe, nb)) != nb) {
204 snprintf(buf, sizeof(buf),
205 _("sigh blob(%d): BAD, read returned %d\n"), nb, xx);
209 /* Check (and convert) the 1st tag element. */
210 xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
212 snprintf(buf, sizeof(buf),
213 _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
214 0, entry->info.tag, entry->info.type,
215 entry->info.offset, entry->info.count);
219 /* Is there an immutable header region tag? */
221 if (entry->info.tag == RPMTAG_HEADERSIGNATURES
222 && entry->info.type == RPM_BIN_TYPE
223 && entry->info.count == REGION_TAG_COUNT)
227 if (entry->info.offset >= dl) {
228 snprintf(buf, sizeof(buf),
229 _("region offset: BAD, tag %d type %d offset %d count %d\n"),
230 entry->info.tag, entry->info.type,
231 entry->info.offset, entry->info.count);
235 /* Is there an immutable header region tag trailer? */
236 dataEnd = dataStart + entry->info.offset;
238 (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
240 dataEnd += REGION_TAG_COUNT;
243 xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
245 !(entry->info.tag == RPMTAG_HEADERSIGNATURES
246 && entry->info.type == RPM_BIN_TYPE
247 && entry->info.count == REGION_TAG_COUNT))
249 snprintf(buf, sizeof(buf),
250 _("region trailer: BAD, tag %d type %d offset %d count %d\n"),
251 entry->info.tag, entry->info.type,
252 entry->info.offset, entry->info.count);
257 memset(info, 0, sizeof(*info));
260 /* Is the no. of tags in the region less than the total no. of tags? */
261 ril = entry->info.offset/sizeof(*pe);
262 if ((entry->info.offset % sizeof(*pe)) || ril > il) {
263 snprintf(buf, sizeof(buf),
264 _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
269 /* Sanity check signature tags */
270 memset(info, 0, sizeof(*info));
271 for (i = 1; i < il; i++) {
272 xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
274 snprintf(buf, sizeof(buf),
275 _("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
276 i, entry->info.tag, entry->info.type,
277 entry->info.offset, entry->info.count);
282 /* OK, blob looks sane, load the header. */
283 sigh = headerLoad(ei);
285 snprintf(buf, sizeof(buf), _("sigh load: BAD\n"));
288 sigh->flags |= HEADERFLAG_ALLOCATED;
290 { int sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
291 int pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
292 int_32 * archSize = NULL;
294 /* Position at beginning of header. */
295 if (pad && (xx = timedRead(fd, (char *)block, pad)) != pad) {
296 snprintf(buf, sizeof(buf),
297 _("sigh pad(%d): BAD, read %d bytes\n"), pad, xx);
301 /* Print package component sizes. */
302 if (headerGetEntry(sigh, RPMSIGTAG_SIZE, NULL,(void **)&archSize, NULL))
303 rc = printSize(fd, sigSize, pad, *archSize);
307 if (rc == RPMRC_OK && sighp && sigh)
308 *sighp = headerLink(sigh);
309 sigh = headerFree(sigh);
313 buf[sizeof(buf)-1] = '\0';
321 int rpmWriteSignature(FD_t fd, Header h)
323 static byte buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
327 rc = headerWrite(fd, h, HEADER_MAGIC_YES);
331 sigSize = headerSizeof(h, HEADER_MAGIC_YES);
332 pad = (8 - (sigSize % 8)) % 8;
335 if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
339 rpmMessage(RPMMESS_DEBUG, _("Signature: size(%d)+pad(%d)\n"), sigSize, pad);
343 Header rpmNewSignature(void)
345 Header h = headerNew();
349 Header rpmFreeSignature(Header h)
351 return headerFree(h);
355 * Generate PGP (aka RSA/MD5) signature(s) for a header+payload file.
356 * @param file header+payload file name
357 * @retval pkt signature packet(s)
358 * @retval pktlen signature packet(s) length
359 * @param passPhrase private key pass phrase
360 * @return 0 on success, 1 on failure
362 static int makePGPSignature(const char * file, /*@out@*/ byte ** pkt,
363 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
364 /*@globals errno, rpmGlobalMacroContext,
365 fileSystem, internalState @*/
366 /*@modifies errno, *pkt, *pktlen, rpmGlobalMacroContext,
367 fileSystem, internalState @*/
369 char * sigfile = alloca(1024);
378 (void) stpcpy( stpcpy(sigfile, file), ".sig");
381 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
382 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
384 inpipe[0] = inpipe[1] = 0;
389 if (!(pid = fork())) {
390 const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
394 (void) close(STDIN_FILENO);
395 (void) dup2(inpipe[0], 3);
396 (void) close(inpipe[1]);
398 (void) dosetenv("PGPPASSFD", "3", 1);
400 if (pgp_path && *pgp_path != '\0')
401 (void) dosetenv("PGPPATH", pgp_path, 1);
404 /* dosetenv("PGPPASS", passPhrase, 1); */
406 unsetenv("MALLOC_CHECK_");
407 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
410 cmd = rpmExpand("%{?__pgp_sign_cmd}", NULL);
411 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
414 rc = execve(av[0], av+1, environ);
418 cmd = rpmExpand("%{?__pgp5_sign_cmd}", NULL);
419 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
422 rc = execve(av[0], av+1, environ);
426 case PGP_NOTDETECTED:
431 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
436 delMacro(NULL, "__plaintext_filename");
437 delMacro(NULL, "__signature_filename");
439 (void) close(inpipe[0]);
441 (void) write(inpipe[1], passPhrase, strlen(passPhrase));
442 (void) write(inpipe[1], "\n", 1);
443 (void) close(inpipe[1]);
445 (void)waitpid(pid, &status, 0);
446 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
447 rpmError(RPMERR_SIGGEN, _("pgp failed\n"));
451 if (stat(sigfile, &st)) {
452 /* PGP failed to write signature */
453 if (sigfile) (void) unlink(sigfile); /* Just in case */
454 rpmError(RPMERR_SIGGEN, _("pgp failed to write signature\n"));
459 *pktlen = st.st_size;
460 rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *pktlen);
461 *pkt = xmalloc(*pktlen);
468 fd = Fopen(sigfile, "r.fdio");
469 if (fd != NULL && !Ferror(fd)) {
470 rc = timedRead(fd, *pkt, *pktlen);
471 if (sigfile) (void) unlink(sigfile);
478 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
483 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *pktlen);
490 * Generate GPG (aka DSA) signature(s) for a header+payload file.
491 * @param file header+payload file name
492 * @retval pkt signature packet(s)
493 * @retval pktlen signature packet(s) length
494 * @param passPhrase private key pass phrase
495 * @return 0 on success, 1 on failure
497 static int makeGPGSignature(const char * file, /*@out@*/ byte ** pkt,
498 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
499 /*@globals rpmGlobalMacroContext,
500 fileSystem, internalState @*/
501 /*@modifies *pkt, *pktlen, rpmGlobalMacroContext,
502 fileSystem, internalState @*/
504 char * sigfile = alloca(1024);
514 (void) stpcpy( stpcpy(sigfile, file), ".sig");
517 addMacro(NULL, "__plaintext_filename", NULL, file, -1);
518 addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
520 inpipe[0] = inpipe[1] = 0;
525 if (!(pid = fork())) {
526 const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
528 (void) close(STDIN_FILENO);
529 (void) dup2(inpipe[0], 3);
530 (void) close(inpipe[1]);
533 if (gpg_path && *gpg_path != '\0')
534 (void) dosetenv("GNUPGHOME", gpg_path, 1);
537 unsetenv("MALLOC_CHECK_");
538 cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
539 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
542 rc = execve(av[0], av+1, environ);
545 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
550 delMacro(NULL, "__plaintext_filename");
551 delMacro(NULL, "__signature_filename");
553 fpipe = fdopen(inpipe[1], "w");
554 (void) close(inpipe[0]);
556 fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
557 (void) fclose(fpipe);
560 (void) waitpid(pid, &status, 0);
561 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
562 rpmError(RPMERR_SIGGEN, _("gpg failed\n"));
566 if (stat(sigfile, &st)) {
567 /* GPG failed to write signature */
568 if (sigfile) (void) unlink(sigfile); /* Just in case */
569 rpmError(RPMERR_SIGGEN, _("gpg failed to write signature\n"));
574 *pktlen = st.st_size;
575 rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *pktlen);
576 *pkt = xmalloc(*pktlen);
583 fd = Fopen(sigfile, "r.fdio");
584 if (fd != NULL && !Ferror(fd)) {
585 rc = timedRead(fd, *pkt, *pktlen);
586 if (sigfile) (void) unlink(sigfile);
593 rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
598 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *pktlen);
605 * Generate header only signature(s) from a header+payload file.
606 * @param sig signature header
607 * @param file header+payload file name
608 * @param sigTag type of signature(s) to add
609 * @param passPhrase private key pass phrase
610 * @return 0 on success, -1 on failure
612 static int makeHDRSignature(Header sig, const char * file, int_32 sigTag,
613 /*@null@*/ const char * passPhrase)
614 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
615 /*@modifies sig, rpmGlobalMacroContext, fileSystem, internalState @*/
621 const char * fn = NULL;
622 const char * SHA1 = NULL;
623 int ret = -1; /* assume failure. */
628 case RPMSIGTAG_PGP5: /* XXX legacy */
632 /*@notreached@*/ break;
634 fd = Fopen(file, "r.fdio");
635 if (fd == NULL || Ferror(fd))
637 h = headerRead(fd, HEADER_MAGIC_YES);
640 (void) Fclose(fd); fd = NULL;
642 if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
647 if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)
653 ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
654 (void) rpmDigestUpdate(ctx, header_magic, sizeof(header_magic));
655 (void) rpmDigestUpdate(ctx, uh, uhc);
656 (void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
657 uh = headerFreeData(uh, uht);
663 if (!headerAddEntry(sig, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
668 fd = Fopen(file, "r.fdio");
669 if (fd == NULL || Ferror(fd))
671 h = headerRead(fd, HEADER_MAGIC_YES);
674 (void) Fclose(fd); fd = NULL;
675 if (makeTempFile(NULL, &fn, &fd))
677 if (headerWrite(fd, h, HEADER_MAGIC_YES))
679 (void) Fclose(fd); fd = NULL;
680 if (makeGPGSignature(fn, &pkt, &pktlen, passPhrase)
681 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
686 fd = Fopen(file, "r.fdio");
687 if (fd == NULL || Ferror(fd))
689 h = headerRead(fd, HEADER_MAGIC_YES);
692 (void) Fclose(fd); fd = NULL;
693 if (makeTempFile(NULL, &fn, &fd))
695 if (headerWrite(fd, h, HEADER_MAGIC_YES))
697 (void) Fclose(fd); fd = NULL;
698 if (makePGPSignature(fn, &pkt, &pktlen, passPhrase)
699 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
712 if (fd) (void) Fclose(fd);
716 int rpmAddSignature(Header sig, const char * file, int_32 sigTag,
717 const char * passPhrase)
722 int ret = -1; /* assume failure. */
726 if (stat(file, &st) != 0)
729 if (!headerAddEntry(sig, sigTag, RPM_INT32_TYPE, &pktlen, 1))
735 pkt = xcalloc(1, pktlen);
736 if (domd5(file, pkt, 0, NULL)
737 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
741 case RPMSIGTAG_PGP5: /* XXX legacy */
743 if (makePGPSignature(file, &pkt, &pktlen, passPhrase)
744 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
746 #ifdef NOTYET /* XXX needs hdrmd5ctx, like hdrsha1ctx. */
747 /* XXX Piggyback a header-only RSA signature as well. */
748 ret = makeHDRSignature(sig, file, RPMSIGTAG_RSA, passPhrase);
753 if (makeGPGSignature(file, &pkt, &pktlen, passPhrase)
754 || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
756 /* XXX Piggyback a header-only DSA signature as well. */
757 ret = makeHDRSignature(sig, file, RPMSIGTAG_DSA, passPhrase);
762 ret = makeHDRSignature(sig, file, sigTag, passPhrase);
769 static int checkPassPhrase(const char * passPhrase, const int sigTag)
770 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
771 /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
773 int passPhrasePipe[2];
778 passPhrasePipe[0] = passPhrasePipe[1] = 0;
780 xx = pipe(passPhrasePipe);
782 if (!(pid = fork())) {
787 xx = close(STDIN_FILENO);
788 xx = close(STDOUT_FILENO);
789 xx = close(passPhrasePipe[1]);
790 if (! rpmIsVerbose())
791 xx = close(STDERR_FILENO);
792 if ((fdno = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
793 xx = dup2(fdno, STDIN_FILENO);
796 if ((fdno = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
797 xx = dup2(fdno, STDOUT_FILENO);
800 xx = dup2(passPhrasePipe[0], 3);
802 unsetenv("MALLOC_CHECK_");
806 { const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
809 if (gpg_path && *gpg_path != '\0')
810 (void) dosetenv("GNUPGHOME", gpg_path, 1);
813 cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
814 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
817 rc = execve(av[0], av+1, environ);
820 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
822 } /*@notreached@*/ break;
824 case RPMSIGTAG_PGP5: /* XXX legacy */
826 { const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
830 (void) dosetenv("PGPPASSFD", "3", 1);
832 if (pgp_path && *pgp_path != '\0')
833 xx = dosetenv("PGPPATH", pgp_path, 1);
836 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
839 cmd = rpmExpand("%{?__pgp_check_password_cmd}", NULL);
840 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
843 rc = execve(av[0], av+1, environ);
845 /*@innerbreak@*/ break;
846 case PGP_5: /* XXX legacy */
847 cmd = rpmExpand("%{?__pgp5_check_password_cmd}", NULL);
848 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
851 rc = execve(av[0], av+1, environ);
853 /*@innerbreak@*/ break;
855 case PGP_NOTDETECTED:
856 /*@innerbreak@*/ break;
859 rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
862 } /*@notreached@*/ break;
863 default: /* This case should have been screened out long ago. */
864 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
865 _exit(RPMERR_SIGGEN);
866 /*@notreached@*/ break;
870 xx = close(passPhrasePipe[0]);
871 xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
872 xx = write(passPhrasePipe[1], "\n", 1);
873 xx = close(passPhrasePipe[1]);
875 (void) waitpid(pid, &status, 0);
877 return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
880 char * rpmGetPassPhrase(const char * prompt, const int sigTag)
889 { const char *name = rpmExpand("%{?_gpg_name}", NULL);
890 aok = (name && *name != '\0');
895 rpmError(RPMERR_SIGGEN,
896 _("You must set \"%%_gpg_name\" in your macro file\n"));
901 case RPMSIGTAG_PGP5: /* XXX legacy */
904 { const char *name = rpmExpand("%{?_pgp_name}", NULL);
905 aok = (name && *name != '\0');
910 rpmError(RPMERR_SIGGEN,
911 _("You must set \"%%_pgp_name\" in your macro file\n"));
916 /* Currently the calling function (rpm.c:main) is checking this and
917 * doing a better job. This section should never be accessed.
919 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
921 /*@notreached@*/ break;
924 /*@-moduncon -nullpass @*/
925 pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
926 /*@=moduncon -nullpass @*/
928 if (checkPassPhrase(pass, sigTag))
934 static /*@observer@*/ const char * rpmSigString(rpmRC res)
939 case RPMRC_OK: str = "OK"; break;
940 case RPMRC_FAIL: str = "BAD"; break;
941 case RPMRC_NOKEY: str = "NOKEY"; break;
942 case RPMRC_NOTTRUSTED: str = "NOTRUSTED"; break;
944 case RPMRC_NOTFOUND: str = "UNKNOWN"; break;
951 verifySizeSignature(const rpmts ts, /*@out@*/ char * t)
954 const void * sig = rpmtsSig(ts);
955 pgpDig dig = rpmtsDig(ts);
957 int_32 size = 0x7fffffff;
960 t = stpcpy(t, _("Header+Payload size: "));
962 if (sig == NULL || dig == NULL || dig->nbytes == 0) {
964 t = stpcpy(t, rpmSigString(res));
968 memcpy(&size, sig, sizeof(size));
970 if (size != dig->nbytes) {
972 t = stpcpy(t, rpmSigString(res));
973 sprintf(t, " Expected(%d) != (%d)\n", size, dig->nbytes);
976 t = stpcpy(t, rpmSigString(res));
977 sprintf(t, " (%d)", dig->nbytes);
988 verifyMD5Signature(const rpmts ts, /*@out@*/ char * t,
989 /*@null@*/ DIGEST_CTX md5ctx)
992 const void * sig = rpmtsSig(ts);
993 int_32 siglen = rpmtsSiglen(ts);
994 pgpDig dig = rpmtsDig(ts);
996 byte * md5sum = NULL;
1000 t = stpcpy(t, _("MD5 digest: "));
1002 if (md5ctx == NULL || sig == NULL || dig == NULL) {
1004 t = stpcpy(t, rpmSigString(res));
1008 (void) rpmDigestFinal(rpmDigestDup(md5ctx),
1009 (void **)&md5sum, &md5len, 0);
1011 if (md5len != siglen || memcmp(md5sum, sig, md5len)) {
1013 t = stpcpy(t, rpmSigString(res));
1014 t = stpcpy(t, " Expected(");
1015 (void) pgpHexCvt(t, sig, siglen);
1017 t = stpcpy(t, ") != (");
1020 t = stpcpy(t, rpmSigString(res));
1021 t = stpcpy(t, " (");
1023 (void) pgpHexCvt(t, md5sum, md5len);
1028 md5sum = _free(md5sum);
1029 t = stpcpy(t, "\n");
1036 * Verify header immutable region SHA1 digest.
1037 * @param ts transaction set
1038 * @retval t verbose success/failure text
1040 * @return RPMRC_OK on success
1043 verifySHA1Signature(const rpmts ts, /*@out@*/ char * t,
1044 /*@null@*/ DIGEST_CTX sha1ctx)
1047 const void * sig = rpmtsSig(ts);
1049 int_32 siglen = rpmtsSiglen(ts);
1051 pgpDig dig = rpmtsDig(ts);
1053 const char * SHA1 = NULL;
1056 t = stpcpy(t, _("Header SHA1 digest: "));
1058 if (sha1ctx == NULL || sig == NULL || dig == NULL) {
1060 t = stpcpy(t, rpmSigString(res));
1064 (void) rpmDigestFinal(rpmDigestDup(sha1ctx),
1065 (void **)&SHA1, NULL, 1);
1067 if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
1069 t = stpcpy(t, rpmSigString(res));
1070 t = stpcpy(t, " Expected(");
1072 t = stpcpy(t, ") != (");
1075 t = stpcpy(t, rpmSigString(res));
1076 t = stpcpy(t, " (");
1079 t = stpcpy(t, SHA1);
1084 t = stpcpy(t, "\n");
1090 * Convert hex to binary nibble.
1091 * @param c hex character
1092 * @return binary nibble
1094 static inline unsigned char nibble(char c)
1097 if (c >= '0' && c <= '9')
1099 if (c >= 'A' && c <= 'F')
1100 return (c - 'A') + 10;
1101 if (c >= 'a' && c <= 'f')
1102 return (c - 'a') + 10;
1108 * Verify PGP (aka RSA/MD5) signature.
1109 * @param ts transaction set
1110 * @retval t verbose success/failure text
1112 * @return RPMRC_OK on success
1115 verifyPGPSignature(rpmts ts, /*@out@*/ char * t,
1116 /*@null@*/ DIGEST_CTX md5ctx)
1117 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1118 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1120 const void * sig = rpmtsSig(ts);
1122 int_32 siglen = rpmtsSiglen(ts);
1124 int_32 sigtag = rpmtsSigtag(ts);
1125 pgpDig dig = rpmtsDig(ts);
1126 pgpDigParams sigp = rpmtsSignature(ts);
1131 t = stpcpy(t, _("V3 RSA/MD5 signature: "));
1133 if (md5ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1138 /* XXX sanity check on sigtag and signature agreement. */
1139 if (!(sigtag == RPMSIGTAG_PGP
1140 && sigp->pubkey_algo == PGPPUBKEYALGO_RSA
1141 && sigp->hash_algo == PGPHASHALGO_MD5))
1147 { DIGEST_CTX ctx = rpmDigestDup(md5ctx);
1151 if (sigp->hash != NULL)
1152 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1154 #ifdef NOTYET /* XXX not for binary/text document signatures. */
1155 if (sigp->sigtype == 4) {
1156 int nb = dig->nbytes + sigp->hashlen;
1161 memcpy(trailer+2, &nb, sizeof(nb));
1162 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1166 xx = rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 1);
1168 /* Compare leading 16 bits of digest for quick check. */
1170 signhash16[0] = (nibble(s[0]) << 4) | nibble(s[1]);
1171 signhash16[1] = (nibble(s[2]) << 4) | nibble(s[3]);
1172 if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
1179 { const char * prefix = "3020300c06082a864886f70d020505000410";
1180 unsigned int nbits = 1024;
1181 unsigned int nb = (nbits + 7) >> 3;
1182 const char * hexstr;
1185 hexstr = tt = xmalloc(2 * nb + 1);
1186 memset(tt, 'f', (2 * nb));
1187 tt[0] = '0'; tt[1] = '0';
1188 tt[2] = '0'; tt[3] = '1';
1189 tt += (2 * nb) - strlen(prefix) - strlen(dig->md5) - 2;
1190 *tt++ = '0'; *tt++ = '0';
1191 tt = stpcpy(tt, prefix);
1192 tt = stpcpy(tt, dig->md5);
1194 mp32nzero(&dig->rsahm); mp32nsethex(&dig->rsahm, hexstr);
1196 hexstr = _free(hexstr);
1200 /* Retrieve the matching public key. */
1201 res = rpmtsFindPubkey(ts);
1202 if (res != RPMRC_OK)
1205 if (rsavrfy(&dig->rsa_pk, &dig->rsahm, &dig->c))
1211 t = stpcpy(t, rpmSigString(res));
1213 t = stpcpy(t, ", key ID ");
1214 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1217 t = stpcpy(t, "\n");
1223 * Verify GPG (aka DSA) signature.
1224 * @param ts transaction set
1225 * @retval t verbose success/failure text
1227 * @return RPMRC_OK on success
1231 verifyGPGSignature(rpmts ts, /*@out@*/ char * t,
1232 /*@null@*/ DIGEST_CTX sha1ctx)
1233 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
1234 /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
1236 const void * sig = rpmtsSig(ts);
1238 int_32 siglen = rpmtsSiglen(ts);
1240 int_32 sigtag = rpmtsSigtag(ts);
1241 pgpDig dig = rpmtsDig(ts);
1242 pgpDigParams sigp = rpmtsSignature(ts);
1247 if (dig != NULL && dig->hdrsha1ctx == sha1ctx)
1248 t = stpcpy(t, _("Header "));
1249 t = stpcpy(t, _("V3 DSA signature: "));
1251 if (sha1ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
1256 /* XXX sanity check on sigtag and signature agreement. */
1257 if (!((sigtag == RPMSIGTAG_GPG || sigtag == RPMSIGTAG_DSA)
1258 && sigp->pubkey_algo == PGPPUBKEYALGO_DSA
1259 && sigp->hash_algo == PGPHASHALGO_SHA1))
1265 { DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
1268 if (sigp->hash != NULL)
1269 xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
1271 #ifdef NOTYET /* XXX not for binary/text document signatures. */
1272 if (sigp->sigtype == 4) {
1273 int nb = dig->nbytes + sigp->hashlen;
1278 memcpy(trailer+2, &nb, sizeof(nb));
1279 xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
1282 xx = rpmDigestFinal(ctx, (void **)&dig->sha1, &dig->sha1len, 1);
1284 mp32nzero(&dig->hm); mp32nsethex(&dig->hm, dig->sha1);
1286 /* Compare leading 16 bits of digest for quick check. */
1287 signhash16[0] = (*dig->hm.data >> 24) & 0xff;
1288 signhash16[1] = (*dig->hm.data >> 16) & 0xff;
1289 if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
1295 /* Retrieve the matching public key. */
1296 res = rpmtsFindPubkey(ts);
1297 if (res != RPMRC_OK)
1300 if (dsavrfy(&dig->p, &dig->q, &dig->g,
1301 &dig->hm, &dig->y, &dig->r, &dig->s))
1307 t = stpcpy(t, rpmSigString(res));
1309 t = stpcpy(t, ", key ID ");
1310 (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
1313 t = stpcpy(t, "\n");
1319 rpmVerifySignature(const rpmts ts, char * result)
1321 const void * sig = rpmtsSig(ts);
1322 int_32 siglen = rpmtsSiglen(ts);
1323 int_32 sigtag = rpmtsSigtag(ts);
1324 pgpDig dig = rpmtsDig(ts);
1327 if (sig == NULL || siglen <= 0 || dig == NULL) {
1328 sprintf(result, _("Verify signature: BAD PARAMETERS\n"));
1329 return RPMRC_NOTFOUND;
1333 case RPMSIGTAG_SIZE:
1334 res = verifySizeSignature(ts, result);
1337 res = verifyMD5Signature(ts, result, dig->md5ctx);
1339 case RPMSIGTAG_SHA1:
1340 res = verifySHA1Signature(ts, result, dig->hdrsha1ctx);
1343 case RPMSIGTAG_PGP5: /* XXX legacy */
1345 res = verifyPGPSignature(ts, result, dig->md5ctx);
1348 res = verifyGPGSignature(ts, result, dig->hdrsha1ctx);
1351 res = verifyGPGSignature(ts, result, dig->sha1ctx);
1353 case RPMSIGTAG_LEMD5_1:
1354 case RPMSIGTAG_LEMD5_2:
1355 sprintf(result, _("Broken MD5 digest: UNSUPPORTED\n"));
1356 res = RPMRC_NOTFOUND;
1359 sprintf(result, _("Signature: UNKNOWN (%d)\n"), sigtag);
1360 res = RPMRC_NOTFOUND;