00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #include <popt.h>
00014
00015 #if !defined(_BEECRYPT_API_H)
00016
00017 typedef unsigned char byte;
00018
00019 #endif
00020
00023 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00024
00027 typedef struct pgpPkt_s * pgpPkt;
00028
00031 typedef const struct pgpValTbl_s {
00032 int val;
00033 const char * str;
00034 } * pgpValTbl;
00035
00043 typedef enum pgpTag_e {
00044 PGPTAG_RESERVED = 0,
00045 PGPTAG_PUBLIC_SESSION_KEY = 1,
00046 PGPTAG_SIGNATURE = 2,
00047 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00048 PGPTAG_ONEPASS_SIGNATURE = 4,
00049 PGPTAG_SECRET_KEY = 5,
00050 PGPTAG_PUBLIC_KEY = 6,
00051 PGPTAG_SECRET_SUBKEY = 7,
00052 PGPTAG_COMPRESSED_DATA = 8,
00053 PGPTAG_SYMMETRIC_DATA = 9,
00054 PGPTAG_MARKER = 10,
00055 PGPTAG_LITERAL_DATA = 11,
00056 PGPTAG_TRUST = 12,
00057 PGPTAG_USER_ID = 13,
00058 PGPTAG_PUBLIC_SUBKEY = 14,
00059 PGPTAG_COMMENT_OLD = 16,
00060 PGPTAG_PHOTOID = 17,
00061 PGPTAG_ENCRYPTED_MDC = 18,
00062 PGPTAG_MDC = 19,
00063 PGPTAG_PRIVATE_60 = 60,
00064 PGPTAG_COMMENT = 61,
00065 PGPTAG_PRIVATE_62 = 62,
00066 PGPTAG_CONTROL = 63
00067 } pgpTag;
00068
00071
00072 extern struct pgpValTbl_s pgpTagTbl[];
00073
00108 typedef struct pgpPktPubkey_s {
00109 byte version;
00110 byte keyid[8];
00111 byte algo;
00112 } pgpPktPubkey;
00113
00114
00121
00122 typedef enum pgpSigType_e {
00123 PGPSIGTYPE_BINARY = 0x00,
00124 PGPSIGTYPE_TEXT = 0x01,
00125 PGPSIGTYPE_STANDALONE = 0x02,
00126 PGPSIGTYPE_GENERIC_CERT = 0x10,
00128 PGPSIGTYPE_PERSONA_CERT = 0x11,
00130 PGPSIGTYPE_CASUAL_CERT = 0x12,
00132 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00134 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00135 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00136 PGPSIGTYPE_KEY_REVOKE = 0x20,
00137 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00138 PGPSIGTYPE_CERT_REVOKE = 0x30,
00139 PGPSIGTYPE_TIMESTAMP = 0x40
00140 } pgpSigType;
00141
00142
00145
00146 extern struct pgpValTbl_s pgpSigTypeTbl[];
00147
00171
00172 typedef enum pgpPubkeyAlgo_e {
00173 PGPPUBKEYALGO_RSA = 1,
00174 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00175 PGPPUBKEYALGO_RSA_SIGN = 3,
00176 PGPPUBKEYALGO_ELGAMAL_ENCRYPT = 16,
00177 PGPPUBKEYALGO_DSA = 17,
00178 PGPPUBKEYALGO_EC = 18,
00179 PGPPUBKEYALGO_ECDSA = 19,
00180 PGPPUBKEYALGO_ELGAMAL = 20,
00181 PGPPUBKEYALGO_DH = 21
00182 } pgpPubkeyAlgo;
00183
00184
00187
00188 extern struct pgpValTbl_s pgpPubkeyTbl[];
00189
00214
00215 typedef enum pgpSymkeyAlgo_e {
00216 PGPSYMKEYALGO_PLAINTEXT = 0,
00217 PGPSYMKEYALGO_IDEA = 1,
00218 PGPSYMKEYALGO_TRIPLE_DES = 2,
00219 PGPSYMKEYALGO_CAST5 = 3,
00220 PGPSYMKEYALGO_BLOWFISH = 4,
00221 PGPSYMKEYALGO_SAFER = 5,
00222 PGPSYMKEYALGO_DES_SK = 6,
00223 PGPSYMKEYALGO_AES_128 = 7,
00224 PGPSYMKEYALGO_AES_192 = 8,
00225 PGPSYMKEYALGO_AES_256 = 9,
00226 PGPSYMKEYALGO_TWOFISH = 10,
00227 PGPSYMKEYALGO_NOENCRYPT = 110
00228 } pgpSymkeyAlgo;
00229
00230
00234
00235 extern struct pgpValTbl_s pgpSymkeyTbl[];
00236
00252
00253 typedef enum pgpCompressAlgo_e {
00254 PGPCOMPRESSALGO_NONE = 0,
00255 PGPCOMPRESSALGO_ZIP = 1,
00256 PGPCOMPRESSALGO_ZLIB = 2,
00257 PGPCOMPRESSALGO_BZIP2 = 3
00258 } pgpCompressAlgo;
00259
00260
00264
00265 extern struct pgpValTbl_s pgpCompressionTbl[];
00266
00288 typedef enum pgpHashAlgo_e {
00289 PGPHASHALGO_ERROR = -1,
00290 PGPHASHALGO_NONE = 0,
00291 PGPHASHALGO_MD5 = 1,
00292 PGPHASHALGO_SHA1 = 2,
00293 PGPHASHALGO_RIPEMD160 = 3,
00294 PGPHASHALGO_MD2 = 5,
00295 PGPHASHALGO_TIGER192 = 6,
00296 PGPHASHALGO_HAVAL_5_160 = 7,
00297 PGPHASHALGO_SHA256 = 8,
00298 PGPHASHALGO_SHA384 = 9,
00299 PGPHASHALGO_SHA512 = 10,
00301 PGPHASHALGO_MD4 = 104,
00302 PGPHASHALGO_RIPEMD128 = 105,
00303 PGPHASHALGO_CRC32 = 106,
00304 PGPHASHALGO_ADLER32 = 107,
00305 PGPHASHALGO_CRC64 = 108,
00306 PGPHASHALGO_JLU32 = 109,
00307 PGPHASHALGO_SHA224 = 110,
00308 PGPHASHALGO_RIPEMD256 = 111,
00309 PGPHASHALGO_RIPEMD320 = 112,
00310 PGPHASHALGO_SALSA10 = 113,
00311 PGPHASHALGO_SALSA20 = 114,
00313 } pgpHashAlgo;
00314
00318
00319 extern struct pgpValTbl_s pgpHashTbl[];
00320
00342 typedef struct pgpPktSigV3_s {
00343 byte version;
00344 byte hashlen;
00345 byte sigtype;
00346 byte time[4];
00347 byte signid[8];
00348 byte pubkey_algo;
00349 byte hash_algo;
00350 byte signhash16[2];
00351 } * pgpPktSigV3;
00352
00374 typedef struct pgpPktSigV4_s {
00375 byte version;
00376 byte sigtype;
00377 byte pubkey_algo;
00378 byte hash_algo;
00379 byte hashlen[2];
00380 } * pgpPktSigV4;
00381
00448
00449 typedef enum pgpSubType_e {
00450 PGPSUBTYPE_NONE = 0,
00451 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00452 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00453 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00454 PGPSUBTYPE_TRUST_SIG = 5,
00455 PGPSUBTYPE_REGEX = 6,
00456 PGPSUBTYPE_REVOCABLE = 7,
00457 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00458 PGPSUBTYPE_ARR = 10,
00459 PGPSUBTYPE_PREFER_SYMKEY = 11,
00460 PGPSUBTYPE_REVOKE_KEY = 12,
00461 PGPSUBTYPE_ISSUER_KEYID = 16,
00462 PGPSUBTYPE_NOTATION = 20,
00463 PGPSUBTYPE_PREFER_HASH = 21,
00464 PGPSUBTYPE_PREFER_COMPRESS = 22,
00465 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00466 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00467 PGPSUBTYPE_PRIMARY_USERID = 25,
00468 PGPSUBTYPE_POLICY_URL = 26,
00469 PGPSUBTYPE_KEY_FLAGS = 27,
00470 PGPSUBTYPE_SIGNER_USERID = 28,
00471 PGPSUBTYPE_REVOKE_REASON = 29,
00472 PGPSUBTYPE_FEATURES = 30,
00473 PGPSUBTYPE_EMBEDDED_SIG = 32,
00475 PGPSUBTYPE_INTERNAL_100 = 100,
00476 PGPSUBTYPE_INTERNAL_101 = 101,
00477 PGPSUBTYPE_INTERNAL_102 = 102,
00478 PGPSUBTYPE_INTERNAL_103 = 103,
00479 PGPSUBTYPE_INTERNAL_104 = 104,
00480 PGPSUBTYPE_INTERNAL_105 = 105,
00481 PGPSUBTYPE_INTERNAL_106 = 106,
00482 PGPSUBTYPE_INTERNAL_107 = 107,
00483 PGPSUBTYPE_INTERNAL_108 = 108,
00484 PGPSUBTYPE_INTERNAL_109 = 109,
00485 PGPSUBTYPE_INTERNAL_110 = 110,
00487 PGPSUBTYPE_CRITICAL = 128
00488 } pgpSubType;
00489
00490
00494
00495 extern struct pgpValTbl_s pgpSubTypeTbl[];
00496
00517 typedef union pgpPktSig_u {
00518 struct pgpPktSigV3_s v3;
00519 struct pgpPktSigV4_s v4;
00520 } * pgpPktSig;
00521
00550 typedef struct pgpPktSymkey_s {
00551 byte version;
00552 byte symkey_algo;
00553 byte s2k[1];
00554 } pgpPktSymkey;
00555
00585 typedef struct pgpPktOnepass_s {
00586 byte version;
00587 byte sigtype;
00588 byte hash_algo;
00589 byte pubkey_algo;
00590 byte signid[8];
00591 byte nested;
00592 } * pgpPktOnepass;
00593
00666 typedef struct pgpPktKeyV3_s {
00667 byte version;
00668 byte time[4];
00669 byte valid[2];
00670 byte pubkey_algo;
00671 } * pgpPktKeyV3;
00672
00704 typedef struct pgpPktKeyV4_s {
00705 byte version;
00706 byte time[4];
00707 byte pubkey_algo;
00708 } * pgpPktKeyV4;
00709
00774 typedef union pgpPktKey_u {
00775 struct pgpPktKeyV3_s v3;
00776 struct pgpPktKeyV4_s v4;
00777 } pgpPktKey;
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 typedef struct pgpPktCdata_s {
00804 byte compressalgo;
00805 byte data[1];
00806 } pgpPktCdata;
00807
00808
00809
00810
00811
00812
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 typedef struct pgpPktEdata_s {
00843 byte data[1];
00844 } pgpPktEdata;
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
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893 typedef struct pgpPktLdata_s {
00894 byte format;
00895 byte filenamelen;
00896 byte filename[1];
00897 } pgpPktLdata;
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912 typedef struct pgpPktTrust_s {
00913 byte flag;
00914 } pgpPktTrust;
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926 typedef struct pgpPktUid_s {
00927 byte userid[1];
00928 } pgpPktUid;
00929
00932 union pgpPktPre_u {
00933 pgpPktPubkey pubkey;
00934 pgpPktSig sig;
00935 pgpPktSymkey symkey;
00936 pgpPktOnepass onepass;
00937 pgpPktKey key;
00938 pgpPktCdata cdata;
00939 pgpPktEdata edata;
00941 pgpPktLdata ldata;
00942 pgpPktTrust tdata;
00943 pgpPktUid uid;
00944 };
00945
00948
00949 typedef enum pgpArmor_e {
00950 PGPARMOR_ERR_CRC_CHECK = -7,
00951 PGPARMOR_ERR_BODY_DECODE = -6,
00952 PGPARMOR_ERR_CRC_DECODE = -5,
00953 PGPARMOR_ERR_NO_END_PGP = -4,
00954 PGPARMOR_ERR_UNKNOWN_PREAMBLE_TAG = -3,
00955 PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE = -2,
00956 PGPARMOR_ERR_NO_BEGIN_PGP = -1,
00957 #define PGPARMOR_ERROR PGPARMOR_ERR_NO_BEGIN_PGP
00958 PGPARMOR_NONE = 0,
00959 PGPARMOR_MESSAGE = 1,
00960 PGPARMOR_PUBKEY = 2,
00961 PGPARMOR_SIGNATURE = 3,
00962 PGPARMOR_SIGNED_MESSAGE = 4,
00963 PGPARMOR_FILE = 5,
00964 PGPARMOR_PRIVKEY = 6,
00965 PGPARMOR_SECKEY = 7
00966 } pgpArmor;
00967
00968
00972
00973 extern struct pgpValTbl_s pgpArmorTbl[];
00974
00977
00978 typedef enum pgpArmorKey_e {
00979 PGPARMORKEY_VERSION = 1,
00980 PGPARMORKEY_COMMENT = 2,
00981 PGPARMORKEY_MESSAGEID = 3,
00982 PGPARMORKEY_HASH = 4,
00983 PGPARMORKEY_CHARSET = 5
00984 } pgpArmorKey;
00985
00986
00990
00991 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00992
00996 typedef enum rpmDigestFlags_e {
00997 RPMDIGEST_NONE = 0
00998 } rpmDigestFlags;
00999
01000
01001 extern pgpHashAlgo rpmDigestHashAlgo;
01002
01003
01004 extern struct poptOption rpmDigestPoptTable[];
01005
01006
01007 #ifdef __cplusplus
01008 extern "C" {
01009 #endif
01010
01017 static inline
01018 unsigned int pgpGrab(const byte *s, int nbytes)
01019
01020 {
01021 unsigned int i = 0;
01022 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
01023
01024 while (nb--)
01025 i = (i << 8) | *s++;
01026
01027 return i;
01028 }
01029
01036 static inline
01037 int pgpLen(const byte *s, unsigned int *lenp)
01038
01039 {
01040
01041 if (*s < 192) {
01042 (*lenp) = *s++;
01043 return 1;
01044 } else if (*s < 255) {
01045 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01046 return 2;
01047 } else {
01048 (*lenp) = pgpGrab(s+1, 4);
01049 return 5;
01050 }
01051
01052 }
01053
01059 static inline
01060 unsigned int pgpMpiBits(const byte *p)
01061
01062
01063 {
01064 return ((p[0] << 8) | p[1]);
01065 }
01066
01072 static inline
01073 unsigned int pgpMpiLen(const byte *p)
01074
01075
01076 {
01077 return (2 + ((pgpMpiBits(p)+7)>>3));
01078 }
01079
01087 static inline
01088 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01089
01090 {
01091 static char hex[] = "0123456789abcdef";
01092
01093 while (nbytes-- > 0) {
01094 unsigned int i;
01095 i = *s++;
01096 *t++ = hex[ (i >> 4) & 0xf ];
01097 *t++ = hex[ (i ) & 0xf ];
01098 }
01099 *t = '\0';
01100
01101 return t;
01102 }
01103
01111 static inline
01112 char * pgpHexStr(const byte *p, unsigned int plen)
01113
01114 {
01115 static char prbuf[8*BUFSIZ];
01116 char *t = prbuf;
01117 t = pgpHexCvt(t, p, plen);
01118 return prbuf;
01119 }
01120
01127 static inline
01128 const char * pgpMpiStr(const byte *p)
01129
01130
01131 {
01132 static char prbuf[8*BUFSIZ];
01133 char *t = prbuf;
01134 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01135 t += strlen(t);
01136 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01137 return prbuf;
01138 }
01139
01146 static inline
01147 const char * pgpValStr(pgpValTbl vs, byte val)
01148
01149 {
01150 do {
01151 if (vs->val == val)
01152 break;
01153 } while ((++vs)->val != -1);
01154 return vs->str;
01155 }
01156
01164 static inline
01165 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01166
01167 {
01168 do {
01169 int vlen = strlen(vs->str);
01170 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01171 break;
01172 } while ((++vs)->val != -1);
01173 return vs->val;
01174 }
01175
01182
01183 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01184
01185 ;
01186
01187
01195
01196 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
01197
01198 ;
01199
01200
01206
01207 int pgpPrtSig(const pgpPkt pp)
01208
01209 ;
01210
01211
01217 int pgpPrtKey(const pgpPkt pp)
01218
01219 ;
01220
01226
01227 int pgpPrtUserID(const pgpPkt pp)
01228
01229 ;
01230
01231
01237
01238 int pgpPrtComment(const pgpPkt pp)
01239
01240 ;
01241
01242
01251
01252 int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
01253 byte * keyid)
01254 ;
01255
01256
01264 int pgpExtractPubkeyFingerprint(const char * b64pkt, byte * keyid)
01265 ;
01266
01274 int pgpPktLen(const byte *pkt, unsigned int pleft, pgpPkt pp)
01275 ;
01276
01283
01284 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01285
01286 ;
01287
01288
01297 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
01298
01299 ;
01300
01308 pgpArmor pgpReadPkts(const char * fn,
01309 const byte ** pkt, size_t * pktlen)
01310
01311 ;
01312
01320 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01321 ;
01322
01327
01328 pgpDig pgpNewDig(void)
01329 ;
01330
01335 void pgpCleanDig( pgpDig dig)
01336 ;
01337
01343
01344 pgpDig pgpFreeDig( pgpDig dig)
01345 ;
01346
01352 static inline
01353 int pgpIsPkt(const byte * p)
01354
01355 {
01356
01357 unsigned int val = *p++;
01358
01359 pgpTag tag;
01360 int rc;
01361
01362
01363 if (!(val & 0x80))
01364 return 0;
01365
01366 if (val & 0x40)
01367 tag = (val & 0x3f);
01368 else
01369 tag = (val >> 2) & 0xf;
01370
01371 switch (tag) {
01372 case PGPTAG_MARKER:
01373 case PGPTAG_SYMMETRIC_SESSION_KEY:
01374 case PGPTAG_ONEPASS_SIGNATURE:
01375 case PGPTAG_PUBLIC_KEY:
01376 case PGPTAG_SECRET_KEY:
01377 case PGPTAG_PUBLIC_SESSION_KEY:
01378 case PGPTAG_SIGNATURE:
01379 case PGPTAG_COMMENT:
01380 case PGPTAG_COMMENT_OLD:
01381 case PGPTAG_LITERAL_DATA:
01382 case PGPTAG_COMPRESSED_DATA:
01383 case PGPTAG_SYMMETRIC_DATA:
01384 rc = 1;
01385 break;
01386 case PGPTAG_PUBLIC_SUBKEY:
01387 case PGPTAG_SECRET_SUBKEY:
01388 case PGPTAG_USER_ID:
01389 case PGPTAG_RESERVED:
01390 case PGPTAG_TRUST:
01391 case PGPTAG_PHOTOID:
01392 case PGPTAG_ENCRYPTED_MDC:
01393 case PGPTAG_MDC:
01394 case PGPTAG_PRIVATE_60:
01395 case PGPTAG_PRIVATE_62:
01396 case PGPTAG_CONTROL:
01397 default:
01398 rc = 0;
01399 break;
01400 }
01401
01402 return rc;
01403 }
01404
01405 #define CRC24_INIT 0xb704ce
01406 #define CRC24_POLY 0x1864cfb
01407
01414 static inline
01415 unsigned int pgpCRC(const byte *octets, size_t len)
01416
01417 {
01418 unsigned int crc = CRC24_INIT;
01419 int i;
01420
01421 while (len--) {
01422
01423 crc ^= (*octets++) << 16;
01424
01425 for (i = 0; i < 8; i++) {
01426 crc <<= 1;
01427 if (crc & 0x1000000)
01428 crc ^= CRC24_POLY;
01429 }
01430 }
01431 return crc & 0xffffff;
01432 }
01433
01439
01440 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
01441 ;
01442
01450
01451 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
01452 ;
01453
01461 int rpmDigestUpdate( DIGEST_CTX ctx, const void * data, size_t len)
01462 ;
01463
01475 int rpmDigestFinal( DIGEST_CTX ctx,
01476 void * datap,
01477 size_t * lenp, int asAscii)
01478 ;
01479
01480 #ifdef __cplusplus
01481 }
01482 #endif
01483
01484
01485 #endif