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 |
|
|
#include "crypto/crypto_clienthello.h" // NOLINT(build/include_inline) |
23 |
|
|
#include "crypto/crypto_clienthello-inl.h" |
24 |
|
|
|
25 |
|
|
namespace node { |
26 |
|
|
namespace crypto { |
27 |
|
21 |
void ClientHelloParser::Parse(const uint8_t* data, size_t avail) { |
28 |
✓✗✗✗
|
21 |
switch (state_) { |
29 |
|
21 |
case kWaiting: |
30 |
✗✓ |
21 |
if (!ParseRecordHeader(data, avail)) |
31 |
|
|
break; |
32 |
|
|
// Fall through |
33 |
|
|
case kTLSHeader: |
34 |
|
21 |
ParseHeader(data, avail); |
35 |
|
21 |
break; |
36 |
|
|
case kPaused: |
37 |
|
|
// Just nop |
38 |
|
|
case kEnded: |
39 |
|
|
// Already ended, just ignore it |
40 |
|
|
break; |
41 |
|
|
default: |
42 |
|
|
break; |
43 |
|
|
} |
44 |
|
21 |
} |
45 |
|
|
|
46 |
|
|
|
47 |
|
21 |
bool ClientHelloParser::ParseRecordHeader(const uint8_t* data, size_t avail) { |
48 |
|
|
// >= 5 bytes for header parsing |
49 |
✗✓ |
21 |
if (avail < 5) |
50 |
|
|
return false; |
51 |
|
|
|
52 |
✓✗ |
21 |
if (data[0] == kChangeCipherSpec || |
53 |
✓✗ |
21 |
data[0] == kAlert || |
54 |
✗✓ |
21 |
data[0] == kHandshake || |
55 |
|
|
data[0] == kApplicationData) { |
56 |
|
21 |
frame_len_ = (data[3] << 8) + data[4]; |
57 |
|
21 |
state_ = kTLSHeader; |
58 |
|
21 |
body_offset_ = 5; |
59 |
|
|
} else { |
60 |
|
|
End(); |
61 |
|
|
return false; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
// Sanity check (too big frame, or too small) |
65 |
|
|
// Let OpenSSL handle it |
66 |
✗✓ |
21 |
if (frame_len_ >= kMaxTLSFrameLen) { |
67 |
|
|
End(); |
68 |
|
|
return false; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
21 |
return true; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
|
75 |
|
21 |
void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) { |
76 |
|
|
ClientHello hello; |
77 |
|
|
|
78 |
|
|
// We need at least six bytes (one byte for kClientHello, three bytes for the |
79 |
|
|
// length of the handshake message, and two bytes for the protocol version). |
80 |
|
|
// If the client sent a frame that suggests a smaller ClientHello, give up. |
81 |
✓✓ |
21 |
if (frame_len_ < 6) return End(); |
82 |
|
|
|
83 |
|
|
// >= 5 + frame size bytes for frame parsing |
84 |
✗✓ |
20 |
if (body_offset_ + frame_len_ > avail) |
85 |
|
|
return; |
86 |
|
|
|
87 |
|
|
// Check hello protocol version. Protocol tuples that we know about: |
88 |
|
|
// |
89 |
|
|
// (3,1) TLS v1.0 |
90 |
|
|
// (3,2) TLS v1.1 |
91 |
|
|
// (3,3) TLS v1.2 |
92 |
|
|
// |
93 |
|
|
// Note that TLS v1.3 uses a TLS v1.2 handshake so requires no specific |
94 |
|
|
// support here. |
95 |
✓✗ |
20 |
if (data[body_offset_ + 4] != 0x03 || |
96 |
✓✗ |
20 |
data[body_offset_ + 5] < 0x01 || |
97 |
✗✓ |
20 |
data[body_offset_ + 5] > 0x03) { |
98 |
|
|
return End(); |
99 |
|
|
} |
100 |
|
|
|
101 |
✓✗ |
20 |
if (data[body_offset_] == kClientHello) { |
102 |
✓✗ |
20 |
if (state_ == kTLSHeader) { |
103 |
✗✓ |
20 |
if (!ParseTLSClientHello(data, avail)) |
104 |
|
|
return End(); |
105 |
|
|
} else { |
106 |
|
|
// We couldn't get here, but whatever |
107 |
|
|
return End(); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
// Check if we overflowed (do not reply with any private data) |
111 |
✓✗ |
20 |
if (session_id_ == nullptr || |
112 |
✓✗ |
20 |
session_size_ > 32 || |
113 |
✗✓ |
20 |
session_id_ + session_size_ > data + avail) { |
114 |
|
|
return End(); |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
20 |
state_ = kPaused; |
119 |
|
20 |
hello.session_id_ = session_id_; |
120 |
|
20 |
hello.session_size_ = session_size_; |
121 |
✓✓✓✓
|
20 |
hello.has_ticket_ = tls_ticket_ != nullptr && tls_ticket_size_ != 0; |
122 |
|
20 |
hello.servername_ = servername_; |
123 |
|
20 |
hello.servername_size_ = static_cast<uint8_t>(servername_size_); |
124 |
|
20 |
onhello_cb_(cb_arg_, hello); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
108 |
void ClientHelloParser::ParseExtension(const uint16_t type, |
129 |
|
|
const uint8_t* data, |
130 |
|
|
size_t len) { |
131 |
|
|
// NOTE: In case of anything we're just returning back, ignoring the problem. |
132 |
|
|
// That's because we're heavily relying on OpenSSL to solve any problem with |
133 |
|
|
// incoming data. |
134 |
✓✓✓ |
108 |
switch (type) { |
135 |
|
18 |
case kServerName: |
136 |
|
|
{ |
137 |
✗✓ |
18 |
if (len < 2) |
138 |
|
|
return; |
139 |
|
18 |
uint32_t server_names_len = (data[0] << 8) + data[1]; |
140 |
✗✓ |
18 |
if (server_names_len + 2 > len) |
141 |
|
|
return; |
142 |
✓✓ |
36 |
for (size_t offset = 2; offset < 2 + server_names_len; ) { |
143 |
✗✓ |
18 |
if (offset + 3 > len) |
144 |
|
|
return; |
145 |
|
18 |
uint8_t name_type = data[offset]; |
146 |
✗✓ |
18 |
if (name_type != kServernameHostname) |
147 |
|
|
return; |
148 |
|
18 |
uint16_t name_len = (data[offset + 1] << 8) + data[offset + 2]; |
149 |
|
18 |
offset += 3; |
150 |
✗✓ |
18 |
if (offset + name_len > len) |
151 |
|
|
return; |
152 |
|
18 |
servername_ = data + offset; |
153 |
|
18 |
servername_size_ = name_len; |
154 |
|
18 |
offset += name_len; |
155 |
|
18 |
} |
156 |
|
|
} |
157 |
|
18 |
break; |
158 |
|
8 |
case kTLSSessionTicket: |
159 |
|
8 |
tls_ticket_size_ = len; |
160 |
|
8 |
tls_ticket_ = data + len; |
161 |
|
8 |
break; |
162 |
|
82 |
default: |
163 |
|
|
// Ignore |
164 |
|
82 |
break; |
165 |
|
|
} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
20 |
bool ClientHelloParser::ParseTLSClientHello(const uint8_t* data, size_t avail) { |
170 |
|
|
const uint8_t* body; |
171 |
|
|
|
172 |
|
|
// Skip frame header, hello header, protocol version and random data |
173 |
|
20 |
size_t session_offset = body_offset_ + 4 + 2 + 32; |
174 |
|
|
|
175 |
✗✓ |
20 |
if (session_offset + 1 >= avail) |
176 |
|
|
return false; |
177 |
|
|
|
178 |
|
20 |
body = data + session_offset; |
179 |
|
20 |
session_size_ = *body; |
180 |
|
20 |
session_id_ = body + 1; |
181 |
|
|
|
182 |
|
20 |
size_t cipher_offset = session_offset + 1 + session_size_; |
183 |
|
|
|
184 |
|
|
// Session OOB failure |
185 |
✗✓ |
20 |
if (cipher_offset + 1 >= avail) |
186 |
|
|
return false; |
187 |
|
|
|
188 |
|
20 |
uint16_t cipher_len = |
189 |
|
20 |
(data[cipher_offset] << 8) + data[cipher_offset + 1]; |
190 |
|
20 |
size_t comp_offset = cipher_offset + 2 + cipher_len; |
191 |
|
|
|
192 |
|
|
// Cipher OOB failure |
193 |
✗✓ |
20 |
if (comp_offset >= avail) |
194 |
|
|
return false; |
195 |
|
|
|
196 |
|
20 |
uint8_t comp_len = data[comp_offset]; |
197 |
|
20 |
size_t extension_offset = comp_offset + 1 + comp_len; |
198 |
|
|
|
199 |
|
|
// Compression OOB failure |
200 |
✗✓ |
20 |
if (extension_offset > avail) |
201 |
|
|
return false; |
202 |
|
|
|
203 |
|
|
// No extensions present |
204 |
✗✓ |
20 |
if (extension_offset == avail) |
205 |
|
|
return true; |
206 |
|
|
|
207 |
|
20 |
size_t ext_off = extension_offset + 2; |
208 |
|
|
|
209 |
|
|
// Parse known extensions |
210 |
✓✓ |
128 |
while (ext_off < avail) { |
211 |
|
|
// Extension OOB |
212 |
✗✓ |
108 |
if (ext_off + 4 > avail) |
213 |
|
|
return false; |
214 |
|
|
|
215 |
|
108 |
uint16_t ext_type = (data[ext_off] << 8) + data[ext_off + 1]; |
216 |
|
108 |
uint16_t ext_len = (data[ext_off + 2] << 8) + data[ext_off + 3]; |
217 |
|
108 |
ext_off += 4; |
218 |
|
|
|
219 |
|
|
// Extension OOB |
220 |
✗✓ |
108 |
if (ext_off + ext_len > avail) |
221 |
|
|
return false; |
222 |
|
|
|
223 |
|
108 |
ParseExtension(ext_type, |
224 |
|
|
data + ext_off, |
225 |
|
|
ext_len); |
226 |
|
|
|
227 |
|
108 |
ext_off += ext_len; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
// Extensions OOB failure |
231 |
✗✓ |
20 |
if (ext_off > avail) |
232 |
|
|
return false; |
233 |
|
|
|
234 |
|
20 |
return true; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
} // namespace crypto |
238 |
|
|
} // namespace node |