9#include <botan/x509cert.h>
11#include <botan/ber_dec.h>
12#include <botan/bigint.h>
13#include <botan/hash.h>
15#include <botan/pk_keys.h>
16#include <botan/x509_ext.h>
17#include <botan/x509_key.h>
18#include <botan/internal/parsing.h>
24struct X509_Certificate_Data {
25 std::vector<uint8_t> m_serial;
26 AlgorithmIdentifier m_sig_algo_inner;
29 std::vector<uint8_t> m_issuer_dn_bits;
30 std::vector<uint8_t> m_subject_dn_bits;
33 std::vector<uint8_t> m_subject_public_key_bits;
34 std::vector<uint8_t> m_subject_public_key_bits_seq;
35 std::vector<uint8_t> m_subject_public_key_bitstring;
36 std::vector<uint8_t> m_subject_public_key_bitstring_sha1;
37 AlgorithmIdentifier m_subject_public_key_algid;
39 std::vector<uint8_t> m_v2_issuer_key_id;
40 std::vector<uint8_t> m_v2_subject_key_id;
41 Extensions m_v3_extensions;
43 std::vector<OID> m_extended_key_usage;
44 std::vector<uint8_t> m_authority_key_id;
45 std::vector<uint8_t> m_subject_key_id;
46 std::vector<OID> m_cert_policies;
48 std::vector<std::string> m_crl_distribution_points;
49 std::string m_ocsp_responder;
50 std::vector<std::string> m_ca_issuers;
52 std::vector<uint8_t> m_issuer_dn_bits_sha256;
53 std::vector<uint8_t> m_subject_dn_bits_sha256;
55 std::string m_fingerprint_sha1;
56 std::string m_fingerprint_sha256;
58 AlternativeName m_subject_alt_name;
59 AlternativeName m_issuer_alt_name;
60 NameConstraints m_name_constraints;
63 size_t m_path_len_constraint = 0;
64 Key_Constraints m_key_constraints;
65 bool m_self_signed =
false;
66 bool m_is_ca_certificate =
false;
67 bool m_serial_negative =
false;
68 bool m_subject_alt_name_exists =
false;
71std::string X509_Certificate::PEM_label()
const {
75std::vector<std::string> X509_Certificate::alternate_PEM_labels()
const {
76 return {
"X509 CERTIFICATE"};
93#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
102std::unique_ptr<X509_Certificate_Data> parse_x509_cert_body(
const X509_Object& obj) {
103 auto data = std::make_unique<X509_Certificate_Data>();
106 BER_Object public_key;
107 BER_Object v3_exts_data;
109 BER_Decoder(obj.signed_body())
112 .decode(data->m_sig_algo_inner)
113 .decode(data->m_issuer_dn)
115 .decode(data->m_not_before)
116 .decode(data->m_not_after)
118 .decode(data->m_subject_dn)
119 .get_next(public_key)
122 .get_next(v3_exts_data)
123 .verify_end(
"TBSCertificate has extra data after extensions block");
125 if(data->m_version > 2) {
126 throw Decoding_Error(
"Unknown X.509 cert version " + std::to_string(data->m_version));
128 if(obj.signature_algorithm() != data->m_sig_algo_inner) {
129 throw Decoding_Error(
"X.509 Certificate had differing algorithm identifers in inner and outer ID fields");
135 data->m_version += 1;
137 data->m_serial = serial_bn.serialize();
139 data->m_serial_negative = serial_bn.is_negative();
143 data->m_subject_public_key_bits.assign(public_key.bits(), public_key.bits() + public_key.length());
147 BER_Decoder(data->m_subject_public_key_bits)
148 .decode(data->m_subject_public_key_algid)
153 BER_Decoder(v3_exts_data).decode(data->m_v3_extensions).verify_end();
154 }
else if(v3_exts_data.is_set()) {
155 throw BER_Bad_Tag(
"Unknown tag in X.509 cert", v3_exts_data.tagging());
159 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Key_Usage>()) {
160 data->m_key_constraints = ext->get_constraints();
165 if(data->m_key_constraints.empty()) {
166 throw Decoding_Error(
"Certificate has invalid encoding for KeyUsage");
170 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Subject_Key_ID>()) {
171 data->m_subject_key_id = ext->get_key_id();
174 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Authority_Key_ID>()) {
175 data->m_authority_key_id = ext->get_key_id();
178 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Name_Constraints>()) {
179 data->m_name_constraints = ext->get_name_constraints();
182 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Extended_Key_Usage>()) {
183 data->m_extended_key_usage = ext->object_identifiers();
186 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Basic_Constraints>()) {
187 if(ext->get_is_ca() ==
true) {
195 const bool allowed_by_ku =
206 const bool allowed_by_ext_ku = [](
const std::vector<OID>& ext_ku) ->
bool {
215 for(
const auto& oid : ext_ku) {
216 if(oid == server_auth || oid == client_auth || oid == ocsp_sign) {
222 }(data->m_extended_key_usage);
224 if(allowed_by_ku && allowed_by_ext_ku) {
225 data->m_is_ca_certificate =
true;
226 data->m_path_len_constraint = ext->get_path_limit();
231 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Issuer_Alternative_Name>()) {
232 data->m_issuer_alt_name = ext->get_alt_name();
235 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Subject_Alternative_Name>()) {
236 data->m_subject_alt_name = ext->get_alt_name();
243 data->m_subject_alt_name_exists = data->m_v3_extensions.extension_set(san_oid);
245 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Certificate_Policies>()) {
246 data->m_cert_policies = ext->get_policy_oids();
249 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::Authority_Information_Access>()) {
250 data->m_ocsp_responder = ext->ocsp_responder();
251 data->m_ca_issuers = ext->ca_issuers();
254 if(
auto ext = data->m_v3_extensions.get_extension_object_as<Cert_Extension::CRL_Distribution_Points>()) {
255 data->m_crl_distribution_points = ext->crl_distribution_urls();
259 if(data->m_subject_dn == data->m_issuer_dn) {
260 if(data->m_subject_key_id.empty() ==
false && data->m_authority_key_id.empty() ==
false) {
261 data->m_self_signed = (data->m_subject_key_id == data->m_authority_key_id);
268 data->m_self_signed =
true;
271 auto pub_key =
X509::load_key(data->m_subject_public_key_bits_seq);
273 const auto sig_status = obj.verify_signature(*pub_key);
277 data->m_self_signed =
true;
279 data->m_self_signed =
false;
287 const std::vector<uint8_t> full_encoding = obj.BER_encode();
290 sha1->update(data->m_subject_public_key_bitstring);
291 data->m_subject_public_key_bitstring_sha1 = sha1->final_stdvec();
298 sha256->update(data->m_issuer_dn_bits);
299 data->m_issuer_dn_bits_sha256 = sha256->final_stdvec();
301 sha256->update(data->m_subject_dn_bits);
302 data->m_subject_dn_bits_sha256 = sha256->final_stdvec();
315void X509_Certificate::force_decode() {
317 m_data = parse_x509_cert_body(*
this);
320const X509_Certificate_Data& X509_Certificate::data()
const {
321 if(m_data ==
nullptr) {
322 throw Invalid_State(
"X509_Certificate uninitialized");
328 return static_cast<uint32_t
>(data().m_version);
332 return data().m_self_signed;
336 return data().m_not_before;
340 return data().m_not_after;
344 return data().m_subject_public_key_algid;
348 return data().m_v2_issuer_key_id;
352 return data().m_v2_subject_key_id;
356 return data().m_subject_public_key_bits;
360 return data().m_subject_public_key_bits_seq;
364 return data().m_subject_public_key_bitstring;
368 if(data().m_subject_public_key_bitstring_sha1.empty()) {
369 throw Encoding_Error(
"X509_Certificate::subject_public_key_bitstring_sha1 called but SHA-1 disabled in build");
372 return data().m_subject_public_key_bitstring_sha1;
376 return data().m_authority_key_id;
380 return data().m_subject_key_id;
384 return data().m_serial;
388 return data().m_serial_negative;
392 return data().m_issuer_dn;
396 return data().m_subject_dn;
400 return data().m_issuer_dn_bits;
404 return data().m_subject_dn_bits;
408 if(data().m_version < 3 && data().m_self_signed) {
412 return data().m_is_ca_certificate;
416 if(data().m_version < 3 && data().m_self_signed) {
420 return static_cast<uint32_t
>(data().m_path_len_constraint);
424 return data().m_key_constraints;
428 return data().m_extended_key_usage;
432 return data().m_cert_policies;
436 return data().m_name_constraints;
440 return data().m_v3_extensions;
465 if(std::find(ex.begin(), ex.end(), usage) != ex.end()) {
508 return (std::find(ex.begin(), ex.end(), usage) != ex.end());
519 return data().m_ocsp_responder;
523 return data().m_ca_issuers;
527 return data().m_crl_distribution_points;
532 if(!data().m_crl_distribution_points.empty()) {
533 return data().m_crl_distribution_points[0];
539 return data().m_subject_alt_name;
543 return data().m_issuer_alt_name;
548std::vector<std::string> get_cert_user_info(std::string_view req,
const X509_DN& dn,
const AlternativeName& alt_name) {
549 auto set_to_vector = [](
const std::set<std::string>& s) -> std::vector<std::string> {
return {s.begin(), s.end()}; };
553 }
else if(req ==
"RFC822" || req ==
"Email") {
554 return set_to_vector(alt_name.
email());
555 }
else if(req ==
"DNS") {
556 return set_to_vector(alt_name.
dns());
557 }
else if(req ==
"URI") {
558 return set_to_vector(alt_name.
uris());
559 }
else if(req ==
"IP") {
560 std::vector<std::string> ip_str;
592 }
catch(std::exception& e) {
602 if(data().m_issuer_dn_bits_sha256.empty()) {
603 throw Encoding_Error(
"X509_Certificate::raw_issuer_dn_sha256 called but SHA-256 disabled in build");
605 return data().m_issuer_dn_bits_sha256;
609 if(data().m_subject_dn_bits_sha256.empty()) {
610 throw Encoding_Error(
"X509_Certificate::raw_subject_dn_sha256 called but SHA-256 disabled in build");
612 return data().m_subject_dn_bits_sha256;
625 if(hash_name ==
"SHA-256" && !data().m_fingerprint_sha256.empty()) {
626 return data().m_fingerprint_sha256;
627 }
else if(hash_name ==
"SHA-1" && !data().m_fingerprint_sha1.empty()) {
628 return data().m_fingerprint_sha1;
641 return ipv4_names.contains(req_ipv4.value());
646 if(!data().m_subject_alt_name_exists) {
650 for(
const auto& issued_name : issued_names) {
682 return !(cert1 == cert2);
686 std::ostringstream out;
690 out <<
"Issuer: " <<
issuer_dn() <<
"\n";
696 out <<
"Public Key [" << pubkey->algo_name() <<
"-" << pubkey->key_length() <<
"]\n\n";
700 out <<
"Public Key Invalid!\n"
702 <<
" Error: " << ex.
what() <<
"\n"
706 out <<
"Constraints:\n";
708 if(constraints.
empty()) {
709 out <<
" No key constraints set\n";
712 out <<
" Digital Signature\n";
715 out <<
" Non-Repudiation\n";
718 out <<
" Key Encipherment\n";
721 out <<
" Data Encipherment\n";
724 out <<
" Key Agreement\n";
727 out <<
" Cert Sign\n";
730 out <<
" CRL Sign\n";
733 out <<
" Encipher Only\n";
736 out <<
" Decipher Only\n";
741 if(!policies.empty()) {
744 for(
const auto& oid : policies) {
745 out <<
" " << oid.to_string() <<
"\n";
750 if(!ex_constraints.empty()) {
751 out <<
"Extended Constraints:\n";
752 for(
auto&& oid : ex_constraints) {
753 out <<
" " << oid.to_formatted_string() <<
"\n";
760 out <<
"Name Constraints:\n";
765 out <<
" " << st.base();
773 out <<
" " << st.base();
784 if(!ca_issuers.empty()) {
785 out <<
"CA Issuers:\n";
787 out <<
" URI: " << ca_issuer <<
"\n";
792 out <<
"CRL " << cdp <<
"\n";
808 out <<
"Certificate is self signed\n";
std::vector< uint8_t > BER_encode() const
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
const std::set< uint32_t > & ipv4_address() const
Return the set of IPv4 addresses included in this alternative name.
const std::set< std::string > & uris() const
Return the set of URIs included in this alternative name.
const std::set< std::string > & dns() const
Return the set of DNS names included in this alternative name.
const std::set< std::string > & email() const
Return the set of email addresses included in this alternative name.
const char * what() const noexcept override
bool critical_extension_set(const OID &oid) const
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
bool includes(Key_Constraints::Bits other) const
const std::vector< GeneralSubtree > & permitted() const
const std::vector< GeneralSubtree > & excluded() const
std::string to_formatted_string() const
static std::optional< OID > from_name(std::string_view name)
static OID from_string(std::string_view str)
const std::vector< OID > & extended_key_usage() const
Key_Constraints constraints() const
bool operator==(const X509_Certificate &other) const
const NameConstraints & name_constraints() const
X509_Certificate()=default
bool is_critical(std::string_view ex_name) const
const std::vector< uint8_t > & serial_number() const
std::string fingerprint(std::string_view hash_name="SHA-1") const
const X509_DN & subject_dn() const
std::vector< uint8_t > raw_subject_dn_sha256() const
uint32_t path_limit() const
const X509_Time & not_after() const
const std::vector< uint8_t > & authority_key_id() const
bool allowed_extended_usage(std::string_view usage) const
const AlternativeName & issuer_alt_name() const
const std::vector< uint8_t > & raw_subject_dn() const
const std::vector< uint8_t > & subject_key_id() const
const std::vector< uint8_t > & subject_public_key_bits() const
bool has_constraints(Key_Constraints constraints) const
const Extensions & v3_extensions() const
bool has_ex_constraint(std::string_view ex_constraint) const
std::vector< std::string > crl_distribution_points() const
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
bool allowed_usage(Key_Constraints usage) const
const X509_DN & issuer_dn() const
const std::vector< uint8_t > & v2_issuer_key_id() const
std::string ocsp_responder() const
bool matches_dns_name(std::string_view name) const
std::vector< std::string > subject_info(std::string_view name) const
uint32_t x509_version() const
std::string crl_distribution_point() const
const std::vector< OID > & certificate_policy_oids() const
std::unique_ptr< Public_Key > load_subject_public_key() const
bool is_self_signed() const
const std::vector< uint8_t > & raw_issuer_dn() const
bool operator<(const X509_Certificate &other) const
const AlgorithmIdentifier & subject_public_key_algo() const
const AlternativeName & subject_alt_name() const
std::vector< std::string > ca_issuers() const
const std::vector< uint8_t > & subject_public_key_info() const
std::vector< uint8_t > raw_issuer_dn_sha256() const
bool is_serial_negative() const
const std::vector< uint8_t > & subject_public_key_bitstring() const
std::unique_ptr< Public_Key > subject_public_key() const
const std::vector< uint8_t > & v2_subject_key_id() const
const X509_Time & not_before() const
std::vector< std::string > issuer_info(std::string_view name) const
std::string to_string() const
bool has_field(const OID &oid) const
std::vector< std::string > get_attribute(std::string_view attr) const
const std::vector< uint8_t > & signed_body() const
const AlgorithmIdentifier & signature_algorithm() const
const std::vector< uint8_t > & signature() const
void load_data(DataSource &src)
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
std::unique_ptr< Public_Key > load_key(DataSource &source)
std::string PEM_encode(const Public_Key &key)
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
bool host_wildcard_match(std::string_view issued_, std::string_view host_)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name)
std::optional< uint32_t > string_to_ipv4(std::string_view str)
std::string ipv4_to_string(uint32_t ip)