| 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/crypto/aead_base_decrypter.h" | 5 #include "net/quic/crypto/aead_base_decrypter.h" |
| 6 | 6 |
| 7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 | 11 |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Clear OpenSSL error stack. | 18 // Clear OpenSSL error stack. |
| 19 void ClearOpenSslErrors() { | 19 void ClearOpenSslErrors() { |
| 20 while (ERR_get_error()) {} | 20 while (ERR_get_error()) { |
| 21 } |
| 21 } | 22 } |
| 22 | 23 |
| 23 // In debug builds only, log OpenSSL error stack. Then clear OpenSSL error | 24 // In debug builds only, log OpenSSL error stack. Then clear OpenSSL error |
| 24 // stack. | 25 // stack. |
| 25 void DLogOpenSslErrors() { | 26 void DLogOpenSslErrors() { |
| 26 #ifdef NDEBUG | 27 #ifdef NDEBUG |
| 27 ClearOpenSslErrors(); | 28 ClearOpenSslErrors(); |
| 28 #else | 29 #else |
| 29 while (unsigned long error = ERR_get_error()) { | 30 while (unsigned long error = ERR_get_error()) { |
| 30 char buf[120]; | 31 char buf[120]; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 size_t auth_tag_size, | 42 size_t auth_tag_size, |
| 42 size_t nonce_prefix_size) | 43 size_t nonce_prefix_size) |
| 43 : aead_alg_(aead_alg), | 44 : aead_alg_(aead_alg), |
| 44 key_size_(key_size), | 45 key_size_(key_size), |
| 45 auth_tag_size_(auth_tag_size), | 46 auth_tag_size_(auth_tag_size), |
| 46 nonce_prefix_size_(nonce_prefix_size) { | 47 nonce_prefix_size_(nonce_prefix_size) { |
| 47 DCHECK_LE(key_size_, sizeof(key_)); | 48 DCHECK_LE(key_size_, sizeof(key_)); |
| 48 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_)); | 49 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 AeadBaseDecrypter::~AeadBaseDecrypter() {} | 52 AeadBaseDecrypter::~AeadBaseDecrypter() { |
| 53 } |
| 52 | 54 |
| 53 bool AeadBaseDecrypter::SetKey(StringPiece key) { | 55 bool AeadBaseDecrypter::SetKey(StringPiece key) { |
| 54 DCHECK_EQ(key.size(), key_size_); | 56 DCHECK_EQ(key.size(), key_size_); |
| 55 if (key.size() != key_size_) { | 57 if (key.size() != key_size_) { |
| 56 return false; | 58 return false; |
| 57 } | 59 } |
| 58 memcpy(key_, key.data(), key.size()); | 60 memcpy(key_, key.data(), key.size()); |
| 59 | 61 |
| 60 EVP_AEAD_CTX_cleanup(ctx_.get()); | 62 EVP_AEAD_CTX_cleanup(ctx_.get()); |
| 61 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, | 63 if (!EVP_AEAD_CTX_init( |
| 62 auth_tag_size_, NULL)) { | 64 ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, NULL)) { |
| 63 DLogOpenSslErrors(); | 65 DLogOpenSslErrors(); |
| 64 return false; | 66 return false; |
| 65 } | 67 } |
| 66 | 68 |
| 67 return true; | 69 return true; |
| 68 } | 70 } |
| 69 | 71 |
| 70 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 72 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
| 71 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | 73 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); |
| 72 if (nonce_prefix.size() != nonce_prefix_size_) { | 74 if (nonce_prefix.size() != nonce_prefix_size_) { |
| 73 return false; | 75 return false; |
| 74 } | 76 } |
| 75 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | 77 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); |
| 76 return true; | 78 return true; |
| 77 } | 79 } |
| 78 | 80 |
| 79 bool AeadBaseDecrypter::Decrypt(StringPiece nonce, | 81 bool AeadBaseDecrypter::Decrypt(StringPiece nonce, |
| 80 StringPiece associated_data, | 82 StringPiece associated_data, |
| 81 StringPiece ciphertext, | 83 StringPiece ciphertext, |
| 82 uint8* output, | 84 uint8* output, |
| 83 size_t* output_length) { | 85 size_t* output_length) { |
| 84 if (ciphertext.length() < auth_tag_size_ || | 86 if (ciphertext.length() < auth_tag_size_ || |
| 85 nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { | 87 nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { |
| 86 return false; | 88 return false; |
| 87 } | 89 } |
| 88 | 90 |
| 89 ssize_t len = EVP_AEAD_CTX_open( | 91 ssize_t len = EVP_AEAD_CTX_open( |
| 90 ctx_.get(), output, ciphertext.size(), | 92 ctx_.get(), |
| 91 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), | 93 output, |
| 92 reinterpret_cast<const uint8_t*>(ciphertext.data()), ciphertext.size(), | 94 ciphertext.size(), |
| 95 reinterpret_cast<const uint8_t*>(nonce.data()), |
| 96 nonce.size(), |
| 97 reinterpret_cast<const uint8_t*>(ciphertext.data()), |
| 98 ciphertext.size(), |
| 93 reinterpret_cast<const uint8_t*>(associated_data.data()), | 99 reinterpret_cast<const uint8_t*>(associated_data.data()), |
| 94 associated_data.size()); | 100 associated_data.size()); |
| 95 | 101 |
| 96 if (len < 0) { | 102 if (len < 0) { |
| 97 // Because QuicFramer does trial decryption, decryption errors are expected | 103 // Because QuicFramer does trial decryption, decryption errors are expected |
| 98 // when encryption level changes. So we don't log decryption errors. | 104 // when encryption level changes. So we don't log decryption errors. |
| 99 ClearOpenSslErrors(); | 105 ClearOpenSslErrors(); |
| 100 return false; | 106 return false; |
| 101 } | 107 } |
| 102 | 108 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 } | 119 } |
| 114 size_t plaintext_size = ciphertext.length(); | 120 size_t plaintext_size = ciphertext.length(); |
| 115 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 121 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
| 116 | 122 |
| 117 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | 123 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; |
| 118 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 124 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
| 119 DCHECK_LE(nonce_size, sizeof(nonce)); | 125 DCHECK_LE(nonce_size, sizeof(nonce)); |
| 120 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 126 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); |
| 121 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 127 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); |
| 122 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 128 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), |
| 123 associated_data, ciphertext, | 129 associated_data, |
| 130 ciphertext, |
| 124 reinterpret_cast<uint8*>(plaintext.get()), | 131 reinterpret_cast<uint8*>(plaintext.get()), |
| 125 &plaintext_size)) { | 132 &plaintext_size)) { |
| 126 return NULL; | 133 return NULL; |
| 127 } | 134 } |
| 128 return new QuicData(plaintext.release(), plaintext_size, true); | 135 return new QuicData(plaintext.release(), plaintext_size, true); |
| 129 } | 136 } |
| 130 | 137 |
| 131 StringPiece AeadBaseDecrypter::GetKey() const { | 138 StringPiece AeadBaseDecrypter::GetKey() const { |
| 132 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 139 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); |
| 133 } | 140 } |
| 134 | 141 |
| 135 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | 142 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { |
| 136 if (nonce_prefix_size_ == 0) { | 143 if (nonce_prefix_size_ == 0) { |
| 137 return StringPiece(); | 144 return StringPiece(); |
| 138 } | 145 } |
| 139 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 146 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 140 nonce_prefix_size_); | 147 nonce_prefix_size_); |
| 141 } | 148 } |
| 142 | 149 |
| 143 } // namespace net | 150 } // namespace net |
| OLD | NEW |