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

Unified Diff: net/cert/internal/parse_certificate.cc

Issue 1288193003: Add a function for parsing RFC 5280's "Certificate". (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@test_helpers
Patch Set: Wrap comments in .pem files Created 5 years, 4 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
Index: net/cert/internal/parse_certificate.cc
diff --git a/net/cert/internal/parse_certificate.cc b/net/cert/internal/parse_certificate.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c8b58c07606cfcdeb0c1240fc358598d87cc972d
--- /dev/null
+++ b/net/cert/internal/parse_certificate.cc
@@ -0,0 +1,66 @@
+// Copyright 2015 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/cert/internal/parse_certificate.h"
+
+#include "net/der/input.h"
+#include "net/der/parse_values.h"
+#include "net/der/parser.h"
+
+namespace net {
+
+namespace {
+
+// Returns true if |input| is a SEQUENCE and nothing else.
+WARN_UNUSED_RESULT bool IsSequenceTLV(const der::Input& input) {
+ der::Parser parser(input);
+ der::Parser unused_sequence_parser;
+ if (!parser.ReadSequence(&unused_sequence_parser))
+ return false;
+ // Should by a single SEQUENCE by definition of the function.
+ return !parser.HasMore();
+}
+
+// Reads a SEQUENCE from |parser| and writes the full tag-length-value into
+// |out|. On failure |parser| may or may not have been advanced.
+WARN_UNUSED_RESULT bool ReadSequenceTLV(der::Parser* parser, der::Input* out) {
+ return parser->ReadRawTLV(out) && IsSequenceTLV(*out);
+}
+
+} // namespace
+
+bool ParseCertificate(const der::Input& certificate_tlv,
+ ParsedCertificate* out) {
+ der::Parser parser(certificate_tlv);
+
+ // Certificate ::= SEQUENCE {
+ der::Parser certificate_parser;
+ if (!parser.ReadSequence(&certificate_parser))
+ return false;
+
+ // tbsCertificate TBSCertificate,
+ if (!ReadSequenceTLV(&certificate_parser, &out->tbs_certificate_tlv))
+ return false;
+
+ // signatureAlgorithm AlgorithmIdentifier,
+ if (!ReadSequenceTLV(&certificate_parser, &out->signature_algorithm_tlv))
+ return false;
+
+ // signatureValue BIT STRING }
+ if (!certificate_parser.ReadBitString(&out->signature_value))
+ return false;
+
+ // There isn't an extension point at the end of Certificate.
+ if (certificate_parser.HasMore())
+ return false;
+
+ // By definition the input was a single Certificate, so there shouldn't be
+ // unconsumed data.
+ if (parser.HasMore())
+ return false;
+
+ return true;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698