Botan 1.10.17

x509_key.cpp
Go to the documentation of this file.
1 /*
2 * X.509 Public Key
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/x509_key.h>
9 #include <botan/filters.h>
10 #include <botan/asn1_obj.h>
11 #include <botan/der_enc.h>
12 #include <botan/ber_dec.h>
13 #include <botan/pem.h>
14 #include <botan/internal/pk_algs.h>
15 #include <memory>
16
17 namespace Botan {
18
19 namespace X509 {
20
21 MemoryVector<byte> BER_encode(const Public_Key& key)
22 {
23 return DER_Encoder()
24 .start_cons(SEQUENCE)
25 .encode(key.algorithm_identifier())
26 .encode(key.x509_subject_public_key(), BIT_STRING)
27 .end_cons()
28 .get_contents();
29 }
30
31 /*
32 * PEM encode a X.509 public key
33 */
34 std::string PEM_encode(const Public_Key& key)
35 {
36 return PEM_Code::encode(X509::BER_encode(key),
37 "PUBLIC KEY");
38 }
39
40 /*
41 * Extract a public key and return it
42 */
43 Public_Key* load_key(DataSource& source)
44 {
45 try {
46 AlgorithmIdentifier alg_id;
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 {
60 DataSource_Memory ber(
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 }
82
83 /*
84 * Extract a public key and return it
85 */
86 Public_Key* load_key(const std::string& fsname)
87 {
88 DataSource_Stream source(fsname, true);
89 return X509::load_key(source);
90 }
91
92 /*
93 * Extract a public key and return it
94 */
95 Public_Key* load_key(const MemoryRegion<byte>& mem)
96 {
97 DataSource_Memory source(mem);
98 return X509::load_key(source);
99 }
100
101 /*
102 * Make a copy of this public key
103 */
104 Public_Key* copy_key(const Public_Key& key)
105 {
106 DataSource_Memory source(PEM_encode(key));
107 return X509::load_key(source);
108 }
109
110 /*
111 * Find the allowable key constraints
112 */
113 Key_Constraints find_constraints(const Public_Key& pub_key,
114 Key_Constraints limits)
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 }
135
136 }
137
138 }
Botan::DataSource_Stream
Definition: data_src.h:128
Botan::DATA_ENCIPHERMENT
Definition: pubkey_enums.h:23
Botan::NON_REPUDIATION
Definition: pubkey_enums.h:21
Botan::Public_Key::algorithm_identifier
virtual AlgorithmIdentifier algorithm_identifier() const =0
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::X509::load_key
Public_Key * load_key(DataSource &source)
Definition: x509_key.cpp:43
Botan::X509::BER_encode
MemoryVector< byte > BER_encode(const Public_Key &key)
Definition: x509_key.cpp:21
Botan::KEY_ENCIPHERMENT
Definition: pubkey_enums.h:22
Botan::Public_Key::algo_name
virtual std::string algo_name() const =0
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_int.cpp:55
Botan::KEY_AGREEMENT
Definition: pubkey_enums.h:24
Botan::DataSource
Definition: data_src.h:20
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::MemoryRegion::empty
bool empty() const
Definition: secmem.h:35
Botan::DIGITAL_SIGNATURE
Definition: pubkey_enums.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:252
Botan::X509::find_constraints
Key_Constraints find_constraints(const Public_Key &pub_key, Key_Constraints limits)
Definition: x509_key.cpp:113
Botan::Public_Key::x509_subject_public_key
virtual MemoryVector< byte > x509_subject_public_key() const =0
Botan::X509::encode
void encode(const Public_Key &key, Pipe &pipe, X509_Encoding encoding=PEM)
Definition: x509_key.h:87
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::MemoryRegion< byte >
Botan::X509::PEM_encode
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:34
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:116
Botan::Public_Key
Definition: pk_keys.h:21
Botan::X509::copy_key
Public_Key * copy_key(const Public_Key &key)
Definition: x509_key.cpp:104
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::BIT_STRING
Definition: asn1_int.h:30
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
Botan::DataSource_Memory
Definition: data_src.h:94
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::Key_Constraints
Key_Constraints
Definition: pubkey_enums.h:18
Botan::PEM_Code::decode_check_label
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Botan::make_public_key
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition: pk_algs.cpp:49