Botan  2.10.0
Crypto and TLS for C++11
Functions | Variables
Botan::PEM_Code Namespace Reference

Functions

 BOTAN_PUBLIC_API (2, 0) secure_vector< uint8_t > decode(DataSource &pem
 
secure_vector< uint8_t > decode (DataSource &source, std::string &label)
 
secure_vector< uint8_t > decode (const std::string &pem, std::string &label)
 
secure_vector< uint8_t > decode_check_label (DataSource &source, const std::string &label_want)
 
secure_vector< uint8_t > decode_check_label (const std::string &pem, const std::string &label_want)
 
template<typename Alloc >
BOTAN_PUBLIC_API(2, 0) std 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)
 

Variables

const std::string & extra = ""
 
std::string & label
 
const std::string size_t search_range = 4096)
 

Function Documentation

◆ BOTAN_PUBLIC_API()

Botan::PEM_Code::BOTAN_PUBLIC_API ( ,
 
) &

Decode PEM data

Parameters
pema datasource containing PEM encoded data
labelis set to the PEM label found for later inspection

Heuristic test for PEM data.

◆ decode() [1/2]

secure_vector<uint8_t> Botan::PEM_Code::decode ( DataSource &  source,
std::string &  label 
)

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  }
bool const OID & b
Definition: asn1_oid.h:109
uint8_t size_t const uint8_t size_t const uint8_t size_t const uint8_t label[]
Definition: ffi.h:643
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:181
const botan_hash_t source
Definition: ffi.h:269

References Botan::b, Botan::base64_decode(), label, and source.

Referenced by Botan::BOTAN_PUBLIC_API(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), decode(), decode_check_label(), Botan::OS2ECP(), and Botan::pbes2_decrypt().

◆ decode() [2/2]

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

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
uint8_t size_t const uint8_t size_t const uint8_t size_t const uint8_t label[]
Definition: ffi.h:643

References decode(), and label.

◆ decode_check_label() [1/2]

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

Decode PEM data

Parameters
pema string containing PEM encoded data
labelis set to the PEM label found for later inspectionDecode PEM data
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  }
secure_vector< uint8_t > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:133
const botan_hash_t source
Definition: ffi.h:269

References decode(), and source.

Referenced by decode_check_label(), Botan::CryptoBox::decrypt_bin(), and Botan::X509::load_key().

◆ decode_check_label() [2/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().

◆ encode() [1/2]

template<typename Alloc >
BOTAN_PUBLIC_API (2,0) std 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
data_lenlength of binary data in bytes
labelPEM label put after BEGIN and END
line_widthafter this many characters, a new line is insertedEncode some binary data in PEM format
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  }
BOTAN_PUBLIC_API(2, 0) std std::string encode(const std::vector< uint8_t, Alloc > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:39
uint8_t size_t const uint8_t size_t const uint8_t size_t const uint8_t label[]
Definition: ffi.h:643

References encode(), and label.

◆ encode() [2/2]

std::string Botan::PEM_Code::encode ( const uint8_t  der[],
size_t  length,
const std::string &  label,
size_t  width 
)

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  }
uint8_t size_t const uint8_t size_t const uint8_t size_t const uint8_t label[]
Definition: ffi.h:643
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:166

References Botan::base64_encode(), and label.

Referenced by botan_privkey_rsa_get_privkey(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), encode(), Botan::CryptoBox::encrypt(), Botan::X509::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(), and Botan::TLS::Server_Key_Exchange::Server_Key_Exchange().

◆ matches()

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

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  }
const std::string size_t search_range
Definition: pem.h:85
const botan_hash_t source
Definition: ffi.h:269
const std::string & extra
Definition: pem.h:84

References extra, search_range, and source.

Referenced by Botan::X509::load_key().

Variable Documentation

◆ extra

const std::string& Botan::PEM_Code::extra = ""

Definition at line 84 of file pem.h.

Referenced by matches().

◆ label

std::string& Botan::PEM_Code::label

Definition at line 52 of file pem.h.

Referenced by decode(), and encode().

◆ search_range

const std::string size_t Botan::PEM_Code::search_range = 4096)

Definition at line 85 of file pem.h.

Referenced by matches().