2 * ssl manipulation functions
3 * By Paolo Abeni <paolo.abeni@email.com>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include "packet-ssl-utils.h"
35 #include "packet-ssl.h"
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>
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" },
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" },
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" },
104 { 0x00001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
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" },
122 { 0x00002C, "TLS_PSK_WITH_NULL_SHA" },
123 { 0x00002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
124 { 0x00002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
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" },
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" },
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" },
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" },
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" },
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 */
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" },
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" },
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" },
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
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]
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
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" },
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" },
349 value_string_ext ssl_20_cipher_suites_ext = VALUE_STRING_EXT_INIT(ssl_20_cipher_suites);
352 const value_string ssl_extension_curves[] = {
378 { 0xFF01, "arbitrary_explicit_prime_curves" },
379 { 0xFF02, "arbitrary_explicit_char2_curves" },
383 const value_string ssl_curve_types[] = {
384 { 1, "explicit_prime" },
385 { 2, "explicit_char2" },
386 { 3, "named_curve" },
390 const value_string ssl_extension_ec_point_formats[] = {
391 { 0, "uncompressed" },
392 { 1, "ansiX962_compressed_prime" },
393 { 2, "ansiX962_compressed_char2" },
397 const value_string ssl_20_certificate_type[] = {
399 { 0x01, "X.509 Certificate" },
403 const value_string ssl_31_content_type[] = {
404 { 20, "Change Cipher Spec" },
407 { 23, "Application Data" },
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" },
423 /* XXX - would be used if we dissected the body of a Change Cipher Spec
425 const value_string ssl_31_change_cipher_spec[] = {
426 { 1, "Change Cipher Spec" },
431 const value_string ssl_31_alert_level[] = {
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" },
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" },
487 const value_string ssl_31_compression_method[] = {
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[] = {
499 { 1, "Diffie Hellman" },
503 const value_string ssl_31_signature_algorithm[] = {
511 const value_string ssl_31_client_certificate_type[] = {
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" },
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[] = {
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" },
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" },
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" },
595 { 0x002C, "TLS_PSK_WITH_NULL_SHA" },
596 { 0x002D, "TLS_DHE_PSK_WITH_NULL_SHA" },
597 { 0x002E, "TLS_RSA_PSK_WITH_NULL_SHA" },
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" },
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" },
627 /* 0x00,0x60-66 Reserved to avoid conflicts with widely deployed implementations */
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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 */
732 { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
733 /* 0x01-BF,* Unassigned */
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" },
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" },
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" },
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" },
801 0xC0,0x3C-FF 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]
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 */
817 value_string_ext ssl_31_ciphersuite_ext = VALUE_STRING_EXT_INIT(ssl_31_ciphersuite);
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" },
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" },
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"},
848 const value_string pct_cert_type[] = {
849 { PCT_CERT_NONE, "None" },
850 { PCT_CERT_X509, "X.509" },
851 { PCT_CERT_PKCS7, "PKCS #7" },
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" },
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" },
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" },
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 */
902 { 35, "SessionTicket TLS" }, /* RFC 4507 */
903 { 65281, "renegotiation_info" },
907 /* RFC 5246 7.4.1.4.1 */
908 const value_string tls_hash_algorithm[] = {
919 const value_string tls_signature_algorithm[] = {
928 const value_string tls_certificate_type[] = {
934 const value_string tls_cert_status_type[] = {
935 { SSL_HND_CERT_STATUS_TYPE_OCSP, "OCSP" },
939 /* we keep this internal to packet-ssl-utils, as there should be
940 no need to access it any other way.
942 This also allows us to hide the dependency on zlib.
944 struct _SslDecompress {
951 /* To assist in parsing client/server key exchange messages
952 0 indicates unknown */
953 gint ssl_get_keyex_alg(gint cipher)
1147 ssl_data_alloc(StringInfo* str, size_t len)
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)
1154 str->data_len = (guint) len;
1159 ssl_data_set(StringInfo* str, const guchar* data, guint len)
1161 memcpy(str->data, data, len);
1162 str->data_len = len;
1165 #if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
1167 static gint ver_major, ver_minor, ver_patch;
1169 /* hmac abstraction layer */
1170 #define SSL_HMAC gcry_md_hd_t
1173 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
1176 const char *err_str, *err_src;
1177 err = gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
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);
1184 gcry_md_setkey (*(md), key, len);
1188 ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
1190 gcry_md_write(*(md), data, len);
1193 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
1197 algo = gcry_md_get_algo (*(md));
1198 len = gcry_md_get_algo_dlen(algo);
1199 memcpy(data, gcry_md_read(*(md), algo), len);
1203 ssl_hmac_cleanup(SSL_HMAC* md)
1205 gcry_md_close(*(md));
1208 /* memory digest abstraction layer*/
1209 #define SSL_MD gcry_md_hd_t
1212 ssl_md_init(SSL_MD* md, gint algo)
1215 const char *err_str, *err_src;
1216 err = gcry_md_open(md,algo, 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);
1226 ssl_md_update(SSL_MD* md, guchar* data, gint len)
1228 gcry_md_write(*(md), data, len);
1231 ssl_md_final(SSL_MD* md, guchar* data, guint* datalen)
1235 algo = gcry_md_get_algo (*(md));
1236 len = gcry_md_get_algo_dlen (algo);
1237 memcpy(data, gcry_md_read(*(md), algo), len);
1241 ssl_md_cleanup(SSL_MD* md)
1243 gcry_md_close(*(md));
1246 /* md5 /sha abstraction layer */
1247 #define SSL_SHA_CTX gcry_md_hd_t
1248 #define SSL_MD5_CTX gcry_md_hd_t
1251 ssl_sha_init(SSL_SHA_CTX* md)
1253 gcry_md_open(md,GCRY_MD_SHA1, 0);
1256 ssl_sha_update(SSL_SHA_CTX* md, guchar* data, gint len)
1258 gcry_md_write(*(md), data, len);
1261 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
1263 memcpy(buf, gcry_md_read(*(md), GCRY_MD_SHA1),
1264 gcry_md_get_algo_dlen(GCRY_MD_SHA1));
1267 ssl_sha_cleanup(SSL_SHA_CTX* md)
1269 gcry_md_close(*(md));
1273 ssl_md5_init(SSL_MD5_CTX* md)
1275 return gcry_md_open(md,GCRY_MD_MD5, 0);
1278 ssl_md5_update(SSL_MD5_CTX* md, guchar* data, gint len)
1280 gcry_md_write(*(md), data, len);
1283 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
1285 memcpy(buf, gcry_md_read(*(md), GCRY_MD_MD5),
1286 gcry_md_get_algo_dlen(GCRY_MD_MD5));
1289 ssl_md5_cleanup(SSL_MD5_CTX* md)
1291 gcry_md_close(*(md));
1295 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
1300 /* gcry_cipher_hd_t c; */
1302 /*c=(gcry_cipher_hd_t)*cipher;*/
1304 ssl_debug_printf("--------------------------------------------------------------------");
1305 /*for(ivp=c->iv,i=0; i < iv_len; i++ )
1307 ssl_debug_printf("%d ",ivp[i]);
1311 ssl_debug_printf("--------------------------------------------------------------------");
1312 ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
1313 /*for(ivp=c->iv,i=0; i < iv_len; i++ )
1315 ssl_debug_printf("%d ",ivp[i]);
1319 ssl_debug_printf("--------------------------------------------------------------------");
1322 /* stream cipher abstraction layer*/
1324 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
1325 guchar* iv, gint mode)
1327 gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC};
1331 *(cipher) = (gcry_cipher_hd_t)-1;
1334 err = gcry_cipher_open(cipher, algo, gcry_modes[mode], 0);
1337 err = gcry_cipher_setkey(*(cipher), sk, gcry_cipher_get_algo_keylen (algo));
1340 err = gcry_cipher_setiv(*(cipher), iv, gcry_cipher_get_algo_blklen (algo));
1346 ssl_cipher_decrypt(gcry_cipher_hd_t *cipher, guchar * out, gint outl,
1347 const guchar * in, gint inl)
1349 if ((*cipher) == (gcry_cipher_hd_t)-1)
1352 memcpy(out, in, outl < inl ? outl : inl);
1355 return gcry_cipher_decrypt ( *(cipher), out, outl, in, inl);
1358 ssl_get_digest_by_name(const gchar*name)
1360 return gcry_md_map_name(name);
1363 ssl_get_cipher_by_name(const gchar* name)
1365 return gcry_cipher_map_name(name);
1369 ssl_cipher_cleanup(gcry_cipher_hd_t *cipher)
1371 if ((*cipher) != (gcry_cipher_hd_t)-1)
1372 gcry_cipher_close(*cipher);
1376 /* private key abstraction layer */
1378 ssl_get_key_len(SSL_PRIVATE_KEY* pk) {return gcry_pk_get_nbits (pk); }
1381 _gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
1382 gcry_mpi_t *skey, gint flags);
1384 #define PUBKEY_FLAG_NO_BLINDING (1 << 0)
1387 ssl_private_key_to_str(SSL_PRIVATE_KEY* pk)
1389 const gchar *str="NULL";
1393 if (!pk) return str;
1395 n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, NULL, 0);
1397 n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, buf, n);
1399 #else /* SSL_FAST */
1400 str = "TO DO: dump mpi gcry_mpi_print()";
1401 #endif /* SSL_FAST */
1406 /* decrypt data with private key. Store decrypted data directly into input
1409 ssl_private_decrypt(guint len, guchar* encr_data, SSL_PRIVATE_KEY* pk)
1413 gcry_sexp_t s_data, s_plain;
1414 gcry_mpi_t encr_mpi;
1416 guchar* decr_data_ptr;
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);
1425 ssl_debug_printf("pcry_private_decrypt: can't convert encr_data to mpi (size %d):%s\n",
1426 len, gcry_strerror(rc));
1430 /*ssl_debug_printf("pcry_private_decrypt: pk=%s\n", ssl_private_key_to_str(pk));*/
1433 /* put the data into a simple list */
1434 rc = gcry_sexp_build(&s_data, NULL, "(enc-val(rsa(a%m)))", encr_mpi);
1436 ssl_debug_printf("pcry_private_decrypt: can't build encr_sexp:%s \n",
1441 /* pass it to libgcrypt */
1442 rc = gcry_pk_decrypt(&s_plain, s_data, pk);
1443 gcry_sexp_release(s_data);
1446 ssl_debug_printf("pcry_private_decrypt: can't decrypt key:%s\n",
1451 /* convert plain text sexp to mpi format */
1452 text = gcry_sexp_nth_mpi(s_plain, 0, 0);
1454 /* compute size requested for plaintext buffer */
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",
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",
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,
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);
1481 /* strip the padding*/
1483 for (i = 1; i < decr_len; i++) {
1484 if (decr_data_ptr[i] == 0) {
1490 ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %" G_GSIZE_MODIFIER "u\n",
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);
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);
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",
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,
1513 ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %d):%s\n",
1514 decr_len, gcry_strerror(rc));
1518 /* strip the padding*/
1520 for (i = 1; i < decr_len; i++) {
1521 if (decr_data_ptr[i] == 0) {
1527 ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %d\n",
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);
1532 #endif /* SSL_FAST */
1533 gcry_mpi_release(text);
1534 return (int) decr_len;
1537 /* stringinfo interface */
1539 ssl_data_realloc(StringInfo* str, guint len)
1541 str->data = g_realloc(str->data, len);
1544 str->data_len = len;
1549 ssl_data_copy(StringInfo* dst, StringInfo* src)
1551 if (dst->data_len < src->data_len) {
1552 if (ssl_data_realloc(dst, src->data_len))
1555 memcpy(dst->data, src->data, src->data_len);
1556 dst->data_len = src->data_len;
1560 static const gchar *digests[]={
1567 static const gchar *ciphers[]={
1570 "ARCFOUR", /* gnutls does not support rc4, but this should be 100% compatible*/
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}
1653 #define MAX_BLOCK_SIZE 16
1654 #define MAX_KEY_SIZE 32
1657 ssl_find_cipher(int num,SslCipherSuite* cs)
1661 for(c=cipher_suites;c->number!=-1;c++){
1672 tls_hash(StringInfo* secret, StringInfo* seed, gint md, StringInfo* out)
1678 guint8 _A[48],tmp[48];
1685 ssl_print_string("tls_hash: hash secret", secret);
1686 ssl_print_string("tls_hash: hash seed", seed);
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);
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);
1703 tocpy=MIN(left,tmp_l);
1704 memcpy(ptr,tmp,tocpy);
1709 ssl_print_string("hash out", out);
1714 tls_prf(StringInfo* secret, const gchar *usage,
1715 StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
1717 StringInfo seed, sha_out, md5_out;
1723 usage_len = strlen(usage);
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");
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");
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));
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;
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);
1751 if (ssl_data_alloc(&s2, s_l) < 0) {
1752 ssl_debug_printf("tls_prf: can't allocate secret(2) %d\n", s_l);
1756 memcpy(s1.data,secret->data,s_l);
1757 memcpy(s2.data,secret->data + (secret->data_len - s_l),s_l);
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)
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)
1766 for(i=0;i<out->data_len;i++)
1767 out->data[i]=md5_out.data[i] ^ sha_out.data[i];
1770 ssl_print_string("PRF out",out);
1778 g_free(md5_out.data);
1780 g_free(sha_out.data);
1785 tls12_prf(gint md, StringInfo* secret, const gchar* usage, StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
1787 StringInfo label_seed;
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");
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);
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);
1804 ssl_print_string("PRF out", out);
1809 ssl3_generate_export_iv(StringInfo* r1,
1810 StringInfo* r2, StringInfo* out)
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);
1821 memcpy(out->data,tmp,out->data_len);
1822 ssl_print_string("export iv", out);
1828 ssl3_prf(StringInfo* secret, const gchar* usage,
1830 StringInfo* r2,StringInfo* out)
1834 StringInfo *rnd1,*rnd2;
1841 for(off=0;off<out->data_len;off+=16){
1846 ssl_debug_printf("ssl3_prf: sha1_hash(%d)\n",i);
1847 /* A, BB, CCC, ... */
1853 ssl_sha_update(&sha,buf,i);
1854 ssl_sha_update(&sha,secret->data,secret->data_len);
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);
1861 ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
1862 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
1865 ssl_sha_final(buf,&sha);
1866 ssl_sha_cleanup(&sha);
1868 ssl_debug_printf("ssl3_prf: md5_hash(%d) datalen %d\n",i,
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);
1876 tocpy=MIN(out->data_len-off,16);
1877 memcpy(out->data+off,outbuf,tocpy);
1883 static gint prf(SslDecryptSession* ssl,StringInfo* secret,gchar* usage,StringInfo* rnd1,StringInfo* rnd2,StringInfo* out)
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);
1892 if (ssl->cipher_suite.dig == DIG_SHA384){
1893 ret = tls12_prf(GCRY_MD_SHA384, secret, usage, rnd1, rnd2, out);
1895 ret = tls12_prf(GCRY_MD_SHA256, secret, usage, rnd1, rnd2, out);
1902 ssl_create_flow(void)
1906 flow = se_alloc(sizeof(SslFlow));
1909 flow->multisegment_pdus = se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "ssl_multisegment_pdus");
1914 /* memory allocation functions for zlib initialization */
1915 static void* ssl_zalloc(void* opaque _U_, unsigned int no, unsigned int size)
1917 return g_malloc0(no*size);
1919 static void ssl_zfree(void* opaque _U_, void* addr)
1925 static SslDecompress*
1926 ssl_create_decompressor(gint compression)
1928 SslDecompress *decomp;
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) {
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));
1949 ssl_debug_printf("ssl_create_decompressor: inflateInit_() failed - %d\n", err);
1955 ssl_debug_printf("ssl_create_decompressor: unsupported compression method %d\n", decomp->compression);
1962 ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
1963 guint8 *mk, guint8 *sk, guint8 *iv)
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]);
1975 ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", "NULL");
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)]);
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);
1991 dec->decomp = ssl_create_decompressor(compression);
1992 dec->flow = ssl_create_flow();
1995 ssl_cipher_cleanup(&dec->evp);
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);
2003 ssl_debug_printf("decoder initialized (digest len %d)\n", cipher_suite->dig_len);
2008 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
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];
2014 guint8 *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
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);
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");
2038 ssl_print_string("master secret",&ssl_session->master_secret);
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;
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;
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,
2057 ssl_debug_printf("ssl_generate_keyring_material can't generate key_block\n");
2060 ssl_print_string("key expansion", &key_block);
2063 c_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
2064 s_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
2066 c_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
2067 s_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
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;
2074 if(ssl_session->cipher_suite.export){
2075 StringInfo iv_c,iv_s;
2076 StringInfo key_c,key_s;
2079 if(ssl_session->cipher_suite.block>1){
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);
2090 iv_c.data_len = ssl_session->cipher_suite.block;
2092 iv_s.data_len = ssl_session->cipher_suite.block;
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");
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");
2109 guint8 _iv_block[MAX_BLOCK_SIZE * 2];
2110 StringInfo iv_block;
2111 StringInfo key_null;
2114 key_null.data = &_key_null;
2115 key_null.data_len = 0;
2117 iv_block.data = _iv_block;
2118 iv_block.data_len = ssl_session->cipher_suite.block*2;
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");
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);
2137 if (ssl_session->version_netorder==SSLV3_VERSION){
2140 ssl_debug_printf("ssl_generate_keyring_material MD5(client_random)\n");
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);
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);
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);
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");
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");
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);
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);
2204 ssl_print_data("Client Write IV",c_iv,8);
2205 ssl_print_data("Server Write IV",s_iv,8);
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");
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");
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;
2229 g_free(key_block.data);
2234 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
2236 ssl_debug_printf("ssl_change_cipher %s\n", (server)?"SERVER":"CLIENT");
2238 ssl_session->server = ssl_session->server_new;
2239 ssl_session->server_new = NULL;
2241 ssl_session->client = ssl_session->client_new;
2242 ssl_session->client_new = NULL;
2247 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
2248 StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk)
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",
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);
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);
2269 ssl_debug_printf("ssl_decrypt_pre_master_secret wrong "
2270 "pre_master_secret length (%d, expected %d)\n", i, 48);
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);
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;
2287 /* convert network byte order 32 byte number to right-aligned host byte order *
2289 static gint fmt_seq(guint32 num, guint8* buf)
2294 netnum=g_htonl(num);
2295 memcpy(buf+4,&netnum,4);
2301 tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
2302 guint32 datalen, guint8* mac)
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);
2314 if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
2317 /* hash sequence number */
2318 fmt_seq(decoder->seq,buf);
2322 ssl_hmac_update(&hm,buf,8);
2324 /* hash content type */
2326 ssl_hmac_update(&hm,buf,1);
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);
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);
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))
2351 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
2352 guint32 datalen, guint8* mac)
2357 guint8 buf[64],dgst[20];
2361 pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
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)
2368 /* do hash computation on data && padding */
2369 ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
2372 memset(buf,0x36,pad_ct);
2373 ssl_md_update(&mc,buf,pad_ct);
2375 /* hash sequence number */
2376 fmt_seq(decoder->seq,buf);
2378 ssl_md_update(&mc,buf,8);
2380 /* hash content type */
2382 ssl_md_update(&mc,buf,1);
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);
2391 /* get partial digest */
2392 ssl_md_final(&mc,dgst,&len);
2393 ssl_md_cleanup(&mc);
2395 ssl_md_init(&mc,md);
2398 ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
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);
2405 ssl_md_final(&mc,dgst,&len);
2406 ssl_md_cleanup(&mc);
2408 if(memcmp(mac,dgst,len))
2415 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
2416 guint32 datalen, guint8* mac)
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);
2428 if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
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;
2436 ssl_hmac_update(&hm,buf,8);
2438 /* hash content type */
2440 ssl_hmac_update(&hm,buf,1);
2442 /* hash version,data length and data */
2443 temp = g_htons(ver);
2444 memcpy(buf, &temp, 2);
2445 ssl_hmac_update(&hm,buf,2);
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))
2463 ssl_decompress_record(SslDecompress* decomp, const guchar* in, guint inl, StringInfo* out_str, guint* outl)
2467 switch (decomp->compression) {
2468 case 1: /* DEFLATE */
2470 if (out_str->data_len < 16384) { /* maximal plain length */
2471 ssl_data_realloc(out_str, 16384);
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;
2478 err = inflate(&decomp->istream, Z_SYNC_FLUSH);
2480 ssl_debug_printf("ssl_decompress_record: inflate() failed - %d\n", err);
2483 *outl = out_str->data_len - decomp->istream.avail_out;
2486 ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2493 ssl_decompress_record(SslDecompress* decomp _U_, const guchar* in _U_, guint inl _U_, StringInfo* out_str _U_, guint* outl _U_)
2495 ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
2501 ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
2502 const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl)
2504 guint pad, worklen, uncomplen;
2507 ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
2508 ssl_print_data("Ciphertext",in, inl);
2510 /* ensure we have enough storage space for decrypted data */
2511 if (inl > out_str->data_len)
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);
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));
2525 ssl_print_data("Plaintext", out_str->data, inl);
2528 /* Now strip off the padding*/
2529 if(decoder->cipher_suite->block!=1) {
2530 pad=out_str->data[inl-1];
2532 ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
2537 if (decoder->cipher_suite->dig_len > (gint)worklen)
2539 ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
2542 worklen-=decoder->cipher_suite->dig_len;
2543 mac = out_str->data + worklen;
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);
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);
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");
2565 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
2570 ssl_debug_printf("ssl_decrypt_record: mac ok\n");
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");
2579 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
2584 ssl_debug_printf("ssl_decrypt_record: mac ok\n");
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");
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");
2596 else if(ssl_ignore_mac_failed) {
2597 ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
2600 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
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");
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);
2624 ssl_get_version(gint* major, gint* minor, gint* patch)
2633 ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
2635 gnutls_datum_t rsa_datum[RSA_PARS]; /* m, e, d, p, q, u */
2637 gcry_sexp_t rsa_priv_key = NULL;
2638 gint major, minor, patch;
2639 gint i, p_idx, q_idx;
2642 unsigned char buf_keyid[32];
2645 gcry_mpi_t* rsa_params = g_malloc(sizeof(gcry_mpi_t)*RSA_PARS);
2647 gcry_mpi_t rsa_params[RSA_PARS];
2650 buf_len = sizeof(buf_keyid);
2651 ret = gnutls_x509_privkey_get_key_id(priv_key, 0, buf_keyid, &buf_len);
2653 ssl_debug_printf( "gnutls_x509_privkey_get_key_id(ssl_pkey, 0, buf_keyid, &buf_len) - %s\n", gnutls_strerror(ret));
2655 ssl_debug_printf( "Private key imported: KeyID %s\n", bytes_to_str_punct(buf_keyid, (int) buf_len, ':'));
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
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;
2669 /* RSA get parameter */
2670 if (gnutls_x509_privkey_export_rsa_raw(priv_key,
2676 &rsa_datum[5]) != 0) {
2677 ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
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);
2695 ssl_get_version(&major, &minor, &patch);
2697 /* certain versions of gnutls require swap of rsa params 'p' and 'q' */
2698 if ((major <= 1) && (minor <= 0) && (patch <=13))
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;
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");
2721 for (i=0; i< 6; i++)
2722 gcry_mpi_release(rsa_params[i]);
2723 return rsa_priv_key;
2729 ssl_load_key(FILE* fp)
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.
2735 gnutls_x509_privkey_t priv_key;
2740 Ssl_private_key_t *private_key = g_malloc0(sizeof(Ssl_private_key_t));
2742 /* init private key data*/
2743 gnutls_x509_privkey_init(&priv_key);
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);
2751 if ((size = ftell(fp)) < 0) {
2752 ssl_debug_printf("ssl_load_key: can't ftell file\n");
2753 g_free(private_key);
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);
2761 key.data = g_malloc(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",
2767 g_free(private_key);
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);
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);
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);
2799 BAGTYPE(gnutls_pkcs12_bag_type_t 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>";
2813 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
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];
2826 gnutls_pkcs12_t ssl_p12 = NULL;
2827 gnutls_x509_crt_t ssl_cert = NULL;
2828 gnutls_x509_privkey_t ssl_pkey = NULL;
2830 Ssl_private_key_t *private_key = g_malloc0(sizeof(Ssl_private_key_t));
2833 data.data = g_malloc(rest);
2836 while ((len = fread(p, 1, rest, fp)) > 0) {
2841 data.data = g_realloc(data.data, data.size + rest);
2842 p = data.data + data.size;
2847 ssl_debug_printf("%d bytes read\n", data.size);
2849 ssl_debug_printf( "Error during certificate reading.\n");
2850 g_free(private_key);
2854 ret = gnutls_pkcs12_init(&ssl_p12);
2856 ssl_debug_printf("gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
2857 g_free(private_key);
2860 ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 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);
2868 ssl_debug_printf( "PKCS#12 imported\n");
2870 for (i=0; ret==0; i++) {
2872 if (bag) { gnutls_pkcs12_bag_deinit(bag); bag = NULL; }
2874 ret = gnutls_pkcs12_bag_init(&bag);
2875 if (ret < 0) continue;
2877 ret = gnutls_pkcs12_get_bag(ssl_p12, i, bag);
2878 if (ret < 0) continue;
2880 for (j=0; ret==0 && j<gnutls_pkcs12_bag_get_count(bag); j++) {
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);
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));
2895 ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
2896 if (ret < 0) continue;
2900 case GNUTLS_BAG_CERTIFICATE:
2902 ret = gnutls_x509_crt_init(&ssl_cert);
2904 ssl_debug_printf( "gnutls_x509_crt_init(&ssl_cert) - %s\n", gnutls_strerror(ret));
2905 g_free(private_key);
2909 ret = gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER);
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);
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); }
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); }
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));
2931 case GNUTLS_BAG_PKCS8_KEY:
2932 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
2934 ret = gnutls_x509_privkey_init(&ssl_pkey);
2936 ssl_debug_printf( "gnutls_x509_privkey_init(&ssl_pkey) - %s\n", gnutls_strerror(ret));
2937 g_free(private_key);
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);
2943 ssl_debug_printf( "Can not decrypt private key - %s\n", gnutls_strerror(ret));
2944 g_free(private_key);
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);
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);
2971 void ssl_free_key(Ssl_private_key_t* key)
2975 for (i=0; i< 6; i++)
2976 gcry_mpi_release(key->sexp_pkey[i]);
2978 gcry_sexp_release(key->sexp_pkey);
2981 if (!key->x509_cert)
2982 gnutls_x509_crt_deinit (key->x509_cert);
2984 if (!key->x509_pkey)
2985 gnutls_x509_privkey_deinit(key->x509_pkey);
2987 g_free((Ssl_private_key_t*)key);
2991 ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree* associations, packet_info *pinfo) {
2993 char ip_addr_any[] = {0,0,0,0};
2995 Ssl_private_key_t * private_key;
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;
3002 dummy.addr = pinfo->dst;
3003 dummy.port = port = pinfo->destport;
3005 ssl_debug_printf("ssl_find_private_key server %s:%u\n",
3006 ep_address_to_str(&dummy.addr),dummy.port);
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*/
3014 ssl_session->private_key = 0;
3015 private_key = g_hash_table_lookup(key_hash, &dummy);
3018 ssl_debug_printf("ssl_find_private_key can't find private key for this server! Try it again with universal port 0\n");
3021 private_key = g_hash_table_lookup(key_hash, &dummy);
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");
3027 dummy.addr.type = AT_IPv4;
3029 dummy.addr.data = ip_addr_any;
3032 private_key = g_hash_table_lookup(key_hash, &dummy);
3036 ssl_debug_printf("ssl_find_private_key can't find any private key!\n");
3038 ssl_session->private_key = private_key->sexp_pkey;
3047 const gchar* str = gnutls_check_version(NULL);
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);
3056 #else /* defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT) */
3057 /* no libgnutl: dummy operation to keep interface consistent*/
3064 ssl_load_key(FILE* fp)
3066 ssl_debug_printf("ssl_load_key: impossible without gnutls. fp %p\n",fp);
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);
3077 ssl_free_key(Ssl_private_key_t* key _U_)
3082 ssl_find_private_key(SslDecryptSession *ssl_session _U_, GHashTable *key_hash _U_, GTree* associations _U_, packet_info *pinfo _U_)
3088 ssl_find_cipher(int num,SslCipherSuite* cs)
3090 ssl_debug_printf("ssl_find_cipher: dummy without gnutls. num %d cs %p\n",
3095 ssl_generate_keyring_material(SslDecryptSession*ssl)
3097 ssl_debug_printf("ssl_generate_keyring_material: impossible without gnutls. ssl %p\n",
3102 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
3104 ssl_debug_printf("ssl_change_cipher %s: makes no sense without gnutls. ssl %p\n",
3105 (server)?"SERVER":"CLIENT", ssl_session);
3109 ssl_decrypt_pre_master_secret(SslDecryptSession* ssl_session,
3110 StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk)
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);
3119 ssl_decrypt_record(SslDecryptSession*ssl, SslDecoder* decoder, gint ct,
3120 const guchar* in, guint inl, StringInfo* comp_str _U_, StringInfo* out, guint* outl)
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);
3129 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher _U_, guchar* iv _U_, gint iv_len _U_)
3131 ssl_debug_printf("ssl_cipher_setiv: impossible without gnutls.\n");
3135 #endif /* HAVE_LIBGNUTLS */
3137 /* get ssl data for this session. if no ssl data is found allocate a new one*/
3139 ssl_session_init(SslDecryptSession* ssl_session)
3141 ssl_debug_printf("ssl_session_init: initializing ptr %p size %" G_GSIZE_MODIFIER "u\n",
3142 (void *)ssl_session, sizeof(SslDecryptSession));
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;
3161 ssl_set_server(SslDecryptSession* ssl, address *addr, port_type ptype, guint32 port)
3163 SE_COPY_ADDRESS(&ssl->srv_addr, addr);
3164 ssl->srv_ptype = ptype;
3165 ssl->srv_port = port;
3168 /* Hash Functions for TLS/DTLS sessions table and private keys table*/
3170 ssl_equal (gconstpointer v, gconstpointer v2)
3172 const StringInfo *val1;
3173 const StringInfo *val2;
3174 val1 = (const StringInfo *)v;
3175 val2 = (const StringInfo *)v2;
3177 if (val1->data_len == val2->data_len &&
3178 !memcmp(val1->data, val2->data, val2->data_len)) {
3185 ssl_hash (gconstpointer v)
3188 const StringInfo* id;
3191 id = (const StringInfo*) v;
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.
3199 cur = (const guint*)(void*) id->data;
3201 for (l=4; (l < id->data_len); l+=4, cur++)
3202 hash = hash ^ (*cur);
3208 ssl_private_key_equal (gconstpointer v, gconstpointer v2)
3210 const SslService *val1;
3211 const SslService *val2;
3212 val1 = (const SslService *)v;
3213 val2 = (const SslService *)v2;
3215 if ((val1->port == val2->port) &&
3216 ! CMP_ADDRESS(&val1->addr, &val2->addr)) {
3223 ssl_private_key_hash (gconstpointer v)
3225 const SslService *key;
3226 guint l, hash, len ;
3228 key = (const SslService *)v;
3230 len = key->addr.len;
3231 cur = (const guint*) key->addr.data;
3233 for (l=4; (l<len); l+=4, cur++)
3234 hash = hash ^ (*cur);
3239 /* private key table entries have a scope 'larger' then packet capture,
3240 * so we can't relay on se_alloc** function */
3242 ssl_private_key_free(gpointer id, gpointer key, gpointer dummy _U_)
3246 ssl_free_key((Ssl_private_key_t*) key);
3250 /* handling of association between tls/dtls ports and clear text protocol */
3252 ssl_association_add(GTree* associations, dissector_handle_t handle, guint port, const gchar *protocol, gboolean tcp, gboolean from_key_list)
3255 SslAssociation* assoc;
3256 assoc = g_malloc(sizeof(SslAssociation));
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;
3264 ssl_debug_printf("association_add %s port %d protocol %s handle %p\n",
3265 (assoc->tcp)?"TCP":"UDP", port, protocol, (void *)(assoc->handle));
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");
3273 if(!assoc->handle) {
3274 fprintf(stderr, "association_add() could not find handle for protocol:%s\n",protocol);
3278 dissector_add_uint("tcp.port", port, handle);
3280 dissector_add_uint("udp.port", port, handle);
3282 g_tree_insert(associations, assoc, assoc);
3287 ssl_association_remove(GTree* associations, SslAssociation *assoc)
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));
3292 dissector_delete_uint((assoc->tcp)?"tcp.port":"udp.port", assoc->ssl_port, assoc->handle);
3294 g_free(assoc->info);
3296 g_tree_remove(associations, assoc);
3301 ssl_association_cmp(gconstpointer a, gconstpointer b)
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;
3309 ssl_association_find(GTree * associations, guint port, gboolean tcp)
3311 register SslAssociation* ret;
3312 SslAssociation assoc_tmp;
3314 assoc_tmp.tcp = tcp;
3315 assoc_tmp.ssl_port = port;
3316 ret = g_tree_lookup(associations, &assoc_tmp);
3318 ssl_debug_printf("association_find: %s port %d found %p\n", (tcp)?"TCP":"UDP", port, (void *)ret);
3323 ssl_assoc_from_key_list(gpointer key _U_, gpointer data, gpointer user_data)
3325 if (((SslAssociation*)data)->from_key_list)
3326 ep_stack_push((ep_stack_t)user_data, data);
3331 ssl_packet_from_server(SslDecryptSession* ssl, GTree* associations, packet_info *pinfo)
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);
3337 ret = ssl_association_find(associations, pinfo->srcport, pinfo->ptype == PT_TCP) != 0;
3340 ssl_debug_printf("packet_from_server: is from server - %s\n", (ret)?"TRUE":"FALSE");
3344 /* add to packet data a copy of the specified real data */
3346 ssl_add_record_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint record_id)
3352 pi = p_get_proto_data(pinfo->fd, proto);
3355 pi = se_alloc0(sizeof(SslPacketInfo));
3356 p_add_proto_data(pinfo->fd, proto, pi);
3359 real_data = se_alloc(data_len);
3360 memcpy(real_data, data, data_len);
3362 rec = se_alloc(sizeof(SslRecordInfo));
3363 rec->id = record_id;
3364 rec->real_data = real_data;
3365 rec->data_len = data_len;
3367 /* head insertion */
3368 rec->next= pi->handshake_data;
3369 pi->handshake_data = rec;
3372 /* search in packet data for the specified id; return a newly created tvb for the associated data */
3374 ssl_get_record_info(tvbuff_t *parent_tvb, int proto, packet_info *pinfo, gint record_id)
3378 pi = p_get_proto_data(pinfo->fd, proto);
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);
3392 ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint key, SslFlow *flow)
3394 SslDataInfo *rec, **prec;
3397 pi = p_get_proto_data(pinfo->fd, proto);
3400 pi = se_alloc0(sizeof(SslPacketInfo));
3401 p_add_proto_data(pinfo->fd, proto,pi);
3404 rec = se_alloc(sizeof(SslDataInfo)+data_len);
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;
3411 rec->seq = flow->byte_seq;
3412 rec->nxtseq = flow->byte_seq + data_len;
3414 flow->byte_seq += data_len;
3419 prec = &pi->appl_data;
3420 while (*prec) prec = &(*prec)->next;
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);
3428 ssl_get_data_info(int proto, packet_info *pinfo, gint key)
3432 pi = p_get_proto_data(pinfo->fd, proto);
3434 if (!pi) return NULL;
3436 rec = pi->appl_data;
3438 if (rec->key == key) return rec;
3445 /* initialize/reset per capture state data (ssl sessions cache) */
3447 ssl_common_init(GHashTable **session_hash, StringInfo *decrypted_data, StringInfo *compressed_data)
3450 g_hash_table_destroy(*session_hash);
3451 *session_hash = g_hash_table_new(ssl_hash, ssl_equal);
3453 g_free(decrypted_data->data);
3454 ssl_data_alloc(decrypted_data, 32);
3456 g_free(compressed_data->data);
3457 ssl_data_alloc(compressed_data, 32);
3460 /* parse ssl related preferences (private keys and ports association strings) */
3462 ssl_parse_key_list(const ssldecrypt_assoc_t * uats, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp)
3464 SslService* service;
3465 Ssl_private_key_t * private_key, *tmp_private_key;
3467 guint32 addr_data[4];
3469 address_type addr_type[2] = { AT_IPv4, AT_IPv6 };
3471 /* try to load keys file first */
3472 fp = ws_fopen(uats->keyfile, "rb");
3474 fprintf(stderr, "Can't open file %s\n",uats->keyfile);
3478 for (at = 0; at < 2; at++) {
3479 memset(addr_data, 0, sizeof(addr_data));
3482 /* any: IPv4 or IPv6 wildcard */
3483 /* anyipv4: IPv4 wildcard */
3484 /* anyipv6: IPv6 wildcard */
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])) {
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)) {
3502 if ((gint)strlen(uats->password) == 0) {
3503 private_key = ssl_load_key(fp);
3505 private_key = ssl_load_pkcs12(fp, uats->password);
3509 fprintf(stderr,"Can't load private key from %s\n", uats->keyfile);
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);
3520 if(strcmp(uats->port,"start_tls")==0) {
3523 service->port = atoi(uats->port);
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);
3530 ssl_debug_printf("ssl_init private key file %s successfully loaded.\n", uats->keyfile);
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);
3539 g_hash_table_insert(key_hash, service, private_key);
3541 ssl_association_add(associations, handle, service->port, uats->protocol, tcp, TRUE);
3547 /* store master secret into session data cache */
3549 ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash)
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));
3557 master_secret->data = ((guchar*)master_secret+sizeof(StringInfo));
3559 /* ssl_hash() depends on session_id->data being aligned for guint access
3560 * so be careful in changing how it is allocated.
3562 session_id->data = ((guchar*)session_id+sizeof(StringInfo));
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);
3572 ssl_restore_session(SslDecryptSession* ssl, GHashTable *session_hash)
3575 ms = g_hash_table_lookup(session_hash, &ssl->session_id);
3578 ssl_debug_printf("ssl_restore_session can't find stored session\n");
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");
3587 ssl_is_valid_content_type(guint8 type)
3589 if (type >= 0x14 && type <= 0x17)
3598 from_hex_char(gchar c) {
3599 if (c >= '0' && c <= '9')
3601 if (c >= 'A' && c <= 'F')
3602 return c - 'A' + 10;
3603 if (c >= 'a' && c <= 'f')
3604 return c - 'a' + 10;
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 */
3617 ssl_debug_printf("trying to use SSL keylog in %s\n", ssl_keylog_filename);
3619 ssl_keylog = ws_fopen(ssl_keylog_filename, "r");
3621 ssl_debug_printf("failed to open SSL keylog\n");
3625 /* The format of the file is a series of records with one of the following formats:
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)
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)
3637 char buf[512], *line;
3639 unsigned int offset;
3641 line = fgets(buf, sizeof(buf), ssl_keylog);
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;
3652 ssl_debug_printf(" checking keylog line: %s\n", line);
3654 if ( memcmp(line, "RSA ", 4) != 0) {
3655 ssl_debug_printf(" rejecting line due to bad format\n");
3661 if ( ssl_session->session_id.data_len>0 && memcmp(line+offset,"Session-ID:",11) == 0 ) {
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)) {
3672 ssl_debug_printf(" line does not match SSL-ID\n");
3676 offset += 2*ssl_session->session_id.data_len;
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)) {
3689 ssl_debug_printf(" line does not match encrypted pre-master secret\n");
3696 ssl_debug_printf(" rejecting line due to bad format\n");
3701 /* This record seems to match. */
3702 if (memcmp(line+offset, "Master-Key:", 11) == 0) {
3703 /* Key is a MasterSecret */
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) {
3713 ssl_session->master_secret.data[i] = a << 4 | b;
3717 ssl_debug_printf(" line contains non-hex chars in master secret\n");
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");
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) {
3738 ssl_session->pre_master_secret.data[i] = a << 4 | b;
3742 ssl_debug_printf(" line contains non-hex chars in pre-master secret\n");
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");
3759 #ifdef SSL_DECRYPT_DEBUG
3761 static FILE* ssl_debug_file=NULL;
3764 ssl_set_debug(char* name)
3766 static gint debug_file_must_be_closed;
3768 debug_file_must_be_closed = 0;
3769 use_stderr = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
3771 if (debug_file_must_be_closed)
3772 fclose(ssl_debug_file);
3774 ssl_debug_file = stderr;
3775 else if (!name || (strcmp(name, "") ==0))
3776 ssl_debug_file = NULL;
3778 ssl_debug_file = ws_fopen(name, "w");
3779 if (!use_stderr && ssl_debug_file)
3780 debug_file_must_be_closed = 1;
3784 ssl_debug_flush(void)
3787 fflush(ssl_debug_file);
3791 ssl_debug_printf(const gchar* fmt, ...)
3797 if (!ssl_debug_file)
3801 ret += vfprintf(ssl_debug_file, fmt, ap);
3806 ssl_print_text_data(const gchar* name, const guchar* data, size_t len)
3809 if (!ssl_debug_file)
3811 fprintf(ssl_debug_file,"%s: ",name);
3812 for (i=0; i< len; i++) {
3813 fprintf(ssl_debug_file,"%c",data[i]);
3815 fprintf(ssl_debug_file,"\n");
3819 ssl_print_data(const gchar* name, const guchar* data, size_t len)
3822 if (!ssl_debug_file)
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);
3830 fprintf(ssl_debug_file,"\n");
3834 ssl_print_string(const gchar* name, const StringInfo* data)
3836 ssl_print_data(name, data->data, data->data_len);
3838 #endif /* SSL_DECRYPT_DEBUG */
3840 /* checks for SSL and DTLS UAT key list fields */
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)
3845 if (!p || strlen(p) == 0u) {
3846 *err = ep_strdup_printf("No IP address given.");
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)
3857 if (!p || strlen(p) == 0u) {
3858 *err = ep_strdup_printf("No Port given.");
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.");
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)
3877 if (!p || strlen(p) == 0u) {
3878 *err = ep_strdup_printf("No protocol given.");
3882 if (!find_dissector(p)) {
3883 *err = ep_strdup_printf("Could not find dissector for: '%s'", p);
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)
3896 if (!p || strlen(p) == 0u) {
3897 *err = ep_strdup_printf("No filename given.");
3900 if (ws_stat64(p, &st) != 0) {
3901 *err = ep_strdup_printf("File '%s' does not exist or access is denied.", p);
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)
3913 ssldecrypt_assoc_t* f = r;
3916 if (p && strlen(p) > 0u) {
3917 fp = ws_fopen(f->keyfile, "rb");
3919 if (!ssl_load_pkcs12(fp, p)) {
3921 *err = ep_strdup_printf("Invalid. Password is necessary only if you use PKCS#12 key file.");
3926 *err = ep_strdup_printf("Leave this field blank if the keyfile is not PKCS#12.");
3936 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3941 * indent-tabs-mode: nil
3944 * vi: set shiftwidth=4 tabstop=8 expandtab:
3945 * :indentSize=4:tabSize=8:noTabs=true: