QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 6 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 7 | #include <string> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 8 | #include <utility> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "third_party/boringssl/src/include/openssl/tls1.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/aes_128_gcm_12_decrypter.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/crypto/aes_128_gcm_decrypter.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/aes_256_gcm_decrypter.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/chacha20_poly1305_decrypter.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/chacha20_poly1305_tls_decrypter.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/quic_hkdf.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | |
| 23 | namespace quic { |
| 24 | |
| 25 | // static |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 26 | std::unique_ptr<QuicDecrypter> QuicDecrypter::Create( |
| 27 | const ParsedQuicVersion& version, |
| 28 | QuicTag algorithm) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | switch (algorithm) { |
| 30 | case kAESG: |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 31 | if (version.UsesInitialObfuscators()) { |
| 32 | return std::make_unique<Aes128GcmDecrypter>(); |
| 33 | } else { |
| 34 | return std::make_unique<Aes128Gcm12Decrypter>(); |
| 35 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 36 | case kCC20: |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 37 | if (version.UsesInitialObfuscators()) { |
| 38 | return std::make_unique<ChaCha20Poly1305TlsDecrypter>(); |
| 39 | } else { |
| 40 | return std::make_unique<ChaCha20Poly1305Decrypter>(); |
| 41 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | default: |
| 43 | QUIC_LOG(FATAL) << "Unsupported algorithm: " << algorithm; |
| 44 | return nullptr; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | std::unique_ptr<QuicDecrypter> QuicDecrypter::CreateFromCipherSuite( |
| 50 | uint32_t cipher_suite) { |
| 51 | switch (cipher_suite) { |
| 52 | case TLS1_CK_AES_128_GCM_SHA256: |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 53 | return std::make_unique<Aes128GcmDecrypter>(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | case TLS1_CK_AES_256_GCM_SHA384: |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 55 | return std::make_unique<Aes256GcmDecrypter>(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | case TLS1_CK_CHACHA20_POLY1305_SHA256: |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 57 | return std::make_unique<ChaCha20Poly1305TlsDecrypter>(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 58 | default: |
| 59 | QUIC_BUG << "TLS cipher suite is unknown to QUIC"; |
| 60 | return nullptr; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // static |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 65 | void QuicDecrypter::DiversifyPreliminaryKey( |
| 66 | quiche::QuicheStringPiece preliminary_key, |
| 67 | quiche::QuicheStringPiece nonce_prefix, |
| 68 | const DiversificationNonce& nonce, |
| 69 | size_t key_size, |
| 70 | size_t nonce_prefix_size, |
| 71 | std::string* out_key, |
| 72 | std::string* out_nonce_prefix) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 73 | QuicHKDF hkdf((std::string(preliminary_key)) + (std::string(nonce_prefix)), |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 74 | quiche::QuicheStringPiece(nonce.data(), nonce.size()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 75 | "QUIC key diversification", 0, key_size, 0, nonce_prefix_size, |
| 76 | 0); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 77 | *out_key = std::string(hkdf.server_write_key()); |
| 78 | *out_nonce_prefix = std::string(hkdf.server_write_iv()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace quic |