Botan 3.9.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 <memory>
14#include <string>
15#include <vector>
16
17namespace Botan::X509 {
18
19/**
20* BER encode a key
21* @param key the public key to encode
22* @return BER encoding of this key
23*/
24inline std::vector<uint8_t> BER_encode(const Public_Key& key) {
25 return key.subject_public_key();
26}
27
28/**
29* PEM encode a public key into a string.
30* @param key the key to encode
31* @return PEM encoded key
32*/
33BOTAN_PUBLIC_API(2, 0) std::string PEM_encode(const Public_Key& key);
34
35/**
36* Create a public key from a data source.
37* @param source the source providing the DER or PEM encoded key
38* @return new public key object
39*/
40BOTAN_PUBLIC_API(3, 0) std::unique_ptr<Public_Key> load_key(DataSource& source);
41
42#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
43/**
44* Create a public key from a file
45* @param filename pathname to the file to load
46* @return new public key object
47*/
48inline std::unique_ptr<Public_Key> load_key(std::string_view filename) {
49 DataSource_Stream source(filename, true);
50 return X509::load_key(source);
51}
52#endif
53
54/**
55* Create a public key from a memory region.
56* @param enc the memory region containing the DER or PEM encoded key
57* @return new public key object
58*/
59inline std::unique_ptr<Public_Key> load_key(const std::vector<uint8_t>& enc) {
60 DataSource_Memory source(enc);
61 return X509::load_key(source);
62}
63
64/**
65* Create a public key from a memory region.
66* @param enc the memory region containing the DER or PEM encoded key
67* @return new public key object
68*/
69inline std::unique_ptr<Public_Key> load_key(std::span<const uint8_t> enc) {
70 DataSource_Memory source(enc);
71 return X509::load_key(source);
72}
73
74/**
75* Copy a key.
76* @param key the public key to copy
77* @return new public key object
78*/
79inline std::unique_ptr<Public_Key> copy_key(const Public_Key& key) {
80 DataSource_Memory source(PEM_encode(key));
81 return X509::load_key(source);
82}
83
84} // namespace Botan::X509
85
86#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:56
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition x509_key.h:24
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:79
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:21