9#include <botan/data_src.h>
10#include <botan/base64.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
18std::string linewrap(
size_t width, std::string_view in)
21 for(
size_t i = 0; i != in.size(); ++i)
23 if(i > 0 && i % width == 0)
29 if(!out.empty() && out[out.size()-1] !=
'\n')
42std::string
encode(
const uint8_t der[],
size_t length, std::string_view label,
size_t width)
44 const std::string PEM_HEADER =
fmt(
"-----BEGIN {}-----\n", label);
45 const std::string PEM_TRAILER =
fmt(
"-----END {}-----\n", label);
47 return (PEM_HEADER + linewrap(width,
base64_encode(der, length)) + PEM_TRAILER);
54 std::string_view label_want)
56 std::string label_got;
58 if(label_got != label_want)
61 label_want, label_got));
72 const size_t RANDOM_CHAR_LIMIT = 8;
76 const std::string PEM_HEADER1 =
"-----BEGIN ";
77 const std::string PEM_HEADER2 =
"-----";
80 while(position != PEM_HEADER1.length())
85 if(
static_cast<char>(b) == PEM_HEADER1[position])
87 else if(position >= RANDOM_CHAR_LIMIT)
93 while(position != PEM_HEADER2.length())
98 if(
static_cast<char>(b) == PEM_HEADER2[position])
104 label +=
static_cast<char>(b);
107 std::vector<char> b64;
109 const std::string PEM_TRAILER =
fmt(
"-----END {}-----", label);
111 while(position != PEM_TRAILER.length())
116 if(
static_cast<char>(b) == PEM_TRAILER[position])
129 std::string_view label_want)
138 return decode(src, label);
147 const std::string PEM_HEADER =
fmt(
"-----BEGIN {}", extra);
150 const size_t got = source.
peek(search_buf.data(), search_buf.size(), 0);
152 if(got < PEM_HEADER.length())
157 for(
size_t j = 0; j != got; ++j)
159 if(
static_cast<char>(search_buf[j]) == PEM_HEADER[index])
168 if(index == PEM_HEADER.size())
size_t read_byte(uint8_t &out)
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
bool matches(DataSource &source, std::string_view extra, size_t search_range)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
std::string fmt(std::string_view format, const T &... args)
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