9#include <botan/data_src.h>
10#include <botan/base64.h>
11#include <botan/exceptn.h>
19std::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')
43std::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;
74 const std::string PEM_HEADER1 =
"-----BEGIN ";
75 const std::string PEM_HEADER2 =
"-----";
78 while(position != PEM_HEADER1.length())
83 if(b == PEM_HEADER1[position])
85 else if(position >= RANDOM_CHAR_LIMIT)
91 while(position != PEM_HEADER2.length())
96 if(b == PEM_HEADER2[position])
102 label +=
static_cast<char>(b);
105 std::vector<char> b64;
107 const std::string PEM_TRAILER =
"-----END " + label +
"-----";
109 while(position != PEM_TRAILER.length())
114 if(b == PEM_TRAILER[position])
127 const std::string& label_want)
136 return decode(src, label);
145 const std::string PEM_HEADER =
"-----BEGIN " + extra;
148 size_t got = source.
peek(search_buf.data(), search_buf.size(), 0);
150 if(got < PEM_HEADER.length())
155 for(
size_t j = 0; j != got; ++j)
157 if(search_buf[j] == PEM_HEADER[index])
161 if(index == PEM_HEADER.size())
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0
size_t read_byte(uint8_t &out)
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)
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
std::vector< T, secure_allocator< T > > secure_vector