Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
12 KiB

  1. // asn.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile asn.h
  4. //! \brief Classes and functions for working with ANS.1 objects
  5. #ifndef CRYPTOPP_ASN_H
  6. #define CRYPTOPP_ASN_H
  7. #include "cryptlib.h"
  8. #include "filters.h"
  9. #include "smartptr.h"
  10. #include "stdcpp.h"
  11. #include "queue.h"
  12. #include "misc.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. // these tags and flags are not complete
  15. enum ASNTag
  16. {
  17. BOOLEAN = 0x01,
  18. INTEGER = 0x02,
  19. BIT_STRING = 0x03,
  20. OCTET_STRING = 0x04,
  21. TAG_NULL = 0x05,
  22. OBJECT_IDENTIFIER = 0x06,
  23. OBJECT_DESCRIPTOR = 0x07,
  24. EXTERNAL = 0x08,
  25. REAL = 0x09,
  26. ENUMERATED = 0x0a,
  27. UTF8_STRING = 0x0c,
  28. SEQUENCE = 0x10,
  29. SET = 0x11,
  30. NUMERIC_STRING = 0x12,
  31. PRINTABLE_STRING = 0x13,
  32. T61_STRING = 0x14,
  33. VIDEOTEXT_STRING = 0x15,
  34. IA5_STRING = 0x16,
  35. UTC_TIME = 0x17,
  36. GENERALIZED_TIME = 0x18,
  37. GRAPHIC_STRING = 0x19,
  38. VISIBLE_STRING = 0x1a,
  39. GENERAL_STRING = 0x1b
  40. };
  41. enum ASNIdFlag
  42. {
  43. UNIVERSAL = 0x00,
  44. // DATA = 0x01,
  45. // HEADER = 0x02,
  46. CONSTRUCTED = 0x20,
  47. APPLICATION = 0x40,
  48. CONTEXT_SPECIFIC = 0x80,
  49. PRIVATE = 0xc0
  50. };
  51. inline void BERDecodeError() {throw BERDecodeErr();}
  52. class CRYPTOPP_DLL UnknownOID : public BERDecodeErr
  53. {
  54. public:
  55. UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
  56. UnknownOID(const char *err) : BERDecodeErr(err) {}
  57. };
  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);
  62. CRYPTOPP_DLL void CRYPTOPP_API DEREncodeNull(BufferedTransformation &out);
  63. CRYPTOPP_DLL void CRYPTOPP_API BERDecodeNull(BufferedTransformation &in);
  64. CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &out, const byte *str, size_t strLen);
  65. CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeOctetString(BufferedTransformation &out, const SecByteBlock &str);
  66. CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &in, SecByteBlock &str);
  67. CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeOctetString(BufferedTransformation &in, BufferedTransformation &str);
  68. // for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
  69. CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeTextString(BufferedTransformation &out, const std::string &str, byte asnTag);
  70. CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeTextString(BufferedTransformation &in, std::string &str, byte asnTag);
  71. CRYPTOPP_DLL size_t CRYPTOPP_API DEREncodeBitString(BufferedTransformation &out, const byte *str, size_t strLen, unsigned int unusedBits=0);
  72. CRYPTOPP_DLL size_t CRYPTOPP_API BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);
  73. // BER decode from source and DER reencode into dest
  74. CRYPTOPP_DLL void CRYPTOPP_API DERReencode(BufferedTransformation &source, BufferedTransformation &dest);
  75. //! Object Identifier
  76. class CRYPTOPP_DLL OID
  77. {
  78. public:
  79. OID() {}
  80. OID(word32 v) : m_values(1, v) {}
  81. OID(BufferedTransformation &bt) {BERDecode(bt);}
  82. inline OID & operator+=(word32 rhs) {m_values.push_back(rhs); return *this;}
  83. void DEREncode(BufferedTransformation &bt) const;
  84. void BERDecode(BufferedTransformation &bt);
  85. // throw BERDecodeErr() if decoded value doesn't equal this OID
  86. void BERDecodeAndCheck(BufferedTransformation &bt) const;
  87. std::vector<word32> m_values;
  88. private:
  89. static void EncodeValue(BufferedTransformation &bt, word32 v);
  90. static size_t DecodeValue(BufferedTransformation &bt, word32 &v);
  91. };
  92. class EncodedObjectFilter : public Filter
  93. {
  94. public:
  95. 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};
  96. EncodedObjectFilter(BufferedTransformation *attachment = NULL, unsigned int nObjects = 1, word32 flags = 0);
  97. void Put(const byte *inString, size_t length);
  98. unsigned int GetNumberOfCompletedObjects() const {return m_nCurrentObject;}
  99. unsigned long GetPositionOfObject(unsigned int i) const {return m_positions[i];}
  100. private:
  101. BufferedTransformation & CurrentTarget();
  102. word32 m_flags;
  103. unsigned int m_nObjects, m_nCurrentObject, m_level;
  104. std::vector<unsigned int> m_positions;
  105. ByteQueue m_queue;
  106. enum State {IDENTIFIER, LENGTH, BODY, TAIL, ALL_DONE} m_state;
  107. byte m_id;
  108. lword m_lengthRemaining;
  109. };
  110. //! BER General Decoder
  111. class CRYPTOPP_DLL BERGeneralDecoder : public Store
  112. {
  113. public:
  114. explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
  115. explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
  116. ~BERGeneralDecoder();
  117. bool IsDefiniteLength() const {return m_definiteLength;}
  118. lword RemainingLength() const {assert(m_definiteLength); return m_length;}
  119. bool EndReached() const;
  120. byte PeekByte() const;
  121. void CheckByte(byte b);
  122. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
  123. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
  124. // call this to denote end of sequence
  125. void MessageEnd();
  126. protected:
  127. BufferedTransformation &m_inQueue;
  128. bool m_finished, m_definiteLength;
  129. lword m_length;
  130. private:
  131. void Init(byte asnTag);
  132. void StoreInitialize(const NameValuePairs &parameters)
  133. {CRYPTOPP_UNUSED(parameters); assert(false);}
  134. lword ReduceLength(lword delta);
  135. };
  136. // GCC (and likely other compilers) identify the explicit DERGeneralEncoder as a copy constructor;
  137. // and not a constructor. We had to remove the default asnTag value to point the compiler in the
  138. // proper direction. We did not break the library or versioning based on the output of
  139. // `nm --demangle libcryptopp.a | grep DERGeneralEncoder::DERGeneralEncoder | grep -v " U "`.
  140. //! DER General Encoder
  141. class CRYPTOPP_DLL DERGeneralEncoder : public ByteQueue
  142. {
  143. public:
  144. #if defined(CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562)
  145. explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
  146. explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
  147. #else
  148. explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag /*= SEQUENCE | CONSTRUCTED*/);
  149. explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag /*= SEQUENCE | CONSTRUCTED*/);
  150. #endif
  151. ~DERGeneralEncoder();
  152. // call this to denote end of sequence
  153. void MessageEnd();
  154. private:
  155. BufferedTransformation &m_outQueue;
  156. bool m_finished;
  157. byte m_asnTag;
  158. };
  159. //! BER Sequence Decoder
  160. class CRYPTOPP_DLL BERSequenceDecoder : public BERGeneralDecoder
  161. {
  162. public:
  163. explicit BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
  164. : BERGeneralDecoder(inQueue, asnTag) {}
  165. explicit BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
  166. : BERGeneralDecoder(inQueue, asnTag) {}
  167. };
  168. //! DER Sequence Encoder
  169. class CRYPTOPP_DLL DERSequenceEncoder : public DERGeneralEncoder
  170. {
  171. public:
  172. explicit DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
  173. : DERGeneralEncoder(outQueue, asnTag) {}
  174. explicit DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
  175. : DERGeneralEncoder(outQueue, asnTag) {}
  176. };
  177. //! BER Set Decoder
  178. class CRYPTOPP_DLL BERSetDecoder : public BERGeneralDecoder
  179. {
  180. public:
  181. explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag = SET | CONSTRUCTED)
  182. : BERGeneralDecoder(inQueue, asnTag) {}
  183. explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag = SET | CONSTRUCTED)
  184. : BERGeneralDecoder(inQueue, asnTag) {}
  185. };
  186. //! DER Set Encoder
  187. class CRYPTOPP_DLL DERSetEncoder : public DERGeneralEncoder
  188. {
  189. public:
  190. explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag = SET | CONSTRUCTED)
  191. : DERGeneralEncoder(outQueue, asnTag) {}
  192. explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag = SET | CONSTRUCTED)
  193. : DERGeneralEncoder(outQueue, asnTag) {}
  194. };
  195. template <class T>
  196. class ASNOptional : public member_ptr<T>
  197. {
  198. public:
  199. void BERDecode(BERSequenceDecoder &seqDecoder, byte tag, byte mask = ~CONSTRUCTED)
  200. {
  201. byte b;
  202. if (seqDecoder.Peek(b) && (b & mask) == tag)
  203. reset(new T(seqDecoder));
  204. }
  205. void DEREncode(BufferedTransformation &out)
  206. {
  207. if (this->get() != NULL)
  208. this->get()->DEREncode(out);
  209. }
  210. };
  211. //! _
  212. template <class BASE>
  213. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ASN1CryptoMaterial : public ASN1Object, public BASE
  214. {
  215. public:
  216. void Save(BufferedTransformation &bt) const
  217. {BEREncode(bt);}
  218. void Load(BufferedTransformation &bt)
  219. {BERDecode(bt);}
  220. };
  221. //! encodes/decodes subjectPublicKeyInfo
  222. class CRYPTOPP_DLL X509PublicKey : public ASN1CryptoMaterial<PublicKey>
  223. {
  224. public:
  225. void BERDecode(BufferedTransformation &bt);
  226. void DEREncode(BufferedTransformation &bt) const;
  227. virtual OID GetAlgorithmID() const =0;
  228. virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
  229. {BERDecodeNull(bt); return false;}
  230. virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
  231. {DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
  232. //! decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
  233. virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
  234. //! encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
  235. virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0;
  236. };
  237. //! encodes/decodes privateKeyInfo
  238. class CRYPTOPP_DLL PKCS8PrivateKey : public ASN1CryptoMaterial<PrivateKey>
  239. {
  240. public:
  241. void BERDecode(BufferedTransformation &bt);
  242. void DEREncode(BufferedTransformation &bt) const;
  243. virtual OID GetAlgorithmID() const =0;
  244. virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
  245. {BERDecodeNull(bt); return false;}
  246. virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
  247. {DEREncodeNull(bt); return false;} // see RFC 2459, section 7.3.1
  248. //! decode privateKey part of privateKeyInfo, without the OCTET STRING header
  249. virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size) =0;
  250. //! encode privateKey part of privateKeyInfo, without the OCTET STRING header
  251. virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0;
  252. //! decode optional attributes including context-specific tag
  253. /*! /note default implementation stores attributes to be output in DEREncodeOptionalAttributes */
  254. virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt);
  255. //! encode optional attributes including context-specific tag
  256. virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const;
  257. protected:
  258. ByteQueue m_optionalAttributes;
  259. };
  260. // ********************************************************
  261. //! DER Encode Unsigned
  262. /*! for INTEGER, BOOLEAN, and ENUM */
  263. template <class T>
  264. size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag = INTEGER)
  265. {
  266. byte buf[sizeof(w)+1];
  267. unsigned int bc;
  268. if (asnTag == BOOLEAN)
  269. {
  270. buf[sizeof(w)] = w ? 0xff : 0;
  271. bc = 1;
  272. }
  273. else
  274. {
  275. buf[0] = 0;
  276. for (unsigned int i=0; i<sizeof(w); i++)
  277. buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
  278. bc = sizeof(w);
  279. while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
  280. --bc;
  281. if (buf[sizeof(w)+1-bc] & 0x80)
  282. ++bc;
  283. }
  284. out.Put(asnTag);
  285. size_t lengthBytes = DERLengthEncode(out, bc);
  286. out.Put(buf+sizeof(w)+1-bc, bc);
  287. return 1+lengthBytes+bc;
  288. }
  289. //! BER Decode Unsigned
  290. template <class T>
  291. void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag = INTEGER,
  292. T minValue = 0, T maxValue = ((std::numeric_limits<T>::max)()))
  293. {
  294. byte b;
  295. if (!in.Get(b) || b != asnTag)
  296. BERDecodeError();
  297. size_t bc;
  298. bool definite = BERLengthDecode(in, bc);
  299. if (!definite)
  300. BERDecodeError();
  301. SecByteBlock buf(bc);
  302. if (bc != in.Get(buf, bc))
  303. BERDecodeError();
  304. const byte *ptr = buf;
  305. while (bc > sizeof(w) && *ptr == 0)
  306. {
  307. bc--;
  308. ptr++;
  309. }
  310. if (bc > sizeof(w))
  311. BERDecodeError();
  312. w = 0;
  313. for (unsigned int i=0; i<bc; i++)
  314. w = (w << 8) | ptr[i];
  315. if (w < minValue || w > maxValue)
  316. BERDecodeError();
  317. }
  318. inline bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
  319. {return lhs.m_values == rhs.m_values;}
  320. inline bool operator!=(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
  321. {return lhs.m_values != rhs.m_values;}
  322. inline bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
  323. {return std::lexicographical_compare(lhs.m_values.begin(), lhs.m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}
  324. inline ::CryptoPP::OID operator+(const ::CryptoPP::OID &lhs, unsigned long rhs)
  325. {return ::CryptoPP::OID(lhs)+=rhs;}
  326. NAMESPACE_END
  327. #endif