8
NAMESPACE_BEGIN(CryptoPP)
10
// these tags and flags are not complete
18
OBJECT_IDENTIFIER = 0x06,
19
OBJECT_DESCRIPTOR = 0x07,
26
NUMERIC_STRING = 0x12,
27
PRINTABLE_STRING = 0x13,
29
VIDEOTEXT_STRING = 0x15,
32
GENERALIZED_TIME = 0x18,
33
GRAPHIC_STRING = 0x19,
34
VISIBLE_STRING = 0x1a,
45
CONTEXT_SPECIFIC = 0x80,
49
inline void BERDecodeError() {throw BERDecodeErr();}
51
class CRYPTOPP_DLL UnknownOID : public BERDecodeErr
54
UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
55
UnknownOID(const char *err) : BERDecodeErr(err) {}
58
// unsigned int DERLengthEncode(unsigned int length, byte *output=0);
59
CRYPTOPP_DLL size_t CRYPTOPP_API DERLengthEncode(BufferedTransformation &out, lword length);
60
// returns false if indefinite length
61
CRYPTOPP_DLL bool CRYPTOPP_API BERLengthDecode(BufferedTransformation &in, size_t &length);
63
CRYPTOPP_DLL void CRYPTOPP_API DEREncodeNull(BufferedTransformation &out);
64
CRYPTOPP_DLL void CRYPTOPP_API BERDecodeNull(BufferedTransformation &in);
66
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &out, const byte *str, size_t strLen);
67
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &out, const SecByteBlock &str);
68
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &in, SecByteBlock &str);
69
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &in, BufferedTransformation &str);
71
// for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
72
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &out, const std::string &str, byte asnTag);
73
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &in, std::string &str, byte asnTag);
75
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeBitString(BufferedTransformation &out, const byte *str, size_t strLen, unsigned int unusedBits=0);
76
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);
78
// BER decode from source and DER reencode into dest
79
CRYPTOPP_DLL void CRYPTOPP_API DERReencode(BufferedTransformation &source, BufferedTransformation &dest);
82
class CRYPTOPP_DLL OID
86
OID(word32 v) : m_values(1, v) {}
87
OID(BufferedTransformation &bt) {BERDecode(bt);}
89
inline OID & operator+=(word32 rhs) {m_values.push_back(rhs); return *this;}
91
void DEREncode(BufferedTransformation &bt) const;
92
void BERDecode(BufferedTransformation &bt);
94
// throw BERDecodeErr() if decoded value doesn't equal this OID
95
void BERDecodeAndCheck(BufferedTransformation &bt) const;
97
std::vector<word32> m_values;
100
static void EncodeValue(BufferedTransformation &bt, word32 v);
101
static size_t DecodeValue(BufferedTransformation &bt, word32 &v);
104
class EncodedObjectFilter : public Filter
107
enum Flag {PUT_OBJECTS=1, PUT_MESSANGE_END_AFTER_EACH_OBJECT=2, PUT_MESSANGE_END_AFTER_ALL_OBJECTS=4, PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS=8};
108
EncodedObjectFilter(BufferedTransformation *attachment = NULL, unsigned int nObjects = 1, word32 flags = 0);
110
void Put(const byte *inString, size_t length);
112
unsigned int GetNumberOfCompletedObjects() const {return m_nCurrentObject;}
113
unsigned long GetPositionOfObject(unsigned int i) const {return m_positions[i];}
116
BufferedTransformation & CurrentTarget();
119
unsigned int m_nObjects, m_nCurrentObject, m_level;
120
std::vector<unsigned int> m_positions;
122
enum State {IDENTIFIER, LENGTH, BODY, TAIL, ALL_DONE} m_state;
124
lword m_lengthRemaining;
127
//! BER General Decoder
128
class CRYPTOPP_DLL BERGeneralDecoder : public Store
131
explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
132
explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
133
~BERGeneralDecoder();
135
bool IsDefiniteLength() const {return m_definiteLength;}
136
lword RemainingLength() const {assert(m_definiteLength); return m_length;}
137
bool EndReached() const;
138
byte PeekByte() const;
139
void CheckByte(byte b);
141
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
142
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
144
// call this to denote end of sequence
148
BufferedTransformation &m_inQueue;
149
bool m_finished, m_definiteLength;
153
void Init(byte asnTag);
154
void StoreInitialize(const NameValuePairs ¶meters) {assert(false);}
155
lword ReduceLength(lword delta);
158
//! DER General Encoder
159
class CRYPTOPP_DLL DERGeneralEncoder : public ByteQueue
162
explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
163
explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
164
~DERGeneralEncoder();
166
// call this to denote end of sequence
170
BufferedTransformation &m_outQueue;
176
//! BER Sequence Decoder
177
class CRYPTOPP_DLL BERSequenceDecoder : public BERGeneralDecoder
180
explicit BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
181
: BERGeneralDecoder(inQueue, asnTag) {}
182
explicit BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
183
: BERGeneralDecoder(inQueue, asnTag) {}
186
//! DER Sequence Encoder
187
class CRYPTOPP_DLL DERSequenceEncoder : public DERGeneralEncoder
190
explicit DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
191
: DERGeneralEncoder(outQueue, asnTag) {}
192
explicit DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
193
: DERGeneralEncoder(outQueue, asnTag) {}
197
class CRYPTOPP_DLL BERSetDecoder : public BERGeneralDecoder
200
explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag = SET | CONSTRUCTED)
201
: BERGeneralDecoder(inQueue, asnTag) {}
202
explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag = SET | CONSTRUCTED)
203
: BERGeneralDecoder(inQueue, asnTag) {}
207
class CRYPTOPP_DLL DERSetEncoder : public DERGeneralEncoder
210
explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag = SET | CONSTRUCTED)
211
: DERGeneralEncoder(outQueue, asnTag) {}
212
explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag = SET | CONSTRUCTED)
213
: DERGeneralEncoder(outQueue, asnTag) {}
217
class ASNOptional : public member_ptr<T>
220
void BERDecode(BERSequenceDecoder &seqDecoder, byte tag, byte mask = ~CONSTRUCTED)
223
if (seqDecoder.Peek(b) && (b & mask) == tag)
224
reset(new T(seqDecoder));
226
void DEREncode(BufferedTransformation &out)
228
if (this->get() != NULL)
229
this->get()->DEREncode(out);
234
template <class BASE>
235
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ASN1CryptoMaterial : public ASN1Object, public BASE
238
void Save(BufferedTransformation &bt) const
240
void Load(BufferedTransformation &bt)
244
//! encodes/decodes subjectPublicKeyInfo
245
class CRYPTOPP_DLL X509PublicKey : public ASN1CryptoMaterial<PublicKey>
248
void BERDecode(BufferedTransformation &bt);
249
void DEREncode(BufferedTransformation &bt) const;
251
virtual OID GetAlgorithmID() const =0;
252
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
253
{BERDecodeNull(bt); return false;}
254
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
255
{DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
257
//! decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
258
virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
259
//! encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
260
virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0;
263
//! encodes/decodes privateKeyInfo
264
class CRYPTOPP_DLL PKCS8PrivateKey : public ASN1CryptoMaterial<PrivateKey>
267
void BERDecode(BufferedTransformation &bt);
268
void DEREncode(BufferedTransformation &bt) const;
270
virtual OID GetAlgorithmID() const =0;
271
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
272
{BERDecodeNull(bt); return false;}
273
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
274
{DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
276
//! decode privateKey part of privateKeyInfo, without the OCTET STRING header
277
virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
278
//! encode privateKey part of privateKeyInfo, without the OCTET STRING header
279
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0;
281
//! decode optional attributes including context-specific tag
282
/*! /note default implementation stores attributes to be output in DEREncodeOptionalAttributes */
283
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt);
284
//! encode optional attributes including context-specific tag
285
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const;
288
ByteQueue m_optionalAttributes;
291
// ********************************************************
293
//! DER Encode Unsigned
294
/*! for INTEGER, BOOLEAN, and ENUM */
296
size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag = INTEGER)
298
byte buf[sizeof(w)+1];
300
if (asnTag == BOOLEAN)
302
buf[sizeof(w)] = w ? 0xff : 0;
308
for (unsigned int i=0; i<sizeof(w); i++)
309
buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
311
while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
313
if (buf[sizeof(w)+1-bc] & 0x80)
317
size_t lengthBytes = DERLengthEncode(out, bc);
318
out.Put(buf+sizeof(w)+1-bc, bc);
319
return 1+lengthBytes+bc;
322
//! BER Decode Unsigned
323
// VC60 workaround: std::numeric_limits<T>::max conflicts with MFC max macro
324
// CW41 workaround: std::numeric_limits<T>::max causes a template error
326
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag = INTEGER,
327
T minValue = 0, T maxValue = 0xffffffff)
330
if (!in.Get(b) || b != asnTag)
334
BERLengthDecode(in, bc);
336
SecByteBlock buf(bc);
338
if (bc != in.Get(buf, bc))
341
const byte *ptr = buf;
342
while (bc > sizeof(w) && *ptr == 0)
351
for (unsigned int i=0; i<bc; i++)
352
w = (w << 8) | ptr[i];
354
if (w < minValue || w > maxValue)
358
inline bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
359
{return lhs.m_values == rhs.m_values;}
360
inline bool operator!=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
361
{return lhs.m_values != rhs.m_values;}
362
inline bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
363
{return std::lexicographical_compare(lhs.m_values.begin(), lhs.m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}
364
inline ::CryptoPP::OID operator+(const ::CryptoPP::OID &lhs, unsigned long rhs)
365
{return ::CryptoPP::OID(lhs)+=rhs;}