00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #include <string.h>
00014
00015 typedef unsigned char byte;
00016
00019 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00020
00023 typedef const struct pgpValTbl_s {
00024 int val;
00025 const char * str;
00026 } * pgpValTbl;
00027
00035 typedef enum pgpTag_e {
00036 PGPTAG_RESERVED = 0,
00037 PGPTAG_PUBLIC_SESSION_KEY = 1,
00038 PGPTAG_SIGNATURE = 2,
00039 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00040 PGPTAG_ONEPASS_SIGNATURE = 4,
00041 PGPTAG_SECRET_KEY = 5,
00042 PGPTAG_PUBLIC_KEY = 6,
00043 PGPTAG_SECRET_SUBKEY = 7,
00044 PGPTAG_COMPRESSED_DATA = 8,
00045 PGPTAG_SYMMETRIC_DATA = 9,
00046 PGPTAG_MARKER = 10,
00047 PGPTAG_LITERAL_DATA = 11,
00048 PGPTAG_TRUST = 12,
00049 PGPTAG_USER_ID = 13,
00050 PGPTAG_PUBLIC_SUBKEY = 14,
00051 PGPTAG_COMMENT_OLD = 16,
00052 PGPTAG_PHOTOID = 17,
00053 PGPTAG_ENCRYPTED_MDC = 18,
00054 PGPTAG_MDC = 19,
00055 PGPTAG_PRIVATE_60 = 60,
00056 PGPTAG_COMMENT = 61,
00057 PGPTAG_PRIVATE_62 = 62,
00058 PGPTAG_CONTROL = 63
00059 } pgpTag;
00060
00063
00064 extern struct pgpValTbl_s pgpTagTbl[];
00065
00100 typedef struct pgpPktPubkey_s {
00101 byte version;
00102 byte keyid[8];
00103 byte algo;
00104 } pgpPktPubkey;
00105
00106
00113
00114 typedef enum pgpSigType_e {
00115 PGPSIGTYPE_BINARY = 0x00,
00116 PGPSIGTYPE_TEXT = 0x01,
00117 PGPSIGTYPE_STANDALONE = 0x02,
00118 PGPSIGTYPE_GENERIC_CERT = 0x10,
00120 PGPSIGTYPE_PERSONA_CERT = 0x11,
00122 PGPSIGTYPE_CASUAL_CERT = 0x12,
00124 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00126 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00127 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00128 PGPSIGTYPE_KEY_REVOKE = 0x20,
00129 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00130 PGPSIGTYPE_CERT_REVOKE = 0x30,
00131 PGPSIGTYPE_TIMESTAMP = 0x40
00132 } pgpSigType;
00133
00134
00137
00138 extern struct pgpValTbl_s pgpSigTypeTbl[];
00139
00163
00164 typedef enum pgpPubkeyAlgo_e {
00165 PGPPUBKEYALGO_RSA = 1,
00166 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00167 PGPPUBKEYALGO_RSA_SIGN = 3,
00168 PGPPUBKEYALGO_ELGAMAL_ENCRYPT = 16,
00169 PGPPUBKEYALGO_DSA = 17,
00170 PGPPUBKEYALGO_EC = 18,
00171 PGPPUBKEYALGO_ECDSA = 19,
00172 PGPPUBKEYALGO_ELGAMAL = 20,
00173 PGPPUBKEYALGO_DH = 21
00174 } pgpPubkeyAlgo;
00175
00176
00179
00180 extern struct pgpValTbl_s pgpPubkeyTbl[];
00181
00206
00207 typedef enum pgpSymkeyAlgo_e {
00208 PGPSYMKEYALGO_PLAINTEXT = 0,
00209 PGPSYMKEYALGO_IDEA = 1,
00210 PGPSYMKEYALGO_TRIPLE_DES = 2,
00211 PGPSYMKEYALGO_CAST5 = 3,
00212 PGPSYMKEYALGO_BLOWFISH = 4,
00213 PGPSYMKEYALGO_SAFER = 5,
00214 PGPSYMKEYALGO_DES_SK = 6,
00215 PGPSYMKEYALGO_AES_128 = 7,
00216 PGPSYMKEYALGO_AES_192 = 8,
00217 PGPSYMKEYALGO_AES_256 = 9,
00218 PGPSYMKEYALGO_TWOFISH = 10,
00219 PGPSYMKEYALGO_NOENCRYPT = 110
00220 } pgpSymkeyAlgo;
00221
00222
00226
00227 extern struct pgpValTbl_s pgpSymkeyTbl[];
00228
00244
00245 typedef enum pgpCompressAlgo_e {
00246 PGPCOMPRESSALGO_NONE = 0,
00247 PGPCOMPRESSALGO_ZIP = 1,
00248 PGPCOMPRESSALGO_ZLIB = 2,
00249 PGPCOMPRESSALGO_BZIP2 = 3
00250 } pgpCompressAlgo;
00251
00252
00256
00257 extern struct pgpValTbl_s pgpCompressionTbl[];
00258
00280 typedef enum pgpHashAlgo_e {
00281 PGPHASHALGO_MD5 = 1,
00282 PGPHASHALGO_SHA1 = 2,
00283 PGPHASHALGO_RIPEMD160 = 3,
00284 PGPHASHALGO_MD2 = 5,
00285 PGPHASHALGO_TIGER192 = 6,
00286 PGPHASHALGO_HAVAL_5_160 = 7,
00287 PGPHASHALGO_SHA256 = 8,
00288 PGPHASHALGO_SHA384 = 9,
00289 PGPHASHALGO_SHA512 = 10,
00290 } pgpHashAlgo;
00291
00295
00296 extern struct pgpValTbl_s pgpHashTbl[];
00297
00319 typedef struct pgpPktSigV3_s {
00320 byte version;
00321 byte hashlen;
00322 byte sigtype;
00323 byte time[4];
00324 byte signid[8];
00325 byte pubkey_algo;
00326 byte hash_algo;
00327 byte signhash16[2];
00328 } * pgpPktSigV3;
00329
00351 typedef struct pgpPktSigV4_s {
00352 byte version;
00353 byte sigtype;
00354 byte pubkey_algo;
00355 byte hash_algo;
00356 byte hashlen[2];
00357 } * pgpPktSigV4;
00358
00425
00426 typedef enum pgpSubType_e {
00427 PGPSUBTYPE_NONE = 0,
00428 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00429 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00430 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00431 PGPSUBTYPE_TRUST_SIG = 5,
00432 PGPSUBTYPE_REGEX = 6,
00433 PGPSUBTYPE_REVOCABLE = 7,
00434 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00435 PGPSUBTYPE_ARR = 10,
00436 PGPSUBTYPE_PREFER_SYMKEY = 11,
00437 PGPSUBTYPE_REVOKE_KEY = 12,
00438 PGPSUBTYPE_ISSUER_KEYID = 16,
00439 PGPSUBTYPE_NOTATION = 20,
00440 PGPSUBTYPE_PREFER_HASH = 21,
00441 PGPSUBTYPE_PREFER_COMPRESS = 22,
00442 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00443 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00444 PGPSUBTYPE_PRIMARY_USERID = 25,
00445 PGPSUBTYPE_POLICY_URL = 26,
00446 PGPSUBTYPE_KEY_FLAGS = 27,
00447 PGPSUBTYPE_SIGNER_USERID = 28,
00448 PGPSUBTYPE_REVOKE_REASON = 29,
00449 PGPSUBTYPE_FEATURES = 30,
00450 PGPSUBTYPE_EMBEDDED_SIG = 32,
00452 PGPSUBTYPE_INTERNAL_100 = 100,
00453 PGPSUBTYPE_INTERNAL_101 = 101,
00454 PGPSUBTYPE_INTERNAL_102 = 102,
00455 PGPSUBTYPE_INTERNAL_103 = 103,
00456 PGPSUBTYPE_INTERNAL_104 = 104,
00457 PGPSUBTYPE_INTERNAL_105 = 105,
00458 PGPSUBTYPE_INTERNAL_106 = 106,
00459 PGPSUBTYPE_INTERNAL_107 = 107,
00460 PGPSUBTYPE_INTERNAL_108 = 108,
00461 PGPSUBTYPE_INTERNAL_109 = 109,
00462 PGPSUBTYPE_INTERNAL_110 = 110,
00464 PGPSUBTYPE_CRITICAL = 128
00465 } pgpSubType;
00466
00467
00471
00472 extern struct pgpValTbl_s pgpSubTypeTbl[];
00473
00494 typedef union pgpPktSig_u {
00495 struct pgpPktSigV3_s v3;
00496 struct pgpPktSigV4_s v4;
00497 } * pgpPktSig;
00498
00527 typedef struct pgpPktSymkey_s {
00528 byte version;
00529 byte symkey_algo;
00530 byte s2k[1];
00531 } pgpPktSymkey;
00532
00562 typedef struct pgpPktOnepass_s {
00563 byte version;
00564 byte sigtype;
00565 byte hash_algo;
00566 byte pubkey_algo;
00567 byte signid[8];
00568 byte nested;
00569 } * pgpPktOnepass;
00570
00643 typedef struct pgpPktKeyV3_s {
00644 byte version;
00645 byte time[4];
00646 byte valid[2];
00647 byte pubkey_algo;
00648 } * pgpPktKeyV3;
00649
00681 typedef struct pgpPktKeyV4_s {
00682 byte version;
00683 byte time[4];
00684 byte pubkey_algo;
00685 } * pgpPktKeyV4;
00686
00751 typedef union pgpPktKey_u {
00752 struct pgpPktKeyV3_s v3;
00753 struct pgpPktKeyV4_s v4;
00754 } pgpPktKey;
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780 typedef struct pgpPktCdata_s {
00781 byte compressalgo;
00782 byte data[1];
00783 } pgpPktCdata;
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 typedef struct pgpPktEdata_s {
00820 byte data[1];
00821 } pgpPktEdata;
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870 typedef struct pgpPktLdata_s {
00871 byte format;
00872 byte filenamelen;
00873 byte filename[1];
00874 } pgpPktLdata;
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889 typedef struct pgpPktTrust_s {
00890 byte flag;
00891 } pgpPktTrust;
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903 typedef struct pgpPktUid_s {
00904 byte userid[1];
00905 } pgpPktUid;
00906
00909 union pgpPktPre_u {
00910 pgpPktPubkey pubkey;
00911 pgpPktSig sig;
00912 pgpPktSymkey symkey;
00913 pgpPktOnepass onepass;
00914 pgpPktKey key;
00915 pgpPktCdata cdata;
00916 pgpPktEdata edata;
00918 pgpPktLdata ldata;
00919 pgpPktTrust tdata;
00920 pgpPktUid uid;
00921 };
00922
00925
00926 typedef enum pgpArmor_e {
00927 PGPARMOR_ERR_CRC_CHECK = -7,
00928 PGPARMOR_ERR_BODY_DECODE = -6,
00929 PGPARMOR_ERR_CRC_DECODE = -5,
00930 PGPARMOR_ERR_NO_END_PGP = -4,
00931 PGPARMOR_ERR_UNKNOWN_PREAMBLE_TAG = -3,
00932 PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE = -2,
00933 PGPARMOR_ERR_NO_BEGIN_PGP = -1,
00934 #define PGPARMOR_ERROR PGPARMOR_ERR_NO_BEGIN_PGP
00935 PGPARMOR_NONE = 0,
00936 PGPARMOR_MESSAGE = 1,
00937 PGPARMOR_PUBKEY = 2,
00938 PGPARMOR_SIGNATURE = 3,
00939 PGPARMOR_SIGNED_MESSAGE = 4,
00940 PGPARMOR_FILE = 5,
00941 PGPARMOR_PRIVKEY = 6,
00942 PGPARMOR_SECKEY = 7
00943 } pgpArmor;
00944
00945
00949
00950 extern struct pgpValTbl_s pgpArmorTbl[];
00951
00954
00955 typedef enum pgpArmorKey_e {
00956 PGPARMORKEY_VERSION = 1,
00957 PGPARMORKEY_COMMENT = 2,
00958 PGPARMORKEY_MESSAGEID = 3,
00959 PGPARMORKEY_HASH = 4,
00960 PGPARMORKEY_CHARSET = 5
00961 } pgpArmorKey;
00962
00963
00967
00968 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00969
00973 typedef enum rpmDigestFlags_e {
00974 RPMDIGEST_NONE = 0
00975 } rpmDigestFlags;
00976
00977
00978
00979 #ifdef __cplusplus
00980 extern "C" {
00981 #endif
00982
00989 static inline
00990 unsigned int pgpGrab(const byte *s, int nbytes)
00991
00992 {
00993 unsigned int i = 0;
00994 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
00995
00996 while (nb--)
00997 i = (i << 8) | *s++;
00998
00999 return i;
01000 }
01001
01008 static inline
01009 int pgpLen(const byte *s, unsigned int *lenp)
01010
01011 {
01012
01013 if (*s < 192) {
01014 (*lenp) = *s++;
01015 return 1;
01016 } else if (*s < 255) {
01017 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01018 return 2;
01019 } else {
01020 (*lenp) = pgpGrab(s+1, 4);
01021 return 5;
01022 }
01023
01024 }
01025
01031 static inline
01032 unsigned int pgpMpiBits(const byte *p)
01033
01034
01035 {
01036 return ((p[0] << 8) | p[1]);
01037 }
01038
01044 static inline
01045 unsigned int pgpMpiLen(const byte *p)
01046
01047
01048 {
01049 return (2 + ((pgpMpiBits(p)+7)>>3));
01050 }
01051
01059 static inline
01060 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01061
01062 {
01063 static char hex[] = "0123456789abcdef";
01064
01065 while (nbytes-- > 0) {
01066 unsigned int i;
01067 i = *s++;
01068 *t++ = hex[ (i >> 4) & 0xf ];
01069 *t++ = hex[ (i ) & 0xf ];
01070 }
01071 *t = '\0';
01072
01073 return t;
01074 }
01075
01083 static inline
01084 char * pgpHexStr(const byte *p, unsigned int plen)
01085
01086 {
01087 static char prbuf[8*BUFSIZ];
01088 char *t = prbuf;
01089 t = pgpHexCvt(t, p, plen);
01090 return prbuf;
01091 }
01092
01099 static inline
01100 const char * pgpMpiStr(const byte *p)
01101
01102
01103 {
01104 static char prbuf[8*BUFSIZ];
01105 char *t = prbuf;
01106 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01107 t += strlen(t);
01108 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01109 return prbuf;
01110 }
01111
01118 static inline
01119 const char * pgpValStr(pgpValTbl vs, byte val)
01120
01121 {
01122 do {
01123 if (vs->val == val)
01124 break;
01125 } while ((++vs)->val != -1);
01126 return vs->str;
01127 }
01128
01136 static inline
01137 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01138
01139 {
01140 do {
01141 int vlen = strlen(vs->str);
01142 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01143 break;
01144 } while ((++vs)->val != -1);
01145 return vs->val;
01146 }
01147
01148
01155 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01156
01157 ;
01158
01166 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
01167
01168 ;
01169
01177 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01178
01179 ;
01180
01188 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01189
01190 ;
01191
01199 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01200
01201 ;
01202
01210 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01211
01212 ;
01213
01222 int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
01223 byte * keyid)
01224 ;
01225
01232 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01233
01234 ;
01235
01236
01245 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
01246
01247 ;
01248
01256 pgpArmor pgpReadPkts(const char * fn,
01257 const byte ** pkt, size_t * pktlen)
01258
01259 ;
01260
01268 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01269 ;
01270
01275
01276 pgpDig pgpNewDig(void)
01277 ;
01278
01283 void pgpCleanDig( pgpDig dig)
01284 ;
01285
01291
01292 pgpDig pgpFreeDig( pgpDig dig)
01293 ;
01294
01300 static inline
01301 int pgpIsPkt(const byte * p)
01302
01303 {
01304
01305 unsigned int val = *p++;
01306
01307 pgpTag tag;
01308 int rc;
01309
01310
01311 if (!(val & 0x80))
01312 return 0;
01313
01314 if (val & 0x40)
01315 tag = (pgpTag)(val & 0x3f);
01316 else
01317 tag = (pgpTag)((val >> 2) & 0xf);
01318
01319 switch (tag) {
01320 case PGPTAG_MARKER:
01321 case PGPTAG_SYMMETRIC_SESSION_KEY:
01322 case PGPTAG_ONEPASS_SIGNATURE:
01323 case PGPTAG_PUBLIC_KEY:
01324 case PGPTAG_SECRET_KEY:
01325 case PGPTAG_PUBLIC_SESSION_KEY:
01326 case PGPTAG_SIGNATURE:
01327 case PGPTAG_COMMENT:
01328 case PGPTAG_COMMENT_OLD:
01329 case PGPTAG_LITERAL_DATA:
01330 case PGPTAG_COMPRESSED_DATA:
01331 case PGPTAG_SYMMETRIC_DATA:
01332 rc = 1;
01333 break;
01334 case PGPTAG_PUBLIC_SUBKEY:
01335 case PGPTAG_SECRET_SUBKEY:
01336 case PGPTAG_USER_ID:
01337 case PGPTAG_RESERVED:
01338 case PGPTAG_TRUST:
01339 case PGPTAG_PHOTOID:
01340 case PGPTAG_ENCRYPTED_MDC:
01341 case PGPTAG_MDC:
01342 case PGPTAG_PRIVATE_60:
01343 case PGPTAG_PRIVATE_62:
01344 case PGPTAG_CONTROL:
01345 default:
01346 rc = 0;
01347 break;
01348 }
01349
01350 return rc;
01351 }
01352
01353 #define CRC24_INIT 0xb704ce
01354 #define CRC24_POLY 0x1864cfb
01355
01362 static inline
01363 unsigned int pgpCRC(const byte *octets, size_t len)
01364
01365 {
01366 unsigned int crc = CRC24_INIT;
01367 int i;
01368
01369 while (len--) {
01370
01371 crc ^= (*octets++) << 16;
01372
01373 for (i = 0; i < 8; i++) {
01374 crc <<= 1;
01375 if (crc & 0x1000000)
01376 crc ^= CRC24_POLY;
01377 }
01378 }
01379 return crc & 0xffffff;
01380 }
01381
01389 int rpmInitCrypto(void);
01390
01396
01397 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
01398 ;
01399
01405
01406 size_t rpmDigestLength(pgpHashAlgo hashalgo)
01407 ;
01408
01416
01417 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
01418 ;
01419
01427 int rpmDigestUpdate( DIGEST_CTX ctx, const void * data, size_t len)
01428 ;
01429
01441 int rpmDigestFinal( DIGEST_CTX ctx,
01442 void ** datap,
01443 size_t * lenp, int asAscii)
01444 ;
01445
01446 #ifdef __cplusplus
01447 }
01448 #endif
01449
01450
01451 #endif