924a7243c4a5be6b78f8cc279ee0ed5690570fd3
[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   int DoWrite(WriteWrap* w,
54               uv_buf_t* bufs,
55               size_t count,
56               uv_stream_t* send_handle,
57               uv_write_cb cb);
58   void AfterWrite(WriteWrap* w);
59   void DoAlloc(uv_handle_t* handle,
60                size_t suggested_size,
61                uv_buf_t* buf);
62   void DoRead(uv_stream_t* handle,
63               ssize_t nread,
64               const uv_buf_t* buf,
65               uv_handle_type pending);
66   int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
67
68  protected:
69   static const int kClearOutChunkSize = 1024;
70
71   // Write callback queue's item
72   class WriteItem {
73    public:
74     WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
75     }
76     ~WriteItem() {
77       w_ = NULL;
78       cb_ = NULL;
79     }
80
81     WriteWrap* w_;
82     uv_write_cb cb_;
83     QUEUE member_;
84   };
85
86   TLSCallbacks(Environment* env,
87                Kind kind,
88                v8::Handle<v8::Object> sc,
89                StreamWrapCallbacks* old);
90   ~TLSCallbacks();
91
92   static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
93   void InitSSL();
94   void EncOut();
95   static void EncOutCb(uv_write_t* req, int status);
96   bool ClearIn();
97   void ClearOut();
98   void InvokeQueued(int status);
99
100   inline void Cycle() {
101     ClearIn();
102     ClearOut();
103     EncOut();
104   }
105
106   v8::Local<v8::Value> GetSSLError(int status, int* err);
107   static void OnClientHelloParseEnd(void* arg);
108
109   static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
110   static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
111   static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
112   static void EnableSessionCallbacks(
113       const v8::FunctionCallbackInfo<v8::Value>& args);
114   static void EnableHelloParser(
115       const v8::FunctionCallbackInfo<v8::Value>& args);
116
117 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
118   static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
119   static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
120   static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
121 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
122
123   crypto::SecureContext* sc_;
124   v8::Persistent<v8::Object> sc_handle_;
125   BIO* enc_in_;
126   BIO* enc_out_;
127   NodeBIO* clear_in_;
128   uv_write_t write_req_;
129   size_t write_size_;
130   size_t write_queue_size_;
131   QUEUE write_item_queue_;
132   WriteItem* pending_write_item_;
133   bool started_;
134   bool established_;
135   bool shutdown_;
136
137 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
138   v8::Persistent<v8::Value> sni_context_;
139 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
140 };
141
142 }  // namespace node
143
144 #endif  // SRC_TLS_WRAP_H_