Botan 2.19.1
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 133 of file pem.cpp.

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

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

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

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

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

56 {
57 std::string label_got;
58 secure_vector<uint8_t> ber = decode(source, label_got);
59 if(label_got != label_want)
60 throw Decoding_Error("PEM: Label mismatch, wanted " + label_want +
61 ", got " + label_got);
62 return ber;
63 }
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 43 of file pem.cpp.

44 {
45 const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n";
46 const std::string PEM_TRAILER = "-----END " + label + "-----\n";
47
48 return (PEM_HEADER + linewrap(width, base64_encode(der, length)) + PEM_TRAILER);
49 }
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 142 of file pem.cpp.

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