| 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" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 70 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
| 71 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | 71 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); |
| 72 if (nonce_prefix.size() != nonce_prefix_size_) { | 72 if (nonce_prefix.size() != nonce_prefix_size_) { |
| 73 return false; | 73 return false; |
| 74 } | 74 } |
| 75 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | 75 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool AeadBaseDecrypter::Decrypt(StringPiece nonce, | 79 bool AeadBaseDecrypter::Decrypt(StringPiece nonce, |
| 80 StringPiece associated_data, | 80 const StringPiece& associated_data, |
| 81 StringPiece ciphertext, | 81 const StringPiece& ciphertext, |
| 82 uint8* output, | 82 uint8* output, |
| 83 size_t* output_length) { | 83 size_t* output_length, |
| 84 size_t max_output_length) { |
| 84 if (ciphertext.length() < auth_tag_size_ || | 85 if (ciphertext.length() < auth_tag_size_ || |
| 85 nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { | 86 nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { |
| 86 return false; | 87 return false; |
| 87 } | 88 } |
| 88 | 89 |
| 89 if (!EVP_AEAD_CTX_open( | 90 if (!EVP_AEAD_CTX_open( |
| 90 ctx_.get(), output, output_length, ciphertext.size(), | 91 ctx_.get(), output, output_length, max_output_length, |
| 91 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), | 92 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
| 92 reinterpret_cast<const uint8_t*>(ciphertext.data()), ciphertext.size(), | 93 reinterpret_cast<const uint8_t*>(ciphertext.data()), |
| 93 reinterpret_cast<const uint8_t*>(associated_data.data()), | 94 ciphertext.size(), |
| 94 associated_data.size())) { | 95 reinterpret_cast<const uint8_t*>(associated_data.data()), |
| 96 associated_data.size())) { |
| 95 // Because QuicFramer does trial decryption, decryption errors are expected | 97 // Because QuicFramer does trial decryption, decryption errors are expected |
| 96 // when encryption level changes. So we don't log decryption errors. | 98 // when encryption level changes. So we don't log decryption errors. |
| 97 ClearOpenSslErrors(); | 99 ClearOpenSslErrors(); |
| 98 return false; | 100 return false; |
| 99 } | 101 } |
| 100 | |
| 101 return true; | 102 return true; |
| 102 } | 103 } |
| 103 | 104 |
| 104 QuicData* AeadBaseDecrypter::DecryptPacket( | 105 bool AeadBaseDecrypter::DecryptPacket(QuicPacketSequenceNumber sequence_number, |
| 105 QuicPacketSequenceNumber sequence_number, | 106 const StringPiece& associated_data, |
| 106 StringPiece associated_data, | 107 const StringPiece& ciphertext, |
| 107 StringPiece ciphertext) { | 108 char* output, |
| 109 size_t* output_length, |
| 110 size_t max_output_length) { |
| 108 if (ciphertext.length() < auth_tag_size_) { | 111 if (ciphertext.length() < auth_tag_size_) { |
| 109 return nullptr; | 112 return false; |
| 110 } | 113 } |
| 111 size_t plaintext_size = ciphertext.length(); | |
| 112 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | |
| 113 | 114 |
| 114 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | 115 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; |
| 115 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 116 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
| 116 DCHECK_LE(nonce_size, sizeof(nonce)); | 117 DCHECK_LE(nonce_size, sizeof(nonce)); |
| 117 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 118 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); |
| 118 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 119 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); |
| 119 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 120 return Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), |
| 120 associated_data, ciphertext, | 121 associated_data, ciphertext, reinterpret_cast<uint8*>(output), |
| 121 reinterpret_cast<uint8*>(plaintext.get()), | 122 output_length, max_output_length); |
| 122 &plaintext_size)) { | |
| 123 return nullptr; | |
| 124 } | |
| 125 return new QuicData(plaintext.release(), plaintext_size, true); | |
| 126 } | 123 } |
| 127 | 124 |
| 128 StringPiece AeadBaseDecrypter::GetKey() const { | 125 StringPiece AeadBaseDecrypter::GetKey() const { |
| 129 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 126 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); |
| 130 } | 127 } |
| 131 | 128 |
| 132 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | 129 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { |
| 133 if (nonce_prefix_size_ == 0) { | 130 if (nonce_prefix_size_ == 0) { |
| 134 return StringPiece(); | 131 return StringPiece(); |
| 135 } | 132 } |
| 136 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 133 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 137 nonce_prefix_size_); | 134 nonce_prefix_size_); |
| 138 } | 135 } |
| 139 | 136 |
| 140 } // namespace net | 137 } // namespace net |
| OLD | NEW |