9 #include <botan/base64.h>
10 #include <botan/parsing.h>
11 #include <botan/exceptn.h>
19 std::string linewrap(
size_t width,
const std::string& in)
22 for(
size_t i = 0; i != in.size(); ++i)
24 if(i > 0 && i % width == 0)
30 if(out.size() > 0 && out[out.size()-1] !=
'\n')
43 std::string
encode(
const uint8_t der[],
size_t length,
const std::string& label,
size_t width)
45 const std::string PEM_HEADER =
"-----BEGIN " + label +
"-----\n";
46 const std::string PEM_TRAILER =
"-----END " + label +
"-----\n";
48 return (PEM_HEADER + linewrap(width,
base64_encode(der, length)) + PEM_TRAILER);
55 const std::string& label_want)
57 std::string label_got;
59 if(label_got != label_want)
61 ", got " + label_got);
70 const size_t RANDOM_CHAR_LIMIT = 8;
72 const std::string PEM_HEADER1 =
"-----BEGIN ";
73 const std::string PEM_HEADER2 =
"-----";
76 while(position != PEM_HEADER1.length())
81 if(b == PEM_HEADER1[position])
83 else if(position >= RANDOM_CHAR_LIMIT)
89 while(position != PEM_HEADER2.length())
94 if(b == PEM_HEADER2[position])
100 label +=
static_cast<char>(b);
103 std::vector<char> b64;
105 const std::string PEM_TRAILER =
"-----END " + label +
"-----";
107 while(position != PEM_TRAILER.length())
112 if(b == PEM_TRAILER[position])
125 const std::string& label_want)
134 return decode(src, label);
143 const std::string PEM_HEADER =
"-----BEGIN " + extra;
146 size_t got = source.
peek(search_buf.data(), search_buf.size(), 0);
148 if(got < PEM_HEADER.length())
153 for(
size_t j = 0; j != got; ++j)
155 if(search_buf[j] == PEM_HEADER[index])
159 if(index == PEM_HEADER.size())
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
size_t base64_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0
std::vector< T, secure_allocator< T >> secure_vector
size_t read_byte(uint8_t &out)
bool matches(DataSource &source, const std::string &extra, size_t search_range)
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)