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