00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #include "base64.h"
00014 #include "dsa.h"
00015 #include "endianness.h"
00016 #include "mp32.h"
00017 #include "rsa.h"
00018 #include "rsapk.h"
00019
00020
00023 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00024
00027 typedef const struct pgpValTbl_s {
00028 int val;
00029 const char * str;
00030 } * pgpValTbl;
00031
00039 typedef enum pgpTag_e {
00040 PGPTAG_RESERVED = 0,
00041 PGPTAG_PUBLIC_SESSION_KEY = 1,
00042 PGPTAG_SIGNATURE = 2,
00043 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00044 PGPTAG_ONEPASS_SIGNATURE = 4,
00045 PGPTAG_SECRET_KEY = 5,
00046 PGPTAG_PUBLIC_KEY = 6,
00047 PGPTAG_SECRET_SUBKEY = 7,
00048 PGPTAG_COMPRESSED_DATA = 8,
00049 PGPTAG_SYMMETRIC_DATA = 9,
00050 PGPTAG_MARKER = 10,
00051 PGPTAG_LITERAL_DATA = 11,
00052 PGPTAG_TRUST = 12,
00053 PGPTAG_USER_ID = 13,
00054 PGPTAG_PUBLIC_SUBKEY = 14,
00055 PGPTAG_COMMENT_OLD = 16,
00056 PGPTAG_PHOTOID = 17,
00057 PGPTAG_ENCRYPTED_MDC = 18,
00058 PGPTAG_MDC = 19,
00059 PGPTAG_PRIVATE_60 = 60,
00060 PGPTAG_COMMENT = 61,
00061 PGPTAG_PRIVATE_62 = 62,
00062 PGPTAG_CONTROL = 63,
00063 } pgpTag;
00064
00067
00068 extern struct pgpValTbl_s pgpTagTbl[];
00069
00104 typedef struct pgpPktPubkey_s {
00105 byte version;
00106 byte keyid[8];
00107 byte algo;
00108 } pgpPktPubkey;
00109
00110
00117
00118 typedef enum pgpSigType_e {
00119 PGPSIGTYPE_BINARY = 0x00,
00120 PGPSIGTYPE_TEXT = 0x01,
00121 PGPSIGTYPE_STANDALONE = 0x02,
00122 PGPSIGTYPE_GENERIC_CERT = 0x10,
00124 PGPSIGTYPE_PERSONA_CERT = 0x11,
00126 PGPSIGTYPE_CASUAL_CERT = 0x12,
00128 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00130 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00131 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00132 PGPSIGTYPE_KEY_REVOKE = 0x20,
00133 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00134 PGPSIGTYPE_CERT_REVOKE = 0x30,
00135 PGPSIGTYPE_TIMESTAMP = 0x40
00136 } pgpSigType;
00137
00138
00141
00142 extern struct pgpValTbl_s pgpSigTypeTbl[];
00143
00167
00168 typedef enum pgpPubkeyAlgo_e {
00169 PGPPUBKEYALGO_RSA = 1,
00170 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00171 PGPPUBKEYALGO_RSA_SIGN = 3,
00172 PGPPUBKEYALGO_ELGAMAL_ENCRYPT= 16,
00173 PGPPUBKEYALGO_DSA = 17,
00174 PGPPUBKEYALGO_EC = 18,
00175 PGPPUBKEYALGO_ECDSA = 19,
00176 PGPPUBKEYALGO_ELGAMAL = 20,
00177 PGPPUBKEYALGO_DH = 21,
00178 } pgpPubkeyAlgo;
00179
00180
00183
00184 extern struct pgpValTbl_s pgpPubkeyTbl[];
00185
00210
00211 typedef enum pgpSymkeyAlgo_e {
00212 PGPSYMKEYALGO_PLAINTEXT = 0,
00213 PGPSYMKEYALGO_IDEA = 1,
00214 PGPSYMKEYALGO_TRIPLE_DES = 2,
00215 PGPSYMKEYALGO_CAST5 = 3,
00216 PGPSYMKEYALGO_BLOWFISH = 4,
00217 PGPSYMKEYALGO_SAFER = 5,
00218 PGPSYMKEYALGO_DES_SK = 6,
00219 PGPSYMKEYALGO_AES_128 = 7,
00220 PGPSYMKEYALGO_AES_192 = 8,
00221 PGPSYMKEYALGO_AES_256 = 9,
00222 PGPSYMKEYALGO_TWOFISH = 10
00223 } pgpSymkeyAlgo;
00224
00225
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;
00252
00253
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
00291
00292 extern struct pgpValTbl_s pgpHashTbl[];
00293
00315 typedef struct pgpPktSigV3_s {
00316 byte version;
00317 byte hashlen;
00318 byte sigtype;
00319 byte time[4];
00320 byte signid[8];
00321 byte pubkey_algo;
00322 byte hash_algo;
00323 byte signhash16[2];
00324 } * pgpPktSigV3;
00325
00347 typedef struct pgpPktSigV4_s {
00348 byte version;
00349 byte sigtype;
00350 byte pubkey_algo;
00351 byte hash_algo;
00352 byte hashlen[2];
00353 } * pgpPktSigV4;
00354
00421
00422 typedef enum pgpSubType_e {
00423 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00424 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00425 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00426 PGPSUBTYPE_TRUST_SIG = 5,
00427 PGPSUBTYPE_REGEX = 6,
00428 PGPSUBTYPE_REVOCABLE = 7,
00429 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00430 PGPSUBTYPE_BACKWARD_COMPAT = 10,
00431 PGPSUBTYPE_PREFER_SYMKEY = 11,
00432 PGPSUBTYPE_REVOKE_KEY = 12,
00433 PGPSUBTYPE_ISSUER_KEYID = 16,
00434 PGPSUBTYPE_NOTATION = 20,
00435 PGPSUBTYPE_PREFER_HASH = 21,
00436 PGPSUBTYPE_PREFER_COMPRESS = 22,
00437 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00438 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00439 PGPSUBTYPE_PRIMARY_USERID = 25,
00440 PGPSUBTYPE_POLICY_URL = 26,
00441 PGPSUBTYPE_KEY_FLAGS = 27,
00442 PGPSUBTYPE_SIGNER_USERID = 28,
00443 PGPSUBTYPE_REVOKE_REASON = 29,
00444 PGPSUBTYPE_INTERNAL_100 = 100,
00445 PGPSUBTYPE_INTERNAL_101 = 101,
00446 PGPSUBTYPE_INTERNAL_102 = 102,
00447 PGPSUBTYPE_INTERNAL_103 = 103,
00448 PGPSUBTYPE_INTERNAL_104 = 104,
00449 PGPSUBTYPE_INTERNAL_105 = 105,
00450 PGPSUBTYPE_INTERNAL_106 = 106,
00451 PGPSUBTYPE_INTERNAL_107 = 107,
00452 PGPSUBTYPE_INTERNAL_108 = 108,
00453 PGPSUBTYPE_INTERNAL_109 = 109,
00454 PGPSUBTYPE_INTERNAL_110 = 110,
00455 } pgpSubType;
00456
00457
00460
00461 extern struct pgpValTbl_s pgpSubTypeTbl[];
00462
00483 typedef union pgpPktSig_u {
00484 struct pgpPktSigV3_s v3;
00485 struct pgpPktSigV4_s v4;
00486 } * pgpPktSig;
00487
00516 typedef struct pgpPktSymkey_s {
00517 byte version;
00518 byte symkey_algo;
00519 byte s2k[1];
00520 } pgpPktSymkey;
00521
00551 typedef struct pgpPktOnepass_s {
00552 byte version;
00553 byte sigtype;
00554 byte hash_algo;
00555 byte pubkey_algo;
00556 byte signid[8];
00557 byte nested;
00558 } * pgpPktOnepass;
00559
00632 typedef struct pgpPktKeyV3_s {
00633 byte version;
00634 byte time[4];
00635 byte valid[2];
00636 byte pubkey_algo;
00637 } * pgpPktKeyV3;
00638
00670 typedef struct pgpPktKeyV4_s {
00671 byte version;
00672 byte time[4];
00673 byte pubkey_algo;
00674 } * pgpPktKeyV4;
00675
00740 typedef union pgpPktKey_u {
00741 struct pgpPktKeyV3_s v3;
00742 struct pgpPktKeyV4_s v4;
00743 } pgpPktKey;
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 typedef struct pgpPktCdata_s {
00770 byte compressalgo;
00771 byte data[1];
00772 } pgpPktCdata;
00773
00774
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 typedef struct pgpPktEdata_s {
00809 byte data[1];
00810 } pgpPktEdata;
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
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 typedef struct pgpPktLdata_s {
00860 byte format;
00861 byte filenamelen;
00862 byte filename[1];
00863 } pgpPktLdata;
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878 typedef struct pgpPktTrust_s {
00879 byte flag;
00880 } pgpPktTrust;
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 typedef struct pgpPktUid_s {
00893 byte userid[1];
00894 } pgpPktUid;
00895
00898 union pgpPktPre_u {
00899 pgpPktPubkey pubkey;
00900 pgpPktSig sig;
00901 pgpPktSymkey symkey;
00902 pgpPktOnepass onepass;
00903 pgpPktKey key;
00904 pgpPktCdata cdata;
00905 pgpPktEdata edata;
00907 pgpPktLdata ldata;
00908 pgpPktTrust tdata;
00909 pgpPktUid uid;
00910 };
00911
00914
00915 typedef enum pgpArmor_e {
00916 PGPARMOR_ERROR = -1,
00917 PGPARMOR_NONE = 0,
00918 PGPARMOR_MESSAGE = 1,
00919 PGPARMOR_PUBKEY = 2,
00920 PGPARMOR_SIGNATURE = 3,
00921 PGPARMOR_SIGNED_MESSAGE = 4,
00922 PGPARMOR_FILE = 5,
00923 PGPARMOR_PRIVKEY = 6,
00924 PGPARMOR_SECKEY = 7,
00925 } pgpArmor;
00926
00927
00930
00931 extern struct pgpValTbl_s pgpArmorTbl[];
00932
00935
00936 typedef enum pgpArmorKey_e {
00937 PGPARMORKEY_VERSION = 1,
00938 PGPARMORKEY_COMMENT = 2,
00939 PGPARMORKEY_MESSAGEID = 3,
00940 PGPARMORKEY_HASH = 4,
00941 PGPARMORKEY_CHARSET = 5,
00942 } pgpArmorKey;
00943
00944
00947
00948 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00949
00952 struct pgpDigParams_s {
00953
00954 const char * userid;
00955
00956 const byte * hash;
00957 const char * params[4];
00958 byte tag;
00959
00960 byte version;
00961 byte time[4];
00962 byte pubkey_algo;
00964 byte hash_algo;
00965 byte sigtype;
00966 byte hashlen;
00967 byte signhash16[2];
00968 byte signid[8];
00969 };
00970
00973 struct pgpDig_s {
00974 struct pgpDigParams_s signature;
00975 struct pgpDigParams_s pubkey;
00976
00977 size_t nbytes;
00979 DIGEST_CTX sha1ctx;
00980 void * sha1;
00981 size_t sha1len;
00983 DIGEST_CTX md5ctx;
00984 void * md5;
00985 size_t md5len;
00987
00988 mp32barrett p;
00989 mp32barrett q;
00990 mp32number g;
00991 mp32number y;
00992 mp32number hm;
00993 mp32number r;
00994 mp32number s;
00995
00996
00997 rsapk rsa_pk;
00998 mp32number m;
00999 mp32number c;
01000 mp32number rsahm;
01001 };
01002
01003
01004
01005 #ifdef __cplusplus
01006 extern "C" {
01007 #endif
01008
01011 static inline
01012 unsigned int pgpGrab(const byte *s, int nbytes)
01013
01014 {
01015 unsigned int i = 0;
01016 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
01017 while (nb--)
01018 i = (i << 8) | *s++;
01019 return i;
01020 }
01021
01024 static inline
01025 int pgpLen(const byte *s, unsigned int *lenp)
01026
01027 {
01028 if (*s < 192) {
01029 (*lenp) = *s++;
01030 return 1;
01031 } else if (*s < 255) {
01032 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01033 return 2;
01034 } else {
01035 (*lenp) = pgpGrab(s+1, 4);
01036 return 5;
01037 }
01038 }
01039
01042 static inline
01043 unsigned int pgpMpiBits(const byte *p)
01044
01045 {
01046 return ((p[0] << 8) | p[1]);
01047 }
01048
01051 static inline
01052 unsigned int pgpMpiLen(const byte *p)
01053
01054 {
01055 return (2 + ((pgpMpiBits(p)+7)>>3));
01056 }
01057
01060 static inline
01061 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01062
01063 {
01064 static char hex[] = "0123456789abcdef";
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 return t;
01073 }
01074
01077 static inline
01078 char * pgpHexStr(const byte *p, unsigned int plen)
01079
01080 {
01081 static char prbuf[2048];
01082 char *t = prbuf;
01083 t = pgpHexCvt(t, p, plen);
01084 return prbuf;
01085 }
01086
01089 static inline
01090 const char * pgpMpiStr(const byte *p)
01091
01092 {
01093 static char prbuf[2048];
01094 char *t = prbuf;
01095 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01096 t += strlen(t);
01097 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01098 return prbuf;
01099 }
01100
01103 static inline
01104 const char * pgpValStr(pgpValTbl vs, byte val)
01105
01106 {
01107 do {
01108 if (vs->val == val)
01109 break;
01110 } while ((++vs)->val != -1);
01111 return vs->str;
01112 }
01113
01116 static inline
01117 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01118
01119 {
01120 do {
01121 int vlen = strlen(vs->str);
01122 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01123 break;
01124 } while ((++vs)->val != -1);
01125 return vs->val;
01126 }
01127
01128
01131 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01132
01133 ;
01134
01137 int pgpPrtSubType(const byte *h, unsigned int hlen)
01138
01139 ;
01140
01143 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01144
01145 ;
01146
01149 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01150
01151 ;
01152
01155 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01156
01157 ;
01158
01161 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01162
01163 ;
01164
01167 int pgpPrtPkt(const byte *pkt)
01168
01169 ;
01170
01171
01174 int pgpPrtPkts(const byte *pkts, unsigned int plen, struct pgpDig_s *dig, int printing)
01175
01176 ;
01177
01180 pgpArmor pgpReadPkts(const char * fn,
01181 const byte ** pkt, size_t * pktlen)
01182
01183 ;
01184
01187
01188 struct pgpDig_s * pgpNewDig(void)
01189 ;
01190
01193
01194 struct pgpDig_s * pgpFreeDig( struct pgpDig_s * dig)
01195 ;
01196
01199 static inline
01200 int pgpIsPkt(const byte * p)
01201
01202 {
01203 unsigned int val = *p++;
01204 pgpTag tag;
01205 int rc;
01206
01207
01208 if (!(val & 0x80))
01209 return 0;
01210
01211 if (val & 0x40)
01212 tag = (val & 0x3f);
01213 else
01214 tag = (val >> 2) & 0xf;
01215
01216 switch (tag) {
01217 case PGPTAG_MARKER:
01218 case PGPTAG_SYMMETRIC_SESSION_KEY:
01219 case PGPTAG_ONEPASS_SIGNATURE:
01220 case PGPTAG_PUBLIC_KEY:
01221 case PGPTAG_SECRET_KEY:
01222 case PGPTAG_PUBLIC_SESSION_KEY:
01223 case PGPTAG_SIGNATURE:
01224 case PGPTAG_COMMENT:
01225 case PGPTAG_COMMENT_OLD:
01226 case PGPTAG_LITERAL_DATA:
01227 case PGPTAG_COMPRESSED_DATA:
01228 case PGPTAG_SYMMETRIC_DATA:
01229 rc = 1;
01230 break;
01231 case PGPTAG_PUBLIC_SUBKEY:
01232 case PGPTAG_SECRET_SUBKEY:
01233 case PGPTAG_USER_ID:
01234 case PGPTAG_RESERVED:
01235 case PGPTAG_TRUST:
01236 case PGPTAG_PHOTOID:
01237 case PGPTAG_ENCRYPTED_MDC:
01238 case PGPTAG_MDC:
01239 case PGPTAG_PRIVATE_60:
01240 case PGPTAG_PRIVATE_62:
01241 case PGPTAG_CONTROL:
01242 default:
01243 rc = 0;
01244 break;
01245 }
01246
01247 return rc;
01248 }
01249
01250 #define CRC24_INIT 0xb704ce
01251 #define CRC24_POLY 0x1864cfb
01252
01255 static inline
01256 uint32 pgpCRC(const byte *octets, size_t len)
01257
01258 {
01259 uint32 crc = CRC24_INIT;
01260 int i;
01261
01262 while (len--) {
01263 crc ^= (*octets++) << 16;
01264 for (i = 0; i < 8; i++) {
01265 crc <<= 1;
01266 if (crc & 0x1000000)
01267 crc ^= CRC24_POLY;
01268 }
01269 }
01270 return crc & 0xffffff;
01271 }
01272
01273 #ifdef __cplusplus
01274 }
01275 #endif
01276
01277
01278 #endif