00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #undef PACKAGE
00014 #undef VERSION
00015 #include "base64.h"
00016 #include "dsa.h"
00017 #include "endianness.h"
00018 #include "mp32.h"
00019 #include "rsa.h"
00020 #include "rsapk.h"
00021
00022
00025 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00026
00029 typedef const struct pgpValTbl_s {
00030 int val;
00031 const char * str;
00032 } * pgpValTbl;
00033
00041 typedef enum pgpTag_e {
00042 PGPTAG_RESERVED = 0,
00043 PGPTAG_PUBLIC_SESSION_KEY = 1,
00044 PGPTAG_SIGNATURE = 2,
00045 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00046 PGPTAG_ONEPASS_SIGNATURE = 4,
00047 PGPTAG_SECRET_KEY = 5,
00048 PGPTAG_PUBLIC_KEY = 6,
00049 PGPTAG_SECRET_SUBKEY = 7,
00050 PGPTAG_COMPRESSED_DATA = 8,
00051 PGPTAG_SYMMETRIC_DATA = 9,
00052 PGPTAG_MARKER = 10,
00053 PGPTAG_LITERAL_DATA = 11,
00054 PGPTAG_TRUST = 12,
00055 PGPTAG_USER_ID = 13,
00056 PGPTAG_PUBLIC_SUBKEY = 14,
00057 PGPTAG_COMMENT_OLD = 16,
00058 PGPTAG_PHOTOID = 17,
00059 PGPTAG_ENCRYPTED_MDC = 18,
00060 PGPTAG_MDC = 19,
00061 PGPTAG_PRIVATE_60 = 60,
00062 PGPTAG_COMMENT = 61,
00063 PGPTAG_PRIVATE_62 = 62,
00064 PGPTAG_CONTROL = 63,
00065 } pgpTag;
00066
00069
00070 extern struct pgpValTbl_s pgpTagTbl[];
00071
00106 typedef struct pgpPktPubkey_s {
00107 byte version;
00108 byte keyid[8];
00109 byte algo;
00110 } pgpPktPubkey;
00111
00112
00119
00120 typedef enum pgpSigType_e {
00121 PGPSIGTYPE_BINARY = 0x00,
00122 PGPSIGTYPE_TEXT = 0x01,
00123 PGPSIGTYPE_STANDALONE = 0x02,
00124 PGPSIGTYPE_GENERIC_CERT = 0x10,
00126 PGPSIGTYPE_PERSONA_CERT = 0x11,
00128 PGPSIGTYPE_CASUAL_CERT = 0x12,
00130 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00132 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00133 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00134 PGPSIGTYPE_KEY_REVOKE = 0x20,
00135 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00136 PGPSIGTYPE_CERT_REVOKE = 0x30,
00137 PGPSIGTYPE_TIMESTAMP = 0x40
00138 } pgpSigType;
00139
00140
00143
00144 extern struct pgpValTbl_s pgpSigTypeTbl[];
00145
00169
00170 typedef enum pgpPubkeyAlgo_e {
00171 PGPPUBKEYALGO_RSA = 1,
00172 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00173 PGPPUBKEYALGO_RSA_SIGN = 3,
00174 PGPPUBKEYALGO_ELGAMAL_ENCRYPT= 16,
00175 PGPPUBKEYALGO_DSA = 17,
00176 PGPPUBKEYALGO_EC = 18,
00177 PGPPUBKEYALGO_ECDSA = 19,
00178 PGPPUBKEYALGO_ELGAMAL = 20,
00179 PGPPUBKEYALGO_DH = 21,
00180 } pgpPubkeyAlgo;
00181
00182
00185
00186 extern struct pgpValTbl_s pgpPubkeyTbl[];
00187
00212
00213 typedef enum pgpSymkeyAlgo_e {
00214 PGPSYMKEYALGO_PLAINTEXT = 0,
00215 PGPSYMKEYALGO_IDEA = 1,
00216 PGPSYMKEYALGO_TRIPLE_DES = 2,
00217 PGPSYMKEYALGO_CAST5 = 3,
00218 PGPSYMKEYALGO_BLOWFISH = 4,
00219 PGPSYMKEYALGO_SAFER = 5,
00220 PGPSYMKEYALGO_DES_SK = 6,
00221 PGPSYMKEYALGO_AES_128 = 7,
00222 PGPSYMKEYALGO_AES_192 = 8,
00223 PGPSYMKEYALGO_AES_256 = 9,
00224 PGPSYMKEYALGO_TWOFISH = 10
00225 } pgpSymkeyAlgo;
00226
00227
00230
00231 extern struct pgpValTbl_s pgpSymkeyTbl[];
00232
00248
00249 typedef enum pgpCompressAlgo_e {
00250 PGPCOMPRESSALGO_NONE = 0,
00251 PGPCOMPRESSALGO_ZIP = 1,
00252 PGPCOMPRESSALGO_ZLIB = 2
00253 } pgpCompressAlgo;
00254
00255
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;
00290
00293
00294 extern struct pgpValTbl_s pgpHashTbl[];
00295
00317 typedef struct pgpPktSigV3_s {
00318 byte version;
00319 byte hashlen;
00320 byte sigtype;
00321 byte time[4];
00322 byte signid[8];
00323 byte pubkey_algo;
00324 byte hash_algo;
00325 byte signhash16[2];
00326 } * pgpPktSigV3;
00327
00349 typedef struct pgpPktSigV4_s {
00350 byte version;
00351 byte sigtype;
00352 byte pubkey_algo;
00353 byte hash_algo;
00354 byte hashlen[2];
00355 } * pgpPktSigV4;
00356
00423
00424 typedef enum pgpSubType_e {
00425 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00426 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00427 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00428 PGPSUBTYPE_TRUST_SIG = 5,
00429 PGPSUBTYPE_REGEX = 6,
00430 PGPSUBTYPE_REVOCABLE = 7,
00431 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00432 PGPSUBTYPE_BACKWARD_COMPAT = 10,
00433 PGPSUBTYPE_PREFER_SYMKEY = 11,
00434 PGPSUBTYPE_REVOKE_KEY = 12,
00435 PGPSUBTYPE_ISSUER_KEYID = 16,
00436 PGPSUBTYPE_NOTATION = 20,
00437 PGPSUBTYPE_PREFER_HASH = 21,
00438 PGPSUBTYPE_PREFER_COMPRESS = 22,
00439 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00440 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00441 PGPSUBTYPE_PRIMARY_USERID = 25,
00442 PGPSUBTYPE_POLICY_URL = 26,
00443 PGPSUBTYPE_KEY_FLAGS = 27,
00444 PGPSUBTYPE_SIGNER_USERID = 28,
00445 PGPSUBTYPE_REVOKE_REASON = 29,
00446 PGPSUBTYPE_INTERNAL_100 = 100,
00447 PGPSUBTYPE_INTERNAL_101 = 101,
00448 PGPSUBTYPE_INTERNAL_102 = 102,
00449 PGPSUBTYPE_INTERNAL_103 = 103,
00450 PGPSUBTYPE_INTERNAL_104 = 104,
00451 PGPSUBTYPE_INTERNAL_105 = 105,
00452 PGPSUBTYPE_INTERNAL_106 = 106,
00453 PGPSUBTYPE_INTERNAL_107 = 107,
00454 PGPSUBTYPE_INTERNAL_108 = 108,
00455 PGPSUBTYPE_INTERNAL_109 = 109,
00456 PGPSUBTYPE_INTERNAL_110 = 110,
00457 } pgpSubType;
00458
00459
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
00932
00933 extern struct pgpValTbl_s pgpArmorTbl[];
00934
00937
00938 typedef enum pgpArmorKey_e {
00939 PGPARMORKEY_VERSION = 1,
00940 PGPARMORKEY_COMMENT = 2,
00941 PGPARMORKEY_MESSAGEID = 3,
00942 PGPARMORKEY_HASH = 4,
00943 PGPARMORKEY_CHARSET = 5,
00944 } pgpArmorKey;
00945
00946
00949
00950 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00951
00954 struct pgpDigParams_s {
00955
00956 const char * userid;
00957
00958 const byte * hash;
00959 const char * params[4];
00960 byte tag;
00961
00962 byte version;
00963 byte time[4];
00964 byte pubkey_algo;
00966 byte hash_algo;
00967 byte sigtype;
00968 byte hashlen;
00969 byte signhash16[2];
00970 byte signid[8];
00971 };
00972
00975 struct pgpDig_s {
00976 struct pgpDigParams_s signature;
00977 struct pgpDigParams_s pubkey;
00978
00979 size_t nbytes;
00981 DIGEST_CTX sha1ctx;
00982 void * sha1;
00983 size_t sha1len;
00985 DIGEST_CTX md5ctx;
00986 void * md5;
00987 size_t md5len;
00989
00990 mp32barrett p;
00991 mp32barrett q;
00992 mp32number g;
00993 mp32number y;
00994 mp32number hm;
00995 mp32number r;
00996 mp32number s;
00997
00998
00999 rsapk rsa_pk;
01000 mp32number m;
01001 mp32number c;
01002 mp32number rsahm;
01003 };
01004
01005
01006
01007 #ifdef __cplusplus
01008 extern "C" {
01009 #endif
01010
01013 static inline
01014 unsigned int pgpGrab(const byte *s, int nbytes)
01015
01016 {
01017 unsigned int i = 0;
01018 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
01019 while (nb--)
01020 i = (i << 8) | *s++;
01021 return i;
01022 }
01023
01026 static inline
01027 int pgpLen(const byte *s, unsigned int *lenp)
01028
01029 {
01030 if (*s < 192) {
01031 (*lenp) = *s++;
01032 return 1;
01033 } else if (*s < 255) {
01034 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01035 return 2;
01036 } else {
01037 (*lenp) = pgpGrab(s+1, 4);
01038 return 5;
01039 }
01040 }
01041
01044 static inline
01045 unsigned int pgpMpiBits(const byte *p)
01046
01047 {
01048 return ((p[0] << 8) | p[1]);
01049 }
01050
01053 static inline
01054 unsigned int pgpMpiLen(const byte *p)
01055
01056 {
01057 return (2 + ((pgpMpiBits(p)+7)>>3));
01058 }
01059
01062 static inline
01063 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01064
01065 {
01066 static char hex[] = "0123456789abcdef";
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 return t;
01075 }
01076
01079 static inline
01080 char * pgpHexStr(const byte *p, unsigned int plen)
01081
01082 {
01083 static char prbuf[2048];
01084 char *t = prbuf;
01085 t = pgpHexCvt(t, p, plen);
01086 return prbuf;
01087 }
01088
01091 static inline
01092 const char * pgpMpiStr(const byte *p)
01093
01094 {
01095 static char prbuf[2048];
01096 char *t = prbuf;
01097 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01098 t += strlen(t);
01099 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01100 return prbuf;
01101 }
01102
01105 static inline
01106 const char * pgpValStr(pgpValTbl vs, byte val)
01107
01108 {
01109 do {
01110 if (vs->val == val)
01111 break;
01112 } while ((++vs)->val != -1);
01113 return vs->str;
01114 }
01115
01118 static inline
01119 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01120
01121 {
01122 do {
01123 int vlen = strlen(vs->str);
01124 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01125 break;
01126 } while ((++vs)->val != -1);
01127 return vs->val;
01128 }
01129
01130
01133 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01134
01135 ;
01136
01139 int pgpPrtSubType(const byte *h, unsigned int hlen)
01140
01141 ;
01142
01145 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01146
01147 ;
01148
01151 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01152
01153 ;
01154
01157 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01158
01159 ;
01160
01163 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01164
01165 ;
01166
01169 int pgpPrtPkt(const byte *pkt)
01170
01171 ;
01172
01173
01176 int pgpPrtPkts(const byte *pkts, unsigned int plen, struct pgpDig_s *dig, int printing)
01177
01178 ;
01179
01182 pgpArmor pgpReadPkts(const char * fn,
01183 const byte ** pkt, size_t * pktlen)
01184
01185 ;
01186
01189
01190 struct pgpDig_s * pgpNewDig(void)
01191 ;
01192
01195
01196 struct pgpDig_s * pgpFreeDig( struct pgpDig_s * dig)
01197 ;
01198
01201 static inline
01202 int pgpIsPkt(const byte * p)
01203
01204 {
01205 unsigned int val = *p++;
01206 pgpTag tag;
01207 int rc;
01208
01209
01210 if (!(val & 0x80))
01211 return 0;
01212
01213 if (val & 0x40)
01214 tag = (val & 0x3f);
01215 else
01216 tag = (val >> 2) & 0xf;
01217
01218 switch (tag) {
01219 case PGPTAG_MARKER:
01220 case PGPTAG_SYMMETRIC_SESSION_KEY:
01221 case PGPTAG_ONEPASS_SIGNATURE:
01222 case PGPTAG_PUBLIC_KEY:
01223 case PGPTAG_SECRET_KEY:
01224 case PGPTAG_PUBLIC_SESSION_KEY:
01225 case PGPTAG_SIGNATURE:
01226 case PGPTAG_COMMENT:
01227 case PGPTAG_COMMENT_OLD:
01228 case PGPTAG_LITERAL_DATA:
01229 case PGPTAG_COMPRESSED_DATA:
01230 case PGPTAG_SYMMETRIC_DATA:
01231 rc = 1;
01232 break;
01233 case PGPTAG_PUBLIC_SUBKEY:
01234 case PGPTAG_SECRET_SUBKEY:
01235 case PGPTAG_USER_ID:
01236 case PGPTAG_RESERVED:
01237 case PGPTAG_TRUST:
01238 case PGPTAG_PHOTOID:
01239 case PGPTAG_ENCRYPTED_MDC:
01240 case PGPTAG_MDC:
01241 case PGPTAG_PRIVATE_60:
01242 case PGPTAG_PRIVATE_62:
01243 case PGPTAG_CONTROL:
01244 default:
01245 rc = 0;
01246 break;
01247 }
01248
01249 return rc;
01250 }
01251
01252 #define CRC24_INIT 0xb704ce
01253 #define CRC24_POLY 0x1864cfb
01254
01257 static inline
01258 uint32 pgpCRC(const byte *octets, size_t len)
01259
01260 {
01261 uint32 crc = CRC24_INIT;
01262 int i;
01263
01264 while (len--) {
01265 crc ^= (*octets++) << 16;
01266 for (i = 0; i < 8; i++) {
01267 crc <<= 1;
01268 if (crc & 0x1000000)
01269 crc ^= CRC24_POLY;
01270 }
01271 }
01272 return crc & 0xffffff;
01273 }
01274
01275 #ifdef __cplusplus
01276 }
01277 #endif
01278
01279
01280 #endif