Botan 3.0.0-alpha0
Crypto and TLS for C&
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/data_src.h>
10#include <botan/ber_dec.h>
11#include <botan/pem.h>
12#include <botan/asn1_obj.h>
13#include <botan/pk_algs.h>
14
15namespace Botan::X509 {
16
17/*
18* PEM encode a X.509 public key
19*/
20std::string PEM_encode(const Public_Key& key)
21 {
23 "PUBLIC KEY");
24 }
25
26/*
27* Extract a public key and return it
28*/
30 {
31 try {
33 std::vector<uint8_t> key_bits;
34
35 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
36 {
37 BER_Decoder(source)
39 .decode(alg_id)
41 .end_cons();
42 }
43 else
44 {
46 PEM_Code::decode_check_label(source, "PUBLIC KEY")
47 );
48
49 BER_Decoder(ber)
51 .decode(alg_id)
53 .end_cons();
54 }
55
56 if(key_bits.empty())
57 throw Decoding_Error("X.509 public key decoding");
58
59 return load_public_key(alg_id, key_bits).release();
60 }
61 catch(Decoding_Error& e)
62 {
63 throw Decoding_Error("X.509 public key decoding", e);
64 }
65 }
66
67}
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:187
BER_Decoder & end_cons()
Definition: ber_dec.cpp:303
BER_Decoder start_sequence()
Definition: ber_dec.h:111
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:38
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:218
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:41
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:140
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:52
Public_Key * load_key(DataSource &source)
Definition: x509_key.cpp:29
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:20
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, const std::vector< uint8_t > &key_bits)
Definition: pk_algs.cpp:78