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;
00222
00223
00227
00228 extern struct pgpValTbl_s pgpSymkeyTbl[];
00229
00245
00246 typedef enum pgpCompressAlgo_e {
00247 PGPCOMPRESSALGO_NONE = 0,
00248 PGPCOMPRESSALGO_ZIP = 1,
00249 PGPCOMPRESSALGO_ZLIB = 2
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;
00288
00292
00293 extern struct pgpValTbl_s pgpHashTbl[];
00294
00316 typedef struct pgpPktSigV3_s {
00317 byte version;
00318 byte hashlen;
00319 byte sigtype;
00320 byte time[4];
00321 byte signid[8];
00322 byte pubkey_algo;
00323 byte hash_algo;
00324 byte signhash16[2];
00325 } * pgpPktSigV3;
00326
00348 typedef struct pgpPktSigV4_s {
00349 byte version;
00350 byte sigtype;
00351 byte pubkey_algo;
00352 byte hash_algo;
00353 byte hashlen[2];
00354 } * pgpPktSigV4;
00355
00422
00423 typedef enum pgpSubType_e {
00424 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00425 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00426 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00427 PGPSUBTYPE_TRUST_SIG = 5,
00428 PGPSUBTYPE_REGEX = 6,
00429 PGPSUBTYPE_REVOCABLE = 7,
00430 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00431 PGPSUBTYPE_BACKWARD_COMPAT = 10,
00432 PGPSUBTYPE_PREFER_SYMKEY = 11,
00433 PGPSUBTYPE_REVOKE_KEY = 12,
00434 PGPSUBTYPE_ISSUER_KEYID = 16,
00435 PGPSUBTYPE_NOTATION = 20,
00436 PGPSUBTYPE_PREFER_HASH = 21,
00437 PGPSUBTYPE_PREFER_COMPRESS = 22,
00438 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00439 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00440 PGPSUBTYPE_PRIMARY_USERID = 25,
00441 PGPSUBTYPE_POLICY_URL = 26,
00442 PGPSUBTYPE_KEY_FLAGS = 27,
00443 PGPSUBTYPE_SIGNER_USERID = 28,
00444 PGPSUBTYPE_REVOKE_REASON = 29,
00445 PGPSUBTYPE_INTERNAL_100 = 100,
00446 PGPSUBTYPE_INTERNAL_101 = 101,
00447 PGPSUBTYPE_INTERNAL_102 = 102,
00448 PGPSUBTYPE_INTERNAL_103 = 103,
00449 PGPSUBTYPE_INTERNAL_104 = 104,
00450 PGPSUBTYPE_INTERNAL_105 = 105,
00451 PGPSUBTYPE_INTERNAL_106 = 106,
00452 PGPSUBTYPE_INTERNAL_107 = 107,
00453 PGPSUBTYPE_INTERNAL_108 = 108,
00454 PGPSUBTYPE_INTERNAL_109 = 109,
00455 PGPSUBTYPE_INTERNAL_110 = 110
00456 } pgpSubType;
00457
00458
00462
00463 extern struct pgpValTbl_s pgpSubTypeTbl[];
00464
00485 typedef union pgpPktSig_u {
00486 struct pgpPktSigV3_s v3;
00487 struct pgpPktSigV4_s v4;
00488 } * pgpPktSig;
00489
00518 typedef struct pgpPktSymkey_s {
00519 byte version;
00520 byte symkey_algo;
00521 byte s2k[1];
00522 } pgpPktSymkey;
00523
00553 typedef struct pgpPktOnepass_s {
00554 byte version;
00555 byte sigtype;
00556 byte hash_algo;
00557 byte pubkey_algo;
00558 byte signid[8];
00559 byte nested;
00560 } * pgpPktOnepass;
00561
00634 typedef struct pgpPktKeyV3_s {
00635 byte version;
00636 byte time[4];
00637 byte valid[2];
00638 byte pubkey_algo;
00639 } * pgpPktKeyV3;
00640
00672 typedef struct pgpPktKeyV4_s {
00673 byte version;
00674 byte time[4];
00675 byte pubkey_algo;
00676 } * pgpPktKeyV4;
00677
00742 typedef union pgpPktKey_u {
00743 struct pgpPktKeyV3_s v3;
00744 struct pgpPktKeyV4_s v4;
00745 } pgpPktKey;
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 typedef struct pgpPktCdata_s {
00772 byte compressalgo;
00773 byte data[1];
00774 } pgpPktCdata;
00775
00776
00777
00778
00779
00780
00781
00782
00783
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 typedef struct pgpPktEdata_s {
00811 byte data[1];
00812 } pgpPktEdata;
00813
00814
00815
00816
00817
00818
00819
00820
00821
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 typedef struct pgpPktLdata_s {
00862 byte format;
00863 byte filenamelen;
00864 byte filename[1];
00865 } pgpPktLdata;
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880 typedef struct pgpPktTrust_s {
00881 byte flag;
00882 } pgpPktTrust;
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894 typedef struct pgpPktUid_s {
00895 byte userid[1];
00896 } pgpPktUid;
00897
00900 union pgpPktPre_u {
00901 pgpPktPubkey pubkey;
00902 pgpPktSig sig;
00903 pgpPktSymkey symkey;
00904 pgpPktOnepass onepass;
00905 pgpPktKey key;
00906 pgpPktCdata cdata;
00907 pgpPktEdata edata;
00909 pgpPktLdata ldata;
00910 pgpPktTrust tdata;
00911 pgpPktUid uid;
00912 };
00913
00916
00917 typedef enum pgpArmor_e {
00918 PGPARMOR_ERROR = -1,
00919 PGPARMOR_NONE = 0,
00920 PGPARMOR_MESSAGE = 1,
00921 PGPARMOR_PUBKEY = 2,
00922 PGPARMOR_SIGNATURE = 3,
00923 PGPARMOR_SIGNED_MESSAGE = 4,
00924 PGPARMOR_FILE = 5,
00925 PGPARMOR_PRIVKEY = 6,
00926 PGPARMOR_SECKEY = 7
00927 } pgpArmor;
00928
00929
00933
00934 extern struct pgpValTbl_s pgpArmorTbl[];
00935
00938
00939 typedef enum pgpArmorKey_e {
00940 PGPARMORKEY_VERSION = 1,
00941 PGPARMORKEY_COMMENT = 2,
00942 PGPARMORKEY_MESSAGEID = 3,
00943 PGPARMORKEY_HASH = 4,
00944 PGPARMORKEY_CHARSET = 5
00945 } pgpArmorKey;
00946
00947
00951
00952 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00953
00957 typedef enum rpmDigestFlags_e {
00958 RPMDIGEST_NONE = 0
00959 } rpmDigestFlags;
00960
00961
00962
00963 #ifdef __cplusplus
00964 extern "C" {
00965 #endif
00966
00973 static inline
00974 unsigned int pgpGrab(const byte *s, int nbytes)
00975
00976 {
00977 unsigned int i = 0;
00978 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
00979
00980 while (nb--)
00981 i = (i << 8) | *s++;
00982
00983 return i;
00984 }
00985
00992 static inline
00993 int pgpLen(const byte *s, unsigned int *lenp)
00994
00995 {
00996
00997 if (*s < 192) {
00998 (*lenp) = *s++;
00999 return 1;
01000 } else if (*s < 255) {
01001 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01002 return 2;
01003 } else {
01004 (*lenp) = pgpGrab(s+1, 4);
01005 return 5;
01006 }
01007
01008 }
01009
01015 static inline
01016 unsigned int pgpMpiBits(const byte *p)
01017
01018
01019 {
01020 return ((p[0] << 8) | p[1]);
01021 }
01022
01028 static inline
01029 unsigned int pgpMpiLen(const byte *p)
01030
01031
01032 {
01033 return (2 + ((pgpMpiBits(p)+7)>>3));
01034 }
01035
01043 static inline
01044 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01045
01046 {
01047 static char hex[] = "0123456789abcdef";
01048
01049 while (nbytes-- > 0) {
01050 unsigned int i;
01051 i = *s++;
01052 *t++ = hex[ (i >> 4) & 0xf ];
01053 *t++ = hex[ (i ) & 0xf ];
01054 }
01055 *t = '\0';
01056
01057 return t;
01058 }
01059
01067 static inline
01068 char * pgpHexStr(const byte *p, unsigned int plen)
01069
01070 {
01071 static char prbuf[2048];
01072 char *t = prbuf;
01073 t = pgpHexCvt(t, p, plen);
01074 return prbuf;
01075 }
01076
01083 static inline
01084 const char * pgpMpiStr(const byte *p)
01085
01086
01087 {
01088 static char prbuf[2048];
01089 char *t = prbuf;
01090 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01091 t += strlen(t);
01092 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01093 return prbuf;
01094 }
01095
01102 static inline
01103 const char * pgpValStr(pgpValTbl vs, byte val)
01104
01105 {
01106 do {
01107 if (vs->val == val)
01108 break;
01109 } while ((++vs)->val != -1);
01110 return vs->str;
01111 }
01112
01120 static inline
01121 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01122
01123 {
01124 do {
01125 int vlen = strlen(vs->str);
01126 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01127 break;
01128 } while ((++vs)->val != -1);
01129 return vs->val;
01130 }
01131
01132
01139 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01140
01141 ;
01142
01150 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
01151
01152 ;
01153
01161 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01162
01163 ;
01164
01172 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01173
01174 ;
01175
01183 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01184
01185 ;
01186
01194 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01195
01196 ;
01197
01204 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01205
01206 ;
01207
01208
01217 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
01218
01219 ;
01220
01228 pgpArmor pgpReadPkts(const char * fn,
01229 const byte ** pkt, size_t * pktlen)
01230
01231 ;
01232
01240 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01241 ;
01242
01247
01248 pgpDig pgpNewDig(void)
01249 ;
01250
01255 void pgpCleanDig( pgpDig dig)
01256 ;
01257
01263
01264 pgpDig pgpFreeDig( pgpDig dig)
01265 ;
01266
01272 static inline
01273 int pgpIsPkt(const byte * p)
01274
01275 {
01276
01277 unsigned int val = *p++;
01278
01279 pgpTag tag;
01280 int rc;
01281
01282
01283 if (!(val & 0x80))
01284 return 0;
01285
01286 if (val & 0x40)
01287 tag = (val & 0x3f);
01288 else
01289 tag = (val >> 2) & 0xf;
01290
01291 switch (tag) {
01292 case PGPTAG_MARKER:
01293 case PGPTAG_SYMMETRIC_SESSION_KEY:
01294 case PGPTAG_ONEPASS_SIGNATURE:
01295 case PGPTAG_PUBLIC_KEY:
01296 case PGPTAG_SECRET_KEY:
01297 case PGPTAG_PUBLIC_SESSION_KEY:
01298 case PGPTAG_SIGNATURE:
01299 case PGPTAG_COMMENT:
01300 case PGPTAG_COMMENT_OLD:
01301 case PGPTAG_LITERAL_DATA:
01302 case PGPTAG_COMPRESSED_DATA:
01303 case PGPTAG_SYMMETRIC_DATA:
01304 rc = 1;
01305 break;
01306 case PGPTAG_PUBLIC_SUBKEY:
01307 case PGPTAG_SECRET_SUBKEY:
01308 case PGPTAG_USER_ID:
01309 case PGPTAG_RESERVED:
01310 case PGPTAG_TRUST:
01311 case PGPTAG_PHOTOID:
01312 case PGPTAG_ENCRYPTED_MDC:
01313 case PGPTAG_MDC:
01314 case PGPTAG_PRIVATE_60:
01315 case PGPTAG_PRIVATE_62:
01316 case PGPTAG_CONTROL:
01317 default:
01318 rc = 0;
01319 break;
01320 }
01321
01322 return rc;
01323 }
01324
01325 #define CRC24_INIT 0xb704ce
01326 #define CRC24_POLY 0x1864cfb
01327
01334 static inline
01335 unsigned int pgpCRC(const byte *octets, size_t len)
01336
01337 {
01338 unsigned int crc = CRC24_INIT;
01339 int i;
01340
01341 while (len--) {
01342
01343 crc ^= (*octets++) << 16;
01344
01345 for (i = 0; i < 8; i++) {
01346 crc <<= 1;
01347 if (crc & 0x1000000)
01348 crc ^= CRC24_POLY;
01349 }
01350 }
01351 return crc & 0xffffff;
01352 }
01353
01359
01360 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
01361 ;
01362
01370
01371 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
01372 ;
01373
01381 int rpmDigestUpdate( DIGEST_CTX ctx, const void * data, size_t len)
01382 ;
01383
01395 int rpmDigestFinal( DIGEST_CTX ctx,
01396 void ** datap,
01397 size_t * lenp, int asAscii)
01398 ;
01399
01400 #ifdef __cplusplus
01401 }
01402 #endif
01403
01404
01405 #endif