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