Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

Functions
Botan::PEM_Code Namespace Reference

## Functions
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)
std::string | encode (const std::vector< uint8_t > &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)
std::string | encode (const secure_vector< uint8_t > &data, const std::string &label, size_t line_width=64)
bool | matches (DataSource &source, const std::string &extra, size_t search_range)

## Function Documentation

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

Decode PEM data

Parameters

  pem | a datasource containing PEM encoded data
  label | is 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(), Botan::BER_Decoder::decode_and_check(), decode_check_label(), Botan::BER_Decoder::decode_optional_string(), Botan::DL_Group::PEM_decode(), and Botan::RSA_PrivateKey::RSA_PrivateKey().

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 uint8_t 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 uint8_t 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 uint8_t 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 }
Botan::base64_decode
size_t base64_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition: base64.cpp:100

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

Decode PEM data

Parameters

  pem | a string containing PEM encoded data
  label | is 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 }
Botan::PEM_Code::decode
secure_vector< uint8_t > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:131

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

Decode PEM data

Parameters

  pem | a datasource containing PEM encoded data
  label | is 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(), Botan::X509::load_key(), Botan::PKCS10_Request::raw_public_key(), and Botan::TLS::Session::Session().

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 }
Botan::PEM_Code::decode
secure_vector< uint8_t > decode(const std::string &pem, std::string &label)
Definition: pem.cpp:131

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

Decode PEM data

Parameters

  pem | a string containing PEM encoded data
  label | is 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 }
Botan::PEM_Code::decode_check_label
secure_vector< uint8_t > 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< uint8_t > & | data,
|  | const std::string & | label,
|  | size_t | line_width = `64`
| ) |  |
| inline

Encode some binary data in PEM format

Parameters

  data | binary data to encode
  label | PEM label
  line_width | after this many characters, a new line is inserted

Definition at line 35 of file pem.h.

References encode().

38 {
39 return encode(data.data(), data.size(), label, line_width);
40 }
Botan::PEM_Code::encode
std::string encode(const secure_vector< uint8_t > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:48

BOTAN_DLL 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

  data | binary data to encode
  data_len | length of binary data in bytes
  label | PEM label put after BEGIN and END
  line_width | after this many characters, a new line is inserted

Definition at line 43 of file pem.cpp.

References Botan::base64_encode().

Referenced by Botan::X509::create_cert_req(), encode(), Botan::DER_Encoder::encode_list(), Botan::DER_Encoder::encode_optional(), Botan::CryptoBox::encrypt(), Botan::X942_PRF::kdf(), Botan::X509_CA::make_cert(), Botan::X509::PEM_encode(), Botan::DL_Group::PEM_encode(), Botan::EC_Group::PEM_encode(), Botan::X509_Object::PEM_encode(), Botan::TLS::Session::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(), and Botan::McEliece_PrivateKey::private_key_bits().

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

std::string Botan::PEM_Code::encode | ( | const secure_vector< uint8_t > & | data,
|  | const std::string & | label,
|  | size_t | line_width = `64`
| ) |  |
| inline

Encode some binary data in PEM format

Parameters

  data | binary data to encode
  label | PEM label put after BEGIN and END
  line_width | after this many characters, a new line is inserted

Definition at line 48 of file pem.h.

References encode().

51 {
52 return encode(data.data(), data.size(), label, line_width);
53 }
Botan::PEM_Code::encode
std::string encode(const secure_vector< uint8_t > &data, const std::string &label, size_t line_width=64)
Definition: pem.h:48

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 Botan::X509::load_key().

142 {
143 const std::string PEM_HEADER = "-----BEGIN " + extra;
144
145 secure_vector<uint8_t> 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 }