Coverage Report

Created: 2020-09-16 07:52

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