Botan 3.13.0
Crypto and TLS for C&
Botan::X509_Certificate Class Reference

#include <x509cert.h>

Inheritance diagram for Botan::X509_Certificate:
Botan::X509_Object Botan::ASN1_Object

Classes

class  Tag
class  TagHash

Public Member Functions

bool allowed_extended_usage (const OID &usage) const
bool allowed_extended_usage (std::string_view usage) const
bool allowed_usage (Key_Constraints usage) const
bool allowed_usage (Usage_Type usage) const
const std::vector< uint8_t > & authority_key_id () const
std::vector< uint8_t > BER_encode () const
const std::vector< URI > & ca_issuer_uris () const
std::vector< std::string > ca_issuers () const
std::span< const uint8_t, 20 > certificate_data_sha1 () const
std::span< const uint8_t, 32 > certificate_data_sha256 () const
const std::vector< OID > & certificate_policy_oids () const
bool check_signature (const Public_Key &key) const
Key_Constraints constraints () const
std::string crl_distribution_point () const
const std::vector< URI > & crl_distribution_point_uris () const
std::vector< std::string > crl_distribution_points () const
void decode_from (BER_Decoder &from) override
void encode_into (DER_Encoder &to) const override
const std::vector< OID > & extended_key_usage () const
std::string fingerprint (std::string_view hash_name="SHA-1") const
bool has_constraints (Key_Constraints constraints) const
bool has_ex_constraint (const OID &ex_constraint) const
bool has_ex_constraint (std::string_view ex_constraint) const
bool is_CA_cert () const
bool is_critical (std::string_view ex_name) const
bool is_self_signed () const
bool is_serial_negative () const
const AlternativeNameissuer_alt_name () const
const X509_DNissuer_dn () const
std::vector< std::string > issuer_info (std::string_view name) const
std::unique_ptr< Public_Keyload_subject_public_key () const
bool matches_dns_name (const DNSName &name) const
bool matches_dns_name (std::string_view name) const
bool matches_ip (const IPv4Address &address) const
bool matches_ip (const IPv6Address &address) const
const NameConstraintsname_constraints () const
const X509_Timenot_after () const
const X509_Timenot_before () const
std::string ocsp_responder () const
const std::vector< URI > & ocsp_responder_uris () const
std::vector< std::string > ocsp_responders () const
bool operator< (const X509_Certificate &other) const
X509_Certificateoperator= (const X509_Certificate &other)=default
X509_Certificateoperator= (X509_Certificate &&other)=default
bool operator== (const X509_Certificate &other) const
std::optional< size_t > path_length_constraint () const
uint32_t path_limit () const
std::string PEM_encode () const
const std::vector< uint8_t > & raw_issuer_dn () const
std::span< const uint8_t, 20 > raw_issuer_dn_sha1 () const
const std::vector< uint8_t > & raw_issuer_dn_sha256 () const
const std::vector< uint8_t > & raw_subject_dn () const
std::span< const uint8_t, 20 > raw_subject_dn_sha1 () const
const std::vector< uint8_t > & raw_subject_dn_sha256 () const
const X509_Serial_Numberserial () const
const std::vector< uint8_t > & serial_number () const
const std::vector< uint8_t > & signature () const
const AlgorithmIdentifiersignature_algorithm () const
const std::vector< uint8_t > & signed_body () const
bool skip_revocation_check () const
const AlternativeNamesubject_alt_name () const
const X509_DNsubject_dn () const
std::vector< EmailAddresssubject_email_addresses () const
std::vector< std::string > subject_info (std::string_view name) const
const std::vector< uint8_t > & subject_key_id () const
std::unique_ptr< Public_Keysubject_public_key () const
const AlgorithmIdentifiersubject_public_key_algo () const
const std::vector< uint8_t > & subject_public_key_bits () const
const std::vector< uint8_t > & subject_public_key_bitstring () const
const std::vector< uint8_t > & subject_public_key_bitstring_sha1 () const
std::span< const uint8_t, 32 > subject_public_key_bitstring_sha256 () const
const std::vector< uint8_t > & subject_public_key_info () const
Tag tag () const
std::vector< uint8_t > tbs_data () const
std::string to_string () const
const std::vector< uint8_t > & v2_issuer_key_id () const
const std::vector< uint8_t > & v2_subject_key_id () const
const Extensionsv3_extensions () const
std::pair< Certificate_Status_Code, std::string > verify_signature (const Public_Key &key) const
 X509_Certificate ()=default
 X509_Certificate (const uint8_t data[], size_t length)
 X509_Certificate (const X509_Certificate &other)=default
 X509_Certificate (DataSource &source)
 X509_Certificate (std::span< const uint8_t > in)
 X509_Certificate (X509_Certificate &&other)=default
uint32_t x509_version () const
 ~X509_Certificate () override

Static Public Member Functions

static std::unique_ptr< PK_Signerchoose_sig_format (const Private_Key &key, RandomNumberGenerator &rng, std::string_view hash_fn, std::string_view padding_algo)
static std::vector< uint8_t > make_signed (PK_Signer &signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, std::span< const uint8_t > tbs)

Protected Member Functions

void load_data (DataSource &src)

Detailed Description

This class represents an X.509 Certificate

TODO(Botan4) mark this final once PKCS11_X509_Certificate is fixed

Definition at line 39 of file x509cert.h.

Constructor & Destructor Documentation

◆ X509_Certificate() [1/6]

Botan::X509_Certificate::X509_Certificate ( DataSource & source)
explicit

Create a certificate from a data source providing the DER or PEM encoded certificate.

Parameters
sourcethe data source

Definition at line 96 of file x509cert.cpp.

96 {
97 load_data(src);
98}
void load_data(DataSource &src)
Definition x509_obj.cpp:24

References Botan::X509_Object::load_data().

Referenced by operator<(), operator=(), operator=(), operator==(), X509_Certificate(), X509_Certificate(), and X509_Certificate().

◆ X509_Certificate() [2/6]

Botan::X509_Certificate::X509_Certificate ( std::span< const uint8_t > in)
explicit

Create a certificate from a buffer

Parameters
inthe buffer containing the DER-encoded certificate

Definition at line 100 of file x509cert.cpp.

100 {
101 DataSource_Memory src(in);
102 load_data(src);
103}

References Botan::X509_Object::load_data().

◆ X509_Certificate() [3/6]

Botan::X509_Certificate::X509_Certificate ( const uint8_t data[],
size_t length )
inline

Create a certificate from a buffer

Parameters
datathe buffer containing the DER-encoded certificate
lengthlength of data in bytes

Definition at line 551 of file x509cert.h.

551: X509_Certificate(std::span{data, length}) {}

References X509_Certificate().

◆ X509_Certificate() [4/6]

Botan::X509_Certificate::X509_Certificate ( )
default

Create an uninitialized certificate object. Any attempts to access this object will throw an exception.

◆ X509_Certificate() [5/6]

Botan::X509_Certificate::X509_Certificate ( const X509_Certificate & other)
default

References X509_Certificate().

◆ X509_Certificate() [6/6]

Botan::X509_Certificate::X509_Certificate ( X509_Certificate && other)
default

References X509_Certificate().

◆ ~X509_Certificate()

Botan::X509_Certificate::~X509_Certificate ( )
overridedefault

Member Function Documentation

◆ allowed_extended_usage() [1/2]

bool Botan::X509_Certificate::allowed_extended_usage ( const OID & usage) const

Returns true if the specified usage is set in the extended key usage extension, or if no extended key usage constraints are set at all. To check if a certain extended key constraint is set in the certificate use

See also
X509_Certificate::has_ex_constraint.

Definition at line 539 of file x509cert.cpp.

539 {
540 const std::vector<OID>& ex = extended_key_usage();
541 if(ex.empty()) {
542 return true;
543 }
544
545 if(has_ex_constraint(usage)) {
546 return true;
547 }
548
549 return false;
550}
const std::vector< OID > & extended_key_usage() const
Definition x509cert.cpp:507
bool has_ex_constraint(std::string_view ex_constraint) const
Definition x509cert.cpp:582

References extended_key_usage(), and has_ex_constraint().

◆ allowed_extended_usage() [2/2]

bool Botan::X509_Certificate::allowed_extended_usage ( std::string_view usage) const

Returns true if the specified

Parameters
usageis set in the extended key usage extension or if no extended key usage constraints are set at all. To check if a certain extended key constraint is set in the certificate use
See also
X509_Certificate::has_ex_constraint.

Definition at line 535 of file x509cert.cpp.

535 {
537}
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
bool allowed_extended_usage(std::string_view usage) const
Definition x509cert.cpp:535

References allowed_extended_usage(), and Botan::OID::from_string().

Referenced by allowed_extended_usage(), and allowed_usage().

◆ allowed_usage() [1/2]

bool Botan::X509_Certificate::allowed_usage ( Key_Constraints usage) const

Returns true if the specified

Parameters
usageis set in the key usage extension or if no key usage constraints are set at all. To check if a certain key constraint is set in the certificate use
See also
X509_Certificate::has_constraints.

Definition at line 528 of file x509cert.cpp.

528 {
529 if(constraints().empty()) {
530 return true;
531 }
532 return constraints().includes(usage);
533}
bool includes(Key_Constraints::Bits other) const
Definition pkix_enums.h:186
Key_Constraints constraints() const
Definition x509cert.cpp:503

References constraints(), and Botan::Key_Constraints::includes().

Referenced by allowed_usage(), Botan::PKIX::check_crl(), and Botan::Cert_Extension::OCSP_NoCheck::validate().

◆ allowed_usage() [2/2]

bool Botan::X509_Certificate::allowed_usage ( Usage_Type usage) const

Returns true if the required key and extended key constraints are set in the certificate for the specified

Parameters
usageor if no key constraints are set in both the key usage and extended key usage extension.

Definition at line 552 of file x509cert.cpp.

552 {
553 // These follow suggestions in RFC 5280 4.2.1.12
554
555 switch(usage) {
557 return true;
558
562 allowed_extended_usage("PKIX.ServerAuth");
563
566 allowed_extended_usage("PKIX.ClientAuth");
567
570 has_ex_constraint("PKIX.OCSPSigning");
571
573 return is_CA_cert();
574
577 }
578
579 return false;
580}
bool is_CA_cert() const
Definition x509cert.cpp:483
bool allowed_usage(Key_Constraints usage) const
Definition x509cert.cpp:528

References allowed_extended_usage(), allowed_usage(), Botan::CERTIFICATE_AUTHORITY, Botan::Key_Constraints::DataEncipherment, Botan::Key_Constraints::DigitalSignature, Botan::ENCRYPTION, has_ex_constraint(), is_CA_cert(), Botan::Key_Constraints::KeyAgreement, Botan::Key_Constraints::KeyEncipherment, Botan::Key_Constraints::NonRepudiation, Botan::OCSP_RESPONDER, Botan::TLS_CLIENT_AUTH, Botan::TLS_SERVER_AUTH, and Botan::UNSPECIFIED.

◆ authority_key_id()

const std::vector< uint8_t > & Botan::X509_Certificate::authority_key_id ( ) const

Get the DER encoded AuthorityKeyIdentifier of this certificate.

Returns
DER encoded AuthorityKeyIdentifier

Definition at line 432 of file x509cert.cpp.

432 {
433 return data().m_authority_key_id;
434}

Referenced by botan_x509_cert_view_binary_values(), Botan::Certificate_Store_In_Memory::find_crl_for(), Botan::X509_CRL::is_revoked(), and to_string().

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 21 of file asn1_obj.cpp.

21 {
22 std::vector<uint8_t> output;
23 DER_Encoder der(output);
24 this->encode_into(der);
25 return output;
26}
virtual void encode_into(DER_Encoder &to) const =0

References encode_into().

Referenced by decode_from(), Botan::PKCS12::export_to(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), and Botan::PSS_Params::PSS_Params().

◆ ca_issuer_uris()

const std::vector< URI > & Botan::X509_Certificate::ca_issuer_uris ( ) const

Return the listed addresses of ca issuers, or empty if not set

Definition at line 649 of file x509cert.cpp.

649 {
650 return data().m_ca_issuers;
651}

Referenced by botan_x509_cert_view_string_values(), and to_string().

◆ ca_issuers()

std::vector< std::string > Botan::X509_Certificate::ca_issuers ( ) const

Return the listed addresses of ca issuers, or empty if not set

Definition at line 645 of file x509cert.cpp.

645 {
646 return uris_as_strings(data().m_ca_issuers);
647}

Referenced by to_string().

◆ certificate_data_sha1()

std::span< const uint8_t, 20 > Botan::X509_Certificate::certificate_data_sha1 ( ) const

SHA-1 of the entire certificate DER encoding

Definition at line 472 of file x509cert.cpp.

472 {
473 if(data().m_fingerprint_sha1.empty()) {
474 throw Not_Implemented("SHA-1 not available");
475 }
476 return data().m_cert_data_sha1;
477}

◆ certificate_data_sha256()

std::span< const uint8_t, 32 > Botan::X509_Certificate::certificate_data_sha256 ( ) const

SHA-256 of the entire certificate DER encoding

Definition at line 479 of file x509cert.cpp.

479 {
480 return data().m_cert_data_sha256;
481}

◆ certificate_policy_oids()

const std::vector< OID > & Botan::X509_Certificate::certificate_policy_oids ( ) const

Get the policies as defined in the CertificatePolicies extension of this certificate.

Returns
certificate policies

Definition at line 511 of file x509cert.cpp.

511 {
512 return data().m_cert_policies;
513}

Referenced by to_string().

◆ check_signature()

bool Botan::X509_Object::check_signature ( const Public_Key & key) const
inherited

Check the signature on this data

Parameters
keythe public key purportedly used to sign this data
Returns
true if the signature is valid, otherwise false

Definition at line 125 of file x509_obj.cpp.

125 {
126 const auto result = this->verify_signature(pub_key);
127 return (result.first == Certificate_Status_Code::VERIFIED);
128}
std::pair< Certificate_Status_Code, std::string > verify_signature(const Public_Key &key) const
Definition x509_obj.cpp:130

References Botan::VERIFIED, and verify_signature().

◆ choose_sig_format()

std::unique_ptr< PK_Signer > Botan::X509_Object::choose_sig_format ( const Private_Key & key,
RandomNumberGenerator & rng,
std::string_view hash_fn,
std::string_view padding_algo )
staticinherited

Choose and return a signature scheme appropriate for X.509 signing using the provided parameters.

Parameters
keywill be the key to choose a padding scheme for
Random Number Generatorsthe random generator to use
hash_fnis the desired hash function
padding_algospecifies the padding method
Returns
a PK_Signer object for generating signatures

Definition at line 240 of file x509_obj.cpp.

243 {
244 const Signature_Format format = key._default_x509_signature_format();
245
246 if(!user_specified_padding.empty()) {
247 try {
248 auto pk_signer = std::make_unique<PK_Signer>(key, rng, user_specified_padding, format);
249 if(!hash_fn.empty() && pk_signer->hash_function() != hash_fn) {
250 throw Invalid_Argument(format_padding_error_message(
251 key.algo_name(), pk_signer->hash_function(), hash_fn, "", user_specified_padding));
252 }
253 return pk_signer;
254 } catch(Lookup_Error&) {}
255 }
256
257 const std::string padding = x509_signature_padding_for(key.algo_name(), hash_fn, user_specified_padding);
258
259 try {
260 auto pk_signer = std::make_unique<PK_Signer>(key, rng, padding, format);
261 if(!hash_fn.empty() && pk_signer->hash_function() != hash_fn) {
262 throw Invalid_Argument(format_padding_error_message(
263 key.algo_name(), pk_signer->hash_function(), hash_fn, padding, user_specified_padding));
264 }
265 return pk_signer;
266 } catch(Not_Implemented&) {
267 throw Invalid_Argument("Signatures using " + key.algo_name() + "/" + padding + " are not supported");
268 }
269}
Signature_Format
Definition pk_keys.h:32

References Botan::Asymmetric_Key::_default_x509_signature_format(), and Botan::Asymmetric_Key::algo_name().

Referenced by Botan::PKCS10_Request::create(), Botan::X509::create_self_signed_cert(), and Botan::X509_CA::X509_CA().

◆ constraints()

Key_Constraints Botan::X509_Certificate::constraints ( ) const

Get the key constraints as defined in the KeyUsage extension of this certificate.

Returns
key constraints

Definition at line 503 of file x509cert.cpp.

503 {
504 return data().m_key_constraints;
505}

Referenced by allowed_usage(), has_constraints(), and to_string().

◆ crl_distribution_point()

std::string Botan::X509_Certificate::crl_distribution_point ( ) const

Return the CRL distribution point, or empty if not set

Definition at line 661 of file x509cert.cpp.

661 {
662 // just returns the first (arbitrarily)
663 if(!data().m_crl_distribution_points.empty()) {
664 return data().m_crl_distribution_points[0].original_input();
665 }
666 return "";
667}

◆ crl_distribution_point_uris()

const std::vector< URI > & Botan::X509_Certificate::crl_distribution_point_uris ( ) const

Return the CRL distribution points, or empty if not set

Definition at line 657 of file x509cert.cpp.

657 {
658 return data().m_crl_distribution_points;
659}

Referenced by botan_x509_cert_view_string_values(), and to_string().

◆ crl_distribution_points()

std::vector< std::string > Botan::X509_Certificate::crl_distribution_points ( ) const

Return the CRL distribution points, or empty if not set

Definition at line 653 of file x509cert.cpp.

653 {
654 return uris_as_strings(data().m_crl_distribution_points);
655}

◆ decode_from()

void Botan::X509_Object::decode_from ( BER_Decoder & from)
overridevirtualinherited

Decode a BER encoded X509_Object See ASN1_Object::decode_from()

Implements Botan::ASN1_Object.

Definition at line 93 of file x509_obj.cpp.

93 {
94 auto data = std::make_shared<Signed_Data>();
95
96 from.start_sequence()
97 .start_sequence()
98 .raw_bytes(data->m_tbs_bits)
99 .end_cons()
100 .decode(data->m_sig_algo)
101 .decode_octet_aligned_bitstring(data->m_sig)
102 .end_cons();
103
104 m_signed_data = std::move(data);
105 force_decode();
106}

References Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_octet_aligned_bitstring(), Botan::BER_Decoder::end_cons(), Botan::BER_Decoder::raw_bytes(), and Botan::BER_Decoder::start_sequence().

Referenced by load_data().

◆ encode_into()

void Botan::X509_Object::encode_into ( DER_Encoder & to) const
overridevirtualinherited

DER encode an X509_Object See ASN1_Object::encode_into()

Implements Botan::ASN1_Object.

Definition at line 80 of file x509_obj.cpp.

80 {
81 to.start_sequence()
82 .start_sequence()
83 .raw_bytes(signed_body())
84 .end_cons()
85 .encode(signature_algorithm())
86 .encode_octet_aligned_bitstring(signature())
87 .end_cons();
88}
const std::vector< uint8_t > & signed_body() const
Definition x509_obj.cpp:66
const AlgorithmIdentifier & signature_algorithm() const
Definition x509_obj.cpp:73
const std::vector< uint8_t > & signature() const
Definition x509_obj.cpp:59

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode_octet_aligned_bitstring(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::raw_bytes(), signature(), signature_algorithm(), signed_body(), and Botan::DER_Encoder::start_sequence().

◆ extended_key_usage()

const std::vector< OID > & Botan::X509_Certificate::extended_key_usage ( ) const

Get the key usage as defined in the ExtendedKeyUsage extension of this certificate, or else an empty vector.

Returns
key usage

Definition at line 507 of file x509cert.cpp.

507 {
508 return data().m_extended_key_usage;
509}

Referenced by allowed_extended_usage(), has_ex_constraint(), and to_string().

◆ fingerprint()

std::string Botan::X509_Certificate::fingerprint ( std::string_view hash_name = "SHA-1") const
Returns
a fingerprint of the certificate
Parameters
hash_namehash function used to calculate the fingerprint

Definition at line 792 of file x509cert.cpp.

792 {
793 /*
794 * The SHA-1 and SHA-256 fingerprints are precomputed since these
795 * are the most commonly used. Especially, SHA-256 fingerprints are
796 * used for cycle detection during path construction.
797 *
798 * If SHA-1 or SHA-256 was missing at parsing time the vectors are
799 * left empty in which case we fall back to create_hex_fingerprint
800 * which will throw if the hash is unavailable.
801 */
802 if(hash_name == "SHA-256" && !data().m_fingerprint_sha256.empty()) {
803 return data().m_fingerprint_sha256;
804 } else if(hash_name == "SHA-1" && !data().m_fingerprint_sha1.empty()) {
805 return data().m_fingerprint_sha1;
806 } else {
807 return create_hex_fingerprint(this->BER_encode(), hash_name);
808 }
809}
std::vector< uint8_t > BER_encode() const
Definition asn1_obj.cpp:21
std::string create_hex_fingerprint(std::span< const uint8_t > bits, std::string_view hash_name)
Definition pk_keys.cpp:38

References Botan::ASN1_Object::BER_encode(), and Botan::create_hex_fingerprint().

Referenced by Botan::Certificate_Store_In_SQL::affirm_cert(), Botan::Certificate_Store_In_SQL::contains(), Botan::Certificate_Store_In_SQL::find_key(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::Certificate_Store_In_SQL::insert_key(), Botan::Certificate_Store_In_SQL::remove_cert(), Botan::Certificate_Store_In_SQL::revoke_cert(), and Botan::Certificate_Store_In_SQL::revoke_cert().

◆ has_constraints()

bool Botan::X509_Certificate::has_constraints ( Key_Constraints constraints) const

Returns true if and only if the specified

Parameters
constraintsare included in the key usage extension.

Typically for applications you want allowed_usage instead.

Definition at line 523 of file x509cert.cpp.

523 {
524 // Unlike allowed_usage, returns false if constraints was not set
525 return constraints().includes(usage);
526}

References constraints(), and Botan::Key_Constraints::includes().

◆ has_ex_constraint() [1/2]

bool Botan::X509_Certificate::has_ex_constraint ( const OID & ex_constraint) const

Returns true if and only if OID

Parameters
ex_constraintis included in the extended key extension.

Definition at line 586 of file x509cert.cpp.

586 {
587 const auto any_eku = OID::from_name("X509v3.AnyExtendedKeyUsage");
588 const auto ocsp_eku = OID::from_name("PKIX.OCSPSigning");
589
590 for(const auto& ext_ku : extended_key_usage()) {
591 if(ext_ku == usage) {
592 return true;
593 }
594
595 /*
596 Do not accept AnyExtendedKeyUsage for OCSP due to RFC 6960 4.2.2.2:
597
598 OCSP signing delegation SHALL be designated by the inclusion of
599 id-kp-OCSPSigning in an extended key usage certificate extension
600 included in the OCSP response signer's certificate.
601 */
602 if(ext_ku == any_eku && usage != ocsp_eku) {
603 return true;
604 }
605 }
606
607 return false;
608}
static std::optional< OID > from_name(std::string_view name)
Definition asn1_oid.cpp:66

References extended_key_usage(), and Botan::OID::from_name().

◆ has_ex_constraint() [2/2]

bool Botan::X509_Certificate::has_ex_constraint ( std::string_view ex_constraint) const

Returns true if and only if OID

Parameters
ex_constraintis included in the extended key extension.

Definition at line 582 of file x509cert.cpp.

582 {
583 return has_ex_constraint(OID::from_string(ex_constraint));
584}

References Botan::OID::from_string(), and has_ex_constraint().

Referenced by allowed_extended_usage(), allowed_usage(), and has_ex_constraint().

◆ is_CA_cert()

bool Botan::X509_Certificate::is_CA_cert ( ) const

Check whether this certificate is a CA certificate.

Returns
true if this certificate is a CA certificate

Definition at line 483 of file x509cert.cpp.

483 {
484 if(data().m_version < 3 && data().m_self_signed) {
485 return true;
486 }
487
488 return data().m_is_ca_certificate;
489}

Referenced by allowed_usage(), Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store(), to_string(), and Botan::Cert_Extension::Name_Constraints::validate().

◆ is_critical()

bool Botan::X509_Certificate::is_critical ( std::string_view ex_name) const

Check whenever a given X509 Extension is marked critical in this certificate.

Definition at line 613 of file x509cert.cpp.

613 {
615}
bool critical_extension_set(const OID &oid) const
Definition x509_ext.cpp:239
const Extensions & v3_extensions() const
Definition x509cert.cpp:519

References Botan::Extensions::critical_extension_set(), Botan::OID::from_string(), and v3_extensions().

Referenced by Botan::Cert_Extension::Name_Constraints::validate().

◆ is_self_signed()

bool Botan::X509_Certificate::is_self_signed ( ) const

Check whether this certificate is self signed. If the DN issuer and subject agree,

Returns
true if this certificate is self signed

Definition at line 384 of file x509cert.cpp.

384 {
385 return data().m_self_signed;
386}

Referenced by Botan::PKIX::check_chain(), Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store(), and to_string().

◆ is_serial_negative()

bool Botan::X509_Certificate::is_serial_negative ( ) const

Get the serial number's sign

Returns
1 iff the serial is negative.

Definition at line 448 of file x509cert.cpp.

448 {
449 return data().m_serial.is_negative();
450}

References Botan::X509_Serial_Number::is_negative().

◆ issuer_alt_name()

const AlternativeName & Botan::X509_Certificate::issuer_alt_name ( ) const

Return the issuer alternative names (DNS, IP, ...)

Definition at line 692 of file x509cert.cpp.

692 {
693 return data().m_issuer_alt_name;
694}

Referenced by botan_x509_cert_issuer_alternative_names(), botan_x509_cert_issuer_alternative_names_count(), issuer_info(), and to_string().

◆ issuer_dn()

const X509_DN & Botan::X509_Certificate::issuer_dn ( ) const

Get the certificate's issuer distinguished name (DN).

Returns
issuer DN of this certificate

Definition at line 456 of file x509cert.cpp.

456 {
457 return data().m_issuer_dn;
458}

Referenced by Botan::PKIX::check_chain(), Botan::Certificate_Store_In_Memory::find_crl_for(), Botan::Certificate_Store_In_SQL::find_crl_for(), Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store(), Botan::X509_CRL::is_revoked(), issuer_info(), Botan::OCSP::Request::Request(), and to_string().

◆ issuer_info()

std::vector< std::string > Botan::X509_Certificate::issuer_info ( std::string_view name) const

Get a value for a specific subject_info parameter name.

Parameters
namethe name of the parameter to look up.
Returns
value(s) of the specified parameter or empty if not found

Definition at line 751 of file x509cert.cpp.

751 {
752 return get_cert_user_info(req, issuer_dn(), issuer_alt_name());
753}
const AlternativeName & issuer_alt_name() const
Definition x509cert.cpp:692
const X509_DN & issuer_dn() const
Definition x509cert.cpp:456

References issuer_alt_name(), and issuer_dn().

◆ load_data()

void Botan::X509_Object::load_data ( DataSource & src)
protectedinherited

Decodes from src as either DER or PEM data, then calls force_decode()

Definition at line 24 of file x509_obj.cpp.

24 {
25 try {
26 if(ASN1::maybe_BER(in) && !PEM_Code::matches(in)) {
27 BER_Decoder dec(in, BER_Decoder::Limits::DER());
28 decode_from(dec);
29 // Call to verify_end omitted here since we have to sometimes decode
30 // multiple certificates encoded sequentially in a DataSource
31 } else {
32 std::string got_label;
33 DataSource_Memory ber(PEM_Code::decode(in, got_label));
34
35 if(got_label != PEM_label()) {
36 bool is_alternate = false;
37 for(const std::string_view alt_label : alternate_PEM_labels()) {
38 if(got_label == alt_label) {
39 is_alternate = true;
40 break;
41 }
42 }
43
44 if(!is_alternate) {
45 throw Decoding_Error("Unexpected PEM label for " + PEM_label() + " of " + got_label);
46 }
47 }
48
49 BER_Decoder dec(ber, BER_Decoder::Limits::DER());
50 decode_from(dec);
51 // Call to verify_end omitted here since we have to sometimes decode
52 // multiple certificates encoded sequentially in a DataSource
53 }
54 } catch(Decoding_Error& e) {
55 throw Decoding_Error(PEM_label() + " decoding", e);
56 }
57}
static Limits DER()
Definition ber_dec.h:42
void decode_from(BER_Decoder &from) override
Definition x509_obj.cpp:93
virtual std::vector< std::string > alternate_PEM_labels() const
Definition x509_obj.h:102
virtual std::string PEM_label() const =0
bool maybe_BER(DataSource &source)
Definition asn1_obj.cpp:231
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:143
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:62

References alternate_PEM_labels(), Botan::PEM_Code::decode(), decode_from(), Botan::BER_Decoder::Limits::DER(), Botan::PEM_Code::matches(), Botan::ASN1::maybe_BER(), and PEM_label().

Referenced by Botan::PKCS10_Request::PKCS10_Request(), Botan::PKCS10_Request::PKCS10_Request(), Botan::X509_Certificate::X509_Certificate(), Botan::X509_Certificate::X509_Certificate(), Botan::X509_CRL::X509_CRL(), Botan::X509_CRL::X509_CRL(), and X509_Object().

◆ load_subject_public_key()

std::unique_ptr< Public_Key > Botan::X509_Certificate::load_subject_public_key ( ) const

Create a public key object associated with the public key bits in this certificate. If the public key bits was valid for X.509 encoding purposes but invalid algorithmically (for example, RSA with an even modulus) that will be detected at this point, and an exception will be thrown.

Returns
subject public key of this certificate

Definition at line 766 of file x509cert.cpp.

766 {
767 return this->subject_public_key();
768}
std::unique_ptr< Public_Key > subject_public_key() const
Definition x509cert.cpp:758

References subject_public_key().

◆ make_signed()

std::vector< uint8_t > Botan::X509_Object::make_signed ( PK_Signer & signer,
RandomNumberGenerator & rng,
const AlgorithmIdentifier & alg_id,
std::span< const uint8_t > tbs )
staticinherited

Create a signed X509 object.

Parameters
signerthe signer used to sign the object
Random Number Generatorsthe random number generator to use
alg_idthe algorithm identifier of the signature scheme
tbsthe tbs bits to be signed
Returns
signed X509 object

Definition at line 156 of file x509_obj.cpp.

159 {
160 const std::vector<uint8_t> signature = signer.sign_message(tbs_bits, rng);
161
162 std::vector<uint8_t> output;
163 DER_Encoder(output)
164 .start_sequence()
165 .raw_bytes(tbs_bits)
166 .encode(algo)
167 .encode_octet_aligned_bitstring(signature)
168 .end_cons();
169
170 return output;
171}

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode_octet_aligned_bitstring(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::raw_bytes(), Botan::PK_Signer::sign_message(), signature(), and Botan::DER_Encoder::start_sequence().

Referenced by Botan::PKCS10_Request::create(), and Botan::X509_CA::make_cert().

◆ matches_dns_name() [1/2]

bool Botan::X509_Certificate::matches_dns_name ( const DNSName & name) const

Check whether name matches the subject DNS names in this certificate.

Compares against the dnsName entries in the subjectAltName, with the RFC 6125 wildcard rules. If the certificate has no SAN at all, falls back to a wildcard comparison against the subject CN.

Definition at line 815 of file x509cert.cpp.

815 {
816 const auto& sans = subject_alt_name().dns_names();
817 if(!sans.empty()) {
818 for(const auto& san : sans) {
819 if(name.matches_wildcard(san.name())) {
820 return true;
821 }
822 }
823 return false;
824 }
825
826 /*
827 Fall back to CN for DNS name only if no SAN is included
828 We assume if the issuer knew about SAN then they would have included
829 the DNS name there if the intention was to provide such a name.
830 */
831 if(!data().m_subject_alt_name_exists) {
832 for(const auto& cn : subject_dn().get_attribute("CN")) {
833 if(auto cn_dns = DNSName::from_san_string(cn)) {
834 if(name.matches_wildcard(cn_dns->name())) {
835 return true;
836 }
837 }
838 }
839 }
840
841 return false;
842}
const std::set< DNSName > & dns_names() const
Return the set of DNS names included in this alternative name.
Definition pkix_types.h:409
static std::optional< DNSName > from_san_string(std::string_view name)
Definition dns_name.cpp:149
const X509_DN & subject_dn() const
Definition x509cert.cpp:460
const AlternativeName & subject_alt_name() const
Definition x509cert.cpp:688

References Botan::AlternativeName::dns_names(), Botan::DNSName::from_san_string(), Botan::DNSName::matches_wildcard(), subject_alt_name(), and subject_dn().

◆ matches_dns_name() [2/2]

bool Botan::X509_Certificate::matches_dns_name ( std::string_view name) const

Check if a certain DNS name matches up with the information in the cert

The string variant additionally accepts a dotted-quad IPv4 input, in which case the SAN for IPv4 addresses will be checked. Prefer the typed overloads for IP and DNS matching.

Parameters
nameDNS name to match

Definition at line 852 of file x509cert.cpp.

852 {
853 if(name.empty()) {
854 return false;
855 }
856
857 if(auto req_ipv4 = IPv4Address::from_string(name)) {
858 return matches_ip(*req_ipv4);
859 }
860
861 if(auto req_ipv6 = IPv6Address::from_string(name)) {
862 return matches_ip(*req_ipv6);
863 }
864
865 if(auto parsed = DNSName::from_string(name)) {
866 return matches_dns_name(*parsed);
867 }
868
869 return false;
870}
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:136
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)
bool matches_dns_name(std::string_view name) const
Definition x509cert.cpp:852
bool matches_ip(const IPv4Address &address) const
Definition x509cert.cpp:844

References Botan::DNSName::from_string(), Botan::IPv4Address::from_string(), Botan::IPv6Address::from_string(), matches_dns_name(), and matches_ip().

Referenced by matches_dns_name().

◆ matches_ip() [1/2]

bool Botan::X509_Certificate::matches_ip ( const IPv4Address & address) const

Check whether address appears as an iPAddress entry in the subjectAltName.

Definition at line 844 of file x509cert.cpp.

844 {
845 return subject_alt_name().ipv4_addresses().contains(address);
846}
const std::set< IPv4Address > & ipv4_addresses() const
Return the set of IPv4 addresses included in this alternative name.
Definition pkix_types.h:420

References Botan::AlternativeName::ipv4_addresses(), and subject_alt_name().

Referenced by matches_dns_name().

◆ matches_ip() [2/2]

bool Botan::X509_Certificate::matches_ip ( const IPv6Address & address) const

Check whether address appears as an iPAddress entry in the subjectAltName.

Definition at line 848 of file x509cert.cpp.

848 {
849 return subject_alt_name().ipv6_addresses().contains(address);
850}
const std::set< IPv6Address > & ipv6_addresses() const
Return the set of IPv6 addresses included in this alternative name.
Definition pkix_types.h:423

References Botan::AlternativeName::ipv6_addresses(), and subject_alt_name().

◆ name_constraints()

const NameConstraints & Botan::X509_Certificate::name_constraints ( ) const

Get the name constraints as defined in the NameConstraints extension of this certificate.

Returns
name constraints

Definition at line 515 of file x509cert.cpp.

515 {
516 return data().m_name_constraints;
517}

Referenced by botan_x509_cert_excluded_name_constraints(), botan_x509_cert_permitted_name_constraints(), and to_string().

◆ not_after()

const X509_Time & Botan::X509_Certificate::not_after ( ) const

Get the notAfter of the certificate as X509_Time

Returns
notAfter of the certificate

Definition at line 392 of file x509cert.cpp.

392 {
393 return data().m_not_after;
394}

Referenced by Botan::PKIX::check_chain(), and to_string().

◆ not_before()

const X509_Time & Botan::X509_Certificate::not_before ( ) const

Get the notBefore of the certificate as X509_Time

Returns
notBefore of the certificate

Definition at line 388 of file x509cert.cpp.

388 {
389 return data().m_not_before;
390}

Referenced by Botan::PKIX::check_chain(), and to_string().

◆ ocsp_responder()

std::string Botan::X509_Certificate::ocsp_responder ( ) const

Return the listed address of an OCSP responder, or empty if not set

Definition at line 630 of file x509cert.cpp.

630 {
631 if(data().m_ocsp_responders.empty()) {
632 return {};
633 }
634 return data().m_ocsp_responders[0].original_input();
635}

Referenced by to_string().

◆ ocsp_responder_uris()

const std::vector< URI > & Botan::X509_Certificate::ocsp_responder_uris ( ) const

Return the listed addresses of OCSP responders, or empty if not set

Definition at line 641 of file x509cert.cpp.

641 {
642 return data().m_ocsp_responders;
643}

Referenced by botan_x509_cert_view_string_values(), and to_string().

◆ ocsp_responders()

std::vector< std::string > Botan::X509_Certificate::ocsp_responders ( ) const

Return the listed addresses of OCSP responders, or empty if not set

Definition at line 637 of file x509cert.cpp.

637 {
638 return uris_as_strings(data().m_ocsp_responders);
639}

Referenced by to_string().

◆ operator<()

bool Botan::X509_Certificate::operator< ( const X509_Certificate & other) const

Impose an arbitrary (but consistent) ordering, eg to allow sorting a container of certificate objects.

Returns
true if this is less than other by some unspecified criteria

Definition at line 880 of file x509cert.cpp.

880 {
881 /* If signature values are not equal, sort by lexicographic ordering of that */
882 if(this->signature() != other.signature()) {
883 return (this->signature() < other.signature());
884 }
885
886 // Then compare the signed contents
887 return this->signed_body() < other.signed_body();
888}

References Botan::X509_Object::signature(), Botan::X509_Object::signed_body(), and X509_Certificate().

◆ operator=() [1/2]

X509_Certificate & Botan::X509_Certificate::operator= ( const X509_Certificate & other)
default

References X509_Certificate().

◆ operator=() [2/2]

X509_Certificate & Botan::X509_Certificate::operator= ( X509_Certificate && other)
default

References X509_Certificate().

◆ operator==()

bool Botan::X509_Certificate::operator== ( const X509_Certificate & other) const

Check to certificates for equality.

Returns
true both certificates are (binary) equal

Definition at line 875 of file x509cert.cpp.

875 {
876 return (this->signature() == other.signature() && this->signature_algorithm() == other.signature_algorithm() &&
877 this->signed_body() == other.signed_body());
878}

References Botan::X509_Object::signature(), Botan::X509_Object::signature_algorithm(), Botan::X509_Object::signed_body(), and X509_Certificate().

◆ path_length_constraint()

std::optional< size_t > Botan::X509_Certificate::path_length_constraint ( ) const

Get the path length constraint as defined in the BasicConstraints extension.

Returns nullopt if either the extension is not set in the certificate, or if the pathLenConstraint field was absent from the extension.

Returns
path limit

Definition at line 499 of file x509cert.cpp.

499 {
500 return data().m_path_len_constraint;
501}

Referenced by Botan::PKIX::check_chain(), and to_string().

◆ path_limit()

uint32_t Botan::X509_Certificate::path_limit ( ) const

Get the path length constraint as defined in the BasicConstraints extension.

This returns an arbitrary value if the extension is not set (either 32 for v1 self-signed certificates, or else Cert_Extension::NO_CERT_PATH_LIMIT for v3 certificates without the extension)

Prefer path_length_constraint

Returns
path limit

Definition at line 491 of file x509cert.cpp.

491 {
492 if(data().m_version < 3 && data().m_self_signed) {
493 return 32; // in theory infinite, but this is more than enough
494 }
495
496 return static_cast<uint32_t>(data().m_path_len_constraint.value_or(Cert_Extension::NO_CERT_PATH_LIMIT));
497}

◆ PEM_encode()

std::string Botan::X509_Object::PEM_encode ( ) const
inherited
Returns
PEM encoding of this

Definition at line 111 of file x509_obj.cpp.

111 {
113}
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39

References Botan::ASN1_Object::BER_encode(), Botan::PEM_Code::encode(), and PEM_label().

◆ raw_issuer_dn()

const std::vector< uint8_t > & Botan::X509_Certificate::raw_issuer_dn ( ) const

Raw issuer DN bits

Definition at line 464 of file x509cert.cpp.

464 {
465 return data().m_issuer_dn_bits;
466}

Referenced by botan_x509_cert_view_binary_values().

◆ raw_issuer_dn_sha1()

std::span< const uint8_t, 20 > Botan::X509_Certificate::raw_issuer_dn_sha1 ( ) const

SHA-1 of Raw issuer DN

Definition at line 784 of file x509cert.cpp.

784 {
785 return data().m_issuer_dn_bits_sha1;
786}

Referenced by Botan::OCSP::CertID::is_id_for().

◆ raw_issuer_dn_sha256()

const std::vector< uint8_t > & Botan::X509_Certificate::raw_issuer_dn_sha256 ( ) const

SHA-256 of Raw issuer DN

Definition at line 770 of file x509cert.cpp.

770 {
771 if(data().m_issuer_dn_bits_sha256.empty()) {
772 throw Encoding_Error("X509_Certificate::raw_issuer_dn_sha256 called but SHA-256 disabled in build");
773 }
774 return data().m_issuer_dn_bits_sha256;
775}

Referenced by Botan::OCSP::CertID::is_id_for().

◆ raw_subject_dn()

const std::vector< uint8_t > & Botan::X509_Certificate::raw_subject_dn ( ) const

Raw subject DN

Definition at line 468 of file x509cert.cpp.

468 {
469 return data().m_subject_dn_bits;
470}

Referenced by botan_x509_cert_view_binary_values(), and Botan::OCSP::CertID::CertID().

◆ raw_subject_dn_sha1()

std::span< const uint8_t, 20 > Botan::X509_Certificate::raw_subject_dn_sha1 ( ) const

SHA-1 of Raw subject DN

Definition at line 788 of file x509cert.cpp.

788 {
789 return data().m_subject_dn_bits_sha1;
790}

◆ raw_subject_dn_sha256()

const std::vector< uint8_t > & Botan::X509_Certificate::raw_subject_dn_sha256 ( ) const

SHA-256 of Raw subject DN

Definition at line 777 of file x509cert.cpp.

777 {
778 if(data().m_subject_dn_bits_sha256.empty()) {
779 throw Encoding_Error("X509_Certificate::raw_subject_dn_sha256 called but SHA-256 disabled in build");
780 }
781 return data().m_subject_dn_bits_sha256;
782}

Referenced by Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store().

◆ serial()

const X509_Serial_Number & Botan::X509_Certificate::serial ( ) const

Get the serial number of this certificate

Definition at line 444 of file x509cert.cpp.

444 {
445 return data().m_serial;
446}

Referenced by botan_x509_cert_serial_number(), Botan::PKIX::check_chain(), Botan::OCSP::CertID::is_id_for(), Botan::X509_CRL::is_revoked(), and to_string().

◆ serial_number()

const std::vector< uint8_t > & Botan::X509_Certificate::serial_number ( ) const

Get the serial number of this certificate.

Note this is the absolute value; the (rare, non-conforming) negative serial numbers are indistinguishable from their positive counterpart. Prefer serial() which preserves the sign.

Returns
certificates serial number

Definition at line 440 of file x509cert.cpp.

440 {
441 return data().m_serial_bits;
442}

Referenced by botan_x509_cert_view_binary_values().

◆ signature()

const std::vector< uint8_t > & Botan::X509_Object::signature ( ) const
inherited
Returns
signature on tbs_data()

Definition at line 59 of file x509_obj.cpp.

59 {
60 if(!m_signed_data) {
61 throw Invalid_State("X509_Object uninitialized");
62 }
63 return m_signed_data->m_sig;
64}

Referenced by encode_into(), make_signed(), Botan::X509_Certificate::operator<(), Botan::X509_Certificate::operator==(), and verify_signature().

◆ signature_algorithm()

const AlgorithmIdentifier & Botan::X509_Object::signature_algorithm ( ) const
inherited
Returns
signature algorithm that was used to generate signature

Definition at line 73 of file x509_obj.cpp.

73 {
74 if(!m_signed_data) {
75 throw Invalid_State("X509_Object uninitialized");
76 }
77 return m_signed_data->m_sig_algo;
78}

Referenced by Botan::PKIX::check_chain(), encode_into(), Botan::X509_Certificate::operator==(), Botan::X509_Certificate::to_string(), and verify_signature().

◆ signed_body()

const std::vector< uint8_t > & Botan::X509_Object::signed_body ( ) const
inherited
Returns
signed body

Definition at line 66 of file x509_obj.cpp.

66 {
67 if(!m_signed_data) {
68 throw Invalid_State("X509_Object uninitialized");
69 }
70 return m_signed_data->m_tbs_bits;
71}

Referenced by encode_into(), Botan::X509_Certificate::operator<(), Botan::X509_Certificate::operator==(), tbs_data(), and verify_signature().

◆ skip_revocation_check()

bool Botan::X509_Certificate::skip_revocation_check ( ) const

Return true if revocation status checking of this certificate should be skipped, as indicated by the presence of either the noRevAvail extension (RFC 9608) or the ocsp-nocheck extension (RFC 6960).

Definition at line 452 of file x509cert.cpp.

452 {
453 return data().m_skip_revocation_check;
454}

Referenced by Botan::PKIX::check_ocsp(), and to_string().

◆ subject_alt_name()

const AlternativeName & Botan::X509_Certificate::subject_alt_name ( ) const

◆ subject_dn()

◆ subject_email_addresses()

std::vector< EmailAddress > Botan::X509_Certificate::subject_email_addresses ( ) const

Return all email addresses associated with the subject of this certificate, in parsed form.

This combines RFC 822 names from the subjectAltName extension with email addresses carried in the subject DN's emailAddress attribute (the latter is the legacy location for subject email, see RFC 5280 4.2.1.10). DN attribute values that fail to parse as a mailbox are silently skipped.

Definition at line 669 of file x509cert.cpp.

669 {
670 const auto& san_emails = subject_alt_name().email_addresses();
671
672 std::vector<EmailAddress> out;
673 out.reserve(san_emails.size());
674
675 for(const auto& addr : san_emails) {
676 out.push_back(addr);
677 }
678
679 for(const auto& dn_email_str : subject_dn().get_attribute("PKCS9.EmailAddress")) {
680 if(auto parsed = EmailAddress::from_string(dn_email_str)) {
681 out.push_back(std::move(*parsed));
682 }
683 }
684
685 return out;
686}
const std::set< EmailAddress > & email_addresses() const
Return the set of email addresses included in this alternative name.
Definition pkix_types.h:400
static std::optional< EmailAddress > from_string(std::string_view addr)
Definition email.cpp:78

References Botan::AlternativeName::email_addresses(), Botan::EmailAddress::from_string(), subject_alt_name(), and subject_dn().

◆ subject_info()

std::vector< std::string > Botan::X509_Certificate::subject_info ( std::string_view name) const

Get a value for a specific subject_info parameter name.

Parameters
namethe name of the parameter to look up.
Returns
value(s) of the specified parameter or empty if not found

Definition at line 744 of file x509cert.cpp.

744 {
745 return get_cert_user_info(req, subject_dn(), subject_alt_name());
746}

References subject_alt_name(), and subject_dn().

Referenced by Botan::NameConstraints::is_excluded(), and Botan::NameConstraints::is_permitted().

◆ subject_key_id()

const std::vector< uint8_t > & Botan::X509_Certificate::subject_key_id ( ) const

Get the DER encoded SubjectKeyIdentifier of this certificate.

Returns
DER encoded SubjectKeyIdentifier

Definition at line 436 of file x509cert.cpp.

436 {
437 return data().m_subject_key_id;
438}

Referenced by botan_x509_cert_view_binary_values(), Botan::X509_CA::choose_extensions(), Botan::Certificate_Store::contains(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::Certificate_Store_In_SQL::remove_cert(), and to_string().

◆ subject_public_key()

std::unique_ptr< Public_Key > Botan::X509_Certificate::subject_public_key ( ) const

Create a public key object associated with the public key bits in this certificate. If the public key bits was valid for X.509 encoding purposes but invalid algorithmically (for example, RSA with an even modulus) that will be detected at this point, and an exception will be thrown.

Returns
subject public key of this certificate

Definition at line 758 of file x509cert.cpp.

758 {
759 try {
760 return std::unique_ptr<Public_Key>(X509::load_key(subject_public_key_info()));
761 } catch(std::exception& e) {
762 throw Decoding_Error("X509_Certificate::subject_public_key", e);
763 }
764}
const std::vector< uint8_t > & subject_public_key_info() const
Definition x509cert.cpp:412
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition x509_key.cpp:28

References Botan::X509::load_key(), and subject_public_key_info().

Referenced by Botan::PKIX::check_chain(), Botan::PKIX::check_crl(), load_subject_public_key(), to_string(), Botan::TLS::Certificate_Verify_12::verify(), and Botan::OCSP::Response::verify_signature().

◆ subject_public_key_algo()

const AlgorithmIdentifier & Botan::X509_Certificate::subject_public_key_algo ( ) const

Return the algorithm identifier of the public key

Definition at line 396 of file x509cert.cpp.

396 {
397 return data().m_subject_public_key_algid;
398}

Referenced by to_string().

◆ subject_public_key_bits()

const std::vector< uint8_t > & Botan::X509_Certificate::subject_public_key_bits ( ) const

Get the public key associated with this certificate. This includes the outer AlgorithmIdentifier

Returns
subject public key of this certificate

Definition at line 408 of file x509cert.cpp.

408 {
409 return data().m_subject_public_key_bits;
410}

◆ subject_public_key_bitstring()

const std::vector< uint8_t > & Botan::X509_Certificate::subject_public_key_bitstring ( ) const

Get the bit string of the public key associated with this certificate

Returns
public key bits

Definition at line 416 of file x509cert.cpp.

416 {
417 return data().m_subject_public_key_bitstring;
418}

Referenced by Botan::OCSP::CertID::CertID(), and to_string().

◆ subject_public_key_bitstring_sha1()

const std::vector< uint8_t > & Botan::X509_Certificate::subject_public_key_bitstring_sha1 ( ) const

Get the SHA-1 bit string of the public key associated with this certificate. This is used for OCSP among other protocols. This function will throw if SHA-1 is not available.

Returns
hash of subject public key of this certificate

Definition at line 420 of file x509cert.cpp.

420 {
421 if(data().m_subject_public_key_bitstring_sha1.empty()) {
422 throw Encoding_Error("X509_Certificate::subject_public_key_bitstring_sha1 called but SHA-1 disabled in build");
423 }
424
425 return data().m_subject_public_key_bitstring_sha1;
426}

Referenced by Botan::Flatfile_Certificate_Store::Flatfile_Certificate_Store(), and Botan::OCSP::CertID::is_id_for().

◆ subject_public_key_bitstring_sha256()

std::span< const uint8_t, 32 > Botan::X509_Certificate::subject_public_key_bitstring_sha256 ( ) const

Get the SHA-256 bit string of the public key associated with this certificate. This is used for OCSP among other protocols.

Returns
hash of subject public key of this certificate

Definition at line 428 of file x509cert.cpp.

428 {
429 return data().m_subject_public_key_bitstring_sha256;
430}

Referenced by Botan::OCSP::CertID::is_id_for().

◆ subject_public_key_info()

const std::vector< uint8_t > & Botan::X509_Certificate::subject_public_key_info ( ) const

Get the SubjectPublicKeyInfo associated with this certificate.

Returns
subject public key info of this certificate

Definition at line 412 of file x509cert.cpp.

412 {
413 return data().m_subject_public_key_bits_seq;
414}

Referenced by botan_x509_cert_view_binary_values(), and subject_public_key().

◆ tag()

X509_Certificate::Tag Botan::X509_Certificate::tag ( ) const

◆ tbs_data()

std::vector< uint8_t > Botan::X509_Object::tbs_data ( ) const
inherited

The underlying data that is to be or was signed

Returns
data that is or was signed

Definition at line 118 of file x509_obj.cpp.

118 {
120}
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:208

References Botan::ASN1::put_in_sequence(), and signed_body().

◆ to_string()

std::string Botan::X509_Certificate::to_string ( ) const
Returns
a free-form string describing the certificate

Definition at line 944 of file x509cert.cpp.

944 {
945 std::ostringstream out;
946
947 out << "Version: " << this->x509_version() << "\n";
948 out << "Subject: " << subject_dn() << "\n";
949 out << "Issuer: " << issuer_dn() << "\n";
950 out << "Issued: " << this->not_before().readable_string() << "\n";
951 out << "Expires: " << this->not_after().readable_string() << "\n";
952
953 try {
954 auto pubkey = this->subject_public_key();
955 out << "Public Key [" << pubkey->algo_name() << "-" << pubkey->key_length() << "]\n\n";
956 out << X509::PEM_encode(*pubkey) << "\n";
957 } catch(const Decoding_Error& ex) {
958 const AlgorithmIdentifier& alg_id = this->subject_public_key_algo();
959 out << "Public Key Invalid!\n"
960 << " OID: " << alg_id.oid().to_formatted_string() << "\n"
961 << " Error: " << ex.what() << "\n"
962 << " Hex: " << hex_encode(this->subject_public_key_bitstring()) << "\n";
963 }
964
965 format_alt_name(out, "Subject Alternative Name", this->subject_alt_name());
966
967 out << "Constraints:\n";
968 const Key_Constraints constraints = this->constraints();
969 if(constraints.empty()) {
970 out << " No key constraints set\n";
971 } else {
973 out << " Digital Signature\n";
974 }
976 out << " Non-Repudiation\n";
977 }
979 out << " Key Encipherment\n";
980 }
982 out << " Data Encipherment\n";
983 }
985 out << " Key Agreement\n";
986 }
988 out << " Cert Sign\n";
989 }
991 out << " CRL Sign\n";
992 }
994 out << " Encipher Only\n";
995 }
997 out << " Decipher Only\n";
998 }
999 }
1000
1001 if(this->is_CA_cert()) {
1002 out << "Basic Constraints: CA";
1003 if(const auto path_len = this->path_length_constraint()) {
1004 out << ", path length " << *path_len;
1005 }
1006 out << "\n";
1007 }
1008
1009 const std::vector<OID>& policies = this->certificate_policy_oids();
1010 if(!policies.empty()) {
1011 out << "Policies: "
1012 << "\n";
1013 for(const auto& oid : policies) {
1014 out << " " << oid.to_string() << "\n";
1015 }
1016 }
1017
1018 const std::vector<OID>& ex_constraints = this->extended_key_usage();
1019 if(!ex_constraints.empty()) {
1020 out << "Extended Constraints:\n";
1021 for(auto&& oid : ex_constraints) {
1022 out << " " << oid.to_formatted_string() << "\n";
1023 }
1024 }
1025
1026 const NameConstraints& name_constraints = this->name_constraints();
1027
1028 if(!name_constraints.permitted().empty() || !name_constraints.excluded().empty()) {
1029 out << "Name Constraints:\n";
1030
1031 if(!name_constraints.permitted().empty()) {
1032 out << " Permit";
1033 for(const auto& st : name_constraints.permitted()) {
1034 out << " " << st.base();
1035 }
1036 out << "\n";
1037 }
1038
1039 if(!name_constraints.excluded().empty()) {
1040 out << " Exclude";
1041 for(const auto& st : name_constraints.excluded()) {
1042 out << " " << st.base();
1043 }
1044 out << "\n";
1045 }
1046 }
1047
1048 const auto& ocsp_responders = this->ocsp_responder_uris();
1049 if(!ocsp_responders.empty()) {
1050 out << "OCSP Responders:\n";
1051 for(const auto& ocsp_responder : ocsp_responders) {
1052 out << " URI: " << ocsp_responder.original_input() << "\n";
1053 }
1054 }
1055
1056 const auto& ca_issuers = this->ca_issuer_uris();
1057 if(!ca_issuers.empty()) {
1058 out << "CA Issuers:\n";
1059 for(const auto& ca_issuer : ca_issuers) {
1060 out << " URI: " << ca_issuer.original_input() << "\n";
1061 }
1062 }
1063
1064 for(const auto& cdp : crl_distribution_point_uris()) {
1065 out << "CRL " << cdp.original_input() << "\n";
1066 }
1067
1068 out << "Signature algorithm: " << this->signature_algorithm().oid().to_formatted_string() << "\n";
1069
1070 out << "Serial number: " << this->serial().to_string() << "\n";
1071
1072 if(!this->authority_key_id().empty()) {
1073 out << "Authority keyid: " << hex_encode(this->authority_key_id()) << "\n";
1074 }
1075
1076 if(!this->subject_key_id().empty()) {
1077 out << "Subject keyid: " << hex_encode(this->subject_key_id()) << "\n";
1078 }
1079
1080 format_alt_name(out, "Issuer Alternative Name", this->issuer_alt_name());
1081
1082 if(this->skip_revocation_check()) {
1083 out << "Revocation status checking is disabled for this certificate\n";
1084 }
1085
1086 if(this->is_self_signed()) {
1087 out << "Certificate is self signed\n";
1088 }
1089
1090 return out.str();
1091}
std::string readable_string() const
Returns a human friendly string representation of no particular formatting.
const OID & oid() const
Definition asn1_obj.h:688
std::string to_formatted_string() const
Definition asn1_oid.cpp:137
const NameConstraints & name_constraints() const
Definition x509cert.cpp:515
std::vector< std::string > ocsp_responders() const
Definition x509cert.cpp:637
const std::vector< URI > & ocsp_responder_uris() const
Definition x509cert.cpp:641
bool skip_revocation_check() const
Definition x509cert.cpp:452
const X509_Serial_Number & serial() const
Definition x509cert.cpp:444
const X509_Time & not_after() const
Definition x509cert.cpp:392
const std::vector< uint8_t > & authority_key_id() const
Definition x509cert.cpp:432
const std::vector< uint8_t > & subject_key_id() const
Definition x509cert.cpp:436
std::optional< size_t > path_length_constraint() const
Definition x509cert.cpp:499
std::string ocsp_responder() const
Definition x509cert.cpp:630
const std::vector< URI > & crl_distribution_point_uris() const
Definition x509cert.cpp:657
uint32_t x509_version() const
Definition x509cert.cpp:380
const std::vector< URI > & ca_issuer_uris() const
Definition x509cert.cpp:649
const std::vector< OID > & certificate_policy_oids() const
Definition x509cert.cpp:511
bool is_self_signed() const
Definition x509cert.cpp:384
const AlgorithmIdentifier & subject_public_key_algo() const
Definition x509cert.cpp:396
std::vector< std::string > ca_issuers() const
Definition x509cert.cpp:645
const std::vector< uint8_t > & subject_public_key_bitstring() const
Definition x509cert.cpp:416
const X509_Time & not_before() const
Definition x509cert.cpp:388
std::string to_string() const
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:21
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:34

References authority_key_id(), ca_issuer_uris(), ca_issuers(), certificate_policy_oids(), constraints(), crl_distribution_point_uris(), Botan::Key_Constraints::CrlSign, Botan::Key_Constraints::DataEncipherment, Botan::Key_Constraints::DecipherOnly, Botan::Key_Constraints::DigitalSignature, Botan::Key_Constraints::empty(), Botan::Key_Constraints::EncipherOnly, extended_key_usage(), Botan::hex_encode(), is_CA_cert(), is_self_signed(), issuer_alt_name(), issuer_dn(), Botan::Key_Constraints::KeyAgreement, Botan::Key_Constraints::KeyCertSign, Botan::Key_Constraints::KeyEncipherment, name_constraints(), Botan::Key_Constraints::NonRepudiation, not_after(), not_before(), ocsp_responder(), ocsp_responder_uris(), ocsp_responders(), Botan::AlgorithmIdentifier::oid(), path_length_constraint(), Botan::X509::PEM_encode(), Botan::NameConstraints::permitted(), Botan::ASN1_Time::readable_string(), serial(), Botan::X509_Object::signature_algorithm(), skip_revocation_check(), subject_alt_name(), subject_dn(), subject_key_id(), subject_public_key(), subject_public_key_algo(), subject_public_key_bitstring(), Botan::OID::to_formatted_string(), Botan::X509_Serial_Number::to_string(), Botan::Exception::what(), and x509_version().

◆ v2_issuer_key_id()

const std::vector< uint8_t > & Botan::X509_Certificate::v2_issuer_key_id ( ) const

Return the v2 issuer key ID. v2 key IDs are almost never used, instead see v3_subject_key_id.

Definition at line 400 of file x509cert.cpp.

400 {
401 return data().m_v2_issuer_key_id;
402}

Referenced by Botan::PKIX::check_chain().

◆ v2_subject_key_id()

const std::vector< uint8_t > & Botan::X509_Certificate::v2_subject_key_id ( ) const

Return the v2 subject key ID. v2 key IDs are almost never used, instead see v3_subject_key_id.

Definition at line 404 of file x509cert.cpp.

404 {
405 return data().m_v2_subject_key_id;
406}

Referenced by Botan::PKIX::check_chain().

◆ v3_extensions()

const Extensions & Botan::X509_Certificate::v3_extensions ( ) const

Get all extensions of this certificate.

Returns
certificate extensions

Definition at line 519 of file x509cert.cpp.

519 {
520 return data().m_v3_extensions;
521}

Referenced by botan_x509_cert_issuer_alternative_names(), botan_x509_cert_subject_alternative_names(), Botan::PKIX::check_chain(), Botan::distribution_point_match(), is_critical(), and Botan::Cert_Extension::NoRevocationAvailable::validate().

◆ verify_signature()

std::pair< Certificate_Status_Code, std::string > Botan::X509_Object::verify_signature ( const Public_Key & key) const
inherited

Check the signature on this data

Parameters
keythe public key purportedly used to sign this data
Returns
status of the signature - OK if verified or otherwise an indicator of the problem preventing verification, along with the hash function that was used, for further policy checks. The second parameter is empty unless the validation was successful.

Definition at line 130 of file x509_obj.cpp.

130 {
131 try {
132 PK_Verifier verifier(pub_key, signature_algorithm());
133 const auto& tbs = signed_body();
134 verifier.update(ASN1::der_sequence_header(tbs.size()));
135 verifier.update(tbs);
136 const bool valid = verifier.check_signature(signature());
137
138 if(valid) {
139 return std::make_pair(Certificate_Status_Code::VERIFIED, verifier.hash_function());
140 } else {
141 return std::make_pair(Certificate_Status_Code::SIGNATURE_ERROR, "");
142 }
143 } catch(Decoding_Error&) {
145 } catch(Algorithm_Not_Found&) {
146 return std::make_pair(Certificate_Status_Code::SIGNATURE_ALGO_UNKNOWN, "");
147 } catch(...) {
148 // This shouldn't happen, fallback to generic signature error
149 return std::make_pair(Certificate_Status_Code::SIGNATURE_ERROR, "");
150 }
151}
std::vector< uint8_t > der_sequence_header(size_t contents_len)
Definition der_enc.cpp:71

References Botan::PK_Verifier::check_signature(), Botan::ASN1::der_sequence_header(), Botan::PK_Verifier::hash_function(), signature(), Botan::SIGNATURE_ALGO_BAD_PARAMS, Botan::SIGNATURE_ALGO_UNKNOWN, signature_algorithm(), Botan::SIGNATURE_ERROR, signed_body(), Botan::PK_Verifier::update(), and Botan::VERIFIED.

Referenced by Botan::PKIX::check_chain(), and check_signature().

◆ x509_version()

uint32_t Botan::X509_Certificate::x509_version ( ) const

Get the X509 version of this certificate object.

Returns
X509 version

Definition at line 380 of file x509cert.cpp.

380 {
381 return static_cast<uint32_t>(data().m_version);
382}

Referenced by Botan::PKIX::check_chain(), and to_string().


The documentation for this class was generated from the following files: