GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/tls_wrap.h Lines: 11 11 100.0 %
Date: 2019-02-23 22:23:05 Branches: 4 4 100.0 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_TLS_WRAP_H_
23
#define SRC_TLS_WRAP_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "node.h"
28
#include "node_crypto.h"  // SSLWrap
29
30
#include "async_wrap.h"
31
#include "env.h"
32
#include "stream_wrap.h"
33
#include "v8.h"
34
35
#include <openssl/ssl.h>
36
37
#include <string>
38
39
namespace node {
40
41
// Forward-declarations
42
class WriteWrap;
43
namespace crypto {
44
class SecureContext;
45
class NodeBIO;
46
}
47
48
class TLSWrap : public AsyncWrap,
49
                public crypto::SSLWrap<TLSWrap>,
50
                public StreamBase,
51
                public StreamListener {
52
 public:
53
  ~TLSWrap() override;
54
55
  static void Initialize(v8::Local<v8::Object> target,
56
                         v8::Local<v8::Value> unused,
57
                         v8::Local<v8::Context> context,
58
                         void* priv);
59
60
  // Implement StreamBase:
61
  bool IsAlive() override;
62
  bool IsClosing() override;
63
  bool IsIPCPipe() override;
64
  int GetFD() override;
65
  ShutdownWrap* CreateShutdownWrap(
66
      v8::Local<v8::Object> req_wrap_object) override;
67
  AsyncWrap* GetAsyncWrap() override;
68
69
70
  // Implement StreamResource:
71
  int ReadStart() override;  // Exposed to JS
72
  int ReadStop() override;   // Exposed to JS
73
  int DoShutdown(ShutdownWrap* req_wrap) override;
74
  int DoWrite(WriteWrap* w,
75
              uv_buf_t* bufs,
76
              size_t count,
77
              uv_stream_t* send_handle) override;
78
  // Return error_ string or nullptr if it's empty.
79
  const char* Error() const override;
80
  // Reset error_ string to empty. Not related to "clear text".
81
  void ClearError() override;
82
83
84
  // Called by the done() callback of the 'newSession' event.
85
  void NewSessionDoneCb();
86
87
  // Implement MemoryRetainer:
88
  void MemoryInfo(MemoryTracker* tracker) const override;
89
2
  SET_MEMORY_INFO_NAME(TLSWrap)
90
2
  SET_SELF_SIZE(TLSWrap)
91
92
 protected:
93
  // Alternative to StreamListener::stream(), that returns a StreamBase instead
94
  // of a StreamResource.
95
9478
  inline StreamBase* underlying_stream() {
96
9478
    return static_cast<StreamBase*>(stream_);
97
  }
98
99
  static const int kClearOutChunkSize = 16384;
100
101
  // Maximum number of bytes for hello parser
102
  static const int kMaxHelloLength = 16384;
103
104
  // Usual ServerHello + Certificate size
105
  static const int kInitialClientBufferLength = 4096;
106
107
  // Maximum number of buffers passed to uv_write()
108
  static const int kSimultaneousBufferCount = 10;
109
110
  TLSWrap(Environment* env,
111
          v8::Local<v8::Object> obj,
112
          Kind kind,
113
          StreamBase* stream,
114
          crypto::SecureContext* sc);
115
116
  static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
117
  void InitSSL();
118
  // SSL has a "clear" text (unencrypted) side (to/from the node API) and
119
  // encrypted ("enc") text side (to/from the underlying socket/stream).
120
  // On each side data flows "in" or "out" of SSL context.
121
  //
122
  // EncIn() doesn't exist. Encrypted data is pushed from underlying stream into
123
  // enc_in_ via the stream listener's OnStreamAlloc()/OnStreamRead() interface.
124
  void EncOut();  // Write encrypted data from enc_out_ to underlying stream.
125
  void ClearIn();  // SSL_write() clear data "in" to SSL.
126
  void ClearOut();  // SSL_read() clear text "out" from SSL.
127
128
  // Call Done() on outstanding WriteWrap request.
129
  bool InvokeQueued(int status, const char* error_str = nullptr);
130
131
  // Drive the SSL state machine by attempting to SSL_read() and SSL_write() to
132
  // it. Transparent handshakes mean SSL_read() might trigger I/O on the
133
  // underlying stream even if there is no clear text to read or write.
134
7023
  inline void Cycle() {
135
    // Prevent recursion
136
7023
    if (++cycle_depth_ > 1)
137
7030
      return;
138
139
14033
    for (; cycle_depth_ > 0; cycle_depth_--) {
140
7023
      ClearIn();
141
7023
      ClearOut();
142
      // EncIn() doesn't exist, it happens via stream listener callbacks.
143
7021
      EncOut();
144
    }
145
  }
146
147
  // Implement StreamListener:
148
  // Returns buf that points into enc_in_.
149
  uv_buf_t OnStreamAlloc(size_t size) override;
150
  void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
151
  void OnStreamAfterWrite(WriteWrap* w, int status) override;
152
153
  v8::Local<v8::Value> GetSSLError(int status, int* err, std::string* msg);
154
155
  static void OnClientHelloParseEnd(void* arg);
156
  static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
157
  static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
158
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
159
  static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
160
  static void EnableSessionCallbacks(
161
      const v8::FunctionCallbackInfo<v8::Value>& args);
162
  static void EnableCertCb(const v8::FunctionCallbackInfo<v8::Value>& args);
163
  static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
164
  static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
165
  static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
166
  static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
167
168
  crypto::SecureContext* sc_;
169
  // BIO buffers hold encrypted data.
170
  BIO* enc_in_ = nullptr;   // StreamListener fills this for SSL_read().
171
  BIO* enc_out_ = nullptr;  // SSL_write()/handshake fills this for EncOut().
172
  // Waiting for ClearIn() to pass to SSL_write().
173
  std::vector<uv_buf_t> pending_cleartext_input_;
174
  size_t write_size_ = 0;
175
  WriteWrap* current_write_ = nullptr;
176
  WriteWrap* current_empty_write_ = nullptr;
177
  bool write_callback_scheduled_ = false;
178
  bool started_ = false;
179
  bool established_ = false;
180
  bool shutdown_ = false;
181
  std::string error_;
182
  int cycle_depth_ = 0;
183
184
  // If true - delivered EOF to the js-land, either after `close_notify`, or
185
  // after the `UV_EOF` on socket.
186
  bool eof_ = false;
187
188
 private:
189
  static void GetWriteQueueSize(
190
      const v8::FunctionCallbackInfo<v8::Value>& info);
191
};
192
193
}  // namespace node
194
195
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
196
197
#endif  // SRC_TLS_WRAP_H_