Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/certificate_reporting/error_reporter.h" | 5 #include "components/certificate_reporting/error_reporter.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" | 10 #include "components/certificate_reporting/encrypted_cert_logger.pb.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 static const uint8 kServerPublicKey[] = { | 27 static const uint8 kServerPublicKey[] = { |
| 28 0x51, 0xcc, 0x52, 0x67, 0x42, 0x47, 0x3b, 0x10, 0xe8, 0x63, 0x18, | 28 0x51, 0xcc, 0x52, 0x67, 0x42, 0x47, 0x3b, 0x10, 0xe8, 0x63, 0x18, |
| 29 0x3c, 0x61, 0xa7, 0x96, 0x76, 0x86, 0x91, 0x40, 0x71, 0x39, 0x5f, | 29 0x3c, 0x61, 0xa7, 0x96, 0x76, 0x86, 0x91, 0x40, 0x71, 0x39, 0x5f, |
| 30 0x31, 0x1a, 0x39, 0x5b, 0x76, 0xb1, 0x6b, 0x3d, 0x6a, 0x2b}; | 30 0x31, 0x1a, 0x39, 0x5b, 0x76, 0xb1, 0x6b, 0x3d, 0x6a, 0x2b}; |
| 31 static const uint32 kServerPublicKeyVersion = 1; | 31 static const uint32 kServerPublicKeyVersion = 1; |
| 32 | 32 |
| 33 #if defined(USE_OPENSSL) | 33 #if defined(USE_OPENSSL) |
| 34 | 34 |
| 35 static const char kHkdfLabel[] = "certificate report"; | 35 static const char kHkdfLabel[] = "certificate report"; |
| 36 | 36 |
| 37 std::string GetHkdfSubkeySecret(size_t subkey_length, | 37 bool GetHkdfSubkeySecret(size_t subkey_length, |
| 38 const uint8* private_key, | 38 const uint8* private_key, |
| 39 const uint8* public_key) { | 39 const uint8* public_key, |
| 40 std::string* secret) { | |
| 40 uint8 shared_secret[crypto::curve25519::kBytes]; | 41 uint8 shared_secret[crypto::curve25519::kBytes]; |
| 41 crypto::curve25519::ScalarMult(private_key, public_key, shared_secret); | 42 if (!crypto::curve25519::ScalarMult(private_key, public_key, shared_secret)) |
| 43 return false; | |
| 42 | 44 |
| 43 // By mistake, the HKDF label here ends up with an extra null byte on | 45 // By mistake, the HKDF label here ends up with an extra null byte on |
| 44 // the end, due to using sizeof(kHkdfLabel) in the StringPiece | 46 // the end, due to using sizeof(kHkdfLabel) in the StringPiece |
| 45 // constructor instead of strlen(kHkdfLabel). Ideally this code should | 47 // constructor instead of strlen(kHkdfLabel). Ideally this code should |
| 46 // be just passing kHkdfLabel directly into the HKDF constructor. | 48 // be just passing kHkdfLabel directly into the HKDF constructor. |
| 47 // | 49 // |
| 48 // TODO(estark): fix this in coordination with the server-side code -- | 50 // TODO(estark): fix this in coordination with the server-side code -- |
| 49 // perhaps by rolling the public key version forward and using the | 51 // perhaps by rolling the public key version forward and using the |
| 50 // version to decide whether to use the extra-null-byte version of the | 52 // version to decide whether to use the extra-null-byte version of the |
| 51 // label. https://crbug.com/517746 | 53 // label. https://crbug.com/517746 |
| 52 crypto::HKDF hkdf(base::StringPiece(reinterpret_cast<char*>(shared_secret), | 54 crypto::HKDF hkdf(base::StringPiece(reinterpret_cast<char*>(shared_secret), |
| 53 sizeof(shared_secret)), | 55 sizeof(shared_secret)), |
| 54 "" /* salt */, | 56 "" /* salt */, |
| 55 base::StringPiece(kHkdfLabel, sizeof(kHkdfLabel)), | 57 base::StringPiece(kHkdfLabel, sizeof(kHkdfLabel)), |
| 56 0 /* key bytes */, 0 /* iv bytes */, subkey_length); | 58 0 /* key bytes */, 0 /* iv bytes */, subkey_length); |
| 57 | 59 |
| 58 return hkdf.subkey_secret().as_string(); | 60 *secret = hkdf.subkey_secret().as_string(); |
| 61 return true; | |
| 59 } | 62 } |
| 60 | 63 |
| 61 bool EncryptSerializedReport(const uint8* server_public_key, | 64 bool EncryptSerializedReport(const uint8* server_public_key, |
| 62 uint32 server_public_key_version, | 65 uint32 server_public_key_version, |
| 63 const std::string& report, | 66 const std::string& report, |
| 64 EncryptedCertLoggerRequest* encrypted_report) { | 67 EncryptedCertLoggerRequest* encrypted_report) { |
| 65 // Generate an ephemeral key pair to generate a shared secret. | 68 // Generate an ephemeral key pair to generate a shared secret. |
| 66 uint8 public_key[crypto::curve25519::kBytes]; | 69 uint8 public_key[crypto::curve25519::kBytes]; |
| 67 uint8 private_key[crypto::curve25519::kScalarBytes]; | 70 uint8 private_key[crypto::curve25519::kScalarBytes]; |
| 68 | 71 |
| 69 crypto::RandBytes(private_key, sizeof(private_key)); | 72 crypto::RandBytes(private_key, sizeof(private_key)); |
| 70 crypto::curve25519::ScalarBaseMult(private_key, public_key); | 73 crypto::curve25519::ScalarBaseMult(private_key, public_key); |
| 71 | 74 |
| 72 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); | 75 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); |
| 73 const std::string key = | 76 const std::string key; |
| 74 GetHkdfSubkeySecret(aead.KeyLength(), private_key, | 77 if (!GetHkdfSubkeySecret(aead.KeyLength(), private_key, |
| 75 reinterpret_cast<const uint8*>(server_public_key)); | 78 reinterpret_cast<const uint8*>(server_public_key), |
| 79 &key)) { | |
| 80 return false; | |
|
estark
2015/11/19 00:25:18
nit: add a LOG(ERROR) here?
davidben
2015/11/19 00:41:36
Done.
| |
| 81 } | |
| 76 aead.Init(&key); | 82 aead.Init(&key); |
| 77 | 83 |
| 78 // Use an all-zero nonce because the key is random per-message. | 84 // Use an all-zero nonce because the key is random per-message. |
| 79 std::string nonce(aead.NonceLength(), '\0'); | 85 std::string nonce(aead.NonceLength(), '\0'); |
| 80 | 86 |
| 81 std::string ciphertext; | 87 std::string ciphertext; |
| 82 if (!aead.Seal(report, nonce, std::string(), &ciphertext)) { | 88 if (!aead.Seal(report, nonce, std::string(), &ciphertext)) { |
| 83 LOG(ERROR) << "Error sealing certificate report."; | 89 LOG(ERROR) << "Error sealing certificate report."; |
| 84 return false; | 90 return false; |
| 85 } | 91 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 #endif | 156 #endif |
| 151 } | 157 } |
| 152 | 158 |
| 153 // Used only by tests. | 159 // Used only by tests. |
| 154 #if defined(USE_OPENSSL) | 160 #if defined(USE_OPENSSL) |
| 155 bool ErrorReporter::DecryptErrorReport( | 161 bool ErrorReporter::DecryptErrorReport( |
| 156 const uint8 server_private_key[32], | 162 const uint8 server_private_key[32], |
| 157 const EncryptedCertLoggerRequest& encrypted_report, | 163 const EncryptedCertLoggerRequest& encrypted_report, |
| 158 std::string* decrypted_serialized_report) { | 164 std::string* decrypted_serialized_report) { |
| 159 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); | 165 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); |
| 160 const std::string key = | 166 const std::string key; |
| 161 GetHkdfSubkeySecret(aead.KeyLength(), server_private_key, | 167 if (!GetHkdfSubkeySecret(aead.KeyLength(), server_private_key, |
| 162 reinterpret_cast<const uint8*>( | 168 reinterpret_cast<const uint8*>( |
| 163 encrypted_report.client_public_key().data())); | 169 encrypted_report.client_public_key().data()), |
| 170 &key)) { | |
| 171 return false; | |
|
estark
2015/11/19 00:25:18
same nit here
davidben
2015/11/19 00:41:37
Done.
| |
| 172 } | |
| 164 aead.Init(&key); | 173 aead.Init(&key); |
| 165 | 174 |
| 166 // Use an all-zero nonce because the key is random per-message. | 175 // Use an all-zero nonce because the key is random per-message. |
| 167 std::string nonce(aead.NonceLength(), 0); | 176 std::string nonce(aead.NonceLength(), 0); |
| 168 | 177 |
| 169 return aead.Open(encrypted_report.encrypted_report(), nonce, std::string(), | 178 return aead.Open(encrypted_report.encrypted_report(), nonce, std::string(), |
| 170 decrypted_serialized_report); | 179 decrypted_serialized_report); |
| 171 } | 180 } |
| 172 #endif | 181 #endif |
| 173 | 182 |
| 174 } // namespace certificate_reporting | 183 } // namespace certificate_reporting |
| OLD | NEW |