00001 
00002 
00003 
00004 
00005 
00006 #include <list>
00007 #include <vector>
00008 
00009 typedef unsigned int uint32;
00010 typedef int int32;
00011 typedef unsigned short uint16;
00012 typedef short int16;
00013 typedef unsigned char uint8;
00014 typedef signed char int8;
00015 
00016 struct SECKEYPrivateKeyStr;
00017 struct SECKEYPublicKeyStr;
00018 
00019 namespace AuthN {
00020 
00021   class PrivateKeyInfoCodec {
00022   public:
00023 
00024     
00025     static const uint8 kRsaAlgorithmIdentifier[];
00026 
00027     
00028     static const uint8 kBitStringTag = 0x03;
00029     static const uint8 kIntegerTag = 0x02;
00030     static const uint8 kNullTag = 0x05;
00031     static const uint8 kOctetStringTag = 0x04;
00032     static const uint8 kSequenceTag = 0x30;
00033 
00034     
00035     
00036     
00037     
00038     explicit PrivateKeyInfoCodec(bool big_endian);
00039 
00040     ~PrivateKeyInfoCodec();
00041 
00042     
00043     
00044     bool Export(std::vector<uint8>* output);
00045 
00046     
00047     
00048     bool ExportPublicKeyInfo(std::vector<uint8>* output);
00049 
00050     
00051     
00052     bool ExportPublicKey(std::vector<uint8>* output);
00053 
00054     
00055     
00056     
00057     
00058     bool Import(const std::vector<uint8>& input);
00059 
00060     
00061     std::vector<uint8>* modulus() { return &modulus_; };
00062     std::vector<uint8>* public_exponent() { return &public_exponent_; };
00063     std::vector<uint8>* private_exponent() { return &private_exponent_; };
00064     std::vector<uint8>* prime1() { return &prime1_; };
00065     std::vector<uint8>* prime2() { return &prime2_; };
00066     std::vector<uint8>* exponent1() { return &exponent1_; };
00067     std::vector<uint8>* exponent2() { return &exponent2_; };
00068     std::vector<uint8>* coefficient() { return &coefficient_; };
00069 
00070   private:
00071     
00072     
00073     void PrependInteger(const std::vector<uint8>& in, std::list<uint8>* out);
00074     void PrependInteger(uint8* val, int num_bytes, std::list<uint8>* data);
00075 
00076     
00077     
00078     void PrependIntegerImpl(uint8* val,
00079                           int num_bytes,
00080                           std::list<uint8>* data,
00081                           bool big_endian);
00082 
00083     
00084     
00085     bool ReadInteger(uint8** pos, uint8* end, std::vector<uint8>* out);
00086     bool ReadIntegerWithExpectedSize(uint8** pos,
00087                                    uint8* end,
00088                                    size_t expected_size,
00089                                    std::vector<uint8>* out);
00090 
00091     
00092     
00093     bool ReadIntegerImpl(uint8** pos,
00094                        uint8* end,
00095                        std::vector<uint8>* out,
00096                        bool big_endian);
00097 
00098     
00099     
00100     void PrependBytes(uint8* val,
00101                     int start,
00102                     int num_bytes,
00103                     std::list<uint8>* data);
00104 
00105     
00106     void PrependLength(size_t size, std::list<uint8>* data);
00107 
00108     
00109     void PrependTypeHeaderAndLength(uint8 type,
00110                                   uint32 length,
00111                                   std::list<uint8>* output);
00112 
00113     
00114     void PrependBitString(uint8* val, int num_bytes, std::list<uint8>* output);
00115 
00116     
00117     
00118     bool ReadLength(uint8** pos, uint8* end, uint32* result);
00119 
00120     
00121     bool ReadTypeHeaderAndLength(uint8** pos,
00122                                uint8* end,
00123                                uint8 expected_tag,
00124                                uint32* length);
00125 
00126     
00127     
00128     bool ReadSequence(uint8** pos, uint8* end);
00129 
00130     
00131     bool ReadAlgorithmIdentifier(uint8** pos, uint8* end);
00132 
00133     
00134     bool ReadVersion(uint8** pos, uint8* end);
00135 
00136     
00137     bool big_endian_;
00138 
00139     
00140     std::vector<uint8> modulus_;
00141     std::vector<uint8> public_exponent_;
00142     std::vector<uint8> private_exponent_;
00143     std::vector<uint8> prime1_;
00144     std::vector<uint8> prime2_;
00145     std::vector<uint8> exponent1_;
00146     std::vector<uint8> exponent2_;
00147     std::vector<uint8> coefficient_;
00148   };
00149 
00150 }