OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/quic/core/crypto/aead_base_decrypter.h" | 5 #include "net/quic/core/crypto/aead_base_decrypter.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 | 8 |
9 #include "net/quic/core/quic_utils.h" | 9 #include "net/quic/core/quic_utils.h" |
10 #include "net/quic/platform/api/quic_bug_tracker.h" | 10 #include "net/quic/platform/api/quic_bug_tracker.h" |
11 #include "net/quic/platform/api/quic_logging.h" | 11 #include "net/quic/platform/api/quic_logging.h" |
12 #include "third_party/boringssl/src/include/openssl/err.h" | 12 #include "third_party/boringssl/src/include/openssl/err.h" |
13 #include "third_party/boringssl/src/include/openssl/evp.h" | 13 #include "third_party/boringssl/src/include/openssl/evp.h" |
14 | 14 |
15 using base::StringPiece; | |
16 using std::string; | 15 using std::string; |
17 | 16 |
18 namespace net { | 17 namespace net { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 // Clear OpenSSL error stack. | 21 // Clear OpenSSL error stack. |
23 void ClearOpenSslErrors() { | 22 void ClearOpenSslErrors() { |
24 while (ERR_get_error()) { | 23 while (ERR_get_error()) { |
25 } | 24 } |
(...skipping 26 matching lines...) Expand all Loading... |
52 have_preliminary_key_(false) { | 51 have_preliminary_key_(false) { |
53 DCHECK_GT(256u, key_size); | 52 DCHECK_GT(256u, key_size); |
54 DCHECK_GT(256u, auth_tag_size); | 53 DCHECK_GT(256u, auth_tag_size); |
55 DCHECK_GT(256u, nonce_prefix_size); | 54 DCHECK_GT(256u, nonce_prefix_size); |
56 DCHECK_LE(key_size_, sizeof(key_)); | 55 DCHECK_LE(key_size_, sizeof(key_)); |
57 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_)); | 56 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_)); |
58 } | 57 } |
59 | 58 |
60 AeadBaseDecrypter::~AeadBaseDecrypter() {} | 59 AeadBaseDecrypter::~AeadBaseDecrypter() {} |
61 | 60 |
62 bool AeadBaseDecrypter::SetKey(StringPiece key) { | 61 bool AeadBaseDecrypter::SetKey(QuicStringPiece key) { |
63 DCHECK_EQ(key.size(), key_size_); | 62 DCHECK_EQ(key.size(), key_size_); |
64 if (key.size() != key_size_) { | 63 if (key.size() != key_size_) { |
65 return false; | 64 return false; |
66 } | 65 } |
67 memcpy(key_, key.data(), key.size()); | 66 memcpy(key_, key.data(), key.size()); |
68 | 67 |
69 EVP_AEAD_CTX_cleanup(ctx_.get()); | 68 EVP_AEAD_CTX_cleanup(ctx_.get()); |
70 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, | 69 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, |
71 nullptr)) { | 70 nullptr)) { |
72 DLogOpenSslErrors(); | 71 DLogOpenSslErrors(); |
73 return false; | 72 return false; |
74 } | 73 } |
75 | 74 |
76 return true; | 75 return true; |
77 } | 76 } |
78 | 77 |
79 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 78 bool AeadBaseDecrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { |
80 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | 79 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); |
81 if (nonce_prefix.size() != nonce_prefix_size_) { | 80 if (nonce_prefix.size() != nonce_prefix_size_) { |
82 return false; | 81 return false; |
83 } | 82 } |
84 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | 83 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); |
85 return true; | 84 return true; |
86 } | 85 } |
87 | 86 |
88 bool AeadBaseDecrypter::SetPreliminaryKey(StringPiece key) { | 87 bool AeadBaseDecrypter::SetPreliminaryKey(QuicStringPiece key) { |
89 DCHECK(!have_preliminary_key_); | 88 DCHECK(!have_preliminary_key_); |
90 SetKey(key); | 89 SetKey(key); |
91 have_preliminary_key_ = true; | 90 have_preliminary_key_ = true; |
92 | 91 |
93 return true; | 92 return true; |
94 } | 93 } |
95 | 94 |
96 bool AeadBaseDecrypter::SetDiversificationNonce( | 95 bool AeadBaseDecrypter::SetDiversificationNonce( |
97 const DiversificationNonce& nonce) { | 96 const DiversificationNonce& nonce) { |
98 if (!have_preliminary_key_) { | 97 if (!have_preliminary_key_) { |
99 return true; | 98 return true; |
100 } | 99 } |
101 | 100 |
102 string key, nonce_prefix; | 101 string key, nonce_prefix; |
103 DiversifyPreliminaryKey( | 102 DiversifyPreliminaryKey( |
104 StringPiece(reinterpret_cast<const char*>(key_), key_size_), | 103 QuicStringPiece(reinterpret_cast<const char*>(key_), key_size_), |
105 StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 104 QuicStringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
106 nonce_prefix_size_), | 105 nonce_prefix_size_), |
107 nonce, key_size_, nonce_prefix_size_, &key, &nonce_prefix); | 106 nonce, key_size_, nonce_prefix_size_, &key, &nonce_prefix); |
108 | 107 |
109 if (!SetKey(key) || !SetNoncePrefix(nonce_prefix)) { | 108 if (!SetKey(key) || !SetNoncePrefix(nonce_prefix)) { |
110 DCHECK(false); | 109 DCHECK(false); |
111 return false; | 110 return false; |
112 } | 111 } |
113 | 112 |
114 have_preliminary_key_ = false; | 113 have_preliminary_key_ = false; |
115 return true; | 114 return true; |
116 } | 115 } |
117 | 116 |
118 bool AeadBaseDecrypter::DecryptPacket(QuicVersion /*version*/, | 117 bool AeadBaseDecrypter::DecryptPacket(QuicVersion /*version*/, |
119 QuicPacketNumber packet_number, | 118 QuicPacketNumber packet_number, |
120 StringPiece associated_data, | 119 QuicStringPiece associated_data, |
121 StringPiece ciphertext, | 120 QuicStringPiece ciphertext, |
122 char* output, | 121 char* output, |
123 size_t* output_length, | 122 size_t* output_length, |
124 size_t max_output_length) { | 123 size_t max_output_length) { |
125 if (ciphertext.length() < auth_tag_size_) { | 124 if (ciphertext.length() < auth_tag_size_) { |
126 return false; | 125 return false; |
127 } | 126 } |
128 | 127 |
129 if (have_preliminary_key_) { | 128 if (have_preliminary_key_) { |
130 QUIC_BUG << "Unable to decrypt while key diversification is pending"; | 129 QUIC_BUG << "Unable to decrypt while key diversification is pending"; |
131 return false; | 130 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
143 reinterpret_cast<const uint8_t*>(associated_data.data()), | 142 reinterpret_cast<const uint8_t*>(associated_data.data()), |
144 associated_data.size())) { | 143 associated_data.size())) { |
145 // Because QuicFramer does trial decryption, decryption errors are expected | 144 // Because QuicFramer does trial decryption, decryption errors are expected |
146 // when encryption level changes. So we don't log decryption errors. | 145 // when encryption level changes. So we don't log decryption errors. |
147 ClearOpenSslErrors(); | 146 ClearOpenSslErrors(); |
148 return false; | 147 return false; |
149 } | 148 } |
150 return true; | 149 return true; |
151 } | 150 } |
152 | 151 |
153 StringPiece AeadBaseDecrypter::GetKey() const { | 152 QuicStringPiece AeadBaseDecrypter::GetKey() const { |
154 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 153 return QuicStringPiece(reinterpret_cast<const char*>(key_), key_size_); |
155 } | 154 } |
156 | 155 |
157 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | 156 QuicStringPiece AeadBaseDecrypter::GetNoncePrefix() const { |
158 if (nonce_prefix_size_ == 0) { | 157 if (nonce_prefix_size_ == 0) { |
159 return StringPiece(); | 158 return QuicStringPiece(); |
160 } | 159 } |
161 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 160 return QuicStringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
162 nonce_prefix_size_); | 161 nonce_prefix_size_); |
163 } | 162 } |
164 | 163 |
165 } // namespace net | 164 } // namespace net |
OLD | NEW |