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: 0 11 0.0 %
Date: 2019-02-01 22:03:38 Branches: 0 4 0.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
  int GetFD() override;
61
  bool IsAlive() override;
62
  bool IsClosing() override;
63
64
  // JavaScript functions
65
  int ReadStart() override;
66
  int ReadStop() override;
67
68
  ShutdownWrap* CreateShutdownWrap(
69
      v8::Local<v8::Object> req_wrap_object) override;
70
  int DoShutdown(ShutdownWrap* req_wrap) override;
71
  int DoWrite(WriteWrap* w,
72
              uv_buf_t* bufs,
73
              size_t count,
74
              uv_stream_t* send_handle) override;
75
  // Return error_ string or nullptr if it's empty.
76
  const char* Error() const override;
77
  // Reset error_ string to empty. Not related to "clear text".
78
  void ClearError() override;
79
80
  void NewSessionDoneCb();
81
82
  void MemoryInfo(MemoryTracker* tracker) const override;
83
84
  SET_MEMORY_INFO_NAME(TLSWrap)
85
  SET_SELF_SIZE(TLSWrap)
86
87
 protected:
88
  inline StreamBase* underlying_stream() {
89
    return static_cast<StreamBase*>(stream_);
90
  }
91
92
  static const int kClearOutChunkSize = 16384;
93
94
  // Maximum number of bytes for hello parser
95
  static const int kMaxHelloLength = 16384;
96
97
  // Usual ServerHello + Certificate size
98
  static const int kInitialClientBufferLength = 4096;
99
100
  // Maximum number of buffers passed to uv_write()
101
  static const int kSimultaneousBufferCount = 10;
102
103
  TLSWrap(Environment* env,
104
          Kind kind,
105
          StreamBase* stream,
106
          crypto::SecureContext* sc);
107
108
  static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
109
  void InitSSL();
110
  // SSL has a "clear" text (unencrypted) side (to/from the node API) and
111
  // encrypted ("enc") text side (to/from the underlying socket/stream).
112
  // On each side data flows "in" or "out" of SSL context.
113
  //
114
  // EncIn() doesn't exist. Encrypted data is pushed from underlying stream into
115
  // enc_in_ via the stream listener's OnStreamAlloc()/OnStreamRead() interface.
116
  void EncOut();  // Write encrypted data from enc_out_ to underlying stream.
117
  void ClearIn();  // SSL_write() clear data "in" to SSL.
118
  void ClearOut();  // SSL_read() clear text "out" from SSL.
119
120
  // Call Done() on outstanding WriteWrap request.
121
  bool InvokeQueued(int status, const char* error_str = nullptr);
122
123
  // Drive the SSL state machine by attempting to SSL_read() and SSL_write() to
124
  // it. Transparent handshakes mean SSL_read() might trigger I/O on the
125
  // underlying stream even if there is no clear text to read or write.
126
  inline void Cycle() {
127
    // Prevent recursion
128
    if (++cycle_depth_ > 1)
129
      return;
130
131
    for (; cycle_depth_ > 0; cycle_depth_--) {
132
      ClearIn();
133
      ClearOut();
134
      // EncIn() doesn't exist, it happens via stream listener callbacks.
135
      EncOut();
136
    }
137
  }
138
139
  AsyncWrap* GetAsyncWrap() override;
140
  bool IsIPCPipe() override;
141
142
  // Resource implementation
143
  void OnStreamAfterWrite(WriteWrap* w, int status) override;
144
  uv_buf_t OnStreamAlloc(size_t size) override;
145
  void OnStreamRead(ssize_t nread, const uv_buf_t& buf) override;
146
147
  v8::Local<v8::Value> GetSSLError(int status, int* err, std::string* msg);
148
149
  static void OnClientHelloParseEnd(void* arg);
150
  static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
151
  static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
152
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
153
  static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
154
  static void EnableSessionCallbacks(
155
      const v8::FunctionCallbackInfo<v8::Value>& args);
156
  static void EnableTrace(const v8::FunctionCallbackInfo<v8::Value>& args);
157
  static void EnableCertCb(const v8::FunctionCallbackInfo<v8::Value>& args);
158
  static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
159
  static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
160
  static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
161
  static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
162
163
  crypto::SecureContext* sc_;
164
  // BIO buffers hold encrypted data.
165
  BIO* enc_in_ = nullptr;   // StreamListener fills this for SSL_read().
166
  BIO* enc_out_ = nullptr;  // SSL_write()/handshake fills this for EncOut().
167
  // Waiting for ClearIn() to pass to SSL_write().
168
  std::vector<uv_buf_t> pending_cleartext_input_;
169
  size_t write_size_ = 0;
170
  WriteWrap* current_write_ = nullptr;
171
  WriteWrap* current_empty_write_ = nullptr;
172
  bool write_callback_scheduled_ = false;
173
  bool started_ = false;
174
  bool established_ = false;
175
  bool shutdown_ = false;
176
  std::string error_;
177
  int cycle_depth_ = 0;
178
179
  // If true - delivered EOF to the js-land, either after `close_notify`, or
180
  // after the `UV_EOF` on socket.
181
  bool eof_ = false;
182
183
 private:
184
  static void GetWriteQueueSize(
185
      const v8::FunctionCallbackInfo<v8::Value>& info);
186
};
187
188
}  // namespace node
189
190
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
191
192
#endif  // SRC_TLS_WRAP_H_