Fabcoin Core  0.16.2
P2P Digital Currency
asn.h
Go to the documentation of this file.
1 // asn.h - written and placed in the public domain by Wei Dai
2 
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 // Issue 340
17 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
18 # pragma GCC diagnostic push
19 # pragma GCC diagnostic ignored "-Wconversion"
20 # pragma GCC diagnostic ignored "-Wsign-conversion"
21 #endif
22 
24 
25 enum ASNTag
28 {
29  BOOLEAN = 0x01,
30  INTEGER = 0x02,
31  BIT_STRING = 0x03,
32  OCTET_STRING = 0x04,
33  TAG_NULL = 0x05,
36  EXTERNAL = 0x08,
37  REAL = 0x09,
38  ENUMERATED = 0x0a,
39  UTF8_STRING = 0x0c,
40  SEQUENCE = 0x10,
41  SET = 0x11,
44  T61_STRING = 0x14,
46  IA5_STRING = 0x16,
47  UTC_TIME = 0x17,
52 };
53 
57 {
58  UNIVERSAL = 0x00,
59 // DATA = 0x01,
60 // HEADER = 0x02,
61  PRIMITIVE = 0x00,
62  CONSTRUCTED = 0x20,
63  APPLICATION = 0x40,
65  PRIVATE = 0xc0
66 };
67 
69 inline void BERDecodeError() {throw BERDecodeErr();}
70 
73 {
74 public:
76  UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
79  UnknownOID(const char *err) : BERDecodeErr(err) {}
80 };
81 
82 // unsigned int DERLengthEncode(unsigned int length, byte *output=0);
83 
89 
97 
101 
105 
111 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen);
112 
118 
124 
130 
137 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &bt, const std::string &str, byte asnTag);
138 
144 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &bt, std::string &str, byte asnTag);
145 
152 CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0);
153 
158 CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits);
159 
164 
167 {
168 public:
169  virtual ~OID() {}
170 
172  OID() {}
175  OID(word32 v) : m_values(1, v) {}
178  OID(BufferedTransformation &bt) {BERDecode(bt);}
179 
182  inline OID & operator+=(word32 rhs) {m_values.push_back(rhs); return *this;}
183 
186  void DEREncode(BufferedTransformation &bt) const;
187 
190  void BERDecode(BufferedTransformation &bt);
191 
202  void BERDecodeAndCheck(BufferedTransformation &bt) const;
203 
204  std::vector<word32> m_values;
205 
206 private:
207  static void EncodeValue(BufferedTransformation &bt, word32 v);
208  static size_t DecodeValue(BufferedTransformation &bt, word32 &v);
209 };
210 
213 {
214 public:
215  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};
216 
217  virtual ~EncodedObjectFilter() {}
218 
223  EncodedObjectFilter(BufferedTransformation *attachment = NULL, unsigned int nObjects = 1, word32 flags = 0);
224 
228  void Put(const byte *inString, size_t length);
229 
230  unsigned int GetNumberOfCompletedObjects() const {return m_nCurrentObject;}
231  unsigned long GetPositionOfObject(unsigned int i) const {return m_positions[i];}
232 
233 private:
234  BufferedTransformation & CurrentTarget();
235 
237  unsigned int m_nObjects, m_nCurrentObject, m_level;
238  std::vector<unsigned int> m_positions;
240  enum State {IDENTIFIER, LENGTH, BODY, TAIL, ALL_DONE} m_state;
243 };
244 
247 {
248 public:
249  virtual ~BERGeneralDecoder();
250 
251  explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
252  explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
253 
254  bool IsDefiniteLength() const {return m_definiteLength;}
255  lword RemainingLength() const {CRYPTOPP_ASSERT(m_definiteLength); return m_length;}
256  bool EndReached() const;
257  byte PeekByte() const;
258  void CheckByte(byte b);
259 
260  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
261  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
262 
263  // call this to denote end of sequence
264  void MessageEnd();
265 
266 protected:
268  bool m_finished, m_definiteLength;
270 
271 private:
272  void Init(byte asnTag);
274  {CRYPTOPP_UNUSED(parameters); CRYPTOPP_ASSERT(false);}
275  lword ReduceLength(lword delta);
276 };
277 
278 // GCC (and likely other compilers) identify the explicit DERGeneralEncoder as a copy constructor;
279 // and not a constructor. We had to remove the default asnTag value to point the compiler in the
280 // proper direction. We did not break the library or versioning based on the output of
281 // `nm --demangle libcryptopp.a | grep DERGeneralEncoder::DERGeneralEncoder | grep -v " U "`.
282 
285 {
286 public:
287  virtual ~DERGeneralEncoder();
288 
289  explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
290  explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
291 
292  // call this to denote end of sequence
293  void MessageEnd();
294 
295 private:
298 
300 };
301 
304 {
305 public:
307  : BERGeneralDecoder(inQueue, asnTag) {}
309  : BERGeneralDecoder(inQueue, asnTag) {}
310 };
311 
314 {
315 public:
317  : DERGeneralEncoder(outQueue, asnTag) {}
319  : DERGeneralEncoder(outQueue, asnTag) {}
320 };
321 
324 {
325 public:
326  explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag = SET | CONSTRUCTED)
327  : BERGeneralDecoder(inQueue, asnTag) {}
328  explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag = SET | CONSTRUCTED)
329  : BERGeneralDecoder(inQueue, asnTag) {}
330 };
331 
334 {
335 public:
336  explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag = SET | CONSTRUCTED)
337  : DERGeneralEncoder(outQueue, asnTag) {}
338  explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag = SET | CONSTRUCTED)
339  : DERGeneralEncoder(outQueue, asnTag) {}
340 };
341 
344 template <class T>
345 class ASNOptional : public member_ptr<T>
346 {
347 public:
353  void BERDecode(BERSequenceDecoder &seqDecoder, byte tag, byte mask = ~CONSTRUCTED)
354  {
355  byte b;
356  if (seqDecoder.Peek(b) && (b & mask) == tag)
357  reset(new T(seqDecoder));
358  }
359 
363  {
364  if (this->get() != NULL)
365  this->get()->DEREncode(out);
366  }
367 };
368 
373 template <class BASE>
375 {
376 public:
382  void Save(BufferedTransformation &bt) const
383  {BEREncode(bt);}
384 
388  {BERDecode(bt);}
389 };
390 
393 {
394 public:
395  virtual ~X509PublicKey() {}
396 
397  void BERDecode(BufferedTransformation &bt);
398  void DEREncode(BufferedTransformation &bt) const;
399 
402  virtual OID GetAlgorithmID() const =0;
404  {BERDecodeNull(bt); return false;}
406  {DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
407 
409  virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
411  virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0;
412 };
413 
416 {
417 public:
418  virtual ~PKCS8PrivateKey() {}
419 
420  void BERDecode(BufferedTransformation &bt);
421  void DEREncode(BufferedTransformation &bt) const;
422 
425  virtual OID GetAlgorithmID() const =0;
427  {BERDecodeNull(bt); return false;}
429  {DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
430 
432  virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
434  virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0;
435 
437 
438  virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt);
440  virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const;
441 
442 protected:
444 };
445 
446 // ********************************************************
447 
454 template <class T>
456 {
457  byte buf[sizeof(w)+1];
458  unsigned int bc;
459  if (asnTag == BOOLEAN)
460  {
461  buf[sizeof(w)] = w ? 0xff : 0;
462  bc = 1;
463  }
464  else
465  {
466  buf[0] = 0;
467  for (unsigned int i=0; i<sizeof(w); i++)
468  buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
469  bc = sizeof(w);
470  while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
471  --bc;
472  if (buf[sizeof(w)+1-bc] & 0x80)
473  ++bc;
474  }
475  out.Put(asnTag);
476  size_t lengthBytes = DERLengthEncode(out, bc);
477  out.Put(buf+sizeof(w)+1-bc, bc);
478  return 1+lengthBytes+bc;
479 }
480 
490 template <class T>
492  T minValue = 0, T maxValue = T(0xffffffff))
493 {
494  byte b;
495  if (!in.Get(b) || b != asnTag)
496  BERDecodeError();
497 
498  size_t bc;
499  bool definite = BERLengthDecode(in, bc);
500  if (!definite)
501  BERDecodeError();
502  if (bc > in.MaxRetrievable()) // Issue 346
503  BERDecodeError();
504  if (asnTag == BOOLEAN && bc != 1) // X.690, 8.2.1
505  BERDecodeError();
506  if ((asnTag == INTEGER || asnTag == ENUMERATED) && bc == 0) // X.690, 8.3.1 and 8.4
507  BERDecodeError();
508 
509  SecByteBlock buf(bc);
510 
511  if (bc != in.Get(buf, bc))
512  BERDecodeError();
513 
514  // This consumes leading 0 octets. According to X.690, 8.3.2, it could be non-conforming behavior.
515  // X.690, 8.3.2 says "the bits of the first octet and bit 8 of the second octet ... (a) shall
516  // not all be ones and (b) shall not all be zeros ... These rules ensure that an integer value
517  // is always encoded in the smallest possible number of octet".
518  // We invented AER (Alternate Encoding Rules), which is more relaxed than BER, CER, and DER.
519  const byte *ptr = buf;
520  while (bc > sizeof(w) && *ptr == 0)
521  {
522  bc--;
523  ptr++;
524  }
525  if (bc > sizeof(w))
526  BERDecodeError();
527 
528  w = 0;
529  for (unsigned int i=0; i<bc; i++)
530  w = (w << 8) | ptr[i];
531 
532  if (w < minValue || w > maxValue)
533  BERDecodeError();
534 }
535 
536 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
537 inline bool operator==(const OID &lhs, const OID &rhs);
546 inline bool operator!=(const OID &lhs, const OID &rhs);
552 inline bool operator<(const OID &lhs, const OID &rhs);
556 inline OID operator+(const OID &lhs, unsigned long rhs);
557 #else
558 inline bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
559  {return lhs.m_values == rhs.m_values;}
560 inline bool operator!=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
561  {return lhs.m_values != rhs.m_values;}
562 inline bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
563  {return std::lexicographical_compare(lhs.m_values.begin(), lhs.m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}
564 inline ::CryptoPP::OID operator+(const ::CryptoPP::OID &lhs, unsigned long rhs)
565  {return ::CryptoPP::OID(lhs)+=rhs;}
566 #endif
567 
569 
570 // Issue 340
571 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
572 # pragma GCC diagnostic pop
573 #endif
574 
575 #endif
UnknownOID(const char *err)
Construct an UnknownOID.
Definition: asn.h:79
lword RemainingLength() const
Definition: asn.h:255
virtual ~PKCS8PrivateKey()
Definition: asn.h:418
Definition: asn.h:41
Definition: asn.h:36
uint8_t byte
Definition: Common.h:57
const lword LWORD_MAX
Definition: config.h:246
Utility functions for the Crypto++ library.
virtual size_t Peek(byte &outByte) const
Peek a 8-bit byte.
Definition: cryptlib.cpp:534
size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag=INTEGER)
DER Encode unsigned value.
Definition: asn.h:455
Definition: asn.h:33
Definition: asn.h:30
Definition: asn.h:61
DERSetEncoder(DERSetEncoder &outQueue, byte asnTag=SET|CONSTRUCTED)
Definition: asn.h:338
Encodes and decodesprivateKeyInfo.
Definition: asn.h:415
#define T(i, x)
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Definition: asn.h:40
DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag=SEQUENCE|CONSTRUCTED)
Definition: asn.h:316
CRYPTOPP_DLL void CRYPTOPP_API DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
Definition: asn.cpp:213
Abstract base classes that provide a uniform interface to this library.
void StoreInitialize(const NameValuePairs &parameters)
Definition: asn.h:273
Definition: asn.h:47
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag=INTEGER, T minValue=0, T maxValue=T(0xffffffff))
BER Decode unsigned value.
Definition: asn.h:491
OID()
Construct an OID.
Definition: asn.h:172
CRYPTOPP_DLL bool CRYPTOPP_API BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
Definition: asn.cpp:76
Classes for automatic resource management.
BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag=SEQUENCE|CONSTRUCTED)
Definition: asn.h:306
void DEREncode(BufferedTransformation &out)
DER encode optional data.
Definition: asn.h:362
CRYPTOPP_DLL void CRYPTOPP_API BERDecodeNull(BufferedTransformation &bt)
BER decode NULL.
Definition: asn.cpp:93
word32 m_flags
Definition: asn.h:236
Acts as a Source for pre-existing, static data.
Definition: simple.h:299
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen)
DER encode octet string.
Definition: asn.cpp:104
BER Set Decoder.
Definition: asn.h:323
Definition: asn.h:58
BER Sequence Decoder.
Definition: asn.h:303
unsigned int m_nObjects
Definition: asn.h:237
Interface for buffered transformations.
Definition: cryptlib.h:1352
virtual ~X509PublicKey()
Definition: asn.h:395
ByteQueue m_optionalAttributes
Definition: asn.h:443
OID(BufferedTransformation &bt)
Construct an OID.
Definition: asn.h:178
bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:558
Pointer that overloads operator ->
Definition: smartptr.h:39
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:59
Optional data encoder and decoder.
Definition: asn.h:345
ASNIdFlag
ASN.1 flags.
Definition: asn.h:56
void BERDecode(BERSequenceDecoder &seqDecoder, byte tag, byte mask=~CONSTRUCTED)
BER decode optional data.
Definition: asn.h:353
std::vector< unsigned int > m_positions
Definition: asn.h:238
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0)
DER encode bit string.
Definition: asn.cpp:179
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &bt, std::string &str, byte asnTag)
BER decode text string.
Definition: asn.cpp:159
inline::CryptoPP::OID operator+(const ::CryptoPP::OID &lhs, unsigned long rhs)
Definition: asn.h:564
unsigned long GetPositionOfObject(unsigned int i) const
Definition: asn.h:231
Definition: asn.h:46
Classes for an unlimited queue to store bytes.
CRYPTOPP_DLL void CRYPTOPP_API DEREncodeNull(BufferedTransformation &bt)
DER encode NULL.
Definition: asn.cpp:87
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1376
byte m_asnTag
Definition: asn.h:299
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
Definition: asn.h:405
UnknownOID()
Construct an UnknownOID.
Definition: asn.h:76
ByteQueue m_queue
Definition: asn.h:239
OID & operator+=(word32 rhs)
Append a value to an OID.
Definition: asn.h:182
BERSetDecoder(BufferedTransformation &inQueue, byte asnTag=SET|CONSTRUCTED)
Definition: asn.h:326
ASN.1 encoded object filter.
Definition: asn.h:212
Interface for encoding and decoding ASN1 objects.
Definition: cryptlib.h:2942
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
Definition: asn.h:428
bool IsDefiniteLength() const
Definition: asn.h:254
std::vector< word32 > m_values
Definition: asn.h:204
DER Set Encoder.
Definition: asn.h:333
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits)
DER decode bit string.
Definition: asn.cpp:188
#define CRYPTOPP_API
Definition: config.h:705
bool operator!=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:560
BERSetDecoder(BERSetDecoder &inQueue, byte asnTag=SET|CONSTRUCTED)
Definition: asn.h:328
CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &bt, const std::string &str, byte asnTag)
DER encode text string.
Definition: asn.cpp:151
#define b(i, j)
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
Definition: asn.h:403
void BERDecodeError()
Raises a BERDecodeErr.
Definition: asn.h:69
Data structure used to store byte strings.
Definition: queue.h:20
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
Implementation of BufferedTransformation&#39;s attachment interface.
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
Definition: asn.h:426
Definition: asn.h:65
BufferedTransformation & m_outQueue
Definition: asn.h:296
DER Sequence Encoder.
Definition: asn.h:313
OID(word32 v)
Construct an OID.
Definition: asn.h:175
BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag=SEQUENCE|CONSTRUCTED)
Definition: asn.h:308
CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BER decode octet string.
Definition: asn.cpp:117
DERSetEncoder(BufferedTransformation &outQueue, byte asnTag=SET|CONSTRUCTED)
Definition: asn.h:336
DER General Encoder.
Definition: asn.h:284
lword m_lengthRemaining
Definition: asn.h:242
uint8_t const size_t const size
Definition: sha3.h:20
BufferedTransformation & m_inQueue
Definition: asn.h:267
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag=SEQUENCE|CONSTRUCTED)
Definition: asn.h:318
void Load(BufferedTransformation &bt)
BER decode ASN.1 object.
Definition: asn.h:387
Exception thrown when an unknown object identifier is encountered.
Definition: asn.h:72
uint8_t byte
Definition: Common.h:10
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:36
Definition: asn.h:44
virtual lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: cryptlib.cpp:496
#define NAMESPACE_END
Definition: config.h:201
virtual ~OID()
Definition: asn.h:169
std::vector< char * > parameters
Definition: boostTest.cpp:46
CRYPTOPP_DLL size_t CRYPTOPP_API DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
Definition: asn.cpp:17
lword m_length
Definition: asn.h:269
virtual size_t Get(byte &outByte)
Retrieve a 8-bit byte.
Definition: cryptlib.cpp:515
virtual ~EncodedObjectFilter()
Definition: asn.h:217
bool m_finished
Definition: asn.h:297
word64 lword
Definition: config.h:245
ASNTag
ASN.1 types.
Definition: asn.h:27
Definition: asn.h:38
Definition: asn.h:37
bool m_finished
Definition: asn.h:268
Encodes and decodes subjectPublicKeyInfo.
Definition: asn.h:392
#define CRYPTOPP_DLL
Definition: config.h:704
BER General Decoder.
Definition: asn.h:246
unsigned int word32
Definition: config.h:231
void Save(BufferedTransformation &bt) const
DER encode ASN.1 object.
Definition: asn.h:382
unsigned int GetNumberOfCompletedObjects() const
Definition: asn.h:230
Encode and decode ASN.1 objects with additional information.
Definition: asn.h:374
Object Identifier.
Definition: asn.h:166
Definition: asn.h:29
Definition: asn.h:31
bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:562
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Exception thrown when an ASN.1 BER decoing error is encountered.
Definition: cryptlib.h:2931