GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/crypto/crypto_clienthello.cc Lines: 0 107 0.0 %
Date: 2021-02-19 04:08:54 Branches: 0 75 0.0 %

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
void ClientHelloParser::Parse(const uint8_t* data, size_t avail) {
28
  switch (state_) {
29
    case kWaiting:
30
      if (!ParseRecordHeader(data, avail))
31
        break;
32
      // Fall through
33
    case kTLSHeader:
34
      ParseHeader(data, avail);
35
      break;
36
    case kPaused:
37
      // Just nop
38
    case kEnded:
39
      // Already ended, just ignore it
40
      break;
41
    default:
42
      break;
43
  }
44
}
45
46
47
bool ClientHelloParser::ParseRecordHeader(const uint8_t* data, size_t avail) {
48
  // >= 5 bytes for header parsing
49
  if (avail < 5)
50
    return false;
51
52
  if (data[0] == kChangeCipherSpec ||
53
      data[0] == kAlert ||
54
      data[0] == kHandshake ||
55
      data[0] == kApplicationData) {
56
    frame_len_ = (data[3] << 8) + data[4];
57
    state_ = kTLSHeader;
58
    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
  if (frame_len_ >= kMaxTLSFrameLen) {
67
    End();
68
    return false;
69
  }
70
71
  return true;
72
}
73
74
75
void ClientHelloParser::ParseHeader(const uint8_t* data, size_t avail) {
76
  ClientHello hello;
77
78
  // >= 5 + frame size bytes for frame parsing
79
  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
  if (data[body_offset_ + 4] != 0x03 ||
91
      data[body_offset_ + 5] < 0x01 ||
92
      data[body_offset_ + 5] > 0x03) {
93
    return End();
94
  }
95
96
  if (data[body_offset_] == kClientHello) {
97
    if (state_ == kTLSHeader) {
98
      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
    if (session_id_ == nullptr ||
107
        session_size_ > 32 ||
108
        session_id_ + session_size_ > data + avail) {
109
      return End();
110
    }
111
  }
112
113
  state_ = kPaused;
114
  hello.session_id_ = session_id_;
115
  hello.session_size_ = session_size_;
116
  hello.has_ticket_ = tls_ticket_ != nullptr && tls_ticket_size_ != 0;
117
  hello.servername_ = servername_;
118
  hello.servername_size_ = static_cast<uint8_t>(servername_size_);
119
  onhello_cb_(cb_arg_, hello);
120
}
121
122
123
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
  switch (type) {
130
    case kServerName:
131
      {
132
        if (len < 2)
133
          return;
134
        uint32_t server_names_len = (data[0] << 8) + data[1];
135
        if (server_names_len + 2 > len)
136
          return;
137
        for (size_t offset = 2; offset < 2 + server_names_len; ) {
138
          if (offset + 3 > len)
139
            return;
140
          uint8_t name_type = data[offset];
141
          if (name_type != kServernameHostname)
142
            return;
143
          uint16_t name_len = (data[offset + 1] << 8) + data[offset + 2];
144
          offset += 3;
145
          if (offset + name_len > len)
146
            return;
147
          servername_ = data + offset;
148
          servername_size_ = name_len;
149
          offset += name_len;
150
        }
151
      }
152
      break;
153
    case kTLSSessionTicket:
154
      tls_ticket_size_ = len;
155
      tls_ticket_ = data + len;
156
      break;
157
    default:
158
      // Ignore
159
      break;
160
  }
161
}
162
163
164
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
  size_t session_offset = body_offset_ + 4 + 2 + 32;
169
170
  if (session_offset + 1 >= avail)
171
    return false;
172
173
  body = data + session_offset;
174
  session_size_ = *body;
175
  session_id_ = body + 1;
176
177
  size_t cipher_offset = session_offset + 1 + session_size_;
178
179
  // Session OOB failure
180
  if (cipher_offset + 1 >= avail)
181
    return false;
182
183
  uint16_t cipher_len =
184
      (data[cipher_offset] << 8) + data[cipher_offset + 1];
185
  size_t comp_offset = cipher_offset + 2 + cipher_len;
186
187
  // Cipher OOB failure
188
  if (comp_offset >= avail)
189
    return false;
190
191
  uint8_t comp_len = data[comp_offset];
192
  size_t extension_offset = comp_offset + 1 + comp_len;
193
194
  // Compression OOB failure
195
  if (extension_offset > avail)
196
    return false;
197
198
  // No extensions present
199
  if (extension_offset == avail)
200
    return true;
201
202
  size_t ext_off = extension_offset + 2;
203
204
  // Parse known extensions
205
  while (ext_off < avail) {
206
    // Extension OOB
207
    if (ext_off + 4 > avail)
208
      return false;
209
210
    uint16_t ext_type = (data[ext_off] << 8) + data[ext_off + 1];
211
    uint16_t ext_len = (data[ext_off + 2] << 8) + data[ext_off + 3];
212
    ext_off += 4;
213
214
    // Extension OOB
215
    if (ext_off + ext_len > avail)
216
      return false;
217
218
    ParseExtension(ext_type,
219
                   data + ext_off,
220
                   ext_len);
221
222
    ext_off += ext_len;
223
  }
224
225
  // Extensions OOB failure
226
  if (ext_off > avail)
227
    return false;
228
229
  return true;
230
}
231
232
}  // namespace crypto
233
}  // namespace node