MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
asn.hpp
1 /*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* asn.hpp provides ASN1 BER, PublicKey, and x509v3 decoding
20 */
21 
22 
23 #ifndef TAO_CRYPT_ASN_HPP
24 #define TAO_CRYPT_ASN_HPP
25 
26 
27 #include <time.h>
28 #include "misc.hpp"
29 #include "block.hpp"
30 #include "error.hpp"
31 #ifdef USE_SYS_STL
32  #include <list>
33 #else
34  #include "list.hpp"
35 #endif
36 
37 
38 namespace STL = STL_NAMESPACE;
39 
40 
41 namespace TaoCrypt {
42 
43 // these tags and flags are not complete
44 enum ASNTag
45 {
46  BOOLEAN = 0x01,
47  INTEGER = 0x02,
48  BIT_STRING = 0x03,
49  OCTET_STRING = 0x04,
50  TAG_NULL = 0x05,
51  OBJECT_IDENTIFIER = 0x06,
52  OBJECT_DESCRIPTOR = 0x07,
53  EXTERNAL = 0x08,
54  REAL = 0x09,
55  ENUMERATED = 0x0a,
56  UTF8_STRING = 0x0c,
57  SEQUENCE = 0x10,
58  SET = 0x11,
59  NUMERIC_STRING = 0x12,
60  PRINTABLE_STRING = 0x13,
61  T61_STRING = 0x14,
62  VIDEOTEXT_STRING = 0x15,
63  IA5_STRING = 0x16,
64  UTC_TIME = 0x17,
65  GENERALIZED_TIME = 0x18,
66  GRAPHIC_STRING = 0x19,
67  VISIBLE_STRING = 0x1a,
68  GENERAL_STRING = 0x1b,
69  LONG_LENGTH = 0x80
70 };
71 
72 enum ASNIdFlag
73 {
74  UNIVERSAL = 0x00,
75  DATA = 0x01,
76  HEADER = 0x02,
77  CONSTRUCTED = 0x20,
78  APPLICATION = 0x40,
79  CONTEXT_SPECIFIC = 0x80,
80  PRIVATE = 0xc0
81 };
82 
83 
84 enum DNTags
85 {
86  COMMON_NAME = 0x03, // CN
87  SUR_NAME = 0x04, // SN
88  COUNTRY_NAME = 0x06, // C
89  LOCALITY_NAME = 0x07, // L
90  STATE_NAME = 0x08, // ST
91  ORG_NAME = 0x0a, // O
92  ORGUNIT_NAME = 0x0b // OU
93 };
94 
95 
96 enum PCKS12_Tags
97 {
98  /* DATA = 1, */ // from ASN1
99  SIGNED_DATA = 2,
100  ENVELOPED_DATA = 3,
101  SIGNED_AND_ENVELOPED_DATA = 4,
102  DIGESTED_DATA = 5,
103  ENCRYPTED_DATA = 6
104 };
105 
106 
107 enum Constants
108 {
109  MIN_DATE_SZ = 13,
110  MAX_DATE_SZ = 16,
111  MAX_ALGO_SZ = 16,
112  MAX_LENGTH_SZ = 5,
113  MAX_SEQ_SZ = 5, // enum(seq|con) + length(4)
114  MAX_ALGO_SIZE = 9,
115  MAX_DIGEST_SZ = 69, // SHA512 + enum(Bit or Octet) + length(4)
116  DSA_SIG_SZ = 40,
117  ASN_NAME_MAX = 512 // max total of all included names
118 };
119 
120 
121 class Source;
122 class RSA_PublicKey;
123 class RSA_PrivateKey;
124 class DSA_PublicKey;
125 class DSA_PrivateKey;
126 class Integer;
127 class DH;
128 
129 
130 // General BER decoding
131 class BER_Decoder : public virtual_base {
132 protected:
133  Source& source_;
134 public:
135  explicit BER_Decoder(Source& s) : source_(s) {}
136  virtual ~BER_Decoder() {}
137 
138  Integer& GetInteger(Integer&);
139  word32 GetSequence();
140  word32 GetSet();
141  word32 GetVersion();
142  word32 GetExplicitVersion();
143 
144  Error GetError();
145 private:
146  virtual void ReadHeader() = 0;
147 
148  BER_Decoder(const BER_Decoder&); // hide copy
149  BER_Decoder& operator=(const BER_Decoder&); // and assign
150 };
151 
152 
153 // RSA Private Key BER Decoder
155 public:
156  explicit RSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
157  void Decode(RSA_PrivateKey&);
158 private:
159  void ReadHeader();
160 };
161 
162 
163 // RSA Public Key BER Decoder
165 public:
166  explicit RSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
167  void Decode(RSA_PublicKey&);
168 private:
169  void ReadHeader();
170  void ReadHeaderOpenSSL();
171 };
172 
173 
174 // DSA Private Key BER Decoder
176 public:
177  explicit DSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
178  void Decode(DSA_PrivateKey&);
179 private:
180  void ReadHeader();
181 };
182 
183 
184 // DSA Public Key BER Decoder
186 public:
187  explicit DSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
188  void Decode(DSA_PublicKey&);
189 private:
190  void ReadHeader();
191 };
192 
193 
194 // DH Key BER Decoder
195 class DH_Decoder : public BER_Decoder {
196 public:
197  explicit DH_Decoder(Source& s) : BER_Decoder(s) {}
198  void Decode(DH&);
199 private:
200  void ReadHeader();
201 };
202 
203 
204 // PKCS12 BER Decoder
205 class PKCS12_Decoder : public BER_Decoder {
206 public:
207  explicit PKCS12_Decoder(Source& s) : BER_Decoder(s) {}
208  void Decode();
209 private:
210  void ReadHeader();
211 };
212 
213 
214 // General PublicKey
215 class PublicKey {
216  byte* key_;
217  word32 sz_;
218 public:
219  explicit PublicKey(const byte* k = 0, word32 s = 0);
220  ~PublicKey() { tcArrayDelete(key_); }
221 
222  const byte* GetKey() const { return key_; }
223  word32 size() const { return sz_; }
224 
225  void SetKey(const byte*);
226  void SetSize(word32 s);
227 
228  void AddToEnd(const byte*, word32);
229 private:
230  PublicKey(const PublicKey&); // hide copy
231  PublicKey& operator=(const PublicKey&); // and assign
232 };
233 
234 
235 enum { SHA_SIZE = 20 };
236 
237 
238 // A Signing Authority
239 class Signer {
240  PublicKey key_;
241  char name_[ASN_NAME_MAX];
242  byte hash_[SHA_SIZE];
243 public:
244  Signer(const byte* k, word32 kSz, const char* n, const byte* h);
245  ~Signer();
246 
247  const PublicKey& GetPublicKey() const { return key_; }
248  const char* GetName() const { return name_; }
249  const byte* GetHash() const { return hash_; }
250 
251 private:
252  Signer(const Signer&); // hide copy
253  Signer& operator=(const Signer&); // and assign
254 };
255 
256 
257 typedef STL::list<Signer*> SignerList;
258 
259 
260 enum ContentType { HUH = 651 };
261 enum SigType { SHAwDSA = 517, MD2wRSA = 646, MD5wRSA = 648, SHAwRSA = 649,
262  SHA256wRSA = 655, SHA384wRSA = 656, SHA512wRSA = 657,
263  SHA256wDSA = 416 };
264 enum HashType { MD2h = 646, MD5h = 649, SHAh = 88, SHA256h = 414,
265  SHA384h = 415, SHA512h = 416 };
266 enum KeyType { DSAk = 515, RSAk = 645 }; // sums of algo OID
267 
268 
269 // an x509v Certificate BER Decoder
270 class CertDecoder : public BER_Decoder {
271 public:
272  enum DateType { BEFORE, AFTER };
273  enum NameType { ISSUER, SUBJECT };
274  enum CertType { CA, USER };
275 
276  explicit CertDecoder(Source&, bool decode = true, SignerList* sl = 0,
277  bool noVerify = false, CertType ct = USER);
278  ~CertDecoder();
279 
280  const PublicKey& GetPublicKey() const { return key_; }
281  KeyType GetKeyType() const { return KeyType(keyOID_); }
282  const char* GetIssuer() const { return issuer_; }
283  const char* GetCommonName() const { return subject_; }
284  const byte* GetHash() const { return subjectHash_; }
285  const char* GetBeforeDate() const { return beforeDate_; }
286  byte GetBeforeDateType() const { return beforeDateType_; }
287  const char* GetAfterDate() const { return afterDate_; }
288  byte GetAfterDateType() const { return afterDateType_; }
289 
290  void DecodeToKey();
291 private:
292  PublicKey key_;
293  word32 certBegin_; // offset to start of cert
294  word32 sigIndex_; // offset to start of signature
295  word32 sigLength_; // length of signature
296  word32 signatureOID_; // sum of algorithm object id
297  word32 keyOID_; // sum of key algo object id
298  byte subjectHash_[SHA_SIZE]; // hash of all Names
299  byte issuerHash_[SHA_SIZE]; // hash of all Names
300  byte* signature_;
301  char issuer_[ASN_NAME_MAX]; // Names
302  char subject_[ASN_NAME_MAX]; // Names
303  char beforeDate_[MAX_DATE_SZ]; // valid before date
304  byte beforeDateType_; // beforeDate time type
305  char afterDate_[MAX_DATE_SZ]; // valid after date
306  byte afterDateType_; // afterDate time type
307  bool verify_; // Default to yes, but could be off
308 
309  void ReadHeader();
310  void Decode(SignerList*, CertType);
311  void StoreKey();
312  void AddDSA();
313  bool ValidateSelfSignature();
314  bool ValidateSignature(SignerList*);
315  bool ConfirmSignature(Source&);
316  void GetKey();
317  char* AddTag(char*, const char*, const char*, word32, word32);
318  void GetName(NameType);
319  void GetValidity();
320  void GetDate(DateType);
321  void GetCompareHash(const byte*, word32, byte*, word32);
322  word32 GetAlgoId();
323  word32 GetSignature();
324  word32 GetDigest();
325 };
326 
327 
328 word32 GetLength(Source&);
329 
330 word32 SetLength(word32, byte*);
331 word32 SetSequence(word32, byte*);
332 
333 word32 EncodeDSA_Signature(const byte* signature, byte* output);
334 word32 EncodeDSA_Signature(const Integer& r, const Integer& s, byte* output);
335 word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz);
336 
337 
338 // General DER encoding
339 class DER_Encoder : public virtual_base {
340 public:
341  DER_Encoder() {}
342  virtual ~DER_Encoder() {}
343 
344  word32 SetAlgoID(HashType, byte*);
345 
346  Error GetError() const { return error_; }
347 private:
348  //virtual void WriteHeader() = 0;
349  Error error_;
350 
351  DER_Encoder(const DER_Encoder&); // hide copy
352  DER_Encoder& operator=(const DER_Encoder&); // and assign
353 };
354 
355 
356 
358  const byte* digest_;
359  word32 digestSz_;
360  SigType digestOID_;
361 public:
362  explicit Signature_Encoder(const byte*, word32, HashType, Source&);
363 
364 private:
365  void WriteHeader();
366  word32 SetDigest(const byte*, word32, byte*);
367 
368  Signature_Encoder(const Signature_Encoder&); // hide copy
369  Signature_Encoder& operator=(const Signature_Encoder&); // and assign
370 };
371 
372 
373 // Get Cert in PEM format from BEGIN to END
374 int GetCert(Source&);
375 
376 // Get Cert in PEM format from pkcs12 file
377 int GetPKCS_Cert(const char* password, Source&);
378 
379 bool ASN1_TIME_extract(const unsigned char* date, unsigned char format,
380  tm *parsed_time);
381 
382 } // namespace
383 
384 
385 #endif // TAO_CRYPT_ASN_HPP