Botan 2.19.5 Crypto and TLS for C&

Functions
Botan::X509 Namespace Reference

## Functions
std::vector< uint8_t > | BER_encode (const Public_Key &key)
Public_Key * | copy_key (const Public_Key &key)
PKCS10_Request | create_cert_req (const X509_Cert_Options &opts, const Private_Key &key, const std::string &hash_fn, RandomNumberGenerator &rng)
X509_Certificate | create_self_signed_cert (const X509_Cert_Options &opts, const Private_Key &key, const std::string &hash_fn, RandomNumberGenerator &rng)
Public_Key * | load_key (const std::vector< uint8_t > &mem)
Public_Key * | load_key (DataSource &source)
std::string | PEM_encode (const Public_Key &key)

## Detailed Description

This namespace contains functions for handling X.509 public keys

## Function Documentation

## ◆ BER_encode()

std::vector< uint8_t > Botan::X509::BER_encode | ( | const Public_Key & | key | ) |

BER encode a key

Parameters

  key | the public key to encode

Returns
  BER encoding of this key

Definition at line 19 of file x509_key.cpp.

20 {
21 // keeping it around for compat
22 return key.subject_public_key();
23 }
Botan::Public_Key::subject_public_key
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:38

References Botan::Public_Key::subject_public_key().

Referenced by botan_privkey_export_pubkey(), botan_pubkey_export(), and create_self_signed_cert().

## ◆ copy_key()

Public_Key * Botan::X509::copy_key | ( | const Public_Key & | key | ) |

Copy a key.

Parameters

  key | the public key to copy

Returns
  new public key object

Definition at line 98 of file x509_key.cpp.

99 {
100 DataSource_Memory source(PEM_encode(key));
101 return X509::load_key(source);
102 }
Botan::DataSource_Memory
Definition: data_src.h:99
Botan::X509::load_key
Public_Key * load_key(const std::vector< uint8_t > &mem)
Definition: x509_key.cpp:89
Botan::X509::PEM_encode
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:28

References load_key(), and PEM_encode().

## ◆ create_cert_req()

PKCS10_Request Botan::X509::create_cert_req | ( | const X509_Cert_Options & | opts,
|  | const Private_Key & | key,
|  | const std::string & | hash_fn,
|  | RandomNumberGenerator & | rng
| ) |  |

Create a PKCS#10 certificate request.

Parameters

  opts | the options defining the request to create
  key | the key used to sign this request
  rng | the rng to use
  hash_fn | the hash function to use

Returns
  newly created PKCS#10 request

Definition at line 110 of file x509self.cpp.

114 {
115 X509_DN subject_dn;
116 AlternativeName subject_alt;
117 load_info(opts, subject_dn, subject_alt);
118
119 Key_Constraints constraints;
120 if(opts.is_CA)
121 {
122 constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
123 }
124 else
125 {
126 verify_cert_constraints_valid_for_key_type(key, opts.constraints);
127 constraints = opts.constraints;
128 }
129
130 Extensions extensions = opts.extensions;
131
132 extensions.add_new(new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit));
133
134 if(constraints != NO_CONSTRAINTS)
135 {
136 extensions.add_new(new Cert_Extension::Key_Usage(constraints));
137 }
138 extensions.add_new(new Cert_Extension::Extended_Key_Usage(opts.ex_constraints));
139 extensions.add_new(new Cert_Extension::Subject_Alternative_Name(subject_alt));
140
141 return PKCS10_Request::create(key,
142 subject_dn,
143 extensions,
144 hash_fn,
145 rng,
146 opts.padding_scheme,
147 opts.challenge);
148 }
Botan::AlternativeName
Definition: pkix_types.h:124
Botan::Extensions::add_new
bool add_new(Certificate_Extension *extn, bool critical=false)
Definition: x509_ext.cpp:139
Botan::X509_Cert_Options::ex_constraints
std::vector< OID > ex_constraints
Definition: x509self.h:130
Botan::X509_Cert_Options::constraints
Key_Constraints constraints
Definition: x509self.h:125
Botan::X509_Cert_Options::is_CA
bool is_CA
Definition: x509self.h:113
Botan::X509_Cert_Options::extensions
Extensions extensions
Definition: x509self.h:135
Botan::X509_Cert_Options::path_limit
size_t path_limit
Definition: x509self.h:118
Botan::X509_Cert_Options::challenge
std::string challenge
Definition: x509self.h:99
Botan::X509_Cert_Options::padding_scheme
std::string padding_scheme
Definition: x509self.h:120
Botan::X509_DN
Definition: pkix_types.h:43
NO_CONSTRAINTS
@ NO_CONSTRAINTS
Definition: ffi.h:1565
CRL_SIGN
@ CRL_SIGN
Definition: ffi.h:1572
KEY_CERT_SIGN
@ KEY_CERT_SIGN
Definition: ffi.h:1571
Botan::verify_cert_constraints_valid_for_key_type
void verify_cert_constraints_valid_for_key_type(const Public_Key &pub_key, Key_Constraints constraints)
Definition: key_constraint.cpp:70
Botan::Key_Constraints
Key_Constraints
Definition: pkix_enums.h:106

References Botan::Extensions::add_new(), Botan::X509_Cert_Options::challenge, Botan::X509_Cert_Options::constraints, Botan::PKCS10_Request::create(), Botan::CRL_SIGN, Botan::X509_Cert_Options::ex_constraints, Botan::X509_Cert_Options::extensions, Botan::X509_Cert_Options::is_CA, Botan::KEY_CERT_SIGN, Botan::NO_CONSTRAINTS, Botan::X509_Cert_Options::padding_scheme, Botan::X509_Cert_Options::path_limit, and Botan::verify_cert_constraints_valid_for_key_type().

## ◆ create_self_signed_cert()

X509_Certificate Botan::X509::create_self_signed_cert | ( | const X509_Cert_Options & | opts,
|  | const Private_Key & | key,
|  | const std::string & | hash_fn,
|  | RandomNumberGenerator & | rng
| ) |  |

Create a self-signed X.509 certificate.

Parameters

  opts | the options defining the certificate to create
  key | the private key used for signing, i.e. the key associated with this self-signed certificate
  hash_fn | the hash function to use
  rng | the rng to use

Returns
  newly created self-signed certificate

Definition at line 51 of file x509self.cpp.

55 {
56 AlgorithmIdentifier sig_algo;
57 X509_DN subject_dn;
58 AlternativeName subject_alt;
59
60 // for now, only the padding option is used
61 std::map<std::string,std::string> sig_opts = { {"padding",opts.padding_scheme} };
62
63 const std::vector<uint8_t> pub_key = X509::BER_encode(key);
64 std::unique_ptr<PK_Signer> signer(choose_sig_format(key, sig_opts, rng, hash_fn, sig_algo));
65 BOTAN_ASSERT_NOMSG(sig_algo.get_oid().has_value());
66 load_info(opts, subject_dn, subject_alt);
67
68 Extensions extensions = opts.extensions;
69
70 Key_Constraints constraints;
71 if(opts.is_CA)
72 {
73 constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
74 }
75 else
76 {
77 verify_cert_constraints_valid_for_key_type(key, opts.constraints);
78 constraints = opts.constraints;
79 }
80
81 extensions.add_new(
82 new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit),
83 true);
84
85 if(constraints != NO_CONSTRAINTS)
86 {
87 extensions.add_new(new Cert_Extension::Key_Usage(constraints), true);
88 }
89
90 std::unique_ptr<Cert_Extension::Subject_Key_ID> skid(new Cert_Extension::Subject_Key_ID(pub_key, hash_fn));
91
92 extensions.add_new(new Cert_Extension::Authority_Key_ID(skid->get_key_id()));
93 extensions.add_new(skid.release());
94
95 extensions.add_new(
96 new Cert_Extension::Subject_Alternative_Name(subject_alt));
97
98 extensions.add_new(
99 new Cert_Extension::Extended_Key_Usage(opts.ex_constraints));
100
101 return X509_CA::make_cert(signer.get(), rng, sig_algo, pub_key,
102 opts.start, opts.end,
103 subject_dn, subject_dn,
104 extensions);
105 }
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68
Botan::AlgorithmIdentifier
Definition: asn1_obj.h:430
Botan::AlgorithmIdentifier::get_oid
const OID & get_oid() const
Definition: asn1_obj.h:445
Botan::OID::has_value
bool has_value() const
Definition: asn1_obj.h:238
Botan::X509_Cert_Options::end
X509_Time end
Definition: x509self.h:108
Botan::X509_Cert_Options::start
X509_Time start
Definition: x509self.h:104
Botan::PKCS8::BER_encode
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
Botan::choose_sig_format
PK_Signer * choose_sig_format(const Private_Key &key, RandomNumberGenerator &rng, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition: x509_ca.cpp:318

References Botan::Extensions::add_new(), BER_encode(), BOTAN_ASSERT_NOMSG, Botan::choose_sig_format(), Botan::X509_Cert_Options::constraints, Botan::CRL_SIGN, Botan::X509_Cert_Options::end, Botan::X509_Cert_Options::ex_constraints, Botan::X509_Cert_Options::extensions, Botan::AlgorithmIdentifier::get_oid(), Botan::OID::has_value(), Botan::X509_Cert_Options::is_CA, Botan::KEY_CERT_SIGN, Botan::X509_CA::make_cert(), Botan::NO_CONSTRAINTS, Botan::X509_Cert_Options::padding_scheme, Botan::X509_Cert_Options::path_limit, Botan::X509_Cert_Options::start, and Botan::verify_cert_constraints_valid_for_key_type().

## ◆ load_key() [1/2]

Public_Key * Botan::X509::load_key | ( | const std::vector< uint8_t > & | enc | ) |

Create a public key from a memory region.

Parameters

  enc | the memory region containing the DER or PEM encoded key

Returns
  new public key object

Definition at line 89 of file x509_key.cpp.

90 {
91 DataSource_Memory source(mem);
92 return X509::load_key(source);
93 }

References load_key().

## ◆ load_key() [2/2]

Public_Key * Botan::X509::load_key | ( | DataSource & | source | ) |

Create a public key from a data source.

Parameters

  source | the source providing the DER or PEM encoded key

Returns
  new public key object

Definition at line 37 of file x509_key.cpp.

38 {
39 try {
40 AlgorithmIdentifier alg_id;
41 std::vector<uint8_t> key_bits;
42
43 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
44 {
45 BER_Decoder(source)
46 .start_cons(SEQUENCE)
47 .decode(alg_id)
48 .decode(key_bits, BIT_STRING)
49 .end_cons();
50 }
51 else
52 {
53 DataSource_Memory ber(
54 PEM_Code::decode_check_label(source, "PUBLIC KEY")
55 );
56
57 BER_Decoder(ber)
58 .start_cons(SEQUENCE)
59 .decode(alg_id)
60 .decode(key_bits, BIT_STRING)
61 .end_cons();
62 }
63
64 if(key_bits.empty())
65 throw Decoding_Error("X.509 public key decoding");
66
67 return load_public_key(alg_id, key_bits).release();
68 }
69 catch(Decoding_Error& e)
70 {
71 throw Decoding_Error("X.509 public key decoding", e);
72 }
73 }
Botan::BER_Decoder
Definition: ber_dec.h:22
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:290
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:300
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:222
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:142
Botan::PEM_Code::decode_check_label
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54
Botan::BIT_STRING
@ BIT_STRING
Definition: asn1_obj.h:37
Botan::SEQUENCE
@ SEQUENCE
Definition: asn1_obj.h:42
Botan::load_public_key
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, const std::vector< uint8_t > &key_bits)
Definition: pk_algs.cpp:78

References Botan::BIT_STRING, Botan::BER_Decoder::decode(), Botan::PEM_Code::decode_check_label(), Botan::BER_Decoder::end_cons(), Botan::load_public_key(), Botan::PEM_Code::matches(), Botan::ASN1::maybe_BER(), Botan::SEQUENCE, and Botan::BER_Decoder::start_cons().

Referenced by botan_privkey_export_pubkey(), botan_pubkey_load(), copy_key(), load_key(), Botan::X509_Certificate::load_subject_public_key(), and Botan::PKCS10_Request::subject_public_key().

## ◆ PEM_encode()

std::string Botan::X509::PEM_encode | ( | const Public_Key & | key | ) |

PEM encode a public key into a string.

Parameters

  key | the key to encode

Returns
  PEM encoded key

Definition at line 28 of file x509_key.cpp.

29 {
30 return PEM_Code::encode(key.subject_public_key(),
31 "PUBLIC KEY");
32 }
Botan::PEM_Code::encode
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43

References Botan::PEM_Code::encode(), and Botan::Public_Key::subject_public_key().

Referenced by botan_pubkey_export(), copy_key(), and Botan::X509_Certificate::to_string().