Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_crypto_clienthello.h
Go to the documentation of this file.
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_NODE_CRYPTO_CLIENTHELLO_H_
23 #define SRC_NODE_CRYPTO_CLIENTHELLO_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "node.h"
28 
29 #include <stddef.h> // size_t
30 #include <stdlib.h> // nullptr
31 
32 namespace node {
33 namespace crypto {
34 
35 class ClientHelloParser {
36  public:
37  ClientHelloParser() : state_(kEnded),
38  onhello_cb_(nullptr),
39  onend_cb_(nullptr),
40  cb_arg_(nullptr),
41  session_size_(0),
42  session_id_(nullptr),
43  servername_size_(0),
44  servername_(nullptr),
45  ocsp_request_(0),
46  tls_ticket_size_(0),
47  tls_ticket_(nullptr) {
48  Reset();
49  }
50 
51  class ClientHello {
52  public:
53  inline uint8_t session_size() const { return session_size_; }
54  inline const uint8_t* session_id() const { return session_id_; }
55  inline bool has_ticket() const { return has_ticket_; }
56  inline uint8_t servername_size() const { return servername_size_; }
57  inline const uint8_t* servername() const { return servername_; }
58  inline int ocsp_request() const { return ocsp_request_; }
59 
60  private:
61  uint8_t session_size_;
62  const uint8_t* session_id_;
63  bool has_ticket_;
64  uint8_t servername_size_;
65  const uint8_t* servername_;
66  int ocsp_request_;
67 
68  friend class ClientHelloParser;
69  };
70 
71  typedef void (*OnHelloCb)(void* arg, const ClientHello& hello);
72  typedef void (*OnEndCb)(void* arg);
73 
74  void Parse(const uint8_t* data, size_t avail);
75 
76  inline void Reset();
77  inline void Start(OnHelloCb onhello_cb, OnEndCb onend_cb, void* onend_arg);
78  inline void End();
79  inline bool IsPaused() const;
80  inline bool IsEnded() const;
81 
82  private:
83  static const size_t kMaxTLSFrameLen = 16 * 1024 + 5;
84  static const size_t kMaxSSLExFrameLen = 32 * 1024;
85  static const uint8_t kServernameHostname = 0;
86  static const uint8_t kStatusRequestOCSP = 1;
87  static const size_t kMinStatusRequestSize = 5;
88 
89  enum ParseState {
90  kWaiting,
91  kTLSHeader,
92  kPaused,
93  kEnded
94  };
95 
96  enum FrameType {
97  kChangeCipherSpec = 20,
98  kAlert = 21,
99  kHandshake = 22,
100  kApplicationData = 23,
101  kOther = 255
102  };
103 
104  enum HandshakeType {
105  kClientHello = 1
106  };
107 
108  enum ExtensionType {
109  kServerName = 0,
110  kStatusRequest = 5,
111  kTLSSessionTicket = 35
112  };
113 
114  bool ParseRecordHeader(const uint8_t* data, size_t avail);
115  void ParseHeader(const uint8_t* data, size_t avail);
116  void ParseExtension(const uint16_t type,
117  const uint8_t* data,
118  size_t len);
119  bool ParseTLSClientHello(const uint8_t* data, size_t avail);
120 
121  ParseState state_;
122  OnHelloCb onhello_cb_;
123  OnEndCb onend_cb_;
124  void* cb_arg_;
125  size_t frame_len_;
126  size_t body_offset_;
127  size_t extension_offset_;
128  uint8_t session_size_;
129  const uint8_t* session_id_;
130  uint16_t servername_size_;
131  const uint8_t* servername_;
132  uint8_t ocsp_request_;
133  uint16_t tls_ticket_size_;
134  const uint8_t* tls_ticket_;
135 };
136 
137 } // namespace crypto
138 } // namespace node
139 
140 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
141 
142 #endif // SRC_NODE_CRYPTO_CLIENTHELLO_H_
int len
Definition: cares_wrap.cc:485
union node::cares_wrap::@8::CaresAsyncData::@0 data
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536