TLS13: fix length of Finished message
[metze/wireshark/wip.git] / epan / dissectors / packet-ssl-utils.c
1 /* packet-ssl-utils.c
2  * ssl manipulation functions
3  * By Paolo Abeni <paolo.abeni@email.com>
4  *
5  * Copyright (c) 2013, Hauke Mehrtens <hauke@hauke-m.de>
6  * Copyright (c) 2014, Peter Wu <peter@lekensteyn.nl>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #ifdef HAVE_ZLIB
30 #define ZLIB_CONST
31 #include <zlib.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39 #include <epan/addr_resolv.h>
40 #include <epan/ipv6.h>
41 #include <epan/expert.h>
42 #include <epan/asn1.h>
43 #include <epan/proto_data.h>
44
45 #include <wsutil/filesystem.h>
46 #include <wsutil/file_util.h>
47 #include <wsutil/str_util.h>
48 #include <wsutil/report_err.h>
49 #include <wsutil/pint.h>
50 #include <wsutil/strtoi.h>
51 #include <ws_version_info.h>
52 #include "packet-x509af.h"
53 #include "packet-x509if.h"
54 #include "packet-ssl-utils.h"
55 #include "packet-ssl.h"
56 #include "packet-dtls.h"
57 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
58 #include <gnutls/abstract.h>
59 #endif
60 #if GCRYPT_VERSION_NUMBER >= 0x010600 /* 1.6.0 */
61 /* Whether to provide support for authentication in addition to decryption. */
62 #define HAVE_LIBGCRYPT_AEAD
63 #endif
64
65 /* Lookup tables {{{ */
66 const value_string ssl_version_short_names[] = {
67     { SSL_VER_UNKNOWN,      "SSL" },
68     { SSLV2_VERSION,        "SSLv2" },
69     { SSLV3_VERSION,        "SSLv3" },
70     { TLSV1_VERSION,        "TLSv1" },
71     { TLSV1DOT1_VERSION,    "TLSv1.1" },
72     { TLSV1DOT2_VERSION,    "TLSv1.2" },
73     { TLSV1DOT3_VERSION,    "TLSv1.3" },
74     { DTLSV1DOT0_VERSION,   "DTLSv1.0" },
75     { DTLSV1DOT2_VERSION,   "DTLSv1.2" },
76     { DTLSV1DOT0_OPENSSL_VERSION, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
77     { PCT_VERSION,          "PCT" },
78     { 0x00, NULL }
79 };
80
81 const value_string ssl_versions[] = {
82     { SSLV2_VERSION,        "SSL 2.0" },
83     { SSLV3_VERSION,        "SSL 3.0" },
84     { TLSV1_VERSION,        "TLS 1.0" },
85     { TLSV1DOT1_VERSION,    "TLS 1.1" },
86     { TLSV1DOT2_VERSION,    "TLS 1.2" },
87     { TLSV1DOT3_VERSION,    "TLS 1.3" },
88     { 0x7F0E,               "TLS 1.3 (draft 14)" },
89     { 0x7F0F,               "TLS 1.3 (draft 15)" },
90     { 0x7F10,               "TLS 1.3 (draft 16)" },
91     { 0x7F11,               "TLS 1.3 (draft 17)" },
92     { 0x7F12,               "TLS 1.3 (draft 18)" },
93     { DTLSV1DOT0_OPENSSL_VERSION, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
94     { DTLSV1DOT0_VERSION,   "DTLS 1.0" },
95     { DTLSV1DOT2_VERSION,   "DTLS 1.2" },
96     { 0x00, NULL }
97 };
98
99 const value_string ssl_20_msg_types[] = {
100     { SSL2_HND_ERROR,               "Error" },
101     { SSL2_HND_CLIENT_HELLO,        "Client Hello" },
102     { SSL2_HND_CLIENT_MASTER_KEY,   "Client Master Key" },
103     { SSL2_HND_CLIENT_FINISHED,     "Client Finished" },
104     { SSL2_HND_SERVER_HELLO,        "Server Hello" },
105     { SSL2_HND_SERVER_VERIFY,       "Server Verify" },
106     { SSL2_HND_SERVER_FINISHED,     "Server Finished" },
107     { SSL2_HND_REQUEST_CERTIFICATE, "Request Certificate" },
108     { SSL2_HND_CLIENT_CERTIFICATE,  "Client Certificate" },
109     { 0x00, NULL }
110 };
111 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
112 /* Note: sorted by ascending value so value_string-ext can do a binary search */
113 static const value_string ssl_20_cipher_suites[] = {
114     { 0x000000, "TLS_NULL_WITH_NULL_NULL" },
115     { 0x000001, "TLS_RSA_WITH_NULL_MD5" },
116     { 0x000002, "TLS_RSA_WITH_NULL_SHA" },
117     { 0x000003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
118     { 0x000004, "TLS_RSA_WITH_RC4_128_MD5" },
119     { 0x000005, "TLS_RSA_WITH_RC4_128_SHA" },
120     { 0x000006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
121     { 0x000007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
122     { 0x000008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
123     { 0x000009, "TLS_RSA_WITH_DES_CBC_SHA" },
124     { 0x00000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
125     { 0x00000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
126     { 0x00000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
127     { 0x00000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
128     { 0x00000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
129     { 0x00000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
130     { 0x000010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
131     { 0x000011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
132     { 0x000012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
133     { 0x000013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
134     { 0x000014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
135     { 0x000015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
136     { 0x000016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
137     { 0x000017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
138     { 0x000018, "TLS_DH_anon_WITH_RC4_128_MD5" },
139     { 0x000019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
140     { 0x00001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
141     { 0x00001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
142     { 0x00001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
143     { 0x00001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
144 #if 0
145     { 0x00001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
146 #endif
147     /* RFC 2712 */
148     { 0x00001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
149     { 0x00001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
150     { 0x000020, "TLS_KRB5_WITH_RC4_128_SHA" },
151     { 0x000021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
152     { 0x000022, "TLS_KRB5_WITH_DES_CBC_MD5" },
153     { 0x000023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
154     { 0x000024, "TLS_KRB5_WITH_RC4_128_MD5" },
155     { 0x000025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
156     { 0x000026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
157     { 0x000027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
158     { 0x000028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
159     { 0x000029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
160     { 0x00002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
161     { 0x00002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
162     /* RFC 4785 */
163     { 0x00002C, "TLS_PSK_WITH_NULL_SHA" },
164     { 0x00002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
165     { 0x00002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
166     /* RFC 5246 */
167     { 0x00002f, "TLS_RSA_WITH_AES_128_CBC_SHA" },
168     { 0x000030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
169     { 0x000031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
170     { 0x000032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
171     { 0x000033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
172     { 0x000034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
173     { 0x000035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
174     { 0x000036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
175     { 0x000037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
176     { 0x000038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
177     { 0x000039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
178     { 0x00003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
179     { 0x00003B, "TLS_RSA_WITH_NULL_SHA256" },
180     { 0x00003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
181     { 0x00003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
182     { 0x00003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
183     { 0x00003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
184     { 0x000040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
185     { 0x000041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
186     { 0x000042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
187     { 0x000043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
188     { 0x000044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
189     { 0x000045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
190     { 0x000046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
191     { 0x000047, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
192     { 0x000048, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
193     { 0x000049, "TLS_ECDH_ECDSA_WITH_DES_CBC_SHA" },
194     { 0x00004A, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
195     { 0x00004B, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
196     { 0x00004C, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
197     { 0x000060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
198     { 0x000061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
199     { 0x000062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
200     { 0x000063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
201     { 0x000064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
202     { 0x000065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
203     { 0x000066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
204     { 0x000067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
205     { 0x000068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
206     { 0x000069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
207     { 0x00006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
208     { 0x00006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
209     { 0x00006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
210     { 0x00006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
211     /* 0x00,0x6E-83 Unassigned  */
212     { 0x000084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
213     { 0x000085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
214     { 0x000086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
215     { 0x000087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
216     { 0x000088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
217     { 0x000089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
218     /* RFC 4279 */
219     { 0x00008A, "TLS_PSK_WITH_RC4_128_SHA" },
220     { 0x00008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
221     { 0x00008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
222     { 0x00008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
223     { 0x00008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
224     { 0x00008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
225     { 0x000090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
226     { 0x000091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
227     { 0x000092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
228     { 0x000093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
229     { 0x000094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
230     { 0x000095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
231     /* RFC 4162 */
232     { 0x000096, "TLS_RSA_WITH_SEED_CBC_SHA" },
233     { 0x000097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
234     { 0x000098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
235     { 0x000099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
236     { 0x00009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
237     { 0x00009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
238     /* RFC 5288 */
239     { 0x00009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
240     { 0x00009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
241     { 0x00009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
242     { 0x00009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
243     { 0x0000A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
244     { 0x0000A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
245     { 0x0000A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
246     { 0x0000A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
247     { 0x0000A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
248     { 0x0000A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
249     { 0x0000A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
250     { 0x0000A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
251     /* RFC 5487 */
252     { 0x0000A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
253     { 0x0000A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
254     { 0x0000AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
255     { 0x0000AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
256     { 0x0000AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
257     { 0x0000AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
258     { 0x0000AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
259     { 0x0000AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
260     { 0x0000B0, "TLS_PSK_WITH_NULL_SHA256" },
261     { 0x0000B1, "TLS_PSK_WITH_NULL_SHA384" },
262     { 0x0000B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
263     { 0x0000B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
264     { 0x0000B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
265     { 0x0000B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
266     { 0x0000B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
267     { 0x0000B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
268     { 0x0000B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
269     { 0x0000B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
270     /* From RFC 5932 */
271     { 0x0000BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
272     { 0x0000BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
273     { 0x0000BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
274     { 0x0000BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
275     { 0x0000BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
276     { 0x0000BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
277     { 0x0000C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
278     { 0x0000C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
279     { 0x0000C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
280     { 0x0000C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
281     { 0x0000C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
282     { 0x0000C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
283     /* 0x00,0xC6-FE Unassigned  */
284     { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
285     /* 0x01-BF,* Unassigned  */
286     /* From RFC 4492 */
287     { 0x00c001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
288     { 0x00c002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
289     { 0x00c003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
290     { 0x00c004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
291     { 0x00c005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
292     { 0x00c006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
293     { 0x00c007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
294     { 0x00c008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
295     { 0x00c009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
296     { 0x00c00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
297     { 0x00c00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
298     { 0x00c00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
299     { 0x00c00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
300     { 0x00c00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
301     { 0x00c00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
302     { 0x00c010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
303     { 0x00c011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
304     { 0x00c012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
305     { 0x00c013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
306     { 0x00c014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
307     { 0x00c015, "TLS_ECDH_anon_WITH_NULL_SHA" },
308     { 0x00c016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
309     { 0x00c017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
310     { 0x00c018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
311     { 0x00c019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
312     /* RFC 5054 */
313     { 0x00C01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
314     { 0x00C01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
315     { 0x00C01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
316     { 0x00C01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
317     { 0x00C01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
318     { 0x00C01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
319     { 0x00C020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
320     { 0x00C021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
321     { 0x00C022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
322     /* RFC 5589 */
323     { 0x00C023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
324     { 0x00C024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
325     { 0x00C025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
326     { 0x00C026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
327     { 0x00C027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
328     { 0x00C028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
329     { 0x00C029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
330     { 0x00C02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
331     { 0x00C02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
332     { 0x00C02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
333     { 0x00C02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
334     { 0x00C02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
335     { 0x00C02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
336     { 0x00C030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
337     { 0x00C031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
338     { 0x00C032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
339     /* RFC 5489 */
340     { 0x00C033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
341     { 0x00C034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
342     { 0x00C035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
343     { 0x00C036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
344     { 0x00C037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
345     { 0x00C038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
346     { 0x00C039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
347     { 0x00C03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
348     { 0x00C03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
349     /* 0xC0,0x3C-FF Unassigned
350             0xC1-FD,* Unassigned
351             0xFE,0x00-FD Unassigned
352             0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
353             0xFF,0x00-FF Reserved for Private Use [RFC5246]
354             */
355
356     /* old numbers used in the beginning
357      * http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305 */
358     { 0x00CC13, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
359     { 0x00CC14, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
360     { 0x00CC15, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
361
362     /* http://tools.ietf.org/html/draft-ietf-tls-chacha20-poly1305 */
363     { 0x00CCA8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
364     { 0x00CCA9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
365     { 0x00CCAA, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
366     { 0x00CCAB, "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256" },
367     { 0x00CCAC, "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
368     { 0x00CCAD, "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
369     { 0x00CCAE, "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256" },
370
371     /* http://tools.ietf.org/html/draft-josefsson-salsa20-tls */
372     { 0x00E410, "TLS_RSA_WITH_ESTREAM_SALSA20_SHA1" },
373     { 0x00E411, "TLS_RSA_WITH_SALSA20_SHA1" },
374     { 0x00E412, "TLS_ECDHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
375     { 0x00E413, "TLS_ECDHE_RSA_WITH_SALSA20_SHA1" },
376     { 0x00E414, "TLS_ECDHE_ECDSA_WITH_ESTREAM_SALSA20_SHA1" },
377     { 0x00E415, "TLS_ECDHE_ECDSA_WITH_SALSA20_SHA1" },
378     { 0x00E416, "TLS_PSK_WITH_ESTREAM_SALSA20_SHA1" },
379     { 0x00E417, "TLS_PSK_WITH_SALSA20_SHA1" },
380     { 0x00E418, "TLS_ECDHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
381     { 0x00E419, "TLS_ECDHE_PSK_WITH_SALSA20_SHA1" },
382     { 0x00E41A, "TLS_RSA_PSK_WITH_ESTREAM_SALSA20_SHA1" },
383     { 0x00E41B, "TLS_RSA_PSK_WITH_SALSA20_SHA1" },
384     { 0x00E41C, "TLS_DHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
385     { 0x00E41D, "TLS_DHE_PSK_WITH_SALSA20_SHA1" },
386     { 0x00E41E, "TLS_DHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
387     { 0x00E41F, "TLS_DHE_RSA_WITH_SALSA20_SHA1" },
388
389     /* these from http://www.mozilla.org/projects/
390          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
391     { 0x00fefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
392     { 0x00feff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
393     { 0x00ffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
394     { 0x00ffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
395     /* note that ciphersuites of {0x00????} are TLS cipher suites in
396      * a sslv2 client hello message; the ???? above is the two-byte
397      * tls cipher suite id
398      */
399
400     { 0x010080, "SSL2_RC4_128_WITH_MD5" },
401     { 0x020080, "SSL2_RC4_128_EXPORT40_WITH_MD5" },
402     { 0x030080, "SSL2_RC2_128_CBC_WITH_MD5" },
403     { 0x040080, "SSL2_RC2_128_CBC_EXPORT40_WITH_MD5" },
404     { 0x050080, "SSL2_IDEA_128_CBC_WITH_MD5" },
405     { 0x060040, "SSL2_DES_64_CBC_WITH_MD5" },
406     { 0x0700c0, "SSL2_DES_192_EDE3_CBC_WITH_MD5" },
407     { 0x080080, "SSL2_RC4_64_WITH_MD5" },
408
409     /* Microsoft's old PCT protocol. These are from Eric Rescorla's
410        book "SSL and TLS" */
411     { 0x800001, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509" },
412     { 0x800003, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509_CHAIN" },
413     { 0x810001, "PCT_SSL_HASH_TYPE | PCT1_HASH_MD5" },
414     { 0x810003, "PCT_SSL_HASH_TYPE | PCT1_HASH_SHA" },
415     { 0x820001, "PCT_SSL_EXCH_TYPE | PCT1_EXCH_RSA_PKCS1" },
416     { 0x830004, "PCT_SSL_CIPHER_TYPE_1ST_HALF | PCT1_CIPHER_RC4" },
417     { 0x842840, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_40 | PCT1_MAC_BITS_128" },
418     { 0x848040, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_128 | PCT1_MAC_BITS_128" },
419     { 0x8f8001, "PCT_SSL_COMPAT | PCT_VERSION_1" },
420     { 0x00, NULL }
421 };
422
423 value_string_ext ssl_20_cipher_suites_ext = VALUE_STRING_EXT_INIT(ssl_20_cipher_suites);
424
425
426 /*
427  * Supported Groups (formerly named "EC Named Curve").
428  * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
429  */
430 const value_string ssl_extension_curves[] = {
431     {  1, "sect163k1" },
432     {  2, "sect163r1" },
433     {  3, "sect163r2" },
434     {  4, "sect193r1" },
435     {  5, "sect193r2" },
436     {  6, "sect233k1" },
437     {  7, "sect233r1" },
438     {  8, "sect239k1" },
439     {  9, "sect283k1" },
440     { 10, "sect283r1" },
441     { 11, "sect409k1" },
442     { 12, "sect409r1" },
443     { 13, "sect571k1" },
444     { 14, "sect571r1" },
445     { 15, "secp160k1" },
446     { 16, "secp160r1" },
447     { 17, "secp160r2" },
448     { 18, "secp192k1" },
449     { 19, "secp192r1" },
450     { 20, "secp224k1" },
451     { 21, "secp224r1" },
452     { 22, "secp256k1" },
453     { 23, "secp256r1" },
454     { 24, "secp384r1" },
455     { 25, "secp521r1" },
456     { 26, "brainpoolP256r1" }, /* RFC 7027 */
457     { 27, "brainpoolP384r1" }, /* RFC 7027 */
458     { 28, "brainpoolP512r1" }, /* RFC 7027 */
459     { 29, "ecdh_x25519" }, /* https://tools.ietf.org/html/draft-ietf-tls-rfc4492bis */
460     { 30, "ecdh_x448" }, /* https://tools.ietf.org/html/draft-ietf-tls-rfc4492bis */
461     { 256, "ffdhe2048" }, /* RFC 7919 */
462     { 257, "ffdhe3072" }, /* RFC 7919 */
463     { 258, "ffdhe4096" }, /* RFC 7919 */
464     { 259, "ffdhe6144" }, /* RFC 7919 */
465     { 260, "ffdhe8192" }, /* RFC 7919 */
466     { 0xFF01, "arbitrary_explicit_prime_curves" },
467     { 0xFF02, "arbitrary_explicit_char2_curves" },
468     { 0x00, NULL }
469 };
470
471 const value_string ssl_curve_types[] = {
472     { 1, "explicit_prime" },
473     { 2, "explicit_char2" },
474     { 3, "named_curve" },
475     { 0x00, NULL }
476 };
477
478 const value_string ssl_extension_ec_point_formats[] = {
479     { 0, "uncompressed" },
480     { 1, "ansiX962_compressed_prime" },
481     { 2, "ansiX962_compressed_char2" },
482     { 0x00, NULL }
483 };
484
485 const value_string ssl_20_certificate_type[] = {
486     { 0x00, "N/A" },
487     { 0x01, "X.509 Certificate" },
488     { 0x00, NULL }
489 };
490
491 const value_string ssl_31_content_type[] = {
492     { 20, "Change Cipher Spec" },
493     { 21, "Alert" },
494     { 22, "Handshake" },
495     { 23, "Application Data" },
496     { 24, "Heartbeat" },
497     { 0x00, NULL }
498 };
499
500 #if 0
501 /* XXX - would be used if we dissected the body of a Change Cipher Spec
502    message. */
503 const value_string ssl_31_change_cipher_spec[] = {
504     { 1, "Change Cipher Spec" },
505     { 0x00, NULL }
506 };
507 #endif
508
509 const value_string ssl_31_alert_level[] = {
510     { 1, "Warning" },
511     { 2, "Fatal" },
512     { 0x00, NULL }
513 };
514
515 const value_string ssl_31_alert_description[] = {
516     {   0,  "Close Notify" },
517     {   1,  "End of Early Data" },
518     {  10,  "Unexpected Message" },
519     {  20,  "Bad Record MAC" },
520     {  21,  "Decryption Failed" },
521     {  22,  "Record Overflow" },
522     {  30,  "Decompression Failure" },
523     {  40,  "Handshake Failure" },
524     {  41,  "No Certificate" },
525     {  42,  "Bad Certificate" },
526     {  43,  "Unsupported Certificate" },
527     {  44,  "Certificate Revoked" },
528     {  45,  "Certificate Expired" },
529     {  46,  "Certificate Unknown" },
530     {  47,  "Illegal Parameter" },
531     {  48,  "Unknown CA" },
532     {  49,  "Access Denied" },
533     {  50,  "Decode Error" },
534     {  51,  "Decrypt Error" },
535     {  60,  "Export Restriction" },
536     {  70,  "Protocol Version" },
537     {  71,  "Insufficient Security" },
538     {  80,  "Internal Error" },
539     {  86,  "Inappropriate Fallback" },
540     {  90,  "User Canceled" },
541     { 100, "No Renegotiation" },
542     { 109, "Missing Extension" },
543     { 110, "Unsupported Extension" },
544     { 111, "Certificate Unobtainable" },
545     { 112, "Unrecognized Name" },
546     { 113, "Bad Certificate Status Response" },
547     { 114, "Bad Certificate Hash Value" },
548     { 115, "Unknown PSK Identity" },
549     { 116, "Certificate Required" },
550     { 120, "No application Protocol" },
551     { 0x00, NULL }
552 };
553
554 const value_string ssl_31_handshake_type[] = {
555     { SSL_HND_HELLO_REQUEST,     "Hello Request" },
556     { SSL_HND_CLIENT_HELLO,      "Client Hello" },
557     { SSL_HND_SERVER_HELLO,      "Server Hello" },
558     { SSL_HND_HELLO_VERIFY_REQUEST, "Hello Verify Request"},
559     { SSL_HND_NEWSESSION_TICKET, "New Session Ticket" },
560     { SSL_HND_HELLO_RETRY_REQUEST, "Hello Retry Request" },
561     { SSL_HND_ENCRYPTED_EXTENSIONS, "Encrypted Extensions" },
562     { SSL_HND_CERTIFICATE,       "Certificate" },
563     { SSL_HND_SERVER_KEY_EXCHG,  "Server Key Exchange" },
564     { SSL_HND_CERT_REQUEST,      "Certificate Request" },
565     { SSL_HND_SVR_HELLO_DONE,    "Server Hello Done" },
566     { SSL_HND_CERT_VERIFY,       "Certificate Verify" },
567     { SSL_HND_CLIENT_KEY_EXCHG,  "Client Key Exchange" },
568     { SSL_HND_FINISHED,          "Finished" },
569     { SSL_HND_CERT_URL,          "Client Certificate URL" },
570     { SSL_HND_CERT_STATUS,       "Certificate Status" },
571     { SSL_HND_SUPPLEMENTAL_DATA, "Supplemental Data" },
572     { SSL_HND_ENCRYPTED_EXTS,    "Encrypted Extensions" },
573     { 0x00, NULL }
574 };
575
576 const value_string tls_heartbeat_type[] = {
577     { 1, "Request" },
578     { 2, "Response" },
579     { 0x00, NULL }
580 };
581
582 const value_string tls_heartbeat_mode[] = {
583     { 1, "Peer allowed to send requests" },
584     { 2, "Peer not allowed to send requests" },
585     { 0x00, NULL }
586 };
587
588 const value_string ssl_31_compression_method[] = {
589     {  0, "null" },
590     {  1, "DEFLATE" },
591     { 64, "LZS" },
592     { 0x00, NULL }
593 };
594
595 #if 0
596 /* XXX - would be used if we dissected a Signature, as would be
597    seen in a server key exchange or certificate verify message. */
598 const value_string ssl_31_key_exchange_algorithm[] = {
599     { 0, "RSA" },
600     { 1, "Diffie Hellman" },
601     { 0x00, NULL }
602 };
603
604 const value_string ssl_31_signature_algorithm[] = {
605     { 0, "Anonymous" },
606     { 1, "RSA" },
607     { 2, "DSA" },
608     { 0x00, NULL }
609 };
610 #endif
611
612 const value_string ssl_31_client_certificate_type[] = {
613     { 1, "RSA Sign" },
614     { 2, "DSS Sign" },
615     { 3, "RSA Fixed DH" },
616     { 4, "DSS Fixed DH" },
617     /* GOST certificate types */
618     /* Section 3.5 of draft-chudov-cryptopro-cptls-04 */
619     { 21, "GOST R 34.10-94" },
620     { 22, "GOST R 34.10-2001" },
621     /* END GOST certificate types */
622     { 64, "ECDSA Sign" },
623     { 65, "RSA Fixed ECDH" },
624     { 66, "ECDSA Fixed ECDH" },
625     { 0x00, NULL }
626 };
627
628 #if 0
629 /* XXX - would be used if we dissected exchange keys, as would be
630    seen in a client key exchange message. */
631 const value_string ssl_31_public_value_encoding[] = {
632     { 0, "Implicit" },
633     { 1, "Explicit" },
634     { 0x00, NULL }
635 };
636 #endif
637
638 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
639 /* Note: sorted by ascending value so value_string_ext fcns can do a binary search */
640 static const value_string ssl_31_ciphersuite[] = {
641     /* RFC 2246, RFC 4346, RFC 5246 */
642     { 0x0000, "TLS_NULL_WITH_NULL_NULL" },
643     { 0x0001, "TLS_RSA_WITH_NULL_MD5" },
644     { 0x0002, "TLS_RSA_WITH_NULL_SHA" },
645     { 0x0003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
646     { 0x0004, "TLS_RSA_WITH_RC4_128_MD5" },
647     { 0x0005, "TLS_RSA_WITH_RC4_128_SHA" },
648     { 0x0006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
649     { 0x0007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
650     { 0x0008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
651     { 0x0009, "TLS_RSA_WITH_DES_CBC_SHA" },
652     { 0x000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
653     { 0x000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
654     { 0x000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
655     { 0x000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
656     { 0x000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
657     { 0x000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
658     { 0x0010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
659     { 0x0011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
660     { 0x0012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
661     { 0x0013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
662     { 0x0014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
663     { 0x0015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
664     { 0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
665     { 0x0017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
666     { 0x0018, "TLS_DH_anon_WITH_RC4_128_MD5" },
667     { 0x0019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
668     { 0x001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
669     { 0x001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
670
671     { 0x001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
672     { 0x001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
673 #if 0 /* Because it clashes with KRB5, is never used any more, and is safe
674          to remove according to David Hopwood <david.hopwood@zetnet.co.uk>
675          of the ietf-tls list */
676     { 0x001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
677 #endif
678
679     /* RFC 2712 */
680     { 0x001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
681     { 0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
682     { 0x0020, "TLS_KRB5_WITH_RC4_128_SHA" },
683     { 0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
684     { 0x0022, "TLS_KRB5_WITH_DES_CBC_MD5" },
685     { 0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
686     { 0x0024, "TLS_KRB5_WITH_RC4_128_MD5" },
687     { 0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
688     { 0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
689     { 0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
690     { 0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
691     { 0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
692     { 0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
693     { 0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
694
695     /* RFC 4785 */
696     { 0x002C, "TLS_PSK_WITH_NULL_SHA" },
697     { 0x002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
698     { 0x002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
699
700     /* RFC 5246 */
701     { 0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA" },
702     { 0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
703     { 0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
704     { 0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
705     { 0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
706     { 0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
707     { 0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
708     { 0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
709     { 0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
710     { 0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
711     { 0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
712     { 0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
713     { 0x003B, "TLS_RSA_WITH_NULL_SHA256" },
714     { 0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
715     { 0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
716     { 0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
717     { 0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
718     { 0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
719
720     /* RFC 4132 */
721     { 0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
722     { 0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
723     { 0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
724     { 0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
725     { 0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
726     { 0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
727
728     /* 0x00,0x60-66 Reserved to avoid conflicts with widely deployed implementations  */
729     /* --- ??? --- */
730     { 0x0060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
731     { 0x0061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
732     /* draft-ietf-tls-56-bit-ciphersuites-01.txt */
733     { 0x0062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
734     { 0x0063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
735     { 0x0064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
736     { 0x0065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
737     { 0x0066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
738     /* --- ??? ---*/
739
740     { 0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
741     { 0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
742     { 0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
743     { 0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
744     { 0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
745     { 0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
746     { 0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
747
748     /* draft-chudov-cryptopro-cptls-04.txt */
749     { 0x0080,  "TLS_GOSTR341094_WITH_28147_CNT_IMIT" },
750     { 0x0081,  "TLS_GOSTR341001_WITH_28147_CNT_IMIT" },
751     { 0x0082,  "TLS_GOSTR341094_WITH_NULL_GOSTR3411" },
752     { 0x0083,  "TLS_GOSTR341001_WITH_NULL_GOSTR3411" },
753
754     /* RFC 4132 */
755     { 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
756     { 0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
757     { 0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
758     { 0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
759     { 0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
760     { 0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
761
762     /* RFC 4279 */
763     { 0x008A, "TLS_PSK_WITH_RC4_128_SHA" },
764     { 0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
765     { 0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
766     { 0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
767     { 0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
768     { 0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
769     { 0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
770     { 0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
771     { 0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
772     { 0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
773     { 0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
774     { 0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
775
776     /* RFC 4162 */
777     { 0x0096, "TLS_RSA_WITH_SEED_CBC_SHA" },
778     { 0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
779     { 0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
780     { 0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
781     { 0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
782     { 0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
783
784     /* RFC 5288 */
785     { 0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
786     { 0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
787     { 0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
788     { 0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
789     { 0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
790     { 0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
791     { 0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
792     { 0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
793     { 0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
794     { 0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
795     { 0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
796     { 0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
797
798     /* RFC 5487 */
799     { 0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
800     { 0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
801     { 0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
802     { 0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
803     { 0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
804     { 0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
805     { 0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
806     { 0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
807     { 0x00B0, "TLS_PSK_WITH_NULL_SHA256" },
808     { 0x00B1, "TLS_PSK_WITH_NULL_SHA384" },
809     { 0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
810     { 0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
811     { 0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
812     { 0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
813     { 0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
814     { 0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
815     { 0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
816     { 0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
817
818     /* From RFC 5932 */
819     { 0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
820     { 0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
821     { 0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
822     { 0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
823     { 0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
824     { 0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
825     { 0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
826     { 0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
827     { 0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
828     { 0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
829     { 0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
830     { 0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
831     /* 0x00,0xC6-FE Unassigned  */
832     /* From RFC 5746 */
833     { 0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
834     /* https://tools.ietf.org/html/draft-ietf-tls-tls13 */
835     { 0x1301, "TLS_AES_128_GCM_SHA256" },
836     { 0x1302, "TLS_AES_256_GCM_SHA384" },
837     { 0x1303, "TLS_CHACHA20_POLY1305_SHA256" },
838     { 0x1304, "TLS_AES_128_CCM_SHA256" },
839     { 0x1305, "TLS_AES_128_CCM_8_SHA256" },
840     /* From RFC 7507 */
841     { 0x5600, "TLS_FALLBACK_SCSV" },
842     /* From RFC 4492 */
843     { 0xc001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
844     { 0xc002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
845     { 0xc003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
846     { 0xc004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
847     { 0xc005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
848     { 0xc006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
849     { 0xc007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
850     { 0xc008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
851     { 0xc009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
852     { 0xc00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
853     { 0xc00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
854     { 0xc00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
855     { 0xc00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
856     { 0xc00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
857     { 0xc00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
858     { 0xc010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
859     { 0xc011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
860     { 0xc012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
861     { 0xc013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
862     { 0xc014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
863     { 0xc015, "TLS_ECDH_anon_WITH_NULL_SHA" },
864     { 0xc016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
865     { 0xc017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
866     { 0xc018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
867     { 0xc019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
868
869     /* RFC 5054 */
870     { 0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
871     { 0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
872     { 0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
873     { 0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
874     { 0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
875     { 0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
876     { 0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
877     { 0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
878     { 0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
879
880     /* RFC 5589 */
881     { 0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
882     { 0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
883     { 0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
884     { 0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
885     { 0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
886     { 0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
887     { 0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
888     { 0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
889     { 0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
890     { 0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
891     { 0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
892     { 0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
893     { 0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
894     { 0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
895     { 0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
896     { 0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
897
898     /* RFC 5489 */
899     { 0xC033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
900     { 0xC034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
901     { 0xC035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
902     { 0xC036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
903     { 0xC037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
904     { 0xC038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
905     { 0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
906     { 0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
907     { 0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
908
909     /* RFC 6209 */
910     { 0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256" },
911     { 0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384" },
912     { 0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256" },
913     { 0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384" },
914     { 0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256" },
915     { 0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384" },
916     { 0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256" },
917     { 0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384" },
918     { 0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256" },
919     { 0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384" },
920     { 0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256" },
921     { 0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384" },
922     { 0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256" },
923     { 0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384" },
924     { 0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256" },
925     { 0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384" },
926     { 0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256" },
927     { 0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384" },
928     { 0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256" },
929     { 0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384" },
930     { 0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256" },
931     { 0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384" },
932     { 0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256" },
933     { 0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384" },
934     { 0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256" },
935     { 0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384" },
936     { 0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256" },
937     { 0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384" },
938     { 0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256" },
939     { 0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384" },
940     { 0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256" },
941     { 0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384" },
942     { 0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256" },
943     { 0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384" },
944     { 0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256" },
945     { 0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384" },
946     { 0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256" },
947     { 0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384" },
948     { 0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256" },
949     { 0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384" },
950     { 0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256" },
951     { 0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384" },
952     { 0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256" },
953     { 0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384" },
954     { 0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256" },
955     { 0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384" },
956     { 0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256" },
957     { 0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384" },
958     { 0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256" },
959     { 0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384" },
960     { 0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256" },
961     { 0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384" },
962     { 0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256" },
963     { 0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384" },
964
965     /* RFC 6367 */
966     { 0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
967     { 0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
968     { 0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
969     { 0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
970     { 0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
971     { 0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
972     { 0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
973     { 0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
974     { 0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
975     { 0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
976     { 0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
977     { 0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
978     { 0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
979     { 0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
980     { 0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
981     { 0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
982     { 0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
983     { 0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
984     { 0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256" },
985     { 0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384" },
986     { 0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
987     { 0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
988     { 0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
989     { 0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
990     { 0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
991     { 0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
992     { 0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
993     { 0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
994     { 0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
995     { 0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
996     { 0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
997     { 0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
998     { 0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
999     { 0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
1000     { 0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
1001     { 0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
1002     { 0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
1003     { 0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
1004     { 0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
1005     { 0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
1006     { 0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
1007     { 0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
1008
1009     /* RFC 6655 */
1010     { 0xC09C, "TLS_RSA_WITH_AES_128_CCM" },
1011     { 0xC09D, "TLS_RSA_WITH_AES_256_CCM" },
1012     { 0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM" },
1013     { 0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM" },
1014     { 0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8" },
1015     { 0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8" },
1016     { 0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8" },
1017     { 0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8" },
1018     { 0xC0A4, "TLS_PSK_WITH_AES_128_CCM" },
1019     { 0xC0A5, "TLS_PSK_WITH_AES_256_CCM" },
1020     { 0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM" },
1021     { 0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM" },
1022     { 0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8" },
1023     { 0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8" },
1024     { 0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8" },
1025     { 0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8" },
1026
1027     /* RFC 7251 */
1028     { 0xC0AC, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM" },
1029     { 0xC0AD, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM" },
1030     { 0xC0AE, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" },
1031     { 0xC0AF, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8" },
1032 /*
1033 0xC0,0xAB-FF Unassigned
1034 0xC1-FD,* Unassigned
1035 0xFE,0x00-FD Unassigned
1036 0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
1037 0xFF,0x00-FF Reserved for Private Use [RFC5246]
1038 */
1039
1040     /* old numbers used in the beginning
1041      * http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305 */
1042     { 0xCC13, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
1043     { 0xCC14, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
1044     { 0xCC15, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
1045
1046     /* http://tools.ietf.org/html/draft-ietf-tls-chacha20-poly1305 */
1047     { 0xCCA8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
1048     { 0xCCA9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
1049     { 0xCCAA, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
1050     { 0xCCAB, "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256" },
1051     { 0xCCAC, "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
1052     { 0xCCAD, "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256" },
1053     { 0xCCAE, "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256" },
1054
1055     /* http://tools.ietf.org/html/draft-josefsson-salsa20-tls */
1056     { 0xE410, "TLS_RSA_WITH_ESTREAM_SALSA20_SHA1" },
1057     { 0xE411, "TLS_RSA_WITH_SALSA20_SHA1" },
1058     { 0xE412, "TLS_ECDHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
1059     { 0xE413, "TLS_ECDHE_RSA_WITH_SALSA20_SHA1" },
1060     { 0xE414, "TLS_ECDHE_ECDSA_WITH_ESTREAM_SALSA20_SHA1" },
1061     { 0xE415, "TLS_ECDHE_ECDSA_WITH_SALSA20_SHA1" },
1062     { 0xE416, "TLS_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1063     { 0xE417, "TLS_PSK_WITH_SALSA20_SHA1" },
1064     { 0xE418, "TLS_ECDHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1065     { 0xE419, "TLS_ECDHE_PSK_WITH_SALSA20_SHA1" },
1066     { 0xE41A, "TLS_RSA_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1067     { 0xE41B, "TLS_RSA_PSK_WITH_SALSA20_SHA1" },
1068     { 0xE41C, "TLS_DHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1069     { 0xE41D, "TLS_DHE_PSK_WITH_SALSA20_SHA1" },
1070     { 0xE41E, "TLS_DHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
1071     { 0xE41F, "TLS_DHE_RSA_WITH_SALSA20_SHA1" },
1072
1073     /* these from http://www.mozilla.org/projects/
1074          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
1075     { 0xfefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
1076     { 0xfeff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
1077     { 0xffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
1078     { 0xffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
1079     /* note that ciphersuites 0xff00 - 0xffff are private */
1080     { 0x00, NULL }
1081 };
1082
1083 value_string_ext ssl_31_ciphersuite_ext = VALUE_STRING_EXT_INIT(ssl_31_ciphersuite);
1084
1085
1086 const value_string pct_msg_types[] = {
1087     { PCT_MSG_CLIENT_HELLO,         "Client Hello" },
1088     { PCT_MSG_SERVER_HELLO,         "Server Hello" },
1089     { PCT_MSG_CLIENT_MASTER_KEY,    "Client Master Key" },
1090     { PCT_MSG_SERVER_VERIFY,        "Server Verify" },
1091     { PCT_MSG_ERROR,                "Error" },
1092     { 0x00, NULL }
1093 };
1094
1095 const value_string pct_cipher_type[] = {
1096     { PCT_CIPHER_DES, "DES" },
1097     { PCT_CIPHER_IDEA, "IDEA" },
1098     { PCT_CIPHER_RC2, "RC2" },
1099     { PCT_CIPHER_RC4, "RC4" },
1100     { PCT_CIPHER_DES_112, "DES 112 bit" },
1101     { PCT_CIPHER_DES_168, "DES 168 bit" },
1102     { 0x00, NULL }
1103 };
1104
1105 const value_string pct_hash_type[] = {
1106     { PCT_HASH_MD5, "MD5" },
1107     { PCT_HASH_MD5_TRUNC_64, "MD5_TRUNC_64"},
1108     { PCT_HASH_SHA, "SHA"},
1109     { PCT_HASH_SHA_TRUNC_80, "SHA_TRUNC_80"},
1110     { PCT_HASH_DES_DM, "DES_DM"},
1111     { 0x00, NULL }
1112 };
1113
1114 const value_string pct_cert_type[] = {
1115     { PCT_CERT_NONE, "None" },
1116     { PCT_CERT_X509, "X.509" },
1117     { PCT_CERT_PKCS7, "PKCS #7" },
1118     { 0x00, NULL }
1119 };
1120 const value_string pct_sig_type[] = {
1121     { PCT_SIG_NONE, "None" },
1122     { PCT_SIG_RSA_MD5, "MD5" },
1123     { PCT_SIG_RSA_SHA, "RSA SHA" },
1124     { PCT_SIG_DSA_SHA, "DSA SHA" },
1125     { 0x00, NULL }
1126 };
1127
1128 const value_string pct_exch_type[] = {
1129     { PCT_EXCH_RSA_PKCS1, "RSA PKCS#1" },
1130     { PCT_EXCH_RSA_PKCS1_TOKEN_DES, "RSA PKCS#1 Token DES" },
1131     { PCT_EXCH_RSA_PKCS1_TOKEN_DES3, "RSA PKCS#1 Token 3DES" },
1132     { PCT_EXCH_RSA_PKCS1_TOKEN_RC2, "RSA PKCS#1 Token RC-2" },
1133     { PCT_EXCH_RSA_PKCS1_TOKEN_RC4, "RSA PKCS#1 Token RC-4" },
1134     { PCT_EXCH_DH_PKCS3, "DH PKCS#3" },
1135     { PCT_EXCH_DH_PKCS3_TOKEN_DES, "DH PKCS#3 Token DES" },
1136     { PCT_EXCH_DH_PKCS3_TOKEN_DES3, "DH PKCS#3 Token 3DES" },
1137     { PCT_EXCH_FORTEZZA_TOKEN, "Fortezza" },
1138     { 0x00, NULL }
1139 };
1140
1141 const value_string pct_error_code[] = {
1142     { PCT_ERR_BAD_CERTIFICATE, "PCT_ERR_BAD_CERTIFICATE" },
1143     { PCT_ERR_CLIENT_AUTH_FAILED, "PCT_ERR_CLIENT_AUTH_FAILE" },
1144     { PCT_ERR_ILLEGAL_MESSAGE, "PCT_ERR_ILLEGAL_MESSAGE" },
1145     { PCT_ERR_INTEGRITY_CHECK_FAILED, "PCT_ERR_INTEGRITY_CHECK_FAILED" },
1146     { PCT_ERR_SERVER_AUTH_FAILED, "PCT_ERR_SERVER_AUTH_FAILED" },
1147     { PCT_ERR_SPECS_MISMATCH, "PCT_ERR_SPECS_MISMATCH" },
1148     { 0x00, NULL }
1149 };
1150
1151 /* http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-1 */
1152 const value_string tls_hello_extension_types[] = {
1153     { SSL_HND_HELLO_EXT_SERVER_NAME, "server_name" }, /* RFC 6066 */
1154     { SSL_HND_HELLO_EXT_MAX_FRAGMENT_LENGTH, "max_fragment_length" },/* RFC 6066 */
1155     { SSL_HND_HELLO_EXT_CLIENT_CERTIFICATE_URL, "client_certificate_url" }, /* RFC 6066 */
1156     { SSL_HND_HELLO_EXT_TRUSTED_CA_KEYS, "trusted_ca_keys" }, /* RFC 6066 */
1157     { SSL_HND_HELLO_EXT_TRUNCATED_HMAC, "truncated_hmac" }, /* RFC 6066 */
1158     { SSL_HND_HELLO_EXT_STATUS_REQUEST, "status_request" }, /* RFC 6066 */
1159     { SSL_HND_HELLO_EXT_USER_MAPPING, "user_mapping" }, /* RFC 4681 */
1160     { SSL_HND_HELLO_EXT_CLIENT_AUTHZ, "client_authz" }, /* RFC 5878 */
1161     { SSL_HND_HELLO_EXT_SERVER_AUTHZ, "server_authz" }, /* RFC 5878 */
1162     { SSL_HND_HELLO_EXT_CERT_TYPE, "cert_type" }, /* RFC 6091 */
1163     { SSL_HND_HELLO_EXT_SUPPORTED_GROUPS, "supported_groups" }, /* RFC 4492, RFC 7919 */
1164     { SSL_HND_HELLO_EXT_EC_POINT_FORMATS, "ec_point_formats" }, /* RFC 4492 */
1165     { SSL_HND_HELLO_EXT_SRP, "srp" }, /* RFC 5054 */
1166     { SSL_HND_HELLO_EXT_SIGNATURE_ALGORITHMS, "signature_algorithms" }, /* RFC 5246 */
1167     { SSL_HND_HELLO_EXT_USE_SRTP, "use_srtp" }, /* RFC 5764 */
1168     { SSL_HND_HELLO_EXT_HEARTBEAT, "heartbeat" }, /* RFC 6520 */
1169     { SSL_HND_HELLO_EXT_ALPN, "application_layer_protocol_negotiation" }, /* RFC 7301 */
1170     { SSL_HND_HELLO_EXT_STATUS_REQUEST_V2, "status_request_v2" }, /* RFC 6961 */
1171     { SSL_HND_HELLO_EXT_SIGNED_CERTIFICATE_TIMESTAMP, "signed_certificate_timestamp" }, /* RFC 6962 */
1172     { SSL_HND_HELLO_EXT_CLIENT_CERT_TYPE, "client_certificate_type" }, /* RFC 7250 */
1173     { SSL_HND_HELLO_EXT_SERVER_CERT_TYPE, "server_certificate_type" }, /* RFC 7250 */
1174     { SSL_HND_HELLO_EXT_PADDING, "padding" }, /* RFC 7685 */
1175     { SSL_HND_HELLO_EXT_ENCRYPT_THEN_MAC, "encrypt_then_mac" }, /* RFC 7366 */
1176     { SSL_HND_HELLO_EXT_EXTENDED_MASTER_SECRET, "extended_master_secret" }, /* RFC 7627 */
1177     { SSL_HND_HELLO_EXT_TOKEN_BINDING, "token_binding" }, /* https://tools.ietf.org/html/draft-ietf-tokbind-negotiation */
1178     { SSL_HND_HELLO_EXT_CACHED_INFO, "cached_info" }, /* RFC 7924 */
1179     { SSL_HND_HELLO_EXT_SESSION_TICKET_TLS, "SessionTicket TLS" }, /* RFC 4507 */
1180     { SSL_HND_HELLO_EXT_KEY_SHARE, "key_share" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1181     { SSL_HND_HELLO_EXT_PRE_SHARED_KEY, "pre_shared_key" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1182     { SSL_HND_HELLO_EXT_EARLY_DATA, "early_data" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1183     { SSL_HND_HELLO_EXT_SUPPORTED_VERSIONS, "supported_versions" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1184     { SSL_HND_HELLO_EXT_COOKIE, "cookie" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1185     { SSL_HND_HELLO_EXT_PSK_KEY_EXCHANGE_MODES, "psk_key_exchange_modes" }, /* TLS 1.3 https://tools.ietf.org/html/draft-ietf-tls-tls13 */
1186     { SSL_HND_HELLO_EXT_NPN, "next_protocol_negotiation"}, /* https://tools.ietf.org/id/draft-agl-tls-nextprotoneg-03.html */
1187     { SSL_HND_HELLO_EXT_CHANNEL_ID_OLD, "channel_id_old" }, /* http://tools.ietf.org/html/draft-balfanz-tls-channelid-00
1188        https://twitter.com/ericlaw/status/274237352531083264 */
1189     { SSL_HND_HELLO_EXT_CHANNEL_ID, "channel_id" }, /* http://tools.ietf.org/html/draft-balfanz-tls-channelid-01
1190        https://code.google.com/p/chromium/codesearch#chromium/src/net/third_party/nss/ssl/sslt.h&l=209 */
1191     { SSL_HND_HELLO_EXT_RENEGOTIATION_INFO, "renegotiation_info" }, /* RFC 5746 */
1192     { SSL_HND_HELLO_EXT_DRAFT_VERSION_TLS13, "Draft version of TLS 1.3" }, /* for experimentation only  https://www.ietf.org/mail-archive/web/tls/current/msg20853.html */
1193     { 0, NULL }
1194 };
1195
1196 const value_string tls_hello_ext_server_name_type_vs[] = {
1197     { 0, "host_name" },
1198     { 0, NULL }
1199 };
1200
1201 /* draft-ietf-tls-tls13-18 4.2.7 */
1202 const value_string tls_hello_ext_psk_ke_mode[] = {
1203     { 0, "PSK-only key establishment (psk_ke)" },
1204     { 1, "PSK key establishment with (EC)DHE key establishment (psk_dhe_ke)" },
1205     { 0, NULL }
1206 };
1207
1208 /* RFC 5246 7.4.1.4.1 */
1209 const value_string tls_hash_algorithm[] = {
1210     { 0, "None" },
1211     { 1, "MD5" },
1212     { 2, "SHA1" },
1213     { 3, "SHA224" },
1214     { 4, "SHA256" },
1215     { 5, "SHA384" },
1216     { 6, "SHA512" },
1217     { 0, NULL }
1218 };
1219
1220 const value_string tls_signature_algorithm[] = {
1221     { 0, "Anonymous" },
1222     { 1, "RSA" },
1223     { 2, "DSA" },
1224     { 3, "ECDSA" },
1225     { 0, NULL }
1226 };
1227
1228 /* RFC 6091 3.1 */
1229 const value_string tls_certificate_type[] = {
1230     { 0, "X.509" },
1231     { 1, "OpenPGP" },
1232     { SSL_HND_CERT_TYPE_RAW_PUBLIC_KEY, "Raw Public Key" }, /* RFC 7250 */
1233     { 0, NULL }
1234 };
1235
1236 const value_string tls_cert_chain_type[] = {
1237     { SSL_HND_CERT_URL_TYPE_INDIVIDUAL_CERT,    "Individual Certificates" },
1238     { SSL_HND_CERT_URL_TYPE_PKIPATH,            "PKI Path" },
1239     { 0, NULL }
1240 };
1241
1242 const value_string tls_cert_status_type[] = {
1243     { SSL_HND_CERT_STATUS_TYPE_OCSP,            "OCSP" },
1244     { SSL_HND_CERT_STATUS_TYPE_OCSP_MULTI,      "OCSP Multi" },
1245     { 0, NULL }
1246 };
1247
1248 /* string_string is inappropriate as it compares strings while
1249  * "byte strings MUST NOT be truncated" (RFC 7301) */
1250 typedef struct ssl_alpn_protocol {
1251     const char      *proto_name;
1252     gboolean         match_exact;
1253     const char      *dissector_name;
1254 } ssl_alpn_protocol_t;
1255 /* http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids */
1256 static const ssl_alpn_protocol_t ssl_alpn_protocols[] = {
1257     { "http/1.1",           TRUE,   "http" },
1258     /* SPDY moves so fast, just 1, 2 and 3 are registered with IANA but there
1259      * already exists 3.1 as of this writing... match the prefix. */
1260     { "spdy/",              FALSE,  "spdy" },
1261     { "stun.turn",          TRUE,   "turnchannel" },
1262     { "stun.nat-discovery", TRUE,   "stun" },
1263     /* draft-ietf-httpbis-http2-16 */
1264     { "h2-",                FALSE,  "http2" }, /* draft versions */
1265     { "h2",                 TRUE,   "http2" }, /* final version */
1266 };
1267
1268 /* Lookup tables }}} */
1269
1270 /* we keep this internal to packet-ssl-utils, as there should be
1271    no need to access it any other way.
1272
1273    This also allows us to hide the dependency on zlib.
1274 */
1275 struct _SslDecompress {
1276     gint compression;
1277 #ifdef HAVE_ZLIB
1278     z_stream istream;
1279 #endif
1280 };
1281
1282 /* To assist in parsing client/server key exchange messages
1283    0 indicates unknown */
1284 gint ssl_get_keyex_alg(gint cipher)
1285 {
1286     /* Map Cipher suite number to Key Exchange algorithm {{{ */
1287     switch(cipher) {
1288     case 0x0017:
1289     case 0x0018:
1290     case 0x0019:
1291     case 0x001a:
1292     case 0x001b:
1293     case 0x0034:
1294     case 0x003a:
1295     case 0x0046:
1296     case 0x006c:
1297     case 0x006d:
1298     case 0x0089:
1299     case 0x009b:
1300     case 0x00a6:
1301     case 0x00a7:
1302     case 0x00bf:
1303     case 0x00c5:
1304     case 0xc084:
1305     case 0xc085:
1306         return KEX_DH_ANON;
1307     case 0x000b:
1308     case 0x000c:
1309     case 0x000d:
1310     case 0x0030:
1311     case 0x0036:
1312     case 0x003e:
1313     case 0x0042:
1314     case 0x0068:
1315     case 0x0085:
1316     case 0x0097:
1317     case 0x00a4:
1318     case 0x00a5:
1319     case 0x00bb:
1320     case 0x00c1:
1321     case 0xc082:
1322     case 0xc083:
1323         return KEX_DH_DSS;
1324     case 0x000e:
1325     case 0x000f:
1326     case 0x0010:
1327     case 0x0031:
1328     case 0x0037:
1329     case 0x003f:
1330     case 0x0043:
1331     case 0x0069:
1332     case 0x0086:
1333     case 0x0098:
1334     case 0x00a0:
1335     case 0x00a1:
1336     case 0x00bc:
1337     case 0x00c2:
1338     case 0xc07e:
1339     case 0xc07f:
1340         return KEX_DH_RSA;
1341     case 0x0011:
1342     case 0x0012:
1343     case 0x0013:
1344     case 0x0032:
1345     case 0x0038:
1346     case 0x0040:
1347     case 0x0044:
1348     case 0x0063:
1349     case 0x0065:
1350     case 0x0066:
1351     case 0x006a:
1352     case 0x0087:
1353     case 0x0099:
1354     case 0x00a2:
1355     case 0x00a3:
1356     case 0x00bd:
1357     case 0x00c3:
1358     case 0xc080:
1359     case 0xc081:
1360         return KEX_DHE_DSS;
1361     case 0x002d:
1362     case 0x008e:
1363     case 0x008f:
1364     case 0x0090:
1365     case 0x0091:
1366     case 0x00aa:
1367     case 0x00ab:
1368     case 0x00b2:
1369     case 0x00b3:
1370     case 0x00b4:
1371     case 0x00b5:
1372     case 0xc090:
1373     case 0xc091:
1374     case 0xc096:
1375     case 0xc097:
1376     case 0xc0a6:
1377     case 0xc0a7:
1378     case 0xc0aa:
1379     case 0xc0ab:
1380     case 0xe41c:
1381     case 0xe41d:
1382         return KEX_DHE_PSK;
1383     case 0x0014:
1384     case 0x0015:
1385     case 0x0016:
1386     case 0x0033:
1387     case 0x0039:
1388     case 0x0045:
1389     case 0x0067:
1390     case 0x006b:
1391     case 0x0088:
1392     case 0x009a:
1393     case 0x009e:
1394     case 0x009f:
1395     case 0x00be:
1396     case 0x00c4:
1397     case 0xc07c:
1398     case 0xc07d:
1399     case 0xc09e:
1400     case 0xc09f:
1401     case 0xc0a2:
1402     case 0xc0a3:
1403     case 0xe41e:
1404     case 0xe41f:
1405         return KEX_DHE_RSA;
1406     case 0xc015:
1407     case 0xc016:
1408     case 0xc017:
1409     case 0xc018:
1410     case 0xc019:
1411         return KEX_ECDH_ANON;
1412     case 0xc001:
1413     case 0xc002:
1414     case 0xc003:
1415     case 0xc004:
1416     case 0xc005:
1417     case 0xc025:
1418     case 0xc026:
1419     case 0xc02d:
1420     case 0xc02e:
1421     case 0xc074:
1422     case 0xc075:
1423     case 0xc088:
1424     case 0xc089:
1425         return KEX_ECDH_ECDSA;
1426     case 0xc00b:
1427     case 0xc00c:
1428     case 0xc00d:
1429     case 0xc00e:
1430     case 0xc00f:
1431     case 0xc029:
1432     case 0xc02a:
1433     case 0xc031:
1434     case 0xc032:
1435     case 0xc078:
1436     case 0xc079:
1437     case 0xc08c:
1438     case 0xc08d:
1439         return KEX_ECDH_RSA;
1440     case 0xc006:
1441     case 0xc007:
1442     case 0xc008:
1443     case 0xc009:
1444     case 0xc00a:
1445     case 0xc023:
1446     case 0xc024:
1447     case 0xc02b:
1448     case 0xc02c:
1449     case 0xc072:
1450     case 0xc073:
1451     case 0xc086:
1452     case 0xc087:
1453     case 0xc0ac:
1454     case 0xc0ad:
1455     case 0xc0ae:
1456     case 0xc0af:
1457     case 0xe414:
1458     case 0xe415:
1459         return KEX_ECDHE_ECDSA;
1460     case 0xc033:
1461     case 0xc034:
1462     case 0xc035:
1463     case 0xc036:
1464     case 0xc037:
1465     case 0xc038:
1466     case 0xc039:
1467     case 0xc03a:
1468     case 0xc03b:
1469     case 0xc09a:
1470     case 0xc09b:
1471     case 0xe418:
1472     case 0xe419:
1473         return KEX_ECDHE_PSK;
1474     case 0xc010:
1475     case 0xc011:
1476     case 0xc012:
1477     case 0xc013:
1478     case 0xc014:
1479     case 0xc027:
1480     case 0xc028:
1481     case 0xc02f:
1482     case 0xc030:
1483     case 0xc076:
1484     case 0xc077:
1485     case 0xc08a:
1486     case 0xc08b:
1487     case 0xe412:
1488     case 0xe413:
1489         return KEX_ECDHE_RSA;
1490     case 0x001e:
1491     case 0x001f:
1492     case 0x0020:
1493     case 0x0021:
1494     case 0x0022:
1495     case 0x0023:
1496     case 0x0024:
1497     case 0x0025:
1498     case 0x0026:
1499     case 0x0027:
1500     case 0x0028:
1501     case 0x0029:
1502     case 0x002a:
1503     case 0x002b:
1504         return KEX_KRB5;
1505     case 0x002c:
1506     case 0x008a:
1507     case 0x008b:
1508     case 0x008c:
1509     case 0x008d:
1510     case 0x00a8:
1511     case 0x00a9:
1512     case 0x00ae:
1513     case 0x00af:
1514     case 0x00b0:
1515     case 0x00b1:
1516     case 0xc064:
1517     case 0xc065:
1518     case 0xc08e:
1519     case 0xc08f:
1520     case 0xc094:
1521     case 0xc095:
1522     case 0xc0a4:
1523     case 0xc0a5:
1524     case 0xc0a8:
1525     case 0xc0a9:
1526     case 0xe416:
1527     case 0xe417:
1528         return KEX_PSK;
1529     case 0x0001:
1530     case 0x0002:
1531     case 0x0003:
1532     case 0x0004:
1533     case 0x0005:
1534     case 0x0006:
1535     case 0x0007:
1536     case 0x0008:
1537     case 0x0009:
1538     case 0x000a:
1539     case 0x002f:
1540     case 0x0035:
1541     case 0x003b:
1542     case 0x003c:
1543     case 0x003d:
1544     case 0x0041:
1545     case 0x0060:
1546     case 0x0061:
1547     case 0x0062:
1548     case 0x0064:
1549     case 0x0084:
1550     case 0x0096:
1551     case 0x009c:
1552     case 0x009d:
1553     case 0x00ba:
1554     case 0x00c0:
1555     case 0xc07a:
1556     case 0xc07b:
1557     case 0xc09c:
1558     case 0xc09d:
1559     case 0xc0a0:
1560     case 0xc0a1:
1561     case 0xe410:
1562     case 0xe411:
1563     case 0xfefe:
1564     case 0xfeff:
1565     case 0xffe0:
1566     case 0xffe1:
1567         return KEX_RSA;
1568     case 0x002e:
1569     case 0x0092:
1570     case 0x0093:
1571     case 0x0094:
1572     case 0x0095:
1573     case 0x00ac:
1574     case 0x00ad:
1575     case 0x00b6:
1576     case 0x00b7:
1577     case 0x00b8:
1578     case 0x00b9:
1579     case 0xc092:
1580     case 0xc093:
1581     case 0xc098:
1582     case 0xc099:
1583     case 0xe41a:
1584     case 0xe41b:
1585         return KEX_RSA_PSK;
1586     case 0xc01a:
1587     case 0xc01d:
1588     case 0xc020:
1589         return KEX_SRP_SHA;
1590     case 0xc01c:
1591     case 0xc01f:
1592     case 0xc022:
1593         return KEX_SRP_SHA_DSS;
1594     case 0xc01b:
1595     case 0xc01e:
1596     case 0xc021:
1597         return KEX_SRP_SHA_RSA;
1598     default:
1599         break;
1600     }
1601
1602     return 0;
1603     /* }}} */
1604 }
1605
1606
1607 /* StringInfo structure (len + data) functions {{{ */
1608
1609 static gint
1610 ssl_data_alloc(StringInfo* str, size_t len)
1611 {
1612     str->data = (guchar *)g_malloc(len);
1613     /* the allocator can return a null pointer for a size equal to 0,
1614      * and that must be allowed */
1615     if (len > 0 && !str->data)
1616         return -1;
1617     str->data_len = (guint) len;
1618     return 0;
1619 }
1620
1621 void
1622 ssl_data_set(StringInfo* str, const guchar* data, guint len)
1623 {
1624     DISSECTOR_ASSERT(data);
1625     memcpy(str->data, data, len);
1626     str->data_len = len;
1627 }
1628
1629 #ifdef HAVE_LIBGCRYPT
1630 static gint
1631 ssl_data_realloc(StringInfo* str, guint len)
1632 {
1633     str->data = (guchar *)g_realloc(str->data, len);
1634     if (!str->data)
1635         return -1;
1636     str->data_len = len;
1637     return 0;
1638 }
1639
1640 static StringInfo *
1641 ssl_data_clone(StringInfo *str)
1642 {
1643     StringInfo *cloned_str;
1644     cloned_str = (StringInfo *) wmem_alloc0(wmem_file_scope(),
1645             sizeof(StringInfo) + str->data_len);
1646     cloned_str->data = (guchar *) (cloned_str + 1);
1647     ssl_data_set(cloned_str, str->data, str->data_len);
1648     return cloned_str;
1649 }
1650
1651 static gint
1652 ssl_data_copy(StringInfo* dst, StringInfo* src)
1653 {
1654     if (dst->data_len < src->data_len) {
1655       if (ssl_data_realloc(dst, src->data_len))
1656         return -1;
1657     }
1658     memcpy(dst->data, src->data, src->data_len);
1659     dst->data_len = src->data_len;
1660     return 0;
1661 }
1662 #endif
1663
1664 /* from_hex converts |hex_len| bytes of hex data from |in| and sets |*out| to
1665  * the result. |out->data| will be allocated using wmem_file_scope. Returns TRUE on
1666  * success. */
1667 static gboolean from_hex(StringInfo* out, const char* in, gsize hex_len) {
1668     gsize i;
1669
1670     if (hex_len & 1)
1671         return FALSE;
1672
1673     out->data = (guchar *)wmem_alloc(wmem_file_scope(), hex_len / 2);
1674     for (i = 0; i < hex_len / 2; i++) {
1675         int a = ws_xton(in[i*2]);
1676         int b = ws_xton(in[i*2 + 1]);
1677         if (a == -1 || b == -1)
1678             return FALSE;
1679         out->data[i] = a << 4 | b;
1680     }
1681     out->data_len = (guint)hex_len / 2;
1682     return TRUE;
1683 }
1684 /* StringInfo structure (len + data) functions }}} */
1685
1686
1687 #ifdef HAVE_LIBGCRYPT
1688
1689 /* libgcrypt wrappers for HMAC/message digest operations {{{ */
1690 /* hmac abstraction layer */
1691 #define SSL_HMAC gcry_md_hd_t
1692
1693 static inline gint
1694 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
1695 {
1696     gcry_error_t  err;
1697     const char   *err_str, *err_src;
1698
1699     err = gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
1700     if (err != 0) {
1701         err_str = gcry_strerror(err);
1702         err_src = gcry_strsource(err);
1703         ssl_debug_printf("ssl_hmac_init(): gcry_md_open failed %s/%s", err_str, err_src);
1704         return -1;
1705     }
1706     gcry_md_setkey (*(md), key, len);
1707     return 0;
1708 }
1709 static inline void
1710 ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
1711 {
1712     gcry_md_write(*(md), data, len);
1713 }
1714 static inline void
1715 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
1716 {
1717     gint  algo;
1718     guint len;
1719
1720     algo = gcry_md_get_algo (*(md));
1721     len  = gcry_md_get_algo_dlen(algo);
1722     DISSECTOR_ASSERT(len <= *datalen);
1723     memcpy(data, gcry_md_read(*(md), algo), len);
1724     *datalen = len;
1725 }
1726 static inline void
1727 ssl_hmac_cleanup(SSL_HMAC* md)
1728 {
1729     gcry_md_close(*(md));
1730 }
1731
1732 /* message digest abstraction layer*/
1733 #define SSL_MD gcry_md_hd_t
1734
1735 static inline gint
1736 ssl_md_init(SSL_MD* md, gint algo)
1737 {
1738     gcry_error_t  err;
1739     const char   *err_str, *err_src;
1740     err = gcry_md_open(md,algo, 0);
1741     if (err != 0) {
1742         err_str = gcry_strerror(err);
1743         err_src = gcry_strsource(err);
1744         ssl_debug_printf("ssl_md_init(): gcry_md_open failed %s/%s", err_str, err_src);
1745         return -1;
1746     }
1747     return 0;
1748 }
1749 static inline void
1750 ssl_md_update(SSL_MD* md, guchar* data, gint len)
1751 {
1752     gcry_md_write(*(md), data, len);
1753 }
1754 static inline void
1755 ssl_md_final(SSL_MD* md, guchar* data, guint* datalen)
1756 {
1757     gint algo;
1758     gint len;
1759     algo = gcry_md_get_algo (*(md));
1760     len = gcry_md_get_algo_dlen (algo);
1761     memcpy(data, gcry_md_read(*(md),  algo), len);
1762     *datalen = len;
1763 }
1764 static inline void
1765 ssl_md_cleanup(SSL_MD* md)
1766 {
1767     gcry_md_close(*(md));
1768 }
1769
1770 /* md5 /sha abstraction layer */
1771 #define SSL_SHA_CTX gcry_md_hd_t
1772 #define SSL_MD5_CTX gcry_md_hd_t
1773
1774 static inline void
1775 ssl_sha_init(SSL_SHA_CTX* md)
1776 {
1777     gcry_md_open(md,GCRY_MD_SHA1, 0);
1778 }
1779 static inline void
1780 ssl_sha_update(SSL_SHA_CTX* md, guchar* data, gint len)
1781 {
1782     gcry_md_write(*(md), data, len);
1783 }
1784 static inline void
1785 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
1786 {
1787     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_SHA1),
1788            gcry_md_get_algo_dlen(GCRY_MD_SHA1));
1789 }
1790 static inline void
1791 ssl_sha_cleanup(SSL_SHA_CTX* md)
1792 {
1793     gcry_md_close(*(md));
1794 }
1795
1796 static inline gint
1797 ssl_md5_init(SSL_MD5_CTX* md)
1798 {
1799     return gcry_md_open(md,GCRY_MD_MD5, 0);
1800 }
1801 static inline void
1802 ssl_md5_update(SSL_MD5_CTX* md, guchar* data, gint len)
1803 {
1804     gcry_md_write(*(md), data, len);
1805 }
1806 static inline void
1807 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
1808 {
1809     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_MD5),
1810            gcry_md_get_algo_dlen(GCRY_MD_MD5));
1811 }
1812 static inline void
1813 ssl_md5_cleanup(SSL_MD5_CTX* md)
1814 {
1815     gcry_md_close(*(md));
1816 }
1817 /* libgcrypt wrappers for HMAC/message digest operations }}} */
1818
1819 /* libgcrypt wrappers for Cipher state manipulation {{{ */
1820 gint
1821 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
1822 {
1823     gint ret;
1824 #if 0
1825     guchar *ivp;
1826     gint i;
1827     gcry_cipher_hd_t c;
1828     c=(gcry_cipher_hd_t)*cipher;
1829 #endif
1830     ssl_debug_printf("--------------------------------------------------------------------");
1831 #if 0
1832     for(ivp=c->iv,i=0; i < iv_len; i++ )
1833         {
1834         ssl_debug_printf("%d ",ivp[i]);
1835         i++;
1836         }
1837 #endif
1838     ssl_debug_printf("--------------------------------------------------------------------");
1839     ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
1840 #if 0
1841     for(ivp=c->iv,i=0; i < iv_len; i++ )
1842         {
1843         ssl_debug_printf("%d ",ivp[i]);
1844         i++;
1845         }
1846 #endif
1847     ssl_debug_printf("--------------------------------------------------------------------");
1848     return ret;
1849 }
1850 /* stream cipher abstraction layer*/
1851 static gint
1852 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
1853         guchar* iv, gint mode)
1854 {
1855     gint gcry_modes[] = {
1856         GCRY_CIPHER_MODE_STREAM,
1857         GCRY_CIPHER_MODE_CBC,
1858 #ifdef HAVE_LIBGCRYPT_AEAD
1859         GCRY_CIPHER_MODE_GCM,
1860         GCRY_CIPHER_MODE_CCM,
1861         GCRY_CIPHER_MODE_CCM
1862 #else
1863         GCRY_CIPHER_MODE_CTR,
1864         GCRY_CIPHER_MODE_CTR,
1865         GCRY_CIPHER_MODE_CTR,
1866 #endif
1867     };
1868     gint err;
1869     if (algo == -1) {
1870         /* NULL mode */
1871         *(cipher) = (gcry_cipher_hd_t)-1;
1872         return 0;
1873     }
1874     err = gcry_cipher_open(cipher, algo, gcry_modes[mode], 0);
1875     if (err !=0)
1876         return  -1;
1877     err = gcry_cipher_setkey(*(cipher), sk, gcry_cipher_get_algo_keylen (algo));
1878     if (err != 0)
1879         return -1;
1880     /* AEAD cipher suites will set the nonce later. */
1881     if (mode == MODE_CBC) {
1882         err = gcry_cipher_setiv(*(cipher), iv, gcry_cipher_get_algo_blklen(algo));
1883         if (err != 0)
1884             return -1;
1885     }
1886     return 0;
1887 }
1888 static inline gint
1889 ssl_cipher_decrypt(gcry_cipher_hd_t *cipher, guchar * out, gint outl,
1890                    const guchar * in, gint inl)
1891 {
1892     if ((*cipher) == (gcry_cipher_hd_t)-1)
1893     {
1894         if (in && inl)
1895             memcpy(out, in, outl < inl ? outl : inl);
1896         return 0;
1897     }
1898     return gcry_cipher_decrypt ( *(cipher), out, outl, in, inl);
1899 }
1900 static inline gint
1901 ssl_get_digest_by_name(const gchar*name)
1902 {
1903     return gcry_md_map_name(name);
1904 }
1905 static inline gint
1906 ssl_get_cipher_by_name(const gchar* name)
1907 {
1908     return gcry_cipher_map_name(name);
1909 }
1910
1911 static inline void
1912 ssl_cipher_cleanup(gcry_cipher_hd_t *cipher)
1913 {
1914     if ((*cipher) != (gcry_cipher_hd_t)-1)
1915         gcry_cipher_close(*cipher);
1916     *cipher = NULL;
1917 }
1918 /* libgcrypt wrappers for Cipher state manipulation }}} */
1919
1920 #ifdef HAVE_LIBGNUTLS
1921 /* libgcrypt wrapper to decrypt using a RSA private key {{{ */
1922 /* decrypt data with private key. Store decrypted data directly into input
1923  * buffer */
1924 static int
1925 ssl_private_decrypt(const guint len, guchar* data, gcry_sexp_t pk)
1926 {
1927     gint        rc = 0;
1928     size_t      decr_len = 0, i = 0;
1929     gcry_sexp_t s_data = NULL, s_plain = NULL;
1930     gcry_mpi_t  encr_mpi = NULL, text = NULL;
1931
1932     /* create mpi representation of encrypted data */
1933     rc = gcry_mpi_scan(&encr_mpi, GCRYMPI_FMT_USG, data, len, NULL);
1934     if (rc != 0 ) {
1935         ssl_debug_printf("pcry_private_decrypt: can't convert data to mpi (size %d):%s\n",
1936             len, gcry_strerror(rc));
1937         return 0;
1938     }
1939
1940     /* put the data into a simple list */
1941     rc = gcry_sexp_build(&s_data, NULL, "(enc-val(rsa(a%m)))", encr_mpi);
1942     if (rc != 0) {
1943         ssl_debug_printf("pcry_private_decrypt: can't build encr_sexp:%s\n",
1944              gcry_strerror(rc));
1945         decr_len = 0;
1946         goto out;
1947     }
1948
1949     /* pass it to libgcrypt */
1950     rc = gcry_pk_decrypt(&s_plain, s_data, pk);
1951     if (rc != 0)
1952     {
1953         ssl_debug_printf("pcry_private_decrypt: can't decrypt key:%s\n",
1954             gcry_strerror(rc));
1955         decr_len = 0;
1956         goto out;
1957     }
1958
1959     /* convert plain text sexp to mpi format */
1960     text = gcry_sexp_nth_mpi(s_plain, 0, 0);
1961     if (! text) {
1962         ssl_debug_printf("pcry_private_decrypt: can't convert sexp to mpi\n");
1963         decr_len = 0;
1964         goto out;
1965     }
1966
1967     /* compute size requested for plaintext buffer */
1968     rc = gcry_mpi_print(GCRYMPI_FMT_USG, NULL, 0, &decr_len, text);
1969     if (rc != 0) {
1970         ssl_debug_printf("pcry_private_decrypt: can't compute decr size:%s\n",
1971             gcry_strerror(rc));
1972         decr_len = 0;
1973         goto out;
1974     }
1975
1976     /* sanity check on out buffer */
1977     if (decr_len > len) {
1978         ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%" G_GSIZE_MODIFIER "u max %d)\n", decr_len, len);
1979         decr_len = 0;
1980         goto out;
1981     }
1982
1983     /* write plain text to newly allocated buffer */
1984     rc = gcry_mpi_print(GCRYMPI_FMT_USG, data, len, &decr_len, text);
1985     if (rc != 0) {
1986         ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %" G_GSIZE_MODIFIER "u):%s\n", decr_len, gcry_strerror(rc));
1987         decr_len = 0;
1988         goto out;
1989     }
1990
1991     ssl_print_data("decrypted_unstrip_pre_master", data, decr_len);
1992
1993     /* strip the padding*/
1994     rc = 0;
1995     for (i = 1; i < decr_len; i++) {
1996         if (data[i] == 0) {
1997             rc = (gint) i+1;
1998             break;
1999         }
2000     }
2001
2002     ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %" G_GSIZE_MODIFIER "u\n", rc, decr_len);
2003     decr_len -= rc;
2004     memmove(data, data+rc, decr_len);
2005
2006 out:
2007     gcry_sexp_release(s_data);
2008     gcry_sexp_release(s_plain);
2009     gcry_mpi_release(encr_mpi);
2010     gcry_mpi_release(text);
2011     return (int) decr_len;
2012 } /* }}} */
2013 #endif /* HAVE_LIBGNUTLS */
2014
2015 #else /* ! HAVE_LIBGCRYPT */
2016
2017 gint
2018 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher _U_, guchar* iv _U_, gint iv_len _U_)
2019 {
2020     ssl_debug_printf("ssl_cipher_setiv: impossible without gnutls.\n");
2021     return 0;
2022 }
2023 #endif /* ! HAVE_LIBGCRYPT */
2024
2025
2026 #ifdef HAVE_LIBGCRYPT /* Save space if decryption is not enabled. */
2027
2028 /* Digests, Ciphers and Cipher Suites registry {{{ */
2029 static const SslDigestAlgo digests[]={
2030     {"MD5",     16},
2031     {"SHA1",    20},
2032     {"SHA256",  32},
2033     {"SHA384",  48},
2034     {"Not Applicable",  0},
2035 };
2036
2037 #define DIGEST_MAX_SIZE 48
2038
2039 /* get index digest index */
2040 static const SslDigestAlgo *
2041 ssl_cipher_suite_dig(const SslCipherSuite *cs) {
2042     return &digests[cs->dig - DIG_MD5];
2043 }
2044
2045 static const gchar *ciphers[]={
2046     "DES",
2047     "3DES",
2048     "ARCFOUR", /* libgcrypt does not support rc4, but this should be 100% compatible*/
2049     "RFC2268_128", /* libgcrypt name for RC2 with a 128-bit key */
2050     "IDEA",
2051     "AES",
2052     "AES256",
2053     "CAMELLIA128",
2054     "CAMELLIA256",
2055     "SEED",
2056     "*UNKNOWN*"
2057 };
2058
2059 static const SslCipherSuite cipher_suites[]={
2060     {0x0001,KEX_RSA,            ENC_NULL,       DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_MD5 */
2061     {0x0002,KEX_RSA,            ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA */
2062     {0x0003,KEX_RSA,            ENC_RC4,        DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 */
2063     {0x0004,KEX_RSA,            ENC_RC4,        DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_MD5 */
2064     {0x0005,KEX_RSA,            ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_SHA */
2065     {0x0006,KEX_RSA,            ENC_RC2,        DIG_MD5,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 */
2066     {0x0007,KEX_RSA,            ENC_IDEA,       DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_IDEA_CBC_SHA */
2067     {0x0008,KEX_RSA,            ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA */
2068     {0x0009,KEX_RSA,            ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_DES_CBC_SHA */
2069     {0x000A,KEX_RSA,            ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_3DES_EDE_CBC_SHA */
2070     {0x000B,KEX_DH_DSS,         ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA */
2071     {0x000C,KEX_DH_DSS,         ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_DES_CBC_SHA */
2072     {0x000D,KEX_DH_DSS,         ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA */
2073     {0x000E,KEX_DH_RSA,         ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA */
2074     {0x000F,KEX_DH_RSA,         ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_DES_CBC_SHA */
2075     {0x0010,KEX_DH_RSA,         ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA */
2076     {0x0011,KEX_DHE_DSS,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA */
2077     {0x0012,KEX_DHE_DSS,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_DES_CBC_SHA */
2078     {0x0013,KEX_DHE_DSS,        ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA */
2079     {0x0014,KEX_DHE_RSA,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA */
2080     {0x0015,KEX_DHE_RSA,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_DES_CBC_SHA */
2081     {0x0016,KEX_DHE_RSA,        ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */
2082     {0x0017,KEX_DH_ANON,        ENC_RC4,        DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 */
2083     {0x0018,KEX_DH_ANON,        ENC_RC4,        DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_WITH_RC4_128_MD5 */
2084     {0x0019,KEX_DH_ANON,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA */
2085     {0x001A,KEX_DH_ANON,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_DES_CBC_SHA */
2086     {0x001B,KEX_DH_ANON,        ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_3DES_EDE_CBC_SHA */
2087     {0x002C,KEX_PSK,            ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA */
2088     {0x002D,KEX_DHE_PSK,        ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA */
2089     {0x002E,KEX_RSA_PSK,        ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA */
2090     {0x002F,KEX_RSA,            ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA */
2091     {0x0030,KEX_DH_DSS,         ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA */
2092     {0x0031,KEX_DH_RSA,         ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA */
2093     {0x0032,KEX_DHE_DSS,        ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA */
2094     {0x0033,KEX_DHE_RSA,        ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
2095     {0x0034,KEX_DH_ANON,        ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA */
2096     {0x0035,KEX_RSA,            ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA */
2097     {0x0036,KEX_DH_DSS,         ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA */
2098     {0x0037,KEX_DH_RSA,         ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA */
2099     {0x0038,KEX_DHE_DSS,        ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA */
2100     {0x0039,KEX_DHE_RSA,        ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */
2101     {0x003A,KEX_DH_ANON,        ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA */
2102     {0x003B,KEX_RSA,            ENC_NULL,       DIG_SHA256, MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA256 */
2103     {0x003C,KEX_RSA,            ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA256 */
2104     {0x003D,KEX_RSA,            ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
2105     {0x003E,KEX_DH_DSS,         ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA256 */
2106     {0x003F,KEX_DH_RSA,         ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA256 */
2107     {0x0040,KEX_DHE_DSS,        ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 */
2108     {0x0041,KEX_RSA,            ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */
2109     {0x0042,KEX_DH_DSS,         ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA */
2110     {0x0043,KEX_DH_RSA,         ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA */
2111     {0x0044,KEX_DHE_DSS,        ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA */
2112     {0x0045,KEX_DHE_RSA,        ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */
2113     {0x0046,KEX_DH_ANON,        ENC_CAMELLIA128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA */
2114     {0x0060,KEX_RSA,            ENC_RC4,        DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC4_56_MD5 */
2115     {0x0061,KEX_RSA,            ENC_RC2,        DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5 */
2116     {0x0062,KEX_RSA,            ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA */
2117     {0x0063,KEX_DHE_DSS,        ENC_DES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA */
2118     {0x0064,KEX_RSA,            ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC4_56_SHA */
2119     {0x0065,KEX_DHE_DSS,        ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA */
2120     {0x0066,KEX_DHE_DSS,        ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_DHE_DSS_WITH_RC4_128_SHA */
2121     {0x0067,KEX_DHE_RSA,        ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */
2122     {0x0068,KEX_DH_DSS,         ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA256 */
2123     {0x0069,KEX_DH_RSA,         ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA256 */
2124     {0x006A,KEX_DHE_DSS,        ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 */
2125     {0x006B,KEX_DHE_RSA,        ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */
2126     {0x006C,KEX_DH_ANON,        ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA256 */
2127     {0x006D,KEX_DH_ANON,        ENC_AES256,     DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA256 */
2128     {0x0084,KEX_RSA,            ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */
2129     {0x0085,KEX_DH_DSS,         ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA */
2130     {0x0086,KEX_DH_RSA,         ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA */
2131     {0x0087,KEX_DHE_DSS,        ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA */
2132     {0x0088,KEX_DHE_RSA,        ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */
2133     {0x0089,KEX_DH_ANON,        ENC_CAMELLIA256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA */
2134     {0x008A,KEX_PSK,            ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_PSK_WITH_RC4_128_SHA */
2135     {0x008B,KEX_PSK,            ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_3DES_EDE_CBC_SHA */
2136     {0x008C,KEX_PSK,            ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_AES_128_CBC_SHA */
2137     {0x008D,KEX_PSK,            ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_AES_256_CBC_SHA */
2138     {0x008E,KEX_DHE_PSK,        ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_DHE_PSK_WITH_RC4_128_SHA */
2139     {0x008F,KEX_DHE_PSK,        ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA */
2140     {0x0090,KEX_DHE_PSK,        ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA */
2141     {0x0091,KEX_DHE_PSK,        ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA */
2142     {0x0092,KEX_RSA_PSK,        ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_RSA_PSK_WITH_RC4_128_SHA */
2143     {0x0093,KEX_RSA_PSK,        ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA */
2144     {0x0094,KEX_RSA_PSK,        ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_128_CBC_SHA */
2145     {0x0095,KEX_RSA_PSK,        ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_256_CBC_SHA */
2146     {0x0096,KEX_RSA,            ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_SEED_CBC_SHA */
2147     {0x0097,KEX_DH_DSS,         ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_SEED_CBC_SHA */
2148     {0x0098,KEX_DH_RSA,         ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_SEED_CBC_SHA */
2149     {0x0099,KEX_DHE_DSS,        ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_SEED_CBC_SHA */
2150     {0x009A,KEX_DHE_RSA,        ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_SEED_CBC_SHA */
2151     {0x009B,KEX_DH_ANON,        ENC_SEED,       DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_SEED_CBC_SHA */
2152     {0x009C,KEX_RSA,            ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_RSA_WITH_AES_128_GCM_SHA256 */
2153     {0x009D,KEX_RSA,            ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_RSA_WITH_AES_256_GCM_SHA384 */
2154     {0x009E,KEX_DHE_RSA,        ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */
2155     {0x009F,KEX_DHE_RSA,        ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */
2156     {0x00A0,KEX_DH_RSA,         ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_128_GCM_SHA256 */
2157     {0x00A1,KEX_DH_RSA,         ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_256_GCM_SHA384 */
2158     {0x00A2,KEX_DHE_DSS,        ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 */
2159     {0x00A3,KEX_DHE_DSS,        ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 */
2160     {0x00A4,KEX_DH_DSS,         ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_128_GCM_SHA256 */
2161     {0x00A5,KEX_DH_DSS,         ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_256_GCM_SHA384 */
2162     {0x00A6,KEX_DH_ANON,        ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_128_GCM_SHA256 */
2163     {0x00A7,KEX_DH_ANON,        ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */
2164     {0x00A8,KEX_PSK,            ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_PSK_WITH_AES_128_GCM_SHA256 */
2165     {0x00A9,KEX_PSK,            ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_PSK_WITH_AES_256_GCM_SHA384 */
2166     {0x00AA,KEX_DHE_PSK,        ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */
2167     {0x00AB,KEX_DHE_PSK,        ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */
2168     {0x00AC,KEX_RSA_PSK,        ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_RSA_PSK_WITH_AES_128_GCM_SHA256 */
2169     {0x00AD,KEX_RSA_PSK,        ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_RSA_PSK_WITH_AES_256_GCM_SHA384 */
2170     {0x00AE,KEX_PSK,            ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_PSK_WITH_AES_128_CBC_SHA256 */
2171     {0x00AF,KEX_PSK,            ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_PSK_WITH_AES_256_CBC_SHA384 */
2172     {0x00B0,KEX_PSK,            ENC_NULL,       DIG_SHA256, MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA256 */
2173     {0x00B1,KEX_PSK,            ENC_NULL,       DIG_SHA384, MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA384 */
2174     {0x00B2,KEX_DHE_PSK,        ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */
2175     {0x00B3,KEX_DHE_PSK,        ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */
2176     {0x00B4,KEX_DHE_PSK,        ENC_NULL,       DIG_SHA256, MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA256 */
2177     {0x00B5,KEX_DHE_PSK,        ENC_NULL,       DIG_SHA384, MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA384 */
2178     {0x00B6,KEX_RSA_PSK,        ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_128_CBC_SHA256 */
2179     {0x00B7,KEX_RSA_PSK,        ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_256_CBC_SHA384 */
2180     {0x00B8,KEX_RSA_PSK,        ENC_NULL,       DIG_SHA256, MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA256 */
2181     {0x00B9,KEX_RSA_PSK,        ENC_NULL,       DIG_SHA384, MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA384 */
2182     {0x00BA,KEX_RSA,            ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2183     {0x00BB,KEX_DH_DSS,         ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
2184     {0x00BC,KEX_DH_RSA,         ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2185     {0x00BD,KEX_DHE_DSS,        ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
2186     {0x00BE,KEX_DHE_RSA,        ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2187     {0x00BF,KEX_DH_ANON,        ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 */
2188     {0x00C0,KEX_RSA,            ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
2189     {0x00C1,KEX_DH_DSS,         ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
2190     {0x00C2,KEX_DH_RSA,         ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
2191     {0x00C3,KEX_DHE_DSS,        ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
2192     {0x00C4,KEX_DHE_RSA,        ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
2193     {0x00C5,KEX_DH_ANON,        ENC_CAMELLIA256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 */
2194
2195     /* NOTE: TLS 1.3 cipher suites are incompatible with TLS 1.2. */
2196     {0x1301,KEX_TLS13,          ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_AES_128_GCM_SHA256 */
2197     {0x1302,KEX_TLS13,          ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_AES_256_GCM_SHA384 */
2198     /* TODO TLS_CHACHA20_POLY1305_SHA256 */
2199     {0x1304,KEX_TLS13,          ENC_AES,        DIG_SHA256, MODE_CCM   },   /* TLS_AES_128_CCM_SHA256 */
2200     {0x1305,KEX_TLS13,          ENC_AES,        DIG_SHA256, MODE_CCM_8 },   /* TLS_AES_128_CCM_8_SHA256 */
2201
2202     {0xC001,KEX_ECDH_ECDSA,     ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_NULL_SHA */
2203     {0xC002,KEX_ECDH_ECDSA,     ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */
2204     {0xC003,KEX_ECDH_ECDSA,     ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */
2205     {0xC004,KEX_ECDH_ECDSA,     ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */
2206     {0xC005,KEX_ECDH_ECDSA,     ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */
2207     {0xC006,KEX_ECDHE_ECDSA,    ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */
2208     {0xC007,KEX_ECDHE_ECDSA,    ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */
2209     {0xC008,KEX_ECDHE_ECDSA,    ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */
2210     {0xC009,KEX_ECDHE_ECDSA,    ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */
2211     {0xC00A,KEX_ECDHE_ECDSA,    ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */
2212     {0xC00B,KEX_ECDH_RSA,       ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_NULL_SHA */
2213     {0xC00C,KEX_ECDH_RSA,       ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_RC4_128_SHA */
2214     {0xC00D,KEX_ECDH_RSA,       ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */
2215     {0xC00E,KEX_ECDH_RSA,       ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */
2216     {0xC00F,KEX_ECDH_RSA,       ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */
2217     {0xC010,KEX_ECDHE_RSA,      ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_NULL_SHA */
2218     {0xC011,KEX_ECDHE_RSA,      ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */
2219     {0xC012,KEX_ECDHE_RSA,      ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */
2220     {0xC013,KEX_ECDHE_RSA,      ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */
2221     {0xC014,KEX_ECDHE_RSA,      ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */
2222     {0xC015,KEX_ECDH_ANON,      ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_NULL_SHA */
2223     {0xC016,KEX_ECDH_ANON,      ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_RC4_128_SHA */
2224     {0xC017,KEX_ECDH_ANON,      ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA */
2225     {0xC018,KEX_ECDH_ANON,      ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_128_CBC_SHA */
2226     {0xC019,KEX_ECDH_ANON,      ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_256_CBC_SHA */
2227     {0xC023,KEX_ECDHE_ECDSA,    ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 */
2228     {0xC024,KEX_ECDHE_ECDSA,    ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 */
2229     {0xC025,KEX_ECDH_ECDSA,     ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */
2230     {0xC026,KEX_ECDH_ECDSA,     ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */
2231     {0xC027,KEX_ECDHE_RSA,      ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */
2232     {0xC028,KEX_ECDHE_RSA,      ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */
2233     {0xC029,KEX_ECDH_RSA,       ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */
2234     {0xC02A,KEX_ECDH_RSA,       ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */
2235     {0xC02B,KEX_ECDHE_ECDSA,    ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */
2236     {0xC02C,KEX_ECDHE_ECDSA,    ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */
2237     {0xC02D,KEX_ECDH_ECDSA,     ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */
2238     {0xC02E,KEX_ECDH_ECDSA,     ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */
2239     {0xC02F,KEX_ECDHE_RSA,      ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
2240     {0xC030,KEX_ECDHE_RSA,      ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */
2241     {0xC031,KEX_ECDH_RSA,       ENC_AES,        DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */
2242     {0xC032,KEX_ECDH_RSA,       ENC_AES256,     DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */
2243     {0xC033,KEX_ECDHE_PSK,      ENC_RC4,        DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_RC4_128_SHA */
2244     {0xC034,KEX_ECDHE_PSK,      ENC_3DES,       DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA */
2245     {0xC035,KEX_ECDHE_PSK,      ENC_AES,        DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA */
2246     {0xC036,KEX_ECDHE_PSK,      ENC_AES256,     DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA */
2247     {0xC037,KEX_ECDHE_PSK,      ENC_AES,        DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */
2248     {0xC038,KEX_ECDHE_PSK,      ENC_AES256,     DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 */
2249     {0xC039,KEX_ECDHE_PSK,      ENC_NULL,       DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA */
2250     {0xC03A,KEX_ECDHE_PSK,      ENC_NULL,       DIG_SHA256, MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */
2251     {0xC03B,KEX_ECDHE_PSK,      ENC_NULL,       DIG_SHA384, MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA384 */
2252     {0xC072,KEX_ECDHE_ECDSA,    ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 */
2253     {0xC073,KEX_ECDHE_ECDSA,    ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 */
2254     {0xC074,KEX_ECDH_ECDSA,     ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 */
2255     {0xC075,KEX_ECDH_ECDSA,     ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 */
2256     {0xC076,KEX_ECDHE_RSA,      ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2257     {0xC077,KEX_ECDHE_RSA,      ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 */
2258     {0xC078,KEX_ECDH_RSA,       ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2259     {0xC079,KEX_ECDH_RSA,       ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 */
2260     {0xC07A,KEX_RSA,            ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2261     {0xC07B,KEX_RSA,            ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2262     {0xC07C,KEX_DHE_RSA,        ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2263     {0xC07D,KEX_DHE_RSA,        ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2264     {0xC07E,KEX_DH_RSA,         ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2265     {0xC07F,KEX_DH_RSA,         ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2266     {0xC080,KEX_DHE_DSS,        ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256 */
2267     {0xC081,KEX_DHE_DSS,        ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384 */
2268     {0xC082,KEX_DH_DSS,         ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256 */
2269     {0xC083,KEX_DH_DSS,         ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384 */
2270     {0xC084,KEX_DH_ANON,        ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256 */
2271     {0xC085,KEX_DH_ANON,        ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384 */
2272     {0xC086,KEX_ECDHE_ECDSA,    ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 */
2273     {0xC087,KEX_ECDHE_ECDSA,    ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 */
2274     {0xC088,KEX_ECDH_ECDSA,     ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 */
2275     {0xC089,KEX_ECDH_ECDSA,     ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 */
2276     {0xC08A,KEX_ECDHE_RSA,      ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2277     {0xC08B,KEX_ECDHE_RSA,      ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2278     {0xC08C,KEX_ECDH_RSA,       ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2279     {0xC08D,KEX_ECDH_RSA,       ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2280     {0xC08E,KEX_PSK,            ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2281     {0xC08F,KEX_PSK,            ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2282     {0xC090,KEX_DHE_PSK,        ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2283     {0xC091,KEX_DHE_PSK,        ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2284     {0xC092,KEX_RSA_PSK,        ENC_CAMELLIA128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2285     {0xC093,KEX_RSA_PSK,        ENC_CAMELLIA256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2286     {0xC094,KEX_PSK,            ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2287     {0xC095,KEX_PSK,            ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2288     {0xC096,KEX_DHE_PSK,        ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2289     {0xC097,KEX_DHE_PSK,        ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2290     {0xC098,KEX_RSA_PSK,        ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2291     {0xC099,KEX_RSA_PSK,        ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2292     {0xC09A,KEX_ECDHE_PSK,      ENC_CAMELLIA128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2293     {0xC09B,KEX_ECDHE_PSK,      ENC_CAMELLIA256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2294     {0xC09C,KEX_RSA,            ENC_AES,        DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_128_CCM */
2295     {0xC09D,KEX_RSA,            ENC_AES256,     DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_256_CCM */
2296     {0xC09E,KEX_DHE_RSA,        ENC_AES,        DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_128_CCM */
2297     {0xC09F,KEX_DHE_RSA,        ENC_AES256,     DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_256_CCM */
2298     {0xC0A0,KEX_RSA,            ENC_AES,        DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_128_CCM_8 */
2299     {0xC0A1,KEX_RSA,            ENC_AES256,     DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_256_CCM_8 */
2300     {0xC0A2,KEX_DHE_RSA,        ENC_AES,        DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_128_CCM_8 */
2301     {0xC0A3,KEX_DHE_RSA,        ENC_AES256,     DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_256_CCM_8 */
2302     {0xC0A4,KEX_PSK,            ENC_AES,        DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_128_CCM */
2303     {0xC0A5,KEX_PSK,            ENC_AES256,     DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_256_CCM */
2304     {0xC0A6,KEX_DHE_PSK,        ENC_AES,        DIG_NA,     MODE_CCM   },   /* TLS_DHE_PSK_WITH_AES_128_CCM */
2305     {0xC0A7,KEX_DHE_PSK,        ENC_AES256,     DIG_NA,     MODE_CCM   },   /* TLS_DHE_PSK_WITH_AES_256_CCM */
2306     {0xC0A8,KEX_PSK,            ENC_AES,        DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_128_CCM_8 */
2307     {0xC0A9,KEX_PSK,            ENC_AES256,     DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_256_CCM_8 */
2308     {0xC0AA,KEX_DHE_PSK,        ENC_AES,        DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_DHE_WITH_AES_128_CCM_8 */
2309     {0xC0AB,KEX_DHE_PSK,        ENC_AES256,     DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_DHE_WITH_AES_256_CCM_8 */
2310     {0xC0AC,KEX_ECDHE_ECDSA,    ENC_AES,        DIG_NA,     MODE_CCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */
2311     {0xC0AD,KEX_ECDHE_ECDSA,    ENC_AES256,     DIG_NA,     MODE_CCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM */
2312     {0xC0AE,KEX_ECDHE_ECDSA,    ENC_AES,        DIG_NA,     MODE_CCM_8 },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */
2313     {0xC0AF,KEX_ECDHE_ECDSA,    ENC_AES256,     DIG_NA,     MODE_CCM_8 },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */
2314     {-1,    0,                  0,              0,          MODE_STREAM}
2315 };
2316
2317 #define MAX_BLOCK_SIZE 16
2318 #define MAX_KEY_SIZE 32
2319
2320 const SslCipherSuite *
2321 ssl_find_cipher(int num)
2322 {
2323     const SslCipherSuite *c;
2324     for(c=cipher_suites;c->number!=-1;c++){
2325         if(c->number==num){
2326             return c;
2327         }
2328     }
2329
2330     return NULL;
2331 }
2332
2333 guint
2334 ssl_get_cipher_blocksize(const SslCipherSuite *cipher_suite)
2335 {
2336     gint cipher_algo;
2337     if (cipher_suite->mode != MODE_CBC) return 0;
2338     cipher_algo = ssl_get_cipher_by_name(ciphers[cipher_suite->enc - 0x30]);
2339     return (guint)gcry_cipher_get_algo_blklen(cipher_algo);
2340 }
2341
2342 static guint
2343 ssl_get_cipher_export_keymat_size(int cipher_suite_num)
2344 {
2345     switch (cipher_suite_num) {
2346     /* See RFC 6101 (SSL 3.0), Table 2, column Key Material. */
2347     case 0x0003:    /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 */
2348     case 0x0006:    /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 */
2349     case 0x0008:    /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA */
2350     case 0x000B:    /* TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA */
2351     case 0x000E:    /* TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA */
2352     case 0x0011:    /* TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA */
2353     case 0x0014:    /* TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA */
2354     case 0x0017:    /* TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 */
2355     case 0x0019:    /* TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA */
2356         return 5;
2357
2358     /* not defined in below draft, but "implemented by several vendors",
2359      * https://www.ietf.org/mail-archive/web/tls/current/msg00036.html */
2360     case 0x0060:    /* TLS_RSA_EXPORT1024_WITH_RC4_56_MD5 */
2361     case 0x0061:    /* TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5 */
2362         return 7;
2363
2364     /* Note: the draft states that DES_CBC needs 8 bytes, but Wireshark always
2365      * used 7. Until a pcap proves 8, let's use the old value. Link:
2366      * https://tools.ietf.org/html/draft-ietf-tls-56-bit-ciphersuites-01 */
2367     case 0x0062:    /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA */
2368     case 0x0063:    /* TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA */
2369     case 0x0064:    /* TLS_RSA_EXPORT1024_WITH_RC4_56_SHA */
2370     case 0x0065:    /* TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA */
2371         return 7;
2372
2373     default:
2374         return 0;
2375     }
2376 }
2377 #else /* ! HAVE_LIBGCRYPT */
2378 const SslCipherSuite *
2379 ssl_find_cipher(int num)
2380 {
2381     ssl_debug_printf("ssl_find_cipher: dummy without gnutls. num %d\n",
2382         num);
2383     return NULL;
2384 }
2385
2386 guint
2387 ssl_get_cipher_blocksize(const SslCipherSuite *cipher_suite _U_)
2388 {
2389     return 0;
2390 }
2391 #endif /* ! HAVE_LIBGCRYPT */
2392
2393 /* Digests, Ciphers and Cipher Suites registry }}} */
2394
2395
2396 #ifdef HAVE_LIBGCRYPT
2397
2398 /* HMAC and the Pseudorandom function {{{ */
2399 static void
2400 tls_hash(StringInfo *secret, StringInfo *seed, gint md,
2401          StringInfo *out, guint out_len)
2402 {
2403     /* RFC 2246 5. HMAC and the pseudorandom function
2404      * '+' denotes concatenation.
2405      * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
2406      *                        HMAC_hash(secret, A(2) + seed) + ...
2407      * A(0) = seed
2408      * A(i) = HMAC_hash(secret, A(i - 1))
2409      */
2410     guint8   *ptr;
2411     guint     left, tocpy;
2412     guint8   *A;
2413     guint8    _A[DIGEST_MAX_SIZE], tmp[DIGEST_MAX_SIZE];
2414     guint     A_l, tmp_l;
2415     SSL_HMAC  hm;
2416
2417     ptr  = out->data;
2418     left = out_len;
2419
2420     ssl_print_string("tls_hash: hash secret", secret);
2421     ssl_print_string("tls_hash: hash seed", seed);
2422     /* A(0) = seed */
2423     A = seed->data;
2424     A_l = seed->data_len;
2425
2426     while (left) {
2427         /* A(i) = HMAC_hash(secret, A(i-1)) */
2428         ssl_hmac_init(&hm, secret->data, secret->data_len, md);
2429         ssl_hmac_update(&hm, A, A_l);
2430         A_l = sizeof(_A); /* upper bound len for hash output */
2431         ssl_hmac_final(&hm, _A, &A_l);
2432         ssl_hmac_cleanup(&hm);
2433         A = _A;
2434
2435         /* HMAC_hash(secret, A(i) + seed) */
2436         ssl_hmac_init(&hm, secret->data, secret->data_len, md);
2437         ssl_hmac_update(&hm, A, A_l);
2438         ssl_hmac_update(&hm, seed->data, seed->data_len);
2439         tmp_l = sizeof(tmp); /* upper bound len for hash output */
2440         ssl_hmac_final(&hm, tmp, &tmp_l);
2441         ssl_hmac_cleanup(&hm);
2442
2443         /* ssl_hmac_final puts the actual digest output size in tmp_l */
2444         tocpy = MIN(left, tmp_l);
2445         memcpy(ptr, tmp, tocpy);
2446         ptr += tocpy;
2447         left -= tocpy;
2448     }
2449     out->data_len = out_len;
2450
2451     ssl_print_string("hash out", out);
2452 }
2453
2454 static gboolean
2455 tls_prf(StringInfo* secret, const gchar *usage,
2456         StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2457 {
2458     StringInfo  seed, sha_out, md5_out;
2459     guint8     *ptr;
2460     StringInfo  s1, s2;
2461     guint       i,s_l;
2462     size_t      usage_len, rnd2_len;
2463     gboolean    success = FALSE;
2464     usage_len = strlen(usage);
2465     rnd2_len = rnd2 ? rnd2->data_len : 0;
2466
2467     /* initalize buffer for sha, md5 random seed*/
2468     if (ssl_data_alloc(&sha_out, MAX(out_len, 20)) < 0) {
2469         ssl_debug_printf("tls_prf: can't allocate sha out\n");
2470         return FALSE;
2471     }
2472     if (ssl_data_alloc(&md5_out, MAX(out_len, 16)) < 0) {
2473         ssl_debug_printf("tls_prf: can't allocate md5 out\n");
2474         goto free_sha;
2475     }
2476     if (ssl_data_alloc(&seed, usage_len+rnd1->data_len+rnd2_len) < 0) {
2477         ssl_debug_printf("tls_prf: can't allocate rnd %d\n",
2478                          (int) (usage_len+rnd1->data_len+rnd2_len));
2479         goto free_md5;
2480     }
2481
2482     ptr=seed.data;
2483     memcpy(ptr,usage,usage_len);
2484     ptr+=usage_len;
2485     memcpy(ptr,rnd1->data,rnd1->data_len);
2486     if (rnd2_len > 0) {
2487         ptr+=rnd1->data_len;
2488         memcpy(ptr,rnd2->data,rnd2->data_len);
2489         /*ptr+=rnd2->data_len;*/
2490     }
2491
2492     /* initalize buffer for client/server seeds*/
2493     s_l=secret->data_len/2 + secret->data_len%2;
2494     if (ssl_data_alloc(&s1, s_l) < 0) {
2495         ssl_debug_printf("tls_prf: can't allocate secret %d\n", s_l);
2496         goto free_seed;
2497     }
2498     if (ssl_data_alloc(&s2, s_l) < 0) {
2499         ssl_debug_printf("tls_prf: can't allocate secret(2) %d\n", s_l);
2500         goto free_s1;
2501     }
2502
2503     memcpy(s1.data,secret->data,s_l);
2504     memcpy(s2.data,secret->data + (secret->data_len - s_l),s_l);
2505
2506     ssl_debug_printf("tls_prf: tls_hash(md5 secret_len %d seed_len %d )\n", s1.data_len, seed.data_len);
2507     tls_hash(&s1, &seed, ssl_get_digest_by_name("MD5"), &md5_out, out_len);
2508     ssl_debug_printf("tls_prf: tls_hash(sha)\n");
2509     tls_hash(&s2, &seed, ssl_get_digest_by_name("SHA1"), &sha_out, out_len);
2510
2511     for (i = 0; i < out_len; i++)
2512         out->data[i] = md5_out.data[i] ^ sha_out.data[i];
2513     /* success, now store the new meaningful data length */
2514     out->data_len = out_len;
2515     success = TRUE;
2516
2517     ssl_print_string("PRF out",out);
2518     g_free(s2.data);
2519 free_s1:
2520     g_free(s1.data);
2521 free_seed:
2522     g_free(seed.data);
2523 free_md5:
2524     g_free(md5_out.data);
2525 free_sha:
2526     g_free(sha_out.data);
2527     return success;
2528 }
2529
2530 static gboolean
2531 tls12_prf(gint md, StringInfo* secret, const gchar* usage,
2532           StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2533 {
2534     StringInfo label_seed;
2535     size_t     usage_len, rnd2_len;
2536     rnd2_len = rnd2 ? rnd2->data_len : 0;
2537
2538     usage_len = strlen(usage);
2539     if (ssl_data_alloc(&label_seed, usage_len+rnd1->data_len+rnd2_len) < 0) {
2540         ssl_debug_printf("tls12_prf: can't allocate label_seed\n");
2541         return FALSE;
2542     }
2543     memcpy(label_seed.data, usage, usage_len);
2544     memcpy(label_seed.data+usage_len, rnd1->data, rnd1->data_len);
2545     if (rnd2_len > 0)
2546         memcpy(label_seed.data+usage_len+rnd1->data_len, rnd2->data, rnd2->data_len);
2547
2548     ssl_debug_printf("tls12_prf: tls_hash(hash_alg %s secret_len %d seed_len %d )\n", gcry_md_algo_name(md), secret->data_len, label_seed.data_len);
2549     tls_hash(secret, &label_seed, md, out, out_len);
2550     g_free(label_seed.data);
2551     ssl_print_string("PRF out", out);
2552     return TRUE;
2553 }
2554
2555 static void
2556 ssl3_generate_export_iv(StringInfo *r1, StringInfo *r2,
2557                         StringInfo *out, guint out_len)
2558 {
2559     SSL_MD5_CTX md5;
2560     guint8      tmp[16];
2561
2562     ssl_md5_init(&md5);
2563     ssl_md5_update(&md5,r1->data,r1->data_len);
2564     ssl_md5_update(&md5,r2->data,r2->data_len);
2565     ssl_md5_final(tmp,&md5);
2566     ssl_md5_cleanup(&md5);
2567
2568     DISSECTOR_ASSERT(out_len <= sizeof(tmp));
2569     ssl_data_set(out, tmp, out_len);
2570     ssl_print_string("export iv", out);
2571 }
2572
2573 static gboolean
2574 ssl3_prf(StringInfo* secret, const gchar* usage,
2575          StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2576 {
2577     SSL_MD5_CTX  md5;
2578     SSL_SHA_CTX  sha;
2579     guint        off;
2580     gint         i = 0,j;
2581     guint8       buf[20];
2582
2583     for (off = 0; off < out_len; off += 16) {
2584         guchar outbuf[16];
2585         i++;
2586
2587         ssl_debug_printf("ssl3_prf: sha1_hash(%d)\n",i);
2588         /* A, BB, CCC,  ... */
2589         for(j=0;j<i;j++){
2590             buf[j]=64+i;
2591         }
2592
2593         ssl_sha_init(&sha);
2594         ssl_sha_update(&sha,buf,i);
2595         ssl_sha_update(&sha,secret->data,secret->data_len);
2596
2597         if(!strcmp(usage,"client write key") || !strcmp(usage,"server write key")){
2598             if (rnd2)
2599                 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2600             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2601         }
2602         else{
2603             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2604             if (rnd2)
2605                 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2606         }
2607
2608         ssl_sha_final(buf,&sha);
2609         ssl_sha_cleanup(&sha);
2610
2611         ssl_debug_printf("ssl3_prf: md5_hash(%d) datalen %d\n",i,
2612             secret->data_len);
2613         ssl_md5_init(&md5);
2614         ssl_md5_update(&md5,secret->data,secret->data_len);
2615         ssl_md5_update(&md5,buf,20);
2616         ssl_md5_final(outbuf,&md5);
2617         ssl_md5_cleanup(&md5);
2618
2619         memcpy(out->data + off, outbuf, MIN(out_len - off, 16));
2620     }
2621     out->data_len = out_len;
2622
2623     return TRUE;
2624 }
2625
2626 /* out_len is the wanted output length for the pseudorandom function.
2627  * Ensure that ssl->cipher_suite is set. */
2628 static gboolean
2629 prf(SslDecryptSession *ssl, StringInfo *secret, const gchar *usage,
2630     StringInfo *rnd1, StringInfo *rnd2, StringInfo *out, guint out_len)
2631 {
2632     switch (ssl->session.version) {
2633     case SSLV3_VERSION:
2634         return ssl3_prf(secret, usage, rnd1, rnd2, out, out_len);
2635
2636     case TLSV1_VERSION:
2637     case TLSV1DOT1_VERSION:
2638     case DTLSV1DOT0_VERSION:
2639     case DTLSV1DOT0_OPENSSL_VERSION:
2640         return tls_prf(secret, usage, rnd1, rnd2, out, out_len);
2641
2642     default: /* TLSv1.2 */
2643         switch (ssl->cipher_suite->dig) {
2644         case DIG_SHA384:
2645             return tls12_prf(GCRY_MD_SHA384, secret, usage, rnd1, rnd2,
2646                              out, out_len);
2647         default:
2648             return tls12_prf(GCRY_MD_SHA256, secret, usage, rnd1, rnd2,
2649                              out, out_len);
2650         }
2651     }
2652 }
2653
2654 static gint tls_handshake_hash(SslDecryptSession* ssl, StringInfo* out)
2655 {
2656     SSL_MD5_CTX  md5;
2657     SSL_SHA_CTX  sha;
2658
2659     if (ssl_data_alloc(out, 36) < 0)
2660         return -1;
2661
2662     ssl_md5_init(&md5);
2663     ssl_md5_update(&md5,ssl->handshake_data.data,ssl->handshake_data.data_len);
2664     ssl_md5_final(out->data,&md5);
2665     ssl_md5_cleanup(&md5);
2666
2667     ssl_sha_init(&sha);
2668     ssl_sha_update(&sha,ssl->handshake_data.data,ssl->handshake_data.data_len);
2669     ssl_sha_final(out->data+16,&sha);
2670     ssl_sha_cleanup(&sha);
2671     return 0;
2672 }
2673
2674 static gint tls12_handshake_hash(SslDecryptSession* ssl, gint md, StringInfo* out)
2675 {
2676     SSL_MD  mc;
2677     guint8 tmp[48];
2678     guint  len;
2679
2680     ssl_md_init(&mc, md);
2681     ssl_md_update(&mc,ssl->handshake_data.data,ssl->handshake_data.data_len);
2682     ssl_md_final(&mc, tmp, &len);
2683     ssl_md_cleanup(&mc);
2684
2685     if (ssl_data_alloc(out, len) < 0)
2686         return -1;
2687     memcpy(out->data, tmp, len);
2688     return 0;
2689 }
2690
2691 static gboolean
2692 tls13_hkdf_expand_label(int md, const StringInfo *secret, const char *label, const char *hash_value,
2693                         guint16 out_len, guchar **out)
2694 {
2695     /* draft-ietf-tls-tls13-18:
2696      * HKDF-Expand-Label(Secret, Label, HashValue, Length) =
2697      *      HKDF-Expand(Secret, HkdfLabel, Length)
2698      * struct {
2699      *     uint16 length = Length;
2700      *     opaque label<9..255> = "TLS 1.3, " + Label;
2701      *     opaque hash_value<0..255> = HashValue;
2702      * } HkdfLabel;
2703      *
2704      * RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF):
2705      * HKDF-Expand(PRK, info, L) -> OKM
2706      */
2707     guchar  lastoutput[DIGEST_MAX_SIZE];
2708     gcry_md_hd_t h;
2709     gcry_error_t err;
2710     const guint label_length = (guint) strlen(label);
2711     const guint hash_value_length = (guint) strlen(hash_value);
2712     const guint hash_len = gcry_md_get_algo_dlen(md);
2713
2714     /* Some sanity checks */
2715     DISSECTOR_ASSERT(out_len > 0 && out_len <= 255 * hash_len);
2716     DISSECTOR_ASSERT(label_length <= 255 - 9);
2717     DISSECTOR_ASSERT(hash_value_length <= 255);
2718     DISSECTOR_ASSERT(hash_len > 0 && hash_len <= DIGEST_MAX_SIZE);
2719
2720     err = gcry_md_open(&h, md, GCRY_MD_FLAG_HMAC);
2721     if (err) {
2722         ssl_debug_printf("%s failed to invoke hash func %d: %s\n", G_STRFUNC, md, gcry_strerror(err));
2723         return FALSE;
2724     }
2725
2726     *out = (guchar *)wmem_alloc(NULL, out_len);
2727
2728     for (guint offset = 0; offset < out_len; offset += hash_len) {
2729         gcry_md_reset(h);
2730         gcry_md_setkey(h, secret->data, secret->data_len);  /* Set PRK */
2731
2732         if (offset > 0) {
2733             gcry_md_write(h, lastoutput, hash_len);         /* T(1..N) */
2734         }
2735
2736         /* info = HkdfLabel { length, label, hash_value } */
2737         gcry_md_putc(h, out_len >> 8);                      /* length */
2738         gcry_md_putc(h, (guint8) out_len);
2739         gcry_md_putc(h, 9 + label_length);                  /* label */
2740         gcry_md_write(h, "TLS 1.3, ", 9);
2741         gcry_md_write(h, label, label_length);
2742         gcry_md_putc(h, hash_value_length);                 /* hash_value */
2743         gcry_md_write(h, hash_value, hash_value_length);
2744
2745         gcry_md_putc(h, (guint8) (offset / hash_len + 1));  /* constant 0x01..N */
2746
2747         memcpy(lastoutput, gcry_md_read(h, md), hash_len);
2748         memcpy(*out + offset, lastoutput, MIN(hash_len, out_len - offset));
2749     }
2750
2751     gcry_md_close(h);
2752     return TRUE;
2753 }
2754 /* HMAC and the Pseudorandom function }}} */
2755
2756 #else /* ! HAVE_LIBGCRYPT */
2757 /* Stub code when decryption support is not available. {{{ */
2758 gboolean
2759 ssl_generate_pre_master_secret(SslDecryptSession *ssl_session _U_,
2760         guint32 length _U_, tvbuff_t *tvb _U_, guint32 offset _U_,
2761         const gchar *ssl_psk _U_, const ssl_master_key_map_t *mk_map _U_)
2762 {
2763     ssl_debug_printf("%s: impossible without gnutls.\n", G_STRFUNC);
2764     return FALSE;
2765 }
2766 int
2767 ssl_generate_keyring_material(SslDecryptSession*ssl)
2768 {
2769     ssl_debug_printf("ssl_generate_keyring_material: impossible without gnutls. ssl %p\n",
2770         ssl);
2771     /* We cannot determine whether the cipher suite is valid. Fail such that
2772      * ssl_set_master_secret bails out. */
2773     return -1;
2774 }
2775 void
2776 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
2777 {
2778     ssl_debug_printf("ssl_change_cipher %s: makes no sense without gnutls. ssl %p\n",
2779         (server)?"SERVER":"CLIENT", ssl_session);
2780 }
2781
2782 int
2783 ssl_decrypt_record(SslDecryptSession *ssl, SslDecoder *decoder, guint8 ct, guint16 record_version,
2784         const guchar *in, guint16 inl, StringInfo *comp_str _U_, StringInfo *out_str, guint *outl)
2785 {
2786     ssl_debug_printf("ssl_decrypt_record: impossible without gnutls. ssl %p"
2787         "decoder %p ct %d version %d in %p inl %d out %p outl %p\n", ssl, decoder, ct,
2788         record_version, in, inl, out_str, outl);
2789     return 0;
2790 }
2791 /* }}} */
2792 #endif /* ! HAVE_LIBGCRYPT */
2793
2794 #ifdef HAVE_LIBGCRYPT
2795 /* Record Decompression (after decryption) {{{ */
2796 #ifdef HAVE_ZLIB
2797 /* memory allocation functions for zlib initialization */
2798 static void* ssl_zalloc(void* opaque _U_, unsigned int no, unsigned int size)
2799 {
2800     return g_malloc0(no*size);
2801 }
2802 static void ssl_zfree(void* opaque _U_, void* addr)
2803 {
2804     g_free(addr);
2805 }
2806 #endif
2807
2808 static SslDecompress*
2809 ssl_create_decompressor(gint compression)
2810 {
2811     SslDecompress *decomp;
2812 #ifdef HAVE_ZLIB
2813     int err;
2814 #endif
2815
2816     if (compression == 0) return NULL;
2817     ssl_debug_printf("ssl_create_decompressor: compression method %d\n", compression);
2818     decomp = (SslDecompress *)wmem_alloc(wmem_file_scope(), sizeof(SslDecompress));
2819     decomp->compression = compression;
2820     switch (decomp->compression) {
2821 #ifdef HAVE_ZLIB
2822         case 1:  /* DEFLATE */
2823             decomp->istream.zalloc = ssl_zalloc;
2824             decomp->istream.zfree = ssl_zfree;
2825             decomp->istream.opaque = Z_NULL;
2826             decomp->istream.next_in = Z_NULL;
2827             decomp->istream.next_out = Z_NULL;
2828             decomp->istream.avail_in = 0;
2829             decomp->istream.avail_out = 0;
2830             err = inflateInit(&decomp->istream);
2831             if (err != Z_OK) {
2832                 ssl_debug_printf("ssl_create_decompressor: inflateInit_() failed - %d\n", err);
2833                 return NULL;
2834             }
2835             break;
2836 #endif
2837         default:
2838             ssl_debug_printf("ssl_create_decompressor: unsupported compression method %d\n", decomp->compression);
2839             return NULL;
2840     }
2841     return decomp;
2842 }
2843
2844 #ifdef HAVE_ZLIB
2845 static int
2846 ssl_decompress_record(SslDecompress* decomp, const guchar* in, guint inl, StringInfo* out_str, guint* outl)
2847 {
2848     gint err;
2849
2850     switch (decomp->compression) {
2851         case 1:  /* DEFLATE */
2852             err = Z_OK;
2853             if (out_str->data_len < 16384) {  /* maximal plain length */
2854                 ssl_data_realloc(out_str, 16384);
2855             }
2856 #ifdef z_const
2857             decomp->istream.next_in = in;
2858 #else
2859 DIAG_OFF(cast-qual)
2860             decomp->istream.next_in = (Bytef *)in;
2861 DIAG_ON(cast-qual)
2862 #endif
2863             decomp->istream.avail_in = inl;
2864             decomp->istream.next_out = out_str->data;
2865             decomp->istream.avail_out = out_str->data_len;
2866             if (inl > 0)
2867                 err = inflate(&decomp->istream, Z_SYNC_FLUSH);
2868             if (err != Z_OK) {
2869                 ssl_debug_printf("ssl_decompress_record: inflate() failed - %d\n", err);
2870                 return -1;
2871             }
2872             *outl = out_str->data_len - decomp->istream.avail_out;
2873             break;
2874         default:
2875             ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2876             return -1;
2877     }
2878     return 0;
2879 }
2880 #else
2881 int
2882 ssl_decompress_record(SslDecompress* decomp _U_, const guchar* in _U_, guint inl _U_, StringInfo* out_str _U_, guint* outl _U_)
2883 {
2884     ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2885     return -1;
2886 }
2887 #endif
2888 /* Record Decompression (after decryption) }}} */
2889 #endif /* HAVE_LIBGCRYPT */
2890
2891 #ifdef HAVE_LIBGCRYPT
2892 /* Create a new structure to store decrypted chunks. {{{ */
2893 static SslFlow*
2894 ssl_create_flow(void)
2895 {
2896   SslFlow *flow;
2897
2898   flow = (SslFlow *)wmem_alloc(wmem_file_scope(), sizeof(SslFlow));
2899   flow->byte_seq = 0;
2900   flow->flags = 0;
2901   flow->multisegment_pdus = wmem_tree_new(wmem_file_scope());
2902   return flow;
2903 }
2904 /* }}} */
2905
2906 /* Use the negotiated security parameters for decryption. {{{ */
2907 void
2908 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
2909 {
2910     ssl_debug_printf("ssl_change_cipher %s\n", (server)?"SERVER":"CLIENT");
2911     if (server) {
2912         ssl_session->server = ssl_session->server_new;
2913         ssl_session->server_new = NULL;
2914     } else {
2915         ssl_session->client = ssl_session->client_new;
2916         ssl_session->client_new = NULL;
2917     }
2918 }
2919 /* }}} */
2920
2921 /* Init cipher state given some security parameters. {{{ */
2922 static gboolean
2923 ssl_decoder_destroy_cb(wmem_allocator_t *, wmem_cb_event_t, void *);
2924
2925 static SslDecoder*
2926 ssl_create_decoder(const SslCipherSuite *cipher_suite, gint cipher_algo,
2927         gint compression, guint8 *mk, guint8 *sk, guint8 *iv, guint iv_length)
2928 {
2929     SslDecoder *dec;
2930     ssl_cipher_mode_t mode = cipher_suite->mode;
2931
2932     dec = (SslDecoder *)wmem_alloc0(wmem_file_scope(), sizeof(SslDecoder));
2933     /* init mac buffer: mac storage is embedded into decoder struct to save a
2934      memory allocation and waste samo more memory*/
2935     dec->cipher_suite=cipher_suite;
2936     dec->compression = compression;
2937     if ((mode == MODE_STREAM && mk != NULL) || mode == MODE_CBC) {
2938         // AEAD ciphers use no MAC key, but stream and block ciphers do. Note
2939         // the special case for NULL ciphers, even if there is insufficieny
2940         // keying material (including MAC key), we will can still create
2941         // decoders since "decryption" is easy for such ciphers.
2942         dec->mac_key.data = dec->_mac_key_or_write_iv;
2943         ssl_data_set(&dec->mac_key, mk, ssl_cipher_suite_dig(cipher_suite)->len);
2944     } else if (mode == MODE_GCM || mode == MODE_CCM || mode == MODE_CCM_8) {
2945         // Input for the nonce, to be used with AEAD ciphers.
2946         DISSECTOR_ASSERT(iv_length <= sizeof(dec->_mac_key_or_write_iv));
2947         dec->write_iv.data = dec->_mac_key_or_write_iv;
2948         ssl_data_set(&dec->write_iv, iv, iv_length);
2949     }
2950     dec->seq = 0;
2951     dec->decomp = ssl_create_decompressor(compression);
2952     wmem_register_callback(wmem_file_scope(), ssl_decoder_destroy_cb, dec);
2953
2954     if (ssl_cipher_init(&dec->evp,cipher_algo,sk,iv,cipher_suite->mode) < 0) {
2955         ssl_debug_printf("%s: can't create cipher id:%d mode:%d\n", G_STRFUNC,
2956             cipher_algo, cipher_suite->mode);
2957         return NULL;
2958     }
2959
2960     ssl_debug_printf("decoder initialized (digest len %d)\n", ssl_cipher_suite_dig(cipher_suite)->len);
2961     return dec;
2962 }
2963
2964 static gboolean
2965 ssl_decoder_destroy_cb(wmem_allocator_t *allocator _U_, wmem_cb_event_t event _U_, void *user_data)
2966 {
2967     SslDecoder *dec = (SslDecoder *) user_data;
2968
2969     if (dec->evp)
2970         ssl_cipher_cleanup(&dec->evp);
2971
2972 #ifdef HAVE_ZLIB
2973     if (dec->decomp != NULL && dec->decomp->compression == 1 /* DEFLATE */)
2974         inflateEnd(&dec->decomp->istream);
2975 #endif
2976
2977     return FALSE;
2978 }
2979 /* }}} */
2980
2981 /* (Pre-)master secrets calculations {{{ */
2982 #ifdef HAVE_LIBGNUTLS
2983 static int
2984 ssl_decrypt_pre_master_secret(SslDecryptSession *ssl_session,
2985                               StringInfo *encrypted_pre_master,
2986                               gcry_sexp_t pk);
2987 #endif /* HAVE_LIBGNUTLS */
2988
2989 static gboolean
2990 ssl_restore_master_key(SslDecryptSession *ssl, const char *label,
2991                        gboolean is_pre_master, GHashTable *ht, StringInfo *key);
2992
2993 gboolean
2994 ssl_generate_pre_master_secret(SslDecryptSession *ssl_session,
2995                                guint32 length, tvbuff_t *tvb, guint32 offset,
2996                                const gchar *ssl_psk,
2997                                const ssl_master_key_map_t *mk_map)
2998 {
2999     /* check for required session data */
3000     ssl_debug_printf("%s: found SSL_HND_CLIENT_KEY_EXCHG, state %X\n",
3001                      G_STRFUNC, ssl_session->state);
3002     if ((ssl_session->state & (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) !=
3003         (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) {
3004         ssl_debug_printf("%s: not enough data to generate key (required state %X)\n", G_STRFUNC,
3005                          (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION));
3006         return FALSE;
3007     }
3008
3009     if (ssl_session->session.version == TLSV1DOT3_VERSION) {
3010         ssl_debug_printf("%s: detected TLS 1.3 which has no pre-master secrets\n", G_STRFUNC);
3011         return FALSE;
3012     }
3013
3014     /* check to see if the PMS was provided to us*/
3015     if (ssl_restore_master_key(ssl_session, "Unencrypted pre-master secret", TRUE,
3016            mk_map->pms, &ssl_session->client_random)) {
3017         return TRUE;
3018     }
3019
3020     if (ssl_session->cipher_suite->kex == KEX_PSK)
3021     {
3022         /* calculate pre master secret*/
3023         StringInfo pre_master_secret;
3024         guint psk_len, pre_master_len;
3025
3026         if (!ssl_psk || (ssl_psk[0] == 0)) {
3027             ssl_debug_printf("%s: can't find pre-shared-key\n", G_STRFUNC);
3028             return FALSE;
3029         }
3030
3031         /* convert hex string into char*/
3032         if (!from_hex(&ssl_session->psk, ssl_psk, strlen(ssl_psk))) {
3033             ssl_debug_printf("%s: ssl.psk/dtls.psk contains invalid hex\n",
3034                              G_STRFUNC);
3035             return FALSE;
3036         }
3037
3038         psk_len = ssl_session->psk.data_len;
3039         if (psk_len >= (2 << 15)) {
3040             ssl_debug_printf("%s: ssl.psk/dtls.psk must not be larger than 2^15 - 1\n",
3041                              G_STRFUNC);
3042             return FALSE;
3043         }
3044
3045
3046         pre_master_len = psk_len * 2 + 4;
3047
3048         pre_master_secret.data = (guchar *)wmem_alloc(wmem_file_scope(), pre_master_len);
3049         pre_master_secret.data_len = pre_master_len;
3050         /* 2 bytes psk_len*/
3051         pre_master_secret.data[0] = psk_len >> 8;
3052         pre_master_secret.data[1] = psk_len & 0xFF;
3053         /* psk_len bytes times 0*/
3054         memset(&pre_master_secret.data[2], 0, psk_len);
3055         /* 2 bytes psk_len*/
3056         pre_master_secret.data[psk_len + 2] = psk_len >> 8;
3057         pre_master_secret.data[psk_len + 3] = psk_len & 0xFF;
3058         /* psk*/
3059         memcpy(&pre_master_secret.data[psk_len + 4], ssl_session->psk.data, psk_len);
3060
3061         ssl_session->pre_master_secret.data = pre_master_secret.data;
3062         ssl_session->pre_master_secret.data_len = pre_master_len;
3063         /*ssl_debug_printf("pre master secret",&ssl->pre_master_secret);*/
3064
3065         /* Remove the master secret if it was there.
3066            This forces keying material regeneration in
3067            case we're renegotiating */
3068         ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
3069         ssl_session->state |= SSL_PRE_MASTER_SECRET;
3070         return TRUE;
3071     }
3072     else
3073     {
3074         StringInfo encrypted_pre_master;
3075         guint encrlen, skip;
3076         encrlen = length;
3077         skip = 0;
3078
3079         /* get encrypted data, on tls1 we have to skip two bytes
3080          * (it's the encrypted len and should be equal to record len - 2)
3081          * in case of rsa1024 that would be 128 + 2 = 130; for psk not necessary
3082          */
3083         if (ssl_session->cipher_suite->kex == KEX_RSA &&
3084            (ssl_session->session.version == TLSV1_VERSION ||
3085             ssl_session->session.version == TLSV1DOT1_VERSION ||
3086             ssl_session->session.version == TLSV1DOT2_VERSION ||
3087             ssl_session->session.version == DTLSV1DOT0_VERSION ||
3088             ssl_session->session.version == DTLSV1DOT2_VERSION))
3089         {
3090             encrlen  = tvb_get_ntohs(tvb, offset);
3091             skip = 2;
3092             if (encrlen > length - 2)
3093             {
3094                 ssl_debug_printf("%s: wrong encrypted length (%d max %d)\n",
3095                                  G_STRFUNC, encrlen, length);
3096                 return FALSE;
3097             }
3098         }
3099         /* the valid lower bound is higher than 8, but it is sufficient for the
3100          * ssl keylog file below */
3101         if (encrlen < 8) {
3102             ssl_debug_printf("%s: invalid encrypted pre-master key length %d\n",
3103                              G_STRFUNC, encrlen);
3104             return FALSE;
3105         }
3106
3107         encrypted_pre_master.data = (guchar *)wmem_alloc(wmem_file_scope(), encrlen);
3108         encrypted_pre_master.data_len = encrlen;
3109         tvb_memcpy(tvb, encrypted_pre_master.data, offset+skip, encrlen);
3110
3111 #ifdef HAVE_LIBGNUTLS
3112         if (ssl_session->private_key) {
3113             /* try to decrypt encrypted pre-master with RSA key */
3114             if (ssl_decrypt_pre_master_secret(ssl_session,
3115                 &encrypted_pre_master, ssl_session->private_key))
3116                 return TRUE;
3117
3118             ssl_debug_printf("%s: can't decrypt pre-master secret\n",
3119                              G_STRFUNC);
3120         }
3121 #endif /* HAVE_LIBGNUTLS */
3122
3123         /* try to find the pre-master secret from the encrypted one. The
3124          * ssl key logfile stores only the first 8 bytes, so truncate it */
3125         encrypted_pre_master.data_len = 8;
3126         if (ssl_restore_master_key(ssl_session, "Encrypted pre-master secret",
3127             TRUE, mk_map->pre_master, &encrypted_pre_master))
3128             return TRUE;
3129     }
3130     return FALSE;
3131 }
3132
3133 /* Used for (D)TLS 1.2 and earlier versions (not with TLS 1.3). */
3134 int
3135 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
3136 {
3137     StringInfo  key_block = { NULL, 0 };
3138     guint8      _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
3139     guint8      _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
3140     gint        needed;
3141     gint        cipher_algo = -1;   /* special value (-1) for NULL encryption */
3142     guint       encr_key_len, write_iv_len = 0;
3143     gboolean    is_export_cipher;
3144     guint8     *ptr, *c_iv = NULL, *s_iv = NULL;
3145     guint8     *c_wk = NULL, *s_wk = NULL, *c_mk = NULL, *s_mk = NULL;
3146     const SslCipherSuite *cipher_suite = ssl_session->cipher_suite;
3147
3148     /* TLS 1.3 is handled directly in tls13_change_key. */
3149     if (ssl_session->session.version == TLSV1DOT3_VERSION) {
3150         ssl_debug_printf("%s: detected TLS 1.3. Should not have been called!\n", G_STRFUNC);
3151         return -1;
3152     }
3153
3154     /* check for enough info to proced */
3155     guint need_all = SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION;
3156     guint need_any = SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET;
3157     if (((ssl_session->state & need_all) != need_all) || ((ssl_session->state & need_any) == 0)) {
3158         ssl_debug_printf("ssl_generate_keyring_material not enough data to generate key "
3159                          "(0x%02X required 0x%02X or 0x%02X)\n", ssl_session->state,
3160                          need_all|SSL_MASTER_SECRET, need_all|SSL_PRE_MASTER_SECRET);
3161         /* Special case: for NULL encryption, allow dissection of data even if
3162          * the Client Hello is missing (MAC keys are now skipped though). */
3163         need_all = SSL_CIPHER|SSL_VERSION;
3164         if ((ssl_session->state & need_all) == need_all &&
3165                 cipher_suite->enc == ENC_NULL) {
3166             ssl_debug_printf("%s NULL cipher found, will create a decoder but "
3167                     "skip MAC validation as keys are missing.\n", G_STRFUNC);
3168             goto create_decoders;
3169         }
3170
3171         return -1;
3172     }
3173
3174     /* if master key is not available, generate is from the pre-master secret */
3175     if (!(ssl_session->state & SSL_MASTER_SECRET)) {
3176         if ((ssl_session->state & SSL_EXTENDED_MASTER_SECRET_MASK) == SSL_EXTENDED_MASTER_SECRET_MASK) {
3177             StringInfo handshake_hashed_data;
3178             gint ret;
3179
3180             handshake_hashed_data.data = NULL;
3181             handshake_hashed_data.data_len = 0;
3182
3183             ssl_debug_printf("%s:PRF(pre_master_secret_extended)\n", G_STRFUNC);
3184             ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
3185             DISSECTOR_ASSERT(ssl_session->handshake_data.data_len > 0);
3186
3187             switch(ssl_session->session.version) {
3188             case TLSV1_VERSION:
3189             case TLSV1DOT1_VERSION:
3190             case DTLSV1DOT0_VERSION:
3191             case DTLSV1DOT0_OPENSSL_VERSION:
3192                 ret = tls_handshake_hash(ssl_session, &handshake_hashed_data);
3193                 break;
3194             default:
3195                 switch (cipher_suite->dig) {
3196                 case DIG_SHA384:
3197                     ret = tls12_handshake_hash(ssl_session, GCRY_MD_SHA384, &handshake_hashed_data);
3198                     break;
3199                 default:
3200                     ret = tls12_handshake_hash(ssl_session, GCRY_MD_SHA256, &handshake_hashed_data);
3201                     break;
3202                 }
3203                 break;
3204             }
3205             if (ret) {
3206                 ssl_debug_printf("%s can't generate handshake hash\n", G_STRFUNC);
3207                 return -1;
3208             }
3209
3210             wmem_free(wmem_file_scope(), ssl_session->handshake_data.data);
3211             ssl_session->handshake_data.data = NULL;
3212             ssl_session->handshake_data.data_len = 0;
3213
3214             if (!prf(ssl_session, &ssl_session->pre_master_secret, "extended master secret",
3215                      &handshake_hashed_data,
3216                      NULL, &ssl_session->master_secret,
3217                      SSL_MASTER_SECRET_LENGTH)) {
3218                 ssl_debug_printf("%s can't generate master_secret\n", G_STRFUNC);
3219                 g_free(handshake_hashed_data.data);
3220                 return -1;
3221             }
3222             g_free(handshake_hashed_data.data);
3223         } else {
3224             ssl_debug_printf("%s:PRF(pre_master_secret)\n", G_STRFUNC);
3225             ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
3226             ssl_print_string("client random",&ssl_session->client_random);
3227             ssl_print_string("server random",&ssl_session->server_random);
3228             if (!prf(ssl_session, &ssl_session->pre_master_secret, "master secret",
3229                      &ssl_session->client_random,
3230                      &ssl_session->server_random, &ssl_session->master_secret,
3231                      SSL_MASTER_SECRET_LENGTH)) {
3232                 ssl_debug_printf("%s can't generate master_secret\n", G_STRFUNC);
3233                 return -1;
3234             }
3235         }
3236         ssl_print_string("master secret",&ssl_session->master_secret);
3237
3238         /* the pre-master secret has been 'consumend' so we must clear it now */
3239         ssl_session->state &= ~SSL_PRE_MASTER_SECRET;
3240         ssl_session->state |= SSL_MASTER_SECRET;
3241     }
3242
3243     /* Find the Libgcrypt cipher algorithm for the given SSL cipher suite ID */
3244     if (cipher_suite->enc != ENC_NULL) {
3245         const char *cipher_name = ciphers[cipher_suite->enc-0x30];
3246         ssl_debug_printf("%s CIPHER: %s\n", G_STRFUNC, cipher_name);
3247         cipher_algo = ssl_get_cipher_by_name(cipher_name);
3248         if (cipher_algo == 0) {
3249             ssl_debug_printf("%s can't find cipher %s\n", G_STRFUNC, cipher_name);
3250             return -1;
3251         }
3252     }
3253
3254     /* Export ciphers consume less material from the key block. */
3255     encr_key_len = ssl_get_cipher_export_keymat_size(cipher_suite->number);
3256     is_export_cipher = encr_key_len > 0;
3257     if (!is_export_cipher && cipher_suite->enc != ENC_NULL) {
3258         encr_key_len = (guint)gcry_cipher_get_algo_keylen(cipher_algo);
3259     }
3260
3261     if (cipher_suite->mode == MODE_CBC) {
3262         write_iv_len = (guint)gcry_cipher_get_algo_blklen(cipher_algo);
3263     } else if (cipher_suite->mode == MODE_GCM || cipher_suite->mode == MODE_CCM || cipher_suite->mode == MODE_CCM_8) {
3264         /* account for a four-byte salt for client and server side (from
3265          * client_write_IV and server_write_IV), see GCMNonce (RFC 5288) */
3266         write_iv_len = 4;
3267     }
3268
3269     /* Compute the key block. First figure out how much data we need */
3270     needed = ssl_cipher_suite_dig(cipher_suite)->len*2;     /* MAC key  */
3271     needed += 2 * encr_key_len;                             /* encryption key */
3272     needed += 2 * write_iv_len;                             /* write IV */
3273
3274     key_block.data = (guchar *)g_malloc(needed);
3275     ssl_debug_printf("%s sess key generation\n", G_STRFUNC);
3276     if (!prf(ssl_session, &ssl_session->master_secret, "key expansion",
3277             &ssl_session->server_random,&ssl_session->client_random,
3278             &key_block, needed)) {
3279         ssl_debug_printf("%s can't generate key_block\n", G_STRFUNC);
3280         goto fail;
3281     }
3282     ssl_print_string("key expansion", &key_block);
3283
3284     ptr=key_block.data;
3285     /* client/server write MAC key (for non-AEAD ciphers) */
3286     if (cipher_suite->mode == MODE_STREAM || cipher_suite->mode == MODE_CBC) {
3287         c_mk=ptr; ptr+=ssl_cipher_suite_dig(cipher_suite)->len;
3288         s_mk=ptr; ptr+=ssl_cipher_suite_dig(cipher_suite)->len;
3289     }
3290     /* client/server write encryption key */
3291     c_wk=ptr; ptr += encr_key_len;
3292     s_wk=ptr; ptr += encr_key_len;
3293     /* client/server write IV (used as IV (for CBC) or salt (for AEAD)) */
3294     if (write_iv_len > 0) {
3295         c_iv=ptr; ptr += write_iv_len;
3296         s_iv=ptr; /* ptr += write_iv_len; */
3297     }
3298
3299     /* export ciphers work with a smaller key length */
3300     if (is_export_cipher) {
3301         if (cipher_suite->mode == MODE_CBC) {
3302
3303             /* We only have room for MAX_BLOCK_SIZE bytes IVs, but that's
3304              all we should need. This is a sanity check */
3305             if (write_iv_len > MAX_BLOCK_SIZE) {
3306                 ssl_debug_printf("%s cipher suite block must be at most %d nut is %d\n",
3307                         G_STRFUNC, MAX_BLOCK_SIZE, write_iv_len);
3308                 goto fail;
3309             }
3310
3311             if(ssl_session->session.version==SSLV3_VERSION){
3312                 /* The length of these fields are ignored by this caller */
3313                 StringInfo iv_c, iv_s;
3314                 iv_c.data = _iv_c;
3315                 iv_s.data = _iv_s;
3316
3317                 ssl_debug_printf("%s ssl3_generate_export_iv\n", G_STRFUNC);
3318                 ssl3_generate_export_iv(&ssl_session->client_random,
3319                         &ssl_session->server_random, &iv_c, write_iv_len);
3320                 ssl_debug_printf("%s ssl3_generate_export_iv(2)\n", G_STRFUNC);
3321                 ssl3_generate_export_iv(&ssl_session->server_random,
3322                         &ssl_session->client_random, &iv_s, write_iv_len);
3323             }
3324             else{
3325                 guint8 _iv_block[MAX_BLOCK_SIZE * 2];
3326                 StringInfo iv_block;
3327                 StringInfo key_null;
3328                 guint8 _key_null;
3329
3330                 key_null.data = &_key_null;
3331                 key_null.data_len = 0;
3332
3333                 iv_block.data = _iv_block;
3334
3335                 ssl_debug_printf("%s prf(iv_block)\n", G_STRFUNC);
3336                 if (!prf(ssl_session, &key_null, "IV block",
3337                         &ssl_session->client_random,
3338                         &ssl_session->server_random, &iv_block,
3339                         write_iv_len * 2)) {
3340                     ssl_debug_printf("%s can't generate tls31 iv block\n", G_STRFUNC);
3341                     goto fail;
3342                 }
3343
3344                 memcpy(_iv_c, iv_block.data, write_iv_len);
3345                 memcpy(_iv_s, iv_block.data + write_iv_len, write_iv_len);
3346             }
3347
3348             c_iv=_iv_c;
3349             s_iv=_iv_s;
3350         }
3351
3352         if (ssl_session->session.version==SSLV3_VERSION){
3353
3354             SSL_MD5_CTX md5;
3355             ssl_debug_printf("%s MD5(client_random)\n", G_STRFUNC);
3356
3357             ssl_md5_init(&md5);
3358             ssl_md5_update(&md5,c_wk,encr_key_len);
3359             ssl_md5_update(&md5,ssl_session->client_random.data,
3360                 ssl_session->client_random.data_len);
3361             ssl_md5_update(&md5,ssl_session->server_random.data,
3362                 ssl_session->server_random.data_len);
3363             ssl_md5_final(_key_c,&md5);
3364             ssl_md5_cleanup(&md5);
3365             c_wk=_key_c;
3366
3367             ssl_md5_init(&md5);
3368             ssl_debug_printf("%s MD5(server_random)\n", G_STRFUNC);
3369             ssl_md5_update(&md5,s_wk,encr_key_len);
3370             ssl_md5_update(&md5,ssl_session->server_random.data,
3371                 ssl_session->server_random.data_len);
3372             ssl_md5_update(&md5,ssl_session->client_random.data,
3373                 ssl_session->client_random.data_len);
3374             ssl_md5_final(_key_s,&md5);
3375             ssl_md5_cleanup(&md5);
3376             s_wk=_key_s;
3377         }
3378         else{
3379             StringInfo key_c, key_s, k;
3380             key_c.data = _key_c;
3381             key_s.data = _key_s;
3382
3383             k.data = c_wk;
3384             k.data_len = encr_key_len;
3385             ssl_debug_printf("%s PRF(key_c)\n", G_STRFUNC);
3386             if (!prf(ssl_session, &k, "client write key",
3387                     &ssl_session->client_random,
3388                     &ssl_session->server_random, &key_c, sizeof(_key_c))) {
3389                 ssl_debug_printf("%s can't generate tll31 server key \n", G_STRFUNC);
3390                 goto fail;
3391             }
3392             c_wk=_key_c;
3393
3394             k.data = s_wk;
3395             k.data_len = encr_key_len;
3396             ssl_debug_printf("%s PRF(key_s)\n", G_STRFUNC);
3397             if (!prf(ssl_session, &k, "server write key",
3398                     &ssl_session->client_random,
3399                     &ssl_session->server_random, &key_s, sizeof(_key_s))) {
3400                 ssl_debug_printf("%s can't generate tll31 client key \n", G_STRFUNC);
3401                 goto fail;
3402             }
3403             s_wk=_key_s;
3404         }
3405     }
3406
3407     /* show key material info */
3408     if (c_mk != NULL) {
3409         ssl_print_data("Client MAC key",c_mk,ssl_cipher_suite_dig(cipher_suite)->len);
3410         ssl_print_data("Server MAC key",s_mk,ssl_cipher_suite_dig(cipher_suite)->len);
3411     }
3412     ssl_print_data("Client Write key", c_wk, encr_key_len);
3413     ssl_print_data("Server Write key", s_wk, encr_key_len);
3414     /* used as IV for CBC mode and the AEAD implicit nonce (salt) */
3415     if (write_iv_len > 0) {
3416         ssl_print_data("Client Write IV", c_iv, write_iv_len);
3417         ssl_print_data("Server Write IV", s_iv, write_iv_len);
3418     }
3419
3420 create_decoders:
3421     /* create both client and server ciphers*/
3422     ssl_debug_printf("%s ssl_create_decoder(client)\n", G_STRFUNC);
3423     ssl_session->client_new = ssl_create_decoder(cipher_suite, cipher_algo, ssl_session->session.compression, c_mk, c_wk, c_iv, write_iv_len);
3424     if (!ssl_session->client_new) {
3425         ssl_debug_printf("%s can't init client decoder\n", G_STRFUNC);
3426         goto fail;
3427     }
3428     ssl_debug_printf("%s ssl_create_decoder(server)\n", G_STRFUNC);
3429     ssl_session->server_new = ssl_create_decoder(cipher_suite, cipher_algo, ssl_session->session.compression, s_mk, s_wk, s_iv, write_iv_len);
3430     if (!ssl_session->server_new) {
3431         ssl_debug_printf("%s can't init client decoder\n", G_STRFUNC);
3432         goto fail;
3433     }
3434
3435     /* Continue the SSL stream after renegotiation with new keys. */
3436     ssl_session->client_new->flow = ssl_session->client ? ssl_session->client->flow : ssl_create_flow();
3437     ssl_session->server_new->flow = ssl_session->server ? ssl_session->server->flow : ssl_create_flow();
3438
3439     ssl_debug_printf("%s: client seq %" G_GUINT64_FORMAT ", server seq %" G_GUINT64_FORMAT "\n",
3440         G_STRFUNC, ssl_session->client_new->seq, ssl_session->server_new->seq);
3441     g_free(key_block.data);
3442     ssl_session->state |= SSL_HAVE_SESSION_KEY;
3443     return 0;
3444
3445 fail:
3446     g_free(key_block.data);
3447     return -1;
3448 }
3449
3450 /* Generated the key material based on the given secret. */
3451 static gboolean
3452 tls13_generate_keys(SslDecryptSession *ssl_session, const StringInfo *secret, gboolean is_from_server)
3453 {
3454     gboolean    success = FALSE;
3455     guchar     *write_key = NULL, *write_iv = NULL;
3456     SslDecoder *decoder;
3457     guint       key_length, iv_length;
3458     int         hash_algo;
3459     const SslCipherSuite *cipher_suite = ssl_session->cipher_suite;
3460     int         cipher_algo;
3461
3462     if (ssl_session->session.version != TLSV1DOT3_VERSION) {
3463         ssl_debug_printf("%s only usable for TLS 1.3, not %#x!\n", G_STRFUNC,
3464                 ssl_session->session.version);
3465         return FALSE;
3466     }
3467
3468     if (cipher_suite == NULL) {
3469         ssl_debug_printf("%s Unknown cipher\n", G_STRFUNC);
3470         return FALSE;
3471     }
3472
3473     if (cipher_suite->kex != KEX_TLS13) {
3474         ssl_debug_printf("%s Invalid cipher suite 0x%04x spotted!\n", G_STRFUNC, cipher_suite->number);
3475         return FALSE;
3476     }
3477
3478     /* Find the Libgcrypt cipher algorithm for the given SSL cipher suite ID */
3479     const char *cipher_name = ciphers[cipher_suite->enc-0x30];
3480     ssl_debug_printf("%s CIPHER: %s\n", G_STRFUNC, cipher_name);
3481     cipher_algo = ssl_get_cipher_by_name(cipher_name);
3482     if (cipher_algo == 0) {
3483         ssl_debug_printf("%s can't find cipher %s\n", G_STRFUNC, cipher_name);
3484         return FALSE;
3485     }
3486
3487     const char *hash_name = ssl_cipher_suite_dig(cipher_suite)->name;
3488     hash_algo = ssl_get_digest_by_name(hash_name);
3489     if (!hash_algo) {
3490         ssl_debug_printf("%s can't find hash function %s\n", G_STRFUNC, hash_name);
3491         return FALSE;
3492     }
3493
3494     key_length = (guint) gcry_cipher_get_algo_blklen(cipher_algo);
3495     /* AES-GCM/AES-CCM/Poly1305-ChaCha20 all have N_MIN=N_MAX = 12. */
3496     iv_length = 12;
3497     ssl_debug_printf("%s key_length %u iv_length %u\n", G_STRFUNC, key_length, iv_length);
3498
3499     if (!tls13_hkdf_expand_label(hash_algo, secret, "key", "", key_length, &write_key)) {
3500         ssl_debug_printf("%s write_key expansion failed\n", G_STRFUNC);
3501         return FALSE;
3502     }
3503     if (!tls13_hkdf_expand_label(hash_algo, secret, "iv", "", iv_length, &write_iv)) {
3504         ssl_debug_printf("%s write_iv expansion failed\n", G_STRFUNC);
3505         goto end;
3506     }
3507
3508     ssl_print_data(is_from_server ? "Server Write Key" : "Client Write Key", write_key, key_length);
3509     ssl_print_data(is_from_server ? "Server Write IV" : "Client Write IV", write_iv, iv_length);
3510
3511     ssl_debug_printf("%s ssl_create_decoder(%s)\n", G_STRFUNC, is_from_server ? "server" : "client");
3512     decoder = ssl_create_decoder(cipher_suite, cipher_algo, 0, NULL, write_key, write_iv, iv_length);
3513     if (!decoder) {
3514         ssl_debug_printf("%s can't init %s decoder\n", G_STRFUNC, is_from_server ? "server" : "client");
3515         goto end;
3516     }
3517
3518     /* Continue the TLS session with new keys, but reuse old flow to keep things
3519      * like "Follow SSL" working (by linking application data records). */
3520     if (is_from_server) {
3521         decoder->flow = ssl_session->server ? ssl_session->server->flow : ssl_create_flow();
3522         ssl_session->server = decoder;
3523     } else {
3524         decoder->flow = ssl_session->client ? ssl_session->client->flow : ssl_create_flow();
3525         ssl_session->client = decoder;
3526     }
3527     ssl_debug_printf("%s %s ready using cipher suite 0x%04x (cipher %s hash %s)\n", G_STRFUNC,
3528                      is_from_server ? "Server" : "Client", cipher_suite->number, cipher_name, hash_name);
3529     success = TRUE;
3530
3531 end:
3532     wmem_free(NULL, write_key);
3533     wmem_free(NULL, write_iv);
3534     return success;
3535 }
3536 /* (Pre-)master secrets calculations }}} */
3537
3538 #ifdef HAVE_LIBGNUTLS
3539 /* Decrypt RSA pre-master secret using RSA private key. {{{ */
3540 static gboolean
3541 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
3542     StringInfo* encrypted_pre_master, gcry_sexp_t pk)
3543 {
3544     gint i;
3545
3546     if (!encrypted_pre_master)
3547         return FALSE;
3548
3549     if (KEX_IS_DH(ssl_session->cipher_suite->kex)) {
3550         ssl_debug_printf("%s: session uses Diffie-Hellman key exchange "
3551                          "(cipher suite 0x%04X %s) and cannot be decrypted "
3552                          "using a RSA private key file.\n",
3553                          G_STRFUNC, ssl_session->session.cipher,
3554                          val_to_str_ext_const(ssl_session->session.cipher,
3555                              &ssl_31_ciphersuite_ext, "unknown"));
3556         return FALSE;
3557     } else if(ssl_session->cipher_suite->kex != KEX_RSA) {
3558          ssl_debug_printf("%s key exchange %d different from KEX_RSA (%d)\n",
3559                           G_STRFUNC, ssl_session->cipher_suite->kex, KEX_RSA);
3560         return FALSE;
3561     }
3562
3563     /* with tls key loading will fail if not rsa type, so no need to check*/
3564     ssl_print_string("pre master encrypted",encrypted_pre_master);
3565     ssl_debug_printf("%s: RSA_private_decrypt\n", G_STRFUNC);
3566     i=ssl_private_decrypt(encrypted_pre_master->data_len,
3567         encrypted_pre_master->data, pk);
3568
3569     if (i!=48) {
3570         ssl_debug_printf("%s wrong pre_master_secret length (%d, expected "
3571                          "%d)\n", G_STRFUNC, i, 48);
3572         return FALSE;
3573     }
3574
3575     /* the decrypted data has been written into the pre_master key buffer */
3576     ssl_session->pre_master_secret.data = encrypted_pre_master->data;
3577     ssl_session->pre_master_secret.data_len=48;
3578     ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
3579
3580     /* Remove the master secret if it was there.
3581        This forces keying material regeneration in
3582        case we're renegotiating */
3583     ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
3584     ssl_session->state |= SSL_PRE_MASTER_SECRET;
3585     return TRUE;
3586 } /* }}} */
3587 #endif /* HAVE_LIBGNUTLS */
3588
3589 /* Decryption integrity check {{{ */
3590
3591 static gint
3592 tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
3593         guint32 datalen, guint8* mac)
3594 {
3595     SSL_HMAC hm;
3596     gint     md;
3597     guint32  len;
3598     guint8   buf[DIGEST_MAX_SIZE];
3599     gint16   temp;
3600
3601     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3602     ssl_debug_printf("tls_check_mac mac type:%s md %d\n",
3603         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
3604
3605     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
3606         return -1;
3607
3608     /* hash sequence number */
3609     phton64(buf, decoder->seq);
3610
3611     decoder->seq++;
3612
3613     ssl_hmac_update(&hm,buf,8);
3614
3615     /* hash content type */
3616     buf[0]=ct;
3617     ssl_hmac_update(&hm,buf,1);
3618
3619     /* hash version,data length and data*/
3620     /* *((gint16*)buf) = g_htons(ver); */
3621     temp = g_htons(ver);
3622     memcpy(buf, &temp, 2);
3623     ssl_hmac_update(&hm,buf,2);
3624
3625     /* *((gint16*)buf) = g_htons(datalen); */
3626     temp = g_htons(datalen);
3627     memcpy(buf, &temp, 2);
3628     ssl_hmac_update(&hm,buf,2);
3629     ssl_hmac_update(&hm,data,datalen);
3630
3631     /* get digest and digest len*/
3632     len = sizeof(buf);
3633     ssl_hmac_final(&hm,buf,&len);
3634     ssl_hmac_cleanup(&hm);
3635     ssl_print_data("Mac", buf, len);
3636     if(memcmp(mac,buf,len))
3637         return -1;
3638
3639     return 0;
3640 }
3641
3642 static int
3643 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
3644         guint32 datalen, guint8* mac)
3645 {
3646     SSL_MD  mc;
3647     gint    md;
3648     guint32 len;
3649     guint8  buf[64],dgst[20];
3650     gint    pad_ct;
3651     gint16  temp;
3652
3653     pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
3654
3655     /* get cipher used for digest comptuation */
3656     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3657     if (ssl_md_init(&mc,md) !=0)
3658         return -1;
3659
3660     /* do hash computation on data && padding */
3661     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
3662
3663     /* hash padding*/
3664     memset(buf,0x36,pad_ct);
3665     ssl_md_update(&mc,buf,pad_ct);
3666
3667     /* hash sequence number */
3668     phton64(buf, decoder->seq);
3669     decoder->seq++;
3670     ssl_md_update(&mc,buf,8);
3671
3672     /* hash content type */
3673     buf[0]=ct;
3674     ssl_md_update(&mc,buf,1);
3675
3676     /* hash data length in network byte order and data*/
3677     /* *((gint16* )buf) = g_htons(datalen); */
3678     temp = g_htons(datalen);
3679     memcpy(buf, &temp, 2);
3680     ssl_md_update(&mc,buf,2);
3681     ssl_md_update(&mc,data,datalen);
3682
3683     /* get partial digest */
3684     ssl_md_final(&mc,dgst,&len);
3685     ssl_md_cleanup(&mc);
3686
3687     ssl_md_init(&mc,md);
3688
3689     /* hash mac key */
3690     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
3691
3692     /* hash padding and partial digest*/
3693     memset(buf,0x5c,pad_ct);
3694     ssl_md_update(&mc,buf,pad_ct);
3695     ssl_md_update(&mc,dgst,len);
3696
3697     ssl_md_final(&mc,dgst,&len);
3698     ssl_md_cleanup(&mc);
3699
3700     if(memcmp(mac,dgst,len))
3701         return -1;
3702
3703     return(0);
3704 }
3705
3706 static gint
3707 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
3708         guint32 datalen, guint8* mac)
3709 {
3710     SSL_HMAC hm;
3711     gint     md;
3712     guint32  len;
3713     guint8   buf[DIGEST_MAX_SIZE];
3714     gint16   temp;
3715
3716     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3717     ssl_debug_printf("dtls_check_mac mac type:%s md %d\n",
3718         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
3719
3720     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
3721         return -1;
3722     ssl_debug_printf("dtls_check_mac seq: %" G_GUINT64_FORMAT " epoch: %d\n",decoder->seq,decoder->epoch);
3723     /* hash sequence number */
3724     phton64(buf, decoder->seq);
3725     buf[0]=decoder->epoch>>8;
3726     buf[1]=(guint8)decoder->epoch;
3727
3728     ssl_hmac_update(&hm,buf,8);
3729
3730     /* hash content type */
3731     buf[0]=ct;
3732     ssl_hmac_update(&hm,buf,1);
3733
3734     /* hash version,data length and data */
3735     temp = g_htons(ver);
3736     memcpy(buf, &temp, 2);
3737     ssl_hmac_update(&hm,buf,2);
3738
3739     temp = g_htons(datalen);
3740     memcpy(buf, &temp, 2);
3741     ssl_hmac_update(&hm,buf,2);
3742     ssl_hmac_update(&hm,data,datalen);
3743     /* get digest and digest len */
3744     len = sizeof(buf);
3745     ssl_hmac_final(&hm,buf,&len);
3746     ssl_hmac_cleanup(&hm);
3747     ssl_print_data("Mac", buf, len);
3748     if(memcmp(mac,buf,len))
3749         return -1;
3750
3751     return(0);
3752 }
3753 /* Decryption integrity check }}} */
3754
3755
3756 static gboolean
3757 tls_decrypt_aead_record(SslDecryptSession *ssl, SslDecoder *decoder,
3758 #ifdef HAVE_LIBGCRYPT_AEAD
3759         guint8 ct, guint16 record_version,
3760 #else
3761         guint8 ct _U_, guint16 record_version _U_,
3762 #endif
3763         const guchar *in, guint16 inl, StringInfo *out_str, guint *outl)
3764 {
3765     /* RFC 5246 (TLS 1.2) 6.2.3.3 defines the TLSCipherText.fragment as:
3766      * GenericAEADCipher: { nonce_explicit, [content] }
3767      * In TLS 1.3 this explicit nonce is gone.
3768      * With AES GCM/CCM, "[content]" is actually the concatenation of the
3769      * ciphertext and authentication tag.
3770      */
3771     const guint16   version = ssl->session.version;
3772     const gboolean  is_v12 = version == TLSV1DOT2_VERSION || version == DTLSV1DOT2_VERSION;
3773     gcry_error_t    err;
3774     const guchar   *explicit_nonce = NULL, *ciphertext;
3775     guint           ciphertext_len, auth_tag_len;
3776     guchar          nonce[12];
3777 #ifdef HAVE_LIBGCRYPT_AEAD
3778     const guchar   *auth_tag_wire;
3779     guchar          auth_tag_calc[16];
3780 #else
3781     guchar          nonce_with_counter[16] = { 0 };
3782 #endif
3783
3784     switch (decoder->cipher_suite->mode) {
3785     case MODE_GCM:
3786     case MODE_CCM:
3787         auth_tag_len = 16;
3788         break;
3789     case MODE_CCM_8:
3790         auth_tag_len = 8;
3791         break;
3792     default:
3793         ssl_debug_printf("%s unsupported cipher!\n", G_STRFUNC);
3794         return FALSE;
3795     }
3796
3797     /* Parse input into explicit nonce (TLS 1.2 only), ciphertext and tag. */
3798     if (is_v12) {
3799         if (inl < EXPLICIT_NONCE_LEN + auth_tag_len) {
3800             ssl_debug_printf("%s input %d is too small for explicit nonce %d and auth tag %d\n",
3801                     G_STRFUNC, inl, EXPLICIT_NONCE_LEN, auth_tag_len);
3802             return FALSE;
3803         }
3804         explicit_nonce = in;
3805         ciphertext = explicit_nonce + EXPLICIT_NONCE_LEN;
3806         ciphertext_len = inl - EXPLICIT_NONCE_LEN - auth_tag_len;
3807     } else if (version == TLSV1DOT3_VERSION) {
3808         if (inl < auth_tag_len) {
3809             ssl_debug_printf("%s input %d has no space for auth tag %d\n", G_STRFUNC, inl, auth_tag_len);
3810             return FALSE;
3811         }
3812         ciphertext = in;
3813         ciphertext_len = inl - auth_tag_len;
3814     } else {
3815         ssl_debug_printf("%s Unexpected TLS version %#x\n", G_STRFUNC, version);
3816         return FALSE;
3817     }
3818 #ifdef HAVE_LIBGCRYPT_AEAD
3819     auth_tag_wire = ciphertext + ciphertext_len;
3820 #endif
3821
3822     /* Nonce construction is version-specific. */
3823     if (is_v12) {
3824         DISSECTOR_ASSERT(decoder->write_iv.data_len == IMPLICIT_NONCE_LEN);
3825         /* Implicit (4) and explicit (8) part of nonce. */
3826         memcpy(nonce, decoder->write_iv.data, IMPLICIT_NONCE_LEN);
3827         memcpy(nonce + IMPLICIT_NONCE_LEN, explicit_nonce, EXPLICIT_NONCE_LEN);
3828
3829 #ifndef HAVE_LIBGCRYPT_AEAD
3830         if (decoder->cipher_suite->mode == MODE_GCM) {
3831             /* NIST SP 800-38D, sect. 7.2 says that the 32-bit counter part starts
3832              * at 1, and gets incremented before passing to the block cipher. */
3833             memcpy(nonce_with_counter, nonce, IMPLICIT_NONCE_LEN + EXPLICIT_NONCE_LEN);
3834             nonce_with_counter[IMPLICIT_NONCE_LEN + EXPLICIT_NONCE_LEN + 3] = 2;
3835         } else { /* MODE_CCM and MODE_CCM_8 */
3836             /* The nonce for CCM and GCM are the same, but the nonce is used as input
3837              * in the CCM algorithm described in RFC 3610. The nonce generated here is
3838              * the one from RFC 3610 sect 2.3. Encryption. */
3839             /* Flags: (L-1) ; L = 16 - 1 - nonceSize */
3840             nonce_with_counter[0] = 3 - 1;
3841             memcpy(nonce_with_counter + 1, nonce, IMPLICIT_NONCE_LEN + EXPLICIT_NONCE_LEN);
3842             /* struct { opaque salt[4]; opaque nonce_explicit[8] } CCMNonce (RFC 6655) */
3843             nonce_with_counter[IMPLICIT_NONCE_LEN + EXPLICIT_NONCE_LEN + 3] = 1;
3844         }
3845 #endif
3846     } else if (version == TLSV1DOT3_VERSION) {
3847         /*
3848          * Technically the nonce length must be at least 8 bytes, but for
3849          * AES-GCM, AES-CCM and Poly1305-ChaCha20 the nonce length is exact 12.
3850          */
3851         const guint nonce_len = 12;
3852         DISSECTOR_ASSERT(decoder->write_iv.data_len == nonce_len);
3853         memcpy(nonce, decoder->write_iv.data, decoder->write_iv.data_len);
3854         /* Sequence number is left-padded with zeroes and XORed with write_iv */
3855         phton64(nonce + nonce_len - 8, pntoh64(nonce + nonce_len - 8) ^ decoder->seq);
3856         ssl_debug_printf("%s seq %" G_GUINT64_FORMAT "\n", G_STRFUNC, decoder->seq);
3857         decoder->seq++;             /* Implicit sequence number for TLS 1.3. */
3858     }
3859
3860     /* Set nonce and additional authentication data */
3861 #ifdef HAVE_LIBGCRYPT_AEAD
3862     gcry_cipher_reset(decoder->evp);
3863     ssl_print_data("nonce", nonce, 12);
3864     err = gcry_cipher_setiv(decoder->evp, nonce, 12);
3865     if (err) {
3866         ssl_debug_printf("%s failed to set nonce: %s\n", G_STRFUNC, gcry_strerror(err));
3867         return FALSE;
3868     }
3869
3870     if (decoder->cipher_suite->mode == MODE_CCM || decoder->cipher_suite->mode == MODE_CCM_8) {
3871         /* size of plaintext, additional authenticated data and auth tag. */
3872         guint64 lengths[3] = { ciphertext_len, is_v12 ? 13 : 0, auth_tag_len };
3873         gcry_cipher_ctl(decoder->evp, GCRYCTL_SET_CCM_LENGTHS, lengths, sizeof(lengths));
3874     }
3875
3876     /* (D)TLS 1.2 needs specific AAD, TLS 1.3 uses empty AAD. */
3877     if (is_v12) {
3878         guchar aad[13];
3879         phton64(aad, decoder->seq);         /* record sequence number */
3880         if (version == TLSV1DOT2_VERSION) {
3881             decoder->seq++;                 /* Implicit sequence number for TLS 1.2. */
3882         } else {
3883             phton16(aad, decoder->epoch);   /* DTLS 1.2 includes epoch. */
3884         }
3885         aad[8] = ct;                        /* TLSCompressed.type */
3886         phton16(aad + 9, record_version);   /* TLSCompressed.version */
3887         phton16(aad + 11, ciphertext_len);  /* TLSCompressed.length */
3888         ssl_print_data("AAD", aad, sizeof(aad));
3889         err = gcry_cipher_authenticate(decoder->evp, aad, sizeof(aad));
3890         if (err) {
3891             ssl_debug_printf("%s failed to set AAD: %s\n", G_STRFUNC, gcry_strerror(err));
3892             return FALSE;
3893         }
3894     }
3895 #else
3896     err = gcry_cipher_setctr(decoder->evp, nonce_with_counter, 16);
3897     if (err) {
3898         ssl_debug_printf("%s failed: failed to set CTR: %s\n", G_STRFUNC, gcry_strerror(err));
3899         return FALSE;
3900     }
3901 #endif
3902
3903     /* Decrypt now that nonce and AAD are set. */
3904     err = gcry_cipher_decrypt(decoder->evp, out_str->data, out_str->data_len, ciphertext, ciphertext_len);
3905     if (err) {
3906         ssl_debug_printf("%s decrypt failed: %s\n", G_STRFUNC, gcry_strerror(err));
3907         return FALSE;
3908     }
3909
3910     /* Check authentication tag for authenticity (replaces MAC) */
3911 #ifdef HAVE_LIBGCRYPT_AEAD
3912     err = gcry_cipher_gettag(decoder->evp, auth_tag_calc, auth_tag_len);
3913     if (err == 0 && !memcmp(auth_tag_calc, auth_tag_wire, auth_tag_len)) {
3914         ssl_print_data("auth_tag(OK)", auth_tag_calc, auth_tag_len);
3915     } else {
3916         if (err) {
3917             ssl_debug_printf("%s cannot obtain tag: %s\n", G_STRFUNC, gcry_strerror(err));
3918         } else {
3919             ssl_debug_printf("%s auth tag mismatch\n", G_STRFUNC);
3920             ssl_print_data("auth_tag(expect)", auth_tag_calc, auth_tag_len);
3921             ssl_print_data("auth_tag(actual)", auth_tag_wire, auth_tag_len);
3922         }
3923         if (ssl_ignore_mac_failed) {
3924             ssl_debug_printf("%s: auth check failed, but ignored for troubleshooting ;-)\n", G_STRFUNC);
3925         } else {
3926             return FALSE;
3927         }
3928     }
3929 #else
3930     ssl_debug_printf("Libgcrypt is older than 1.6, unable to verify auth tag!\n");
3931 #endif
3932
3933     ssl_print_data("Plaintext", out_str->data, ciphertext_len);
3934     *outl = ciphertext_len;
3935     return TRUE;
3936 }
3937
3938 /* Record decryption glue based on security parameters {{{ */
3939 /* Assume that we are called only for a non-NULL decoder which also means that
3940  * we have a non-NULL decoder->cipher_suite. */
3941 int
3942 ssl_decrypt_record(SslDecryptSession *ssl, SslDecoder *decoder, guint8 ct, guint16 record_version,
3943         const guchar *in, guint16 inl, StringInfo *comp_str, StringInfo *out_str, guint *outl)
3944 {
3945     guint   pad, worklen, uncomplen;
3946     guint8 *mac;
3947
3948     ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
3949     ssl_print_data("Ciphertext",in, inl);
3950
3951     if ((ssl->session.version == TLSV1DOT3_VERSION) != (decoder->cipher_suite->kex == KEX_TLS13)) {
3952         ssl_debug_printf("%s Invalid cipher suite for the protocol version!\n", G_STRFUNC);
3953         return -1;
3954     }
3955
3956     /* ensure we have enough storage space for decrypted data */
3957     if (inl > out_str->data_len)
3958     {
3959         ssl_debug_printf("ssl_decrypt_record: allocating %d bytes for decrypt data (old len %d)\n",
3960                 inl + 32, out_str->data_len);
3961         ssl_data_realloc(out_str, inl + 32);
3962     }
3963
3964     /* AEAD ciphers (GenericAEADCipher in TLS 1.2; TLS 1.3) have no padding nor
3965      * a separate MAC, so use a different routine for simplicity. */
3966     if (decoder->cipher_suite->mode == MODE_GCM ||
3967         decoder->cipher_suite->mode == MODE_CCM ||
3968         decoder->cipher_suite->mode == MODE_CCM_8 ||
3969         ssl->session.version == TLSV1DOT3_VERSION) {
3970
3971         if (!tls_decrypt_aead_record(ssl, decoder, ct, record_version, in, inl, out_str, &worklen)) {
3972             /* decryption failed */
3973             return -1;
3974         }
3975
3976         goto skip_mac;
3977     }
3978
3979     /* RFC 6101/2246: SSLCipherText/TLSCipherText has two structures for types:
3980      * (notation: { unencrypted, [ encrypted ] })
3981      * GenericStreamCipher: { [content, mac] }
3982      * GenericBlockCipher: { IV (TLS 1.1+), [content, mac, padding, padding_len] }
3983      * RFC 5426 (TLS 1.2): TLSCipherText has additionally:
3984      * GenericAEADCipher: { nonce_explicit, [content] }
3985      * RFC 4347 (DTLS): based on TLS 1.1, only GenericBlockCipher is supported.
3986      * RFC 6347 (DTLS 1.2): based on TLS 1.2, includes GenericAEADCipher too.
3987      */
3988
3989     /* (TLS 1.1 and later, DTLS) Extract explicit IV for GenericBlockCipher */
3990     if (decoder->cipher_suite->mode == MODE_CBC) {
3991         guint blocksize;
3992
3993         switch (ssl->session.version) {
3994         case TLSV1DOT1_VERSION:
3995         case TLSV1DOT2_VERSION:
3996         case DTLSV1DOT0_VERSION:
3997         case DTLSV1DOT2_VERSION:
3998         case DTLSV1DOT0_OPENSSL_VERSION:
3999             blocksize = ssl_get_cipher_blocksize(decoder->cipher_suite);
4000             if (inl < blocksize) {
4001                 ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for IV %d\n",
4002                         inl, blocksize);
4003                 return -1;
4004             }
4005             pad = gcry_cipher_setiv(decoder->evp, in, blocksize);
4006             if (pad != 0) {
4007                 ssl_debug_printf("ssl_decrypt_record failed: failed to set IV: %s %s\n",
4008                         gcry_strsource (pad), gcry_strerror (pad));
4009             }
4010
4011             inl -= blocksize;
4012             in += blocksize;
4013             break;
4014         }
4015     }
4016
4017     /* First decrypt*/
4018     if ((pad = ssl_cipher_decrypt(&decoder->evp, out_str->data, out_str->data_len, in, inl)) != 0) {
4019         ssl_debug_printf("ssl_decrypt_record failed: ssl_cipher_decrypt: %s %s\n", gcry_strsource (pad),
4020                     gcry_strerror (pad));
4021         return -1;
4022     }
4023
4024     ssl_print_data("Plaintext", out_str->data, inl);
4025     worklen=inl;
4026
4027
4028     /* strip padding for GenericBlockCipher */
4029     if (decoder->cipher_suite->mode == MODE_CBC) {
4030         if (inl < 1) { /* Should this check happen earlier? */
4031             ssl_debug_printf("ssl_decrypt_record failed: input length %d too small\n", inl);
4032             return -1;
4033         }
4034         pad=out_str->data[inl-1];
4035         if (worklen <= pad) {
4036             ssl_debug_printf("ssl_decrypt_record failed: padding %d too large for work %d\n",
4037                 pad, worklen);
4038             return -1;
4039         }
4040         worklen-=(pad+1);
4041         ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
4042             pad, worklen);
4043     }
4044
4045     /* MAC for GenericStreamCipher and GenericBlockCipher */
4046     if (ssl_cipher_suite_dig(decoder->cipher_suite)->len > (gint)worklen) {
4047         ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
4048         return -1;
4049     }
4050     worklen -= ssl_cipher_suite_dig(decoder->cipher_suite)->len;
4051     mac = out_str->data + worklen;
4052
4053     /* If NULL encryption active and no keys are available, do not bother
4054      * checking the MAC. We do not have keys for that. */
4055     if (decoder->cipher_suite->mode == MODE_STREAM &&
4056             decoder->cipher_suite->enc == ENC_NULL &&
4057             !(ssl->state & SSL_MASTER_SECRET)) {
4058         ssl_debug_printf("MAC check skipped due to missing keys\n");
4059         goto skip_mac;
4060     }
4061
4062     /* Now check the MAC */
4063     ssl_debug_printf("checking mac (len %d, version %X, ct %d seq %" G_GUINT64_FORMAT ")\n",
4064         worklen, ssl->session.version, ct, decoder->seq);
4065     if(ssl->session.version==SSLV3_VERSION){
4066         if(ssl3_check_mac(decoder,ct,out_str->data,worklen,mac) < 0) {
4067             if(ssl_ignore_mac_failed) {
4068                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
4069             }
4070             else{
4071                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
4072                 return -1;
4073             }
4074         }
4075         else{
4076             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
4077         }
4078     }
4079     else if(ssl->session.version==TLSV1_VERSION || ssl->session.version==TLSV1DOT1_VERSION || ssl->session.version==TLSV1DOT2_VERSION){
4080         if(tls_check_mac(decoder,ct,ssl->session.version,out_str->data,worklen,mac)< 0) {
4081             if(ssl_ignore_mac_failed) {
4082                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
4083             }
4084             else{
4085                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
4086                 return -1;
4087             }
4088         }
4089         else{
4090             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
4091         }
4092     }
4093     else if(ssl->session.version==DTLSV1DOT0_VERSION ||
4094         ssl->session.version==DTLSV1DOT2_VERSION ||
4095         ssl->session.version==DTLSV1DOT0_OPENSSL_VERSION){
4096         /* Try rfc-compliant mac first, and if failed, try old openssl's non-rfc-compliant mac */
4097         if(dtls_check_mac(decoder,ct,ssl->session.version,out_str->data,worklen,mac)>= 0) {
4098             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
4099         }
4100         else if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)>= 0) {
4101             ssl_debug_printf("ssl_decrypt_record: dtls rfc-compliant mac failed, but old openssl's non-rfc-compliant mac ok\n");
4102         }
4103         else if(ssl_ignore_mac_failed) {
4104             ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
4105         }
4106         else{
4107             ssl_debug_printf("ssl_decrypt_record: mac failed\n");
4108             return -1;
4109         }
4110     }
4111 skip_mac:
4112
4113     *outl = worklen;
4114
4115     if (decoder->compression > 0) {
4116         ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
4117         ssl_data_copy(comp_str, out_str);
4118         ssl_print_data("Plaintext compressed", comp_str->data, worklen);
4119         if (!decoder->decomp) {
4120             ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
4121             return -1;
4122         }
4123         if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
4124         ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
4125         *outl = uncomplen;
4126     }
4127
4128     return 0;
4129 }
4130 /* Record decryption glue based on security parameters }}} */
4131
4132 #endif /* HAVE_LIBGCRYPT */
4133
4134
4135 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
4136 /* RSA private key file processing {{{ */
4137 #define RSA_PARS 6
4138 static gcry_sexp_t
4139 ssl_privkey_to_sexp(gnutls_x509_privkey_t priv_key)
4140 {
4141     gnutls_datum_t rsa_datum[RSA_PARS]; /* m, e, d, p, q, u */
4142     size_t         tmp_size;
4143     gcry_error_t   gret;
4144     gcry_sexp_t    rsa_priv_key = NULL;
4145     gint           i;
4146     gcry_mpi_t     rsa_params[RSA_PARS];
4147
4148     /* RSA get parameter */
4149     if (gnutls_x509_privkey_export_rsa_raw(priv_key,
4150                                            &rsa_datum[0],
4151                                            &rsa_datum[1],
4152                                            &rsa_datum[2],
4153                                            &rsa_datum[3],
4154                                            &rsa_datum[4],
4155                                            &rsa_datum[5])  != 0) {
4156         ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
4157         return NULL;
4158     }
4159
4160     /* convert each rsa parameter to mpi format*/
4161     for(i=0; i<RSA_PARS; i++) {
4162       gret = gcry_mpi_scan(&rsa_params[i], GCRYMPI_FMT_USG, rsa_datum[i].data, rsa_datum[i].size,&tmp_size);
4163       /* these buffers were allocated by gnutls_x509_privkey_export_rsa_raw() */
4164       g_free(rsa_datum[i].data);
4165       if (gret != 0) {
4166         ssl_debug_printf("ssl_load_key: can't convert m rsa param to int (size %d)\n", rsa_datum[i].size);
4167         return NULL;
4168       }
4169     }
4170
4171     /* libgcrypt expects p < q, and gnutls might not return it as such, depending on gnutls version and its crypto backend */
4172     if (gcry_mpi_cmp(rsa_params[3], rsa_params[4]) > 0)
4173     {
4174         ssl_debug_printf("ssl_load_key: swapping p and q parameters and recomputing u\n");
4175         /* p, q = q, p */
4176         gcry_mpi_swap(rsa_params[3], rsa_params[4]);
4177         /* due to swapping p and q, u = p^-1 mod p which happens to be needed. */
4178     }
4179     /* libgcrypt expects u = p^-1 mod q (for OpenPGP), but the u parameter
4180      * says u = q^-1 mod p. Recompute u = p^-1 mod q. Do this unconditionally as
4181      * at least GnuTLS 2.12.23 computes an invalid value. */
4182     gcry_mpi_invm(rsa_params[5], rsa_params[3], rsa_params[4]);
4183
4184     if  (gcry_sexp_build( &rsa_priv_key, NULL,
4185             "(private-key(rsa((n%m)(e%m)(d%m)(p%m)(q%m)(u%m))))", rsa_params[0],
4186             rsa_params[1], rsa_params[2], rsa_params[3], rsa_params[4],
4187             rsa_params[5]) != 0) {
4188         ssl_debug_printf("ssl_load_key: can't build rsa private key s-exp\n");
4189         return NULL;
4190     }
4191
4192     for (i=0; i< 6; i++)
4193         gcry_mpi_release(rsa_params[i]);
4194     return rsa_priv_key;
4195 }
4196
4197 /** Load an RSA private key from specified file
4198  @param fp the file that contain the key data
4199  @return a pointer to the loaded key on success, or NULL */
4200 static gnutls_x509_privkey_t
4201 ssl_load_key(FILE* fp)
4202 {
4203     /* gnutls makes our work much harder, since we have to work internally with
4204      * s-exp formatted data, but PEM loader exports only in "gnutls_datum_t"
4205      * format, and a datum -> s-exp convertion function does not exist.
4206      */
4207     gnutls_x509_privkey_t priv_key;
4208     gnutls_datum_t        key;
4209     ws_statb64            statbuf;
4210     gint                  ret;
4211     guint                 bytes;
4212
4213     if (ws_fstat64(ws_fileno(fp), &statbuf) == -1) {
4214         ssl_debug_printf("ssl_load_key: can't ws_fstat64 file\n");
4215         return NULL;
4216     }
4217     if (S_ISDIR(statbuf.st_mode)) {
4218         ssl_debug_printf("ssl_load_key: file is a directory\n");
4219         errno = EISDIR;
4220         return NULL;
4221     }
4222     if (S_ISFIFO(statbuf.st_mode)) {
4223         ssl_debug_printf("ssl_load_key: file is a named pipe\n");
4224         errno = EINVAL;
4225         return NULL;
4226     }
4227     if (!S_ISREG(statbuf.st_mode)) {
4228         ssl_debug_printf("ssl_load_key: file is not a regular file\n");
4229         errno = EINVAL;
4230         return NULL;
4231     }
4232     /* XXX - check for a too-big size */
4233     /* load all file contents into a datum buffer*/
4234     key.data = (unsigned char *)g_malloc((size_t)statbuf.st_size);
4235     key.size = (int)statbuf.st_size;
4236     bytes = (guint) fread(key.data, 1, key.size, fp);
4237     if (bytes < key.size) {
4238         ssl_debug_printf("ssl_load_key: can't read from file %d bytes, got %d\n",
4239             key.size, bytes);
4240         g_free(key.data);
4241         return NULL;
4242     }
4243
4244     /* init private key data*/
4245     gnutls_x509_privkey_init(&priv_key);
4246
4247     /* import PEM data*/
4248     if ((ret = gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) {
4249         ssl_debug_printf("ssl_load_key: can't import pem data: %s\n", gnutls_strerror(ret));
4250         g_free(key.data);
4251         return NULL;
4252     }
4253
4254     if (gnutls_x509_privkey_get_pk_algorithm(priv_key) != GNUTLS_PK_RSA) {
4255         ssl_debug_printf("ssl_load_key: private key public key algorithm isn't RSA\n");
4256         g_free(key.data);
4257         return NULL;
4258     }
4259
4260     g_free(key.data);
4261
4262     return priv_key;
4263 }
4264
4265 static const char *
4266 BAGTYPE(gnutls_pkcs12_bag_type_t x) {
4267     switch (x) {
4268         case GNUTLS_BAG_EMPTY:               return "Empty";
4269         case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY: return "PKCS#8 Encrypted key";
4270         case GNUTLS_BAG_PKCS8_KEY:           return "PKCS#8 Key";
4271         case GNUTLS_BAG_CERTIFICATE:         return "Certificate";
4272         case GNUTLS_BAG_CRL:                 return "CRL";
4273         case GNUTLS_BAG_ENCRYPTED:           return "Encrypted";
4274         case GNUTLS_BAG_UNKNOWN:             return "Unknown";
4275         default:                             return "<undefined>";
4276     }
4277 }
4278
4279 /**
4280  * Load a RSA private key from a PKCS#12 file.
4281  * @param fp the file that contains the key data.
4282  * @param cert_passwd password to decrypt the PKCS#12 file.
4283  * @param[out] err error message upon failure; NULL upon success.
4284  * @return a pointer to the loaded key on success; NULL upon failure.
4285  */
4286 static gnutls_x509_privkey_t
4287 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd, char** err) {
4288
4289     int                       i, j, ret;
4290     int                       rest;
4291     unsigned char            *p;
4292     gnutls_datum_t            data;
4293     gnutls_pkcs12_bag_t       bag = NULL;
4294     gnutls_pkcs12_bag_type_t  bag_type;
4295     size_t                    len;
4296
4297     gnutls_pkcs12_t       ssl_p12  = NULL;
4298     gnutls_x509_privkey_t ssl_pkey = NULL;
4299
4300     gnutls_x509_privkey_t     priv_key = NULL;
4301     *err = NULL;
4302
4303     rest = 4096;
4304     data.data = (unsigned char *)g_malloc(rest);
4305     data.size = rest;
4306     p = data.data;
4307     while ((len = fread(p, 1, rest, fp)) > 0) {
4308         p += len;
4309         rest -= (int) len;
4310         if (!rest) {
4311             rest = 1024;
4312             data.data = (unsigned char *)g_realloc(data.data, data.size + rest);
4313             p = data.data + data.size;
4314             data.size += rest;
4315         }
4316     }
4317     data.size -= rest;
4318     ssl_debug_printf("%d bytes read\n", data.size);
4319     if (!feof(fp)) {
4320         *err = g_strdup("Error during certificate reading.");
4321         ssl_debug_printf("%s\n", *err);
4322         g_free(data.data);
4323         return 0;
4324     }
4325
4326     ret = gnutls_pkcs12_init(&ssl_p12);
4327     if (ret < 0) {
4328         *err = g_strdup_printf("gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
4329         ssl_debug_printf("%s\n", *err);
4330         g_free(data.data);
4331         return 0;
4332     }
4333
4334     /* load PKCS#12 in DER or PEM format */
4335     ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 0);
4336     if (ret < 0) {
4337         *err = g_strdup_printf("could not load PKCS#12 in DER format: %s", gnutls_strerror(ret));
4338         ssl_debug_printf("%s\n", *err);
4339         g_free(*err);
4340
4341         ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_PEM, 0);
4342         if (ret < 0) {
4343             *err = g_strdup_printf("could not load PKCS#12 in PEM format: %s", gnutls_strerror(ret));
4344             ssl_debug_printf("%s\n", *err);
4345         } else {
4346             *err = NULL;
4347         }
4348     }
4349     g_free(data.data);
4350     if (ret < 0) {
4351         return 0;
4352     }
4353
4354     ssl_debug_printf( "PKCS#12 imported\n");
4355
4356     /* TODO: Use gnutls_pkcs12_simple_parse, since 3.1.0 (August 2012) */
4357     for (i=0; ; i++) {
4358
4359         ret = gnutls_pkcs12_bag_init(&bag);
4360         if (ret < 0) break;
4361
4362         ret = gnutls_pkcs12_get_bag(ssl_p12, i, bag);
4363         if (ret < 0) break;
4364
4365         for (j=0; j<gnutls_pkcs12_bag_get_count(bag); j++) {
4366
4367             ret = gnutls_pkcs12_bag_get_type(bag, j);
4368             if (ret < 0) goto done;
4369             bag_type = (gnutls_pkcs12_bag_type_t)ret;
4370             if (bag_type >= GNUTLS_BAG_UNKNOWN) goto done;
4371             ssl_debug_printf( "Bag %d/%d: %s\n", i, j, BAGTYPE(bag_type));
4372             if (bag_type == GNUTLS_BAG_ENCRYPTED) {
4373                 ret = gnutls_pkcs12_bag_decrypt(bag, cert_passwd);
4374                 if (ret == 0) {
4375                     ret = gnutls_pkcs12_bag_get_type(bag, j);
4376                     if (ret < 0) goto done;
4377                     bag_type = (gnutls_pkcs12_bag_type_t)ret;
4378                     if (bag_type >= GNUTLS_BAG_UNKNOWN) goto done;
4379                     ssl_debug_printf( "Bag %d/%d decrypted: %s\n", i, j, BAGTYPE(bag_type));
4380                 }
4381             }
4382
4383             ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
4384             if (ret < 0) goto done;
4385
4386             switch (bag_type) {
4387
4388                 case GNUTLS_BAG_PKCS8_KEY:
4389                 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
4390
4391                     ret = gnutls_x509_privkey_init(&ssl_pkey);
4392                     if (ret < 0) {
4393                         *err = g_strdup_printf("gnutls_x509_privkey_init(&ssl_pkey) - %s", gnutls_strerror(ret));
4394                         ssl_debug_printf("%s\n", *err);
4395                         goto done;
4396                     }
4397                     ret = gnutls_x509_privkey_import_pkcs8(ssl_pkey, &data, GNUTLS_X509_FMT_DER, cert_passwd,
4398                                                            (bag_type==GNUTLS_BAG_PKCS8_KEY) ? GNUTLS_PKCS_PLAIN : 0);
4399                     if (ret < 0) {
4400                         *err = g_strdup_printf("Can not decrypt private key - %s", gnutls_strerror(ret));
4401                         ssl_debug_printf("%s\n", *err);
4402                         goto done;
4403                     }
4404
4405                     if (gnutls_x509_privkey_get_pk_algorithm(ssl_pkey) != GNUTLS_PK_RSA) {
4406                         *err = g_strdup("ssl_load_pkcs12: private key public key algorithm isn't RSA");
4407                         ssl_debug_printf("%s\n", *err);
4408                         goto done;
4409                     }
4410
4411                     /* Private key found, return it. */
4412                     priv_key = ssl_pkey;
4413                     goto done;
4414
4415                 default: ;
4416             }
4417         }  /* j */
4418         if (bag) { gnutls_pkcs12_bag_deinit(bag); bag = NULL; }
4419     }  /* i */
4420
4421 done:
4422     if (!priv_key && ssl_pkey)
4423         gnutls_x509_privkey_deinit(ssl_pkey);
4424     if (bag)
4425         gnutls_pkcs12_bag_deinit(bag);
4426
4427     return priv_key;
4428 }
4429
4430
4431 void
4432 ssl_private_key_free(gpointer key)
4433 {
4434     gcry_sexp_release((gcry_sexp_t) key);
4435 }
4436
4437 static void
4438 ssl_find_private_key_by_pubkey(SslDecryptSession *ssl, GHashTable *key_hash,
4439                                gnutls_datum_t *subjectPublicKeyInfo)
4440 {
4441     gnutls_pubkey_t pubkey = NULL;
4442     guchar key_id[20];
4443     size_t key_id_len = sizeof(key_id);
4444     int r;
4445
4446     if (!subjectPublicKeyInfo->size) {
4447         ssl_debug_printf("%s: could not find SubjectPublicKeyInfo\n", G_STRFUNC);
4448         return;
4449     }
4450
4451     r = gnutls_pubkey_init(&pubkey);
4452     if (r < 0) {
4453         ssl_debug_printf("%s: failed to init pubkey: %s\n",
4454                 G_STRFUNC, gnutls_strerror(r));
4455         return;
4456     }
4457
4458     r = gnutls_pubkey_import(pubkey, subjectPublicKeyInfo, GNUTLS_X509_FMT_DER);
4459     if (r < 0) {
4460         ssl_debug_printf("%s: failed to import pubkey from handshake: %s\n",
4461                 G_STRFUNC, gnutls_strerror(r));
4462         goto end;
4463     }
4464
4465     /* Generate a 20-byte SHA-1 hash. */
4466     r = gnutls_pubkey_get_key_id(pubkey, 0, key_id, &key_id_len);
4467     if (r < 0) {
4468         ssl_debug_printf("%s: failed to extract key id from pubkey: %s\n",
4469                 G_STRFUNC, gnutls_strerror(r));
4470         goto end;
4471     }
4472
4473     ssl_print_data("lookup(KeyID)", key_id, key_id_len);
4474     ssl->private_key = (gcry_sexp_t)g_hash_table_lookup(key_hash, key_id);
4475     ssl_debug_printf("%s: lookup result: %p\n", G_STRFUNC, (void *) ssl->private_key);
4476
4477 end:
4478     gnutls_pubkey_deinit(pubkey);
4479 }
4480
4481 /* RSA private key file processing }}} */
4482
4483 #else /* ! (defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)) */
4484 void
4485 ssl_private_key_free(gpointer key _U_)
4486 {
4487 }
4488 #endif /* ! (defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)) */
4489
4490
4491 /*--- Start of dissector-related code below ---*/
4492
4493 /* get ssl data for this session. if no ssl data is found allocate a new one*/
4494 SslDecryptSession *
4495 ssl_get_session(conversation_t *conversation, dissector_handle_t ssl_handle)
4496 {
4497     void               *conv_data;
4498     SslDecryptSession  *ssl_session;
4499     int                 proto_ssl;
4500
4501     proto_ssl = dissector_handle_get_protocol_index(ssl_handle);
4502     conv_data = conversation_get_proto_data(conversation, proto_ssl);
4503     if (conv_data != NULL)
4504         return (SslDecryptSession *)conv_data;
4505
4506     /* no previous SSL conversation info, initialize it. */
4507     ssl_session = wmem_new0(wmem_file_scope(), SslDecryptSession);
4508
4509     /* data_len is the part that is meaningful, not the allocated length */
4510     ssl_session->master_secret.data_len = 0;
4511     ssl_session->master_secret.data = ssl_session->_master_secret;
4512     ssl_session->session_id.data_len = 0;
4513     ssl_session->session_id.data = ssl_session->_session_id;
4514     ssl_session->client_random.data_len = 0;
4515     ssl_session->client_random.data = ssl_session->_client_random;
4516     ssl_session->server_random.data_len = 0;
4517     ssl_session->server_random.data = ssl_session->_server_random;
4518     ssl_session->session_ticket.data_len = 0;
4519     ssl_session->session_ticket.data = NULL; /* will be re-alloced as needed */
4520     ssl_session->server_data_for_iv.data_len = 0;
4521     ssl_session->server_data_for_iv.data = ssl_session->_server_data_for_iv;
4522     ssl_session->client_data_for_iv.data_len = 0;
4523     ssl_session->client_data_for_iv.data = ssl_session->_client_data_for_iv;
4524     ssl_session->app_data_segment.data = NULL;
4525     ssl_session->app_data_segment.data_len = 0;
4526     ssl_session->handshake_data.data=NULL;
4527     ssl_session->handshake_data.data_len=0;
4528
4529     /* Initialize parameters which are not necessary specific to decryption. */
4530     ssl_session->session.version = SSL_VER_UNKNOWN;
4531     clear_address(&ssl_session->session.srv_addr);
4532     ssl_session->session.srv_ptype = PT_NONE;
4533     ssl_session->session.srv_port = 0;
4534
4535     conversation_add_proto_data(conversation, proto_ssl, ssl_session);
4536     return ssl_session;
4537 }
4538
4539 /* Resets the decryption parameters for the next decoder. */
4540 static void ssl_reset_session(SslSession *session, SslDecryptSession *ssl, gboolean is_client)
4541 {
4542     if (ssl) {
4543         /* Ensure that secrets are not restored using stale identifiers. Split
4544          * between client and server in case the packets somehow got out of order. */
4545         gint clear_flags = SSL_HAVE_SESSION_KEY | SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET;
4546
4547         if (is_client) {
4548             clear_flags |= SSL_CLIENT_EXTENDED_MASTER_SECRET;
4549             ssl->session_id.data_len = 0;
4550             ssl->session_ticket.data_len = 0;
4551             ssl->master_secret.data_len = 0;
4552             ssl->client_random.data_len = 0;
4553             ssl->has_early_data = FALSE;
4554         } else {
4555             clear_flags |= SSL_SERVER_EXTENDED_MASTER_SECRET | SSL_NEW_SESSION_TICKET;
4556             ssl->server_random.data_len = 0;
4557             ssl->pre_master_secret.data_len = 0;
4558 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
4559             ssl->private_key = NULL;
4560 #endif
4561             ssl->psk.data_len = 0;
4562         }
4563
4564         if (ssl->state & clear_flags) {
4565             ssl_debug_printf("%s detected renegotiation, clearing 0x%02x (%s side)\n",
4566                     G_STRFUNC, ssl->state & clear_flags, is_client ? "client" : "server");
4567             ssl->state &= ~clear_flags;
4568         }
4569     }
4570
4571     /* These flags might be used for non-decryption purposes and may affect the
4572      * dissection, so reset them as well. */
4573     if (is_client) {
4574         session->client_cert_type = 0;
4575     } else {
4576         session->compression = 0;
4577         session->server_cert_type = 0;
4578         /* session->is_session_resumed is already handled in the ServerHello dissection. */
4579     }
4580 }
4581
4582 static guint32
4583 ssl_starttls(dissector_handle_t ssl_handle, packet_info *pinfo,
4584                  dissector_handle_t app_handle, guint32 last_nontls_frame)
4585 {
4586     conversation_t  *conversation;
4587     SslSession      *session;
4588
4589     /* Ignore if the SSL dissector is disabled. */
4590     if (!ssl_handle)
4591         return 0;
4592     /* The caller should always pass a valid handle to its own dissector. */
4593     DISSECTOR_ASSERT(app_handle);
4594
4595     conversation = find_or_create_conversation(pinfo);
4596     session = &ssl_get_session(conversation, ssl_handle)->session;
4597
4598     ssl_debug_printf("%s: old frame %d, app_handle=%p (%s)\n", G_STRFUNC,
4599                      session->last_nontls_frame,
4600                      (void *)session->app_handle,
4601                      dissector_handle_get_dissector_name(session->app_handle));
4602     ssl_debug_printf("%s: current frame %d, app_handle=%p (%s)\n", G_STRFUNC,
4603                      pinfo->num, (void *)app_handle,
4604                      dissector_handle_get_dissector_name(app_handle));
4605
4606     /* Do not switch again if a dissector did it before. */
4607     if (session->last_nontls_frame) {
4608         ssl_debug_printf("%s: not overriding previous app handle!\n", G_STRFUNC);
4609         return session->last_nontls_frame;
4610     }
4611
4612     session->app_handle = app_handle;
4613     /* The SSL dissector should be called first for this conversation. */
4614     conversation_set_dissector(conversation, ssl_handle);
4615     /* SSL starts after this frame. */
4616     session->last_nontls_frame = last_nontls_frame;
4617     return 0;
4618 } /* }}} */
4619
4620 /* ssl_starttls_ack: mark future frames as encrypted. {{{ */
4621 guint32
4622 ssl_starttls_ack(dissector_handle_t ssl_handle, packet_info *pinfo,
4623                  dissector_handle_t app_handle)
4624 {
4625     return ssl_starttls(ssl_handle, pinfo, app_handle, pinfo->num);
4626 }
4627
4628 guint32
4629 ssl_starttls_post_ack(dissector_handle_t ssl_handle, packet_info *pinfo,
4630                  dissector_handle_t app_handle)
4631 {
4632     return ssl_starttls(ssl_handle, pinfo, app_handle, pinfo->num - 1);
4633 }
4634
4635 dissector_handle_t
4636 ssl_find_appdata_dissector(const char *name)
4637 {
4638     /* Accept 'http' for backwards compatibility and sanity. */
4639     if (!strcmp(name, "http"))
4640         name = "http-over-tls";
4641     return find_dissector(name);
4642 }
4643
4644 /* Functions for TLS/DTLS sessions and RSA private keys hashtables. {{{ */
4645 static gint
4646 ssl_equal (gconstpointer v, gconstpointer v2)
4647 {
4648     const StringInfo *val1;
4649     const StringInfo *val2;
4650     val1 = (const StringInfo *)v;
4651     val2 = (const StringInfo *)v2;
4652
4653     if (val1->data_len == val2->data_len &&
4654         !memcmp(val1->data, val2->data, val2->data_len)) {
4655         return 1;
4656     }
4657     return 0;
4658 }
4659
4660 static guint
4661 ssl_hash  (gconstpointer v)
4662 {
4663     guint l,hash;
4664     const StringInfo* id;
4665     const guint* cur;
4666     hash = 0;
4667     id = (const StringInfo*) v;
4668
4669     /*  id and id->data are mallocated in ssl_save_master_key().  As such 'data'
4670      *  should be aligned for any kind of access (for example as a guint as
4671      *  is done below).  The intermediate void* cast is to prevent "cast
4672      *  increases required alignment of target type" warnings on CPUs (such
4673      *  as SPARCs) that do not allow misaligned memory accesses.
4674      */
4675     cur = (const guint*)(void*) id->data;
4676
4677     for (l=4; (l < id->data_len); l+=4, cur++)
4678         hash = hash ^ (*cur);
4679
4680     return hash;
4681 }
4682
4683 gboolean
4684 ssl_private_key_equal (gconstpointer v, gconstpointer v2)
4685 {
4686     /* key ID length (SHA-1 hash, per GNUTLS_KEYID_USE_SHA1) */
4687     return !memcmp(v, v2, 20);
4688 }
4689
4690 guint
4691 ssl_private_key_hash (gconstpointer v)
4692 {
4693     guint        l, hash = 0;
4694     const guint8 *cur = (const guint8 *)v;
4695
4696     /* The public key' SHA-1 hash (which maps to a private key) has a uniform
4697      * distribution, hence simply xor'ing them should be sufficient. */
4698     for (l = 0; l < 20; l += 4, cur += 4)
4699         hash ^= pntoh32(cur);
4700
4701     return hash;
4702 }
4703 /* Functions for TLS/DTLS sessions and RSA private keys hashtables. }}} */
4704
4705 /* Handling of association between tls/dtls ports and clear text protocol. {{{ */
4706 void
4707 ssl_association_add(const char* dissector_table_name, dissector_handle_t main_handle, dissector_handle_t subdissector_handle, guint port, gboolean tcp)
4708 {
4709     DISSECTOR_ASSERT(main_handle);
4710     DISSECTOR_ASSERT(subdissector_handle);
4711     /* Registration is required for Export PDU feature to work properly. */
4712     DISSECTOR_ASSERT_HINT(dissector_handle_get_dissector_name(subdissector_handle),
4713             "SSL appdata dissectors must register with register_dissector()!");
4714     ssl_debug_printf("association_add %s port %d handle %p\n", dissector_table_name, port, (void *)subdissector_handle);
4715
4716     if (port) {
4717         dissector_add_uint(dissector_table_name, port, subdissector_handle);
4718         if (tcp)
4719             dissector_add_uint("tcp.port", port, main_handle);
4720         else
4721             dissector_add_uint("udp.port", port, main_handle);
4722         dissector_add_uint("sctp.port", port, main_handle);
4723     } else {
4724         dissector_add_for_decode_as(dissector_table_name, subdissector_handle);
4725     }
4726 }
4727
4728 void
4729 ssl_association_remove(const char* dissector_table_name, dissector_handle_t main_handle, dissector_handle_t subdissector_handle, guint port, gboolean tcp)
4730 {
4731     ssl_debug_printf("ssl_association_remove removing %s %u - handle %p\n",
4732                      tcp?"TCP":"UDP", port, (void *)subdissector_handle);
4733     if (main_handle) {
4734         dissector_delete_uint(tcp?"tcp.port":"udp.port", port, main_handle);
4735         dissector_delete_uint("sctp.port", port, main_handle);
4736     }
4737
4738     if (port) {
4739         dissector_delete_uint(dissector_table_name, port, subdissector_handle);
4740     }
4741 }
4742
4743 void
4744 ssl_set_server(SslSession *session, address *addr, port_type ptype, guint32 port)
4745 {
4746     copy_address_wmem(wmem_file_scope(), &session->srv_addr, addr);
4747     session->srv_ptype = ptype;
4748     session->srv_port = port;
4749 }
4750
4751 int
4752 ssl_packet_from_server(SslSession *session, dissector_table_t table, packet_info *pinfo)
4753 {
4754     gint ret;
4755     if (session->srv_addr.type != AT_NONE) {
4756         ret = (session->srv_ptype == pinfo->ptype) &&
4757               (session->srv_port == pinfo->srcport) &&
4758               addresses_equal(&session->srv_addr, &pinfo->src);
4759     } else {
4760         ret = (dissector_get_uint_handle(table, pinfo->srcport) != 0);
4761     }
4762
4763     ssl_debug_printf("packet_from_server: is from server - %s\n", (ret)?"TRUE":"FALSE");
4764     return ret;
4765 }
4766 /* Handling of association between tls/dtls ports and clear text protocol. }}} */
4767
4768
4769 /* Links SSL records with the real packet data. {{{ */
4770 /**
4771  * Remembers the decrypted TLS record fragment (TLSInnerPlaintext in TLS 1.3) to
4772  * avoid the need for a decoder in the second pass. Additionally, it remembers
4773  * sequence numbers (for reassembly and Follow SSL Stream).
4774  *
4775  * @param proto The protocol identifier (proto_ssl or proto_dtls).
4776  * @param pinfo The packet where the record originates from.
4777  * @param data Decrypted data to store in the record.
4778  * @param data_len Length of decrypted record data.
4779  * @param record_id The identifier for this record within the current packet.
4780  * @param flow Information about sequence numbers, etc.
4781  * @param type TLS Content Type (such as handshake or application_data).
4782  */
4783 void
4784 ssl_add_record_info(gint proto, packet_info *pinfo, const guchar *data, gint data_len, gint record_id, SslFlow *flow, ContentType type)
4785 {
4786     SslRecordInfo* rec, **prec;
4787     SslPacketInfo* pi;
4788
4789     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
4790     if (!pi)
4791     {
4792         pi = wmem_new0(wmem_file_scope(), SslPacketInfo);
4793         p_add_proto_data(wmem_file_scope(), pinfo, proto, 0, pi);
4794     }
4795
4796     rec = wmem_new(wmem_file_scope(), SslRecordInfo);
4797     rec->plain_data = (guchar *)wmem_memdup(wmem_file_scope(), data, data_len);
4798     rec->data_len = data_len;
4799     rec->id = record_id;
4800     rec->type = type;
4801     rec->next = NULL;
4802
4803     /* TODO allow Handshake records also to be reassembled. There needs to be
4804      * one "flow" for each record type (appdata, handshake). "seq" for the
4805      * record should then be relative within this flow. */
4806     if (flow && type == SSL_ID_APP_DATA) {
4807         rec->seq = flow->byte_seq;
4808         rec->flow = flow;
4809         flow->byte_seq += data_len;
4810         ssl_debug_printf("%s stored decrypted record seq=%d nxtseq=%d flow=%p\n",
4811                          G_STRFUNC, rec->seq, rec->seq + data_len, (void*)flow);
4812     }
4813
4814     /* Remember decrypted records. */
4815     prec = &pi->records;
4816     while (*prec) prec = &(*prec)->next;
4817     *prec = rec;
4818 }
4819
4820 /* search in packet data for the specified id; return a newly created tvb for the associated data */
4821 tvbuff_t*
4822 ssl_get_record_info(tvbuff_t *parent_tvb, int proto, packet_info *pinfo, gint record_id, SslRecordInfo **matched_record)
4823 {
4824     SslRecordInfo* rec;
4825     SslPacketInfo* pi;
4826     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
4827
4828     if (!pi)
4829         return NULL;
4830
4831     for (rec = pi->records; rec; rec = rec->next)
4832         if (rec->id == record_id) {
4833             *matched_record = rec;
4834             /* link new real_data_tvb with a parent tvb so it is freed when frame dissection is complete */
4835             return tvb_new_child_real_data(parent_tvb, rec->plain_data, rec->data_len, rec->data_len);
4836         }
4837
4838     return NULL;
4839 }
4840 /* Links SSL records with the real packet data. }}} */
4841
4842 /* initialize/reset per capture state data (ssl sessions cache). {{{ */
4843 void
4844 ssl_common_init(ssl_master_key_map_t *mk_map,
4845                 StringInfo *decrypted_data, StringInfo *compressed_data)
4846 {
4847     mk_map->session = g_hash_table_new(ssl_hash, ssl_equal);
4848     mk_map->tickets = g_hash_table_new(ssl_hash, ssl_equal);
4849     mk_map->crandom = g_hash_table_new(ssl_hash, ssl_equal);
4850     mk_map->pre_master = g_hash_table_new(ssl_hash, ssl_equal);
4851     mk_map->pms = g_hash_table_new(ssl_hash, ssl_equal);
4852     mk_map->tls13_client_early = g_hash_table_new(ssl_hash, ssl_equal);
4853     mk_map->tls13_client_handshake = g_hash_table_new(ssl_hash, ssl_equal);
4854     mk_map->tls13_server_handshake = g_hash_table_new(ssl_hash, ssl_equal);
4855     mk_map->tls13_client_appdata = g_hash_table_new(ssl_hash, ssl_equal);
4856     mk_map->tls13_server_appdata = g_hash_table_new(ssl_hash, ssl_equal);
4857     ssl_data_alloc(decrypted_data, 32);
4858     ssl_data_alloc(compressed_data, 32);
4859 }
4860
4861 void
4862 ssl_common_cleanup(ssl_master_key_map_t *mk_map, FILE **ssl_keylog_file,
4863                    StringInfo *decrypted_data, StringInfo *compressed_data)
4864 {
4865     g_hash_table_destroy(mk_map->session);
4866     g_hash_table_destroy(mk_map->tickets);
4867     g_hash_table_destroy(mk_map->crandom);
4868     g_hash_table_destroy(mk_map->pre_master);
4869     g_hash_table_destroy(mk_map->pms);
4870     g_hash_table_destroy(mk_map->tls13_client_early);
4871     g_hash_table_destroy(mk_map->tls13_client_handshake);
4872     g_hash_table_destroy(mk_map->tls13_server_handshake);
4873     g_hash_table_destroy(mk_map->tls13_client_appdata);
4874     g_hash_table_destroy(mk_map->tls13_server_appdata);
4875
4876     g_free(decrypted_data->data);
4877     g_free(compressed_data->data);
4878
4879     /* close the previous keylog file now that the cache are cleared, this
4880      * allows the cache to be filled with the full keylog file contents. */
4881     if (*ssl_keylog_file) {
4882         fclose(*ssl_keylog_file);
4883         *ssl_keylog_file = NULL;
4884     }
4885 }
4886 /* }}} */
4887
4888 /* parse ssl related preferences (private keys and ports association strings) */
4889 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
4890 /* Load a single RSA key file item from preferences. {{{ */
4891 void
4892 ssl_parse_key_list(const ssldecrypt_assoc_t *uats, GHashTable *key_hash, const char* dissector_table_name, dissector_handle_t main_handle, gboolean tcp)
4893 {
4894     gnutls_x509_privkey_t priv_key;
4895     gcry_sexp_t        private_key;
4896     FILE*              fp     = NULL;
4897     int                ret;
4898     size_t             key_id_len = 20;
4899     guchar            *key_id = NULL;
4900     dissector_handle_t handle;
4901     /* try to load keys file first */
4902     fp = ws_fopen(uats->keyfile, "rb");
4903     if (!fp) {
4904         report_open_failure(uats->keyfile, errno, FALSE);
4905         return;
4906     }
4907
4908     if ((gint)strlen(uats->password) == 0) {
4909         priv_key = ssl_load_key(fp);
4910     } else {
4911         char *err = NULL;
4912         priv_key = ssl_load_pkcs12(fp, uats->password, &err);
4913         if (err) {
4914             report_failure("%s\n", err);
4915             g_free(err);
4916         }
4917     }
4918     fclose(fp);
4919
4920     if (!priv_key) {
4921         report_failure("Can't load private key from %s\n", uats->keyfile);
4922         return;
4923     }
4924
4925     key_id = (guchar *) g_malloc0(key_id_len);
4926     ret = gnutls_x509_privkey_get_key_id(priv_key, 0, key_id, &key_id_len);
4927     if (ret < 0) {
4928         report_failure("Can't calculate public key ID for %s: %s",
4929                 uats->keyfile, gnutls_strerror(ret));
4930         goto end;
4931     }
4932     ssl_print_data("KeyID", key_id, key_id_len);
4933
4934     private_key = ssl_privkey_to_sexp(priv_key);
4935     if (!private_key) {
4936         report_failure("Can't extract private key parameters for %s", uats->keyfile);
4937         goto end;
4938     }
4939
4940     g_hash_table_replace(key_hash, key_id, private_key);
4941     key_id = NULL; /* used in key_hash, do not free. */
4942     ssl_debug_printf("ssl_init private key file %s successfully loaded.\n", uats->keyfile);
4943
4944     handle = ssl_find_appdata_dissector(uats->protocol);
4945     if (handle) {
4946         /* Port to subprotocol mapping */
4947         guint16 port = 0;
4948         if (ws_strtou16(uats->port, NULL, &port)) {
4949             if (port > 0) {
4950                 ssl_debug_printf("ssl_init port '%d' filename '%s' password(only for p12 file) '%s'\n",
4951                     port, uats->keyfile, uats->password);
4952
4953                 ssl_association_add(dissector_table_name, main_handle, handle, port, tcp);
4954             }
4955         } else {
4956             if (strcmp(uats->port, "start_tls"))
4957                 ssl_debug_printf("invalid ssl_init_port: %s\n", uats->port);
4958         }
4959     }
4960
4961 end:
4962     gnutls_x509_privkey_deinit(priv_key);
4963     g_free(key_id);
4964 }
4965 /* }}} */
4966 #else
4967 void
4968 ssl_parse_key_list(const ssldecrypt_assoc_t *uats _U_, GHashTable *key_hash _U_, const char* dissector_table_name _U_, dissector_handle_t main_handle _U_, gboolean tcp _U_)
4969 {
4970     report_failure("Can't load private key files, support is not compiled in.");
4971 }
4972 #endif
4973
4974
4975 #ifdef HAVE_LIBGCRYPT /* useless without decryption support. */
4976 /* Store/load a known (pre-)master secret from/for this SSL session. {{{ */
4977 /** store a known (pre-)master secret into cache */
4978 static void
4979 ssl_save_master_key(const char *label, GHashTable *ht, StringInfo *key,
4980                     StringInfo *mk)
4981 {
4982     StringInfo *ht_key, *master_secret;
4983
4984     if (key->data_len == 0) {
4985         ssl_debug_printf("%s: not saving empty %s!\n", G_STRFUNC, label);
4986         return;
4987     }
4988
4989     if (mk->data_len == 0) {
4990         ssl_debug_printf("%s not saving empty (pre-)master secret for %s!\n",
4991                          G_STRFUNC, label);
4992         return;
4993     }
4994
4995     /* ssl_hash() depends on session_ticket->data being aligned for guint access
4996      * so be careful in changing how it is allocated. */
4997     ht_key = ssl_data_clone(key);
4998     master_secret = ssl_data_clone(mk);
4999     g_hash_table_insert(ht, ht_key, master_secret);
5000
5001     ssl_debug_printf("%s inserted (pre-)master secret for %s\n", G_STRFUNC, label);
5002     ssl_print_string("stored key", ht_key);
5003     ssl_print_string("stored (pre-)master secret", master_secret);
5004 }
5005
5006 /** restore a (pre-)master secret given some key in the cache */
5007 static gboolean
5008 ssl_restore_master_key(SslDecryptSession *ssl, const char *label,
5009                        gboolean is_pre_master, GHashTable *ht, StringInfo *key)
5010 {
5011     StringInfo *ms;
5012
5013     if (key->data_len == 0) {
5014         ssl_debug_printf("%s can't restore %smaster secret using an empty %s\n",
5015                          G_STRFUNC, is_pre_master ? "pre-" : "", label);
5016         return FALSE;
5017     }
5018
5019     ms = (StringInfo *)g_hash_table_lookup(ht, key);
5020     if (!ms) {
5021         ssl_debug_printf("%s can't find %smaster secret by %s\n", G_STRFUNC,
5022                          is_pre_master ? "pre-" : "", label);
5023         return FALSE;
5024     }
5025
5026     /* (pre)master secret found, clear knowledge of other keys and set it in the
5027      * current conversation */
5028     ssl->state &= ~(SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET |
5029                     SSL_HAVE_SESSION_KEY);
5030     if (is_pre_master) {
5031         /* unlike master secret, pre-master secret has a variable size (48 for
5032          * RSA, varying for PSK) and is therefore not statically allocated */
5033         ssl->pre_master_secret.data = (guchar *) wmem_alloc(wmem_file_scope(),
5034                                                             ms->data_len);
5035         ssl_data_set(&ssl->pre_master_secret, ms->data, ms->data_len);
5036         ssl->state |= SSL_PRE_MASTER_SECRET;
5037     } else {
5038         ssl_data_set(&ssl->master_secret, ms->data, ms->data_len);
5039         ssl->state |= SSL_MASTER_SECRET;
5040     }
5041     ssl_debug_printf("%s %smaster secret retrieved using %s\n", G_STRFUNC,
5042                      is_pre_master ? "pre-" : "", label);
5043     ssl_print_string(label, key);
5044     ssl_print_string("(pre-)master secret", ms);
5045     return TRUE;
5046 }
5047 /* Store/load a known (pre-)master secret from/for this SSL session. }}} */
5048
5049 /* Should be called when all parameters are ready (after ChangeCipherSpec), and
5050  * the decoder should be attempted to be initialized. {{{*/
5051 void
5052 ssl_finalize_decryption(SslDecryptSession *ssl, ssl_master_key_map_t *mk_map)
5053 {
5054     if (ssl->session.version == TLSV1DOT3_VERSION) {
5055         /* TLS 1.3 implementations only provide secrets derived from the master
5056          * secret which are loaded in tls13_change_key. No master secrets can be
5057          * loaded here, so just return. */
5058         return;
5059     }
5060     ssl_debug_printf("%s state = 0x%02X\n", G_STRFUNC, ssl->state);
5061     if (ssl->state & SSL_HAVE_SESSION_KEY) {
5062         ssl_debug_printf("  session key already available, nothing to do.\n");
5063         return;
5064     }
5065     if (!(ssl->state & SSL_CIPHER)) {
5066         ssl_debug_printf("  Cipher suite (Server Hello) is missing!\n");
5067         return;
5068     }
5069
5070     /* for decryption, there needs to be a master secret (which can be derived
5071      * from pre-master secret). If missing, try to pick a master key from cache
5072      * (an earlier packet in the capture or key logfile). */
5073     if (!(ssl->state & (SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET)) &&
5074         !ssl_restore_master_key(ssl, "Session ID", FALSE,
5075                                 mk_map->session, &ssl->session_id) &&
5076         (!ssl->session.is_session_resumed ||
5077          !ssl_restore_master_key(ssl, "Session Ticket", FALSE,
5078                                  mk_map->tickets, &ssl->session_ticket)) &&
5079         !ssl_restore_master_key(ssl, "Client Random", FALSE,
5080                                 mk_map->crandom, &ssl->client_random)) {
5081         if (ssl->cipher_suite->enc != ENC_NULL) {
5082             /* how unfortunate, the master secret could not be found */
5083             ssl_debug_printf("  Cannot find master secret\n");
5084             return;
5085         } else {
5086             ssl_debug_printf(" Cannot find master secret, continuing anyway "
5087                     "because of a NULL cipher\n");
5088         }
5089     }
5090
5091     if (ssl_generate_keyring_material(ssl) < 0) {
5092         ssl_debug_printf("%s can't generate keyring material\n", G_STRFUNC);
5093         return;
5094     }
5095     /* Save Client Random/ Session ID for "SSL Export Session keys" */
5096     ssl_save_master_key("Client Random", mk_map->crandom,
5097                         &ssl->client_random, &ssl->master_secret);
5098     ssl_save_master_key("Session ID", mk_map->session,
5099                         &ssl->session_id, &ssl->master_secret);
5100     /* Only save the new secrets if the server sent the ticket. The client
5101      * ticket might have become stale. */
5102     if (ssl->state & SSL_NEW_SESSION_TICKET) {
5103         ssl_save_master_key("Session Ticket", mk_map->tickets,
5104                             &ssl->session_ticket, &ssl->master_secret);
5105     }
5106 } /* }}} */
5107
5108 /* Load the new key. */
5109 void
5110 tls13_change_key(SslDecryptSession *ssl, ssl_master_key_map_t *mk_map,
5111                  gboolean is_from_server, TLSRecordType type)
5112 {
5113     GHashTable *key_map;
5114     const char *label;
5115
5116     if (ssl->session.version != TLSV1DOT3_VERSION) {
5117         ssl_debug_printf("%s TLS version %#x is not 1.3\n", G_STRFUNC, ssl->session.version);
5118         return;
5119     }
5120
5121     if (ssl->client_random.data_len == 0) {
5122         /* May happen if Hello message is missing and Finished is found. */
5123         ssl_debug_printf("%s missing Client Random\n", G_STRFUNC);
5124         return;
5125     }
5126
5127     switch (type) {
5128     case TLS_SECRET_0RTT_APP:
5129         DISSECTOR_ASSERT(!is_from_server);
5130         label = "CLIENT_EARLY_TRAFFIC_SECRET";
5131         key_map = mk_map->tls13_client_early;
5132         break;
5133     case TLS_SECRET_HANDSHAKE:
5134         if (is_from_server) {
5135             label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
5136             key_map = mk_map->tls13_server_handshake;
5137         } else {
5138             label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
5139             key_map = mk_map->tls13_client_handshake;
5140         }
5141         break;
5142     case TLS_SECRET_APP:
5143         if (is_from_server) {
5144             label = "SERVER_TRAFFIC_SECRET_0";
5145             key_map = mk_map->tls13_server_appdata;
5146         } else {
5147             label = "CLIENT_TRAFFIC_SECRET_0";
5148             key_map = mk_map->tls13_client_appdata;
5149         }
5150         break;
5151     default:
5152         g_assert_not_reached();
5153     }
5154
5155     /* Transitioning to new keys, mark old ones as unusable. */
5156     ssl_debug_printf("%s transitioning to new key, old state 0x%02x\n", G_STRFUNC, ssl->state);
5157     ssl->state &= ~(SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET | SSL_HAVE_SESSION_KEY);
5158
5159     StringInfo *secret = (StringInfo *)g_hash_table_lookup(key_map, &ssl->client_random);
5160     if (!secret) {
5161         ssl_debug_printf("%s Cannot find %s, decryption impossible\n", G_STRFUNC, label);
5162         /* Disable decryption, the keys are invalid. */
5163         if (is_from_server) {
5164             ssl->server = NULL;
5165         } else {
5166             ssl->client = NULL;
5167         }
5168         return;
5169     }
5170
5171     /* TLS 1.3 secret found, set new keys. */
5172     ssl->traffic_secret.data = (guchar *) wmem_realloc(wmem_file_scope(),
5173             ssl->traffic_secret.data, secret->data_len);
5174     ssl_data_set(&ssl->traffic_secret, secret->data, secret->data_len);
5175     ssl_debug_printf("%s Retrieved TLS 1.3 traffic secret.\n", G_STRFUNC);
5176     ssl_print_string("Client Random", &ssl->client_random);
5177     ssl_print_string(label, secret);
5178     tls13_generate_keys(ssl, secret, is_from_server);
5179 }
5180 #endif /* HAVE_LIBGCRYPT */
5181
5182 /** SSL keylog file handling. {{{ */
5183
5184 static GRegex *
5185 ssl_compile_keyfile_regex(void)
5186 {
5187 #define OCTET "(?:[[:xdigit:]]{2})"
5188     const gchar *pattern =
5189         "(?:"
5190         /* Matches Client Hellos having this Client Random */
5191         "PMS_CLIENT_RANDOM (?<client_random_pms>" OCTET "{32}) "
5192         /* Matches first part of encrypted RSA pre-master secret */
5193         "|RSA (?<encrypted_pmk>" OCTET "{8}) "
5194         /* Pre-Master-Secret is given, it is 48 bytes for RSA,
5195            but it can be of any length for DHE */
5196         ")(?<pms>" OCTET "+)"
5197         "|(?:"
5198         /* Matches Server Hellos having a Session ID */
5199         "RSA Session-ID:(?<session_id>" OCTET "+) Master-Key:"
5200         /* Matches Client Hellos having this Client Random */
5201         "|CLIENT_RANDOM (?<client_random>" OCTET "{32}) "
5202         /* Master-Secret is given, its length is fixed */
5203         ")(?<master_secret>" OCTET "{" G_STRINGIFY(SSL_MASTER_SECRET_LENGTH) "})"
5204         "|(?"
5205         /* TLS 1.3 Client Random to Derived Secrets mapping. */
5206         ":CLIENT_EARLY_TRAFFIC_SECRET (?<client_early>" OCTET "{32})"
5207         "|CLIENT_HANDSHAKE_TRAFFIC_SECRET (?<client_handshake>" OCTET "{32})"
5208         "|SERVER_HANDSHAKE_TRAFFIC_SECRET (?<server_handshake>" OCTET "{32})"
5209         "|CLIENT_TRAFFIC_SECRET_0 (?<client_appdata>" OCTET "{32})"
5210         "|SERVER_TRAFFIC_SECRET_0 (?<server_appdata>" OCTET "{32})"
5211         ") (?<derived_secret>" OCTET "+)";
5212 #undef OCTET
5213     static GRegex *regex = NULL;
5214     GError *gerr = NULL;
5215
5216     if (!regex) {
5217         regex = g_regex_new(pattern,
5218                 (GRegexCompileFlags)(G_REGEX_OPTIMIZE | G_REGEX_ANCHORED),
5219                 G_REGEX_MATCH_ANCHORED, &gerr);
5220         if (gerr) {
5221             ssl_debug_printf("%s failed to compile regex: %s\n", G_STRFUNC,
5222                              gerr->message);
5223             g_error_free(gerr);
5224             regex = NULL;
5225         }
5226     }
5227
5228     return regex;
5229 }
5230
5231 static gboolean
5232 file_needs_reopen(FILE *fp, const char *filename)
5233 {
5234     ws_statb64 open_stat, current_stat;
5235
5236     /* consider a file deleted when stat fails for either file,
5237      * or when the residing device / inode has changed. */
5238     if (0 != ws_fstat64(ws_fileno(fp), &open_stat))
5239         return TRUE;
5240     if (0 != ws_stat64(filename, &current_stat))
5241         return TRUE;
5242
5243     /* Note: on Windows, ino may be 0. Existing files cannot be deleted on
5244      * Windows, but hopefully the size is a good indicator when a file got
5245      * removed and recreated */
5246     return  open_stat.st_dev != current_stat.st_dev ||
5247             open_stat.st_ino != current_stat.st_ino ||
5248             open_stat.st_size > current_stat.st_size;
5249 }
5250
5251 typedef struct ssl_master_key_match_group {
5252     const char *re_group_name;
5253     GHashTable *master_key_ht;
5254 } ssl_master_key_match_group_t;
5255
5256 void
5257 ssl_load_keyfile(const gchar *ssl_keylog_filename, FILE **keylog_file,
5258                  const ssl_master_key_map_t *mk_map)
5259 {
5260     unsigned i;
5261     GRegex *regex;
5262     ssl_master_key_match_group_t mk_groups[] = {
5263         { "encrypted_pmk",  mk_map->pre_master },
5264         { "session_id",     mk_map->session },
5265         { "client_random",  mk_map->crandom },
5266         { "client_random_pms",  mk_map->pms },
5267         /* TLS 1.3 map from Client Random to derived secret. */
5268         { "client_early",       mk_map->tls13_client_early },
5269         { "client_handshake",   mk_map->tls13_client_handshake },
5270         { "server_handshake",   mk_map->tls13_server_handshake },
5271         { "client_appdata",     mk_map->tls13_client_appdata },
5272         { "server_appdata",     mk_map->tls13_server_appdata },
5273     };
5274     /* no need to try if no key log file is configured. */
5275     if (!ssl_keylog_filename || !*ssl_keylog_filename) {
5276         ssl_debug_printf("%s dtls/ssl.keylog_file is not configured!\n",
5277                          G_STRFUNC);
5278         return;
5279     }
5280
5281     /* The format of the file is a series of records with one of the following formats:
5282      *   - "RSA xxxx yyyy"
5283      *     Where xxxx are the first 8 bytes of the encrypted pre-master secret (hex-encoded)
5284      *     Where yyyy is the cleartext pre-master secret (hex-encoded)
5285      *     (this is the original format introduced with bug 4349)
5286      *
5287      *   - "RSA Session-ID:xxxx Master-Key:yyyy"
5288      *     Where xxxx is the SSL session ID (hex-encoded)
5289      *     Where yyyy is the cleartext master secret (hex-encoded)
5290      *     (added to support openssl s_client Master-Key output)
5291      *     This is somewhat is a misnomer because there's nothing RSA specific
5292      *     about this.
5293      *
5294      *   - "PMS_CLIENT_RANDOM xxxx yyyy"
5295      *     Where xxxx is the client_random from the ClientHello (hex-encoded)
5296      *     Where yyyy is the cleartext pre-master secret (hex-encoded)
5297      *     (This format allows SSL connections to be decrypted, if a user can
5298      *     capture the PMS but could not recover the MS for a specific session
5299      *     with a SSL Server.)
5300      *
5301      *   - "CLIENT_RANDOM xxxx yyyy"
5302      *     Where xxxx is the client_random from the ClientHello (hex-encoded)
5303      *     Where yyyy is the cleartext master secret (hex-encoded)
5304      *     (This format allows non-RSA SSL connections to be decrypted, i.e.
5305      *     ECDHE-RSA.)
5306      *
5307      *   - "CLIENT_EARLY_TRAFFIC_SECRET xxxx yyyy"
5308      *   - "CLIENT_HANDSHAKE_TRAFFIC_SECRET xxxx yyyy"
5309      *   - "SERVER_HANDSHAKE_TRAFFIC_SECRET xxxx yyyy"
5310      *   - "CLIENT_TRAFFIC_SECRET_0 xxxx yyyy"
5311      *   - "SERVER_TRAFFIC_SECRET_0 xxxx yyyy"
5312      *     Where xxxx is the client_random from the ClientHello (hex-encoded)
5313      *     Where yyyy is the secret (hex-encoded) derived from the early,
5314      *     handshake or master secrets. (This format is introduced with TLS 1.3
5315      *     and supported by BoringSSL, OpenSSL, etc. See bug 12779.)
5316      */
5317     regex = ssl_compile_keyfile_regex();
5318     if (!regex)
5319         return;
5320
5321     ssl_debug_printf("trying to use SSL keylog in %s\n", ssl_keylog_filename);
5322
5323     /* if the keylog file was deleted, re-open it */
5324     if (*keylog_file && file_needs_reopen(*keylog_file, ssl_keylog_filename)) {
5325         ssl_debug_printf("%s file got deleted, trying to re-open\n", G_STRFUNC);
5326         fclose(*keylog_file);
5327         *keylog_file = NULL;
5328     }
5329
5330     if (*keylog_file == NULL) {
5331         *keylog_file = ws_fopen(ssl_keylog_filename, "r");
5332         if (!*keylog_file) {
5333             ssl_debug_printf("%s failed to open SSL keylog\n", G_STRFUNC);
5334             return;
5335         }
5336     }
5337
5338     for (;;) {
5339         char buf[512], *line;
5340         gsize bytes_read;
5341         GMatchInfo *mi;
5342
5343         line = fgets(buf, sizeof(buf), *keylog_file);
5344         if (!line)
5345             break;
5346
5347         bytes_read = strlen(line);
5348         /* fgets includes the \n at the end of the line. */
5349         if (bytes_read > 0 && line[bytes_read - 1] == '\n') {
5350             line[bytes_read - 1] = 0;
5351             bytes_read--;
5352         }
5353         if (bytes_read > 0 && line[bytes_read - 1] == '\r') {
5354             line[bytes_read - 1] = 0;
5355             bytes_read--;
5356         }
5357
5358         ssl_debug_printf("  checking keylog line: %s\n", line);
5359         if (g_regex_match(regex, line, G_REGEX_MATCH_ANCHORED, &mi)) {
5360             gchar *hex_key, *hex_pre_ms_or_ms;
5361             StringInfo *key = wmem_new(wmem_file_scope(), StringInfo);
5362             StringInfo *pre_ms_or_ms = NULL;
5363             GHashTable *ht = NULL;
5364
5365             /* Is the PMS being supplied with the PMS_CLIENT_RANDOM
5366              * otherwise we will use the Master Secret
5367              */
5368             hex_pre_ms_or_ms = g_match_info_fetch_named(mi, "master_secret");
5369             if (hex_pre_ms_or_ms == NULL || !*hex_pre_ms_or_ms) {
5370                 g_free(hex_pre_ms_or_ms);
5371                 hex_pre_ms_or_ms = g_match_info_fetch_named(mi, "pms");
5372             }
5373             if (hex_pre_ms_or_ms == NULL || !*hex_pre_ms_or_ms) {
5374                 g_free(hex_pre_ms_or_ms);
5375                 hex_pre_ms_or_ms = g_match_info_fetch_named(mi, "derived_secret");
5376             }
5377             /* There is always a match, otherwise the regex is wrong. */
5378             DISSECTOR_ASSERT(hex_pre_ms_or_ms && strlen(hex_pre_ms_or_ms));
5379
5380             /* convert from hex to bytes and save to hashtable */
5381             pre_ms_or_ms = wmem_new(wmem_file_scope(), StringInfo);
5382             from_hex(pre_ms_or_ms, hex_pre_ms_or_ms, strlen(hex_pre_ms_or_ms));
5383             g_free(hex_pre_ms_or_ms);
5384
5385             /* Find a master key from any format (CLIENT_RANDOM, SID, ...) */
5386             for (i = 0; i < G_N_ELEMENTS(mk_groups); i++) {
5387                 ssl_master_key_match_group_t *g = &mk_groups[i];
5388                 hex_key = g_match_info_fetch_named(mi, g->re_group_name);
5389                 if (hex_key && *hex_key) {
5390                     ssl_debug_printf("    matched %s\n", g->re_group_name);
5391                     ht = g->master_key_ht;
5392                     from_hex(key, hex_key, strlen(hex_key));
5393                     g_free(hex_key);
5394                     break;
5395                 }
5396                 g_free(hex_key);
5397             }
5398             DISSECTOR_ASSERT(ht); /* Cannot be reached, or regex is wrong. */
5399
5400             g_hash_table_insert(ht, key, pre_ms_or_ms);
5401
5402         } else {
5403             ssl_debug_printf("    unrecognized line\n");
5404         }
5405         /* always free match info even if there is no match. */
5406         g_match_info_free(mi);
5407     }
5408 }
5409 /** SSL keylog file handling. }}} */
5410
5411 #ifdef SSL_DECRYPT_DEBUG /* {{{ */
5412
5413 static FILE* ssl_debug_file=NULL;
5414
5415 void
5416 ssl_set_debug(const gchar* name)
5417 {
5418     static gint debug_file_must_be_closed;
5419     gint        use_stderr;
5420
5421     use_stderr                = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
5422
5423     if (debug_file_must_be_closed)
5424         fclose(ssl_debug_file);
5425
5426     if (use_stderr)
5427         ssl_debug_file = stderr;
5428     else if (!name || (strcmp(name, "") ==0))
5429         ssl_debug_file = NULL;
5430     else
5431         ssl_debug_file = ws_fopen(name, "w");
5432
5433     if (!use_stderr && ssl_debug_file)
5434         debug_file_must_be_closed = 1;
5435     else
5436         debug_file_must_be_closed = 0;
5437
5438     ssl_debug_printf("Wireshark SSL debug log \n\n");
5439     ssl_debug_printf("Wireshark version: %s\n", get_ws_vcs_version_info());
5440 #ifdef HAVE_LIBGNUTLS
5441     ssl_debug_printf("GnuTLS version:    %s\n", gnutls_check_version(NULL));
5442 #endif
5443 #ifdef HAVE_LIBGCRYPT
5444     ssl_debug_printf("Libgcrypt version: %s\n", gcry_check_version(NULL));
5445 #endif
5446     ssl_debug_printf("\n");
5447 }
5448
5449 void
5450 ssl_debug_flush(void)
5451 {
5452     if (ssl_debug_file)
5453         fflush(ssl_debug_file);
5454 }
5455
5456 void
5457 ssl_debug_printf(const gchar* fmt, ...)
5458 {
5459     va_list ap;
5460
5461     if (!ssl_debug_file)
5462         return;
5463
5464     va_start(ap, fmt);
5465     vfprintf(ssl_debug_file, fmt, ap);
5466     va_end(ap);
5467 }
5468
5469 void
5470 ssl_print_data(const gchar* name, const guchar* data, size_t len)
5471 {
5472     size_t i, j, k;
5473     if (!ssl_debug_file)
5474         return;
5475     fprintf(ssl_debug_file,"%s[%d]:\n",name, (int) len);
5476     for (i=0; i<len; i+=16) {
5477         fprintf(ssl_debug_file,"| ");
5478         for (j=i, k=0; k<16 && j<len; ++j, ++k)
5479             fprintf(ssl_debug_file,"%.2x ",data[j]);
5480         for (; k<16; ++k)
5481             fprintf(ssl_debug_file,"   ");
5482         fputc('|', ssl_debug_file);
5483         for (j=i, k=0; k<16 && j<len; ++j, ++k) {
5484             guchar c = data[j];
5485             if (!g_ascii_isprint(c) || (c=='\t')) c = '.';
5486             fputc(c, ssl_debug_file);
5487         }
5488         for (; k<16; ++k)
5489             fputc(' ', ssl_debug_file);
5490         fprintf(ssl_debug_file,"|\n");
5491     }
5492 }
5493
5494 void
5495 ssl_print_string(const gchar* name, const StringInfo* data)
5496 {
5497     ssl_print_data(name, data->data, data->data_len);
5498 }
5499 #endif /* SSL_DECRYPT_DEBUG }}} */
5500
5501 /* UAT preferences callbacks. {{{ */
5502 /* checks for SSL and DTLS UAT key list fields */
5503
5504 gboolean
5505 ssldecrypt_uat_fld_ip_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, char** err)
5506 {
5507     if (!p || strlen(p) == 0u) {
5508         *err = g_strdup("No IP address given.");
5509         return FALSE;
5510     }
5511
5512     *err = NULL;
5513     return TRUE;
5514 }
5515
5516 gboolean
5517 ssldecrypt_uat_fld_port_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, char** err)
5518 {
5519     if (!p || strlen(p) == 0u) {
5520         *err = g_strdup("No Port given.");
5521         return FALSE;
5522     }
5523
5524     if (strcmp(p, "start_tls") != 0){
5525         guint16 port;
5526         if (!ws_strtou16(p, NULL, &port)) {
5527             *err = g_strdup("Invalid port given.");
5528             return FALSE;
5529         }
5530     }
5531
5532     *err = NULL;
5533     return TRUE;
5534 }
5535
5536 gboolean
5537 ssldecrypt_uat_fld_fileopen_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, char** err)
5538 {
5539     ws_statb64 st;
5540
5541     if (!p || strlen(p) == 0u) {
5542         *err = g_strdup("No filename given.");
5543         return FALSE;
5544     } else {
5545         if (ws_stat64(p, &st) != 0) {
5546             *err = g_strdup_printf("File '%s' does not exist or access is denied.", p);
5547             return FALSE;
5548         }
5549     }
5550
5551     *err = NULL;
5552     return TRUE;
5553 }
5554
5555 gboolean
5556 ssldecrypt_uat_fld_password_chk_cb(void *r _U_, const char *p _U_, guint len _U_, const void *u1 _U_, const void *u2 _U_, char **err)
5557 {
5558 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
5559     ssldecrypt_assoc_t*  f  = (ssldecrypt_assoc_t *)r;
5560     FILE                *fp = NULL;
5561
5562     if (p && (strlen(p) > 0u)) {
5563         fp = ws_fopen(f->keyfile, "rb");
5564         if (fp) {
5565             char *msg = NULL;
5566             gnutls_x509_privkey_t priv_key = ssl_load_pkcs12(fp, p, &msg);
5567             if (!priv_key) {
5568                 fclose(fp);
5569                 *err = g_strdup_printf("Could not load PKCS#12 key file: %s", msg);
5570                 g_free(msg);
5571                 return FALSE;
5572             }
5573             g_free(msg);
5574             gnutls_x509_privkey_deinit(priv_key);
5575             fclose(fp);
5576         } else {
5577             *err = g_strdup_printf("Leave this field blank if the keyfile is not PKCS#12.");
5578             return FALSE;
5579         }
5580     }
5581
5582     *err = NULL;
5583     return TRUE;
5584 #else
5585     *err = g_strdup("Cannot load key files, support is not compiled in.");
5586     return FALSE;
5587 #endif
5588 }
5589 /* UAT preferences callbacks. }}} */
5590
5591 /** maximum size of ssl_association_info() string */
5592 #define SSL_ASSOC_MAX_LEN 8192
5593
5594 typedef struct ssl_association_info_callback_data
5595 {
5596     gchar *str;
5597     const char *table_protocol;
5598 } ssl_association_info_callback_data_t;
5599
5600 /**
5601  * callback function used by ssl_association_info() to traverse the SSL associations.
5602  */
5603 static void
5604 ssl_association_info_(const gchar *table _U_, gpointer handle, gpointer user_data)
5605 {
5606     ssl_association_info_callback_data_t* data = (ssl_association_info_callback_data_t*)user_data;
5607     const int l = (const int)strlen(data->str);
5608     g_snprintf(data->str+l, SSL_ASSOC_MAX_LEN-l, "'%s' %s\n", dissector_handle_get_short_name((dissector_handle_t)handle), data->table_protocol);
5609 }
5610
5611 /**
5612  * @return an information string on the SSL protocol associations. The string has ephemeral lifetime/scope.
5613  */
5614 gchar*
5615 ssl_association_info(const char* dissector_table_name, const char* table_protocol)
5616 {
5617     ssl_association_info_callback_data_t data;
5618
5619     data.str = (gchar *)g_malloc0(SSL_ASSOC_MAX_LEN);
5620     data.table_protocol = table_protocol;
5621     dissector_table_foreach_handle(dissector_table_name, ssl_association_info_, &data);
5622     return data.str;
5623 }
5624
5625
5626 /** Begin of code related to dissection of wire data. */
5627
5628 /* Helpers for dissecting Variable-Length Vectors. {{{ */
5629 gboolean
5630 ssl_add_vector(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
5631                guint offset, guint offset_end, guint32 *ret_length,
5632                int hf_length, guint32 min_value, guint32 max_value)
5633 {
5634     guint       veclen_size;
5635     guint32     veclen_value;
5636     proto_item *pi;
5637
5638     DISSECTOR_ASSERT(offset <= offset_end);
5639     DISSECTOR_ASSERT(min_value <= max_value);
5640
5641     if (max_value > 0xffffff) {
5642         veclen_size = 4;
5643     } else if (max_value > 0xffff) {
5644         veclen_size = 3;
5645     } else if (max_value > 0xff) {
5646         veclen_size = 2;
5647     } else {
5648         veclen_size = 1;
5649     }
5650
5651     if (offset_end - offset < veclen_size) {
5652         proto_tree_add_expert_format(tree, pinfo, &hf->ei.malformed_buffer_too_small,
5653                                      tvb, offset, offset_end - offset,
5654                                      "No more room for vector of length %u",
5655                                      veclen_size);
5656         *ret_length = 0;
5657         return FALSE;   /* Cannot read length. */
5658     }
5659
5660     pi = proto_tree_add_item_ret_uint(tree, hf_length, tvb, offset, veclen_size, ENC_BIG_ENDIAN, &veclen_value);
5661     offset += veclen_size;
5662
5663     if (veclen_value < min_value) {
5664         expert_add_info_format(pinfo, pi, &hf->ei.malformed_vector_length,
5665                                "Vector length %u is smaller than minimum %u",
5666                                veclen_value, min_value);
5667     } else if (veclen_value > max_value) {
5668         expert_add_info_format(pinfo, pi, &hf->ei.malformed_vector_length,
5669                                "Vector length %u is larger than maximum %u",
5670                                veclen_value, max_value);
5671     }
5672
5673     if (offset_end - offset < veclen_value) {
5674         expert_add_info_format(pinfo, pi, &hf->ei.malformed_buffer_too_small,
5675                                "Vector length %u is too large, truncating it to %u",
5676                                veclen_value, offset_end - offset);
5677         *ret_length = offset_end - offset;
5678         return FALSE;   /* Length is truncated to avoid overflow. */
5679     }
5680
5681     *ret_length = veclen_value;
5682     return TRUE;        /* Length is OK. */
5683 }
5684
5685 gboolean
5686 ssl_end_vector(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
5687                guint offset, guint offset_end)
5688 {
5689     if (offset < offset_end) {
5690         guint trailing = offset_end - offset;
5691         proto_tree_add_expert_format(tree, pinfo, &hf->ei.malformed_trailing_data,
5692                                      tvb, offset, trailing,
5693                                      "%u trailing byte%s unprocessed",
5694                                      trailing, plurality(trailing, " was", "s were"));
5695         return FALSE;   /* unprocessed data warning */
5696     } else if (offset > offset_end) {
5697         /*
5698          * Returned offset runs past the end. This should not happen and is
5699          * possibly a dissector bug.
5700          */
5701         guint excess = offset - offset_end;
5702         proto_tree_add_expert_format(tree, pinfo, &hf->ei.malformed_buffer_too_small,
5703                                      tvb, offset_end, excess,
5704                                      "Dissector processed too much data (%u byte%s)",
5705                                      excess, plurality(excess, "", "s"));
5706         return FALSE;   /* overflow error */
5707     }
5708
5709     return TRUE;    /* OK, offset matches. */
5710 }
5711 /** }}} */
5712
5713
5714 /* change_cipher_spec(20) dissection */
5715 void
5716 ssl_dissect_change_cipher_spec(ssl_common_dissect_t *hf, tvbuff_t *tvb,
5717                                packet_info *pinfo, proto_tree *tree,
5718                                guint32 offset, SslSession *session,
5719                                gboolean is_from_server,
5720                                const SslDecryptSession *ssl)
5721 {
5722     /*
5723      * struct {
5724      *     enum { change_cipher_spec(1), (255) } type;
5725      * } ChangeCipherSpec;
5726      */
5727     proto_item *ti;
5728     proto_item_set_text(tree,
5729             "%s Record Layer: %s Protocol: Change Cipher Spec",
5730             val_to_str_const(session->version, ssl_version_short_names, "SSL"),
5731             val_to_str_const(SSL_ID_CHG_CIPHER_SPEC, ssl_31_content_type, "unknown"));
5732     ti = proto_tree_add_item(tree, hf->hf.change_cipher_spec, tvb, offset, 1, ENC_NA);
5733
5734     /* Use heuristics to detect an abbreviated handshake, assume that missing
5735      * ServerHelloDone implies reusing previously negotiating keys. Then when
5736      * a Session ID or ticket is present, it must be a resumed session.
5737      * Normally this should be done at the Finished message, but that may be
5738      * encrypted so we do it here, at the last cleartext message. */
5739     if (is_from_server && ssl) {
5740         if (session->is_session_resumed) {
5741             const char *resumed = NULL;
5742             if (ssl->session_ticket.data_len) {
5743                 resumed = "Session Ticket";
5744             } else if (ssl->session_id.data_len) {
5745                 resumed = "Session ID";
5746             }
5747             if (resumed) {
5748                 ssl_debug_printf("%s Session resumption using %s\n", G_STRFUNC, resumed);
5749             } else {
5750                 /* Can happen if the capture somehow starts in the middle */
5751                 ssl_debug_printf("%s No Session resumption, missing packets?\n", G_STRFUNC);
5752             }
5753         } else {
5754             ssl_debug_printf("%s Not using Session resumption\n", G_STRFUNC);
5755         }
5756     }
5757     if (is_from_server && session->is_session_resumed)
5758         expert_add_info(pinfo, ti, &hf->ei.resumed);
5759 }
5760
5761 /** Begin of handshake(22) record dissections */
5762 /* dissect a list of hash algorithms, return the number of bytes dissected
5763    this is used for the signature algorithms extension and for the
5764    TLS1.2 certificate request. {{{ */
5765 static gint
5766 ssl_dissect_hash_alg_list(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
5767                           packet_info* pinfo, guint32 offset, guint32 offset_end)
5768 {
5769     /* https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
5770      *  struct {
5771      *       HashAlgorithm hash;
5772      *       SignatureAlgorithm signature;
5773      *  } SignatureAndHashAlgorithm;
5774      *  SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
5775      */
5776     proto_tree *subtree, *alg_tree;
5777     proto_item *ti;
5778     guint sh_alg_length;
5779     guint32     next_offset;
5780
5781     /* SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2> */
5782     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &sh_alg_length,
5783                         hf->hf.hs_sig_hash_alg_len, 2, G_MAXUINT16 - 1)) {
5784         return offset_end;
5785     }
5786     offset += 2;
5787     next_offset = offset + sh_alg_length;
5788
5789     ti = proto_tree_add_none_format(tree, hf->hf.hs_sig_hash_algs, tvb, offset, sh_alg_length,
5790                                     "Signature Hash Algorithms (%u algorithm%s)",
5791                                     sh_alg_length / 2, plurality(sh_alg_length / 2, "", "s"));
5792     subtree = proto_item_add_subtree(ti, hf->ett.hs_sig_hash_algs);
5793
5794     while (offset + 2 <= next_offset) {
5795         ti = proto_tree_add_item(subtree, hf->hf.hs_sig_hash_alg,
5796                                  tvb, offset, 2, ENC_BIG_ENDIAN);
5797         alg_tree = proto_item_add_subtree(ti, hf->ett.hs_sig_hash_alg);
5798
5799         proto_tree_add_item(alg_tree, hf->hf.hs_sig_hash_hash,
5800                             tvb, offset, 1, ENC_BIG_ENDIAN);
5801         proto_tree_add_item(alg_tree, hf->hf.hs_sig_hash_sig,
5802                             tvb, offset+1, 1, ENC_BIG_ENDIAN);
5803
5804         offset += 2;
5805     }
5806
5807     if (!ssl_end_vector(hf, tvb, pinfo, subtree, offset, next_offset)) {
5808         offset = next_offset;
5809     }
5810
5811     return offset;
5812 } /* }}} */
5813
5814 /** TLS Extensions (in Client Hello and Server Hello). {{{ */
5815 static gint
5816 ssl_dissect_hnd_hello_ext_sig_hash_algs(ssl_common_dissect_t *hf, tvbuff_t *tvb,
5817                                         proto_tree *tree, packet_info* pinfo, guint32 offset, guint32 offset_end)
5818 {
5819     return ssl_dissect_hash_alg_list(hf, tvb, tree, pinfo, offset, offset_end);
5820 }
5821
5822 static gint
5823 ssl_dissect_hnd_hello_ext_alpn(ssl_common_dissect_t *hf, tvbuff_t *tvb,
5824                                packet_info *pinfo, proto_tree *tree,
5825                                guint32 offset, guint32 offset_end,
5826                                guint8 hnd_type, SslSession *session)
5827 {
5828
5829     /* https://tools.ietf.org/html/rfc7301#section-3.1
5830      *  opaque ProtocolName<1..2^8-1>;
5831      *  struct {
5832      *      ProtocolName protocol_name_list<2..2^16-1>
5833      *  } ProtocolNameList;
5834      */
5835     proto_tree *alpn_tree;
5836     proto_item *ti;
5837     guint32     next_offset, alpn_length, name_length;
5838     guint8     *proto_name = NULL;
5839     guint32     proto_name_length = 0;
5840
5841     /* ProtocolName protocol_name_list<2..2^16-1> */
5842     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &alpn_length,
5843                         hf->hf.hs_ext_alpn_len, 2, G_MAXUINT16)) {
5844         return offset_end;
5845     }
5846     offset += 2;
5847     next_offset = offset + alpn_length;
5848
5849     ti = proto_tree_add_item(tree, hf->hf.hs_ext_alpn_list,
5850                              tvb, offset, alpn_length, ENC_NA);
5851     alpn_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_alpn);
5852
5853     /* Parse list (note missing check for end of vector, ssl_add_vector below
5854      * ensures that data is always available.) */
5855     while (offset < next_offset) {
5856         /* opaque ProtocolName<1..2^8-1> */
5857         if (!ssl_add_vector(hf, tvb, pinfo, alpn_tree, offset, next_offset, &name_length,
5858                             hf->hf.hs_ext_alpn_str_len, 1, G_MAXUINT8)) {
5859             return next_offset;
5860         }
5861         offset++;
5862
5863         proto_tree_add_item(alpn_tree, hf->hf.hs_ext_alpn_str,
5864                             tvb, offset, name_length, ENC_ASCII|ENC_NA);
5865         /* Remember first ALPN ProtocolName entry for server. */
5866         if (hnd_type == SSL_HND_SERVER_HELLO) {
5867             proto_name_length = name_length;
5868             proto_name = tvb_get_string_enc(wmem_packet_scope(), tvb, offset,
5869                                             proto_name_length, ENC_ASCII);
5870         }
5871         offset += name_length;
5872     }
5873
5874     /* If ALPN is given in ServerHello, then ProtocolNameList MUST contain
5875      * exactly one "ProtocolName". */
5876     if (hnd_type == SSL_HND_SERVER_HELLO && proto_name) {
5877         /* '\0'-terminated string for prefix/full string comparison purposes. */
5878         for (size_t i = 0; i < G_N_ELEMENTS(ssl_alpn_protocols); i++) {
5879             const ssl_alpn_protocol_t *alpn_proto = &ssl_alpn_protocols[i];
5880
5881             if ((alpn_proto->match_exact &&
5882                         proto_name_length == strlen(alpn_proto->proto_name) &&
5883                         !strcmp(proto_name, alpn_proto->proto_name)) ||
5884                 (!alpn_proto->match_exact && g_str_has_prefix(proto_name, alpn_proto->proto_name))) {
5885
5886                 dissector_handle_t handle;
5887                 /* ProtocolName match, so set the App data dissector handle.
5888                  * This may override protocols given via the UAT dialog, but
5889                  * since the ALPN hint is precise, do it anyway. */
5890                 handle = ssl_find_appdata_dissector(alpn_proto->dissector_name);
5891                 ssl_debug_printf("%s: changing handle %p to %p (%s)", G_STRFUNC,
5892                                  (void *)session->app_handle,
5893                                  (void *)handle, alpn_proto->dissector_name);
5894                 /* if dissector is disabled, do not overwrite previous one */
5895                 if (handle)
5896                     session->app_handle = handle;
5897                 break;
5898             }
5899         }
5900     }
5901
5902     return offset;
5903 }
5904
5905 static gint
5906 ssl_dissect_hnd_hello_ext_npn(ssl_common_dissect_t *hf, tvbuff_t *tvb,
5907                               packet_info *pinfo, proto_tree *tree,
5908                               guint32 offset, guint32 offset_end)
5909 {
5910     /* https://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-3
5911      *   The "extension_data" field of a "next_protocol_negotiation" extension
5912      *   in a "ServerHello" contains an optional list of protocols advertised
5913      *   by the server.  Protocols are named by opaque, non-empty byte strings
5914      *   and the list of protocols is serialized as a concatenation of 8-bit,
5915      *   length prefixed byte strings.  Implementations MUST ensure that the
5916      *   empty string is not included and that no byte strings are truncated.
5917      */
5918     guint32     npn_length;
5919     proto_tree *npn_tree;
5920
5921     /* List is optional, do not add tree if there are no entries. */
5922     if (offset == offset_end) {
5923         return offset;
5924     }
5925
5926     npn_tree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset, hf->ett.hs_ext_npn, NULL, "Next Protocol Negotiation");
5927
5928     while (offset < offset_end) {
5929         /* non-empty, 8-bit length prefixed strings means range 1..255 */
5930         if (!ssl_add_vector(hf, tvb, pinfo, npn_tree, offset, offset_end, &npn_length,
5931                             hf->hf.hs_ext_npn_str_len, 1, G_MAXUINT8)) {
5932             return offset_end;
5933         }
5934         offset++;
5935
5936         proto_tree_add_item(npn_tree, hf->hf.hs_ext_npn_str,
5937                             tvb, offset, npn_length, ENC_ASCII|ENC_NA);
5938         offset += npn_length;
5939     }
5940
5941     return offset;
5942 }
5943
5944 static gint
5945 ssl_dissect_hnd_hello_ext_reneg_info(ssl_common_dissect_t *hf, tvbuff_t *tvb,
5946                                      packet_info *pinfo, proto_tree *tree,
5947                                      guint32 offset, guint32 offset_end)
5948 {
5949     /* https://tools.ietf.org/html/rfc5746#section-3.2
5950      *  struct {
5951      *      opaque renegotiated_connection<0..255>;
5952      *  } RenegotiationInfo;
5953      *
5954      */
5955     proto_tree *reneg_info_tree;
5956     guint32     reneg_info_length;
5957
5958     reneg_info_tree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset, hf->ett.hs_ext_reneg_info, NULL, "Renegotiation Info extension");
5959
5960     /* opaque renegotiated_connection<0..255> */
5961     if (!ssl_add_vector(hf, tvb, pinfo, reneg_info_tree, offset, offset_end, &reneg_info_length,
5962                         hf->hf.hs_ext_reneg_info_len, 0, 255)) {
5963         return offset_end;
5964     }
5965     offset++;
5966
5967     if (reneg_info_length > 0) {
5968         proto_tree_add_item(reneg_info_tree, hf->hf.hs_ext_reneg_info, tvb, offset, reneg_info_length, ENC_NA);
5969         offset += reneg_info_length;
5970     }
5971
5972     return offset;
5973 }
5974
5975 static gint
5976 ssl_dissect_hnd_hello_ext_key_share_entry(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
5977                                           proto_tree *tree, guint32 offset, guint32 offset_end)
5978 {
5979    /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.5
5980     *   struct {
5981     *       NamedGroup group;
5982     *       opaque key_exchange<1..2^16-1>;
5983     *   } KeyShareEntry;
5984     */
5985     guint32 key_exchange_length, group;
5986     proto_tree *ks_tree;
5987
5988     ks_tree = proto_tree_add_subtree(tree, tvb, offset, 4, hf->ett.hs_ext_key_share_ks, NULL, "Key Share Entry");
5989
5990     proto_tree_add_item_ret_uint(ks_tree, hf->hf.hs_ext_key_share_group, tvb, offset, 2, ENC_BIG_ENDIAN, &group);
5991     offset += 2;
5992     proto_item_append_text(ks_tree, ": Group: %s", val_to_str(group, ssl_extension_curves, "Unknown (%u)"));
5993
5994     /* opaque key_exchange<1..2^16-1> */
5995     if (!ssl_add_vector(hf, tvb, pinfo, ks_tree, offset, offset_end, &key_exchange_length,
5996                         hf->hf.hs_ext_key_share_key_exchange_length, 1, G_MAXUINT16)) {
5997         return offset_end;  /* Bad (possible truncated) length, skip to end of KeyShare extension. */
5998     }
5999     offset += 2;
6000     proto_item_set_len(ks_tree, 2 + 2 + key_exchange_length);
6001     proto_item_append_text(ks_tree, ", Key Exchange length: %u", key_exchange_length);
6002
6003     proto_tree_add_item(ks_tree, hf->hf.hs_ext_key_share_key_exchange, tvb, offset, key_exchange_length, ENC_NA);
6004     offset += key_exchange_length;
6005
6006     return offset;
6007 }
6008
6009 static gint
6010 ssl_dissect_hnd_hello_ext_key_share(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6011                                     proto_tree *tree, guint32 offset, guint32 offset_end,
6012                                     guint8 hnd_type)
6013 {
6014     proto_tree *key_share_tree;
6015     guint32 next_offset;
6016     guint32 client_shares_length;
6017
6018     if (offset_end <= offset) {  /* Check if ext_len == 0 and "overflow" (offset + ext_len) > guint32) */
6019         return offset;
6020     }
6021
6022     key_share_tree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset, hf->ett.hs_ext_key_share, NULL, "Key Share extension");
6023
6024     switch(hnd_type){
6025         case SSL_HND_CLIENT_HELLO:
6026             /* KeyShareEntry client_shares<0..2^16-1> */
6027             if (!ssl_add_vector(hf, tvb, pinfo, key_share_tree, offset, offset_end, &client_shares_length,
6028                                 hf->hf.hs_ext_key_share_client_length, 0, G_MAXUINT16)) {
6029                 return offset_end;
6030             }
6031             offset += 2;
6032             next_offset = offset + client_shares_length;
6033             while (offset + 4 <= next_offset) { /* (NamedGroup (2 bytes), key_exchange (1 byte for length, 1 byte minimum data) */
6034                 offset = ssl_dissect_hnd_hello_ext_key_share_entry(hf, tvb, pinfo, key_share_tree, offset, next_offset);
6035             }
6036             if (!ssl_end_vector(hf, tvb, pinfo, key_share_tree, offset, next_offset)) {
6037                 return next_offset;
6038             }
6039         break;
6040         case SSL_HND_SERVER_HELLO:
6041             offset = ssl_dissect_hnd_hello_ext_key_share_entry(hf, tvb, pinfo, key_share_tree, offset, offset_end);
6042         break;
6043         case SSL_HND_HELLO_RETRY_REQUEST:
6044             proto_tree_add_item(key_share_tree, hf->hf.hs_ext_key_share_selected_group, tvb, offset, 2, ENC_BIG_ENDIAN );
6045             offset += 2;
6046         break;
6047         default: /* no default */
6048         break;
6049     }
6050
6051     return offset;
6052 }
6053
6054 static gint
6055 ssl_dissect_hnd_hello_ext_pre_shared_key(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6056                                          proto_tree *tree, guint32 offset, guint32 offset_end,
6057                                          guint8 hnd_type)
6058 {
6059     /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
6060      *  struct {
6061      *      opaque identity<0..2^16-1>;
6062      *      uint32 obfuscated_ticket_age;
6063      *  } PskIdentity;
6064      *  opaque PskBinderEntry<32..255>;
6065      *  struct {
6066      *      select (Handshake.msg_type) {
6067      *          case client_hello:
6068      *              PskIdentity identities<6..2^16-1>;
6069      *              PskBinderEntry binders<33..2^16-1>;
6070      *          case server_hello:
6071      *              uint16 selected_identity;
6072      *      };
6073      *  } PreSharedKeyExtension;
6074      */
6075
6076     proto_tree *psk_tree;
6077
6078     psk_tree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset, hf->ett.hs_ext_pre_shared_key, NULL, "Pre-Shared Key extension");
6079
6080     switch (hnd_type){
6081         case SSL_HND_CLIENT_HELLO: {
6082             guint32 identities_length, identities_end, binders_length;
6083
6084             /* PskIdentity identities<6..2^16-1> */
6085             if (!ssl_add_vector(hf, tvb, pinfo, psk_tree, offset, offset_end, &identities_length,
6086                                 hf->hf.hs_ext_psk_identities_length, 6, G_MAXUINT16)) {
6087                 return offset_end;
6088             }
6089             offset += 2;
6090             identities_end = offset + identities_length;
6091
6092             while (offset < identities_end) {
6093                 guint32 identity_length;
6094                 proto_tree *identity_tree;
6095
6096                 identity_tree = proto_tree_add_subtree(psk_tree, tvb, offset, 4, hf->ett.hs_ext_psk_identity, NULL, "PSK Identity (");
6097
6098                 /* opaque identity<0..2^16-1> */
6099                 if (!ssl_add_vector(hf, tvb, pinfo, identity_tree, offset, identities_end, &identity_length,
6100                                     hf->hf.hs_ext_psk_identity_identity_length, 0, G_MAXUINT16)) {
6101                     return identities_end;
6102                 }
6103                 offset += 2;
6104                 proto_item_append_text(identity_tree, "length: %u)", identity_length);
6105
6106                 proto_tree_add_item(identity_tree, hf->hf.hs_ext_psk_identity_identity, tvb, offset, identity_length, ENC_BIG_ENDIAN);
6107                 offset += identity_length;
6108
6109                 proto_tree_add_item(identity_tree, hf->hf.hs_ext_psk_identity_obfuscated_ticket_age, tvb, offset, 4, ENC_BIG_ENDIAN);
6110                 offset += 4;
6111
6112                 proto_item_set_len(identity_tree, 2 + identity_length + 4);
6113             }
6114             if (!ssl_end_vector(hf, tvb, pinfo, psk_tree, offset, identities_end)) {
6115                 offset = identities_end;
6116             }
6117
6118             /* PskBinderEntry binders<33..2^16-1> */
6119             if (!ssl_add_vector(hf, tvb, pinfo, psk_tree, offset, offset_end, &binders_length,
6120                                 hf->hf.hs_ext_psk_binders_length, 33, G_MAXUINT16)) {
6121                 return offset_end;
6122             }
6123             offset += 2;
6124
6125             proto_tree_add_item(psk_tree, hf->hf.hs_ext_psk_binders, tvb, offset, binders_length, ENC_NA);
6126             offset += binders_length;
6127         }
6128         break;
6129         case SSL_HND_SERVER_HELLO: {
6130             proto_tree_add_item(psk_tree, hf->hf.hs_ext_psk_identity_selected, tvb, offset, 2, ENC_BIG_ENDIAN);
6131             offset += 2;
6132         }
6133         break;
6134         default:
6135         break;
6136     }
6137
6138     return offset;
6139 }
6140
6141 static gint
6142 ssl_dissect_hnd_hello_ext_supported_versions(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6143                                              proto_tree *tree, guint32 offset, guint32 offset_end)
6144 {
6145
6146    /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.1
6147     * struct {
6148     *     ProtocolVersion versions<2..254>;
6149     * } SupportedVersions;
6150     */
6151     guint32     versions_length, next_offset;
6152     /* ProtocolVersion versions<2..254> */
6153     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &versions_length,
6154                         hf->hf.hs_ext_supported_versions_len, 2, 254)) {
6155         return offset_end;
6156     }
6157     offset++;
6158     next_offset = offset + versions_length;
6159
6160     while (offset + 2 <= next_offset) {
6161         proto_tree_add_item(tree, hf->hf.hs_ext_supported_versions, tvb, offset, 2, ENC_BIG_ENDIAN);
6162         offset += 2;
6163     }
6164     if (!ssl_end_vector(hf, tvb, pinfo, tree, offset, next_offset)) {
6165         offset = next_offset;
6166     }
6167
6168     return offset;
6169 }
6170
6171 static gint
6172 ssl_dissect_hnd_hello_ext_cookie(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6173                                  packet_info *pinfo, proto_tree *tree,
6174                                  guint32 offset, guint32 offset_end)
6175 {
6176     /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.2
6177      *  struct {
6178      *      opaque cookie<1..2^16-1>;
6179      *  } Cookie;
6180      */
6181     guint32 cookie_length;
6182     /* opaque cookie<1..2^16-1> */
6183     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &cookie_length,
6184                         hf->hf.hs_ext_cookie_len, 1, G_MAXUINT16)) {
6185         return offset_end;
6186     }
6187     offset += 2;
6188
6189     proto_tree_add_item(tree, hf->hf.hs_ext_cookie, tvb, offset, cookie_length, ENC_NA);
6190     offset += cookie_length;
6191
6192     return offset;
6193 }
6194
6195 static gint
6196 ssl_dissect_hnd_hello_ext_psk_key_exchange_modes(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6197                                                  proto_tree *tree, guint32 offset, guint32 offset_end)
6198 {
6199     /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
6200      * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
6201      *
6202      * struct {
6203      *     PskKeyExchangeMode ke_modes<1..255>;
6204      * } PskKeyExchangeModes;
6205      */
6206     guint32 ke_modes_length, next_offset;
6207
6208     /* PskKeyExchangeMode ke_modes<1..255> */
6209     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &ke_modes_length,
6210                         hf->hf.hs_ext_psk_ke_modes_len, 1, 255)) {
6211         return offset_end;
6212     }
6213     offset++;
6214     next_offset = offset + ke_modes_length;
6215
6216     while (offset < next_offset) {
6217         proto_tree_add_item(tree, hf->hf.hs_ext_psk_ke_mode, tvb, offset, 1, ENC_NA);
6218         offset++;
6219     }
6220
6221     return offset;
6222 }
6223
6224 static gint
6225 ssl_dissect_hnd_hello_ext_server_name(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6226                                       packet_info *pinfo, proto_tree *tree,
6227                                       guint32 offset, guint32 offset_end)
6228 {
6229     /* https://tools.ietf.org/html/rfc6066#section-3
6230      *
6231      *  struct {
6232      *      NameType name_type;
6233      *      select (name_type) {
6234      *          case host_name: HostName;
6235      *      } name;
6236      *  } ServerName;
6237      *
6238      *  enum {
6239      *      host_name(0), (255)
6240      *  } NameType;
6241      *
6242      *  opaque HostName<1..2^16-1>;
6243      *
6244      *  struct {
6245      *      ServerName server_name_list<1..2^16-1>
6246      *  } ServerNameList;
6247      */
6248     proto_tree *server_name_tree;
6249     guint32     list_length, server_name_length, next_offset;
6250
6251     /* The server SHALL include "server_name" extension with empty data. */
6252     if (offset == offset_end) {
6253         return offset;
6254     }
6255
6256     server_name_tree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset, hf->ett.hs_ext_server_name, NULL, "Server Name Indication extension");
6257
6258     /* ServerName server_name_list<1..2^16-1> */
6259     if (!ssl_add_vector(hf, tvb, pinfo, server_name_tree, offset, offset_end, &list_length,
6260                         hf->hf.hs_ext_server_name_list_len, 1, G_MAXUINT16)) {
6261         return offset_end;
6262     }
6263     offset += 2;
6264     next_offset = offset + list_length;
6265
6266     while (offset < next_offset) {
6267         proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name_type,
6268                             tvb, offset, 1, ENC_NA);
6269         offset++;
6270
6271         /* opaque HostName<1..2^16-1> */
6272         if (!ssl_add_vector(hf, tvb, pinfo, server_name_tree, offset, next_offset, &server_name_length,
6273                            hf->hf.hs_ext_server_name_len, 1, G_MAXUINT16)) {
6274             return next_offset;
6275         }
6276         offset += 2;
6277
6278         proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name,
6279                             tvb, offset, server_name_length, ENC_ASCII|ENC_NA);
6280         offset += server_name_length;
6281     }
6282     return offset;
6283 }
6284
6285 static gint
6286 ssl_dissect_hnd_hello_ext_session_ticket(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6287                                       proto_tree *tree, guint32 offset, guint32 offset_end, guint8 hnd_type, SslDecryptSession *ssl)
6288 {
6289     guint       ext_len = offset_end - offset;
6290     if (hnd_type == SSL_HND_CLIENT_HELLO && ssl && ext_len != 0) {
6291         tvb_ensure_bytes_exist(tvb, offset, ext_len);
6292         /* Save the Session Ticket such that it can be used as identifier for
6293          * restoring a previous Master Secret (in ChangeCipherSpec) */
6294         ssl->session_ticket.data = (guchar*)wmem_realloc(wmem_file_scope(),
6295                                     ssl->session_ticket.data, ext_len);
6296         ssl->session_ticket.data_len = ext_len;
6297         tvb_memcpy(tvb,ssl->session_ticket.data, offset, ext_len);
6298     }
6299     proto_tree_add_bytes_format(tree, hf->hf.hs_ext_data,
6300                                 tvb, offset, ext_len, NULL,
6301                                 "Data (%u byte%s)",
6302                                 ext_len, plurality(ext_len, "", "s"));
6303     return offset + ext_len;
6304 }
6305
6306 static gint
6307 ssl_dissect_hnd_hello_ext_cert_type(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6308                                     proto_tree *tree, guint32 offset, guint32 offset_end,
6309                                     guint8 hnd_type, guint16 ext_type, SslSession *session)
6310 {
6311     guint8      cert_list_length;
6312     guint8      cert_type;
6313     proto_tree *cert_list_tree;
6314     proto_item *ti;
6315
6316     switch(hnd_type){
6317     case SSL_HND_CLIENT_HELLO:
6318         cert_list_length = tvb_get_guint8(tvb, offset);
6319         proto_tree_add_item(tree, hf->hf.hs_ext_cert_types_len,
6320                             tvb, offset, 1, ENC_BIG_ENDIAN);
6321         offset += 1;
6322         if (offset_end - offset != (guint32)cert_list_length)
6323             return offset;
6324
6325         ti = proto_tree_add_item(tree, hf->hf.hs_ext_cert_types, tvb, offset,
6326                                  cert_list_length, cert_list_length);
6327         proto_item_append_text(ti, " (%d)", cert_list_length);
6328
6329         /* make this a subtree */
6330         cert_list_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_cert_types);
6331
6332         /* loop over all point formats */
6333         while (cert_list_length > 0)
6334         {
6335             proto_tree_add_item(cert_list_tree, hf->hf.hs_ext_cert_type, tvb, offset, 1, ENC_BIG_ENDIAN);
6336             offset++;
6337             cert_list_length--;
6338         }
6339     break;
6340     case SSL_HND_SERVER_HELLO:
6341         cert_type = tvb_get_guint8(tvb, offset);
6342         proto_tree_add_item(tree, hf->hf.hs_ext_cert_type, tvb, offset, 1, ENC_BIG_ENDIAN);
6343         offset += 1;
6344         if (ext_type == SSL_HND_HELLO_EXT_CERT_TYPE || ext_type == SSL_HND_HELLO_EXT_CLIENT_CERT_TYPE) {
6345             session->client_cert_type = cert_type;
6346         }
6347         if (ext_type == SSL_HND_HELLO_EXT_CERT_TYPE || ext_type == SSL_HND_HELLO_EXT_SERVER_CERT_TYPE) {
6348            session->server_cert_type = cert_type;
6349         }
6350     break;
6351     default: /* no default */
6352     break;
6353     }
6354
6355     return offset;
6356 }
6357
6358 static gint
6359 ssl_dissect_hnd_hello_common(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6360                              proto_tree *tree, guint32 offset,
6361                              SslSession *session, SslDecryptSession *ssl,
6362                              gboolean from_server)
6363 {
6364     nstime_t     gmt_unix_time;
6365     guint8       sessid_length;
6366     proto_tree  *rnd_tree;
6367     proto_tree  *ti_rnd;
6368
6369     /* Prepare for renegotiation by resetting the state. */
6370     ssl_reset_session(session, ssl, !from_server);
6371
6372     if (ssl) {
6373         StringInfo *rnd;
6374         if (from_server)
6375             rnd = &ssl->server_random;
6376         else
6377             rnd = &ssl->client_random;
6378
6379         /* save provided random for later keyring generation */
6380         tvb_memcpy(tvb, rnd->data, offset, 32);
6381         rnd->data_len = 32;
6382         if (from_server)
6383             ssl->state |= SSL_SERVER_RANDOM;
6384         else
6385             ssl->state |= SSL_CLIENT_RANDOM;
6386         ssl_debug_printf("%s found %s RANDOM -> state 0x%02X\n", G_STRFUNC,
6387                 from_server ? "SERVER" : "CLIENT", ssl->state);
6388     }
6389
6390     ti_rnd = proto_tree_add_item(tree, hf->hf.hs_random, tvb, offset, 32, ENC_NA);
6391
6392     if (session->version != TLSV1DOT3_VERSION) { /* No time on first bytes random with TLS 1.3 */
6393
6394         rnd_tree = proto_item_add_subtree(ti_rnd, hf->ett.hs_random);
6395         /* show the time */
6396         gmt_unix_time.secs  = tvb_get_ntohl(tvb, offset);
6397         gmt_unix_time.nsecs = 0;
6398         proto_tree_add_time(rnd_tree, hf->hf.hs_random_time,
6399                 tvb, offset, 4, &gmt_unix_time);
6400         offset += 4;
6401
6402         /* show the random bytes */
6403         proto_tree_add_item(rnd_tree, hf->hf.hs_random_bytes,
6404                 tvb, offset, 28, ENC_NA);
6405         offset += 28;
6406     } else {
6407         offset += 32;
6408     }
6409
6410     if (from_server == 0 || session->version != TLSV1DOT3_VERSION) { /* No Session ID with TLS 1.3 on Server Hello */
6411         /* show the session id (length followed by actual Session ID) */
6412         sessid_length = tvb_get_guint8(tvb, offset);
6413         proto_tree_add_item(tree, hf->hf.hs_session_id_len,
6414                 tvb, offset, 1, ENC_BIG_ENDIAN);
6415         offset++;
6416
6417         if (ssl) {
6418             /* save the authorative SID for later use in ChangeCipherSpec.
6419              * (D)TLS restricts the SID to 32 chars, it does not make sense to
6420              * save more, so ignore larger ones. */
6421             if (from_server && sessid_length <= 32) {
6422                 tvb_memcpy(tvb, ssl->session_id.data, offset, sessid_length);
6423                 ssl->session_id.data_len = sessid_length;
6424             }
6425         }
6426         if (sessid_length > 0) {
6427             proto_tree_add_item(tree, hf->hf.hs_session_id,
6428                     tvb, offset, sessid_length, ENC_NA);
6429             offset += sessid_length;
6430         }
6431     }
6432
6433     return offset;
6434 }
6435
6436 static gint
6437 ssl_dissect_hnd_hello_ext_status_request(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
6438                                          guint32 offset, gboolean has_length)
6439 {
6440     guint    cert_status_type;
6441
6442     cert_status_type = tvb_get_guint8(tvb, offset);
6443     proto_tree_add_item(tree, hf->hf.hs_ext_cert_status_type,
6444                         tvb, offset, 1, ENC_NA);
6445     offset++;
6446
6447     if (has_length) {
6448         proto_tree_add_item(tree, hf->hf.hs_ext_cert_status_request_len,
6449                             tvb, offset, 2, ENC_BIG_ENDIAN);
6450         offset += 2;
6451     }
6452
6453     switch (cert_status_type) {
6454     case SSL_HND_CERT_STATUS_TYPE_OCSP:
6455     case SSL_HND_CERT_STATUS_TYPE_OCSP_MULTI:
6456         {
6457             guint16      responder_id_list_len;
6458             guint16      request_extensions_len;
6459             proto_item  *responder_id;
6460             proto_item  *request_extensions;
6461
6462             responder_id_list_len = tvb_get_ntohs(tvb, offset);
6463             responder_id =
6464                 proto_tree_add_item(tree,
6465                                     hf->hf.hs_ext_cert_status_responder_id_list_len,
6466                                     tvb, offset, 2, ENC_BIG_ENDIAN);
6467             offset += 2;
6468             if (responder_id_list_len != 0) {
6469                 expert_add_info_format(NULL, responder_id,
6470                                        &hf->ei.hs_ext_cert_status_undecoded,
6471                                        "Responder ID list is not implemented, contact Wireshark"
6472                                        " developers if you want this to be supported");
6473                 /* Non-empty responder ID list would mess with extensions. */
6474                 break;
6475             }
6476
6477             request_extensions_len = tvb_get_ntohs(tvb, offset);
6478             request_extensions =
6479                 proto_tree_add_item(tree,
6480                                     hf->hf.hs_ext_cert_status_request_extensions_len, tvb, offset,
6481                                     2, ENC_BIG_ENDIAN);
6482             offset += 2;
6483             if (request_extensions_len != 0)
6484                 expert_add_info_format(NULL, request_extensions,
6485                                        &hf->ei.hs_ext_cert_status_undecoded,
6486                                        "Request Extensions are not implemented, contact"
6487                                        " Wireshark developers if you want this to be supported");
6488             break;
6489         }
6490     }
6491
6492     return offset;
6493 }
6494
6495 static gint
6496 ssl_dissect_hnd_hello_ext_status_request_v2(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
6497                                             guint32 offset)
6498 {
6499     gint32   list_len;
6500
6501     list_len = tvb_get_ntohs(tvb, offset);
6502     offset += 2;
6503
6504     while (list_len > 0) {
6505         guint32 prev_offset = offset;
6506         offset = ssl_dissect_hnd_hello_ext_status_request(hf, tvb, tree, offset, TRUE);
6507         list_len -= (offset - prev_offset);
6508     }
6509
6510     return offset;
6511 }
6512
6513 static guint
6514 ssl_dissect_hnd_hello_ext_supported_groups(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6515                                            proto_tree *tree, guint32 offset, guint32 offset_end)
6516 {
6517     /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.4
6518      *  enum { ..., (0xFFFF) } NamedGroup;
6519      *  struct {
6520      *      NamedGroup named_group_list<2..2^16-1>
6521      *  } NamedGroupList;
6522      *
6523      * NOTE: "NamedCurve" (RFC 4492) is renamed to "NamedGroup" (RFC 7919) and
6524      * the extension itself from "elliptic_curves" to "supported_groups".
6525      */
6526     guint32     groups_length, next_offset;
6527     proto_tree *groups_tree;
6528     proto_item *ti;
6529
6530     /* NamedGroup named_group_list<2..2^16-1> */
6531     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &groups_length,
6532                         hf->hf.hs_ext_supported_groups_len, 2, G_MAXUINT16)) {
6533         return offset_end;
6534     }
6535     offset += 2;
6536     next_offset = offset + groups_length;
6537
6538     ti = proto_tree_add_none_format(tree,
6539                                     hf->hf.hs_ext_supported_groups,
6540                                     tvb, offset, groups_length,
6541                                     "Supported Groups (%d group%s)",
6542                                     groups_length / 2,
6543                                     plurality(groups_length/2, "", "s"));
6544
6545     /* make this a subtree */
6546     groups_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_groups);
6547
6548     /* loop over all groups */
6549     while (offset + 2 <= offset_end) {
6550         proto_tree_add_item(groups_tree, hf->hf.hs_ext_supported_group, tvb, offset, 2, ENC_BIG_ENDIAN);
6551         offset += 2;
6552     }
6553     if (!ssl_end_vector(hf, tvb, pinfo, groups_tree, offset, next_offset)) {
6554         offset = next_offset;
6555     }
6556
6557     return offset;
6558 }
6559
6560 static gint
6561 ssl_dissect_hnd_hello_ext_ec_point_formats(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6562                                            proto_tree *tree, guint32 offset)
6563 {
6564     guint8      ecpf_length;
6565     proto_tree *ecpf_tree;
6566     proto_item *ti;
6567
6568     ecpf_length = tvb_get_guint8(tvb, offset);
6569     proto_tree_add_item(tree, hf->hf.hs_ext_ec_point_formats_len,
6570         tvb, offset, 1, ENC_BIG_ENDIAN);
6571
6572     offset += 1;
6573     ti = proto_tree_add_none_format(tree,
6574                                     hf->hf.hs_ext_ec_point_formats,
6575                                     tvb, offset, ecpf_length,
6576                                     "Elliptic curves point formats (%d)",
6577                                     ecpf_length);
6578
6579     /* make this a subtree */
6580     ecpf_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_curves_point_formats);
6581
6582     /* loop over all point formats */
6583     while (ecpf_length > 0)
6584     {
6585         proto_tree_add_item(ecpf_tree, hf->hf.hs_ext_ec_point_format, tvb, offset, 1, ENC_BIG_ENDIAN);
6586         offset++;
6587         ecpf_length--;
6588     }
6589
6590     return offset;
6591 }
6592 /** TLS Extensions (in Client Hello and Server Hello). }}} */
6593
6594 /* Whether the Content and Handshake Types are valid; handle Protocol Version. {{{ */
6595 gboolean
6596 ssl_is_valid_content_type(guint8 type)
6597 {
6598     switch ((ContentType) type) {
6599     case SSL_ID_CHG_CIPHER_SPEC:
6600     case SSL_ID_ALERT:
6601     case SSL_ID_HANDSHAKE:
6602     case SSL_ID_APP_DATA:
6603     case SSL_ID_HEARTBEAT:
6604         return TRUE;
6605     }
6606     return FALSE;
6607 }
6608
6609 gboolean
6610 ssl_is_valid_handshake_type(guint8 hs_type, gboolean is_dtls)
6611 {
6612     switch ((HandshakeType) hs_type) {
6613     case SSL_HND_HELLO_VERIFY_REQUEST:
6614         /* hello_verify_request is DTLS-only */
6615         return is_dtls;
6616
6617     case SSL_HND_HELLO_REQUEST:
6618     case SSL_HND_CLIENT_HELLO:
6619     case SSL_HND_SERVER_HELLO:
6620     case SSL_HND_NEWSESSION_TICKET:
6621     case SSL_HND_HELLO_RETRY_REQUEST:
6622     case SSL_HND_ENCRYPTED_EXTENSIONS:
6623     case SSL_HND_CERTIFICATE:
6624     case SSL_HND_SERVER_KEY_EXCHG:
6625     case SSL_HND_CERT_REQUEST:
6626     case SSL_HND_SVR_HELLO_DONE:
6627     case SSL_HND_CERT_VERIFY:
6628     case SSL_HND_CLIENT_KEY_EXCHG:
6629     case SSL_HND_FINISHED:
6630     case SSL_HND_CERT_URL:
6631     case SSL_HND_CERT_STATUS:
6632     case SSL_HND_SUPPLEMENTAL_DATA:
6633     case SSL_HND_ENCRYPTED_EXTS:
6634         return TRUE;
6635     }
6636     return FALSE;
6637 }
6638
6639 static gboolean
6640 ssl_is_authoritative_version_message(guint8 content_type, guint8 handshake_type,
6641                                      gboolean is_dtls)
6642 {
6643     /* Consider all valid Handshake messages (except for Client Hello) and
6644      * all other valid record types (other than Handshake) */
6645     return (content_type == SSL_ID_HANDSHAKE &&
6646             ssl_is_valid_handshake_type(handshake_type, is_dtls) &&
6647             handshake_type != SSL_HND_CLIENT_HELLO) ||
6648            (content_type != SSL_ID_HANDSHAKE &&
6649             ssl_is_valid_content_type(content_type));
6650 }
6651
6652 void
6653 ssl_try_set_version(SslSession *session, SslDecryptSession *ssl,
6654                     guint8 content_type, guint8 handshake_type,
6655                     gboolean is_dtls, guint16 version)
6656 {
6657     if (!ssl_is_authoritative_version_message(content_type, handshake_type,
6658                 is_dtls))
6659         return;
6660
6661     switch (version) {
6662     case SSLV3_VERSION:
6663     case TLSV1_VERSION:
6664     case TLSV1DOT1_VERSION:
6665     case TLSV1DOT2_VERSION:
6666     case TLSV1DOT3_VERSION:
6667         if (is_dtls)
6668             return;
6669         break;
6670
6671     case DTLSV1DOT0_VERSION:
6672     case DTLSV1DOT0_OPENSSL_VERSION:
6673     case DTLSV1DOT2_VERSION:
6674         if (!is_dtls)
6675             return;
6676         break;
6677
6678     default: /* invalid version number */
6679         return;
6680     }
6681
6682     session->version = version;
6683     if (ssl) {
6684         ssl->state |= SSL_VERSION;
6685         ssl_debug_printf("%s found version 0x%04X -> state 0x%02X\n", G_STRFUNC, version, ssl->state);
6686     }
6687 }
6688
6689 static void
6690 ssl_set_cipher(SslDecryptSession *ssl, guint16 cipher)
6691 {
6692     /* store selected cipher suite for decryption */
6693     ssl->session.cipher = cipher;
6694
6695     if (!(ssl->cipher_suite = ssl_find_cipher(cipher))) {
6696         ssl->state &= ~SSL_CIPHER;
6697         ssl_debug_printf("%s can't find cipher suite 0x%04X\n", G_STRFUNC, cipher);
6698     } else {
6699         /* Cipher found, save this for the delayed decoder init */
6700         ssl->state |= SSL_CIPHER;
6701         ssl_debug_printf("%s found CIPHER 0x%04X %s -> state 0x%02X\n", G_STRFUNC, cipher,
6702                          val_to_str_ext_const(cipher, &ssl_31_ciphersuite_ext, "unknown"),
6703                          ssl->state);
6704     }
6705 }
6706 /* }}} */
6707
6708
6709 /* Client Hello and Server Hello dissections. {{{ */
6710 static gint
6711 ssl_dissect_hnd_hello_ext(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
6712                           packet_info* pinfo, guint32 offset, guint32 offset_end, guint8 hnd_type,
6713                           SslSession *session, SslDecryptSession *ssl,
6714                           gboolean is_dtls);
6715 void
6716 ssl_dissect_hnd_cli_hello(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6717                           packet_info *pinfo, proto_tree *tree, guint32 offset,
6718                           guint32 offset_end, SslSession *session,
6719                           SslDecryptSession *ssl, dtls_hfs_t *dtls_hfs)
6720 {
6721     /* struct {
6722      *     ProtocolVersion client_version;
6723      *     Random random;
6724      *     SessionID session_id;
6725      *     opaque cookie<0..32>;                   //new field for DTLS
6726      *     CipherSuite cipher_suites<2..2^16-1>;
6727      *     CompressionMethod compression_methods<1..2^8-1>;
6728      *     Extension client_hello_extension_list<0..2^16-1>;
6729      * } ClientHello;
6730      */
6731     proto_item *ti;
6732     proto_tree *cs_tree;
6733     guint32     cipher_suite_length;
6734     guint32     compression_methods_length;
6735     guint8      compression_method;
6736     guint32     next_offset;
6737
6738     /* show the client version */
6739     proto_tree_add_item(tree, hf->hf.hs_client_version, tvb,
6740                         offset, 2, ENC_BIG_ENDIAN);
6741     offset += 2;
6742
6743     /* dissect fields that are also present in ClientHello */
6744     offset = ssl_dissect_hnd_hello_common(hf, tvb, tree, offset, session, ssl, FALSE);
6745
6746     /* fields specific for DTLS (cookie_len, cookie) */
6747     if (dtls_hfs != NULL) {
6748         guint32 cookie_length;
6749         /* opaque cookie<0..32> (for DTLS only) */
6750         if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &cookie_length,
6751                             dtls_hfs->hf_dtls_handshake_cookie_len, 0, 32)) {
6752             return;
6753         }
6754         offset++;
6755         if (cookie_length > 0) {
6756             proto_tree_add_item(tree, dtls_hfs->hf_dtls_handshake_cookie,
6757                                 tvb, offset, cookie_length, ENC_NA);
6758             offset += cookie_length;
6759         }
6760     }
6761
6762     /* CipherSuite cipher_suites<2..2^16-1> */
6763     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &cipher_suite_length,
6764                         hf->hf.hs_cipher_suites_len, 2, G_MAXUINT16)) {
6765         return;
6766     }
6767     offset += 2;
6768     next_offset = offset + cipher_suite_length;
6769     if (ssl && cipher_suite_length == 2) {
6770         /*
6771          * If there is only a single cipher, assume that this will be used
6772          * (needed for 0-RTT decryption support in TLS 1.3).
6773          */
6774         ssl_set_cipher(ssl, tvb_get_ntohs(tvb, offset));
6775     }
6776     ti = proto_tree_add_none_format(tree,
6777                                     hf->hf.hs_cipher_suites,
6778                                     tvb, offset, cipher_suite_length,
6779                                     "Cipher Suites (%d suite%s)",
6780                                     cipher_suite_length / 2,
6781                                     plurality(cipher_suite_length/2, "", "s"));
6782     cs_tree = proto_item_add_subtree(ti, hf->ett.cipher_suites);
6783     while (offset + 2 <= next_offset) {
6784         proto_tree_add_item(cs_tree, hf->hf.hs_cipher_suite, tvb, offset, 2, ENC_BIG_ENDIAN);
6785         offset += 2;
6786     }
6787     if (!ssl_end_vector(hf, tvb, pinfo, cs_tree, offset, next_offset)) {
6788         offset = next_offset;
6789     }
6790
6791     /* CompressionMethod compression_methods<1..2^8-1> */
6792     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &compression_methods_length,
6793                         hf->hf.hs_comp_methods_len, 1, G_MAXUINT8)) {
6794         return;
6795     }
6796     offset++;
6797     next_offset = offset + compression_methods_length;
6798     ti = proto_tree_add_none_format(tree,
6799                                     hf->hf.hs_comp_methods,
6800                                     tvb, offset, compression_methods_length,
6801                                     "Compression Methods (%u method%s)",
6802                                     compression_methods_length,
6803                                     plurality(compression_methods_length,
6804                                       "", "s"));
6805     cs_tree = proto_item_add_subtree(ti, hf->ett.comp_methods);
6806     while (offset < next_offset) {
6807         compression_method = tvb_get_guint8(tvb, offset);
6808         /* TODO: make reserved/private comp meth. fields selectable */
6809         if (compression_method < 64)
6810             proto_tree_add_uint(cs_tree, hf->hf.hs_comp_method,
6811                                 tvb, offset, 1, compression_method);
6812         else if (compression_method > 63 && compression_method < 193)
6813             proto_tree_add_uint_format_value(cs_tree, hf->hf.hs_comp_method, tvb, offset, 1,
6814                                 compression_method, "Reserved - to be assigned by IANA (%u)",
6815                                 compression_method);
6816         else
6817             proto_tree_add_uint_format_value(cs_tree, hf->hf.hs_comp_method, tvb, offset, 1,
6818                                 compression_method, "Private use range (%u)",
6819                                 compression_method);
6820         offset++;
6821     }
6822
6823     /* SSL v3.0 has no extensions, so length field can indeed be missing. */
6824     if (offset < offset_end) {
6825         ssl_dissect_hnd_hello_ext(hf, tvb, tree, pinfo, offset,
6826                                   offset_end, SSL_HND_CLIENT_HELLO,
6827                                   session, ssl, dtls_hfs != NULL);
6828     }
6829 }
6830
6831 void
6832 ssl_dissect_hnd_srv_hello(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6833                           packet_info* pinfo, proto_tree *tree, guint32 offset, guint32 offset_end,
6834                           SslSession *session, SslDecryptSession *ssl,
6835                           gboolean is_dtls)
6836 {
6837     /* struct {
6838      *     ProtocolVersion server_version;
6839      *     Random random;
6840      *     SessionID session_id;                    // TLS 1.2 and before
6841      *     CipherSuite cipher_suite;
6842      *     CompressionMethod compression_method;    // TLS 1.2 and before
6843      *     Extension server_hello_extension_list<0..2^16-1>;
6844      * } ServerHello;
6845      */
6846     guint16 server_version;
6847
6848     /* This version is always better than the guess at the Record Layer */
6849     server_version = tvb_get_ntohs(tvb, offset);
6850     if((server_version & 0xFF00) == 0x7f00) { /* if server_version start with 0x7f, it is (and force) TLS 1.3 */
6851         server_version = TLSV1DOT3_VERSION;
6852     }
6853     ssl_try_set_version(session, ssl, SSL_ID_HANDSHAKE, SSL_HND_SERVER_HELLO,
6854             is_dtls, server_version);
6855     col_set_str(pinfo->cinfo, COL_PROTOCOL,
6856                 val_to_str_const(server_version, ssl_version_short_names, "SSL"));
6857
6858     /* Initially assume that the session is resumed. If this is not the case, a
6859      * ServerHelloDone will be observed before the ChangeCipherSpec message
6860      * which will reset this flag. */
6861     session->is_session_resumed = TRUE;
6862
6863     /* show the server version */
6864     proto_tree_add_item(tree, hf->hf.hs_server_version, tvb,
6865                         offset, 2, ENC_BIG_ENDIAN);
6866     offset += 2;
6867
6868     /* dissect fields that are also present in ClientHello */
6869     offset = ssl_dissect_hnd_hello_common(hf, tvb, tree, offset, session, ssl, TRUE);
6870
6871     if (ssl) {
6872         /* store selected cipher suite for decryption */
6873         ssl_set_cipher(ssl, tvb_get_ntohs(tvb, offset));
6874     }
6875
6876     /* now the server-selected cipher suite */
6877     proto_tree_add_item(tree, hf->hf.hs_cipher_suite,
6878                         tvb, offset, 2, ENC_BIG_ENDIAN);
6879     offset += 2;
6880
6881     if (session->version != TLSV1DOT3_VERSION) { /* No compression with TLS 1.3 */
6882         if (ssl) {
6883             /* store selected compression method for decryption */
6884             ssl->session.compression = tvb_get_guint8(tvb, offset);
6885         }
6886         /* and the server-selected compression method */
6887         proto_tree_add_item(tree, hf->hf.hs_comp_method,
6888                             tvb, offset, 1, ENC_BIG_ENDIAN);
6889         offset++;
6890     }
6891
6892     /* SSL v3.0 has no extensions, so length field can indeed be missing. */
6893     if (offset < offset_end) {
6894         ssl_dissect_hnd_hello_ext(hf, tvb, tree, pinfo, offset,
6895                                   offset_end, SSL_HND_SERVER_HELLO,
6896                                   session, ssl, is_dtls);
6897     }
6898 }
6899 /* Client Hello and Server Hello dissections. }}} */
6900
6901 /* New Session Ticket dissection. {{{ */
6902 void
6903 ssl_dissect_hnd_new_ses_ticket(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
6904                                proto_tree *tree, guint32 offset, guint32 offset_end,
6905                                const SslSession *session, SslDecryptSession *ssl _U_,
6906                                GHashTable *session_hash _U_)
6907 {
6908     /* https://tools.ietf.org/html/rfc5077#section-3.3 (TLS >= 1.0):
6909      *  struct {
6910      *      uint32 ticket_lifetime_hint;
6911      *      opaque ticket<0..2^16-1>;
6912      *  } NewSessionTicket;
6913      *
6914      * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.5.1
6915      *  struct {
6916      *      uint32 ticket_lifetime;
6917      *      uint32 ticket_age_add;
6918      *      opaque ticket<1..2^16-1>;
6919      *      Extension extensions<0..2^16-2>;
6920      *  } NewSessionTicket;
6921      */
6922     proto_tree *subtree;
6923     guint32     ticket_len;
6924     gboolean    is_tls13 = session->version == TLSV1DOT3_VERSION;
6925
6926     subtree = proto_tree_add_subtree(tree, tvb, offset, offset_end - offset,
6927                                      hf->ett.session_ticket, NULL,
6928                                      "TLS Session Ticket");
6929
6930     /* ticket lifetime hint */
6931     proto_tree_add_item(subtree, hf->hf.hs_session_ticket_lifetime_hint,
6932                         tvb, offset, 4, ENC_BIG_ENDIAN);
6933     offset += 4;
6934
6935     if (is_tls13) {
6936         /* for TLS 1.3: ticket_age_add */
6937         proto_tree_add_item(subtree, hf->hf.hs_session_ticket_age_add,
6938                             tvb, offset, 4, ENC_BIG_ENDIAN);
6939         offset += 4;
6940     }
6941
6942     /* opaque ticket<0..2^16-1> (with TLS 1.3 the minimum is 1) */
6943     if (!ssl_add_vector(hf, tvb, pinfo, subtree, offset, offset_end, &ticket_len,
6944                         hf->hf.hs_session_ticket_len, is_tls13 ? 1 : 0, G_MAXUINT16)) {
6945         return;
6946     }
6947     offset += 2;
6948
6949     /* Content depends on implementation, so just show data! */
6950     proto_tree_add_item(subtree, hf->hf.hs_session_ticket,
6951                         tvb, offset, ticket_len, ENC_NA);
6952     /* save the session ticket to cache for ssl_finalize_decryption */
6953 #ifdef HAVE_LIBGCRYPT
6954     if (ssl && !is_tls13) {
6955         tvb_ensure_bytes_exist(tvb, offset, ticket_len);
6956         ssl->session_ticket.data = (guchar*)wmem_realloc(wmem_file_scope(),
6957                                     ssl->session_ticket.data, ticket_len);
6958         ssl->session_ticket.data_len = ticket_len;
6959         tvb_memcpy(tvb, ssl->session_ticket.data, offset, ticket_len);
6960         /* NewSessionTicket is received after the first (client)
6961          * ChangeCipherSpec, and before the second (server) ChangeCipherSpec.
6962          * Since the second CCS has already the session key available it will
6963          * just return. To ensure that the session ticket is mapped to a
6964          * master key (from the first CCS), save the ticket here too. */
6965         ssl_save_master_key("Session Ticket", session_hash,
6966                             &ssl->session_ticket, &ssl->master_secret);
6967         ssl->state |= SSL_NEW_SESSION_TICKET;
6968     }
6969 #endif
6970     offset += ticket_len;
6971
6972     if (is_tls13) {
6973         guint32     exts_len;
6974
6975         /* Extension extensions<0..2^16-2> */
6976         if (!ssl_add_vector(hf, tvb, pinfo, subtree, offset, offset_end, &exts_len,
6977                             hf->hf.hs_exts_len, 0, G_MAXUINT16)) {
6978             return;
6979         }
6980         /* TODO handle ticket extensions (only early_data at the moment) */
6981     }
6982 } /* }}} */
6983
6984 void
6985 ssl_dissect_hnd_hello_retry_request(ssl_common_dissect_t *hf, tvbuff_t *tvb,
6986                                     packet_info* pinfo, proto_tree *tree, guint32 offset, guint32 offset_end,
6987                                     SslSession *session, SslDecryptSession *ssl,
6988                                     gboolean is_dtls)
6989 {
6990     /* https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.1.4
6991      * struct {
6992      *     ProtocolVersion server_version;
6993      *     Extension extensions<2..2^16-1>;
6994      * } HelloRetryRequest;
6995      */
6996     proto_tree_add_item(tree, hf->hf.hs_server_version, tvb,
6997                         offset, 2, ENC_BIG_ENDIAN);
6998     offset += 2;
6999
7000     /* remaining data are extensions */
7001     ssl_dissect_hnd_hello_ext(hf, tvb, tree, pinfo, offset,
7002                               offset_end, SSL_HND_HELLO_RETRY_REQUEST,
7003                               session, ssl, is_dtls);
7004 }
7005
7006 void
7007 ssl_dissect_hnd_encrypted_extensions(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7008                                      packet_info* pinfo, proto_tree *tree, guint32 offset, guint32 length,
7009                                      SslSession *session, SslDecryptSession *ssl,
7010                                      gboolean is_dtls)
7011 {
7012     /* struct {
7013      *     Extension extensions<0..2^16-1>;
7014      * } EncryptedExtensions;
7015      */
7016     ssl_dissect_hnd_hello_ext(hf, tvb, tree, pinfo, offset,
7017                               offset + length, SSL_HND_ENCRYPTED_EXTENSIONS,
7018                               session, ssl, is_dtls);
7019 }
7020
7021 /* Certificate and Certificate Request dissections. {{{ */
7022 void
7023 ssl_dissect_hnd_cert(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
7024                      guint32 offset, packet_info *pinfo,
7025                      const SslSession *session, SslDecryptSession *ssl _U_,
7026                      GHashTable *key_hash _U_, gint is_from_server)
7027 {
7028     /* opaque ASN.1Cert<1..2^24-1>;
7029      *
7030      * struct {
7031      *     select(certificate_type) {
7032      *
7033      *         // certificate type defined in RFC 7250
7034      *         case RawPublicKey:
7035      *           opaque ASN.1_subjectPublicKeyInfo<1..2^24-1>;
7036      *
7037      *         // X.509 certificate defined in RFC 5246
7038      *         case X.509:
7039      *           ASN.1Cert certificate_list<0..2^24-1>;
7040      *     };
7041      * } Certificate;
7042      *
7043      * draft-ietf-tls-tls13-18:
7044      *     opaque ASN1Cert<1..2^24-1>;
7045      *     struct {
7046      *         ASN1Cert cert_data;
7047      *         Extension extensions<0..2^16-1>;
7048      *     } CertificateEntry;
7049      *     struct {
7050      *         opaque certificate_request_context<0..2^8-1>;
7051      *         CertificateEntry certificate_list<0..2^24-1>;
7052      *     } Certificate;
7053      */
7054     enum { CERT_X509, CERT_RPK } cert_type;
7055     asn1_ctx_t  asn1_ctx;
7056 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
7057     gnutls_datum_t subjectPublicKeyInfo = { NULL, 0 };
7058 #endif
7059
7060     asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
7061
7062     if ((is_from_server && session->server_cert_type == SSL_HND_CERT_TYPE_RAW_PUBLIC_KEY) ||
7063         (!is_from_server && session->client_cert_type == SSL_HND_CERT_TYPE_RAW_PUBLIC_KEY)) {
7064         cert_type = CERT_RPK;
7065     } else {
7066         cert_type = CERT_X509;
7067     }
7068
7069 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
7070     /* Ask the pkcs1 dissector to return the public key details */
7071     if (ssl)
7072         asn1_ctx.private_data = &subjectPublicKeyInfo;
7073 #endif
7074
7075     switch (cert_type) {
7076     case CERT_RPK:
7077         {
7078             proto_tree_add_item(tree, hf->hf.hs_certificate_len,
7079                                 tvb, offset, 3, ENC_BIG_ENDIAN);
7080             offset += 3;
7081
7082             dissect_x509af_SubjectPublicKeyInfo(FALSE, tvb, offset, &asn1_ctx, tree, hf->hf.hs_certificate);
7083
7084             break;
7085         }
7086     case CERT_X509:
7087         {
7088             guint32     certificate_list_length;
7089             proto_item *ti;
7090             proto_tree *subtree;
7091
7092             /* TLS 1.3: opaque certificate_request_context<0..2^8-1> */
7093             if (session->version == TLSV1DOT3_VERSION) {
7094                 guint32 context_length;
7095                 proto_tree_add_item_ret_uint(tree, hf->hf.hs_certificate_request_context_length,
7096                                              tvb, offset, 1, ENC_NA, &context_length);
7097                 offset++;
7098                 if (context_length > 0) {
7099                     proto_tree_add_item(tree, hf->hf.hs_certificate_request_context,
7100                                         tvb, offset, context_length, ENC_NA);
7101                     offset += context_length;
7102                 }
7103             }
7104
7105             proto_tree_add_item_ret_uint(tree, hf->hf.hs_certificates_len,
7106                                          tvb, offset, 3, ENC_BIG_ENDIAN, &certificate_list_length);
7107             offset += 3;            /* 24-bit length value */
7108
7109             if (certificate_list_length > 0) {
7110                 ti = proto_tree_add_none_format(tree,
7111                                                 hf->hf.hs_certificates,
7112                                                 tvb, offset, certificate_list_length,
7113                                                 "Certificates (%u bytes)",
7114                                                 certificate_list_length);
7115
7116                 /* make it a subtree */
7117                 subtree = proto_item_add_subtree(ti, hf->ett.certificates);
7118
7119                 /* iterate through each certificate */
7120                 while (certificate_list_length > 0) {
7121                     /* get the length of the current certificate */
7122                     guint32 cert_length;
7123                     proto_tree_add_item_ret_uint(subtree, hf->hf.hs_certificate_len,
7124                                                  tvb, offset, 3, ENC_BIG_ENDIAN, &cert_length);
7125                     offset += 3;
7126                     certificate_list_length -= 3 + cert_length;
7127
7128                     dissect_x509af_Certificate(FALSE, tvb, offset, &asn1_ctx, subtree, hf->hf.hs_certificate);
7129 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
7130                     /* Only attempt to get the RSA modulus for the first cert. */
7131                     asn1_ctx.private_data = NULL;
7132 #endif
7133
7134                     offset += cert_length;
7135
7136                     /* TLS 1.3: Extension extensions<0..2^16-1> */
7137                     if (session->version == TLSV1DOT3_VERSION) {
7138                         guint32 extensions_length;
7139                         proto_tree_add_item_ret_uint(subtree, hf->hf.hs_exts_len,
7140                                                      tvb, offset, 2, ENC_BIG_ENDIAN, &extensions_length);
7141                         offset += 2;
7142
7143                         // XXX dissect OCSP and SCT extensions
7144                         // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.4.1.1
7145                         offset += extensions_length;
7146                         certificate_list_length -= 2 + extensions_length;
7147                     }
7148                 }
7149             }
7150             break;
7151         }
7152     }
7153
7154 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
7155     if (is_from_server && ssl)
7156         ssl_find_private_key_by_pubkey(ssl, key_hash, &subjectPublicKeyInfo);
7157 #endif
7158 }
7159
7160 void
7161 ssl_dissect_hnd_cert_req(ssl_common_dissect_t *hf, tvbuff_t *tvb, packet_info *pinfo,
7162                          proto_tree *tree, guint32 offset, guint32 offset_end,
7163                          const SslSession *session)
7164 {
7165     /* From SSL 3.0 and up (note that since TLS 1.1 certificate_authorities can be empty):
7166      *    enum {
7167      *        rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
7168      *        (255)
7169      *    } ClientCertificateType;
7170      *
7171      *    opaque DistinguishedName<1..2^16-1>;
7172      *
7173      *    struct {
7174      *        ClientCertificateType certificate_types<1..2^8-1>;
7175      *        DistinguishedName certificate_authorities<3..2^16-1>;
7176      *    } CertificateRequest;
7177      *
7178      *
7179      * As per TLSv1.2 (RFC 5246) the format has changed to:
7180      *
7181      *    enum {
7182      *        rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
7183      *        rsa_ephemeral_dh_RESERVED(5), dss_ephemeral_dh_RESERVED(6),
7184      *        fortezza_dms_RESERVED(20), (255)
7185      *    } ClientCertificateType;
7186      *
7187      *    enum {
7188      *        none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
7189      *        sha512(6), (255)
7190      *    } HashAlgorithm;
7191      *
7192      *    enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
7193      *      SignatureAlgorithm;
7194      *
7195      *    struct {
7196      *          HashAlgorithm hash;
7197      *          SignatureAlgorithm signature;
7198      *    } SignatureAndHashAlgorithm;
7199      *
7200      *    SignatureAndHashAlgorithm
7201      *      supported_signature_algorithms<2..2^16-2>;
7202      *
7203      *    opaque DistinguishedName<1..2^16-1>;
7204      *
7205      *    struct {
7206      *        ClientCertificateType certificate_types<1..2^8-1>;
7207      *        SignatureAndHashAlgorithm supported_signature_algorithms<2^16-1>;
7208      *        DistinguishedName certificate_authorities<0..2^16-1>;
7209      *    } CertificateRequest;
7210      *
7211      * draft-ietf-tls-tls13-18 (soon obsolete!):
7212      * Note: certificate_extensions is not dissected since it is removed in next
7213      * draft.
7214      *
7215      *    struct {
7216      *        opaque certificate_request_context<0..2^8-1>;
7217      *        SignatureScheme supported_signature_algorithms<2..2^16-2>;
7218      *        DistinguishedName certificate_authorities<0..2^16-1>;
7219      *        CertificateExtension certificate_extensions<0..2^16-1>;
7220      *    } CertificateRequest;
7221      *
7222      * draft-ietf-tls-tls13 (between -18 and -19, 2017-01-30):
7223      *
7224      *    struct {
7225      *        opaque certificate_request_context<0..2^8-1>;
7226      *        Extension extensions<2..2^16-1>;
7227      *    } CertificateRequest;
7228      */
7229     proto_item *ti;
7230     proto_tree *subtree;
7231     guint32     dnames_length = 0, next_offset;
7232     asn1_ctx_t  asn1_ctx;
7233
7234     if (!tree)
7235         return;
7236
7237     asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
7238
7239     if (session->version == TLSV1DOT3_VERSION) {
7240         guint32 context_length;
7241         /* opaque certificate_request_context<0..2^8-1> */
7242         if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &context_length,
7243                             hf->hf.hs_certificate_request_context_length, 0, G_MAXUINT8)) {
7244             return;
7245         }
7246         offset++;
7247         if (context_length > 0) {
7248             proto_tree_add_item(tree, hf->hf.hs_certificate_request_context,
7249                                 tvb, offset, context_length, ENC_NA);
7250             offset += context_length;
7251         }
7252     } else {
7253         guint32 cert_types_count;
7254         /* ClientCertificateType certificate_types<1..2^8-1> */
7255         if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &cert_types_count,
7256                             hf->hf.hs_cert_types_count, 1, G_MAXUINT8)) {
7257             return;
7258         }
7259         offset++;
7260         next_offset = offset + cert_types_count;
7261
7262         ti = proto_tree_add_none_format(tree,
7263                 hf->hf.hs_cert_types,
7264                 tvb, offset, cert_types_count,
7265                 "Certificate types (%u type%s)",
7266                 cert_types_count,
7267                 plurality(cert_types_count, "", "s"));
7268         subtree = proto_item_add_subtree(ti, hf->ett.cert_types);
7269
7270         while (offset < next_offset) {
7271             proto_tree_add_item(subtree, hf->hf.hs_cert_type, tvb, offset, 1, ENC_BIG_ENDIAN);
7272             offset++;
7273         }
7274     }
7275
7276     switch (session->version) {
7277         case TLSV1DOT2_VERSION:
7278         case DTLSV1DOT2_VERSION:
7279         case TLSV1DOT3_VERSION: /* XXX draft -18 only, remove for next version */
7280             offset = ssl_dissect_hash_alg_list(hf, tvb, tree, pinfo, offset, offset_end);
7281             break;
7282
7283         default:
7284             break;
7285     }
7286
7287     /* DistinguishedName certificate_authorities<0..2^16-1> */
7288     if (!ssl_add_vector(hf, tvb, pinfo, tree, offset, offset_end, &dnames_length,
7289                         hf->hf.hs_dnames_len, 0, G_MAXUINT16)) {
7290         return;
7291     }
7292     offset += 2;
7293     next_offset = offset + dnames_length;
7294
7295     if (dnames_length > 0) {
7296         ti = proto_tree_add_none_format(tree,
7297                 hf->hf.hs_dnames,
7298                 tvb, offset, dnames_length,
7299                 "Distinguished Names (%d byte%s)",
7300                 dnames_length,
7301                 plurality(dnames_length, "", "s"));
7302         subtree = proto_item_add_subtree(ti, hf->ett.dnames);
7303
7304         while (offset < next_offset) {
7305             /* get the length of the current certificate */
7306             guint32 name_length;
7307             /* opaque DistinguishedName<1..2^16-1> */
7308             if (!ssl_add_vector(hf, tvb, pinfo, subtree, offset, next_offset, &name_length,
7309                                 hf->hf.hs_dname_len, 1, G_MAXUINT16)) {
7310                 return;
7311             }
7312             offset += 2;
7313
7314             dissect_x509if_DistinguishedName(FALSE, tvb, offset, &asn1_ctx,
7315                                              subtree, hf->hf.hs_dname);
7316             offset += name_length;
7317         }
7318     }
7319
7320     /* TODO Extensions specific to certificates (TLS 1.3). */
7321 }
7322 /* Certificate and Certificate Request dissections. }}} */
7323
7324 static void
7325 ssl_dissect_digitally_signed(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7326                              proto_tree *tree, guint32 offset,
7327                              const SslSession *session,
7328                              gint hf_sig_len, gint hf_sig);
7329
7330 void
7331 ssl_dissect_hnd_cli_cert_verify(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7332                                 proto_tree *tree, guint32 offset,
7333                                 const SslSession *session)
7334 {
7335     ssl_dissect_digitally_signed(hf, tvb, tree, offset, session,
7336                                  hf->hf.hs_client_cert_vrfy_sig_len,
7337                                  hf->hf.hs_client_cert_vrfy_sig);
7338 }
7339
7340 /* Finished dissection. {{{ */
7341 void
7342 ssl_dissect_hnd_finished(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7343                          proto_tree *tree, guint32 offset, guint32 offset_end,
7344                          const SslSession *session, ssl_hfs_t *ssl_hfs)
7345 {
7346     /* For SSLv3:
7347      *     struct {
7348      *         opaque md5_hash[16];
7349      *         opaque sha_hash[20];
7350      *     } Finished;
7351      *
7352      * For (D)TLS:
7353      *     struct {
7354      *         opaque verify_data[12];
7355      *     } Finished;
7356      *
7357      * For TLS 1.3:
7358      *     struct {
7359      *         opaque verify_data[Hash.length];
7360      *     }
7361      */
7362     if (!tree)
7363         return;
7364
7365     if (session->version == SSLV3_VERSION) {
7366         if (ssl_hfs != NULL) {
7367             proto_tree_add_item(tree, ssl_hfs->hs_md5_hash,
7368                                 tvb, offset, 16, ENC_NA);
7369             proto_tree_add_item(tree, ssl_hfs->hs_sha_hash,
7370                                 tvb, offset + 16, 20, ENC_NA);
7371         }
7372     } else {
7373         /* Length should be 12 for TLS before 1.3, assume this is the case. */
7374         proto_tree_add_item(tree, hf->hf.hs_finished,
7375                             tvb, offset, offset_end - offset, ENC_NA);
7376     }
7377 } /* }}} */
7378
7379 /* RFC 6066 Certificate URL handshake message dissection. {{{ */
7380 void
7381 ssl_dissect_hnd_cert_url(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree, guint32 offset)
7382 {
7383     guint16  url_hash_len;
7384
7385     /* enum {
7386      *     individual_certs(0), pkipath(1), (255)
7387      * } CertChainType;
7388      *
7389      * struct {
7390      *     CertChainType type;
7391      *     URLAndHash url_and_hash_list<1..2^16-1>;
7392      * } CertificateURL;
7393      *
7394      * struct {
7395      *     opaque url<1..2^16-1>;
7396      *     unint8 padding;
7397      *     opaque SHA1Hash[20];
7398      * } URLAndHash;
7399      */
7400
7401     proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_type,
7402                         tvb, offset, 1, ENC_NA);
7403     offset++;
7404
7405     url_hash_len = tvb_get_ntohs(tvb, offset);
7406     proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_url_hash_list_len,
7407                         tvb, offset, 2, ENC_BIG_ENDIAN);
7408     offset += 2;
7409     while (url_hash_len-- > 0) {
7410         proto_item  *urlhash_item;
7411         proto_tree  *urlhash_tree;
7412         guint16      url_len;
7413
7414         urlhash_item = proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_item,
7415                                            tvb, offset, -1, ENC_NA);
7416         urlhash_tree = proto_item_add_subtree(urlhash_item, hf->ett.urlhash);
7417
7418         url_len = tvb_get_ntohs(tvb, offset);
7419         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_url_len,
7420                             tvb, offset, 2, ENC_BIG_ENDIAN);
7421         offset += 2;
7422
7423         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_url,
7424                             tvb, offset, url_len, ENC_ASCII|ENC_NA);
7425         offset += url_len;
7426
7427         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_padding,
7428                             tvb, offset, 1, ENC_NA);
7429         offset++;
7430         /* Note: RFC 6066 says that padding must be 0x01 */
7431
7432         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_sha1,
7433                             tvb, offset, 20, ENC_NA);
7434         offset += 20;
7435     }
7436 } /* }}} */
7437
7438 /* Client Hello and Server Hello TLS extensions dissection. {{{ */
7439 static gint
7440 ssl_dissect_hnd_hello_ext(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
7441                           packet_info* pinfo, guint32 offset, guint32 offset_end, guint8 hnd_type,
7442                           SslSession *session, SslDecryptSession *ssl,
7443                           gboolean is_dtls)
7444 {
7445     guint16     extension_length;
7446     guint16     ext_type;
7447     guint32     ext_len;
7448     guint32     next_offset;
7449     proto_tree *ext_tree;
7450
7451     if (offset_end - offset < 2)
7452         return offset;
7453
7454     extension_length = tvb_get_ntohs(tvb, offset);
7455     proto_tree_add_uint(tree, hf->hf.hs_exts_len,
7456                         tvb, offset, 2, extension_length);
7457     offset += 2;
7458
7459     while (offset_end - offset >= 4)
7460     {
7461         ext_type = tvb_get_ntohs(tvb, offset);
7462         ext_len  = tvb_get_ntohs(tvb, offset + 2);
7463
7464         ext_tree = proto_tree_add_subtree_format(tree, tvb, offset, 4 + ext_len, hf->ett.hs_ext, NULL,
7465                                   "Extension: %s (len=%u)", val_to_str(ext_type,
7466                                             tls_hello_extension_types,
7467                                             "Unknown type %u"), ext_len);
7468
7469         proto_tree_add_uint(ext_tree, hf->hf.hs_ext_type,
7470                             tvb, offset, 2, ext_type);
7471         offset += 2;
7472
7473         /* opaque extension_data<0..2^16-1> */
7474         if (!ssl_add_vector(hf, tvb, pinfo, ext_tree, offset, offset_end, &ext_len,
7475                             hf->hf.hs_ext_len, 0, G_MAXUINT16)) {
7476             return offset_end;
7477         }
7478         offset += 2;
7479         next_offset = offset + ext_len;
7480
7481         switch (ext_type) {
7482         case SSL_HND_HELLO_EXT_STATUS_REQUEST:
7483             if (hnd_type == SSL_HND_CLIENT_HELLO)
7484                 offset = ssl_dissect_hnd_hello_ext_status_request(hf, tvb, ext_tree, offset, FALSE);
7485             break;
7486         case SSL_HND_HELLO_EXT_STATUS_REQUEST_V2:
7487             if (hnd_type == SSL_HND_CLIENT_HELLO)
7488                 offset = ssl_dissect_hnd_hello_ext_status_request_v2(hf, tvb, ext_tree, offset);
7489             break;
7490         case SSL_HND_HELLO_EXT_SUPPORTED_GROUPS:
7491             offset = ssl_dissect_hnd_hello_ext_supported_groups(hf, tvb, pinfo, ext_tree, offset, next_offset);
7492             break;
7493         case SSL_HND_HELLO_EXT_EC_POINT_FORMATS:
7494             offset = ssl_dissect_hnd_hello_ext_ec_point_formats(hf, tvb, ext_tree, offset);
7495             break;
7496         case SSL_HND_HELLO_EXT_SIGNATURE_ALGORITHMS:
7497             offset = ssl_dissect_hnd_hello_ext_sig_hash_algs(hf, tvb, ext_tree, pinfo, offset, next_offset);
7498             break;
7499         case SSL_HND_HELLO_EXT_ALPN:
7500             offset = ssl_dissect_hnd_hello_ext_alpn(hf, tvb, pinfo, ext_tree, offset, next_offset, hnd_type, session);
7501             break;
7502         case SSL_HND_HELLO_EXT_NPN:
7503             offset = ssl_dissect_hnd_hello_ext_npn(hf, tvb, pinfo, ext_tree, offset, next_offset);
7504             break;
7505         case SSL_HND_HELLO_EXT_RENEGOTIATION_INFO:
7506             offset = ssl_dissect_hnd_hello_ext_reneg_info(hf, tvb, pinfo, ext_tree, offset, next_offset);
7507             break;
7508         case SSL_HND_HELLO_EXT_KEY_SHARE:
7509             offset = ssl_dissect_hnd_hello_ext_key_share(hf, tvb, pinfo, ext_tree, offset, next_offset, hnd_type);
7510             break;
7511         case SSL_HND_HELLO_EXT_PRE_SHARED_KEY:
7512             offset = ssl_dissect_hnd_hello_ext_pre_shared_key(hf, tvb, pinfo, ext_tree, offset, next_offset, hnd_type);
7513             break;
7514         case SSL_HND_HELLO_EXT_EARLY_DATA:
7515             if (hnd_type == SSL_HND_CLIENT_HELLO && ssl) {
7516                 ssl_debug_printf("%s found early_data extension\n", G_STRFUNC);
7517                 ssl->has_early_data = TRUE;
7518             }
7519             break;
7520         case SSL_HND_HELLO_EXT_SUPPORTED_VERSIONS:
7521             offset = ssl_dissect_hnd_hello_ext_supported_versions(hf, tvb, pinfo, ext_tree, offset, next_offset);
7522             break;
7523         case SSL_HND_HELLO_EXT_COOKIE:
7524             offset = ssl_dissect_hnd_hello_ext_cookie(hf, tvb, pinfo, ext_tree, offset, next_offset);
7525             break;
7526         case SSL_HND_HELLO_EXT_PSK_KEY_EXCHANGE_MODES:
7527             offset = ssl_dissect_hnd_hello_ext_psk_key_exchange_modes(hf, tvb, pinfo, ext_tree, offset, next_offset);
7528             break;
7529         case SSL_HND_HELLO_EXT_DRAFT_VERSION_TLS13:
7530             proto_tree_add_item(ext_tree, hf->hf.hs_ext_draft_version_tls13,
7531                                 tvb, offset, 2, ENC_BIG_ENDIAN);
7532             offset += 2;
7533             break;
7534         case SSL_HND_HELLO_EXT_SERVER_NAME:
7535             offset = ssl_dissect_hnd_hello_ext_server_name(hf, tvb, pinfo, ext_tree, offset, next_offset);
7536             break;
7537         case SSL_HND_HELLO_EXT_USE_SRTP:
7538             if (is_dtls) {
7539                 offset = dtls_dissect_hnd_hello_ext_use_srtp(tvb, ext_tree, offset, next_offset);
7540             } else {
7541                 // XXX expert info: This extension MUST only be used with DTLS, and not with TLS.
7542             }
7543             break;
7544         case SSL_HND_HELLO_EXT_HEARTBEAT:
7545             proto_tree_add_item(ext_tree, hf->hf.hs_ext_heartbeat_mode,
7546                                 tvb, offset, 1, ENC_BIG_ENDIAN);
7547             offset++;
7548             break;
7549         case SSL_HND_HELLO_EXT_PADDING:
7550             proto_tree_add_item(ext_tree, hf->hf.hs_ext_padding_data, tvb, offset, ext_len, ENC_NA);
7551             offset += ext_len;
7552             break;
7553         case SSL_HND_HELLO_EXT_SESSION_TICKET_TLS:
7554             offset = ssl_dissect_hnd_hello_ext_session_ticket(hf, tvb, ext_tree, offset, next_offset, hnd_type, ssl);
7555             break;
7556         case SSL_HND_HELLO_EXT_CERT_TYPE:
7557         case SSL_HND_HELLO_EXT_SERVER_CERT_TYPE:
7558         case SSL_HND_HELLO_EXT_CLIENT_CERT_TYPE:
7559             offset = ssl_dissect_hnd_hello_ext_cert_type(hf, tvb, ext_tree,
7560                                                          offset, next_offset,
7561                                                          hnd_type, ext_type,
7562                                                          session);
7563             break;
7564         case SSL_HND_HELLO_EXT_EXTENDED_MASTER_SECRET:
7565             if (ssl){
7566                 switch(hnd_type){
7567                     case SSL_HND_CLIENT_HELLO:
7568                         ssl->state |= SSL_CLIENT_EXTENDED_MASTER_SECRET;
7569                     break;
7570                     case SSL_HND_SERVER_HELLO:
7571                         ssl->state |= SSL_SERVER_EXTENDED_MASTER_SECRET;
7572                     break;
7573                     default: /* no default */
7574                     break;
7575                 }
7576             }
7577             break;
7578         default:
7579             proto_tree_add_item(ext_tree, hf->hf.hs_ext_data,
7580                                         tvb, offset, ext_len, ENC_NA);
7581             offset += ext_len;
7582             break;
7583         }
7584
7585         if (!ssl_end_vector(hf, tvb, pinfo, ext_tree, offset, next_offset)) {
7586             /* Dissection did not end at expected location, fix it. */
7587             offset = next_offset;
7588         }
7589     }
7590
7591     return offset;
7592 } /* }}} */
7593
7594
7595 /* ClientKeyExchange algo-specific dissectors. {{{ */
7596
7597 static void
7598 dissect_ssl3_hnd_cli_keyex_ecdh(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7599                                 proto_tree *tree, guint32 offset,
7600                                 guint32 length)
7601 {
7602     gint        point_len;
7603     proto_tree *ssl_ecdh_tree;
7604
7605     ssl_ecdh_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7606                                   hf->ett.keyex_params, NULL, "EC Diffie-Hellman Client Params");
7607
7608     /* point */
7609     point_len = tvb_get_guint8(tvb, offset);
7610     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_client_keyex_point_len, tvb,
7611                         offset, 1, ENC_BIG_ENDIAN);
7612     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_client_keyex_point, tvb,
7613                         offset + 1, point_len, ENC_NA);
7614 }
7615
7616 static void
7617 dissect_ssl3_hnd_cli_keyex_dh(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7618                               proto_tree *tree, guint32 offset, guint32 length)
7619 {
7620     gint        yc_len;
7621     proto_tree *ssl_dh_tree;
7622
7623     ssl_dh_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7624                                 hf->ett.keyex_params, NULL, "Diffie-Hellman Client Params");
7625
7626     /* ClientDiffieHellmanPublic.dh_public (explicit) */
7627     yc_len  = tvb_get_ntohs(tvb, offset);
7628     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_client_keyex_yc_len, tvb,
7629                         offset, 2, ENC_BIG_ENDIAN);
7630     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_client_keyex_yc, tvb,
7631                         offset + 2, yc_len, ENC_NA);
7632 }
7633
7634 static void
7635 dissect_ssl3_hnd_cli_keyex_rsa(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7636                                proto_tree *tree, guint32 offset,
7637                                guint32 length, const SslSession *session)
7638 {
7639     gint        epms_len;
7640     proto_tree *ssl_rsa_tree;
7641
7642     ssl_rsa_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7643                                  hf->ett.keyex_params, NULL, "RSA Encrypted PreMaster Secret");
7644
7645     /* EncryptedPreMasterSecret.pre_master_secret */
7646     switch (session->version) {
7647     case SSLV2_VERSION:
7648     case SSLV3_VERSION:
7649     case DTLSV1DOT0_OPENSSL_VERSION:
7650         /* OpenSSL pre-0.9.8f DTLS and pre-TLS quirk: 2-octet length vector is
7651          * not present. The handshake contents represents the EPMS, see:
7652          * https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=10222 */
7653         epms_len = length;
7654         break;
7655
7656     default:
7657         /* TLS and DTLS include vector length before EPMS */
7658         epms_len = tvb_get_ntohs(tvb, offset);
7659         proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_client_keyex_epms_len, tvb,
7660                             offset, 2, ENC_BIG_ENDIAN);
7661         offset += 2;
7662         break;
7663     }
7664     proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_client_keyex_epms, tvb,
7665                         offset, epms_len, ENC_NA);
7666 }
7667
7668 /* Used in PSK cipher suites */
7669 static void
7670 dissect_ssl3_hnd_cli_keyex_psk(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7671                                proto_tree *tree, guint32 offset, guint32 length)
7672 {
7673     guint        identity_len;
7674     proto_tree *ssl_psk_tree;
7675
7676     ssl_psk_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7677                                  hf->ett.keyex_params, NULL, "PSK Client Params");
7678     /* identity */
7679     identity_len = tvb_get_ntohs(tvb, offset);
7680     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_identity_len, tvb,
7681                         offset, 2, ENC_BIG_ENDIAN);
7682     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_identity, tvb,
7683                         offset + 2, identity_len, ENC_NA);
7684 }
7685
7686 /* Used in RSA PSK cipher suites */
7687 static void
7688 dissect_ssl3_hnd_cli_keyex_rsa_psk(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7689                                    proto_tree *tree, guint32 offset,
7690                                    guint32 length)
7691 {
7692     gint        identity_len, epms_len;
7693     proto_tree *ssl_psk_tree;
7694
7695     ssl_psk_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7696                                  hf->ett.keyex_params, NULL, "RSA PSK Client Params");
7697
7698     /* identity */
7699     identity_len = tvb_get_ntohs(tvb, offset);
7700     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_identity_len,
7701                         tvb, offset, 2, ENC_BIG_ENDIAN);
7702     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_identity,
7703                         tvb, offset + 2, identity_len, ENC_NA);
7704     offset += 2 + identity_len;
7705
7706     /* Yc */
7707     epms_len = tvb_get_ntohs(tvb, offset);
7708     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_epms_len, tvb,
7709                         offset, 2, ENC_BIG_ENDIAN);
7710     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_client_keyex_epms, tvb,
7711                         offset + 2, epms_len, ENC_NA);
7712 }
7713 /* ClientKeyExchange algo-specific dissectors. }}} */
7714
7715
7716 /* Dissects DigitallySigned (see RFC 5246 4.7 Cryptographic Attributes). {{{ */
7717 static void
7718 ssl_dissect_digitally_signed(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7719                              proto_tree *tree, guint32 offset,
7720                              const SslSession *session,
7721                              gint hf_sig_len, gint hf_sig)
7722 {
7723     gint        sig_len;
7724     proto_item *ti_algo;
7725     proto_tree *ssl_algo_tree;
7726
7727     switch (session->version) {
7728     case TLSV1DOT2_VERSION:
7729     case DTLSV1DOT2_VERSION:
7730     case TLSV1DOT3_VERSION: /* XXX merge both fields into one SignatureScheme? */
7731         ti_algo = proto_tree_add_item(tree, hf->hf.hs_sig_hash_alg, tvb,
7732                                       offset, 2, ENC_BIG_ENDIAN);
7733         ssl_algo_tree = proto_item_add_subtree(ti_algo, hf->ett.hs_sig_hash_alg);
7734
7735         /* SignatureAndHashAlgorithm { hash, signature } */
7736         proto_tree_add_item(ssl_algo_tree, hf->hf.hs_sig_hash_hash, tvb,
7737                             offset, 1, ENC_BIG_ENDIAN);
7738         proto_tree_add_item(ssl_algo_tree, hf->hf.hs_sig_hash_sig, tvb,
7739                             offset + 1, 1, ENC_BIG_ENDIAN);
7740         offset += 2;
7741         break;
7742
7743     default:
7744         break;
7745     }
7746
7747     /* Sig */
7748     sig_len = tvb_get_ntohs(tvb, offset);
7749     proto_tree_add_item(tree, hf_sig_len, tvb, offset, 2, ENC_BIG_ENDIAN);
7750     proto_tree_add_item(tree, hf_sig, tvb, offset + 2, sig_len, ENC_NA);
7751 } /* }}} */
7752
7753 /* ServerKeyExchange algo-specific dissectors. {{{ */
7754
7755 /* dissects signed_params inside a ServerKeyExchange for some keyex algos */
7756 static void
7757 dissect_ssl3_hnd_srv_keyex_sig(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7758                                proto_tree *tree, guint32 offset,
7759                                const SslSession *session)
7760 {
7761     /*
7762      * TLSv1.2 (RFC 5246 sec 7.4.8)
7763      *  struct {
7764      *      digitally-signed struct {
7765      *          opaque handshake_messages[handshake_messages_length];
7766      *      }
7767      *  } CertificateVerify;
7768      *
7769      * TLSv1.0/TLSv1.1 (RFC 5436 sec 7.4.8 and 7.4.3) works essentially the same
7770      * as TLSv1.2, but the hash algorithms are not explicit in digitally-signed.
7771      *
7772      * SSLv3 (RFC 6101 sec 5.6.8) esseentially works the same as TLSv1.0 but it
7773      * does more hashing including the master secret and padding.
7774      */
7775     ssl_dissect_digitally_signed(hf, tvb, tree, offset, session,
7776                                  hf->hf.hs_server_keyex_sig_len,
7777                                  hf->hf.hs_server_keyex_sig);
7778 }
7779
7780 static void
7781 dissect_ssl3_hnd_srv_keyex_ecdh(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7782                                 proto_tree *tree, guint32 offset,
7783                                 guint32 length, const SslSession *session,
7784                                 gboolean anon)
7785 {
7786     /*
7787      * RFC 4492 ECC cipher suites for TLS
7788      *
7789      *  struct {
7790      *      ECCurveType    curve_type;
7791      *      select (curve_type) {
7792      *          case explicit_prime:
7793      *              ...
7794      *          case explicit_char2:
7795      *              ...
7796      *          case named_curve:
7797      *              NamedCurve namedcurve;
7798      *      };
7799      *  } ECParameters;
7800      *
7801      *  struct {
7802      *      opaque point <1..2^8-1>;
7803      *  } ECPoint;
7804      *
7805      *  struct {
7806      *      ECParameters    curve_params;
7807      *      ECPoint         public;
7808      *  } ServerECDHParams;
7809      *
7810      *  select (KeyExchangeAlgorithm) {
7811      *      case ec_diffie_hellman:
7812      *          ServerECDHParams    params;
7813      *          Signature           signed_params;
7814      *  } ServerKeyExchange;
7815      */
7816
7817     gint        curve_type;
7818     gint        point_len;
7819     proto_tree *ssl_ecdh_tree;
7820
7821     ssl_ecdh_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7822                                   hf->ett.keyex_params, NULL, "EC Diffie-Hellman Server Params");
7823
7824     /* ECParameters.curve_type */
7825     curve_type = tvb_get_guint8(tvb, offset);
7826     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_server_keyex_curve_type, tvb,
7827                         offset, 1, ENC_BIG_ENDIAN);
7828     offset++;
7829     if (curve_type != 3)
7830         return; /* only named_curves are supported */
7831
7832     /* case curve_type == named_curve; ECParameters.namedcurve */
7833     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_server_keyex_named_curve, tvb,
7834                         offset, 2, ENC_BIG_ENDIAN);
7835     offset += 2;
7836
7837     /* ECPoint.point */
7838     point_len = tvb_get_guint8(tvb, offset);
7839     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_server_keyex_point_len, tvb,
7840                         offset, 1, ENC_BIG_ENDIAN);
7841     proto_tree_add_item(ssl_ecdh_tree, hf->hf.hs_server_keyex_point, tvb,
7842                         offset + 1, point_len, ENC_NA);
7843     offset += 1 + point_len;
7844
7845     /* Signature (if non-anonymous KEX) */
7846     if (!anon) {
7847         dissect_ssl3_hnd_srv_keyex_sig(hf, tvb, ssl_ecdh_tree, offset, session);
7848     }
7849 }
7850
7851 static void
7852 dissect_ssl3_hnd_srv_keyex_dhe(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7853                                proto_tree *tree, guint32 offset, guint32 length,
7854                                const SslSession *session, gboolean anon)
7855 {
7856     gint        p_len, g_len, ys_len;
7857     proto_tree *ssl_dh_tree;
7858
7859     ssl_dh_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7860                                 hf->ett.keyex_params, NULL, "Diffie-Hellman Server Params");
7861
7862     /* p */
7863     p_len = tvb_get_ntohs(tvb, offset);
7864     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_server_keyex_p_len, tvb,
7865                         offset, 2, ENC_BIG_ENDIAN);
7866     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_server_keyex_p, tvb,
7867                         offset + 2, p_len, ENC_NA);
7868     offset += 2 + p_len;
7869
7870     /* g */
7871     g_len = tvb_get_ntohs(tvb, offset);
7872     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_server_keyex_g_len, tvb,
7873                         offset, 2, ENC_BIG_ENDIAN);
7874     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_server_keyex_g, tvb,
7875                         offset + 2, g_len, ENC_NA);
7876     offset += 2 + g_len;
7877
7878     /* Ys */
7879     ys_len = tvb_get_ntohs(tvb, offset);
7880     proto_tree_add_uint(ssl_dh_tree, hf->hf.hs_server_keyex_ys_len, tvb,
7881                         offset, 2, ys_len);
7882     proto_tree_add_item(ssl_dh_tree, hf->hf.hs_server_keyex_ys, tvb,
7883                         offset + 2, ys_len, ENC_NA);
7884     offset += 2 + ys_len;
7885
7886     /* Signature (if non-anonymous KEX) */
7887     if (!anon) {
7888         dissect_ssl3_hnd_srv_keyex_sig(hf, tvb, ssl_dh_tree, offset, session);
7889     }
7890 }
7891
7892 /* Only used in RSA-EXPORT cipher suites */
7893 static void
7894 dissect_ssl3_hnd_srv_keyex_rsa(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7895                                proto_tree *tree, guint32 offset, guint32 length,
7896                                const SslSession *session)
7897 {
7898     gint        modulus_len, exponent_len;
7899     proto_tree *ssl_rsa_tree;
7900
7901     ssl_rsa_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7902                                  hf->ett.keyex_params, NULL, "RSA-EXPORT Server Params");
7903
7904     /* modulus */
7905     modulus_len = tvb_get_ntohs(tvb, offset);
7906     proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_server_keyex_modulus_len, tvb,
7907                         offset, 2, ENC_BIG_ENDIAN);
7908     proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_server_keyex_modulus, tvb,
7909                         offset + 2, modulus_len, ENC_NA);
7910     offset += 2 + modulus_len;
7911
7912     /* exponent */
7913     exponent_len = tvb_get_ntohs(tvb, offset);
7914     proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_server_keyex_exponent_len,
7915                         tvb, offset, 2, ENC_BIG_ENDIAN);
7916     proto_tree_add_item(ssl_rsa_tree, hf->hf.hs_server_keyex_exponent,
7917                         tvb, offset + 2, exponent_len, ENC_NA);
7918     offset += 2 + exponent_len;
7919
7920     /* Signature */
7921     dissect_ssl3_hnd_srv_keyex_sig(hf, tvb, ssl_rsa_tree, offset, session);
7922 }
7923
7924 /* Used in RSA PSK and PSK cipher suites */
7925 static void
7926 dissect_ssl3_hnd_srv_keyex_psk(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7927                                proto_tree *tree, guint32 offset, guint32 length)
7928 {
7929     guint        hint_len;
7930     proto_tree *ssl_psk_tree;
7931
7932     hint_len = tvb_get_ntohs(tvb, offset);
7933     if ((2 + hint_len) != length) {
7934         /* Lengths don't line up (wasn't what we expected?) */
7935         return;
7936     }
7937
7938     ssl_psk_tree = proto_tree_add_subtree(tree, tvb, offset, length,
7939                                  hf->ett.keyex_params, NULL, "PSK Server Params");
7940
7941     /* hint */
7942     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_server_keyex_hint_len, tvb,
7943                         offset, 2, ENC_BIG_ENDIAN);
7944     proto_tree_add_item(ssl_psk_tree, hf->hf.hs_server_keyex_hint, tvb,
7945                         offset + 2, hint_len, ENC_NA);
7946 }
7947 /* ServerKeyExchange algo-specific dissectors. }}} */
7948
7949 /* Client Key Exchange and Server Key Exchange handshake dissections. {{{ */
7950 void
7951 ssl_dissect_hnd_cli_keyex(ssl_common_dissect_t *hf, tvbuff_t *tvb,
7952                           proto_tree *tree, guint32 offset, guint32 length,
7953                           const SslSession *session)
7954 {
7955     switch (ssl_get_keyex_alg(session->cipher)) {
7956     case KEX_DH_ANON: /* RFC 5246; DHE_DSS, DHE_RSA, DH_DSS, DH_RSA, DH_ANON: ClientDiffieHellmanPublic */
7957     case KEX_DH_DSS:
7958     case KEX_DH_RSA:
7959     case KEX_DHE_DSS:
7960     case KEX_DHE_RSA:
7961         dissect_ssl3_hnd_cli_keyex_dh(hf, tvb, tree, offset, length);
7962         break;
7963     case KEX_DHE_PSK: /* RFC 4279; diffie_hellman_psk: psk_identity, ClientDiffieHellmanPublic */
7964         /* XXX: implement support for DHE_PSK */
7965         break;
7966     case KEX_ECDH_ANON: /* RFC 4492; ec_diffie_hellman: ClientECDiffieHellmanPublic */
7967     case KEX_ECDH_ECDSA:
7968     case KEX_ECDH_RSA:
7969     case KEX_ECDHE_ECDSA:
7970     case KEX_ECDHE_RSA:
7971         dissect_ssl3_hnd_cli_keyex_ecdh(hf, tvb, tree, offset, length);
7972         break;
7973     case KEX_ECDHE_PSK: /* RFC 5489; ec_diffie_hellman_psk: psk_identity, ClientECDiffieHellmanPublic */
7974         /* XXX: implement support for ECDHE_PSK */
7975         break;
7976     case KEX_KRB5: /* RFC 2712; krb5: KerberosWrapper */
7977         /* XXX: implement support for KRB5 */
7978         break;
7979     case KEX_PSK: /* RFC 4279; psk: psk_identity */
7980         dissect_ssl3_hnd_cli_keyex_psk(hf, tvb, tree, offset, length);
7981         break;
7982     case KEX_RSA: /* RFC 5246; rsa: EncryptedPreMasterSecret */
7983         dissect_ssl3_hnd_cli_keyex_rsa(hf, tvb, tree, offset, length, session);
7984         break;
7985     case KEX_RSA_PSK: /* RFC 4279; rsa_psk: psk_identity, EncryptedPreMasterSecret */
7986         dissect_ssl3_hnd_cli_keyex_rsa_psk(hf, tvb, tree, offset, length);
7987         break;
7988     case KEX_SRP_SHA: /* RFC 5054; srp: ClientSRPPublic */
7989     case KEX_SRP_SHA_DSS:
7990     case KEX_SRP_SHA_RSA:
7991         /* XXX: implement support for SRP_SHA* */
7992         break;
7993     default:
7994         /* XXX: add info message for not supported KEX algo */
7995         break;
7996     }
7997 }
7998
7999 void
8000 ssl_dissect_hnd_srv_keyex(ssl_common_dissect_t *hf, tvbuff_t *tvb,
8001                           proto_tree *tree, guint32 offset, guint32 length,
8002                           const SslSession *session)
8003 {
8004     switch (ssl_get_keyex_alg(session->cipher)) {
8005     case KEX_DH_ANON: /* RFC 5246; ServerDHParams */
8006         dissect_ssl3_hnd_srv_keyex_dhe(hf, tvb, tree, offset, length, session, TRUE);
8007         break;
8008     case KEX_DH_DSS: /* RFC 5246; not allowed */
8009     case KEX_DH_RSA:
8010         /* XXX: add error on not allowed KEX */
8011         break;
8012     case KEX_DHE_DSS: /* RFC 5246; dhe_dss, dhe_rsa: ServerDHParams, Signature */
8013     case KEX_DHE_RSA:
8014         dissect_ssl3_hnd_srv_keyex_dhe(hf, tvb, tree, offset, length, session, FALSE);
8015         break;
8016     case KEX_DHE_PSK: /* RFC 4279; diffie_hellman_psk: psk_identity_hint, ServerDHParams */
8017         /* XXX: implement support for DHE_PSK */
8018         break;
8019     case KEX_ECDH_ANON: /* RFC 4492; ec_diffie_hellman: ServerECDHParams (without signature for anon) */
8020         dissect_ssl3_hnd_srv_keyex_ecdh(hf, tvb, tree, offset, length, session, TRUE);
8021         break;
8022     case KEX_ECDHE_PSK: /* RFC 5489; psk_identity_hint, ServerECDHParams */
8023         /* XXX: implement support for ECDHE_PSK */
8024         break;
8025     case KEX_ECDH_ECDSA: /* RFC 4492; ec_diffie_hellman: ServerECDHParams, Signature */
8026     case KEX_ECDH_RSA:
8027     case KEX_ECDHE_ECDSA:
8028     case KEX_ECDHE_RSA:
8029         dissect_ssl3_hnd_srv_keyex_ecdh(hf, tvb, tree, offset, length, session, FALSE);
8030         break;
8031     case KEX_KRB5: /* RFC 2712; not allowed */
8032         /* XXX: add error on not allowed KEX */
8033         break;
8034     case KEX_PSK: /* RFC 4279; psk, rsa: psk_identity*/
8035     case KEX_RSA_PSK:
8036         dissect_ssl3_hnd_srv_keyex_psk(hf, tvb, tree, offset, length);
8037         break;
8038     case KEX_RSA: /* only allowed if the public key in the server certificate is longer than 512 bits*/
8039         dissect_ssl3_hnd_srv_keyex_rsa(hf, tvb, tree, offset, length, session);
8040         break;
8041     case KEX_SRP_SHA: /* RFC 5054; srp: ServerSRPParams, Signature */
8042     case KEX_SRP_SHA_DSS:
8043     case KEX_SRP_SHA_RSA:
8044         /* XXX: implement support for SRP_SHA* */
8045         break;
8046     default:
8047         /* XXX: add info message for not supported KEX algo */
8048         break;
8049     }
8050 }
8051 /* Client Key Exchange and Server Key Exchange handshake dissections. }}} */
8052
8053 #ifdef HAVE_LIBGCRYPT
8054 void
8055 ssl_common_register_options(module_t *module, ssl_common_options_t *options)
8056 {
8057         prefs_register_string_preference(module, "psk", "Pre-Shared-Key",
8058              "Pre-Shared-Key as HEX string. Should be 0 to 16 bytes.",
8059              &(options->psk));
8060
8061         prefs_register_filename_preference(module, "keylog_file", "(Pre)-Master-Secret log filename",
8062              "The name of a file which contains a list of \n"
8063              "(pre-)master secrets in one of the following formats:\n"
8064              "\n"
8065              "RSA <EPMS> <PMS>\n"
8066              "RSA Session-ID:<SSLID> Master-Key:<MS>\n"
8067              "CLIENT_RANDOM <CRAND> <MS>\n"
8068              "PMS_CLIENT_RANDOM <CRAND> <PMS>\n"
8069              "\n"
8070              "Where:\n"
8071              "<EPMS> = First 8 bytes of the Encrypted PMS\n"
8072              "<PMS> = The Pre-Master-Secret (PMS) used to derive the MS\n"
8073              "<SSLID> = The SSL Session ID\n"
8074              "<MS> = The Master-Secret (MS)\n"
8075              "<CRAND> = The Client's random number from the ClientHello message\n"
8076              "\n"
8077              "(All fields are in hex notation)",
8078              &(options->keylog_filename));
8079 }
8080 #else
8081 void
8082 ssl_common_register_options(module_t *module _U_, ssl_common_options_t *options _U_)
8083 {
8084 }
8085 #endif
8086
8087 void
8088 ssl_calculate_handshake_hash(SslDecryptSession *ssl_session, tvbuff_t *tvb, guint32 offset, guint32 length)
8089 {
8090     if (ssl_session && ssl_session->session.version != TLSV1DOT3_VERSION && !(ssl_session->state & SSL_MASTER_SECRET)) {
8091         guint32 old_length = ssl_session->handshake_data.data_len;
8092         ssl_debug_printf("Calculating hash with offset %d %d\n", offset, length);
8093         ssl_session->handshake_data.data = (guchar *)wmem_realloc(wmem_file_scope(), ssl_session->handshake_data.data, old_length + length);
8094         tvb_memcpy(tvb, ssl_session->handshake_data.data + old_length, offset, length);
8095         ssl_session->handshake_data.data_len += length;
8096     }
8097 }
8098
8099
8100 /*
8101  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
8102  *
8103  * Local variables:
8104  * c-basic-offset: 4
8105  * tab-width: 8
8106  * indent-tabs-mode: nil
8107  * End:
8108  *
8109  * vi: set shiftwidth=4 tabstop=8 expandtab:
8110  * :indentSize=4:tabSize=8:noTabs=true:
8111  */