Botan  1.11.26
Functions
Botan::PEM_Code Namespace Reference

Functions

secure_vector< bytedecode (DataSource &source, std::string &label)
 
secure_vector< bytedecode (const std::string &pem, std::string &label)
 
secure_vector< bytedecode_check_label (DataSource &source, const std::string &label_want)
 
secure_vector< bytedecode_check_label (const std::string &pem, const std::string &label_want)
 
std::string encode (const std::vector< byte > &data, const std::string &label, size_t line_width=64)
 
std::string encode (const secure_vector< byte > &data, const std::string &label, size_t line_width=64)
 
std::string encode (const byte 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

BOTAN_DLL secure_vector< byte > 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.

References Botan::base64_decode(), and Botan::DataSource::read_byte().

Referenced by decode(), decode_check_label(), encode(), Botan::DL_Group::PEM_decode(), and Botan::X509_Object::X509_Object().

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

References decode().

132  {
133  DataSource_Memory src(pem);
134  return decode(src, label);
135  }
secure_vector< byte > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:131
BOTAN_DLL secure_vector< byte > 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.

References decode().

Referenced by decode_check_label(), Botan::CryptoBox::decrypt(), Botan::EC_Group::EC_Group(), encode(), Botan::X509::load_key(), Botan::PKCS10_Request::raw_public_key(), and Botan::TLS::Session::Session().

56  {
57  std::string label_got;
58  secure_vector<byte> 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< byte > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:131
BOTAN_DLL secure_vector< byte > 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 124 of file pem.cpp.

References decode_check_label().

126  {
127  DataSource_Memory src(pem);
128  return decode_check_label(src, label_want);
129  }
secure_vector< byte > decode_check_label(const std::string &pem, const std::string &label_want)
Definition: pem.cpp:124
std::string Botan::PEM_Code::encode ( const std::vector< byte > &  data,
const std::string &  label,
size_t  line_width = 64 
)
inline

Encode some binary data in PEM format

Definition at line 28 of file pem.h.

References encode().

31  {
32  return encode(data.data(), data.size(), label, line_width);
33  }
std::string encode(const secure_vector< byte > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:38
std::string Botan::PEM_Code::encode ( const secure_vector< byte > &  data,
const std::string &  label,
size_t  line_width = 64 
)
inline

Encode some binary data in PEM format

Definition at line 38 of file pem.h.

References decode(), decode_check_label(), encode(), and matches().

41  {
42  return encode(data.data(), data.size(), label, line_width);
43  }
std::string encode(const secure_vector< byte > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:38
BOTAN_DLL std::string Botan::PEM_Code::encode ( const byte  data[],
size_t  data_len,
const std::string &  label,
size_t  line_width = 64 
)
BOTAN_DLL bool Botan::PEM_Code::matches ( DataSource source,
const std::string &  extra = "",
size_t  search_range = 4096 
)

Heuristic test for PEM data.

Definition at line 140 of file pem.cpp.

References Botan::DataSource::peek().

Referenced by encode(), Botan::X509::load_key(), Botan::GeneralName::name(), and Botan::X509_Object::X509_Object().

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