Botan 3.14.1
Crypto and TLS for C&
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
10#include <botan/base64.h>
11#include <botan/data_src.h>
12#include <botan/exceptn.h>
13#include <botan/internal/fmt.h>
14#include <algorithm>
15#include <array>
16
17namespace Botan::PEM_Code {
18
19namespace {
20
21std::string linewrap(size_t width, std::string_view in) {
22 std::string out;
23 for(size_t i = 0; i != in.size(); ++i) {
24 if(i > 0 && i % width == 0) {
25 out.push_back('\n');
26 }
27 out.push_back(in[i]);
28 }
29 if(!out.empty() && out[out.size() - 1] != '\n') {
30 out.push_back('\n');
31 }
32
33 return out;
34}
35
36} // namespace
37
38/*
39* PEM encode BER/DER-encoded objects
40*/
41std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width) {
42 const std::string PEM_HEADER = fmt("-----BEGIN {}-----\n", label);
43 const std::string PEM_TRAILER = fmt("-----END {}-----\n", label);
44
45 return (PEM_HEADER + linewrap(width, base64_encode(der, length)) + PEM_TRAILER);
46}
47
48/*
49* Decode PEM down to raw BER/DER
50*/
51secure_vector<uint8_t> decode_check_label(DataSource& source, std::string_view label_want) {
52 std::string label_got;
53 secure_vector<uint8_t> ber = decode(source, label_got);
54 if(label_got != label_want) {
55 throw Decoding_Error(fmt("PEM: Label mismatch, wanted '{}' got '{}'", label_want, label_got));
56 }
57
58 return ber;
59}
60
61/*
62* Decode PEM down to raw BER/DER
63*/
64secure_vector<uint8_t> decode(DataSource& source, std::string& label) {
65 const size_t RANDOM_CHAR_LIMIT = 8;
66
67 label.clear();
68
69 const std::string PEM_HEADER1 = "-----BEGIN ";
70 const std::string PEM_HEADER2 = "-----";
71 size_t position = 0;
72
73 while(position != PEM_HEADER1.length()) {
74 auto b = source.read_byte();
75
76 if(!b) {
77 throw Decoding_Error("PEM: No PEM header found");
78 }
79 if(static_cast<char>(*b) == PEM_HEADER1[position]) {
80 ++position;
81 } else if(position >= RANDOM_CHAR_LIMIT) {
82 throw Decoding_Error("PEM: Malformed PEM header");
83 } else {
84 position = 0;
85 }
86 }
87 position = 0;
88 while(position != PEM_HEADER2.length()) {
89 auto b = source.read_byte();
90
91 if(!b) {
92 throw Decoding_Error("PEM: No PEM header found");
93 }
94 if(static_cast<char>(*b) == PEM_HEADER2[position]) {
95 ++position;
96 } else if(position > 0) {
97 throw Decoding_Error("PEM: Malformed PEM header");
98 }
99
100 if(position == 0) {
101 if(label.size() >= 128) {
102 throw Decoding_Error("PEM: Label too long");
103 }
104 label += static_cast<char>(*b);
105 }
106 }
107
108 std::vector<char> b64;
109
110 const std::string PEM_TRAILER = fmt("-----END {}-----", label);
111 position = 0;
112 while(position != PEM_TRAILER.length()) {
113 auto b = source.read_byte();
114
115 if(!b) {
116 throw Decoding_Error("PEM: No PEM trailer found");
117 }
118 if(static_cast<char>(*b) == PEM_TRAILER[position]) {
119 ++position;
120 } else if(position > 0) {
121 throw Decoding_Error("PEM: Malformed PEM trailer");
122 }
123
124 if(position == 0) {
125 b64.push_back(*b);
126 }
127 }
128
129 return base64_decode(b64.data(), b64.size());
130}
131
132secure_vector<uint8_t> decode_check_label(std::string_view pem, std::string_view label_want) {
133 DataSource_Memory src(pem);
134 return decode_check_label(src, label_want);
135}
136
137secure_vector<uint8_t> decode(std::string_view pem, std::string& label) {
138 DataSource_Memory src(pem);
139 return decode(src, label);
140}
141
142/*
143* Search for a PEM signature
144*/
145// NOLINTNEXTLINE(*-const-correctness) TODO(Botan4) take a const DataSource&
146bool matches(DataSource& source, std::string_view extra, size_t search_range) {
147 const std::string PEM_HEADER = fmt("-----BEGIN {}", extra);
148
149 std::array<uint8_t, 4096> stack_buf{};
150 std::vector<uint8_t> heap_buf;
151 uint8_t* search_buf = stack_buf.data();
152 if(search_range > stack_buf.size()) {
153 heap_buf.resize(search_range);
154 search_buf = heap_buf.data();
155 }
156
157 const size_t got = source.peek(search_buf, search_range, 0);
158
159 if(got < PEM_HEADER.length()) {
160 return false;
161 }
162
163 return std::search(search_buf, search_buf + got, PEM_HEADER.begin(), PEM_HEADER.end()) != search_buf + got;
164}
165
166} // namespace Botan::PEM_Code
size_t read_byte(uint8_t &out)
Definition data_src.cpp:27
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:41
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
Definition pem.cpp:51
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:146
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:64
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:167
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:203
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128