Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

signature.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include "rpmio_internal.h"
00008 #include <rpmlib.h>
00009 #include <rpmmacro.h>   /* XXX for rpmGetPath() */
00010 #include "rpmdb.h"
00011 
00012 #include "rpmts.h"
00013 
00014 #include "misc.h"       /* XXX for dosetenv() and makeTempFile() */
00015 #include "legacy.h"     /* XXX for mdbinfile() */
00016 #include "rpmlead.h"
00017 #include "signature.h"
00018 #include "header_internal.h"
00019 #include "debug.h"
00020 
00021 /*@access FD_t@*/               /* XXX ufdio->read arg1 is void ptr */
00022 /*@access Header@*/             /* XXX compared with NULL */
00023 /*@access entryInfo @*/         /* XXX rpmReadSignature */
00024 /*@access indexEntry @*/        /* XXX rpmReadSignature */
00025 /*@access DIGEST_CTX@*/         /* XXX compared with NULL */
00026 /*@access pgpDig@*/
00027 /*@access pgpDigParams@*/
00028 
00029 #if !defined(__GLIBC__)
00030 char ** environ = NULL;
00031 #endif
00032 
00033 int rpmLookupSignatureType(int action)
00034 {
00035     /*@unchecked@*/
00036     static int disabled = 0;
00037     int rc = 0;
00038 
00039     switch (action) {
00040     case RPMLOOKUPSIG_DISABLE:
00041         disabled = -2;
00042         break;
00043     case RPMLOOKUPSIG_ENABLE:
00044         disabled = 0;
00045         /*@fallthrough@*/
00046     case RPMLOOKUPSIG_QUERY:
00047         if (disabled)
00048             break;      /* Disabled */
00049 /*@-boundsread@*/
00050       { const char *name = rpmExpand("%{?_signature}", NULL);
00051         if (!(name && *name != '\0'))
00052             rc = 0;
00053         else if (!xstrcasecmp(name, "none"))
00054             rc = 0;
00055         else if (!xstrcasecmp(name, "pgp"))
00056             rc = RPMSIGTAG_PGP;
00057         else if (!xstrcasecmp(name, "pgp5"))    /* XXX legacy */
00058             rc = RPMSIGTAG_PGP;
00059         else if (!xstrcasecmp(name, "gpg"))
00060             rc = RPMSIGTAG_GPG;
00061         else
00062             rc = -1;    /* Invalid %_signature spec in macro file */
00063         name = _free(name);
00064       } break;
00065 /*@=boundsread@*/
00066     }
00067     return rc;
00068 }
00069 
00070 /* rpmDetectPGPVersion() returns the absolute path to the "pgp"  */
00071 /* executable of the requested version, or NULL when none found. */
00072 
00073 const char * rpmDetectPGPVersion(pgpVersion * pgpVer)
00074 {
00075     /* Actually this should support having more then one pgp version. */
00076     /* At the moment only one version is possible since we only       */
00077     /* have one %_pgpbin and one %_pgp_path.                          */
00078 
00079     static pgpVersion saved_pgp_version = PGP_UNKNOWN;
00080     const char *pgpbin = rpmGetPath("%{?_pgpbin}", NULL);
00081 
00082     if (saved_pgp_version == PGP_UNKNOWN) {
00083         char *pgpvbin;
00084         struct stat st;
00085 
00086 /*@-boundsread@*/
00087         if (!(pgpbin && pgpbin[0] != '\0')) {
00088             pgpbin = _free(pgpbin);
00089             saved_pgp_version = -1;
00090             return NULL;
00091         }
00092 /*@=boundsread@*/
00093 /*@-boundswrite@*/
00094         pgpvbin = (char *)alloca(strlen(pgpbin) + sizeof("v"));
00095         (void)stpcpy(stpcpy(pgpvbin, pgpbin), "v");
00096 /*@=boundswrite@*/
00097 
00098         if (stat(pgpvbin, &st) == 0)
00099             saved_pgp_version = PGP_5;
00100         else if (stat(pgpbin, &st) == 0)
00101             saved_pgp_version = PGP_2;
00102         else
00103             saved_pgp_version = PGP_NOTDETECTED;
00104     }
00105 
00106 /*@-boundswrite@*/
00107     if (pgpVer && pgpbin)
00108         *pgpVer = saved_pgp_version;
00109 /*@=boundswrite@*/
00110     return pgpbin;
00111 }
00112 
00122 static inline rpmRC printSize(FD_t fd, int siglen, int pad, int datalen)
00123         /*@globals fileSystem @*/
00124         /*@modifies fileSystem @*/
00125 {
00126     struct stat st;
00127 
00128     if (fstat(Fileno(fd), &st) < 0)
00129         return RPMRC_FAIL;
00130 
00131     /*@-sizeoftype@*/
00132     rpmMessage(RPMMESS_DEBUG,
00133         _("Expected size: %12d = lead(%d)+sigs(%d)+pad(%d)+data(%d)\n"),
00134                 (int)sizeof(struct rpmlead)+siglen+pad+datalen,
00135                 (int)sizeof(struct rpmlead), siglen, pad, datalen);
00136     /*@=sizeoftype@*/
00137     rpmMessage(RPMMESS_DEBUG,
00138         _("  Actual size: %12d\n"), (int)st.st_size);
00139 
00140     return RPMRC_OK;
00141 }
00142 
00143 /*@unchecked@*/
00144 static unsigned char header_magic[8] = {
00145     0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
00146 };
00147 
00148 rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type,
00149                 const char ** msg)
00150 {
00151     char buf[BUFSIZ];
00152     int_32 block[4];
00153     int_32 il;
00154     int_32 dl;
00155     int_32 * ei = NULL;
00156     entryInfo pe;
00157     size_t nb;
00158     int_32 ril = 0;
00159     indexEntry entry = memset(alloca(sizeof(*entry)), 0, sizeof(*entry));
00160     entryInfo info = memset(alloca(sizeof(*info)), 0, sizeof(*info));
00161     unsigned char * dataStart;
00162     unsigned char * dataEnd = NULL;
00163     Header sigh = NULL;
00164     rpmRC rc = RPMRC_FAIL;              /* assume failure */
00165     int xx;
00166     int i;
00167 
00168 /*@-boundswrite@*/
00169     if (sighp)
00170         *sighp = NULL;
00171 
00172     buf[0] = '\0';
00173 /*@=boundswrite@*/
00174 
00175     if (sig_type != RPMSIGTYPE_HEADERSIG)
00176         goto exit;
00177 
00178     memset(block, 0, sizeof(block));
00179     if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
00180         (void) snprintf(buf, sizeof(buf),
00181                 _("sigh size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
00182         goto exit;
00183     }
00184     if (memcmp(block, header_magic, sizeof(header_magic))) {
00185         (void) snprintf(buf, sizeof(buf),
00186                 _("sigh magic: BAD\n"));
00187         goto exit;
00188     }
00189 /*@-boundsread@*/
00190     il = ntohl(block[2]);
00191 /*@=boundsread@*/
00192     if (il < 0 || il > 32) {
00193         (void) snprintf(buf, sizeof(buf),
00194                 _("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
00195         goto exit;
00196     }
00197 /*@-boundsread@*/
00198     dl = ntohl(block[3]);
00199 /*@=boundsread@*/
00200     if (dl < 0 || dl > 8192) {
00201         (void) snprintf(buf, sizeof(buf),
00202                 _("sigh data: BAD, no. of  bytes(%d) out of range\n"), dl);
00203         goto exit;
00204     }
00205 
00206 /*@-sizeoftype@*/
00207     nb = (il * sizeof(struct entryInfo_s)) + dl;
00208 /*@=sizeoftype@*/
00209     ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
00210 /*@-bounds@*/
00211     ei[0] = block[2];
00212     ei[1] = block[3];
00213     pe = (entryInfo) &ei[2];
00214 /*@=bounds@*/
00215     dataStart = (unsigned char *) (pe + il);
00216     if ((xx = timedRead(fd, (char *)pe, nb)) != nb) {
00217         (void) snprintf(buf, sizeof(buf),
00218                 _("sigh blob(%d): BAD, read returned %d\n"), (int)nb, xx);
00219         goto exit;
00220     }
00221     
00222     /* Check (and convert) the 1st tag element. */
00223     xx = headerVerifyInfo(1, dl, pe, &entry->info, 0);
00224     if (xx != -1) {
00225         (void) snprintf(buf, sizeof(buf),
00226                 _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
00227                 0, entry->info.tag, entry->info.type,
00228                 entry->info.offset, entry->info.count);
00229         goto exit;
00230     }
00231 
00232     /* Is there an immutable header region tag? */
00233 /*@-sizeoftype@*/
00234     if (entry->info.tag == RPMTAG_HEADERSIGNATURES
00235        && entry->info.type == RPM_BIN_TYPE
00236        && entry->info.count == REGION_TAG_COUNT)
00237     {
00238 /*@=sizeoftype@*/
00239 
00240         if (entry->info.offset >= dl) {
00241             (void) snprintf(buf, sizeof(buf),
00242                 _("region offset: BAD, tag %d type %d offset %d count %d\n"),
00243                 entry->info.tag, entry->info.type,
00244                 entry->info.offset, entry->info.count);
00245             goto exit;
00246         }
00247 
00248         /* Is there an immutable header region tag trailer? */
00249         dataEnd = dataStart + entry->info.offset;
00250 /*@-sizeoftype@*/
00251 /*@-bounds@*/
00252         (void) memcpy(info, dataEnd, REGION_TAG_COUNT);
00253 /*@=bounds@*/
00254         dataEnd += REGION_TAG_COUNT;
00255 
00256         xx = headerVerifyInfo(1, dl, info, &entry->info, 1);
00257         if (xx != -1 ||
00258             !(entry->info.tag == RPMTAG_HEADERSIGNATURES
00259            && entry->info.type == RPM_BIN_TYPE
00260            && entry->info.count == REGION_TAG_COUNT))
00261         {
00262             (void) snprintf(buf, sizeof(buf),
00263                 _("region trailer: BAD, tag %d type %d offset %d count %d\n"),
00264                 entry->info.tag, entry->info.type,
00265                 entry->info.offset, entry->info.count);
00266             goto exit;
00267         }
00268 /*@=sizeoftype@*/
00269 /*@-boundswrite@*/
00270         memset(info, 0, sizeof(*info));
00271 /*@=boundswrite@*/
00272 
00273         /* Is the no. of tags in the region less than the total no. of tags? */
00274         ril = entry->info.offset/sizeof(*pe);
00275         if ((entry->info.offset % sizeof(*pe)) || ril > il) {
00276             (void) snprintf(buf, sizeof(buf),
00277                 _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
00278             goto exit;
00279         }
00280     }
00281 
00282     /* Sanity check signature tags */
00283 /*@-boundswrite@*/
00284     memset(info, 0, sizeof(*info));
00285 /*@=boundswrite@*/
00286     for (i = 1; i < il; i++) {
00287         xx = headerVerifyInfo(1, dl, pe+i, &entry->info, 0);
00288         if (xx != -1) {
00289             (void) snprintf(buf, sizeof(buf),
00290                 _("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
00291                 i, entry->info.tag, entry->info.type,
00292                 entry->info.offset, entry->info.count);
00293             goto exit;
00294         }
00295     }
00296 
00297     /* OK, blob looks sane, load the header. */
00298     sigh = headerLoad(ei);
00299     if (sigh == NULL) {
00300         (void) snprintf(buf, sizeof(buf), _("sigh load: BAD\n"));
00301         goto exit;
00302     }
00303     sigh->flags |= HEADERFLAG_ALLOCATED;
00304 
00305     {   int sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
00306         int pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
00307         int_32 * archSize = NULL;
00308 
00309         /* Position at beginning of header. */
00310         if (pad && (xx = timedRead(fd, (char *)block, pad)) != pad) {
00311             (void) snprintf(buf, sizeof(buf),
00312                 _("sigh pad(%d): BAD, read %d bytes\n"), pad, xx);
00313             goto exit;
00314         }
00315 
00316         /* Print package component sizes. */
00317         if (headerGetEntry(sigh, RPMSIGTAG_SIZE, NULL,(void **)&archSize, NULL))
00318             rc = printSize(fd, sigSize, pad, *archSize);
00319     }
00320 
00321 exit:
00322 /*@-boundswrite@*/
00323     if (sighp && sigh && rc == RPMRC_OK)
00324         *sighp = headerLink(sigh);
00325     sigh = headerFree(sigh);
00326 
00327     if (msg != NULL) {
00328         buf[sizeof(buf)-1] = '\0';
00329         *msg = xstrdup(buf);
00330     }
00331 /*@=boundswrite@*/
00332 
00333     return rc;
00334 }
00335 
00336 int rpmWriteSignature(FD_t fd, Header h)
00337 {
00338     static byte buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
00339     int sigSize, pad;
00340     int rc;
00341 
00342     rc = headerWrite(fd, h, HEADER_MAGIC_YES);
00343     if (rc)
00344         return rc;
00345 
00346     sigSize = headerSizeof(h, HEADER_MAGIC_YES);
00347     pad = (8 - (sigSize % 8)) % 8;
00348     if (pad) {
00349 /*@-boundswrite@*/
00350         if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
00351             rc = 1;
00352 /*@=boundswrite@*/
00353     }
00354     rpmMessage(RPMMESS_DEBUG, _("Signature: size(%d)+pad(%d)\n"), sigSize, pad);
00355     return rc;
00356 }
00357 
00358 Header rpmNewSignature(void)
00359 {
00360     Header h = headerNew();
00361     return h;
00362 }
00363 
00364 Header rpmFreeSignature(Header h)
00365 {
00366     return headerFree(h);
00367 }
00368 
00377 static int makePGPSignature(const char * file, /*@out@*/ byte ** pkt,
00378                 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
00379         /*@globals errno, rpmGlobalMacroContext, h_errno,
00380                 fileSystem, internalState @*/
00381         /*@modifies errno, *pkt, *pktlen, rpmGlobalMacroContext,
00382                 fileSystem, internalState @*/
00383 {
00384     char * sigfile = alloca(1024);
00385     int pid, status;
00386     int inpipe[2];
00387     struct stat st;
00388     const char * cmd;
00389     char *const *av;
00390     int rc;
00391 
00392 /*@-boundswrite@*/
00393     (void) stpcpy( stpcpy(sigfile, file), ".sig");
00394 /*@=boundswrite@*/
00395 
00396     addMacro(NULL, "__plaintext_filename", NULL, file, -1);
00397     addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
00398 
00399     inpipe[0] = inpipe[1] = 0;
00400 /*@-boundsread@*/
00401     (void) pipe(inpipe);
00402 /*@=boundsread@*/
00403 
00404     if (!(pid = fork())) {
00405         const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
00406         const char *path;
00407         pgpVersion pgpVer;
00408 
00409         (void) close(STDIN_FILENO);
00410         (void) dup2(inpipe[0], 3);
00411         (void) close(inpipe[1]);
00412 
00413         (void) dosetenv("PGPPASSFD", "3", 1);
00414 /*@-boundsread@*/
00415         if (pgp_path && *pgp_path != '\0')
00416             (void) dosetenv("PGPPATH", pgp_path, 1);
00417 /*@=boundsread@*/
00418 
00419         /* dosetenv("PGPPASS", passPhrase, 1); */
00420 
00421         unsetenv("MALLOC_CHECK_");
00422 #if defined(__GLIBC__)
00423 
00427         {
00428            char* bypassVar = (char*) malloc(1024*sizeof(char));
00429            if (bypassVar != NULL)
00430            {
00431               snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00432               bypassVar[1023] = '\0';
00433               if (getenv(bypassVar) != NULL)
00434               {
00435                  char* bypassVal = (char*) malloc(1024*sizeof(char));
00436                  if (bypassVal != NULL)
00437                  {
00438                     rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00439                     snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00440                     unsetenv(bypassVar);
00441                     snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00442                     bypassVar[1023] = '\0';
00443                     putenv(bypassVar);
00444                     free(bypassVal);
00445                  }
00446                  else
00447                  {
00448                     free(bypassVar);
00449                  }
00450               }
00451            }
00452         }
00453 #endif
00454         if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
00455             switch(pgpVer) {
00456             case PGP_2:
00457                 cmd = rpmExpand("%{?__pgp_sign_cmd}", NULL);
00458                 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00459 /*@-boundsread@*/
00460                 if (!rc)
00461                     rc = execve(av[0], av+1, environ);
00462 /*@=boundsread@*/
00463                 break;
00464             case PGP_5:
00465                 cmd = rpmExpand("%{?__pgp5_sign_cmd}", NULL);
00466                 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00467 /*@-boundsread@*/
00468                 if (!rc)
00469                     rc = execve(av[0], av+1, environ);
00470 /*@=boundsread@*/
00471                 break;
00472             case PGP_UNKNOWN:
00473             case PGP_NOTDETECTED:
00474                 errno = ENOENT;
00475                 break;
00476             }
00477         }
00478         rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
00479                         strerror(errno));
00480         _exit(RPMERR_EXEC);
00481     }
00482 
00483     delMacro(NULL, "__plaintext_filename");
00484     delMacro(NULL, "__signature_filename");
00485 
00486     (void) close(inpipe[0]);
00487     if (passPhrase)
00488         (void) write(inpipe[1], passPhrase, strlen(passPhrase));
00489     (void) write(inpipe[1], "\n", 1);
00490     (void) close(inpipe[1]);
00491 
00492     (void)waitpid(pid, &status, 0);
00493     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
00494         rpmError(RPMERR_SIGGEN, _("pgp failed\n"));
00495         return 1;
00496     }
00497 
00498     if (stat(sigfile, &st)) {
00499         /* PGP failed to write signature */
00500         if (sigfile) (void) unlink(sigfile);  /* Just in case */
00501         rpmError(RPMERR_SIGGEN, _("pgp failed to write signature\n"));
00502         return 1;
00503     }
00504 
00505 /*@-boundswrite@*/
00506     *pktlen = st.st_size;
00507     rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *pktlen);
00508     *pkt = xmalloc(*pktlen);
00509 /*@=boundswrite@*/
00510 
00511 /*@-boundsread@*/
00512     {   FD_t fd;
00513 
00514         rc = 0;
00515         fd = Fopen(sigfile, "r.fdio");
00516         if (fd != NULL && !Ferror(fd)) {
00517             rc = timedRead(fd, *pkt, *pktlen);
00518             if (sigfile) (void) unlink(sigfile);
00519             (void) Fclose(fd);
00520         }
00521         if (rc != *pktlen) {
00522 /*@-boundswrite@*/
00523             *pkt = _free(*pkt);
00524 /*@=boundswrite@*/
00525             rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
00526             return 1;
00527         }
00528     }
00529 
00530     rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *pktlen);
00531 /*@=boundsread@*/
00532 
00533     return 0;
00534 }
00535 
00544 static int makeGPGSignature(const char * file, /*@out@*/ byte ** pkt,
00545                 /*@out@*/ int_32 * pktlen, /*@null@*/ const char * passPhrase)
00546         /*@globals rpmGlobalMacroContext, h_errno,
00547                 fileSystem, internalState @*/
00548         /*@modifies *pkt, *pktlen, rpmGlobalMacroContext,
00549                 fileSystem, internalState @*/
00550 {
00551     char * sigfile = alloca(1024);
00552     int pid, status;
00553     int inpipe[2];
00554     FILE * fpipe;
00555     struct stat st;
00556     const char * cmd;
00557     char *const *av;
00558     int rc;
00559 
00560 /*@-boundswrite@*/
00561     (void) stpcpy( stpcpy(sigfile, file), ".sig");
00562 /*@=boundswrite@*/
00563 
00564     addMacro(NULL, "__plaintext_filename", NULL, file, -1);
00565     addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
00566 
00567     inpipe[0] = inpipe[1] = 0;
00568 /*@-boundsread@*/
00569     (void) pipe(inpipe);
00570 /*@=boundsread@*/
00571 
00572     if (!(pid = fork())) {
00573         const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
00574 
00575         (void) close(STDIN_FILENO);
00576         (void) dup2(inpipe[0], 3);
00577         (void) close(inpipe[1]);
00578 
00579 /*@-boundsread@*/
00580         if (gpg_path && *gpg_path != '\0')
00581             (void) dosetenv("GNUPGHOME", gpg_path, 1);
00582 /*@=boundsread@*/
00583 
00584         unsetenv("MALLOC_CHECK_");
00585 
00586 #if defined(__GLIBC__)
00587 
00591         {
00592            char* bypassVar = (char*) malloc(1024*sizeof(char));
00593            if (bypassVar != NULL)
00594            {
00595               snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00596               bypassVar[1023] = '\0';
00597               if (getenv(bypassVar) != NULL)
00598               {
00599                  char* bypassVal = (char*) malloc(1024*sizeof(char));
00600                  if (bypassVal != NULL)
00601                  {
00602                     rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00603                     snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00604                     unsetenv(bypassVar);
00605                     snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00606                     bypassVar[1023] = '\0';
00607                     putenv(bypassVar);
00608                     free(bypassVal);
00609                  }
00610                  else
00611                  {
00612                     free(bypassVar);
00613                  }
00614               }
00615            }
00616         }
00617 #endif
00618         cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
00619         rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00620 /*@-boundsread@*/
00621         if (!rc)
00622             rc = execve(av[0], av+1, environ);
00623 /*@=boundsread@*/
00624 
00625         rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
00626                         strerror(errno));
00627         _exit(RPMERR_EXEC);
00628     }
00629 
00630     delMacro(NULL, "__plaintext_filename");
00631     delMacro(NULL, "__signature_filename");
00632 
00633     fpipe = fdopen(inpipe[1], "w");
00634     (void) close(inpipe[0]);
00635     if (fpipe) {
00636         fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
00637         (void) fclose(fpipe);
00638     }
00639 
00640     (void) waitpid(pid, &status, 0);
00641     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
00642         rpmError(RPMERR_SIGGEN, _("gpg failed\n"));
00643         return 1;
00644     }
00645 
00646     if (stat(sigfile, &st)) {
00647         /* GPG failed to write signature */
00648         if (sigfile) (void) unlink(sigfile);  /* Just in case */
00649         rpmError(RPMERR_SIGGEN, _("gpg failed to write signature\n"));
00650         return 1;
00651     }
00652 
00653 /*@-boundswrite@*/
00654     *pktlen = st.st_size;
00655     rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *pktlen);
00656     *pkt = xmalloc(*pktlen);
00657 /*@=boundswrite@*/
00658 
00659 /*@-boundsread@*/
00660     {   FD_t fd;
00661 
00662         rc = 0;
00663         fd = Fopen(sigfile, "r.fdio");
00664         if (fd != NULL && !Ferror(fd)) {
00665             rc = timedRead(fd, *pkt, *pktlen);
00666             if (sigfile) (void) unlink(sigfile);
00667             (void) Fclose(fd);
00668         }
00669         if (rc != *pktlen) {
00670 /*@-boundswrite@*/
00671             *pkt = _free(*pkt);
00672 /*@=boundswrite@*/
00673             rpmError(RPMERR_SIGGEN, _("unable to read the signature\n"));
00674             return 1;
00675         }
00676     }
00677 
00678     rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *pktlen);
00679 /*@=boundsread@*/
00680 
00681     return 0;
00682 }
00683 
00692 static int makeHDRSignature(Header sig, const char * file, int_32 sigTag,
00693                 /*@null@*/ const char * passPhrase)
00694         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00695         /*@modifies sig, rpmGlobalMacroContext, fileSystem, internalState @*/
00696 {
00697     Header h = NULL;
00698     FD_t fd = NULL;
00699     byte * pkt;
00700     int_32 pktlen;
00701     const char * fn = NULL;
00702     const char * SHA1 = NULL;
00703     int ret = -1;       /* assume failure. */
00704 
00705     switch (sigTag) {
00706     case RPMSIGTAG_SIZE:
00707     case RPMSIGTAG_MD5:
00708     case RPMSIGTAG_PGP5:        /* XXX legacy */
00709     case RPMSIGTAG_PGP:
00710     case RPMSIGTAG_GPG:
00711         goto exit;
00712         /*@notreached@*/ break;
00713     case RPMSIGTAG_SHA1:
00714         fd = Fopen(file, "r.fdio");
00715         if (fd == NULL || Ferror(fd))
00716             goto exit;
00717         h = headerRead(fd, HEADER_MAGIC_YES);
00718         if (h == NULL)
00719             goto exit;
00720         (void) Fclose(fd);      fd = NULL;
00721 
00722         if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
00723             DIGEST_CTX ctx;
00724             void * uh;
00725             int_32 uht, uhc;
00726         
00727             if (!headerGetEntry(h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)
00728              ||  uh == NULL)
00729             {
00730                 h = headerFree(h);
00731                 goto exit;
00732             }
00733             ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
00734             (void) rpmDigestUpdate(ctx, header_magic, sizeof(header_magic));
00735             (void) rpmDigestUpdate(ctx, uh, uhc);
00736             (void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
00737             uh = headerFreeData(uh, uht);
00738         }
00739         h = headerFree(h);
00740 
00741         if (SHA1 == NULL)
00742             goto exit;
00743         if (!headerAddEntry(sig, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
00744             goto exit;
00745         ret = 0;
00746         break;
00747     case RPMSIGTAG_DSA:
00748         fd = Fopen(file, "r.fdio");
00749         if (fd == NULL || Ferror(fd))
00750             goto exit;
00751         h = headerRead(fd, HEADER_MAGIC_YES);
00752         if (h == NULL)
00753             goto exit;
00754         (void) Fclose(fd);      fd = NULL;
00755         if (makeTempFile(NULL, &fn, &fd))
00756             goto exit;
00757         if (headerWrite(fd, h, HEADER_MAGIC_YES))
00758             goto exit;
00759         (void) Fclose(fd);      fd = NULL;
00760         if (makeGPGSignature(fn, &pkt, &pktlen, passPhrase)
00761          || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
00762             goto exit;
00763         ret = 0;
00764         break;
00765     case RPMSIGTAG_RSA:
00766         fd = Fopen(file, "r.fdio");
00767         if (fd == NULL || Ferror(fd))
00768             goto exit;
00769         h = headerRead(fd, HEADER_MAGIC_YES);
00770         if (h == NULL)
00771             goto exit;
00772         (void) Fclose(fd);      fd = NULL;
00773         if (makeTempFile(NULL, &fn, &fd))
00774             goto exit;
00775         if (headerWrite(fd, h, HEADER_MAGIC_YES))
00776             goto exit;
00777         (void) Fclose(fd);      fd = NULL;
00778         if (makePGPSignature(fn, &pkt, &pktlen, passPhrase)
00779          || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
00780             goto exit;
00781         ret = 0;
00782         break;
00783     }
00784 
00785 exit:
00786     if (fn) {
00787         (void) unlink(fn);
00788         fn = _free(fn);
00789     }
00790     SHA1 = _free(SHA1);
00791     h = headerFree(h);
00792     if (fd != NULL) (void) Fclose(fd);
00793     return ret;
00794 }
00795 
00796 int rpmAddSignature(Header sig, const char * file, int_32 sigTag,
00797                 const char * passPhrase)
00798 {
00799     struct stat st;
00800     byte * pkt;
00801     int_32 pktlen;
00802     int ret = -1;       /* assume failure. */
00803 
00804     switch (sigTag) {
00805     case RPMSIGTAG_SIZE:
00806         if (stat(file, &st) != 0)
00807             break;
00808         pktlen = st.st_size;
00809         if (!headerAddEntry(sig, sigTag, RPM_INT32_TYPE, &pktlen, 1))
00810             break;
00811         ret = 0;
00812         break;
00813     case RPMSIGTAG_MD5:
00814         pktlen = 16;
00815         pkt = memset(alloca(pktlen), 0, pktlen);
00816         if (domd5(file, pkt, 0, NULL)
00817          || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
00818             break;
00819         ret = 0;
00820         break;
00821     case RPMSIGTAG_PGP5:        /* XXX legacy */
00822     case RPMSIGTAG_PGP:
00823         if (makePGPSignature(file, &pkt, &pktlen, passPhrase)
00824          || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
00825             break;
00826 #ifdef  NOTYET  /* XXX needs hdrmd5ctx, like hdrsha1ctx. */
00827         /* XXX Piggyback a header-only RSA signature as well. */
00828         ret = makeHDRSignature(sig, file, RPMSIGTAG_RSA, passPhrase);
00829 #endif
00830         ret = 0;
00831         break;
00832     case RPMSIGTAG_GPG:
00833         if (makeGPGSignature(file, &pkt, &pktlen, passPhrase)
00834          || !headerAddEntry(sig, sigTag, RPM_BIN_TYPE, pkt, pktlen))
00835             break;
00836         /* XXX Piggyback a header-only DSA signature as well. */
00837         ret = makeHDRSignature(sig, file, RPMSIGTAG_DSA, passPhrase);
00838         break;
00839     case RPMSIGTAG_RSA:
00840     case RPMSIGTAG_DSA:
00841     case RPMSIGTAG_SHA1:
00842         ret = makeHDRSignature(sig, file, sigTag, passPhrase);
00843         break;
00844     }
00845 
00846     return ret;
00847 }
00848 
00849 static int checkPassPhrase(const char * passPhrase, const int sigTag)
00850         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00851         /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
00852 {
00853     int passPhrasePipe[2];
00854     int pid, status;
00855     int rc;
00856     int xx;
00857 
00858     passPhrasePipe[0] = passPhrasePipe[1] = 0;
00859 /*@-boundsread@*/
00860     xx = pipe(passPhrasePipe);
00861 /*@=boundsread@*/
00862     if (!(pid = fork())) {
00863         const char * cmd;
00864         char *const *av;
00865         int fdno;
00866 
00867         xx = close(STDIN_FILENO);
00868         xx = close(STDOUT_FILENO);
00869         xx = close(passPhrasePipe[1]);
00870         if (! rpmIsVerbose())
00871             xx = close(STDERR_FILENO);
00872         if ((fdno = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
00873             xx = dup2(fdno, STDIN_FILENO);
00874             xx = close(fdno);
00875         }
00876         if ((fdno = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
00877             xx = dup2(fdno, STDOUT_FILENO);
00878             xx = close(fdno);
00879         }
00880         xx = dup2(passPhrasePipe[0], 3);
00881 
00882         unsetenv("MALLOC_CHECK_");
00883 #if defined(__GLIBC__)
00884 
00888         {
00889            char* bypassVar = (char*) malloc(1024*sizeof(char));
00890            if (bypassVar != NULL)
00891            {
00892               snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00893               bypassVar[1023] = '\0';
00894               if (getenv(bypassVar) != NULL)
00895               {
00896                  char* bypassVal = (char*) malloc(1024*sizeof(char));
00897                  if (bypassVal != NULL)
00898                  {
00899                     rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00900                     snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00901                     unsetenv(bypassVar);
00902                     snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00903                     bypassVar[1023] = '\0';
00904                     putenv(bypassVar);
00905                     free(bypassVal);
00906                  }
00907                  else
00908                  {
00909                     free(bypassVar);
00910                  }
00911               }
00912            }
00913         }
00914 #endif
00915         switch (sigTag) {
00916         case RPMSIGTAG_DSA:
00917         case RPMSIGTAG_GPG:
00918         {   const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
00919 
00920 /*@-boundsread@*/
00921             if (gpg_path && *gpg_path != '\0')
00922                 (void) dosetenv("GNUPGHOME", gpg_path, 1);
00923 /*@=boundsread@*/
00924 
00925             cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
00926             rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00927 /*@-boundsread@*/
00928             if (!rc)
00929                 rc = execve(av[0], av+1, environ);
00930 /*@=boundsread@*/
00931 
00932             rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "gpg",
00933                         strerror(errno));
00934         }   /*@notreached@*/ break;
00935         case RPMSIGTAG_RSA:
00936         case RPMSIGTAG_PGP5:    /* XXX legacy */
00937         case RPMSIGTAG_PGP:
00938         {   const char *pgp_path = rpmExpand("%{?_pgp_path}", NULL);
00939             const char *path;
00940             pgpVersion pgpVer;
00941 
00942             (void) dosetenv("PGPPASSFD", "3", 1);
00943 /*@-boundsread@*/
00944             if (pgp_path && *pgp_path != '\0')
00945                 xx = dosetenv("PGPPATH", pgp_path, 1);
00946 /*@=boundsread@*/
00947 
00948             if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
00949                 switch(pgpVer) {
00950                 case PGP_2:
00951                     cmd = rpmExpand("%{?__pgp_check_password_cmd}", NULL);
00952                     rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00953 /*@-boundsread@*/
00954                     if (!rc)
00955                         rc = execve(av[0], av+1, environ);
00956 /*@=boundsread@*/
00957                     /*@innerbreak@*/ break;
00958                 case PGP_5:     /* XXX legacy */
00959                     cmd = rpmExpand("%{?__pgp5_check_password_cmd}", NULL);
00960                     rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
00961 /*@-boundsread@*/
00962                     if (!rc)
00963                         rc = execve(av[0], av+1, environ);
00964 /*@=boundsread@*/
00965                     /*@innerbreak@*/ break;
00966                 case PGP_UNKNOWN:
00967                 case PGP_NOTDETECTED:
00968                     /*@innerbreak@*/ break;
00969                 }
00970             }
00971             rpmError(RPMERR_EXEC, _("Could not exec %s: %s\n"), "pgp",
00972                         strerror(errno));
00973             _exit(RPMERR_EXEC);
00974         }   /*@notreached@*/ break;
00975         default: /* This case should have been screened out long ago. */
00976             rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
00977             _exit(RPMERR_SIGGEN);
00978             /*@notreached@*/ break;
00979         }
00980     }
00981 
00982     xx = close(passPhrasePipe[0]);
00983     xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
00984     xx = write(passPhrasePipe[1], "\n", 1);
00985     xx = close(passPhrasePipe[1]);
00986 
00987     (void) waitpid(pid, &status, 0);
00988 
00989     return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
00990 }
00991 
00992 char * rpmGetPassPhrase(const char * prompt, const int sigTag)
00993 {
00994     char *pass;
00995     int aok;
00996 
00997     switch (sigTag) {
00998     case RPMSIGTAG_DSA:
00999     case RPMSIGTAG_GPG:
01000 /*@-boundsread@*/
01001       { const char *name = rpmExpand("%{?_gpg_name}", NULL);
01002         aok = (name && *name != '\0');
01003         name = _free(name);
01004       }
01005 /*@=boundsread@*/
01006         if (!aok) {
01007             rpmError(RPMERR_SIGGEN,
01008                 _("You must set \"%%_gpg_name\" in your macro file\n"));
01009             return NULL;
01010         }
01011         break;
01012     case RPMSIGTAG_RSA:
01013     case RPMSIGTAG_PGP5:        /* XXX legacy */
01014     case RPMSIGTAG_PGP:
01015 /*@-boundsread@*/
01016       { const char *name = rpmExpand("%{?_pgp_name}", NULL);
01017         aok = (name && *name != '\0');
01018         name = _free(name);
01019       }
01020 /*@=boundsread@*/
01021         if (!aok) {
01022             rpmError(RPMERR_SIGGEN,
01023                 _("You must set \"%%_pgp_name\" in your macro file\n"));
01024             return NULL;
01025         }
01026         break;
01027     default:
01028         /* Currently the calling function (rpm.c:main) is checking this and
01029          * doing a better job.  This section should never be accessed.
01030          */
01031         rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file\n"));
01032         return NULL;
01033         /*@notreached@*/ break;
01034     }
01035 
01036 /*@-moduncon -nullpass @*/
01037     pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
01038 /*@=moduncon -nullpass @*/
01039 
01040     if (checkPassPhrase(pass, sigTag))
01041         return NULL;
01042 
01043     return pass;
01044 }
01045 
01046 static /*@observer@*/ const char * rpmSigString(rpmRC res)
01047         /*@*/
01048 {
01049     const char * str;
01050     switch (res) {
01051     case RPMRC_OK:              str = "OK";             break;
01052     case RPMRC_FAIL:            str = "BAD";            break;
01053     case RPMRC_NOKEY:           str = "NOKEY";          break;
01054     case RPMRC_NOTTRUSTED:      str = "NOTRUSTED";      break;
01055     default:
01056     case RPMRC_NOTFOUND:        str = "UNKNOWN";        break;
01057     }
01058     return str;
01059 }
01060 
01061 /*@-boundswrite@*/
01062 static rpmRC
01063 verifySizeSignature(const rpmts ts, /*@out@*/ char * t)
01064         /*@modifies *t @*/
01065 {
01066     const void * sig = rpmtsSig(ts);
01067     pgpDig dig = rpmtsDig(ts);
01068     rpmRC res;
01069     int_32 size = 0x7fffffff;
01070 
01071     *t = '\0';
01072     t = stpcpy(t, _("Header+Payload size: "));
01073 
01074     if (sig == NULL || dig == NULL || dig->nbytes == 0) {
01075         res = RPMRC_NOKEY;
01076         t = stpcpy(t, rpmSigString(res));
01077         goto exit;
01078     }
01079 
01080     memcpy(&size, sig, sizeof(size));
01081 
01082     if (size != dig->nbytes) {
01083         res = RPMRC_FAIL;
01084         t = stpcpy(t, rpmSigString(res));
01085         sprintf(t, " Expected(%d) != (%d)\n", (int)size, (int)dig->nbytes);
01086     } else {
01087         res = RPMRC_OK;
01088         t = stpcpy(t, rpmSigString(res));
01089         sprintf(t, " (%d)", (int)dig->nbytes);
01090     }
01091 
01092 exit:
01093     t = stpcpy(t, "\n");
01094     return res;
01095 }
01096 /*@=boundswrite@*/
01097 
01098 /*@-boundswrite@*/
01099 static rpmRC
01100 verifyMD5Signature(const rpmts ts, /*@out@*/ char * t,
01101                 /*@null@*/ DIGEST_CTX md5ctx)
01102         /*@globals internalState @*/
01103         /*@modifies *t, internalState @*/
01104 {
01105     const void * sig = rpmtsSig(ts);
01106     int_32 siglen = rpmtsSiglen(ts);
01107     pgpDig dig = rpmtsDig(ts);
01108     rpmRC res;
01109     byte * md5sum = NULL;
01110     size_t md5len = 0;
01111 
01112     *t = '\0';
01113     t = stpcpy(t, _("MD5 digest: "));
01114 
01115     if (md5ctx == NULL || sig == NULL || dig == NULL) {
01116         res = RPMRC_NOKEY;
01117         t = stpcpy(t, rpmSigString(res));
01118         goto exit;
01119     }
01120 
01121     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01122     (void) rpmDigestFinal(rpmDigestDup(md5ctx),
01123                 (void **)&md5sum, &md5len, 0);
01124     (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01125 
01126     if (md5len != siglen || memcmp(md5sum, sig, md5len)) {
01127         res = RPMRC_FAIL;
01128         t = stpcpy(t, rpmSigString(res));
01129         t = stpcpy(t, " Expected(");
01130         (void) pgpHexCvt(t, sig, siglen);
01131         t += strlen(t);
01132         t = stpcpy(t, ") != (");
01133     } else {
01134         res = RPMRC_OK;
01135         t = stpcpy(t, rpmSigString(res));
01136         t = stpcpy(t, " (");
01137     }
01138     (void) pgpHexCvt(t, md5sum, md5len);
01139     t += strlen(t);
01140     t = stpcpy(t, ")");
01141 
01142 exit:
01143     md5sum = _free(md5sum);
01144     t = stpcpy(t, "\n");
01145     return res;
01146 }
01147 /*@=boundswrite@*/
01148 
01149 /*@-boundswrite@*/
01157 static rpmRC
01158 verifySHA1Signature(const rpmts ts, /*@out@*/ char * t,
01159                 /*@null@*/ DIGEST_CTX sha1ctx)
01160         /*@globals internalState @*/
01161         /*@modifies *t, internalState @*/
01162 {
01163     const void * sig = rpmtsSig(ts);
01164 #ifdef  NOTYET
01165     int_32 siglen = rpmtsSiglen(ts);
01166 #endif
01167     pgpDig dig = rpmtsDig(ts);
01168     rpmRC res;
01169     const char * SHA1 = NULL;
01170 
01171     *t = '\0';
01172     t = stpcpy(t, _("Header SHA1 digest: "));
01173 
01174     if (sha1ctx == NULL || sig == NULL || dig == NULL) {
01175         res = RPMRC_NOKEY;
01176         t = stpcpy(t, rpmSigString(res));
01177         goto exit;
01178     }
01179 
01180     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01181     (void) rpmDigestFinal(rpmDigestDup(sha1ctx),
01182                 (void **)&SHA1, NULL, 1);
01183     (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01184 
01185     if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
01186         res = RPMRC_FAIL;
01187         t = stpcpy(t, rpmSigString(res));
01188         t = stpcpy(t, " Expected(");
01189         t = stpcpy(t, sig);
01190         t = stpcpy(t, ") != (");
01191     } else {
01192         res = RPMRC_OK;
01193         t = stpcpy(t, rpmSigString(res));
01194         t = stpcpy(t, " (");
01195     }
01196     if (SHA1)
01197         t = stpcpy(t, SHA1);
01198     t = stpcpy(t, ")");
01199 
01200 exit:
01201     SHA1 = _free(SHA1);
01202     t = stpcpy(t, "\n");
01203     return res;
01204 }
01205 /*@=boundswrite@*/
01206 
01212 static inline unsigned char nibble(char c)
01213         /*@*/
01214 {
01215     if (c >= '0' && c <= '9')
01216         return (c - '0');
01217     if (c >= 'A' && c <= 'F')
01218         return (c - 'A') + 10;
01219     if (c >= 'a' && c <= 'f')
01220         return (c - 'a') + 10;
01221     return 0;
01222 }
01223 
01224 /*@-boundswrite@*/
01232 static rpmRC
01233 verifyPGPSignature(rpmts ts, /*@out@*/ char * t,
01234                 /*@null@*/ DIGEST_CTX md5ctx)
01235         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01236         /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
01237 {
01238     const void * sig = rpmtsSig(ts);
01239 #ifdef  NOTYET
01240     int_32 siglen = rpmtsSiglen(ts);
01241 #endif
01242     int_32 sigtag = rpmtsSigtag(ts);
01243     pgpDig dig = rpmtsDig(ts);
01244     pgpDigParams sigp = rpmtsSignature(ts);
01245     rpmRC res;
01246     int xx;
01247 
01248     *t = '\0';
01249     t = stpcpy(t, _("V3 RSA/MD5 signature: "));
01250 
01251     if (md5ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
01252         res = RPMRC_NOKEY;
01253         goto exit;
01254     }
01255 
01256     /* XXX sanity check on sigtag and signature agreement. */
01257     if (!(sigtag == RPMSIGTAG_PGP
01258         && sigp->pubkey_algo == PGPPUBKEYALGO_RSA
01259         && sigp->hash_algo == PGPHASHALGO_MD5))
01260     {
01261         res = RPMRC_NOKEY;
01262         goto exit;
01263     }
01264 
01265     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01266     {   DIGEST_CTX ctx = rpmDigestDup(md5ctx);
01267         byte signhash16[2];
01268         const char * s;
01269 
01270         if (sigp->hash != NULL)
01271             xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
01272 
01273 #ifdef  NOTYET  /* XXX not for binary/text document signatures. */
01274         if (sigp->sigtype == 4) {
01275             int nb = dig->nbytes + sigp->hashlen;
01276             byte trailer[6];
01277             nb = htonl(nb);
01278             trailer[0] = 0x4;
01279             trailer[1] = 0xff;
01280             memcpy(trailer+2, &nb, sizeof(nb));
01281             xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
01282         }
01283 #endif
01284 
01285         xx = rpmDigestFinal(ctx, (void **)&dig->md5, &dig->md5len, 1);
01286         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
01287 
01288         /* Compare leading 16 bits of digest for quick check. */
01289         s = dig->md5;
01290         signhash16[0] = (nibble(s[0]) << 4) | nibble(s[1]);
01291         signhash16[1] = (nibble(s[2]) << 4) | nibble(s[3]);
01292         if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
01293             res = RPMRC_FAIL;
01294             goto exit;
01295         }
01296 
01297     }
01298 
01299     {   const char * prefix = "3020300c06082a864886f70d020505000410";
01300         unsigned int nbits = 1024;
01301         unsigned int nb = (nbits + 7) >> 3;
01302         const char * hexstr;
01303         char * tt;
01304 
01305         hexstr = tt = xmalloc(2 * nb + 1);
01306         memset(tt, 'f', (2 * nb));
01307         tt[0] = '0'; tt[1] = '0';
01308         tt[2] = '0'; tt[3] = '1';
01309         tt += (2 * nb) - strlen(prefix) - strlen(dig->md5) - 2;
01310         *tt++ = '0'; *tt++ = '0';
01311         tt = stpcpy(tt, prefix);
01312         tt = stpcpy(tt, dig->md5);
01313 
01314         mpnzero(&dig->rsahm);   mpnsethex(&dig->rsahm, hexstr);
01315 
01316         hexstr = _free(hexstr);
01317 
01318     }
01319 
01320     /* Retrieve the matching public key. */
01321     res = rpmtsFindPubkey(ts);
01322     if (res != RPMRC_OK)
01323         goto exit;
01324 
01325     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
01326 #if HAVE_BEECRYPT_API_H
01327     xx = rsavrfy(&dig->rsa_pk.n, &dig->rsa_pk.e, &dig->rsahm, &dig->c);
01328 #else
01329     xx = rsavrfy(&dig->rsa_pk, &dig->rsahm, &dig->c);
01330 #endif
01331     if (xx)
01332         res = RPMRC_OK;
01333     else
01334         res = RPMRC_FAIL;
01335     (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
01336 
01337 exit:
01338     t = stpcpy(t, rpmSigString(res));
01339     if (sigp != NULL) {
01340         t = stpcpy(t, ", key ID ");
01341         (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
01342         t += strlen(t);
01343     }
01344     t = stpcpy(t, "\n");
01345     return res;
01346 }
01347 /*@=boundswrite@*/
01348 
01356 /*@-boundswrite@*/
01357 static rpmRC
01358 verifyGPGSignature(rpmts ts, /*@out@*/ char * t,
01359                 /*@null@*/ DIGEST_CTX sha1ctx)
01360         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01361         /*@modifies ts, *t, rpmGlobalMacroContext, fileSystem, internalState */
01362 {
01363     const void * sig = rpmtsSig(ts);
01364 #ifdef  NOTYET
01365     int_32 siglen = rpmtsSiglen(ts);
01366 #endif
01367     int_32 sigtag = rpmtsSigtag(ts);
01368     pgpDig dig = rpmtsDig(ts);
01369     pgpDigParams sigp = rpmtsSignature(ts);
01370     rpmRC res;
01371     int xx;
01372 
01373     *t = '\0';
01374     if (dig != NULL && dig->hdrsha1ctx == sha1ctx)
01375         t = stpcpy(t, _("Header "));
01376     t = stpcpy(t, _("V3 DSA signature: "));
01377 
01378     if (sha1ctx == NULL || sig == NULL || dig == NULL || sigp == NULL) {
01379         res = RPMRC_NOKEY;
01380         goto exit;
01381     }
01382 
01383     /* XXX sanity check on sigtag and signature agreement. */
01384     if (!((sigtag == RPMSIGTAG_GPG || sigtag == RPMSIGTAG_DSA)
01385         && sigp->pubkey_algo == PGPPUBKEYALGO_DSA
01386         && sigp->hash_algo == PGPHASHALGO_SHA1))
01387     {
01388         res = RPMRC_NOKEY;
01389         goto exit;
01390     }
01391 
01392     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DIGEST), 0);
01393     {   DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
01394         byte signhash16[2];
01395 
01396         if (sigp->hash != NULL)
01397             xx = rpmDigestUpdate(ctx, sigp->hash, sigp->hashlen);
01398 
01399 #ifdef  NOTYET  /* XXX not for binary/text document signatures. */
01400         if (sigp->sigtype == 4) {
01401             int nb = dig->nbytes + sigp->hashlen;
01402             byte trailer[6];
01403             nb = htonl(nb);
01404             trailer[0] = 0x4;
01405             trailer[1] = 0xff;
01406             memcpy(trailer+2, &nb, sizeof(nb));
01407             xx = rpmDigestUpdate(ctx, trailer, sizeof(trailer));
01408         }
01409 #endif
01410         xx = rpmDigestFinal(ctx, (void **)&dig->sha1, &dig->sha1len, 1);
01411         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DIGEST), sigp->hashlen);
01412 
01413         mpnzero(&dig->hm);      mpnsethex(&dig->hm, dig->sha1);
01414 
01415         /* Compare leading 16 bits of digest for quick check. */
01416         signhash16[0] = (*dig->hm.data >> 24) & 0xff;
01417         signhash16[1] = (*dig->hm.data >> 16) & 0xff;
01418         if (memcmp(signhash16, sigp->signhash16, sizeof(signhash16))) {
01419             res = RPMRC_FAIL;
01420             goto exit;
01421         }
01422     }
01423 
01424     /* Retrieve the matching public key. */
01425     res = rpmtsFindPubkey(ts);
01426     if (res != RPMRC_OK)
01427         goto exit;
01428 
01429     (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
01430     if (dsavrfy(&dig->p, &dig->q, &dig->g,
01431                 &dig->hm, &dig->y, &dig->r, &dig->s))
01432         res = RPMRC_OK;
01433     else
01434         res = RPMRC_FAIL;
01435     (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_SIGNATURE), 0);
01436 
01437 exit:
01438     t = stpcpy(t, rpmSigString(res));
01439     if (sigp != NULL) {
01440         t = stpcpy(t, ", key ID ");
01441         (void) pgpHexCvt(t, sigp->signid+4, sizeof(sigp->signid)-4);
01442         t += strlen(t);
01443     }
01444     t = stpcpy(t, "\n");
01445     return res;
01446 }
01447 /*@=boundswrite@*/
01448 
01449 rpmRC
01450 rpmVerifySignature(const rpmts ts, char * result)
01451 {
01452     const void * sig = rpmtsSig(ts);
01453     int_32 siglen = rpmtsSiglen(ts);
01454     int_32 sigtag = rpmtsSigtag(ts);
01455     pgpDig dig = rpmtsDig(ts);
01456     rpmRC res;
01457 
01458     if (sig == NULL || siglen <= 0 || dig == NULL) {
01459         sprintf(result, _("Verify signature: BAD PARAMETERS\n"));
01460         return RPMRC_NOTFOUND;
01461     }
01462 
01463     switch (sigtag) {
01464     case RPMSIGTAG_SIZE:
01465         res = verifySizeSignature(ts, result);
01466         break;
01467     case RPMSIGTAG_MD5:
01468         res = verifyMD5Signature(ts, result, dig->md5ctx);
01469         break;
01470     case RPMSIGTAG_SHA1:
01471         res = verifySHA1Signature(ts, result, dig->hdrsha1ctx);
01472         break;
01473     case RPMSIGTAG_RSA:
01474     case RPMSIGTAG_PGP5:        /* XXX legacy */
01475     case RPMSIGTAG_PGP:
01476         res = verifyPGPSignature(ts, result, dig->md5ctx);
01477         break;
01478     case RPMSIGTAG_DSA:
01479         res = verifyGPGSignature(ts, result, dig->hdrsha1ctx);
01480         break;
01481     case RPMSIGTAG_GPG:
01482         res = verifyGPGSignature(ts, result, dig->sha1ctx);
01483         break;
01484     case RPMSIGTAG_LEMD5_1:
01485     case RPMSIGTAG_LEMD5_2:
01486         sprintf(result, _("Broken MD5 digest: UNSUPPORTED\n"));
01487         res = RPMRC_NOTFOUND;
01488         break;
01489     default:
01490         sprintf(result, _("Signature: UNKNOWN (%d)\n"), sigtag);
01491         res = RPMRC_NOTFOUND;
01492         break;
01493     }
01494     return res;
01495 }

Generated on Mon Sep 26 13:35:54 2005 for rpm by  doxygen 1.3.9.1