Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

asn.h

00001 #ifndef CRYPTOPP_ASN_H
00002 #define CRYPTOPP_ASN_H
00003 
00004 #include "filters.h"
00005 #include "queue.h"
00006 #include <vector>
00007 
00008 NAMESPACE_BEGIN(CryptoPP)
00009 
00010 // these tags and flags are not complete
00011 enum ASNTag
00012 {
00013         BOOLEAN                         = 0x01,
00014         INTEGER                         = 0x02,
00015         BIT_STRING                      = 0x03,
00016         OCTET_STRING            = 0x04,
00017         TAG_NULL                        = 0x05,
00018         OBJECT_IDENTIFIER       = 0x06,
00019         OBJECT_DESCRIPTOR       = 0x07,
00020         EXTERNAL                        = 0x08,
00021         REAL                            = 0x09,
00022         ENUMERATED                      = 0x0a,
00023         UTF8_STRING                     = 0x0c,
00024         SEQUENCE                        = 0x10,
00025         SET                             = 0x11,
00026         NUMERIC_STRING          = 0x12,
00027         PRINTABLE_STRING        = 0x13,
00028         T61_STRING                      = 0x14,
00029         VIDEOTEXT_STRING        = 0x15,
00030         IA5_STRING                      = 0x16,
00031         UTC_TIME                        = 0x17,
00032         GENERALIZED_TIME        = 0x18,
00033         GRAPHIC_STRING          = 0x19,
00034         VISIBLE_STRING          = 0x1a,
00035         GENERAL_STRING          = 0x1b
00036 };
00037 
00038 enum ASNIdFlag
00039 {
00040         UNIVERSAL                       = 0x00,
00041         DATA                            = 0x01,
00042         HEADER                          = 0x02,
00043         CONSTRUCTED             = 0x20,
00044         APPLICATION             = 0x40,
00045         CONTEXT_SPECIFIC        = 0x80,
00046         PRIVATE                         = 0xc0
00047 };
00048 
00049 #define BERDecodeError() throw BERDecodeErr()
00050 
00052 class BERDecodeErr : public Exception
00053 {
00054 public: 
00055         BERDecodeErr() : Exception("BER decode error") {}
00056         BERDecodeErr(const char *err) : Exception(err) {}
00057 };
00058 
00059 class UnknownOID : public BERDecodeErr
00060 {
00061 public:
00062         UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
00063         UnknownOID(const char *err) : BERDecodeErr(err) {}
00064 };
00065 
00066 // unsigned int DERLengthEncode(unsigned int length, byte *output=0);
00067 unsigned int DERLengthEncode(BufferedTransformation &out, unsigned int length);
00068 // returns false if indefinite length
00069 bool BERLengthDecode(BufferedTransformation &in, unsigned int &length);
00070 
00071 void DEREncodeNull(BufferedTransformation &out);
00072 void BERDecodeNull(BufferedTransformation &in);
00073 
00074 unsigned int DEREncodeOctetString(BufferedTransformation &out, const byte *str, unsigned int strLen);
00075 unsigned int DEREncodeOctetString(BufferedTransformation &out, const SecByteBlock &str);
00076 unsigned int BERDecodeOctetString(BufferedTransformation &in, SecByteBlock &str);
00077 unsigned int BERDecodeOctetString(BufferedTransformation &in, BufferedTransformation &str);
00078 
00079 // for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
00080 unsigned int DEREncodeTextString(BufferedTransformation &out, const std::string &str, byte asnTag);
00081 unsigned int BERDecodeTextString(BufferedTransformation &in, std::string &str, byte asnTag);
00082 
00083 unsigned int DEREncodeBitString(BufferedTransformation &out, const byte *str, unsigned int strLen, unsigned int unusedBits=0);
00084 unsigned int BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);
00085 
00087 class OID
00088 {
00089 public:
00090         OID() {}
00091         OID(unsigned long v) : m_values(1, v) {}
00092         OID(BufferedTransformation &bt) {BERDecode(bt);}
00093 
00094         bool operator==(const OID &rhs) const {return m_values == rhs.m_values;}
00095         bool operator!=(const OID &rhs) const {return !operator==(rhs);}
00096         bool operator<(const OID &rhs) const {return std::lexicographical_compare(m_values.begin(), m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}
00097 
00098         inline OID & operator+=(unsigned long rhs) {m_values.push_back(rhs); return *this;}
00099         inline OID operator+(unsigned long rhs) const {return OID(*this)+=rhs;}
00100 
00101         void DEREncode(BufferedTransformation &bt) const;
00102         void BERDecode(BufferedTransformation &bt);
00103 
00104         // throw BERDecodeErr() if decoded value doesn't equal this OID
00105         void BERDecodeAndCheck(BufferedTransformation &bt) const;
00106 
00107         std::vector<unsigned long> m_values;
00108 
00109 private:
00110         static void EncodeValue(BufferedTransformation &bt, unsigned long v);
00111         static unsigned int DecodeValue(BufferedTransformation &bt, unsigned long &v);
00112 };
00113 
00115 class BERGeneralDecoder : public Store
00116 {
00117 public:
00118         explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
00119         explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
00120         ~BERGeneralDecoder();
00121 
00122         bool IsDefiniteLength() const {return m_definiteLength;}
00123         unsigned int RemainingLength() const {assert(m_definiteLength); return m_length;}
00124         bool EndReached() const;
00125         byte PeekByte() const;
00126         void CheckByte(byte b);
00127 
00128         unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax);
00129         unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax) const;
00130 
00131         // call this to denote end of sequence
00132         void MessageEnd(int=-1);
00133 
00134 protected:
00135         BufferedTransformation &m_inQueue;
00136         bool m_finished, m_definiteLength;
00137         unsigned int m_length;
00138 
00139 private:
00140         unsigned int ReduceLength(unsigned int delta);
00141 };
00142 
00144 class DERGeneralEncoder : public ByteQueue
00145 {
00146 public:
00147         explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
00148         explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
00149         ~DERGeneralEncoder();
00150 
00151         // call this to denote end of sequence
00152         void MessageEnd(int=-1);
00153 
00154 private:
00155         BufferedTransformation &m_outQueue;
00156         bool m_finished;
00157 
00158         byte m_asnTag;
00159 };
00160 
00162 class BERSequenceDecoder : public BERGeneralDecoder
00163 {
00164 public:
00165         explicit BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
00166                 : BERGeneralDecoder(inQueue, asnTag) {}
00167         explicit BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
00168                 : BERGeneralDecoder(inQueue, asnTag) {}
00169 };
00170 
00172 class DERSequenceEncoder : public DERGeneralEncoder
00173 {
00174 public:
00175         explicit DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
00176                 : DERGeneralEncoder(outQueue, asnTag) {}
00177         explicit DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
00178                 : DERGeneralEncoder(outQueue, asnTag) {}
00179 };
00180 
00182 class BERSetDecoder : public BERGeneralDecoder
00183 {
00184 public:
00185         explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag = SET | CONSTRUCTED)
00186                 : BERGeneralDecoder(inQueue, asnTag) {}
00187         explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag = SET | CONSTRUCTED)
00188                 : BERGeneralDecoder(inQueue, asnTag) {}
00189 };
00190 
00192 class DERSetEncoder : public DERGeneralEncoder
00193 {
00194 public:
00195         explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag = SET | CONSTRUCTED)
00196                 : DERGeneralEncoder(outQueue, asnTag) {}
00197         explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag = SET | CONSTRUCTED)
00198                 : DERGeneralEncoder(outQueue, asnTag) {}
00199 };
00200 
00201 // ********************************************************
00202 
00204 
00205 template <class T>
00206 unsigned int DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag = INTEGER)
00207 {
00208         byte buf[sizeof(w)+1];
00209         unsigned int bc;
00210         if (asnTag == BOOLEAN)
00211         {
00212                 buf[sizeof(w)] = w ? 0xff : 0;
00213                 bc = 1;
00214         }
00215         else
00216         {
00217                 buf[0] = 0;
00218                 for (unsigned int i=0; i<sizeof(w); i++)
00219                         buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
00220                 bc = sizeof(w);
00221                 while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
00222                         --bc;
00223                 if (buf[sizeof(w)+1-bc] & 0x80)
00224                         ++bc;
00225         }
00226         out.Put(asnTag);
00227         unsigned int lengthBytes = DERLengthEncode(out, bc);
00228         out.Put(buf+sizeof(w)+1-bc, bc);
00229         return 1+lengthBytes+bc;
00230 }
00231 
00233 // VC60 workaround: std::numeric_limits<T>::max conflicts with MFC max macro
00234 // CW41 workaround: std::numeric_limits<T>::max causes a template error
00235 template <class T>
00236 void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag = INTEGER,
00237                                            T minValue = 0, T maxValue = 0xffffffff)
00238 {
00239         byte b;
00240         if (!in.Get(b) || b != asnTag)
00241                 BERDecodeError();
00242 
00243         unsigned int bc;
00244         BERLengthDecode(in, bc);
00245 
00246         SecByteBlock buf(bc);
00247 
00248         if (bc != in.Get(buf, bc))
00249                 BERDecodeError();
00250 
00251         const byte *ptr = buf;
00252         while (bc > sizeof(w) && *ptr == 0)
00253         {
00254                 bc--;
00255                 ptr++;
00256         }
00257         if (bc > sizeof(w))
00258                 BERDecodeError();
00259 
00260         w = 0;
00261         for (unsigned int i=0; i<bc; i++)
00262                 w = (w << 8) | ptr[i];
00263 
00264         if (w < minValue || w > maxValue)
00265                 BERDecodeError();
00266 }
00267 
00268 NAMESPACE_END
00269 
00270 #endif

Generated at Mon Jan 15 01:16:29 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000