OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cert/ct_log_verifier.h" | 5 #include "net/cert/ct_log_verifier.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <openssl/bytestring.h> | 8 #include <openssl/bytestring.h> |
9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
10 | 10 |
| 11 #include <vector> |
| 12 |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
13 #include "crypto/scoped_openssl_types.h" | 15 #include "crypto/scoped_openssl_types.h" |
14 #include "crypto/sha2.h" | 16 #include "crypto/sha2.h" |
15 #include "net/cert/ct_log_verifier_util.h" | 17 #include "net/cert/ct_log_verifier_util.h" |
16 #include "net/cert/ct_serialization.h" | 18 #include "net/cert/ct_serialization.h" |
| 19 #include "net/cert/merkle_audit_proof.h" |
17 #include "net/cert/merkle_consistency_proof.h" | 20 #include "net/cert/merkle_consistency_proof.h" |
18 #include "net/cert/signed_tree_head.h" | 21 #include "net/cert/signed_tree_head.h" |
19 | 22 |
20 namespace net { | 23 namespace net { |
21 | 24 |
22 namespace { | 25 namespace { |
23 | 26 |
24 // The SHA-256 hash of the empty string. | 27 // The SHA-256 hash of the empty string. |
25 const unsigned char kSHA256EmptyStringHash[ct::kSthRootHashLength] = { | 28 const unsigned char kSHA256EmptyStringHash[ct::kSthRootHashLength] = { |
26 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, | 29 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 sn >>= 1; | 239 sn >>= 1; |
237 } | 240 } |
238 | 241 |
239 // 6. After completing iterating through the "consistency_path" array as | 242 // 6. After completing iterating through the "consistency_path" array as |
240 // described above, verify that the "fr" calculated is equal to the | 243 // described above, verify that the "fr" calculated is equal to the |
241 // "first_hash" supplied, that the "sr" calculated is equal to the | 244 // "first_hash" supplied, that the "sr" calculated is equal to the |
242 // "second_hash" supplied and that "sn" is 0. | 245 // "second_hash" supplied and that "sn" is 0. |
243 return fr == old_tree_hash && sr == new_tree_hash && sn == 0; | 246 return fr == old_tree_hash && sr == new_tree_hash && sn == 0; |
244 } | 247 } |
245 | 248 |
| 249 bool CTLogVerifier::VerifyAuditProof(const ct::MerkleAuditProof& proof, |
| 250 const std::string& root_hash, |
| 251 const std::string& leaf_hash) const { |
| 252 // Implements the algorithm described in |
| 253 // https://tools.ietf.org/html/draft-ietf-trans-rfc6962-bis-19#section-10.4.1 |
| 254 // |
| 255 // It maintains a hash |r|, initialized to |leaf_hash|, and hashes nodes from |
| 256 // |proof| into it. The proof is then valid if |r| is |root_hash|, proving |
| 257 // that |root_hash| includes |leaf_hash|. |
| 258 |
| 259 // 1. Compare "leaf_index" against "tree_size". If "leaf_index" is |
| 260 // greater than or equal to "tree_size" fail the proof verification. |
| 261 if (proof.leaf_index >= proof.tree_size) |
| 262 return false; |
| 263 |
| 264 // 2. Set "fn" to "leaf_index" and "sn" to "tree_size - 1". |
| 265 uint64_t fn = proof.leaf_index; |
| 266 uint64_t sn = proof.tree_size - 1; |
| 267 // 3. Set "r" to "hash". |
| 268 std::string r = leaf_hash; |
| 269 |
| 270 // 4. For each value "p" in the "inclusion_path" array: |
| 271 for (const std::string& p : proof.nodes) { |
| 272 // If "sn" is 0, stop the iteration and fail the proof verification. |
| 273 if (sn == 0) |
| 274 return false; |
| 275 |
| 276 // If "LSB(fn)" is set, or if "fn" is equal to "sn", then: |
| 277 if ((fn & 1) || fn == sn) { |
| 278 // 1. Set "r" to "HASH(0x01 || p || r)" |
| 279 r = ct::internal::HashNodes(p, r); |
| 280 |
| 281 // 2. If "LSB(fn)" is not set, then right-shift both "fn" and "sn" |
| 282 // equally until either "LSB(fn)" is set or "fn" is "0". |
| 283 while (!(fn & 1) && fn != 0) { |
| 284 fn >>= 1; |
| 285 sn >>= 1; |
| 286 } |
| 287 } else { // Otherwise: |
| 288 // Set "r" to "HASH(0x01 || r || p)" |
| 289 r = ct::internal::HashNodes(r, p); |
| 290 } |
| 291 |
| 292 // Finally, right-shift both "fn" and "sn" one time. |
| 293 fn >>= 1; |
| 294 sn >>= 1; |
| 295 } |
| 296 |
| 297 // 5. Compare "sn" to 0. Compare "r" against the "root_hash". If "sn" |
| 298 // is equal to 0, and "r" and the "root_hash" are equal, then the |
| 299 // log has proven the inclusion of "hash". Otherwise, fail the |
| 300 // proof verification. |
| 301 return sn == 0 && r == root_hash; |
| 302 } |
| 303 |
246 CTLogVerifier::~CTLogVerifier() { | 304 CTLogVerifier::~CTLogVerifier() { |
247 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 305 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
248 | 306 |
249 if (public_key_) | 307 if (public_key_) |
250 EVP_PKEY_free(public_key_); | 308 EVP_PKEY_free(public_key_); |
251 } | 309 } |
252 | 310 |
253 bool CTLogVerifier::Init(const base::StringPiece& public_key) { | 311 bool CTLogVerifier::Init(const base::StringPiece& public_key) { |
254 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 312 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
255 | 313 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 data_to_sign.size()) && | 364 data_to_sign.size()) && |
307 1 == EVP_DigestVerifyFinal( | 365 1 == EVP_DigestVerifyFinal( |
308 &ctx, reinterpret_cast<const uint8_t*>(signature.data()), | 366 &ctx, reinterpret_cast<const uint8_t*>(signature.data()), |
309 signature.size())); | 367 signature.size())); |
310 | 368 |
311 EVP_MD_CTX_cleanup(&ctx); | 369 EVP_MD_CTX_cleanup(&ctx); |
312 return ok; | 370 return ok; |
313 } | 371 } |
314 | 372 |
315 } // namespace net | 373 } // namespace net |
OLD | NEW |