Chromium Code Reviews| Index: net/cert/ct_log_response_parser.cc |
| diff --git a/net/cert/ct_log_response_parser.cc b/net/cert/ct_log_response_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0836c757f16d436fe9892d96ac584100efac3cff |
| --- /dev/null |
| +++ b/net/cert/ct_log_response_parser.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2014 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/ct_log_response_parser.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_value_converter.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 net { |
| + |
| +namespace ct { |
| + |
| +namespace { |
| + |
| +// Structure for making JSON decoding easier. The string fields |
| +// are base64-encoded so will require further decoding. |
| +struct JsonSignedTreeHead { |
| + int tree_size; |
| + double timestamp; |
| + std::string sha256_root_hash; |
| + std::string tree_head_signature; |
| + |
| + static void RegisterJSONConverter( |
| + base::JSONValueConverter<JsonSignedTreeHead>* converted); |
| +}; |
| + |
| +void JsonSignedTreeHead::RegisterJSONConverter( |
| + base::JSONValueConverter<JsonSignedTreeHead>* converter) { |
| + converter->RegisterIntField("tree_size", &JsonSignedTreeHead::tree_size); |
| + converter->RegisterDoubleField("timestamp", &JsonSignedTreeHead::timestamp); |
| + converter->RegisterStringField("sha256_root_hash", |
| + &JsonSignedTreeHead::sha256_root_hash); |
| + converter->RegisterStringField("tree_head_signature", |
| + &JsonSignedTreeHead::tree_head_signature); |
|
Ryan Sleevi
2014/04/25 23:33:19
SECURITY: Should there be any customer converters
Eran Messeri
2014/04/29 15:22:24
Good catch - I've added custom converters for the
|
| +} |
| + |
| +} // namespace |
| + |
| +bool FillSignedTreeHead(const base::StringPiece& json_signed_tree_head, |
| + SignedTreeHead* signed_tree_head) { |
| + base::JSONReader json_reader; |
| + scoped_ptr<base::Value> json(json_reader.Read(json_signed_tree_head)); |
| + if (json.get() == NULL) { |
| + DVLOG(1) << "Empty Signed Tree Head JSON."; |
| + return false; |
| + } |
| + |
| + JsonSignedTreeHead parsed_sth; |
| + base::JSONValueConverter<JsonSignedTreeHead> converter; |
| + if (!converter.Convert(*json.get(), &parsed_sth)) { |
| + DVLOG(1) << "Invalid Signed Tree Head JSON."; |
| + return false; |
| + } |
| + |
| + std::string decoded_root_hash; |
| + if (!base::Base64Decode(parsed_sth.sha256_root_hash, &decoded_root_hash)) { |
| + DVLOG(1) << "Failed decoding sha256_root_hash"; |
| + return false; |
| + } |
| + |
| + if (decoded_root_hash.length() != kSthRootHashLength) { |
| + DVLOG(1) << "sha256_root_hash is expected to be 32 bytes, but is " |
| + << decoded_root_hash.length() << " bytes."; |
| + return false; |
| + } |
| + |
| + std::string decoded_signature; |
| + if (!base::Base64Decode(parsed_sth.tree_head_signature, &decoded_signature)) { |
| + DVLOG(1) << "Failed decoding tree_head_signature"; |
| + return false; |
| + } |
| + |
| + // It's OK to decode the signature directly into the output as this is the |
| + // last check before filling in all other fields. |
| + // If decoding of the DigitallySigned part fails, the STH will not contain |
| + // partial data. If it passes, all other fields will be filled as well. |
| + base::StringPiece sp(decoded_signature); |
| + if (!DecodeDigitallySigned(&sp, &(signed_tree_head->signature))) { |
| + DVLOG(1) << "Failed decoding signature to DigitallySigned"; |
| + return false; |
| + } |
| + |
| + signed_tree_head->version = SignedTreeHead::V1; |
| + signed_tree_head->tree_size = parsed_sth.tree_size; |
| + signed_tree_head->timestamp = |
| + base::Time::UnixEpoch() + |
| + base::TimeDelta::FromMilliseconds(parsed_sth.timestamp); |
| + memcpy(signed_tree_head->sha256_root_hash, |
| + decoded_root_hash.c_str(), |
| + kSthRootHashLength); |
| + return true; |
| +} |
| + |
| +} // namespace ct |
| + |
| +} // namespace net |