Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
tls_wrap.h
Go to the documentation of this file.
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 "util.h"
34 #include "v8.h"
35 
36 #include <openssl/ssl.h>
37 
38 namespace node {
39 
40 // Forward-declarations
41 class WriteWrap;
42 namespace crypto {
43 class SecureContext;
44 class NodeBIO;
45 }
46 
47 class TLSWrap : public AsyncWrap,
48  public crypto::SSLWrap<TLSWrap>,
49  public StreamBase {
50  public:
51  ~TLSWrap() override;
52 
53  static void Initialize(v8::Local<v8::Object> target,
54  v8::Local<v8::Value> unused,
55  v8::Local<v8::Context> context);
56 
57  void* Cast() override;
58  int GetFD() override;
59  bool IsAlive() override;
60  bool IsClosing() override;
61 
62  // JavaScript functions
63  int ReadStart() override;
64  int ReadStop() override;
65 
66  int DoShutdown(ShutdownWrap* req_wrap) override;
67  int DoWrite(WriteWrap* w,
68  uv_buf_t* bufs,
69  size_t count,
70  uv_stream_t* send_handle) override;
71  const char* Error() const override;
72  void ClearError() override;
73 
74  void NewSessionDoneCb();
75 
76  size_t self_size() const override { return sizeof(*this); }
77 
78  void clear_stream() { stream_ = nullptr; }
79 
80  protected:
81  static const int kClearOutChunkSize = 16384;
82 
83  // Maximum number of bytes for hello parser
84  static const int kMaxHelloLength = 16384;
85 
86  // Usual ServerHello + Certificate size
87  static const int kInitialClientBufferLength = 4096;
88 
89  // Maximum number of buffers passed to uv_write()
90  static const int kSimultaneousBufferCount = 10;
91 
92  // Write callback queue's item
93  class WriteItem {
94  public:
95  explicit WriteItem(WriteWrap* w) : w_(w) {
96  }
97  ~WriteItem() {
98  w_ = nullptr;
99  }
100 
101  WriteWrap* w_;
102  ListNode<WriteItem> member_;
103  };
104 
105  TLSWrap(Environment* env,
106  Kind kind,
107  StreamBase* stream,
108  crypto::SecureContext* sc);
109 
110  static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
111  void InitSSL();
112  void EncOut();
113  static void EncOutCb(WriteWrap* req_wrap, int status);
114  bool ClearIn();
115  void ClearOut();
116  void MakePending();
117  bool InvokeQueued(int status, const char* error_str = nullptr);
118 
119  inline void Cycle() {
120  // Prevent recursion
121  if (++cycle_depth_ > 1)
122  return;
123 
124  for (; cycle_depth_ > 0; cycle_depth_--) {
125  ClearIn();
126  ClearOut();
127  EncOut();
128  }
129  }
130 
131  AsyncWrap* GetAsyncWrap() override;
132  bool IsIPCPipe() override;
133 
134  // Resource implementation
135  static void OnAfterWriteImpl(WriteWrap* w, void* ctx);
136  static void OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx);
137  static void OnReadImpl(ssize_t nread,
138  const uv_buf_t* buf,
139  uv_handle_type pending,
140  void* ctx);
141  static void OnAfterWriteSelf(WriteWrap* w, void* ctx);
142  static void OnAllocSelf(size_t size, uv_buf_t* buf, void* ctx);
143  static void OnReadSelf(ssize_t nread,
144  const uv_buf_t* buf,
145  uv_handle_type pending,
146  void* ctx);
147  static void OnDestructImpl(void* ctx);
148 
149  void DoRead(ssize_t nread, const uv_buf_t* buf, uv_handle_type pending);
150 
151  // If |msg| is not nullptr, caller is responsible for calling `delete[] *msg`.
152  v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
153 
154  static void OnClientHelloParseEnd(void* arg);
155  static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
156  static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
157  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
158  static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
159  static void EnableSessionCallbacks(
160  const v8::FunctionCallbackInfo<v8::Value>& args);
161  static void EnableCertCb(
162  const v8::FunctionCallbackInfo<v8::Value>& args);
163  static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
164 
165 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
166  static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
167  static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
168  static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
169 #endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
170 
171  crypto::SecureContext* sc_;
172  StreamBase* stream_;
173  BIO* enc_in_;
174  BIO* enc_out_;
175  crypto::NodeBIO* clear_in_;
176  size_t write_size_;
177  typedef ListHead<WriteItem, &WriteItem::member_> WriteItemList;
178  WriteItemList write_item_queue_;
179  WriteItemList pending_write_items_;
180  bool started_;
181  bool established_;
182  bool shutdown_;
183  const char* error_;
184  int cycle_depth_;
185 
186  // If true - delivered EOF to the js-land, either after `close_notify`, or
187  // after the `UV_EOF` on socket.
188  bool eof_;
189 };
190 
191 } // namespace node
192 
193 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
194 
195 #endif // SRC_TLS_WRAP_H_
unsigned char * buf
Definition: cares_wrap.cc:483
int status
Definition: cares_wrap.cc:479
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
dtrace s
Definition: v8ustack.d:615
this ctx
Definition: v8ustack.d:369
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536