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 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 LOG(ERROR) << "Error getting subkey secret."; |
| 81 return false; |
| 82 } |
76 aead.Init(&key); | 83 aead.Init(&key); |
77 | 84 |
78 // Use an all-zero nonce because the key is random per-message. | 85 // Use an all-zero nonce because the key is random per-message. |
79 std::string nonce(aead.NonceLength(), '\0'); | 86 std::string nonce(aead.NonceLength(), '\0'); |
80 | 87 |
81 std::string ciphertext; | 88 std::string ciphertext; |
82 if (!aead.Seal(report, nonce, std::string(), &ciphertext)) { | 89 if (!aead.Seal(report, nonce, std::string(), &ciphertext)) { |
83 LOG(ERROR) << "Error sealing certificate report."; | 90 LOG(ERROR) << "Error sealing certificate report."; |
84 return false; | 91 return false; |
85 } | 92 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 #endif | 157 #endif |
151 } | 158 } |
152 | 159 |
153 // Used only by tests. | 160 // Used only by tests. |
154 #if defined(USE_OPENSSL) | 161 #if defined(USE_OPENSSL) |
155 bool ErrorReporter::DecryptErrorReport( | 162 bool ErrorReporter::DecryptErrorReport( |
156 const uint8 server_private_key[32], | 163 const uint8 server_private_key[32], |
157 const EncryptedCertLoggerRequest& encrypted_report, | 164 const EncryptedCertLoggerRequest& encrypted_report, |
158 std::string* decrypted_serialized_report) { | 165 std::string* decrypted_serialized_report) { |
159 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); | 166 crypto::Aead aead(crypto::Aead::AES_128_CTR_HMAC_SHA256); |
160 const std::string key = | 167 std::string key; |
161 GetHkdfSubkeySecret(aead.KeyLength(), server_private_key, | 168 if (!GetHkdfSubkeySecret(aead.KeyLength(), server_private_key, |
162 reinterpret_cast<const uint8*>( | 169 reinterpret_cast<const uint8*>( |
163 encrypted_report.client_public_key().data())); | 170 encrypted_report.client_public_key().data()), |
| 171 &key)) { |
| 172 LOG(ERROR) << "Error getting subkey secret."; |
| 173 return false; |
| 174 } |
164 aead.Init(&key); | 175 aead.Init(&key); |
165 | 176 |
166 // Use an all-zero nonce because the key is random per-message. | 177 // Use an all-zero nonce because the key is random per-message. |
167 std::string nonce(aead.NonceLength(), 0); | 178 std::string nonce(aead.NonceLength(), 0); |
168 | 179 |
169 return aead.Open(encrypted_report.encrypted_report(), nonce, std::string(), | 180 return aead.Open(encrypted_report.encrypted_report(), nonce, std::string(), |
170 decrypted_serialized_report); | 181 decrypted_serialized_report); |
171 } | 182 } |
172 #endif | 183 #endif |
173 | 184 |
174 } // namespace certificate_reporting | 185 } // namespace certificate_reporting |
OLD | NEW |