Botan 3.4.0
Crypto and TLS for C&
x509_key.h
Go to the documentation of this file.
1/*
2* X.509 Public Key
3* (C) 1999-2010,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_X509_PUBLIC_KEY_H_
9#define BOTAN_X509_PUBLIC_KEY_H_
10
11#include <botan/data_src.h>
12#include <botan/pk_keys.h>
13#include <string>
14#include <vector>
15
16namespace Botan::X509 {
17
18/**
19* BER encode a key
20* @param key the public key to encode
21* @return BER encoding of this key
22*/
23inline std::vector<uint8_t> BER_encode(const Public_Key& key) {
24 return key.subject_public_key();
25}
26
27/**
28* PEM encode a public key into a string.
29* @param key the key to encode
30* @return PEM encoded key
31*/
32BOTAN_PUBLIC_API(2, 0) std::string PEM_encode(const Public_Key& key);
33
34/**
35* Create a public key from a data source.
36* @param source the source providing the DER or PEM encoded key
37* @return new public key object
38*/
39BOTAN_PUBLIC_API(3, 0) std::unique_ptr<Public_Key> load_key(DataSource& source);
40
41#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
42/**
43* Create a public key from a file
44* @param filename pathname to the file to load
45* @return new public key object
46*/
47inline std::unique_ptr<Public_Key> load_key(std::string_view filename) {
48 DataSource_Stream source(filename, true);
49 return X509::load_key(source);
50}
51#endif
52
53/**
54* Create a public key from a memory region.
55* @param enc the memory region containing the DER or PEM encoded key
56* @return new public key object
57*/
58inline std::unique_ptr<Public_Key> load_key(const std::vector<uint8_t>& enc) {
59 DataSource_Memory source(enc);
60 return X509::load_key(source);
61}
62
63/**
64* Create a public key from a memory region.
65* @param enc the memory region containing the DER or PEM encoded key
66* @return new public key object
67*/
68inline std::unique_ptr<Public_Key> load_key(std::span<const uint8_t> enc) {
69 DataSource_Memory source(enc);
70 return X509::load_key(source);
71}
72
73/**
74* Copy a key.
75* @param key the public key to copy
76* @return new public key object
77*/
78inline std::unique_ptr<Public_Key> copy_key(const Public_Key& key) {
79 DataSource_Memory source(PEM_encode(key));
80 return X509::load_key(source);
81}
82
83} // namespace Botan::X509
84
85#endif
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:48
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition x509_key.h:23
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition x509_key.cpp:28
std::unique_ptr< Public_Key > copy_key(const Public_Key &key)
Definition x509_key.h:78
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:21