b2ac8b733ac26cc51f2e2f7f6b5002c4361bd801
[metze/wireshark/wip.git] / epan / dissectors / packet-ssl-utils.c
1 /* packet-ssl-utils.c
2  * ssl manipulation functions
3  * By Paolo Abeni <paolo.abeni@email.com>
4  *
5  * Copyright (c) 2013, Hauke Mehrtens <hauke@hauke-m.de>
6  * Copyright (c) 2014, Peter Wu <peter@lekensteyn.nl>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #ifdef HAVE_LIBZ
30 #include <zlib.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <errno.h>
35
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38 #include <epan/addr_resolv.h>
39 #include <epan/ipv6-utils.h>
40 #include <epan/expert.h>
41 #include <epan/asn1.h>
42 #include <wsutil/file_util.h>
43 #include <wsutil/str_util.h>
44 #include <wsutil/report_err.h>
45 #include <wsutil/pint.h>
46 #include "packet-x509af.h"
47 #include "packet-x509if.h"
48 #include "packet-ssl-utils.h"
49 #include "packet-ssl.h"
50
51 /*
52  * Lookup tables
53  */
54 const value_string ssl_version_short_names[] = {
55     { SSL_VER_UNKNOWN,    "SSL" },
56     { SSL_VER_SSLv2,      "SSLv2" },
57     { SSL_VER_SSLv3,      "SSLv3" },
58     { SSL_VER_TLS,        "TLSv1" },
59     { SSL_VER_TLSv1DOT1,  "TLSv1.1" },
60     { SSL_VER_DTLS,       "DTLSv1.0" },
61     { SSL_VER_DTLS1DOT2,  "DTLSv1.2" },
62     { SSL_VER_DTLS_OPENSSL, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
63     { SSL_VER_PCT,        "PCT" },
64     { SSL_VER_TLSv1DOT2,  "TLSv1.2" },
65     { 0x00, NULL }
66 };
67
68 const value_string ssl_20_msg_types[] = {
69     { SSL2_HND_ERROR,               "Error" },
70     { SSL2_HND_CLIENT_HELLO,        "Client Hello" },
71     { SSL2_HND_CLIENT_MASTER_KEY,   "Client Master Key" },
72     { SSL2_HND_CLIENT_FINISHED,     "Client Finished" },
73     { SSL2_HND_SERVER_HELLO,        "Server Hello" },
74     { SSL2_HND_SERVER_VERIFY,       "Server Verify" },
75     { SSL2_HND_SERVER_FINISHED,     "Server Finished" },
76     { SSL2_HND_REQUEST_CERTIFICATE, "Request Certificate" },
77     { SSL2_HND_CLIENT_CERTIFICATE,  "Client Certificate" },
78     { 0x00, NULL }
79 };
80 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
81 /* Note: sorted by ascending value so value_string-ext can do a binary search */
82 static const value_string ssl_20_cipher_suites[] = {
83     { 0x000000, "TLS_NULL_WITH_NULL_NULL" },
84     { 0x000001, "TLS_RSA_WITH_NULL_MD5" },
85     { 0x000002, "TLS_RSA_WITH_NULL_SHA" },
86     { 0x000003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
87     { 0x000004, "TLS_RSA_WITH_RC4_128_MD5" },
88     { 0x000005, "TLS_RSA_WITH_RC4_128_SHA" },
89     { 0x000006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
90     { 0x000007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
91     { 0x000008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
92     { 0x000009, "TLS_RSA_WITH_DES_CBC_SHA" },
93     { 0x00000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
94     { 0x00000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
95     { 0x00000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
96     { 0x00000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
97     { 0x00000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
98     { 0x00000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
99     { 0x000010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
100     { 0x000011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
101     { 0x000012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
102     { 0x000013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
103     { 0x000014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
104     { 0x000015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
105     { 0x000016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
106     { 0x000017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
107     { 0x000018, "TLS_DH_anon_WITH_RC4_128_MD5" },
108     { 0x000019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
109     { 0x00001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
110     { 0x00001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
111     { 0x00001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
112     { 0x00001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
113 #if 0
114     { 0x00001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
115 #endif
116     /* RFC 2712 */
117     { 0x00001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
118     { 0x00001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
119     { 0x000020, "TLS_KRB5_WITH_RC4_128_SHA" },
120     { 0x000021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
121     { 0x000022, "TLS_KRB5_WITH_DES_CBC_MD5" },
122     { 0x000023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
123     { 0x000024, "TLS_KRB5_WITH_RC4_128_MD5" },
124     { 0x000025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
125     { 0x000026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
126     { 0x000027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
127     { 0x000028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
128     { 0x000029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
129     { 0x00002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
130     { 0x00002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
131     /* RFC 4785 */
132     { 0x00002C, "TLS_PSK_WITH_NULL_SHA" },
133     { 0x00002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
134     { 0x00002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
135     /* RFC 5246 */
136     { 0x00002f, "TLS_RSA_WITH_AES_128_CBC_SHA" },
137     { 0x000030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
138     { 0x000031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
139     { 0x000032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
140     { 0x000033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
141     { 0x000034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
142     { 0x000035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
143     { 0x000036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
144     { 0x000037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
145     { 0x000038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
146     { 0x000039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
147     { 0x00003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
148     { 0x00003B, "TLS_RSA_WITH_NULL_SHA256" },
149     { 0x00003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
150     { 0x00003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
151     { 0x00003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
152     { 0x00003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
153     { 0x000040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
154     { 0x000041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
155     { 0x000042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
156     { 0x000043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
157     { 0x000044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
158     { 0x000045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
159     { 0x000046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
160     { 0x000047, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
161     { 0x000048, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
162     { 0x000049, "TLS_ECDH_ECDSA_WITH_DES_CBC_SHA" },
163     { 0x00004A, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
164     { 0x00004B, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
165     { 0x00004C, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
166     { 0x000060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
167     { 0x000061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
168     { 0x000062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
169     { 0x000063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
170     { 0x000064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
171     { 0x000065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
172     { 0x000066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
173     { 0x000067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
174     { 0x000068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
175     { 0x000069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
176     { 0x00006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
177     { 0x00006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
178     { 0x00006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
179     { 0x00006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
180     /* 0x00,0x6E-83 Unassigned  */
181     { 0x000084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
182     { 0x000085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
183     { 0x000086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
184     { 0x000087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
185     { 0x000088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
186     { 0x000089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
187     /* RFC 4279 */
188     { 0x00008A, "TLS_PSK_WITH_RC4_128_SHA" },
189     { 0x00008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
190     { 0x00008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
191     { 0x00008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
192     { 0x00008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
193     { 0x00008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
194     { 0x000090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
195     { 0x000091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
196     { 0x000092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
197     { 0x000093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
198     { 0x000094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
199     { 0x000095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
200     /* RFC 4162 */
201     { 0x000096, "TLS_RSA_WITH_SEED_CBC_SHA" },
202     { 0x000097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
203     { 0x000098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
204     { 0x000099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
205     { 0x00009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
206     { 0x00009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
207     /* RFC 5288 */
208     { 0x00009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
209     { 0x00009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
210     { 0x00009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
211     { 0x00009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
212     { 0x0000A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
213     { 0x0000A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
214     { 0x0000A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
215     { 0x0000A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
216     { 0x0000A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
217     { 0x0000A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
218     { 0x0000A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
219     { 0x0000A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
220     /* RFC 5487 */
221     { 0x0000A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
222     { 0x0000A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
223     { 0x0000AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
224     { 0x0000AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
225     { 0x0000AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
226     { 0x0000AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
227     { 0x0000AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
228     { 0x0000AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
229     { 0x0000B0, "TLS_PSK_WITH_NULL_SHA256" },
230     { 0x0000B1, "TLS_PSK_WITH_NULL_SHA384" },
231     { 0x0000B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
232     { 0x0000B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
233     { 0x0000B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
234     { 0x0000B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
235     { 0x0000B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
236     { 0x0000B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
237     { 0x0000B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
238     { 0x0000B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
239     /* From RFC 5932 */
240     { 0x0000BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
241     { 0x0000BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
242     { 0x0000BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
243     { 0x0000BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
244     { 0x0000BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
245     { 0x0000BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
246     { 0x0000C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
247     { 0x0000C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
248     { 0x0000C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
249     { 0x0000C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
250     { 0x0000C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
251     { 0x0000C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
252     /* 0x00,0xC6-FE Unassigned  */
253     { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
254     /* 0x01-BF,* Unassigned  */
255     /* From RFC 4492 */
256     { 0x00c001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
257     { 0x00c002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
258     { 0x00c003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
259     { 0x00c004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
260     { 0x00c005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
261     { 0x00c006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
262     { 0x00c007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
263     { 0x00c008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
264     { 0x00c009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
265     { 0x00c00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
266     { 0x00c00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
267     { 0x00c00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
268     { 0x00c00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
269     { 0x00c00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
270     { 0x00c00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
271     { 0x00c010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
272     { 0x00c011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
273     { 0x00c012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
274     { 0x00c013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
275     { 0x00c014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
276     { 0x00c015, "TLS_ECDH_anon_WITH_NULL_SHA" },
277     { 0x00c016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
278     { 0x00c017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
279     { 0x00c018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
280     { 0x00c019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
281     /* RFC 5054 */
282     { 0x00C01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
283     { 0x00C01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
284     { 0x00C01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
285     { 0x00C01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
286     { 0x00C01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
287     { 0x00C01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
288     { 0x00C020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
289     { 0x00C021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
290     { 0x00C022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
291     /* RFC 5589 */
292     { 0x00C023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
293     { 0x00C024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
294     { 0x00C025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
295     { 0x00C026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
296     { 0x00C027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
297     { 0x00C028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
298     { 0x00C029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
299     { 0x00C02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
300     { 0x00C02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
301     { 0x00C02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
302     { 0x00C02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
303     { 0x00C02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
304     { 0x00C02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
305     { 0x00C030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
306     { 0x00C031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
307     { 0x00C032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
308     /* RFC 5489 */
309     { 0x00C033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
310     { 0x00C034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
311     { 0x00C035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
312     { 0x00C036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
313     { 0x00C037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
314     { 0x00C038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
315     { 0x00C039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
316     { 0x00C03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
317     { 0x00C03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
318     /* 0xC0,0x3C-FF Unassigned
319             0xC1-FD,* Unassigned
320             0xFE,0x00-FD Unassigned
321             0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
322             0xFF,0x00-FF Reserved for Private Use [RFC5246]
323             */
324
325     /* http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305 */
326     { 0x00CC13, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
327     { 0x00CC14, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
328     { 0x00CC15, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
329
330     /* http://tools.ietf.org/html/draft-josefsson-salsa20-tls */
331     { 0x00E410, "TLS_RSA_WITH_ESTREAM_SALSA20_SHA1" },
332     { 0x00E411, "TLS_RSA_WITH_SALSA20_SHA1" },
333     { 0x00E412, "TLS_ECDHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
334     { 0x00E413, "TLS_ECDHE_RSA_WITH_SALSA20_SHA1" },
335     { 0x00E414, "TLS_ECDHE_ECDSA_WITH_ESTREAM_SALSA20_SHA1" },
336     { 0x00E415, "TLS_ECDHE_ECDSA_WITH_SALSA20_SHA1" },
337     { 0x00E416, "TLS_PSK_WITH_ESTREAM_SALSA20_SHA1" },
338     { 0x00E417, "TLS_PSK_WITH_SALSA20_SHA1" },
339     { 0x00E418, "TLS_ECDHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
340     { 0x00E419, "TLS_ECDHE_PSK_WITH_SALSA20_SHA1" },
341     { 0x00E41A, "TLS_RSA_PSK_WITH_ESTREAM_SALSA20_SHA1" },
342     { 0x00E41B, "TLS_RSA_PSK_WITH_SALSA20_SHA1" },
343     { 0x00E41C, "TLS_DHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
344     { 0x00E41D, "TLS_DHE_PSK_WITH_SALSA20_SHA1" },
345     { 0x00E41E, "TLS_DHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
346     { 0x00E41F, "TLS_DHE_RSA_WITH_SALSA20_SHA1" },
347
348     /* these from http://www.mozilla.org/projects/
349          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
350     { 0x00fefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
351     { 0x00feff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
352     { 0x00ffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
353     { 0x00ffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
354     /* note that ciphersuites of {0x00????} are TLS cipher suites in
355      * a sslv2 client hello message; the ???? above is the two-byte
356      * tls cipher suite id
357      */
358
359     { 0x010080, "SSL2_RC4_128_WITH_MD5" },
360     { 0x020080, "SSL2_RC4_128_EXPORT40_WITH_MD5" },
361     { 0x030080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
362     { 0x040080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
363     { 0x050080, "SSL2_IDEA_128_CBC_WITH_MD5" },
364     { 0x060040, "SSL2_DES_64_CBC_WITH_MD5" },
365     { 0x0700c0, "SSL2_DES_192_EDE3_CBC_WITH_MD5" },
366     { 0x080080, "SSL2_RC4_64_WITH_MD5" },
367
368     /* Microsoft's old PCT protocol. These are from Eric Rescorla's
369        book "SSL and TLS" */
370     { 0x800001, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509" },
371     { 0x800003, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509_CHAIN" },
372     { 0x810001, "PCT_SSL_HASH_TYPE | PCT1_HASH_MD5" },
373     { 0x810003, "PCT_SSL_HASH_TYPE | PCT1_HASH_SHA" },
374     { 0x820001, "PCT_SSL_EXCH_TYPE | PCT1_EXCH_RSA_PKCS1" },
375     { 0x830004, "PCT_SSL_CIPHER_TYPE_1ST_HALF | PCT1_CIPHER_RC4" },
376     { 0x842840, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_40 | PCT1_MAC_BITS_128" },
377     { 0x848040, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_128 | PCT1_MAC_BITS_128" },
378     { 0x8f8001, "PCT_SSL_COMPAT | PCT_VERSION_1" },
379     { 0x00, NULL }
380 };
381
382 value_string_ext ssl_20_cipher_suites_ext = VALUE_STRING_EXT_INIT(ssl_20_cipher_suites);
383
384
385 const value_string ssl_extension_curves[] = {
386     {  1, "sect163k1" },
387     {  2, "sect163r1" },
388     {  3, "sect163r2" },
389     {  4, "sect193r1" },
390     {  5, "sect193r2" },
391     {  6, "sect233k1" },
392     {  7, "sect233r1" },
393     {  8, "sect239k1" },
394     {  9, "sect283k1" },
395     { 10, "sect283r1" },
396     { 11, "sect409k1" },
397     { 12, "sect409r1" },
398     { 13, "sect571k1" },
399     { 14, "sect571r1" },
400     { 15, "secp160k1" },
401     { 16, "secp160r1" },
402     { 17, "secp160r2" },
403     { 18, "secp192k1" },
404     { 19, "secp192r1" },
405     { 20, "secp224k1" },
406     { 21, "secp224r1" },
407     { 22, "secp256k1" },
408     { 23, "secp256r1" },
409     { 24, "secp384r1" },
410     { 25, "secp521r1" },
411     { 26, "brainpoolP256r1" }, /* RFC 7027 */
412     { 27, "brainpoolP384r1" }, /* RFC 7027 */
413     { 28, "brainpoolP512r1" }, /* RFC 7027 */
414     { 0xFF01, "arbitrary_explicit_prime_curves" },
415     { 0xFF02, "arbitrary_explicit_char2_curves" },
416     { 0x00, NULL }
417 };
418
419 const value_string ssl_curve_types[] = {
420     { 1, "explicit_prime" },
421     { 2, "explicit_char2" },
422     { 3, "named_curve" },
423     { 0x00, NULL }
424 };
425
426 const value_string ssl_extension_ec_point_formats[] = {
427     { 0, "uncompressed" },
428     { 1, "ansiX962_compressed_prime" },
429     { 2, "ansiX962_compressed_char2" },
430     { 0x00, NULL }
431 };
432
433 const value_string ssl_20_certificate_type[] = {
434     { 0x00, "N/A" },
435     { 0x01, "X.509 Certificate" },
436     { 0x00, NULL }
437 };
438
439 const value_string ssl_31_content_type[] = {
440     { 20, "Change Cipher Spec" },
441     { 21, "Alert" },
442     { 22, "Handshake" },
443     { 23, "Application Data" },
444     { 24, "Heartbeat" },
445     { 0x00, NULL }
446 };
447
448 const value_string ssl_versions[] = {
449     { 0xfefd, "DTLS 1.2" },
450     { 0xfeff, "DTLS 1.0" },
451     { 0x0100, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
452     { 0x0303, "TLS 1.2" },
453     { 0x0302, "TLS 1.1" },
454     { 0x0301, "TLS 1.0" },
455     { 0x0300, "SSL 3.0" },
456     { 0x0002, "SSL 2.0" },
457     { 0x00, NULL }
458 };
459
460 #if 0
461 /* XXX - would be used if we dissected the body of a Change Cipher Spec
462    message. */
463 const value_string ssl_31_change_cipher_spec[] = {
464     { 1, "Change Cipher Spec" },
465     { 0x00, NULL }
466 };
467 #endif
468
469 const value_string ssl_31_alert_level[] = {
470     { 1, "Warning" },
471     { 2, "Fatal" },
472     { 0x00, NULL }
473 };
474
475 const value_string ssl_31_alert_description[] = {
476     {   0,  "Close Notify" },
477     {  10,  "Unexpected Message" },
478     {  20,  "Bad Record MAC" },
479     {  21,  "Decryption Failed" },
480     {  22,  "Record Overflow" },
481     {  30,  "Decompression Failure" },
482     {  40,  "Handshake Failure" },
483     {  41,  "No Certificate" },
484     {  42,  "Bad Certificate" },
485     {  43,  "Unsupported Certificate" },
486     {  44,  "Certificate Revoked" },
487     {  45,  "Certificate Expired" },
488     {  46,  "Certificate Unknown" },
489     {  47,  "Illegal Parameter" },
490     {  48,  "Unknown CA" },
491     {  49,  "Access Denied" },
492     {  50,  "Decode Error" },
493     {  51,  "Decrypt Error" },
494     {  60,  "Export Restriction" },
495     {  70,  "Protocol Version" },
496     {  71,  "Insufficient Security" },
497     {  80,  "Internal Error" },
498     {  86,  "Inappropriate Fallback" },
499     {  90,  "User Canceled" },
500     { 100, "No Renegotiation" },
501     { 110, "Unsupported Extension" },
502     { 111, "Certificate Unobtainable" },
503     { 112, "Unrecognized Name" },
504     { 113, "Bad Certificate Status Response" },
505     { 114, "Bad Certificate Hash Value" },
506     { 115, "Unknown PSK Identity" },
507     { 120, "No application Protocol" },
508     { 0x00, NULL }
509 };
510
511 const value_string ssl_31_handshake_type[] = {
512     { SSL_HND_HELLO_REQUEST,     "Hello Request" },
513     { SSL_HND_CLIENT_HELLO,      "Client Hello" },
514     { SSL_HND_SERVER_HELLO,      "Server Hello" },
515     { SSL_HND_HELLO_VERIFY_REQUEST, "Hello Verify Request"},
516     { SSL_HND_NEWSESSION_TICKET, "New Session Ticket" },
517     { SSL_HND_CERTIFICATE,       "Certificate" },
518     { SSL_HND_SERVER_KEY_EXCHG,  "Server Key Exchange" },
519     { SSL_HND_CERT_REQUEST,      "Certificate Request" },
520     { SSL_HND_SVR_HELLO_DONE,    "Server Hello Done" },
521     { SSL_HND_CERT_VERIFY,       "Certificate Verify" },
522     { SSL_HND_CLIENT_KEY_EXCHG,  "Client Key Exchange" },
523     { SSL_HND_FINISHED,          "Finished" },
524     { SSL_HND_CERT_URL,          "Client Certificate URL" },
525     { SSL_HND_CERT_STATUS,       "Certificate Status" },
526     { SSL_HND_SUPPLEMENTAL_DATA, "Supplemental Data" },
527     { SSL_HND_ENCRYPTED_EXTS,    "Encrypted Extensions" },
528     { 0x00, NULL }
529 };
530
531 const value_string tls_heartbeat_type[] = {
532     { 1, "Request" },
533     { 2, "Response" },
534     { 0x00, NULL }
535 };
536
537 const value_string tls_heartbeat_mode[] = {
538     { 1, "Peer allowed to send requests" },
539     { 2, "Peer not allowed to send requests" },
540     { 0x00, NULL }
541 };
542
543 const value_string ssl_31_compression_method[] = {
544     {  0, "null" },
545     {  1, "DEFLATE" },
546     { 64, "LZS" },
547     { 0x00, NULL }
548 };
549
550 #if 0
551 /* XXX - would be used if we dissected a Signature, as would be
552    seen in a server key exchange or certificate verify message. */
553 const value_string ssl_31_key_exchange_algorithm[] = {
554     { 0, "RSA" },
555     { 1, "Diffie Hellman" },
556     { 0x00, NULL }
557 };
558
559 const value_string ssl_31_signature_algorithm[] = {
560     { 0, "Anonymous" },
561     { 1, "RSA" },
562     { 2, "DSA" },
563     { 0x00, NULL }
564 };
565 #endif
566
567 const value_string ssl_31_client_certificate_type[] = {
568     { 1, "RSA Sign" },
569     { 2, "DSS Sign" },
570     { 3, "RSA Fixed DH" },
571     { 4, "DSS Fixed DH" },
572     /* GOST certificate types */
573     /* Section 3.5 of draft-chudov-cryptopro-cptls-04 */
574     { 21, "GOST R 34.10-94" },
575     { 22, "GOST R 34.10-2001" },
576     /* END GOST certificate types */
577     { 64, "ECDSA Sign" },
578     { 65, "RSA Fixed ECDH" },
579     { 66, "ECDSA Fixed ECDH" },
580     { 0x00, NULL }
581 };
582
583 #if 0
584 /* XXX - would be used if we dissected exchange keys, as would be
585    seen in a client key exchange message. */
586 const value_string ssl_31_public_value_encoding[] = {
587     { 0, "Implicit" },
588     { 1, "Explicit" },
589     { 0x00, NULL }
590 };
591 #endif
592
593 /* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml */
594 /* Note: sorted by ascending value so value_string_ext fcns can do a binary search */
595 static const value_string ssl_31_ciphersuite[] = {
596     /* RFC 2246, RFC 4346, RFC 5246 */
597     { 0x0000, "TLS_NULL_WITH_NULL_NULL" },
598     { 0x0001, "TLS_RSA_WITH_NULL_MD5" },
599     { 0x0002, "TLS_RSA_WITH_NULL_SHA" },
600     { 0x0003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
601     { 0x0004, "TLS_RSA_WITH_RC4_128_MD5" },
602     { 0x0005, "TLS_RSA_WITH_RC4_128_SHA" },
603     { 0x0006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
604     { 0x0007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
605     { 0x0008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
606     { 0x0009, "TLS_RSA_WITH_DES_CBC_SHA" },
607     { 0x000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
608     { 0x000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
609     { 0x000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
610     { 0x000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
611     { 0x000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
612     { 0x000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
613     { 0x0010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
614     { 0x0011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
615     { 0x0012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
616     { 0x0013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
617     { 0x0014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
618     { 0x0015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
619     { 0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
620     { 0x0017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
621     { 0x0018, "TLS_DH_anon_WITH_RC4_128_MD5" },
622     { 0x0019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
623     { 0x001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
624     { 0x001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
625
626     { 0x001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
627     { 0x001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
628 #if 0 /* Because it clashes with KRB5, is never used any more, and is safe
629          to remove according to David Hopwood <david.hopwood@zetnet.co.uk>
630          of the ietf-tls list */
631     { 0x001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
632 #endif
633
634     /* RFC 2712 */
635     { 0x001E, "TLS_KRB5_WITH_DES_CBC_SHA" },
636     { 0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" },
637     { 0x0020, "TLS_KRB5_WITH_RC4_128_SHA" },
638     { 0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA" },
639     { 0x0022, "TLS_KRB5_WITH_DES_CBC_MD5" },
640     { 0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5" },
641     { 0x0024, "TLS_KRB5_WITH_RC4_128_MD5" },
642     { 0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5" },
643     { 0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA" },
644     { 0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA" },
645     { 0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA" },
646     { 0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5" },
647     { 0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5" },
648     { 0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5" },
649
650     /* RFC 4785 */
651     { 0x002C, "TLS_PSK_WITH_NULL_SHA" },
652     { 0x002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
653     { 0x002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
654
655     /* RFC 5246 */
656     { 0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA" },
657     { 0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
658     { 0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
659     { 0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
660     { 0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
661     { 0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
662     { 0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
663     { 0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
664     { 0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
665     { 0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
666     { 0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
667     { 0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
668     { 0x003B, "TLS_RSA_WITH_NULL_SHA256" },
669     { 0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256" },
670     { 0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256" },
671     { 0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256" },
672     { 0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256" },
673     { 0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" },
674
675     /* RFC 4132 */
676     { 0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
677     { 0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
678     { 0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
679     { 0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
680     { 0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
681     { 0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
682
683     /* 0x00,0x60-66 Reserved to avoid conflicts with widely deployed implementations  */
684     /* --- ??? --- */
685     { 0x0060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
686     { 0x0061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
687     /* draft-ietf-tls-56-bit-ciphersuites-01.txt */
688     { 0x0062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
689     { 0x0063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
690     { 0x0064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
691     { 0x0065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
692     { 0x0066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
693     /* --- ??? ---*/
694
695     { 0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" },
696     { 0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256" },
697     { 0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256" },
698     { 0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" },
699     { 0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
700     { 0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
701     { 0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
702
703     /* draft-chudov-cryptopro-cptls-04.txt */
704     { 0x0080,  "TLS_GOSTR341094_WITH_28147_CNT_IMIT" },
705     { 0x0081,  "TLS_GOSTR341001_WITH_28147_CNT_IMIT" },
706     { 0x0082,  "TLS_GOSTR341094_WITH_NULL_GOSTR3411" },
707     { 0x0083,  "TLS_GOSTR341001_WITH_NULL_GOSTR3411" },
708
709     /* RFC 4132 */
710     { 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
711     { 0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
712     { 0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
713     { 0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
714     { 0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
715     { 0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
716
717     /* RFC 4279 */
718     { 0x008A, "TLS_PSK_WITH_RC4_128_SHA" },
719     { 0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA" },
720     { 0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA" },
721     { 0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA" },
722     { 0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA" },
723     { 0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" },
724     { 0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" },
725     { 0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" },
726     { 0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA" },
727     { 0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" },
728     { 0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" },
729     { 0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" },
730
731     /* RFC 4162 */
732     { 0x0096, "TLS_RSA_WITH_SEED_CBC_SHA" },
733     { 0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA" },
734     { 0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA" },
735     { 0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA" },
736     { 0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA" },
737     { 0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA" },
738
739     /* RFC 5288 */
740     { 0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256" },
741     { 0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384" },
742     { 0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" },
743     { 0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" },
744     { 0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256" },
745     { 0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384" },
746     { 0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" },
747     { 0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" },
748     { 0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256" },
749     { 0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384" },
750     { 0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256" },
751     { 0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384" },
752
753     /* RFC 5487 */
754     { 0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256" },
755     { 0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384" },
756     { 0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" },
757     { 0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" },
758     { 0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" },
759     { 0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" },
760     { 0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256" },
761     { 0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384" },
762     { 0x00B0, "TLS_PSK_WITH_NULL_SHA256" },
763     { 0x00B1, "TLS_PSK_WITH_NULL_SHA384" },
764     { 0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" },
765     { 0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" },
766     { 0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256" },
767     { 0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384" },
768     { 0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" },
769     { 0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" },
770     { 0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256" },
771     { 0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384" },
772
773     /* From RFC 5932 */
774     { 0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
775     { 0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
776     { 0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
777     { 0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" },
778     { 0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
779     { 0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" },
780     { 0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
781     { 0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
782     { 0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
783     { 0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
784     { 0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
785     { 0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
786     /* 0x00,0xC6-FE Unassigned  */
787     /* From RFC 5746 */
788     { 0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
789     /* From RFC 7507 */
790     { 0x5600, "TLS_FALLBACK_SCSV" },
791     /* From RFC 4492 */
792     { 0xc001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
793     { 0xc002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
794     { 0xc003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
795     { 0xc004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
796     { 0xc005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
797     { 0xc006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA" },
798     { 0xc007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" },
799     { 0xc008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" },
800     { 0xc009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" },
801     { 0xc00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" },
802     { 0xc00b, "TLS_ECDH_RSA_WITH_NULL_SHA" },
803     { 0xc00c, "TLS_ECDH_RSA_WITH_RC4_128_SHA" },
804     { 0xc00d, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA" },
805     { 0xc00e, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA" },
806     { 0xc00f, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA" },
807     { 0xc010, "TLS_ECDHE_RSA_WITH_NULL_SHA" },
808     { 0xc011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA" },
809     { 0xc012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" },
810     { 0xc013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" },
811     { 0xc014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" },
812     { 0xc015, "TLS_ECDH_anon_WITH_NULL_SHA" },
813     { 0xc016, "TLS_ECDH_anon_WITH_RC4_128_SHA" },
814     { 0xc017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" },
815     { 0xc018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" },
816     { 0xc019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" },
817
818     /* RFC 5054 */
819     { 0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" },
820     { 0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" },
821     { 0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" },
822     { 0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" },
823     { 0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" },
824     { 0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" },
825     { 0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" },
826     { 0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" },
827     { 0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" },
828
829     /* RFC 5589 */
830     { 0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" },
831     { 0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" },
832     { 0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256" },
833     { 0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384" },
834     { 0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" },
835     { 0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" },
836     { 0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256" },
837     { 0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384" },
838     { 0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" },
839     { 0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" },
840     { 0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256" },
841     { 0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384" },
842     { 0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" },
843     { 0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" },
844     { 0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256" },
845     { 0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384" },
846
847     /* RFC 5489 */
848     { 0xC033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA" },
849     { 0xC034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" },
850     { 0xC035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" },
851     { 0xC036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" },
852     { 0xC037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" },
853     { 0xC038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" },
854     { 0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
855     { 0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
856     { 0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
857
858     /* RFC 6209 */
859     { 0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256" },
860     { 0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384" },
861     { 0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256" },
862     { 0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384" },
863     { 0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256" },
864     { 0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384" },
865     { 0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256" },
866     { 0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384" },
867     { 0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256" },
868     { 0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384" },
869     { 0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256" },
870     { 0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384" },
871     { 0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256" },
872     { 0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384" },
873     { 0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256" },
874     { 0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384" },
875     { 0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256" },
876     { 0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384" },
877     { 0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256" },
878     { 0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384" },
879     { 0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256" },
880     { 0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384" },
881     { 0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256" },
882     { 0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384" },
883     { 0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256" },
884     { 0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384" },
885     { 0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256" },
886     { 0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384" },
887     { 0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256" },
888     { 0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384" },
889     { 0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256" },
890     { 0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384" },
891     { 0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256" },
892     { 0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384" },
893     { 0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256" },
894     { 0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384" },
895     { 0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256" },
896     { 0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384" },
897     { 0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256" },
898     { 0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384" },
899     { 0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256" },
900     { 0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384" },
901     { 0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256" },
902     { 0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384" },
903     { 0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256" },
904     { 0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384" },
905     { 0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256" },
906     { 0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384" },
907     { 0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256" },
908     { 0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384" },
909     { 0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256" },
910     { 0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384" },
911     { 0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256" },
912     { 0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384" },
913
914     /* RFC 6367 */
915     { 0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
916     { 0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
917     { 0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
918     { 0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
919     { 0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
920     { 0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
921     { 0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
922     { 0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
923     { 0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
924     { 0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
925     { 0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
926     { 0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
927     { 0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
928     { 0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
929     { 0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
930     { 0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
931     { 0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
932     { 0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
933     { 0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256" },
934     { 0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384" },
935     { 0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
936     { 0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
937     { 0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
938     { 0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
939     { 0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
940     { 0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
941     { 0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
942     { 0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
943     { 0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
944     { 0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
945     { 0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
946     { 0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
947     { 0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
948     { 0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
949     { 0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
950     { 0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
951     { 0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
952     { 0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
953     { 0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
954     { 0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
955     { 0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
956     { 0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
957
958     /* RFC 6655 */
959     { 0xC09C, "TLS_RSA_WITH_AES_128_CCM" },
960     { 0xC09D, "TLS_RSA_WITH_AES_256_CCM" },
961     { 0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM" },
962     { 0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM" },
963     { 0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8" },
964     { 0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8" },
965     { 0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8" },
966     { 0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8" },
967     { 0xC0A4, "TLS_PSK_WITH_AES_128_CCM" },
968     { 0xC0A5, "TLS_PSK_WITH_AES_256_CCM" },
969     { 0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM" },
970     { 0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM" },
971     { 0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8" },
972     { 0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8" },
973     { 0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8" },
974     { 0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8" },
975
976     /* RFC 7251 */
977     { 0xC0AC, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM" },
978     { 0xC0AD, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM" },
979     { 0xC0AE, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" },
980     { 0xC0AF, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8" },
981 /*
982 0xC0,0xAB-FF Unassigned
983 0xC1-FD,* Unassigned
984 0xFE,0x00-FD Unassigned
985 0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
986 0xFF,0x00-FF Reserved for Private Use [RFC5246]
987 */
988
989     /* http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305 */
990     { 0xCC13, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
991     { 0xCC14, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" },
992     { 0xCC15, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" },
993
994     /* http://tools.ietf.org/html/draft-josefsson-salsa20-tls */
995     { 0xE410, "TLS_RSA_WITH_ESTREAM_SALSA20_SHA1" },
996     { 0xE411, "TLS_RSA_WITH_SALSA20_SHA1" },
997     { 0xE412, "TLS_ECDHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
998     { 0xE413, "TLS_ECDHE_RSA_WITH_SALSA20_SHA1" },
999     { 0xE414, "TLS_ECDHE_ECDSA_WITH_ESTREAM_SALSA20_SHA1" },
1000     { 0xE415, "TLS_ECDHE_ECDSA_WITH_SALSA20_SHA1" },
1001     { 0xE416, "TLS_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1002     { 0xE417, "TLS_PSK_WITH_SALSA20_SHA1" },
1003     { 0xE418, "TLS_ECDHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1004     { 0xE419, "TLS_ECDHE_PSK_WITH_SALSA20_SHA1" },
1005     { 0xE41A, "TLS_RSA_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1006     { 0xE41B, "TLS_RSA_PSK_WITH_SALSA20_SHA1" },
1007     { 0xE41C, "TLS_DHE_PSK_WITH_ESTREAM_SALSA20_SHA1" },
1008     { 0xE41D, "TLS_DHE_PSK_WITH_SALSA20_SHA1" },
1009     { 0xE41E, "TLS_DHE_RSA_WITH_ESTREAM_SALSA20_SHA1" },
1010     { 0xE41F, "TLS_DHE_RSA_WITH_SALSA20_SHA1" },
1011
1012     /* these from http://www.mozilla.org/projects/
1013          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
1014     { 0xfefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
1015     { 0xfeff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
1016     { 0xffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
1017     { 0xffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
1018     /* note that ciphersuites 0xff00 - 0xffff are private */
1019     { 0x00, NULL }
1020 };
1021
1022 value_string_ext ssl_31_ciphersuite_ext = VALUE_STRING_EXT_INIT(ssl_31_ciphersuite);
1023
1024
1025 const value_string pct_msg_types[] = {
1026     { PCT_MSG_CLIENT_HELLO,         "Client Hello" },
1027     { PCT_MSG_SERVER_HELLO,         "Server Hello" },
1028     { PCT_MSG_CLIENT_MASTER_KEY,    "Client Master Key" },
1029     { PCT_MSG_SERVER_VERIFY,        "Server Verify" },
1030     { PCT_MSG_ERROR,                "Error" },
1031     { 0x00, NULL }
1032 };
1033
1034 const value_string pct_cipher_type[] = {
1035     { PCT_CIPHER_DES, "DES" },
1036     { PCT_CIPHER_IDEA, "IDEA" },
1037     { PCT_CIPHER_RC2, "RC2" },
1038     { PCT_CIPHER_RC4, "RC4" },
1039     { PCT_CIPHER_DES_112, "DES 112 bit" },
1040     { PCT_CIPHER_DES_168, "DES 168 bit" },
1041     { 0x00, NULL }
1042 };
1043
1044 const value_string pct_hash_type[] = {
1045     { PCT_HASH_MD5, "MD5" },
1046     { PCT_HASH_MD5_TRUNC_64, "MD5_TRUNC_64"},
1047     { PCT_HASH_SHA, "SHA"},
1048     { PCT_HASH_SHA_TRUNC_80, "SHA_TRUNC_80"},
1049     { PCT_HASH_DES_DM, "DES_DM"},
1050     { 0x00, NULL }
1051 };
1052
1053 const value_string pct_cert_type[] = {
1054     { PCT_CERT_NONE, "None" },
1055     { PCT_CERT_X509, "X.509" },
1056     { PCT_CERT_PKCS7, "PKCS #7" },
1057     { 0x00, NULL }
1058 };
1059 const value_string pct_sig_type[] = {
1060     { PCT_SIG_NONE, "None" },
1061     { PCT_SIG_RSA_MD5, "MD5" },
1062     { PCT_SIG_RSA_SHA, "RSA SHA" },
1063     { PCT_SIG_DSA_SHA, "DSA SHA" },
1064     { 0x00, NULL }
1065 };
1066
1067 const value_string pct_exch_type[] = {
1068     { PCT_EXCH_RSA_PKCS1, "RSA PKCS#1" },
1069     { PCT_EXCH_RSA_PKCS1_TOKEN_DES, "RSA PKCS#1 Token DES" },
1070     { PCT_EXCH_RSA_PKCS1_TOKEN_DES3, "RSA PKCS#1 Token 3DES" },
1071     { PCT_EXCH_RSA_PKCS1_TOKEN_RC2, "RSA PKCS#1 Token RC-2" },
1072     { PCT_EXCH_RSA_PKCS1_TOKEN_RC4, "RSA PKCS#1 Token RC-4" },
1073     { PCT_EXCH_DH_PKCS3, "DH PKCS#3" },
1074     { PCT_EXCH_DH_PKCS3_TOKEN_DES, "DH PKCS#3 Token DES" },
1075     { PCT_EXCH_DH_PKCS3_TOKEN_DES3, "DH PKCS#3 Token 3DES" },
1076     { PCT_EXCH_FORTEZZA_TOKEN, "Fortezza" },
1077     { 0x00, NULL }
1078 };
1079
1080 const value_string pct_error_code[] = {
1081     { PCT_ERR_BAD_CERTIFICATE, "PCT_ERR_BAD_CERTIFICATE" },
1082     { PCT_ERR_CLIENT_AUTH_FAILED, "PCT_ERR_CLIENT_AUTH_FAILE" },
1083     { PCT_ERR_ILLEGAL_MESSAGE, "PCT_ERR_ILLEGAL_MESSAGE" },
1084     { PCT_ERR_INTEGRITY_CHECK_FAILED, "PCT_ERR_INTEGRITY_CHECK_FAILED" },
1085     { PCT_ERR_SERVER_AUTH_FAILED, "PCT_ERR_SERVER_AUTH_FAILED" },
1086     { PCT_ERR_SPECS_MISMATCH, "PCT_ERR_SPECS_MISMATCH" },
1087     { 0x00, NULL }
1088 };
1089
1090 /* http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-1 */
1091 const value_string tls_hello_extension_types[] = {
1092     { SSL_HND_HELLO_EXT_SERVER_NAME, "server_name" }, /* RFC 3546 */
1093     { 1, "max_fragment_length" },
1094     { 2, "client_certificate_url" },
1095     { 3, "trusted_ca_keys" },
1096     { 4, "truncated_hmac" },
1097     { SSL_HND_HELLO_EXT_STATUS_REQUEST, "status_request" }, /* RFC 6066 */
1098     { 6, "user_mapping" },  /* RFC 4681 */
1099     { 7, "client_authz" },
1100     { 8, "server_authz" },
1101     { SSL_HND_HELLO_EXT_CERT_TYPE, "cert_type" },  /* RFC 5081 */
1102     { SSL_HND_HELLO_EXT_ELLIPTIC_CURVES, "elliptic_curves" },  /* RFC 4492 */
1103     { SSL_HND_HELLO_EXT_EC_POINT_FORMATS, "ec_point_formats" },  /* RFC 4492 */
1104     { 12, "srp" },  /* RFC 5054 */
1105     { 13, "signature_algorithms" },  /* RFC 5246 */
1106     { 14, "use_srtp" },
1107     { SSL_HND_HELLO_EXT_HEARTBEAT, "Heartbeat" },  /* RFC 6520 */
1108     { SSL_HND_HELLO_EXT_ALPN, "Application Layer Protocol Negotiation" }, /* RFC 7301 */
1109     { SSL_HND_HELLO_EXT_STATUS_REQUEST_V2, "status_request_v2" }, /* RFC 6961 */
1110     { 18, "signed_certificate_timestamp" }, /* RFC 6962 */
1111     { SSL_HND_HELLO_EXT_CLIENT_CERT_TYPE, "client_certificate_type" }, /* RFC 7250 */
1112     { SSL_HND_HELLO_EXT_SERVER_CERT_TYPE, "server_certificate_type" }, /* RFC 7250 */
1113     { SSL_HND_HELLO_EXT_PADDING, "Padding" }, /* http://tools.ietf.org/html/draft-agl-tls-padding */
1114     { SSL_HND_HELLO_EXT_EXTENDED_MASTER_SECRET_TYPE, "Extended Master Secret" }, /* https://tools.ietf.org/html/draft-ietf-tls-session-hash-01 */
1115     { SSL_HND_HELLO_EXT_SESSION_TICKET, "SessionTicket TLS" },  /* RFC 4507 */
1116     { SSL_HND_HELLO_EXT_NPN, "next_protocol_negotiation"}, /* http://technotes.googlecode.com/git/nextprotoneg.html */
1117     { SSL_HND_HELLO_EXT_RENEG_INFO, "renegotiation_info" }, /* RFC 5746 */
1118     /* http://tools.ietf.org/html/draft-balfanz-tls-channelid-00
1119        https://twitter.com/ericlaw/status/274237352531083264 */
1120     { SSL_HND_HELLO_EXT_CHANNEL_ID_OLD, "channel_id_old" },
1121     /* http://tools.ietf.org/html/draft-balfanz-tls-channelid-01
1122        https://code.google.com/p/chromium/codesearch#chromium/src/net/third_party/nss/ssl/sslt.h&l=209 */
1123     { SSL_HND_HELLO_EXT_CHANNEL_ID, "channel_id" },
1124     { 0, NULL }
1125 };
1126
1127 const value_string tls_hello_ext_server_name_type_vs[] = {
1128     { 0, "host_name" },
1129     { 0, NULL }
1130 };
1131
1132 /* RFC 5246 7.4.1.4.1 */
1133 const value_string tls_hash_algorithm[] = {
1134     { 0, "None" },
1135     { 1, "MD5" },
1136     { 2, "SHA1" },
1137     { 3, "SHA224" },
1138     { 4, "SHA256" },
1139     { 5, "SHA384" },
1140     { 6, "SHA512" },
1141     { 0, NULL }
1142 };
1143
1144 const value_string tls_signature_algorithm[] = {
1145     { 0, "Anonymous" },
1146     { 1, "RSA" },
1147     { 2, "DSA" },
1148     { 3, "ECDSA" },
1149     { 0, NULL }
1150 };
1151
1152 /* RFC 6091 3.1 */
1153 const value_string tls_certificate_type[] = {
1154     { 0, "X.509" },
1155     { 1, "OpenPGP" },
1156     { SSL_HND_CERT_TYPE_RAW_PUBLIC_KEY, "Raw Public Key" }, /* RFC 7250 */
1157     { 0, NULL }
1158 };
1159
1160 const value_string tls_cert_chain_type[] = {
1161     { SSL_HND_CERT_URL_TYPE_INDIVIDUAL_CERT,    "Individual Certificates" },
1162     { SSL_HND_CERT_URL_TYPE_PKIPATH,            "PKI Path" },
1163     { 0, NULL }
1164 };
1165
1166 const value_string tls_cert_status_type[] = {
1167     { SSL_HND_CERT_STATUS_TYPE_OCSP,            "OCSP" },
1168     { SSL_HND_CERT_STATUS_TYPE_OCSP_MULTI,      "OCSP Multi" },
1169     { 0, NULL }
1170 };
1171
1172 /* string_string is inappropriate as it compares strings while
1173  * "byte strings MUST NOT be truncated" (RFC 7301) */
1174 typedef struct ssl_alpn_protocol {
1175     const guint8    *proto_name;
1176     size_t           proto_name_len;
1177     const char      *dissector_name;
1178 } ssl_alpn_protocol_t;
1179 /* http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids */
1180 static const ssl_alpn_protocol_t ssl_alpn_protocols[] = {
1181     { "http/1.1",   sizeof("http/1.1"),     "http" },
1182     /* SPDY moves so fast, just 1, 2 and 3 are registered with IANA but there
1183      * already exists 3.1 as of this writing... match the prefix. */
1184     { "spdy/",      sizeof("spdy/") - 1,    "spdy" },
1185     { "stun.turn",  sizeof("stun.turn"),    "turnchannel" },
1186     { "stun.nat-discovery", sizeof("stun.nat-discovery"), "stun" },
1187     /* draft-ietf-httpbis-http2-16 */
1188     { "h2-",         sizeof("h2-") - 1,     "http2" }, /* draft versions */
1189     { "h2",          sizeof("h2"),          "http2" }, /* final version */
1190 };
1191
1192 /* we keep this internal to packet-ssl-utils, as there should be
1193    no need to access it any other way.
1194
1195    This also allows us to hide the dependency on zlib.
1196 */
1197 struct _SslDecompress {
1198     gint compression;
1199 #ifdef HAVE_LIBZ
1200     z_stream istream;
1201 #endif
1202 };
1203
1204 /* To assist in parsing client/server key exchange messages
1205    0 indicates unknown */
1206 gint ssl_get_keyex_alg(gint cipher)
1207 {
1208     switch(cipher) {
1209     case 0x0001:
1210     case 0x0002:
1211     case 0x0003:
1212     case 0x0004:
1213     case 0x0005:
1214     case 0x0006:
1215     case 0x0007:
1216     case 0x0008:
1217     case 0x0009:
1218     case 0x000a:
1219     case 0x002f:
1220     case 0x0035:
1221     case 0x003b:
1222     case 0x003c:
1223     case 0x003d:
1224     case 0x0041:
1225     case 0x0060:
1226     case 0x0061:
1227     case 0x0062:
1228     case 0x0064:
1229     case 0x0084:
1230     case 0x0096:
1231     case 0x009c:
1232     case 0x009d:
1233     case 0x00ba:
1234     case 0x00c0:
1235     case 0xfefe:
1236     case 0xfeff:
1237     case 0xffe0:
1238     case 0xffe1:
1239         return KEX_RSA;
1240     case 0x000b:
1241     case 0x000c:
1242     case 0x000d:
1243     case 0x000e:
1244     case 0x000f:
1245     case 0x0010:
1246     case 0x0011:
1247     case 0x0012:
1248     case 0x0013:
1249     case 0x0014:
1250     case 0x0015:
1251     case 0x0016:
1252     case 0x0017:
1253     case 0x0018:
1254     case 0x0019:
1255     case 0x001a:
1256     case 0x001b:
1257     case 0x002d:
1258     case 0x0030:
1259     case 0x0031:
1260     case 0x0032:
1261     case 0x0033:
1262     case 0x0034:
1263     case 0x0036:
1264     case 0x0037:
1265     case 0x0038:
1266     case 0x0039:
1267     case 0x003a:
1268     case 0x003e:
1269     case 0x003f:
1270     case 0x0040:
1271     case 0x0042:
1272     case 0x0043:
1273     case 0x0044:
1274     case 0x0045:
1275     case 0x0046:
1276     case 0x0063:
1277     case 0x0065:
1278     case 0x0066:
1279     case 0x0067:
1280     case 0x0068:
1281     case 0x0069:
1282     case 0x006a:
1283     case 0x006b:
1284     case 0x006c:
1285     case 0x006d:
1286     case 0x0085:
1287     case 0x0086:
1288     case 0x0087:
1289     case 0x0088:
1290     case 0x0089:
1291     case 0x008e:
1292     case 0x008f:
1293     case 0x0090:
1294     case 0x0091:
1295     case 0x0097:
1296     case 0x0098:
1297     case 0x0099:
1298     case 0x009a:
1299     case 0x009b:
1300     case 0x009e:
1301     case 0x009f:
1302     case 0x00a0:
1303     case 0x00a1:
1304     case 0x00a2:
1305     case 0x00a3:
1306     case 0x00a4:
1307     case 0x00a5:
1308     case 0x00a6:
1309     case 0x00a7:
1310     case 0x00aa:
1311     case 0x00ab:
1312     case 0x00b2:
1313     case 0x00b3:
1314     case 0x00b4:
1315     case 0x00b5:
1316     case 0x00bb:
1317     case 0x00bc:
1318     case 0x00bd:
1319     case 0x00be:
1320     case 0x00bf:
1321     case 0x00c1:
1322     case 0x00c2:
1323     case 0x00c3:
1324     case 0x00c4:
1325     case 0x00c5:
1326         return KEX_DH;
1327     case 0xc001:
1328     case 0xc002:
1329     case 0xc003:
1330     case 0xc004:
1331     case 0xc005:
1332     case 0xc006:
1333     case 0xc007:
1334     case 0xc008:
1335     case 0xc009:
1336     case 0xc00a:
1337     case 0xc00b:
1338     case 0xc00c:
1339     case 0xc00d:
1340     case 0xc00e:
1341     case 0xc00f:
1342     case 0xc010:
1343     case 0xc011:
1344     case 0xc012:
1345     case 0xc013:
1346     case 0xc014:
1347     case 0xc015:
1348     case 0xc016:
1349     case 0xc017:
1350     case 0xc018:
1351     case 0xc019:
1352     case 0xc023:
1353     case 0xc024:
1354     case 0xc025:
1355     case 0xc026:
1356     case 0xc027:
1357     case 0xc028:
1358     case 0xc029:
1359     case 0xc02a:
1360     case 0xc02b:
1361     case 0xc02c:
1362     case 0xc02d:
1363     case 0xc02e:
1364     case 0xc02f:
1365     case 0xc030:
1366     case 0xc031:
1367     case 0xc032:
1368     case 0xc033:
1369     case 0xc034:
1370     case 0xc035:
1371     case 0xc036:
1372     case 0xc037:
1373     case 0xc038:
1374     case 0xc039:
1375     case 0xc03a:
1376     case 0xc03b:
1377     case 0xc0ac:
1378     case 0xc0ad:
1379     case 0xc0ae:
1380     case 0xc0af:
1381         return KEX_ECDH;
1382     case 0x002C:
1383     case 0x008A:
1384     case 0x008B:
1385     case 0x008C:
1386     case 0x008D:
1387     case 0x00A8:
1388     case 0x00A9:
1389     case 0x00AE:
1390     case 0x00AF:
1391     case 0x00B0:
1392     case 0x00B1:
1393     case 0xC064:
1394     case 0xC065:
1395     case 0xC06A:
1396     case 0xC06B:
1397     case 0xC08E:
1398     case 0xC08F:
1399     case 0xC094:
1400     case 0xC095:
1401     case 0xC0A4:
1402     case 0xC0A5:
1403     case 0xC0A8:
1404     case 0xC0A9:
1405     case 0xC0AA:
1406     case 0xC0AB:
1407         return KEX_PSK;
1408     case 0x002E:
1409     case 0x0092:
1410     case 0x0093:
1411     case 0x0094:
1412     case 0x0095:
1413     case 0x00AC:
1414     case 0x00AD:
1415     case 0x00B6:
1416     case 0x00B7:
1417     case 0x00B8:
1418     case 0x00B9:
1419     case 0xC068:
1420     case 0xC069:
1421     case 0xC06E:
1422     case 0xC06F:
1423     case 0xC092:
1424     case 0xC093:
1425     case 0xC098:
1426     case 0xC099:
1427         return KEX_RSA_PSK;
1428     default:
1429         break;
1430     }
1431
1432     return 0;
1433 }
1434
1435
1436
1437
1438 static gint
1439 ssl_data_alloc(StringInfo* str, size_t len)
1440 {
1441     str->data = (guchar *)g_malloc(len);
1442     /* the allocator can return a null pointer for a size equal to 0,
1443      * and that must be allowed */
1444     if (len > 0 && !str->data)
1445         return -1;
1446     str->data_len = (guint) len;
1447     return 0;
1448 }
1449
1450 void
1451 ssl_data_set(StringInfo* str, const guchar* data, guint len)
1452 {
1453     DISSECTOR_ASSERT(data);
1454     memcpy(str->data, data, len);
1455     str->data_len = len;
1456 }
1457
1458 static StringInfo *
1459 ssl_data_clone(StringInfo *str)
1460 {
1461     StringInfo *cloned_str;
1462     cloned_str = (StringInfo *) wmem_alloc0(wmem_file_scope(),
1463             sizeof(StringInfo) + str->data_len);
1464     cloned_str->data = (guchar *) (cloned_str + 1);
1465     ssl_data_set(cloned_str, str->data, str->data_len);
1466     return cloned_str;
1467 }
1468
1469 /* from_hex converts |hex_len| bytes of hex data from |in| and sets |*out| to
1470  * the result. |out->data| will be allocated using wmem_file_scope. Returns TRUE on
1471  * success. */
1472 static gboolean from_hex(StringInfo* out, const char* in, gsize hex_len) {
1473     gsize i;
1474
1475     if (hex_len & 1)
1476         return FALSE;
1477
1478     out->data = (guchar *)wmem_alloc(wmem_file_scope(), hex_len / 2);
1479     for (i = 0; i < hex_len / 2; i++) {
1480         int a = ws_xton(in[i*2]);
1481         int b = ws_xton(in[i*2 + 1]);
1482         if (a == -1 || b == -1)
1483             return FALSE;
1484         out->data[i] = a << 4 | b;
1485     }
1486     out->data_len = (guint)hex_len / 2;
1487     return TRUE;
1488 }
1489
1490
1491 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
1492
1493 /* hmac abstraction layer */
1494 #define SSL_HMAC gcry_md_hd_t
1495
1496 static inline gint
1497 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
1498 {
1499     gcry_error_t  err;
1500     const char   *err_str, *err_src;
1501
1502     err = gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
1503     if (err != 0) {
1504         err_str = gcry_strerror(err);
1505         err_src = gcry_strsource(err);
1506         ssl_debug_printf("ssl_hmac_init(): gcry_md_open failed %s/%s", err_str, err_src);
1507         return -1;
1508     }
1509     gcry_md_setkey (*(md), key, len);
1510     return 0;
1511 }
1512 static inline void
1513 ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
1514 {
1515     gcry_md_write(*(md), data, len);
1516 }
1517 static inline void
1518 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
1519 {
1520     gint  algo;
1521     guint len;
1522
1523     algo = gcry_md_get_algo (*(md));
1524     len  = gcry_md_get_algo_dlen(algo);
1525     DISSECTOR_ASSERT(len <= *datalen);
1526     memcpy(data, gcry_md_read(*(md), algo), len);
1527     *datalen = len;
1528 }
1529 static inline void
1530 ssl_hmac_cleanup(SSL_HMAC* md)
1531 {
1532     gcry_md_close(*(md));
1533 }
1534
1535 /* memory digest abstraction layer*/
1536 #define SSL_MD gcry_md_hd_t
1537
1538 static inline gint
1539 ssl_md_init(SSL_MD* md, gint algo)
1540 {
1541     gcry_error_t  err;
1542     const char   *err_str, *err_src;
1543     err = gcry_md_open(md,algo, 0);
1544     if (err != 0) {
1545         err_str = gcry_strerror(err);
1546         err_src = gcry_strsource(err);
1547         ssl_debug_printf("ssl_md_init(): gcry_md_open failed %s/%s", err_str, err_src);
1548         return -1;
1549     }
1550     return 0;
1551 }
1552 static inline void
1553 ssl_md_update(SSL_MD* md, guchar* data, gint len)
1554 {
1555     gcry_md_write(*(md), data, len);
1556 }
1557 static inline void
1558 ssl_md_final(SSL_MD* md, guchar* data, guint* datalen)
1559 {
1560     gint algo;
1561     gint len;
1562     algo = gcry_md_get_algo (*(md));
1563     len = gcry_md_get_algo_dlen (algo);
1564     memcpy(data, gcry_md_read(*(md),  algo), len);
1565     *datalen = len;
1566 }
1567 static inline void
1568 ssl_md_cleanup(SSL_MD* md)
1569 {
1570     gcry_md_close(*(md));
1571 }
1572
1573 /* md5 /sha abstraction layer */
1574 #define SSL_SHA_CTX gcry_md_hd_t
1575 #define SSL_MD5_CTX gcry_md_hd_t
1576
1577 static inline void
1578 ssl_sha_init(SSL_SHA_CTX* md)
1579 {
1580     gcry_md_open(md,GCRY_MD_SHA1, 0);
1581 }
1582 static inline void
1583 ssl_sha_update(SSL_SHA_CTX* md, guchar* data, gint len)
1584 {
1585     gcry_md_write(*(md), data, len);
1586 }
1587 static inline void
1588 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
1589 {
1590     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_SHA1),
1591            gcry_md_get_algo_dlen(GCRY_MD_SHA1));
1592 }
1593 static inline void
1594 ssl_sha_cleanup(SSL_SHA_CTX* md)
1595 {
1596     gcry_md_close(*(md));
1597 }
1598
1599 static inline gint
1600 ssl_md5_init(SSL_MD5_CTX* md)
1601 {
1602     return gcry_md_open(md,GCRY_MD_MD5, 0);
1603 }
1604 static inline void
1605 ssl_md5_update(SSL_MD5_CTX* md, guchar* data, gint len)
1606 {
1607     gcry_md_write(*(md), data, len);
1608 }
1609 static inline void
1610 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
1611 {
1612     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_MD5),
1613            gcry_md_get_algo_dlen(GCRY_MD_MD5));
1614 }
1615 static inline void
1616 ssl_md5_cleanup(SSL_MD5_CTX* md)
1617 {
1618     gcry_md_close(*(md));
1619 }
1620
1621 gint
1622 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
1623 {
1624     gint ret;
1625 #if 0
1626     guchar *ivp;
1627     gint i;
1628     gcry_cipher_hd_t c;
1629     c=(gcry_cipher_hd_t)*cipher;
1630 #endif
1631     ssl_debug_printf("--------------------------------------------------------------------");
1632 #if 0
1633     for(ivp=c->iv,i=0; i < iv_len; i++ )
1634         {
1635         ssl_debug_printf("%d ",ivp[i]);
1636         i++;
1637         }
1638 #endif
1639     ssl_debug_printf("--------------------------------------------------------------------");
1640     ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
1641 #if 0
1642     for(ivp=c->iv,i=0; i < iv_len; i++ )
1643         {
1644         ssl_debug_printf("%d ",ivp[i]);
1645         i++;
1646         }
1647 #endif
1648     ssl_debug_printf("--------------------------------------------------------------------");
1649     return ret;
1650 }
1651 /* stream cipher abstraction layer*/
1652 static gint
1653 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
1654         guchar* iv, gint mode)
1655 {
1656     gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC,GCRY_CIPHER_MODE_CTR,GCRY_CIPHER_MODE_CTR,GCRY_CIPHER_MODE_CTR};
1657     gint err;
1658     if (algo == -1) {
1659         /* NULL mode */
1660         *(cipher) = (gcry_cipher_hd_t)-1;
1661         return 0;
1662     }
1663     err = gcry_cipher_open(cipher, algo, gcry_modes[mode], 0);
1664     if (err !=0)
1665         return  -1;
1666     err = gcry_cipher_setkey(*(cipher), sk, gcry_cipher_get_algo_keylen (algo));
1667     if (err != 0)
1668         return -1;
1669     err = gcry_cipher_setiv(*(cipher), iv, gcry_cipher_get_algo_blklen (algo));
1670     if (err != 0)
1671         return -1;
1672     return 0;
1673 }
1674 static inline gint
1675 ssl_cipher_decrypt(gcry_cipher_hd_t *cipher, guchar * out, gint outl,
1676                    const guchar * in, gint inl)
1677 {
1678     if ((*cipher) == (gcry_cipher_hd_t)-1)
1679     {
1680         if (in && inl)
1681             memcpy(out, in, outl < inl ? outl : inl);
1682         return 0;
1683     }
1684     return gcry_cipher_decrypt ( *(cipher), out, outl, in, inl);
1685 }
1686 static inline gint
1687 ssl_get_digest_by_name(const gchar*name)
1688 {
1689     return gcry_md_map_name(name);
1690 }
1691 static inline gint
1692 ssl_get_cipher_by_name(const gchar* name)
1693 {
1694     return gcry_cipher_map_name(name);
1695 }
1696
1697 static inline void
1698 ssl_cipher_cleanup(gcry_cipher_hd_t *cipher)
1699 {
1700     if ((*cipher) != (gcry_cipher_hd_t)-1)
1701         gcry_cipher_close(*cipher);
1702     *cipher = NULL;
1703 }
1704
1705 gcry_err_code_t
1706 _gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
1707                    gcry_mpi_t *skey, gint flags);
1708
1709 /* decrypt data with private key. Store decrypted data directly into input
1710  * buffer */
1711 static int
1712 ssl_private_decrypt(const guint len, guchar* data, SSL_PRIVATE_KEY* pk)
1713 {
1714     gint        rc = 0;
1715     size_t      decr_len = 0, i = 0;
1716     gcry_sexp_t s_data = NULL, s_plain = NULL;
1717     gcry_mpi_t  encr_mpi = NULL, text = NULL;
1718
1719     /* create mpi representation of encrypted data */
1720     rc = gcry_mpi_scan(&encr_mpi, GCRYMPI_FMT_USG, data, len, NULL);
1721     if (rc != 0 ) {
1722         ssl_debug_printf("pcry_private_decrypt: can't convert data to mpi (size %d):%s\n",
1723             len, gcry_strerror(rc));
1724         return 0;
1725     }
1726
1727 #ifndef SSL_FAST
1728     /* put the data into a simple list */
1729     rc = gcry_sexp_build(&s_data, NULL, "(enc-val(rsa(a%m)))", encr_mpi);
1730     if (rc != 0) {
1731         ssl_debug_printf("pcry_private_decrypt: can't build encr_sexp:%s\n",
1732              gcry_strerror(rc));
1733         decr_len = 0;
1734         goto out;
1735     }
1736
1737     /* pass it to libgcrypt */
1738     rc = gcry_pk_decrypt(&s_plain, s_data, pk);
1739     if (rc != 0)
1740     {
1741         ssl_debug_printf("pcry_private_decrypt: can't decrypt key:%s\n",
1742             gcry_strerror(rc));
1743         decr_len = 0;
1744         goto out;
1745     }
1746
1747     /* convert plain text sexp to mpi format */
1748     text = gcry_sexp_nth_mpi(s_plain, 0, 0);
1749     if (! text) {
1750         ssl_debug_printf("pcry_private_decrypt: can't convert sexp to mpi\n");
1751         decr_len = 0;
1752         goto out;
1753     }
1754
1755     /* compute size requested for plaintext buffer */
1756     rc = gcry_mpi_print(GCRYMPI_FMT_USG, NULL, 0, &decr_len, text);
1757     if (rc != 0) {
1758         ssl_debug_printf("pcry_private_decrypt: can't compute decr size:%s\n",
1759             gcry_strerror(rc));
1760         decr_len = 0;
1761         goto out;
1762     }
1763
1764 #else /* SSL_FAST */
1765     rc = _gcry_rsa_decrypt(0, &text,  &encr_mpi, pk,0);
1766     gcry_mpi_print( GCRYMPI_FMT_USG, 0, 0, &decr_len, text);
1767 #endif /* SSL_FAST */
1768
1769     /* sanity check on out buffer */
1770     if (decr_len > len) {
1771         ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%" G_GSIZE_MODIFIER "u max %d)\n", decr_len, len);
1772         decr_len = 0;
1773         goto out;
1774     }
1775
1776     /* write plain text to newly allocated buffer */
1777     rc = gcry_mpi_print(GCRYMPI_FMT_USG, data, len, &decr_len, text);
1778     if (rc != 0) {
1779         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));
1780         decr_len = 0;
1781         goto out;
1782     }
1783
1784     ssl_print_data("decrypted_unstrip_pre_master", data, decr_len);
1785
1786     /* strip the padding*/
1787     rc = 0;
1788     for (i = 1; i < decr_len; i++) {
1789         if (data[i] == 0) {
1790             rc = (gint) i+1;
1791             break;
1792         }
1793     }
1794
1795     ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %" G_GSIZE_MODIFIER "u\n", rc, decr_len);
1796     decr_len -= rc;
1797     memmove(data, data+rc, decr_len);
1798
1799 out:
1800     gcry_sexp_release(s_data);
1801     gcry_sexp_release(s_plain);
1802     gcry_mpi_release(encr_mpi);
1803     gcry_mpi_release(text);
1804     return (int) decr_len;
1805 }
1806
1807 /* stringinfo interface */
1808 static gint
1809 ssl_data_realloc(StringInfo* str, guint len)
1810 {
1811     str->data = (guchar *)g_realloc(str->data, len);
1812     if (!str->data)
1813         return -1;
1814     str->data_len = len;
1815     return 0;
1816 }
1817
1818 static gint
1819 ssl_data_copy(StringInfo* dst, StringInfo* src)
1820 {
1821     if (dst->data_len < src->data_len) {
1822       if (ssl_data_realloc(dst, src->data_len))
1823         return -1;
1824     }
1825     memcpy(dst->data, src->data, src->data_len);
1826     dst->data_len = src->data_len;
1827     return 0;
1828 }
1829
1830 static const SslDigestAlgo digests[]={
1831     {"MD5",     16},
1832     {"SHA1",    20},
1833     {"SHA256",  32},
1834     {"SHA384",  48},
1835     {"Not Applicable",  0},
1836 };
1837
1838 #define DIGEST_MAX_SIZE 48
1839
1840 /* get index digest index */
1841 static const SslDigestAlgo *
1842 ssl_cipher_suite_dig(SslCipherSuite *cs) {
1843     return &digests[cs->dig - DIG_MD5];
1844 }
1845
1846 static const gchar *ciphers[]={
1847     "DES",
1848     "3DES",
1849     "ARCFOUR", /* libgcrypt does not support rc4, but this should be 100% compatible*/
1850     "RFC2268_128", /* libgcrypt name for RC2 with a 128-bit key */
1851     "IDEA",
1852     "AES",
1853     "AES256",
1854     "CAMELLIA128",
1855     "CAMELLIA256",
1856     "SEED",
1857     "*UNKNOWN*"
1858 };
1859
1860 static SslCipherSuite cipher_suites[]={
1861     {0x0001,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_MD5 */
1862     {0x0002,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA */
1863     {0x0003,KEX_RSA,    ENC_RC4,         1,128, 40,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 */
1864     {0x0004,KEX_RSA,    ENC_RC4,         1,128,128,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_MD5 */
1865     {0x0005,KEX_RSA,    ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_SHA */
1866     {0x0006,KEX_RSA,    ENC_RC2,         8,128, 40,DIG_MD5,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 */
1867     {0x0007,KEX_RSA,    ENC_IDEA,        8,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_IDEA_CBC_SHA */
1868     {0x0008,KEX_RSA,    ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA */
1869     {0x0009,KEX_RSA,    ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_DES_CBC_SHA */
1870     {0x000A,KEX_RSA,    ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_3DES_EDE_CBC_SHA */
1871     {0x000B,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA */
1872     {0x000C,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_DES_CBC_SHA */
1873     {0x000D,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA */
1874     {0x000E,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA */
1875     {0x000F,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_DES_CBC_SHA */
1876     {0x0010,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA */
1877     {0x0011,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA */
1878     {0x0012,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_DES_CBC_SHA */
1879     {0x0013,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA */
1880     {0x0014,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA */
1881     {0x0015,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_DES_CBC_SHA */
1882     {0x0016,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */
1883     {0x0017,KEX_DH,     ENC_RC4,         1,128, 40,DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 */
1884     {0x0018,KEX_DH,     ENC_RC4,         1,128,128,DIG_MD5,    MODE_STREAM},   /* TLS_DH_anon_WITH_RC4_128_MD5 */
1885     {0x0019,KEX_DH,     ENC_DES,         8, 64, 40,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA */
1886     {0x001A,KEX_DH,     ENC_DES,         8, 64, 64,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_DES_CBC_SHA */
1887     {0x001B,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_3DES_EDE_CBC_SHA */
1888     {0x002C,KEX_PSK,    ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA */
1889     {0x002D,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA */
1890     {0x002E,KEX_RSA_PSK,ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA */
1891     {0x002F,KEX_RSA,    ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA */
1892     {0x0030,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA */
1893     {0x0031,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA */
1894     {0x0032,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA */
1895     {0x0033,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
1896     {0x0034,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA */
1897     {0x0035,KEX_RSA,    ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA */
1898     {0x0036,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA */
1899     {0x0037,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA */
1900     {0x0038,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA */
1901     {0x0039,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */
1902     {0x003A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA */
1903     {0x003B,KEX_RSA,    ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA256 */
1904     {0x003C,KEX_RSA,    ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_128_CBC_SHA256 */
1905     {0x003D,KEX_RSA,    ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
1906     {0x003E,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA256 */
1907     {0x003F,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA256 */
1908     {0x0040,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 */
1909     {0x0041,KEX_RSA,    ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */
1910     {0x0042,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA */
1911     {0x0043,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA */
1912     {0x0044,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA */
1913     {0x0045,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */
1914     {0x0046,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA */
1915     {0x0060,KEX_RSA,    ENC_RC4,         1,128, 56,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC4_56_MD5 */
1916     {0x0061,KEX_RSA,    ENC_RC2,         1,128, 56,DIG_MD5,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5 */
1917     {0x0062,KEX_RSA,    ENC_DES,         8, 64, 56,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA */
1918     {0x0063,KEX_DH,     ENC_DES,         8, 64, 56,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA */
1919     {0x0064,KEX_RSA,    ENC_RC4,         1,128, 56,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_EXPORT1024_WITH_RC4_56_SHA */
1920     {0x0065,KEX_DH,     ENC_RC4,         1,128, 56,DIG_SHA,    MODE_STREAM},   /* TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA */
1921     {0x0066,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_DHE_DSS_WITH_RC4_128_SHA */
1922     {0x0067,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */
1923     {0x0068,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA256 */
1924     {0x0069,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA256 */
1925     {0x006A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 */
1926     {0x006B,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */
1927     {0x006C,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_128_CBC_SHA256 */
1928     {0x006D,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_AES_256_CBC_SHA256 */
1929     {0x0084,KEX_RSA,    ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */
1930     {0x0085,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA */
1931     {0x0086,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA */
1932     {0x0087,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA */
1933     {0x0088,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */
1934     {0x0089,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA */
1935     {0x008A,KEX_PSK,    ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_PSK_WITH_RC4_128_SHA */
1936     {0x008B,KEX_PSK,    ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_3DES_EDE_CBC_SHA */
1937     {0x008C,KEX_PSK,    ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_AES_128_CBC_SHA */
1938     {0x008D,KEX_PSK,    ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_PSK_WITH_AES_256_CBC_SHA */
1939     {0x008E,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_DHE_PSK_WITH_RC4_128_SHA */
1940     {0x008F,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA */
1941     {0x0090,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA */
1942     {0x0091,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA */
1943     {0x0092,KEX_RSA_PSK,ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_RSA_PSK_WITH_RC4_128_SHA */
1944     {0x0093,KEX_RSA_PSK,ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA */
1945     {0x0094,KEX_RSA_PSK,ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_128_CBC_SHA */
1946     {0x0095,KEX_RSA_PSK,ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_256_CBC_SHA */
1947     {0x0096,KEX_RSA,    ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_RSA_WITH_SEED_CBC_SHA */
1948     {0x0097,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_DSS_WITH_SEED_CBC_SHA */
1949     {0x0098,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_RSA_WITH_SEED_CBC_SHA */
1950     {0x0099,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_DSS_WITH_SEED_CBC_SHA */
1951     {0x009A,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DHE_RSA_WITH_SEED_CBC_SHA */
1952     {0x009B,KEX_DH,     ENC_SEED,       16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_DH_anon_WITH_SEED_CBC_SHA */
1953     {0x009C,KEX_RSA,    ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_WITH_AES_128_GCM_SHA256 */
1954     {0x009D,KEX_RSA,    ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_WITH_AES_256_GCM_SHA384 */
1955     {0x009E,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */
1956     {0x009F,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */
1957     {0x00A0,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_128_GCM_SHA256 */
1958     {0x00A1,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_RSA_WITH_AES_256_GCM_SHA384 */
1959     {0x00A2,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 */
1960     {0x00A3,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 */
1961     {0x00A4,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_128_GCM_SHA256 */
1962     {0x00A5,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_DSS_WITH_AES_256_GCM_SHA384 */
1963     {0x00A6,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_128_GCM_SHA256 */
1964     {0x00A7,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */
1965     {0x00A8,KEX_PSK,    ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_PSK_WITH_AES_128_GCM_SHA256 */
1966     {0x00A9,KEX_PSK,    ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_PSK_WITH_AES_256_GCM_SHA384 */
1967     {0x00AA,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */
1968     {0x00AB,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */
1969     {0x00AC,KEX_RSA_PSK,ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_PSK_WITH_AES_128_GCM_SHA256 */
1970     {0x00AD,KEX_RSA_PSK,ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_PSK_WITH_AES_256_GCM_SHA384 */
1971     {0x00AE,KEX_PSK,    ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_PSK_WITH_AES_128_CBC_SHA256 */
1972     {0x00AF,KEX_PSK,    ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_PSK_WITH_AES_256_CBC_SHA384 */
1973     {0x00B0,KEX_PSK,    ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA256 */
1974     {0x00B1,KEX_PSK,    ENC_NULL,        1,  0,  0,DIG_SHA384, MODE_STREAM},   /* TLS_PSK_WITH_NULL_SHA384 */
1975     {0x00B2,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */
1976     {0x00B3,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */
1977     {0x00B4,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA256 */
1978     {0x00B5,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA384, MODE_STREAM},   /* TLS_DHE_PSK_WITH_NULL_SHA384 */
1979     {0x00B6,KEX_RSA_PSK,ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_128_CBC_SHA256 */
1980     {0x00B7,KEX_RSA_PSK,ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_RSA_PSK_WITH_AES_256_CBC_SHA384 */
1981     {0x00B8,KEX_RSA_PSK,ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA256 */
1982     {0x00B9,KEX_RSA_PSK,ENC_NULL,        1,  0,  0,DIG_SHA384, MODE_STREAM},   /* TLS_RSA_PSK_WITH_NULL_SHA384 */
1983     {0x00BA,KEX_RSA,    ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1984     {0x00BB,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
1985     {0x00BC,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1986     {0x00BD,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
1987     {0x00BE,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
1988     {0x00BF,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 */
1989     {0x00C0,KEX_RSA,    ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1990     {0x00C1,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
1991     {0x00C2,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1992     {0x00C3,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
1993     {0x00C4,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
1994     {0x00C5,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA256, MODE_CBC   },   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 */
1995     {0xC001,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_NULL_SHA */
1996     {0xC002,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */
1997     {0xC003,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */
1998     {0xC004,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */
1999     {0xC005,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */
2000     {0xC006,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */
2001     {0xC007,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */
2002     {0xC008,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */
2003     {0xC009,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */
2004     {0xC00A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */
2005     {0xC00B,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_NULL_SHA */
2006     {0xC00C,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_RSA_WITH_RC4_128_SHA */
2007     {0xC00D,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */
2008     {0xC00E,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */
2009     {0xC00F,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */
2010     {0xC010,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_NULL_SHA */
2011     {0xC011,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */
2012     {0xC012,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */
2013     {0xC013,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */
2014     {0xC014,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */
2015     {0xC015,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_NULL_SHA */
2016     {0xC016,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDH_anon_WITH_RC4_128_SHA */
2017     {0xC017,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA */
2018     {0xC018,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_128_CBC_SHA */
2019     {0xC019,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDH_anon_WITH_AES_256_CBC_SHA */
2020     {0xC023,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 */
2021     {0xC024,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 */
2022     {0xC025,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */
2023     {0xC026,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */
2024     {0xC027,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */
2025     {0xC028,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */
2026     {0xC029,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */
2027     {0xC02A,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */
2028     {0xC02B,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */
2029     {0xC02C,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */
2030     {0xC02D,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */
2031     {0xC02E,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */
2032     {0xC02F,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
2033     {0xC030,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */
2034     {0xC031,KEX_DH,     ENC_AES,         4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */
2035     {0xC032,KEX_DH,     ENC_AES256,      4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */
2036     {0xC033,KEX_DH,     ENC_RC4,         1,128,128,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_RC4_128_SHA */
2037     {0xC034,KEX_DH,     ENC_3DES,        8,192,192,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA */
2038     {0xC035,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA */
2039     {0xC036,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA,    MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA */
2040     {0xC037,KEX_DH,     ENC_AES,        16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */
2041     {0xC038,KEX_DH,     ENC_AES256,     16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 */
2042     {0xC039,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA,    MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA */
2043     {0xC03A,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA256, MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */
2044     {0xC03B,KEX_DH,     ENC_NULL,        1,  0,  0,DIG_SHA384, MODE_STREAM},   /* TLS_ECDHE_PSK_WITH_NULL_SHA384 */
2045     {0xC072,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 */
2046     {0xC073,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 */
2047     {0xC074,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 */
2048     {0xC075,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 */
2049     {0xC076,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2050     {0xC077,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 */
2051     {0xC078,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
2052     {0xC079,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 */
2053     {0xC07A,KEX_RSA,    ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2054     {0xC07B,KEX_RSA,    ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2055     {0xC07C,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2056     {0xC07D,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2057     {0xC07E,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2058     {0xC07F,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2059     {0xC080,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256 */
2060     {0xC081,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384 */
2061     {0xC082,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256 */
2062     {0xC083,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384 */
2063     {0xC084,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256 */
2064     {0xC085,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384 */
2065     {0xC086,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 */
2066     {0xC087,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 */
2067     {0xC088,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 */
2068     {0xC089,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 */
2069     {0xC08A,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2070     {0xC08B,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2071     {0xC08C,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256 */
2072     {0xC08D,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384 */
2073     {0xC08E,KEX_PSK,    ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2074     {0xC08F,KEX_PSK,    ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2075     {0xC090,KEX_DH,     ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2076     {0xC091,KEX_DH,     ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2077     {0xC092,KEX_RSA_PSK,ENC_CAMELLIA128, 4,128,128,DIG_SHA256, MODE_GCM   },   /* TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256 */
2078     {0xC093,KEX_RSA_PSK,ENC_CAMELLIA256, 4,256,256,DIG_SHA384, MODE_GCM   },   /* TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384 */
2079     {0xC094,KEX_PSK,    ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2080     {0xC095,KEX_PSK,    ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2081     {0xC096,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2082     {0xC097,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2083     {0xC098,KEX_RSA_PSK,ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2084     {0xC099,KEX_RSA_PSK,ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2085     {0xC09A,KEX_DH,     ENC_CAMELLIA128,16,128,128,DIG_SHA256, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 */
2086     {0xC09B,KEX_DH,     ENC_CAMELLIA256,16,256,256,DIG_SHA384, MODE_CBC   },   /* TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 */
2087     {0xC09C,KEX_RSA,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_128_CCM */
2088     {0xC09D,KEX_RSA,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_RSA_WITH_AES_256_CCM */
2089     {0xC09E,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_128_CCM */
2090     {0xC09F,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_DHE_RSA_WITH_AES_256_CCM */
2091     {0xC0A0,KEX_RSA,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_128_CCM_8 */
2092     {0xC0A1,KEX_RSA,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_RSA_WITH_AES_256_CCM_8 */
2093     {0xC0A2,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_128_CCM_8 */
2094     {0xC0A3,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_DHE_RSA_WITH_AES_256_CCM_8 */
2095     {0xC0A4,KEX_PSK,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_128_CCM */
2096     {0xC0A5,KEX_PSK,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_PSK_WITH_AES_256_CCM */
2097     {0xC0A6,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_DHE_PSK_WITH_AES_128_CCM */
2098     {0xC0A7,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_DHE_PSK_WITH_AES_256_CCM */
2099     {0xC0A8,KEX_PSK,    ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_128_CCM_8 */
2100     {0xC0A9,KEX_PSK,    ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_WITH_AES_256_CCM_8 */
2101     {0xC0AA,KEX_DH,     ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_DHE_WITH_AES_128_CCM_8 */
2102     {0xC0AB,KEX_DH,     ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_PSK_DHE_WITH_AES_256_CCM_8 */
2103     {0xC0AC,KEX_ECDH,   ENC_AES,         4,128,128,DIG_NA,     MODE_CCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */
2104     {0xC0AD,KEX_ECDH,   ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM   },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM */
2105     {0xC0AE,KEX_ECDH,   ENC_AES,         4,128,128,DIG_NA,     MODE_CCM_8 },   /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */
2106     {0xC0AF,KEX_ECDH,   ENC_AES256,      4,256,256,DIG_NA,     MODE_CCM_8 },   /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */
2107     {-1,    0,          0,               0,  0,  0,0,          MODE_STREAM}
2108 };
2109
2110 #define MAX_BLOCK_SIZE 16
2111 #define MAX_KEY_SIZE 32
2112
2113 int
2114 ssl_find_cipher(int num,SslCipherSuite* cs)
2115 {
2116     SslCipherSuite *c;
2117
2118     for(c=cipher_suites;c->number!=-1;c++){
2119         if(c->number==num){
2120             *cs=*c;
2121             return 0;
2122         }
2123     }
2124
2125     return -1;
2126 }
2127
2128 static void
2129 tls_hash(StringInfo *secret, StringInfo *seed, gint md,
2130          StringInfo *out, guint out_len)
2131 {
2132     /* RFC 2246 5. HMAC and the pseudorandom function
2133      * '+' denotes concatenation.
2134      * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
2135      *                        HMAC_hash(secret, A(2) + seed) + ...
2136      * A(0) = seed
2137      * A(i) = HMAC_hash(secret, A(i - 1))
2138      */
2139     guint8   *ptr;
2140     guint     left, tocpy;
2141     guint8   *A;
2142     guint8    _A[DIGEST_MAX_SIZE], tmp[DIGEST_MAX_SIZE];
2143     guint     A_l, tmp_l;
2144     SSL_HMAC  hm;
2145
2146     ptr  = out->data;
2147     left = out_len;
2148
2149     ssl_print_string("tls_hash: hash secret", secret);
2150     ssl_print_string("tls_hash: hash seed", seed);
2151     /* A(0) = seed */
2152     A = seed->data;
2153     A_l = seed->data_len;
2154
2155     while (left) {
2156         /* A(i) = HMAC_hash(secret, A(i-1)) */
2157         ssl_hmac_init(&hm, secret->data, secret->data_len, md);
2158         ssl_hmac_update(&hm, A, A_l);
2159         A_l = sizeof(_A); /* upper bound len for hash output */
2160         ssl_hmac_final(&hm, _A, &A_l);
2161         ssl_hmac_cleanup(&hm);
2162         A = _A;
2163
2164         /* HMAC_hash(secret, A(i) + seed) */
2165         ssl_hmac_init(&hm, secret->data, secret->data_len, md);
2166         ssl_hmac_update(&hm, A, A_l);
2167         ssl_hmac_update(&hm, seed->data, seed->data_len);
2168         tmp_l = sizeof(tmp); /* upper bound len for hash output */
2169         ssl_hmac_final(&hm, tmp, &tmp_l);
2170         ssl_hmac_cleanup(&hm);
2171
2172         /* ssl_hmac_final puts the actual digest output size in tmp_l */
2173         tocpy = MIN(left, tmp_l);
2174         memcpy(ptr, tmp, tocpy);
2175         ptr += tocpy;
2176         left -= tocpy;
2177     }
2178     out->data_len = out_len;
2179
2180     ssl_print_string("hash out", out);
2181 }
2182
2183 static gboolean
2184 tls_prf(StringInfo* secret, const gchar *usage,
2185         StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2186 {
2187     StringInfo  seed, sha_out, md5_out;
2188     guint8     *ptr;
2189     StringInfo  s1, s2;
2190     guint       i,s_l;
2191     size_t      usage_len, rnd2_len;
2192     gboolean    success = FALSE;
2193     usage_len = strlen(usage);
2194     rnd2_len = rnd2 ? rnd2->data_len : 0;
2195
2196     /* initalize buffer for sha, md5 random seed*/
2197     if (ssl_data_alloc(&sha_out, MAX(out_len, 20)) < 0) {
2198         ssl_debug_printf("tls_prf: can't allocate sha out\n");
2199         return FALSE;
2200     }
2201     if (ssl_data_alloc(&md5_out, MAX(out_len, 16)) < 0) {
2202         ssl_debug_printf("tls_prf: can't allocate md5 out\n");
2203         goto free_sha;
2204     }
2205     if (ssl_data_alloc(&seed, usage_len+rnd1->data_len+rnd2_len) < 0) {
2206         ssl_debug_printf("tls_prf: can't allocate rnd %d\n",
2207                          (int) (usage_len+rnd1->data_len+rnd2_len));
2208         goto free_md5;
2209     }
2210
2211     ptr=seed.data;
2212     memcpy(ptr,usage,usage_len);
2213     ptr+=usage_len;
2214     memcpy(ptr,rnd1->data,rnd1->data_len);
2215     if (rnd2_len > 0) {
2216         ptr+=rnd1->data_len;
2217         memcpy(ptr,rnd2->data,rnd2->data_len);
2218         /*ptr+=rnd2->data_len;*/
2219     }
2220
2221     /* initalize buffer for client/server seeds*/
2222     s_l=secret->data_len/2 + secret->data_len%2;
2223     if (ssl_data_alloc(&s1, s_l) < 0) {
2224         ssl_debug_printf("tls_prf: can't allocate secret %d\n", s_l);
2225         goto free_seed;
2226     }
2227     if (ssl_data_alloc(&s2, s_l) < 0) {
2228         ssl_debug_printf("tls_prf: can't allocate secret(2) %d\n", s_l);
2229         goto free_s1;
2230     }
2231
2232     memcpy(s1.data,secret->data,s_l);
2233     memcpy(s2.data,secret->data + (secret->data_len - s_l),s_l);
2234
2235     ssl_debug_printf("tls_prf: tls_hash(md5 secret_len %d seed_len %d )\n", s1.data_len, seed.data_len);
2236     tls_hash(&s1, &seed, ssl_get_digest_by_name("MD5"), &md5_out, out_len);
2237     ssl_debug_printf("tls_prf: tls_hash(sha)\n");
2238     tls_hash(&s2, &seed, ssl_get_digest_by_name("SHA1"), &sha_out, out_len);
2239
2240     for (i = 0; i < out_len; i++)
2241         out->data[i] = md5_out.data[i] ^ sha_out.data[i];
2242     /* success, now store the new meaningful data length */
2243     out->data_len = out_len;
2244     success = TRUE;
2245
2246     ssl_print_string("PRF out",out);
2247     g_free(s2.data);
2248 free_s1:
2249     g_free(s1.data);
2250 free_seed:
2251     g_free(seed.data);
2252 free_md5:
2253     g_free(md5_out.data);
2254 free_sha:
2255     g_free(sha_out.data);
2256     return success;
2257 }
2258
2259 static gboolean
2260 tls12_prf(gint md, StringInfo* secret, const gchar* usage,
2261           StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2262 {
2263     StringInfo label_seed;
2264     size_t     usage_len, rnd2_len;
2265     rnd2_len = rnd2 ? rnd2->data_len : 0;
2266
2267     usage_len = strlen(usage);
2268     if (ssl_data_alloc(&label_seed, usage_len+rnd1->data_len+rnd2_len) < 0) {
2269         ssl_debug_printf("tls12_prf: can't allocate label_seed\n");
2270         return FALSE;
2271     }
2272     memcpy(label_seed.data, usage, usage_len);
2273     memcpy(label_seed.data+usage_len, rnd1->data, rnd1->data_len);
2274     if (rnd2_len > 0)
2275         memcpy(label_seed.data+usage_len+rnd1->data_len, rnd2->data, rnd2->data_len);
2276
2277     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);
2278     tls_hash(secret, &label_seed, md, out, out_len);
2279     g_free(label_seed.data);
2280     ssl_print_string("PRF out", out);
2281     return TRUE;
2282 }
2283
2284 static void
2285 ssl3_generate_export_iv(StringInfo *r1, StringInfo *r2,
2286                         StringInfo *out, guint out_len)
2287 {
2288     SSL_MD5_CTX md5;
2289     guint8      tmp[16];
2290
2291     ssl_md5_init(&md5);
2292     ssl_md5_update(&md5,r1->data,r1->data_len);
2293     ssl_md5_update(&md5,r2->data,r2->data_len);
2294     ssl_md5_final(tmp,&md5);
2295     ssl_md5_cleanup(&md5);
2296
2297     DISSECTOR_ASSERT(out_len <= sizeof(tmp));
2298     ssl_data_set(out, tmp, out_len);
2299     ssl_print_string("export iv", out);
2300 }
2301
2302 static gboolean
2303 ssl3_prf(StringInfo* secret, const gchar* usage,
2304          StringInfo* rnd1, StringInfo* rnd2, StringInfo* out, guint out_len)
2305 {
2306     SSL_MD5_CTX  md5;
2307     SSL_SHA_CTX  sha;
2308     guint        off;
2309     gint         i = 0,j;
2310     guint8       buf[20];
2311
2312     for (off = 0; off < out_len; off += 16) {
2313         guchar outbuf[16];
2314         i++;
2315
2316         ssl_debug_printf("ssl3_prf: sha1_hash(%d)\n",i);
2317         /* A, BB, CCC,  ... */
2318         for(j=0;j<i;j++){
2319             buf[j]=64+i;
2320         }
2321
2322         ssl_sha_init(&sha);
2323         ssl_sha_update(&sha,buf,i);
2324         ssl_sha_update(&sha,secret->data,secret->data_len);
2325
2326         if(!strcmp(usage,"client write key") || !strcmp(usage,"server write key")){
2327             if (rnd2)
2328                 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2329             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2330         }
2331         else{
2332             ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
2333             if (rnd2)
2334                 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
2335         }
2336
2337         ssl_sha_final(buf,&sha);
2338         ssl_sha_cleanup(&sha);
2339
2340         ssl_debug_printf("ssl3_prf: md5_hash(%d) datalen %d\n",i,
2341             secret->data_len);
2342         ssl_md5_init(&md5);
2343         ssl_md5_update(&md5,secret->data,secret->data_len);
2344         ssl_md5_update(&md5,buf,20);
2345         ssl_md5_final(outbuf,&md5);
2346         ssl_md5_cleanup(&md5);
2347
2348         memcpy(out->data + off, outbuf, MIN(out_len - off, 16));
2349     }
2350     out->data_len = out_len;
2351
2352     return TRUE;
2353 }
2354
2355 /* out_len is the wanted output length for the pseudorandom function */
2356 static gboolean
2357 prf(SslDecryptSession *ssl, StringInfo *secret, const gchar *usage,
2358     StringInfo *rnd1, StringInfo *rnd2, StringInfo *out, guint out_len)
2359 {
2360     switch (ssl->version_netorder) {
2361     case SSLV3_VERSION:
2362         return ssl3_prf(secret, usage, rnd1, rnd2, out, out_len);
2363
2364     case TLSV1_VERSION:
2365     case TLSV1DOT1_VERSION:
2366     case DTLSV1DOT0_VERSION:
2367     case DTLSV1DOT0_VERSION_NOT:
2368         return tls_prf(secret, usage, rnd1, rnd2, out, out_len);
2369
2370     default: /* TLSv1.2 */
2371         switch (ssl->cipher_suite.dig) {
2372         case DIG_SHA384:
2373             return tls12_prf(GCRY_MD_SHA384, secret, usage, rnd1, rnd2,
2374                              out, out_len);
2375         default:
2376             return tls12_prf(GCRY_MD_SHA256, secret, usage, rnd1, rnd2,
2377                              out, out_len);
2378         }
2379     }
2380 }
2381
2382 static gint tls_handshake_hash(SslDecryptSession* ssl, StringInfo* out)
2383 {
2384     SSL_MD5_CTX  md5;
2385     SSL_SHA_CTX  sha;
2386
2387     if (ssl_data_alloc(out, 36) < 0)
2388         return -1;
2389
2390     ssl_md5_init(&md5);
2391     ssl_md5_update(&md5,ssl->handshake_data.data,ssl->handshake_data.data_len);
2392     ssl_md5_final(out->data,&md5);
2393     ssl_md5_cleanup(&md5);
2394
2395     ssl_sha_init(&sha);
2396     ssl_sha_update(&sha,ssl->handshake_data.data,ssl->handshake_data.data_len);
2397     ssl_sha_final(out->data+16,&sha);
2398     ssl_sha_cleanup(&sha);
2399     return 0;
2400 }
2401
2402 static gint tls12_handshake_hash(SslDecryptSession* ssl, gint md, StringInfo* out)
2403 {
2404     SSL_MD  mc;
2405     guint8 tmp[48];
2406     guint  len;
2407
2408     ssl_md_init(&mc, md);
2409     ssl_md_update(&mc,ssl->handshake_data.data,ssl->handshake_data.data_len);
2410     ssl_md_final(&mc, tmp, &len);
2411     ssl_md_cleanup(&mc);
2412
2413     if (ssl_data_alloc(out, len) < 0)
2414         return -1;
2415     memcpy(out->data, tmp, len);
2416     return 0;
2417 }
2418
2419 static SslFlow*
2420 ssl_create_flow(void)
2421 {
2422   SslFlow *flow;
2423
2424   flow = (SslFlow *)wmem_alloc(wmem_file_scope(), sizeof(SslFlow));
2425   flow->byte_seq = 0;
2426   flow->flags = 0;
2427   flow->multisegment_pdus = wmem_tree_new(wmem_file_scope());
2428   return flow;
2429 }
2430
2431 #ifdef HAVE_LIBZ
2432 /* memory allocation functions for zlib initialization */
2433 static void* ssl_zalloc(void* opaque _U_, unsigned int no, unsigned int size)
2434 {
2435     return g_malloc0(no*size);
2436 }
2437 static void ssl_zfree(void* opaque _U_, void* addr)
2438 {
2439     g_free(addr);
2440 }
2441 #endif
2442
2443 static SslDecompress*
2444 ssl_create_decompressor(gint compression)
2445 {
2446     SslDecompress *decomp;
2447 #ifdef HAVE_LIBZ
2448     int err;
2449 #endif
2450
2451     if (compression == 0) return NULL;
2452     ssl_debug_printf("ssl_create_decompressor: compression method %d\n", compression);
2453     decomp = (SslDecompress *)wmem_alloc(wmem_file_scope(), sizeof(SslDecompress));
2454     decomp->compression = compression;
2455     switch (decomp->compression) {
2456 #ifdef HAVE_LIBZ
2457         case 1:  /* DEFLATE */
2458             decomp->istream.zalloc = ssl_zalloc;
2459             decomp->istream.zfree = ssl_zfree;
2460             decomp->istream.opaque = Z_NULL;
2461             decomp->istream.next_in = Z_NULL;
2462             decomp->istream.next_out = Z_NULL;
2463             decomp->istream.avail_in = 0;
2464             decomp->istream.avail_out = 0;
2465             err = inflateInit_(&decomp->istream, ZLIB_VERSION, sizeof(z_stream));
2466             if (err != Z_OK) {
2467                 ssl_debug_printf("ssl_create_decompressor: inflateInit_() failed - %d\n", err);
2468                 return NULL;
2469             }
2470             break;
2471 #endif
2472         default:
2473             ssl_debug_printf("ssl_create_decompressor: unsupported compression method %d\n", decomp->compression);
2474             return NULL;
2475     }
2476     return decomp;
2477 }
2478
2479 static SslDecoder*
2480 ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
2481         guint8 *mk, guint8 *sk, guint8 *iv)
2482 {
2483     SslDecoder *dec;
2484     gint        ciph;
2485
2486     dec = (SslDecoder *)wmem_alloc0(wmem_file_scope(), sizeof(SslDecoder));
2487     /* Find the SSLeay cipher */
2488     if(cipher_suite->enc!=ENC_NULL) {
2489         ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", ciphers[cipher_suite->enc-0x30]);
2490         ciph=ssl_get_cipher_by_name(ciphers[cipher_suite->enc-0x30]);
2491     } else {
2492         ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", "NULL");
2493         ciph = -1;
2494     }
2495     if (ciph == 0) {
2496         ssl_debug_printf("ssl_create_decoder can't find cipher %s\n",
2497             ciphers[cipher_suite->enc > ENC_NULL ? ENC_NULL-0x30 : (cipher_suite->enc-0x30)]);
2498         return NULL;
2499     }
2500
2501     /* init mac buffer: mac storage is embedded into decoder struct to save a
2502      memory allocation and waste samo more memory*/
2503     dec->cipher_suite=cipher_suite;
2504     dec->compression = compression;
2505     /* AEED ciphers don't have a MAC but need to keep the write IV instead */
2506     if (mk == NULL) {
2507         dec->write_iv.data = dec->_mac_key_or_write_iv;
2508         ssl_data_set(&dec->write_iv, iv, cipher_suite->block);
2509     } else {
2510         dec->mac_key.data = dec->_mac_key_or_write_iv;
2511         ssl_data_set(&dec->mac_key, mk, ssl_cipher_suite_dig(cipher_suite)->len);
2512     }
2513     dec->seq = 0;
2514     dec->decomp = ssl_create_decompressor(compression);
2515     dec->flow = ssl_create_flow();
2516
2517     if (dec->evp)
2518         ssl_cipher_cleanup(&dec->evp);
2519
2520     if (ssl_cipher_init(&dec->evp,ciph,sk,iv,cipher_suite->mode) < 0) {
2521         ssl_debug_printf("ssl_create_decoder: can't create cipher id:%d mode:%d\n",
2522             ciph, cipher_suite->mode);
2523         return NULL;
2524     }
2525
2526     ssl_debug_printf("decoder initialized (digest len %d)\n", ssl_cipher_suite_dig(cipher_suite)->len);
2527     return dec;
2528 }
2529
2530 static int
2531 ssl_decrypt_pre_master_secret(SslDecryptSession *ssl_session,
2532                               StringInfo *encrypted_pre_master,
2533                               SSL_PRIVATE_KEY *pk);
2534 static gboolean
2535 ssl_restore_master_key(SslDecryptSession *ssl, const char *label,
2536                        gboolean is_pre_master, GHashTable *ht, StringInfo *key);
2537
2538 gboolean
2539 ssl_generate_pre_master_secret(SslDecryptSession *ssl_session,
2540                                guint32 length, tvbuff_t *tvb, guint32 offset,
2541                                const gchar *ssl_psk,
2542                                const ssl_master_key_map_t *mk_map)
2543 {
2544     /* check for required session data */
2545     ssl_debug_printf("%s: found SSL_HND_CLIENT_KEY_EXCHG, state %X\n",
2546                      G_STRFUNC, ssl_session->state);
2547     if ((ssl_session->state & (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) !=
2548         (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) {
2549         ssl_debug_printf("%s: not enough data to generate key (required state %X)\n", G_STRFUNC,
2550                          (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION));
2551         return FALSE;
2552     }
2553
2554     if (ssl_session->cipher_suite.kex == KEX_PSK)
2555     {
2556         /* calculate pre master secret*/
2557         StringInfo pre_master_secret;
2558         guint psk_len, pre_master_len;
2559
2560         if (!ssl_psk || (ssl_psk[0] == 0)) {
2561             ssl_debug_printf("%s: can't find pre-shared-key\n", G_STRFUNC);
2562             return FALSE;
2563         }
2564
2565         /* convert hex string into char*/
2566         if (!from_hex(&ssl_session->psk, ssl_psk, strlen(ssl_psk))) {
2567             ssl_debug_printf("%s: ssl.psk/dtls.psk contains invalid hex\n",
2568                              G_STRFUNC);
2569             return FALSE;
2570         }
2571
2572         psk_len = ssl_session->psk.data_len;
2573         if (psk_len >= (2 << 15)) {
2574             ssl_debug_printf("%s: ssl.psk/dtls.psk must not be larger than 2^15 - 1\n",
2575                              G_STRFUNC);
2576             return FALSE;
2577         }
2578
2579
2580         pre_master_len = psk_len * 2 + 4;
2581
2582         pre_master_secret.data = (guchar *)wmem_alloc(wmem_file_scope(), pre_master_len);
2583         pre_master_secret.data_len = pre_master_len;
2584         /* 2 bytes psk_len*/
2585         pre_master_secret.data[0] = psk_len >> 8;
2586         pre_master_secret.data[1] = psk_len & 0xFF;
2587         /* psk_len bytes times 0*/
2588         memset(&pre_master_secret.data[2], 0, psk_len);
2589         /* 2 bytes psk_len*/
2590         pre_master_secret.data[psk_len + 2] = psk_len >> 8;
2591         pre_master_secret.data[psk_len + 3] = psk_len & 0xFF;
2592         /* psk*/
2593         memcpy(&pre_master_secret.data[psk_len + 4], ssl_session->psk.data, psk_len);
2594
2595         ssl_session->pre_master_secret.data = pre_master_secret.data;
2596         ssl_session->pre_master_secret.data_len = pre_master_len;
2597         /*ssl_debug_printf("pre master secret",&ssl->pre_master_secret);*/
2598
2599         /* Remove the master secret if it was there.
2600            This forces keying material regeneration in
2601            case we're renegotiating */
2602         ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
2603         ssl_session->state |= SSL_PRE_MASTER_SECRET;
2604         return TRUE;
2605     }
2606     else
2607     {
2608         StringInfo encrypted_pre_master;
2609         guint encrlen, skip;
2610         encrlen = length;
2611         skip = 0;
2612
2613         /* get encrypted data, on tls1 we have to skip two bytes
2614          * (it's the encrypted len and should be equal to record len - 2)
2615          * in case of rsa1024 that would be 128 + 2 = 130; for psk not necessary
2616          */
2617         if (ssl_session->cipher_suite.kex == KEX_RSA &&
2618            (ssl_session->session.version == SSL_VER_TLS || ssl_session->session.version == SSL_VER_TLSv1DOT1 ||
2619             ssl_session->session.version == SSL_VER_TLSv1DOT2 || ssl_session->session.version == SSL_VER_DTLS ||
2620             ssl_session->session.version == SSL_VER_DTLS1DOT2))
2621         {
2622             encrlen  = tvb_get_ntohs(tvb, offset);
2623             skip = 2;
2624             if (encrlen > length - 2)
2625             {
2626                 ssl_debug_printf("%s: wrong encrypted length (%d max %d)\n",
2627                                  G_STRFUNC, encrlen, length);
2628                 return FALSE;
2629             }
2630         }
2631         /* the valid lower bound is higher than 8, but it is sufficient for the
2632          * ssl keylog file below */
2633         if (encrlen < 8) {
2634             ssl_debug_printf("%s: invalid encrypted pre-master key length %d\n",
2635                              G_STRFUNC, encrlen);
2636             return FALSE;
2637         }
2638
2639         encrypted_pre_master.data = (guchar *)wmem_alloc(wmem_file_scope(), encrlen);
2640         encrypted_pre_master.data_len = encrlen;
2641         tvb_memcpy(tvb, encrypted_pre_master.data, offset+skip, encrlen);
2642
2643         if (ssl_session->private_key) {
2644             /* try to decrypt encrypted pre-master with RSA key */
2645             if (ssl_decrypt_pre_master_secret(ssl_session,
2646                 &encrypted_pre_master, ssl_session->private_key))
2647                 return TRUE;
2648
2649             ssl_debug_printf("%s: can't decrypt pre-master secret\n",
2650                              G_STRFUNC);
2651         }
2652
2653         /* try to find the pre-master secret from the encrypted one. The
2654          * ssl key logfile stores only the first 8 bytes, so truncate it */
2655         encrypted_pre_master.data_len = 8;
2656         if (ssl_restore_master_key(ssl_session, "Encrypted pre-master secret",
2657             TRUE, mk_map->pre_master, &encrypted_pre_master))
2658             return TRUE;
2659     }
2660     return FALSE;
2661 }
2662
2663 int
2664 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
2665 {
2666     StringInfo  key_block;
2667     guint8      _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
2668     guint8      _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
2669     gint        needed;
2670     guint8     *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
2671
2672     /* check for enough info to proced */
2673     guint need_all = SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION;
2674     guint need_any = SSL_MASTER_SECRET | SSL_PRE_MASTER_SECRET;
2675     if (((ssl_session->state & need_all) != need_all) || ((ssl_session->state & need_any) == 0)) {
2676         ssl_debug_printf("ssl_generate_keyring_material not enough data to generate key "
2677                          "(0x%02X required 0x%02X or 0x%02X)\n", ssl_session->state,
2678                          need_all|SSL_MASTER_SECRET, need_all|SSL_PRE_MASTER_SECRET);
2679         return -1;
2680     }
2681
2682     /* if master key is not available, generate is from the pre-master secret */
2683     if (!(ssl_session->state & SSL_MASTER_SECRET)) {
2684         if ((ssl_session->state & SSL_EXTENDED_MASTER_SECRET_MASK) == SSL_EXTENDED_MASTER_SECRET_MASK) {
2685             StringInfo handshake_hashed_data;
2686             gint ret;
2687
2688             handshake_hashed_data.data = NULL;
2689             handshake_hashed_data.data_len = 0;
2690
2691             ssl_debug_printf("%s:PRF(pre_master_secret_extended)\n", G_STRFUNC);
2692             ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
2693             DISSECTOR_ASSERT(ssl_session->handshake_data.data_len > 0);
2694
2695             switch(ssl_session->version_netorder) {
2696             case TLSV1_VERSION:
2697             case TLSV1DOT1_VERSION:
2698             case DTLSV1DOT0_VERSION:
2699             case DTLSV1DOT0_VERSION_NOT:
2700                 ret = tls_handshake_hash(ssl_session, &handshake_hashed_data);
2701                 break;
2702             default:
2703                 switch (ssl_session->cipher_suite.dig) {
2704                 case DIG_SHA384:
2705                     ret = tls12_handshake_hash(ssl_session, GCRY_MD_SHA384, &handshake_hashed_data);
2706                     break;
2707                 default:
2708                     ret = tls12_handshake_hash(ssl_session, GCRY_MD_SHA256, &handshake_hashed_data);
2709                     break;
2710                 }
2711                 break;
2712             }
2713             if (ret) {
2714                 ssl_debug_printf("%s can't generate handshake hash\n", G_STRFUNC);
2715                 return -1;
2716             }
2717
2718             wmem_free(wmem_file_scope(), ssl_session->handshake_data.data);
2719             ssl_session->handshake_data.data = NULL;
2720             ssl_session->handshake_data.data_len = 0;
2721
2722             if (!prf(ssl_session, &ssl_session->pre_master_secret, "extended master secret",
2723                      &handshake_hashed_data,
2724                      NULL, &ssl_session->master_secret,
2725                      SSL_MASTER_SECRET_LENGTH)) {
2726                 ssl_debug_printf("%s can't generate master_secret\n", G_STRFUNC);
2727                 g_free(handshake_hashed_data.data);
2728                 return -1;
2729             }
2730             g_free(handshake_hashed_data.data);
2731         } else {
2732             ssl_debug_printf("%s:PRF(pre_master_secret)\n", G_STRFUNC);
2733             ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
2734             ssl_print_string("client random",&ssl_session->client_random);
2735             ssl_print_string("server random",&ssl_session->server_random);
2736             if (!prf(ssl_session, &ssl_session->pre_master_secret, "master secret",
2737                      &ssl_session->client_random,
2738                      &ssl_session->server_random, &ssl_session->master_secret,
2739                      SSL_MASTER_SECRET_LENGTH)) {
2740                 ssl_debug_printf("%s can't generate master_secret\n", G_STRFUNC);
2741                 return -1;
2742             }
2743         }
2744         ssl_print_string("master secret",&ssl_session->master_secret);
2745
2746         /* the pre-master secret has been 'consumend' so we must clear it now */
2747         ssl_session->state &= ~SSL_PRE_MASTER_SECRET;
2748         ssl_session->state |= SSL_MASTER_SECRET;
2749     }
2750
2751     /* Compute the key block. First figure out how much data we need*/
2752     needed=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len*2;
2753     needed+=ssl_session->cipher_suite.bits / 4;
2754     if(ssl_session->cipher_suite.block>1)
2755         needed+=ssl_session->cipher_suite.block*2;
2756
2757     key_block.data = (guchar *)g_malloc(needed);
2758     ssl_debug_printf("%s sess key generation\n", G_STRFUNC);
2759     if (!prf(ssl_session, &ssl_session->master_secret, "key expansion",
2760             &ssl_session->server_random,&ssl_session->client_random,
2761             &key_block, needed)) {
2762         ssl_debug_printf("%s can't generate key_block\n", G_STRFUNC);
2763         goto fail;
2764     }
2765     ssl_print_string("key expansion", &key_block);
2766
2767     ptr=key_block.data;
2768     /* AEAD ciphers do not have a separate MAC */
2769     if (ssl_session->cipher_suite.mode == MODE_GCM ||
2770         ssl_session->cipher_suite.mode == MODE_CCM ||
2771         ssl_session->cipher_suite.mode == MODE_CCM_8) {
2772         c_mk = s_mk = NULL;
2773     } else {
2774         c_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
2775         s_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
2776     }
2777
2778     c_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
2779     s_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
2780
2781     if(ssl_session->cipher_suite.block>1){
2782         c_iv=ptr; ptr+=ssl_session->cipher_suite.block;
2783         s_iv=ptr; /*ptr+=ssl_session->cipher_suite.block;*/
2784     }
2785
2786     /* export ciphers work with a smaller key length */
2787     if (ssl_session->cipher_suite.eff_bits < ssl_session->cipher_suite.bits) {
2788         if(ssl_session->cipher_suite.block>1){
2789
2790             /* We only have room for MAX_BLOCK_SIZE bytes IVs, but that's
2791              all we should need. This is a sanity check */
2792             if(ssl_session->cipher_suite.block>MAX_BLOCK_SIZE) {
2793                 ssl_debug_printf("%s cipher suite block must be at most %d nut is %d\n",
2794                 G_STRFUNC, MAX_BLOCK_SIZE, ssl_session->cipher_suite.block);
2795                 goto fail;
2796             }
2797
2798             if(ssl_session->version_netorder==SSLV3_VERSION){
2799                 /* The length of these fields are ignored by this caller */
2800                 StringInfo iv_c, iv_s;
2801                 iv_c.data = _iv_c;
2802                 iv_s.data = _iv_s;
2803
2804                 ssl_debug_printf("%s ssl3_generate_export_iv\n", G_STRFUNC);
2805                 ssl3_generate_export_iv(&ssl_session->client_random,
2806                         &ssl_session->server_random, &iv_c,
2807                         ssl_session->cipher_suite.block);
2808                 ssl_debug_printf("%s ssl3_generate_export_iv(2)\n", G_STRFUNC);
2809                 ssl3_generate_export_iv(&ssl_session->server_random,
2810                         &ssl_session->client_random, &iv_s,
2811                         ssl_session->cipher_suite.block);
2812             }
2813             else{
2814                 guint8 _iv_block[MAX_BLOCK_SIZE * 2];
2815                 StringInfo iv_block;
2816                 StringInfo key_null;
2817                 guint8 _key_null;
2818
2819                 key_null.data = &_key_null;
2820                 key_null.data_len = 0;
2821
2822                 iv_block.data = _iv_block;
2823
2824                 ssl_debug_printf("%s prf(iv_block)\n", G_STRFUNC);
2825                 if (!prf(ssl_session, &key_null, "IV block",
2826                         &ssl_session->client_random,
2827                         &ssl_session->server_random, &iv_block,
2828                         ssl_session->cipher_suite.block * 2)) {
2829                     ssl_debug_printf("%s can't generate tls31 iv block\n", G_STRFUNC);
2830                     goto fail;
2831                 }
2832
2833                 memcpy(_iv_c,iv_block.data,ssl_session->cipher_suite.block);
2834                 memcpy(_iv_s,iv_block.data+ssl_session->cipher_suite.block,
2835                     ssl_session->cipher_suite.block);
2836             }
2837
2838             c_iv=_iv_c;
2839             s_iv=_iv_s;
2840         }
2841
2842         if (ssl_session->version_netorder==SSLV3_VERSION){
2843
2844             SSL_MD5_CTX md5;
2845             ssl_debug_printf("%s MD5(client_random)\n", G_STRFUNC);
2846
2847             ssl_md5_init(&md5);
2848             ssl_md5_update(&md5,c_wk,ssl_session->cipher_suite.eff_bits/8);
2849             ssl_md5_update(&md5,ssl_session->client_random.data,
2850                 ssl_session->client_random.data_len);
2851             ssl_md5_update(&md5,ssl_session->server_random.data,
2852                 ssl_session->server_random.data_len);
2853             ssl_md5_final(_key_c,&md5);
2854             ssl_md5_cleanup(&md5);
2855             c_wk=_key_c;
2856
2857             ssl_md5_init(&md5);
2858             ssl_debug_printf("%s MD5(server_random)\n", G_STRFUNC);
2859             ssl_md5_update(&md5,s_wk,ssl_session->cipher_suite.eff_bits/8);
2860             ssl_md5_update(&md5,ssl_session->server_random.data,
2861                 ssl_session->server_random.data_len);
2862             ssl_md5_update(&md5,ssl_session->client_random.data,
2863                 ssl_session->client_random.data_len);
2864             ssl_md5_final(_key_s,&md5);
2865             ssl_md5_cleanup(&md5);
2866             s_wk=_key_s;
2867         }
2868         else{
2869             StringInfo key_c, key_s, k;
2870             key_c.data = _key_c;
2871             key_s.data = _key_s;
2872
2873             k.data = c_wk;
2874             k.data_len = ssl_session->cipher_suite.eff_bits/8;
2875             ssl_debug_printf("%s PRF(key_c)\n", G_STRFUNC);
2876             if (!prf(ssl_session, &k, "client write key",
2877                     &ssl_session->client_random,
2878                     &ssl_session->server_random, &key_c, sizeof(_key_c))) {
2879                 ssl_debug_printf("%s can't generate tll31 server key \n", G_STRFUNC);
2880                 goto fail;
2881             }
2882             c_wk=_key_c;
2883
2884             k.data = s_wk;
2885             k.data_len = ssl_session->cipher_suite.eff_bits/8;
2886             ssl_debug_printf("%s PRF(key_s)\n", G_STRFUNC);
2887             if (!prf(ssl_session, &k, "server write key",
2888                     &ssl_session->client_random,
2889                     &ssl_session->server_random, &key_s, sizeof(_key_s))) {
2890                 ssl_debug_printf("%s can't generate tll31 client key \n", G_STRFUNC);
2891                 goto fail;
2892             }
2893             s_wk=_key_s;
2894         }
2895     }
2896
2897     /* show key material info */
2898     if (c_mk != NULL) {
2899         ssl_print_data("Client MAC key",c_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
2900         ssl_print_data("Server MAC key",s_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
2901     }
2902     ssl_print_data("Client Write key",c_wk,ssl_session->cipher_suite.bits/8);
2903     ssl_print_data("Server Write key",s_wk,ssl_session->cipher_suite.bits/8);
2904
2905     if(ssl_session->cipher_suite.block>1) {
2906         ssl_print_data("Client Write IV",c_iv,ssl_session->cipher_suite.block);
2907         ssl_print_data("Server Write IV",s_iv,ssl_session->cipher_suite.block);
2908     }
2909     else {
2910         ssl_print_data("Client Write IV",c_iv,8);
2911         ssl_print_data("Server Write IV",s_iv,8);
2912     }
2913
2914     /* create both client and server ciphers*/
2915     ssl_debug_printf("%s ssl_create_decoder(client)\n", G_STRFUNC);
2916     ssl_session->client_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->session.compression, c_mk, c_wk, c_iv);
2917     if (!ssl_session->client_new) {
2918         ssl_debug_printf("%s can't init client decoder\n", G_STRFUNC);
2919         goto fail;
2920     }
2921     ssl_debug_printf("%s ssl_create_decoder(server)\n", G_STRFUNC);
2922     ssl_session->server_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->session.compression, s_mk, s_wk, s_iv);
2923     if (!ssl_session->server_new) {
2924         ssl_debug_printf("%s can't init client decoder\n", G_STRFUNC);
2925         goto fail;
2926     }
2927
2928     ssl_debug_printf("%s: client seq %d, server seq %d\n",
2929         G_STRFUNC, ssl_session->client_new->seq, ssl_session->server_new->seq);
2930     g_free(key_block.data);
2931     ssl_session->state |= SSL_HAVE_SESSION_KEY;
2932     return 0;
2933
2934 fail:
2935     g_free(key_block.data);
2936     return -1;
2937 }
2938
2939 void
2940 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
2941 {
2942     ssl_debug_printf("ssl_change_cipher %s\n", (server)?"SERVER":"CLIENT");
2943     if (server) {
2944         ssl_session->server = ssl_session->server_new;
2945         ssl_session->server_new = NULL;
2946     } else {
2947         ssl_session->client = ssl_session->client_new;
2948         ssl_session->client_new = NULL;
2949     }
2950 }
2951
2952 static gboolean
2953 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
2954     StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk)
2955 {
2956     gint i;
2957
2958     if (!encrypted_pre_master)
2959         return FALSE;
2960
2961     if(ssl_session->cipher_suite.kex == KEX_DH) {
2962         ssl_debug_printf("%s: session uses DH (%d) key exchange, which is "
2963                          "impossible to decrypt\n", G_STRFUNC, KEX_DH);
2964         return FALSE;
2965     } else if(ssl_session->cipher_suite.kex != KEX_RSA) {
2966          ssl_debug_printf("%s key exchange %d different from KEX_RSA (%d)\n",
2967                           G_STRFUNC, ssl_session->cipher_suite.kex, KEX_RSA);
2968         return FALSE;
2969     }
2970
2971     /* with tls key loading will fail if not rsa type, so no need to check*/
2972     ssl_print_string("pre master encrypted",encrypted_pre_master);
2973     ssl_debug_printf("%s: RSA_private_decrypt\n", G_STRFUNC);
2974     i=ssl_private_decrypt(encrypted_pre_master->data_len,
2975         encrypted_pre_master->data, pk);
2976
2977     if (i!=48) {
2978         ssl_debug_printf("%s wrong pre_master_secret length (%d, expected "
2979                          "%d)\n", G_STRFUNC, i, 48);
2980         return FALSE;
2981     }
2982
2983     /* the decrypted data has been written into the pre_master key buffer */
2984     ssl_session->pre_master_secret.data = encrypted_pre_master->data;
2985     ssl_session->pre_master_secret.data_len=48;
2986     ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
2987
2988     /* Remove the master secret if it was there.
2989        This forces keying material regeneration in
2990        case we're renegotiating */
2991     ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
2992     ssl_session->state |= SSL_PRE_MASTER_SECRET;
2993     return TRUE;
2994 }
2995
2996 /* convert network byte order 32 byte number to right-aligned host byte order *
2997  * 8 bytes buffer */
2998 static gint fmt_seq(guint32 num, guint8* buf)
2999 {
3000     guint32 netnum;
3001
3002     memset(buf,0,8);
3003     netnum=g_htonl(num);
3004     memcpy(buf+4,&netnum,4);
3005
3006     return(0);
3007 }
3008
3009 static gint
3010 tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
3011         guint32 datalen, guint8* mac)
3012 {
3013     SSL_HMAC hm;
3014     gint     md;
3015     guint32  len;
3016     guint8   buf[DIGEST_MAX_SIZE];
3017     gint16   temp;
3018
3019     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3020     ssl_debug_printf("tls_check_mac mac type:%s md %d\n",
3021         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
3022
3023     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
3024         return -1;
3025
3026     /* hash sequence number */
3027     fmt_seq(decoder->seq,buf);
3028
3029     decoder->seq++;
3030
3031     ssl_hmac_update(&hm,buf,8);
3032
3033     /* hash content type */
3034     buf[0]=ct;
3035     ssl_hmac_update(&hm,buf,1);
3036
3037     /* hash version,data length and data*/
3038     /* *((gint16*)buf) = g_htons(ver); */
3039     temp = g_htons(ver);
3040     memcpy(buf, &temp, 2);
3041     ssl_hmac_update(&hm,buf,2);
3042
3043     /* *((gint16*)buf) = g_htons(datalen); */
3044     temp = g_htons(datalen);
3045     memcpy(buf, &temp, 2);
3046     ssl_hmac_update(&hm,buf,2);
3047     ssl_hmac_update(&hm,data,datalen);
3048
3049     /* get digest and digest len*/
3050     len = sizeof(buf);
3051     ssl_hmac_final(&hm,buf,&len);
3052     ssl_hmac_cleanup(&hm);
3053     ssl_print_data("Mac", buf, len);
3054     if(memcmp(mac,buf,len))
3055         return -1;
3056
3057     return 0;
3058 }
3059
3060 static int
3061 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
3062         guint32 datalen, guint8* mac)
3063 {
3064     SSL_MD  mc;
3065     gint    md;
3066     guint32 len;
3067     guint8  buf[64],dgst[20];
3068     gint    pad_ct;
3069     gint16  temp;
3070
3071     pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
3072
3073     /* get cipher used for digest comptuation */
3074     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3075     if (ssl_md_init(&mc,md) !=0)
3076         return -1;
3077
3078     /* do hash computation on data && padding */
3079     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
3080
3081     /* hash padding*/
3082     memset(buf,0x36,pad_ct);
3083     ssl_md_update(&mc,buf,pad_ct);
3084
3085     /* hash sequence number */
3086     fmt_seq(decoder->seq,buf);
3087     decoder->seq++;
3088     ssl_md_update(&mc,buf,8);
3089
3090     /* hash content type */
3091     buf[0]=ct;
3092     ssl_md_update(&mc,buf,1);
3093
3094     /* hash data length in network byte order and data*/
3095     /* *((gint16* )buf) = g_htons(datalen); */
3096     temp = g_htons(datalen);
3097     memcpy(buf, &temp, 2);
3098     ssl_md_update(&mc,buf,2);
3099     ssl_md_update(&mc,data,datalen);
3100
3101     /* get partial digest */
3102     ssl_md_final(&mc,dgst,&len);
3103     ssl_md_cleanup(&mc);
3104
3105     ssl_md_init(&mc,md);
3106
3107     /* hash mac key */
3108     ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
3109
3110     /* hash padding and partial digest*/
3111     memset(buf,0x5c,pad_ct);
3112     ssl_md_update(&mc,buf,pad_ct);
3113     ssl_md_update(&mc,dgst,len);
3114
3115     ssl_md_final(&mc,dgst,&len);
3116     ssl_md_cleanup(&mc);
3117
3118     if(memcmp(mac,dgst,len))
3119         return -1;
3120
3121     return(0);
3122 }
3123
3124 static gint
3125 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
3126         guint32 datalen, guint8* mac)
3127 {
3128     SSL_HMAC hm;
3129     gint     md;
3130     guint32  len;
3131     guint8   buf[DIGEST_MAX_SIZE];
3132     gint16   temp;
3133
3134     md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
3135     ssl_debug_printf("dtls_check_mac mac type:%s md %d\n",
3136         ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
3137
3138     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
3139         return -1;
3140     ssl_debug_printf("dtls_check_mac seq: %d epoch: %d\n",decoder->seq,decoder->epoch);
3141     /* hash sequence number */
3142     fmt_seq(decoder->seq,buf);
3143     buf[0]=decoder->epoch>>8;
3144     buf[1]=(guint8)decoder->epoch;
3145
3146     ssl_hmac_update(&hm,buf,8);
3147
3148     /* hash content type */
3149     buf[0]=ct;
3150     ssl_hmac_update(&hm,buf,1);
3151
3152     /* hash version,data length and data */
3153     temp = g_htons(ver);
3154     memcpy(buf, &temp, 2);
3155     ssl_hmac_update(&hm,buf,2);
3156
3157     temp = g_htons(datalen);
3158     memcpy(buf, &temp, 2);
3159     ssl_hmac_update(&hm,buf,2);
3160     ssl_hmac_update(&hm,data,datalen);
3161     /* get digest and digest len */
3162     len = sizeof(buf);
3163     ssl_hmac_final(&hm,buf,&len);
3164     ssl_hmac_cleanup(&hm);
3165     ssl_print_data("Mac", buf, len);
3166     if(memcmp(mac,buf,len))
3167         return -1;
3168
3169     return(0);
3170 }
3171
3172 #ifdef HAVE_LIBZ
3173 static int
3174 ssl_decompress_record(SslDecompress* decomp, const guchar* in, guint inl, StringInfo* out_str, guint* outl)
3175 {
3176     gint err;
3177
3178     switch (decomp->compression) {
3179         case 1:  /* DEFLATE */
3180             err = Z_OK;
3181             if (out_str->data_len < 16384) {  /* maximal plain length */
3182                 ssl_data_realloc(out_str, 16384);
3183             }
3184             decomp->istream.next_in = (guchar*)in;
3185             decomp->istream.avail_in = inl;
3186             decomp->istream.next_out = out_str->data;
3187             decomp->istream.avail_out = out_str->data_len;
3188             if (inl > 0)
3189                 err = inflate(&decomp->istream, Z_SYNC_FLUSH);
3190             if (err != Z_OK) {
3191                 ssl_debug_printf("ssl_decompress_record: inflate() failed - %d\n", err);
3192                 return -1;
3193             }
3194             *outl = out_str->data_len - decomp->istream.avail_out;
3195             break;
3196         default:
3197             ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
3198             return -1;
3199     }
3200     return 0;
3201 }
3202 #else
3203 int
3204 ssl_decompress_record(SslDecompress* decomp _U_, const guchar* in _U_, guint inl _U_, StringInfo* out_str _U_, guint* outl _U_)
3205 {
3206     ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
3207     return -1;
3208 }
3209 #endif
3210
3211 int
3212 ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
3213         const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl)
3214 {
3215     guint   pad, worklen, uncomplen;
3216     guint8 *mac;
3217
3218     ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
3219     ssl_print_data("Ciphertext",in, inl);
3220
3221     /* ensure we have enough storage space for decrypted data */
3222     if (inl > out_str->data_len)
3223     {
3224         ssl_debug_printf("ssl_decrypt_record: allocating %d bytes for decrypt data (old len %d)\n",
3225                 inl + 32, out_str->data_len);
3226         ssl_data_realloc(out_str, inl + 32);
3227     }
3228
3229     /* RFC 6101/2246: SSLCipherText/TLSCipherText has two structures for types:
3230      * (notation: { unencrypted, [ encrypted ] })
3231      * GenericStreamCipher: { [content, mac] }
3232      * GenericBlockCipher: { IV (TLS 1.1+), [content, mac, padding, padding_len] }
3233      * RFC 5426 (TLS 1.2): TLSCipherText has additionally:
3234      * GenericAEADCipher: { nonce_explicit, [content] }
3235      * RFC 4347 (DTLS): based on TLS 1.1, only GenericBlockCipher is supported.
3236      * RFC 6347 (DTLS 1.2): based on TLS 1.2, includes GenericAEADCipher too.
3237      */
3238
3239     /* (TLS 1.1 and later, DTLS) Extract explicit IV for GenericBlockCipher */
3240     if (decoder->cipher_suite->mode == MODE_CBC) {
3241         switch (ssl->version_netorder) {
3242         case TLSV1DOT1_VERSION:
3243         case TLSV1DOT2_VERSION:
3244         case DTLSV1DOT0_VERSION:
3245         case DTLSV1DOT2_VERSION:
3246         case DTLSV1DOT0_VERSION_NOT:
3247             if ((gint)inl < decoder->cipher_suite->block) {
3248                 ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for IV %d\n",
3249                         inl, decoder->cipher_suite->block);
3250                 return -1;
3251             }
3252             pad = gcry_cipher_setiv(decoder->evp, in, decoder->cipher_suite->block);
3253             if (pad != 0) {
3254                 ssl_debug_printf("ssl_decrypt_record failed: failed to set IV: %s %s\n",
3255                         gcry_strsource (pad), gcry_strerror (pad));
3256             }
3257
3258             inl -= decoder->cipher_suite->block;
3259             in += decoder->cipher_suite->block;
3260             break;
3261         }
3262     }
3263
3264     /* Nonce for GenericAEADCipher */
3265     if (decoder->cipher_suite->mode == MODE_GCM ||
3266         decoder->cipher_suite->mode == MODE_CCM ||
3267         decoder->cipher_suite->mode == MODE_CCM_8) {
3268         /* 4 bytes write_iv, 8 bytes explicit_nonce, 4 bytes counter */
3269         guchar gcm_nonce[16] = { 0 };
3270
3271         if ((gint)inl < SSL_EX_NONCE_LEN_GCM) {
3272             ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for nonce %d\n",
3273                 inl, SSL_EX_NONCE_LEN_GCM);
3274             return -1;
3275         }
3276
3277         if (decoder->cipher_suite->mode == MODE_GCM) {
3278             memcpy(gcm_nonce, decoder->write_iv.data, decoder->write_iv.data_len); /* salt */
3279             memcpy(gcm_nonce + decoder->write_iv.data_len, in, SSL_EX_NONCE_LEN_GCM);
3280             /* NIST SP 800-38D, sect. 7.2 says that the 32-bit counter part starts
3281              * at 1, and gets incremented before passing to the block cipher. */
3282             gcm_nonce[4 + SSL_EX_NONCE_LEN_GCM + 3] = 2;
3283         } else { /* MODE_CCM and MODE_CCM_8 */
3284             /* The nonce for CCM and GCM are the same, but the nonce is used as input
3285              * in the CCM algorithm described in RFC 3610. The nonce generated here is
3286              * the one from RFC 3610 sect 2.3. Encryption. */
3287             /* Flags: (L-1) ; L = 16 - 1 - nonceSize */
3288             gcm_nonce[0] = 3 - 1;
3289
3290             memcpy(gcm_nonce + 1, decoder->write_iv.data, decoder->write_iv.data_len); /* salt */
3291             memcpy(gcm_nonce + 1 + decoder->write_iv.data_len, in, SSL_EX_NONCE_LEN_GCM);
3292             gcm_nonce[4 + SSL_EX_NONCE_LEN_GCM + 3] = 1;
3293         }
3294
3295         pad = gcry_cipher_setctr (decoder->evp, gcm_nonce, sizeof (gcm_nonce));
3296         if (pad != 0) {
3297             ssl_debug_printf("ssl_decrypt_record failed: failed to set CTR: %s %s\n",
3298                     gcry_strsource (pad), gcry_strerror (pad));
3299             return -1;
3300         }
3301         inl -= SSL_EX_NONCE_LEN_GCM;
3302         in += SSL_EX_NONCE_LEN_GCM;
3303     }
3304
3305     /* First decrypt*/
3306     if ((pad = ssl_cipher_decrypt(&decoder->evp, out_str->data, out_str->data_len, in, inl))!= 0) {
3307         ssl_debug_printf("ssl_decrypt_record failed: ssl_cipher_decrypt: %s %s\n", gcry_strsource (pad),
3308                     gcry_strerror (pad));
3309         return -1;
3310     }
3311
3312     ssl_print_data("Plaintext", out_str->data, inl);
3313     worklen=inl;
3314
3315     /* RFC 5116 sect 5.1/5.3: AES128/256 GCM/CCM uses 16 bytes for auth tag
3316      * RFC 6655 sect 6.1: AEAD_AES_128_CCM uses 16 bytes for auth tag */
3317     if (decoder->cipher_suite->mode == MODE_GCM ||
3318         decoder->cipher_suite->mode == MODE_CCM) {
3319         if (worklen < 16) {
3320             ssl_debug_printf("ssl_decrypt_record failed: missing tag, work %d\n", worklen);
3321             return -1;
3322         }
3323         /* XXX - validate auth tag */
3324         worklen -= 16;
3325     }
3326     /* RFC 6655 sect 6.1: AEAD_AES_128_CCM_8 uses 8 bytes for auth tag */
3327     if (decoder->cipher_suite->mode == MODE_CCM_8) {
3328         if (worklen < 8) {
3329             ssl_debug_printf("ssl_decrypt_record failed: missing tag, work %d\n", worklen);
3330             return -1;
3331         }
3332         /* XXX - validate auth tag */
3333         worklen -= 8;
3334     }
3335
3336     /* strip padding for GenericBlockCipher */
3337     if (decoder->cipher_suite->mode == MODE_CBC) {
3338         if (inl < 1) { /* Should this check happen earlier? */
3339             ssl_debug_printf("ssl_decrypt_record failed: input length %d too small\n", inl);
3340             return -1;
3341         }
3342         pad=out_str->data[inl-1];
3343         if (worklen <= pad) {
3344             ssl_debug_printf("ssl_decrypt_record failed: padding %d too large for work %d\n",
3345                 pad, worklen);
3346             return -1;
3347         }
3348         worklen-=(pad+1);
3349         ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
3350             pad, worklen);
3351     }
3352
3353     /* MAC for GenericStreamCipher and GenericBlockCipher */
3354     if (decoder->cipher_suite->mode == MODE_STREAM ||
3355         decoder->cipher_suite->mode == MODE_CBC) {
3356         if (ssl_cipher_suite_dig(decoder->cipher_suite)->len > (gint)worklen) {
3357             ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
3358             return -1;
3359         }
3360         worklen-=ssl_cipher_suite_dig(decoder->cipher_suite)->len;
3361         mac = out_str->data + worklen;
3362     } else /* if (decoder->cipher_suite->mode == MODE_GCM) */ {
3363         /* GenericAEADCipher has no MAC */
3364         goto skip_mac;
3365     }
3366
3367     /* Now check the MAC */
3368     ssl_debug_printf("checking mac (len %d, version %X, ct %d seq %d)\n",
3369         worklen, ssl->version_netorder, ct, decoder->seq);
3370     if(ssl->version_netorder==SSLV3_VERSION){
3371         if(ssl3_check_mac(decoder,ct,out_str->data,worklen,mac) < 0) {
3372             if(ssl_ignore_mac_failed) {
3373                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3374             }
3375             else{
3376                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3377                 return -1;
3378             }
3379         }
3380         else{
3381             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3382         }
3383     }
3384     else if(ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION || ssl->version_netorder==TLSV1DOT2_VERSION){
3385         if(tls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)< 0) {
3386             if(ssl_ignore_mac_failed) {
3387                 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3388             }
3389             else{
3390                 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3391                 return -1;
3392             }
3393         }
3394         else{
3395             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3396         }
3397     }
3398     else if(ssl->version_netorder==DTLSV1DOT0_VERSION ||
3399         ssl->version_netorder==DTLSV1DOT2_VERSION ||
3400         ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
3401         /* Try rfc-compliant mac first, and if failed, try old openssl's non-rfc-compliant mac */
3402         if(dtls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)>= 0) {
3403             ssl_debug_printf("ssl_decrypt_record: mac ok\n");
3404         }
3405         else if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)>= 0) {
3406             ssl_debug_printf("ssl_decrypt_record: dtls rfc-compliant mac failed, but old openssl's non-rfc-compliant mac ok\n");
3407         }
3408         else if(ssl_ignore_mac_failed) {
3409             ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
3410         }
3411         else{
3412             ssl_debug_printf("ssl_decrypt_record: mac failed\n");
3413             return -1;
3414         }
3415     }
3416 skip_mac:
3417
3418     *outl = worklen;
3419
3420     if (decoder->compression > 0) {
3421         ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
3422         ssl_data_copy(comp_str, out_str);
3423         ssl_print_data("Plaintext compressed", comp_str->data, worklen);
3424         if (!decoder->decomp) {
3425             ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
3426             return -1;
3427         }
3428         if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
3429         ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
3430         *outl = uncomplen;
3431     }
3432
3433     return 0;
3434 }
3435
3436 #define RSA_PARS 6
3437 static SSL_PRIVATE_KEY*
3438 ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
3439 {
3440     gnutls_datum_t rsa_datum[RSA_PARS]; /* m, e, d, p, q, u */
3441     size_t         tmp_size;
3442     gcry_error_t   gret;
3443     gcry_sexp_t    rsa_priv_key = NULL;
3444     gint           i;
3445     int            ret;
3446     size_t         buf_len;
3447     unsigned char  buf_keyid[32];
3448
3449 #ifdef SSL_FAST
3450     gcry_mpi_t* rsa_params = g_malloc(sizeof(gcry_mpi_t)*RSA_PARS);
3451 #else
3452     gcry_mpi_t rsa_params[RSA_PARS];
3453 #endif
3454
3455     buf_len = sizeof(buf_keyid);
3456     ret = gnutls_x509_privkey_get_key_id(priv_key, 0, buf_keyid, &buf_len);
3457     if (ret != 0) {
3458         ssl_debug_printf( "gnutls_x509_privkey_get_key_id(ssl_pkey, 0, buf_keyid, &buf_len) - %s\n", gnutls_strerror(ret));
3459     } else {
3460         char* keyid = (char*)bytestring_to_str(NULL, buf_keyid, (int) buf_len, ':');
3461         ssl_debug_printf( "Private key imported: KeyID %s\n", keyid);
3462         wmem_free(NULL, keyid);
3463     }
3464
3465     /* RSA get parameter */
3466     if (gnutls_x509_privkey_export_rsa_raw(priv_key,
3467                                            &rsa_datum[0],
3468                                            &rsa_datum[1],
3469                                            &rsa_datum[2],
3470                                            &rsa_datum[3],
3471                                            &rsa_datum[4],
3472                                            &rsa_datum[5])  != 0) {
3473         ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
3474 #ifdef SSL_FAST
3475         g_free(rsa_params);
3476 #endif
3477         return NULL;
3478     }
3479
3480     /* convert each rsa parameter to mpi format*/
3481     for(i=0; i<RSA_PARS; i++) {
3482       gret = gcry_mpi_scan(&rsa_params[i], GCRYMPI_FMT_USG, rsa_datum[i].data, rsa_datum[i].size,&tmp_size);
3483       /* these buffers were allocated by gnutls_x509_privkey_export_rsa_raw() */
3484       g_free(rsa_datum[i].data);
3485       if (gret != 0) {
3486         ssl_debug_printf("ssl_load_key: can't convert m rsa param to int (size %d)\n", rsa_datum[i].size);
3487 #ifdef SSL_FAST
3488         g_free(rsa_params);
3489 #endif
3490         return NULL;
3491       }
3492     }
3493
3494     /* libgcrypt expects p < q, and gnutls might not return it as such, depending on gnutls version and its crypto backend */
3495     if (gcry_mpi_cmp(rsa_params[3], rsa_params[4]) > 0)
3496     {
3497         ssl_debug_printf("ssl_load_key: swapping p and q parameters and recomputing u\n");
3498         gcry_mpi_swap(rsa_params[3], rsa_params[4]);
3499         gcry_mpi_invm(rsa_params[5], rsa_params[3], rsa_params[4]);
3500     }
3501
3502     if  (gcry_sexp_build( &rsa_priv_key, NULL,
3503             "(private-key(rsa((n%m)(e%m)(d%m)(p%m)(q%m)(u%m))))", rsa_params[0],
3504             rsa_params[1], rsa_params[2], rsa_params[3], rsa_params[4],
3505             rsa_params[5]) != 0) {
3506         ssl_debug_printf("ssl_load_key: can't build rsa private key s-exp\n");
3507 #ifdef SSL_FAST
3508         g_free(rsa_params);
3509 #endif
3510         return NULL;
3511     }
3512
3513 #ifdef SSL_FAST
3514     return rsa_params;
3515 #else
3516     for (i=0; i< 6; i++)
3517         gcry_mpi_release(rsa_params[i]);
3518     return rsa_priv_key;
3519 #endif
3520
3521 }
3522
3523 Ssl_private_key_t *
3524 ssl_load_key(FILE* fp)
3525 {
3526     /* gnutls makes our work much harder, since we have to work internally with
3527      * s-exp formatted data, but PEM loader exports only in "gnutls_datum_t"
3528      * format, and a datum -> s-exp convertion function does not exist.
3529      */
3530     gnutls_x509_privkey_t priv_key;
3531     gnutls_datum_t        key;
3532     long                  size;
3533     gint                  ret;
3534     guint                 bytes;
3535
3536     Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
3537
3538     /* init private key data*/
3539     gnutls_x509_privkey_init(&priv_key);
3540
3541     /* compute file size and load all file contents into a datum buffer*/
3542     if (fseek(fp, 0, SEEK_END) < 0) {
3543         ssl_debug_printf("ssl_load_key: can't fseek file\n");
3544         g_free(private_key);
3545         return NULL;
3546     }
3547     if ((size = ftell(fp)) < 0) {
3548         ssl_debug_printf("ssl_load_key: can't ftell file\n");
3549         g_free(private_key);
3550         return NULL;
3551     }
3552     if (fseek(fp, 0, SEEK_SET) < 0) {
3553         ssl_debug_printf("ssl_load_key: can't re-fseek file\n");
3554         g_free(private_key);
3555         return NULL;
3556     }
3557     key.data = (unsigned char *)g_malloc(size);
3558     key.size = (int)size;
3559     bytes = (guint) fread(key.data, 1, key.size, fp);
3560     if (bytes < key.size) {
3561         ssl_debug_printf("ssl_load_key: can't read from file %d bytes, got %d\n",
3562             key.size, bytes);
3563         g_free(private_key);
3564         g_free(key.data);
3565         return NULL;
3566     }
3567
3568     /* import PEM data*/
3569     if ((ret = gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) {
3570         ssl_debug_printf("ssl_load_key: can't import pem data: %s\n", gnutls_strerror(ret));
3571         g_free(private_key);
3572         g_free(key.data);
3573         return NULL;
3574     }
3575
3576     if (gnutls_x509_privkey_get_pk_algorithm(priv_key) != GNUTLS_PK_RSA) {
3577         ssl_debug_printf("ssl_load_key: private key public key algorithm isn't RSA\n");
3578         g_free(private_key);
3579         g_free(key.data);
3580         return NULL;
3581     }
3582
3583     g_free(key.data);
3584
3585     private_key->x509_pkey = priv_key;
3586     private_key->sexp_pkey = ssl_privkey_to_sexp(priv_key);
3587     if ( !private_key->sexp_pkey ) {
3588         g_free(private_key);
3589         return NULL;
3590     }
3591     return private_key;
3592 }
3593
3594 static const char *
3595 BAGTYPE(gnutls_pkcs12_bag_type_t x) {
3596     switch (x) {
3597         case GNUTLS_BAG_EMPTY:               return "Empty";
3598         case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY: return "PKCS#8 Encrypted key";
3599         case GNUTLS_BAG_PKCS8_KEY:           return "PKCS#8 Key";
3600         case GNUTLS_BAG_CERTIFICATE:         return "Certificate";
3601         case GNUTLS_BAG_CRL:                 return "CRL";
3602         case GNUTLS_BAG_ENCRYPTED:           return "Encrypted";
3603         case GNUTLS_BAG_UNKNOWN:             return "Unknown";
3604         default:                             return "<undefined>";
3605     }
3606 }
3607
3608 /**
3609  * Load a RSA private key from a PKCS#12 file.
3610  * @param fp the file that contains the key data.
3611  * @param cert_passwd password to decrypt the PKCS#12 file.
3612  * @param[out] err error message upon failure; NULL upon success.
3613  * @return a pointer to the loaded key on success; NULL upon failure.
3614  */
3615 static Ssl_private_key_t *
3616 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd, char** err) {
3617
3618     int                       i, j, ret;
3619     int                       rest;
3620     unsigned char            *p;
3621     gnutls_datum_t            data;
3622     gnutls_pkcs12_bag_t       bag = NULL;
3623     gnutls_pkcs12_bag_type_t  bag_type;
3624     size_t                    len, buf_len;
3625     static char               buf_name[256];
3626     static char               buf_email[128];
3627     unsigned char             buf_keyid[32];
3628     char                     *tmp_str;
3629
3630     gnutls_pkcs12_t       ssl_p12  = NULL;
3631     gnutls_x509_crt_t     ssl_cert = NULL;
3632     gnutls_x509_privkey_t ssl_pkey = NULL;
3633
3634     Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
3635     *err = NULL;
3636
3637     rest = 4096;
3638     data.data = (unsigned char *)g_malloc(rest);
3639     data.size = rest;
3640     p = data.data;
3641     while ((len = fread(p, 1, rest, fp)) > 0) {
3642         p += len;
3643         rest -= (int) len;
3644         if (!rest) {
3645             rest = 1024;
3646             data.data = (unsigned char *)g_realloc(data.data, data.size + rest);
3647             p = data.data + data.size;
3648             data.size += rest;
3649         }
3650     }
3651     data.size -= rest;
3652     ssl_debug_printf("%d bytes read\n", data.size);
3653     if (!feof(fp)) {
3654         *err = g_strdup("Error during certificate reading.");
3655         ssl_debug_printf("%s\n", *err);
3656         g_free(private_key);
3657         g_free(data.data);
3658         return 0;
3659     }
3660
3661     ret = gnutls_pkcs12_init(&ssl_p12);
3662     if (ret < 0) {
3663         *err = g_strdup_printf("gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
3664         ssl_debug_printf("%s\n", *err);
3665         g_free(private_key);
3666         g_free(data.data);
3667         return 0;
3668     }
3669
3670     /* load PKCS#12 in DER or PEM format */
3671     ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 0);
3672     if (ret < 0) {
3673         *err = g_strdup_printf("could not load PKCS#12 in DER format: %s", gnutls_strerror(ret));
3674         ssl_debug_printf("%s\n", *err);
3675         g_free(*err);
3676
3677         ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_PEM, 0);
3678         if (ret < 0) {
3679             *err = g_strdup_printf("could not load PKCS#12 in PEM format: %s", gnutls_strerror(ret));
3680             ssl_debug_printf("%s\n", *err);
3681         } else {
3682             *err = NULL;
3683         }
3684     }
3685     g_free(data.data);
3686     if (ret < 0) {
3687         g_free(private_key);
3688         return 0;
3689     }
3690
3691     ssl_debug_printf( "PKCS#12 imported\n");
3692
3693     for (i=0; ret==0; i++) {
3694
3695         if (bag) { gnutls_pkcs12_bag_deinit(bag); bag = NULL; }
3696
3697         ret = gnutls_pkcs12_bag_init(&bag);
3698         if (ret < 0) continue;
3699
3700         ret = gnutls_pkcs12_get_bag(ssl_p12, i, bag);
3701         if (ret < 0) continue;
3702
3703         for (j=0; ret==0 && j<gnutls_pkcs12_bag_get_count(bag); j++) {
3704
3705             bag_type = gnutls_pkcs12_bag_get_type(bag, j);
3706             if (bag_type >= GNUTLS_BAG_UNKNOWN) continue;
3707             ssl_debug_printf( "Bag %d/%d: %s\n", i, j, BAGTYPE(bag_type));
3708             if (bag_type == GNUTLS_BAG_ENCRYPTED) {
3709                 ret = gnutls_pkcs12_bag_decrypt(bag, cert_passwd);
3710                 if (ret == 0) {
3711                     bag_type = gnutls_pkcs12_bag_get_type(bag, j);
3712                     if (bag_type >= GNUTLS_BAG_UNKNOWN) continue;
3713                     ssl_debug_printf( "Bag %d/%d decrypted: %s\n", i, j, BAGTYPE(bag_type));
3714                 }
3715             }
3716
3717             ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
3718             if (ret < 0) continue;
3719
3720             switch (bag_type) {
3721
3722                 case GNUTLS_BAG_CERTIFICATE:
3723
3724                     ret = gnutls_x509_crt_init(&ssl_cert);
3725                     if (ret < 0) {
3726                         *err = g_strdup_printf("gnutls_x509_crt_init(&ssl_cert) - %s", gnutls_strerror(ret));
3727                         ssl_debug_printf("%s\n", *err);
3728                         g_free(private_key);
3729                         return 0;
3730                     }
3731
3732                     ret = gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER);
3733                     if (ret < 0) {
3734                         *err = g_strdup_printf("gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER) - %s", gnutls_strerror(ret));
3735                         ssl_debug_printf("%s\n", *err);
3736                         g_free(private_key);
3737                         return 0;
3738                     }
3739
3740                     buf_len = sizeof(buf_name);
3741                     ret = gnutls_x509_crt_get_dn_by_oid(ssl_cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, buf_name, &buf_len);
3742                     if (ret < 0) { g_strlcpy(buf_name, "<ERROR>", 256); }
3743                     buf_len = sizeof(buf_email);
3744                     ret = gnutls_x509_crt_get_dn_by_oid(ssl_cert, GNUTLS_OID_PKCS9_EMAIL, 0, 0, buf_email, &buf_len);
3745                     if (ret < 0) { g_strlcpy(buf_email, "<ERROR>", 128); }
3746
3747                     buf_len = sizeof(buf_keyid);
3748                     ret = gnutls_x509_crt_get_key_id(ssl_cert, 0, buf_keyid, &buf_len);
3749                     if (ret < 0) { g_strlcpy(buf_keyid, "<ERROR>", 32); }
3750
3751                     private_key->x509_cert = ssl_cert;
3752                     tmp_str = bytes_to_str(NULL, buf_keyid, (int) buf_len);
3753                     ssl_debug_printf( "Certificate imported: %s <%s>, KeyID %s\n", buf_name, buf_email, tmp_str);
3754                     wmem_free(NULL, tmp_str);
3755                     break;
3756
3757                 case GNUTLS_BAG_PKCS8_KEY:
3758                 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
3759
3760                     ret = gnutls_x509_privkey_init(&ssl_pkey);
3761                     if (ret < 0) {
3762                         *err = g_strdup_printf("gnutls_x509_privkey_init(&ssl_pkey) - %s", gnutls_strerror(ret));
3763                         ssl_debug_printf("%s\n", *err);
3764                         g_free(private_key);
3765                         return 0;
3766                     }
3767                     ret = gnutls_x509_privkey_import_pkcs8(ssl_pkey, &data, GNUTLS_X509_FMT_DER, cert_passwd,
3768                                                            (bag_type==GNUTLS_BAG_PKCS8_KEY) ? GNUTLS_PKCS_PLAIN : 0);
3769                     if (ret < 0) {
3770                         *err = g_strdup_printf("Can not decrypt private key - %s", gnutls_strerror(ret));
3771                         ssl_debug_printf("%s\n", *err);
3772                         g_free(private_key);