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