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