diff options
Diffstat (limited to 'rpmio')
-rw-r--r-- | rpmio/rpmpgp.c | 165 | ||||
-rw-r--r-- | rpmio/rpmpgp.h | 214 |
2 files changed, 158 insertions, 221 deletions
diff --git a/rpmio/rpmpgp.c b/rpmio/rpmpgp.c index 8db097e6e..50cc5c1b1 100644 --- a/rpmio/rpmpgp.c +++ b/rpmio/rpmpgp.c @@ -202,6 +202,22 @@ static void pgpPrtStr(const char *pre, const char *s) fprintf(stderr, " %s", s); } +/** \ingroup rpmpgp + * Return string representation of am OpenPGP value. + * @param vs table of (string,value) pairs + * @param val byte value to lookup + * @return string value of byte + */ +static inline +const char * pgpValStr(pgpValTbl vs, uint8_t val) +{ + do { + if (vs->val == val) + break; + } while ((++vs)->val != -1); + return vs->str; +} + static void pgpPrtHex(const char *pre, const uint8_t *p, size_t plen) { char *hex = NULL; @@ -213,7 +229,7 @@ static void pgpPrtHex(const char *pre, const uint8_t *p, size_t plen) free(hex); } -void pgpPrtVal(const char * pre, pgpValTbl vs, uint8_t val) +static void pgpPrtVal(const char * pre, pgpValTbl vs, uint8_t val) { if (!_print) return; if (pre && *pre) @@ -221,6 +237,60 @@ void pgpPrtVal(const char * pre, pgpValTbl vs, uint8_t val) fprintf(stderr, "%s(%u)", pgpValStr(vs, val), (unsigned)val); } +/** \ingroup rpmpgp + * Return no. of bits in a multiprecision integer. + * @param p pointer to multiprecision integer + * @return no. of bits + */ +static +unsigned int pgpMpiBits(const uint8_t *p) +{ + return ((p[0] << 8) | p[1]); +} + +/** \ingroup rpmpgp + * Return no. of bytes in a multiprecision integer. + * @param p pointer to multiprecision integer + * @return no. of bytes + */ +static +size_t pgpMpiLen(const uint8_t *p) +{ + return (2 + ((pgpMpiBits(p)+7)>>3)); +} + +/** \ingroup rpmpgp + * Return hex formatted representation of a multiprecision integer. + * @param p bytes + * @return hex formatted string (malloc'ed) + */ +static inline +char * pgpMpiStr(const uint8_t *p) +{ + char *str = NULL; + char *hex = pgpHexStr(p+2, pgpMpiLen(p)-2); + rasprintf(&str, "[%4u]: %s", pgpGrab(p, (size_t) 2), hex); + free(hex); + return str; +} + +/** \ingroup rpmpgp + * Return value of an OpenPGP string. + * @param vs table of (string,value) pairs + * @param s string token to lookup + * @param se end-of-string address + * @return byte value + */ +static inline +int pgpValTok(pgpValTbl vs, const char * s, const char * se) +{ + do { + size_t vlen = strlen(vs->str); + if (vlen <= (se-s) && !strncmp(s, vs->str, vlen)) + break; + } while ((++vs)->val != -1); + return vs->val; +} /** * @return 0 on success */ @@ -315,7 +385,88 @@ static SECKEYPublicKey *pgpNewDSAKey(void) return pgpNewPublicKey(dsaKey); } -int pgpPrtSubType(const uint8_t *h, size_t hlen, pgpSigType sigtype) +/** \ingroup rpmpgp + * Is buffer at beginning of an OpenPGP packet? + * @param p buffer + * @return 1 if an OpenPGP packet, 0 otherwise + */ +static inline +int pgpIsPkt(const uint8_t * p) +{ + unsigned int val = *p++; + pgpTag tag; + int rc; + + /* XXX can't deal with these. */ + if (!(val & 0x80)) + return 0; + + if (val & 0x40) + tag = (pgpTag)(val & 0x3f); + else + tag = (pgpTag)((val >> 2) & 0xf); + + switch (tag) { + case PGPTAG_MARKER: + case PGPTAG_SYMMETRIC_SESSION_KEY: + case PGPTAG_ONEPASS_SIGNATURE: + case PGPTAG_PUBLIC_KEY: + case PGPTAG_SECRET_KEY: + case PGPTAG_PUBLIC_SESSION_KEY: + case PGPTAG_SIGNATURE: + case PGPTAG_COMMENT: + case PGPTAG_COMMENT_OLD: + case PGPTAG_LITERAL_DATA: + case PGPTAG_COMPRESSED_DATA: + case PGPTAG_SYMMETRIC_DATA: + rc = 1; + break; + case PGPTAG_PUBLIC_SUBKEY: + case PGPTAG_SECRET_SUBKEY: + case PGPTAG_USER_ID: + case PGPTAG_RESERVED: + case PGPTAG_TRUST: + case PGPTAG_PHOTOID: + case PGPTAG_ENCRYPTED_MDC: + case PGPTAG_MDC: + case PGPTAG_PRIVATE_60: + case PGPTAG_PRIVATE_62: + case PGPTAG_CONTROL: + default: + rc = 0; + break; + } + + return rc; +} + +#define CRC24_INIT 0xb704ce +#define CRC24_POLY 0x1864cfb + +/** \ingroup rpmpgp + * Return CRC of a buffer. + * @param octets bytes + * @param len no. of bytes + * @return crc of buffer + */ +static inline +unsigned int pgpCRC(const uint8_t *octets, size_t len) +{ + unsigned int crc = CRC24_INIT; + size_t i; + + while (len--) { + crc ^= (*octets++) << 16; + for (i = 0; i < 8; i++) { + crc <<= 1; + if (crc & 0x1000000) + crc ^= CRC24_POLY; + } + } + return crc & 0xffffff; +} + +static int pgpPrtSubType(const uint8_t *h, size_t hlen, pgpSigType sigtype) { const uint8_t *p = h; size_t plen, i; @@ -496,7 +647,7 @@ static int pgpPrtSigParams(pgpTag tag, uint8_t pubkey_algo, uint8_t sigtype, return 0; } -int pgpPrtSig(pgpTag tag, const uint8_t *h, size_t hlen) +static int pgpPrtSig(pgpTag tag, const uint8_t *h, size_t hlen) { uint8_t version = h[0]; uint8_t * p; @@ -804,7 +955,7 @@ static const uint8_t * pgpPrtSeckeyParams(uint8_t pubkey_algo, return p; } -int pgpPrtKey(pgpTag tag, const uint8_t *h, size_t hlen) +static int pgpPrtKey(pgpTag tag, const uint8_t *h, size_t hlen) { uint8_t version = *h; const uint8_t * p; @@ -863,7 +1014,7 @@ int pgpPrtKey(pgpTag tag, const uint8_t *h, size_t hlen) return rc; } -int pgpPrtUserID(pgpTag tag, const uint8_t *h, size_t hlen) +static int pgpPrtUserID(pgpTag tag, const uint8_t *h, size_t hlen) { pgpPrtVal("", pgpTagTbl, tag); if (_print) @@ -877,7 +1028,7 @@ int pgpPrtUserID(pgpTag tag, const uint8_t *h, size_t hlen) return 0; } -int pgpPrtComment(pgpTag tag, const uint8_t *h, size_t hlen) +static int pgpPrtComment(pgpTag tag, const uint8_t *h, size_t hlen) { size_t i = hlen; @@ -985,7 +1136,7 @@ int pgpExtractPubkeyFingerprint(const char * b64pkt, pgpKeyID_t keyid) return sizeof(keyid); /* no. of bytes of pubkey signid */ } -int pgpPrtPkt(const uint8_t *pkt, size_t pleft) +static int pgpPrtPkt(const uint8_t *pkt, size_t pleft) { unsigned int val = *pkt; size_t pktlen; diff --git a/rpmio/rpmpgp.h b/rpmio/rpmpgp.h index 2da3250e8..e3f4e9316 100644 --- a/rpmio/rpmpgp.h +++ b/rpmio/rpmpgp.h @@ -1007,28 +1007,6 @@ size_t pgpLen(const uint8_t *s, size_t * lenp) } /** \ingroup rpmpgp - * Return no. of bits in a multiprecision integer. - * @param p pointer to multiprecision integer - * @return no. of bits - */ -static inline -unsigned int pgpMpiBits(const uint8_t *p) -{ - return ((p[0] << 8) | p[1]); -} - -/** \ingroup rpmpgp - * Return no. of bytes in a multiprecision integer. - * @param p pointer to multiprecision integer - * @return no. of bytes - */ -static inline -size_t pgpMpiLen(const uint8_t *p) -{ - return (2 + ((pgpMpiBits(p)+7)>>3)); -} - -/** \ingroup rpmpgp * Return hex formatted representation of bytes. * @param p bytes * @param plen no. of bytes @@ -1037,108 +1015,6 @@ size_t pgpMpiLen(const uint8_t *p) char * pgpHexStr(const uint8_t *p, size_t plen); /** \ingroup rpmpgp - * Return hex formatted representation of a multiprecision integer. - * @param p bytes - * @return hex formatted string (malloc'ed) - */ -static inline -char * pgpMpiStr(const uint8_t *p) -{ - char *str = NULL; - char *hex = pgpHexStr(p+2, pgpMpiLen(p)-2); - rasprintf(&str, "[%4u]: %s", pgpGrab(p, (size_t) 2), hex); - free(hex); - return str; -} - -/** \ingroup rpmpgp - * Return string representation of am OpenPGP value. - * @param vs table of (string,value) pairs - * @param val byte value to lookup - * @return string value of byte - */ -static inline -const char * pgpValStr(pgpValTbl vs, uint8_t val) -{ - do { - if (vs->val == val) - break; - } while ((++vs)->val != -1); - return vs->str; -} - -/** \ingroup rpmpgp - * Return value of an OpenPGP string. - * @param vs table of (string,value) pairs - * @param s string token to lookup - * @param se end-of-string address - * @return byte value - */ -static inline -int pgpValTok(pgpValTbl vs, const char * s, const char * se) -{ - do { - size_t vlen = strlen(vs->str); - if (vlen <= (se-s) && !strncmp(s, vs->str, vlen)) - break; - } while ((++vs)->val != -1); - return vs->val; -} - -/** \ingroup rpmpgp - * Print an OpenPGP value. - * @param pre output prefix - * @param vs table of (string,value) pairs - * @param val byte value to print - */ -void pgpPrtVal(const char * pre, pgpValTbl vs, uint8_t val); - -/** \ingroup rpmpgp - * Print/parse an OpenPGP subtype packet. - * @param h packet - * @param hlen packet length (no. of bytes) - * @param sigtype signature type - * @return 0 on success - */ -int pgpPrtSubType(const uint8_t *h, size_t hlen, pgpSigType sigtype); - -/** \ingroup rpmpgp - * Print/parse an OpenPGP signature packet. - * @param tag packet tag - * @param h packet contents - * @param hlen packet length (no. of bytes) - * @return 0 on success - */ -int pgpPrtSig(pgpTag tag, const uint8_t *h, size_t hlen); - -/** \ingroup rpmpgp - * Print/parse an OpenPGP key packet. - * @param tag packet tag - * @param h packet contents - * @param hlen packet length (no. of bytes) - * @return 0 on success - */ -int pgpPrtKey(pgpTag tag, const uint8_t *h, size_t hlen); - -/** \ingroup rpmpgp - * Print/parse an OpenPGP userid packet. - * @param tag packet tag - * @param h packet contents - * @param hlen packet length (no. of bytes) - * @return 0 on success - */ -int pgpPrtUserID(pgpTag tag, const uint8_t *h, size_t hlen); - -/** \ingroup rpmpgp - * Print/parse an OpenPGP comment packet. - * @param tag packet tag - * @param h packet contents - * @param hlen packet length (no. of bytes) - * @return 0 on success - */ -int pgpPrtComment(pgpTag tag, const uint8_t *h, size_t hlen); - -/** \ingroup rpmpgp * Calculate OpenPGP public key fingerprint. * @todo V3 non-RSA public keys not implemented. * @param pkt OpenPGP packet (i.e. PGPTAG_PUBLIC_KEY) @@ -1158,15 +1034,6 @@ int pgpPubkeyFingerprint(const uint8_t * pkt, size_t pktlen, */ int pgpExtractPubkeyFingerprint(const char * b64pkt, pgpKeyID_t keyid); - -/** \ingroup rpmpgp - * Print/parse next OpenPGP packet. - * @param pkt OpenPGP packet - * @param pleft no. bytes remaining - * @return -1 on error, otherwise this packet length - */ -int pgpPrtPkt(const uint8_t *pkt, size_t pleft); - /** \ingroup rpmpgp * Print/parse a OpenPGP packet(s). * @param pkts OpenPGP packet(s) @@ -1215,87 +1082,6 @@ void pgpCleanDig(pgpDig dig); pgpDig pgpFreeDig(pgpDig dig); /** \ingroup rpmpgp - * Is buffer at beginning of an OpenPGP packet? - * @param p buffer - * @return 1 if an OpenPGP packet, 0 otherwise - */ -static inline -int pgpIsPkt(const uint8_t * p) -{ - unsigned int val = *p++; - pgpTag tag; - int rc; - - /* XXX can't deal with these. */ - if (!(val & 0x80)) - return 0; - - if (val & 0x40) - tag = (pgpTag)(val & 0x3f); - else - tag = (pgpTag)((val >> 2) & 0xf); - - switch (tag) { - case PGPTAG_MARKER: - case PGPTAG_SYMMETRIC_SESSION_KEY: - case PGPTAG_ONEPASS_SIGNATURE: - case PGPTAG_PUBLIC_KEY: - case PGPTAG_SECRET_KEY: - case PGPTAG_PUBLIC_SESSION_KEY: - case PGPTAG_SIGNATURE: - case PGPTAG_COMMENT: - case PGPTAG_COMMENT_OLD: - case PGPTAG_LITERAL_DATA: - case PGPTAG_COMPRESSED_DATA: - case PGPTAG_SYMMETRIC_DATA: - rc = 1; - break; - case PGPTAG_PUBLIC_SUBKEY: - case PGPTAG_SECRET_SUBKEY: - case PGPTAG_USER_ID: - case PGPTAG_RESERVED: - case PGPTAG_TRUST: - case PGPTAG_PHOTOID: - case PGPTAG_ENCRYPTED_MDC: - case PGPTAG_MDC: - case PGPTAG_PRIVATE_60: - case PGPTAG_PRIVATE_62: - case PGPTAG_CONTROL: - default: - rc = 0; - break; - } - - return rc; -} - -#define CRC24_INIT 0xb704ce -#define CRC24_POLY 0x1864cfb - -/** \ingroup rpmpgp - * Return CRC of a buffer. - * @param octets bytes - * @param len no. of bytes - * @return crc of buffer - */ -static inline -unsigned int pgpCRC(const uint8_t *octets, size_t len) -{ - unsigned int crc = CRC24_INIT; - size_t i; - - while (len--) { - crc ^= (*octets++) << 16; - for (i = 0; i < 8; i++) { - crc <<= 1; - if (crc & 0x1000000) - crc ^= CRC24_POLY; - } - } - return crc & 0xffffff; -} - -/** \ingroup rpmpgp * Perform cryptography initialization. * It must be called before any cryptography can be used within rpm. * It's not normally necessary to call it directly as it's called in |