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);
71 static const int kClearOutChunkSize = 1024;
73 // Maximum number of buffers passed to uv_write()
74 static const int kSimultaneousBufferCount = 10;
76 // Write callback queue's item
79 WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
91 TLSCallbacks(Environment* env,
93 v8::Handle<v8::Object> sc,
94 StreamWrapCallbacks* old);
97 static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
100 static void EncOutCb(uv_write_t* req, int status);
104 bool InvokeQueued(int status);
106 inline void Cycle() {
112 v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
113 const char* PrintErrors();
115 static int PrintErrorsCb(const char* str, size_t len, void* arg);
116 static void OnClientHelloParseEnd(void* arg);
118 static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
119 static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
120 static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
121 static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
122 static void EnableSessionCallbacks(
123 const v8::FunctionCallbackInfo<v8::Value>& args);
124 static void EnableHelloParser(
125 const v8::FunctionCallbackInfo<v8::Value>& args);
127 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
128 static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
129 static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
130 static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
131 #endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
133 crypto::SecureContext* sc_;
134 v8::Persistent<v8::Object> sc_handle_;
138 uv_write_t write_req_;
140 size_t write_queue_size_;
141 QUEUE write_item_queue_;
142 QUEUE pending_write_items_;
148 // If true - delivered EOF to the js-land, either after `close_notify`, or
149 // after the `UV_EOF` on socket.
152 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
153 v8::Persistent<v8::Value> sni_context_;
154 #endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
156 static size_t error_off_;
157 static char error_buf_[1024];
162 #endif // SRC_TLS_WRAP_H_