Botan 3.14.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* (C) 2024 Anton Einax, Dominik Schricker
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_X509_EXTENSIONS_H_
10#define BOTAN_X509_EXTENSIONS_H_
11
12#include <botan/bigint.h>
13#include <botan/ipv4_address.h>
14#include <botan/ipv6_address.h>
15#include <botan/pkix_types.h>
16#include <botan/telephone_number.h>
17
18#include <array>
19#include <memory>
20#include <optional>
21#include <set>
22#include <span>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <variant>
27#include <vector>
28
29namespace Botan {
30
32
33namespace Cert_Extension {
34
35static const size_t NO_CERT_PATH_LIMIT = 0xFFFFFFF0;
36
37/**
38* Basic Constraints Extension
39*/
41 public:
42 std::unique_ptr<Certificate_Extension> copy() const override {
43 return std::make_unique<Basic_Constraints>(m_is_ca, m_path_length_constraint);
44 }
45
46 BOTAN_FUTURE_EXPLICIT Basic_Constraints(bool is_ca = false, size_t path_length_constraint = 0);
47
48 Basic_Constraints(bool is_ca, std::optional<size_t> path_length_constraint);
49
50 BOTAN_DEPRECATED("Use is_ca") bool get_is_ca() const { return m_is_ca; }
51
52 /**
53 * Note that this function returns NO_CERT_PATH_LIMIT if the value was not set
54 * in the extension.
55 */
56 BOTAN_DEPRECATED("Use path_length_constraint") size_t get_path_limit() const;
57
58 bool is_ca() const { return m_is_ca; }
59
60 std::optional<size_t> path_length_constraint() const { return m_path_length_constraint; }
61
62 static OID static_oid() { return OID({2, 5, 29, 19}); }
63
64 OID oid_of() const override { return static_oid(); }
65
66 private:
67 std::string oid_name() const override { return "X509v3.BasicConstraints"; }
68
69 bool is_appropriate_context(Extension_Context context) const override;
70
71 std::vector<uint8_t> encode_inner() const override;
72 void decode_inner(const std::vector<uint8_t>& in) override;
73
74 bool m_is_ca;
75 std::optional<size_t> m_path_length_constraint;
76};
77
78/**
79* Key Usage Constraints Extension
80*/
82 public:
83 std::unique_ptr<Certificate_Extension> copy() const override {
84 return std::make_unique<Key_Usage>(m_constraints);
85 }
86
87 explicit Key_Usage(Key_Constraints c) : m_constraints(c) {}
88
89 explicit Key_Usage() : m_constraints(Key_Constraints::None) {}
90
91 Key_Constraints get_constraints() const { return m_constraints; }
92
93 static OID static_oid() { return OID({2, 5, 29, 15}); }
94
95 OID oid_of() const override { return static_oid(); }
96
97 private:
98 std::string oid_name() const override { return "X509v3.KeyUsage"; }
99
100 bool is_appropriate_context(Extension_Context context) const override;
101
102 bool should_encode() const override { return !m_constraints.empty(); }
103
104 std::vector<uint8_t> encode_inner() const override;
105 void decode_inner(const std::vector<uint8_t>& in) override;
106
107 Key_Constraints m_constraints;
108};
109
110/**
111* Subject Key Identifier Extension
112*/
114 public:
115 Subject_Key_ID() = default;
116
117 explicit Subject_Key_ID(const std::vector<uint8_t>& k) : m_key_id(k) {}
118
119 /**
120 * Derive the key identifier from the public key, using the first
121 * method given in RFC 5280 4.2.1.2:
122 *
123 * (1) The keyIdentifier is composed of the 160-bit SHA-1 hash of
124 * the value of the BIT STRING subjectPublicKey (excluding the tag,
125 * length, and number of unused bits).
126 */
127 explicit Subject_Key_ID(const Public_Key& pub_key);
128
129 BOTAN_DEPRECATED("Use Subject_Key_ID(const Public_Key&)")
130 Subject_Key_ID(const std::vector<uint8_t>& public_key, std::string_view hash_fn);
131
132 std::unique_ptr<Certificate_Extension> copy() const override {
133 return std::make_unique<Subject_Key_ID>(m_key_id);
134 }
135
136 const std::vector<uint8_t>& get_key_id() const { return m_key_id; }
137
138 static OID static_oid() { return OID({2, 5, 29, 14}); }
139
140 OID oid_of() const override { return static_oid(); }
141
142 private:
143 std::string oid_name() const override { return "X509v3.SubjectKeyIdentifier"; }
144
145 bool is_appropriate_context(Extension_Context context) const override;
146
147 bool should_encode() const override { return (!m_key_id.empty()); }
148
149 std::vector<uint8_t> encode_inner() const override;
150 void decode_inner(const std::vector<uint8_t>& in) override;
151
152 std::vector<uint8_t> m_key_id;
153};
154
155/**
156* Authority Key Identifier Extension
157*/
159 public:
160 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<Authority_Key_ID>(*this); }
161
162 Authority_Key_ID() = default;
163
164 explicit Authority_Key_ID(const std::vector<uint8_t>& k) : m_key_id(k) {}
165
166 /**
167 * The authorityCertIssuer and authorityCertSerialNumber fields, which
168 * identify the certificate holding the authority's signing key.
169 */
174
175 Authority_Key_ID(const std::vector<uint8_t>& k, Authority_Cert_Identifier authority_cert) :
176 m_key_id(k), m_authority_cert(std::move(authority_cert)) {}
177
178 const std::vector<uint8_t>& get_key_id() const { return m_key_id; }
179
180 /**
181 * The authorityCertIssuer/authorityCertSerialNumber fields, if present
182 */
183 const std::optional<Authority_Cert_Identifier>& authority_cert_identifier() const { return m_authority_cert; }
184
185 static OID static_oid() { return OID({2, 5, 29, 35}); }
186
187 OID oid_of() const override { return static_oid(); }
188
189 private:
190 std::string oid_name() const override { return "X509v3.AuthorityKeyIdentifier"; }
191
192 bool is_appropriate_context(Extension_Context context) const override;
193
194 bool should_encode() const override { return !m_key_id.empty() || m_authority_cert.has_value(); }
195
196 std::vector<uint8_t> encode_inner() const override;
197 void decode_inner(const std::vector<uint8_t>& in) override;
198
199 std::vector<uint8_t> m_key_id;
200 std::optional<Authority_Cert_Identifier> m_authority_cert;
201};
202
203/**
204* Subject Alternative Name Extension
205*/
207 public:
208 const AlternativeName& get_alt_name() const { return m_alt_name; }
209
210 static OID static_oid() { return OID({2, 5, 29, 17}); }
211
212 OID oid_of() const override { return static_oid(); }
213
214 std::unique_ptr<Certificate_Extension> copy() const override {
215 return std::make_unique<Subject_Alternative_Name>(get_alt_name());
216 }
217
219
220 private:
221 std::string oid_name() const override { return "X509v3.SubjectAlternativeName"; }
222
223 bool is_appropriate_context(Extension_Context context) const override;
224
225 bool should_encode() const override { return m_alt_name.has_items(); }
226
227 std::vector<uint8_t> encode_inner() const override;
228 void decode_inner(const std::vector<uint8_t>& in) override;
229
230 AlternativeName m_alt_name;
231};
232
233/**
234* Issuer Alternative Name Extension
235*/
237 public:
238 const AlternativeName& get_alt_name() const { return m_alt_name; }
239
240 static OID static_oid() { return OID({2, 5, 29, 18}); }
241
242 OID oid_of() const override { return static_oid(); }
243
244 std::unique_ptr<Certificate_Extension> copy() const override {
245 return std::make_unique<Issuer_Alternative_Name>(get_alt_name());
246 }
247
249
250 private:
251 std::string oid_name() const override { return "X509v3.IssuerAlternativeName"; }
252
253 bool is_appropriate_context(Extension_Context context) const override;
254
255 bool should_encode() const override { return m_alt_name.has_items(); }
256
257 std::vector<uint8_t> encode_inner() const override;
258 void decode_inner(const std::vector<uint8_t>& in) override;
259
260 AlternativeName m_alt_name;
261};
262
263/**
264* Extended Key Usage Extension
265*/
267 public:
268 std::unique_ptr<Certificate_Extension> copy() const override {
269 return std::make_unique<Extended_Key_Usage>(m_oids);
270 }
271
273
274 explicit Extended_Key_Usage(const std::vector<OID>& o) : m_oids(o) {}
275
276 const std::vector<OID>& object_identifiers() const { return m_oids; }
277
278 static OID static_oid() { return OID({2, 5, 29, 37}); }
279
280 OID oid_of() const override { return static_oid(); }
281
282 private:
283 std::string oid_name() const override { return "X509v3.ExtendedKeyUsage"; }
284
285 bool is_appropriate_context(Extension_Context context) const override;
286
287 bool should_encode() const override { return (!m_oids.empty()); }
288
289 std::vector<uint8_t> encode_inner() const override;
290 void decode_inner(const std::vector<uint8_t>& in) override;
291
292 std::vector<OID> m_oids;
293};
294
295/**
296* Name Constraints
297*/
299 public:
300 std::unique_ptr<Certificate_Extension> copy() const override {
301 return std::make_unique<Name_Constraints>(m_name_constraints);
302 }
303
304 Name_Constraints() = default;
305
306 BOTAN_FUTURE_EXPLICIT Name_Constraints(const NameConstraints& nc) : m_name_constraints(nc) {}
307
308 void validate(const X509_Certificate& subject,
309 const std::optional<X509_Certificate>& issuer,
310 const std::vector<X509_Certificate>& cert_path,
311 std::vector<std::set<Certificate_Status_Code>>& cert_status,
312 size_t pos) const override;
313
314 const NameConstraints& get_name_constraints() const { return m_name_constraints; }
315
316 static OID static_oid() { return OID({2, 5, 29, 30}); }
317
318 OID oid_of() const override { return static_oid(); }
319
320 private:
321 std::string oid_name() const override { return "X509v3.NameConstraints"; }
322
323 bool is_appropriate_context(Extension_Context context) const override;
324
325 bool should_encode() const override { return true; }
326
327 std::vector<uint8_t> encode_inner() const override;
328 void decode_inner(const std::vector<uint8_t>& in) override;
329
330 NameConstraints m_name_constraints;
331};
332
333/**
334* Certificate Policies Extension
335*/
337 public:
338 std::unique_ptr<Certificate_Extension> copy() const override {
339 return std::make_unique<Certificate_Policies>(*this);
340 }
341
343
344 explicit Certificate_Policies(const std::vector<OID>& oids);
345
346 const std::vector<OID>& get_policy_oids() const { return m_oids; }
347
348 static OID static_oid() { return OID({2, 5, 29, 32}); }
349
350 OID oid_of() const override { return static_oid(); }
351
352 void validate(const X509_Certificate& subject,
353 const std::optional<X509_Certificate>& issuer,
354 const std::vector<X509_Certificate>& cert_path,
355 std::vector<std::set<Certificate_Status_Code>>& cert_status,
356 size_t pos) const override;
357
358 private:
359 std::string oid_name() const override { return "X509v3.CertificatePolicies"; }
360
361 bool is_appropriate_context(Extension_Context context) const override;
362
363 bool should_encode() const override { return (!m_oids.empty()); }
364
365 std::vector<uint8_t> encode_inner() const override;
366 void decode_inner(const std::vector<uint8_t>& in) override;
367
368 std::vector<OID> m_oids;
369 bool m_has_duplicate = false;
370};
371
372/**
373* Authority Information Access Extension
374*/
376 public:
377 /**
378 * An AccessDescription preserving accessMethod plus a raw view of the
379 * accessLocation GeneralName.
380 *
381 * AccessDescription ::= SEQUENCE {
382 * accessMethod OBJECT IDENTIFIER,
383 * accessLocation GeneralName }
384 */
386 public:
390 std::vector<uint8_t> location_value) :
391 m_method(std::move(method)),
392 m_location_tag(location_tag),
393 m_location_class(location_class),
394 m_location_value(std::move(location_value)) {}
395
396 const OID& access_method() const { return m_method; }
397
398 /**
399 * The GeneralName CHOICE tag (the [n] of the access location).
400 */
401 ASN1_Type location_tag() const { return m_location_tag; }
402
403 ASN1_Class location_class() const { return m_location_class; }
404
405 /**
406 * The raw value bytes of the accessLocation, without the leading
407 * tag/length. For a URI accessLocation this is the IA5String value;
408 * for directoryName it is the DER of the Name.
409 */
410 const std::vector<uint8_t>& location_value() const { return m_location_value; }
411
412 /**
413 * If location is a URI (tag 6 IMPLICIT IA5String), return the string;
414 * nullopt otherwise.
415 */
416 std::optional<std::string> location_as_uri_string() const;
417
418 private:
419 OID m_method;
420 ASN1_Type m_location_tag;
421 ASN1_Class m_location_class;
422 std::vector<uint8_t> m_location_value;
423 };
424
425 std::unique_ptr<Certificate_Extension> copy() const override;
426
428
429 BOTAN_DEPRECATED("Use constructor with list of OCSP responder URIs")
430 explicit Authority_Information_Access(std::string_view ocsp,
431 const std::vector<std::string>& ca_issuers = std::vector<std::string>());
432
433 BOTAN_DEPRECATED("Use constructor that accepts URI types")
434 explicit Authority_Information_Access(const std::vector<std::string>& ocsp_responders,
435 const std::vector<std::string>& ca_issuers = std::vector<std::string>());
436
438 std::vector<URI> ca_issuers = std::vector<URI>());
439
440 /**
441 * Construct an AIA from raw AccessDescriptions, allowing the caller to
442 * emit access methods beyond id-ad-ocsp / id-ad-caIssuers and access
443 * locations beyond URIs. The typed URI accessors (ocsp_responder_uris,
444 * ca_issuer_uris) are also populated from any URI-form entries whose
445 * accessMethod is id-ad-ocsp or id-ad-caIssuers so the two views stay
446 * consistent.
447 */
449
450 /**
451 * Append a single AccessDescription. URI-form id-ad-ocsp / id-ad-caIssuers
452 * entries also populate the corresponding typed URI accessor list.
453 */
455
456 BOTAN_DEPRECATED("Use ocsp_responder_uris") std::string ocsp_responder() const {
457 if(m_ocsp_responders.empty()) {
458 return {};
459 }
460 return m_ocsp_responders[0].original_input();
461 }
462
463 BOTAN_DEPRECATED("Use ocsp_responder_uris") std::vector<std::string> ocsp_responders() const;
464
465 const std::vector<URI>& ocsp_responder_uris() const { return m_ocsp_responders; }
466
467 /**
468 * The full set of AccessDescriptions, including access methods that are
469 * not id-ad-ocsp or id-ad-caIssuers and access locations that are not URIs
470 */
471 const std::vector<AccessDescription>& access_descriptions() const { return m_access_descriptions; }
472
473 static OID static_oid() { return OID({1, 3, 6, 1, 5, 5, 7, 1, 1}); }
474
475 OID oid_of() const override { return static_oid(); }
476
477 BOTAN_DEPRECATED("Use ca_issuer_uris") std::vector<std::string> ca_issuers() const;
478
479 const std::vector<URI>& ca_issuer_uris() const { return m_ca_issuers; }
480
481 private:
482 std::string oid_name() const override { return "PKIX.AuthorityInformationAccess"; }
483
484 bool is_appropriate_context(Extension_Context context) const override;
485
486 bool should_encode() const override {
487 // The URI lists are views into the general AccessDescription list
488 return !m_access_descriptions.empty();
489 }
490
491 std::vector<uint8_t> encode_inner() const override;
492 void decode_inner(const std::vector<uint8_t>& in) override;
493
494 std::vector<URI> m_ocsp_responders;
495 std::vector<URI> m_ca_issuers;
496 std::vector<AccessDescription> m_access_descriptions;
497};
498
499/**
500* CRL Number Extension
501*/
503 public:
504 std::unique_ptr<Certificate_Extension> copy() const override;
505
506 CRL_Number() : m_has_value(false), m_crl_number(BigInt::zero()) {}
507
509
510 explicit CRL_Number(BigInt n);
511
512 const BigInt& crl_number() const;
513
514 BOTAN_DEPRECATED("Use crl_number") size_t get_crl_number() const;
515
516 static OID static_oid() { return OID({2, 5, 29, 20}); }
517
518 OID oid_of() const override { return static_oid(); }
519
520 private:
521 std::string oid_name() const override { return "X509v3.CRLNumber"; }
522
523 bool is_appropriate_context(Extension_Context context) const override;
524
525 bool should_encode() const override { return m_has_value; }
526
527 std::vector<uint8_t> encode_inner() const override;
528 void decode_inner(const std::vector<uint8_t>& in) override;
529
530 bool m_has_value;
531 BigInt m_crl_number;
532};
533
534/**
535* CRL Entry Reason Code Extension
536*/
538 public:
539 std::unique_ptr<Certificate_Extension> copy() const override {
540 return std::make_unique<CRL_ReasonCode>(m_reason);
541 }
542
543 explicit CRL_ReasonCode(CRL_Code r = CRL_Code::Unspecified) : m_reason(r) {}
544
545 CRL_Code get_reason() const { return m_reason; }
546
547 static OID static_oid() { return OID({2, 5, 29, 21}); }
548
549 OID oid_of() const override { return static_oid(); }
550
551 private:
552 std::string oid_name() const override { return "X509v3.ReasonCode"; }
553
554 bool is_appropriate_context(Extension_Context context) const override;
555
556 bool should_encode() const override { return (m_reason != CRL_Code::Unspecified); }
557
558 std::vector<uint8_t> encode_inner() const override;
559 void decode_inner(const std::vector<uint8_t>& in) override;
560
561 CRL_Code m_reason;
562};
563
564/**
565* DistributionPointName used by CRLDistributionPoints and
566* IssuingDistributionPoint (RFC 5280 4.2.1.13 / 5.2.5).
567*
568* DistributionPointName ::= CHOICE {
569* fullName [0] GeneralNames,
570* nameRelativeToCRLIssuer [1] RelativeDistinguishedName }
571*
572* Currently only the fullName CHOICE arm is supported; nameRelativeToCRLIssuer
573* is rejected at decode time.
574*/
576 public:
578
579 explicit DistributionPointName(AlternativeName full_name) : m_full_name(std::move(full_name)) {}
580
581 void encode_into(DER_Encoder& to) const override;
582 void decode_from(BER_Decoder& from) override;
583
584 /**
585 * The fullName GeneralNames
586 *
587 * In the current implementation this will always be set, it returns an
588 * optional to help any future addition of nameRelativeToCRLIssuer, in
589 * which case full_name would return nullopt and another getter would
590 * return the relative name.
591 */
592 const std::optional<AlternativeName>& full_name() const { return m_full_name; }
593
594 private:
595 std::optional<AlternativeName> m_full_name;
596};
597
598/**
599* CRL Distribution Points Extension (RFC 5280 4.2.1.13)
600*/
602 public:
603 /*
604 * DistributionPoint ::= SEQUENCE {
605 * distributionPoint [0] DistributionPointName OPTIONAL,
606 * reasons [1] ReasonFlags OPTIONAL,
607 * cRLIssuer [2] GeneralNames OPTIONAL }
608 */
610 public:
611 void encode_into(DER_Encoder& to) const override;
612 void decode_from(BER_Decoder& from) override;
613
615
617
618 Distribution_Point(std::optional<DistributionPointName> dp_name,
619 std::optional<ReasonFlags> reasons,
620 std::optional<AlternativeName> crl_issuer) :
621 m_dp_name(std::move(dp_name)), m_reasons(reasons), m_crl_issuer(std::move(crl_issuer)) {}
622
623 /**
624 * Return the optional distribution point name
625 */
626 const std::optional<DistributionPointName>& distribution_point_name() const { return m_dp_name; }
627
628 /**
629 * Return the optional reason flags
630 */
631 const std::optional<ReasonFlags>& reasons() const { return m_reasons; }
632
633 /**
634 * Return the optional CRL issuer name
635 */
636 const std::optional<AlternativeName>& crl_issuer() const { return m_crl_issuer; }
637
638 /**
639 * Deprecated compatibility shim. Raises Invalid_State if the distributionPoint
640 * field is absent or the name is a relative name.
641 *
642 * Prefer distribution_point_name(), which surfaces both the OPTIONAL field and
643 * the CHOICE arm explicitly.
644 */
645 BOTAN_DEPRECATED("Use distribution_point_name()") const AlternativeName& point() const;
646
647 private:
648 std::optional<DistributionPointName> m_dp_name;
649 std::optional<ReasonFlags> m_reasons;
650 std::optional<AlternativeName> m_crl_issuer;
651 };
652
653 std::unique_ptr<Certificate_Extension> copy() const override {
654 return std::make_unique<CRL_Distribution_Points>(*this);
655 }
656
658
659 explicit CRL_Distribution_Points(const std::vector<Distribution_Point>& points);
660
661 const std::vector<Distribution_Point>& distribution_points() const { return m_distribution_points; }
662
663 BOTAN_DEPRECATED("Use crl_distribution_point_uris") std::vector<std::string> crl_distribution_urls() const;
664
665 const std::vector<URI>& crl_distribution_point_uris() const { return m_crl_distribution_urls; }
666
667 static OID static_oid() { return OID({2, 5, 29, 31}); }
668
669 OID oid_of() const override { return static_oid(); }
670
671 private:
672 std::string oid_name() const override { return "X509v3.CRLDistributionPoints"; }
673
674 bool is_appropriate_context(Extension_Context context) const override;
675
676 bool should_encode() const override { return !m_distribution_points.empty(); }
677
678 std::vector<uint8_t> encode_inner() const override;
679 void decode_inner(const std::vector<uint8_t>& in) override;
680
681 std::vector<Distribution_Point> m_distribution_points;
682 std::vector<URI> m_crl_distribution_urls;
683};
684
685/**
686* CRL Issuing Distribution Point Extension (RFC 5280 5.2.5)
687*
688* IssuingDistributionPoint ::= SEQUENCE {
689* distributionPoint [0] DistributionPointName OPTIONAL,
690* onlyContainsUserCerts [1] BOOLEAN DEFAULT FALSE,
691* onlyContainsCACerts [2] BOOLEAN DEFAULT FALSE,
692* onlySomeReasons [3] ReasonFlags OPTIONAL,
693* indirectCRL [4] BOOLEAN DEFAULT FALSE,
694* onlyContainsAttributeCerts [5] BOOLEAN DEFAULT FALSE }
695*/
697 public:
699
700 explicit CRL_Issuing_Distribution_Point(DistributionPointName dp_name) : m_dp_name(std::move(dp_name)) {}
701
702 CRL_Issuing_Distribution_Point(std::optional<DistributionPointName> dp_name,
705 std::optional<ReasonFlags> only_some_reasons,
706 bool indirect_crl,
708 m_dp_name(std::move(dp_name)),
709 m_only_contains_user_certs(only_contains_user_certs),
710 m_only_contains_ca_certs(only_contains_ca_certs),
711 m_only_some_reasons(only_some_reasons),
712 m_indirect_crl(indirect_crl),
713 m_only_contains_attribute_certs(only_contains_attribute_certs) {}
714
715 /**
716 * Deprecated compatibility shim for the pre-3.13 API. Extracts the
717 * DistributionPointName from a cert-side Distribution_Point.
718 */
719 BOTAN_DEPRECATED("Use the DistributionPointName constructor")
720 explicit CRL_Issuing_Distribution_Point(const CRL_Distribution_Points::Distribution_Point& distribution_point) :
721 m_dp_name(distribution_point.distribution_point_name()) {}
722
723 std::unique_ptr<Certificate_Extension> copy() const override {
724 return std::make_unique<CRL_Issuing_Distribution_Point>(*this);
725 }
726
727 /**
728 * distributionPoint [0] DistributionPointName OPTIONAL.
729 */
730 const std::optional<DistributionPointName>& distribution_point_name() const { return m_dp_name; }
731
732 bool only_contains_user_certs() const { return m_only_contains_user_certs; }
733
734 bool only_contains_ca_certs() const { return m_only_contains_ca_certs; }
735
736 const std::optional<ReasonFlags>& only_some_reasons() const { return m_only_some_reasons; }
737
738 bool indirect_crl() const { return m_indirect_crl; }
739
740 bool only_contains_attribute_certs() const { return m_only_contains_attribute_certs; }
741
742 /**
743 * Deprecated compatibility shim for the pre-3.13 API. Returns the
744 * fullName GeneralNames; raises Invalid_State if the distributionPoint
745 * field is absent or its CHOICE arm is nameRelativeToCRLIssuer.
746 */
747 BOTAN_DEPRECATED("Use distribution_point_name()") const AlternativeName& get_point() const;
748
749 static OID static_oid() { return OID({2, 5, 29, 28}); }
750
751 OID oid_of() const override { return static_oid(); }
752
753 private:
754 std::string oid_name() const override { return "X509v3.CRLIssuingDistributionPoint"; }
755
756 bool is_appropriate_context(Extension_Context context) const override;
757
758 /**
759 * RFC 5280 5.2.5: "Conforming CRL issuers MUST NOT issue CRLs where
760 * the DER encoding of the issuing distribution point extension is
761 * an empty sequence." Suppress emission when no field is set.
762 */
763 bool should_encode() const override {
764 return m_dp_name.has_value() || m_only_contains_user_certs || m_only_contains_ca_certs ||
765 m_only_some_reasons.has_value() || m_indirect_crl || m_only_contains_attribute_certs;
766 }
767
768 std::vector<uint8_t> encode_inner() const override;
769 void decode_inner(const std::vector<uint8_t>& in) override;
770
771 std::optional<DistributionPointName> m_dp_name;
772 bool m_only_contains_user_certs = false;
773 bool m_only_contains_ca_certs = false;
774 std::optional<ReasonFlags> m_only_some_reasons;
775 bool m_indirect_crl = false;
776 bool m_only_contains_attribute_certs = false;
777};
778
779/**
780* OCSP NoCheck Extension
781*
782* RFC6960 4.2.2.2.1
783* A CA may specify that an OCSP client can trust a responder for the
784* lifetime of the responder's certificate. The CA does so by
785* including the extension id-pkix-ocsp-nocheck.
786*
787* In other words: OCSP responder certificates with this extension do not need
788* to be validated against some revocation info.
789*/
791 public:
792 OCSP_NoCheck() = default;
793
794 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<OCSP_NoCheck>(); }
795
796 static OID static_oid() { return OID({1, 3, 6, 1, 5, 5, 7, 48, 1, 5}); }
797
798 OID oid_of() const override { return static_oid(); }
799
800 void validate(const X509_Certificate& subject,
801 const std::optional<X509_Certificate>& issuer,
802 const std::vector<X509_Certificate>& cert_path,
803 std::vector<std::set<Certificate_Status_Code>>& cert_status,
804 size_t pos) const override;
805
806 private:
807 std::string oid_name() const override { return "PKIX.OCSP.NoCheck"; }
808
809 bool is_appropriate_context(Extension_Context context) const override;
810
811 bool should_encode() const override { return true; }
812
813 std::vector<uint8_t> encode_inner() const override;
814
815 void decode_inner(const std::vector<uint8_t>& in) override;
816};
817
818/**
819* No Revocation Available Extension
820*
821* RFC 9608 Section 2
822*
823* The noRevAvail extension, defined in [X.509-2019-TC2], allows a CA to
824* indicate that no revocation information will be made available for
825* this certificate.
826*
827* This extension MUST NOT be present in CA public key certificates.
828*
829* Conforming CAs MUST include this extension in certificates for which
830* no revocation information will be published. When present,
831* conforming CAs MUST mark this extension as non-critical.
832*/
834 public:
836
837 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<NoRevocationAvailable>(); }
838
839 static OID static_oid() { return OID({2, 5, 29, 56}); }
840
841 OID oid_of() const override { return static_oid(); }
842
843 void validate(const X509_Certificate& subject,
844 const std::optional<X509_Certificate>& issuer,
845 const std::vector<X509_Certificate>& cert_path,
846 std::vector<std::set<Certificate_Status_Code>>& cert_status,
847 size_t pos) const override;
848
849 private:
850 std::string oid_name() const override { return "X509v3.NoRevocationAvailable"; }
851
852 bool is_appropriate_context(Extension_Context context) const override;
853
854 bool should_encode() const override { return true; }
855
856 std::vector<uint8_t> encode_inner() const override;
857 void decode_inner(const std::vector<uint8_t>& in) override;
858};
859
860/**
861* TNAuthList extension
862*
863* RFC 8226 Secure Telephone Identity Credentials: Certificates, Section 9
864*
865* The extension is a list of entries, each of which is a service provider
866* code, a contiguous range of telephone numbers, or a single telephone number.
867*/
869 public:
870 class BOTAN_PUBLIC_API(3, 5) Entry final : public ASN1_Object {
871 public:
872 /**
873 * The TNEntry CHOICE alternative; the values are the context specific tags
874 */
875 enum Type : uint8_t /* NOLINT(*-use-enum-class) */ {
879 };
880
881 /**
882 * Retained for the deprecated telephone_number_range accessor
883 */
888
889 using RangeContainer = std::vector<TelephoneNumberRangeData>;
890 using DataContainer = std::variant<ASN1_String, RangeContainer>;
891
892 /**
893 * Create a service provider code entry, for instance an OCN or SPID
894 *
895 * Throws Invalid_Argument if @p spc is empty or not IA5
896 */
897 static Entry from_service_provider_code(std::string_view spc);
898
899 /**
900 * Create a telephone number range entry
901 *
902 * Throws Invalid_Argument if @p range holds fewer than two numbers
903 */
905
906 /**
907 * Create a single telephone number entry
908 */
910
911 /**
912 * Default constructor; the entry is only meaningful once decoded
913 */
914 Entry() = default;
915
916 void encode_into(DER_Encoder& to) const override;
917 void decode_from(BER_Decoder& from) override;
918
919 Type type() const { return m_type; }
920
921 /**
922 * Return the service provider code; throws unless type() is ServiceProviderCode
923 */
924 const std::string& service_provider_code() const;
925
926 /**
927 * Return the range; throws unless type() is TelephoneNumberRange
928 */
929 const Botan::TelephoneNumberRange& range() const;
930
931 /**
932 * Return the telephone number; throws unless type() is TelephoneNumber
933 */
934 const Botan::TelephoneNumber& number() const;
935
936 /**
937 * Return the range as a single element list; throws unless type() is TelephoneNumberRange
938 */
939 BOTAN_DEPRECATED("Use range()") const RangeContainer& telephone_number_range() const;
940
941 /**
942 * Return the telephone number as a string; throws unless type() is TelephoneNumber
943 */
944 BOTAN_DEPRECATED("Use number()") const std::string& telephone_number() const;
945
946 private:
947 void set_range(Botan::TelephoneNumberRange range);
948
949 Type m_type = ServiceProviderCode;
950 std::variant<std::string, Botan::TelephoneNumber, Botan::TelephoneNumberRange> m_data;
951 // TODO(Botan4) remove along with telephone_number_range()
952 RangeContainer m_legacy_range;
953 };
954
955 /**
956 * Create an empty extension; only useful as a decoding target
957 */
958 TNAuthList() = default;
959
960 /**
961 * Create an extension from a list of entries; throws if @p entries is empty
962 */
963 explicit TNAuthList(std::vector<Entry> entries);
964
965 /**
966 * Decode a bare DER encoded TNAuthorizationList, such as one retrieved
967 * via an id-ad-stirTNList access description (RFC 8226 Section 10.1)
968 */
969 static TNAuthList from_der(std::span<const uint8_t> der);
970
971 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<TNAuthList>(*this); }
972
973 static OID static_oid() { return OID({1, 3, 6, 1, 5, 5, 7, 1, 26}); }
974
975 OID oid_of() const override { return static_oid(); }
976
977 const std::vector<Entry>& entries() const { return m_tn_entries; }
978
979 /**
980 * Return true if every entry of @p delegate lies within the authority of
981 * this list, following RFC 9060 Section 4. Service provider codes must
982 * appear in this list verbatim. If this list contains a service provider
983 * code, all numbers and ranges are accepted, including numbers containing
984 * '*' or '#': the numbers behind a code are not known locally, so the
985 * issuer's assignment checks are trusted. Otherwise, numbers containing
986 * '*' or '#' must appear in this list verbatim, and numeric numbers and
987 * ranges must fall within this list's numbers and ranges.
988 */
989 bool encompasses(const TNAuthList& delegate) const;
990
991 /**
992 * Checks this list against the list of every CA above it in the path,
993 * adding TN_AUTH_LIST_ERROR to this certificate's status if any of
994 * them does not encompass it.
995 */
996 void validate(const X509_Certificate& subject,
997 const std::optional<X509_Certificate>& issuer,
998 const std::vector<X509_Certificate>& cert_path,
999 std::vector<std::set<Certificate_Status_Code>>& cert_status,
1000 size_t pos) const override;
1001
1002 private:
1003 std::string oid_name() const override { return "PKIX.TNAuthList"; }
1004
1005 bool is_appropriate_context(Extension_Context context) const override;
1006
1007 bool should_encode() const override { return !m_tn_entries.empty(); }
1008
1009 std::vector<uint8_t> encode_inner() const override;
1010 void decode_inner(const std::vector<uint8_t>& in) override;
1011
1012 std::vector<Entry> m_tn_entries;
1013};
1014
1015/**
1016 * IP Address Blocks Extension
1017 *
1018 * RFC 3779 X.509 Extensions for IP Addr
1019 *
1020*/
1022 public:
1023 enum class Version : uint8_t {
1024 IPv4 = 4,
1025 IPv6 = 16,
1026 };
1027
1028 /**
1029 * An IP address of the given version
1030 *
1031 * In a future major release this will become an alias of IPv4Address or
1032 * IPv6Address. Use to_bytes and the conversions to/from IPv4Address and
1033 * IPv6Address to write code that works with both.
1034 *
1035 * TODO(Botan4) remove this type
1036 */
1037 template <Version V>
1038 class BOTAN_PUBLIC_API(3, 9) IPAddress final {
1039 static constexpr size_t Length = static_cast<size_t>(V);
1040
1041 using Address = std::conditional_t<V == Version::IPv4, IPv4Address, IPv6Address>;
1042
1043 public:
1044 BOTAN_DEPRECATED("Construct from an IPv4Address or IPv6Address")
1045 explicit IPAddress(std::span<const uint8_t> v);
1046
1047 /**
1048 * Create from an IPv4Address (for IPv4) or IPv6Address (for IPv6)
1049 */
1050 explicit IPAddress(const Address& addr) : m_value(addr.to_bytes()) {}
1051
1052 /**
1053 * Return the address as bytes, network-byte-order
1054 */
1055 BOTAN_DEPRECATED("Use to_bytes") std::array<uint8_t, Length> value() const { return m_value; }
1056
1057 /**
1058 * Return the address as bytes, network-byte-order
1059 */
1060 std::array<uint8_t, Length> to_bytes() const { return m_value; }
1061
1062 /**
1063 * Convert to IPv4Address or IPv6Address
1064 */
1065 // NOLINTNEXTLINE(*-explicit-conversions,*-explicit-constructor)
1066 operator Address() const { return Address(m_value); }
1067
1068 private:
1069 friend class IPAddressBlocks;
1070 IPAddress() = default;
1071
1072 static IPAddress<V> from_bytes(std::span<const uint8_t> v);
1073
1074 // The next address, wrapping to zero after the maximum value
1075 IPAddress<V> successor() const;
1076
1077 BOTAN_DEPRECATED("Will be removed in a future major release")
1078 friend IPAddress<V> operator+(IPAddress<V> lhs, size_t rhs) {
1079 // we only really need to be able to compute +1, so this is fine
1080 for(size_t i = 0; i < rhs; i++) {
1081 lhs = lhs.successor();
1082 }
1083 return lhs;
1084 }
1085
1086 friend std::strong_ordering operator<=>(const IPAddress<V> lhs, const IPAddress<V>& rhs) {
1087 for(size_t i = 0; i < Length; i++) {
1088 if(lhs.m_value[i] < rhs.m_value[i]) {
1089 return std::strong_ordering::less;
1090 } else if(lhs.m_value[i] > rhs.m_value[i]) {
1091 return std::strong_ordering::greater;
1092 }
1093 }
1094 return std::strong_ordering::equal;
1095 }
1096
1097 friend bool operator==(const IPAddress<V>& lhs, const IPAddress<V>& rhs) {
1098 return lhs.m_value == rhs.m_value;
1099 }
1100
1101 std::array<uint8_t, Length> m_value;
1102 };
1103
1104 /*
1105 * TODO(Botan4) remove this type
1106 */
1107 template <Version V>
1108 class BOTAN_PUBLIC_API(3, 9) IPAddressOrRange final : public ASN1_Object {
1109 using Address = std::conditional_t<V == Version::IPv4, IPv4Address, IPv6Address>;
1110 using Subnet = std::conditional_t<V == Version::IPv4, IPv4Subnet, IPv6Subnet>;
1111
1112 public:
1113 void encode_into(DER_Encoder& to) const override;
1114 void decode_from(BER_Decoder& from) override;
1115
1116 IPAddressOrRange() = default;
1117
1118 explicit IPAddressOrRange(const IPAddress<V>& addr) : m_min(addr), m_max(addr) {}
1119
1120 IPAddressOrRange(const IPAddress<V>& min, const IPAddress<V>& max) : m_min(min), m_max(max) {
1121 if(max < min) {
1122 throw Decoding_Error("IP address ranges must be sorted");
1123 }
1124 }
1125
1126 /**
1127 * Create a range containing a single address (IPv4Address or IPv6Address)
1128 */
1129 explicit IPAddressOrRange(const Address& addr) : IPAddressOrRange(addr, addr) {}
1130
1131 /**
1132 * Create a range of addresses (IPv4Address or IPv6Address) from min to max (inclusive)
1133 */
1134 IPAddressOrRange(const Address& min, const Address& max) :
1136
1137 /**
1138 * Create a range covering exactly the addresses of a subnet (IPv4Subnet or IPv6Subnet)
1139 */
1140 explicit IPAddressOrRange(const Subnet& subnet) :
1141 IPAddressOrRange(subnet.address(), subnet.last_address()) {}
1142
1143 IPAddress<V> min() const { return m_min; }
1144
1145 IPAddress<V> max() const { return m_max; }
1146
1147 /**
1148 * If this range covers exactly the addresses of some subnet, return it.
1149 * Otherwise return nullopt.
1150 */
1151 std::optional<Subnet> as_subnet() const;
1152
1153 private:
1154 IPAddress<V> m_min{};
1155 IPAddress<V> m_max{};
1156
1157 IPAddress<V> decode_single_address(const ASN1_BitString& decoded, bool min);
1158 };
1159
1160 template <Version V>
1161 class BOTAN_PUBLIC_API(3, 9) IPAddressChoice final : public ASN1_Object {
1162 public:
1163 void encode_into(DER_Encoder& to) const override;
1164 void decode_from(BER_Decoder& from) override;
1165
1166 const std::optional<std::vector<IPAddressOrRange<V>>>& ranges() const { return m_ip_addr_ranges; }
1167
1168 IPAddressChoice() = default;
1169
1170 explicit IPAddressChoice(std::optional<std::span<const IPAddressOrRange<V>>> ranges);
1171
1172 private:
1173 std::optional<std::vector<IPAddressOrRange<V>>> m_ip_addr_ranges;
1174 };
1175
1176 class BOTAN_PUBLIC_API(3, 9) IPAddressFamily final : public ASN1_Object {
1177 public:
1178 typedef std::variant<IPAddressChoice<Version::IPv4>, IPAddressChoice<Version::IPv6>> AddrChoice;
1179
1180 void encode_into(DER_Encoder& to) const override;
1181 void decode_from(BER_Decoder& from) override;
1182
1183 IPAddressFamily() = default;
1184
1185 explicit IPAddressFamily(const AddrChoice& choice, std::optional<uint8_t> safi = std::nullopt) :
1186 m_safi(safi), m_ip_addr_choice(choice) {
1187 if(std::holds_alternative<IPAddressChoice<Version::IPv4>>(choice)) {
1188 m_afi = 1;
1189 } else {
1190 m_afi = 2;
1191 }
1192 }
1193
1194 uint16_t afi() const { return m_afi; }
1195
1196 std::optional<uint8_t> safi() const { return m_safi; }
1197
1198 const AddrChoice& addr_choice() const { return m_ip_addr_choice; }
1199
1200 private:
1201 uint16_t m_afi = 1;
1202 std::optional<uint8_t> m_safi;
1203 AddrChoice m_ip_addr_choice;
1204 };
1205
1206 IPAddressBlocks() = default;
1207
1208 explicit IPAddressBlocks(const std::vector<IPAddressFamily>& blocks) : m_ip_addr_blocks(blocks) {
1209 this->sort_and_merge();
1210 }
1211
1212 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<IPAddressBlocks>(*this); }
1213
1214 static OID static_oid() { return OID({1, 3, 6, 1, 5, 5, 7, 1, 7}); }
1215
1216 OID oid_of() const override { return static_oid(); }
1217
1218 void validate(const X509_Certificate& subject,
1219 const std::optional<X509_Certificate>& issuer,
1220 const std::vector<X509_Certificate>& cert_path,
1221 std::vector<std::set<Certificate_Status_Code>>& cert_status,
1222 size_t pos) const override;
1223
1224 /// Add a single IPv4 address to this extension (for the specified SAFI, if any)
1225 void add_address(const IPv4Address& address, std::optional<uint8_t> safi = std::nullopt) {
1226 add_address(address, address, safi);
1227 }
1228
1229 /// Add a single IPv6 address to this extension (for the specified SAFI, if any)
1230 void add_address(const IPv6Address& address, std::optional<uint8_t> safi = std::nullopt) {
1231 add_address(address, address, safi);
1232 }
1233
1234 /// Add an IPv4 address range to this extension (for the specified SAFI, if any)
1235 void add_address(const IPv4Address& min, const IPv4Address& max, std::optional<uint8_t> safi = std::nullopt) {
1236 add_range(IPAddressOrRange<Version::IPv4>(min, max), safi);
1237 }
1238
1239 /// Add an IPv6 address range to this extension (for the specified SAFI, if any)
1240 void add_address(const IPv6Address& min, const IPv6Address& max, std::optional<uint8_t> safi = std::nullopt) {
1241 add_range(IPAddressOrRange<Version::IPv6>(min, max), safi);
1242 }
1243
1244 /// Add an IPv4 subnet to this extension (for the specified SAFI, if any)
1245 void add_address(const IPv4Subnet& subnet, std::optional<uint8_t> safi = std::nullopt) {
1246 add_range(IPAddressOrRange<Version::IPv4>(subnet), safi);
1247 }
1248
1249 /// Add an IPv6 subnet to this extension (for the specified SAFI, if any)
1250 void add_address(const IPv6Subnet& subnet, std::optional<uint8_t> safi = std::nullopt) {
1251 add_range(IPAddressOrRange<Version::IPv6>(subnet), safi);
1252 }
1253
1254 /// Add a single IP address to this extension (for the specified SAFI, if any)
1255 template <Version V>
1256 BOTAN_DEPRECATED("Use add_address taking an IPv4Address or IPv6Address")
1257 void add_address(const std::array<uint8_t, static_cast<size_t>(V)>& address,
1258 std::optional<uint8_t> safi = std::nullopt) {
1259 add_range(IPAddressOrRange<V>(IPAddress<V>::from_bytes(address)), safi);
1260 }
1261
1262 /// Add an IP address range to this extension (for the specified SAFI, if any)
1263 template <Version V>
1264 BOTAN_DEPRECATED("Use add_address taking an IPv4Address or IPv6Address")
1265 void add_address(const std::array<uint8_t, static_cast<std::size_t>(V)>& min,
1266 const std::array<uint8_t, static_cast<std::size_t>(V)>& max,
1267 std::optional<uint8_t> safi = std::nullopt) {
1268 add_range(IPAddressOrRange<V>(IPAddress<V>::from_bytes(min), IPAddress<V>::from_bytes(max)), safi);
1269 }
1270
1271 /// Make the extension contain no allowed IP addresses for the specified IP version (and SAFI, if any)
1272 template <Version V>
1273 void restrict(std::optional<uint8_t> safi = std::nullopt) {
1274 std::vector<IPAddressOrRange<V>> addresses = {};
1275 m_ip_addr_blocks.push_back(IPAddressFamily(IPAddressChoice<V>(addresses), safi));
1276 sort_and_merge();
1277 }
1278
1279 /// Mark the specified IP version as 'inherit' (for the specified SAFI, if any)
1280 template <Version V>
1281 void inherit(std::optional<uint8_t> safi = std::nullopt) {
1282 m_ip_addr_blocks.push_back(IPAddressFamily(IPAddressChoice<V>(), safi));
1283 sort_and_merge();
1284 }
1285
1286 const std::vector<IPAddressFamily>& addr_blocks() const { return m_ip_addr_blocks; }
1287
1288 /// The number of IPv4 families contained in the extension
1289 size_t v4_count() const { return m_v4_count; }
1290
1291 /// The number of IPv6 families contained in the extension
1292 size_t v6_count() const { return m_v6_count; }
1293
1294 private:
1295 std::string oid_name() const override { return "PKIX.IpAddrBlocks"; }
1296
1297 bool is_appropriate_context(Extension_Context context) const override;
1298
1299 bool should_encode() const override { return true; }
1300
1301 std::vector<uint8_t> encode_inner() const override;
1302 void decode_inner(const std::vector<uint8_t>& in) override;
1303
1304 std::vector<IPAddressFamily> m_ip_addr_blocks;
1305 size_t m_v4_count = 0;
1306 size_t m_v6_count = 0;
1307
1308 template <Version V>
1309 void add_range(const IPAddressOrRange<V>& range, std::optional<uint8_t> safi) {
1310 std::vector<IPAddressOrRange<V>> addresses = {range};
1311 m_ip_addr_blocks.push_back(IPAddressFamily(IPAddressChoice<V>(addresses), safi));
1312 sort_and_merge();
1313 }
1314
1315 void sort_and_merge();
1316};
1317
1318/**
1319 * AS Blocks Extension
1320 *
1321 * RFC 3779 X.509 Extensions for AS ID
1322 *
1323*/
1325 public:
1326 typedef uint32_t asnum_t;
1327
1328 class BOTAN_PUBLIC_API(3, 9) ASIdOrRange final : public ASN1_Object {
1329 public:
1330 void encode_into(DER_Encoder& to) const override;
1331 void decode_from(BER_Decoder& from) override;
1332
1333 asnum_t min() const { return m_min; }
1334
1335 asnum_t max() const { return m_max; }
1336
1337 ASIdOrRange() = default;
1338
1339 explicit ASIdOrRange(asnum_t id) : m_min(id), m_max(id) {}
1340
1341 ASIdOrRange(asnum_t min, asnum_t max) : m_min(min), m_max(max) {
1342 if(max < min) {
1343 throw Decoding_Error("AS range numbers must be sorted");
1344 }
1345 }
1346
1347 private:
1348 asnum_t m_min = 0;
1349 asnum_t m_max = 0;
1350 };
1351
1353 public:
1354 void encode_into(DER_Encoder& to) const override;
1355 void decode_from(BER_Decoder& from) override;
1356
1358
1359 explicit ASIdentifierChoice(const std::optional<std::vector<ASIdOrRange>>& ranges);
1360
1361 const std::optional<std::vector<ASIdOrRange>>& ranges() const { return m_as_ranges; }
1362
1363 private:
1364 std::optional<std::vector<ASIdOrRange>> m_as_ranges;
1365 };
1366
1367 class BOTAN_PUBLIC_API(3, 9) ASIdentifiers final : public ASN1_Object {
1368 public:
1369 void encode_into(DER_Encoder& to) const override;
1370 void decode_from(BER_Decoder& from) override;
1371
1372 explicit ASIdentifiers(const std::optional<ASIdentifierChoice>& asnum,
1373 const std::optional<ASIdentifierChoice>& rdi) :
1374 m_asnum(asnum), m_rdi(rdi) {
1375 if(!m_asnum.has_value() && !m_rdi.has_value()) {
1376 throw Decoding_Error("One of asnum, rdi must be present");
1377 }
1378 }
1379
1380 const std::optional<ASIdentifierChoice>& asnum() const { return m_asnum; }
1381
1382 const std::optional<ASIdentifierChoice>& rdi() const { return m_rdi; }
1383
1384 private:
1385 friend class ASBlocks;
1386 ASIdentifiers() = default;
1387
1388 std::optional<ASIdentifierChoice> m_asnum;
1389 std::optional<ASIdentifierChoice> m_rdi;
1390 };
1391
1392 ASBlocks() = default;
1393
1394 explicit ASBlocks(const ASIdentifiers& as_idents) : m_as_identifiers(as_idents) {}
1395
1396 std::unique_ptr<Certificate_Extension> copy() const override { return std::make_unique<ASBlocks>(*this); }
1397
1398 static OID static_oid() { return OID({1, 3, 6, 1, 5, 5, 7, 1, 8}); }
1399
1400 OID oid_of() const override { return static_oid(); }
1401
1402 void validate(const X509_Certificate& subject,
1403 const std::optional<X509_Certificate>& issuer,
1404 const std::vector<X509_Certificate>& cert_path,
1405 std::vector<std::set<Certificate_Status_Code>>& cert_status,
1406 size_t pos) const override;
1407
1408 /// Add a single asnum to this extension
1409 void add_asnum(asnum_t asnum) { add_asnum(asnum, asnum); }
1410
1411 /// Add an asnum range to this extension
1412 void add_asnum(asnum_t min, asnum_t max) {
1413 m_as_identifiers = ASIdentifiers(add_new(m_as_identifiers.asnum(), min, max), m_as_identifiers.rdi());
1414 }
1415
1416 /// Make the extension contain no allowed asnum's
1418 std::vector<ASIdOrRange> empty;
1419 m_as_identifiers = ASIdentifiers(ASIdentifierChoice(empty), m_as_identifiers.rdi());
1420 }
1421
1422 /// Mark the asnum entry as 'inherit'
1423 void inherit_asnum() { m_as_identifiers = ASIdentifiers(ASIdentifierChoice(), m_as_identifiers.rdi()); }
1424
1425 /// Add a single rdi to this extension
1426 void add_rdi(asnum_t rdi) { add_rdi(rdi, rdi); }
1427
1428 /// Add an rdi range to this extension
1429 void add_rdi(asnum_t min, asnum_t max) {
1430 m_as_identifiers = ASIdentifiers(m_as_identifiers.asnum(), add_new(m_as_identifiers.rdi(), min, max));
1431 }
1432
1433 /// Make the extension contain no allowed rdi's
1435 std::vector<ASIdOrRange> empty;
1436 m_as_identifiers = ASIdentifiers(m_as_identifiers.asnum(), ASIdentifierChoice(empty));
1437 }
1438
1439 /// Mark the rdi entry as 'inherit'
1440 void inherit_rdi() { m_as_identifiers = ASIdentifiers(m_as_identifiers.asnum(), ASIdentifierChoice()); }
1441
1442 const ASIdentifiers& as_identifiers() const { return m_as_identifiers; }
1443
1444 private:
1445 ASIdentifiers m_as_identifiers;
1446
1447 std::string oid_name() const override { return "PKIX.AutonomousSysIds"; }
1448
1449 bool is_appropriate_context(Extension_Context context) const override;
1450
1451 bool should_encode() const override { return true; }
1452
1453 static ASIdentifierChoice add_new(const std::optional<ASIdentifierChoice>& old, asnum_t min, asnum_t max);
1454
1455 std::vector<uint8_t> encode_inner() const override;
1456 void decode_inner(const std::vector<uint8_t>& in) override;
1457};
1458
1459/**
1460* An unknown X.509 extension
1461* Will add a failure to the path validation result, if critical
1462*/
1464 public:
1465 Unknown_Extension(const OID& oid, bool critical, bool failed_to_decode = false) :
1466 m_oid(oid), m_critical(critical), m_failed_to_decode(failed_to_decode) {}
1467
1468 std::unique_ptr<Certificate_Extension> copy() const override {
1469 return std::make_unique<Unknown_Extension>(*this);
1470 }
1471
1472 /**
1473 * Return the OID of this unknown extension
1474 */
1475 OID oid_of() const override { return m_oid; }
1476
1477 //static_oid not defined for Unknown_Extension
1478
1479 /**
1480 * Return the extension contents
1481 */
1482 const std::vector<uint8_t>& extension_contents() const { return m_bytes; }
1483
1484 /**
1485 * Return if this extension was marked critical
1486 */
1487 bool is_critical_extension() const { return m_critical; }
1488
1489 /**
1490 * Return true if this extension's OID was recognized but the contents
1491 * failed to decode.
1492 */
1493 bool failed_to_decode() const { return m_failed_to_decode; }
1494
1495 void validate(const X509_Certificate& /*subject*/,
1496 const std::optional<X509_Certificate>& /*issuer*/,
1497 const std::vector<X509_Certificate>& /*cert_path*/,
1498 std::vector<std::set<Certificate_Status_Code>>& cert_status,
1499 size_t pos) const override {
1500 if(m_failed_to_decode) {
1501 cert_status.at(pos).insert(Certificate_Status_Code::EXTENSION_ENCODING_ERROR);
1502 } else if(m_critical) {
1503 cert_status.at(pos).insert(Certificate_Status_Code::UNKNOWN_CRITICAL_EXTENSION);
1504 }
1505 }
1506
1507 private:
1508 std::string oid_name() const override { return ""; }
1509
1510 bool is_appropriate_context(Extension_Context context) const override;
1511
1512 bool should_encode() const override { return true; }
1513
1514 std::vector<uint8_t> encode_inner() const override;
1515 void decode_inner(const std::vector<uint8_t>& in) override;
1516
1517 OID m_oid;
1518 bool m_critical;
1519 bool m_failed_to_decode;
1520 std::vector<uint8_t> m_bytes;
1521};
1522
1523} // namespace Cert_Extension
1524
1525} // namespace Botan
1526
1527#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
ASN1_Object()=default
void encode_into(DER_Encoder &to) const override
ASIdOrRange(asnum_t min, asnum_t max)
Definition x509_ext.h:1341
void decode_from(BER_Decoder &from) override
void encode_into(DER_Encoder &to) const override
void decode_from(BER_Decoder &from) override
const std::optional< std::vector< ASIdOrRange > > & ranges() const
Definition x509_ext.h:1361
const std::optional< ASIdentifierChoice > & asnum() const
Definition x509_ext.h:1380
ASIdentifiers(const std::optional< ASIdentifierChoice > &asnum, const std::optional< ASIdentifierChoice > &rdi)
Definition x509_ext.h:1372
const std::optional< ASIdentifierChoice > & rdi() const
Definition x509_ext.h:1382
void encode_into(DER_Encoder &to) const override
void decode_from(BER_Decoder &from) override
void inherit_asnum()
Mark the asnum entry as 'inherit'.
Definition x509_ext.h:1423
void restrict_rdi()
Make the extension contain no allowed rdi's.
Definition x509_ext.h:1434
void inherit_rdi()
Mark the rdi entry as 'inherit'.
Definition x509_ext.h:1440
void add_rdi(asnum_t min, asnum_t max)
Add an rdi range to this extension.
Definition x509_ext.h:1429
const ASIdentifiers & as_identifiers() const
Definition x509_ext.h:1442
ASBlocks(const ASIdentifiers &as_idents)
Definition x509_ext.h:1394
OID oid_of() const override
Definition x509_ext.h:1400
void restrict_asnum()
Make the extension contain no allowed asnum's.
Definition x509_ext.h:1417
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:1396
void add_asnum(asnum_t asnum)
Add a single asnum to this extension.
Definition x509_ext.h:1409
void add_rdi(asnum_t rdi)
Add a single rdi to this extension.
Definition x509_ext.h:1426
void add_asnum(asnum_t min, asnum_t max)
Add an asnum range to this extension.
Definition x509_ext.h:1412
AccessDescription(OID method, ASN1_Type location_tag, ASN1_Class location_class, std::vector< uint8_t > location_value)
Definition x509_ext.h:387
std::vector< std::string > ca_issuers() const
const std::vector< URI > & ca_issuer_uris() const
Definition x509_ext.h:479
const std::vector< AccessDescription > & access_descriptions() const
Definition x509_ext.h:471
const std::vector< URI > & ocsp_responder_uris() const
Definition x509_ext.h:465
std::vector< std::string > ocsp_responders() const
Definition x509_ext.cpp:999
Authority_Key_ID(const std::vector< uint8_t > &k)
Definition x509_ext.h:164
const std::optional< Authority_Cert_Identifier > & authority_cert_identifier() const
Definition x509_ext.h:183
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:160
const std::vector< uint8_t > & get_key_id() const
Definition x509_ext.h:178
Authority_Key_ID(const std::vector< uint8_t > &k, Authority_Cert_Identifier authority_cert)
Definition x509_ext.h:175
BOTAN_FUTURE_EXPLICIT Basic_Constraints(bool is_ca=false, size_t path_length_constraint=0)
Definition x509_ext.cpp:436
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:42
std::optional< size_t > path_length_constraint() const
Definition x509_ext.h:60
const std::optional< ReasonFlags > & reasons() const
Definition x509_ext.h:631
const std::optional< AlternativeName > & crl_issuer() const
Definition x509_ext.h:636
Distribution_Point(std::optional< DistributionPointName > dp_name, std::optional< ReasonFlags > reasons, std::optional< AlternativeName > crl_issuer)
Definition x509_ext.h:618
const std::optional< DistributionPointName > & distribution_point_name() const
Definition x509_ext.h:626
const std::vector< URI > & crl_distribution_point_uris() const
Definition x509_ext.h:665
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:653
const std::vector< Distribution_Point > & distribution_points() const
Definition x509_ext.h:661
const std::optional< DistributionPointName > & distribution_point_name() const
Definition x509_ext.h:730
const std::optional< ReasonFlags > & only_some_reasons() const
Definition x509_ext.h:736
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:723
CRL_Issuing_Distribution_Point(DistributionPointName dp_name)
Definition x509_ext.h:700
CRL_Issuing_Distribution_Point(std::optional< DistributionPointName > dp_name, bool only_contains_user_certs, bool only_contains_ca_certs, std::optional< ReasonFlags > only_some_reasons, bool indirect_crl, bool only_contains_attribute_certs)
Definition x509_ext.h:702
OID oid_of() const override
Definition x509_ext.h:518
std::unique_ptr< Certificate_Extension > copy() const override
BOTAN_FUTURE_EXPLICIT CRL_Number(size_t n)
Definition x509_ext.h:508
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:539
CRL_ReasonCode(CRL_Code r=CRL_Code::Unspecified)
Definition x509_ext.h:543
const std::vector< OID > & get_policy_oids() const
Definition x509_ext.h:346
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:338
const std::optional< AlternativeName > & full_name() const
Definition x509_ext.h:592
DistributionPointName(AlternativeName full_name)
Definition x509_ext.h:579
const std::vector< OID > & object_identifiers() const
Definition x509_ext.h:276
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:268
Extended_Key_Usage(const std::vector< OID > &o)
Definition x509_ext.h:274
const std::optional< std::vector< IPAddressOrRange< V > > > & ranges() const
Definition x509_ext.h:1166
void encode_into(DER_Encoder &to) const override
IPAddressChoice(std::optional< std::span< const IPAddressOrRange< V > > > ranges)
void encode_into(DER_Encoder &to) const override
std::variant< IPAddressChoice< Version::IPv4 >, IPAddressChoice< Version::IPv6 > > AddrChoice
Definition x509_ext.h:1178
IPAddressFamily(const AddrChoice &choice, std::optional< uint8_t > safi=std::nullopt)
Definition x509_ext.h:1185
IPAddressOrRange(const Address &min, const Address &max)
Definition x509_ext.h:1134
void encode_into(DER_Encoder &to) const override
IPAddressOrRange(const IPAddress< V > &min, const IPAddress< V > &max)
Definition x509_ext.h:1120
friend bool operator==(const IPAddress< V > &lhs, const IPAddress< V > &rhs)
Definition x509_ext.h:1097
std::array< uint8_t, Length > to_bytes() const
Definition x509_ext.h:1060
friend std::strong_ordering operator<=>(const IPAddress< V > lhs, const IPAddress< V > &rhs)
Definition x509_ext.h:1086
std::array< uint8_t, Length > value() const
Definition x509_ext.h:1055
void inherit(std::optional< uint8_t > safi=std::nullopt)
Mark the specified IP version as 'inherit' (for the specified SAFI, if any).
Definition x509_ext.h:1281
size_t v6_count() const
The number of IPv6 families contained in the extension.
Definition x509_ext.h:1292
void add_address(const IPv6Address &address, std::optional< uint8_t > safi=std::nullopt)
Add a single IPv6 address to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1230
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:1212
void restrict(std::optional< uint8_t > safi=std::nullopt)
Make the extension contain no allowed IP addresses for the specified IP version (and SAFI,...
Definition x509_ext.h:1273
void add_address(const IPv6Subnet &subnet, std::optional< uint8_t > safi=std::nullopt)
Add an IPv6 subnet to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1250
void add_address(const IPv6Address &min, const IPv6Address &max, std::optional< uint8_t > safi=std::nullopt)
Add an IPv6 address range to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1240
const std::vector< IPAddressFamily > & addr_blocks() const
Definition x509_ext.h:1286
void add_address(const IPv4Address &min, const IPv4Address &max, std::optional< uint8_t > safi=std::nullopt)
Add an IPv4 address range to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1235
void add_address(const IPv4Address &address, std::optional< uint8_t > safi=std::nullopt)
Add a single IPv4 address to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1225
void add_address(const IPv4Subnet &subnet, std::optional< uint8_t > safi=std::nullopt)
Add an IPv4 subnet to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1245
IPAddressBlocks(const std::vector< IPAddressFamily > &blocks)
Definition x509_ext.h:1208
size_t v4_count() const
The number of IPv4 families contained in the extension.
Definition x509_ext.h:1289
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:244
const AlternativeName & get_alt_name() const
Definition x509_ext.h:238
Issuer_Alternative_Name(const AlternativeName &name=AlternativeName())
Definition x509_ext.h:248
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:83
OID oid_of() const override
Definition x509_ext.h:95
Key_Usage(Key_Constraints c)
Definition x509_ext.h:87
Key_Constraints get_constraints() const
Definition x509_ext.h:91
BOTAN_FUTURE_EXPLICIT Name_Constraints(const NameConstraints &nc)
Definition x509_ext.h:306
const NameConstraints & get_name_constraints() const
Definition x509_ext.h:314
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:300
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:837
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:794
OID oid_of() const override
Definition x509_ext.h:798
const AlternativeName & get_alt_name() const
Definition x509_ext.h:208
Subject_Alternative_Name(const AlternativeName &name=AlternativeName())
Definition x509_ext.h:218
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:214
Subject_Key_ID(const std::vector< uint8_t > &k)
Definition x509_ext.h:117
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:132
const std::vector< uint8_t > & get_key_id() const
Definition x509_ext.h:136
static Entry from_service_provider_code(std::string_view spc)
static Entry from_telephone_number(const Botan::TelephoneNumber &tn)
void decode_from(BER_Decoder &from) override
std::vector< TelephoneNumberRangeData > RangeContainer
Definition x509_ext.h:889
static Entry from_telephone_number_range(const Botan::TelephoneNumberRange &range)
void encode_into(DER_Encoder &to) const override
std::variant< ASN1_String, RangeContainer > DataContainer
Definition x509_ext.h:890
const Botan::TelephoneNumberRange & range() const
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:971
static TNAuthList from_der(std::span< const uint8_t > der)
OID oid_of() const override
Definition x509_ext.h:975
const std::vector< Entry > & entries() const
Definition x509_ext.h:977
std::unique_ptr< Certificate_Extension > copy() const override
Definition x509_ext.h:1468
Unknown_Extension(const OID &oid, bool critical, bool failed_to_decode=false)
Definition x509_ext.h:1465
const std::vector< uint8_t > & extension_contents() const
Definition x509_ext.h:1482
void validate(const X509_Certificate &, const std::optional< X509_Certificate > &, const std::vector< X509_Certificate > &, std::vector< std::set< Certificate_Status_Code > > &cert_status, size_t pos) const override
Definition x509_ext.h:1495
Name Constraints.
Definition pkix_types.h:748
const char * name
Definition ffi_tls.h:59
ASN1_Class
Definition asn1_obj.h:32
ASN1_Type
Definition asn1_obj.h:47
Extension_Context
Definition pkix_types.h:795