OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "net/base/pem_tokenizer.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/string_util.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 const char kPEMSearchBlock[] = "-----BEGIN "; |
| 13 const char kPEMBeginBlock[] = "-----BEGIN %s-----"; |
| 14 const char kPEMEndBlock[] = "-----END %s-----"; |
| 15 |
| 16 } // namespace |
| 17 |
| 18 namespace net { |
| 19 |
| 20 using base::StringPiece; |
| 21 |
| 22 PEMTokenizer::PEMTokenizer( |
| 23 const StringPiece* str, |
| 24 const std::vector<std::string>& allowed_block_types) { |
| 25 Init(str, allowed_block_types); |
| 26 } |
| 27 |
| 28 bool PEMTokenizer::GetNext() { |
| 29 if (pos_ == StringPiece::npos) |
| 30 return false; // End of search |
| 31 |
| 32 while (pos_ != StringPiece::npos) { |
| 33 pos_ = str_->find(kPEMSearchBlock, pos_); |
| 34 if (pos_ == StringPiece::npos) |
| 35 return false; // No more PEM blocks |
| 36 |
| 37 std::vector<PEMType>::const_iterator it = block_types_.end(); |
| 38 // Check to see if it is of an acceptable block type |
| 39 for (it = block_types_.begin(); it != block_types_.end(); ++it) { |
| 40 if (!str_->substr(pos_).starts_with(it->header)) |
| 41 continue; |
| 42 |
| 43 StringPiece::size_type footer_pos = str_->find(it->footer, pos_); |
| 44 if (footer_pos == StringPiece::npos) { |
| 45 // A PEM header with no PEM footer has been found. All data |
| 46 // after pos_ is now suspect, and thus will not be parsed. |
| 47 pos_ = StringPiece::npos; |
| 48 return false; |
| 49 } |
| 50 |
| 51 StringPiece::size_type data_begin = pos_ + it->header.size(); |
| 52 pos_ = footer_pos + it->footer.size(); |
| 53 block_type_ = it->type; |
| 54 |
| 55 StringPiece encoded = str_->substr(data_begin, |
| 56 footer_pos - data_begin); |
| 57 if (!base::Base64Decode(CollapseWhitespaceASCII(encoded.as_string(), |
| 58 true), &data_)) { |
| 59 // The most likely cause for a decode failure is a datatype that |
| 60 // includes PEM headers, which are not supported. |
| 61 break; |
| 62 } |
| 63 |
| 64 // PEM block succesfully decoded into |data_|, and the type has |
| 65 // been stored into |block_type_| |
| 66 return true; |
| 67 } |
| 68 |
| 69 // If the block did not match any acceptable type, move past it and |
| 70 // continue the search. If |it| is any other value, then we can be |
| 71 // assured that |pos_| has been updated to the most appropriate search |
| 72 // position to continue searching from. |
| 73 if (it == block_types_.end()) |
| 74 pos_ += sizeof(kPEMSearchBlock); |
| 75 } |
| 76 |
| 77 return false; |
| 78 } |
| 79 |
| 80 void PEMTokenizer::Init( |
| 81 const StringPiece* str, |
| 82 const std::vector<std::string>& allowed_block_types) { |
| 83 DCHECK(str); |
| 84 |
| 85 str_ = str; |
| 86 pos_ = 0; |
| 87 |
| 88 // Construct PEM strings for all the accepted types for efficient |
| 89 // comparison. |
| 90 for (std::vector<std::string>::const_iterator it = |
| 91 allowed_block_types.begin(); it != allowed_block_types.end(); ++it) { |
| 92 PEMType allowed_type; |
| 93 allowed_type.type = *it; |
| 94 allowed_type.header = StringPrintf(kPEMBeginBlock, it->c_str()); |
| 95 allowed_type.footer = StringPrintf(kPEMEndBlock, it->c_str()); |
| 96 block_types_.push_back(allowed_type); |
| 97 } |
| 98 } |
| 99 |
| 100 } // namespace net |
OLD | NEW |