Counter Strike : Global Offensive Source Code
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.

369 lines
12 KiB

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