| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <openssl/err.h> | |
| 6 #include <openssl/evp.h> | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "net/quic/crypto/aead_base_decrypter.h" | |
| 11 #include "net/quic/quic_flags.h" | |
| 12 #include "net/quic/quic_utils.h" | |
| 13 | |
| 14 using base::StringPiece; | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Clear OpenSSL error stack. | |
| 21 void ClearOpenSslErrors() { | |
| 22 while (ERR_get_error()) { | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 // In debug builds only, log OpenSSL error stack. Then clear OpenSSL error | |
| 27 // stack. | |
| 28 void DLogOpenSslErrors() { | |
| 29 #ifdef NDEBUG | |
| 30 ClearOpenSslErrors(); | |
| 31 #else | |
| 32 while (uint32_t error = ERR_get_error()) { | |
| 33 char buf[120]; | |
| 34 ERR_error_string_n(error, buf, arraysize(buf)); | |
| 35 DLOG(ERROR) << "OpenSSL error: " << buf; | |
| 36 } | |
| 37 #endif | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 AeadBaseDecrypter::AeadBaseDecrypter(const EVP_AEAD* aead_alg, | |
| 43 size_t key_size, | |
| 44 size_t auth_tag_size, | |
| 45 size_t nonce_prefix_size) | |
| 46 : aead_alg_(aead_alg), | |
| 47 key_size_(key_size), | |
| 48 auth_tag_size_(auth_tag_size), | |
| 49 nonce_prefix_size_(nonce_prefix_size) { | |
| 50 DCHECK_LE(key_size_, sizeof(key_)); | |
| 51 DCHECK_LE(nonce_prefix_size_, sizeof(nonce_prefix_)); | |
| 52 } | |
| 53 | |
| 54 AeadBaseDecrypter::~AeadBaseDecrypter() {} | |
| 55 | |
| 56 bool AeadBaseDecrypter::SetKey(StringPiece key) { | |
| 57 DCHECK_EQ(key.size(), key_size_); | |
| 58 if (key.size() != key_size_) { | |
| 59 return false; | |
| 60 } | |
| 61 memcpy(key_, key.data(), key.size()); | |
| 62 | |
| 63 EVP_AEAD_CTX_cleanup(ctx_.get()); | |
| 64 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, | |
| 65 nullptr)) { | |
| 66 DLogOpenSslErrors(); | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | |
| 74 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | |
| 75 if (nonce_prefix.size() != nonce_prefix_size_) { | |
| 76 return false; | |
| 77 } | |
| 78 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool AeadBaseDecrypter::DecryptPacket(QuicPathId path_id, | |
| 83 QuicPacketNumber packet_number, | |
| 84 StringPiece associated_data, | |
| 85 StringPiece ciphertext, | |
| 86 char* output, | |
| 87 size_t* output_length, | |
| 88 size_t max_output_length) { | |
| 89 if (ciphertext.length() < auth_tag_size_) { | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 uint8_t nonce[sizeof(nonce_prefix_) + sizeof(packet_number)]; | |
| 94 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); | |
| 95 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | |
| 96 uint64_t path_id_packet_number = | |
| 97 QuicUtils::PackPathIdAndPacketNumber(path_id, packet_number); | |
| 98 memcpy(nonce + nonce_prefix_size_, &path_id_packet_number, | |
| 99 sizeof(path_id_packet_number)); | |
| 100 if (!EVP_AEAD_CTX_open( | |
| 101 ctx_.get(), reinterpret_cast<uint8_t*>(output), output_length, | |
| 102 max_output_length, reinterpret_cast<const uint8_t*>(nonce), | |
| 103 nonce_size, reinterpret_cast<const uint8_t*>(ciphertext.data()), | |
| 104 ciphertext.size(), | |
| 105 reinterpret_cast<const uint8_t*>(associated_data.data()), | |
| 106 associated_data.size())) { | |
| 107 // Because QuicFramer does trial decryption, decryption errors are expected | |
| 108 // when encryption level changes. So we don't log decryption errors. | |
| 109 ClearOpenSslErrors(); | |
| 110 return false; | |
| 111 } | |
| 112 return true; | |
| 113 } | |
| 114 | |
| 115 StringPiece AeadBaseDecrypter::GetKey() const { | |
| 116 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | |
| 117 } | |
| 118 | |
| 119 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | |
| 120 if (nonce_prefix_size_ == 0) { | |
| 121 return StringPiece(); | |
| 122 } | |
| 123 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | |
| 124 nonce_prefix_size_); | |
| 125 } | |
| 126 | |
| 127 } // namespace net | |
| OLD | NEW |