6d483fb5fe17933ea923ce7584d48e569d6cab3b
[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  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28 #include "config.h"
29
30 #ifdef HAVE_LIBZ
31 #include <zlib.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include "packet-ssl-utils.h"
37 #include "packet-ssl.h"
38
39 #include <epan/emem.h>
40 #include <epan/strutil.h>
41 #include <epan/addr_resolv.h>
42 #include <epan/ipv6-utils.h>
43 #include <epan/expert.h>
44 #include <wsutil/file_util.h>
45
46 /*
47  * Lookup tables
48  */
49 const value_string ssl_version_short_names[] = {
50     { SSL_VER_UNKNOWN,    "SSL" },
51     { SSL_VER_SSLv2,      "SSLv2" },
52     { SSL_VER_SSLv3,      "SSLv3" },
53     { SSL_VER_TLS,        "TLSv1" },
54     { SSL_VER_TLSv1DOT1,  "TLSv1.1" },
55     { SSL_VER_DTLS,       "DTLSv1.0" },
56     { SSL_VER_DTLS1DOT2,  "DTLSv1.2" },
57     { SSL_VER_DTLS_OPENSSL, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
58     { SSL_VER_PCT,        "PCT" },
59     { SSL_VER_TLSv1DOT2,  "TLSv1.2" },
60     { 0x00, NULL }
61 };
62
63 const value_string ssl_20_msg_types[] = {
64     { SSL2_HND_ERROR,               "Error" },
65     { SSL2_HND_CLIENT_HELLO,        "Client Hello" },
66     { SSL2_HND_CLIENT_MASTER_KEY,   "Client Master Key" },
67     { SSL2_HND_CLIENT_FINISHED,     "Client Finished" },
68     { SSL2_HND_SERVER_HELLO,        "Server Hello" },
69     { SSL2_HND_SERVER_VERIFY,       "Server Verify" },
70     { SSL2_HND_SERVER_FINISHED,     "Server Finished" },
71     { SSL2_HND_REQUEST_CERTIFICATE, "Request Certificate" },
72     { SSL2_HND_CLIENT_CERTIFICATE,  "Client Certificate" },
73     { 0x00, NULL }
74 };
75 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
76 /* Note: sorted by ascending value so value_string-ext can do a binary search */
77 static const value_string ssl_20_cipher_suites[] = {
78     { 0x000000, "TLS_NULL_WITH_NULL_NULL" },
79     { 0x000001, "TLS_RSA_WITH_NULL_MD5" },
80     { 0x000002, "TLS_RSA_WITH_NULL_SHA" },
81     { 0x000003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
82     { 0x000004, "TLS_RSA_WITH_RC4_128_MD5" },
83     { 0x000005, "TLS_RSA_WITH_RC4_128_SHA" },
84     { 0x000006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
85     { 0x000007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
86     { 0x000008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
87     { 0x000009, "TLS_RSA_WITH_DES_CBC_SHA" },
88     { 0x00000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
89     { 0x00000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
90     { 0x00000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
91     { 0x00000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
92     { 0x00000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
93     { 0x00000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
94     { 0x000010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
95     { 0x000011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
96     { 0x000012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
97     { 0x000013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
98     { 0x000014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
99     { 0x000015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
100     { 0x000016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
101     { 0x000017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
102     { 0x000018, "TLS_DH_anon_WITH_RC4_128_MD5" },
103     { 0x000019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
104     { 0x00001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
105     { 0x00001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
106     { 0x00001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
107     { 0x00001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
108 #if 0
109     { 0x00001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
110 #endif
111     /* RFC 2712 */
112     { 0x00001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
113     { 0x00001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
114     { 0x000020, "TLS_KRB5_WITH_RC4_128_SHA" },
115     { 0x000021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
116     { 0x000022, "TLS_KRB5_WITH_DES_CBC_MD5" },
117     { 0x000023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
118     { 0x000024, "TLS_KRB5_WITH_RC4_128_MD5" },
119     { 0x000025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
120     { 0x000026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
121     { 0x000027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
122     { 0x000028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
123     { 0x000029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
124     { 0x00002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
125     { 0x00002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
126     /* RFC 4785 */
127     { 0x00002C, "TLS_PSK_WITH_NULL_SHA" },
128     { 0x00002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
129     { 0x00002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
130     /* RFC 5246 */
131     { 0x00002f, "TLS_RSA_WITH_AES_128_CBC_SHA" },
132     { 0x000030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
133     { 0x000031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
134     { 0x000032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
135     { 0x000033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
136     { 0x000034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
137     { 0x000035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
138     { 0x000036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
139     { 0x000037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
140     { 0x000038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
141     { 0x000039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
142     { 0x00003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
143     { 0x00003B, "TLS_RSA_WITH_NULL_SHA256" },
144     { 0x00003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
145     { 0x00003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
146     { 0x00003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
147     { 0x00003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
148     { 0x000040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
149     { 0x000041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
150     { 0x000042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
151     { 0x000043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
152     { 0x000044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
153     { 0x000045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
154     { 0x000046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
155     { 0x000047, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
156     { 0x000048, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
157     { 0x000049, "TLS_ECDH_ECDSA_WITH_DES_CBC_SHA" },
158     { 0x00004A, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
159     { 0x00004B, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
160     { 0x00004C, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
161     { 0x000060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
162     { 0x000061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
163     { 0x000062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
164     { 0x000063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
165     { 0x000064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
166     { 0x000065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
167     { 0x000066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
168     { 0x000067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
169     { 0x000068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
170     { 0x000069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
171     { 0x00006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
172     { 0x00006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
173     { 0x00006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
174     { 0x00006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
175     /* 0x00,0x6E-83 Unassigned  */
176     { 0x000084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
177     { 0x000085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
178     { 0x000086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
179     { 0x000087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
180     { 0x000088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
181     { 0x000089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
182     /* RFC 4279 */
183     { 0x00008A, "TLS_PSK_WITH_RC4_128_SHA" },
184     { 0x00008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
185     { 0x00008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
186     { 0x00008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
187     { 0x00008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
188     { 0x00008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
189     { 0x000090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
190     { 0x000091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
191     { 0x000092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
192     { 0x000093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
193     { 0x000094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
194     { 0x000095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
195     /* RFC 4162 */
196     { 0x000096, "TLS_RSA_WITH_SEED_CBC_SHA" },
197     { 0x000097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
198     { 0x000098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
199     { 0x000099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
200     { 0x00009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
201     { 0x00009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
202     /* RFC 5288 */
203     { 0x00009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
204     { 0x00009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
205     { 0x00009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
206     { 0x00009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
207     { 0x0000A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
208     { 0x0000A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
209     { 0x0000A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
210     { 0x0000A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
211     { 0x0000A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
212     { 0x0000A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
213     { 0x0000A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
214     { 0x0000A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
215     /* RFC 5487 */
216     { 0x0000A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
217     { 0x0000A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
218     { 0x0000AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
219     { 0x0000AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
220     { 0x0000AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
221     { 0x0000AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
222     { 0x0000AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
223     { 0x0000AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
224     { 0x0000B0, "TLS_PSK_WITH_NULL_SHA256" },
225     { 0x0000B1, "TLS_PSK_WITH_NULL_SHA384" },
226     { 0x0000B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
227     { 0x0000B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
228     { 0x0000B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
229     { 0x0000B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
230     { 0x0000B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
231     { 0x0000B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
232     { 0x0000B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
233     { 0x0000B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
234     /* From RFC 5932 */
235     { 0x0000BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
236     { 0x0000BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
237     { 0x0000BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
238     { 0x0000BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
239     { 0x0000BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
240     { 0x0000BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
241     { 0x0000C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
242     { 0x0000C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
243     { 0x0000C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
244     { 0x0000C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
245     { 0x0000C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
246     { 0x0000C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
247     /* 0x00,0xC6-FE Unassigned  */
248     { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
249     /* 0x01-BF,* Unassigned  */
250     /* From RFC 4492 */
251     { 0x00c001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
252     { 0x00c002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
253     { 0x00c003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
254     { 0x00c004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
255     { 0x00c005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
256     { 0x00c006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
257     { 0x00c007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
258     { 0x00c008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
259     { 0x00c009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
260     { 0x00c00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
261     { 0x00c00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
262     { 0x00c00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
263     { 0x00c00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
264     { 0x00c00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
265     { 0x00c00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
266     { 0x00c010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
267     { 0x00c011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
268     { 0x00c012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
269     { 0x00c013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
270     { 0x00c014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
271     { 0x00c015, "TLS_ECDH_anon_WITH_NULL_SHA" },
272     { 0x00c016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
273     { 0x00c017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
274     { 0x00c018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
275     { 0x00c019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
276     /* RFC 5054 */
277     { 0x00C01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
278     { 0x00C01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
279     { 0x00C01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
280     { 0x00C01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
281     { 0x00C01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
282     { 0x00C01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
283     { 0x00C020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
284     { 0x00C021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
285     { 0x00C022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
286     /* RFC 5589 */
287     { 0x00C023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
288     { 0x00C024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
289     { 0x00C025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
290     { 0x00C026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
291     { 0x00C027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
292     { 0x00C028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
293     { 0x00C029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
294     { 0x00C02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
295     { 0x00C02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
296     { 0x00C02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
297     { 0x00C02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
298     { 0x00C02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
299     { 0x00C02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
300     { 0x00C030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
301     { 0x00C031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
302     { 0x00C032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
303     /* RFC 5489 */
304     { 0x00C033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
305     { 0x00C034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
306     { 0x00C035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
307     { 0x00C036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
308     { 0x00C037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
309     { 0x00C038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
310     { 0x00C039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
311     { 0x00C03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
312     { 0x00C03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
313     /* 0xC0,0x3C-FF Unassigned
314             0xC1-FD,* Unassigned
315             0xFE,0x00-FD Unassigned
316             0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
317             0xFF,0x00-FF Reserved for Private Use [RFC5246]
318             */
319
320     /* these from http://www.mozilla.org/projects/
321          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
322     { 0x00fefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
323     { 0x00feff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
324     { 0x00ffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
325     { 0x00ffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
326     /* note that ciphersuites of {0x00????} are TLS cipher suites in
327      * a sslv2 client hello message; the ???? above is the two-byte
328      * tls cipher suite id
329      */
330
331     { 0x010080, "SSL2_RC4_128_WITH_MD5" },
332     { 0x020080, "SSL2_RC4_128_EXPORT40_WITH_MD5" },
333     { 0x030080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
334     { 0x040080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
335     { 0x050080, "SSL2_IDEA_128_CBC_WITH_MD5" },
336     { 0x060040, "SSL2_DES_64_CBC_WITH_MD5" },
337     { 0x0700c0, "SSL2_DES_192_EDE3_CBC_WITH_MD5" },
338     { 0x080080, "SSL2_RC4_64_WITH_MD5" },
339
340     /* Microsoft's old PCT protocol. These are from Eric Rescorla's
341        book "SSL and TLS" */
342     { 0x800001, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509" },
343     { 0x800003, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509_CHAIN" },
344     { 0x810001, "PCT_SSL_HASH_TYPE | PCT1_HASH_MD5" },
345     { 0x810003, "PCT_SSL_HASH_TYPE | PCT1_HASH_SHA" },
346     { 0x820001, "PCT_SSL_EXCH_TYPE | PCT1_EXCH_RSA_PKCS1" },
347     { 0x830004, "PCT_SSL_CIPHER_TYPE_1ST_HALF | PCT1_CIPHER_RC4" },
348     { 0x842840, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_40 | PCT1_MAC_BITS_128" },
349     { 0x848040, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_128 | PCT1_MAC_BITS_128" },
350     { 0x8f8001, "PCT_SSL_COMPAT | PCT_VERSION_1" },
351     { 0x00, NULL }
352 };
353
354 value_string_ext ssl_20_cipher_suites_ext = VALUE_STRING_EXT_INIT(ssl_20_cipher_suites);
355
356
357 const value_string ssl_extension_curves[] = {
358     {  1, "sect163k1" },
359     {  2, "sect163r1" },
360     {  3, "sect163r2" },
361     {  4, "sect193r1" },
362     {  5, "sect193r2" },
363     {  6, "sect233k1" },
364     {  7, "sect233r1" },
365     {  8, "sect239k1" },
366     {  9, "sect283k1" },
367     { 10, "sect283r1" },
368     { 11, "sect409k1" },
369     { 12, "sect409r1" },
370     { 13, "sect571k1" },
371     { 14, "sect571r1" },
372     { 15, "secp160k1" },
373     { 16, "secp160r1" },
374     { 17, "secp160r2" },
375     { 18, "secp192k1" },
376     { 19, "secp192r1" },
377     { 20, "secp224k1" },
378     { 21, "secp224r1" },
379     { 22, "secp256k1" },
380     { 23, "secp256r1" },
381     { 24, "secp384r1" },
382     { 25, "secp521r1" },
383     { 0xFF01, "arbitrary_explicit_prime_curves" },
384     { 0xFF02, "arbitrary_explicit_char2_curves" },
385     { 0x00, NULL }
386 };
387
388 const value_string ssl_curve_types[] = {
389     { 1, "explicit_prime" },
390     { 2, "explicit_char2" },
391     { 3, "named_curve" },
392     { 0x00, NULL }
393 };
394
395 const value_string ssl_extension_ec_point_formats[] = {
396     { 0, "uncompressed" },
397     { 1, "ansiX962_compressed_prime" },
398     { 2, "ansiX962_compressed_char2" },
399     { 0x00, NULL }
400 };
401
402 const value_string ssl_20_certificate_type[] = {
403     { 0x00, "N/A" },
404     { 0x01, "X.509 Certificate" },
405     { 0x00, NULL }
406 };
407
408 const value_string ssl_31_content_type[] = {
409     { 20, "Change Cipher Spec" },
410     { 21, "Alert" },
411     { 22, "Handshake" },
412     { 23, "Application Data" },
413     { 24, "Heartbeat" },
414     { 0x00, NULL }
415 };
416
417 const value_string ssl_versions[] = {
418     { 0xfefd, "DTLS 1.2" },
419     { 0xfeff, "DTLS 1.0" },
420     { 0x0100, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
421     { 0x0303, "TLS 1.2" },
422     { 0x0302, "TLS 1.1" },
423     { 0x0301, "TLS 1.0" },
424     { 0x0300, "SSL 3.0" },
425     { 0x0002, "SSL 2.0" },
426     { 0x00, NULL }
427 };
428
429 #if 0
430 /* XXX - would be used if we dissected the body of a Change Cipher Spec
431    message. */
432 const value_string ssl_31_change_cipher_spec[] = {
433     { 1, "Change Cipher Spec" },
434     { 0x00, NULL }
435 };
436 #endif
437
438 const value_string ssl_31_alert_level[] = {
439     { 1, "Warning" },
440     { 2, "Fatal" },
441     { 0x00, NULL }
442 };
443
444 const value_string ssl_31_alert_description[] = {
445     {   0,  "Close Notify" },
446     {  10,  "Unexpected Message" },
447     {  20,  "Bad Record MAC" },
448     {  21,  "Decryption Failed" },
449     {  22,  "Record Overflow" },
450     {  30,  "Decompression Failure" },
451     {  40,  "Handshake Failure" },
452     {  41,  "No Certificate" },
453     {  42,  "Bad Certificate" },
454     {  43,  "Unsupported Certificate" },
455     {  44,  "Certificate Revoked" },
456     {  45,  "Certificate Expired" },
457     {  46,  "Certificate Unknown" },
458     {  47,  "Illegal Parameter" },
459     {  48,  "Unknown CA" },
460     {  49,  "Access Denied" },
461     {  50,  "Decode Error" },
462     {  51,  "Decrypt Error" },
463     {  60,  "Export Restriction" },
464     {  70,  "Protocol Version" },
465     {  71,  "Insufficient Security" },
466     {  80,  "Internal Error" },
467     {  90,  "User Canceled" },
468     { 100, "No Renegotiation" },
469     { 110, "Unsupported Extension" },
470     { 111, "Certificate Unobtainable" },
471     { 112, "Unrecognized Name" },
472     { 113, "Bad Certificate Status Response" },
473     { 114, "Bad Certificate Hash Value" },
474     { 115, "Unknown PSK Identity" },
475     { 0x00, NULL }
476 };
477
478 const value_string ssl_31_handshake_type[] = {
479     { SSL_HND_HELLO_REQUEST,     "Hello Request" },
480     { SSL_HND_CLIENT_HELLO,      "Client Hello" },
481     { SSL_HND_SERVER_HELLO,      "Server Hello" },
482     { SSL_HND_HELLO_VERIFY_REQUEST, "Hello Verify Request"},
483     { SSL_HND_NEWSESSION_TICKET, "New Session Ticket" },
484     { SSL_HND_CERTIFICATE,       "Certificate" },
485     { SSL_HND_SERVER_KEY_EXCHG,  "Server Key Exchange" },
486     { SSL_HND_CERT_REQUEST,      "Certificate Request" },
487     { SSL_HND_SVR_HELLO_DONE,    "Server Hello Done" },
488     { SSL_HND_CERT_VERIFY,       "Certificate Verify" },
489     { SSL_HND_CLIENT_KEY_EXCHG,  "Client Key Exchange" },
490     { SSL_HND_FINISHED,          "Finished" },
491     { SSL_HND_CERT_URL,          "Client Certificate URL" },
492     { SSL_HND_CERT_STATUS,       "Certificate Status" },
493     { SSL_HND_ENCRYPTED_EXTS,    "Encrypted Extensions" },
494     { 0x00, NULL }
495 };
496
497 const value_string tls_heartbeat_type[] = {
498     { 1, "Request" },
499     { 2, "Response" },
500     { 0x00, NULL }
501 };
502
503 const value_string tls_heartbeat_mode[] = {
504     { 1, "Peer allowed to send requests" },
505     { 2, "Peer not allowed to send requests" },
506     { 0x00, NULL }
507 };
508
509 const value_string ssl_31_compression_method[] = {
510     {  0, "null" },
511     {  1, "DEFLATE" },
512     { 64, "LZS" },
513     { 0x00, NULL }
514 };
515
516 #if 0
517 /* XXX - would be used if we dissected a Signature, as would be
518    seen in a server key exchange or certificate verify message. */
519 const value_string ssl_31_key_exchange_algorithm[] = {
520     { 0, "RSA" },
521     { 1, "Diffie Hellman" },
522     { 0x00, NULL }
523 };
524
525 const value_string ssl_31_signature_algorithm[] = {
526     { 0, "Anonymous" },
527     { 1, "RSA" },
528     { 2, "DSA" },
529     { 0x00, NULL }
530 };
531 #endif
532
533 const value_string ssl_31_client_certificate_type[] = {
534     { 1, "RSA Sign" },
535     { 2, "DSS Sign" },
536     { 3, "RSA Fixed DH" },
537     { 4, "DSS Fixed DH" },
538     /* GOST certificate types */
539     /* Section 3.5 of draft-chudov-cryptopro-cptls-04 */
540     { 21, "GOST R 34.10-94" },
541     { 22, "GOST R 34.10-2001" },
542     /* END GOST certificate types */
543     { 64, "ECDSA Sign" },
544     { 65, "RSA Fixed ECDH" },
545     { 66, "ECDSA Fixed ECDH" },
546     { 0x00, NULL }
547 };
548
549 #if 0
550 /* XXX - would be used if we dissected exchange keys, as would be
551    seen in a client key exchange message. */
552 const value_string ssl_31_public_value_encoding[] = {
553     { 0, "Implicit" },
554     { 1, "Explicit" },
555     { 0x00, NULL }
556 };
557 #endif
558
559 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
560 /* Note: sorted by ascending value so value_string_ext fcns can do a binary search */
561 static const value_string ssl_31_ciphersuite[] = {
562     /* RFC 2246, RFC 4346, RFC 5246 */
563     { 0x0000, "TLS_NULL_WITH_NULL_NULL" },
564     { 0x0001, "TLS_RSA_WITH_NULL_MD5" },
565     { 0x0002, "TLS_RSA_WITH_NULL_SHA" },
566     { 0x0003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
567     { 0x0004, "TLS_RSA_WITH_RC4_128_MD5" },
568     { 0x0005, "TLS_RSA_WITH_RC4_128_SHA" },
569     { 0x0006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
570     { 0x0007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
571     { 0x0008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
572     { 0x0009, "TLS_RSA_WITH_DES_CBC_SHA" },
573     { 0x000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
574     { 0x000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
575     { 0x000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
576     { 0x000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
577     { 0x000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
578     { 0x000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
579     { 0x0010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
580     { 0x0011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
581     { 0x0012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
582     { 0x0013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
583     { 0x0014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
584     { 0x0015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
585     { 0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
586     { 0x0017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
587     { 0x0018, "TLS_DH_anon_WITH_RC4_128_MD5" },
588     { 0x0019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
589     { 0x001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
590     { 0x001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
591
592     { 0x001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
593     { 0x001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
594 #if 0 /* Because it clashes with KRB5, is never used any more, and is safe
595          to remove according to David Hopwood <david.hopwood@zetnet.co.uk>
596          of the ietf-tls list */
597     { 0x001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
598 #endif
599
600     /* RFC 2712 */
601     { 0x001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
602     { 0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
603     { 0x0020, "TLS_KRB5_WITH_RC4_128_SHA" },
604     { 0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
605     { 0x0022, "TLS_KRB5_WITH_DES_CBC_MD5" },
606     { 0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
607     { 0x0024, "TLS_KRB5_WITH_RC4_128_MD5" },
608     { 0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
609     { 0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
610     { 0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
611     { 0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
612     { 0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
613     { 0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
614     { 0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
615
616     /* RFC 4785 */
617     { 0x002C, "TLS_PSK_WITH_NULL_SHA" },
618     { 0x002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
619     { 0x002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
620
621     /* RFC 5246 */
622     { 0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA" },
623     { 0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
624     { 0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
625     { 0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
626     { 0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
627     { 0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
628     { 0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
629     { 0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
630     { 0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
631     { 0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
632     { 0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
633     { 0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
634     { 0x003B, "TLS_RSA_WITH_NULL_SHA256" },
635     { 0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
636     { 0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
637     { 0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
638     { 0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
639     { 0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
640
641     /* RFC 4132 */
642     { 0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
643     { 0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
644     { 0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
645     { 0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
646     { 0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
647     { 0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
648
649     /* 0x00,0x60-66 Reserved to avoid conflicts with widely deployed implementations  */
650     /* --- ??? --- */
651     { 0x0060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
652     { 0x0061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
653     /* draft-ietf-tls-56-bit-ciphersuites-01.txt */
654     { 0x0062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
655     { 0x0063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
656     { 0x0064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
657     { 0x0065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
658     { 0x0066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
659     /* --- ??? ---*/
660
661     { 0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
662     { 0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
663     { 0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
664     { 0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
665     { 0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
666     { 0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
667     { 0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
668
669     /* draft-chudov-cryptopro-cptls-04.txt */
670     { 0x0080,  "TLS_GOSTR341094_WITH_28147_CNT_IMIT" },
671     { 0x0081,  "TLS_GOSTR341001_WITH_28147_CNT_IMIT" },
672     { 0x0082,  "TLS_GOSTR341094_WITH_NULL_GOSTR3411" },
673     { 0x0083,  "TLS_GOSTR341001_WITH_NULL_GOSTR3411" },
674
675     /* RFC 4132 */
676     { 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
677     { 0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
678     { 0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
679     { 0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
680     { 0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
681     { 0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
682
683     /* RFC 4279 */
684     { 0x008A, "TLS_PSK_WITH_RC4_128_SHA" },
685     { 0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
686     { 0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
687     { 0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
688     { 0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
689     { 0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
690     { 0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
691     { 0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
692     { 0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
693     { 0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
694     { 0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
695     { 0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
696
697     /* RFC 4162 */
698     { 0x0096, "TLS_RSA_WITH_SEED_CBC_SHA" },
699     { 0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
700     { 0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
701     { 0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
702     { 0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
703     { 0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
704
705     /* RFC 5288 */
706     { 0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
707     { 0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
708     { 0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
709     { 0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
710     { 0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
711     { 0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
712     { 0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
713     { 0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
714     { 0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
715     { 0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
716     { 0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
717     { 0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
718
719     /* RFC 5487 */
720     { 0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
721     { 0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
722     { 0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
723     { 0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
724     { 0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
725     { 0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
726     { 0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
727     { 0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
728     { 0x00B0, "TLS_PSK_WITH_NULL_SHA256" },
729     { 0x00B1, "TLS_PSK_WITH_NULL_SHA384" },
730     { 0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
731     { 0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
732     { 0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
733     { 0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
734     { 0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
735     { 0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
736     { 0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
737     { 0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
738
739     /* From RFC 5932 */
740     { 0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
741     { 0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
742     { 0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
743     { 0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
744     { 0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
745     { 0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
746     { 0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
747     { 0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
748     { 0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
749     { 0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
750     { 0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
751     { 0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
752     /* 0x00,0xC6-FE Unassigned  */
753     /* From RFC 5746 */
754     { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
755     /* 0x01-BF,* Unassigned */
756     /* From RFC 4492 */
757     { 0xc001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
758     { 0xc002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
759     { 0xc003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
760     { 0xc004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
761     { 0xc005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
762     { 0xc006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
763     { 0xc007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
764     { 0xc008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
765     { 0xc009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
766     { 0xc00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
767     { 0xc00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
768     { 0xc00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
769     { 0xc00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
770     { 0xc00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
771     { 0xc00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
772     { 0xc010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
773     { 0xc011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
774     { 0xc012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
775     { 0xc013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
776     { 0xc014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
777     { 0xc015, "TLS_ECDH_anon_WITH_NULL_SHA" },
778     { 0xc016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
779     { 0xc017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
780     { 0xc018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
781     { 0xc019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
782
783     /* RFC 5054 */
784     { 0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
785     { 0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
786     { 0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
787     { 0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
788     { 0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
789     { 0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
790     { 0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
791     { 0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
792     { 0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
793
794     /* RFC 5589 */
795     { 0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
796     { 0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
797     { 0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
798     { 0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
799     { 0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
800     { 0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
801     { 0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
802     { 0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
803     { 0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
804     { 0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
805     { 0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
806     { 0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
807     { 0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
808     { 0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
809     { 0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
810     { 0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
811
812     /* RFC 5489 */
813     { 0xC033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
814     { 0xC034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
815     { 0xC035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
816     { 0xC036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
817     { 0xC037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
818     { 0xC038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
819     { 0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
820     { 0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
821     { 0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
822
823     /* RFC 6209 */
824     { 0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256" },
825     { 0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384" },
826     { 0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256" },
827     { 0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384" },
828     { 0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256" },
829     { 0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384" },
830     { 0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256" },
831     { 0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384" },
832     { 0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256" },
833     { 0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384" },
834     { 0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256" },
835     { 0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384" },
836     { 0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256" },
837     { 0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384" },
838     { 0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256" },
839     { 0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384" },
840     { 0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256" },
841     { 0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384" },
842     { 0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256" },
843     { 0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384" },
844     { 0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256" },
845     { 0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384" },
846     { 0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256" },
847     { 0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384" },
848     { 0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256" },
849     { 0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384" },
850     { 0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256" },
851     { 0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384" },
852     { 0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256" },
853     { 0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384" },
854     { 0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256" },
855     { 0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384" },
856     { 0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256" },
857     { 0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384" },
858     { 0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256" },
859     { 0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384" },
860     { 0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256" },
861     { 0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384" },
862     { 0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256" },
863     { 0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384" },
864     { 0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256" },
865     { 0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384" },
866     { 0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256" },
867     { 0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384" },
868     { 0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256" },
869     { 0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384" },
870     { 0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256" },
871     { 0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384" },
872     { 0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256" },
873     { 0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384" },
874     { 0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256" },
875     { 0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384" },
876     { 0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256" },
877     { 0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384" },
878
879     /* RFC 6367 */
880     { 0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
881     { 0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
882     { 0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
883     { 0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
884     { 0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
885     { 0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
886     { 0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
887     { 0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
888     { 0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
889     { 0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
890     { 0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
891     { 0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
892     { 0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
893     { 0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
894     { 0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
895     { 0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
896     { 0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
897     { 0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
898     { 0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256" },
899     { 0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384" },
900     { 0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
901     { 0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
902     { 0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
903     { 0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
904     { 0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
905     { 0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
906     { 0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
907     { 0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
908     { 0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
909     { 0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
910     { 0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
911     { 0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
912     { 0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
913     { 0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
914     { 0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
915     { 0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
916     { 0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
917     { 0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
918     { 0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
919     { 0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
920     { 0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
921     { 0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
922
923     /* RFC 6655 */
924     { 0xC09C, "TLS_RSA_WITH_AES_128_CCM" },
925     { 0xC09D, "TLS_RSA_WITH_AES_256_CCM" },
926     { 0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM" },
927     { 0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM" },
928     { 0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8" },
929     { 0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8" },
930     { 0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8" },
931     { 0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8" },
932     { 0xC0A4, "TLS_PSK_WITH_AES_128_CCM" },
933     { 0xC0A5, "TLS_PSK_WITH_AES_256_CCM" },
934     { 0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM" },
935     { 0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM" },
936     { 0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8" },
937     { 0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8" },
938     { 0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8" },
939     { 0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8" },
940 /*
941 0xC0,0xAB-FF Unassigned
942 0xC1-FD,* Unassigned
943 0xFE,0x00-FD Unassigned
944 0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
945 0xFF,0x00-FF Reserved for Private Use [RFC5246]
946 */
947     /* these from http://www.mozilla.org/projects/
948          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
949     { 0xfefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
950     { 0xfeff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
951     { 0xffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
952     { 0xffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
953     /* note that ciphersuites 0xff00 - 0xffff are private */
954     { 0x00, NULL }
955 };
956
957 value_string_ext ssl_31_ciphersuite_ext = VALUE_STRING_EXT_INIT(ssl_31_ciphersuite);
958
959
960 const value_string pct_msg_types[] = {
961     { PCT_MSG_CLIENT_HELLO,         "Client Hello" },
962     { PCT_MSG_SERVER_HELLO,         "Server Hello" },
963     { PCT_MSG_CLIENT_MASTER_KEY,    "Client Master Key" },
964     { PCT_MSG_SERVER_VERIFY,        "Server Verify" },
965     { PCT_MSG_ERROR,                "Error" },
966     { 0x00, NULL }
967 };
968
969 const value_string pct_cipher_type[] = {
970     { PCT_CIPHER_DES, "DES" },
971     { PCT_CIPHER_IDEA, "IDEA" },
972     { PCT_CIPHER_RC2, "RC2" },
973     { PCT_CIPHER_RC4, "RC4" },
974     { PCT_CIPHER_DES_112, "DES 112 bit" },
975     { PCT_CIPHER_DES_168, "DES 168 bit" },
976     { 0x00, NULL }
977 };
978
979 const value_string pct_hash_type[] = {
980     { PCT_HASH_MD5, "MD5" },
981     { PCT_HASH_MD5_TRUNC_64, "MD5_TRUNC_64"},
982     { PCT_HASH_SHA, "SHA"},
983     { PCT_HASH_SHA_TRUNC_80, "SHA_TRUNC_80"},
984     { PCT_HASH_DES_DM, "DES_DM"},
985     { 0x00, NULL }
986 };
987
988 const value_string pct_cert_type[] = {
989     { PCT_CERT_NONE, "None" },
990     { PCT_CERT_X509, "X.509" },
991     { PCT_CERT_PKCS7, "PKCS #7" },
992     { 0x00, NULL }
993 };
994 const value_string pct_sig_type[] = {
995     { PCT_SIG_NONE, "None" },
996     { PCT_SIG_RSA_MD5, "MD5" },
997     { PCT_SIG_RSA_SHA, "RSA SHA" },
998     { PCT_SIG_DSA_SHA, "DSA SHA" },
999     { 0x00, NULL }
1000 };
1001
1002 const value_string pct_exch_type[] = {
1003     { PCT_EXCH_RSA_PKCS1, "RSA PKCS#1" },
1004     { PCT_EXCH_RSA_PKCS1_TOKEN_DES, "RSA PKCS#1 Token DES" },
1005     { PCT_EXCH_RSA_PKCS1_TOKEN_DES3, "RSA PKCS#1 Token 3DES" },
1006     { PCT_EXCH_RSA_PKCS1_TOKEN_RC2, "RSA PKCS#1 Token RC-2" },
1007     { PCT_EXCH_RSA_PKCS1_TOKEN_RC4, "RSA PKCS#1 Token RC-4" },
1008     { PCT_EXCH_DH_PKCS3, "DH PKCS#3" },
1009     { PCT_EXCH_DH_PKCS3_TOKEN_DES, "DH PKCS#3 Token DES" },
1010     { PCT_EXCH_DH_PKCS3_TOKEN_DES3, "DH PKCS#3 Token 3DES" },
1011     { PCT_EXCH_FORTEZZA_TOKEN, "Fortezza" },
1012     { 0x00, NULL }
1013 };
1014
1015 const value_string pct_error_code[] = {
1016     { PCT_ERR_BAD_CERTIFICATE, "PCT_ERR_BAD_CERTIFICATE" },
1017     { PCT_ERR_CLIENT_AUTH_FAILED, "PCT_ERR_CLIENT_AUTH_FAILE" },
1018     { PCT_ERR_ILLEGAL_MESSAGE, "PCT_ERR_ILLEGAL_MESSAGE" },
1019     { PCT_ERR_INTEGRITY_CHECK_FAILED, "PCT_ERR_INTEGRITY_CHECK_FAILED" },
1020     { PCT_ERR_SERVER_AUTH_FAILED, "PCT_ERR_SERVER_AUTH_FAILED" },
1021     { PCT_ERR_SPECS_MISMATCH, "PCT_ERR_SPECS_MISMATCH" },
1022     { 0x00, NULL }
1023 };
1024
1025 /* RFC 4366 */
1026 const value_string tls_hello_extension_types[] = {
1027     { SSL_HND_HELLO_EXT_SERVER_NAME, "server_name" }, /* RFC 3546 */
1028     { 1, "max_fragment_length" },
1029     { 2, "client_certificate_url" },
1030     { 3, "trusted_ca_keys" },
1031     { 4, "truncated_hmac" },
1032     { SSL_HND_HELLO_EXT_STATUS_REQUEST, "status_request" }, /* RFC 6066 */
1033     { 6, "user_mapping" },  /* RFC 4681 */
1034     { 7, "client_authz" },
1035     { 8, "server_authz" },
1036     { 9, "cert_type" },  /* RFC 5081 */
1037     { SSL_HND_HELLO_EXT_ELLIPTIC_CURVES, "elliptic_curves" },  /* RFC 4492 */
1038     { SSL_HND_HELLO_EXT_EC_POINT_FORMATS, "ec_point_formats" },  /* RFC 4492 */
1039     { 12, "srp" },  /* RFC 5054 */
1040     { 13, "signature_algorithms" },  /* RFC 5246 */
1041     { 14, "use_srtp" },
1042     { SSL_HND_HELLO_EXT_HEARTBEAT, "Heartbeat" },  /* RFC 6520 */
1043     { SSL_HND_HELLO_EXT_ALPN, "Application Layer Protocol Negotiation" }, /* draft-ietf-tls-applayerprotoneg-01 */
1044     { SSL_HND_HELLO_EXT_STATUS_REQUEST_V2, "status_request_v2" }, /* RFC 6961 */
1045     { 35, "SessionTicket TLS" },  /* RFC 4507 */
1046     { SSL_HND_HELLO_EXT_NPN, "next_protocol_negotiation"}, /* http://technotes.googlecode.com/git/nextprotoneg.html */
1047     { SSL_HND_HELLO_EXT_RENEG_INFO, "renegotiation_info" }, /* RFC 5746 */
1048     { 0, NULL }
1049 };
1050
1051 const value_string tls_hello_ext_server_name_type_vs[] = {
1052     { 0, "host_name" },
1053     { 0, NULL }
1054 };
1055
1056 /* RFC 5246 7.4.1.4.1 */
1057 const value_string tls_hash_algorithm[] = {
1058     { 0, "None" },
1059     { 1, "MD5" },
1060     { 2, "SHA1" },
1061     { 3, "SHA224" },
1062     { 4, "SHA256" },
1063     { 5, "SHA384" },
1064     { 6, "SHA512" },
1065     { 0, NULL }
1066 };
1067
1068 const value_string tls_signature_algorithm[] = {
1069     { 0, "Anonymous" },
1070     { 1, "RSA" },
1071     { 2, "DSA" },
1072     { 3, "ECDSA" },
1073     { 0, NULL }
1074 };
1075
1076 /* RFC 6091 3.1 */
1077 const value_string tls_certificate_type[] = {
1078     { 0, "X.509" },
1079     { 1, "OpenPGP" },
1080     { 0, NULL }
1081 };
1082
1083 const value_string tls_cert_chain_type[] = {
1084     { SSL_HND_CERT_URL_TYPE_INDIVIDUAL_CERT,    "Individual Certificates" },
1085     { SSL_HND_CERT_URL_TYPE_PKIPATH,            "PKI Path" },
1086     { 0, NULL }
1087 };
1088
1089 const value_string tls_cert_status_type[] = {
1090     { SSL_HND_CERT_STATUS_TYPE_OCSP,            "OCSP" },
1091     { SSL_HND_CERT_STATUS_TYPE_OCSP_MULTI,      "OCSP Multi" },
1092     { 0, NULL }
1093 };
1094
1095 /* we keep this internal to packet-ssl-utils, as there should be
1096    no need to access it any other way.
1097
1098    This also allows us to hide the dependency on zlib.
1099 */
1100 struct _SslDecompress {
1101     gint compression;
1102 #ifdef HAVE_LIBZ
1103     z_stream istream;
1104 #endif
1105 };
1106
1107 /* To assist in parsing client/server key exchange messages
1108    0 indicates unknown */
1109 gint ssl_get_keyex_alg(gint cipher)
1110 {
1111     switch(cipher) {
1112     case 0x0001:
1113     case 0x0002:
1114     case 0x0003:
1115     case 0x0004:
1116     case 0x0005:
1117     case 0x0006:
1118     case 0x0007:
1119     case 0x0008:
1120     case 0x0009:
1121     case 0x000a:
1122     case 0x002f:
1123     case 0x0035:
1124     case 0x003b:
1125     case 0x003c:
1126     case 0x003d:
1127     case 0x0041:
1128     case 0x0060:
1129     case 0x0061:
1130     case 0x0062:
1131     case 0x0064:
1132     case 0x0084:
1133     case 0x0096:
1134     case 0x009c:
1135     case 0x009d:
1136     case 0x00ba:
1137     case 0x00c0:
1138     case 0xfefe:
1139     case 0xfeff:
1140     case 0xffe0:
1141     case 0xffe1:
1142         return KEX_RSA;
1143     case 0x000b:
1144     case 0x000c:
1145     case 0x000d:
1146     case 0x000e:
1147     case 0x000f:
1148     case 0x0010:
1149     case 0x0011:
1150     case 0x0012:
1151     case 0x0013:
1152     case 0x0014:
1153     case 0x0015:
1154     case 0x0016:
1155     case 0x0017:
1156     case 0x0018:
1157     case 0x0019:
1158     case 0x001a:
1159     case 0x001b:
1160     case 0x002d:
1161     case 0x0030:
1162     case 0x0031:
1163     case 0x0032:
1164     case 0x0033:
1165     case 0x0034:
1166     case 0x0036:
1167     case 0x0037:
1168     case 0x0038:
1169     case 0x0039:
1170     case 0x003a:
1171     case 0x003e:
1172     case 0x003f:
1173     case 0x0040:
1174     case 0x0042:
1175     case 0x0043:
1176     case 0x0044:
1177     case 0x0045:
1178     case 0x0046:
1179     case 0x0063:
1180     case 0x0065:
1181     case 0x0066:
1182     case 0x0067:
1183     case 0x0068:
1184     case 0x0069:
1185     case 0x006a:
1186     case 0x006b:
1187     case 0x006c:
1188     case 0x006d:
1189     case 0x0085:
1190     case 0x0086:
1191     case 0x0087:
1192     case 0x0088:
1193     case 0x0089:
1194     case 0x008e:
1195     case 0x008f:
1196     case 0x0090:
1197     case 0x0091:
1198     case 0x0097:
1199     case 0x0098:
1200     case 0x0099:
1201     case 0x009a:
1202     case 0x009b:
1203     case 0x009e:
1204     case 0x009f:
1205     case 0x00a0:
1206     case 0x00a1:
1207     case 0x00a2:
1208     case 0x00a3:
1209     case 0x00a4:
1210     case 0x00a5:
1211     case 0x00a6:
1212     case 0x00a7:
1213     case 0x00aa:
1214     case 0x00ab:
1215     case 0x00b2:
1216     case 0x00b3:
1217     case 0x00b4:
1218     case 0x00b5:
1219     case 0x00bb:
1220     case 0x00bc:
1221     case 0x00bd:
1222     case 0x00be:
1223     case 0x00bf:
1224     case 0x00c1:
1225     case 0x00c2:
1226     case 0x00c3:
1227     case 0x00c4:
1228     case 0x00c5:
1229         return KEX_DH;
1230     case 0xc001:
1231     case 0xc002:
1232     case 0xc003:
1233     case 0xc004:
1234     case 0xc005:
1235     case 0xc006:
1236     case 0xc007:
1237     case 0xc008:
1238     case 0xc009:
1239     case 0xc00a:
1240     case 0xc00b:
1241     case 0xc00c:
1242     case 0xc00d:
1243     case 0xc00e:
1244     case 0xc00f:
1245     case 0xc010:
1246     case 0xc011:
1247     case 0xc012:
1248     case 0xc013:
1249     case 0xc014:
1250     case 0xc015:
1251     case 0xc016:
1252     case 0xc017:
1253     case 0xc018:
1254     case 0xc019:
1255     case 0xc023:
1256     case 0xc024:
1257     case 0xc025:
1258     case 0xc026:
1259     case 0xc027:
1260     case 0xc028:
1261     case 0xc029:
1262     case 0xc02a:
1263     case 0xc02b:
1264     case 0xc02c:
1265     case 0xc02d:
1266     case 0xc02e:
1267     case 0xc02f:
1268     case 0xc030:
1269     case 0xc031:
1270     case 0xc032:
1271     case 0xc033:
1272     case 0xc034:
1273     case 0xc035:
1274     case 0xc036:
1275     case 0xc037:
1276     case 0xc038:
1277     case 0xc039:
1278     case 0xc03a:
1279     case 0xc03b:
1280         return KEX_ECDH;
1281     case 0x002C:
1282     case 0x008A:
1283     case 0x008B:
1284     case 0x008C:
1285     case 0x008D:
1286     case 0x00A8:
1287     case 0x00A9:
1288     case 0x00AE:
1289     case 0x00AF:
1290     case 0x00B0:
1291     case 0x00B1:
1292     case 0xC064:
1293     case 0xC065:
1294     case 0xC06A:
1295     case 0xC06B:
1296     case 0xC08E:
1297     case 0xC08F:
1298     case 0xC094:
1299     case 0xC095:
1300     case 0xC0A4:
1301     case 0xC0A5:
1302     case 0xC0A8:
1303     case 0xC0A9:
1304     case 0xC0AA:
1305     case 0xC0AB:
1306         return KEX_PSK;
1307     case 0x002E:
1308     case 0x0092:
1309     case 0x0093:
1310     case 0x0094:
1311     case 0x0095:
1312     case 0x00AC:
1313     case 0x00AD:
1314     case 0x00B6:
1315     case 0x00B7:
1316     case 0x00B8:
1317     case 0x00B9:
1318     case 0xC068:
1319     case 0xC069:
1320     case 0xC06E:
1321     case 0xC06F:
1322     case 0xC092:
1323     case 0xC093:
1324     case 0xC098:
1325     case 0xC099:
1326         return KEX_RSA_PSK;
1327     default:
1328         break;
1329     }
1330
1331     return 0;
1332 }
1333
1334
1335
1336
1337 static gint
1338 ssl_data_alloc(StringInfo* str, size_t len)
1339 {
1340     str->data = (guchar *)g_malloc(len);
1341     /* the allocator can return a null pointer for a size equal to 0,
1342      * and that must be allowed */
1343     if (len > 0 && !str->data)
1344         return -1;
1345     str->data_len = (guint) len;
1346     return 0;
1347 }
1348
1349 void
1350 ssl_data_set(StringInfo* str, const guchar* data, guint len)
1351 {
1352     memcpy(str->data, data, len);
1353     str->data_len = len;
1354 }
1355
1356
1357 static guint8
1358 from_hex_char(gchar c) {
1359     if ((c >= '0') && (c <= '9'))
1360         return c - '0';
1361     if ((c >= 'A') && (c <= 'F'))
1362         return c - 'A' + 10;
1363     if ((c >= 'a') && (c <= 'f'))
1364         return c - 'a' + 10;
1365     return 16;
1366 }
1367
1368 /* from_hex converts |hex_len| bytes of hex data from |in| and sets |*out| to
1369  * the result. |out->data| will be allocated using se_alloc. Returns TRUE on
1370  * success. */
1371 static gboolean from_hex(StringInfo* out, const char* in, gsize hex_len) {
1372     gsize i;
1373
1374     if (hex_len & 1)
1375         return FALSE;
1376
1377     out->data_len = (guint)hex_len/2;
1378     out->data = (guchar *)wmem_alloc(wmem_file_scope(), out->data_len);
1379     for (i = 0; i < out->data_len; i++) {
1380         guint8 a = from_hex_char(in[i*2]);
1381         guint8 b = from_hex_char(in[i*2 + 1]);
1382         if (a == 16 || b == 16)
1383             return FALSE;
1384         out->data[i] = a << 4 | b;
1385     }
1386     return TRUE;
1387 }
1388
1389
1390 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
1391
1392 /* hmac abstraction layer */
1393 #define SSL_HMAC gcry_md_hd_t
1394
1395 static inline gint
1396 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
1397 {
1398     gcry_error_t  err;
1399     const char   *err_str, *err_src;
1400
1401     err = gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
1402     if (err != 0) {
1403         err_str = gcry_strerror(err);
1404         err_src = gcry_strsource(err);
1405         ssl_debug_printf("ssl_hmac_init(): gcry_md_open failed %s/%s", err_str, err_src);
1406         return -1;
1407     }
1408     gcry_md_setkey (*(md), key, len);
1409     return 0;
1410 }
1411 static inline void
1412 ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
1413 {
1414     gcry_md_write(*(md), data, len);
1415 }
1416 static inline void
1417 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
1418 {
1419     gint  algo;
1420     guint len;
1421
1422     algo = gcry_md_get_algo (*(md));
1423     len  = gcry_md_get_algo_dlen(algo);
1424     DISSECTOR_ASSERT(len <= *datalen);
1425     memcpy(data, gcry_md_read(*(md), algo), len);
1426     *datalen = len;
1427 }
1428 static inline void
1429 ssl_hmac_cleanup(SSL_HMAC* md)
1430 {
1431     gcry_md_close(*(md));
1432 }
1433
1434 /* memory digest abstraction layer*/
1435 #define SSL_MD gcry_md_hd_t
1436
1437 static inline gint
1438 ssl_md_init(SSL_MD* md, gint algo)
1439 {
1440     gcry_error_t  err;
1441     const char   *err_str, *err_src;
1442     err = gcry_md_open(md,algo, 0);
1443     if (err != 0) {
1444         err_str = gcry_strerror(err);
1445         err_src = gcry_strsource(err);
1446         ssl_debug_printf("ssl_md_init(): gcry_md_open failed %s/%s", err_str, err_src);
1447         return -1;
1448     }
1449     return 0;
1450 }
1451 static inline void
1452 ssl_md_update(SSL_MD* md, guchar* data, gint len)
1453 {
1454     gcry_md_write(*(md), data, len);
1455 }
1456 static inline void
1457 ssl_md_final(SSL_MD* md, guchar* data, guint* datalen)
1458 {
1459     gint algo;
1460     gint len;
1461     algo = gcry_md_get_algo (*(md));
1462     len = gcry_md_get_algo_dlen (algo);
1463     memcpy(data, gcry_md_read(*(md),  algo), len);
1464     *datalen = len;
1465 }
1466 static inline void
1467 ssl_md_cleanup(SSL_MD* md)
1468 {
1469     gcry_md_close(*(md));
1470 }
1471
1472 /* md5 /sha abstraction layer */
1473 #define SSL_SHA_CTX gcry_md_hd_t
1474 #define SSL_MD5_CTX gcry_md_hd_t
1475
1476 static inline void
1477 ssl_sha_init(SSL_SHA_CTX* md)
1478 {
1479     gcry_md_open(md,GCRY_MD_SHA1, 0);
1480 }
1481 static inline void
1482 ssl_sha_update(SSL_SHA_CTX* md, guchar* data, gint len)
1483 {
1484     gcry_md_write(*(md), data, len);
1485 }
1486 static inline void
1487 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
1488 {
1489     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_SHA1),
1490            gcry_md_get_algo_dlen(GCRY_MD_SHA1));
1491 }
1492 static inline void
1493 ssl_sha_cleanup(SSL_SHA_CTX* md)
1494 {
1495     gcry_md_close(*(md));
1496 }
1497
1498 static inline gint
1499 ssl_md5_init(SSL_MD5_CTX* md)
1500 {
1501     return gcry_md_open(md,GCRY_MD_MD5, 0);
1502 }
1503 static inline void
1504 ssl_md5_update(SSL_MD5_CTX* md, guchar* data, gint len)
1505 {
1506     gcry_md_write(*(md), data, len);
1507 }
1508 static inline void
1509 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
1510 {
1511     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_MD5),
1512            gcry_md_get_algo_dlen(GCRY_MD_MD5));
1513 }
1514 static inline void
1515 ssl_md5_cleanup(SSL_MD5_CTX* md)
1516 {
1517     gcry_md_close(*(md));
1518 }
1519
1520 gint
1521 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
1522 {
1523     gint ret;
1524 #if 0
1525     guchar *ivp;
1526     gint i;
1527     gcry_cipher_hd_t c;
1528     c=(gcry_cipher_hd_t)*cipher;
1529 #endif
1530     ssl_debug_printf("--------------------------------------------------------------------");
1531 #if 0
1532     for(ivp=c->iv,i=0; i < iv_len; i++ )
1533         {
1534         ssl_debug_printf("%d ",ivp[i]);
1535         i++;
1536         }
1537 #endif
1538     ssl_debug_printf("--------------------------------------------------------------------");
1539     ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
1540 #if 0
1541     for(ivp=c->iv,i=0; i < iv_len; i++ )
1542         {
1543         ssl_debug_printf("%d ",ivp[i]);
1544         i++;
1545         }
1546 #endif
1547     ssl_debug_printf("--------------------------------------------------------------------");
1548     return ret;
1549 }
1550 /* stream cipher abstraction layer*/
1551 static gint
1552 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
1553         guchar* iv, gint mode)
1554 {
1555     gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC,GCRY_CIPHER_MODE_CTR,GCRY_CIPHER_MODE_CTR,GCRY_CIPHER_MODE_CTR};
1556     gint err;
1557     if (algo == -1) {
1558         /* NULL mode */
1559         *(cipher) = (gcry_cipher_hd_t)-1;
1560         return 0;
1561     }
1562     err = gcry_cipher_open(cipher, algo, gcry_modes[mode], 0);
1563     if (err !=0)
1564         return  -1;
1565     err = gcry_cipher_setkey(*(cipher), sk, gcry_cipher_get_algo_keylen (algo));
1566     if (err != 0)
1567         return -1;
1568     err = gcry_cipher_setiv(*(cipher), iv, gcry_cipher_get_algo_blklen (algo));
1569     if (err != 0)
1570         return -1;
1571     return 0;
1572 }
1573 static inline gint
1574 ssl_cipher_decrypt(gcry_cipher_hd_t *cipher, guchar * out, gint outl,
1575                    const guchar * in, gint inl)
1576 {
1577     if ((*cipher) == (gcry_cipher_hd_t)-1)
1578     {
1579         if (in && inl)
1580             memcpy(out, in, outl < inl ? outl : inl);
1581         return 0;
1582     }
1583     return gcry_cipher_decrypt ( *(cipher), out, outl, in, inl);
1584 }
1585 static inline gint
1586 ssl_get_digest_by_name(const gchar*name)
1587 {
1588     return gcry_md_map_name(name);
1589 }
1590 static inline gint
1591 ssl_get_cipher_by_name(const gchar* name)
1592 {
1593     return gcry_cipher_map_name(name);
1594 }
1595
1596 static inline void
1597 ssl_cipher_cleanup(gcry_cipher_hd_t *cipher)
1598 {
1599     if ((*cipher) != (gcry_cipher_hd_t)-1)
1600         gcry_cipher_close(*cipher);
1601     *cipher = NULL;
1602 }
1603
1604 #if 0
1605 /* private key abstraction layer */
1606 static inline gint
1607 ssl_get_key_len(SSL_PRIVATE_KEY* pk) {return gcry_pk_get_nbits (pk); }
1608 #endif
1609
1610 gcry_err_code_t
1611 _gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
1612                    gcry_mpi_t *skey, gint flags);
1613
1614 #define PUBKEY_FLAG_NO_BLINDING (1 << 0)
1615 #if 0
1616 static const gchar*
1617 ssl_private_key_to_str(SSL_PRIVATE_KEY* pk)
1618 {
1619     const gchar *str = "NULL";
1620     size_t       n;
1621     gchar       *buf;
1622
1623     if (!pk) return str;
1624 #ifndef SSL_FAST
1625     n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, NULL, 0);
1626     buf = (gchar *)ep_alloc(n);
1627     /*n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, buf, n);*/
1628     str = buf;
1629 #else /* SSL_FAST */
1630     str = "TO DO: dump mpi gcry_mpi_print()";
1631 #endif /* SSL_FAST */
1632     return str;
1633 }
1634 #endif
1635
1636 /* decrypt data with private key. Store decrypted data directly into input
1637  * buffer */
1638 static int
1639 ssl_private_decrypt(guint len, guchar* encr_data, SSL_PRIVATE_KEY* pk)
1640 {
1641     gint        rc;
1642     size_t      decr_len;
1643     gcry_sexp_t s_data, s_plain;
1644     gcry_mpi_t  encr_mpi;
1645     size_t      i, encr_len;
1646     guchar*     decr_data_ptr;
1647     gcry_mpi_t  text;
1648     decr_len = 0;
1649     encr_len = len;
1650     text     = NULL;
1651
1652     /* build up a mpi rappresentation for encrypted data */
1653     rc = gcry_mpi_scan(&encr_mpi, GCRYMPI_FMT_USG,encr_data, encr_len, &encr_len);
1654     if (rc != 0 ) {
1655         ssl_debug_printf("pcry_private_decrypt: can't convert encr_data to mpi (size %d):%s\n",
1656             len, gcry_strerror(rc));
1657         return 0;
1658     }
1659
1660     /*ssl_debug_printf("pcry_private_decrypt: pk=%s\n", ssl_private_key_to_str(pk));*/
1661
1662 #ifndef SSL_FAST
1663     /* put the data into a simple list */
1664     rc = gcry_sexp_build(&s_data, NULL, "(enc-val(rsa(a%m)))", encr_mpi);
1665     if (rc != 0) {
1666         ssl_debug_printf("pcry_private_decrypt: can't build encr_sexp:%s \n",
1667              gcry_strerror(rc));
1668         return 0;
1669     }
1670
1671     /* pass it to libgcrypt */
1672     rc = gcry_pk_decrypt(&s_plain, s_data, pk);
1673     gcry_sexp_release(s_data);
1674     if (rc != 0)
1675     {
1676         ssl_debug_printf("pcry_private_decrypt: can't decrypt key:%s\n",
1677             gcry_strerror(rc));
1678         goto out;
1679     }
1680
1681     /* convert plain text sexp to mpi format */
1682     text = gcry_sexp_nth_mpi(s_plain, 0, 0);
1683
1684     /* compute size requested for plaintext buffer */
1685     decr_len = len;
1686     if (gcry_mpi_print(GCRYMPI_FMT_USG, NULL, decr_len, &decr_len, text) != 0) {
1687         ssl_debug_printf("pcry_private_decrypt: can't compute decr size:%s\n",
1688             gcry_strerror(rc));
1689         decr_len = 0;
1690         goto out;
1691     }
1692
1693     /* sanity check on out buffer */
1694     if (decr_len > len) {
1695         ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%" G_GSIZE_MODIFIER "u max %d)\n",
1696             decr_len, len);
1697         return 0;
1698     }
1699
1700     /* write plain text to encrypted data buffer */
1701     decr_data_ptr = encr_data;
1702     if (gcry_mpi_print( GCRYMPI_FMT_USG, decr_data_ptr, decr_len, &decr_len,
1703             text) != 0) {
1704         ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %" G_GSIZE_MODIFIER "u):%s\n",
1705             decr_len, gcry_strerror(rc));
1706         g_free(decr_data_ptr);
1707         decr_len = 0;
1708         goto out;
1709     }
1710
1711     /* strip the padding*/
1712     rc = 0;
1713     for (i = 1; i < decr_len; i++) {
1714         if (decr_data_ptr[i] == 0) {
1715             rc = (gint) i+1;
1716             break;
1717         }
1718     }
1719
1720     ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %" G_GSIZE_MODIFIER "u\n",
1721         rc, decr_len);
1722     ssl_print_data("decrypted_unstrip_pre_master", decr_data_ptr, decr_len);
1723     memmove(decr_data_ptr, &decr_data_ptr[rc], decr_len - rc);
1724     decr_len -= rc;
1725
1726 out:
1727     gcry_sexp_release(s_plain);
1728 #else /* SSL_FAST */
1729     rc = _gcry_rsa_decrypt(0, &text,  &encr_mpi, pk,0);
1730     gcry_mpi_print( GCRYMPI_FMT_USG, 0, 0, &decr_len, text);
1731
1732     /* sanity check on out buffer */
1733     if (decr_len > len) {
1734         ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%d max %d)\n",
1735             decr_len, len);
1736         return 0;
1737     }
1738
1739     /* write plain text to newly allocated buffer */
1740     decr_data_ptr = encr_data;
1741     if (gcry_mpi_print( GCRYMPI_FMT_USG, decr_data_ptr, decr_len, &decr_len,
1742             text) != 0) {
1743         ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %d):%s\n",
1744             decr_len, gcry_strerror(rc));
1745         return 0;
1746     }
1747
1748     /* strip the padding*/
1749     rc = 0;
1750     for (i = 1; i < decr_len; i++) {
1751         if (decr_data_ptr[i] == 0) {
1752             rc = i+1;
1753             break;
1754         }
1755     }
1756
1757     ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %d\n",
1758         rc, decr_len);
1759     ssl_print_data("decrypted_unstrip_pre_master", decr_data_ptr, decr_len);
1760     memmove(decr_data_ptr, &decr_data_ptr[rc], decr_len - rc);
1761     decr_len -= rc;
1762 #endif /* SSL_FAST */
1763     gcry_mpi_release(text);
1764     return (int) decr_len;
1765 }
1766
1767 /* stringinfo interface */
1768 static gint
1769 ssl_data_realloc(StringInfo* str, guint len)
1770 {
1771     str->data = (guchar *)g_realloc(str->data, len);
1772     if (!str->data)
1773         return -1;
1774     str->data_len = len;
1775     return 0;
1776 }
1777
1778 static gint
1779 ssl_data_copy(StringInfo* dst, StringInfo* src)
1780 {
1781     if (dst->data_len < src->data_len) {
1782       if (ssl_data_realloc(dst, src->data_len))
1783         return -1;
1784     }
1785     memcpy(dst->data, src->data, src->data_len);
1786     dst->data_len = src->data_len;
1787     return 0;
1788 }
1789
1790 static const SslDigestAlgo digests[]={
1791     {"MD5",     16},
1792     {"SHA1",    20},
1793     {"SHA256",  32},
1794     {"SHA384",  48},
1795     {"Not Applicable",  0},
1796 };
1797
1798 #define DIGEST_MAX_SIZE 48
1799
1800 /* get index digest index */
1801 static const SslDigestAlgo *
1802 ssl_cipher_suite_dig(SslCipherSuite *cs) {
1803     return &digests[cs->dig - DIG_MD5];
1804 }
1805
1806 static const gchar *ciphers[]={
1807     "DES",
1808     "3DES",
1809     "ARCFOUR", /* libgcrypt does not support rc4, but this should be 100% compatible*/
1810     "RFC2268_128", /* libgcrypt name for RC2 with a 128-bit key */
1811     "IDEA",
1812     "AES",
1813     "AES256",
1814     "CAMELLIA128",
1815     "CAMELLIA256",
1816     "SEED",
1817     "*UNKNOWN*"
1818 };
1819
1820 static SslCipherSuite cipher_suites[]={
1821     {0x0001,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_MD5 */
1822     {0x0002,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA */
1823     {0x0003,KEX_RSA,    ENC_RC4,         1,128, 40,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 */
1824     {0x0004,KEX_RSA,    ENC_RC4,         1,128,128,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_MD5 */
1825     {0x0005,KEX_RSA,    ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_SHA */
1826     {0x0006,KEX_RSA,    ENC_RC2,         8,128, 40,DIG_MD5,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 */
1827     {0x0007,KEX_RSA,    ENC_IDEA,        8,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_IDEA_CBC_SHA */
1828     {0x0008,KEX_RSA,    ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA */
1829     {0x0009,KEX_RSA,    ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_DES_CBC_SHA */
1830     {0x000A,KEX_RSA,    ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_3DES_EDE_CBC_SHA */
1831     {0x000B,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA */
1832     {0x000C,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_DES_CBC_SHA */
1833     {0x000D,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA */
1834     {0x000E,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA */
1835     {0x000F,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_DES_CBC_SHA */
1836     {0x0010,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA */
1837     {0x0011,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA */
1838     {0x0012,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_DES_CBC_SHA */
1839     {0x0013,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA */
1840     {0x0014,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA */
1841     {0x0015,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_DES_CBC_SHA */
1842     {0x0016,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */
1843     {0x0017,KEX_DH,     ENC_RC4,         1,128, 40,DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 */
1844     {0x0018,KEX_DH,     ENC_RC4,         1,128,128,DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_WITH_RC4_128_MD5 */
1845     {0x0019,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA */
1846     {0x001A,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_DES_CBC_SHA */
1847     {0x001B,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_3DES_EDE_CBC_SHA */
1848     {0x002F,KEX_RSA,    ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA */
1849     {0x0030,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA */
1850     {0x0031,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA */
1851     {0x0032,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA */
1852     {0x0033,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
1853     {0x0034,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA */
1854     {0x0035,KEX_RSA,    ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA */
1855     {0x0036,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA */
1856     {0x0037,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA */
1857     {0x0038,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA */
1858     {0x0039,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */
1859     {0x003A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA */
1860     {0x003B,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA256 */
1861     {0x003C,KEX_RSA,    ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA256 */
1862     {0x003D,KEX_RSA,    ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
1863     {0x003E,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA256 */
1864     {0x003F,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA256 */
1865     {0x0040,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 */
1866     {0x0041,KEX_RSA,    ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */
1867     {0x0042,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA */
1868     {0x0043,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA */
1869     {0x0044,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA */
1870     {0x0045,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */
1871     {0x0046,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA */
1872     {0x0060,KEX_RSA,    ENC_RC4,         1,128, 56,DIG_MD5,    MODE_STREAM},
1873     {0x0061,KEX_RSA,    ENC_RC2,         1,128, 56,DIG_MD5,    MODE_STREAM},
1874     {0x0062,KEX_RSA,    ENC_DES,         8, 64, 56,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA */
1875     {0x0063,KEX_DH,     ENC_DES,         8, 64, 56,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA */
1876     {0x0064,KEX_RSA,    ENC_RC4,         1,128, 56,DIG_SHA,    MODE_STREAM},
1877     {0x0065,KEX_DH,     ENC_RC4,         1,128, 56,DIG_SHA,    MODE_STREAM},
1878     {0x0066,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},
1879     {0x0067,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */
1880     {0x0068,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA256 */
1881     {0x0069,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA256 */
1882     {0x006A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 */
1883     {0x006B,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */
1884     {0x006C,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA256 */
1885     {0x006D,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA256 */
1886     {0x0084,KEX_RSA,    ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */
1887     {0x0085,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA */
1888     {0x0086,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA */
1889     {0x0087,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA */
1890     {0x0088,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */
1891     {0x0089,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA */
1892     {0x008B,KEX_PSK,    ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },
1893     {0x008C,KEX_PSK,    ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },
1894     {0x008D,KEX_PSK,    ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },
1895     {0x0096,KEX_RSA,    ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_SEED_CBC_SHA */
1896     {0x0097,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_SEED_CBC_SHA */
1897     {0x0098,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_SEED_CBC_SHA */
1898     {0x0099,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_SEED_CBC_SHA */
1899     {0x009A,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_SEED_CBC_SHA */
1900     {0x009B,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_SEED_CBC_SHA */
1901     {0x009C,KEX_RSA,    ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_WITH_AES_128_GCM_SHA256 */
1902     {0x009D,KEX_RSA,    ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_WITH_AES_256_GCM_SHA384 */
1903     {0x009E,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */
1904     {0x009F,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */
1905     {0x00A0,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_128_GCM_SHA256 */
1906     {0x00A1,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_256_GCM_SHA384 */
1907     {0x00A2,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 */
1908     {0x00A3,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 */
1909     {0x00A4,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_128_GCM_SHA256 */
1910     {0x00A5,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_256_GCM_SHA384 */
1911     {0x00A6,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_128_GCM_SHA256 */
1912     {0x00A7,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */
1913     {0x00BA,KEX_RSA,    ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1914     {0x00BB,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
1915     {0x00BC,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1916     {0x00BD,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
1917     {0x00BE,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1918     {0x00BF,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 */
1919     {0x00C0,KEX_RSA,    ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1920     {0x00C1,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
1921     {0x00C2,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1922     {0x00C3,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
1923     {0x00C4,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1924     {0x00C5,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 */
1925     {0xC001,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_NULL_SHA */
1926     {0xC002,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */
1927     {0xC003,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */
1928     {0xC004,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */
1929     {0xC005,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */
1930     {0xC006,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */
1931     {0xC007,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */
1932     {0xC008,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */
1933     {0xC009,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */
1934     {0xC00A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */
1935     {0xC00B,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_NULL_SHA */
1936     {0xC00C,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_RC4_128_SHA */
1937     {0xC00D,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */
1938     {0xC00E,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */
1939     {0xC00F,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */
1940     {0xC010,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_NULL_SHA */
1941     {0xC011,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */
1942     {0xC012,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */
1943     {0xC013,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */
1944     {0xC014,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */
1945     {0xC015,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_NULL_SHA */
1946     {0xC016,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_RC4_128_SHA */
1947     {0xC017,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA */
1948     {0xC018,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_128_CBC_SHA */
1949     {0xC019,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_256_CBC_SHA */
1950     {0xC023,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 */
1951     {0xC024,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 */
1952     {0xC025,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */
1953     {0xC026,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */
1954     {0xC027,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */
1955     {0xC028,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */
1956     {0xC029,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */
1957     {0xC02A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */
1958     {0xC02B,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */
1959     {0xC02C,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */
1960     {0xC02D,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */
1961     {0xC02E,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */
1962     {0xC02F,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
1963     {0xC030,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */
1964     {0xC031,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */
1965     {0xC032,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */
1966     {0xC09C,KEX_RSA,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_128_CCM */
1967     {0xC09D,KEX_RSA,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_256_CCM */
1968     {0xC09E,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_128_CCM */
1969     {0xC09F,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_256_CCM */
1970     {0xC0A0,KEX_RSA,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_128_CCM_8 */
1971     {0xC0A1,KEX_RSA,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_256_CCM_8 */
1972     {0xC0A2,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_128_CCM_8 */
1973     {0xC0A3,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_256_CCM_8 */
1974     {0xC0A4,KEX_PSK,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_128_CCM */
1975     {0xC0A5,KEX_PSK,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_256_CCM */
1976     {0xC0A8,KEX_PSK,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_128_CCM_8 */
1977     {0xC0A9,KEX_PSK,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_256_CCM_8 */
1978     {-1,    0,          0,               0,  0,  0,0,          MODE_STREAM}
1979 };
1980
1981 #define MAX_BLOCK_SIZE 16
1982 #define MAX_KEY_SIZE 32
1983
1984 int
1985 ssl_find_cipher(int num,SslCipherSuite* cs)
1986 {
1987     SslCipherSuite *c;
1988
1989     for(c=cipher_suites;c->number!=-1;c++){
1990         if(c->number==num){
1991             *cs=*c;
1992             return 0;
1993         }
1994     }
1995
1996     return -1;
1997 }
1998
1999 static gint
2000 tls_hash(StringInfo* secret, StringInfo* seed, gint md, StringInfo* out)
2001 {
2002     guint8   *ptr;
2003     guint     left;
2004     gint      tocpy;
2005     guint8   *A;
2006     guint8    _A[DIGEST_MAX_SIZE],tmp[DIGEST_MAX_SIZE];
2007     guint     A_l,tmp_l;
2008     SSL_HMAC  hm;
2009     ptr  = out->data;
2010     left = out->data_len;
2011
2012
2013     ssl_print_string("tls_hash: hash secret", secret);
2014     ssl_print_string("tls_hash: hash seed", seed);
2015     A=seed->data;
2016     A_l=seed->data_len;
2017
2018     while(left){
2019         ssl_hmac_init(&hm,secret->data,secret->data_len,md);
2020         ssl_hmac_update(&hm,A,A_l);
2021         A_l = sizeof(_A);
2022         ssl_hmac_final(&hm,_A,&A_l);
2023         ssl_hmac_cleanup(&hm);
2024         A=_A;
2025
2026         ssl_hmac_init(&hm,secret->data,secret->data_len,md);
2027         ssl_hmac_update(&hm,A,A_l);
2028         ssl_hmac_update(&hm,seed->data,seed->data_len);
2029         tmp_l = sizeof(tmp);
2030         ssl_hmac_final(&hm,tmp,&tmp_l);
2031         ssl_hmac_cleanup(&hm);
2032
2033         tocpy=MIN(left,tmp_l);
2034         memcpy(ptr,tmp,tocpy);
2035         ptr+=tocpy;
2036         left-=tocpy;
2037     }
2038
2039     ssl_print_string("hash out", out);
2040     return (0);
2041 }
2042
2043 static gint
2044 tls_prf(StringInfo* secret, const gchar *usage,
2045         StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
2046 {
2047     StringInfo  seed, sha_out, md5_out;
2048     guint8     *ptr;
2049     StringInfo  s1, s2;
2050     guint       i,s_l, r;
2051     size_t      usage_len;
2052     r         = -1;
2053     usage_len = strlen(usage);
2054
2055     /* initalize buffer for sha, md5 random seed*/
2056     if (ssl_data_alloc(&sha_out, MAX(out->data_len,20)) < 0) {
2057         ssl_debug_printf("tls_prf: can't allocate sha out\n");
2058         return -1;
2059     }
2060     if (ssl_data_alloc(&md5_out, MAX(out->data_len,16)) < 0) {
2061         ssl_debug_printf("tls_prf: can't allocate md5 out\n");
2062         goto free_sha;
2063     }
2064     if (ssl_data_alloc(&seed, usage_len+rnd1->data_len+rnd2->data_len) < 0) {
2065         ssl_debug_printf("tls_prf: can't allocate rnd %d\n",
2066                          (int) (usage_len+rnd1->data_len+rnd2->data_len));
2067         goto free_md5;
2068     }
2069
2070     ptr=seed.data;
2071     memcpy(ptr,usage,usage_len);
2072     ptr+=usage_len;
2073     memcpy(ptr,rnd1->data,rnd1->data_len);
2074     ptr+=rnd1->data_len;
2075     memcpy(ptr,rnd2->data,rnd2->data_len);
2076     /*ptr+=rnd2->data_len;*/
2077
2078     /* initalize buffer for client/server seeds*/
2079     s_l=secret->data_len/2 + secret->data_len%2;
2080     if (ssl_data_alloc(&s1, s_l) < 0) {
2081         ssl_debug_printf("tls_prf: can't allocate secret %d\n", s_l);
2082         goto free_seed;
2083     }
2084     if (ssl_data_alloc(&s2, s_l) < 0) {
2085         ssl_debug_printf("tls_prf: can't allocate secret(2) %d\n", s_l);
2086         goto free_s1;
2087     }
2088
2089     memcpy(s1.data,secret->data,s_l);
2090     memcpy(s2.data,secret->data + (secret->data_len - s_l),s_l);
2091
2092     ssl_debug_printf("tls_prf: tls_hash(md5 secret_len %d seed_len %d )\n", s1.data_len, seed.data_len);
2093     if(tls_hash(&s1,&seed,ssl_get_digest_by_name("MD5"),&md5_out) != 0)
2094         goto free_all;
2095     ssl_debug_printf("tls_prf: tls_hash(sha)\n");
2096     if(tls_hash(&s2,&seed,ssl_get_digest_by_name("SHA1"),&sha_out) != 0)
2097         goto free_all;
2098
2099     for(i=0;i<out->data_len;i++)
2100       out->data[i]=md5_out.data[i] ^ sha_out.data[i];
2101     r =0;
2102
2103     ssl_print_string("PRF out",out);
2104 free_all:
2105     g_free(s2.data);
2106 free_s1:
2107     g_free(s1.data);
2108 free_seed:
2109     g_free(seed.data);
2110 free_md5:
2111     g_free(md5_out.data);
2112 free_sha:
2113     g_free(sha_out.data);
2114     return r;
2115 }
2116
2117 static gint
2118 tls12_prf(gint md, StringInfo* secret, const gchar* usage, StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
2119 {
2120     StringInfo label_seed;
2121     size_t     usage_len;
2122
2123     usage_len = strlen(usage);
2124     if (ssl_data_alloc(&label_seed, usage_len+rnd1->data_len+rnd2->data_len) < 0) {
2125         ssl_debug_printf("tls12_prf: can't allocate label_seed\n");
2126         return -1;
2127     }
2128     memcpy(label_seed.data, usage, usage_len);
2129     memcpy(label_seed.data+usage_len, rnd1->data, rnd1->data_len);
2130     memcpy(label_seed.data+usage_len+rnd1->data_len, rnd2->data, rnd2->data_len);
2131
2132     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);
2133     if (tls_hash(secret, &label_seed, md, out) != 0){
2134         g_free(label_seed.data);
2135         return -1;
2136     }
2137     ssl_print_string("PRF out", out);
2138     return 0;
2139 }
2140
2141 static gint
2142 ssl3_generate_export_iv(StringInfo* r1,
2143         StringInfo* r2, StringInfo* out)
2144 {
2145     SSL_MD5_CTX md5;
2146     guint8      tmp[16];
2147
2148     ssl_md5_init(&md5);
2149     ssl_md5_update(&md5,r1->data,r1->data_len);
2150     ssl_md5_update(&md5,r2->data,r2->data_len);
2151     ssl_md5_final(tmp,&md5);
2152     ssl_md5_cleanup(&md5);
2153
2154     memcpy(out->data,tmp,out->data_len);
2155     ssl_print_string("export iv", out);
2156
2157     return(0);
2158 }
2159
2160 static gint
2161 ssl3_prf(StringInfo* secret, const gchar* usage,
2162         StringInfo* r1,
2163         StringInfo* r2,StringInfo* out)
2164 {
2165     SSL_MD5_CTX  md5;
2166     SSL_SHA_CTX  sha;
2167     StringInfo  *rnd1,*rnd2;
2168     guint        off;
2169     gint         i = 0,j;
2170     guint8       buf[20];
2171
2172     rnd1=r1; rnd2=r2;
2173
2174     for(off=0;off<out->data_len;off+=16){
2175         guchar outbuf[16];
2176         gint tocpy;
2177         i++;
2178
2179         ssl_debug_printf("ssl3_prf: sha1_hash(%d)\n",i);
2180         /* A, BB, CCC,  ... */
2181         for(j=0;j<i;j++){
2182             buf[j]=64+i;
2183         }
2184
2185         ssl_sha_init(&sha);
2186         ssl_sha_update(&sha,buf,i);
2187         ssl_sha_update(&sha,secret->data,secret->data_len);
2188
2189         if(!strcmp(usage,"client write key") || !strcmp(usage,"server write key")){
2190             ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2191             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2192         }
2193         else{
2194             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2195             ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2196         }
2197
2198         ssl_sha_final(buf,&sha);
2199         ssl_sha_cleanup(&sha);
2200
2201         ssl_debug_printf("ssl3_prf: md5_hash(%d) datalen %d\n",i,
2202             secret->data_len);
2203         ssl_md5_init(&md5);
2204         ssl_md5_update(&md5,secret->data,secret->data_len);
2205         ssl_md5_update(&md5,buf,20);
2206         ssl_md5_final(outbuf,&md5);
2207         ssl_md5_cleanup(&md5);
2208
2209         tocpy=MIN(out->data_len-off,16);
2210         memcpy(out->data+off,outbuf,tocpy);
2211     }
2212
2213     return(0);
2214 }
2215
2216 static gint prf(SslDecryptSession* ssl,StringInfo* secret,const gchar* usage,StringInfo* rnd1,StringInfo* rnd2,StringInfo* out)
2217 {
2218     gint ret;
2219     if (ssl->version_netorder==SSLV3_VERSION){
2220         ret = ssl3_prf(secret,usage,rnd1,rnd2,out);
2221     }else if (ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION ||
2222             ssl->version_netorder==DTLSV1DOT0_VERSION || ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
2223         ret = tls_prf(secret,usage,rnd1,rnd2,out);
2224     }else{
2225         if (ssl->cipher_suite.dig == DIG_SHA384){
2226             ret = tls12_prf(GCRY_MD_SHA384, secret, usage, rnd1, rnd2, out);
2227         }else{
2228             ret = tls12_prf(GCRY_MD_SHA256, secret, usage, rnd1, rnd2, out);
2229         }
2230     }
2231     return ret;
2232 }
2233
2234 static SslFlow*
2235 ssl_create_flow(void)
2236 {
2237   SslFlow *flow;
2238
2239   flow = (SslFlow *)wmem_alloc(wmem_file_scope(), sizeof(SslFlow));
2240   flow->byte_seq = 0;
2241   flow->flags = 0;
2242   flow->multisegment_pdus = wmem_tree_new(wmem_file_scope());
2243   return flow;
2244 }
2245
2246 #ifdef HAVE_LIBZ
2247 /* memory allocation functions for zlib initialization */
2248 static void* ssl_zalloc(void* opaque _U_, unsigned int no, unsigned int size)
2249 {
2250     return g_malloc0(no*size);
2251 }
2252 static void ssl_zfree(void* opaque _U_, void* addr)
2253 {
2254     g_free(addr);
2255 }
2256 #endif
2257
2258 static SslDecompress*
2259 ssl_create_decompressor(gint compression)
2260 {
2261     SslDecompress *decomp;
2262 #ifdef HAVE_LIBZ
2263     int err;
2264 #endif
2265
2266     if (compression == 0) return NULL;
2267     ssl_debug_printf("ssl_create_decompressor: compression method %d\n", compression);
2268     decomp = (SslDecompress *)wmem_alloc(wmem_file_scope(), sizeof(SslDecompress));
2269     decomp->compression = compression;
2270     switch (decomp->compression) {
2271 #ifdef HAVE_LIBZ
2272         case 1:  /* DEFLATE */
2273             decomp->istream.zalloc = ssl_zalloc;
2274             decomp->istream.zfree = ssl_zfree;
2275             decomp->istream.opaque = Z_NULL;
2276             decomp->istream.next_in = Z_NULL;
2277             decomp->istream.next_out = Z_NULL;
2278             decomp->istream.avail_in = 0;
2279             decomp->istream.avail_out = 0;
2280             err = inflateInit_(&decomp->istream, ZLIB_VERSION, sizeof(z_stream));
2281             if (err != Z_OK) {
2282                 ssl_debug_printf("ssl_create_decompressor: inflateInit_() failed - %d\n", err);
2283                 return NULL;
2284             }
2285             break;
2286 #endif
2287         default:
2288             ssl_debug_printf("ssl_create_decompressor: unsupported compression method %d\n", decomp->compression);
2289             return NULL;
2290     }
2291     return decomp;
2292 }
2293
2294 static SslDecoder*
2295 ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
2296         guint8 *mk, guint8 *sk, guint8 *iv)
2297 {
2298     SslDecoder *dec;
2299     gint        ciph;
2300
2301     dec = (SslDecoder *)wmem_alloc0(wmem_file_scope(), sizeof(SslDecoder));
2302     /* Find the SSLeay cipher */
2303     if(cipher_suite->enc!=ENC_NULL) {
2304         ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", ciphers[cipher_suite->enc-0x30]);
2305         ciph=ssl_get_cipher_by_name(ciphers[cipher_suite->enc-0x30]);
2306     } else {
2307         ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", "NULL");
2308         ciph = -1;
2309     }
2310     if (ciph == 0) {
2311         ssl_debug_printf("ssl_create_decoder can't find cipher %s\n",
2312             ciphers[cipher_suite->enc > ENC_NULL ? ENC_NULL-0x30 : (cipher_suite->enc-0x30)]);
2313         return NULL;
2314     }
2315
2316     /* init mac buffer: mac storage is embedded into decoder struct to save a
2317      memory allocation and waste samo more memory*/
2318     dec->cipher_suite=cipher_suite;
2319     dec->compression = compression;
2320     /* AEED ciphers don't have a MAC but need to keep the write IV instead */
2321     if (mk == NULL) {
2322         dec->write_iv.data = dec->_mac_key_or_write_iv;
2323         ssl_data_set(&dec->write_iv, iv, cipher_suite->block);
2324     } else {
2325         dec->mac_key.data = dec->_mac_key_or_write_iv;
2326         ssl_data_set(&dec->mac_key, mk, ssl_cipher_suite_dig(cipher_suite)->len);
2327     }
2328     dec->seq = 0;
2329     dec->decomp = ssl_create_decompressor(compression);
2330     dec->flow = ssl_create_flow();
2331
2332     if (dec->evp)
2333         ssl_cipher_cleanup(&dec->evp);
2334
2335     if (ssl_cipher_init(&dec->evp,ciph,sk,iv,cipher_suite->mode) < 0) {
2336         ssl_debug_printf("ssl_create_decoder: can't create cipher id:%d mode:%d\n",
2337             ciph, cipher_suite->mode);
2338         return NULL;
2339     }
2340
2341     ssl_debug_printf("decoder initialized (digest len %d)\n", ssl_cipher_suite_dig(cipher_suite)->len);
2342     return dec;
2343 }
2344
2345
2346 int
2347 ssl_generate_pre_master_secret(SslDecryptSession *ssl_session,
2348                                guint32 length, tvbuff_t *tvb, guint32 offset,
2349                                const gchar *ssl_psk, const gchar *keylog_filename)
2350 {
2351     /* check for required session data */
2352     ssl_debug_printf("ssl_generate_pre_master_secret: found SSL_HND_CLIENT_KEY_EXCHG, state %X\n",
2353                      ssl_session->state);
2354     if ((ssl_session->state & (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) !=
2355         (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) {
2356         ssl_debug_printf("ssl_generate_pre_master_secret: not enough data to generate key (required state %X)\n",
2357                          (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION));
2358         return -1;
2359     }
2360
2361     if (ssl_session->cipher_suite.kex == KEX_PSK)
2362     {
2363         /* calculate pre master secret*/
2364         StringInfo pre_master_secret;
2365         guint psk_len, pre_master_len;
2366
2367         if (!ssl_psk || (ssl_psk[0] == 0)) {
2368             ssl_debug_printf("ssl_generate_pre_master_secret: can't find pre-shared-key\n");
2369             return -1;
2370         }
2371
2372         /* convert hex string into char*/
2373         if (!from_hex(&ssl_session->psk, ssl_psk, strlen(ssl_psk))) {
2374             ssl_debug_printf("ssl_generate_pre_master_secret: ssl.psk/dtls.psk contains invalid hex\n");
2375             return -1;
2376         }
2377
2378         psk_len = ssl_session->psk.data_len;
2379         if (psk_len >= (2 << 15)) {
2380             ssl_debug_printf("ssl_generate_pre_master_secret: ssl.psk/dtls.psk must not be larger than 2^15 - 1\n");
2381             return -1;
2382         }
2383
2384
2385         pre_master_len = psk_len * 2 + 4;
2386
2387         pre_master_secret.data = (guchar *)wmem_alloc(wmem_file_scope(), pre_master_len);
2388         pre_master_secret.data_len = pre_master_len;
2389         /* 2 bytes psk_len*/
2390         pre_master_secret.data[0] = psk_len >> 8;
2391         pre_master_secret.data[1] = psk_len & 0xFF;
2392         /* psk_len bytes times 0*/
2393         memset(&pre_master_secret.data[2], 0, psk_len);
2394         /* 2 bytes psk_len*/
2395         pre_master_secret.data[psk_len + 2] = psk_len >> 8;
2396         pre_master_secret.data[psk_len + 3] = psk_len & 0xFF;
2397         /* psk*/
2398         memcpy(&pre_master_secret.data[psk_len + 4], ssl_session->psk.data, psk_len);
2399
2400         ssl_session->pre_master_secret.data = pre_master_secret.data;
2401         ssl_session->pre_master_secret.data_len = pre_master_len;
2402         /*ssl_debug_printf("pre master secret",&ssl->pre_master_secret);*/
2403
2404         /* Remove the master secret if it was there.
2405            This forces keying material regeneration in
2406            case we're renegotiating */
2407         ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
2408         ssl_session->state |= SSL_PRE_MASTER_SECRET;
2409         return 0;
2410     }
2411     else
2412     {
2413         StringInfo encrypted_pre_master;
2414         gint ret;
2415         guint encrlen, skip;
2416         encrlen = length;
2417         skip = 0;
2418
2419         /* get encrypted data, on tls1 we have to skip two bytes
2420          * (it's the encrypted len and should be equal to record len - 2)
2421          * in case of rsa1024 that would be 128 + 2 = 130; for psk not necessary
2422          */
2423         if (ssl_session->cipher_suite.kex == KEX_RSA &&
2424            (ssl_session->version == SSL_VER_TLS || ssl_session->version == SSL_VER_TLSv1DOT1 ||
2425             ssl_session->version == SSL_VER_TLSv1DOT2 || ssl_session->version == SSL_VER_DTLS ||
2426             ssl_session->version == SSL_VER_DTLS1DOT2))
2427         {
2428             encrlen  = tvb_get_ntohs(tvb, offset);
2429             skip = 2;
2430             if (encrlen > length - 2)
2431             {
2432                 ssl_debug_printf("ssl_generate_pre_master_secret: wrong encrypted length (%d max %d)\n",
2433                     encrlen, length);
2434                 return -1;
2435             }
2436         }
2437         encrypted_pre_master.data = (guchar *)wmem_alloc(wmem_file_scope(), encrlen);
2438         encrypted_pre_master.data_len = encrlen;
2439         tvb_memcpy(tvb, encrypted_pre_master.data, offset+skip, encrlen);
2440
2441         if (ssl_session->private_key) {
2442             /* go with ssl key processessing; encrypted_pre_master
2443              * will be used for master secret store*/
2444             ret = ssl_decrypt_pre_master_secret(ssl_session, &encrypted_pre_master, ssl_session->private_key);
2445             if (ret < 0) {
2446                 ssl_debug_printf("ssl_generate_pre_master_secret: can't decrypt pre master secret\n");
2447                 return -1;
2448             }
2449             return 0;
2450         } else if (keylog_filename != NULL) {
2451             /* try to find the key in the key log */
2452             if (ssl_keylog_lookup(ssl_session, keylog_filename, &encrypted_pre_master) < 0) {
2453                 return -1;
2454             }
2455             return 0;
2456         }
2457     }
2458     return -1;
2459 }
2460
2461 int
2462 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
2463 {
2464     StringInfo  key_block;
2465     guint8      _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
2466     guint8      _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
2467     gint        needed;
2468     guint8     *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
2469
2470     /* check for enough info to proced */
2471     guint need_all = SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION;
2472     guint need_any = SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET;
2473     if (((ssl_session->state & need_all) != need_all) || ((ssl_session->state & need_any) == 0)) {
2474         ssl_debug_printf("ssl_generate_keyring_material not enough data to generate key "
2475                          "(0x%02X required 0x%02X or 0x%02X)\n", ssl_session->state,
2476                          need_all|SSL_MASTER_SECRET, need_all|SSL_PRE_MASTER_SECRET);
2477         return -1;
2478     }
2479
2480     /* if master_key is not yet generate, create it now*/
2481     if (!(ssl_session->state & SSL_MASTER_SECRET)) {
2482         ssl_debug_printf("ssl_generate_keyring_material:PRF(pre_master_secret)\n");
2483         ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
2484         ssl_print_string("client random",&ssl_session->client_random);
2485         ssl_print_string("server random",&ssl_session->server_random);
2486         if (prf(ssl_session,&ssl_session->pre_master_secret,"master secret",
2487                 &ssl_session->client_random,
2488                 &ssl_session->server_random, &ssl_session->master_secret)) {
2489             ssl_debug_printf("ssl_generate_keyring_material can't generate master_secret\n");
2490             return -1;
2491         }
2492         ssl_print_string("master secret",&ssl_session->master_secret);
2493
2494         /* the pre-master secret has been 'consumend' so we must clear it now */
2495         ssl_session->state &= ~SSL_PRE_MASTER_SECRET;
2496         ssl_session->state |= SSL_MASTER_SECRET;
2497     }
2498
2499     /* Compute the key block. First figure out how much data we need*/
2500     needed=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len*2;
2501     needed+=ssl_session->cipher_suite.bits / 4;
2502     if(ssl_session->cipher_suite.block>1)
2503         needed+=ssl_session->cipher_suite.block*2;
2504
2505     key_block.data_len = needed;
2506     key_block.data = (guchar *)g_malloc(needed);
2507     ssl_debug_printf("ssl_generate_keyring_material sess key generation\n");
2508     if (prf(ssl_session,&ssl_session->master_secret,"key expansion",
2509             &ssl_session->server_random,&ssl_session->client_random,
2510             &key_block)) {
2511         ssl_debug_printf("ssl_generate_keyring_material can't generate key_block\n");
2512         goto fail;
2513     }
2514     ssl_print_string("key expansion", &key_block);
2515
2516     ptr=key_block.data;
2517     /* AEAD ciphers do not have a separate MAC */
2518     if (ssl_session->cipher_suite.mode == MODE_GCM ||
2519         ssl_session->cipher_suite.mode == MODE_CCM ||
2520         ssl_session->cipher_suite.mode == MODE_CCM_8) {
2521         c_mk = s_mk = NULL;
2522     } else {
2523         c_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
2524         s_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
2525     }
2526
2527     c_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
2528     s_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
2529
2530     if(ssl_session->cipher_suite.block>1){
2531         c_iv=ptr; ptr+=ssl_session->cipher_suite.block;
2532         s_iv=ptr; /*ptr+=ssl_session->cipher_suite.block;*/
2533     }
2534
2535     /* export ciphers work with a smaller key length */
2536     if (ssl_session->cipher_suite.eff_bits < ssl_session->cipher_suite.bits) {
2537         StringInfo iv_c,iv_s;
2538         StringInfo key_c,key_s;
2539         StringInfo k;
2540
2541         if(ssl_session->cipher_suite.block>1){
2542
2543             /* We only have room for MAX_BLOCK_SIZE bytes IVs, but that's
2544              all we should need. This is a sanity check */
2545             if(ssl_session->cipher_suite.block>MAX_BLOCK_SIZE) {
2546                 ssl_debug_printf("ssl_generate_keyring_material cipher suite block must be at most %d nut is %d\n",
2547                     MAX_BLOCK_SIZE, ssl_session->cipher_suite.block);
2548                 goto fail;
2549             }
2550
2551             iv_c.data = _iv_c;
2552             iv_c.data_len = ssl_session->cipher_suite.block;
2553             iv_s.data = _iv_s;
2554             iv_s.data_len = ssl_session->cipher_suite.block;
2555
2556             if(ssl_session->version_netorder==SSLV3_VERSION){
2557                 ssl_debug_printf("ssl_generate_keyring_material ssl3_generate_export_iv\n");
2558                 if (ssl3_generate_export_iv(&ssl_session->client_random,
2559                         &ssl_session->server_random,&iv_c)) {
2560                     ssl_debug_printf("ssl_generate_keyring_material can't generate sslv3 client iv\n");
2561                     goto fail;
2562                 }
2563                 ssl_debug_printf("ssl_generate_keyring_material ssl3_generate_export_iv(2)\n");
2564                 if (ssl3_generate_export_iv(&ssl_session->server_random,
2565                         &ssl_session->client_random,&iv_s)) {
2566                     ssl_debug_printf("ssl_generate_keyring_material can't generate sslv3 server iv\n");
2567                     goto fail;
2568                 }
2569             }
2570             else{
2571                 guint8 _iv_block[MAX_BLOCK_SIZE * 2];
2572                 StringInfo iv_block;
2573                 StringInfo key_null;
2574                 guint8 _key_null;
2575
2576                 key_null.data = &_key_null;
2577                 key_null.data_len = 0;
2578
2579                 iv_block.data = _iv_block;
2580                 iv_block.data_len = ssl_session->cipher_suite.block*2;
2581
2582                 ssl_debug_printf("ssl_generate_keyring_material prf(iv_block)\n");
2583                 if(prf(ssl_session,&key_null, "IV block",
2584                         &ssl_session->client_random,
2585                         &ssl_session->server_random,&iv_block)) {
2586                     ssl_debug_printf("ssl_generate_keyring_material can't generate tls31 iv block\n");
2587                     goto fail;
2588                 }
2589
2590                 memcpy(_iv_c,iv_block.data,ssl_session->cipher_suite.block);
2591                 memcpy(_iv_s,iv_block.data+ssl_session->cipher_suite.block,
2592                     ssl_session->cipher_suite.block);
2593             }
2594
2595             c_iv=_iv_c;
2596             s_iv=_iv_s;
2597         }
2598
2599         if (ssl_session->version_netorder==SSLV3_VERSION){
2600
2601             SSL_MD5_CTX md5;
2602             ssl_debug_printf("ssl_generate_keyring_material MD5(client_random)\n");
2603
2604             ssl_md5_init(&md5);
2605             ssl_md5_update(&md5,c_wk,ssl_session->cipher_suite.eff_bits/8);
2606             ssl_md5_update(&md5,ssl_session->client_random.data,
2607                 ssl_session->client_random.data_len);
2608             ssl_md5_update(&md5,ssl_session->server_random.data,
2609                 ssl_session->server_random.data_len);
2610             ssl_md5_final(_key_c,&md5);
2611             ssl_md5_cleanup(&md5);
2612             c_wk=_key_c;
2613
2614             ssl_md5_init(&md5);
2615             ssl_debug_printf("ssl_generate_keyring_material MD5(server_random)\n");
2616             ssl_md5_update(&md5,s_wk,ssl_session->cipher_suite.eff_bits/8);
2617             ssl_md5_update(&md5,ssl_session->server_random.data,
2618                 ssl_session->server_random.data_len);
2619             ssl_md5_update(&md5,ssl_session->client_random.data,
2620                 ssl_session->client_random.data_len);
2621             ssl_md5_final(_key_s,&md5);
2622             ssl_md5_cleanup(&md5);
2623             s_wk=_key_s;
2624         }
2625         else{
2626             key_c.data = _key_c;
2627             key_c.data_len = sizeof(_key_c);
2628             key_s.data = _key_s;
2629             key_s.data_len = sizeof(_key_s);
2630
2631             k.data = c_wk;
2632             k.data_len = ssl_session->cipher_suite.eff_bits/8;
2633             ssl_debug_printf("ssl_generate_keyring_material PRF(key_c)\n");
2634             if (prf(ssl_session,&k,"client write key",
2635                     &ssl_session->client_random,
2636                     &ssl_session->server_random, &key_c)) {
2637                 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 server key \n");
2638                 goto fail;
2639             }
2640             c_wk=_key_c;
2641
2642             k.data = s_wk;
2643             k.data_len = ssl_session->cipher_suite.eff_bits/8;
2644             ssl_debug_printf("ssl_generate_keyring_material PRF(key_s)\n");
2645             if(prf(ssl_session,&k,"server write key",
2646                     &ssl_session->client_random,
2647                     &ssl_session->server_random, &key_s)) {
2648                 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 client key \n");
2649                 goto fail;
2650             }
2651             s_wk=_key_s;
2652         }
2653     }
2654
2655     /* show key material info */
2656     if (c_mk != NULL) {
2657         ssl_print_data("Client MAC key",c_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
2658         ssl_print_data("Server MAC key",s_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
2659     }
2660     ssl_print_data("Client Write key",c_wk,ssl_session->cipher_suite.bits/8);
2661     ssl_print_data("Server Write key",s_wk,ssl_session->cipher_suite.bits/8);
2662
2663     if(ssl_session->cipher_suite.block>1) {
2664         ssl_print_data("Client Write IV",c_iv,ssl_session->cipher_suite.block);
2665         ssl_print_data("Server Write IV",s_iv,ssl_session->cipher_suite.block);
2666     }
2667     else {
2668         ssl_print_data("Client Write IV",c_iv,8);
2669         ssl_print_data("Server Write IV",s_iv,8);
2670     }
2671
2672     /* create both client and server ciphers*/
2673     ssl_debug_printf("ssl_generate_keyring_material ssl_create_decoder(client)\n");
2674     ssl_session->client_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->compression, c_mk, c_wk, c_iv);
2675     if (!ssl_session->client_new) {
2676         ssl_debug_printf("ssl_generate_keyring_material can't init client decoder\n");
2677         goto fail;
2678     }
2679     ssl_debug_printf("ssl_generate_keyring_material ssl_create_decoder(server)\n");
2680     ssl_session->server_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->compression, s_mk, s_wk, s_iv);
2681     if (!ssl_session->server_new) {
2682         ssl_debug_printf("ssl_generate_keyring_material can't init client decoder\n");
2683         goto fail;
2684     }
2685
2686     ssl_debug_printf("ssl_generate_keyring_material: client seq %d, server seq %d\n",
2687         ssl_session->client_new->seq, ssl_session->server_new->seq);
2688     g_free(key_block.data);
2689     ssl_session->state |= SSL_HAVE_SESSION_KEY;
2690     return 0;
2691
2692 fail:
2693     g_free(key_block.data);
2694     return -1;
2695 }
2696
2697 void
2698 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
2699 {
2700     ssl_debug_printf("ssl_change_cipher %s\n", (server)?"SERVER":"CLIENT");
2701     if (server) {
2702         ssl_session->server = ssl_session->server_new;
2703         ssl_session->server_new = NULL;
2704     } else {
2705         ssl_session->client = ssl_session->client_new;
2706         ssl_session->client_new = NULL;
2707     }
2708 }
2709
2710 int
2711 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
2712     StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk)
2713 {
2714     gint i;
2715
2716     if (!encrypted_pre_master)
2717         return -1;
2718
2719     if(ssl_session->cipher_suite.kex == KEX_DH) {
2720         ssl_debug_printf("ssl_decrypt_pre_master_secret session uses DH (%d) key exchange, which is impossible to decrypt\n",
2721             KEX_DH);
2722         return -1;
2723     } else if(ssl_session->cipher_suite.kex != KEX_RSA) {
2724          ssl_debug_printf("ssl_decrypt_pre_master_secret key exchange %d different from KEX_RSA (%d)\n",
2725             ssl_session->cipher_suite.kex, KEX_RSA);
2726         return -1;
2727     }
2728
2729     /* with tls key loading will fail if not rsa type, so no need to check*/
2730     ssl_print_string("pre master encrypted",encrypted_pre_master);
2731     ssl_debug_printf("ssl_decrypt_pre_master_secret:RSA_private_decrypt\n");
2732     i=ssl_private_decrypt(encrypted_pre_master->data_len,
2733         encrypted_pre_master->data, pk);
2734
2735     if (i!=48) {
2736         ssl_debug_printf("ssl_decrypt_pre_master_secret wrong "
2737             "pre_master_secret length (%d, expected %d)\n", i, 48);
2738         return -1;
2739     }
2740
2741     /* the decrypted data has been written into the pre_master key buffer */
2742     ssl_session->pre_master_secret.data = encrypted_pre_master->data;
2743     ssl_session->pre_master_secret.data_len=48;
2744     ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
2745
2746     /* Remove the master secret if it was there.
2747        This forces keying material regeneration in
2748        case we're renegotiating */
2749     ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
2750     ssl_session->state |= SSL_PRE_MASTER_SECRET;
2751     return 0;
2752 }
2753
2754 /* convert network byte order 32 byte number to right-aligned host byte order *
2755  * 8 bytes buffer */
2756 static gint fmt_seq(guint32 num, guint8* buf)
2757 {
2758     guint32 netnum;
2759
2760     memset(buf,0,8);
2761     netnum=g_htonl(num);
2762     memcpy(buf+4,&netnum,4);
2763
2764     return(0);
2765 }
2766
2767 static gint
2768 tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
2769         guint32 datalen, guint8* mac)
2770 {
2771     SSL_HMAC hm;
2772     gint     md;
2773     guint32  len;
2774     guint8   buf[DIGEST_MAX_SIZE];
2775     gint16   temp;
2776
2777     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
2778     ssl_debug_printf("tls_check_mac mac type:%s md %d\n",
2779         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
2780
2781     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
2782         return -1;
2783
2784     /* hash sequence number */
2785     fmt_seq(decoder->seq,buf);
2786
2787     decoder->seq++;
2788
2789     ssl_hmac_update(&hm,buf,8);
2790
2791     /* hash content type */
2792     buf[0]=ct;
2793     ssl_hmac_update(&hm,buf,1);
2794
2795     /* hash version,data length and data*/
2796     /* *((gint16*)buf) = g_htons(ver); */
2797     temp = g_htons(ver);
2798     memcpy(buf, &temp, 2);
2799     ssl_hmac_update(&hm,buf,2);
2800
2801     /* *((gint16*)buf) = g_htons(datalen); */
2802     temp = g_htons(datalen);
2803     memcpy(buf, &temp, 2);
2804     ssl_hmac_update(&hm,buf,2);
2805     ssl_hmac_update(&hm,data,datalen);
2806
2807     /* get digest and digest len*/
2808     len = sizeof(buf);
2809     ssl_hmac_final(&hm,buf,&len);
2810     ssl_hmac_cleanup(&hm);
2811     ssl_print_data("Mac", buf, len);
2812     if(memcmp(mac,buf,len))
2813         return -1;
2814
2815     return 0;
2816 }
2817
2818 static int
2819 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
2820         guint32 datalen, guint8* mac)
2821 {
2822     SSL_MD  mc;
2823     gint    md;
2824     guint32 len;
2825     guint8  buf[64],dgst[20];
2826     gint    pad_ct;
2827     gint16  temp;
2828
2829     pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
2830
2831     /* get cipher used for digest comptuation */
2832     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
2833     if (ssl_md_init(&mc,md) !=0)
2834         return -1;
2835
2836     /* do hash computation on data && padding */
2837     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
2838
2839     /* hash padding*/
2840     memset(buf,0x36,pad_ct);
2841     ssl_md_update(&mc,buf,pad_ct);
2842
2843     /* hash sequence number */
2844     fmt_seq(decoder->seq,buf);
2845     decoder->seq++;
2846     ssl_md_update(&mc,buf,8);
2847
2848     /* hash content type */
2849     buf[0]=ct;
2850     ssl_md_update(&mc,buf,1);
2851
2852     /* hash data length in network byte order and data*/
2853     /* *((gint16* )buf) = g_htons(datalen); */
2854     temp = g_htons(datalen);
2855     memcpy(buf, &temp, 2);
2856     ssl_md_update(&mc,buf,2);
2857     ssl_md_update(&mc,data,datalen);
2858
2859     /* get partial digest */
2860     ssl_md_final(&mc,dgst,&len);
2861     ssl_md_cleanup(&mc);
2862
2863     ssl_md_init(&mc,md);
2864
2865     /* hash mac key */
2866     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
2867
2868     /* hash padding and partial digest*/
2869     memset(buf,0x5c,pad_ct);
2870     ssl_md_update(&mc,buf,pad_ct);
2871     ssl_md_update(&mc,dgst,len);
2872
2873     ssl_md_final(&mc,dgst,&len);
2874     ssl_md_cleanup(&mc);
2875
2876     if(memcmp(mac,dgst,len))
2877         return -1;
2878
2879     return(0);
2880 }
2881
2882 static gint
2883 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
2884         guint32 datalen, guint8* mac)
2885 {
2886     SSL_HMAC hm;
2887     gint     md;
2888     guint32  len;
2889     guint8   buf[DIGEST_MAX_SIZE];
2890     gint16   temp;
2891
2892     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
2893     ssl_debug_printf("dtls_check_mac mac type:%s md %d\n",
2894         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
2895
2896     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
2897         return -1;
2898     ssl_debug_printf("dtls_check_mac seq: %d epoch: %d\n",decoder->seq,decoder->epoch);
2899     /* hash sequence number */
2900     fmt_seq(decoder->seq,buf);
2901     buf[0]=decoder->epoch>>8;
2902     buf[1]=(guint8)decoder->epoch;
2903
2904     ssl_hmac_update(&hm,buf,8);
2905
2906     /* hash content type */
2907     buf[0]=ct;
2908     ssl_hmac_update(&hm,buf,1);
2909
2910     /* hash version,data length and data */
2911     temp = g_htons(ver);
2912     memcpy(buf, &temp, 2);
2913     ssl_hmac_update(&hm,buf,2);
2914
2915     temp = g_htons(datalen);
2916     memcpy(buf, &temp, 2);
2917     ssl_hmac_update(&hm,buf,2);
2918     ssl_hmac_update(&hm,data,datalen);
2919     /* get digest and digest len */
2920     len = sizeof(buf);
2921     ssl_hmac_final(&hm,buf,&len);
2922     ssl_hmac_cleanup(&hm);
2923     ssl_print_data("Mac", buf, len);
2924     if(memcmp(mac,buf,len))
2925         return -1;
2926
2927     return(0);
2928 }
2929
2930 #ifdef HAVE_LIBZ
2931 static int
2932 ssl_decompress_record(SslDecompress* decomp, const guchar* in, guint inl, StringInfo* out_str, guint* outl)
2933 {
2934     gint err;
2935
2936     switch (decomp->compression) {
2937         case 1:  /* DEFLATE */
2938             err = Z_OK;
2939             if (out_str->data_len < 16384) {  /* maximal plain length */
2940                 ssl_data_realloc(out_str, 16384);
2941             }
2942             decomp->istream.next_in = (guchar*)in;
2943             decomp->istream.avail_in = inl;
2944             decomp->istream.next_out = out_str->data;
2945             decomp->istream.avail_out = out_str->data_len;
2946             if (inl > 0)
2947                 err = inflate(&decomp->istream, Z_SYNC_FLUSH);
2948             if (err != Z_OK) {
2949                 ssl_debug_printf("ssl_decompress_record: inflate() failed - %d\n", err);
2950                 return -1;
2951             }
2952             *outl = out_str->data_len - decomp->istream.avail_out;
2953             break;
2954         default:
2955             ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2956             return -1;
2957     }
2958     return 0;
2959 }
2960 #else
2961 int
2962 ssl_decompress_record(SslDecompress* decomp _U_, const guchar* in _U_, guint inl _U_, StringInfo* out_str _U_, guint* outl _U_)
2963 {
2964     ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2965     return -1;
2966 }
2967 #endif
2968
2969 int
2970 ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
2971         const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl)
2972 {
2973     guint   pad, worklen, uncomplen;
2974     guint8 *mac;
2975
2976     ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
2977     ssl_print_data("Ciphertext",in, inl);
2978
2979     /* ensure we have enough storage space for decrypted data */
2980     if (inl > out_str->data_len)
2981     {
2982         ssl_debug_printf("ssl_decrypt_record: allocating %d bytes for decrypt data (old len %d)\n",
2983                 inl + 32, out_str->data_len);
2984         ssl_data_realloc(out_str, inl + 32);
2985     }
2986
2987     /* RFC 6101/2246: SSLCipherText/TLSCipherText has two structures for types:
2988      * (notation: { unencrypted, [ encrypted ] })
2989      * GenericStreamCipher: { [content, mac] }
2990      * GenericBlockCipher: { IV (TLS 1.1+), [content, mac, padding, padding_len] }
2991      * RFC 5426 (TLS 1.2): TLSCipherText has additionally:
2992      * GenericAEADCipher: { nonce_explicit, [content] }
2993      * RFC 4347 (DTLS): based on TLS 1.1, only GenericBlockCipher is supported.
2994      * RFC 6347 (DTLS 1.2): based on TLS 1.2, includes GenericAEADCipher too.
2995      */
2996
2997     /* (TLS 1.1 and later, DTLS) Extract explicit IV for GenericBlockCipher */
2998     if (decoder->cipher_suite->mode == MODE_CBC) {
2999         switch (ssl->version_netorder) {
3000         case TLSV1DOT1_VERSION:
3001         case TLSV1DOT2_VERSION:
3002         case DTLSV1DOT0_VERSION:
3003         case DTLSV1DOT2_VERSION:
3004         case DTLSV1DOT0_VERSION_NOT:
3005             if ((gint)inl < decoder->cipher_suite->block) {
3006                 ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for IV %d\n",
3007                         inl, decoder->cipher_suite->block);
3008                 return -1;
3009             }
3010             pad = gcry_cipher_setiv(decoder->evp, in, decoder->cipher_suite->block);
3011             if (pad != 0) {
3012                 ssl_debug_printf("ssl_decrypt_record failed: failed to set IV: %s %s\n",
3013                         gcry_strsource (pad), gcry_strerror (pad));
3014             }
3015
3016             inl -= decoder->cipher_suite->block;
3017             in += decoder->cipher_suite->block;
3018             break;
3019         }
3020     }
3021
3022     /* Nonce for GenericAEADCipher */
3023     if (decoder->cipher_suite->mode == MODE_GCM ||
3024         decoder->cipher_suite->mode == MODE_CCM ||
3025         decoder->cipher_suite->mode == MODE_CCM_8) {
3026         /* 4 bytes write_iv, 8 bytes explicit_nonce, 4 bytes counter */
3027         guchar gcm_nonce[16] = { 0 };
3028
3029         if ((gint)inl < SSL_EX_NONCE_LEN_GCM) {
3030             ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for nonce %d\n",
3031                 inl, SSL_EX_NONCE_LEN_GCM);
3032             return -1;
3033         }
3034
3035         if (decoder->cipher_suite->mode == MODE_GCM) {
3036             memcpy(gcm_nonce, decoder->write_iv.data, decoder->write_iv.data_len); /* salt */
3037             memcpy(gcm_nonce + decoder->write_iv.data_len, in, SSL_EX_NONCE_LEN_GCM);
3038             /* NIST SP 800-38D, sect. 7.2 says that the 32-bit counter part starts
3039              * at 1, and gets incremented before passing to the block cipher. */
3040             gcm_nonce[4 + SSL_EX_NONCE_LEN_GCM + 3] = 2;
3041         } else { /* MODE_CCM and MODE_CCM_8 */
3042             /* The nonce for CCM and GCM are the same, but the nonce is used as input
3043              * in the CCM algorithm described in RFC 3610. The nonce generated here is
3044              * the one from RFC 3610 sect 2.3. Encryption. */
3045             /* Flags: (L-1) ; L = 16 - 1 - nonceSize */
3046             gcm_nonce[0] = 3 - 1;
3047
3048             memcpy(gcm_nonce + 1, decoder->write_iv.data, decoder->write_iv.data_len); /* salt */
3049             memcpy(gcm_nonce + 1 + decoder->write_iv.data_len, in, SSL_EX_NONCE_LEN_GCM);
3050             gcm_nonce[4 + SSL_EX_NONCE_LEN_GCM + 3] = 1;
3051         }
3052
3053         pad = gcry_cipher_setctr (decoder->evp, gcm_nonce, sizeof (gcm_nonce));
3054         if (pad != 0) {
3055             ssl_debug_printf("ssl_decrypt_record failed: failed to set CTR: %s %s\n",
3056                     gcry_strsource (pad), gcry_strerror (pad));
3057             return -1;
3058         }
3059         inl -= SSL_EX_NONCE_LEN_GCM;
3060         in += SSL_EX_NONCE_LEN_GCM;
3061     }
3062
3063     /* First decrypt*/
3064     if ((pad = ssl_cipher_decrypt(&decoder->evp, out_str->data, out_str->data_len, in, inl))!= 0) {
3065         ssl_debug_printf("ssl_decrypt_record failed: ssl_cipher_decrypt: %s %s\n", gcry_strsource (pad),
3066                     gcry_strerror (pad));
3067         return -1;
3068     }
3069
3070     ssl_print_data("Plaintext", out_str->data, inl);
3071     worklen=inl;
3072
3073     /* RFC 5116 sect 5.1/5.3: AES128/256 GCM/CCM uses 16 bytes for auth tag
3074      * RFC 6655 sect 6.1: AEAD_AES_128_CCM uses 16 bytes for auth tag */
3075     if (decoder->cipher_suite->mode == MODE_GCM ||
3076         decoder->cipher_suite->mode == MODE_CCM) {
3077         if (worklen < 16) {
3078             ssl_debug_printf("ssl_decrypt_record failed: missing tag, work %d\n", worklen);
3079             return -1;
3080         }
3081         /* XXX - validate auth tag */
3082         worklen -= 16;
3083     }
3084     /* RFC 6655 sect 6.1: AEAD_AES_128_CCM_8 uses 8 bytes for auth tag */
3085     if (decoder->cipher_suite->mode == MODE_CCM_8) {
3086         if (worklen < 8) {
3087             ssl_debug_printf("ssl_decrypt_record failed: missing tag, work %d\n", worklen);
3088             return -1;
3089         }
3090         /* XXX - validate auth tag */
3091         worklen -= 8;
3092     }
3093
3094     /* strip padding for GenericBlockCipher */
3095     if (decoder->cipher_suite->mode == MODE_CBC) {
3096         pad=out_str->data[inl-1];
3097         if (worklen <= pad) {
3098             ssl_debug_printf("ssl_decrypt_record failed: padding %d too large for work %d\n",
3099                 pad, worklen);
3100             return -1;
3101         }
3102         worklen-=(pad+1);
3103         ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
3104             pad, worklen);
3105     }
3106
3107     /* MAC for GenericStreamCipher and GenericBlockCipher */
3108     if (decoder->cipher_suite->mode == MODE_STREAM ||
3109         decoder->cipher_suite->mode == MODE_CBC) {
3110         if (ssl_cipher_suite_dig(decoder->cipher_suite)->len > (gint)worklen) {
3111             ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
3112             return -1;
3113         }
3114         worklen-=ssl_cipher_suite_dig(decoder->cipher_suite)->len;
3115         mac = out_str->data + worklen;
3116     } else /* if (decoder->cipher_suite->mode == MODE_GCM) */ {
3117         /* GenericAEADCipher has no MAC */
3118         goto skip_mac;
3119     }
3120
3121     /* Now check the MAC */
3122     ssl_debug_printf("checking mac (len %d, version %X, ct %d seq %d)\n",
3123         worklen, ssl->version_netorder, ct, decoder->seq);
3124     if(ssl->version_netorder==SSLV3_VERSION){
3125         if(ssl3_check_mac(decoder,ct,out_str->data,worklen,mac) < 0) {
3126             if(ssl_ignore_mac_failed) {
3127                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3128             }
3129             else{
3130                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3131                 return -1;
3132             }
3133         }
3134         else{
3135             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3136         }
3137     }
3138     else if(ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION || ssl->version_netorder==TLSV1DOT2_VERSION){
3139         if(tls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)< 0) {
3140             if(ssl_ignore_mac_failed) {
3141                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3142             }
3143             else{
3144                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3145                 return -1;
3146             }
3147         }
3148         else{
3149             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3150         }
3151     }
3152     else if(ssl->version_netorder==DTLSV1DOT0_VERSION ||
3153         ssl->version_netorder==DTLSV1DOT2_VERSION ||
3154         ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
3155         /* Try rfc-compliant mac first, and if failed, try old openssl's non-rfc-compliant mac */
3156         if(dtls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)>= 0) {
3157             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3158         }
3159         else if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)>= 0) {
3160             ssl_debug_printf("ssl_decrypt_record: dtls rfc-compliant mac failed, but old openssl's non-rfc-compliant mac ok\n");
3161         }
3162         else if(ssl_ignore_mac_failed) {
3163             ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3164         }
3165         else{
3166             ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3167             return -1;
3168         }
3169     }
3170 skip_mac:
3171
3172     *outl = worklen;
3173
3174     if (decoder->compression > 0) {
3175         ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
3176         ssl_data_copy(comp_str, out_str);
3177         ssl_print_data("Plaintext compressed", comp_str->data, worklen);
3178         if (!decoder->decomp) {
3179             ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
3180             return -1;
3181         }
3182         if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
3183         ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
3184         *outl = uncomplen;
3185     }
3186
3187     return 0;
3188 }
3189
3190 #define RSA_PARS 6
3191 static SSL_PRIVATE_KEY*
3192 ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
3193 {
3194     gnutls_datum_t rsa_datum[RSA_PARS]; /* m, e, d, p, q, u */
3195     size_t         tmp_size;
3196     gcry_sexp_t    rsa_priv_key = NULL;
3197     gint           i;
3198     int            ret;
3199     size_t         buf_len;
3200     unsigned char  buf_keyid[32];
3201
3202 #ifdef SSL_FAST
3203     gcry_mpi_t* rsa_params = g_malloc(sizeof(gcry_mpi_t)*RSA_PARS);
3204 #else
3205     gcry_mpi_t rsa_params[RSA_PARS];
3206 #endif
3207
3208     buf_len = sizeof(buf_keyid);
3209     ret = gnutls_x509_privkey_get_key_id(priv_key, 0, buf_keyid, &buf_len);
3210     if (ret != 0) {
3211         ssl_debug_printf( "gnutls_x509_privkey_get_key_id(ssl_pkey, 0, buf_keyid, &buf_len) - %s\n", gnutls_strerror(ret));
3212     } else {
3213         ssl_debug_printf( "Private key imported: KeyID %s\n", bytes_to_ep_str_punct(buf_keyid, (int) buf_len, ':'));
3214     }
3215
3216     /* RSA get parameter */
3217     if (gnutls_x509_privkey_export_rsa_raw(priv_key,
3218                                            &rsa_datum[0],
3219                                            &rsa_datum[1],
3220                                            &rsa_datum[2],
3221                                            &rsa_datum[3],
3222                                            &rsa_datum[4],
3223                                            &rsa_datum[5])  != 0) {
3224         ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
3225 #ifdef SSL_FAST
3226         g_free(rsa_params);
3227 #endif
3228         return NULL;
3229     }
3230
3231     /* convert each rsa parameter to mpi format*/
3232     for(i=0; i<RSA_PARS; i++) {
3233       if (gcry_mpi_scan(&rsa_params[i], GCRYMPI_FMT_USG, rsa_datum[i].data, rsa_datum[i].size,&tmp_size) != 0) {
3234         ssl_debug_printf("ssl_load_key: can't convert m rsa param to int (size %d)\n", rsa_datum[i].size);
3235 #ifdef SSL_FAST
3236         g_free(rsa_params);
3237 #endif
3238         return NULL;
3239       }
3240     }
3241
3242     /* libgcrypt expects p < q, and gnutls might not return it as such, depending on gnutls version and its crypto backend */
3243     if (gcry_mpi_cmp(rsa_params[3], rsa_params[4]) > 0)
3244     {
3245         ssl_debug_printf("ssl_load_key: swapping p and q parameters and recomputing u\n");
3246         gcry_mpi_swap(rsa_params[3], rsa_params[4]);
3247         gcry_mpi_invm(rsa_params[5], rsa_params[3], rsa_params[4]);
3248     }
3249
3250     if  (gcry_sexp_build( &rsa_priv_key, NULL,
3251             "(private-key(rsa((n%m)(e%m)(d%m)(p%m)(q%m)(u%m))))", rsa_params[0],
3252             rsa_params[1], rsa_params[2], rsa_params[3], rsa_params[4],
3253             rsa_params[5]) != 0) {
3254         ssl_debug_printf("ssl_load_key: can't build rsa private key s-exp\n");
3255 #ifdef SSL_FAST
3256         g_free(rsa_params);
3257 #endif
3258         return NULL;
3259     }
3260
3261 #ifdef SSL_FAST
3262     return rsa_params;
3263 #else
3264     for (i=0; i< 6; i++)
3265         gcry_mpi_release(rsa_params[i]);
3266     return rsa_priv_key;
3267 #endif
3268
3269 }
3270
3271 Ssl_private_key_t *
3272 ssl_load_key(FILE* fp)
3273 {
3274     /* gnutls makes our work much harder, since we have to work internally with
3275      * s-exp formatted data, but PEM loader exports only in "gnutls_datum_t"
3276      * format, and a datum -> s-exp convertion function does not exist.
3277      */
3278     gnutls_x509_privkey_t priv_key;
3279     gnutls_datum_t        key;
3280     long                  size;
3281     gint                  ret;
3282     guint                 bytes;
3283
3284     Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
3285
3286     /* init private key data*/
3287     gnutls_x509_privkey_init(&priv_key);
3288
3289     /* compute file size and load all file contents into a datum buffer*/
3290     if (fseek(fp, 0, SEEK_END) < 0) {
3291         ssl_debug_printf("ssl_load_key: can't fseek file\n");
3292         g_free(private_key);
3293         return NULL;
3294     }
3295     if ((size = ftell(fp)) < 0) {
3296         ssl_debug_printf("ssl_load_key: can't ftell file\n");
3297         g_free(private_key);
3298         return NULL;
3299     }
3300     if (fseek(fp, 0, SEEK_SET) < 0) {
3301         ssl_debug_printf("ssl_load_key: can't re-fseek file\n");
3302         g_free(private_key);
3303         return NULL;
3304     }
3305     key.data = (unsigned char *)g_malloc(size);
3306     key.size = (int)size;
3307     bytes = (guint) fread(key.data, 1, key.size, fp);
3308     if (bytes < key.size) {
3309         ssl_debug_printf("ssl_load_key: can't read from file %d bytes, got %d\n",
3310             key.size, bytes);
3311         g_free(private_key);
3312         g_free(key.data);
3313         return NULL;
3314     }
3315
3316     /* import PEM data*/
3317     if ((ret = gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) {
3318         ssl_debug_printf("ssl_load_key: can't import pem data: %s\n", gnutls_strerror(ret));
3319         g_free(private_key);
3320         g_free(key.data);
3321         return NULL;
3322     }
3323
3324     if (gnutls_x509_privkey_get_pk_algorithm(priv_key) != GNUTLS_PK_RSA) {
3325         ssl_debug_printf("ssl_load_key: private key public key algorithm isn't RSA\n");
3326         g_free(private_key);
3327         g_free(key.data);
3328         return NULL;
3329     }
3330
3331     g_free(key.data);
3332
3333     private_key->x509_pkey = priv_key;
3334     private_key->sexp_pkey = ssl_privkey_to_sexp(priv_key);
3335     if ( !private_key->sexp_pkey ) {
3336         g_free(private_key);
3337         return NULL;
3338     }
3339     return private_key;
3340 }
3341
3342 static const char *
3343 BAGTYPE(gnutls_pkcs12_bag_type_t x) {
3344     switch (x) {
3345         case GNUTLS_BAG_EMPTY:               return "Empty";
3346         case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY: return "PKCS#8 Encrypted key";
3347         case GNUTLS_BAG_PKCS8_KEY:           return "PKCS#8 Key";
3348         case GNUTLS_BAG_CERTIFICATE:         return "Certificate";
3349         case GNUTLS_BAG_CRL:                 return "CRL";
3350         case GNUTLS_BAG_ENCRYPTED:           return "Encrypted";
3351         case GNUTLS_BAG_UNKNOWN:             return "Unknown";
3352         default:                             return "<undefined>";
3353     }
3354 }
3355
3356 /**
3357  * Load a RSA private key from a PKCS#12 file.
3358  * @param fp the file that contains the key data.
3359  * @param cert_passwd password to decrypt the PKCS#12 file.
3360  * @param[out] err error message upon failure; NULL upon success.
3361  * @return a pointer to the loaded key on success; NULL upon failure.
3362  */
3363 static Ssl_private_key_t *
3364 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd, const char** err) {
3365
3366     int                       i, j, ret;
3367     int                       rest;
3368     unsigned char            *p;
3369     gnutls_datum_t            data;
3370     gnutls_pkcs12_bag_t       bag = NULL;
3371     gnutls_pkcs12_bag_type_t  bag_type;
3372     size_t                    len, buf_len;
3373     static char               buf_name[256];
3374     static char               buf_email[128];
3375     unsigned char             buf_keyid[32];
3376
3377     gnutls_pkcs12_t       ssl_p12  = NULL;
3378     gnutls_x509_crt_t     ssl_cert = NULL;
3379     gnutls_x509_privkey_t ssl_pkey = NULL;
3380
3381     Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
3382     *err = NULL;
3383
3384     rest = 4096;
3385     data.data = (unsigned char *)g_malloc(rest);
3386     data.size = rest;
3387     p = data.data;
3388     while ((len = fread(p, 1, rest, fp)) > 0) {
3389         p += len;
3390         rest -= (int) len;
3391         if (!rest) {
3392             rest = 1024;
3393             data.data = (unsigned char *)g_realloc(data.data, data.size + rest);
3394             p = data.data + data.size;
3395             data.size += rest;
3396         }
3397     }
3398     data.size -= rest;
3399     ssl_debug_printf("%d bytes read\n", data.size);
3400     if (!feof(fp)) {
3401         *err = "Error during certificate reading.";
3402         ssl_debug_printf("%s\n", *err);
3403         g_free(private_key);
3404         g_free(data.data);
3405         return 0;
3406     }
3407
3408     ret = gnutls_pkcs12_init(&ssl_p12);
3409     if (ret < 0) {
3410         *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
3411         ssl_debug_printf("%s\n", *err);
3412         g_free(private_key);
3413         g_free(data.data);
3414         return 0;
3415     }
3416
3417     /* load PKCS#12 in DER or PEM format */
3418     ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 0);
3419     if (ret < 0) {
3420         *err = wmem_strdup_printf(wmem_file_scope(), "could not load PKCS#12 in DER format: %s", gnutls_strerror(ret));
3421         ssl_debug_printf("%s\n", *err);
3422
3423         ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_PEM, 0);
3424         if (ret < 0) {
3425             *err = wmem_strdup_printf(wmem_file_scope(), "could not load PKCS#12 in PEM format: %s", gnutls_strerror(ret));
3426             ssl_debug_printf("%s\n", *err);
3427         } else {
3428             *err = NULL;
3429         }
3430     }
3431     g_free(data.data);
3432     if (ret < 0) {
3433         g_free(private_key);
3434         return 0;
3435     }
3436
3437     ssl_debug_printf( "PKCS#12 imported\n");
3438
3439     for (i=0; ret==0; i++) {
3440
3441         if (bag) { gnutls_pkcs12_bag_deinit(bag); bag = NULL; }
3442
3443         ret = gnutls_pkcs12_bag_init(&bag);
3444         if (ret < 0) continue;
3445
3446         ret = gnutls_pkcs12_get_bag(ssl_p12, i, bag);
3447         if (ret < 0) continue;
3448
3449         for (j=0; ret==0 && j<gnutls_pkcs12_bag_get_count(bag); j++) {
3450
3451             bag_type = gnutls_pkcs12_bag_get_type(bag, j);
3452             if (bag_type >= GNUTLS_BAG_UNKNOWN) continue;
3453             ssl_debug_printf( "Bag %d/%d: %s\n", i, j, BAGTYPE(bag_type));
3454             if (bag_type == GNUTLS_BAG_ENCRYPTED) {
3455                 ret = gnutls_pkcs12_bag_decrypt(bag, cert_passwd);
3456                 if (ret == 0) {
3457                     bag_type = gnutls_pkcs12_bag_get_type(bag, j);
3458                     if (bag_type >= GNUTLS_BAG_UNKNOWN) continue;
3459                     ssl_debug_printf( "Bag %d/%d decrypted: %s\n", i, j, BAGTYPE(bag_type));
3460                 }
3461             }
3462
3463             ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
3464             if (ret < 0) continue;
3465
3466             switch (bag_type) {
3467
3468                 case GNUTLS_BAG_CERTIFICATE:
3469
3470                     ret = gnutls_x509_crt_init(&ssl_cert);
3471                     if (ret < 0) {
3472                         *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_crt_init(&ssl_cert) - %s", gnutls_strerror(ret));
3473                         ssl_debug_printf("%s\n", *err);
3474                         g_free(private_key);
3475                         return 0;
3476                     }
3477
3478                     ret = gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER);
3479                     if (ret < 0) {
3480                         *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER) - %s", gnutls_strerror(ret));
3481                         ssl_debug_printf("%s\n", *err);
3482                         g_free(private_key);
3483                         return 0;
3484                     }
3485
3486                     buf_len = sizeof(buf_name);
3487                     ret = gnutls_x509_crt_get_dn_by_oid(ssl_cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, buf_name, &buf_len);
3488                     if (ret < 0) { g_strlcpy(buf_name, "<ERROR>", 256); }
3489                     buf_len = sizeof(buf_email);
3490                     ret = gnutls_x509_crt_get_dn_by_oid(ssl_cert, GNUTLS_OID_PKCS9_EMAIL, 0, 0, buf_email, &buf_len);
3491                     if (ret < 0) { g_strlcpy(buf_email, "<ERROR>", 128); }
3492
3493                     buf_len = sizeof(buf_keyid);
3494                     ret = gnutls_x509_crt_get_key_id(ssl_cert, 0, buf_keyid, &buf_len);
3495                     if (ret < 0) { g_strlcpy(buf_keyid, "<ERROR>", 32); }
3496
3497                     private_key->x509_cert = ssl_cert;
3498                     ssl_debug_printf( "Certificate imported: %s <%s>, KeyID %s\n", buf_name, buf_email, bytes_to_ep_str(buf_keyid, (int) buf_len));
3499                     break;
3500
3501                 case GNUTLS_BAG_PKCS8_KEY:
3502                 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
3503
3504                     ret = gnutls_x509_privkey_init(&ssl_pkey);
3505                     if (ret < 0) {
3506                         *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_privkey_init(&ssl_pkey) - %s", gnutls_strerror(ret));
3507                         ssl_debug_printf("%s\n", *err);
3508                         g_free(private_key);
3509                         return 0;
3510                     }
3511                     ret = gnutls_x509_privkey_import_pkcs8(ssl_pkey, &data, GNUTLS_X509_FMT_DER, cert_passwd,
3512                                                            (bag_type==GNUTLS_BAG_PKCS8_KEY) ? GNUTLS_PKCS_PLAIN : 0);
3513                     if (ret < 0) {
3514                         *err = wmem_strdup_printf(wmem_file_scope(), "Can not decrypt private key - %s", gnutls_strerror(ret));
3515                         ssl_debug_printf("%s\n", *err);
3516                         g_free(private_key);
3517                         return 0;
3518                     }
3519
3520                     if (gnutls_x509_privkey_get_pk_algorithm(ssl_pkey) != GNUTLS_PK_RSA) {
3521                         *err = "ssl_load_pkcs12: private key public key algorithm isn't RSA";
3522                         ssl_debug_printf("%s\n", *err);
3523                         g_free(private_key);
3524                         return 0;
3525                     }
3526
3527                     private_key->x509_pkey = ssl_pkey;
3528                     private_key->sexp_pkey = ssl_privkey_to_sexp(ssl_pkey);
3529                     if ( !private_key->sexp_pkey ) {
3530                         *err = "ssl_load_pkcs12: could not create sexp_pkey";
3531                         ssl_debug_printf("%s\n", *err);
3532                         g_free(private_key);
3533                         return NULL;
3534                     }
3535                     break;
3536
3537                 default: ;
3538             }
3539         }  /* j */
3540     }  /* i */
3541
3542     return private_key;
3543 }
3544
3545
3546 void ssl_free_key(Ssl_private_key_t* key)
3547 {
3548 #ifdef SSL_FAST
3549     gint i;
3550     for (i=0; i< 6; i++)
3551         gcry_mpi_release(key->sexp_pkey[i]);
3552 #else
3553     gcry_sexp_release(key->sexp_pkey);
3554 #endif
3555
3556     if (!key->x509_cert)
3557         gnutls_x509_crt_deinit (key->x509_cert);
3558
3559     if (!key->x509_pkey)
3560         gnutls_x509_privkey_deinit(key->x509_pkey);
3561
3562     g_free((Ssl_private_key_t*)key);
3563 }
3564
3565 gint
3566 ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree* associations, packet_info *pinfo) {
3567     SslService dummy;
3568     char       ip_addr_any[] = {0,0,0,0};
3569     guint32    port    = 0;
3570     Ssl_private_key_t * private_key;
3571
3572     if (!ssl_session) {
3573         return 0;
3574     }
3575
3576     /* we need to know which side of the conversation is speaking */
3577     if (ssl_packet_from_server(ssl_session, associations, pinfo)) {
3578         dummy.addr = pinfo->src;
3579         dummy.port = port = pinfo->srcport;
3580     } else {
3581         dummy.addr = pinfo->dst;
3582         dummy.port = port = pinfo->destport;
3583     }
3584     ssl_debug_printf("ssl_find_private_key server %s:%u\n",
3585                      ep_address_to_str(&dummy.addr),dummy.port);
3586
3587     /* try to retrieve private key for this service. Do it now 'cause pinfo
3588      * is not always available
3589      * Note that with HAVE_LIBGNUTLS undefined private_key is allways 0
3590      * and thus decryption never engaged*/
3591
3592
3593     ssl_session->private_key = 0;
3594     private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
3595
3596     if (!private_key) {
3597         ssl_debug_printf("ssl_find_private_key can't find private key for this server! Try it again with universal port 0\n");
3598
3599         dummy.port = 0;
3600         private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
3601     }
3602
3603     if (!private_key) {
3604         ssl_debug_printf("ssl_find_private_key can't find private key for this server (universal port)! Try it again with universal address 0.0.0.0\n");
3605
3606         dummy.addr.type = AT_IPv4;
3607         dummy.addr.len = 4;
3608         dummy.addr.data = ip_addr_any;
3609
3610         dummy.port = port;
3611         private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
3612     }
3613
3614     if (!private_key) {
3615         ssl_debug_printf("ssl_find_private_key can't find any private key!\n");
3616     } else {
3617         ssl_session->private_key = private_key->sexp_pkey;
3618     }
3619
3620     return 0;
3621 }
3622
3623 void
3624 ssl_lib_init(void)
3625 {
3626     ssl_debug_printf("gnutls version: %s\n", gnutls_check_version(NULL));
3627 }
3628
3629 #else /* defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT) */
3630 /* no libgnutl: dummy operation to keep interface consistent*/
3631 void
3632 ssl_lib_init(void)
3633 {
3634 }
3635
3636 Ssl_private_key_t *
3637 ssl_load_key(FILE* fp)
3638 {
3639     ssl_debug_printf("ssl_load_key: impossible without gnutls. fp %p\n",fp);
3640     return NULL;
3641 }
3642
3643 Ssl_private_key_t *
3644 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd _U_, const char** err) {
3645     *err = NULL;
3646     ssl_debug_printf("ssl_load_pkcs12: impossible without gnutls. fp %p\n",fp);
3647     return NULL;
3648 }
3649
3650 void
3651 ssl_free_key(Ssl_private_key_t* key _U_)
3652 {
3653 }
3654
3655 gint
3656 ssl_find_private_key(SslDecryptSession *ssl_session _U_, GHashTable *key_hash _U_, GTree* associations _U_, packet_info *pinfo _U_)
3657 {
3658     return 0;
3659 }
3660
3661 int
3662 ssl_find_cipher(int num,SslCipherSuite* cs)
3663 {
3664     ssl_debug_printf("ssl_find_cipher: dummy without gnutls. num %d cs %p\n",
3665         num,cs);
3666     return 0;
3667 }
3668 int
3669 ssl_generate_pre_master_secret(SslDecryptSession *ssl_session _U_,
3670         guint32 length _U_, tvbuff_t *tvb _U_, guint32 offset _U_,
3671         const gchar *ssl_psk _U_, const gchar *keylog_filename _U_)
3672 {
3673     ssl_debug_printf("ssl_generate_pre_master_secret: impossible without gnutls.\n");
3674     return 0;
3675 }
3676 int
3677 ssl_generate_keyring_material(SslDecryptSession*ssl)
3678 {
3679     ssl_debug_printf("ssl_generate_keyring_material: impossible without gnutls. ssl %p\n",
3680         ssl);
3681     return 0;
3682 }
3683 void
3684 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
3685 {
3686     ssl_debug_printf("ssl_change_cipher %s: makes no sense without gnutls. ssl %p\n",
3687         (server)?"SERVER":"CLIENT", ssl_session);
3688 }
3689
3690 int
3691 ssl_decrypt_pre_master_secret(SslDecryptSession* ssl_session,
3692     StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk)
3693 {
3694     ssl_debug_printf("ssl_decrypt_pre_master_secret: impossible without gnutls."
3695         " ssl %p encrypted_pre_master %p pk %p\n", ssl_session,
3696         encrypted_pre_master, pk);
3697     return 0;
3698 }
3699
3700 int
3701 ssl_decrypt_record(SslDecryptSession*ssl, SslDecoder* decoder, gint ct,
3702         const guchar* in, guint inl, StringInfo* comp_str _U_, StringInfo* out, guint* outl)
3703 {
3704     ssl_debug_printf("ssl_decrypt_record: impossible without gnutls. ssl %p"
3705         "decoder %p ct %d, in %p inl %d out %p outl %p\n", ssl, decoder, ct,
3706         in, inl, out, outl);
3707     return 0;
3708 }
3709
3710 gint
3711 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher _U_, guchar* iv _U_, gint iv_len _U_)
3712 {
3713     ssl_debug_printf("ssl_cipher_setiv: impossible without gnutls.\n");
3714     return 0;
3715 }
3716
3717 #endif /* HAVE_LIBGNUTLS */
3718
3719 /* get ssl data for this session. if no ssl data is found allocate a new one*/
3720 void
3721 ssl_session_init(SslDecryptSession* ssl_session)
3722 {
3723     ssl_debug_printf("ssl_session_init: initializing ptr %p size %" G_GSIZE_MODIFIER "u\n",
3724                      (void *)ssl_session, sizeof(SslDecryptSession));
3725
3726     ssl_session->master_secret.data = ssl_session->_master_secret;
3727     ssl_session->session_id.data = ssl_session->_session_id;
3728     ssl_session->client_random.data = ssl_session->_client_random;
3729     ssl_session->server_random.data = ssl_session->_server_random;
3730     ssl_session->master_secret.data_len = 48;
3731     ssl_session->server_data_for_iv.data_len = 0;
3732     ssl_session->server_data_for_iv.data = ssl_session->_server_data_for_iv;
3733     ssl_session->client_data_for_iv.data_len = 0;
3734     ssl_session->client_data_for_iv.data = ssl_session->_client_data_for_iv;
3735     ssl_session->app_data_segment.data=NULL;
3736     ssl_session->app_data_segment.data_len=0;
3737     SET_ADDRESS(&ssl_session->srv_addr, AT_NONE, 0, NULL);
3738     ssl_session->srv_ptype = PT_NONE;
3739     ssl_session->srv_port = 0;
3740 }
3741
3742 void
3743 ssl_set_server(SslDecryptSession* ssl, address *addr, port_type ptype, guint32 port)
3744 {
3745     SE_COPY_ADDRESS(&ssl->srv_addr, addr);
3746     ssl->srv_ptype = ptype;
3747     ssl->srv_port = port;
3748 }
3749
3750 /* Hash Functions for TLS/DTLS sessions table and private keys table*/
3751 gint
3752 ssl_equal (gconstpointer v, gconstpointer v2)
3753 {
3754     const StringInfo *val1;
3755     const StringInfo *val2;
3756     val1 = (const StringInfo *)v;
3757     val2 = (const StringInfo *)v2;
3758
3759     if (val1->data_len == val2->data_len &&
3760         !memcmp(val1->data, val2->data, val2->data_len)) {
3761         return 1;
3762     }
3763     return 0;
3764 }
3765
3766 guint
3767 ssl_hash  (gconstpointer v)
3768 {
3769     guint l,hash;
3770     const StringInfo* id;
3771     const guint* cur;
3772     hash = 0;
3773     id = (const StringInfo*) v;
3774
3775     /*  id and id->data are mallocated in ssl_save_session().  As such 'data'
3776      *  should be aligned for any kind of access (for example as a guint as
3777      *  is done below).  The intermediate void* cast is to prevent "cast
3778      *  increases required alignment of target type" warnings on CPUs (such
3779      *  as SPARCs) that do not allow misaligned memory accesses.
3780      */
3781     cur = (const guint*)(void*) id->data;
3782
3783     for (l=4; (l < id->data_len); l+=4, cur++)
3784         hash = hash ^ (*cur);
3785
3786     return hash;
3787 }
3788
3789 gint
3790 ssl_private_key_equal (gconstpointer v, gconstpointer v2)
3791 {
3792     const SslService *val1;
3793     const SslService *val2;
3794     val1 = (const SslService *)v;
3795     val2 = (const SslService *)v2;
3796
3797     if ((val1->port == val2->port) &&
3798         ! CMP_ADDRESS(&val1->addr, &val2->addr)) {
3799         return 1;
3800     }
3801     return 0;
3802 }
3803
3804 guint
3805 ssl_private_key_hash  (gconstpointer v)
3806 {
3807     const SslService *key;
3808     guint        l, hash, len ;
3809     const guint* cur;
3810
3811     key  = (const SslService *)v;
3812     hash = key->port;
3813     len  = key->addr.len;
3814     cur  = (const guint*) key->addr.data;
3815
3816     for (l=4; (l<len); l+=4, cur++)
3817         hash = hash ^ (*cur);
3818
3819     return hash;
3820 }
3821
3822 /* private key table entries have a scope 'larger' then packet capture,
3823  * so we can't relay on se_alloc** function */
3824 void
3825 ssl_private_key_free(gpointer id, gpointer key, gpointer dummy _U_)
3826 {
3827     if (id != NULL) {
3828         g_free(id);
3829         ssl_free_key((Ssl_private_key_t*) key);
3830     }
3831 }
3832
3833 /* handling of association between tls/dtls ports and clear text protocol */
3834 void
3835 ssl_association_add(GTree* associations, dissector_handle_t handle, guint port, const gchar *protocol, gboolean tcp, gboolean from_key_list)
3836 {
3837
3838     SslAssociation* assoc;
3839     assoc = (SslAssociation *)g_malloc(sizeof(SslAssociation));
3840
3841     assoc->tcp = tcp;
3842     assoc->ssl_port = port;
3843     assoc->info=g_strdup(protocol);
3844     assoc->handle = find_dissector(protocol);
3845     assoc->from_key_list = from_key_list;
3846
3847     ssl_debug_printf("association_add %s port %d protocol %s handle %p\n",
3848                      (assoc->tcp)?"TCP":"UDP", port, protocol, (void *)(assoc->handle));
3849
3850
3851     if (!assoc->handle) {
3852         ssl_debug_printf("association_add could not find handle for protocol '%s', try to find 'data' dissector\n", protocol);
3853         assoc->handle = find_dissector("data");
3854     }
3855
3856     if (!assoc->handle) {
3857         fprintf(stderr, "association_add() could not find handle for protocol:%s\n",protocol);
3858     } else {
3859         if (port) {
3860             if (tcp)
3861                 dissector_add_uint("tcp.port", port, handle);
3862             else
3863                 dissector_add_uint("udp.port", port, handle);
3864         }
3865         g_tree_insert(associations, assoc, assoc);
3866
3867         dissector_add_uint("sctp.port", port, handle);
3868     }
3869 }
3870
3871 void
3872 ssl_association_remove(GTree* associations, SslAssociation *assoc)
3873 {
3874     ssl_debug_printf("ssl_association_remove removing %s %u - %s handle %p\n",
3875                      (assoc->tcp)?"TCP":"UDP", assoc->ssl_port, assoc->info, (void *)(assoc->handle));
3876     if (assoc->handle)
3877         dissector_delete_uint((assoc->tcp)?"tcp.port":"udp.port", assoc->ssl_port, assoc->handle);
3878
3879     g_free(assoc->info);
3880
3881     g_tree_remove(associations, assoc);
3882     g_free(assoc);
3883 }
3884
3885 gint
3886 ssl_association_cmp(gconstpointer a, gconstpointer b)
3887 {
3888     const SslAssociation *assoc_a=(SslAssociation *)a, *assoc_b=(SslAssociation *)b;
3889     if (assoc_a->tcp != assoc_b->tcp) return (assoc_a->tcp)?1:-1;
3890     return assoc_a->ssl_port - assoc_b->ssl_port;
3891 }
3892
3893 SslAssociation*
3894 ssl_association_find(GTree * associations, guint port, gboolean tcp)
3895 {
3896     register SslAssociation* ret;
3897     SslAssociation           assoc_tmp;
3898
3899     assoc_tmp.tcp = tcp;
3900     assoc_tmp.ssl_port = port;
3901     ret = (SslAssociation *)g_tree_lookup(associations, &assoc_tmp);
3902
3903     ssl_debug_printf("association_find: %s port %d found %p\n", (tcp)?"TCP":"UDP", port, (void *)ret);
3904     return ret;
3905 }
3906
3907 gint
3908 ssl_assoc_from_key_list(gpointer key _U_, gpointer data, gpointer user_data)
3909 {
3910     if (((SslAssociation*)data)->from_key_list)
3911         wmem_stack_push((wmem_stack_t*)user_data, data);
3912     return FALSE;
3913 }
3914
3915 int
3916 ssl_packet_from_server(SslDecryptSession* ssl, GTree* associations, packet_info *pinfo)
3917 {
3918     gint ret;
3919     if (ssl && (ssl->srv_ptype != PT_NONE)) {
3920         ret = (ssl->srv_ptype == pinfo->ptype) && (ssl->srv_port == pinfo->srcport) && ADDRESSES_EQUAL(&ssl->srv_addr, &pinfo->src);
3921     } else {
3922         ret = ssl_association_find(associations, pinfo->srcport, pinfo->ptype == PT_TCP) != 0;
3923     }
3924
3925     ssl_debug_printf("packet_from_server: is from server - %s\n", (ret)?"TRUE":"FALSE");
3926     return ret;
3927 }
3928
3929 /* add to packet data a copy of the specified real data */
3930 void
3931 ssl_add_record_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint record_id)
3932 {
3933     guchar*        real_data;
3934     SslRecordInfo* rec;
3935     SslPacketInfo* pi;
3936
3937     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
3938     if (!pi)
3939     {
3940         pi = (SslPacketInfo *)wmem_alloc0(wmem_file_scope(), sizeof(SslPacketInfo));
3941         p_add_proto_data(wmem_file_scope(), pinfo, proto, 0, pi);
3942     }
3943
3944     real_data = (guchar *)wmem_alloc(wmem_file_scope(), data_len);
3945     memcpy(real_data, data, data_len);
3946
3947     rec = (SslRecordInfo *)wmem_alloc(wmem_file_scope(), sizeof(SslRecordInfo));
3948     rec->id = record_id;
3949     rec->real_data = real_data;
3950     rec->data_len = data_len;
3951
3952     /* head insertion */
3953     rec->next= pi->handshake_data;
3954     pi->handshake_data = rec;
3955 }
3956
3957 /* search in packet data for the specified id; return a newly created tvb for the associated data */
3958 tvbuff_t*
3959 ssl_get_record_info(tvbuff_t *parent_tvb, int proto, packet_info *pinfo, gint record_id)
3960 {
3961     SslRecordInfo* rec;
3962     SslPacketInfo* pi;
3963     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
3964
3965     if (!pi)
3966         return NULL;
3967
3968     for (rec = pi->handshake_data; rec; rec = rec->next)
3969         if (rec->id == record_id)
3970             /* link new real_data_tvb with a parent tvb so it is freed when frame dissection is complete */
3971             return tvb_new_child_real_data(parent_tvb, rec->real_data, rec->data_len, rec->data_len);
3972
3973     return NULL;
3974 }
3975
3976 void
3977 ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint key, SslFlow *flow)
3978 {
3979     SslDataInfo   *rec, **prec;
3980     SslPacketInfo *pi;
3981
3982     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
3983     if (!pi)
3984     {
3985         pi = (SslPacketInfo *)wmem_alloc0(wmem_file_scope(), sizeof(SslPacketInfo));
3986         p_add_proto_data(wmem_file_scope(), pinfo, proto, 0, pi);
3987     }
3988
3989     rec = (SslDataInfo *)wmem_alloc(wmem_file_scope(), sizeof(SslDataInfo)+data_len);
3990     rec->key = key;
3991     rec->plain_data.data = (guchar*)(rec + 1);
3992     memcpy(rec->plain_data.data, data, data_len);
3993     rec->plain_data.data_len = data_len;
3994     if (flow)
3995     {
3996         rec->seq = flow->byte_seq;
3997         rec->nxtseq = flow->byte_seq + data_len;
3998         rec->flow = flow;
3999         flow->byte_seq += data_len;
4000     }
4001     rec->next = NULL;
4002
4003     /* insertion */
4004     prec = &pi->appl_data;
4005     while (*prec) prec = &(*prec)->next;
4006     *prec = rec;
4007
4008     ssl_debug_printf("ssl_add_data_info: new data inserted data_len = %d, seq = %u, nxtseq = %u\n",
4009                      rec->plain_data.data_len, rec->seq, rec->nxtseq);
4010 }
4011
4012 SslDataInfo*
4013 ssl_get_data_info(int proto, packet_info *pinfo, gint key)
4014 {
4015     SslDataInfo*   rec;
4016     SslPacketInfo* pi;
4017     pi = (SslPacketInfo *)p_get_proto_data(wmem_file_scope(), pinfo, proto, 0);
4018
4019     if (!pi) return NULL;
4020
4021     rec = pi->appl_data;
4022     while (rec) {
4023         if (rec->key == key) return rec;
4024         rec = rec->next;
4025     }
4026
4027     return NULL;
4028 }
4029
4030 /* initialize/reset per capture state data (ssl sessions cache) */
4031 void
4032 ssl_common_init(GHashTable **session_hash, StringInfo *decrypted_data, StringInfo *compressed_data)
4033 {
4034     if (*session_hash)
4035         g_hash_table_destroy(*session_hash);
4036     *session_hash = g_hash_table_new(ssl_hash, ssl_equal);
4037
4038     g_free(decrypted_data->data);
4039     ssl_data_alloc(decrypted_data, 32);
4040
4041     g_free(compressed_data->data);
4042     ssl_data_alloc(compressed_data, 32);
4043 }
4044
4045 /* parse ssl related preferences (private keys and ports association strings) */
4046 void
4047 ssl_parse_key_list(const ssldecrypt_assoc_t * uats, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp)
4048 {
4049     SslService*        service;
4050     Ssl_private_key_t* private_key, *tmp_private_key;
4051     FILE*              fp     = NULL;
4052     guint32            addr_data[4];
4053     int                addr_len, at;
4054     address_type addr_type[2] = { AT_IPv4, AT_IPv6 };
4055
4056     /* try to load keys file first */
4057     fp = ws_fopen(uats->keyfile, "rb");
4058     if (!fp) {
4059         fprintf(stderr, "Can't open file %s\n",uats->keyfile);
4060         return;
4061     }
4062
4063     for (at = 0; at < 2; at++) {
4064         memset(addr_data, 0, sizeof(addr_data));
4065         addr_len = 0;
4066
4067         /* any: IPv4 or IPv6 wildcard */
4068         /* anyipv4: IPv4 wildcard */
4069         /* anyipv6: IPv6 wildcard */
4070
4071         if(addr_type[at] == AT_IPv4) {
4072             if (strcmp(uats->ipaddr, "any") == 0 || strcmp(uats->ipaddr, "anyipv4") == 0 ||
4073                     get_host_ipaddr(uats->ipaddr, &addr_data[0])) {
4074                 addr_len = 4;
4075             }
4076         } else { /* AT_IPv6 */
4077             if(strcmp(uats->ipaddr, "any") == 0 || strcmp(uats->ipaddr, "anyipv6") == 0 ||
4078                     get_host_ipaddr6(uats->ipaddr, (struct e_in6_addr *) addr_data)) {
4079                 addr_len = 16;
4080             }
4081         }
4082
4083         if (! addr_len) {
4084             continue;
4085         }
4086
4087         if ((gint)strlen(uats->password) == 0) {
4088             private_key = ssl_load_key(fp);
4089         } else {
4090             const char *err = NULL;
4091             private_key = ssl_load_pkcs12(fp, uats->password, &err);
4092             if (err) {
4093                 fprintf(stderr, "%s\n", err);
4094             }
4095         }
4096
4097         if (!private_key) {
4098             fprintf(stderr,"Can't load private key from %s\n", uats->keyfile);
4099             fclose(fp);
4100             return;
4101         }
4102
4103         service = (SslService *)g_malloc(sizeof(SslService) + addr_len);
4104         service->addr.type = addr_type[at];
4105         service->addr.len = addr_len;
4106         service->addr.data = ((guchar*)service) + sizeof(SslService);
4107         memcpy((void*)service->addr.data, addr_data, addr_len);
4108
4109         if(strcmp(uats->port,"start_tls")==0) {
4110             service->port = 0;
4111         } else {
4112             service->port = atoi(uats->port);
4113         }
4114
4115         ssl_debug_printf("ssl_init %s addr '%s' (%s) port '%d' filename '%s' password(only for p12 file) '%s'\n",
4116             (addr_type[at] == AT_IPv4) ? "IPv4" : "IPv6", uats->ipaddr, ep_address_to_str(&service->addr),
4117             service->port, uats->keyfile, uats->password);
4118
4119         ssl_debug_printf("ssl_init private key file %s successfully loaded.\n", uats->keyfile);
4120
4121         /* if item exists, remove first */
4122         tmp_private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, service);
4123         if (tmp_private_key) {
4124             g_hash_table_remove(key_hash, service);
4125             ssl_free_key(tmp_private_key);
4126         }
4127
4128         g_hash_table_insert(key_hash, service, private_key);
4129
4130         ssl_association_add(associations, handle, service->port, uats->protocol, tcp, TRUE);
4131     }
4132
4133     fclose(fp);
4134 }
4135
4136 /* store master secret into session data cache */
4137 void
4138 ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash)
4139 {
4140     /* allocate stringinfo chunks for session id and master secret data*/
4141     StringInfo* session_id;
4142     StringInfo* master_secret;
4143
4144     if (ssl->session_id.data_len == 0) {
4145         ssl_debug_printf("ssl_save_session SessionID is empty!\n");
4146         return;
4147     }
4148
4149     session_id = (StringInfo *)wmem_alloc0(wmem_file_scope(), sizeof(StringInfo) + ssl->session_id.data_len);
4150     master_secret = (StringInfo *)wmem_alloc0(wmem_file_scope(), 48 + sizeof(StringInfo));
4151
4152     master_secret->data = ((guchar*)master_secret+sizeof(StringInfo));
4153
4154     /*  ssl_hash() depends on session_id->data being aligned for guint access
4155      *  so be careful in changing how it is allocated.
4156      */
4157     session_id->data = ((guchar*)session_id+sizeof(StringInfo));
4158
4159     ssl_data_set(session_id, ssl->session_id.data, ssl->session_id.data_len);
4160     ssl_data_set(master_secret, ssl->master_secret.data, ssl->master_secret.data_len);
4161     g_hash_table_insert(session_hash, session_id, master_secret);
4162     ssl_print_string("ssl_save_session stored session id", session_id);
4163     ssl_print_string("ssl_save_session stored master secret", master_secret);
4164 }
4165
4166 gboolean
4167 ssl_restore_session(SslDecryptSession* ssl, GHashTable *session_hash)
4168 {
4169     StringInfo* ms;
4170
4171     if (ssl->session_id.data_len == 0) {
4172         ssl_debug_printf("ssl_restore_session Cannot restore using an empty SessionID\n");
4173         return FALSE;
4174     }
4175
4176     ms = (StringInfo *)g_hash_table_lookup(session_hash, &ssl->session_id);
4177
4178     if (!ms) {
4179         ssl_debug_printf("ssl_restore_session can't find stored session\n");
4180         return FALSE;
4181     }
4182     ssl_data_set(&ssl->master_secret, ms->data, ms->data_len);
4183     ssl->state |= SSL_MASTER_SECRET;
4184     ssl_debug_printf("ssl_restore_session master key retrieved\n");
4185     return TRUE;
4186 }
4187
4188 int
4189 ssl_is_valid_content_type(guint8 type)
4190 {
4191     if ((type >= 0x14) && (type <= 0x18))
4192     {
4193         return 1;
4194     }
4195
4196     return 0;
4197 }
4198
4199 static const unsigned int kRSAMasterSecretLength = 48; /* RFC5246 8.1 */
4200
4201 /* ssl_keylog_parse_session_id parses, from |line|, a string that looks like:
4202  *   RSA Session-ID:<hex session id> Master-Key:<hex TLS master secret>.
4203  *
4204  * It returns TRUE iff the session id matches |ssl_session| and the master
4205  * secret is correctly extracted. */
4206 static gboolean
4207 ssl_keylog_parse_session_id(const char* line,
4208                             SslDecryptSession* ssl_session)
4209 {
4210     gsize len = strlen(line);
4211     unsigned int i;
4212
4213     if (ssl_session->session_id.data_len == 0)
4214         return FALSE;
4215
4216     if (len < 15 || memcmp(line, "RSA Session-ID:", 15) != 0)
4217         return FALSE;
4218     line += 15;
4219     len -= 15;
4220
4221     if (len < ssl_session->session_id.data_len*2)
4222         return FALSE;
4223
4224     for (i = 0; i < ssl_session->session_id.data_len; i++) {
4225         if (from_hex_char(line[2*i]) != (ssl_session->session_id.data[i] >> 4) ||
4226             from_hex_char(line[2*i+1]) != (ssl_session->session_id.data[i] & 15)) {
4227             ssl_debug_printf("    line does not match session id\n");
4228             return FALSE;
4229         }
4230     }
4231
4232     line += 2*i;
4233     len -= 2*i;
4234
4235     if (len != 12 + kRSAMasterSecretLength*2 ||
4236         memcmp(line, " Master-Key:", 12) != 0) {
4237         return FALSE;
4238     }
4239     line += 12;
4240     len -= 12;
4241
4242     if (!from_hex(&ssl_session->master_secret, line, len))
4243         return FALSE;
4244     ssl_session->state &= ~(SSL_PRE_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
4245     ssl_session->state |= SSL_MASTER_SECRET;
4246     ssl_debug_printf("found master secret in key log\n");
4247     return TRUE;
4248 }
4249
4250 /* ssl_keylog_parse_client_random parses, from |line|, a string that looks like:
4251  *   CLIENT_RANDOM <hex client_random> <hex TLS master secret>.
4252  *
4253  * It returns TRUE iff the client_random matches |ssl_session| and the master
4254  * secret is correctly extracted. */
4255 static gboolean
4256 ssl_keylog_parse_client_random(const char* line,
4257                                SslDecryptSession* ssl_session)
4258 {
4259     static const unsigned int kTLSRandomSize = 32; /* RFC5246 A.6 */
4260     gsize len = strlen(line);
4261     unsigned int i;
4262
4263     if (len < 14 || memcmp(line, "CLIENT_RANDOM ", 14) != 0)
4264         return FALSE;
4265     line += 14;
4266     len -= 14;
4267
4268     if (len < kTLSRandomSize*2 ||
4269         ssl_session->client_random.data_len != kTLSRandomSize) {
4270         return FALSE;
4271     }
4272
4273     for (i = 0; i < kTLSRandomSize; i++) {
4274         if (from_hex_char(line[2*i]) != (ssl_session->client_random.data[i] >> 4) ||
4275             from_hex_char(line[2*i+1]) != (ssl_session->client_random.data[i] & 15)) {
4276             ssl_debug_printf("    line does not match client random\n");
4277             return FALSE;
4278         }
4279     }
4280
4281     line += 2*kTLSRandomSize;
4282     len -= 2*kTLSRandomSize;
4283
4284     if (len != 1 + kRSAMasterSecretLength*2 || line[0] != ' ')
4285         return FALSE;
4286     line++;
4287     len--;
4288
4289     if (!from_hex(&ssl_session->master_secret, line, len))
4290         return FALSE;
4291     ssl_session->state &= ~(SSL_PRE_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
4292     ssl_session->state |= SSL_MASTER_SECRET;
4293     ssl_debug_printf("found master secret in key log\n");
4294     return TRUE;
4295 }
4296
4297 /* ssl_keylog_parse_session_id parses, from |line|, a string that looks like:
4298  *   RSA <hex, 8-bytes of encrypted pre-master secret> <hex pre-master secret>.
4299  *
4300  * It returns TRUE iff the session id matches |ssl_session| and the master
4301  * secret is correctly extracted. */
4302 static gboolean
4303 ssl_keylog_parse_rsa_premaster(const char* line,
4304                                SslDecryptSession* ssl_session,
4305                                StringInfo* encrypted_pre_master)
4306 {
4307     static const unsigned int kRSAPremasterLength = 48; /* RFC5246 7.4.7.1 */
4308     gsize len = strlen(line);
4309     unsigned int i;
4310
4311     if (encrypted_pre_master == NULL)
4312         return FALSE;
4313
4314     if (encrypted_pre_master->data_len < 8)
4315         return FALSE;
4316
4317     if (len < 4 || memcmp(line, "RSA ", 4) != 0)
4318         return FALSE;
4319     line += 4;
4320     len -= 4;
4321
4322     if (len < 16)
4323         return FALSE;
4324
4325     for (i = 0; i < 8; i++) {
4326         if (from_hex_char(line[2*i]) != (encrypted_pre_master->data[i] >> 4) ||
4327             from_hex_char(line[2*i+1]) != (encrypted_pre_master->data[i] & 15)) {
4328             ssl_debug_printf("    line does not match encrypted pre-master secret");
4329             return FALSE;
4330         }
4331     }
4332
4333     line += 16;
4334     len -= 16;
4335
4336     if (len != 1 + kRSAPremasterLength*2 || line[0] != ' ')
4337         return FALSE;
4338     line++;
4339     len--;
4340
4341     if (!from_hex(&ssl_session->pre_master_secret, line, len))
4342         return FALSE;
4343     ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
4344     ssl_session->state |= SSL_PRE_MASTER_SECRET;
4345     ssl_debug_printf("found pre-master secret in key log\n");
4346
4347     return TRUE;
4348 }
4349
4350 int
4351 ssl_keylog_lookup(SslDecryptSession* ssl_session,
4352                   const gchar* ssl_keylog_filename,
4353                   StringInfo* encrypted_pre_master) {
4354     FILE* ssl_keylog;
4355     int ret = -1;
4356
4357     if (!ssl_keylog_filename)
4358         return -1;
4359
4360     ssl_debug_printf("trying to use SSL keylog in %s\n", ssl_keylog_filename);
4361
4362     ssl_keylog = ws_fopen(ssl_keylog_filename, "r");
4363     if (!ssl_keylog) {
4364         ssl_debug_printf("failed to open SSL keylog\n");
4365         return -1;
4366     }
4367
4368     /* The format of the file is a series of records with one of the following formats:
4369      *   - "RSA xxxx yyyy"
4370      *     Where xxxx are the first 8 bytes of the encrypted pre-master secret (hex-encoded)
4371      *     Where yyyy is the cleartext pre-master secret (hex-encoded)
4372      *     (this is the original format introduced with bug 4349)
4373      *
4374      *   - "RSA Session-ID:xxxx Master-Key:yyyy"
4375      *     Where xxxx is the SSL session ID (hex-encoded)
4376      *     Where yyyy is the cleartext master secret (hex-encoded)
4377      *     (added to support openssl s_client Master-Key output)
4378      *     This is somewhat is a misnomer because there's nothing RSA specific
4379      *     about this.
4380      *
4381      *   - "CLIENT_RANDOM xxxx yyyy"
4382      *     Where xxxx is the client_random from the ClientHello (hex-encoded)
4383      *     Where yyy is the cleartext master secret (hex-encoded)
4384      *     (This format allows non-RSA SSL connections to be decrypted, i.e.
4385      *     ECDHE-RSA.)
4386      */
4387     for (;;) {
4388         char buf[512], *line;
4389         gsize bytes_read;
4390
4391         line = fgets(buf, sizeof(buf), ssl_keylog);
4392         if (!line)
4393             break;
4394
4395         bytes_read = strlen(line);
4396         /* fgets includes the \n at the end of the line. */
4397         if (bytes_read > 0) {
4398             line[bytes_read - 1] = 0;
4399             bytes_read--;
4400         }
4401         if (bytes_read > 0 && line[bytes_read - 1] == '\r') {
4402             line[bytes_read - 1] = 0;
4403             bytes_read--;
4404         }
4405
4406         ssl_debug_printf("  checking keylog line: %s\n", line);
4407
4408         if (ssl_keylog_parse_session_id(line, ssl_session) ||
4409             ssl_keylog_parse_rsa_premaster(line, ssl_session,
4410                                            encrypted_pre_master) ||
4411             ssl_keylog_parse_client_random(line, ssl_session)) {
4412             ret = 1;
4413             break;
4414         } else {
4415             ssl_debug_printf("    line does not match\n");
4416         }
4417     }
4418
4419     fclose(ssl_keylog);
4420     return ret;
4421 }
4422
4423 #ifdef SSL_DECRYPT_DEBUG
4424
4425 static FILE* ssl_debug_file=NULL;
4426
4427 void
4428 ssl_set_debug(const gchar* name)
4429 {
4430     static gint debug_file_must_be_closed;
4431     gint        use_stderr;
4432
4433     debug_file_must_be_closed = 0;
4434     use_stderr                = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
4435
4436     if (debug_file_must_be_closed)
4437         fclose(ssl_debug_file);
4438     if (use_stderr)
4439         ssl_debug_file = stderr;
4440     else if (!name || (strcmp(name, "") ==0))
4441         ssl_debug_file = NULL;
4442     else
4443         ssl_debug_file = ws_fopen(name, "w");
4444     if (!use_stderr && ssl_debug_file)
4445         debug_file_must_be_closed = 1;
4446
4447     ssl_debug_printf("Wireshark SSL debug log \n\n");
4448 }
4449
4450 void
4451 ssl_debug_flush(void)
4452 {
4453     if (ssl_debug_file)
4454         fflush(ssl_debug_file);
4455 }
4456
4457 void
4458 ssl_debug_printf(const gchar* fmt, ...)
4459 {
4460     va_list ap;
4461
4462     if (!ssl_debug_file)
4463         return;
4464
4465     va_start(ap, fmt);
4466     vfprintf(ssl_debug_file, fmt, ap);
4467     va_end(ap);
4468 }
4469
4470 void
4471 ssl_print_data(const gchar* name, const guchar* data, size_t len)
4472 {
4473     size_t i, j, k;
4474     if (!ssl_debug_file)
4475         return;
4476     fprintf(ssl_debug_file,"%s[%d]:\n",name, (int) len);
4477     for (i=0; i<len; i+=16) {
4478         fprintf(ssl_debug_file,"| ");
4479         for (j=i, k=0; k<16 && j<len; ++j, ++k)
4480             fprintf(ssl_debug_file,"%.2x ",data[j]);
4481         for (; k<16; ++k)
4482             fprintf(ssl_debug_file,"   ");
4483         fputc('|', ssl_debug_file);
4484         for (j=i, k=0; k<16 && j<len; ++j, ++k) {
4485             guchar c = data[j];
4486             if (!isprint(c) || (c=='\t')) c = '.';
4487             fputc(c, ssl_debug_file);
4488         }
4489         for (; k<16; ++k)
4490             fputc(' ', ssl_debug_file);
4491         fprintf(ssl_debug_file,"|\n");
4492     }
4493 }
4494
4495 void
4496 ssl_print_string(const gchar* name, const StringInfo* data)
4497 {
4498     ssl_print_data(name, data->data, data->data_len);
4499 }
4500 #endif /* SSL_DECRYPT_DEBUG */
4501
4502 /* checks for SSL and DTLS UAT key list fields */
4503
4504 gboolean
4505 ssldecrypt_uat_fld_ip_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
4506 {
4507     if (!p || strlen(p) == 0u) {
4508         *err = ep_strdup_printf("No IP address given.");
4509         return FALSE;
4510     }
4511
4512     *err = NULL;
4513     return TRUE;
4514 }
4515
4516 gboolean
4517 ssldecrypt_uat_fld_port_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
4518 {
4519     if (!p || strlen(p) == 0u) {
4520         *err = ep_strdup_printf("No Port given.");
4521         return FALSE;
4522     }
4523
4524     if (strcmp(p, "start_tls") != 0){
4525         const gint i = atoi(p);
4526         if (i < 0 || i > 65535) {
4527             *err = ep_strdup_printf("Invalid port given.");
4528             return FALSE;
4529         }
4530     }
4531
4532     *err = NULL;
4533     return TRUE;
4534 }
4535
4536 gboolean
4537 ssldecrypt_uat_fld_protocol_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
4538 {
4539     if (!p || strlen(p) == 0u) {
4540         *err = ep_strdup_printf("No protocol given.");
4541         return FALSE;
4542     }
4543
4544     if (!find_dissector(p)) {
4545         *err = ep_strdup_printf("Could not find dissector for: '%s'\nValid dissectors are:\n%s", p, ssl_association_info());
4546         return FALSE;
4547     }
4548
4549     *err = NULL;
4550     return TRUE;
4551 }
4552
4553 gboolean
4554 ssldecrypt_uat_fld_fileopen_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
4555 {
4556     ws_statb64 st;
4557
4558     if (!p || strlen(p) == 0u) {
4559         *err = ep_strdup_printf("No filename given.");
4560         return FALSE;
4561     } else {
4562         if (ws_stat64(p, &st) != 0) {
4563             *err = ep_strdup_printf("File '%s' does not exist or access is denied.", p);
4564             return FALSE;
4565         }
4566     }
4567
4568     *err = NULL;
4569     return TRUE;
4570 }
4571
4572 gboolean
4573 ssldecrypt_uat_fld_password_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char ** err)
4574 {
4575     ssldecrypt_assoc_t*  f  = (ssldecrypt_assoc_t *)r;
4576     FILE                *fp = NULL;
4577
4578     if (p && (strlen(p) > 0u)) {
4579         fp = ws_fopen(f->keyfile, "rb");
4580         if (fp) {
4581             const char *msg = NULL;
4582             if (!ssl_load_pkcs12(fp, p, &msg)) {
4583                 fclose(fp);
4584                 *err = ep_strdup_printf("Could not load PKCS#12 key file: %s", msg);
4585                 return FALSE;
4586             }
4587             fclose(fp);
4588         } else {
4589             *err = ep_strdup_printf("Leave this field blank if the keyfile is not PKCS#12.");
4590             return FALSE;
4591         }
4592     }
4593
4594     *err = NULL;
4595     return TRUE;
4596 }
4597
4598
4599 /* dissect a list of hash algorithms, return the number of bytes dissected
4600    this is used for the signature algorithms extension and for the
4601    TLS1.2 certificate request */
4602 gint
4603 ssl_dissect_hash_alg_list(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
4604                           guint32 offset, guint16 len)
4605 {
4606     guint32     offset_start;
4607     proto_tree *subtree, *alg_tree;
4608     proto_item *ti;
4609
4610     offset_start = offset;
4611     if (len==0)
4612         return 0;
4613
4614     ti = proto_tree_add_none_format(tree, hf->hf.hs_sig_hash_algs, tvb,
4615                                     offset, len,
4616                                     "Signature Hash Algorithms (%u algorithm%s)",
4617                                     len / 2, plurality(len / 2, "", "s"));
4618     subtree = proto_item_add_subtree(ti, hf->ett.hs_sig_hash_algs);
4619
4620     if (len % 2) {
4621         proto_tree_add_text(tree, tvb, offset, 2,
4622                             "Invalid Signature Hash Algorithm length: %d", len);
4623         return offset-offset_start;
4624     }
4625
4626     while (len > 0) {
4627         ti = proto_tree_add_item(subtree, hf->hf.hs_sig_hash_alg,
4628                                  tvb, offset, 2, ENC_BIG_ENDIAN);
4629         alg_tree = proto_item_add_subtree(ti, hf->ett.hs_sig_hash_alg);
4630
4631         proto_tree_add_item(alg_tree, hf->hf.hs_sig_hash_hash,
4632                             tvb, offset, 1, ENC_BIG_ENDIAN);
4633         proto_tree_add_item(alg_tree, hf->hf.hs_sig_hash_sig,
4634                             tvb, offset+1, 1, ENC_BIG_ENDIAN);
4635
4636         offset += 2;
4637         len -= 2;
4638     }
4639     return offset-offset_start;
4640 }
4641
4642 static gint
4643 ssl_dissect_hnd_hello_ext_sig_hash_algs(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4644                                         proto_tree *tree, guint32 offset, guint32 ext_len)
4645 {
4646     guint16  sh_alg_length;
4647     gint     ret;
4648
4649     sh_alg_length = tvb_get_ntohs(tvb, offset);
4650     proto_tree_add_uint(tree, hf->hf.hs_sig_hash_alg_len,
4651                         tvb, offset, 2, sh_alg_length);
4652     offset += 2;
4653     if (ext_len < 2 || sh_alg_length != ext_len - 2) {
4654         /* ERROR: sh_alg_length must be 2 less than ext_len */
4655         return offset;
4656     }
4657
4658     ret = ssl_dissect_hash_alg_list(hf, tvb, tree, offset, sh_alg_length);
4659     if (ret >= 0)
4660         offset += ret;
4661
4662     return offset;
4663 }
4664
4665 static gint
4666 ssl_dissect_hnd_hello_ext_alpn(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4667                                proto_tree *tree, guint32 offset, guint32 ext_len)
4668 {
4669     guint16 alpn_length;
4670     guint8 name_length;
4671     proto_tree *alpn_tree;
4672     proto_item *ti;
4673
4674     alpn_length = tvb_get_ntohs(tvb, offset);
4675     if (ext_len < 2 || alpn_length != ext_len - 2) {
4676         /* ERROR: alpn_length must be 2 less than ext_len */
4677         return offset;
4678     }
4679     proto_tree_add_item(tree, hf->hf.hs_ext_alpn_len,
4680                         tvb, offset, 2, ENC_BIG_ENDIAN);
4681     offset += 2;
4682
4683     ti = proto_tree_add_item(tree, hf->hf.hs_ext_alpn_list,
4684                              tvb, offset, alpn_length, ENC_NA);
4685     alpn_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_alpn);
4686
4687     while (alpn_length > 0) {
4688         name_length = tvb_get_guint8(tvb, offset);
4689         proto_tree_add_item(alpn_tree, hf->hf.hs_ext_alpn_str_len,
4690                             tvb, offset, 1, ENC_NA);
4691         offset++;
4692         alpn_length--;
4693         proto_tree_add_item(alpn_tree, hf->hf.hs_ext_alpn_str,
4694                             tvb, offset, name_length, ENC_ASCII|ENC_NA);
4695         offset += name_length;
4696         alpn_length -= name_length;
4697     }
4698
4699     return offset;
4700 }
4701
4702 static gint
4703 ssl_dissect_hnd_hello_ext_npn(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4704                               proto_tree *tree, guint32 offset, guint32 ext_len)
4705 {
4706     guint8      npn_length;
4707     proto_tree *npn_tree;
4708     proto_item *ti;
4709
4710     if (ext_len == 0) {
4711         return offset;
4712     }
4713
4714     ti = proto_tree_add_text(tree, tvb, offset, ext_len, "Next Protocol Negotiation");
4715     npn_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_npn);
4716
4717     while (ext_len > 0) {
4718         npn_length = tvb_get_guint8(tvb, offset);
4719         proto_tree_add_item(npn_tree, hf->hf.hs_ext_npn_str_len,
4720                             tvb, offset, 1, ENC_NA);
4721         offset++;
4722         ext_len--;
4723
4724         if (npn_length > 0) {
4725             tvb_ensure_bytes_exist(tvb, offset, npn_length);
4726             proto_tree_add_item(npn_tree, hf->hf.hs_ext_npn_str,
4727                                 tvb, offset, npn_length, ENC_ASCII|ENC_NA);
4728             offset += npn_length;
4729             ext_len -= npn_length;
4730         }
4731     }
4732
4733     return offset;
4734 }
4735
4736 static gint
4737 ssl_dissect_hnd_hello_ext_reneg_info(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4738                                      proto_tree *tree, guint32 offset, guint32 ext_len)
4739 {
4740     guint8      reneg_info_length;
4741     proto_tree *reneg_info_tree;
4742     proto_item *ti;
4743
4744     if (ext_len == 0) {
4745         return offset;
4746     }
4747
4748     ti = proto_tree_add_text(tree, tvb, offset, ext_len, "Renegotiation Info extension");
4749     reneg_info_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_reneg_info);
4750
4751     reneg_info_length = tvb_get_guint8(tvb, offset);
4752     proto_tree_add_item(reneg_info_tree, hf->hf.hs_ext_reneg_info_len,
4753               tvb, offset, 1, ENC_NA);
4754     offset += 1;
4755
4756     if (reneg_info_length > 0) {
4757         tvb_ensure_bytes_exist(tvb, offset, reneg_info_length);
4758         proto_tree_add_text(reneg_info_tree, tvb, offset, reneg_info_length, "Renegotiation Info");
4759         offset += reneg_info_length;
4760     }
4761
4762     return offset;
4763 }
4764
4765 static gint
4766 ssl_dissect_hnd_hello_ext_server_name(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4767                                       proto_tree *tree, guint32 offset, guint32 ext_len)
4768 {
4769     guint16     server_name_length;
4770     proto_tree *server_name_tree;
4771     proto_item *ti;
4772
4773
4774    if (ext_len == 0) {
4775        return offset;
4776    }
4777
4778    ti = proto_tree_add_text(tree, tvb, offset, ext_len, "Server Name Indication extension");
4779    server_name_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_server_name);
4780
4781    proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name_list_len,
4782                        tvb, offset, 2, ENC_BIG_ENDIAN);
4783    offset += 2;
4784    ext_len -= 2;
4785
4786    while (ext_len > 0) {
4787        proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name_type,
4788                            tvb, offset, 1, ENC_NA);
4789        offset += 1;
4790        ext_len -= 1;
4791
4792        server_name_length = tvb_get_ntohs(tvb, offset);
4793        proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name_len,
4794                            tvb, offset, 2, ENC_BIG_ENDIAN);
4795        offset += 2;
4796        ext_len -= 2;
4797
4798        if (server_name_length > 0) {
4799            tvb_ensure_bytes_exist(tvb, offset, server_name_length);
4800            proto_tree_add_item(server_name_tree, hf->hf.hs_ext_server_name,
4801                                tvb, offset, server_name_length, ENC_ASCII|ENC_NA);
4802            offset += server_name_length;
4803            ext_len -= server_name_length;
4804        }
4805    }
4806    return offset;
4807 }
4808
4809 void
4810 ssl_dissect_hnd_cert_url(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree, guint32 offset)
4811 {
4812     guint16  url_hash_len;
4813
4814     /* enum {
4815      *     individual_certs(0), pkipath(1), (255)
4816      * } CertChainType;
4817      *
4818      * struct {
4819      *     CertChainType type;
4820      *     URLAndHash url_and_hash_list<1..2^16-1>;
4821      * } CertificateURL;
4822      *
4823      * struct {
4824      *     opaque url<1..2^16-1>;
4825      *     unint8 padding;
4826      *     opaque SHA1Hash[20];
4827      * } URLAndHash;
4828      */
4829
4830     proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_type,
4831                         tvb, offset, 1, ENC_NA);
4832     offset++;
4833
4834     url_hash_len = tvb_get_ntohs(tvb, offset);
4835     proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_url_hash_list_len,
4836                         tvb, offset, 2, ENC_BIG_ENDIAN);
4837     offset += 2;
4838     while (url_hash_len-- > 0) {
4839         proto_item  *urlhash_item;
4840         proto_tree  *urlhash_tree;
4841         guint16      url_len;
4842
4843         urlhash_item = proto_tree_add_item(tree, hf->hf.hs_ext_cert_url_item,
4844                                            tvb, offset, -1, ENC_NA);
4845         urlhash_tree = proto_item_add_subtree(urlhash_item, hf->ett.urlhash);
4846
4847         url_len = tvb_get_ntohs(tvb, offset);
4848         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_url_len,
4849                             tvb, offset, 2, ENC_BIG_ENDIAN);
4850         offset += 2;
4851
4852         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_url,
4853                             tvb, offset, url_len, ENC_ASCII|ENC_NA);
4854         offset += url_len;
4855
4856         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_padding,
4857                             tvb, offset, 1, ENC_NA);
4858         offset++;
4859         /* Note: RFC 6066 says that padding must be 0x01 */
4860
4861         proto_tree_add_item(urlhash_tree, hf->hf.hs_ext_cert_url_sha1,
4862                             tvb, offset, 20, ENC_NA);
4863         offset += 20;
4864     }
4865 }
4866
4867 static gint
4868 ssl_dissect_hnd_hello_ext_status_request(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
4869                                          guint32 offset, gboolean has_length)
4870 {
4871     guint    cert_status_type;
4872
4873     cert_status_type = tvb_get_guint8(tvb, offset);
4874     proto_tree_add_item(tree, hf->hf.hs_ext_cert_status_type,
4875                         tvb, offset, 1, ENC_NA);
4876     offset++;
4877
4878     if (has_length) {
4879         proto_tree_add_item(tree, hf->hf.hs_ext_cert_status_request_len,
4880                             tvb, offset, 2, ENC_BIG_ENDIAN);
4881         offset += 2;
4882     }
4883
4884     switch (cert_status_type) {
4885     case SSL_HND_CERT_STATUS_TYPE_OCSP:
4886     case SSL_HND_CERT_STATUS_TYPE_OCSP_MULTI:
4887         {
4888             guint16      responder_id_list_len;
4889             guint16      request_extensions_len;
4890             proto_item  *responder_id;
4891             proto_item  *request_extensions;
4892
4893             responder_id_list_len = tvb_get_ntohs(tvb, offset);
4894             responder_id =
4895                 proto_tree_add_item(tree,
4896                                     hf->hf.hs_ext_cert_status_responder_id_list_len,
4897                                     tvb, offset, 2, ENC_BIG_ENDIAN);
4898             offset += 2;
4899             if (responder_id_list_len != 0) {
4900                 expert_add_info_format(NULL, responder_id,
4901                                        &hf->ei.hs_ext_cert_status_undecoded,
4902                                        "Responder ID list is not implemented, contact Wireshark"
4903                                        " developers if you want this to be supported");
4904                 /* Non-empty responder ID list would mess with extensions. */
4905                 break;
4906             }
4907
4908             request_extensions_len = tvb_get_ntohs(tvb, offset);
4909             request_extensions =
4910                 proto_tree_add_item(tree,
4911                                     hf->hf.hs_ext_cert_status_request_extensions_len, tvb, offset,
4912                                     2, ENC_BIG_ENDIAN);
4913             offset += 2;
4914             if (request_extensions_len != 0)
4915                 expert_add_info_format(NULL, request_extensions,
4916                                        &hf->ei.hs_ext_cert_status_undecoded,
4917                                        "Request Extensions are not implemented, contact"
4918                                        " Wireshark developers if you want this to be supported");
4919             break;
4920         }
4921     }
4922
4923     return offset;
4924 }
4925
4926 static gint
4927 ssl_dissect_hnd_hello_ext_status_request_v2(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
4928                                             guint32 offset)
4929 {
4930     guint   list_len;
4931
4932     list_len = tvb_get_ntoh24(tvb, offset);
4933     offset += 3;
4934
4935     while (list_len-- > 0)
4936         offset = ssl_dissect_hnd_hello_ext_status_request(hf, tvb, tree, offset, TRUE);
4937
4938     return offset;
4939 }
4940
4941 static gint
4942 ssl_dissect_hnd_hello_ext_elliptic_curves(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4943                                           proto_tree *tree, guint32 offset)
4944 {
4945     guint16     curves_length;
4946     proto_tree *curves_tree;
4947     proto_item *ti;
4948
4949     curves_length = tvb_get_ntohs(tvb, offset);
4950     proto_tree_add_item(tree, hf->hf.hs_ext_elliptic_curves_len,
4951                         tvb, offset, 2, ENC_BIG_ENDIAN);
4952
4953     offset += 2;
4954     tvb_ensure_bytes_exist(tvb, offset, curves_length);
4955     ti = proto_tree_add_none_format(tree,
4956                                     hf->hf.hs_ext_elliptic_curves,
4957                                     tvb, offset, curves_length,
4958                                     "Elliptic curves (%d curve%s)",
4959                                     curves_length / 2,
4960                                     plurality(curves_length/2, "", "s"));
4961
4962     /* make this a subtree */
4963     curves_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_curves);
4964
4965     /* loop over all curves */
4966     while (curves_length > 0)
4967     {
4968         proto_tree_add_item(curves_tree, hf->hf.hs_ext_elliptic_curve, tvb, offset, 2, ENC_BIG_ENDIAN);
4969         offset += 2;
4970         curves_length -= 2;
4971     }
4972
4973     return offset;
4974 }
4975
4976 static gint
4977 ssl_dissect_hnd_hello_ext_ec_point_formats(ssl_common_dissect_t *hf, tvbuff_t *tvb,
4978                                            proto_tree *tree, guint32 offset)
4979 {
4980     guint8      ecpf_length;
4981     proto_tree *ecpf_tree;
4982     proto_item *ti;
4983
4984     ecpf_length = tvb_get_guint8(tvb, offset);
4985     proto_tree_add_item(tree, hf->hf.hs_ext_ec_point_formats_len,
4986         tvb, offset, 1, ENC_BIG_ENDIAN);
4987
4988     offset += 1;
4989     tvb_ensure_bytes_exist(tvb, offset, ecpf_length);
4990     ti = proto_tree_add_none_format(tree,
4991                                     hf->hf.hs_ext_elliptic_curves,
4992                                     tvb, offset, ecpf_length,
4993                                     "Elliptic curves point formats (%d)",
4994                                     ecpf_length);
4995
4996     /* make this a subtree */
4997     ecpf_tree = proto_item_add_subtree(ti, hf->ett.hs_ext_curves_point_formats);
4998
4999     /* loop over all point formats */
5000     while (ecpf_length > 0)
5001     {
5002         proto_tree_add_item(ecpf_tree, hf->hf.hs_ext_ec_point_format, tvb, offset, 1, ENC_BIG_ENDIAN);
5003         offset++;
5004         ecpf_length--;
5005     }
5006
5007     return offset;
5008 }
5009
5010 gint
5011 ssl_dissect_hnd_hello_ext(ssl_common_dissect_t *hf, tvbuff_t *tvb, proto_tree *tree,
5012                           guint32 offset, guint32 left, gboolean is_client)
5013 {
5014     guint16     extension_length;
5015     guint16     ext_type;
5016     guint16     ext_len;
5017     proto_item *pi;
5018     proto_tree *ext_tree;
5019
5020     if (left < 2)
5021         return offset;
5022
5023     extension_length = tvb_get_ntohs(tvb, offset);
5024     proto_tree_add_uint(tree, hf->hf.hs_exts_len,
5025                         tvb, offset, 2, extension_length);
5026     offset += 2;
5027     left   -= 2;
5028
5029     while (left >= 4)
5030     {
5031         ext_type = tvb_get_ntohs(tvb, offset);
5032         ext_len  = tvb_get_ntohs(tvb, offset + 2);
5033
5034         pi = proto_tree_add_text(tree, tvb, offset, 4 + ext_len,  "Extension: %s",
5035                                  val_to_str(ext_type,
5036                                             tls_hello_extension_types,
5037                                             "Unknown %u"));
5038         ext_tree = proto_item_add_subtree(pi, hf->ett.hs_ext);
5039         if (!ext_tree)
5040             ext_tree = tree;
5041
5042         proto_tree_add_uint(ext_tree, hf->hf.hs_ext_type,
5043                             tvb, offset, 2, ext_type);
5044         offset += 2;
5045
5046         proto_tree_add_uint(ext_tree, hf->hf.hs_ext_len,
5047                             tvb, offset, 2, ext_len);
5048         offset += 2;
5049
5050         switch (ext_type) {
5051         case SSL_HND_HELLO_EXT_STATUS_REQUEST:
5052             if (is_client)
5053                 offset = ssl_dissect_hnd_hello_ext_status_request(hf, tvb, ext_tree, offset, FALSE);
5054             else
5055                 offset += ext_len; /* server must return empty extension_data */
5056             break;
5057         case SSL_HND_HELLO_EXT_STATUS_REQUEST_V2:
5058             if (is_client)
5059                 offset = ssl_dissect_hnd_hello_ext_status_request_v2(hf, tvb, ext_tree, offset);
5060             else
5061                 offset += ext_len; /* server must return empty extension_data */
5062             break;
5063         case SSL_HND_HELLO_EXT_ELLIPTIC_CURVES:
5064             offset = ssl_dissect_hnd_hello_ext_elliptic_curves(hf, tvb, ext_tree, offset);
5065             break;
5066         case SSL_HND_HELLO_EXT_EC_POINT_FORMATS:
5067             offset = ssl_dissect_hnd_hello_ext_ec_point_formats(hf, tvb, ext_tree, offset);
5068             break;
5069         case SSL_HND_HELLO_EXT_SIG_HASH_ALGS:
5070             offset = ssl_dissect_hnd_hello_ext_sig_hash_algs(hf, tvb, ext_tree, offset, ext_len);
5071             break;
5072         case SSL_HND_HELLO_EXT_ALPN:
5073             offset = ssl_dissect_hnd_hello_ext_alpn(hf, tvb, ext_tree, offset, ext_len);
5074             break;
5075         case SSL_HND_HELLO_EXT_NPN:
5076             offset = ssl_dissect_hnd_hello_ext_npn(hf, tvb, ext_tree, offset, ext_len);
5077             break;
5078         case SSL_HND_HELLO_EXT_RENEG_INFO:
5079             offset = ssl_dissect_hnd_hello_ext_reneg_info(hf, tvb, ext_tree, offset, ext_len);
5080             break;
5081         case SSL_HND_HELLO_EXT_SERVER_NAME:
5082             offset = ssl_dissect_hnd_hello_ext_server_name(hf, tvb, ext_tree, offset, ext_len);
5083             break;
5084         case SSL_HND_HELLO_EXT_HEARTBEAT:
5085             proto_tree_add_item(ext_tree, hf->hf.hs_ext_heartbeat_mode,
5086                                 tvb, offset, 1, ENC_BIG_ENDIAN);
5087             offset += ext_len;
5088             break;
5089         default:
5090             proto_tree_add_bytes_format(ext_tree, hf->hf.hs_ext_data,
5091                                         tvb, offset, ext_len, NULL,
5092                                         "Data (%u byte%s)",
5093                                         ext_len, plurality(ext_len, "", "s"));
5094             offset += ext_len;
5095             break;
5096         }
5097
5098         left -= 2 + 2 + ext_len;
5099     }
5100
5101     return offset;
5102 }
5103
5104 /*
5105  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
5106  *
5107  * Local variables:
5108  * c-basic-offset: 4
5109  * tab-width: 8
5110  * indent-tabs-mode: nil
5111  * End:
5112  *
5113  * vi: set shiftwidth=4 tabstop=8 expandtab:
5114  * :indentSize=4:tabSize=8:noTabs=true:
5115  */