aboutsummaryrefslogtreecommitdiffstats
path: root/src/x509cert.cpp
blob: 9f8fe85a28e2f202ec55097ab4aa885c2bb6d52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*************************************************
* X.509 Certificates Source File                 *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/x509cert.h>
#include <botan/x509_ext.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/stl_util.h>
#include <botan/parsing.h>
#include <botan/bigint.h>
#include <botan/conf.h>
#include <botan/oids.h>
#include <botan/pem.h>
#include <algorithm>

#include <assert.h>

namespace Botan {

namespace {

/*************************************************
* Get information from the DistinguishedName     *
*************************************************/
void load_info(std::multimap<std::string, std::string>& names,
               const X509_DN& dn_info)
   {
   typedef std::multimap<OID, std::string>::const_iterator rdn_iter;
   std::multimap<OID, std::string> attr = dn_info.get_attributes();

   for(rdn_iter j = attr.begin(); j != attr.end(); ++j)
      {
      const std::string oid_name = OIDS::lookup(j->first);

      if(oid_name == "PKCS9.EmailAddress")
         multimap_insert(names, std::string("RFC822"), j->second);
      else
         multimap_insert(names, oid_name, j->second);
      }
   }

/*************************************************
* Get information from the alternative name      *
*************************************************/
void load_info(std::multimap<std::string, std::string>& names,
               const AlternativeName& alt_info)
   {
   typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;
   std::multimap<std::string, std::string> attr = alt_info.get_attributes();

   for(rdn_iter j = attr.begin(); j != attr.end(); ++j)
      multimap_insert(names, j->first, j->second);

   typedef std::multimap<OID, ASN1_String>::const_iterator on_iter;
   std::multimap<OID, ASN1_String> othernames = alt_info.get_othernames();
   for(on_iter j = othernames.begin(); j != othernames.end(); ++j)
      multimap_insert(names, OIDS::lookup(j->first), j->second.value());
   }

/*************************************************
* Get some information from names                *
*************************************************/
std::string get_info(const std::multimap<std::string, std::string>& names,
                     const std::string& info)
   {
   typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;

   const std::string what = X509_DN::deref_info_field(info);
   std::pair<rdn_iter, rdn_iter> range = names.equal_range(what);

   std::vector<std::string> results;
   for(rdn_iter j = range.first; j != range.second; ++j)
      {
      if(std::find(results.begin(), results.end(), j->second) == results.end())
         results.push_back(j->second);
      }

   std::string value;
   for(u32bit j = 0; j != results.size(); ++j)
      value += results[j] + '/';
   if(value.size())
      value.erase(value.size() - 1, 1);
   return value;
   }

/*************************************************
* Create and populate a X509_DN                  *
*************************************************/
X509_DN create_dn(const std::multimap<std::string, std::string>& names)
   {
   typedef std::multimap<std::string, std::string>::const_iterator rdn_iter;

   X509_DN new_dn;
   for(rdn_iter j = names.begin(); j != names.end(); ++j)
      {
      const std::string oid = j->first;
      const std::string value = j->second;
      if(!OIDS::have_oid(oid))
         continue;
      new_dn.add_attribute(oid, j->second);
      }
   return new_dn;
   }

}

/*************************************************
* X509_Certificate Constructor                   *
*************************************************/
X509_Certificate::X509_Certificate(DataSource& in) :
   X509_Object(in, "CERTIFICATE/X509 CERTIFICATE")
   {
   is_ca = false;
   do_decode();
   }

/*************************************************
* X509_Certificate Constructor                   *
*************************************************/
X509_Certificate::X509_Certificate(const std::string& in) :
   X509_Object(in, "CERTIFICATE/X509 CERTIFICATE")
   {
   is_ca = false;
   do_decode();
   }

/*************************************************
* Decode the TBSCertificate data                 *
*************************************************/
void X509_Certificate::force_decode()
   {
   BER_Decoder tbs_cert(tbs_bits);

   u32bit version;
   tbs_cert.decode_optional(version, ASN1_Tag(0),
                            ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));

   if(version > 2)
      throw Decoding_Error("Unknown X.509 cert version " + to_string(version));
   if(version < 2)
      {
      is_ca = Config::get_bool("x509/v1_assume_ca");
      info.add("X509v3.BasicConstraints.path_constraint", NO_CERT_PATH_LIMIT);
      }

   BigInt serial_bn;
   tbs_cert.decode(serial_bn);

   AlgorithmIdentifier sig_algo_inner;
   tbs_cert.decode(sig_algo_inner);

   if(sig_algo != sig_algo_inner)
      throw Decoding_Error("Algorithm identifier mismatch");

   X509_DN dn_issuer;
   tbs_cert.decode(dn_issuer);
   load_info(issuer, dn_issuer);

   X509_Time start, end;

   tbs_cert.start_cons(SEQUENCE)
      .decode(start)
      .decode(end)
      .verify_end()
   .end_cons();

   X509_DN dn_subject;
   tbs_cert.decode(dn_subject);
   load_info(subject, dn_subject);

   BER_Object public_key = tbs_cert.get_next_object();
   if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED)
      throw BER_Bad_Tag("X509_Certificate: Unexpected tag for public key",
                        public_key.type_tag, public_key.class_tag);

   MemoryVector<byte> v2_issuer_key_id, v2_subject_key_id;

   tbs_cert.decode_optional_string(v2_issuer_key_id, BIT_STRING, 1);
   tbs_cert.decode_optional_string(v2_subject_key_id, BIT_STRING, 2);

   BER_Object v3_exts_data = tbs_cert.get_next_object();
   if(v3_exts_data.type_tag == 3 &&
      v3_exts_data.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC))
      {
      BER_Decoder v3_exts_decoder(v3_exts_data.value);
      BER_Decoder ext_data = v3_exts_decoder.start_cons(SEQUENCE);
      v3_exts_decoder.verify_end();

      while(ext_data.more_items())
         {
         Extension extn;
         ext_data.decode(extn);
         handle_v3_extension(extn);
         }
      ext_data.end_cons();
      }
   else if(v3_exts_data.type_tag != NO_OBJECT)
      throw BER_Bad_Tag("Unknown tag in X.509 cert",
                        v3_exts_data.type_tag, v3_exts_data.class_tag);

   if(tbs_cert.more_items())
      throw Decoding_Error("TBSCertificate has more items that expected");

   info.add("X509.Certificate.version", version);
   info.add("X509.Certificate.serial", BigInt::encode(serial_bn));
   info.add("X509.Certificate.start", start.readable_string());
   info.add("X509.Certificate.end", end.readable_string());

   info.add("X509.Certificate.v2.issuer_key_id", v2_issuer_key_id);
   info.add("X509.Certificate.v2.subject_key_id", v2_subject_key_id);

   info.add("X509.Certificate.public_key",
            PEM_Code::encode(
               ASN1::put_in_sequence(public_key.value),
               "PUBLIC KEY"
               )
      );
   }

/*************************************************
* Decode a particular v3 extension               *
*************************************************/
void X509_Certificate::handle_v3_extension(const Extension& extn)
   {
   BER_Decoder value(extn.value);

   if(extn.oid == OIDS::lookup("X509v3.KeyUsage"))
      {
      Key_Constraints constraints;
      BER::decode(value, constraints);

      if(constraints != NO_CONSTRAINTS)
         info.add("X509v3.KeyUsage", constraints);
      }
   else if(extn.oid == OIDS::lookup("X509v3.ExtendedKeyUsage"))
      {
      BER_Decoder key_usage = value.start_cons(SEQUENCE);
      while(key_usage.more_items())
         {
         OID usage_oid;
         key_usage.decode(usage_oid);
         info.add("X509v3.ExtendedKeyUsage", usage_oid.as_string());
         }
      }
   else if(extn.oid == OIDS::lookup("X509v3.BasicConstraints"))
      {
      u32bit max_path_len = 0;

      value.start_cons(SEQUENCE)
            .decode_optional(is_ca, BOOLEAN, UNIVERSAL, false)
            .decode_optional(max_path_len, INTEGER, UNIVERSAL,
                             NO_CERT_PATH_LIMIT)
            .verify_end()
         .end_cons();

      info.add("X509v3.BasicConstraints.is_ca", is_ca);
      info.add("X509v3.BasicConstraints.path_constraint", max_path_len);
      }
   else if(extn.oid == OIDS::lookup("X509v3.SubjectKeyIdentifier"))
      {
      MemoryVector<byte> v3_subject_key_id;
      value.decode(v3_subject_key_id, OCTET_STRING);
      info.add("X509v3.SubjectKeyIdentifier", v3_subject_key_id);
      }
   else if(extn.oid == OIDS::lookup("X509v3.AuthorityKeyIdentifier"))
      {
      MemoryVector<byte> v3_issuer_key_id;
      BER_Decoder key_id = value.start_cons(SEQUENCE);
      key_id.decode_optional_string(v3_issuer_key_id, OCTET_STRING, 0);

      info.add("X509v3.AuthorityKeyIdentifier", v3_issuer_key_id);
      }
   else if(extn.oid == OIDS::lookup("X509v3.SubjectAlternativeName"))
      {
      AlternativeName alt_name;
      value.decode(alt_name);
      load_info(subject, alt_name);
      }
   else if(extn.oid == OIDS::lookup("X509v3.IssuerAlternativeName"))
      {
      AlternativeName alt_name;
      value.decode(alt_name);
      load_info(issuer, alt_name);
      }
   else if(extn.oid == OIDS::lookup("X509v3.CertificatePolicies"))
      {

      BER_Decoder ber_policies = value.start_cons(SEQUENCE);
      while(ber_policies.more_items())
         {
         OID oid;
         BER_Decoder policy = ber_policies.start_cons(SEQUENCE);
         policy.decode(oid);

         if(extn.critical && policy.more_items())
            throw Decoding_Error("X.509 v3 critical policy has qualifiers");

         info.add("X509v3.CertificatePolicies", oid.as_string());
         }
      }
   else
      {
      if(extn.critical)
         throw Decoding_Error("Unknown critical X.509 v3 extension: " +
                              extn.oid.as_string());
      return;
      }

   value.verify_end();
   }

/*************************************************
* Return the X.509 version in use                *
*************************************************/
u32bit X509_Certificate::x509_version() const
   {
   return (info.get1_u32bit("X509.Certificate.version") + 1);
   }

/*************************************************
* Return the time this cert becomes valid        *
*************************************************/
std::string X509_Certificate::start_time() const
   {
   return info.get1("X509.Certificate.start");
   }

/*************************************************
* Return the time this cert becomes invalid      *
*************************************************/
std::string X509_Certificate::end_time() const
   {
   return info.get1("X509.Certificate.end");
   }

/*************************************************
* Return information about the subject           *
*************************************************/
std::string X509_Certificate::subject_info(const std::string& info) const
   {
   return get_info(subject, info);
   }

/*************************************************
* Return information about the issuer            *
*************************************************/
std::string X509_Certificate::issuer_info(const std::string& info) const
   {
   return get_info(issuer, info);
   }

/*************************************************
* Return the public key in this certificate      *
*************************************************/
X509_PublicKey* X509_Certificate::subject_public_key() const
   {
   DataSource_Memory source(info.get1("X509.Certificate.public_key"));
   return X509::load_key(source);
   }

/*************************************************
* Check if the certificate is self-signed        *
*************************************************/
bool X509_Certificate::self_signed() const
   {
   return (create_dn(issuer) == create_dn(subject));
   }

/*************************************************
* Check if the certificate is for a CA           *
*************************************************/
bool X509_Certificate::is_CA_cert() const
   {
   if(!is_ca) return false;
   if((constraints() & KEY_CERT_SIGN) ||
      (constraints() == NO_CONSTRAINTS))
      return true;
   return false;
   }

/*************************************************
* Return the path length constraint              *
*************************************************/
u32bit X509_Certificate::path_limit() const
   {
   return info.get1_u32bit("X509v3.BasicConstraints.path_constraint");
   }

/*************************************************
* Return the key usage constraints               *
*************************************************/
Key_Constraints X509_Certificate::constraints() const
   {
   return Key_Constraints(info.get1_u32bit("X509v3.KeyUsage"));
   }

/*************************************************
* Return the list of extended key usage OIDs     *
*************************************************/
std::vector<std::string> X509_Certificate::ex_constraints() const
   {
   return info.get("X509v3.ExtendedKeyUsage");
   }

/*************************************************
* Return the list of certificate policies        *
*************************************************/
std::vector<std::string> X509_Certificate::policies() const
   {
   return info.get("X509v3.CertificatePolicies");
   }

/*************************************************
* Return the authority key id                    *
*************************************************/
MemoryVector<byte> X509_Certificate::authority_key_id() const
   {
   return info.get1_memvec("X509v3.AuthorityKeyIdentifier");
   }

/*************************************************
* Return the subject key id                      *
*************************************************/
MemoryVector<byte> X509_Certificate::subject_key_id() const
   {
   return info.get1_memvec("X509v3.SubjectKeyIdentifier");
   }

/*************************************************
* Return the certificate serial number           *
*************************************************/
MemoryVector<byte> X509_Certificate::serial_number() const
   {
   return info.get1_memvec("X509.Certificate.serial");
   }

/*************************************************
* Return the distinguished name of the issuer    *
*************************************************/
X509_DN X509_Certificate::issuer_dn() const
   {
   return create_dn(issuer);
   }

/*************************************************
* Return the distinguished name of the subject   *
*************************************************/
X509_DN X509_Certificate::subject_dn() const
   {
   return create_dn(subject);
   }

/*************************************************
* Compare two certificates for equality          *
*************************************************/
bool X509_Certificate::operator==(const X509_Certificate& cert) const
   {
   if(sig != cert.sig || sig_algo != cert.sig_algo)
      return false;
   if(issuer != cert.issuer || subject != cert.subject)
      return false;
   return (info == cert.info);
   }

/*************************************************
* X.509 Certificate Comparison                   *
*************************************************/
bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2)
   {
   return !(cert1 == cert2);
   }

}