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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 sn >>= 1; | 234 sn >>= 1; |
232 } | 235 } |
233 | 236 |
234 // 6. After completing iterating through the "consistency_path" array as | 237 // 6. After completing iterating through the "consistency_path" array as |
235 // described above, verify that the "fr" calculated is equal to the | 238 // described above, verify that the "fr" calculated is equal to the |
236 // "first_hash" supplied, that the "sr" calculated is equal to the | 239 // "first_hash" supplied, that the "sr" calculated is equal to the |
237 // "second_hash" supplied and that "sn" is 0. | 240 // "second_hash" supplied and that "sn" is 0. |
238 return fr == old_tree_hash && sr == new_tree_hash && sn == 0; | 241 return fr == old_tree_hash && sr == new_tree_hash && sn == 0; |
239 } | 242 } |
240 | 243 |
| 244 bool CTLogVerifier::VerifyAuditProof(const ct::MerkleAuditProof& proof, |
| 245 const std::string& root_hash, |
| 246 const std::string& leaf_hash) const { |
| 247 // Implements the algorithm described in |
| 248 // https://tools.ietf.org/html/draft-ietf-trans-rfc6962-bis-16#section-10.4.1. |
| 249 |
| 250 // 1. Compare "leaf_index" against "tree_size". If "leaf_index" is |
| 251 // greater than or equal to "tree_size" fail the proof verification. |
| 252 if (proof.leaf_index >= proof.tree_size) |
| 253 return false; |
| 254 |
| 255 // 2. Set "fn" to "leaf_index" and "sn" to "tree_size - 1". |
| 256 uint64_t fn = proof.leaf_index; |
| 257 uint64_t sn = proof.tree_size - 1; |
| 258 // 3. Set "r" to "hash". |
| 259 std::string r = leaf_hash; |
| 260 |
| 261 // 4. For each value "p" in the "inclusion_path" array: |
| 262 for (const std::string& p : proof.nodes) { |
| 263 // If "LSB(fn)" is set, or if "fn" is equal to "sn", then: |
| 264 if ((fn & 1) || fn == sn) { |
| 265 // 1. Set "r" to "HASH(0x01 || p || r)" |
| 266 r = ct::internal::HashNodes(p, r); |
| 267 |
| 268 // 2. If "LSB(fn)" is not set, then right-shift both "fn" and "sn" |
| 269 // equally until either "LSB(fn)" is set or "fn" is "0". |
| 270 while (!(fn & 1) && fn != 0) { |
| 271 fn >>= 1; |
| 272 sn >>= 1; |
| 273 } |
| 274 } else { // Otherwise: |
| 275 // Set "r" to "HASH(0x01 || r || p)" |
| 276 r = ct::internal::HashNodes(r, p); |
| 277 } |
| 278 |
| 279 // Finally, right-shift both "fn" and "sn" one time. |
| 280 fn >>= 1; |
| 281 sn >>= 1; |
| 282 } |
| 283 |
| 284 // 5. Compare "sn" to 0. Compare "r" against the "root_hash". If "sn" |
| 285 // is equal to 0, and "r" and the "root_hash" are equal, then the |
| 286 // log has proven the inclusion of "hash". Otherwise, fail the |
| 287 // proof verification. |
| 288 return sn == 0 && r == root_hash; |
| 289 } |
| 290 |
241 CTLogVerifier::~CTLogVerifier() { | 291 CTLogVerifier::~CTLogVerifier() { |
242 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 292 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
243 | 293 |
244 if (public_key_) | 294 if (public_key_) |
245 EVP_PKEY_free(public_key_); | 295 EVP_PKEY_free(public_key_); |
246 } | 296 } |
247 | 297 |
248 bool CTLogVerifier::Init(const base::StringPiece& public_key) { | 298 bool CTLogVerifier::Init(const base::StringPiece& public_key) { |
249 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 299 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
250 | 300 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 data_to_sign.size()) && | 351 data_to_sign.size()) && |
302 1 == EVP_DigestVerifyFinal( | 352 1 == EVP_DigestVerifyFinal( |
303 &ctx, reinterpret_cast<const uint8_t*>(signature.data()), | 353 &ctx, reinterpret_cast<const uint8_t*>(signature.data()), |
304 signature.size())); | 354 signature.size())); |
305 | 355 |
306 EVP_MD_CTX_cleanup(&ctx); | 356 EVP_MD_CTX_cleanup(&ctx); |
307 return ok; | 357 return ok; |
308 } | 358 } |
309 | 359 |
310 } // namespace net | 360 } // namespace net |
OLD | NEW |