Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

x509_key.cpp
Go to the documentation of this file.
1 /*
2 * X.509 Public Key
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/x509_key.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/pem.h>
12 #include <botan/alg_id.h>
13 #include <botan/pk_algs.h>
14
15 namespace Botan {
16
17 namespace X509 {
18
19 std::vector<uint8_t> BER_encode(const Public_Key& key)
20 {
21 // keeping it around for compat
22 return key.subject_public_key();
23 }
24
25 /*
26 * PEM encode a X.509 public key
27 */
28 std::string PEM_encode(const Public_Key& key)
29 {
30 return PEM_Code::encode(key.subject_public_key(),
31 "PUBLIC KEY");
32 }
33
34 /*
35 * Extract a public key and return it
36 */
37 Public_Key* load_key(DataSource& source)
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 failed");
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 failed: " + std::string(e.what()));
72 }
73 }
74
75 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
76 /*
77 * Extract a public key and return it
78 */
79 Public_Key* load_key(const std::string& fsname)
80 {
81 DataSource_Stream source(fsname, true);
82 return X509::load_key(source);
83 }
84 #endif
85
86 /*
87 * Extract a public key and return it
88 */
89 Public_Key* load_key(const std::vector<uint8_t>& mem)
90 {
91 DataSource_Memory source(mem);
92 return X509::load_key(source);
93 }
94
95 /*
96 * Make a copy of this public key
97 */
98 Public_Key* copy_key(const Public_Key& key)
99 {
100 DataSource_Memory source(PEM_encode(key));
101 return X509::load_key(source);
102 }
103
104 }
105
106 }
Botan::X509::BER_encode
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition: x509_key.cpp:19
Botan::DataSource_Stream
Definition: data_src.h:143
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
Botan::X509::load_key
Public_Key * load_key(DataSource &source)
Definition: x509_key.cpp:37
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:55
Botan::DataSource
Definition: data_src.h:21
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:62
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:272
Botan::Decoding_Error
Definition: exceptn.h:189
Botan::Exception::what
const char * what() const BOTAN_NOEXCEPT override
Definition: exceptn.h:26
Botan::SEQUENCE
Definition: asn1_obj.h:39
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:258
Botan::X509::PEM_encode
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:28
Botan
Definition: alg_id.cpp:13
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:140
Botan::Public_Key
Definition: pk_keys.h:24
Botan::X509::copy_key
Public_Key * copy_key(const Public_Key &key)
Definition: x509_key.cpp:98
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
Definition: asn1_obj.h:34
Botan::DataSource_Memory
Definition: data_src.h:98
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::Public_Key::subject_public_key
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:17
Botan::BER_Decoder
Definition: ber_dec.h:19