Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/x509/x509cert.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* X.509 Certificates
3
* (C) 1999-2010,2015,2017 Jack Lloyd
4
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/x509cert.h>
10
#include <botan/x509_key.h>
11
#include <botan/pk_keys.h>
12
#include <botan/x509_ext.h>
13
#include <botan/ber_dec.h>
14
#include <botan/internal/parsing.h>
15
#include <botan/bigint.h>
16
#include <botan/oids.h>
17
#include <botan/hash.h>
18
#include <botan/hex.h>
19
#include <algorithm>
20
#include <sstream>
21
22
namespace Botan {
23
24
struct X509_Certificate_Data
25
   {
26
   std::vector<uint8_t> m_serial;
27
   AlgorithmIdentifier m_sig_algo_inner;
28
   X509_DN m_issuer_dn;
29
   X509_DN m_subject_dn;
30
   std::vector<uint8_t> m_issuer_dn_bits;
31
   std::vector<uint8_t> m_subject_dn_bits;
32
   X509_Time m_not_before;
33
   X509_Time m_not_after;
34
   std::vector<uint8_t> m_subject_public_key_bits;
35
   std::vector<uint8_t> m_subject_public_key_bits_seq;
36
   std::vector<uint8_t> m_subject_public_key_bitstring;
37
   std::vector<uint8_t> m_subject_public_key_bitstring_sha1;
38
   AlgorithmIdentifier m_subject_public_key_algid;
39
40
   std::vector<uint8_t> m_v2_issuer_key_id;
41
   std::vector<uint8_t> m_v2_subject_key_id;
42
   Extensions m_v3_extensions;
43
44
   std::vector<OID> m_extended_key_usage;
45
   std::vector<uint8_t> m_authority_key_id;
46
   std::vector<uint8_t> m_subject_key_id;
47
   std::vector<OID> m_cert_policies;
48
49
   std::vector<std::string> m_crl_distribution_points;
50
   std::string m_ocsp_responder;
51
   std::vector<std::string> m_ca_issuers;
52
53
   std::vector<uint8_t> m_issuer_dn_bits_sha256;
54
   std::vector<uint8_t> m_subject_dn_bits_sha256;
55
56
   std::string m_fingerprint_sha1;
57
   std::string m_fingerprint_sha256;
58
59
   AlternativeName m_subject_alt_name;
60
   AlternativeName m_issuer_alt_name;
61
   NameConstraints m_name_constraints;
62
63
   size_t m_version = 0;
64
   size_t m_path_len_constraint = 0;
65
   Key_Constraints m_key_constraints = NO_CONSTRAINTS;
66
   bool m_self_signed = false;
67
   bool m_is_ca_certificate = false;
68
   bool m_serial_negative = false;
69
   };
70
71
std::string X509_Certificate::PEM_label() const
72
10.0k
   {
73
10.0k
   return "CERTIFICATE";
74
10.0k
   }
75
76
std::vector<std::string> X509_Certificate::alternate_PEM_labels() const
77
103
   {
78
103
   return { "X509 CERTIFICATE" };
79
103
   }
80
81
X509_Certificate::X509_Certificate(DataSource& src)
82
15.1k
   {
83
15.1k
   load_data(src);
84
15.1k
   }
85
86
X509_Certificate::X509_Certificate(const std::vector<uint8_t>& vec)
87
0
   {
88
0
   DataSource_Memory src(vec.data(), vec.size());
89
0
   load_data(src);
90
0
   }
91
92
X509_Certificate::X509_Certificate(const uint8_t data[], size_t len)
93
1.32k
   {
94
1.32k
   DataSource_Memory src(data, len);
95
1.32k
   load_data(src);
96
1.32k
   }
97
98
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
99
X509_Certificate::X509_Certificate(const std::string& fsname)
100
0
   {
101
0
   DataSource_Stream src(fsname, true);
102
0
   load_data(src);
103
0
   }
104
#endif
105
106
namespace {
107
108
std::unique_ptr<X509_Certificate_Data> parse_x509_cert_body(const X509_Object& obj)
109
14.2k
   {
110
14.2k
   std::unique_ptr<X509_Certificate_Data> data(new X509_Certificate_Data);
111
112
14.2k
   BigInt serial_bn;
113
14.2k
   BER_Object public_key;
114
14.2k
   BER_Object v3_exts_data;
115
116
14.2k
   BER_Decoder(obj.signed_body())
117
14.2k
      .decode_optional(data->m_version, ASN1_Type(0), ASN1_Class::Constructed | ASN1_Class::ContextSpecific)
118
14.2k
      .decode(serial_bn)
119
14.2k
      .decode(data->m_sig_algo_inner)
120
14.2k
      .decode(data->m_issuer_dn)
121
14.2k
      .start_sequence()
122
14.2k
         .decode(data->m_not_before)
123
14.2k
         .decode(data->m_not_after)
124
14.2k
      .end_cons()
125
14.2k
      .decode(data->m_subject_dn)
126
14.2k
      .get_next(public_key)
127
14.2k
      .decode_optional_string(data->m_v2_issuer_key_id, ASN1_Type::BitString, 1)
128
14.2k
      .decode_optional_string(data->m_v2_subject_key_id, ASN1_Type::BitString, 2)
129
14.2k
      .get_next(v3_exts_data)
130
14.2k
      .verify_end("TBSCertificate has extra data after extensions block");
131
132
14.2k
   if(data->m_version > 2)
133
10
      throw Decoding_Error("Unknown X.509 cert version " + std::to_string(data->m_version));
134
14.2k
   if(obj.signature_algorithm() != data->m_sig_algo_inner)
135
106
      throw Decoding_Error("X.509 Certificate had differing algorithm identifers in inner and outer ID fields");
136
137
14.1k
   public_key.assert_is_a(ASN1_Type::Sequence, ASN1_Class::Constructed, "X.509 certificate public key");
138
139
   // crude method to save the serial's sign; will get lost during decoding, otherwise
140
14.1k
   data->m_serial_negative = serial_bn.is_negative();
141
142
   // for general sanity convert wire version (0 based) to standards version (v1 .. v3)
143
14.1k
   data->m_version += 1;
144
145
14.1k
   data->m_serial = BigInt::encode(serial_bn);
146
14.1k
   data->m_subject_dn_bits = ASN1::put_in_sequence(data->m_subject_dn.get_bits());
147
14.1k
   data->m_issuer_dn_bits = ASN1::put_in_sequence(data->m_issuer_dn.get_bits());
148
149
   // validate_public_key_params(public_key.value);
150
14.1k
   AlgorithmIdentifier public_key_alg_id;
151
14.1k
   BER_Decoder(public_key).decode(public_key_alg_id).discard_remaining();
152
153
14.1k
   const std::vector<std::string> public_key_info =
154
14.1k
      split_on(OIDS::oid2str_or_empty(public_key_alg_id.get_oid()), '/');
155
156
14.1k
   if(!public_key_info.empty() && public_key_info[0] == "RSA")
157
6.67k
      {
158
      // RFC4055: If PublicKeyAlgo = PSS or OAEP: limit the use of the public key exclusively to either RSASSA - PSS or RSAES - OAEP
159
6.67k
      if(public_key_info.size() >= 2)
160
709
         {
161
709
         if(public_key_info[1] == "EMSA4")
162
5
            {
163
            /*
164
            When the RSA private key owner wishes to limit the use of the public
165
            key exclusively to RSASSA-PSS, then the id-RSASSA-PSS object
166
            identifier MUST be used in the algorithm field within the subject
167
            public key information, and, if present, the parameters field MUST
168
            contain RSASSA-PSS-params.
169
170
            All parameters in the signature structure algorithm identifier MUST
171
            match the parameters in the key structure algorithm identifier
172
            except the saltLength field. The saltLength field in the signature parameters
173
            MUST be greater or equal to that in the key parameters field.
174
175
            ToDo: Allow salt length to be greater
176
            */
177
5
            if(public_key_alg_id != obj.signature_algorithm())
178
3
               {
179
3
               throw Decoding_Error("Algorithm identifier mismatch");
180
3
               }
181
5.96k
            }
182
5.96k
         }
183
5.96k
      else
184
5.96k
         {
185
         // oid = rsaEncryption -> parameters field MUST contain NULL
186
5.96k
         if(public_key_alg_id != AlgorithmIdentifier(public_key_alg_id.get_oid(), AlgorithmIdentifier::USE_NULL_PARAM))
187
34
            {
188
34
            throw Decoding_Error("RSA algorithm parameters field MUST contain NULL");
189
34
            }
190
14.1k
         }
191
6.67k
      }
192
193
14.1k
   data->m_subject_public_key_bits.assign(public_key.bits(), public_key.bits() + public_key.length());
194
195
14.1k
   data->m_subject_public_key_bits_seq = ASN1::put_in_sequence(data->m_subject_public_key_bits);
196
197
14.1k
   BER_Decoder(data->m_subject_public_key_bits)
198
14.1k
      .decode(data->m_subject_public_key_algid)
199
14.1k
      .decode(data->m_subject_public_key_bitstring, ASN1_Type::BitString);
200
201
14.1k
   if(v3_exts_data.is_a(3, ASN1_Class::Constructed | ASN1_Class::ContextSpecific))
202
4.81k
      {
203
      // Path validation will reject a v1/v2 cert with v3 extensions
204
4.81k
      BER_Decoder(v3_exts_data).decode(data->m_v3_extensions).verify_end();
205
4.81k
      }
206
9.29k
   else if(v3_exts_data.is_set())
207
12
      {
208
12
          throw BER_Bad_Tag("Unknown tag in X.509 cert", v3_exts_data.tagging());
209
12
      }
210
211
   // Now cache some fields from the extensions
212
14.1k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Key_Usage>())
213
77
      {
214
77
      data->m_key_constraints = ext->get_constraints();
215
      /*
216
      RFC 5280: When the keyUsage extension appears in a certificate,
217
      at least one of the bits MUST be set to 1.
218
      */
219
77
      if(data->m_key_constraints == NO_CONSTRAINTS)
220
10
         {
221
10
         throw Decoding_Error("Certificate has invalid encoding for KeyUsage");
222
10
         }
223
14.0k
      }
224
14.0k
   else
225
14.0k
      {
226
14.0k
      data->m_key_constraints = NO_CONSTRAINTS;
227
14.0k
      }
228
229
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Subject_Key_ID>())
230
269
      {
231
269
      data->m_subject_key_id = ext->get_key_id();
232
269
      }
233
234
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Authority_Key_ID>())
235
424
      {
236
424
      data->m_authority_key_id = ext->get_key_id();
237
424
      }
238
239
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Name_Constraints>())
240
45
      {
241
45
      data->m_name_constraints = ext->get_name_constraints();
242
45
      }
243
244
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Basic_Constraints>())
245
150
      {
246
150
      if(ext->get_is_ca() == true)
247
55
         {
248
         /*
249
         * RFC 5280 section 4.2.1.3 requires that CAs include KeyUsage in all
250
         * intermediate CA certificates they issue. Currently we accept it being
251
         * missing, as do most other implementations. But it may be worth
252
         * removing this entirely, or alternately adding a warning level
253
         * validation failure for it.
254
         */
255
55
         if(data->m_key_constraints == NO_CONSTRAINTS ||
256
22
            (data->m_key_constraints & KEY_CERT_SIGN))
257
43
            {
258
43
            data->m_is_ca_certificate = true;
259
43
            data->m_path_len_constraint = ext->get_path_limit();
260
43
            }
261
55
         }
262
150
      }
263
264
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Issuer_Alternative_Name>())
265
163
      {
266
163
      data->m_issuer_alt_name = ext->get_alt_name();
267
163
      }
268
269
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Subject_Alternative_Name>())
270
166
      {
271
166
      data->m_subject_alt_name = ext->get_alt_name();
272
166
      }
273
274
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Extended_Key_Usage>())
275
88
      {
276
88
      data->m_extended_key_usage = ext->get_oids();
277
88
      }
278
279
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Certificate_Policies>())
280
74
      {
281
74
      data->m_cert_policies = ext->get_policy_oids();
282
74
      }
283
284
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Authority_Information_Access>())
285
43
      {
286
43
      data->m_ocsp_responder = ext->ocsp_responder();
287
43
      data->m_ca_issuers = ext->ca_issuers();
288
43
      }
289
290
14.0k
   if(auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::CRL_Distribution_Points>())
291
81
      {
292
81
      data->m_crl_distribution_points = ext->crl_distribution_urls();
293
81
      }
294
295
   // Check for self-signed vs self-issued certificates
296
14.0k
   if(data->m_subject_dn == data->m_issuer_dn)
297
9.13k
      {
298
9.13k
      if(data->m_subject_key_id.empty() == false && data->m_authority_key_id.empty() == false)
299
81
         {
300
81
         data->m_self_signed = (data->m_subject_key_id == data->m_authority_key_id);
301
81
         }
302
9.05k
      else
303
9.05k
         {
304
         /*
305
         If a parse error or unknown algorithm is encountered, default
306
         to assuming it is self signed. We have no way of being certain but
307
         that is usually the default case (self-issued is rare in practice).
308
         */
309
9.05k
         data->m_self_signed = true;
310
311
9.05k
         try
312
9.05k
            {
313
9.05k
            std::unique_ptr<Public_Key> pub_key(X509::load_key(data->m_subject_public_key_bits_seq));
314
315
9.05k
            Certificate_Status_Code sig_status = obj.verify_signature(*pub_key);
316
317
9.05k
            if(sig_status == Certificate_Status_Code::OK ||
318
2.18k
               sig_status == Certificate_Status_Code::SIGNATURE_ALGO_UNKNOWN)
319
5.31k
               {
320
5.31k
               data->m_self_signed = true;
321
5.31k
               }
322
3.74k
            else
323
3.74k
               {
324
3.74k
               data->m_self_signed = false;
325
3.74k
               }
326
9.05k
            }
327
9.05k
         catch(...)
328
1.56k
            {
329
            // ignore errors here to allow parsing to continue
330
1.56k
            }
331
9.05k
         }
332
9.13k
      }
333
334
14.0k
   const std::vector<uint8_t> full_encoding = obj.BER_encode();
335
336
14.0k
   std::unique_ptr<HashFunction> sha1(HashFunction::create("SHA-1"));
337
14.0k
   if(sha1)
338
11.4k
      {
339
11.4k
      sha1->update(data->m_subject_public_key_bitstring);
340
11.4k
      data->m_subject_public_key_bitstring_sha1 = sha1->final_stdvec();
341
      // otherwise left as empty, and we will throw if subject_public_key_bitstring_sha1 is called
342
343
11.4k
      data->m_fingerprint_sha1 = create_hex_fingerprint(full_encoding, "SHA-1");
344
11.4k
      }
345
346
14.0k
   std::unique_ptr<HashFunction> sha256(HashFunction::create("SHA-256"));
347
14.0k
   if(sha256)
348
11.4k
      {
349
11.4k
      sha256->update(data->m_issuer_dn_bits);
350
11.4k
      data->m_issuer_dn_bits_sha256 = sha256->final_stdvec();
351
352
11.4k
      sha256->update(data->m_subject_dn_bits);
353
11.4k
      data->m_subject_dn_bits_sha256 = sha256->final_stdvec();
354
355
11.4k
      data->m_fingerprint_sha256 = create_hex_fingerprint(full_encoding, "SHA-256");
356
11.4k
      }
357
358
14.0k
   return data;
359
14.0k
   }
360
361
}
362
363
/*
364
* Decode the TBSCertificate data
365
*/
366
void X509_Certificate::force_decode()
367
14.2k
   {
368
14.2k
   m_data.reset();
369
370
14.2k
   std::unique_ptr<X509_Certificate_Data> data = parse_x509_cert_body(*this);
371
372
14.2k
   m_data.reset(data.release());
373
14.2k
   }
374
375
const X509_Certificate_Data& X509_Certificate::data() const
376
47.9k
   {
377
47.9k
   if(m_data == nullptr)
378
0
      {
379
0
      throw Invalid_State("X509_Certificate uninitialized");
380
0
      }
381
47.9k
   return *m_data.get();
382
47.9k
   }
383
384
uint32_t X509_Certificate::x509_version() const
385
1.64k
   {
386
1.64k
   return static_cast<uint32_t>(data().m_version);
387
1.64k
   }
388
389
bool X509_Certificate::is_self_signed() const
390
3.83k
   {
391
3.83k
   return data().m_self_signed;
392
3.83k
   }
393
394
const X509_Time& X509_Certificate::not_before() const
395
0
   {
396
0
   return data().m_not_before;
397
0
   }
398
399
const X509_Time& X509_Certificate::not_after() const
400
0
   {
401
0
   return data().m_not_after;
402
0
   }
403
404
const AlgorithmIdentifier& X509_Certificate::subject_public_key_algo() const
405
0
   {
406
0
   return data().m_subject_public_key_algid;
407
0
   }
408
409
const std::vector<uint8_t>& X509_Certificate::v2_issuer_key_id() const
410
0
   {
411
0
   return data().m_v2_issuer_key_id;
412
0
   }
413
414
const std::vector<uint8_t>& X509_Certificate::v2_subject_key_id() const
415
0
   {
416
0
   return data().m_v2_subject_key_id;
417
0
   }
418
419
const std::vector<uint8_t>& X509_Certificate::subject_public_key_bits() const
420
0
   {
421
0
   return data().m_subject_public_key_bits;
422
0
   }
423
424
const std::vector<uint8_t>& X509_Certificate::subject_public_key_info() const
425
1.54k
   {
426
1.54k
   return data().m_subject_public_key_bits_seq;
427
1.54k
   }
428
429
const std::vector<uint8_t>& X509_Certificate::subject_public_key_bitstring() const
430
0
   {
431
0
   return data().m_subject_public_key_bitstring;
432
0
   }
433
434
const std::vector<uint8_t>& X509_Certificate::subject_public_key_bitstring_sha1() const
435
0
   {
436
0
   if(data().m_subject_public_key_bitstring_sha1.empty())
437
0
      throw Encoding_Error("X509_Certificate::subject_public_key_bitstring_sha1 called but SHA-1 disabled in build");
438
439
0
   return data().m_subject_public_key_bitstring_sha1;
440
0
   }
441
442
const std::vector<uint8_t>& X509_Certificate::authority_key_id() const
443
3.13k
   {
444
3.13k
   return data().m_authority_key_id;
445
3.13k
   }
446
447
const std::vector<uint8_t>& X509_Certificate::subject_key_id() const
448
5.00k
   {
449
5.00k
   return data().m_subject_key_id;
450
5.00k
   }
451
452
const std::vector<uint8_t>& X509_Certificate::serial_number() const
453
0
   {
454
0
   return data().m_serial;
455
0
   }
456
457
bool X509_Certificate::is_serial_negative() const
458
0
   {
459
0
   return data().m_serial_negative;
460
0
   }
461
462
463
const X509_DN& X509_Certificate::issuer_dn() const
464
3.13k
   {
465
3.13k
   return data().m_issuer_dn;
466
3.13k
   }
467
468
const X509_DN& X509_Certificate::subject_dn() const
469
9.49k
   {
470
9.49k
   return data().m_subject_dn;
471
9.49k
   }
472
473
const std::vector<uint8_t>& X509_Certificate::raw_issuer_dn() const
474
0
   {
475
0
   return data().m_issuer_dn_bits;
476
0
   }
477
478
const std::vector<uint8_t>& X509_Certificate::raw_subject_dn() const
479
0
   {
480
0
   return data().m_subject_dn_bits;
481
0
   }
482
483
bool X509_Certificate::is_CA_cert() const
484
0
   {
485
0
   if(data().m_version < 3 && data().m_self_signed)
486
0
      return true;
487
488
0
   return data().m_is_ca_certificate;
489
0
   }
490
491
uint32_t X509_Certificate::path_limit() const
492
0
   {
493
0
   if(data().m_version < 3 && data().m_self_signed)
494
0
      return 32; // in theory infinite, but this is more than enough
495
496
0
   return static_cast<uint32_t>(data().m_path_len_constraint);
497
0
   }
498
499
Key_Constraints X509_Certificate::constraints() const
500
759
   {
501
759
   return data().m_key_constraints;
502
759
   }
503
504
const std::vector<OID>& X509_Certificate::extended_key_usage() const
505
0
   {
506
0
   return data().m_extended_key_usage;
507
0
   }
508
509
const std::vector<OID>& X509_Certificate::certificate_policy_oids() const
510
0
   {
511
0
   return data().m_cert_policies;
512
0
   }
513
514
const NameConstraints& X509_Certificate::name_constraints() const
515
0
   {
516
0
   return data().m_name_constraints;
517
0
   }
518
519
const Extensions& X509_Certificate::v3_extensions() const
520
0
   {
521
0
   return data().m_v3_extensions;
522
0
   }
523
524
bool X509_Certificate::allowed_usage(Key_Constraints usage) const
525
0
   {
526
0
   if(constraints() == NO_CONSTRAINTS)
527
0
      return true;
528
0
   return ((constraints() & usage) == usage);
529
0
   }
530
531
bool X509_Certificate::allowed_extended_usage(const std::string& usage) const
532
0
   {
533
0
   return allowed_extended_usage(OID::from_string(usage));
534
0
   }
535
536
bool X509_Certificate::allowed_extended_usage(const OID& usage) const
537
0
   {
538
0
   const std::vector<OID>& ex = extended_key_usage();
539
0
   if(ex.empty())
540
0
      return true;
541
542
0
   if(std::find(ex.begin(), ex.end(), usage) != ex.end())
543
0
      return true;
544
545
0
   return false;
546
0
   }
547
548
bool X509_Certificate::allowed_usage(Usage_Type usage) const
549
0
   {
550
   // These follow suggestions in RFC 5280 4.2.1.12
551
552
0
   switch(usage)
553
0
      {
554
0
      case Usage_Type::UNSPECIFIED:
555
0
         return true;
556
557
0
      case Usage_Type::TLS_SERVER_AUTH:
558
0
         return (allowed_usage(KEY_AGREEMENT) || allowed_usage(KEY_ENCIPHERMENT) || allowed_usage(DIGITAL_SIGNATURE)) && allowed_extended_usage("PKIX.ServerAuth");
559
560
0
      case Usage_Type::TLS_CLIENT_AUTH:
561
0
         return (allowed_usage(DIGITAL_SIGNATURE) || allowed_usage(KEY_AGREEMENT)) && allowed_extended_usage("PKIX.ClientAuth");
562
563
0
      case Usage_Type::OCSP_RESPONDER:
564
0
         return (allowed_usage(DIGITAL_SIGNATURE) || allowed_usage(NON_REPUDIATION)) && allowed_extended_usage("PKIX.OCSPSigning");
565
566
0
      case Usage_Type::CERTIFICATE_AUTHORITY:
567
0
         return is_CA_cert();
568
569
0
      case Usage_Type::ENCRYPTION:
570
0
         return (allowed_usage(KEY_ENCIPHERMENT) || allowed_usage(DATA_ENCIPHERMENT));
571
0
      }
572
573
0
   return false;
574
0
   }
575
576
bool X509_Certificate::has_constraints(Key_Constraints constraints) const
577
0
   {
578
0
   if(this->constraints() == NO_CONSTRAINTS)
579
0
      {
580
0
      return false;
581
0
      }
582
583
0
   return ((this->constraints() & constraints) != 0);
584
0
   }
585
586
bool X509_Certificate::has_ex_constraint(const OID& usage) const
587
0
   {
588
0
   const std::vector<OID>& ex = extended_key_usage();
589
0
   return (std::find(ex.begin(), ex.end(), usage) != ex.end());
590
0
   }
591
592
/*
593
* Return if a certificate extension is marked critical
594
*/
595
bool X509_Certificate::is_critical(const std::string& ex_name) const
596
0
   {
597
0
   return v3_extensions().critical_extension_set(OID::from_string(ex_name));
598
0
   }
599
600
std::string X509_Certificate::ocsp_responder() const
601
0
   {
602
0
   return data().m_ocsp_responder;
603
0
   }
604
605
std::vector<std::string> X509_Certificate::ca_issuers() const
606
0
   {
607
0
   return data().m_ca_issuers;
608
0
   }
609
610
std::string X509_Certificate::crl_distribution_point() const
611
0
   {
612
   // just returns the first (arbitrarily)
613
0
   if(data().m_crl_distribution_points.size() > 0)
614
0
      return data().m_crl_distribution_points[0];
615
0
   return "";
616
0
   }
617
618
const AlternativeName& X509_Certificate::subject_alt_name() const
619
0
   {
620
0
   return data().m_subject_alt_name;
621
0
   }
622
623
const AlternativeName& X509_Certificate::issuer_alt_name() const
624
0
   {
625
0
   return data().m_issuer_alt_name;
626
0
   }
627
628
/*
629
* Return information about the subject
630
*/
631
std::vector<std::string>
632
X509_Certificate::subject_info(const std::string& req) const
633
0
   {
634
0
   if(req == "Email")
635
0
      return this->subject_info("RFC822");
636
637
0
   if(subject_dn().has_field(req))
638
0
      return subject_dn().get_attribute(req);
639
640
0
   if(subject_alt_name().has_field(req))
641
0
      return subject_alt_name().get_attribute(req);
642
643
0
   return {};
644
0
   }
645
646
/*
647
* Return information about the issuer
648
*/
649
std::vector<std::string>
650
X509_Certificate::issuer_info(const std::string& req) const
651
0
   {
652
0
   if(issuer_dn().has_field(req))
653
0
      return issuer_dn().get_attribute(req);
654
655
0
   if(issuer_alt_name().has_field(req))
656
0
      return issuer_alt_name().get_attribute(req);
657
658
0
   return {};
659
0
   }
660
661
/*
662
* Return the public key in this certificate
663
*/
664
std::unique_ptr<Public_Key> X509_Certificate::load_subject_public_key() const
665
1.54k
   {
666
1.54k
   try
667
1.54k
      {
668
1.54k
      return std::unique_ptr<Public_Key>(X509::load_key(subject_public_key_info()));
669
1.54k
      }
670
653
   catch(std::exception& e)
671
653
      {
672
653
      throw Decoding_Error("X509_Certificate::load_subject_public_key", e);
673
653
      }
674
1.54k
   }
675
676
Public_Key* X509_Certificate::subject_public_key() const
677
1.54k
   {
678
1.54k
   return load_subject_public_key().release();
679
1.54k
   }
680
681
std::vector<uint8_t> X509_Certificate::raw_issuer_dn_sha256() const
682
0
   {
683
0
   if(data().m_issuer_dn_bits_sha256.empty())
684
0
      throw Encoding_Error("X509_Certificate::raw_issuer_dn_sha256 called but SHA-256 disabled in build");
685
0
   return data().m_issuer_dn_bits_sha256;
686
0
   }
687
688
std::vector<uint8_t> X509_Certificate::raw_subject_dn_sha256() const
689
0
   {
690
0
   if(data().m_subject_dn_bits_sha256.empty())
691
0
      throw Encoding_Error("X509_Certificate::raw_subject_dn_sha256 called but SHA-256 disabled in build");
692
0
   return data().m_subject_dn_bits_sha256;
693
0
   }
694
695
std::string X509_Certificate::fingerprint(const std::string& hash_name) const
696
9.70k
   {
697
   /*
698
   * The SHA-1 and SHA-256 fingerprints are precomputed since these
699
   * are the most commonly used. Especially, SHA-256 fingerprints are
700
   * used for cycle detection during path construction.
701
   *
702
   * If SHA-1 or SHA-256 was missing at parsing time the vectors are
703
   * left empty in which case we fall back to create_hex_fingerprint
704
   * which will throw if the hash is unavailable.
705
   */
706
9.70k
   if(hash_name == "SHA-256" && data().m_fingerprint_sha256.size() > 0)
707
9.70k
      return data().m_fingerprint_sha256;
708
0
   else if(hash_name == "SHA-1" && data().m_fingerprint_sha1.size() > 0)
709
0
      return data().m_fingerprint_sha1;
710
0
   else
711
0
      return create_hex_fingerprint(this->BER_encode(), hash_name);
712
9.70k
   }
713
714
bool X509_Certificate::matches_dns_name(const std::string& name) const
715
0
   {
716
0
   if(name.empty())
717
0
      return false;
718
719
0
   std::vector<std::string> issued_names = subject_info("DNS");
720
721
   // Fall back to CN only if no DNS names are set (RFC 6125 sec 6.4.4)
722
0
   if(issued_names.empty())
723
0
      issued_names = subject_info("Name");
724
725
0
   for(size_t i = 0; i != issued_names.size(); ++i)
726
0
      {
727
0
      if(host_wildcard_match(issued_names[i], name))
728
0
         return true;
729
0
      }
730
731
0
   return false;
732
0
   }
733
734
/*
735
* Compare two certificates for equality
736
*/
737
bool X509_Certificate::operator==(const X509_Certificate& other) const
738
944
   {
739
944
   return (this->signature() == other.signature() &&
740
49
           this->signature_algorithm() == other.signature_algorithm() &&
741
43
           this->signed_body() == other.signed_body());
742
944
   }
743
744
bool X509_Certificate::operator<(const X509_Certificate& other) const
745
0
   {
746
   /* If signature values are not equal, sort by lexicographic ordering of that */
747
0
   if(this->signature() != other.signature())
748
0
      {
749
0
      return (this->signature() < other.signature());
750
0
      }
751
752
   // Then compare the signed contents
753
0
   return this->signed_body() < other.signed_body();
754
0
   }
755
756
/*
757
* X.509 Certificate Comparison
758
*/
759
bool operator!=(const X509_Certificate& cert1, const X509_Certificate& cert2)
760
0
   {
761
0
   return !(cert1 == cert2);
762
0
   }
763
764
std::string X509_Certificate::to_string() const
765
0
   {
766
0
   std::ostringstream out;
767
768
0
   out << "Version: " << this->x509_version() << "\n";
769
0
   out << "Subject: " << subject_dn() << "\n";
770
0
   out << "Issuer: " << issuer_dn() << "\n";
771
0
   out << "Issued: " << this->not_before().readable_string() << "\n";
772
0
   out << "Expires: " << this->not_after().readable_string() << "\n";
773
774
0
   out << "Constraints:\n";
775
0
   Key_Constraints constraints = this->constraints();
776
0
   if(constraints == NO_CONSTRAINTS)
777
0
      out << " None\n";
778
0
   else
779
0
      {
780
0
      if(constraints & DIGITAL_SIGNATURE)
781
0
         out << "   Digital Signature\n";
782
0
      if(constraints & NON_REPUDIATION)
783
0
         out << "   Non-Repudiation\n";
784
0
      if(constraints & KEY_ENCIPHERMENT)
785
0
         out << "   Key Encipherment\n";
786
0
      if(constraints & DATA_ENCIPHERMENT)
787
0
         out << "   Data Encipherment\n";
788
0
      if(constraints & KEY_AGREEMENT)
789
0
         out << "   Key Agreement\n";
790
0
      if(constraints & KEY_CERT_SIGN)
791
0
         out << "   Cert Sign\n";
792
0
      if(constraints & CRL_SIGN)
793
0
         out << "   CRL Sign\n";
794
0
      if(constraints & ENCIPHER_ONLY)
795
0
         out << "   Encipher Only\n";
796
0
      if(constraints & DECIPHER_ONLY)
797
0
         out << "   Decipher Only\n";
798
0
      }
799
800
0
   const std::vector<OID>& policies = this->certificate_policy_oids();
801
0
   if(!policies.empty())
802
0
      {
803
0
      out << "Policies: " << "\n";
804
0
      for(auto oid : policies)
805
0
         out << "   " << oid.to_string() << "\n";
806
0
      }
807
808
0
   const std::vector<OID>& ex_constraints = this->extended_key_usage();
809
0
   if(!ex_constraints.empty())
810
0
      {
811
0
      out << "Extended Constraints:\n";
812
0
      for(auto&& oid : ex_constraints)
813
0
         {
814
0
         out << "   " << oid.to_formatted_string() << "\n";
815
0
         }
816
0
      }
817
818
0
   const NameConstraints& name_constraints = this->name_constraints();
819
820
0
   if(!name_constraints.permitted().empty() || !name_constraints.excluded().empty())
821
0
      {
822
0
      out << "Name Constraints:\n";
823
824
0
      if(!name_constraints.permitted().empty())
825
0
         {
826
0
         out << "   Permit";
827
0
         for(auto st: name_constraints.permitted())
828
0
            {
829
0
            out << " " << st.base();
830
0
            }
831
0
         out << "\n";
832
0
         }
833
834
0
      if(!name_constraints.excluded().empty())
835
0
         {
836
0
         out << "   Exclude";
837
0
         for(auto st: name_constraints.excluded())
838
0
            {
839
0
            out << " " << st.base();
840
0
            }
841
0
         out << "\n";
842
0
         }
843
0
      }
844
845
0
   if(!ocsp_responder().empty())
846
0
      out << "OCSP responder " << ocsp_responder() << "\n";
847
848
0
   const std::vector<std::string> ca_issuers = this->ca_issuers();
849
0
   if(!ca_issuers.empty())
850
0
      {
851
0
      out << "CA Issuers:\n";
852
0
      for(size_t i = 0; i != ca_issuers.size(); i++)
853
0
         out << "   URI: " << ca_issuers[i] << "\n";
854
0
      }
855
856
0
   if(!crl_distribution_point().empty())
857
0
      out << "CRL " << crl_distribution_point() << "\n";
858
859
0
   out << "Signature algorithm: " << this->signature_algorithm().get_oid().to_formatted_string() << "\n";
860
861
0
   out << "Serial number: " << hex_encode(this->serial_number()) << "\n";
862
863
0
   if(this->authority_key_id().size())
864
0
     out << "Authority keyid: " << hex_encode(this->authority_key_id()) << "\n";
865
866
0
   if(this->subject_key_id().size())
867
0
     out << "Subject keyid: " << hex_encode(this->subject_key_id()) << "\n";
868
869
0
   try
870
0
      {
871
0
      std::unique_ptr<Public_Key> pubkey(this->subject_public_key());
872
0
      out << "Public Key [" << pubkey->algo_name() << "-" << pubkey->key_length() << "]\n\n";
873
0
      out << X509::PEM_encode(*pubkey);
874
0
      }
875
0
   catch(Decoding_Error&)
876
0
      {
877
0
      const AlgorithmIdentifier& alg_id = this->subject_public_key_algo();
878
0
      out << "Failed to decode key with oid " << alg_id.get_oid().to_string() << "\n";
879
0
      }
880
881
0
   return out.str();
882
0
   }
883
884
}