Botan 3.4.0
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
10#include <botan/asn1_obj.h>
11#include <botan/ber_dec.h>
12#include <botan/data_src.h>
13#include <botan/pem.h>
14#include <botan/pk_algs.h>
15
16namespace Botan::X509 {
17
18/*
19* PEM encode a X.509 public key
20*/
21std::string PEM_encode(const Public_Key& key) {
22 return PEM_Code::encode(key.subject_public_key(), "PUBLIC KEY");
23}
24
25/*
26* Extract a public key and return it
27*/
28std::unique_ptr<Public_Key> load_key(DataSource& source) {
29 try {
31 std::vector<uint8_t> key_bits;
32
33 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) {
35 } else {
36 DataSource_Memory ber(PEM_Code::decode_check_label(source, "PUBLIC KEY"));
37
39 }
40
41 if(key_bits.empty()) {
42 throw Decoding_Error("X.509 public key decoding");
43 }
44
45 return load_public_key(alg_id, key_bits);
46 } catch(Decoding_Error& e) {
47 throw Decoding_Error("X.509 public key decoding", e);
48 }
49}
50
51} // namespace Botan::X509
BER_Decoder & decode(bool &out)
Definition ber_dec.h:176
BER_Decoder & end_cons()
Definition ber_dec.cpp:295
BER_Decoder start_sequence()
Definition ber_dec.h:113
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:48
bool maybe_BER(DataSource &source)
Definition asn1_obj.cpp:192
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
Definition pem.cpp:49
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:137
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition x509_key.cpp:28
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:21
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition pk_algs.cpp:103