1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #ifndef SRC_TLS_WRAP_H_
23 #define SRC_TLS_WRAP_H_
26 #include "node_crypto.h" // SSLWrap
28 #include "async-wrap.h"
31 #include "stream_wrap.h"
34 #include <openssl/ssl.h>
38 // Forward-declarations
45 class TLSCallbacks : public crypto::SSLWrap<TLSCallbacks>,
46 public StreamWrapCallbacks,
49 static void Initialize(v8::Handle<v8::Object> target,
50 v8::Handle<v8::Value> unused,
51 v8::Handle<v8::Context> context);
54 int TryWrite(uv_buf_t** bufs, size_t* count);
55 int DoWrite(WriteWrap* w,
58 uv_stream_t* send_handle,
60 void AfterWrite(WriteWrap* w);
61 void DoAlloc(uv_handle_t* handle,
62 size_t suggested_size,
64 void DoRead(uv_stream_t* handle,
67 uv_handle_type pending);
68 int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
70 void NewSessionDoneCb();
73 static const int kClearOutChunkSize = 1024;
75 // Maximum number of buffers passed to uv_write()
76 static const int kSimultaneousBufferCount = 10;
78 // Write callback queue's item
81 WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
93 TLSCallbacks(Environment* env,
95 v8::Handle<v8::Object> sc,
96 StreamWrapCallbacks* old);
99 static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
102 static void EncOutCb(uv_write_t* req, int status);
106 bool InvokeQueued(int status);
108 inline void Cycle() {
110 if (++cycle_depth_ > 1)
113 for (; cycle_depth_ > 0; cycle_depth_--) {
120 v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
121 const char* PrintErrors();
123 static int PrintErrorsCb(const char* str, size_t len, void* arg);
124 static void OnClientHelloParseEnd(void* arg);
126 static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
127 static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
128 static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
129 static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
130 static void EnableSessionCallbacks(
131 const v8::FunctionCallbackInfo<v8::Value>& args);
132 static void EnableHelloParser(
133 const v8::FunctionCallbackInfo<v8::Value>& args);
135 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
136 static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
137 static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
138 static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
139 #endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
141 crypto::SecureContext* sc_;
142 v8::Persistent<v8::Object> sc_handle_;
146 uv_write_t write_req_;
148 size_t write_queue_size_;
149 QUEUE write_item_queue_;
150 QUEUE pending_write_items_;
157 // If true - delivered EOF to the js-land, either after `close_notify`, or
158 // after the `UV_EOF` on socket.
161 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
162 v8::Persistent<v8::Value> sni_context_;
163 #endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
165 static size_t error_off_;
166 static char error_buf_[1024];
171 #endif // SRC_TLS_WRAP_H_