Botan 3.14.1
Crypto and TLS for C&
Botan::PEM_Code Namespace Reference

Functions

secure_vector< uint8_t > decode (DataSource &source, std::string &label)
secure_vector< uint8_t > decode (std::string_view pem, std::string &label)
secure_vector< uint8_t > decode_check_label (DataSource &source, std::string_view label_want)
secure_vector< uint8_t > decode_check_label (std::string_view pem, std::string_view label_want)
template<typename Alloc>
std::string encode (const std::vector< uint8_t, Alloc > &data, std::string_view label, size_t line_width=64)
std::string encode (const uint8_t der[], size_t length, std::string_view label, size_t width)
bool matches (DataSource &source, std::string_view extra, size_t search_range)

Function Documentation

◆ decode() [1/2]

secure_vector< uint8_t > Botan::PEM_Code::decode ( DataSource & pem,
std::string & label )

Decode PEM data

Parameters
PEMa datasource containing PEM encoded data
labelis set to the PEM label found for later inspection

Definition at line 64 of file pem.cpp.

64 {
65 const size_t RANDOM_CHAR_LIMIT = 8;
66
67 label.clear();
68
69 const std::string PEM_HEADER1 = "-----BEGIN ";
70 const std::string PEM_HEADER2 = "-----";
71 size_t position = 0;
72
73 while(position != PEM_HEADER1.length()) {
74 auto b = source.read_byte();
75
76 if(!b) {
77 throw Decoding_Error("PEM: No PEM header found");
78 }
79 if(static_cast<char>(*b) == PEM_HEADER1[position]) {
80 ++position;
81 } else if(position >= RANDOM_CHAR_LIMIT) {
82 throw Decoding_Error("PEM: Malformed PEM header");
83 } else {
84 position = 0;
85 }
86 }
87 position = 0;
88 while(position != PEM_HEADER2.length()) {
89 auto b = source.read_byte();
90
91 if(!b) {
92 throw Decoding_Error("PEM: No PEM header found");
93 }
94 if(static_cast<char>(*b) == PEM_HEADER2[position]) {
95 ++position;
96 } else if(position > 0) {
97 throw Decoding_Error("PEM: Malformed PEM header");
98 }
99
100 if(position == 0) {
101 if(label.size() >= 128) {
102 throw Decoding_Error("PEM: Label too long");
103 }
104 label += static_cast<char>(*b);
105 }
106 }
107
108 std::vector<char> b64;
109
110 const std::string PEM_TRAILER = fmt("-----END {}-----", label);
111 position = 0;
112 while(position != PEM_TRAILER.length()) {
113 auto b = source.read_byte();
114
115 if(!b) {
116 throw Decoding_Error("PEM: No PEM trailer found");
117 }
118 if(static_cast<char>(*b) == PEM_TRAILER[position]) {
119 ++position;
120 } else if(position > 0) {
121 throw Decoding_Error("PEM: Malformed PEM trailer");
122 }
123
124 if(position == 0) {
125 b64.push_back(*b);
126 }
127 }
128
129 return base64_decode(b64.data(), b64.size());
130}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:203

References Botan::base64_decode(), Botan::fmt(), and Botan::DataSource::read_byte().

Referenced by decode(), decode_check_label(), Botan::DL_Group::DL_Group(), Botan::DL_Group::from_PEM(), and Botan::X509_Object::load_data().

◆ decode() [2/2]

secure_vector< uint8_t > Botan::PEM_Code::decode ( std::string_view pem,
std::string & label )

Decode PEM data

Parameters
PEMa string containing PEM encoded data
labelis set to the PEM label found for later inspection

Definition at line 137 of file pem.cpp.

137 {
138 DataSource_Memory src(pem);
139 return decode(src, label);
140}
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:64

References decode().

◆ decode_check_label() [1/2]

secure_vector< uint8_t > Botan::PEM_Code::decode_check_label ( DataSource & pem,
std::string_view label )

Decode PEM data

Parameters
PEMa datasource containing PEM encoded data
labelis what we expect the label to be

Definition at line 51 of file pem.cpp.

51 {
52 std::string label_got;
53 secure_vector<uint8_t> ber = decode(source, label_got);
54 if(label_got != label_want) {
55 throw Decoding_Error(fmt("PEM: Label mismatch, wanted '{}' got '{}'", label_want, label_got));
56 }
57
58 return ber;
59}
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References decode(), and Botan::fmt().

Referenced by decode_check_label(), Botan::CryptoBox::decrypt_bin(), Botan::EC_Group::EC_Group(), Botan::EC_Group::from_PEM(), and Botan::X509::load_key().

◆ decode_check_label() [2/2]

secure_vector< uint8_t > Botan::PEM_Code::decode_check_label ( std::string_view pem,
std::string_view label )

Decode PEM data

Parameters
PEMa string containing PEM encoded data
labelis what we expect the label to be

Definition at line 132 of file pem.cpp.

132 {
133 DataSource_Memory src(pem);
134 return decode_check_label(src, label_want);
135}
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
Definition pem.cpp:51

References decode_check_label().

◆ encode() [1/2]

template<typename Alloc>
std::string Botan::PEM_Code::encode ( const std::vector< uint8_t, Alloc > & data,
std::string_view label,
size_t line_width = 64 )

Encode some binary data in PEM format

Parameters
databinary data to encode
labelPEM label
line_widthafter this many characters, a new line is inserted

Definition at line 40 of file pem.h.

40 {
41 return encode(data.data(), data.size(), label, line_width);
42}
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:41

References encode().

◆ encode() [2/2]

std::string Botan::PEM_Code::encode ( const uint8_t data[],
size_t data_len,
std::string_view label,
size_t line_width = 64 )

Encode some binary data in PEM format

Parameters
databinary data to encode
data_lenlength of binary data in bytes
labelPEM label put after BEGIN and END
line_widthafter this many characters, a new line is inserted

Definition at line 41 of file pem.cpp.

41 {
42 const std::string PEM_HEADER = fmt("-----BEGIN {}-----\n", label);
43 const std::string PEM_TRAILER = fmt("-----END {}-----\n", label);
44
45 return (PEM_HEADER + linewrap(width, base64_encode(der, length)) + PEM_TRAILER);
46}
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:167

References Botan::base64_encode(), and Botan::fmt().

Referenced by botan_privkey_rsa_get_privkey(), encode(), Botan::CryptoBox::encrypt(), Botan::DL_Group::PEM_encode(), Botan::EC_Group::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::TLS::Session::PEM_encode(), Botan::X509::PEM_encode(), Botan::X509_Object::PEM_encode(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(), and Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec().

◆ matches()

bool Botan::PEM_Code::matches ( DataSource & source,
std::string_view extra = "",
size_t search_range = 4096 )

Heuristic test for PEM data.

Definition at line 146 of file pem.cpp.

146 {
147 const std::string PEM_HEADER = fmt("-----BEGIN {}", extra);
148
149 std::array<uint8_t, 4096> stack_buf{};
150 std::vector<uint8_t> heap_buf;
151 uint8_t* search_buf = stack_buf.data();
152 if(search_range > stack_buf.size()) {
153 heap_buf.resize(search_range);
154 search_buf = heap_buf.data();
155 }
156
157 const size_t got = source.peek(search_buf, search_range, 0);
158
159 if(got < PEM_HEADER.length()) {
160 return false;
161 }
162
163 return std::search(search_buf, search_buf + got, PEM_HEADER.begin(), PEM_HEADER.end()) != search_buf + got;
164}
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0

References Botan::fmt(), and Botan::DataSource::peek().

Referenced by Botan::X509_Object::load_data(), and Botan::X509::load_key().