summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/TunnelUtils.h
blob: 834d0888c3365a323424e541182fbf6db781afa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_net_TLSFilterTransaction_h
#define mozilla_net_TLSFilterTransaction_h

#include "mozilla/Attributes.h"
#include "mozilla/UniquePtr.h"
#include "nsAHttpTransaction.h"
#include "nsIAsyncInputStream.h"
#include "nsIAsyncOutputStream.h"
#include "nsINamed.h"
#include "nsISocketTransport.h"
#include "nsITimer.h"
#include "NullHttpTransaction.h"
#include "mozilla/TimeStamp.h"
#include "prio.h"

// a TLSFilterTransaction wraps another nsAHttpTransaction but
// applies a encode/decode filter of TLS onto the ReadSegments
// and WriteSegments data. It is not used for basic https://
// but it is used for supplemental TLS tunnels - such as those
// needed by CONNECT tunnels in HTTP/2 or even CONNECT tunnels when
// the underlying proxy connection is already running TLS
//
// HTTP/2 CONNECT tunnels cannot use pushed IO layers because of
// the multiplexing involved on the base stream. i.e. the base stream
// once it is decrypted may have parts that are encrypted with a
// variety of keys, or none at all

/* ************************************************************************
The input path of http over a spdy CONNECT tunnel once it is established as a
stream

note the "real http transaction" can be either a http/1 transaction or another
spdy session inside the tunnel.

  nsHttpConnection::OnInputStreamReady (real socket)
  nsHttpConnection::OnSocketReadable()
  SpdySession::WriteSegment()
  SpdyStream::WriteSegment (tunnel stream)
  SpdyConnectTransaction::WriteSegment
  SpdyStream::OnWriteSegment(tunnel stream)
  SpdySession::OnWriteSegment()
  SpdySession::NetworkRead()
  nsHttpConnection::OnWriteSegment (real socket)
  realSocketIn->Read() return data from network

now pop the stack back up to SpdyConnectTransaction::WriteSegment, the data
that has been read is stored mInputData

  SpdyConnectTransaction.mTunneledConn::OnInputStreamReady(mTunnelStreamIn)
  SpdyConnectTransaction.mTunneledConn::OnSocketReadable()
  TLSFilterTransaction::WriteSegment()
  nsHttpTransaction::WriteSegment(real http transaction)
  TLSFilterTransaction::OnWriteSegment() removes tls on way back up stack
  SpdyConnectTransaction.mTunneledConn::OnWriteSegment()
  // gets data from mInputData
  SpdyConnectTransaction.mTunneledConn.mTunnelStreamIn->Read()

The output path works similarly:
  nsHttpConnection::OnOutputStreamReady (real socket)
  nsHttpConnection::OnSocketWritable()
  SpdySession::ReadSegments (locates tunnel)
  SpdyStream::ReadSegments (tunnel stream)
  SpdyConnectTransaction::ReadSegments()
  SpdyConnectTransaction.mTunneledConn::OnOutputStreamReady (tunnel connection)
  SpdyConnectTransaction.mTunneledConn::OnSocketWritable (tunnel connection)
  TLSFilterTransaction::ReadSegment()
  nsHttpTransaction::ReadSegment (real http transaction generates plaintext on
                                  way down)
  TLSFilterTransaction::OnReadSegment (BUF and LEN gets encrypted here on way
                                       down)
  SpdyConnectTransaction.mTunneledConn::OnReadSegment (BUF and LEN)
                                                      (tunnel connection)
  SpdyConnectTransaction.mTunneledConn.mTunnelStreamOut->Write(BUF, LEN) ..
                                                     get stored in mOutputData

Now pop the stack back up to SpdyConnectTransaction::ReadSegment(), where it has
the encrypted text available in mOutputData

  SpdyStream->OnReadSegment(BUF,LEN) from mOutputData. Tunnel stream
  SpdySession->OnReadSegment() // encrypted data gets put in a data frame
  nsHttpConnection->OnReadSegment()
  realSocketOut->write() writes data to network

**************************************************************************/

struct PRSocketOptionData;

namespace mozilla {
namespace net {

class nsHttpRequestHead;
class NullHttpTransaction;
class TLSFilterTransaction;

class NudgeTunnelCallback : public nsISupports {
 public:
  virtual nsresult OnTunnelNudged(TLSFilterTransaction*) = 0;
};

#define NS_DECL_NUDGETUNNELCALLBACK \
  nsresult OnTunnelNudged(TLSFilterTransaction*) override;

class TLSFilterTransaction final : public nsAHttpTransaction,
                                   public nsAHttpSegmentReader,
                                   public nsAHttpSegmentWriter,
                                   public nsITimerCallback,
                                   public nsINamed {
  ~TLSFilterTransaction();

 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSAHTTPTRANSACTION
  NS_DECL_NSAHTTPSEGMENTREADER
  NS_DECL_NSAHTTPSEGMENTWRITER
  NS_DECL_NSITIMERCALLBACK
  NS_DECL_NSINAMED

  TLSFilterTransaction(nsAHttpTransaction* aWrappedTransaction,
                       const char* tlsHost, int32_t tlsPort,
                       nsAHttpSegmentReader* reader,
                       nsAHttpSegmentWriter* writer);

  const nsAHttpTransaction* Transaction() const { return mTransaction.get(); }
  [[nodiscard]] nsresult CommitToSegmentSize(uint32_t size,
                                             bool forceCommitment) override;
  [[nodiscard]] nsresult GetTransactionSecurityInfo(nsISupports**) override;
  [[nodiscard]] nsresult NudgeTunnel(NudgeTunnelCallback* callback);
  [[nodiscard]] nsresult SetProxiedTransaction(
      nsAHttpTransaction* aTrans,
      nsAHttpTransaction* aSpdyConnectTransaction = nullptr);
  void newIODriver(nsIAsyncInputStream* aSocketIn,
                   nsIAsyncOutputStream* aSocketOut,
                   nsIAsyncInputStream** outSocketIn,
                   nsIAsyncOutputStream** outSocketOut);

  // nsAHttpTransaction overloads
  bool IsNullTransaction() override;
  NullHttpTransaction* QueryNullTransaction() override;
  nsHttpTransaction* QueryHttpTransaction() override;
  SpdyConnectTransaction* QuerySpdyConnectTransaction() override;
  [[nodiscard]] nsresult WriteSegmentsAgain(nsAHttpSegmentWriter* writer,
                                            uint32_t count,
                                            uint32_t* countWritten,
                                            bool* again) override;

  bool HasDataToRecv();

 private:
  [[nodiscard]] nsresult StartTimerCallback();
  void Cleanup();
  int32_t FilterOutput(const char* aBuf, int32_t aAmount);
  int32_t FilterInput(char* aBuf, int32_t aAmount);

  static PRStatus GetPeerName(PRFileDesc* fd, PRNetAddr* addr);
  static PRStatus GetSocketOption(PRFileDesc* fd, PRSocketOptionData* data);
  static PRStatus SetSocketOption(PRFileDesc* fd,
                                  const PRSocketOptionData* data);
  static int32_t FilterWrite(PRFileDesc* fd, const void* buf, int32_t amount);
  static int32_t FilterRead(PRFileDesc* fd, void* buf, int32_t amount);
  static int32_t FilterSend(PRFileDesc* fd, const void* buf, int32_t amount,
                            int flags, PRIntervalTime timeout);
  static int32_t FilterRecv(PRFileDesc* fd, void* buf, int32_t amount,
                            int flags, PRIntervalTime timeout);
  static PRStatus FilterClose(PRFileDesc* fd);

 private:
  RefPtr<nsAHttpTransaction> mTransaction;
  nsWeakPtr mWeakTrans;  // SpdyConnectTransaction *
  nsCOMPtr<nsISupports> mSecInfo;
  nsCOMPtr<nsITimer> mTimer;
  RefPtr<NudgeTunnelCallback> mNudgeCallback;

  // buffered network output, after encryption
  UniquePtr<char[]> mEncryptedText;
  uint32_t mEncryptedTextUsed;
  uint32_t mEncryptedTextSize;

  PRFileDesc* mFD;
  nsAHttpSegmentReader* mSegmentReader;
  nsAHttpSegmentWriter* mSegmentWriter;

  nsresult mFilterReadCode;
  int32_t mFilterReadAmount;
  // Set only when we are calling PR_Write from inside OnReadSegment.  Prevents
  // calling back to OnReadSegment from inside FilterOutput.
  bool mInOnReadSegment;
  bool mForce;
  nsresult mReadSegmentReturnValue;
  // Before Close() is called this is NS_ERROR_UNEXPECTED, in Close() we either
  // take the reason, if it is a failure, or we change to
  // NS_ERROR_BASE_STREAM_CLOSE.  This is returned when Write/ReadSegments is
  // called after Close, when we don't have mTransaction any more.
  nsresult mCloseReason;
  uint32_t mNudgeCounter;
};

class SocketTransportShim;
class InputStreamShim;
class OutputStreamShim;
class nsHttpConnection;

class SpdyConnectTransaction final : public NullHttpTransaction {
 public:
  SpdyConnectTransaction(nsHttpConnectionInfo* ci,
                         nsIInterfaceRequestor* callbacks, uint32_t caps,
                         nsHttpTransaction* trans, nsAHttpConnection* session,
                         bool isWebsocket);
  ~SpdyConnectTransaction();

  SpdyConnectTransaction* QuerySpdyConnectTransaction() override {
    return this;
  }

  // A transaction is forced into plaintext when it is intended to be used as a
  // CONNECT tunnel but the setup fails. The plaintext only carries the CONNECT
  // error.
  void ForcePlainText();
  // True if we successfully map stream to a nsHttpConnection. Currently we skip
  // 1xx response only.
  bool MapStreamToHttpConnection(nsISocketTransport* aTransport,
                                 nsHttpConnectionInfo* aConnInfo,
                                 const nsACString& aFlat407Headers,
                                 int32_t aHttpResponseCode);

  [[nodiscard]] nsresult ReadSegments(nsAHttpSegmentReader* reader,
                                      uint32_t count,
                                      uint32_t* countRead) final;
  [[nodiscard]] nsresult WriteSegments(nsAHttpSegmentWriter* writer,
                                       uint32_t count,
                                       uint32_t* countWritten) final;
  nsHttpRequestHead* RequestHead() final;
  void Close(nsresult reason) final;

  // ConnectedReadyForInput() tests whether the spdy connect transaction is
  // attached to an nsHttpConnection that can properly deal with flow control,
  // etc..
  bool ConnectedReadyForInput();

  bool IsWebsocket() { return mIsWebsocket; }
  void SetConnRefTaken();

 private:
  friend class SocketTransportShim;
  friend class InputStreamShim;
  friend class OutputStreamShim;

  [[nodiscard]] nsresult Flush(uint32_t count, uint32_t* countRead);
  void CreateShimError(nsresult code);

  nsCString mConnectString;
  uint32_t mConnectStringOffset;

  nsAHttpConnection* mSession;
  nsAHttpSegmentReader* mSegmentReader;

  UniquePtr<char[]> mInputData;
  uint32_t mInputDataSize;
  uint32_t mInputDataUsed;
  uint32_t mInputDataOffset;

  UniquePtr<char[]> mOutputData;
  uint32_t mOutputDataSize;
  uint32_t mOutputDataUsed;
  uint32_t mOutputDataOffset;

  bool mForcePlainText;
  TimeStamp mTimestampSyn;

  // mTunneledConn, mTunnelTransport, mTunnelStreamIn, mTunnelStreamOut
  // are the connectors to the "real" http connection. They are created
  // together when the tunnel setup is complete and a static reference is held
  // for the lifetime of the tunnel.
  RefPtr<nsHttpConnection> mTunneledConn;
  RefPtr<SocketTransportShim> mTunnelTransport;
  RefPtr<InputStreamShim> mTunnelStreamIn;
  RefPtr<OutputStreamShim> mTunnelStreamOut;
  RefPtr<nsHttpTransaction> mDrivingTransaction;

  // This is all for websocket support
  bool mIsWebsocket;
  bool mConnRefTaken;
  nsCOMPtr<nsIAsyncOutputStream> mInputShimPipe;
  nsCOMPtr<nsIAsyncInputStream> mOutputShimPipe;
  nsresult WriteDataToBuffer(nsAHttpSegmentWriter* writer, uint32_t count,
                             uint32_t* countWritten);
  [[nodiscard]] nsresult WebsocketWriteSegments(nsAHttpSegmentWriter* writer,
                                                uint32_t count,
                                                uint32_t* countWritten);

  bool mCreateShimErrorCalled;
};

}  // namespace net
}  // namespace mozilla

#endif  // mozilla_net_TLSFilterTransaction_h