GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/tls_wrap.h Lines: 9 10 90.0 %
Date: 2016-11-30 Branches: 4 4 100.0 %

Line Branch Exec Source
1
#ifndef SRC_TLS_WRAP_H_
2
#define SRC_TLS_WRAP_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "node.h"
7
#include "node_crypto.h"  // SSLWrap
8
9
#include "async-wrap.h"
10
#include "env.h"
11
#include "stream_wrap.h"
12
#include "util.h"
13
#include "v8.h"
14
15
#include <openssl/ssl.h>
16
17
namespace node {
18
19
// Forward-declarations
20
class NodeBIO;
21
class WriteWrap;
22
namespace crypto {
23
class SecureContext;
24
}
25
26
class TLSWrap : public AsyncWrap,
27
                public crypto::SSLWrap<TLSWrap>,
28
                public StreamBase {
29
 public:
30
  ~TLSWrap() override;
31
32
  static void Initialize(v8::Local<v8::Object> target,
33
                         v8::Local<v8::Value> unused,
34
                         v8::Local<v8::Context> context);
35
36
  void* Cast() override;
37
  int GetFD() override;
38
  bool IsAlive() override;
39
  bool IsClosing() override;
40
41
  // JavaScript functions
42
  int ReadStart() override;
43
  int ReadStop() override;
44
45
  int DoShutdown(ShutdownWrap* req_wrap) override;
46
  int DoWrite(WriteWrap* w,
47
              uv_buf_t* bufs,
48
              size_t count,
49
              uv_stream_t* send_handle) override;
50
  const char* Error() const override;
51
  void ClearError() override;
52
53
  void NewSessionDoneCb();
54
55
  size_t self_size() const override { return sizeof(*this); }
56
57
 protected:
58
  static const int kClearOutChunkSize = 16384;
59
60
  // Maximum number of bytes for hello parser
61
  static const int kMaxHelloLength = 16384;
62
63
  // Usual ServerHello + Certificate size
64
  static const int kInitialClientBufferLength = 4096;
65
66
  // Maximum number of buffers passed to uv_write()
67
  static const int kSimultaneousBufferCount = 10;
68
69
  // Write callback queue's item
70
  class WriteItem {
71
   public:
72
1818
    explicit WriteItem(WriteWrap* w) : w_(w) {
73
    }
74
1814
    ~WriteItem() {
75
907
      w_ = nullptr;
76
    }
77
78
    WriteWrap* w_;
79
    ListNode<WriteItem> member_;
80
  };
81
82
  TLSWrap(Environment* env,
83
          Kind kind,
84
          StreamBase* stream,
85
          crypto::SecureContext* sc);
86
87
  static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
88
  void InitSSL();
89
  void EncOut();
90
  static void EncOutCb(WriteWrap* req_wrap, int status);
91
  bool ClearIn();
92
  void ClearOut();
93
  void MakePending();
94
  bool InvokeQueued(int status, const char* error_str = nullptr);
95
96
2121
  inline void Cycle() {
97
    // Prevent recursion
98
2121
    if (++cycle_depth_ > 1)
99
      return;
100
101
6344
    for (; cycle_depth_ > 0; cycle_depth_--) {
102
2121
      ClearIn();
103
2121
      ClearOut();
104
2118
      EncOut();
105
    }
106
  }
107
108
  AsyncWrap* GetAsyncWrap() override;
109
  bool IsIPCPipe() override;
110
111
  // Resource implementation
112
  static void OnAfterWriteImpl(WriteWrap* w, void* ctx);
113
  static void OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx);
114
  static void OnReadImpl(ssize_t nread,
115
                         const uv_buf_t* buf,
116
                         uv_handle_type pending,
117
                         void* ctx);
118
  static void OnAfterWriteSelf(WriteWrap* w, void* ctx);
119
  static void OnAllocSelf(size_t size, uv_buf_t* buf, void* ctx);
120
  static void OnReadSelf(ssize_t nread,
121
                         const uv_buf_t* buf,
122
                         uv_handle_type pending,
123
                         void* ctx);
124
125
  void DoRead(ssize_t nread, const uv_buf_t* buf, uv_handle_type pending);
126
127
  // If |msg| is not nullptr, caller is responsible for calling `delete[] *msg`.
128
  v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
129
130
  static void OnClientHelloParseEnd(void* arg);
131
  static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
132
  static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
133
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
134
  static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
135
  static void EnableSessionCallbacks(
136
      const v8::FunctionCallbackInfo<v8::Value>& args);
137
  static void EnableCertCb(
138
      const v8::FunctionCallbackInfo<v8::Value>& args);
139
  static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
140
141
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
142
  static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
143
  static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
144
  static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
145
#endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
146
147
  crypto::SecureContext* sc_;
148
  StreamBase* stream_;
149
  BIO* enc_in_;
150
  BIO* enc_out_;
151
  NodeBIO* clear_in_;
152
  size_t write_size_;
153
  typedef ListHead<WriteItem, &WriteItem::member_> WriteItemList;
154
  WriteItemList write_item_queue_;
155
  WriteItemList pending_write_items_;
156
  bool started_;
157
  bool established_;
158
  bool shutdown_;
159
  const char* error_;
160
  int cycle_depth_;
161
162
  // If true - delivered EOF to the js-land, either after `close_notify`, or
163
  // after the `UV_EOF` on socket.
164
  bool eof_;
165
};
166
167
}  // namespace node
168
169
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
170
171
#endif  // SRC_TLS_WRAP_H_