42 explicit X509_DN(
const std::multimap<OID, std::string>& args) {
43 for(
const auto& i : args) {
44 add_attribute(i.first, i.second);
48 explicit X509_DN(
const std::multimap<std::string, std::string>& args) {
49 for(
const auto& i : args) {
50 add_attribute(i.first, i.second);
57 bool has_field(
const OID& oid)
const;
63 const std::vector<uint8_t>&
get_bits()
const {
return m_dn_bits; }
65 std::vector<uint8_t> DER_encode()
const;
67 bool empty()
const {
return m_rdn.empty(); }
69 size_t count()
const {
return m_rdn.size(); }
71 std::string to_string()
const;
73 const std::vector<std::pair<OID, ASN1_String>>&
dn_info()
const {
return m_rdn; }
75 std::multimap<OID, std::string> get_attributes()
const;
76 std::multimap<std::string, std::string> contents()
const;
78 bool has_field(std::string_view attr)
const;
79 std::vector<std::string> get_attribute(std::string_view attr)
const;
80 std::string get_first_attribute(std::string_view attr)
const;
82 void add_attribute(std::string_view key, std::string_view val);
88 static std::string deref_info_field(std::string_view key);
97 static size_t lookup_ub(
const OID& oid);
100 std::vector<std::pair<OID, ASN1_String>> m_rdn;
101 std::vector<uint8_t> m_dn_bits;
128 void add_uri(std::string_view uri);
131 void add_email(std::string_view addr);
134 void add_dns(std::string_view dns);
140 void add_dn(
const X509_DN& dn);
143 void add_ipv4_address(uint32_t ipv4);
146 const std::set<std::string>&
uris()
const {
return m_uri; }
149 const std::set<std::string>&
email()
const {
return m_email; }
152 const std::set<std::string>&
dns()
const {
return m_dns; }
170 size_t count()
const;
173 bool has_items()
const;
176 BOTAN_DEPRECATED(
"Use AlternativeName::{uris, email, dns, othernames, directory_names}")
177 std::multimap<std::
string, std::
string> contents() const;
180 bool has_field(std::string_view attr) const;
182 BOTAN_DEPRECATED("Use AlternativeName::{uris, email, dns, othernames, directory_names}
")
183 std::vector<std::string> get_attribute(std::string_view attr) const;
185 BOTAN_DEPRECATED("Use AlternativeName::{uris, email, dns, othernames, directory_names}
")
186 std::multimap<std::string, std::string, std::less<>> get_attributes() const;
188 BOTAN_DEPRECATED("Use AlternativeName::{uris, email, dns, othernames, directory_names}
")
189 std::string get_first_attribute(std::string_view attr) const;
191 BOTAN_DEPRECATED("Use AlternativeName::add_{uri, dns, email, ...}
")
192 void add_attribute(std::string_view type, std::string_view value);
195 void add_othername(const OID& oid, std::string_view value, ASN1_Type type);
197 BOTAN_DEPRECATED("Use AlternativeName::othernames
") std::multimap<OID, ASN1_String> get_othernames() const;
201 BOTAN_DEPRECATED("Use plain constructor plus add_{uri,dns,email,ipv4_address}
")
202 AlternativeName(std::string_view email_addr,
203 std::string_view uri = "",
204 std::string_view dns = "",
205 std::string_view ip_address = "");
208 std::set<std::string> m_dns;
209 std::set<std::string> m_uri;
210 std::set<std::string> m_email;
211 std::set<uint32_t> m_ipv4_addr;
212 std::set<X509_DN> m_dn_names;
213 std::set<std::pair<OID, ASN1_String>> m_othernames;
219class BOTAN_PUBLIC_API(2, 0) Attribute final : public ASN1_Object {
221 void encode_into(DER_Encoder& to) const override;
222 void decode_from(BER_Decoder& from) override;
224 Attribute() = default;
225 Attribute(const OID& oid, const std::vector<uint8_t>& params);
226 Attribute(std::string_view oid_str, const std::vector<uint8_t>& params);
228 const OID& oid() const { return m_oid; }
230 const std::vector<uint8_t>& parameters() const { return m_parameters; }
232 const OID& object_identifier() const { return m_oid; }
234 const std::vector<uint8_t>& get_parameters() const { return m_parameters; }
238 std::vector<uint8_t> m_parameters;
251class BOTAN_PUBLIC_API(2, 0) GeneralName final : public ASN1_Object {
253 enum MatchResult : int {
261 enum class NameType : uint8_t {
273 // Encoding is not implemented
274 void encode_into(DER_Encoder&) const override;
276 void decode_from(BER_Decoder&) override;
281 NameType type_code() const { return m_type; }
286 BOTAN_DEPRECATED("Deprecated use type_code
") std::string type() const;
291 BOTAN_DEPRECATED("Deprecated no replacement
") std::string name() const;
298 BOTAN_DEPRECATED("Deprecated use
NameConstraints type
") MatchResult matches(const X509_Certificate& cert) const;
300 bool matches_dns(const std::string& dns_name) const;
301 bool matches_ipv4(uint32_t ip) const;
302 bool matches_dn(const X509_DN& dn) const;
305 static constexpr size_t RFC822_IDX = 0;
306 static constexpr size_t DNS_IDX = 1;
307 static constexpr size_t URI_IDX = 2;
308 static constexpr size_t DN_IDX = 3;
309 static constexpr size_t IPV4_IDX = 4;
312 std::variant<std::string, std::string, std::string, X509_DN, std::pair<uint32_t, uint32_t>> m_name;
314 static bool matches_dns(std::string_view name, std::string_view constraint);
316 static bool matches_dn(const X509_DN& name, const X509_DN& constraint);
358class BOTAN_PUBLIC_API(2, 0) NameConstraints final {
363 NameConstraints() : m_permitted_subtrees(), m_excluded_subtrees() {}
370 NameConstraints(std::vector<GeneralSubtree>&& permitted_subtrees,
371 std::vector<GeneralSubtree>&& excluded_subtrees);
376 BOTAN_DEPRECATED("Deprecated no replacement
") const std::vector<GeneralSubtree>& permitted() const {
377 return m_permitted_subtrees;
383 BOTAN_DEPRECATED("Deprecated no replacement
") const std::vector<GeneralSubtree>& excluded() const {
384 return m_excluded_subtrees;
390 bool is_permitted(const X509_Certificate& cert, bool reject_unknown) const;
395 bool is_excluded(const X509_Certificate& cert, bool reject_unknown) const;
398 std::vector<GeneralSubtree> m_permitted_subtrees;
399 std::vector<GeneralSubtree> m_excluded_subtrees;
401 std::set<GeneralName::NameType> m_permitted_name_types;
402 std::set<GeneralName::NameType> m_excluded_name_types;
408class BOTAN_PUBLIC_API(2, 0) Certificate_Extension {
413 virtual OID oid_of() const = 0;
416 * @return specific OID name
417 * If possible OIDS table should match oid_name to OIDS, ie
418 * OID::from_string(ext->oid_name()) == ext->oid_of()
419 * Should return empty string if OID is not known
421 virtual std::string oid_name() const = 0;
428 virtual std::unique_ptr<Certificate_Extension> copy() const = 0;
431 * Callback visited during path validation.
433 * An extension can implement this callback to inspect
434 * the path during path validation.
436 * If an error occurs during validation of this extension,
437 * an appropriate status code shall be added to cert_status.
439 * @param subject Subject certificate that contains this extension
440 * @param issuer Issuer certificate
441 * @param status Certificate validation status codes for subject certificate
442 * @param cert_path Certificate path which is currently validated
443 * @param pos Position of subject certificate in cert_path
445 virtual void validate(const X509_Certificate& subject,
446 const X509_Certificate& issuer,
447 const std::vector<X509_Certificate>& cert_path,
448 std::vector<std::set<Certificate_Status_Code>>& cert_status,
451 virtual ~Certificate_Extension() = default;
454 friend class Extensions;
456 virtual bool should_encode() const { return true; }
458 virtual std::vector<uint8_t> encode_inner() const = 0;
459 virtual void decode_inner(const std::vector<uint8_t>&) = 0;
465class BOTAN_PUBLIC_API(2, 0) Extensions final : public ASN1_Object {
474 const Certificate_Extension* get_extension_object(const OID& oid) const;
476 template <typename T>
477 const T* get_extension_object_as(const OID& oid = T::static_oid()) const {
478 if(const Certificate_Extension* extn = get_extension_object(oid)) {
479 // Unknown_Extension oid_name is empty
480 if(extn->oid_name().empty()) {
482 } else if(const T* extn_as_T = dynamic_cast<const T*>(extn)) {
485 throw Decoding_Error("Exception::get_extension_object_as dynamic_cast failed
");
496 const std::vector<OID>& get_extension_oids() const { return m_extension_oids; }
501 bool extension_set(const OID& oid) const;
506 bool critical_extension_set(const OID& oid) const;
512 std::vector<uint8_t> get_extension_bits(const OID& oid) const;
514 void encode_into(DER_Encoder&) const override;
515 void decode_from(BER_Decoder&) override;
523 void add(std::unique_ptr<Certificate_Extension> extn, bool critical = false);
533 bool add_new(std::unique_ptr<Certificate_Extension> extn, bool critical = false);
540 void replace(std::unique_ptr<Certificate_Extension> extn, bool critical = false);
546 bool remove(const OID& oid);
555 std::unique_ptr<Certificate_Extension> get(const OID& oid) const;
567 template <typename T>
568 std::unique_ptr<T> get_raw(const OID& oid) const {
569 auto extn_info = m_extension_info.find(oid);
571 if(extn_info != m_extension_info.end()) {
572 // Unknown_Extension oid_name is empty
573 if(extn_info->second.obj().oid_name().empty()) {
574 auto ext = std::make_unique<T>();
575 ext->decode_inner(extn_info->second.bits());
588 std::vector<std::pair<std::unique_ptr<Certificate_Extension>, bool>> extensions() const;
595 std::map<OID, std::pair<std::vector<uint8_t>, bool>> extensions_raw() const;
597 Extensions() = default;
599 Extensions(const Extensions&) = default;
600 Extensions& operator=(const Extensions&) = default;
602 Extensions(Extensions&&) = default;
603 Extensions& operator=(Extensions&&) = default;
606 static std::unique_ptr<Certificate_Extension> create_extn_obj(const OID& oid,
608 const std::vector<uint8_t>& body);
610 class Extensions_Info {
612 Extensions_Info(bool critical, std::unique_ptr<Certificate_Extension> ext) :
613 m_obj(std::move(ext)), m_bits(m_obj->encode_inner()), m_critical(critical) {}
615 Extensions_Info(bool critical,
616 const std::vector<uint8_t>& encoding,
617 std::unique_ptr<Certificate_Extension> ext) :
618 m_obj(std::move(ext)), m_bits(encoding), m_critical(critical) {}
620 bool is_critical() const { return m_critical; }
622 const std::vector<uint8_t>& bits() const { return m_bits; }
624 const Certificate_Extension& obj() const {
625 BOTAN_ASSERT_NONNULL(m_obj.get());
630 std::shared_ptr<Certificate_Extension> m_obj;
631 std::vector<uint8_t> m_bits;
632 bool m_critical = false;
635 std::vector<OID> m_extension_oids;
636 std::map<OID, Extensions_Info> m_extension_info;