Botan: src/codec/pem/pem.cpp Source File

Botan 1.10.16

pem.cpp
Go to the documentation of this file.
1 /*
2 * PEM Encoding/Decoding
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/pem.h>
9 #include <botan/filters.h>
10 #include <botan/parsing.h>
11
12 namespace Botan {
13
14 namespace PEM_Code {
15
16 /*
17 * PEM encode BER/DER-encoded objects
18 */
19 std::string encode(const byte der[], size_t length, const std::string& label,
20 size_t width)
21 {
22 const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n";
23 const std::string PEM_TRAILER = "-----END " + label + "-----\n";
24
25 Pipe pipe(new Base64_Encoder(true, width));
26 pipe.process_msg(der, length);
27 return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER);
28 }
29
30 /*
31 * PEM encode BER/DER-encoded objects
32 */
33 std::string encode(const MemoryRegion<byte>& data, const std::string& label,
34 size_t width)
35 {
36 return encode(&data[0], data.size(), label, width);
37 }
38
39 /*
40 * Decode PEM down to raw BER/DER
41 */
42 SecureVector<byte> decode_check_label(DataSource& source,
43 const std::string& label_want)
44 {
45 std::string label_got;
46 SecureVector<byte> ber = decode(source, label_got);
47 if(label_got != label_want)
48 throw Decoding_Error("PEM: Label mismatch, wanted " + label_want +
49 ", got " + label_got);
50 return ber;
51 }
52
53 /*
54 * Decode PEM down to raw BER/DER
55 */
56 SecureVector<byte> decode(DataSource& source, std::string& label)
57 {
58 const size_t RANDOM_CHAR_LIMIT = 8;
59
60 const std::string PEM_HEADER1 = "-----BEGIN ";
61 const std::string PEM_HEADER2 = "-----";
62 size_t position = 0;
63
64 while(position != PEM_HEADER1.length())
65 {
66 byte b;
67 if(!source.read_byte(b))
68 throw Decoding_Error("PEM: No PEM header found");
69 if(b == PEM_HEADER1[position])
70 ++position;
71 else if(position >= RANDOM_CHAR_LIMIT)
72 throw Decoding_Error("PEM: Malformed PEM header");
73 else
74 position = 0;
75 }
76 position = 0;
77 while(position != PEM_HEADER2.length())
78 {
79 byte b;
80 if(!source.read_byte(b))
81 throw Decoding_Error("PEM: No PEM header found");
82 if(b == PEM_HEADER2[position])
83 ++position;
84 else if(position)
85 throw Decoding_Error("PEM: Malformed PEM header");
86
87 if(position == 0)
88 label += static_cast<char>(b);
89 }
90
91 Pipe base64(new Base64_Decoder);
92 base64.start_msg();
93
94 const std::string PEM_TRAILER = "-----END " + label + "-----";
95 position = 0;
96 while(position != PEM_TRAILER.length())
97 {
98 byte b;
99 if(!source.read_byte(b))
100 throw Decoding_Error("PEM: No PEM trailer found");
101 if(b == PEM_TRAILER[position])
102 ++position;
103 else if(position)
104 throw Decoding_Error("PEM: Malformed PEM trailer");
105
106 if(position == 0)
107 base64.write(b);
108 }
109 base64.end_msg();
110 return base64.read_all();
111 }
112
113 /*
114 * Search for a PEM signature
115 */
116 bool matches(DataSource& source, const std::string& extra,
117 size_t search_range)
118 {
119 const std::string PEM_HEADER = "-----BEGIN " + extra;
120
121 SecureVector<byte> search_buf(search_range);
122 size_t got = source.peek(&search_buf[0], search_buf.size(), 0);
123
124 if(got < PEM_HEADER.length())
125 return false;
126
127 size_t index = 0;
128
129 for(size_t j = 0; j != got; ++j)
130 {
131 if(search_buf[j] == PEM_HEADER[index])
132 ++index;
133 else
134 index = 0;
135 if(index == PEM_HEADER.size())
136 return true;
137 }
138 return false;
139 }
140
141 }
142
143 }
Botan::Pipe::start_msg
void start_msg()
Definition: pipe.cpp:152
Botan::Pipe::read_all_as_string
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:117
Botan::Pipe::write
void write(const byte in[], size_t length)
Definition: pipe_rw.cpp:34
Botan::DataSource
Definition: data_src.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::PEM_Code::decode
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
Botan::Pipe::end_msg
void end_msg()
Definition: pipe.cpp:166
Botan::Pipe::read_all
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
Botan::DataSource::peek
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
Botan::Base64_Decoder
Definition: b64_filt.h:57
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::DataSource::read_byte
size_t read_byte(byte &out)
Definition: data_src.cpp:19
Botan::Base64_Encoder
Definition: b64_filt.h:18
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:116
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::SecureVector< byte >
Botan::Pipe
Definition: pipe.h:25
Botan::PEM_Code::decode_check_label
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Botan::Pipe::process_msg
void process_msg(const byte in[], size_t length)
Definition: pipe.cpp:116