src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / node_crypto_clienthello.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_NODE_CRYPTO_CLIENTHELLO_H_
23 #define SRC_NODE_CRYPTO_CLIENTHELLO_H_
24
25 #include "node.h"
26
27 #include <stddef.h>  // size_t
28 #include <stdlib.h>  // NULL
29
30 namespace node {
31
32 class ClientHelloParser {
33  public:
34   ClientHelloParser() : state_(kEnded),
35                         onhello_cb_(NULL),
36                         onend_cb_(NULL),
37                         cb_arg_(NULL) {
38     Reset();
39   }
40
41   class ClientHello {
42    public:
43     ClientHello() {
44     }
45
46     inline uint8_t session_size() const { return session_size_; }
47     inline const uint8_t* session_id() const { return session_id_; }
48     inline bool has_ticket() const { return has_ticket_; }
49     inline uint8_t servername_size() const { return servername_size_; }
50     inline const uint8_t* servername() const { return servername_; }
51
52    private:
53     uint8_t session_size_;
54     const uint8_t* session_id_;
55     bool has_ticket_;
56     uint8_t servername_size_;
57     const uint8_t* servername_;
58
59     friend class ClientHelloParser;
60   };
61
62   typedef void (*OnHelloCb)(void* arg, const ClientHello& hello);
63   typedef void (*OnEndCb)(void* arg);
64
65   void Parse(const uint8_t* data, size_t avail);
66
67   inline void Reset();
68   inline void Start(OnHelloCb onhello_cb, OnEndCb onend_cb, void* onend_arg);
69   inline void End();
70   inline bool IsPaused() const;
71   inline bool IsEnded() const;
72
73  private:
74   static const uint8_t kSSL2TwoByteHeaderBit = 0x80;
75   static const uint8_t kSSL2HeaderMask = 0x3f;
76   static const size_t kMaxTLSFrameLen = 16 * 1024 + 5;
77   static const size_t kMaxSSLExFrameLen = 32 * 1024;
78   static const uint8_t kServernameHostname = 0;
79
80   enum ParseState {
81     kWaiting,
82     kTLSHeader,
83     kSSL2Header,
84     kPaused,
85     kEnded
86   };
87
88   enum FrameType {
89     kChangeCipherSpec = 20,
90     kAlert = 21,
91     kHandshake = 22,
92     kApplicationData = 23,
93     kOther = 255
94   };
95
96   enum HandshakeType {
97     kClientHello = 1
98   };
99
100   enum ExtensionType {
101     kServerName = 0,
102     kTLSSessionTicket = 35
103   };
104
105   bool ParseRecordHeader(const uint8_t* data, size_t avail);
106   void ParseHeader(const uint8_t* data, size_t avail);
107   void ParseExtension(ExtensionType type,
108                       const uint8_t* data,
109                       size_t len);
110   bool ParseTLSClientHello(const uint8_t* data, size_t avail);
111 #ifdef OPENSSL_NO_SSL2
112   bool ParseSSL2ClientHello(const uint8_t* data, size_t avail);
113 #endif  // OPENSSL_NO_SSL2
114
115   ParseState state_;
116   OnHelloCb onhello_cb_;
117   OnEndCb onend_cb_;
118   void* cb_arg_;
119   size_t frame_len_;
120   size_t body_offset_;
121   size_t extension_offset_;
122   uint8_t session_size_;
123   const uint8_t* session_id_;
124   uint16_t servername_size_;
125   const uint8_t* servername_;
126   uint16_t tls_ticket_size_;
127   const uint8_t* tls_ticket_;
128 };
129
130 }  // namespace node
131
132 #endif  // SRC_NODE_CRYPTO_CLIENTHELLO_H_