1 // asn.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file asn.h
4 /// \brief Classes and functions for working with ANS.1 objects
5 
6 #ifndef CRYPTOPP_ASN_H
7 #define CRYPTOPP_ASN_H
8 
9 #include "cryptlib.h"
10 #include "filters.h"
11 #include "smartptr.h"
12 #include "stdcpp.h"
13 #include "queue.h"
14 #include "misc.h"
15 
16 #include <iosfwd>
17 
18 // Issue 340
19 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
20 # pragma GCC diagnostic push
21 # pragma GCC diagnostic ignored "-Wconversion"
22 # pragma GCC diagnostic ignored "-Wsign-conversion"
23 #endif
24 
25 NAMESPACE_BEGIN(CryptoPP)
26 
27 /// \brief ASN.1 types
28 /// \note These tags are not complete
29 enum ASNTag
30 {
31 	/// \brief ASN.1 Boolean
32 	BOOLEAN 			= 0x01,
33 	/// \brief ASN.1 Integer
34 	INTEGER 			= 0x02,
35 	/// \brief ASN.1 Bit string
36 	BIT_STRING			= 0x03,
37 	/// \brief ASN.1 Octet string
38 	OCTET_STRING		= 0x04,
39 	/// \brief ASN.1 Null
40 	TAG_NULL			= 0x05,
41 	/// \brief ASN.1 Object identifier
42 	OBJECT_IDENTIFIER	= 0x06,
43 	/// \brief ASN.1 Object descriptor
44 	OBJECT_DESCRIPTOR	= 0x07,
45 	/// \brief ASN.1 External reference
46 	EXTERNAL			= 0x08,
47 	/// \brief ASN.1 Real integer
48 	REAL				= 0x09,
49 	/// \brief ASN.1 Enumerated value
50 	ENUMERATED			= 0x0a,
51 	/// \brief ASN.1 UTF-8 string
52 	UTF8_STRING			= 0x0c,
53 	/// \brief ASN.1 Sequence
54 	SEQUENCE			= 0x10,
55 	/// \brief ASN.1 Set
56 	SET 				= 0x11,
57 	/// \brief ASN.1 Numeric string
58 	NUMERIC_STRING		= 0x12,
59 	/// \brief ASN.1 Printable string
60 	PRINTABLE_STRING 	= 0x13,
61 	/// \brief ASN.1 T61 string
62 	T61_STRING			= 0x14,
63 	/// \brief ASN.1 Videotext string
64 	VIDEOTEXT_STRING 	= 0x15,
65 	/// \brief ASN.1 IA5 string
66 	IA5_STRING			= 0x16,
67 	/// \brief ASN.1 UTC time
68 	UTC_TIME 			= 0x17,
69 	/// \brief ASN.1 Generalized time
70 	GENERALIZED_TIME 	= 0x18,
71 	/// \brief ASN.1 Graphic string
72 	GRAPHIC_STRING		= 0x19,
73 	/// \brief ASN.1 Visible string
74 	VISIBLE_STRING		= 0x1a,
75 	/// \brief ASN.1 General string
76 	GENERAL_STRING		= 0x1b,
77 	/// \brief ASN.1 Universal string
78 	UNIVERSAL_STRING	= 0x1c,
79 	/// \brief ASN.1 BMP string
80 	BMP_STRING  		= 0x1e
81 };
82 
83 /// \brief ASN.1 flags
84 /// \note These flags are not complete
85 enum ASNIdFlag
86 {
87 	/// \brief ASN.1 Universal class
88 	UNIVERSAL           = 0x00,
89 	// DATA           = 0x01,
90 	// HEADER           = 0x02,
91 	/// \brief ASN.1 Primitive flag
92 	PRIMITIVE           = 0x00,
93 	/// \brief ASN.1 Constructed flag
94 	CONSTRUCTED         = 0x20,
95 	/// \brief ASN.1 Application class
96 	APPLICATION         = 0x40,
97 	/// \brief ASN.1 Context specific class
98 	CONTEXT_SPECIFIC    = 0x80,
99 	/// \brief ASN.1 Private class
100 	PRIVATE             = 0xc0
101 };
102 
103 /// \brief Raises a BERDecodeErr
BERDecodeError()104 inline void BERDecodeError() {throw BERDecodeErr();}
105 
106 /// \brief Exception thrown when an unknown object identifier is encountered
107 class CRYPTOPP_DLL UnknownOID : public BERDecodeErr
108 {
109 public:
110 	/// \brief Construct an UnknownOID
UnknownOID()111 	UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
112 	/// \brief Construct an UnknownOID
113 	/// \param err error message to use for the execption
UnknownOID(const char * err)114 	UnknownOID(const char *err) : BERDecodeErr(err) {}
115 };
116 
117 /// \brief DER encode a length
118 /// \param bt BufferedTransformation object for writing
119 /// \param length the size to encode
120 /// \return the number of octets used for the encoding
121 CRYPTOPP_DLL size_t CRYPTOPP_API DERLengthEncode(BufferedTransformation &bt, lword length);
122 
123 /// \brief BER decode a length
124 /// \param bt BufferedTransformation object for reading
125 /// \param length the decoded size
126 /// \return true if the value was decoded
127 /// \throw BERDecodeError if the value fails to decode or is too large for size_t
128 /// \details BERLengthDecode() returns false if the encoding is indefinite length.
129 CRYPTOPP_DLL bool CRYPTOPP_API BERLengthDecode(BufferedTransformation &bt, size_t &length);
130 
131 /// \brief DER encode NULL
132 /// \param bt BufferedTransformation object for writing
133 CRYPTOPP_DLL void CRYPTOPP_API DEREncodeNull(BufferedTransformation &bt);
134 
135 /// \brief BER decode NULL
136 /// \param bt BufferedTransformation object for reading
137 CRYPTOPP_DLL void CRYPTOPP_API BERDecodeNull(BufferedTransformation &bt);
138 
139 /// \brief DER encode octet string
140 /// \param bt BufferedTransformation object for writing
141 /// \param str the string to encode
142 /// \param strLen the length of the string
143 /// \return the number of octets used for the encoding
144 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen);
145 
146 /// \brief DER encode octet string
147 /// \param bt BufferedTransformation object for reading
148 /// \param str the string to encode
149 /// \return the number of octets used for the encoding
150 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &bt, const SecByteBlock &str);
151 
152 /// \brief BER decode octet string
153 /// \param bt BufferedTransformation object for reading
154 /// \param str the decoded string
155 /// \return the number of octets used for the encoding
156 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str);
157 
158 /// \brief BER decode octet string
159 /// \param bt BufferedTransformation object for reading
160 /// \param str the decoded string
161 /// \return the number of octets used for the encoding
162 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &bt, BufferedTransformation &str);
163 
164 /// \brief DER encode text string
165 /// \param bt BufferedTransformation object for writing
166 /// \param str the string to encode
167 /// \param strLen the length of the string, in bytes
168 /// \param asnTag the ASN.1 identifier
169 /// \return the number of octets used for the encoding
170 /// \details DEREncodeTextString() can be used for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
171 /// \since Crypto++ 8.3
172 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &bt, const byte* str, size_t strLen, byte asnTag);
173 
174 /// \brief DER encode text string
175 /// \param bt BufferedTransformation object for writing
176 /// \param str the string to encode
177 /// \param asnTag the ASN.1 identifier
178 /// \return the number of octets used for the encoding
179 /// \details DEREncodeTextString() can be used for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
180 /// \since Crypto++ 8.3
181 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &bt, const SecByteBlock &str, byte asnTag);
182 
183 /// \brief DER encode text string
184 /// \param bt BufferedTransformation object for writing
185 /// \param str the string to encode
186 /// \param asnTag the ASN.1 identifier
187 /// \return the number of octets used for the encoding
188 /// \details DEREncodeTextString() can be used for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
189 /// \since Crypto++ 6.0
190 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &bt, const std::string &str, byte asnTag);
191 
192 /// \brief BER decode text string
193 /// \param bt BufferedTransformation object for reading
194 /// \param str the string to decode
195 /// \param asnTag the ASN.1 identifier
196 /// \details BERDecodeTextString() can be used for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
197 /// \since Crypto++ 8.3
198 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &bt, SecByteBlock &str, byte asnTag);
199 
200 /// \brief BER decode text string
201 /// \param bt BufferedTransformation object for reading
202 /// \param str the string to decode
203 /// \param asnTag the ASN.1 identifier
204 /// \details BERDecodeTextString() can be used for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
205 /// \since Crypto++ 6.0
206 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &bt, std::string &str, byte asnTag);
207 
208 /// \brief DER encode date
209 /// \param bt BufferedTransformation object for writing
210 /// \param str the date to encode
211 /// \param asnTag the ASN.1 identifier
212 /// \return the number of octets used for the encoding
213 /// \details BERDecodeDate() can be used for UTC_TIME and GENERALIZED_TIME
214 /// \since Crypto++ 8.3
215 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeDate(BufferedTransformation &bt, const SecByteBlock &str, byte asnTag);
216 
217 /// \brief BER decode date
218 /// \param bt BufferedTransformation object for reading
219 /// \param str the date to decode
220 /// \param asnTag the ASN.1 identifier
221 /// \details BERDecodeDate() can be used for UTC_TIME and GENERALIZED_TIME
222 /// \since Crypto++ 8.3
223 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeDate(BufferedTransformation &bt, SecByteBlock &str, byte asnTag);
224 
225 /// \brief DER encode bit string
226 /// \param bt BufferedTransformation object for writing
227 /// \param str the string to encode
228 /// \param strLen the length of the string
229 /// \param unusedBits the number of unused bits
230 /// \return the number of octets used for the encoding
231 /// \details The caller is responsible for shifting octets if unusedBits is
232 ///  not 0. For example, to DER encode a web server X.509 key usage, the 101b
233 ///  bit mask is often used (digitalSignature and keyEncipherment). In this
234 ///  case <tt>str</tt> is one octet with a value=0xa0 and unusedBits=5. The
235 ///  value 0xa0 is <tt>101b << 5</tt>.
236 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0);
237 
238 /// \brief DER decode bit string
239 /// \param bt BufferedTransformation object for reading
240 /// \param str the decoded string
241 /// \param unusedBits the number of unused bits
242 /// \details The caller is responsible for shifting octets if unusedBits is
243 ///  not 0. For example, to DER encode a web server X.509 key usage, the 101b
244 ///  bit mask is often used (digitalSignature and keyEncipherment). In this
245 ///  case <tt>str</tt> is one octet with a value=0xa0 and unusedBits=5. The
246 ///  value 0xa0 is <tt>101b << 5</tt>.
247 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits);
248 
249 /// \brief BER decode and DER re-encode
250 /// \param bt BufferedTransformation object for writing
251 /// \param dest BufferedTransformation object
252 CRYPTOPP_DLL void CRYPTOPP_API DERReencode(BufferedTransformation &bt, BufferedTransformation &dest);
253 
254 /// \brief BER decode size
255 /// \param bt BufferedTransformation object for reading
256 /// \return the length of the ASN.1 value, in bytes
257 /// \details BERDecodePeekLength() determines the length of a value without
258 ///  consuming octets in the stream. The stream must use definite length encoding.
259 ///  If indefinite length encoding is used or an error occurs, then 0 is returned.
260 /// \since Crypto++ 8.3
261 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodePeekLength(const BufferedTransformation &bt);
262 
263 /// \brief Object Identifier
264 class CRYPTOPP_DLL OID
265 {
266 public:
~OID()267 	virtual ~OID() {}
268 
269 	/// \brief Construct an OID
OID()270 	OID() {}
271 
272 	/// \brief Construct an OID
273 	/// \param v value to initialize the OID
OID(word32 v)274 	OID(word32 v) : m_values(1, v) {}
275 
276 	/// \brief Construct an OID
277 	/// \param bt BufferedTransformation object
OID(BufferedTransformation & bt)278 	OID(BufferedTransformation &bt) {
279 		BERDecode(bt);
280 	}
281 
282 	/// \brief Append a value to an OID
283 	/// \param rhs the value to append
284 	inline OID & operator+=(word32 rhs) {
285 		m_values.push_back(rhs); return *this;
286 	}
287 
288 	/// \brief DER encode this OID
289 	/// \param bt BufferedTransformation object
290 	void DEREncode(BufferedTransformation &bt) const;
291 
292 	/// \brief BER decode an OID
293 	/// \param bt BufferedTransformation object
294 	void BERDecode(BufferedTransformation &bt);
295 
296 	/// \brief BER decode an OID
297 	/// \param bt BufferedTransformation object
298 	/// \throw BERDecodeErr() if decoded value doesn't match an expected OID
299 	/// \details BERDecodeAndCheck() can be used to parse an OID and verify it matches an expected.
300 	/// <pre>
301 	///   BERSequenceDecoder key(bt);
302 	///   ...
303 	///   BERSequenceDecoder algorithm(key);
304 	///   GetAlgorithmID().BERDecodeAndCheck(algorithm);
305 	/// </pre>
306 	void BERDecodeAndCheck(BufferedTransformation &bt) const;
307 
308 	/// \brief Determine if OID is empty
309 	/// \return true if OID has 0 elements, false otherwise
310 	/// \since Crypto++ 8.0
Empty()311 	bool Empty() const {
312 		return m_values.empty();
313 	}
314 
315 	/// \brief Retrieve OID value array
316 	/// \return OID value vector
317 	/// \since Crypto++ 8.0
GetValues()318 	const std::vector<word32>& GetValues() const {
319 		return m_values;
320 	}
321 
322 	/// \brief Print an OID
323 	/// \param out ostream object
324 	/// \return ostream reference
325 	/// \details Print() writes the OID in a customary format, like
326 	///  1.2.840.113549.1.1.11. The caller is reposnsible to convert the
327 	///  OID to a friendly name, like sha256WithRSAEncryption.
328 	/// \since Crypto++ 8.3
329 	std::ostream& Print(std::ostream& out) const;
330 
331 protected:
332 	friend bool operator==(const OID &lhs, const OID &rhs);
333 	friend bool operator!=(const OID &lhs, const OID &rhs);
334 	friend bool operator<(const OID &lhs, const OID &rhs);
335 	friend bool operator<=(const OID &lhs, const OID &rhs);
336 	friend bool operator>=(const OID &lhs, const OID &rhs);
337 
338 	std::vector<word32> m_values;
339 
340 private:
341 	static void EncodeValue(BufferedTransformation &bt, word32 v);
342 	static size_t DecodeValue(BufferedTransformation &bt, word32 &v);
343 };
344 
345 /// \brief ASN.1 encoded object filter
346 class EncodedObjectFilter : public Filter
347 {
348 public:
349 	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};
350 	enum State {IDENTIFIER, LENGTH, BODY, TAIL, ALL_DONE} m_state;
351 
~EncodedObjectFilter()352 	virtual ~EncodedObjectFilter() {}
353 
354 	/// \brief Construct an EncodedObjectFilter
355 	/// \param attachment a BufferedTrasformation to attach to this object
356 	/// \param nObjects the number of objects
357 	/// \param flags bitwise OR of EncodedObjectFilter::Flag
358 	EncodedObjectFilter(BufferedTransformation *attachment = NULLPTR, unsigned int nObjects = 1, word32 flags = 0);
359 
360 	/// \brief Input a byte buffer for processing
361 	/// \param inString the byte buffer to process
362 	/// \param length the size of the string, in bytes
363 	void Put(const byte *inString, size_t length);
364 
GetNumberOfCompletedObjects()365 	unsigned int GetNumberOfCompletedObjects() const {return m_nCurrentObject;}
GetPositionOfObject(unsigned int i)366 	unsigned long GetPositionOfObject(unsigned int i) const {return m_positions[i];}
367 
368 private:
369 	BufferedTransformation & CurrentTarget();
370 
371 	ByteQueue m_queue;
372 	std::vector<unsigned int> m_positions;
373 	lword m_lengthRemaining;
374 	word32 m_nObjects, m_nCurrentObject, m_level, m_flags;
375 	byte m_id;
376 };
377 
378 /// \brief BER General Decoder
379 class CRYPTOPP_DLL BERGeneralDecoder : public Store
380 {
381 public:
382 	/// \brief Default ASN.1 tag
383 	enum {DefaultTag = SEQUENCE | CONSTRUCTED};
384 
385 	virtual ~BERGeneralDecoder();
386 
387 	/// \brief Construct an ASN.1 decoder
388 	/// \param inQueue input byte queue
389 	/// \details BERGeneralDecoder uses DefaultTag
390 	explicit BERGeneralDecoder(BufferedTransformation &inQueue);
391 
392 	/// \brief Construct an ASN.1 decoder
393 	/// \param inQueue input byte queue
394 	/// \param asnTag ASN.1 tag
395 	explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
396 
397 	/// \brief Construct an ASN.1 decoder
398 	/// \param inQueue input byte queue
399 	/// \param asnTag ASN.1 tag
400 	explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
401 
402 	/// \brief Determine length encoding
403 	/// \return true if the ASN.1 object is definite length encoded, false otherwise
IsDefiniteLength()404 	bool IsDefiniteLength() const {
405 		return m_definiteLength;
406 	}
407 
408 	/// \brief Determine remaining length
409 	/// \return number of octets that remain to be consumed
410 	/// \details RemainingLength() is only valid if IsDefiniteLength()
411 	///  returns true.
RemainingLength()412 	lword RemainingLength() const {
413 		CRYPTOPP_ASSERT(m_definiteLength);
414 		return IsDefiniteLength() ? m_length : 0;
415 	}
416 
417 	/// \brief Determine end of stream
418 	/// \return true if all octets have been consumed, false otherwise
419 	bool EndReached() const;
420 
421 	/// \brief Determine next octet
422 	/// \return next octet in the stream
423 	/// \details PeekByte does not consume the octet.
424 	/// \throw BERDecodeError if there are no octets remaining
425 	byte PeekByte() const;
426 
427 	/// \brief Determine next octet
428 	/// \details CheckByte reads the next byte in the stream and verifies
429 	///  the octet matches b.
430 	/// \throw BERDecodeError if the next octet is not b
431 	void CheckByte(byte b);
432 
433 	/// \brief Transfer bytes to another BufferedTransformation
434 	/// \param target the destination BufferedTransformation
435 	/// \param transferBytes the number of bytes to transfer
436 	/// \param channel the channel on which the transfer should occur
437 	/// \param blocking specifies whether the object should block when
438 	///  processing input
439 	/// \return the number of bytes that remain in the transfer block
440 	///  (i.e., bytes not transferred)
441 	/// \details TransferTo2() removes bytes and moves
442 	///  them to the destination. Transfer begins at the index position
443 	///  in the current stream, and not from an absolute position in the
444 	///  stream.
445 	/// \details transferBytes is an \a IN and \a OUT parameter. When
446 	///  the call is made, transferBytes is the requested size of the
447 	///  transfer. When the call returns, transferBytes is the number
448 	///  of bytes that were transferred.
449 	size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
450 
451 	/// \brief Copy bytes to another BufferedTransformation
452 	/// \param target the destination BufferedTransformation
453 	/// \param begin the 0-based index of the first byte to copy in
454 	///  the stream
455 	/// \param end the 0-based index of the last byte to copy in
456 	///  the stream
457 	/// \param channel the channel on which the transfer should occur
458 	/// \param blocking specifies whether the object should block when
459 	///  processing input
460 	/// \return the number of bytes that remain in the copy block
461 	///  (i.e., bytes not copied)
462 	/// \details CopyRangeTo2 copies bytes to the
463 	///  destination. The bytes are not removed from this object. Copying
464 	///  begins at the index position in the current stream, and not from
465 	///  an absolute position in the stream.
466 	/// \details begin is an \a IN and \a OUT parameter. When the call is
467 	///  made, begin is the starting position of the copy. When the call
468 	///  returns, begin is the position of the first byte that was \a not
469 	///  copied (which may be different than end). begin can be used for
470 	///  subsequent calls to CopyRangeTo2().
471 	size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
472 
473 	/// \brief Signals the end of messages to the object
474 	/// \details Call this to denote end of sequence
475 	void MessageEnd();
476 
477 protected:
478 	BufferedTransformation &m_inQueue;
479 	lword m_length;
480 	bool m_finished, m_definiteLength;
481 
482 private:
483 	void Init(byte asnTag);
StoreInitialize(const NameValuePairs & parameters)484 	void StoreInitialize(const NameValuePairs &parameters)
485 		{CRYPTOPP_UNUSED(parameters); CRYPTOPP_ASSERT(false);}
486 	lword ReduceLength(lword delta);
487 };
488 
489 /// \brief DER General Encoder
490 class CRYPTOPP_DLL DERGeneralEncoder : public ByteQueue
491 {
492 public:
493 	/// \brief Default ASN.1 tag
494 	enum {DefaultTag = SEQUENCE | CONSTRUCTED};
495 
496 	virtual ~DERGeneralEncoder();
497 
498 	/// \brief Construct an ASN.1 encoder
499 	/// \param outQueue output byte queue
500 	/// \details DERGeneralEncoder uses DefaultTag
501 	explicit DERGeneralEncoder(BufferedTransformation &outQueue);
502 
503 	/// \brief Construct an ASN.1 encoder
504 	/// \param outQueue output byte queue
505 	/// \param asnTag ASN.1 tag
506 	explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag);
507 
508 	/// \brief Construct an ASN.1 encoder
509 	/// \param outQueue output byte queue
510 	/// \param asnTag ASN.1 tag
511 	explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag);
512 
513 	/// \brief Signals the end of messages to the object
514 	/// \details Call this to denote end of sequence
515 	void MessageEnd();
516 
517 private:
518 	BufferedTransformation &m_outQueue;
519 	byte m_asnTag;
520 	bool m_finished;
521 };
522 
523 /// \brief BER Sequence Decoder
524 class CRYPTOPP_DLL BERSequenceDecoder : public BERGeneralDecoder
525 {
526 public:
527 	/// \brief Default ASN.1 tag
528 	enum {DefaultTag = SEQUENCE | CONSTRUCTED};
529 
530 	/// \brief Construct an ASN.1 decoder
531 	/// \param inQueue input byte queue
532 	/// \details BERSequenceDecoder uses DefaultTag
BERSequenceDecoder(BufferedTransformation & inQueue)533 	explicit BERSequenceDecoder(BufferedTransformation &inQueue)
534 		: BERGeneralDecoder(inQueue, DefaultTag) {}
535 
536 	/// \brief Construct an ASN.1 decoder
537 	/// \param inQueue input byte queue
538 	/// \param asnTag ASN.1 tag
BERSequenceDecoder(BufferedTransformation & inQueue,byte asnTag)539 	explicit BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag)
540 		: BERGeneralDecoder(inQueue, asnTag) {}
541 
542 	/// \brief Construct an ASN.1 decoder
543 	/// \param inQueue input byte queue
544 	/// \details BERSequenceDecoder uses DefaultTag
BERSequenceDecoder(BERSequenceDecoder & inQueue)545 	explicit BERSequenceDecoder(BERSequenceDecoder &inQueue)
546 		: BERGeneralDecoder(inQueue, DefaultTag) {}
547 
548 	/// \brief Construct an ASN.1 decoder
549 	/// \param inQueue input byte queue
550 	/// \param asnTag ASN.1 tag
BERSequenceDecoder(BERSequenceDecoder & inQueue,byte asnTag)551 	explicit BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag)
552 		: BERGeneralDecoder(inQueue, asnTag) {}
553 };
554 
555 /// \brief DER Sequence Encoder
556 class CRYPTOPP_DLL DERSequenceEncoder : public DERGeneralEncoder
557 {
558 public:
559 	/// \brief Default ASN.1 tag
560 	enum {DefaultTag = SEQUENCE | CONSTRUCTED};
561 
562 	/// \brief Construct an ASN.1 encoder
563 	/// \param outQueue output byte queue
564 	/// \details DERSequenceEncoder uses DefaultTag
DERSequenceEncoder(BufferedTransformation & outQueue)565 	explicit DERSequenceEncoder(BufferedTransformation &outQueue)
566 		: DERGeneralEncoder(outQueue, DefaultTag) {}
567 
568 	/// \brief Construct an ASN.1 encoder
569 	/// \param outQueue output byte queue
570 	/// \param asnTag ASN.1 tag
DERSequenceEncoder(BufferedTransformation & outQueue,byte asnTag)571 	explicit DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag)
572 		: DERGeneralEncoder(outQueue, asnTag) {}
573 
574 	/// \brief Construct an ASN.1 encoder
575 	/// \param outQueue output byte queue
576 	/// \details DERSequenceEncoder uses DefaultTag
DERSequenceEncoder(DERSequenceEncoder & outQueue)577 	explicit DERSequenceEncoder(DERSequenceEncoder &outQueue)
578 		: DERGeneralEncoder(outQueue, DefaultTag) {}
579 
580 	/// \brief Construct an ASN.1 encoder
581 	/// \param outQueue output byte queue
582 	/// \param asnTag ASN.1 tag
DERSequenceEncoder(DERSequenceEncoder & outQueue,byte asnTag)583 	explicit DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag)
584 		: DERGeneralEncoder(outQueue, asnTag) {}
585 };
586 
587 /// \brief BER Set Decoder
588 class CRYPTOPP_DLL BERSetDecoder : public BERGeneralDecoder
589 {
590 public:
591 	/// \brief Default ASN.1 tag
592 	enum {DefaultTag = SET | CONSTRUCTED};
593 
594 	/// \brief Construct an ASN.1 decoder
595 	/// \param inQueue input byte queue
596 	/// \details BERSetDecoder uses DefaultTag
BERSetDecoder(BufferedTransformation & inQueue)597 	explicit BERSetDecoder(BufferedTransformation &inQueue)
598 		: BERGeneralDecoder(inQueue, DefaultTag) {}
599 
600 	/// \brief Construct an ASN.1 decoder
601 	/// \param inQueue input byte queue
602 	/// \param asnTag ASN.1 tag
BERSetDecoder(BufferedTransformation & inQueue,byte asnTag)603 	explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag)
604 		: BERGeneralDecoder(inQueue, asnTag) {}
605 
606 	/// \brief Construct an ASN.1 decoder
607 	/// \param inQueue input byte queue
608 	/// \details BERSetDecoder uses DefaultTag
BERSetDecoder(BERSetDecoder & inQueue)609 	explicit BERSetDecoder(BERSetDecoder &inQueue)
610 		: BERGeneralDecoder(inQueue, DefaultTag) {}
611 
612 	/// \brief Construct an ASN.1 decoder
613 	/// \param inQueue input byte queue
614 	/// \param asnTag ASN.1 tag
BERSetDecoder(BERSetDecoder & inQueue,byte asnTag)615 	explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag)
616 		: BERGeneralDecoder(inQueue, asnTag) {}
617 };
618 
619 /// \brief DER Set Encoder
620 class CRYPTOPP_DLL DERSetEncoder : public DERGeneralEncoder
621 {
622 public:
623 	/// \brief Default ASN.1 tag
624 	enum {DefaultTag = SET | CONSTRUCTED};
625 
626 	/// \brief Construct an ASN.1 encoder
627 	/// \param outQueue output byte queue
628 	/// \details DERSetEncoder uses DefaultTag
DERSetEncoder(BufferedTransformation & outQueue)629 	explicit DERSetEncoder(BufferedTransformation &outQueue)
630 		: DERGeneralEncoder(outQueue, DefaultTag) {}
631 
632 	/// \brief Construct an ASN.1 encoder
633 	/// \param outQueue output byte queue
634 	/// \param asnTag ASN.1 tag
DERSetEncoder(BufferedTransformation & outQueue,byte asnTag)635 	explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag)
636 		: DERGeneralEncoder(outQueue, asnTag) {}
637 
638 	/// \brief Construct an ASN.1 encoder
639 	/// \param outQueue output byte queue
640 	/// \details DERSetEncoder uses DefaultTag
DERSetEncoder(DERSetEncoder & outQueue)641 	explicit DERSetEncoder(DERSetEncoder &outQueue)
642 		: DERGeneralEncoder(outQueue, DefaultTag) {}
643 
644 	/// \brief Construct an ASN.1 encoder
645 	/// \param outQueue output byte queue
646 	/// \param asnTag ASN.1 tag
DERSetEncoder(DERSetEncoder & outQueue,byte asnTag)647 	explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag)
648 		: DERGeneralEncoder(outQueue, asnTag) {}
649 };
650 
651 /// \brief Optional data encoder and decoder
652 /// \tparam T class or type
653 template <class T>
654 class ASNOptional : public member_ptr<T>
655 {
656 public:
657 	/// \brief BER decode optional data
658 	/// \param seqDecoder sequence with the optional ASN.1 data
659 	/// \param tag ASN.1 tag to match as optional data
660 	/// \param mask the mask to apply when matching the tag
661 	/// \sa ASNTag and ASNIdFlag
662 	void BERDecode(BERSequenceDecoder &seqDecoder, byte tag, byte mask = ~CONSTRUCTED)
663 	{
664 		byte b;
665 		if (seqDecoder.Peek(b) && (b & mask) == tag)
666 			reset(new T(seqDecoder));
667 	}
668 
669 	/// \brief DER encode optional data
670 	/// \param out BufferedTransformation object
DEREncode(BufferedTransformation & out)671 	void DEREncode(BufferedTransformation &out)
672 	{
673 		if (this->get() != NULLPTR)
674 			this->get()->DEREncode(out);
675 	}
676 };
677 
678 /// \brief Encode and decode ASN.1 objects with additional information
679 /// \tparam BASE base class or type
680 /// \details Encodes and decodes public keys, private keys and group
681 ///   parameters with OID identifying the algorithm or scheme.
682 template <class BASE>
683 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ASN1CryptoMaterial : public ASN1Object, public BASE
684 {
685 public:
686 	/// \brief DER encode ASN.1 object
687 	/// \param bt BufferedTransformation object
688 	/// \details Save() will write the OID associated with algorithm or scheme.
689 	///   In the case of public and private keys, this function writes the
690 	///   subjectPubicKeyInfo and privateKeyInfo parts.
Save(BufferedTransformation & bt)691 	void Save(BufferedTransformation &bt) const
692 		{BEREncode(bt);}
693 
694 	/// \brief BER decode ASN.1 object
695 	/// \param bt BufferedTransformation object
Load(BufferedTransformation & bt)696 	void Load(BufferedTransformation &bt)
697 		{BERDecode(bt);}
698 };
699 
700 /// \brief Encodes and decodes subjectPublicKeyInfo
701 class CRYPTOPP_DLL X509PublicKey : public ASN1CryptoMaterial<PublicKey>
702 {
703 public:
~X509PublicKey()704 	virtual ~X509PublicKey() {}
705 
706 	void BERDecode(BufferedTransformation &bt);
707 	void DEREncode(BufferedTransformation &bt) const;
708 
709 	/// \brief Retrieves the OID of the algorithm
710 	/// \return OID of the algorithm
711 	virtual OID GetAlgorithmID() const =0;
712 
713 	/// \brief Decode algorithm parameters
714 	/// \param bt BufferedTransformation object
715 	/// \sa BERDecodePublicKey, <A HREF="http://www.ietf.org/rfc/rfc2459.txt">RFC
716 	///  2459, section 7.3.1</A>
BERDecodeAlgorithmParameters(BufferedTransformation & bt)717 	virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
718 		{BERDecodeNull(bt); return false;}
719 
720 	/// \brief Encode algorithm parameters
721 	/// \param bt BufferedTransformation object
722 	/// \sa DEREncodePublicKey, <A HREF="http://www.ietf.org/rfc/rfc2459.txt">RFC
723 	///  2459, section 7.3.1</A>
DEREncodeAlgorithmParameters(BufferedTransformation & bt)724 	virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
725 		{DEREncodeNull(bt); return false;}
726 
727 	/// \brief Decode subjectPublicKey part of subjectPublicKeyInfo
728 	/// \param bt BufferedTransformation object
729 	/// \param parametersPresent flag indicating if algorithm parameters are present
730 	/// \param size number of octets to read for the parameters, in bytes
731 	/// \details BERDecodePublicKey() the decodes subjectPublicKey part of
732 	///  subjectPublicKeyInfo, without the BIT STRING header.
733 	/// \details When <tt>parametersPresent = true</tt> then BERDecodePublicKey() calls
734 	///  BERDecodeAlgorithmParameters() to parse algorithm parameters.
735 	/// \sa BERDecodeAlgorithmParameters
736 	virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
737 
738 	/// \brief Encode subjectPublicKey part of subjectPublicKeyInfo
739 	/// \param bt BufferedTransformation object
740 	/// \details DEREncodePublicKey() encodes the subjectPublicKey part of
741 	///  subjectPublicKeyInfo, without the BIT STRING header.
742 	/// \sa DEREncodeAlgorithmParameters
743 	virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0;
744 };
745 
746 /// \brief Encodes and Decodes privateKeyInfo
747 class CRYPTOPP_DLL PKCS8PrivateKey : public ASN1CryptoMaterial<PrivateKey>
748 {
749 public:
~PKCS8PrivateKey()750 	virtual ~PKCS8PrivateKey() {}
751 
752 	void BERDecode(BufferedTransformation &bt);
753 	void DEREncode(BufferedTransformation &bt) const;
754 
755 	/// \brief Retrieves the OID of the algorithm
756 	/// \return OID of the algorithm
757 	virtual OID GetAlgorithmID() const =0;
758 
759 	/// \brief Decode optional parameters
760 	/// \param bt BufferedTransformation object
761 	/// \sa BERDecodePrivateKey, <A HREF="http://www.ietf.org/rfc/rfc2459.txt">RFC
762 	///  2459, section 7.3.1</A>
BERDecodeAlgorithmParameters(BufferedTransformation & bt)763 	virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
764 		{BERDecodeNull(bt); return false;}
765 
766 	/// \brief Encode optional parameters
767 	/// \param bt BufferedTransformation object
768 	/// \sa DEREncodePrivateKey, <A HREF="http://www.ietf.org/rfc/rfc2459.txt">RFC
769 	///  2459, section 7.3.1</A>
DEREncodeAlgorithmParameters(BufferedTransformation & bt)770 	virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
771 		{DEREncodeNull(bt); return false;}
772 
773 	/// \brief Decode privateKey part of privateKeyInfo
774 	/// \param bt BufferedTransformation object
775 	/// \param parametersPresent flag indicating if algorithm parameters are present
776 	/// \param size number of octets to read for the parameters, in bytes
777 	/// \details BERDecodePrivateKey() the decodes privateKey part of privateKeyInfo,
778 	///  without the OCTET STRING header.
779 	/// \details When <tt>parametersPresent = true</tt> then BERDecodePrivateKey() calls
780 	///  BERDecodeAlgorithmParameters() to parse algorithm parameters.
781 	/// \sa BERDecodeAlgorithmParameters
782 	virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
783 
784 	/// \brief Encode privateKey part of privateKeyInfo
785 	/// \param bt BufferedTransformation object
786 	/// \details DEREncodePrivateKey() encodes the privateKey part of privateKeyInfo,
787 	///  without the OCTET STRING header.
788 	/// \sa DEREncodeAlgorithmParameters
789 	virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0;
790 
791 	/// \brief Decode optional attributes
792 	/// \param bt BufferedTransformation object
793 	/// \details BERDecodeOptionalAttributes() decodes optional attributes including
794 	///  context-specific tag.
795 	/// \sa BERDecodeAlgorithmParameters, DEREncodeOptionalAttributes
796 	/// \note default implementation stores attributes to be output using
797 	///  DEREncodeOptionalAttributes
798 	virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt);
799 
800 	/// \brief Encode optional attributes
801 	/// \param bt BufferedTransformation object
802 	/// \details DEREncodeOptionalAttributes() encodes optional attributes including
803 	///  context-specific tag.
804 	/// \sa BERDecodeAlgorithmParameters
805 	virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const;
806 
807 protected:
808 	ByteQueue m_optionalAttributes;
809 };
810 
811 // ********************************************************
812 
813 /// \brief DER Encode unsigned value
814 /// \tparam T class or type
815 /// \param out BufferedTransformation object
816 /// \param w unsigned value to encode
817 /// \param asnTag the ASN.1 identifier
818 /// \details DEREncodeUnsigned() can be used with INTEGER, BOOLEAN, and ENUM
819 template <class T>
820 size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag = INTEGER)
821 {
822 	byte buf[sizeof(w)+1];
823 	unsigned int bc;
824 	if (asnTag == BOOLEAN)
825 	{
826 		buf[sizeof(w)] = w ? 0xff : 0;
827 		bc = 1;
828 	}
829 	else
830 	{
831 		buf[0] = 0;
832 		for (unsigned int i=0; i<sizeof(w); i++)
833 			buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
834 		bc = sizeof(w);
835 		while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
836 			--bc;
837 		if (buf[sizeof(w)+1-bc] & 0x80)
838 			++bc;
839 	}
840 	out.Put(asnTag);
841 	size_t lengthBytes = DERLengthEncode(out, bc);
842 	out.Put(buf+sizeof(w)+1-bc, bc);
843 	return 1+lengthBytes+bc;
844 }
845 
846 /// \brief BER Decode unsigned value
847 /// \tparam T fundamental C++ type
848 /// \param in BufferedTransformation object
849 /// \param w the decoded value
850 /// \param asnTag the ASN.1 identifier
851 /// \param minValue the minimum expected value
852 /// \param maxValue the maximum expected value
853 /// \throw BERDecodeErr() if the value cannot be parsed or the decoded value is not within range.
854 /// \details DEREncodeUnsigned() can be used with INTEGER, BOOLEAN, and ENUM
855 template <class T>
856 void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag = INTEGER,
857 					   T minValue = 0, T maxValue = T(0xffffffff))
858 {
859 	byte b;
860 	if (!in.Get(b) || b != asnTag)
861 		BERDecodeError();
862 
863 	size_t bc;
864 	bool definite = BERLengthDecode(in, bc);
865 	if (!definite)
866 		BERDecodeError();
867 	if (bc > in.MaxRetrievable())  // Issue 346
868 		BERDecodeError();
869 	if (asnTag == BOOLEAN && bc != 1) // X.690, 8.2.1
870 		BERDecodeError();
871 	if ((asnTag == INTEGER || asnTag == ENUMERATED) && bc == 0) // X.690, 8.3.1 and 8.4
872 		BERDecodeError();
873 
874 	SecByteBlock buf(bc);
875 
876 	if (bc != in.Get(buf, bc))
877 		BERDecodeError();
878 
879 	// This consumes leading 0 octets. According to X.690, 8.3.2, it could be non-conforming behavior.
880 	//  X.690, 8.3.2 says "the bits of the first octet and bit 8 of the second octet ... (a) shall
881 	//  not all be ones and (b) shall not all be zeros ... These rules ensure that an integer value
882 	//  is always encoded in the smallest possible number of octet".
883 	// We invented AER (Alternate Encoding Rules), which is more relaxed than BER, CER, and DER.
884 	const byte *ptr = buf;
885 	while (bc > sizeof(w) && *ptr == 0)
886 	{
887 		bc--;
888 		ptr++;
889 	}
890 	if (bc > sizeof(w))
891 		BERDecodeError();
892 
893 	w = 0;
894 	for (unsigned int i=0; i<bc; i++)
895 		w = (w << 8) | ptr[i];
896 
897 	if (w < minValue || w > maxValue)
898 		BERDecodeError();
899 }
900 
901 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
902 /// \brief Compare two OIDs for equality
903 /// \param lhs the first OID
904 /// \param rhs the second OID
905 /// \return true if the OIDs are equal, false otherwise
906 inline bool operator==(const OID &lhs, const OID &rhs);
907 /// \brief Compare two OIDs for inequality
908 /// \param lhs the first OID
909 /// \param rhs the second OID
910 /// \return true if the OIDs are not equal, false otherwise
911 inline bool operator!=(const OID &lhs, const OID &rhs);
912 /// \brief Compare two OIDs for ordering
913 /// \param lhs the first OID
914 /// \param rhs the second OID
915 /// \return true if the first OID is less than the second OID, false otherwise
916 /// \details operator<() calls std::lexicographical_compare() on each element in the array of values.
917 inline bool operator<(const OID &lhs, const OID &rhs);
918 /// \brief Compare two OIDs for ordering
919 /// \param lhs the first OID
920 /// \param rhs the second OID
921 /// \return true if the first OID is less than or equal to the second OID, false otherwise
922 /// \details operator<=() is implemented in terms of operator==() and operator<().
923 /// \since Crypto++ 8.3
924 inline bool operator<=(const OID &lhs, const OID &rhs);
925 /// \brief Compare two OIDs for ordering
926 /// \param lhs the first OID
927 /// \param rhs the second OID
928 /// \return true if the first OID is greater than or equal to the second OID, false otherwise
929 /// \details operator>=() is implemented in terms of operator<().
930 /// \since Crypto++ 8.3
931 inline bool operator>=(const OID &lhs, const OID &rhs);
932 /// \brief Append a value to an OID
933 /// \param lhs the OID
934 /// \param rhs the value to append
935 inline OID operator+(const OID &lhs, unsigned long rhs);
936 /// \brief Print a OID value
937 /// \param out the output stream
938 /// \param oid the OID
939 inline std::ostream& operator<<(std::ostream& out, const OID &oid)
940 	{ return oid.Print(out); }
941 #else
942 inline bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
943 	{return lhs.m_values == rhs.m_values;}
944 inline bool operator!=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
945 	{return lhs.m_values != rhs.m_values;}
946 inline bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
947 	{return std::lexicographical_compare(lhs.m_values.begin(), lhs.m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}
948 inline bool operator<=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
949 	{return lhs<rhs || lhs==rhs;}
950 inline bool operator>=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
951 	{return ! (lhs<rhs);}
952 inline ::CryptoPP::OID operator+(const ::CryptoPP::OID &lhs, unsigned long rhs)
953 	{return ::CryptoPP::OID(lhs)+=rhs;}
954 inline std::ostream& operator<<(std::ostream& out, const OID &oid)
955 	{ return oid.Print(out); }
956 #endif
957 
958 NAMESPACE_END
959 
960 // Issue 340
961 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
962 # pragma GCC diagnostic pop
963 #endif
964 
965 #endif
966