vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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/quic_transport/quic_transport_client_session.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "url/gurl.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 17 | |
| 18 | namespace quic { |
| 19 | |
| 20 | const char* kQuicTransportAlpn = "wq-draft01"; |
| 21 | |
| 22 | namespace { |
| 23 | // ProofHandler is primarily used by QUIC crypto to persist QUIC server configs |
| 24 | // and perform some of related debug logging. QuicTransport does not support |
| 25 | // QUIC crypto, so those methods are not called. |
| 26 | class DummyProofHandler : public QuicCryptoClientStream::ProofHandler { |
| 27 | public: |
| 28 | void OnProofValid( |
| 29 | const QuicCryptoClientConfig::CachedState& /*cached*/) override {} |
| 30 | void OnProofVerifyDetailsAvailable( |
| 31 | const ProofVerifyDetails& /*verify_details*/) override {} |
| 32 | }; |
| 33 | } // namespace |
| 34 | |
| 35 | QuicTransportClientSession::QuicTransportClientSession( |
| 36 | QuicConnection* connection, |
| 37 | Visitor* owner, |
| 38 | const QuicConfig& config, |
| 39 | const ParsedQuicVersionVector& supported_versions, |
| 40 | const QuicServerId& server_id, |
| 41 | QuicCryptoClientConfig* crypto_config, |
| 42 | url::Origin origin) |
| 43 | : QuicSession(connection, |
| 44 | owner, |
| 45 | config, |
| 46 | supported_versions, |
| 47 | /*num_expected_unidirectional_static_streams*/ 0), |
| 48 | origin_(origin) { |
| 49 | for (const ParsedQuicVersion& version : supported_versions) { |
| 50 | QUIC_BUG_IF(version.handshake_protocol != PROTOCOL_TLS1_3) |
| 51 | << "QuicTransport requires TLS 1.3 handshake"; |
| 52 | } |
| 53 | // ProofHandler API is not used by TLS 1.3. |
| 54 | static DummyProofHandler* proof_handler = new DummyProofHandler(); |
| 55 | crypto_stream_ = std::make_unique<QuicCryptoClientStream>( |
| 56 | server_id, this, crypto_config->proof_verifier()->CreateDefaultContext(), |
| 57 | crypto_config, proof_handler); |
| 58 | } |
| 59 | |
| 60 | void QuicTransportClientSession::OnCryptoHandshakeEvent( |
| 61 | CryptoHandshakeEvent event) { |
| 62 | QuicSession::OnCryptoHandshakeEvent(event); |
| 63 | if (event != HANDSHAKE_CONFIRMED) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | auto it = config()->received_custom_transport_parameters().find( |
| 68 | WebAcceptedOriginsParameter()); |
| 69 | if (it == config()->received_custom_transport_parameters().end()) { |
| 70 | connection()->CloseConnection( |
| 71 | QUIC_HANDSHAKE_FAILED, |
| 72 | "QuicTransport requires web_accepted_origins transport parameter", |
| 73 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | QUIC_DLOG(INFO) << "QuicTransport using origin: " << origin_.Serialize(); |
| 78 | QUIC_DLOG(INFO) << "QuicTransport origins offered: " << it->second; |
| 79 | |
| 80 | if (CheckOrigin(it->second)) { |
| 81 | is_origin_valid_ = true; |
| 82 | } else { |
| 83 | QUIC_DLOG(ERROR) << "Origin check failed for " << origin_ |
| 84 | << ", allowed origin list: " << it->second; |
| 85 | connection()->CloseConnection( |
| 86 | QUIC_HANDSHAKE_FAILED, "QuicTransport origin check failed", |
| 87 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool QuicTransportClientSession::CheckOrigin( |
| 92 | QuicStringPiece raw_accepted_origins) { |
| 93 | if (raw_accepted_origins == "*") { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | std::vector<QuicStringPiece> accepted_origins = |
| 98 | QuicTextUtils::Split(raw_accepted_origins, ','); |
| 99 | for (QuicStringPiece raw_origin : accepted_origins) { |
| 100 | url::Origin accepted_origin = |
| 101 | url::Origin::Create(GURL(std::string(raw_origin))); |
| 102 | QUIC_DVLOG(1) << "QuicTransport offered origin normalized: " |
| 103 | << accepted_origin.Serialize(); |
| 104 | if (accepted_origin.IsSameOriginWith(origin_)) { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | } // namespace quic |