pem.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/cert/pem.h"
  5. #include "base/base64.h"
  6. #include "base/strings/string_piece.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/strings/stringprintf.h"
  9. namespace {
  10. const char kPEMSearchBlock[] = "-----BEGIN ";
  11. const char kPEMBeginBlock[] = "-----BEGIN %s-----";
  12. const char kPEMEndBlock[] = "-----END %s-----";
  13. } // namespace
  14. namespace net {
  15. using base::StringPiece;
  16. struct PEMTokenizer::PEMType {
  17. std::string type;
  18. std::string header;
  19. std::string footer;
  20. };
  21. PEMTokenizer::PEMTokenizer(
  22. const StringPiece& str,
  23. const std::vector<std::string>& allowed_block_types) {
  24. Init(str, allowed_block_types);
  25. }
  26. PEMTokenizer::~PEMTokenizer() = default;
  27. bool PEMTokenizer::GetNext() {
  28. while (pos_ != StringPiece::npos) {
  29. // Scan for the beginning of the next PEM encoded block.
  30. pos_ = str_.find(kPEMSearchBlock, pos_);
  31. if (pos_ == StringPiece::npos)
  32. return false; // No more PEM blocks
  33. std::vector<PEMType>::const_iterator it;
  34. // Check to see if it is of an acceptable block type.
  35. for (it = block_types_.begin(); it != block_types_.end(); ++it) {
  36. if (!base::StartsWith(str_.substr(pos_), it->header))
  37. continue;
  38. // Look for a footer matching the header. If none is found, then all
  39. // data following this point is invalid and should not be parsed.
  40. StringPiece::size_type footer_pos = str_.find(it->footer, pos_);
  41. if (footer_pos == StringPiece::npos) {
  42. pos_ = StringPiece::npos;
  43. return false;
  44. }
  45. // Chop off the header and footer and parse the data in between.
  46. StringPiece::size_type data_begin = pos_ + it->header.size();
  47. pos_ = footer_pos + it->footer.size();
  48. block_type_ = it->type;
  49. StringPiece encoded = str_.substr(data_begin, footer_pos - data_begin);
  50. if (!base::Base64Decode(base::CollapseWhitespaceASCII(encoded, true),
  51. &data_)) {
  52. // The most likely cause for a decode failure is a datatype that
  53. // includes PEM headers, which are not supported.
  54. break;
  55. }
  56. return true;
  57. }
  58. // If the block did not match any acceptable type, move past it and
  59. // continue the search. Otherwise, |pos_| has been updated to the most
  60. // appropriate search position to continue searching from and should not
  61. // be adjusted.
  62. if (it == block_types_.end())
  63. pos_ += sizeof(kPEMSearchBlock);
  64. }
  65. return false;
  66. }
  67. void PEMTokenizer::Init(const StringPiece& str,
  68. const std::vector<std::string>& allowed_block_types) {
  69. str_ = str;
  70. pos_ = 0;
  71. // Construct PEM header/footer strings for all the accepted types, to
  72. // reduce parsing later.
  73. for (const auto& allowed_block_type : allowed_block_types) {
  74. PEMType allowed_type;
  75. allowed_type.type = allowed_block_type;
  76. allowed_type.header =
  77. base::StringPrintf(kPEMBeginBlock, allowed_block_type.c_str());
  78. allowed_type.footer =
  79. base::StringPrintf(kPEMEndBlock, allowed_block_type.c_str());
  80. block_types_.push_back(allowed_type);
  81. }
  82. }
  83. std::string PEMEncode(base::StringPiece data, const std::string& type) {
  84. std::string b64_encoded;
  85. base::Base64Encode(data, &b64_encoded);
  86. // Divide the Base-64 encoded data into 64-character chunks, as per
  87. // 4.3.2.4 of RFC 1421.
  88. static const size_t kChunkSize = 64;
  89. size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
  90. std::string pem_encoded;
  91. pem_encoded.reserve(
  92. // header & footer
  93. 17 + 15 + type.size() * 2 +
  94. // encoded data
  95. b64_encoded.size() +
  96. // newline characters for line wrapping in encoded data
  97. chunks);
  98. pem_encoded = "-----BEGIN ";
  99. pem_encoded.append(type);
  100. pem_encoded.append("-----\n");
  101. for (size_t i = 0, chunk_offset = 0; i < chunks;
  102. ++i, chunk_offset += kChunkSize) {
  103. pem_encoded.append(b64_encoded, chunk_offset, kChunkSize);
  104. pem_encoded.append("\n");
  105. }
  106. pem_encoded.append("-----END ");
  107. pem_encoded.append(type);
  108. pem_encoded.append("-----\n");
  109. return pem_encoded;
  110. }
  111. } // namespace net