00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003
00013 #include <beecrypt/base64.h>
00014 #include <beecrypt/dsa.h>
00015 #include <beecrypt/endianness.h>
00016 #include <beecrypt/mp32.h>
00017 #include <beecrypt/rsa.h>
00018 #include <beecrypt/rsapk.h>
00019
00022 typedef struct DIGEST_CTX_s * DIGEST_CTX;
00023
00026 typedef const struct pgpValTbl_s {
00027 int val;
00028 const char * str;
00029 } * pgpValTbl;
00030
00038 typedef enum pgpTag_e {
00039 PGPTAG_RESERVED = 0,
00040 PGPTAG_PUBLIC_SESSION_KEY = 1,
00041 PGPTAG_SIGNATURE = 2,
00042 PGPTAG_SYMMETRIC_SESSION_KEY= 3,
00043 PGPTAG_ONEPASS_SIGNATURE = 4,
00044 PGPTAG_SECRET_KEY = 5,
00045 PGPTAG_PUBLIC_KEY = 6,
00046 PGPTAG_SECRET_SUBKEY = 7,
00047 PGPTAG_COMPRESSED_DATA = 8,
00048 PGPTAG_SYMMETRIC_DATA = 9,
00049 PGPTAG_MARKER = 10,
00050 PGPTAG_LITERAL_DATA = 11,
00051 PGPTAG_TRUST = 12,
00052 PGPTAG_USER_ID = 13,
00053 PGPTAG_PUBLIC_SUBKEY = 14,
00054 PGPTAG_COMMENT_OLD = 16,
00055 PGPTAG_PHOTOID = 17,
00056 PGPTAG_ENCRYPTED_MDC = 18,
00057 PGPTAG_MDC = 19,
00058 PGPTAG_PRIVATE_60 = 60,
00059 PGPTAG_COMMENT = 61,
00060 PGPTAG_PRIVATE_62 = 62,
00061 PGPTAG_CONTROL = 63
00062 } pgpTag;
00063
00066
00067 extern struct pgpValTbl_s pgpTagTbl[];
00068
00103 typedef struct pgpPktPubkey_s {
00104 byte version;
00105 byte keyid[8];
00106 byte algo;
00107 } pgpPktPubkey;
00108
00109
00116
00117 typedef enum pgpSigType_e {
00118 PGPSIGTYPE_BINARY = 0x00,
00119 PGPSIGTYPE_TEXT = 0x01,
00120 PGPSIGTYPE_STANDALONE = 0x02,
00121 PGPSIGTYPE_GENERIC_CERT = 0x10,
00123 PGPSIGTYPE_PERSONA_CERT = 0x11,
00125 PGPSIGTYPE_CASUAL_CERT = 0x12,
00127 PGPSIGTYPE_POSITIVE_CERT = 0x13,
00129 PGPSIGTYPE_SUBKEY_BINDING = 0x18,
00130 PGPSIGTYPE_SIGNED_KEY = 0x1F,
00131 PGPSIGTYPE_KEY_REVOKE = 0x20,
00132 PGPSIGTYPE_SUBKEY_REVOKE = 0x28,
00133 PGPSIGTYPE_CERT_REVOKE = 0x30,
00134 PGPSIGTYPE_TIMESTAMP = 0x40
00135 } pgpSigType;
00136
00137
00140
00141 extern struct pgpValTbl_s pgpSigTypeTbl[];
00142
00166
00167 typedef enum pgpPubkeyAlgo_e {
00168 PGPPUBKEYALGO_RSA = 1,
00169 PGPPUBKEYALGO_RSA_ENCRYPT = 2,
00170 PGPPUBKEYALGO_RSA_SIGN = 3,
00171 PGPPUBKEYALGO_ELGAMAL_ENCRYPT= 16,
00172 PGPPUBKEYALGO_DSA = 17,
00173 PGPPUBKEYALGO_EC = 18,
00174 PGPPUBKEYALGO_ECDSA = 19,
00175 PGPPUBKEYALGO_ELGAMAL = 20,
00176 PGPPUBKEYALGO_DH = 21
00177 } pgpPubkeyAlgo;
00178
00179
00182
00183 extern struct pgpValTbl_s pgpPubkeyTbl[];
00184
00209
00210 typedef enum pgpSymkeyAlgo_e {
00211 PGPSYMKEYALGO_PLAINTEXT = 0,
00212 PGPSYMKEYALGO_IDEA = 1,
00213 PGPSYMKEYALGO_TRIPLE_DES = 2,
00214 PGPSYMKEYALGO_CAST5 = 3,
00215 PGPSYMKEYALGO_BLOWFISH = 4,
00216 PGPSYMKEYALGO_SAFER = 5,
00217 PGPSYMKEYALGO_DES_SK = 6,
00218 PGPSYMKEYALGO_AES_128 = 7,
00219 PGPSYMKEYALGO_AES_192 = 8,
00220 PGPSYMKEYALGO_AES_256 = 9,
00221 PGPSYMKEYALGO_TWOFISH = 10
00222 } pgpSymkeyAlgo;
00223
00224
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
00255
00256 extern struct pgpValTbl_s pgpCompressionTbl[];
00257
00279 typedef enum pgpHashAlgo_e {
00280 PGPHASHALGO_MD5 = 1,
00281 PGPHASHALGO_SHA1 = 2,
00282 PGPHASHALGO_RIPEMD160 = 3,
00283 PGPHASHALGO_MD2 = 5,
00284 PGPHASHALGO_TIGER192 = 6,
00285 PGPHASHALGO_HAVAL_5_160 = 7
00286 } pgpHashAlgo;
00287
00290
00291 extern struct pgpValTbl_s pgpHashTbl[];
00292
00314 typedef struct pgpPktSigV3_s {
00315 byte version;
00316 byte hashlen;
00317 byte sigtype;
00318 byte time[4];
00319 byte signid[8];
00320 byte pubkey_algo;
00321 byte hash_algo;
00322 byte signhash16[2];
00323 } * pgpPktSigV3;
00324
00346 typedef struct pgpPktSigV4_s {
00347 byte version;
00348 byte sigtype;
00349 byte pubkey_algo;
00350 byte hash_algo;
00351 byte hashlen[2];
00352 } * pgpPktSigV4;
00353
00420
00421 typedef enum pgpSubType_e {
00422 PGPSUBTYPE_SIG_CREATE_TIME = 2,
00423 PGPSUBTYPE_SIG_EXPIRE_TIME = 3,
00424 PGPSUBTYPE_EXPORTABLE_CERT = 4,
00425 PGPSUBTYPE_TRUST_SIG = 5,
00426 PGPSUBTYPE_REGEX = 6,
00427 PGPSUBTYPE_REVOCABLE = 7,
00428 PGPSUBTYPE_KEY_EXPIRE_TIME = 9,
00429 PGPSUBTYPE_BACKWARD_COMPAT = 10,
00430 PGPSUBTYPE_PREFER_SYMKEY = 11,
00431 PGPSUBTYPE_REVOKE_KEY = 12,
00432 PGPSUBTYPE_ISSUER_KEYID = 16,
00433 PGPSUBTYPE_NOTATION = 20,
00434 PGPSUBTYPE_PREFER_HASH = 21,
00435 PGPSUBTYPE_PREFER_COMPRESS = 22,
00436 PGPSUBTYPE_KEYSERVER_PREFERS= 23,
00437 PGPSUBTYPE_PREFER_KEYSERVER = 24,
00438 PGPSUBTYPE_PRIMARY_USERID = 25,
00439 PGPSUBTYPE_POLICY_URL = 26,
00440 PGPSUBTYPE_KEY_FLAGS = 27,
00441 PGPSUBTYPE_SIGNER_USERID = 28,
00442 PGPSUBTYPE_REVOKE_REASON = 29,
00443 PGPSUBTYPE_INTERNAL_100 = 100,
00444 PGPSUBTYPE_INTERNAL_101 = 101,
00445 PGPSUBTYPE_INTERNAL_102 = 102,
00446 PGPSUBTYPE_INTERNAL_103 = 103,
00447 PGPSUBTYPE_INTERNAL_104 = 104,
00448 PGPSUBTYPE_INTERNAL_105 = 105,
00449 PGPSUBTYPE_INTERNAL_106 = 106,
00450 PGPSUBTYPE_INTERNAL_107 = 107,
00451 PGPSUBTYPE_INTERNAL_108 = 108,
00452 PGPSUBTYPE_INTERNAL_109 = 109,
00453 PGPSUBTYPE_INTERNAL_110 = 110
00454 } pgpSubType;
00455
00456
00459
00460 extern struct pgpValTbl_s pgpSubTypeTbl[];
00461
00482 typedef union pgpPktSig_u {
00483 struct pgpPktSigV3_s v3;
00484 struct pgpPktSigV4_s v4;
00485 } * pgpPktSig;
00486
00515 typedef struct pgpPktSymkey_s {
00516 byte version;
00517 byte symkey_algo;
00518 byte s2k[1];
00519 } pgpPktSymkey;
00520
00550 typedef struct pgpPktOnepass_s {
00551 byte version;
00552 byte sigtype;
00553 byte hash_algo;
00554 byte pubkey_algo;
00555 byte signid[8];
00556 byte nested;
00557 } * pgpPktOnepass;
00558
00631 typedef struct pgpPktKeyV3_s {
00632 byte version;
00633 byte time[4];
00634 byte valid[2];
00635 byte pubkey_algo;
00636 } * pgpPktKeyV3;
00637
00669 typedef struct pgpPktKeyV4_s {
00670 byte version;
00671 byte time[4];
00672 byte pubkey_algo;
00673 } * pgpPktKeyV4;
00674
00739 typedef union pgpPktKey_u {
00740 struct pgpPktKeyV3_s v3;
00741 struct pgpPktKeyV4_s v4;
00742 } pgpPktKey;
00743
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 typedef struct pgpPktCdata_s {
00769 byte compressalgo;
00770 byte data[1];
00771 } pgpPktCdata;
00772
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 typedef struct pgpPktEdata_s {
00808 byte data[1];
00809 } pgpPktEdata;
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
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858 typedef struct pgpPktLdata_s {
00859 byte format;
00860 byte filenamelen;
00861 byte filename[1];
00862 } pgpPktLdata;
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877 typedef struct pgpPktTrust_s {
00878 byte flag;
00879 } pgpPktTrust;
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891 typedef struct pgpPktUid_s {
00892 byte userid[1];
00893 } pgpPktUid;
00894
00897 union pgpPktPre_u {
00898 pgpPktPubkey pubkey;
00899 pgpPktSig sig;
00900 pgpPktSymkey symkey;
00901 pgpPktOnepass onepass;
00902 pgpPktKey key;
00903 pgpPktCdata cdata;
00904 pgpPktEdata edata;
00906 pgpPktLdata ldata;
00907 pgpPktTrust tdata;
00908 pgpPktUid uid;
00909 };
00910
00913
00914 typedef enum pgpArmor_e {
00915 PGPARMOR_ERROR = -1,
00916 PGPARMOR_NONE = 0,
00917 PGPARMOR_MESSAGE = 1,
00918 PGPARMOR_PUBKEY = 2,
00919 PGPARMOR_SIGNATURE = 3,
00920 PGPARMOR_SIGNED_MESSAGE = 4,
00921 PGPARMOR_FILE = 5,
00922 PGPARMOR_PRIVKEY = 6,
00923 PGPARMOR_SECKEY = 7
00924 } pgpArmor;
00925
00926
00929
00930 extern struct pgpValTbl_s pgpArmorTbl[];
00931
00934
00935 typedef enum pgpArmorKey_e {
00936 PGPARMORKEY_VERSION = 1,
00937 PGPARMORKEY_COMMENT = 2,
00938 PGPARMORKEY_MESSAGEID = 3,
00939 PGPARMORKEY_HASH = 4,
00940 PGPARMORKEY_CHARSET = 5
00941 } pgpArmorKey;
00942
00943
00946
00947 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00948
00951 struct pgpDigParams_s {
00952
00953 const char * userid;
00954
00955 const byte * hash;
00956 const char * params[4];
00957 byte tag;
00958
00959 byte version;
00960 byte time[4];
00961 byte pubkey_algo;
00963 byte hash_algo;
00964 byte sigtype;
00965 byte hashlen;
00966 byte signhash16[2];
00967 byte signid[8];
00968 byte saved;
00969 #define PGPDIG_SAVED_TIME (1 << 0)
00970 #define PGPDIG_SAVED_ID (1 << 1)
00971
00972 };
00973
00976 struct pgpDig_s {
00977 struct pgpDigParams_s signature;
00978 struct pgpDigParams_s pubkey;
00979
00980 size_t nbytes;
00982
00983 DIGEST_CTX sha1ctx;
00984
00985 DIGEST_CTX hdrsha1ctx;
00986
00987 void * sha1;
00988 size_t sha1len;
00990
00991 DIGEST_CTX md5ctx;
00992
00993 void * md5;
00994 size_t md5len;
00996
00997 mp32barrett p;
00998 mp32barrett q;
00999 mp32number g;
01000 mp32number y;
01001 mp32number hm;
01002 mp32number r;
01003 mp32number s;
01004
01005
01006 rsapk rsa_pk;
01007 mp32number m;
01008 mp32number c;
01009 mp32number rsahm;
01010 };
01011
01012
01013
01014 #ifdef __cplusplus
01015 extern "C" {
01016 #endif
01017
01020 static inline
01021 unsigned int pgpGrab(const byte *s, int nbytes)
01022
01023 {
01024 unsigned int i = 0;
01025 int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
01026 while (nb--)
01027 i = (i << 8) | *s++;
01028 return i;
01029 }
01030
01033 static inline
01034 int pgpLen(const byte *s, unsigned int *lenp)
01035
01036 {
01037 if (*s < 192) {
01038 (*lenp) = *s++;
01039 return 1;
01040 } else if (*s < 255) {
01041 (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01042 return 2;
01043 } else {
01044 (*lenp) = pgpGrab(s+1, 4);
01045 return 5;
01046 }
01047 }
01048
01051 static inline
01052 unsigned int pgpMpiBits(const byte *p)
01053
01054 {
01055 return ((p[0] << 8) | p[1]);
01056 }
01057
01060 static inline
01061 unsigned int pgpMpiLen(const byte *p)
01062
01063 {
01064 return (2 + ((pgpMpiBits(p)+7)>>3));
01065 }
01066
01069 static inline
01070 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01071
01072 {
01073 static char hex[] = "0123456789abcdef";
01074 while (nbytes-- > 0) {
01075 unsigned int i;
01076 i = *s++;
01077 *t++ = hex[ (i >> 4) & 0xf ];
01078 *t++ = hex[ (i ) & 0xf ];
01079 }
01080 *t = '\0';
01081 return t;
01082 }
01083
01086 static inline
01087 char * pgpHexStr(const byte *p, unsigned int plen)
01088
01089 {
01090 static char prbuf[2048];
01091 char *t = prbuf;
01092 t = pgpHexCvt(t, p, plen);
01093 return prbuf;
01094 }
01095
01098 static inline
01099 const char * pgpMpiStr(const byte *p)
01100
01101 {
01102 static char prbuf[2048];
01103 char *t = prbuf;
01104 sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01105 t += strlen(t);
01106 t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01107 return prbuf;
01108 }
01109
01112 static inline
01113 const char * pgpValStr(pgpValTbl vs, byte val)
01114
01115 {
01116 do {
01117 if (vs->val == val)
01118 break;
01119 } while ((++vs)->val != -1);
01120 return vs->str;
01121 }
01122
01125 static inline
01126 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01127
01128 {
01129 do {
01130 int vlen = strlen(vs->str);
01131 if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01132 break;
01133 } while ((++vs)->val != -1);
01134 return vs->val;
01135 }
01136
01137
01140 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01141
01142 ;
01143
01146 int pgpPrtSubType(const byte *h, unsigned int hlen)
01147
01148 ;
01149
01152 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01153
01154 ;
01155
01158 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01159
01160 ;
01161
01164 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01165
01166 ;
01167
01170 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01171
01172 ;
01173
01176 int pgpPrtPkt(const byte *pkt)
01177
01178 ;
01179
01180
01183 int pgpPrtPkts(const byte *pkts, unsigned int plen, struct pgpDig_s *dig, int printing)
01184
01185 ;
01186
01189 pgpArmor pgpReadPkts(const char * fn,
01190 const byte ** pkt, size_t * pktlen)
01191
01192 ;
01193
01196
01197 struct pgpDig_s * pgpNewDig(void)
01198 ;
01199
01202 void pgpCleanDig( struct pgpDig_s * dig)
01203 ;
01204
01207
01208 struct pgpDig_s * pgpFreeDig( struct pgpDig_s * dig)
01209 ;
01210
01213 static inline
01214 int pgpIsPkt(const byte * p)
01215
01216 {
01217 unsigned int val = *p++;
01218 pgpTag tag;
01219 int rc;
01220
01221
01222 if (!(val & 0x80))
01223 return 0;
01224
01225 if (val & 0x40)
01226 tag = (val & 0x3f);
01227 else
01228 tag = (val >> 2) & 0xf;
01229
01230 switch (tag) {
01231 case PGPTAG_MARKER:
01232 case PGPTAG_SYMMETRIC_SESSION_KEY:
01233 case PGPTAG_ONEPASS_SIGNATURE:
01234 case PGPTAG_PUBLIC_KEY:
01235 case PGPTAG_SECRET_KEY:
01236 case PGPTAG_PUBLIC_SESSION_KEY:
01237 case PGPTAG_SIGNATURE:
01238 case PGPTAG_COMMENT:
01239 case PGPTAG_COMMENT_OLD:
01240 case PGPTAG_LITERAL_DATA:
01241 case PGPTAG_COMPRESSED_DATA:
01242 case PGPTAG_SYMMETRIC_DATA:
01243 rc = 1;
01244 break;
01245 case PGPTAG_PUBLIC_SUBKEY:
01246 case PGPTAG_SECRET_SUBKEY:
01247 case PGPTAG_USER_ID:
01248 case PGPTAG_RESERVED:
01249 case PGPTAG_TRUST:
01250 case PGPTAG_PHOTOID:
01251 case PGPTAG_ENCRYPTED_MDC:
01252 case PGPTAG_MDC:
01253 case PGPTAG_PRIVATE_60:
01254 case PGPTAG_PRIVATE_62:
01255 case PGPTAG_CONTROL:
01256 default:
01257 rc = 0;
01258 break;
01259 }
01260
01261 return rc;
01262 }
01263
01264 #define CRC24_INIT 0xb704ce
01265 #define CRC24_POLY 0x1864cfb
01266
01269 static inline
01270 uint32 pgpCRC(const byte *octets, size_t len)
01271
01272 {
01273 uint32 crc = CRC24_INIT;
01274 int i;
01275
01276 while (len--) {
01277 crc ^= (*octets++) << 16;
01278 for (i = 0; i < 8; i++) {
01279 crc <<= 1;
01280 if (crc & 0x1000000)
01281 crc ^= CRC24_POLY;
01282 }
01283 }
01284 return crc & 0xffffff;
01285 }
01286
01287 #ifdef __cplusplus
01288 }
01289 #endif
01290
01291
01292 #endif