OLD | NEW |
| (Empty) |
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/quic/quic_crypto_stream.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/strings/string_piece.h" | |
10 #include "net/quic/crypto/crypto_handshake.h" | |
11 #include "net/quic/crypto/crypto_utils.h" | |
12 #include "net/quic/quic_connection.h" | |
13 #include "net/quic/quic_flags.h" | |
14 #include "net/quic/quic_session.h" | |
15 #include "net/quic/quic_utils.h" | |
16 | |
17 using std::string; | |
18 using base::StringPiece; | |
19 using net::SpdyPriority; | |
20 | |
21 namespace net { | |
22 | |
23 #define ENDPOINT \ | |
24 (session()->perspective() == Perspective::IS_SERVER ? "Server: " : "Client:" \ | |
25 " ") | |
26 | |
27 QuicCryptoStream::QuicCryptoStream(QuicSession* session) | |
28 : ReliableQuicStream(kCryptoStreamId, session), | |
29 encryption_established_(false), | |
30 handshake_confirmed_(false) { | |
31 crypto_framer_.set_visitor(this); | |
32 // The crypto stream is exempt from connection level flow control. | |
33 DisableConnectionFlowControlForThisStream(); | |
34 } | |
35 | |
36 void QuicCryptoStream::OnError(CryptoFramer* framer) { | |
37 DLOG(WARNING) << "Error processing crypto data: " | |
38 << QuicUtils::ErrorToString(framer->error()); | |
39 } | |
40 | |
41 void QuicCryptoStream::OnHandshakeMessage( | |
42 const CryptoHandshakeMessage& message) { | |
43 DVLOG(1) << ENDPOINT << "Received " << message.DebugString(); | |
44 session()->OnCryptoHandshakeMessageReceived(message); | |
45 } | |
46 | |
47 void QuicCryptoStream::OnDataAvailable() { | |
48 struct iovec iov; | |
49 while (true) { | |
50 if (sequencer()->GetReadableRegions(&iov, 1) != 1) { | |
51 // No more data to read. | |
52 break; | |
53 } | |
54 StringPiece data(static_cast<char*>(iov.iov_base), iov.iov_len); | |
55 if (!crypto_framer_.ProcessInput(data)) { | |
56 CloseConnectionWithDetails(crypto_framer_.error(), | |
57 crypto_framer_.error_detail()); | |
58 return; | |
59 } | |
60 sequencer()->MarkConsumed(iov.iov_len); | |
61 } | |
62 } | |
63 | |
64 void QuicCryptoStream::SendHandshakeMessage( | |
65 const CryptoHandshakeMessage& message) { | |
66 SendHandshakeMessage(message, nullptr); | |
67 } | |
68 | |
69 void QuicCryptoStream::SendHandshakeMessage( | |
70 const CryptoHandshakeMessage& message, | |
71 QuicAckListenerInterface* listener) { | |
72 DVLOG(1) << ENDPOINT << "Sending " << message.DebugString(); | |
73 if (FLAGS_quic_neuter_unencrypted_when_sending) { | |
74 session()->connection()->NeuterUnencryptedPackets(); | |
75 } | |
76 session()->OnCryptoHandshakeMessageSent(message); | |
77 const QuicData& data = message.GetSerialized(); | |
78 // TODO(wtc): check the return value. | |
79 WriteOrBufferData(StringPiece(data.data(), data.length()), false, listener); | |
80 } | |
81 | |
82 bool QuicCryptoStream::ExportKeyingMaterial(StringPiece label, | |
83 StringPiece context, | |
84 size_t result_len, | |
85 string* result) const { | |
86 if (!handshake_confirmed()) { | |
87 DLOG(ERROR) << "ExportKeyingMaterial was called before forward-secure" | |
88 << "encryption was established."; | |
89 return false; | |
90 } | |
91 return CryptoUtils::ExportKeyingMaterial( | |
92 crypto_negotiated_params_.subkey_secret, label, context, result_len, | |
93 result); | |
94 } | |
95 | |
96 bool QuicCryptoStream::ExportTokenBindingKeyingMaterial(string* result) const { | |
97 if (!encryption_established()) { | |
98 QUIC_BUG << "ExportTokenBindingKeyingMaterial was called before initial" | |
99 << "encryption was established."; | |
100 return false; | |
101 } | |
102 return CryptoUtils::ExportKeyingMaterial( | |
103 crypto_negotiated_params_.initial_subkey_secret, "EXPORTER-Token-Binding", | |
104 /* context= */ "", 32, result); | |
105 } | |
106 | |
107 const QuicCryptoNegotiatedParameters& | |
108 QuicCryptoStream::crypto_negotiated_params() const { | |
109 return crypto_negotiated_params_; | |
110 } | |
111 | |
112 } // namespace net | |
OLD | NEW |