GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_clienthello.cc Lines: 86 115 74.8 %
Date: 2022-05-22 04:15:48 Branches: 41 75 54.7 %

Line Branch Exec Source
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
20
void ClientHelloParser::Parse(const uint8_t* data, size_t avail) {
28

20
  switch (state_) {
29
20
    case kWaiting:
30
20
      if (!ParseRecordHeader(data, avail))
31
        break;
32
      // Fall through
33
    case kTLSHeader:
34
20
      ParseHeader(data, avail);
35
20
      break;
36
    case kPaused:
37
      // Just nop
38
    case kEnded:
39
      // Already ended, just ignore it
40
      break;
41
    default:
42
      break;
43
  }
44
20
}
45
46
47
20
bool ClientHelloParser::ParseRecordHeader(const uint8_t* data, size_t avail) {
48
  // >= 5 bytes for header parsing
49
20
  if (avail < 5)
50
    return false;
51
52
20
  if (data[0] == kChangeCipherSpec ||
53
20
      data[0] == kAlert ||
54
20
      data[0] == kHandshake ||
55
      data[0] == kApplicationData) {
56
20
    frame_len_ = (data[3] << 8) + data[4];
57
20
    state_ = kTLSHeader;
58
20
    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
20
  if (frame_len_ >= kMaxTLSFrameLen) {
67
    End();
68
    return false;
69
  }
70
71
20
  return true;
72
}
73
74
75
20
void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) {
76
  ClientHello hello;
77
78
  // >= 5 + frame size bytes for frame parsing
79
20
  if (body_offset_ + frame_len_ > avail)
80
    return;
81
82
  // Check hello protocol version.  Protocol tuples that we know about:
83
  //
84
  // (3,1) TLS v1.0
85
  // (3,2) TLS v1.1
86
  // (3,3) TLS v1.2
87
  //
88
  // Note that TLS v1.3 uses a TLS v1.2 handshake so requires no specific
89
  // support here.
90
20
  if (data[body_offset_ + 4] != 0x03 ||
91
20
      data[body_offset_ + 5] < 0x01 ||
92
20
      data[body_offset_ + 5] > 0x03) {
93
    return End();
94
  }
95
96
20
  if (data[body_offset_] == kClientHello) {
97
20
    if (state_ == kTLSHeader) {
98
20
      if (!ParseTLSClientHello(data, avail))
99
        return End();
100
    } else {
101
      // We couldn't get here, but whatever
102
      return End();
103
    }
104
105
    // Check if we overflowed (do not reply with any private data)
106
20
    if (session_id_ == nullptr ||
107
20
        session_size_ > 32 ||
108
20
        session_id_ + session_size_ > data + avail) {
109
      return End();
110
    }
111
  }
112
113
20
  state_ = kPaused;
114
20
  hello.session_id_ = session_id_;
115
20
  hello.session_size_ = session_size_;
116

20
  hello.has_ticket_ = tls_ticket_ != nullptr && tls_ticket_size_ != 0;
117
20
  hello.servername_ = servername_;
118
20
  hello.servername_size_ = static_cast<uint8_t>(servername_size_);
119
20
  onhello_cb_(cb_arg_, hello);
120
}
121
122
123
108
void ClientHelloParser::ParseExtension(const uint16_t type,
124
                                       const uint8_t* data,
125
                                       size_t len) {
126
  // NOTE: In case of anything we're just returning back, ignoring the problem.
127
  // That's because we're heavily relying on OpenSSL to solve any problem with
128
  // incoming data.
129
108
  switch (type) {
130
18
    case kServerName:
131
      {
132
18
        if (len < 2)
133
          return;
134
18
        uint32_t server_names_len = (data[0] << 8) + data[1];
135
18
        if (server_names_len + 2 > len)
136
          return;
137
36
        for (size_t offset = 2; offset < 2 + server_names_len; ) {
138
18
          if (offset + 3 > len)
139
            return;
140
18
          uint8_t name_type = data[offset];
141
18
          if (name_type != kServernameHostname)
142
            return;
143
18
          uint16_t name_len = (data[offset + 1] << 8) + data[offset + 2];
144
18
          offset += 3;
145
18
          if (offset + name_len > len)
146
            return;
147
18
          servername_ = data + offset;
148
18
          servername_size_ = name_len;
149
18
          offset += name_len;
150
18
        }
151
      }
152
18
      break;
153
8
    case kTLSSessionTicket:
154
8
      tls_ticket_size_ = len;
155
8
      tls_ticket_ = data + len;
156
8
      break;
157
82
    default:
158
      // Ignore
159
82
      break;
160
  }
161
}
162
163
164
20
bool ClientHelloParser::ParseTLSClientHello(const uint8_t* data, size_t avail) {
165
  const uint8_t* body;
166
167
  // Skip frame header, hello header, protocol version and random data
168
20
  size_t session_offset = body_offset_ + 4 + 2 + 32;
169
170
20
  if (session_offset + 1 >= avail)
171
    return false;
172
173
20
  body = data + session_offset;
174
20
  session_size_ = *body;
175
20
  session_id_ = body + 1;
176
177
20
  size_t cipher_offset = session_offset + 1 + session_size_;
178
179
  // Session OOB failure
180
20
  if (cipher_offset + 1 >= avail)
181
    return false;
182
183
20
  uint16_t cipher_len =
184
20
      (data[cipher_offset] << 8) + data[cipher_offset + 1];
185
20
  size_t comp_offset = cipher_offset + 2 + cipher_len;
186
187
  // Cipher OOB failure
188
20
  if (comp_offset >= avail)
189
    return false;
190
191
20
  uint8_t comp_len = data[comp_offset];
192
20
  size_t extension_offset = comp_offset + 1 + comp_len;
193
194
  // Compression OOB failure
195
20
  if (extension_offset > avail)
196
    return false;
197
198
  // No extensions present
199
20
  if (extension_offset == avail)
200
    return true;
201
202
20
  size_t ext_off = extension_offset + 2;
203
204
  // Parse known extensions
205
128
  while (ext_off < avail) {
206
    // Extension OOB
207
108
    if (ext_off + 4 > avail)
208
      return false;
209
210
108
    uint16_t ext_type = (data[ext_off] << 8) + data[ext_off + 1];
211
108
    uint16_t ext_len = (data[ext_off + 2] << 8) + data[ext_off + 3];
212
108
    ext_off += 4;
213
214
    // Extension OOB
215
108
    if (ext_off + ext_len > avail)
216
      return false;
217
218
108
    ParseExtension(ext_type,
219
                   data + ext_off,
220
                   ext_len);
221
222
108
    ext_off += ext_len;
223
  }
224
225
  // Extensions OOB failure
226
20
  if (ext_off > avail)
227
    return false;
228
229
20
  return true;
230
}
231
232
}  // namespace crypto
233
}  // namespace node