Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: net/base/pem_tokenizer.cc

Issue 2812064: Revert 52799 - Add support for parsing certificate formats other than raw, DE... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 while (pos_ != StringPiece::npos) {
30 // Scan for the beginning of the next PEM encoded block.
31 pos_ = str_.find(kPEMSearchBlock, pos_);
32 if (pos_ == StringPiece::npos)
33 return false; // No more PEM blocks
34
35 std::vector<PEMType>::const_iterator it;
36 // Check to see if it is of an acceptable block type.
37 for (it = block_types_.begin(); it != block_types_.end(); ++it) {
38 if (!str_.substr(pos_).starts_with(it->header))
39 continue;
40
41 // Look for a footer matching the header. If none is found, then all
42 // data following this point is invalid and should not be parsed.
43 StringPiece::size_type footer_pos = str_.find(it->footer, pos_);
44 if (footer_pos == StringPiece::npos) {
45 pos_ = StringPiece::npos;
46 return false;
47 }
48
49 // Chop off the header and footer and parse the data in between.
50 StringPiece::size_type data_begin = pos_ + it->header.size();
51 pos_ = footer_pos + it->footer.size();
52 block_type_ = it->type;
53
54 StringPiece encoded = str_.substr(data_begin,
55 footer_pos - data_begin);
56 if (!base::Base64Decode(CollapseWhitespaceASCII(encoded.as_string(),
57 true), &data_)) {
58 // The most likely cause for a decode failure is a datatype that
59 // includes PEM headers, which are not supported.
60 break;
61 }
62
63 return true;
64 }
65
66 // If the block did not match any acceptable type, move past it and
67 // continue the search. Otherwise, |pos_| has been updated to the most
68 // appropriate search position to continue searching from and should not
69 // be adjusted.
70 if (it == block_types_.end())
71 pos_ += sizeof(kPEMSearchBlock);
72 }
73
74 return false;
75 }
76
77 void PEMTokenizer::Init(
78 const StringPiece& str,
79 const std::vector<std::string>& allowed_block_types) {
80 str_ = str;
81 pos_ = 0;
82
83 // Construct PEM header/footer strings for all the accepted types, to
84 // reduce parsing later.
85 for (std::vector<std::string>::const_iterator it =
86 allowed_block_types.begin(); it != allowed_block_types.end(); ++it) {
87 PEMType allowed_type;
88 allowed_type.type = *it;
89 allowed_type.header = StringPrintf(kPEMBeginBlock, it->c_str());
90 allowed_type.footer = StringPrintf(kPEMEndBlock, it->c_str());
91 block_types_.push_back(allowed_type);
92 }
93 }
94
95 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698