Botan 3.12.0
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 62 of file pem.cpp.

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

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 135 of file pem.cpp.

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

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 49 of file pem.cpp.

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

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 130 of file pem.cpp.

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

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 38 of file pem.h.

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

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 39 of file pem.cpp.

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

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 143 of file pem.cpp.

143 {
144 const std::string PEM_HEADER = fmt("-----BEGIN {}", extra);
145
146 secure_vector<uint8_t> search_buf(search_range);
147 const size_t got = source.peek(search_buf.data(), search_buf.size(), 0);
148
149 if(got < PEM_HEADER.length()) {
150 return false;
151 }
152
153 size_t index = 0;
154
155 for(size_t j = 0; j != got; ++j) {
156 if(static_cast<char>(search_buf[j]) == PEM_HEADER[index]) {
157 ++index;
158 } else {
159 index = 0;
160 }
161
162 if(index == PEM_HEADER.size()) {
163 return true;
164 }
165 }
166
167 return false;
168}
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().