Index: components/certificate_transparency/log_response_parser.cc |
diff --git a/components/certificate_transparency/log_response_parser.cc b/components/certificate_transparency/log_response_parser.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb37bc4223ba627171ea25251e9e5d494841403f |
--- /dev/null |
+++ b/components/certificate_transparency/log_response_parser.cc |
@@ -0,0 +1,80 @@ |
+// 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 "components/certificate_transparency/log_response_parser.h" |
+ |
+#include "base/base64.h" |
+#include "base/logging.h" |
+#include "base/time/time.h" |
+#include "base/values.h" |
+#include "net/cert/ct_serialization.h" |
+#include "net/cert/signed_tree_head.h" |
+ |
+namespace certificate_transparency { |
+ |
+bool CTLogResponseParser::FillSignedTreeHead(scoped_ptr<base::Value> json, |
+ net::ct::SignedTreeHead* sth) { |
+ const base::DictionaryValue* json_dict; |
+ if (!json->GetAsDictionary(&json_dict)) { |
+ LOG(WARNING) << "Json value not a dictionary."; |
+ return false; |
+ } |
+ |
+ int tree_size; |
+ if (!json_dict->GetInteger("tree_size", &tree_size)) { |
+ LOG(WARNING) << "Json dictionary does not contain tree_size"; |
+ return false; |
+ } |
+ |
+ double timestamp; |
+ if (!json_dict->GetDouble("timestamp", ×tamp)) { |
+ LOG(WARNING) << "Json dictionary does not contain timestamp"; |
+ return false; |
+ } |
+ |
+ std::string sha256_root_hash; |
+ if (!json_dict->GetString("sha256_root_hash", &sha256_root_hash)) { |
+ LOG(WARNING) << "Json dictionary does not contain sha256_root_hash"; |
+ return false; |
+ } |
+ |
+ std::string tree_head_signature; |
+ if (!json_dict->GetString("tree_head_signature", &tree_head_signature)) { |
+ LOG(WARNING) << "Json dictionary does not contain tree_head_signature"; |
+ return false; |
+ } |
+ |
+ std::string decoded_root_hash; |
+ if (!base::Base64Decode(sha256_root_hash, &decoded_root_hash)) { |
+ LOG(WARNING) << "Failed decoding sha256_root_hash"; |
+ return false; |
+ } |
+ |
+ if (decoded_root_hash.length() != net::ct::kSthRootHashLength) { |
+ LOG(WARNING) << "sha256_root_hash must be exactly 32 bit."; |
+ return false; |
+ } |
+ |
+ std::string decoded_signature; |
+ if (!base::Base64Decode(tree_head_signature, &decoded_signature)) { |
+ LOG(WARNING) << "Failed decoding tree_head_signature"; |
+ return false; |
+ } |
+ |
+ base::StringPiece sp(decoded_signature); |
+ if (!DecodeDigitallySigned(&sp, &(sth->signature))) { |
+ LOG(WARNING) << "Failed decoding signature to DigitallySigned"; |
+ return false; |
+ } |
+ |
+ sth->version = net::ct::SignedTreeHead::V1; |
+ sth->tree_size = tree_size; |
+ sth->timestamp = |
+ base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(timestamp); |
+ memcpy(sth->sha256_root_hash, decoded_root_hash.c_str(), |
+ net::ct::kSthRootHashLength); |
+ return true; |
+} |
+ |
+} // namespace certificate_transparency |