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