Botan  1.11.34
Crypto and TLS for C++11
pem.cpp
Go to the documentation of this file.
1 /*
2 * PEM Encoding/Decoding
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/pem.h>
9 #include <botan/base64.h>
10 #include <botan/parsing.h>
11 #include <botan/exceptn.h>
12 
13 namespace Botan {
14 
15 namespace PEM_Code {
16 
17 namespace {
18 
19 std::string linewrap(size_t width, const std::string& in)
20  {
21  std::string out;
22  for(size_t i = 0; i != in.size(); ++i)
23  {
24  if(i > 0 && i % width == 0)
25  {
26  out.push_back('\n');
27  }
28  out.push_back(in[i]);
29  }
30  if(out.size() > 0 && out[out.size()-1] != '\n')
31  {
32  out.push_back('\n');
33  }
34 
35  return out;
36  }
37 
38 }
39 
40 /*
41 * PEM encode BER/DER-encoded objects
42 */
43 std::string encode(const byte der[], size_t length, const std::string& label, size_t width)
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  }
50 
51 /*
52 * Decode PEM down to raw BER/DER
53 */
55  const std::string& label_want)
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  }
64 
65 /*
66 * Decode PEM down to raw BER/DER
67 */
68 secure_vector<byte> decode(DataSource& source, std::string& label)
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  }
123 
125  const std::string& label_want)
126  {
127  DataSource_Memory src(pem);
128  return decode_check_label(src, label_want);
129  }
130 
131 secure_vector<byte> decode(const std::string& pem, std::string& label)
132  {
133  DataSource_Memory src(pem);
134  return decode(src, label);
135  }
136 
137 /*
138 * Search for a PEM signature
139 */
140 bool matches(DataSource& source, const std::string& extra,
141  size_t search_range)
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  }
164 
165 }
166 
167 }
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::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
size_t read_byte(byte &out)
Definition: data_src.cpp:22
Definition: alg_id.cpp:13
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:140
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0
size_t base64_encode(char out[], const byte in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:35
secure_vector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68
secure_vector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54
std::uint8_t byte
Definition: types.h:31