src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / tls_wrap.h
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 #include "node.h"
26 #include "node_crypto.h"  // SSLWrap
27
28 #include "async-wrap.h"
29 #include "env.h"
30 #include "queue.h"
31 #include "stream_wrap.h"
32 #include "v8.h"
33
34 #include <openssl/ssl.h>
35
36 namespace node {
37
38 // Forward-declarations
39 class NodeBIO;
40 class WriteWrap;
41 namespace crypto {
42   class SecureContext;
43 }
44
45 class TLSCallbacks : public crypto::SSLWrap<TLSCallbacks>,
46                      public StreamWrapCallbacks,
47                      public AsyncWrap {
48  public:
49   static void Initialize(v8::Handle<v8::Object> target,
50                          v8::Handle<v8::Value> unused,
51                          v8::Handle<v8::Context> context);
52
53   const char* Error();
54   int TryWrite(uv_buf_t** bufs, size_t* count);
55   int DoWrite(WriteWrap* w,
56               uv_buf_t* bufs,
57               size_t count,
58               uv_stream_t* send_handle,
59               uv_write_cb cb);
60   void AfterWrite(WriteWrap* w);
61   void DoAlloc(uv_handle_t* handle,
62                size_t suggested_size,
63                uv_buf_t* buf);
64   void DoRead(uv_stream_t* handle,
65               ssize_t nread,
66               const uv_buf_t* buf,
67               uv_handle_type pending);
68   int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
69
70   void NewSessionDoneCb();
71
72  protected:
73   static const int kClearOutChunkSize = 1024;
74
75   // Maximum number of buffers passed to uv_write()
76   static const int kSimultaneousBufferCount = 10;
77
78   // Write callback queue's item
79   class WriteItem {
80    public:
81     WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
82     }
83     ~WriteItem() {
84       w_ = NULL;
85       cb_ = NULL;
86     }
87
88     WriteWrap* w_;
89     uv_write_cb cb_;
90     QUEUE member_;
91   };
92
93   TLSCallbacks(Environment* env,
94                Kind kind,
95                v8::Handle<v8::Object> sc,
96                StreamWrapCallbacks* old);
97   ~TLSCallbacks();
98
99   static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
100   void InitSSL();
101   void EncOut();
102   static void EncOutCb(uv_write_t* req, int status);
103   bool ClearIn();
104   void ClearOut();
105   void MakePending();
106   bool InvokeQueued(int status);
107
108   inline void Cycle() {
109     // Prevent recursion
110     if (++cycle_depth_ > 1)
111       return;
112
113     for (; cycle_depth_ > 0; cycle_depth_--) {
114       ClearIn();
115       ClearOut();
116       EncOut();
117     }
118   }
119
120   v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
121   const char* PrintErrors();
122
123   static int PrintErrorsCb(const char* str, size_t len, void* arg);
124   static void OnClientHelloParseEnd(void* arg);
125
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);
134
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
140
141   crypto::SecureContext* sc_;
142   v8::Persistent<v8::Object> sc_handle_;
143   BIO* enc_in_;
144   BIO* enc_out_;
145   NodeBIO* clear_in_;
146   uv_write_t write_req_;
147   size_t write_size_;
148   size_t write_queue_size_;
149   QUEUE write_item_queue_;
150   QUEUE pending_write_items_;
151   bool started_;
152   bool established_;
153   bool shutdown_;
154   const char* error_;
155   int cycle_depth_;
156
157   // If true - delivered EOF to the js-land, either after `close_notify`, or
158   // after the `UV_EOF` on socket.
159   bool eof_;
160
161 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
162   v8::Persistent<v8::Value> sni_context_;
163 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
164
165   static size_t error_off_;
166   static char error_buf_[1024];
167 };
168
169 }  // namespace node
170
171 #endif  // SRC_TLS_WRAP_H_