2 * \file lib/signature.c
5 /* signature.c - RPM signature functions */
9 * Things have been cleaned up wrt PGP. We can now handle
10 * signatures of any length (which means you can use any
11 * size key you like). We also honor PGPPATH finally.
16 #if HAVE_ASM_BYTEORDER_H
17 #include <asm/byteorder.h>
21 #include <rpmmacro.h> /* XXX for rpmGetPath */
26 #include "signature.h"
28 /*@access Header@*/ /* XXX compared with NULL */
30 typedef unsigned char byte;
32 typedef int (*md5func)(const char * fn, /*@out@*/ byte * digest);
34 int rpmLookupSignatureType(int action)
36 static int disabled = 0;
40 case RPMLOOKUPSIG_DISABLE:
43 case RPMLOOKUPSIG_ENABLE:
47 case RPMLOOKUPSIG_QUERY:
50 { const char *name = rpmExpand("%{_signature}", NULL);
51 if (!(name && *name != '%'))
53 else if (!strcasecmp(name, "none"))
55 else if (!strcasecmp(name, "pgp"))
57 else if (!strcasecmp(name, "pgp5")) /* XXX legacy */
59 else if (!strcasecmp(name, "gpg"))
62 rc = -1; /* Invalid %_signature spec in macro file */
69 /* rpmDetectPGPVersion() returns the absolute path to the "pgp" */
70 /* executable of the requested version, or NULL when none found. */
72 const char * rpmDetectPGPVersion(pgpVersion *pgpVer)
74 /* Actually this should support having more then one pgp version. */
75 /* At the moment only one version is possible since we only */
76 /* have one %_pgpbin and one %_pgp_path. */
78 static pgpVersion saved_pgp_version = PGP_UNKNOWN;
79 const char *pgpbin = rpmGetPath("%{_pgpbin}", NULL);
81 if (saved_pgp_version == PGP_UNKNOWN) {
85 if (!(pgpbin && pgpbin[0] != '%')) {
86 if (pgpbin) xfree(pgpbin);
87 saved_pgp_version = -1;
90 pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
91 (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
93 if (stat(pgpvbin, &statbuf) == 0)
94 saved_pgp_version = PGP_5;
95 else if (stat(pgpbin, &statbuf) == 0)
96 saved_pgp_version = PGP_2;
98 saved_pgp_version = PGP_NOTDETECTED;
101 if (pgpbin && pgpVer)
102 *pgpVer = saved_pgp_version;
106 static int checkSize(FD_t fd, int size, int sigsize)
108 int headerArchiveSize;
111 fstat(Fileno(fd), &statbuf);
113 if (S_ISREG(statbuf.st_mode)) {
114 headerArchiveSize = statbuf.st_size - sizeof(struct rpmlead) - sigsize;
116 rpmMessage(RPMMESS_DEBUG, _("sigsize : %d\n"), sigsize);
117 rpmMessage(RPMMESS_DEBUG, _("Header + Archive: %d\n"), headerArchiveSize);
118 rpmMessage(RPMMESS_DEBUG, _("expected size : %d\n"), size);
120 return size - headerArchiveSize;
122 rpmMessage(RPMMESS_DEBUG, _("file is not regular -- skipping size check\n"));
127 int rpmReadSignature(FD_t fd, Header *headerp, short sig_type)
134 int rc = 1; /* assume failure */
141 rpmMessage(RPMMESS_DEBUG, _("No signature\n"));
144 case RPMSIG_PGP262_1024:
145 rpmMessage(RPMMESS_DEBUG, _("Old PGP signature\n"));
146 /* These are always 256 bytes */
147 if (timedRead(fd, buf, 256) != 256)
150 headerAddEntry(h, RPMSIGTAG_PGP, RPM_BIN_TYPE, buf, 152);
155 rpmError(RPMERR_BADSIGTYPE,
156 _("Old (internal-only) signature! How did you get that!?"));
158 case RPMSIG_HEADERSIG:
159 rpmMessage(RPMMESS_DEBUG, _("New Header signature\n"));
160 /* This is a new style signature */
161 h = headerRead(fd, HEADER_MAGIC_YES);
164 sigSize = headerSizeof(h, HEADER_MAGIC_YES);
165 pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
166 rpmMessage(RPMMESS_DEBUG, _("Signature size: %d\n"), sigSize);
167 rpmMessage(RPMMESS_DEBUG, _("Signature pad : %d\n"), pad);
168 if (! headerGetEntry(h, RPMSIGTAG_SIZE, &type, (void **)&archSize, &count))
170 if (checkSize(fd, *archSize, sigSize + pad))
173 if (timedRead(fd, buf, pad) != pad)
182 if (rc == 0 && headerp)
190 int rpmWriteSignature(FD_t fd, Header header)
196 rc = headerWrite(fd, header, HEADER_MAGIC_YES);
200 sigSize = headerSizeof(header, HEADER_MAGIC_YES);
201 pad = (8 - (sigSize % 8)) % 8;
203 rpmMessage(RPMMESS_DEBUG, _("Signature size: %d\n"), sigSize);
204 rpmMessage(RPMMESS_DEBUG, _("Signature pad : %d\n"), pad);
206 if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
212 Header rpmNewSignature(void)
214 Header h = headerNew();
218 void rpmFreeSignature(Header h)
223 static int makePGPSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_32 *size,
224 const char *passPhrase)
231 sprintf(sigfile, "%s.sig", file);
233 inpipe[0] = inpipe[1] = 0;
236 if (!(pid = fork())) {
237 const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
238 const char *name = rpmExpand("+myname=\"%{_pgp_name}\"", NULL);
246 dosetenv("PGPPASSFD", "3", 1);
247 if (pgp_path && *pgp_path != '%')
248 dosetenv("PGPPATH", pgp_path, 1);
250 /* dosetenv("PGPPASS", passPhrase, 1); */
252 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
255 execlp(path, "pgp", "+batchmode=on", "+verbose=0", "+armor=off",
256 name, "-sb", file, sigfile, NULL);
259 execlp(path,"pgps", "+batchmode=on", "+verbose=0", "+armor=off",
260 name, "-b", file, "-o", sigfile, NULL);
263 case PGP_NOTDETECTED:
267 rpmError(RPMERR_EXEC, _("Couldn't exec pgp (%s)"), path);
272 (void)write(inpipe[1], passPhrase, strlen(passPhrase));
273 (void)write(inpipe[1], "\n", 1);
276 (void)waitpid(pid, &status, 0);
277 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
278 rpmError(RPMERR_SIGGEN, _("pgp failed"));
282 if (stat(sigfile, &statbuf)) {
283 /* PGP failed to write signature */
284 unlink(sigfile); /* Just in case */
285 rpmError(RPMERR_SIGGEN, _("pgp failed to write signature"));
289 *size = statbuf.st_size;
290 rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *size);
291 *sig = xmalloc(*size);
295 fd = Fopen(sigfile, "r.fdio");
296 rc = timedRead(fd, *sig, *size);
301 rpmError(RPMERR_SIGGEN, _("unable to read the signature"));
306 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *size);
311 /* This is an adaptation of the makePGPSignature function to use GPG instead
312 * of PGP to create signatures. I think I've made all the changes necessary,
313 * but this could be a good place to start looking if errors in GPG signature
316 static int makeGPGSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_32 *size,
317 const char *passPhrase)
325 sprintf(sigfile, "%s.sig", file);
327 inpipe[0] = inpipe[1] = 0;
330 if (!(pid = fork())) {
331 const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
332 const char *name = rpmExpand("%{_gpg_name}", NULL);
338 if (gpg_path && *gpg_path != '%')
339 dosetenv("GNUPGHOME", gpg_path, 1);
341 "--batch", "--no-verbose", "--no-armor", "--passphrase-fd", "3",
342 "-u", name, "-sbo", sigfile, file,
344 rpmError(RPMERR_EXEC, _("Couldn't exec gpg"));
348 fpipe = fdopen(inpipe[1], "w");
350 fprintf(fpipe, "%s\n", passPhrase);
353 (void)waitpid(pid, &status, 0);
354 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
355 rpmError(RPMERR_SIGGEN, _("gpg failed"));
359 if (stat(sigfile, &statbuf)) {
360 /* GPG failed to write signature */
361 unlink(sigfile); /* Just in case */
362 rpmError(RPMERR_SIGGEN, _("gpg failed to write signature"));
366 *size = statbuf.st_size;
367 rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *size);
368 *sig = xmalloc(*size);
372 fd = Fopen(sigfile, "r.fdio");
373 rc = timedRead(fd, *sig, *size);
378 rpmError(RPMERR_SIGGEN, _("unable to read the signature"));
383 rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *size);
388 int rpmAddSignature(Header header, const char *file, int_32 sigTag, const char *passPhrase)
398 stat(file, &statbuf);
399 size = statbuf.st_size;
401 headerAddEntry(header, RPMSIGTAG_SIZE, RPM_INT32_TYPE, &size, 1);
404 ret = mdbinfile(file, buf);
406 headerAddEntry(header, sigTag, RPM_BIN_TYPE, buf, 16);
408 case RPMSIGTAG_PGP5: /* XXX legacy */
410 rpmMessage(RPMMESS_VERBOSE, _("Generating signature using PGP.\n"));
411 ret = makePGPSignature(file, &sig, &size, passPhrase);
413 headerAddEntry(header, sigTag, RPM_BIN_TYPE, sig, size);
416 rpmMessage(RPMMESS_VERBOSE, _("Generating signature using GPG.\n"));
417 ret = makeGPGSignature(file, &sig, &size, passPhrase);
419 headerAddEntry(header, sigTag, RPM_BIN_TYPE, sig, size);
426 static rpmVerifySignatureReturn
427 verifySizeSignature(const char *datafile, int_32 size, char *result)
431 stat(datafile, &statbuf);
432 if (size != statbuf.st_size) {
433 sprintf(result, "Header+Archive size mismatch.\n"
434 "Expected %d, saw %d.\n",
435 size, (int)statbuf.st_size);
439 sprintf(result, "Header+Archive size OK: %d bytes\n", size);
443 #define X(_x) (unsigned)((_x) & 0xff)
445 static rpmVerifySignatureReturn
446 verifyMD5Signature(const char *datafile, const byte *sig,
447 char *result, md5func fn)
451 fn(datafile, md5sum);
452 if (memcmp(md5sum, sig, 16)) {
453 sprintf(result, "MD5 sum mismatch\n"
454 "Expected: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
455 "%02x%02x%02x%02x%02x\n"
456 "Saw : %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
457 "%02x%02x%02x%02x%02x\n",
458 X(sig[0]), X(sig[1]), X(sig[2]), X(sig[3]),
459 X(sig[4]), X(sig[5]), X(sig[6]), X(sig[7]),
460 X(sig[8]), X(sig[9]), X(sig[10]), X(sig[11]),
461 X(sig[12]), X(sig[13]), X(sig[14]), X(sig[15]),
462 X(md5sum[0]), X(md5sum[1]), X(md5sum[2]), X(md5sum[3]),
463 X(md5sum[4]), X(md5sum[5]), X(md5sum[6]), X(md5sum[7]),
464 X(md5sum[8]), X(md5sum[9]), X(md5sum[10]), X(md5sum[11]),
465 X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
469 sprintf(result, "MD5 sum OK: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
470 "%02x%02x%02x%02x%02x\n",
471 X(md5sum[0]), X(md5sum[1]), X(md5sum[2]), X(md5sum[3]),
472 X(md5sum[4]), X(md5sum[5]), X(md5sum[6]), X(md5sum[7]),
473 X(md5sum[8]), X(md5sum[9]), X(md5sum[10]), X(md5sum[11]),
474 X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
479 static rpmVerifySignatureReturn
480 verifyPGPSignature(const char *datafile, const void * sig, int count, char *result)
482 int pid, status, outpipe[2];
491 /* What version do we have? */
492 if ((path = rpmDetectPGPVersion(&pgpVer)) == NULL) {
494 rpmError(RPMERR_EXEC,
495 _("Could not run pgp. Use --nopgp to skip PGP checks."));
500 * Sad but true: pgp-5.0 returns exit value of 0 on bad signature.
501 * Instead we have to use the text output to detect a bad signature.
506 /* Write out the signature */
507 { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
508 sigfile = tempnam(tmppath, "rpmsig");
511 sfd = Fopen(sigfile, "w.fdio");
512 (void)Fwrite(sig, sizeof(char), count, sfd);
516 outpipe[0] = outpipe[1] = 0;
519 if (!(pid = fork())) {
520 const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
523 close(STDOUT_FILENO); /* XXX unnecessary */
524 dup2(outpipe[1], STDOUT_FILENO);
526 if (pgp_path && *pgp_path != '%')
527 dosetenv("PGPPATH", pgp_path, 1);
531 /* Some output (in particular "This signature applies to */
532 /* another message") is _always_ written to stderr; we */
533 /* want to catch that output, so dup stdout to stderr: */
534 { int save_stderr = dup(2);
536 execlp(path, "pgpv", "+batchmode=on", "+verbose=0",
537 /* Write "Good signature..." to stdout: */
538 "+OutputInformationFD=1",
539 /* Write "WARNING: ... is not trusted to... to stdout: */
540 "+OutputWarningFD=1",
541 sigfile, "-o", datafile, NULL);
542 /* Restore stderr so we can print the error message below. */
543 dup2(save_stderr, 2);
547 execlp(path, "pgp", "+batchmode=on", "+verbose=0",
548 sigfile, datafile, NULL);
551 case PGP_NOTDETECTED:
555 fprintf(stderr, _("exec failed!\n"));
556 rpmError(RPMERR_EXEC,
557 _("Could not run pgp. Use --nopgp to skip PGP checks."));
562 file = fdopen(outpipe[0], "r");
564 while (fgets(buf, 1024, file)) {
565 if (strncmp("File '", buf, 6) &&
566 strncmp("Text is assu", buf, 12) &&
567 strncmp("This signature applies to another message", buf, 41) &&
571 if (!strncmp("WARNING: Can't find the right public key", buf, 40))
573 else if (!strncmp("Signature by unknown keyid:", buf, 27))
575 else if (!strncmp("WARNING: The signing key is not trusted", buf, 39))
576 res = RPMSIG_NOTTRUSTED;
577 else if (!strncmp("Good signature", buf, 14))
582 (void)waitpid(pid, &status, 0);
584 if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
591 static rpmVerifySignatureReturn
592 verifyGPGSignature(const char *datafile, const void * sig, int count, char *result)
594 int pid, status, outpipe[2];
601 /* Write out the signature */
602 { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
603 sigfile = tempnam(tmppath, "rpmsig");
606 sfd = Fopen(sigfile, "w.fdio");
607 (void)Fwrite(sig, sizeof(char), count, sfd);
611 outpipe[0] = outpipe[1] = 0;
614 if (!(pid = fork())) {
615 const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
618 /* gpg version 0.9 sends its output to stderr. */
619 dup2(outpipe[1], STDERR_FILENO);
621 if (gpg_path && *gpg_path != '%')
622 dosetenv("GNUPGHOME", gpg_path, 1);
625 "--batch", "--no-verbose",
626 "--verify", sigfile, datafile,
628 fprintf(stderr, _("exec failed!\n"));
629 rpmError(RPMERR_EXEC,
630 _("Could not run gpg. Use --nogpg to skip GPG checks."));
635 file = fdopen(outpipe[0], "r");
637 while (fgets(buf, 1024, file)) {
639 if (!strncmp("gpg: Can't check signature: Public key not found", buf, 48)) {
645 (void)waitpid(pid, &status, 0);
647 if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
654 static int checkPassPhrase(const char *passPhrase, const int sigTag)
656 int passPhrasePipe[2];
660 passPhrasePipe[0] = passPhrasePipe[1] = 0;
661 pipe(passPhrasePipe);
662 if (!(pid = fork())) {
664 close(STDOUT_FILENO);
665 close(passPhrasePipe[1]);
666 if (! rpmIsVerbose()) {
667 close(STDERR_FILENO);
669 if ((fd = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
670 dup2(fd, STDIN_FILENO);
673 if ((fd = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
674 dup2(fd, STDOUT_FILENO);
677 dup2(passPhrasePipe[0], 3);
681 { const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
682 const char *name = rpmExpand("%{_gpg_name}", NULL);
683 if (gpg_path && *gpg_path != '%')
684 dosetenv("GNUPGHOME", gpg_path, 1);
686 "--batch", "--no-verbose", "--passphrase-fd", "3",
687 "-u", name, "-so", "-",
689 rpmError(RPMERR_EXEC, _("Couldn't exec gpg"));
691 } /*@notreached@*/ break;
692 case RPMSIGTAG_PGP5: /* XXX legacy */
694 { const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
695 const char *name = rpmExpand("+myname=\"%{_pgp_name}\"", NULL);
699 dosetenv("PGPPASSFD", "3", 1);
700 if (pgp_path && *pgp_path != '%')
701 dosetenv("PGPPATH", pgp_path, 1);
703 if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
706 execlp(path, "pgp", "+batchmode=on", "+verbose=0",
709 case PGP_5: /* XXX legacy */
710 execlp(path,"pgps", "+batchmode=on", "+verbose=0",
714 case PGP_NOTDETECTED:
718 rpmError(RPMERR_EXEC, _("Couldn't exec pgp"));
720 } /*@notreached@*/ break;
721 default: /* This case should have been screened out long ago. */
722 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
723 _exit(RPMERR_SIGGEN);
724 /*@notreached@*/ break;
728 close(passPhrasePipe[0]);
729 (void)write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
730 (void)write(passPhrasePipe[1], "\n", 1);
731 close(passPhrasePipe[1]);
733 (void)waitpid(pid, &status, 0);
734 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
738 /* passPhrase is good */
742 char *rpmGetPassPhrase(const char *prompt, const int sigTag)
749 { const char *name = rpmExpand("%{_gpg_name}", NULL);
750 aok = (name && *name != '%');
754 rpmError(RPMERR_SIGGEN,
755 _("You must set \"%%_gpg_name\" in your macro file"));
759 case RPMSIGTAG_PGP5: /* XXX legacy */
761 { const char *name = rpmExpand("%{_pgp_name}", NULL);
762 aok = (name && *name != '%');
766 rpmError(RPMERR_SIGGEN,
767 _("You must set \"%%_pgp_name\" in your macro file"));
772 /* Currently the calling function (rpm.c:main) is checking this and
773 * doing a better job. This section should never be accessed.
775 rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
777 /*@notreached@*/ break;
780 pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
782 if (checkPassPhrase(pass, sigTag))
788 rpmVerifySignatureReturn
789 rpmVerifySignature(const char *file, int_32 sigTag, const void * sig, int count,
794 return verifySizeSignature(file, *(int_32 *)sig, result);
795 /*@notreached@*/ break;
797 return verifyMD5Signature(file, sig, result, mdbinfile);
798 /*@notreached@*/ break;
799 case RPMSIGTAG_LEMD5_1:
800 case RPMSIGTAG_LEMD5_2:
801 return verifyMD5Signature(file, sig, result, mdbinfileBroken);
802 /*@notreached@*/ break;
803 case RPMSIGTAG_PGP5: /* XXX legacy */
805 return verifyPGPSignature(file, sig, count, result);
806 /*@notreached@*/ break;
808 return verifyGPGSignature(file, sig, count, result);
809 /*@notreached@*/ break;
811 sprintf(result, "Do not know how to verify sig type %d\n", sigTag);
812 return RPMSIG_UNKNOWN;