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

Unified Diff: net/base/pem_tokenizer.cc

Issue 2668005: Bring the handling of <keygen> and support for the application/x-x509-user-ce... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Whitespace/style Created 10 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/pem_tokenizer.h ('k') | net/base/pem_tokenizer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/pem_tokenizer.cc
===================================================================
--- net/base/pem_tokenizer.cc (revision 0)
+++ net/base/pem_tokenizer.cc (revision 0)
@@ -0,0 +1,100 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/pem_tokenizer.h"
+
+#include "base/base64.h"
+#include "base/string_util.h"
+
+namespace {
+
+const char kPEMSearchBlock[] = "-----BEGIN ";
+const char kPEMBeginBlock[] = "-----BEGIN %s-----";
+const char kPEMEndBlock[] = "-----END %s-----";
+
+} // namespace
+
+namespace net {
+
+using base::StringPiece;
+
+PEMTokenizer::PEMTokenizer(
+ const StringPiece* str,
+ const std::vector<std::string>& allowed_block_types) {
+ Init(str, allowed_block_types);
+}
+
+bool PEMTokenizer::GetNext() {
+ if (pos_ == StringPiece::npos)
+ return false; // End of search
+
+ while (pos_ != StringPiece::npos) {
+ pos_ = str_->find(kPEMSearchBlock, pos_);
+ if (pos_ == StringPiece::npos)
+ return false; // No more PEM blocks
+
+ std::vector<PEMType>::const_iterator it = block_types_.end();
+ // Check to see if it is of an acceptable block type
+ for (it = block_types_.begin(); it != block_types_.end(); ++it) {
+ if (!str_->substr(pos_).starts_with(it->header))
+ continue;
+
+ StringPiece::size_type footer_pos = str_->find(it->footer, pos_);
+ if (footer_pos == StringPiece::npos) {
+ // A PEM header with no PEM footer has been found. All data
+ // after pos_ is now suspect, and thus will not be parsed.
+ pos_ = StringPiece::npos;
+ return false;
+ }
+
+ StringPiece::size_type data_begin = pos_ + it->header.size();
+ pos_ = footer_pos + it->footer.size();
+ block_type_ = it->type;
+
+ StringPiece encoded = str_->substr(data_begin,
+ footer_pos - data_begin);
+ if (!base::Base64Decode(CollapseWhitespaceASCII(encoded.as_string(),
+ true), &data_)) {
+ // The most likely cause for a decode failure is a datatype that
+ // includes PEM headers, which are not supported.
+ break;
+ }
+
+ // PEM block succesfully decoded into |data_|, and the type has
+ // been stored into |block_type_|
+ return true;
+ }
+
+ // If the block did not match any acceptable type, move past it and
+ // continue the search. If |it| is any other value, then we can be
+ // assured that |pos_| has been updated to the most appropriate search
+ // position to continue searching from.
+ if (it == block_types_.end())
+ pos_ += sizeof(kPEMSearchBlock);
+ }
+
+ return false;
+}
+
+void PEMTokenizer::Init(
+ const StringPiece* str,
+ const std::vector<std::string>& allowed_block_types) {
+ DCHECK(str);
+
+ str_ = str;
+ pos_ = 0;
+
+ // Construct PEM strings for all the accepted types for efficient
+ // comparison.
+ for (std::vector<std::string>::const_iterator it =
+ allowed_block_types.begin(); it != allowed_block_types.end(); ++it) {
+ PEMType allowed_type;
+ allowed_type.type = *it;
+ allowed_type.header = StringPrintf(kPEMBeginBlock, it->c_str());
+ allowed_type.footer = StringPrintf(kPEMEndBlock, it->c_str());
+ block_types_.push_back(allowed_type);
+ }
+}
+
+} // namespace net
Property changes on: net\base\pem_tokenizer.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « net/base/pem_tokenizer.h ('k') | net/base/pem_tokenizer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698