00001 #ifndef H_RPMPGP
00002 #define H_RPMPGP
00003 
00013 #include <string.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 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_NOENCRYPT     = 110   
00224 } pgpSymkeyAlgo;
00225 
00226 
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_BZIP2       =  3    
00254 } pgpCompressAlgo;
00255 
00256 
00260   
00261 extern struct pgpValTbl_s pgpCompressionTbl[];
00262 
00284 typedef enum pgpHashAlgo_e {
00285     PGPHASHALGO_MD5             =  1,   
00286     PGPHASHALGO_SHA1            =  2,   
00287     PGPHASHALGO_RIPEMD160       =  3,   
00288     PGPHASHALGO_MD2             =  5,   
00289     PGPHASHALGO_TIGER192        =  6,   
00290     PGPHASHALGO_HAVAL_5_160     =  7,   
00291     PGPHASHALGO_SHA256          =  8,   
00292     PGPHASHALGO_SHA384          =  9,   
00293     PGPHASHALGO_SHA512          = 10,   
00294 } pgpHashAlgo;
00295 
00299   
00300 extern struct pgpValTbl_s pgpHashTbl[];
00301 
00323 typedef struct pgpPktSigV3_s {
00324     byte version;       
00325     byte hashlen;       
00326     byte sigtype;       
00327     byte time[4];       
00328     byte signid[8];     
00329     byte pubkey_algo;   
00330     byte hash_algo;     
00331     byte signhash16[2]; 
00332 } * pgpPktSigV3;
00333 
00355 typedef struct pgpPktSigV4_s {
00356     byte version;       
00357     byte sigtype;       
00358     byte pubkey_algo;   
00359     byte hash_algo;     
00360     byte hashlen[2];    
00361 } * pgpPktSigV4;
00362 
00429 
00430 typedef enum pgpSubType_e {
00431     PGPSUBTYPE_NONE             =   0, 
00432     PGPSUBTYPE_SIG_CREATE_TIME  =   2, 
00433     PGPSUBTYPE_SIG_EXPIRE_TIME  =   3, 
00434     PGPSUBTYPE_EXPORTABLE_CERT  =   4, 
00435     PGPSUBTYPE_TRUST_SIG        =   5, 
00436     PGPSUBTYPE_REGEX            =   6, 
00437     PGPSUBTYPE_REVOCABLE        =   7, 
00438     PGPSUBTYPE_KEY_EXPIRE_TIME  =   9, 
00439     PGPSUBTYPE_ARR              =  10, 
00440     PGPSUBTYPE_PREFER_SYMKEY    =  11, 
00441     PGPSUBTYPE_REVOKE_KEY       =  12, 
00442     PGPSUBTYPE_ISSUER_KEYID     =  16, 
00443     PGPSUBTYPE_NOTATION         =  20, 
00444     PGPSUBTYPE_PREFER_HASH      =  21, 
00445     PGPSUBTYPE_PREFER_COMPRESS  =  22, 
00446     PGPSUBTYPE_KEYSERVER_PREFERS=  23, 
00447     PGPSUBTYPE_PREFER_KEYSERVER =  24, 
00448     PGPSUBTYPE_PRIMARY_USERID   =  25, 
00449     PGPSUBTYPE_POLICY_URL       =  26, 
00450     PGPSUBTYPE_KEY_FLAGS        =  27, 
00451     PGPSUBTYPE_SIGNER_USERID    =  28, 
00452     PGPSUBTYPE_REVOKE_REASON    =  29, 
00453     PGPSUBTYPE_FEATURES         =  30, 
00454     PGPSUBTYPE_EMBEDDED_SIG     =  32, 
00456     PGPSUBTYPE_INTERNAL_100     = 100, 
00457     PGPSUBTYPE_INTERNAL_101     = 101, 
00458     PGPSUBTYPE_INTERNAL_102     = 102, 
00459     PGPSUBTYPE_INTERNAL_103     = 103, 
00460     PGPSUBTYPE_INTERNAL_104     = 104, 
00461     PGPSUBTYPE_INTERNAL_105     = 105, 
00462     PGPSUBTYPE_INTERNAL_106     = 106, 
00463     PGPSUBTYPE_INTERNAL_107     = 107, 
00464     PGPSUBTYPE_INTERNAL_108     = 108, 
00465     PGPSUBTYPE_INTERNAL_109     = 109, 
00466     PGPSUBTYPE_INTERNAL_110     = 110, 
00468     PGPSUBTYPE_CRITICAL         = 128  
00469 } pgpSubType;
00470 
00471 
00475   
00476 extern struct pgpValTbl_s pgpSubTypeTbl[];
00477 
00498 typedef union pgpPktSig_u {
00499     struct pgpPktSigV3_s v3;
00500     struct pgpPktSigV4_s v4;
00501 } * pgpPktSig;
00502 
00531 typedef struct pgpPktSymkey_s {
00532     byte version;       
00533     byte symkey_algo;
00534     byte s2k[1];
00535 } pgpPktSymkey;
00536 
00566 typedef struct pgpPktOnepass_s {
00567     byte version;       
00568     byte sigtype;       
00569     byte hash_algo;     
00570     byte pubkey_algo;   
00571     byte signid[8];     
00572     byte nested;
00573 } * pgpPktOnepass;
00574 
00647 typedef struct pgpPktKeyV3_s {
00648     byte version;       
00649     byte time[4];       
00650     byte valid[2];      
00651     byte pubkey_algo;   
00652 } * pgpPktKeyV3;
00653 
00685 typedef struct pgpPktKeyV4_s {
00686     byte version;       
00687     byte time[4];       
00688     byte pubkey_algo;   
00689 } * pgpPktKeyV4;
00690 
00755 typedef union pgpPktKey_u {
00756     struct pgpPktKeyV3_s v3;
00757     struct pgpPktKeyV4_s v4;
00758 } pgpPktKey;
00759 
00760 
00761 
00762 
00763 
00764 
00765 
00766 
00767 
00768 
00769 
00770 
00771 
00772 
00773 
00774 
00775 
00776 
00777 
00778 
00779 
00780 
00781 
00782 
00783 
00784 typedef struct pgpPktCdata_s {
00785     byte compressalgo;
00786     byte data[1];
00787 } pgpPktCdata;
00788 
00789 
00790 
00791 
00792 
00793 
00794 
00795 
00796 
00797 
00798 
00799 
00800 
00801 
00802 
00803 
00804 
00805 
00806 
00807 
00808 
00809 
00810 
00811 
00812 
00813 
00814 
00815 
00816 
00817 
00818 
00819 
00820 
00821 
00822 
00823 typedef struct pgpPktEdata_s {
00824     byte data[1];
00825 } pgpPktEdata;
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 
00862 
00863 
00864 
00865 
00866 
00867 
00868 
00869 
00870 
00871 
00872 
00873 
00874 typedef struct pgpPktLdata_s {
00875     byte format;
00876     byte filenamelen;
00877     byte filename[1];
00878 } pgpPktLdata;
00879 
00880 
00881 
00882 
00883 
00884 
00885 
00886 
00887 
00888 
00889 
00890 
00891 
00892 
00893 typedef struct pgpPktTrust_s {
00894     byte flag;
00895 } pgpPktTrust;
00896 
00897 
00898 
00899 
00900 
00901 
00902 
00903 
00904 
00905 
00906 
00907 typedef struct pgpPktUid_s {
00908     byte userid[1];
00909 } pgpPktUid;
00910 
00913 union pgpPktPre_u {
00914     pgpPktPubkey pubkey;        
00915     pgpPktSig sig;              
00916     pgpPktSymkey symkey;        
00917     pgpPktOnepass onepass;      
00918     pgpPktKey key;              
00919     pgpPktCdata cdata;          
00920     pgpPktEdata edata;          
00922     pgpPktLdata ldata;          
00923     pgpPktTrust tdata;          
00924     pgpPktUid uid;              
00925 };
00926 
00929 
00930 typedef enum pgpArmor_e {
00931     PGPARMOR_ERR_CRC_CHECK              = -7,
00932     PGPARMOR_ERR_BODY_DECODE            = -6,
00933     PGPARMOR_ERR_CRC_DECODE             = -5,
00934     PGPARMOR_ERR_NO_END_PGP             = -4,
00935     PGPARMOR_ERR_UNKNOWN_PREAMBLE_TAG   = -3,
00936     PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE     = -2,
00937     PGPARMOR_ERR_NO_BEGIN_PGP           = -1,
00938 #define PGPARMOR_ERROR  PGPARMOR_ERR_NO_BEGIN_PGP
00939     PGPARMOR_NONE               =  0,
00940     PGPARMOR_MESSAGE            =  1, 
00941     PGPARMOR_PUBKEY             =  2, 
00942     PGPARMOR_SIGNATURE          =  3, 
00943     PGPARMOR_SIGNED_MESSAGE     =  4, 
00944     PGPARMOR_FILE               =  5, 
00945     PGPARMOR_PRIVKEY            =  6, 
00946     PGPARMOR_SECKEY             =  7  
00947 } pgpArmor;
00948 
00949 
00953   
00954 extern struct pgpValTbl_s pgpArmorTbl[];
00955 
00958 
00959 typedef enum pgpArmorKey_e {
00960     PGPARMORKEY_VERSION         = 1, 
00961     PGPARMORKEY_COMMENT         = 2, 
00962     PGPARMORKEY_MESSAGEID       = 3, 
00963     PGPARMORKEY_HASH            = 4, 
00964     PGPARMORKEY_CHARSET         = 5  
00965 } pgpArmorKey;
00966 
00967 
00971   
00972 extern struct pgpValTbl_s pgpArmorKeyTbl[];
00973 
00977 typedef enum rpmDigestFlags_e {
00978     RPMDIGEST_NONE      = 0
00979 } rpmDigestFlags;
00980 
00981 
00982 
00983 #ifdef __cplusplus
00984 extern "C" {
00985 #endif
00986 
00993  static inline
00994 unsigned int pgpGrab(const byte *s, int nbytes)
00995         
00996 {
00997     unsigned int i = 0;
00998     int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
00999 
01000     while (nb--)
01001         i = (i << 8) | *s++;
01002 
01003     return i;
01004 }
01005 
01012  static inline
01013 int pgpLen(const byte *s,  unsigned int *lenp)
01014         
01015 {
01016 
01017     if (*s < 192) {
01018         (*lenp) = *s++;
01019         return 1;
01020     } else if (*s < 255) {
01021         (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
01022         return 2;
01023     } else {
01024         (*lenp) = pgpGrab(s+1, 4);
01025         return 5;
01026     }
01027 
01028 }
01029 
01035  static inline
01036 unsigned int pgpMpiBits(const byte *p)
01037         
01038         
01039 {
01040     return ((p[0] << 8) | p[1]);
01041 }
01042 
01048  static inline
01049 unsigned int pgpMpiLen(const byte *p)
01050         
01051         
01052 {
01053     return (2 + ((pgpMpiBits(p)+7)>>3));
01054 }
01055         
01063  static inline
01064 char * pgpHexCvt( char *t, const byte *s, int nbytes)
01065         
01066 {
01067     static char hex[] = "0123456789abcdef";
01068 
01069     while (nbytes-- > 0) {
01070         unsigned int i;
01071         i = *s++;
01072         *t++ = hex[ (i >> 4) & 0xf ];
01073         *t++ = hex[ (i     ) & 0xf ];
01074     }
01075     *t = '\0';
01076 
01077     return t;
01078 }
01079 
01087  static inline 
01088 char * pgpHexStr(const byte *p, unsigned int plen)
01089         
01090 {
01091     static char prbuf[8*BUFSIZ];        
01092     char *t = prbuf;
01093     t = pgpHexCvt(t, p, plen);
01094     return prbuf;
01095 }
01096 
01103  static inline 
01104 const char * pgpMpiStr(const byte *p)
01105         
01106         
01107 {
01108     static char prbuf[8*BUFSIZ];        
01109     char *t = prbuf;
01110     sprintf(t, "[%4u]: ", pgpGrab(p, 2));
01111     t += strlen(t);
01112     t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
01113     return prbuf;
01114 }
01115 
01122  static inline 
01123 const char * pgpValStr(pgpValTbl vs, byte val)
01124         
01125 {
01126     do {
01127         if (vs->val == val)
01128             break;
01129     } while ((++vs)->val != -1);
01130     return vs->str;
01131 }
01132 
01140  static inline
01141 int pgpValTok(pgpValTbl vs, const char * s, const char * se)
01142         
01143 {
01144     do {
01145         int vlen = strlen(vs->str);
01146         if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
01147             break;
01148     } while ((++vs)->val != -1);
01149     return vs->val;
01150 }
01151 
01152 
01159 void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
01160         
01161         ;
01162 
01170 int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
01171         
01172         ;
01173 
01181 int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
01182         
01183         ;
01184 
01192 int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
01193         
01194         ;
01195 
01203 int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
01204         
01205         ;
01206 
01214 int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
01215         
01216         ;
01217 
01226 int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
01227                  byte * keyid)
01228         ;
01229 
01236 int pgpPrtPkt(const byte *pkt, unsigned int pleft)
01237         
01238         ;
01239 
01240 
01249 int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
01250         
01251         ;
01252 
01260 pgpArmor pgpReadPkts(const char * fn,
01261                  const byte ** pkt,  size_t * pktlen)
01262         
01263         ;
01264 
01272 char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
01273         ;
01274 
01279 
01280 pgpDig pgpNewDig(void)
01281         ;
01282 
01287 void pgpCleanDig( pgpDig dig)
01288         ;
01289 
01295  
01296 pgpDig pgpFreeDig(  pgpDig dig)
01297         ;
01298 
01304  static inline
01305 int pgpIsPkt(const byte * p)
01306         
01307 {
01308 
01309     unsigned int val = *p++;
01310 
01311     pgpTag tag;
01312     int rc;
01313 
01314     
01315     if (!(val & 0x80))
01316         return 0;
01317 
01318     if (val & 0x40)
01319         tag = (pgpTag)(val & 0x3f);
01320     else
01321         tag = (pgpTag)((val >> 2) & 0xf);
01322 
01323     switch (tag) {
01324     case PGPTAG_MARKER:
01325     case PGPTAG_SYMMETRIC_SESSION_KEY:
01326     case PGPTAG_ONEPASS_SIGNATURE:
01327     case PGPTAG_PUBLIC_KEY:
01328     case PGPTAG_SECRET_KEY:
01329     case PGPTAG_PUBLIC_SESSION_KEY:
01330     case PGPTAG_SIGNATURE:
01331     case PGPTAG_COMMENT:
01332     case PGPTAG_COMMENT_OLD:
01333     case PGPTAG_LITERAL_DATA:
01334     case PGPTAG_COMPRESSED_DATA:
01335     case PGPTAG_SYMMETRIC_DATA:
01336         rc = 1;
01337         break;
01338     case PGPTAG_PUBLIC_SUBKEY:
01339     case PGPTAG_SECRET_SUBKEY:
01340     case PGPTAG_USER_ID:
01341     case PGPTAG_RESERVED:
01342     case PGPTAG_TRUST:
01343     case PGPTAG_PHOTOID:
01344     case PGPTAG_ENCRYPTED_MDC:
01345     case PGPTAG_MDC:
01346     case PGPTAG_PRIVATE_60:
01347     case PGPTAG_PRIVATE_62:
01348     case PGPTAG_CONTROL:
01349     default:
01350         rc = 0;
01351         break;
01352     }
01353 
01354     return rc;
01355 }
01356 
01357 #define CRC24_INIT      0xb704ce
01358 #define CRC24_POLY      0x1864cfb
01359 
01366  static inline
01367 unsigned int pgpCRC(const byte *octets, size_t len)
01368         
01369 {
01370     unsigned int crc = CRC24_INIT;
01371     int i;
01372 
01373     while (len--) {
01374 
01375         crc ^= (*octets++) << 16;
01376 
01377         for (i = 0; i < 8; i++) {
01378             crc <<= 1;
01379             if (crc & 0x1000000)
01380                 crc ^= CRC24_POLY;
01381         }
01382     }
01383     return crc & 0xffffff;
01384 }
01385 
01391 
01392 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
01393         ;
01394 
01402  
01403 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
01404         ;
01405 
01413 int rpmDigestUpdate( DIGEST_CTX ctx, const void * data, size_t len)
01414         ;
01415 
01427 int rpmDigestFinal(  DIGEST_CTX ctx,
01428           void ** datap,
01429           size_t * lenp, int asAscii)
01430                 ;
01431 
01432 #ifdef __cplusplus
01433 }
01434 #endif
01435 
01436 
01437 #endif