diff options
Diffstat (limited to 'rpmio')
-rw-r--r-- | rpmio/digest.c | 49 | ||||
-rw-r--r-- | rpmio/rpmio.c | 24 | ||||
-rw-r--r-- | rpmio/rpmio_internal.h | 57 | ||||
-rw-r--r-- | rpmio/tdigest.c | 34 | ||||
-rw-r--r-- | rpmio/tkey.c | 4 |
5 files changed, 110 insertions, 58 deletions
diff --git a/rpmio/digest.c b/rpmio/digest.c index 4c51914ee..af4f8c02d 100644 --- a/rpmio/digest.c +++ b/rpmio/digest.c @@ -23,8 +23,9 @@ */ struct DIGEST_CTX_s { rpmDigestFlags flags; /*!< Bit(s) to control digest operation. */ - uint32 digestlen; /*!< No. bytes of digest. */ uint32 datalen; /*!< No. bytes in block of plaintext data. */ + uint32 paramlen; /*!< No. bytes of digest parameters. */ + uint32 digestlen; /*!< No. bytes of digest. */ void * param; /*!< Digest parameters. */ int (*Reset) (void * param) /*@modifies param @*/; /*!< Digest initialize. */ @@ -37,60 +38,70 @@ struct DIGEST_CTX_s { DIGEST_CTX rpmDigestDup(DIGEST_CTX octx) { - DIGEST_CTX nctx = xcalloc(1, sizeof(*nctx)); - memcpy(nctx, octx, sizeof(*nctx)); + DIGEST_CTX nctx = memcpy(xcalloc(1, sizeof(*nctx)), octx, sizeof(*nctx)); + nctx->param = memcpy(xcalloc(1, nctx->paramlen), octx->param, nctx->paramlen); return nctx; } DIGEST_CTX -rpmDigestInit(rpmDigestFlags flags) +rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags) { DIGEST_CTX ctx = xcalloc(1, sizeof(*ctx)); + int xx; ctx->flags = flags; - if (flags & RPMDIGEST_MD5) { + switch (hashalgo) { + case PGPHASHALGO_MD5: ctx->digestlen = 16; ctx->datalen = 64; /*@-sizeoftype@*/ /* FIX: union, not void pointer */ - ctx->param = xcalloc(1, sizeof(md5Param)); + ctx->paramlen = sizeof(md5Param); /*@=sizeoftype@*/ + ctx->param = xcalloc(1, ctx->paramlen); /*@-type@*/ /* FIX: cast? */ ctx->Reset = (void *) md5Reset; ctx->Update = (void *) md5Update; ctx->Digest = (void *) md5Digest; /*@=type@*/ - } - - if (flags & RPMDIGEST_SHA1) { + break; + case PGPHASHALGO_SHA1: ctx->digestlen = 20; ctx->datalen = 64; /*@-sizeoftype@*/ /* FIX: union, not void pointer */ - ctx->param = xcalloc(1, sizeof(sha1Param)); + ctx->paramlen = sizeof(sha1Param); /*@=sizeoftype@*/ + ctx->param = xcalloc(1, ctx->paramlen); /*@-type@*/ /* FIX: cast? */ ctx->Reset = (void *) sha1Reset; ctx->Update = (void *) sha1Update; ctx->Digest = (void *) sha1Digest; /*@=type@*/ + break; + case PGPHASHALGO_RIPEMD160: + case PGPHASHALGO_MD2: + case PGPHASHALGO_TIGER192: + case PGPHASHALGO_HAVAL_5_160: + default: + free(ctx); + return NULL; + /*@notreached@*/ break; } - /*@-noeffectuncon@*/ /* FIX: check rc */ - (void) (*ctx->Reset) (ctx->param); - /*@=noeffectuncon@*/ /* FIX: check rc */ + xx = (*ctx->Reset) (ctx->param); DPRINTF((stderr, "*** Init(%x) ctx %p param %p\n", flags, ctx, ctx->param)); return ctx; } -void +/*@-mustmod@*/ /* LCL: ctx->param may be modified, but ctx is abstract @*/ +int rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len) { DPRINTF((stderr, "*** Update(%p,%p,%d) param %p \"%s\"\n", ctx, data, len, ctx->param, ((char *)data))); - /*@-noeffectuncon@*/ /* FIX: check rc */ - (void) (*ctx->Update) (ctx->param, data, len); - /*@=noeffectuncon@*/ + return (*ctx->Update) (ctx->param, data, len); } +/*@=mustmod@*/ /*@unchecked@*/ static int _ie = 0x44332211; @@ -104,7 +115,7 @@ static union _dendian { #define IS_BIG_ENDIAN() (_endian->b[0] == '\x44') #define IS_LITTLE_ENDIAN() (_endian->b[0] == '\x11') -void +int rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx, /*@out@*/ void ** datap, /*@out@*/ size_t *lenp, int asAscii) { @@ -150,7 +161,9 @@ DPRINTF((stderr, "*** Final(%p,%p,%p,%d) param %p digest %p\n", ctx, datap, lenp memset(digest, 0, ctx->digestlen); /* In case it's sensitive */ free(digest); } + memset(ctx->param, 0, ctx->paramlen); /* In case it's sensitive */ free(ctx->param); memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ free(ctx); + return 0; } diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index 91a9edb6c..36478fc33 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -15,6 +15,18 @@ #if HAVE_NETINET_IN_SYSTM_H # include <sys/types.h> + +#if defined(__LCLINT__) +/*@-redef@*/ /* FIX: rpmdb/db3.c also declares */ +typedef unsigned int u_int32_t; +typedef unsigned short u_int16_t; +typedef unsigned char u_int8_t; +/*@-incondefs@*/ /* LCLint 3.0.0.15 */ +typedef int int32_t; +/*@=incondefs@*/ +/*@=redef@*/ +#endif + # include <netinet/in_systm.h> #endif @@ -358,7 +370,7 @@ ssize_t fdRead(void * cookie, /*@out@*/ char * buf, size_t count) rc = read(fdFileno(fd), buf, (count > fd->bytesRemain ? fd->bytesRemain : count)); fdstat_exit(fd, FDSTAT_READ, rc); - if (fd->digest && rc > 0) rpmDigestUpdate(fd->digest, buf, rc); + if (fd->digest && rc > 0) (void) rpmDigestUpdate(fd->digest, buf, rc); /*@-modfilesys@*/ DBGIO(fd, (stderr, "==>\tfdRead(%p,%p,%ld) rc %ld %s\n", cookie, buf, (long)count, (long)rc, fdbg(fd))); @@ -377,7 +389,7 @@ ssize_t fdWrite(void * cookie, const char * buf, size_t count) if (fd->bytesRemain == 0) return 0; /* XXX simulate EOF */ - if (fd->digest && count > 0) rpmDigestUpdate(fd->digest, buf, count); + if (fd->digest && count > 0) (void) rpmDigestUpdate(fd->digest, buf, count); if (fd->wr_chunked) { char chunksize[20]; @@ -2176,7 +2188,7 @@ DBGIO(fd, (stderr, "==>\tgzdRead(%p,%p,%u) rc %lx %s\n", cookie, buf, (unsigned) } else if (rc >= 0) { fdstat_exit(fd, FDSTAT_READ, rc); /*@-compdef@*/ - if (fd->digest && rc > 0) rpmDigestUpdate(fd->digest, buf, rc); + if (fd->digest && rc > 0) (void) rpmDigestUpdate(fd->digest, buf, rc); /*@=compdef@*/ } return rc; @@ -2193,7 +2205,7 @@ static ssize_t gzdWrite(void * cookie, const char * buf, size_t count) if (fd == NULL || fd->bytesRemain == 0) return 0; /* XXX simulate EOF */ - if (fd->digest && count > 0) rpmDigestUpdate(fd->digest, buf, count); + if (fd->digest && count > 0) (void) rpmDigestUpdate(fd->digest, buf, count); gzfile = gzdFileno(fd); fdstat_enter(fd, FDSTAT_WRITE); @@ -2410,7 +2422,7 @@ static ssize_t bzdRead(void * cookie, /*@out@*/ char * buf, size_t count) } else if (rc >= 0) { fdstat_exit(fd, FDSTAT_READ, rc); /*@-compdef@*/ - if (fd->digest && rc > 0) rpmDigestUpdate(fd->digest, buf, rc); + if (fd->digest && rc > 0) (void) rpmDigestUpdate(fd->digest, buf, rc); /*@=compdef@*/ } return rc; @@ -2429,7 +2441,7 @@ static ssize_t bzdWrite(void * cookie, const char * buf, size_t count) if (fd->bytesRemain == 0) return 0; /* XXX simulate EOF */ - if (fd->digest && count > 0) rpmDigestUpdate(fd->digest, buf, count); + if (fd->digest && count > 0) (void) rpmDigestUpdate(fd->digest, buf, count); bzfile = bzdFileno(fd); fdstat_enter(fd, FDSTAT_WRITE); diff --git a/rpmio/rpmio_internal.h b/rpmio/rpmio_internal.h index c91df8740..7fc3db883 100644 --- a/rpmio/rpmio_internal.h +++ b/rpmio/rpmio_internal.h @@ -8,6 +8,7 @@ #include <rpmio.h> #include <rpmurl.h> +#include <rpmpgp.h> /** \ingroup rpmio */ @@ -49,14 +50,15 @@ typedef /*@abstract@*/ struct { * Bit(s) to control digest operation. */ typedef enum rpmDigestFlags_e { + RPMDIGEST_NONE = 0, +#ifdef DYING RPMDIGEST_MD5 = (1 << 0), /*!< MD5 digest. */ RPMDIGEST_SHA1 = (1 << 1) /*!< SHA1 digest. */ -#ifdef DYING RPMDIGEST_REVERSE = (1 << 16), /*!< Should bytes be reversed? */ RPMDIGEST_BCSWAP = (1 << 17), /*!< Should bit count be reversed? */ +#define RPMDIGEST_MASK 0xffff #endif } rpmDigestFlags; -#define RPMDIGEST_MASK 0xffff /** */ @@ -78,7 +80,7 @@ DIGEST_CTX rpmDigestDup(DIGEST_CTX octx) * @return digest context */ /*@only@*/ -DIGEST_CTX rpmDigestInit(rpmDigestFlags flags) +DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags) /*@*/; /** \ingroup rpmio @@ -86,8 +88,9 @@ DIGEST_CTX rpmDigestInit(rpmDigestFlags flags) * @param ctx digest context * @param data next data buffer * @param len no. bytes of data + * @return 0 on success */ -void rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len) +int rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len) /*@modifies ctx @*/; /** \ingroup rpmio @@ -99,8 +102,9 @@ void rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len) * @retval datap address of returned digest * @retval lenp address of digest length * @param asAscii return digest as ascii string? + * @return 0 on success */ -void rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx, +int rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx, /*@null@*/ /*@out@*/ void ** datap, /*@null@*/ /*@out@*/ size_t * lenp, int asAscii) /*@modifies *datap, *lenp @*/; @@ -448,14 +452,44 @@ FD_t c2f(/*@null@*/ void * cookie) /** \ingroup rpmio */ /*@unused@*/ static inline +void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags) + /*@modifies fd @*/ +{ + fd->digest = rpmDigestInit(hashalgo, flags); +} + +/** \ingroup rpmio + */ +/*@unused@*/ static inline +void fdFiniDigest(FD_t fd, + /*@null@*/ /*@out@*/ void ** datap, + /*@null@*/ /*@out@*/ size_t * lenp, + int asAscii) + /*@modifies fd, *datap, *lenp @*/ +{ + if (fd->digest == NULL) { + if (datap) *datap = NULL; + if (lenp) *lenp = 0; + return; + } + /*@-mayaliasunique@*/ + (void) rpmDigestFinal(fd->digest, datap, lenp, asAscii); + /*@=mayaliasunique@*/ + fd->digest = NULL; +} + +#ifdef DYING +/** \ingroup rpmio + */ +/*@unused@*/ static inline void fdInitMD5(FD_t fd, int flags) /*@modifies fd @*/ { #ifdef DYING if (flags) flags = RPMDIGEST_REVERSE; -#endif flags |= RPMDIGEST_MD5; - fd->digest = rpmDigestInit(flags); +#endif + fd->digest = rpmDigestInit(PGPHASHALGO_MD5, flags); } /** \ingroup rpmio @@ -466,9 +500,9 @@ void fdInitSHA1(FD_t fd, int flags) { #ifdef DYING if (flags) flags = RPMDIGEST_REVERSE; -#endif flags |= RPMDIGEST_SHA1; - fd->digest = rpmDigestInit(flags); +#endif + fd->digest = rpmDigestInit(PGPHASHALGO_SHA1, flags); } /** \ingroup rpmio @@ -486,7 +520,7 @@ void fdFiniMD5(FD_t fd, return; } /*@-mayaliasunique@*/ - rpmDigestFinal(fd->digest, datap, lenp, asAscii); + (void) rpmDigestFinal(fd->digest, datap, lenp, asAscii); /*@=mayaliasunique@*/ fd->digest = NULL; } @@ -506,10 +540,11 @@ void fdFiniSHA1(FD_t fd, return; } /*@-mayaliasunique@*/ - rpmDigestFinal(fd->digest, datap, lenp, asAscii); + (void) rpmDigestFinal(fd->digest, datap, lenp, asAscii); /*@=mayaliasunique@*/ fd->digest = NULL; } +#endif /*@-shadow@*/ /** \ingroup rpmio diff --git a/rpmio/tdigest.c b/rpmio/tdigest.c index ab9627ef2..da850ce08 100644 --- a/rpmio/tdigest.c +++ b/rpmio/tdigest.c @@ -4,7 +4,8 @@ #include "debug.h" -static rpmDigestFlags flags = RPMDIGEST_MD5; +static pgpHashAlgo hashalgo = PGPHASHALGO_MD5; +static rpmDigestFlags flags = RPMDIGEST_NONE; extern int _rpmio_debug; static int fips = 0; @@ -14,14 +15,14 @@ const char * FIPSBdigest = "84983e441c3bd26ebaae4aa1f95129e5e54670f1"; const char * FIPSCdigest = "34aa973cd4c4daa4f61eeb2bdbad27316534016f"; static struct poptOption optionsTable[] = { - { "md5", '\0', POPT_BIT_SET, &flags, RPMDIGEST_MD5, NULL, NULL }, - { "sha1",'\0', POPT_BIT_SET, &flags, RPMDIGEST_SHA1, NULL, NULL }, + { "md5", '\0', POPT_ARG_VAL, &hashalgo, PGPHASHALGO_MD5, NULL, NULL }, + { "sha1",'\0', POPT_ARG_VAL, &hashalgo, PGPHASHALGO_SHA1, NULL, NULL }, #ifdef DYING { "reverse",'\0', POPT_BIT_SET, &flags, RPMDIGEST_REVERSE, NULL, NULL }, #endif - { "fipsa",'\0', POPT_BIT_SET, &fips, 1, NULL, NULL }, - { "fipsb",'\0', POPT_BIT_SET, &fips, 2, NULL, NULL }, - { "fipsc",'\0', POPT_BIT_SET, &fips, 3, NULL, NULL }, + { "fipsa",'\0', POPT_ARG_VAL, &fips, 1, NULL, NULL }, + { "fipsb",'\0', POPT_ARG_VAL, &fips, 2, NULL, NULL }, + { "fipsc",'\0', POPT_ARG_VAL, &fips, 3, NULL, NULL }, { "debug",'d', POPT_ARG_VAL, &_rpmio_debug, -1, NULL, NULL }, POPT_AUTOHELP POPT_TABLEEND @@ -53,14 +54,11 @@ main(int argc, const char *argv[]) while ((rc = poptGetNextOpt(optCon)) > 0) ; - if (flags & RPMDIGEST_SHA1) flags &= ~RPMDIGEST_MD5; #ifdef DYING reverse = (flags & RPMDIGEST_REVERSE); #endif if (fips) { - flags &= ~RPMDIGEST_MD5; - flags |= RPMDIGEST_SHA1; - ctx = rpmDigestInit(flags); + ctx = rpmDigestInit(PGPHASHALGO_SHA1, flags); ifn = NULL; appendix = ' '; sdigest = NULL; @@ -117,7 +115,7 @@ main(int argc, const char *argv[]) se = buf; *se = '\0'; - se = stpcpy(se, ((flags & RPMDIGEST_SHA1) ? SHA1_CMD : MD5_CMD)); + se = stpcpy(se, ((hashalgo == PGPHASHALGO_SHA1) ? SHA1_CMD : MD5_CMD)); *se++ = ' '; se = stpcpy(se, ifn); if ((sfp = popen(buf, "r")) != NULL) { @@ -137,7 +135,7 @@ main(int argc, const char *argv[]) continue; } idigest = NULL; - (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ifd, reverse) : fdInitMD5(ifd, reverse); + fdInitDigest(ifd, hashalgo, reverse); ofd = Fopen(ofn, "w.ufdio"); if (ofd == NULL || Ferror(ofd)) { @@ -148,24 +146,20 @@ main(int argc, const char *argv[]) continue; } odigest = NULL; - (flags & RPMDIGEST_SHA1) ? fdInitSHA1(ofd, reverse) : fdInitMD5(ofd, reverse); + fdInitDigest(ofd, hashalgo, reverse); - ctx = rpmDigestInit(flags); + ctx = rpmDigestInit(hashalgo, flags); while ((nb = Fread(buf, 1, sizeof(buf), ifd)) > 0) { rpmDigestUpdate(ctx, buf, nb); (void) Fwrite(buf, 1, nb, ofd); } - (flags & RPMDIGEST_SHA1) - ? fdFiniSHA1(ifd, (void **)&idigest, NULL, asAscii) - : fdFiniMD5(ifd, (void **)&idigest, NULL, asAscii); + fdFiniDigest(ifd, (void **)&idigest, NULL, asAscii); Fclose(ifd); Fflush(ofd); - (flags & RPMDIGEST_SHA1) - ? fdFiniSHA1(ofd, (void **)&odigest, NULL, asAscii) - : fdFiniMD5(ofd, (void **)&odigest, NULL, asAscii); + fdFiniDigest(ofd, (void **)&odigest, NULL, asAscii); Fclose(ofd); rpmDigestFinal(ctx, (void **)&digest, &digestlen, asAscii); diff --git a/rpmio/tkey.c b/rpmio/tkey.c index e03094bfb..caab06612 100644 --- a/rpmio/tkey.c +++ b/rpmio/tkey.c @@ -143,13 +143,11 @@ fprintf(stderr, "=============================== GPG Signature of \"abc\"\n"); if ((rc = doit(abcSignatureDSA, dig, printing)) != 0) fprintf(stderr, "==> FAILED: rc %d\n", rc); - { DIGEST_CTX ctx = rpmDigestInit(RPMDIGEST_SHA1); + { DIGEST_CTX ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE); const char * digest = NULL; size_t digestlen = 0; const char * txt = "abc"; - ctx = rpmDigestInit(RPMDIGEST_SHA1); - rpmDigestUpdate(ctx, txt, strlen(txt)); rpmDigestUpdate(ctx, &dig->sig.v3.sigtype, dig->sig.v3.hashlen); rpmDigestFinal(ctx, (void **)&digest, &digestlen, 1); |