00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #if !defined(_BEECRYPT_API_H)
00014
00015 typedef unsigned char byte;
00016
00017 #endif
00018
00021 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00022
00025 typedef const struct pgpValTbl_s {
00026 int val;
00027 const char * str;
00028 } * pgpValTbl;
00029
00037 typedef enum pgpTag_e {
00038 PGPTAG_RESERVED = 0,
00039 PGPTAG_PUBLIC_SESSION_KEY = 1,
00040 PGPTAG_SIGNATURE = 2,
00041 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00042 PGPTAG_ONEPASS_SIGNATURE = 4,
00043 PGPTAG_SECRET_KEY = 5,
00044 PGPTAG_PUBLIC_KEY = 6,
00045 PGPTAG_SECRET_SUBKEY = 7,
00046 PGPTAG_COMPRESSED_DATA = 8,
00047 PGPTAG_SYMMETRIC_DATA = 9,
00048 PGPTAG_MARKER = 10,
00049 PGPTAG_LITERAL_DATA = 11,
00050 PGPTAG_TRUST = 12,
00051 PGPTAG_USER_ID = 13,
00052 PGPTAG_PUBLIC_SUBKEY = 14,
00053 PGPTAG_COMMENT_OLD = 16,
00054 PGPTAG_PHOTOID = 17,
00055 PGPTAG_ENCRYPTED_MDC = 18,
00056 PGPTAG_MDC = 19,
00057 PGPTAG_PRIVATE_60 = 60,
00058 PGPTAG_COMMENT = 61,
00059 PGPTAG_PRIVATE_62 = 62,
00060 PGPTAG_CONTROL = 63
00061 } pgpTag;
00062
00065
00066 extern struct pgpValTbl_s pgpTagTbl[];
00067
00102 typedef struct pgpPktPubkey_s {
00103 byte version;
00104 byte keyid[8];
00105 byte algo;
00106 } pgpPktPubkey;
00107
00108
00115
00116 typedef enum pgpSigType_e {
00117 PGPSIGTYPE_BINARY = 0x00,
00118 PGPSIGTYPE_TEXT = 0x01,
00119 PGPSIGTYPE_STANDALONE = 0x02,
00120 PGPSIGTYPE_GENERIC_CERT = 0x10,
00122 PGPSIGTYPE_PERSONA_CERT = 0x11,
00124 PGPSIGTYPE_CASUAL_CERT = 0x12,
00126 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00128 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00129 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00130 PGPSIGTYPE_KEY_REVOKE = 0x20,
00131 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00132 PGPSIGTYPE_CERT_REVOKE = 0x30,
00133 PGPSIGTYPE_TIMESTAMP = 0x40
00134 } pgpSigType;
00135
00136
00139
00140 extern struct pgpValTbl_s pgpSigTypeTbl[];
00141
00165
00166 typedef enum pgpPubkeyAlgo_e {
00167 PGPPUBKEYALGO_RSA = 1,
00168 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00169 PGPPUBKEYALGO_RSA_SIGN = 3,
00170 PGPPUBKEYALGO_ELGAMAL_ENCRYPT = 16,
00171 PGPPUBKEYALGO_DSA = 17,
00172 PGPPUBKEYALGO_EC = 18,
00173 PGPPUBKEYALGO_ECDSA = 19,
00174 PGPPUBKEYALGO_ELGAMAL = 20,
00175 PGPPUBKEYALGO_DH = 21
00176 } pgpPubkeyAlgo;
00177
00178
00181
00182 extern struct pgpValTbl_s pgpPubkeyTbl[];
00183
00208
00209 typedef enum pgpSymkeyAlgo_e {
00210 PGPSYMKEYALGO_PLAINTEXT = 0,
00211 PGPSYMKEYALGO_IDEA = 1,
00212 PGPSYMKEYALGO_TRIPLE_DES = 2,
00213 PGPSYMKEYALGO_CAST5 = 3,
00214 PGPSYMKEYALGO_BLOWFISH = 4,
00215 PGPSYMKEYALGO_SAFER = 5,
00216 PGPSYMKEYALGO_DES_SK = 6,
00217 PGPSYMKEYALGO_AES_128 = 7,
00218 PGPSYMKEYALGO_AES_192 = 8,
00219 PGPSYMKEYALGO_AES_256 = 9,
00220 PGPSYMKEYALGO_TWOFISH = 10,
00221 PGPSYMKEYALGO_NOENCRYPT = 110
00222 } pgpSymkeyAlgo;
00223
00224
00228
00229 extern struct pgpValTbl_s pgpSymkeyTbl[];
00230
00246
00247 typedef enum pgpCompressAlgo_e {
00248 PGPCOMPRESSALGO_NONE = 0,
00249 PGPCOMPRESSALGO_ZIP = 1,
00250 PGPCOMPRESSALGO_ZLIB = 2,
00251 PGPCOMPRESSALGO_BZIP2 = 3
00252 } pgpCompressAlgo;
00253
00254
00258
00259 extern struct pgpValTbl_s pgpCompressionTbl[];
00260
00282 typedef enum pgpHashAlgo_e {
00283 PGPHASHALGO_MD5 = 1,
00284 PGPHASHALGO_SHA1 = 2,
00285 PGPHASHALGO_RIPEMD160 = 3,
00286 PGPHASHALGO_MD2 = 5,
00287 PGPHASHALGO_TIGER192 = 6,
00288 PGPHASHALGO_HAVAL_5_160 = 7,
00289 PGPHASHALGO_SHA256 = 8,
00290 PGPHASHALGO_SHA384 = 9,
00291 PGPHASHALGO_SHA512 = 10,
00292 } pgpHashAlgo;
00293
00297
00298 extern struct pgpValTbl_s pgpHashTbl[];
00299
00321 typedef struct pgpPktSigV3_s {
00322 byte version;
00323 byte hashlen;
00324 byte sigtype;
00325 byte time[4];
00326 byte signid[8];
00327 byte pubkey_algo;
00328 byte hash_algo;
00329 byte signhash16[2];
00330 } * pgpPktSigV3;
00331
00353 typedef struct pgpPktSigV4_s {
00354 byte version;
00355 byte sigtype;
00356 byte pubkey_algo;
00357 byte hash_algo;
00358 byte hashlen[2];
00359 } * pgpPktSigV4;
00360
00427
00428 typedef enum pgpSubType_e {
00429 PGPSUBTYPE_NONE = 0,
00430 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00431 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00432 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00433 PGPSUBTYPE_TRUST_SIG = 5,
00434 PGPSUBTYPE_REGEX = 6,
00435 PGPSUBTYPE_REVOCABLE = 7,
00436 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00437 PGPSUBTYPE_ARR = 10,
00438 PGPSUBTYPE_PREFER_SYMKEY = 11,
00439 PGPSUBTYPE_REVOKE_KEY = 12,
00440 PGPSUBTYPE_ISSUER_KEYID = 16,
00441 PGPSUBTYPE_NOTATION = 20,
00442 PGPSUBTYPE_PREFER_HASH = 21,
00443 PGPSUBTYPE_PREFER_COMPRESS = 22,
00444 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00445 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00446 PGPSUBTYPE_PRIMARY_USERID = 25,
00447 PGPSUBTYPE_POLICY_URL = 26,
00448 PGPSUBTYPE_KEY_FLAGS = 27,
00449 PGPSUBTYPE_SIGNER_USERID = 28,
00450 PGPSUBTYPE_REVOKE_REASON = 29,
00451 PGPSUBTYPE_FEATURES = 30,
00452 PGPSUBTYPE_EMBEDDED_SIG = 32,
00454 PGPSUBTYPE_INTERNAL_100 = 100,
00455 PGPSUBTYPE_INTERNAL_101 = 101,
00456 PGPSUBTYPE_INTERNAL_102 = 102,
00457 PGPSUBTYPE_INTERNAL_103 = 103,
00458 PGPSUBTYPE_INTERNAL_104 = 104,
00459 PGPSUBTYPE_INTERNAL_105 = 105,
00460 PGPSUBTYPE_INTERNAL_106 = 106,
00461 PGPSUBTYPE_INTERNAL_107 = 107,
00462 PGPSUBTYPE_INTERNAL_108 = 108,
00463 PGPSUBTYPE_INTERNAL_109 = 109,
00464 PGPSUBTYPE_INTERNAL_110 = 110,
00466 PGPSUBTYPE_CRITICAL = 128
00467 } pgpSubType;
00468
00469
00473
00474 extern struct pgpValTbl_s pgpSubTypeTbl[];
00475
00496 typedef union pgpPktSig_u {
00497 struct pgpPktSigV3_s v3;
00498 struct pgpPktSigV4_s v4;
00499 } * pgpPktSig;
00500
00529 typedef struct pgpPktSymkey_s {
00530 byte version;
00531 byte symkey_algo;
00532 byte s2k[1];
00533 } pgpPktSymkey;
00534
00564 typedef struct pgpPktOnepass_s {
00565 byte version;
00566 byte sigtype;
00567 byte hash_algo;
00568 byte pubkey_algo;
00569 byte signid[8];
00570 byte nested;
00571 } * pgpPktOnepass;
00572
00645 typedef struct pgpPktKeyV3_s {
00646 byte version;
00647 byte time[4];
00648 byte valid[2];
00649 byte pubkey_algo;
00650 } * pgpPktKeyV3;
00651
00683 typedef struct pgpPktKeyV4_s {
00684 byte version;
00685 byte time[4];
00686 byte pubkey_algo;
00687 } * pgpPktKeyV4;
00688
00753 typedef union pgpPktKey_u {
00754 struct pgpPktKeyV3_s v3;
00755 struct pgpPktKeyV4_s v4;
00756 } pgpPktKey;
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 typedef struct pgpPktCdata_s {
00783 byte compressalgo;
00784 byte data[1];
00785 } pgpPktCdata;
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
00820
00821 typedef struct pgpPktEdata_s {
00822 byte data[1];
00823 } pgpPktEdata;
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
00871
00872 typedef struct pgpPktLdata_s {
00873 byte format;
00874 byte filenamelen;
00875 byte filename[1];
00876 } pgpPktLdata;
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891 typedef struct pgpPktTrust_s {
00892 byte flag;
00893 } pgpPktTrust;
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 typedef struct pgpPktUid_s {
00906 byte userid[1];
00907 } pgpPktUid;
00908
00911 union pgpPktPre_u {
00912 pgpPktPubkey pubkey;
00913 pgpPktSig sig;
00914 pgpPktSymkey symkey;
00915 pgpPktOnepass onepass;
00916 pgpPktKey key;
00917 pgpPktCdata cdata;
00918 pgpPktEdata edata;
00920 pgpPktLdata ldata;
00921 pgpPktTrust tdata;
00922 pgpPktUid uid;
00923 };
00924
00927
00928 typedef enum pgpArmor_e {
00929 PGPARMOR_ERR_CRC_CHECK = -7,
00930 PGPARMOR_ERR_BODY_DECODE = -6,
00931 PGPARMOR_ERR_CRC_DECODE = -5,
00932 PGPARMOR_ERR_NO_END_PGP = -4,
00933 PGPARMOR_ERR_UNKNOWN_PREAMBLE_TAG = -3,
00934 PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE = -2,
00935 PGPARMOR_ERR_NO_BEGIN_PGP = -1,
00936 #define PGPARMOR_ERROR PGPARMOR_ERR_NO_BEGIN_PGP
00937 PGPARMOR_NONE = 0,
00938 PGPARMOR_MESSAGE = 1,
00939 PGPARMOR_PUBKEY = 2,
00940 PGPARMOR_SIGNATURE = 3,
00941 PGPARMOR_SIGNED_MESSAGE = 4,
00942 PGPARMOR_FILE = 5,
00943 PGPARMOR_PRIVKEY = 6,
00944 PGPARMOR_SECKEY = 7
00945 } pgpArmor;
00946
00947
00951
00952 extern struct pgpValTbl_s pgpArmorTbl[];
00953
00956
00957 typedef enum pgpArmorKey_e {
00958 PGPARMORKEY_VERSION = 1,
00959 PGPARMORKEY_COMMENT = 2,
00960 PGPARMORKEY_MESSAGEID = 3,
00961 PGPARMORKEY_HASH = 4,
00962 PGPARMORKEY_CHARSET = 5
00963 } pgpArmorKey;
00964
00965
00969
00970 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00971
00975 typedef enum rpmDigestFlags_e {
00976 RPMDIGEST_NONE = 0
00977 } rpmDigestFlags;
00978
00979
00980
00981 #ifdef __cplusplus
00982 extern "C" {
00983 #endif
00984
00991 static inline
00992 unsigned int pgpGrab(const byte *s, int nbytes)
00993
00994 {
00995 unsigned int i = 0;
00996 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
00997
00998 while (nb--)
00999 i = (i << 8) | *s++;
01000
01001 return i;
01002 }
01003
01010 static inline
01011 int pgpLen(const byte *s, unsigned int *lenp)
01012
01013 {
01014
01015 if (*s < 192) {
01016 (*lenp) = *s++;
01017 return 1;
01018 } else if (*s < 255) {
01019 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01020 return 2;
01021 } else {
01022 (*lenp) = pgpGrab(s+1, 4);
01023 return 5;
01024 }
01025
01026 }
01027
01033 static inline
01034 unsigned int pgpMpiBits(const byte *p)
01035
01036
01037 {
01038 return ((p[0] << 8) | p[1]);
01039 }
01040
01046 static inline
01047 unsigned int pgpMpiLen(const byte *p)
01048
01049
01050 {
01051 return (2 + ((pgpMpiBits(p)+7)>>3));
01052 }
01053
01061 static inline
01062 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01063
01064 {
01065 static char hex[] = "0123456789abcdef";
01066
01067 while (nbytes-- > 0) {
01068 unsigned int i;
01069 i = *s++;
01070 *t++ = hex[ (i >> 4) & 0xf ];
01071 *t++ = hex[ (i ) & 0xf ];
01072 }
01073 *t = '\0';
01074
01075 return t;
01076 }
01077
01085 static inline
01086 char * pgpHexStr(const byte *p, unsigned int plen)
01087
01088 {
01089 static char prbuf[8*BUFSIZ];
01090 char *t = prbuf;
01091 t = pgpHexCvt(t, p, plen);
01092 return prbuf;
01093 }
01094
01101 static inline
01102 const char * pgpMpiStr(const byte *p)
01103
01104
01105 {
01106 static char prbuf[8*BUFSIZ];
01107 char *t = prbuf;
01108 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01109 t += strlen(t);
01110 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01111 return prbuf;
01112 }
01113
01120 static inline
01121 const char * pgpValStr(pgpValTbl vs, byte val)
01122
01123 {
01124 do {
01125 if (vs->val == val)
01126 break;
01127 } while ((++vs)->val != -1);
01128 return vs->str;
01129 }
01130
01138 static inline
01139 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01140
01141 {
01142 do {
01143 int vlen = strlen(vs->str);
01144 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01145 break;
01146 } while ((++vs)->val != -1);
01147 return vs->val;
01148 }
01149
01150
01157 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01158
01159 ;
01160
01168 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
01169
01170 ;
01171
01179 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01180
01181 ;
01182
01190 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01191
01192 ;
01193
01201 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01202
01203 ;
01204
01212 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01213
01214 ;
01215
01224 int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
01225 byte * keyid)
01226 ;
01227
01234 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01235
01236 ;
01237
01238
01247 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
01248
01249 ;
01250
01258 pgpArmor pgpReadPkts(const char * fn,
01259 const byte ** pkt, size_t * pktlen)
01260
01261 ;
01262
01270 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01271 ;
01272
01277
01278 pgpDig pgpNewDig(void)
01279 ;
01280
01285 void pgpCleanDig( pgpDig dig)
01286 ;
01287
01293
01294 pgpDig pgpFreeDig( pgpDig dig)
01295 ;
01296
01302 static inline
01303 int pgpIsPkt(const byte * p)
01304
01305 {
01306
01307 unsigned int val = *p++;
01308
01309 pgpTag tag;
01310 int rc;
01311
01312
01313 if (!(val & 0x80))
01314 return 0;
01315
01316 if (val & 0x40)
01317 tag = (val & 0x3f);
01318 else
01319 tag = (val >> 2) & 0xf;
01320
01321 switch (tag) {
01322 case PGPTAG_MARKER:
01323 case PGPTAG_SYMMETRIC_SESSION_KEY:
01324 case PGPTAG_ONEPASS_SIGNATURE:
01325 case PGPTAG_PUBLIC_KEY:
01326 case PGPTAG_SECRET_KEY:
01327 case PGPTAG_PUBLIC_SESSION_KEY:
01328 case PGPTAG_SIGNATURE:
01329 case PGPTAG_COMMENT:
01330 case PGPTAG_COMMENT_OLD:
01331 case PGPTAG_LITERAL_DATA:
01332 case PGPTAG_COMPRESSED_DATA:
01333 case PGPTAG_SYMMETRIC_DATA:
01334 rc = 1;
01335 break;
01336 case PGPTAG_PUBLIC_SUBKEY:
01337 case PGPTAG_SECRET_SUBKEY:
01338 case PGPTAG_USER_ID:
01339 case PGPTAG_RESERVED:
01340 case PGPTAG_TRUST:
01341 case PGPTAG_PHOTOID:
01342 case PGPTAG_ENCRYPTED_MDC:
01343 case PGPTAG_MDC:
01344 case PGPTAG_PRIVATE_60:
01345 case PGPTAG_PRIVATE_62:
01346 case PGPTAG_CONTROL:
01347 default:
01348 rc = 0;
01349 break;
01350 }
01351
01352 return rc;
01353 }
01354
01355 #define CRC24_INIT 0xb704ce
01356 #define CRC24_POLY 0x1864cfb
01357
01364 static inline
01365 unsigned int pgpCRC(const byte *octets, size_t len)
01366
01367 {
01368 unsigned int crc = CRC24_INIT;
01369 int i;
01370
01371 while (len--) {
01372
01373 crc ^= (*octets++) << 16;
01374
01375 for (i = 0; i < 8; i++) {
01376 crc <<= 1;
01377 if (crc & 0x1000000)
01378 crc ^= CRC24_POLY;
01379 }
01380 }
01381 return crc & 0xffffff;
01382 }
01383
01389
01390 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
01391 ;
01392
01400
01401 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
01402 ;
01403
01411 int rpmDigestUpdate( DIGEST_CTX ctx, const void * data, size_t len)
01412 ;
01413
01425 int rpmDigestFinal( DIGEST_CTX ctx,
01426 void ** datap,
01427 size_t * lenp, int asAscii)
01428 ;
01429
01430 #ifdef __cplusplus
01431 }
01432 #endif
01433
01434
01435 #endif