Botan 3.6.1
Crypto and TLS for C&
x509_ext.h
Go to the documentation of this file.
1/*
2* X.509 Certificate Extensions
3* (C) 1999-2007,2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_X509_EXTENSIONS_H_
9#define BOTAN_X509_EXTENSIONS_H_
10
11#include <botan/pkix_types.h>
12#include <set>
13
14namespace Botan {
15
16class X509_Certificate;
17
18namespace Cert_Extension {
19
20static const size_t NO_CERT_PATH_LIMIT = 0xFFFFFFF0;
21
22/**
23* Basic Constraints Extension
24*/
26 public:
27 std::unique_ptr<Certificate_Extension> copy() const override {
28 return std::make_unique<Basic_Constraints>(m_is_ca, m_path_limit);
29 }
30
31 Basic_Constraints(bool ca = false, size_t limit = 0) : m_is_ca(ca), m_path_limit(limit) {}
32
33 bool get_is_ca() const { return m_is_ca; }
34
35 size_t get_path_limit() const;
36
37 static OID static_oid() { return OID("2.5.29.19"); }
38
39 OID oid_of() const override { return static_oid(); }
40
41 private:
42 std::string oid_name() const override { return "X509v3.BasicConstraints"; }
43
44 std::vector<uint8_t> encode_inner() const override;
45 void decode_inner(const std::vector<uint8_t>&) override;
46
47 bool m_is_ca;
48 size_t m_path_limit;
49};
50
51/**
52* Key Usage Constraints Extension
53*/
55 public:
56 std::unique_ptr<Certificate_Extension> copy() const override {
57 return std::make_unique<Key_Usage>(m_constraints);
58 }
59
60 explicit Key_Usage(Key_Constraints c) : m_constraints(c) {}
61
62 explicit Key_Usage() : m_constraints(Key_Constraints::None) {}
63
64 Key_Constraints get_constraints() const { return m_constraints; }
65
66 static OID static_oid() { return OID("2.5.29.15"); }
67
68 OID oid_of() const override { return static_oid(); }
69
70 private:
71 std::string oid_name() const override { return "X509v3.KeyUsage"; }
72
73 bool should_encode() const override { return !m_constraints.empty(); }
74
75 std::vector<uint8_t> encode_inner() const override;
76 void decode_inner(const std::vector<uint8_t>&) override;
77
78 Key_Constraints m_constraints;
79};
80
81/**
82* Subject Key Identifier Extension
83*/
85 public:
86 Subject_Key_ID() = default;
87
88 explicit Subject_Key_ID(const std::vector<uint8_t>& k) : m_key_id(k) {}
89
90 Subject_Key_ID(const std::vector<uint8_t>& public_key, std::string_view hash_fn);
91
92 std::unique_ptr<Certificate_Extension> copy() const override {
93 return std::make_unique<Subject_Key_ID>(m_key_id);
94 }
95
96 const std::vector<uint8_t>& get_key_id() const { return m_key_id; }
97
98 static OID static_oid() { return OID("2.5.29.14"); }
99
100 OID oid_of() const override { return static_oid(); }
101
102 private:
103 std::string oid_name() const override { return "X509v3.SubjectKeyIdentifier"; }
104
105 bool should_encode() const override { return (!m_key_id.empty()); }
106
107 std::vector<uint8_t> encode_inner() const override;
108 void decode_inner(const std::vector<uint8_t>&) override;
109
110 std::vector<uint8_t> m_key_id;
111};
112
113/**
114* Authority Key Identifier Extension
115*/
117 public:
118 std::unique_ptr<Certificate_Extension> copy() const override {
119 return std::make_unique<Authority_Key_ID>(m_key_id);
120 }
121
122 Authority_Key_ID() = default;
123
124 explicit Authority_Key_ID(const std::vector<uint8_t>& k) : m_key_id(k) {}
125
126 const std::vector<uint8_t>& get_key_id() const { return m_key_id; }
127
128 static OID static_oid() { return OID("2.5.29.35"); }
129
130 OID oid_of() const override { return static_oid(); }
131
132 private:
133 std::string oid_name() const override { return "X509v3.AuthorityKeyIdentifier"; }
134
135 bool should_encode() const override { return (!m_key_id.empty()); }
136
137 std::vector<uint8_t> encode_inner() const override;
138 void decode_inner(const std::vector<uint8_t>&) override;
139
140 std::vector<uint8_t> m_key_id;
141};
142
143/**
144* Subject Alternative Name Extension
145*/
147 public:
148 const AlternativeName& get_alt_name() const { return m_alt_name; }
149
150 static OID static_oid() { return OID("2.5.29.17"); }
151
152 OID oid_of() const override { return static_oid(); }
153
154 std::unique_ptr<Certificate_Extension> copy() const override {
155 return std::make_unique<Subject_Alternative_Name>(get_alt_name());
156 }
157
159
160 private:
161 std::string oid_name() const override { return "X509v3.SubjectAlternativeName"; }
162
163 bool should_encode() const override { return m_alt_name.has_items(); }
164
165 std::vector<uint8_t> encode_inner() const override;
166 void decode_inner(const std::vector<uint8_t>&) override;
167
168 AlternativeName m_alt_name;
169};
170
171/**
172* Issuer Alternative Name Extension
173*/
175 public:
176 const AlternativeName& get_alt_name() const { return m_alt_name; }
177
178 static OID static_oid() { return OID("2.5.29.18"); }
179
180 OID oid_of() const override { return static_oid(); }
181
182 std::unique_ptr<Certificate_Extension> copy() const override {
183 return std::make_unique<Issuer_Alternative_Name>(get_alt_name());
184 }
185
187
188 private:
189 std::string oid_name() const override { return "X509v3.IssuerAlternativeName"; }
190
191 bool should_encode() const override { return m_alt_name.has_items(); }
192
193 std::vector<uint8_t> encode_inner() const override;
194 void decode_inner(const std::vector<uint8_t>&) override;
195
196 AlternativeName m_alt_name;
197};
198
199/**
200* Extended Key Usage Extension
201*/
203 public:
204 std::unique_ptr<Certificate_Extension> copy() const override {
205 return std::make_unique<Extended_Key_Usage>(m_oids);
206 }
207
209
210 explicit Extended_Key_Usage(const std::vector<OID>& o) : m_oids(o) {}
211
212 const std::vector<OID>& object_identifiers() const { return m_oids; }
213
214 static OID static_oid() { return OID("2.5.29.37"); }
215
216 OID oid_of() const override { return static_oid(); }
217
218 private:
219 std::string oid_name() const override { return "X509v3.ExtendedKeyUsage"; }
220
221 bool should_encode() const override { return (!m_oids.empty()); }
222
223 std::vector<uint8_t> encode_inner() const override;
224 void decode_inner(const std::vector<uint8_t>&) override;
225
226 std::vector<OID> m_oids;
227};
228
229/**
230* Name Constraints
231*/
233 public:
234 std::unique_ptr<Certificate_Extension> copy() const override {
235 return std::make_unique<Name_Constraints>(m_name_constraints);
236 }
237
238 Name_Constraints() = default;
239
240 Name_Constraints(const NameConstraints& nc) : m_name_constraints(nc) {}
241
242 void validate(const X509_Certificate& subject,
243 const X509_Certificate& issuer,
244 const std::vector<X509_Certificate>& cert_path,
245 std::vector<std::set<Certificate_Status_Code>>& cert_status,
246 size_t pos) override;
247
248 const NameConstraints& get_name_constraints() const { return m_name_constraints; }
249
250 static OID static_oid() { return OID("2.5.29.30"); }
251
252 OID oid_of() const override { return static_oid(); }
253
254 private:
255 std::string oid_name() const override { return "X509v3.NameConstraints"; }
256
257 bool should_encode() const override { return true; }
258
259 std::vector<uint8_t> encode_inner() const override;
260 void decode_inner(const std::vector<uint8_t>&) override;
261
262 NameConstraints m_name_constraints;
263};
264
265/**
266* Certificate Policies Extension
267*/
269 public:
270 std::unique_ptr<Certificate_Extension> copy() const override {
271 return std::make_unique<Certificate_Policies>(m_oids);
272 }
273
275
276 explicit Certificate_Policies(const std::vector<OID>& o) : m_oids(o) {}
277
278 const std::vector<OID>& get_policy_oids() const { return m_oids; }
279
280 static OID static_oid() { return OID("2.5.29.32"); }
281
282 OID oid_of() const override { return static_oid(); }
283
284 void validate(const X509_Certificate& subject,
285 const X509_Certificate& issuer,
286 const std::vector<X509_Certificate>& cert_path,
287 std::vector<std::set<Certificate_Status_Code>>& cert_status,
288 size_t pos) override;
289
290 private:
291 std::string oid_name() const override { return "X509v3.CertificatePolicies"; }
292
293 bool should_encode() const override { return (!m_oids.empty()); }
294
295 std::vector<uint8_t> encode_inner() const override;
296 void decode_inner(const std::vector<uint8_t>&) override;
297
298 std::vector<OID> m_oids;
299};
300
301/**
302* Authority Information Access Extension
303*/
305 public:
306 std::unique_ptr<Certificate_Extension> copy() const override {
307 return std::make_unique<Authority_Information_Access>(m_ocsp_responder, m_ca_issuers);
308 }
309
311
312 explicit Authority_Information_Access(std::string_view ocsp,
313 const std::vector<std::string>& ca_issuers = std::vector<std::string>()) :
314 m_ocsp_responder(ocsp), m_ca_issuers(ca_issuers) {}
315
316 std::string ocsp_responder() const { return m_ocsp_responder; }
317
318 static OID static_oid() { return OID("1.3.6.1.5.5.7.1.1"); }
319
320 OID oid_of() const override { return static_oid(); }
321
322 const std::vector<std::string>& ca_issuers() const { return m_ca_issuers; }
323
324 private:
325 std::string oid_name() const override { return "PKIX.AuthorityInformationAccess"; }
326
327 bool should_encode() const override { return (!m_ocsp_responder.empty() || !m_ca_issuers.empty()); }
328
329 std::vector<uint8_t> encode_inner() const override;
330 void decode_inner(const std::vector<uint8_t>&) override;
331
332 std::string m_ocsp_responder;
333 std::vector<std::string> m_ca_issuers;
334};
335
336/**
337* CRL Number Extension
338*/
340 public:
341 std::unique_ptr<Certificate_Extension> copy() const override;
342
343 CRL_Number() : m_has_value(false), m_crl_number(0) {}
344
345 CRL_Number(size_t n) : m_has_value(true), m_crl_number(n) {}
346
347 size_t get_crl_number() const;
348
349 static OID static_oid() { return OID("2.5.29.20"); }
350
351 OID oid_of() const override { return static_oid(); }
352
353 private:
354 std::string oid_name() const override { return "X509v3.CRLNumber"; }
355
356 bool should_encode() const override { return m_has_value; }
357
358 std::vector<uint8_t> encode_inner() const override;
359 void decode_inner(const std::vector<uint8_t>&) override;
360
361 bool m_has_value;
362 size_t m_crl_number;
363};
364
365/**
366* CRL Entry Reason Code Extension
367*/
369 public:
370 std::unique_ptr<Certificate_Extension> copy() const override {
371 return std::make_unique<CRL_ReasonCode>(m_reason);
372 }
373
374 explicit CRL_ReasonCode(CRL_Code r = CRL_Code::Unspecified) : m_reason(r) {}
375
376 CRL_Code get_reason() const { return m_reason; }
377
378 static OID static_oid() { return OID("2.5.29.21"); }
379
380 OID oid_of() const override { return static_oid(); }
381
382 private:
383 std::string oid_name() const override { return "X509v3.ReasonCode"; }
384
385 bool should_encode() const override { return (m_reason != CRL_Code::Unspecified); }
386
387 std::vector<uint8_t> encode_inner() const override;
388 void decode_inner(const std::vector<uint8_t>&) override;
389
390 CRL_Code m_reason;
391};
392
393/**
394* CRL Distribution Points Extension
395* todo enforce restrictions from RFC 5280 4.2.1.13
396*/
398 public:
400 public:
401 void encode_into(DER_Encoder&) const override;
402 void decode_from(BER_Decoder&) override;
403
404 explicit Distribution_Point(const AlternativeName& name = AlternativeName()) : m_point(name) {}
405
406 const AlternativeName& point() const { return m_point; }
407
408 private:
409 AlternativeName m_point;
410 };
411
412 std::unique_ptr<Certificate_Extension> copy() const override {
413 return std::make_unique<CRL_Distribution_Points>(m_distribution_points);
414 }
415
417
418 explicit CRL_Distribution_Points(const std::vector<Distribution_Point>& points) : m_distribution_points(points) {}
419
420 const std::vector<Distribution_Point>& distribution_points() const { return m_distribution_points; }
421
422 const std::vector<std::string>& crl_distribution_urls() const { return m_crl_distribution_urls; }
423
424 static OID static_oid() { return OID("2.5.29.31"); }
425
426 OID oid_of() const override { return static_oid(); }
427
428 private:
429 std::string oid_name() const override { return "X509v3.CRLDistributionPoints"; }
430
431 bool should_encode() const override { return !m_distribution_points.empty(); }
432
433 std::vector<uint8_t> encode_inner() const override;
434 void decode_inner(const std::vector<uint8_t>&) override;
435
436 std::vector<Distribution_Point> m_distribution_points;
437 std::vector<std::string> m_crl_distribution_urls;
438};
439
440/**
441* CRL Issuing Distribution Point Extension
442* todo enforce restrictions from RFC 5280 5.2.5
443*/
445 public:
447
449 m_distribution_point(distribution_point) {}
450
451 std::unique_ptr<Certificate_Extension> copy() const override {
452 return std::make_unique<CRL_Issuing_Distribution_Point>(m_distribution_point);
453 }
454
455 const AlternativeName& get_point() const { return m_distribution_point.point(); }
456
457 static OID static_oid() { return OID("2.5.29.28"); }
458
459 OID oid_of() const override { return static_oid(); }
460
461 private:
462 std::string oid_name() const override { return "X509v3.CRLIssuingDistributionPoint"; }
463
464 bool should_encode() const override { return true; }
465
466 std::vector<uint8_t> encode_inner() const override;
467 void decode_inner(const std::vector<uint8_t>&) override;
468
469 CRL_Distribution_Points::Distribution_Point m_distribution_point;
470};
471
472/**
473* OCSP NoCheck Extension
474*
475* RFC6960 4.2.2.2.1
476* A CA may specify that an OCSP client can trust a responder for the
477* lifetime of the responder's certificate. The CA does so by
478* including the extension id-pkix-ocsp-nocheck.
479*
480* In other words: OCSP responder certificates with this extension do not need
481* to be validated against some revocation info.
482*/
484 public:
485 OCSP_NoCheck() = default;
486
487 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<OCSP_NoCheck>(); }
488
489 static OID static_oid() { return OID("1.3.6.1.5.5.7.48.1.5"); }
490
491 OID oid_of() const override { return static_oid(); }
492
493 private:
494 std::string oid_name() const override { return "PKIX.OCSP.NoCheck"; }
495
496 bool should_encode() const override { return true; }
497
498 std::vector<uint8_t> encode_inner() const override { return {}; }
499
500 void decode_inner(const std::vector<uint8_t>&) override;
501};
502
503/**
504* TNAuthList extension
505*
506* RFC8226 Secure Telephone Identity Credentials
507* https://www.rfc-editor.org/rfc/rfc8226#section-9
508*/
510 public:
511 class Entry final : public ASN1_Object {
512 public:
513 /* TNEntry choice values
514 * see: https://datatracker.ietf.org/doc/html/rfc8226#section-9 */
515 enum Type { ServiceProviderCode = 0, TelephoneNumberRange = 1, TelephoneNumber = 2 };
516
518 ASN1_String start; //TelephoneNumber (IA5String)
519 size_t count; //2..MAX
520 };
521
522 using RangeContainer = std::vector<TelephoneNumberRangeData>;
523 using DataContainer = std::variant<ASN1_String, RangeContainer>;
524
525 void encode_into(DER_Encoder&) const override;
526 void decode_from(class BER_Decoder& from) override;
527
528 Type type() const { return m_type; }
529
530 const std::string& service_provider_code() const {
531 BOTAN_STATE_CHECK(type() == Type::ServiceProviderCode);
532 return std::get<ASN1_String>(m_data).value();
533 }
534
536 BOTAN_STATE_CHECK(type() == Type::TelephoneNumberRange);
537 return std::get<RangeContainer>(m_data);
538 }
539
540 const std::string& telephone_number() const {
541 BOTAN_STATE_CHECK(type() == Type::TelephoneNumber);
542 return std::get<ASN1_String>(m_data).value();
543 }
544
545 private:
546 Type m_type;
547 DataContainer m_data;
548 };
549
550 TNAuthList() = default;
551
552 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<TNAuthList>(*this); }
553
554 static OID static_oid() { return OID("1.3.6.1.5.5.7.1.26"); }
555
556 OID oid_of() const override { return static_oid(); }
557
558 const std::vector<Entry>& entries() const { return m_tn_entries; }
559
560 private:
561 std::string oid_name() const override { return "PKIX.TNAuthList"; }
562
563 bool should_encode() const override { return true; }
564
565 std::vector<uint8_t> encode_inner() const override;
566 void decode_inner(const std::vector<uint8_t>&) override;
567
568 std::vector<Entry> m_tn_entries;
569};
570
571/**
572* An unknown X.509 extension
573* Will add a failure to the path validation result, if critical
574*/
576 public:
577 Unknown_Extension(const OID& oid, bool critical) : m_oid(oid), m_critical(critical) {}
578
579 std::unique_ptr<Certificate_Extension> copy() const override {
580 return std::make_unique<Unknown_Extension>(m_oid, m_critical);
581 }
582
583 /**
584 * Return the OID of this unknown extension
585 */
586 OID oid_of() const override { return m_oid; }
587
588 //static_oid not defined for Unknown_Extension
589
590 /**
591 * Return the extension contents
592 */
593 const std::vector<uint8_t>& extension_contents() const { return m_bytes; }
594
595 /**
596 * Return if this extension was marked critical
597 */
598 bool is_critical_extension() const { return m_critical; }
599
601 const X509_Certificate&,
602 const std::vector<X509_Certificate>&,
603 std::vector<std::set<Certificate_Status_Code>>& cert_status,
604 size_t pos) override {
605 if(m_critical) {
606 cert_status.at(pos).insert(Certificate_Status_Code::UNKNOWN_CRITICAL_EXTENSION);
607 }
608 }
609
610 private:
611 std::string oid_name() const override { return ""; }
612
613 bool should_encode() const override { return true; }
614
615 std::vector<uint8_t> encode_inner() const override;
616 void decode_inner(const std::vector<uint8_t>&) override;
617
618 OID m_oid;
619 bool m_critical;
620 std::vector<uint8_t> m_bytes;
621};
622
623} // namespace Cert_Extension
624
625} // namespace Botan
626
627#endif
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
Authority_Information_Access(std::string_view ocsp, const std::vector< std::string > &ca_issuers=std::vector< std::string >())
Definition x509_ext.h:312
const std::vector< std::string > & ca_issuers() const
Definition x509_ext.h:322
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:306
Authority_Key_ID(const std::vector< uint8_t > &k)
Definition x509_ext.h:124
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:118
const std::vector< uint8_t > & get_key_id() const
Definition x509_ext.h:126
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:27
Basic_Constraints(bool ca=false, size_t limit=0)
Definition x509_ext.h:31
Distribution_Point(const AlternativeName &name=AlternativeName())
Definition x509_ext.h:404
CRL_Distribution_Points(const std::vector< Distribution_Point > &points)
Definition x509_ext.h:418
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:412
const std::vector< std::string > & crl_distribution_urls() const
Definition x509_ext.h:422
const std::vector< Distribution_Point > & distribution_points() const
Definition x509_ext.h:420
const AlternativeName & get_point() const
Definition x509_ext.h:455
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:451
CRL_Issuing_Distribution_Point(const CRL_Distribution_Points::Distribution_Point &distribution_point)
Definition x509_ext.h:448
OID oid_of() const override
Definition x509_ext.h:351
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:370
CRL_ReasonCode(CRL_Code r=CRL_Code::Unspecified)
Definition x509_ext.h:374
const std::vector< OID > & get_policy_oids() const
Definition x509_ext.h:278
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:270
Certificate_Policies(const std::vector< OID > &o)
Definition x509_ext.h:276
const std::vector< OID > & object_identifiers() const
Definition x509_ext.h:212
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:204
Extended_Key_Usage(const std::vector< OID > &o)
Definition x509_ext.h:210
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:182
const AlternativeName & get_alt_name() const
Definition x509_ext.h:176
Issuer_Alternative_Name(const AlternativeName &name=AlternativeName())
Definition x509_ext.h:186
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:56
OID oid_of() const override
Definition x509_ext.h:68
Key_Usage(Key_Constraints c)
Definition x509_ext.h:60
Key_Constraints get_constraints() const
Definition x509_ext.h:64
Name_Constraints(const NameConstraints &nc)
Definition x509_ext.h:240
const NameConstraints & get_name_constraints() const
Definition x509_ext.h:248
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:234
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:487
OID oid_of() const override
Definition x509_ext.h:491
const AlternativeName & get_alt_name() const
Definition x509_ext.h:148
Subject_Alternative_Name(const AlternativeName &name=AlternativeName())
Definition x509_ext.h:158
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:154
Subject_Key_ID(const std::vector< uint8_t > &k)
Definition x509_ext.h:88
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:92
const std::vector< uint8_t > & get_key_id() const
Definition x509_ext.h:96
const std::string & service_provider_code() const
Definition x509_ext.h:530
const std::string & telephone_number() const
Definition x509_ext.h:540
const RangeContainer & telephone_number_range() const
Definition x509_ext.h:535
std::vector< TelephoneNumberRangeData > RangeContainer
Definition x509_ext.h:522
std::variant< ASN1_String, RangeContainer > DataContainer
Definition x509_ext.h:523
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:552
OID oid_of() const override
Definition x509_ext.h:556
const std::vector< Entry > & entries() const
Definition x509_ext.h:558
void validate(const X509_Certificate &, const X509_Certificate &, const std::vector< X509_Certificate > &, std::vector< std::set< Certificate_Status_Code > > &cert_status, size_t pos) override
Definition x509_ext.h:600
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:579
Unknown_Extension(const OID &oid, bool critical)
Definition x509_ext.h:577
const std::vector< uint8_t > & extension_contents() const
Definition x509_ext.h:593
Name Constraints.
Definition pkix_types.h:358
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31