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/data_src.h>
10 #include <botan/base64.h>
11 #include <botan/exceptn.h>
12
13 namespace Botan {
14
15 namespace PEM_Code {
16
17 namespace {
18
linewrap(size_t width,const std::string & in)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 */
encode(const uint8_t der[],size_t length,const std::string & label,size_t width)43 std::string encode(const uint8_t 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 */
decode_check_label(DataSource & source,const std::string & label_want)54 secure_vector<uint8_t> decode_check_label(DataSource& source,
55 const std::string& label_want)
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 }
64
65 /*
66 * Decode PEM down to raw BER/DER
67 */
decode(DataSource & source,std::string & label)68 secure_vector<uint8_t> decode(DataSource& source, std::string& label)
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 }
125
decode_check_label(const std::string & pem,const std::string & label_want)126 secure_vector<uint8_t> decode_check_label(const std::string& pem,
127 const std::string& label_want)
128 {
129 DataSource_Memory src(pem);
130 return decode_check_label(src, label_want);
131 }
132
decode(const std::string & pem,std::string & label)133 secure_vector<uint8_t> decode(const std::string& pem, std::string& label)
134 {
135 DataSource_Memory src(pem);
136 return decode(src, label);
137 }
138
139 /*
140 * Search for a PEM signature
141 */
matches(DataSource & source,const std::string & extra,size_t search_range)142 bool matches(DataSource& source, const std::string& extra,
143 size_t search_range)
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 }
166
167 }
168
169 }
170