Botan 1.10.17
Botan::X509 Namespace Reference

Functions

MemoryVector< byteBER_encode (const Public_Key &key)
Public_Keycopy_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)
void encode (const Public_Key &key, Pipe &pipe, X509_Encoding encoding=PEM)
Key_Constraints find_constraints (const Public_Key &pub_key, Key_Constraints limits)
Public_Keyload_key (const MemoryRegion< byte > &mem)
Public_Keyload_key (const std::string &fsname)
Public_Keyload_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()

BOTAN_DLL MemoryVector< byte > Botan::X509::BER_encode ( const Public_Key & key)

BER encode a key

Parameters
keythe public key to encode
Returns
BER encoding of this key

Definition at line 21 of file x509_key.cpp.

22 {
23 return DER_Encoder()
27 .end_cons()
28 .get_contents();
29 }
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
SecureVector< byte > get_contents()
Definition der_enc.cpp:122
DER_Encoder & end_cons()
Definition der_enc.cpp:145
DER_Encoder & encode(bool b)
Definition der_enc.cpp:209
virtual MemoryVector< byte > x509_subject_public_key() const =0
virtual AlgorithmIdentifier algorithm_identifier() const =0
@ BIT_STRING
Definition asn1_int.h:30
@ SEQUENCE
Definition asn1_int.h:35

References Botan::Public_Key::algorithm_identifier(), BER_encode(), Botan::BIT_STRING, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::Public_Key::x509_subject_public_key().

Referenced by BER_encode(), create_cert_req(), create_self_signed_cert(), encode(), and PEM_encode().

◆ copy_key()

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

Copy a key.

Parameters
keythe public key to copy
Returns
new public key object

Definition at line 104 of file x509_key.cpp.

105 {
106 DataSource_Memory source(PEM_encode(key));
107 return X509::load_key(source);
108 }
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:34
Public_Key * load_key(DataSource &source)
Definition x509_key.cpp:43

References copy_key(), load_key(), and PEM_encode().

Referenced by copy_key().

◆ create_cert_req()

BOTAN_DLL 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
optsthe options defining the request to create
keythe key used to sign this request
rngthe rng to use
hash_fnthe hash function to use
Returns
newly created PKCS#10 request

Definition at line 91 of file x509self.cpp.

95 {
96 AlgorithmIdentifier sig_algo;
97 X509_DN subject_dn;
98 AlternativeName subject_alt;
99
100 opts.sanity_check();
101
103 std::auto_ptr<PK_Signer> signer(choose_sig_format(key, hash_fn, sig_algo));
104 load_info(opts, subject_dn, subject_alt);
105
106 const size_t PKCS10_VERSION = 0;
107
108 Extensions extensions;
109
110 extensions.add(
112 extensions.add(
116 )
117 );
118 extensions.add(
120 extensions.add(
122
123 DER_Encoder tbs_req;
124
125 tbs_req.start_cons(SEQUENCE)
126 .encode(PKCS10_VERSION)
127 .encode(subject_dn)
128 .raw_bytes(pub_key)
129 .start_explicit(0);
130
131 if(opts.challenge != "")
132 {
133 ASN1_String challenge(opts.challenge, DIRECTORY_STRING);
134
135 tbs_req.encode(
136 Attribute("PKCS9.ChallengePassword",
137 DER_Encoder().encode(challenge).get_contents()
138 )
139 );
140 }
141
142 tbs_req.encode(
143 Attribute("PKCS9.ExtensionRequest",
145 .start_cons(SEQUENCE)
146 .encode(extensions)
147 .end_cons()
148 .get_contents()
149 )
150 )
151 .end_explicit()
152 .end_cons();
153
154 DataSource_Memory source(
155 X509_Object::make_signed(signer.get(),
156 rng,
157 sig_algo,
158 tbs_req.get_contents())
159 );
160
161 return PKCS10_Request(source);
162 }
DER_Encoder & end_explicit()
Definition der_enc.cpp:172
DER_Encoder & start_explicit(u16bit type_tag)
Definition der_enc.cpp:159
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition der_enc.cpp:188
void add(Certificate_Extension *extn, bool critical=false)
Definition x509_ext.cpp:77
PKCS10_Request(DataSource &source)
Definition pkcs10.cpp:22
std::vector< OID > ex_constraints
Definition x509self.h:115
Key_Constraints constraints
Definition x509self.h:110
void sanity_check() const
Definition x509opt.cpp:67
std::string challenge
Definition x509self.h:86
static MemoryVector< byte > make_signed(class PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &tbs)
Definition x509_obj.cpp:204
MemoryVector< byte > BER_encode(const Public_Key &key)
Definition x509_key.cpp:21
Key_Constraints find_constraints(const Public_Key &pub_key, Key_Constraints limits)
Definition x509_key.cpp:113
void encode(const Public_Key &key, Pipe &pipe, X509_Encoding encoding=PEM)
Definition x509_key.h:87
PK_Signer * choose_sig_format(const Private_Key &key, const std::string &hash_fn, AlgorithmIdentifier &sig_algo)
Definition x509_ca.cpp:218
@ DIRECTORY_STRING
Definition asn1_int.h:50
Key_Constraints
@ KEY_CERT_SIGN

References Botan::Extensions::add(), Botan::Attribute::Attribute(), BER_encode(), Botan::X509_Cert_Options::challenge, Botan::choose_sig_format(), Botan::X509_Cert_Options::constraints, create_cert_req(), Botan::CRL_SIGN, Botan::DIRECTORY_STRING, Botan::DER_Encoder::encode(), encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::end_explicit(), Botan::X509_Cert_Options::ex_constraints, find_constraints(), Botan::DER_Encoder::get_contents(), Botan::X509_Cert_Options::is_CA, Botan::KEY_CERT_SIGN, Botan::X509_Object::make_signed(), Botan::X509_Cert_Options::path_limit, Botan::PKCS10_Request::PKCS10_Request(), Botan::DER_Encoder::raw_bytes(), Botan::X509_Cert_Options::sanity_check(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::DER_Encoder::start_explicit().

Referenced by create_cert_req().

◆ create_self_signed_cert()

BOTAN_DLL 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
optsthe options defining the certificate to create
keythe private key used for signing, i.e. the key associated with this self-signed certificate
hash_fnthe hash function to use
rngthe rng to use
Returns
newly created self-signed certificate

Definition at line 45 of file x509self.cpp.

49 {
50 AlgorithmIdentifier sig_algo;
51 X509_DN subject_dn;
52 AlternativeName subject_alt;
53
54 opts.sanity_check();
55
57 std::auto_ptr<PK_Signer> signer(choose_sig_format(key, hash_fn, sig_algo));
58 load_info(opts, subject_dn, subject_alt);
59
60 Key_Constraints constraints;
61 if(opts.is_CA)
62 constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
63 else
64 constraints = find_constraints(key, opts.constraints);
65
66 Extensions extensions;
67
68 extensions.add(
70 true);
71
72 extensions.add(new Cert_Extension::Key_Usage(constraints), true);
73
74 extensions.add(new Cert_Extension::Subject_Key_ID(pub_key));
75
76 extensions.add(
78
79 extensions.add(
81
82 return X509_CA::make_cert(signer.get(), rng, sig_algo, pub_key,
83 opts.start, opts.end,
84 subject_dn, subject_dn,
85 extensions);
86 }
static X509_Certificate make_cert(PK_Signer *signer, RandomNumberGenerator &rng, const AlgorithmIdentifier &sig_algo, const MemoryRegion< byte > &pub_key, const X509_Time &not_before, const X509_Time &not_after, const X509_DN &issuer_dn, const X509_DN &subject_dn, const Extensions &extensions)
Definition x509_ca.cpp:90

References Botan::Extensions::add(), BER_encode(), Botan::choose_sig_format(), Botan::X509_Cert_Options::constraints, create_self_signed_cert(), Botan::CRL_SIGN, Botan::X509_Cert_Options::end, Botan::X509_Cert_Options::ex_constraints, find_constraints(), Botan::X509_Cert_Options::is_CA, Botan::KEY_CERT_SIGN, Botan::X509_CA::make_cert(), Botan::X509_Cert_Options::path_limit, Botan::X509_Cert_Options::sanity_check(), and Botan::X509_Cert_Options::start.

Referenced by create_self_signed_cert().

◆ encode()

void Botan::X509::encode ( const Public_Key & key,
Pipe & pipe,
X509_Encoding encoding = PEM )
inline

Encode a key into a pipe.

Deprecated
Use PEM_encode or BER_encode instead
Parameters
keythe public key to encode
pipethe pipe to feed the encoded key into
encodingthe encoding type to use

Definition at line 87 of file x509_key.h.

90 {
91 if(encoding == PEM)
92 pipe.write(X509::PEM_encode(key));
93 else
94 pipe.write(X509::BER_encode(key));
95 }
void write(const byte in[], size_t length)
Definition pipe_rw.cpp:34

References BER_encode(), encode(), Botan::PEM, and PEM_encode().

Referenced by create_cert_req(), and encode().

◆ find_constraints()

BOTAN_DLL Key_Constraints Botan::X509::find_constraints ( const Public_Key & pub_key,
Key_Constraints limits )

Create the key constraints for a specific public key.

Parameters
pub_keythe public key from which the basic set of constraints to be placed in the return value is derived
limitsadditional limits that will be incorporated into the return value
Returns
combination of key type specific constraints and additional limits

Definition at line 113 of file x509_key.cpp.

115 {
116 const std::string name = pub_key.algo_name();
117
118 size_t constraints = 0;
119
120 if(name == "DH" || name == "ECDH")
121 constraints |= KEY_AGREEMENT;
122
123 if(name == "RSA" || name == "ElGamal")
124 constraints |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT;
125
126 if(name == "RSA" || name == "RW" || name == "NR" ||
127 name == "DSA" || name == "ECDSA")
128 constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION;
129
130 if(limits)
131 constraints &= limits;
132
133 return Key_Constraints(constraints);
134 }
virtual std::string algo_name() const =0
@ DATA_ENCIPHERMENT
@ DIGITAL_SIGNATURE
@ KEY_AGREEMENT
@ KEY_ENCIPHERMENT
@ NON_REPUDIATION

References Botan::Public_Key::algo_name(), Botan::DATA_ENCIPHERMENT, Botan::DIGITAL_SIGNATURE, find_constraints(), Botan::KEY_AGREEMENT, Botan::KEY_ENCIPHERMENT, and Botan::NON_REPUDIATION.

Referenced by create_cert_req(), create_self_signed_cert(), find_constraints(), and Botan::X509_CA::sign_request().

◆ load_key() [1/3]

BOTAN_DLL Public_Key * Botan::X509::load_key ( const MemoryRegion< byte > & enc)

Create a public key from a memory region.

Parameters
encthe memory region containing the DER or PEM encoded key
Returns
new public key object

Definition at line 95 of file x509_key.cpp.

96 {
97 DataSource_Memory source(mem);
98 return X509::load_key(source);
99 }

References load_key().

◆ load_key() [2/3]

BOTAN_DLL Public_Key * Botan::X509::load_key ( const std::string & filename)

Create a public key from a file

Parameters
filenamepathname to the file to load
Returns
new public key object

Definition at line 86 of file x509_key.cpp.

87 {
88 DataSource_Stream source(fsname, true);
89 return X509::load_key(source);
90 }

References load_key().

◆ load_key() [3/3]

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

Create a public key from a data source.

Parameters
sourcethe source providing the DER or PEM encoded key
Returns
new public key object

Definition at line 43 of file x509_key.cpp.

44 {
45 try {
47 MemoryVector<byte> key_bits;
48
49 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
50 {
51 BER_Decoder(source)
52 .start_cons(SEQUENCE)
53 .decode(alg_id)
54 .decode(key_bits, BIT_STRING)
55 .verify_end()
56 .end_cons();
57 }
58 else
59 {
61 PEM_Code::decode_check_label(source, "PUBLIC KEY")
62 );
63
64 BER_Decoder(ber)
65 .start_cons(SEQUENCE)
66 .decode(alg_id)
67 .decode(key_bits, BIT_STRING)
68 .verify_end()
69 .end_cons();
70 }
71
72 if(key_bits.empty())
73 throw Decoding_Error("X.509 public key decoding failed");
74
75 return make_public_key(alg_id, key_bits);
76 }
77 catch(Decoding_Error)
78 {
79 throw Decoding_Error("X.509 public key decoding failed");
80 }
81 }
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
bool empty() const
Definition secmem.h:35
bool maybe_BER(DataSource &source)
Definition asn1_int.cpp:55
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition pem.cpp:42
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition pem.cpp:116
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition pk_algs.cpp:49
Decoding_Error(const std::string &name)
Definition exceptn.h:140

References Botan::BER_Decoder::BER_Decoder(), Botan::BIT_STRING, Botan::PEM_Code::decode_check_label(), Botan::Decoding_Error::Decoding_Error(), Botan::MemoryRegion< T >::empty(), load_key(), Botan::make_public_key(), Botan::PEM_Code::matches(), Botan::ASN1::maybe_BER(), and Botan::SEQUENCE.

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

◆ PEM_encode()

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

PEM encode a public key into a string.

Parameters
keythe key to encode
Returns
PEM encoded key

Definition at line 34 of file x509_key.cpp.

35 {
37 "PUBLIC KEY");
38 }
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19

References BER_encode(), Botan::PEM_Code::encode(), and PEM_encode().

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