2
Copyright (C) 2000-2007 MySQL AB
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.
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.
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,
19
/* asn.hpp provides ASN1 BER, PublicKey, and x509v3 decoding
23
#ifndef TAO_CRYPT_ASN_HPP
24
#define TAO_CRYPT_ASN_HPP
37
namespace STL = STL_NAMESPACE;
42
// these tags and flags are not complete
50
OBJECT_IDENTIFIER = 0x06,
51
OBJECT_DESCRIPTOR = 0x07,
58
NUMERIC_STRING = 0x12,
59
PRINTABLE_STRING = 0x13,
61
VIDEOTEXT_STRING = 0x15,
64
GENERALIZED_TIME = 0x18,
65
GRAPHIC_STRING = 0x19,
66
VISIBLE_STRING = 0x1a,
67
GENERAL_STRING = 0x1b,
78
CONTEXT_SPECIFIC = 0x80,
85
COMMON_NAME = 0x03, // CN
86
SUR_NAME = 0x04, // SN
87
COUNTRY_NAME = 0x06, // C
88
LOCALITY_NAME = 0x07, // L
89
STATE_NAME = 0x08, // ST
91
ORGUNIT_NAME = 0x0b // OU
97
/* DATA = 1, */ // from ASN1
100
SIGNED_AND_ENVELOPED_DATA = 4,
112
MAX_SEQ_SZ = 5, // enum(seq|con) + length(4)
114
MAX_DIGEST_SZ = 25, // SHA + enum(Bit or Octet) + length(4)
116
ASN_NAME_MAX = 512 // max total of all included names
122
class RSA_PrivateKey;
124
class DSA_PrivateKey;
129
// General BER decoding
130
class BER_Decoder : public virtual_base {
134
explicit BER_Decoder(Source& s) : source_(s) {}
135
virtual ~BER_Decoder() {}
137
Integer& GetInteger(Integer&);
138
word32 GetSequence();
141
word32 GetExplicitVersion();
145
virtual void ReadHeader() = 0;
147
BER_Decoder(const BER_Decoder&); // hide copy
148
BER_Decoder& operator=(const BER_Decoder&); // and assign
152
// RSA Private Key BER Decoder
153
class RSA_Private_Decoder : public BER_Decoder {
155
explicit RSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
156
void Decode(RSA_PrivateKey&);
162
// RSA Public Key BER Decoder
163
class RSA_Public_Decoder : public BER_Decoder {
165
explicit RSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
166
void Decode(RSA_PublicKey&);
172
// DSA Private Key BER Decoder
173
class DSA_Private_Decoder : public BER_Decoder {
175
explicit DSA_Private_Decoder(Source& s) : BER_Decoder(s) {}
176
void Decode(DSA_PrivateKey&);
182
// DSA Public Key BER Decoder
183
class DSA_Public_Decoder : public BER_Decoder {
185
explicit DSA_Public_Decoder(Source& s) : BER_Decoder(s) {}
186
void Decode(DSA_PublicKey&);
192
// DH Key BER Decoder
193
class DH_Decoder : public BER_Decoder {
195
explicit DH_Decoder(Source& s) : BER_Decoder(s) {}
202
// PKCS12 BER Decoder
203
class PKCS12_Decoder : public BER_Decoder {
205
explicit PKCS12_Decoder(Source& s) : BER_Decoder(s) {}
217
explicit PublicKey(const byte* k = 0, word32 s = 0);
218
~PublicKey() { tcArrayDelete(key_); }
220
const byte* GetKey() const { return key_; }
221
word32 size() const { return sz_; }
223
void SetKey(const byte*);
224
void SetSize(word32 s);
226
void AddToEnd(const byte*, word32);
228
PublicKey(const PublicKey&); // hide copy
229
PublicKey& operator=(const PublicKey&); // and assign
233
enum { SHA_SIZE = 20 };
236
// A Signing Authority
239
char name_[ASN_NAME_MAX];
240
byte hash_[SHA_SIZE];
242
Signer(const byte* k, word32 kSz, const char* n, const byte* h);
245
const PublicKey& GetPublicKey() const { return key_; }
246
const char* GetName() const { return name_; }
247
const byte* GetHash() const { return hash_; }
250
Signer(const Signer&); // hide copy
251
Signer& operator=(const Signer&); // and assign
255
typedef STL::list<Signer*> SignerList;
258
enum ContentType { HUH = 651 };
259
enum SigType { SHAwDSA = 517, MD2wRSA = 646, MD5wRSA = 648, SHAwRSA =649};
260
enum HashType { MD2h = 646, MD5h = 649, SHAh = 88 };
261
enum KeyType { DSAk = 515, RSAk = 645 }; // sums of algo OID
264
// an x509v Certificate BER Decoder
265
class CertDecoder : public BER_Decoder {
267
enum DateType { BEFORE, AFTER };
268
enum NameType { ISSUER, SUBJECT };
269
enum CertType { CA, USER };
271
explicit CertDecoder(Source&, bool decode = true, SignerList* sl = 0,
272
bool noVerify = false, CertType ct = USER);
275
const PublicKey& GetPublicKey() const { return key_; }
276
KeyType GetKeyType() const { return KeyType(keyOID_); }
277
const char* GetIssuer() const { return issuer_; }
278
const char* GetCommonName() const { return subject_; }
279
const byte* GetHash() const { return subjectHash_; }
280
const char* GetBeforeDate() const { return beforeDate_; }
281
const char* GetAfterDate() const { return afterDate_; }
286
word32 certBegin_; // offset to start of cert
287
word32 sigIndex_; // offset to start of signature
288
word32 sigLength_; // length of signature
289
word32 signatureOID_; // sum of algorithm object id
290
word32 keyOID_; // sum of key algo object id
291
byte subjectHash_[SHA_SIZE]; // hash of all Names
292
byte issuerHash_[SHA_SIZE]; // hash of all Names
294
char issuer_[ASN_NAME_MAX]; // Names
295
char subject_[ASN_NAME_MAX]; // Names
296
char beforeDate_[MAX_DATE_SZ]; // valid before date
297
char afterDate_[MAX_DATE_SZ]; // valid after date
298
bool verify_; // Default to yes, but could be off
301
void Decode(SignerList*, CertType);
304
bool ValidateSelfSignature();
305
bool ValidateSignature(SignerList*);
306
bool ConfirmSignature(Source&);
308
char* AddTag(char*, const char*, const char*, word32, word32);
309
void GetName(NameType);
311
void GetDate(DateType);
312
void GetCompareHash(const byte*, word32, byte*, word32);
314
word32 GetSignature();
319
word32 GetLength(Source&);
321
word32 SetLength(word32, byte*);
322
word32 SetSequence(word32, byte*);
324
word32 EncodeDSA_Signature(const byte* signature, byte* output);
325
word32 EncodeDSA_Signature(const Integer& r, const Integer& s, byte* output);
326
word32 DecodeDSA_Signature(byte* decoded, const byte* encoded, word32 sz);
329
// General DER encoding
330
class DER_Encoder : public virtual_base {
333
virtual ~DER_Encoder() {}
335
word32 SetAlgoID(HashType, byte*);
337
Error GetError() const { return error_; }
339
//virtual void WriteHeader() = 0;
342
DER_Encoder(const DER_Encoder&); // hide copy
343
DER_Encoder& operator=(const DER_Encoder&); // and assign
348
class Signature_Encoder : public DER_Encoder {
353
explicit Signature_Encoder(const byte*, word32, HashType, Source&);
357
word32 SetDigest(const byte*, word32, byte*);
359
Signature_Encoder(const Signature_Encoder&); // hide copy
360
Signature_Encoder& operator=(const Signature_Encoder&); // and assign
364
// Get Cert in PEM format from BEGIN to END
365
int GetCert(Source&);
367
// Get Cert in PEM format from pkcs12 file
368
int GetPKCS_Cert(const char* password, Source&);
373
#endif // TAO_CRYPT_ASN_HPP