Botan 3.0.0-alpha0
Crypto and TLS for C&
Functions
Botan::PEM_Code Namespace Reference

Functions

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

Function Documentation

◆ decode() [1/2]

secure_vector< uint8_t > Botan::PEM_Code::decode ( const std::string &  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 131 of file pem.cpp.

132 {
133 DataSource_Memory src(pem);
134 return decode(src, label);
135 }
secure_vector< uint8_t > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:131

References decode().

◆ decode() [2/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 66 of file pem.cpp.

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

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

Referenced by Botan::BER_Decoder::decode(), decode(), Botan::BER_Decoder::decode_and_check(), decode_check_label(), Botan::BER_Decoder::decode_optional_string(), Botan::DL_Group::DL_Group(), Botan::DL_Group::DL_Group_from_PEM(), Botan::BER_Decoder::get_next_octet_string(), Botan::Kyber_PrivateKey::Kyber_PrivateKey(), Botan::X509_Object::load_data(), and Botan::RSA_PrivateKey::RSA_PrivateKey().

◆ decode_check_label() [1/2]

secure_vector< uint8_t > Botan::PEM_Code::decode_check_label ( const std::string &  pem,
const std::string &  label 
)

Decode PEM data

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

Definition at line 124 of file pem.cpp.

126 {
127 DataSource_Memory src(pem);
128 return decode_check_label(src, label_want);
129 }
secure_vector< uint8_t > decode_check_label(const std::string &pem, const std::string &label_want)
Definition: pem.cpp:124

References decode_check_label().

◆ decode_check_label() [2/2]

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

Decode PEM data

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

Definition at line 52 of file pem.cpp.

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

References decode().

Referenced by decode_check_label(), Botan::CryptoBox::decrypt_bin(), Botan::EC_Group::EC_Group(), Botan::EC_Group::EC_Group_from_PEM(), Botan::X509::load_key(), and Botan::TLS::Session::Session().

◆ encode() [1/2]

template<typename Alloc >
std::string Botan::PEM_Code::encode ( const std::vector< uint8_t, Alloc > &  data,
const std::string &  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 39 of file pem.h.

42 {
43 return encode(data.data(), data.size(), label, line_width);
44 }
std::string encode(const std::vector< uint8_t, Alloc > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:39

References encode().

◆ encode() [2/2]

std::string Botan::PEM_Code::encode ( const uint8_t  data[],
size_t  data_len,
const std::string &  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.

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

References Botan::base64_encode().

Referenced by botan_privkey_rsa_get_privkey(), Botan::EC_Group::DER_encode(), encode(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode_if(), Botan::DER_Encoder::encode_list(), Botan::DER_Encoder::encode_optional(), Botan::CryptoBox::encrypt(), Botan::X942_PRF::kdf(), Botan::X509_CA::make_cert(), Botan::EC_Group::PEM_encode(), Botan::TLS::Session::PEM_encode(), Botan::X509_Object::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::X509::PEM_encode(), Botan::DL_Group::PEM_encode(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(), and Botan::McEliece_PrivateKey::private_key_bits().

◆ matches()

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

Heuristic test for PEM data.

Definition at line 140 of file pem.cpp.

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

References Botan::DataSource::peek().

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