Skip to content

Latest commit

 

History

History
176 lines (152 loc) · 5.32 KB

tls_parser.h

File metadata and controls

176 lines (152 loc) · 5.32 KB
 
1
2
3
4
5
6
7
8
9
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef tls_parser_h_
#define tls_parser_h_
Mar 3, 2015
Mar 3, 2015
10
11
#include <cstdint>
#include <cstring>
Aug 16, 2016
Aug 16, 2016
12
#include <memory>
Aug 17, 2015
Aug 17, 2015
13
14
15
#if defined(WIN32) || defined(WIN64)
#include <winsock2.h>
#else
Aug 17, 2015
Aug 17, 2015
17
#endif
May 4, 2017
May 4, 2017
19
#include "sslt.h"
Mar 3, 2015
Mar 3, 2015
21
22
namespace nss_test {
Mar 20, 2015
Mar 20, 2015
23
24
const uint8_t kTlsHandshakeClientHello = 1;
const uint8_t kTlsHandshakeServerHello = 2;
Mar 16, 2017
Mar 16, 2017
25
const uint8_t kTlsHandshakeNewSessionTicket = 4;
Sep 12, 2016
Sep 12, 2016
26
const uint8_t kTlsHandshakeHelloRetryRequest = 6;
May 19, 2016
May 19, 2016
27
const uint8_t kTlsHandshakeEncryptedExtensions = 8;
Mar 20, 2015
Mar 20, 2015
28
29
const uint8_t kTlsHandshakeCertificate = 11;
const uint8_t kTlsHandshakeServerKeyExchange = 12;
Jan 31, 2017
Jan 31, 2017
30
const uint8_t kTlsHandshakeCertificateRequest = 13;
Sep 9, 2015
Sep 9, 2015
31
const uint8_t kTlsHandshakeCertificateVerify = 15;
Jan 10, 2015
Jan 10, 2015
32
const uint8_t kTlsHandshakeClientKeyExchange = 16;
Sep 9, 2015
Sep 9, 2015
33
const uint8_t kTlsHandshakeFinished = 20;
Dec 2, 2019
Dec 2, 2019
34
const uint8_t kTlsHandshakeKeyUpdate = 24;
Mar 3, 2015
Mar 3, 2015
36
37
38
const uint8_t kTlsAlertWarning = 1;
const uint8_t kTlsAlertFatal = 2;
Mar 20, 2017
Mar 20, 2017
39
const uint8_t kTlsAlertCloseNotify = 0;
Mar 20, 2015
Mar 20, 2015
40
const uint8_t kTlsAlertUnexpectedMessage = 10;
Jan 10, 2015
Jan 10, 2015
41
const uint8_t kTlsAlertBadRecordMac = 20;
Mar 20, 2017
Mar 20, 2017
42
const uint8_t kTlsAlertRecordOverflow = 22;
Mar 20, 2015
Mar 20, 2015
43
const uint8_t kTlsAlertHandshakeFailure = 40;
Jan 21, 2018
Jan 21, 2018
44
const uint8_t kTlsAlertBadCertificate = 42;
May 1, 2018
May 1, 2018
45
46
const uint8_t kTlsAlertCertificateRevoked = 44;
const uint8_t kTlsAlertCertificateExpired = 45;
Mar 20, 2015
Mar 20, 2015
47
48
const uint8_t kTlsAlertIllegalParameter = 47;
const uint8_t kTlsAlertDecodeError = 50;
Aug 15, 2016
Aug 15, 2016
49
const uint8_t kTlsAlertDecryptError = 51;
Mar 20, 2017
Mar 20, 2017
50
const uint8_t kTlsAlertProtocolVersion = 70;
Jul 15, 2019
Jul 15, 2019
51
const uint8_t kTlsAlertInsufficientSecurity = 71;
Jul 16, 2017
Jul 16, 2017
52
const uint8_t kTlsAlertInternalError = 80;
Mar 20, 2017
Mar 20, 2017
53
const uint8_t kTlsAlertInappropriateFallback = 86;
Aug 15, 2016
Aug 15, 2016
54
const uint8_t kTlsAlertMissingExtension = 109;
Mar 20, 2015
Mar 20, 2015
55
const uint8_t kTlsAlertUnsupportedExtension = 110;
Sep 22, 2016
Sep 22, 2016
56
const uint8_t kTlsAlertUnrecognizedName = 112;
Apr 8, 2019
Apr 8, 2019
57
const uint8_t kTlsAlertCertificateRequired = 116;
Mar 20, 2015
Mar 20, 2015
58
const uint8_t kTlsAlertNoApplicationProtocol = 120;
Mar 3, 2015
Mar 3, 2015
59
60
const uint8_t kTlsFakeChangeCipherSpec[] = {
Jun 26, 2018
Jun 26, 2018
61
ssl_ct_change_cipher_spec, // Type
Aug 16, 2016
Aug 16, 2016
62
63
64
65
66
67
68
69
70
71
72
73
74
0xfe,
0xff, // Version
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x10, // Fictitious sequence #
0x00,
0x01, // Length
0x01 // Value
Jan 6, 2020
Jan 6, 2020
77
78
79
80
81
const uint8_t kCtDtlsCiphertext = 0x20;
const uint8_t kCtDtlsCiphertextMask = 0xE0;
const uint8_t kCtDtlsCiphertext16bSeqno = 0x08;
const uint8_t kCtDtlsCiphertextLengthPresent = 0x04;
Sep 22, 2016
Sep 22, 2016
82
83
84
85
86
static const uint8_t kTls13PskKe = 0;
static const uint8_t kTls13PskDhKe = 1;
static const uint8_t kTls13PskAuth = 0;
static const uint8_t kTls13PskSignAuth = 1;
Apr 30, 2017
Apr 30, 2017
87
88
89
90
inline std::ostream& operator<<(std::ostream& os, SSLProtocolVariant v) {
return os << ((v == ssl_variant_stream) ? "TLS" : "DTLS");
}
Feb 17, 2019
Feb 17, 2019
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
inline std::ostream& operator<<(std::ostream& os, SSLContentType v) {
switch (v) {
case ssl_ct_change_cipher_spec:
return os << "CCS";
case ssl_ct_alert:
return os << "alert";
case ssl_ct_handshake:
return os << "handshake";
case ssl_ct_application_data:
return os << "application data";
case ssl_ct_ack:
return os << "ack";
}
return os << "UNKNOWN content type " << static_cast<int>(v);
}
inline std::ostream& operator<<(std::ostream& os, SSLSecretDirection v) {
switch (v) {
case ssl_secret_read:
return os << "read";
case ssl_secret_write:
return os << "write";
}
return os << "UNKNOWN secret direction " << static_cast<int>(v);
}
Aug 16, 2016
Aug 16, 2016
117
inline bool IsDtls(uint16_t version) { return (version & 0x8000) == 0x8000; }
Mar 3, 2015
Mar 3, 2015
118
119
120
inline uint16_t NormalizeTlsVersion(uint16_t version) {
if (version == 0xfeff) {
Aug 16, 2016
Aug 16, 2016
121
return 0x0302; // special: DTLS 1.0 == TLS 1.1
Mar 3, 2015
Mar 3, 2015
122
123
124
125
126
127
128
}
if (IsDtls(version)) {
return (version ^ 0xffff) + 0x0201;
}
return version;
}
Aug 16, 2016
Aug 16, 2016
129
inline uint16_t TlsVersionToDtlsVersion(uint16_t version) {
May 19, 2016
May 19, 2016
130
131
132
if (version == 0x0302) {
return 0xfeff;
}
Oct 1, 2016
Oct 1, 2016
133
134
135
if (version == 0x0304) {
return version;
}
May 19, 2016
May 19, 2016
136
137
138
return 0xffff - version + 0x0201;
}
Sep 22, 2016
Sep 22, 2016
139
inline size_t WriteVariable(DataBuffer* target, size_t index,
Sep 22, 2016
Sep 22, 2016
140
const DataBuffer& buf, size_t len_size) {
Sep 22, 2016
Sep 22, 2016
141
142
index = target->Write(index, static_cast<uint32_t>(buf.len()), len_size);
return target->Write(index, buf.data(), buf.len());
Mar 3, 2015
Mar 3, 2015
143
144
}
145
146
class TlsParser {
public:
Aug 16, 2016
Aug 16, 2016
147
148
TlsParser(const uint8_t* data, size_t len) : buffer_(data, len), offset_(0) {}
explicit TlsParser(const DataBuffer& buf) : buffer_(buf), offset_(0) {}
Mar 3, 2015
Mar 3, 2015
150
bool Read(uint8_t* val);
151
// Read an integral type of specified width.
Mar 3, 2015
Mar 3, 2015
152
153
154
bool Read(uint32_t* val, size_t size);
// Reads len bytes into dest buffer, overwriting it.
bool Read(DataBuffer* dest, size_t len);
Mar 17, 2018
Mar 17, 2018
155
bool ReadFromMark(DataBuffer* val, size_t len, size_t mark);
Mar 3, 2015
Mar 3, 2015
156
157
158
// Reads bytes into dest buffer, overwriting it. The number of bytes is
// determined by reading from len_size bytes from the stream first.
bool ReadVariable(DataBuffer* dest, size_t len_size);
Mar 3, 2015
Mar 3, 2015
160
161
bool Skip(size_t len);
bool SkipVariable(size_t len_size);
Mar 3, 2015
Mar 3, 2015
163
size_t consumed() const { return offset_; }
164
165
166
167
size_t remaining() const { return buffer_.len() - offset_; }
private:
void consume(size_t len) { offset_ += len; }
Mar 3, 2015
Mar 3, 2015
168
const uint8_t* ptr() const { return buffer_.data() + offset_; }
169
170
171
172
173
DataBuffer buffer_;
size_t offset_;
};
Aug 16, 2016
Aug 16, 2016
174
} // namespace nss_test