| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "pem.h" |
| 16 | #include "string_util.h" |
| 17 |
| 18 | #include <string_view> |
| 19 |
| 20 | namespace { |
| 21 |
| 22 | constexpr std::string_view kPEMHeaderBeginBlock = "-----BEGIN "; |
| 23 | constexpr std::string_view kPEMHeaderEndBlock = "-----END "; |
| 24 | constexpr std::string_view kPEMHeaderTail = "-----"; |
| 25 |
| 26 | } // namespace |
| 27 |
| 28 | BSSL_NAMESPACE_BEGIN |
| 29 |
| 30 |
| 31 |
| 32 | struct PEMTokenizer::PEMType { |
| 33 | std::string type; |
| 34 | std::string header; |
| 35 | std::string footer; |
| 36 | }; |
| 37 |
| 38 | PEMTokenizer::PEMTokenizer( |
| 39 | std::string_view str, const std::vector<std::string> &allowed_block_types) { |
| 40 | Init(str, allowed_block_types); |
| 41 | } |
| 42 |
| 43 | PEMTokenizer::~PEMTokenizer() = default; |
| 44 |
| 45 | bool PEMTokenizer::GetNext() { |
| 46 | while (pos_ != std::string_view::npos) { |
| 47 | // Scan for the beginning of the next PEM encoded block. |
| 48 | pos_ = str_.find(kPEMHeaderBeginBlock, pos_); |
| 49 | if (pos_ == std::string_view::npos) { |
| 50 | return false; // No more PEM blocks |
| 51 | } |
| 52 |
| 53 | std::vector<PEMType>::const_iterator it; |
| 54 | // Check to see if it is of an acceptable block type. |
| 55 | for (it = block_types_.begin(); it != block_types_.end(); ++it) { |
| 56 | if (!bssl::string_util::StartsWith(str_.substr(pos_), it->header)) { |
| 57 | continue; |
| 58 | } |
| 59 |
| 60 | // Look for a footer matching the header. If none is found, then all |
| 61 | // data following this point is invalid and should not be parsed. |
| 62 | std::string_view::size_type footer_pos = str_.find(it->footer, pos_); |
| 63 | if (footer_pos == std::string_view::npos) { |
| 64 | pos_ = std::string_view::npos; |
| 65 | return false; |
| 66 | } |
| 67 |
| 68 | // Chop off the header and footer and parse the data in between. |
| 69 | std::string_view::size_type data_begin = pos_ + it->header.size(); |
| 70 | pos_ = footer_pos + it->footer.size(); |
| 71 | block_type_ = it->type; |
| 72 |
| 73 | std::string_view encoded = |
| 74 | str_.substr(data_begin, footer_pos - data_begin); |
| 75 | if (!string_util::Base64Decode( |
| 76 | string_util::CollapseWhitespaceASCII(encoded, true), &data_)) { |
| 77 | // The most likely cause for a decode failure is a datatype that |
| 78 | // includes PEM headers, which are not supported. |
| 79 | break; |
| 80 | } |
| 81 |
| 82 | return true; |
| 83 | } |
| 84 |
| 85 | // If the block did not match any acceptable type, move past it and |
| 86 | // continue the search. Otherwise, |pos_| has been updated to the most |
| 87 | // appropriate search position to continue searching from and should not |
| 88 | // be adjusted. |
| 89 | if (it == block_types_.end()) { |
| 90 | pos_ += kPEMHeaderBeginBlock.size(); |
| 91 | } |
| 92 | } |
| 93 |
| 94 | return false; |
| 95 | } |
| 96 |
| 97 | void PEMTokenizer::Init(std::string_view str, |
| 98 | const std::vector<std::string> &allowed_block_types) { |
| 99 | str_ = str; |
| 100 | pos_ = 0; |
| 101 |
| 102 | // Construct PEM header/footer strings for all the accepted types, to |
| 103 | // reduce parsing later. |
| 104 | for (const auto &allowed_block_type : allowed_block_types) { |
| 105 | PEMType allowed_type; |
| 106 | allowed_type.type = allowed_block_type; |
| 107 | allowed_type.header = kPEMHeaderBeginBlock; |
| 108 | allowed_type.header.append(allowed_block_type); |
| 109 | allowed_type.header.append(kPEMHeaderTail); |
| 110 | allowed_type.footer = kPEMHeaderEndBlock; |
| 111 | allowed_type.footer.append(allowed_block_type); |
| 112 | allowed_type.footer.append(kPEMHeaderTail); |
| 113 | block_types_.push_back(allowed_type); |
| 114 | } |
| 115 | } |
| 116 |
| 117 | std::string PEMEncode(std::string_view data, const std::string &type) { |
| 118 | std::string b64_encoded; |
| 119 | string_util::Base64Encode(data, &b64_encoded); |
| 120 |
| 121 | // Divide the Base-64 encoded data into 64-character chunks, as per |
| 122 | // 4.3.2.4 of RFC 1421. |
| 123 | static const size_t kChunkSize = 64; |
| 124 | size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize; |
| 125 |
| 126 | std::string pem_encoded; |
| 127 | pem_encoded.reserve( |
| 128 | // header & footer |
| 129 | 17 + 15 + type.size() * 2 + |
| 130 | // encoded data |
| 131 | b64_encoded.size() + |
| 132 | // newline characters for line wrapping in encoded data |
| 133 | chunks); |
| 134 |
| 135 | pem_encoded = kPEMHeaderBeginBlock; |
| 136 | pem_encoded.append(type); |
| 137 | pem_encoded.append(kPEMHeaderTail); |
| 138 | pem_encoded.append("\n"); |
| 139 |
| 140 | for (size_t i = 0, chunk_offset = 0; i < chunks; |
| 141 | ++i, chunk_offset += kChunkSize) { |
| 142 | pem_encoded.append(b64_encoded, chunk_offset, kChunkSize); |
| 143 | pem_encoded.append("\n"); |
| 144 | } |
| 145 |
| 146 | pem_encoded.append(kPEMHeaderEndBlock); |
| 147 | pem_encoded.append(type); |
| 148 | pem_encoded.append(kPEMHeaderTail); |
| 149 | pem_encoded.append("\n"); |
| 150 | return pem_encoded; |
| 151 | } |
| 152 |
| 153 | BSSL_NAMESPACE_END |
| 154 |