5 * ssl manipulation functions
6 * By Paolo Abeni <paolo.abeni@email.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include "packet-ssl-utils.h"
35 #include <epan/emem.h>
41 const value_string ssl_20_msg_types[] = {
42 { SSL2_HND_ERROR, "Error" },
43 { SSL2_HND_CLIENT_HELLO, "Client Hello" },
44 { SSL2_HND_CLIENT_MASTER_KEY, "Client Master Key" },
45 { SSL2_HND_CLIENT_FINISHED, "Client Finished" },
46 { SSL2_HND_SERVER_HELLO, "Server Hello" },
47 { SSL2_HND_SERVER_VERIFY, "Server Verify" },
48 { SSL2_HND_SERVER_FINISHED, "Server Finished" },
49 { SSL2_HND_REQUEST_CERTIFICATE, "Request Certificate" },
50 { SSL2_HND_CLIENT_CERTIFICATE, "Client Certificate" },
54 const value_string ssl_20_cipher_suites[] = {
55 { 0x010080, "SSL2_RC4_128_WITH_MD5" },
56 { 0x020080, "SSL2_RC4_128_EXPORT40_WITH_MD5" },
57 { 0x030080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
58 { 0x040080, "SSL2_RC2_CBC_128_CBC_WITH_MD5" },
59 { 0x050080, "SSL2_IDEA_128_CBC_WITH_MD5" },
60 { 0x060040, "SSL2_DES_64_CBC_WITH_MD5" },
61 { 0x0700c0, "SSL2_DES_192_EDE3_CBC_WITH_MD5" },
62 { 0x080080, "SSL2_RC4_64_WITH_MD5" },
63 { 0x000000, "TLS_NULL_WITH_NULL_NULL" },
64 { 0x000001, "TLS_RSA_WITH_NULL_MD5" },
65 { 0x000002, "TLS_RSA_WITH_NULL_SHA" },
66 { 0x000003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
67 { 0x000004, "TLS_RSA_WITH_RC4_128_MD5" },
68 { 0x000005, "TLS_RSA_WITH_RC4_128_SHA" },
69 { 0x000006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
70 { 0x000007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
71 { 0x000008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
72 { 0x000009, "TLS_RSA_WITH_DES_CBC_SHA" },
73 { 0x00000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
74 { 0x00000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
75 { 0x00000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
76 { 0x00000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
77 { 0x00000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
78 { 0x00000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
79 { 0x000010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
80 { 0x000011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
81 { 0x000012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
82 { 0x000013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
83 { 0x000014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
84 { 0x000015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
85 { 0x000016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
86 { 0x000017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
87 { 0x000018, "TLS_DH_anon_WITH_RC4_128_MD5" },
88 { 0x000019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
89 { 0x00001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
90 { 0x00001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
91 { 0x00001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
92 { 0x00001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
93 { 0x00001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
94 { 0x00002f, "TLS_RSA_WITH_AES_128_CBC_SHA" },
95 { 0x000030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
96 { 0x000031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
97 { 0x000032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
98 { 0x000033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
99 { 0x000034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
100 { 0x000035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
101 { 0x000036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
102 { 0x000037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
103 { 0x000038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
104 { 0x000039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
105 { 0x00003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
106 { 0x000041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
107 { 0x000042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
108 { 0x000043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
109 { 0x000044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
110 { 0x000045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
111 { 0x000046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
112 { 0x000047, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
113 { 0x000048, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
114 { 0x000049, "TLS_ECDH_ECDSA_WITH_DES_CBC_SHA" },
115 { 0x00004A, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
116 { 0x00004B, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
117 { 0x00004C, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
118 { 0x000060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
119 { 0x000061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
120 { 0x000062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
121 { 0x000063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
122 { 0x000064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
123 { 0x000065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
124 { 0x000066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
125 { 0x000084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
126 { 0x000085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
127 { 0x000086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
128 { 0x000087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
129 { 0x000088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
130 { 0x000089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
131 /* these from http://www.mozilla.org/projects/
132 security/pki/nss/ssl/fips-ssl-ciphersuites.html */
133 { 0x00fefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
134 { 0x00feff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
135 { 0x00ffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
136 { 0x00ffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
137 /* Microsoft's old PCT protocol. These are from Eric Rescorla's
138 book "SSL and TLS" */
139 { 0x8f8001, "PCT_SSL_COMPAT | PCT_VERSION_1" },
140 { 0x800003, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509_CHAIN" },
141 { 0x800001, "PCT_SSL_CERT_TYPE | PCT1_CERT_X509" },
142 { 0x810001, "PCT_SSL_HASH_TYPE | PCT1_HASH_MD5" },
143 { 0x810003, "PCT_SSL_HASH_TYPE | PCT1_HASH_SHA" },
144 { 0x820001, "PCT_SSL_EXCH_TYPE | PCT1_EXCH_RSA_PKCS1" },
145 { 0x830004, "PCT_SSL_CIPHER_TYPE_1ST_HALF | PCT1_CIPHER_RC4" },
146 { 0x848040, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_128 | PCT1_MAC_BITS_128" },
147 { 0x842840, "PCT_SSL_CIPHER_TYPE_2ND_HALF | PCT1_ENC_BITS_40 | PCT1_MAC_BITS_128" },
148 /* note that ciphersuites of {0x00????} are TLS cipher suites in
149 * a sslv2 client hello message; the ???? above is the two-byte
150 * tls cipher suite id
155 const value_string ssl_20_certificate_type[] = {
157 { 0x01, "X.509 Certificate" },
161 const value_string ssl_31_content_type[] = {
162 { 20, "Change Cipher Spec" },
165 { 23, "Application Data" },
169 const value_string ssl_versions[] = {
170 { 0x0100, "DTLS 1.0" },
171 { 0x0302, "TLS 1.1" },
172 { 0x0301, "TLS 1.0" },
173 { 0x0300, "SSL 3.0" },
174 { 0x0002, "SSL 2.0" },
179 /* XXX - would be used if we dissected the body of a Change Cipher Spec
181 const value_string ssl_31_change_cipher_spec[] = {
182 { 1, "Change Cipher Spec" },
187 const value_string ssl_31_alert_level[] = {
193 const value_string ssl_31_alert_description[] = {
194 { 0, "Close Notify" },
195 { 10, "Unexpected Message" },
196 { 20, "Bad Record MAC" },
197 { 21, "Decryption Failed" },
198 { 22, "Record Overflow" },
199 { 30, "Decompression Failure" },
200 { 40, "Handshake Failure" },
201 { 42, "Bad Certificate" },
202 { 43, "Unsupported Certificate" },
203 { 44, "Certificate Revoked" },
204 { 45, "Certificate Expired" },
205 { 46, "Certificate Unknown" },
206 { 47, "Illegal Parameter" },
207 { 48, "Unknown CA" },
208 { 49, "Access Denied" },
209 { 50, "Decode Error" },
210 { 51, "Decrypt Error" },
211 { 60, "Export Restriction" },
212 { 70, "Protocol Version" },
213 { 71, "Insufficient Security" },
214 { 80, "Internal Error" },
215 { 90, "User Canceled" },
216 { 100, "No Renegotiation" },
220 const value_string ssl_31_handshake_type[] = {
221 { SSL_HND_HELLO_REQUEST, "Hello Request" },
222 { SSL_HND_CLIENT_HELLO, "Client Hello" },
223 { SSL_HND_SERVER_HELLO, "Server Hello" },
224 { SSL_HND_HELLO_VERIFY_REQUEST, "Hello Verify Request"},
225 { SSL_HND_CERTIFICATE, "Certificate" },
226 { SSL_HND_SERVER_KEY_EXCHG, "Server Key Exchange" },
227 { SSL_HND_CERT_REQUEST, "Certificate Request" },
228 { SSL_HND_SVR_HELLO_DONE, "Server Hello Done" },
229 { SSL_HND_CERT_VERIFY, "Certificate Verify" },
230 { SSL_HND_CLIENT_KEY_EXCHG, "Client Key Exchange" },
231 { SSL_HND_FINISHED, "Finished" },
235 const value_string ssl_31_compression_method[] = {
243 /* XXX - would be used if we dissected a Signature, as would be
244 seen in a server key exchange or certificate verify message. */
245 const value_string ssl_31_key_exchange_algorithm[] = {
247 { 1, "Diffie Hellman" },
251 const value_string ssl_31_signature_algorithm[] = {
259 const value_string ssl_31_client_certificate_type[] = {
262 { 3, "RSA Fixed DH" },
263 { 4, "DSS Fixed DH" },
268 /* XXX - would be used if we dissected exchange keys, as would be
269 seen in a client key exchange message. */
270 const value_string ssl_31_public_value_encoding[] = {
277 const value_string ssl_31_ciphersuite[] = {
278 { 0x0000, "TLS_NULL_WITH_NULL_NULL" },
279 { 0x0001, "TLS_RSA_WITH_NULL_MD5" },
280 { 0x0002, "TLS_RSA_WITH_NULL_SHA" },
281 { 0x0003, "TLS_RSA_EXPORT_WITH_RC4_40_MD5" },
282 { 0x0004, "TLS_RSA_WITH_RC4_128_MD5" },
283 { 0x0005, "TLS_RSA_WITH_RC4_128_SHA" },
284 { 0x0006, "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5" },
285 { 0x0007, "TLS_RSA_WITH_IDEA_CBC_SHA" },
286 { 0x0008, "TLS_RSA_EXPORT_WITH_DES40_CBC_SHA" },
287 { 0x0009, "TLS_RSA_WITH_DES_CBC_SHA" },
288 { 0x000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" },
289 { 0x000b, "TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA" },
290 { 0x000c, "TLS_DH_DSS_WITH_DES_CBC_SHA" },
291 { 0x000d, "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA" },
292 { 0x000e, "TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA" },
293 { 0x000f, "TLS_DH_RSA_WITH_DES_CBC_SHA" },
294 { 0x0010, "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA" },
295 { 0x0011, "TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" },
296 { 0x0012, "TLS_DHE_DSS_WITH_DES_CBC_SHA" },
297 { 0x0013, "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" },
298 { 0x0014, "TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA" },
299 { 0x0015, "TLS_DHE_RSA_WITH_DES_CBC_SHA" },
300 { 0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" },
301 { 0x0017, "TLS_DH_anon_EXPORT_WITH_RC4_40_MD5" },
302 { 0x0018, "TLS_DH_anon_WITH_RC4_128_MD5" },
303 { 0x0019, "TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA" },
304 { 0x001a, "TLS_DH_anon_WITH_DES_CBC_SHA" },
305 { 0x001b, "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" },
306 { 0x001c, "SSL_FORTEZZA_KEA_WITH_NULL_SHA" },
307 { 0x001d, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA" },
308 { 0x001e, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA" },
309 { 0x002f, "TLS_RSA_WITH_AES_128_CBC_SHA" },
310 { 0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA" },
311 { 0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA" },
312 { 0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" },
313 { 0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" },
314 { 0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA" },
315 { 0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA" },
316 { 0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA" },
317 { 0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA" },
318 { 0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" },
319 { 0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" },
320 { 0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA" },
321 { 0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" },
322 { 0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA" },
323 { 0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA" },
324 { 0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" },
325 { 0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" },
326 { 0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" },
327 { 0x0047, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
328 { 0x0048, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
329 { 0x0049, "TLS_ECDH_ECDSA_WITH_DES_CBC_SHA" },
330 { 0x004A, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA" },
331 { 0x004B, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA" },
332 { 0x004C, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA" },
333 { 0x0060, "TLS_RSA_EXPORT1024_WITH_RC4_56_MD5" },
334 { 0x0061, "TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5" },
335 { 0x0062, "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA" },
336 { 0x0063, "TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA" },
337 { 0x0064, "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA" },
338 { 0x0065, "TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA" },
339 { 0x0066, "TLS_DHE_DSS_WITH_RC4_128_SHA" },
340 { 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
341 { 0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
342 { 0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
343 { 0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" },
344 { 0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" },
345 { 0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" },
346 /* these from http://www.mozilla.org/projects/
347 security/pki/nss/ssl/fips-ssl-ciphersuites.html */
348 { 0xfefe, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
349 { 0xfeff, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
350 { 0xffe0, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA" },
351 { 0xffe1, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
352 /* note that ciphersuites 0xff00 - 0xffff are private */
356 const value_string pct_msg_types[] = {
357 { PCT_MSG_CLIENT_HELLO, "Client Hello" },
358 { PCT_MSG_SERVER_HELLO, "Server Hello" },
359 { PCT_MSG_CLIENT_MASTER_KEY, "Client Master Key" },
360 { PCT_MSG_SERVER_VERIFY, "Server Verify" },
361 { PCT_MSG_ERROR, "Error" },
365 const value_string pct_cipher_type[] = {
366 { PCT_CIPHER_DES, "DES" },
367 { PCT_CIPHER_IDEA, "IDEA" },
368 { PCT_CIPHER_RC2, "RC2" },
369 { PCT_CIPHER_RC4, "RC4" },
370 { PCT_CIPHER_DES_112, "DES 112 bit" },
371 { PCT_CIPHER_DES_168, "DES 168 bit" },
375 const value_string pct_hash_type[] = {
376 { PCT_HASH_MD5, "MD5" },
377 { PCT_HASH_MD5_TRUNC_64, "MD5_TRUNC_64"},
378 { PCT_HASH_SHA, "SHA"},
379 { PCT_HASH_SHA_TRUNC_80, "SHA_TRUNC_80"},
380 { PCT_HASH_DES_DM, "DES_DM"},
384 const value_string pct_cert_type[] = {
385 { PCT_CERT_NONE, "None" },
386 { PCT_CERT_X509, "X.509" },
387 { PCT_CERT_PKCS7, "PKCS #7" },
390 const value_string pct_sig_type[] = {
391 { PCT_SIG_NONE, "None" },
392 { PCT_SIG_RSA_MD5, "MD5" },
393 { PCT_SIG_RSA_SHA, "RSA SHA" },
394 { PCT_SIG_DSA_SHA, "DSA SHA" },
398 const value_string pct_exch_type[] = {
399 { PCT_EXCH_RSA_PKCS1, "RSA PKCS#1" },
400 { PCT_EXCH_RSA_PKCS1_TOKEN_DES, "RSA PKCS#1 Token DES" },
401 { PCT_EXCH_RSA_PKCS1_TOKEN_DES3, "RSA PKCS#1 Token 3DES" },
402 { PCT_EXCH_RSA_PKCS1_TOKEN_RC2, "RSA PKCS#1 Token RC-2" },
403 { PCT_EXCH_RSA_PKCS1_TOKEN_RC4, "RSA PKCS#1 Token RC-4" },
404 { PCT_EXCH_DH_PKCS3, "DH PKCS#3" },
405 { PCT_EXCH_DH_PKCS3_TOKEN_DES, "DH PKCS#3 Token DES" },
406 { PCT_EXCH_DH_PKCS3_TOKEN_DES3, "DH PKCS#3 Token 3DES" },
407 { PCT_EXCH_FORTEZZA_TOKEN, "Fortezza" },
411 const value_string pct_error_code[] = {
412 { PCT_ERR_BAD_CERTIFICATE, "PCT_ERR_BAD_CERTIFICATE" },
413 { PCT_ERR_CLIENT_AUTH_FAILED, "PCT_ERR_CLIENT_AUTH_FAILE" },
414 { PCT_ERR_ILLEGAL_MESSAGE, "PCT_ERR_ILLEGAL_MESSAGE" },
415 { PCT_ERR_INTEGRITY_CHECK_FAILED, "PCT_ERR_INTEGRITY_CHECK_FAILED" },
416 { PCT_ERR_SERVER_AUTH_FAILED, "PCT_ERR_SERVER_AUTH_FAILED" },
417 { PCT_ERR_SPECS_MISMATCH, "PCT_ERR_SPECS_MISMATCH" },
422 const value_string tls_hello_extension_types[] = {
423 { 0, "server_name" },
424 { 1, "max_fragment_length" },
425 { 2, "client_certificate_url" },
426 { 3, "trusted_ca_keys" },
427 { 4, "truncated_hmac" },
428 { 5, "status_request" },
429 { 35, "EAP-FAST PAC-Opaque" /* draft-cam-winget-eap-fast-00.txt */ },
434 /* we keep this internal to packet-ssl-utils, as there should be
435 no need to access it any other way.
437 This also allows us to hide the dependency on zlib.
439 struct _SslDecompress {
447 ssl_data_alloc(StringInfo* str, guint len)
449 str->data = g_malloc(len);
457 ssl_data_set(StringInfo* str, const guchar* data, guint len)
459 memcpy(str->data, data, len);
463 #ifdef HAVE_LIBGNUTLS
465 static gint ver_major, ver_minor, ver_patch;
467 /* hmac abstraction layer */
468 #define SSL_HMAC gcry_md_hd_t
471 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
473 gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
474 gcry_md_setkey (*(md), key, len);
477 ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
479 gcry_md_write(*(md), data, len);
482 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
486 algo = gcry_md_get_algo (*(md));
487 len = gcry_md_get_algo_dlen(algo);
488 memcpy(data, gcry_md_read(*(md), algo), len);
492 ssl_hmac_cleanup(SSL_HMAC* md)
494 gcry_md_close(*(md));
497 /* memory digest abstraction layer*/
498 #define SSL_MD gcry_md_hd_t
501 ssl_md_init(SSL_MD* md, gint algo)
503 gcry_md_open(md,algo, 0);
506 ssl_md_update(SSL_MD* md, guchar* data, gint len)
508 gcry_md_write(*(md), data, len);
511 ssl_md_final(SSL_MD* md, guchar* data, guint* datalen)
515 algo = gcry_md_get_algo (*(md));
516 len = gcry_md_get_algo_dlen (algo);
517 memcpy(data, gcry_md_read(*(md), algo), len);
521 ssl_md_cleanup(SSL_MD* md)
523 gcry_md_close(*(md));
526 /* md5 /sha abstraction layer */
527 #define SSL_SHA_CTX gcry_md_hd_t
528 #define SSL_MD5_CTX gcry_md_hd_t
531 ssl_sha_init(SSL_SHA_CTX* md)
533 gcry_md_open(md,GCRY_MD_SHA1, 0);
536 ssl_sha_update(SSL_SHA_CTX* md, guchar* data, gint len)
538 gcry_md_write(*(md), data, len);
541 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
543 memcpy(buf, gcry_md_read(*(md), GCRY_MD_SHA1),
544 gcry_md_get_algo_dlen(GCRY_MD_SHA1));
547 ssl_sha_cleanup(SSL_SHA_CTX* md)
549 gcry_md_close(*(md));
553 ssl_md5_init(SSL_MD5_CTX* md)
555 return gcry_md_open(md,GCRY_MD_MD5, 0);
558 ssl_md5_update(SSL_MD5_CTX* md, guchar* data, gint len)
560 gcry_md_write(*(md), data, len);
563 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
565 memcpy(buf, gcry_md_read(*(md), GCRY_MD_MD5),
566 gcry_md_get_algo_dlen(GCRY_MD_MD5));
569 ssl_md5_cleanup(SSL_MD5_CTX* md)
571 gcry_md_close(*(md));
575 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
582 c=(gcry_cipher_hd_t)*cipher;
584 ssl_debug_printf("--------------------------------------------------------------------");
585 /*for(ivp=c->iv,i=0; i < iv_len; i++ )
587 ssl_debug_printf("%d ",ivp[i]);
591 ssl_debug_printf("--------------------------------------------------------------------");
592 ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
593 /*for(ivp=c->iv,i=0; i < iv_len; i++ )
595 ssl_debug_printf("%d ",ivp[i]);
599 ssl_debug_printf("--------------------------------------------------------------------");
602 /* stream cipher abstraction layer*/
604 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
605 guchar* iv, gint mode)
607 gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC};
609 err = gcry_cipher_open(cipher, algo, gcry_modes[mode], 0);
612 err = gcry_cipher_setkey(*(cipher), sk, gcry_cipher_get_algo_keylen (algo));
615 err = gcry_cipher_setiv(*(cipher), iv, gcry_cipher_get_algo_blklen (algo));
621 ssl_cipher_decrypt(gcry_cipher_hd_t *cipher, guchar * out, gint outl,
622 const guchar * in, gint inl)
624 return gcry_cipher_decrypt ( *(cipher), out, outl, in, inl);
627 ssl_get_digest_by_name(const gchar*name)
629 return gcry_md_map_name(name);
632 ssl_get_cipher_by_name(const gchar* name)
634 return gcry_cipher_map_name(name);
638 ssl_cipher_cleanup(gcry_cipher_hd_t *cipher)
640 gcry_cipher_close(*cipher);
644 /* private key abstraction layer */
646 ssl_get_key_len(SSL_PRIVATE_KEY* pk) {return gcry_pk_get_nbits (pk); }
649 _gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
650 gcry_mpi_t *skey, gint flags);
652 #define PUBKEY_FLAG_NO_BLINDING (1 << 0)
654 /* decrypt data with private key. Store decrypted data directly into input
657 ssl_private_decrypt(guint len, guchar* encr_data, SSL_PRIVATE_KEY* pk)
661 gcry_sexp_t s_data, s_plain;
664 guchar* decr_data_ptr;
669 /* build up a mpi rappresentation for encrypted data */
670 rc = gcry_mpi_scan(&encr_mpi, GCRYMPI_FMT_USG,encr_data, encr_len, &encr_len);
672 ssl_debug_printf("pcry_private_decrypt: can't convert encr_data to mpi (size %d):%s\n",
673 len, gcry_strerror(rc));
678 /* put the data into a simple list */
679 rc = gcry_sexp_build(&s_data, NULL, "(enc-val(rsa(a%m)))", encr_mpi);
681 ssl_debug_printf("pcry_private_decrypt: can't build encr_sexp:%s \n",
686 /* pass it to libgcrypt */
687 rc = gcry_pk_decrypt(&s_plain, s_data, pk);
688 gcry_sexp_release(s_data);
691 ssl_debug_printf("pcry_private_decrypt: can't decrypt key:%s\n",
696 /* convert plain text sexp to mpi format */
697 text = gcry_sexp_nth_mpi(s_plain, 0, 0);
699 /* compute size requested for plaintext buffer */
701 if (gcry_mpi_print(GCRYMPI_FMT_USG, NULL, decr_len, &decr_len, text) != 0) {
702 ssl_debug_printf("pcry_private_decrypt: can't compute decr size:%s\n",
708 /* sanity check on out buffer */
709 if (decr_len > len) {
710 ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%d max %d)\n",
715 /* write plain text to encrypted data buffer */
716 decr_data_ptr = encr_data;
717 if (gcry_mpi_print( GCRYMPI_FMT_USG, decr_data_ptr, decr_len, &decr_len,
719 ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %d):%s\n",
720 decr_len, gcry_strerror(rc));
721 g_free(decr_data_ptr);
726 /* strip the padding*/
728 for (i = 1; i < decr_len; i++) {
729 if (decr_data_ptr[i] == 0) {
735 ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %d\n",
737 ssl_print_data("decypted_unstrip_pre_master", decr_data_ptr, decr_len);
738 g_memmove(decr_data_ptr, &decr_data_ptr[rc], decr_len - rc);
742 gcry_sexp_release(s_plain);
744 rc = _gcry_rsa_decrypt(0, &text, &encr_mpi, pk,0);
745 gcry_mpi_print( GCRYMPI_FMT_USG, 0, 0, &decr_len, text);
747 /* sanity check on out buffer */
748 if (decr_len > len) {
749 ssl_debug_printf("pcry_private_decrypt: decrypted data is too long ?!? (%d max %d)\n",
754 /* write plain text to newly allocated buffer */
755 decr_data_ptr = encr_data;
756 if (gcry_mpi_print( GCRYMPI_FMT_USG, decr_data_ptr, decr_len, &decr_len,
758 ssl_debug_printf("pcry_private_decrypt: can't print decr data to mpi (size %d):%s\n",
759 decr_len, gcry_strerror(rc));
763 /* strip the padding*/
765 for (i = 1; i < decr_len; i++) {
766 if (decr_data_ptr[i] == 0) {
772 ssl_debug_printf("pcry_private_decrypt: stripping %d bytes, decr_len %d\n",
774 ssl_print_data("decypted_unstrip_pre_master", decr_data_ptr, decr_len);
775 g_memmove(decr_data_ptr, &decr_data_ptr[rc], decr_len - rc);
777 #endif /* SSL_FAST */
778 gcry_mpi_release(text);
782 /* stringinfo interface */
784 ssl_data_realloc(StringInfo* str, guint len)
786 str->data = g_realloc(str->data, len);
794 ssl_data_copy(StringInfo* dst, StringInfo* src)
796 if (dst->data_len < src->data_len) {
797 if (ssl_data_realloc(dst, src->data_len))
800 memcpy(dst->data, src->data, src->data_len);
801 dst->data_len = src->data_len;
805 #define PRF(ssl,secret,usage,rnd1,rnd2,out) ((ssl->version_netorder==SSLV3_VERSION)? \
806 ssl3_prf(secret,usage,rnd1,rnd2,out): \
807 tls_prf(secret,usage,rnd1,rnd2,out))
809 static const gchar *digests[]={
814 static const gchar *ciphers[]={
817 "ARCFOUR", /* gnutls does not support rc4, but this should be 100% compatible*/
825 /* look in openssl/ssl/ssl_lib.c for a complete list of available cipersuite*/
826 static SslCipherSuite cipher_suites[]={
827 {1,KEX_RSA,SIG_RSA,ENC_NULL,0,0,0,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
828 {2,KEX_RSA,SIG_RSA,ENC_NULL,0,0,0,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
829 {3,KEX_RSA,SIG_RSA,ENC_RC4,1,128,40,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
830 {4,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
831 {5,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
832 {6,KEX_RSA,SIG_RSA,ENC_RC2,8,128,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
833 {7,KEX_RSA,SIG_RSA,ENC_IDEA,8,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
834 {8,KEX_RSA,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
835 {9,KEX_RSA,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
836 {10,KEX_RSA,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
837 {11,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
838 {12,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
839 {13,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
840 {14,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
841 {15,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
842 {16,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
843 {17,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
844 {18,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
845 {19,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
846 {20,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
847 {21,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
848 {22,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
849 {23,KEX_DH,SIG_NONE,ENC_RC4,1,128,40,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
850 {24,KEX_DH,SIG_NONE,ENC_RC4,1,128,128,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
851 {25,KEX_DH,SIG_NONE,ENC_DES,8,64,40,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
852 {26,KEX_DH,SIG_NONE,ENC_DES,8,64,64,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
853 {27,KEX_DH,SIG_NONE,ENC_3DES,8,192,192,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
854 {47,KEX_RSA,SIG_RSA,ENC_AES,16,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
855 {53,KEX_RSA,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
856 {96,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
857 {97,KEX_RSA,SIG_RSA,ENC_RC2,1,128,56,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
858 {98,KEX_RSA,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
859 {99,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,16,1, SSL_CIPHER_MODE_STREAM},
860 {100,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
861 {101,KEX_DH,SIG_DSS,ENC_RC4,1,128,56,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
862 {102,KEX_DH,SIG_DSS,ENC_RC4,1,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
863 {-1, 0,0,0,0,0,0,0,0,0, 0}
866 #define MAX_BLOCK_SIZE 16
867 #define MAX_KEY_SIZE 32
870 ssl_find_cipher(int num,SslCipherSuite* cs)
874 for(c=cipher_suites;c->number!=-1;c++){
885 tls_hash(StringInfo* secret,
886 StringInfo* seed, gint md, StringInfo* out)
892 guint8 _A[20],tmp[20];
899 ssl_print_string("tls_hash: hash secret", secret);
900 ssl_print_string("tls_hash: hash seed", seed);
905 ssl_hmac_init(&hm,secret->data,secret->data_len,md);
906 ssl_hmac_update(&hm,A,A_l);
907 ssl_hmac_final(&hm,_A,&A_l);
908 ssl_hmac_cleanup(&hm);
911 ssl_hmac_init(&hm,secret->data,secret->data_len,md);
912 ssl_hmac_update(&hm,A,A_l);
913 ssl_hmac_update(&hm,seed->data,seed->data_len);
914 ssl_hmac_final(&hm,tmp,&tmp_l);
915 ssl_hmac_cleanup(&hm);
917 tocpy=MIN(left,tmp_l);
918 memcpy(ptr,tmp,tocpy);
923 ssl_print_string("hash out", out);
928 tls_prf(StringInfo* secret, const gchar *usage,
929 StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
931 StringInfo seed, sha_out, md5_out;
937 usage_len = strlen(usage);
939 /* initalize buffer for sha, md5 random seed*/
940 if (ssl_data_alloc(&sha_out, MAX(out->data_len,20)) < 0)
942 if (ssl_data_alloc(&md5_out, MAX(out->data_len,16)) < 0)
944 if (ssl_data_alloc(&seed, usage_len+rnd1->data_len+rnd2->data_len) < 0)
948 memcpy(ptr,usage,usage_len); ptr+=usage_len;
949 memcpy(ptr,rnd1->data,rnd1->data_len); ptr+=rnd1->data_len;
950 memcpy(ptr,rnd2->data,rnd2->data_len); ptr+=rnd2->data_len;
952 /* initalize buffer for client/server seeds*/
953 s_l=secret->data_len/2 + secret->data_len%2;
954 if (ssl_data_alloc(&s1, s_l) < 0)
956 if (ssl_data_alloc(&s2, s_l) < 0)
959 memcpy(s1.data,secret->data,s_l);
960 memcpy(s2.data,secret->data + (secret->data_len - s_l),s_l);
962 ssl_debug_printf("tls_prf: tls_hash(md5 secret_len %d seed_len %d )\n", s1.data_len, seed.data_len);
963 if(tls_hash(&s1,&seed,ssl_get_digest_by_name("MD5"),&md5_out) != 0)
965 ssl_debug_printf("tls_prf: tls_hash(sha)\n");
966 if(tls_hash(&s2,&seed,ssl_get_digest_by_name("SHA1"),&sha_out) != 0)
969 for(i=0;i<out->data_len;i++)
970 out->data[i]=md5_out.data[i] ^ sha_out.data[i];
973 ssl_print_string("PRF out",out);
981 g_free(md5_out.data);
983 g_free(sha_out.data);
988 ssl3_generate_export_iv(StringInfo* r1,
989 StringInfo* r2, StringInfo* out)
995 ssl_md5_update(&md5,r1->data,r1->data_len);
996 ssl_md5_update(&md5,r2->data,r2->data_len);
997 ssl_md5_final(tmp,&md5);
998 ssl_md5_cleanup(&md5);
1000 memcpy(out->data,tmp,out->data_len);
1001 ssl_print_string("export iv", out);
1007 ssl3_prf(StringInfo* secret, const gchar* usage,
1009 StringInfo* r2,StringInfo* out)
1013 StringInfo *rnd1,*rnd2;
1020 for(off=0;off<out->data_len;off+=16){
1025 ssl_debug_printf("ssl3_prf: sha1_hash(%d)\n",i);
1026 /* A, BB, CCC, ... */
1032 ssl_sha_update(&sha,buf,i);
1033 ssl_sha_update(&sha,secret->data,secret->data_len);
1035 if(!strcmp(usage,"client write key") || !strcmp(usage,"server write key")){
1036 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
1037 ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
1040 ssl_sha_update(&sha,rnd1->data,rnd1->data_len);
1041 ssl_sha_update(&sha,rnd2->data,rnd2->data_len);
1044 ssl_sha_final(buf,&sha);
1045 ssl_sha_cleanup(&sha);
1047 ssl_debug_printf("ssl3_prf: md5_hash(%d) datalen %d\n",i,
1050 ssl_md5_update(&md5,secret->data,secret->data_len);
1051 ssl_md5_update(&md5,buf,20);
1052 ssl_md5_final(outbuf,&md5);
1053 ssl_md5_cleanup(&md5);
1055 tocpy=MIN(out->data_len-off,16);
1056 memcpy(out->data+off,outbuf,tocpy);
1063 ssl_create_flow(void)
1067 flow = se_alloc(sizeof(SslFlow));
1070 flow->multisegment_pdus = se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "ssl_multisegment_pdus");
1074 /* memory allocations functions for zlib intialization */
1075 static void* ssl_zalloc(void* opaque _U_, unsigned int no, unsigned int size)
1077 return g_malloc0(no*size);
1079 static void ssl_zfree(void* opaque _U_, void* address)
1084 static SslDecompress*
1085 ssl_create_decompressor(gint compression)
1087 SslDecompress *decomp;
1090 if (compression == 0) return NULL;
1091 ssl_debug_printf("ssl_create_decompressor: compression method %d\n", compression);
1092 decomp = se_alloc(sizeof(SslDecompress));
1093 decomp->compression = compression;
1094 switch (decomp->compression) {
1096 case 1: /* DEFLATE */
1097 decomp->istream.zalloc = ssl_zalloc;
1098 decomp->istream.zfree = ssl_zfree;
1099 decomp->istream.opaque = Z_NULL;
1100 decomp->istream.next_in = Z_NULL;
1101 decomp->istream.next_out = Z_NULL;
1102 decomp->istream.avail_in = 0;
1103 decomp->istream.avail_out = 0;
1104 err = inflateInit_(&decomp->istream, ZLIB_VERSION, sizeof(z_stream));
1106 ssl_debug_printf("ssl_create_decompressor: inflateInit_() failed - %d\n", err);
1112 ssl_debug_printf("ssl_create_decompressor: unsupported compression method %d\n", decomp->compression);
1119 ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
1120 guint8 *mk, guint8 *sk, guint8 *iv)
1126 dec = se_alloc0(sizeof(SslDecoder));
1127 /* Find the SSLeay cipher */
1128 if(cipher_suite->enc!=ENC_NULL) {
1129 ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", ciphers[cipher_suite->enc-0x30]);
1130 ciph=ssl_get_cipher_by_name(ciphers[cipher_suite->enc-0x30]);
1133 ssl_debug_printf("ssl_create_decoder can't find cipher %s\n",
1134 ciphers[(cipher_suite->enc-0x30) > 7 ? 7 : (cipher_suite->enc-0x30)]);
1138 /* init mac buffer: mac storage is embedded into decoder struct to save a
1139 memory allocation and waste samo more memory*/
1140 dec->cipher_suite=cipher_suite;
1141 dec->compression = compression;
1142 dec->mac_key.data = dec->_mac_key;
1143 ssl_data_set(&dec->mac_key, mk, cipher_suite->dig_len);
1145 dec->decomp = ssl_create_decompressor(compression);
1146 dec->flow = ssl_create_flow();
1149 ssl_cipher_cleanup(&dec->evp);
1151 if (ssl_cipher_init(&dec->evp,ciph,sk,iv,cipher_suite->mode) < 0) {
1152 ssl_debug_printf("ssl_create_decoder: can't create cipher id:%d mode:%d\n",
1153 ciph, cipher_suite->mode);
1157 ssl_debug_printf("decoder initialized (digest len %d)\n", cipher_suite->dig_len);
1162 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
1164 StringInfo key_block;
1165 guint8 _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
1166 guint8 _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
1168 guint8 *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
1170 /* if master_key is not yet generate, create it now*/
1171 if (!(ssl_session->state & SSL_MASTER_SECRET)) {
1172 ssl_debug_printf("ssl_generate_keyring_material:PRF(pre_master_secret)\n");
1173 if (PRF(ssl_session,&ssl_session->pre_master_secret,"master secret",
1174 &ssl_session->client_random,
1175 &ssl_session->server_random, &ssl_session->master_secret)) {
1176 ssl_debug_printf("ssl_generate_keyring_material can't generate master_secret\n");
1179 ssl_print_string("master secret",&ssl_session->master_secret);
1182 /* Compute the key block. First figure out how much data we need*/
1183 needed=ssl_session->cipher_suite.dig_len*2;
1184 needed+=ssl_session->cipher_suite.bits / 4;
1185 if(ssl_session->cipher_suite.block>1)
1186 needed+=ssl_session->cipher_suite.block*2;
1188 key_block.data_len = needed;
1189 key_block.data = g_malloc(needed);
1190 if (!key_block.data) {
1191 ssl_debug_printf("ssl_generate_keyring_material can't allacate key_block\n");
1194 ssl_debug_printf("ssl_generate_keyring_material sess key generation\n");
1195 if (PRF(ssl_session,&ssl_session->master_secret,"key expansion",
1196 &ssl_session->server_random,&ssl_session->client_random,
1198 ssl_debug_printf("ssl_generate_keyring_material can't generate key_block\n");
1201 ssl_print_string("key expansion", &key_block);
1204 c_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
1205 s_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
1207 c_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
1208 s_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
1210 if(ssl_session->cipher_suite.block>1){
1211 c_iv=ptr; ptr+=ssl_session->cipher_suite.block;
1212 s_iv=ptr; ptr+=ssl_session->cipher_suite.block;
1215 if(ssl_session->cipher_suite.export){
1216 StringInfo iv_c,iv_s;
1217 StringInfo key_c,key_s;
1220 if(ssl_session->cipher_suite.block>1){
1222 /* We only have room for MAX_BLOCK_SIZE bytes IVs, but that's
1223 all we should need. This is a sanity check */
1224 if(ssl_session->cipher_suite.block>MAX_BLOCK_SIZE) {
1225 ssl_debug_printf("ssl_generate_keyring_material cipher suite block must be at most %d nut is %d\n",
1226 MAX_BLOCK_SIZE, ssl_session->cipher_suite.block);
1231 iv_c.data_len = ssl_session->cipher_suite.block;
1233 iv_s.data_len = ssl_session->cipher_suite.block;
1235 if(ssl_session->version_netorder==SSLV3_VERSION){
1236 ssl_debug_printf("ssl_generate_keyring_material ssl3_generate_export_iv\n");
1237 if (ssl3_generate_export_iv(&ssl_session->client_random,
1238 &ssl_session->server_random,&iv_c)) {
1239 ssl_debug_printf("ssl_generate_keyring_material can't generate sslv3 client iv\n");
1242 ssl_debug_printf("ssl_generate_keyring_material ssl3_generate_export_iv(2)\n");
1243 if (ssl3_generate_export_iv(&ssl_session->server_random,
1244 &ssl_session->client_random,&iv_s)) {
1245 ssl_debug_printf("ssl_generate_keyring_material can't generate sslv3 server iv\n");
1250 guint8 _iv_block[MAX_BLOCK_SIZE * 2];
1251 StringInfo iv_block;
1252 StringInfo key_null;
1255 key_null.data = &_key_null;
1256 key_null.data_len = 0;
1258 iv_block.data = _iv_block;
1259 iv_block.data_len = ssl_session->cipher_suite.block*2;
1261 ssl_debug_printf("ssl_generate_keyring_material prf(iv_block)\n");
1262 if(PRF(ssl_session,&key_null, "IV block",
1263 &ssl_session->client_random,
1264 &ssl_session->server_random,&iv_block)) {
1265 ssl_debug_printf("ssl_generate_keyring_material can't generate tls31 iv block\n");
1269 memcpy(_iv_c,iv_block.data,ssl_session->cipher_suite.block);
1270 memcpy(_iv_s,iv_block.data+ssl_session->cipher_suite.block,
1271 ssl_session->cipher_suite.block);
1278 if (ssl_session->version_netorder==SSLV3_VERSION){
1281 ssl_debug_printf("ssl_generate_keyring_material MD5(client_random)\n");
1284 ssl_md5_update(&md5,c_wk,ssl_session->cipher_suite.eff_bits/8);
1285 ssl_md5_update(&md5,ssl_session->client_random.data,
1286 ssl_session->client_random.data_len);
1287 ssl_md5_update(&md5,ssl_session->server_random.data,
1288 ssl_session->server_random.data_len);
1289 ssl_md5_final(_key_c,&md5);
1290 ssl_md5_cleanup(&md5);
1294 ssl_debug_printf("ssl_generate_keyring_material MD5(server_random)\n");
1295 ssl_md5_update(&md5,s_wk,ssl_session->cipher_suite.eff_bits/8);
1296 ssl_md5_update(&md5,ssl_session->server_random.data,
1297 ssl_session->server_random.data_len);
1298 ssl_md5_update(&md5,ssl_session->client_random.data,
1299 ssl_session->client_random.data_len);
1300 ssl_md5_final(_key_s,&md5);
1301 ssl_md5_cleanup(&md5);
1305 key_c.data = _key_c;
1306 key_c.data_len = sizeof(_key_c);
1307 key_s.data = _key_s;
1308 key_s.data_len = sizeof(_key_s);
1311 k.data_len = ssl_session->cipher_suite.eff_bits/8;
1312 ssl_debug_printf("ssl_generate_keyring_material PRF(key_c)\n");
1313 if (PRF(ssl_session,&k,"client write key",
1314 &ssl_session->client_random,
1315 &ssl_session->server_random, &key_c)) {
1316 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 server key \n");
1322 k.data_len = ssl_session->cipher_suite.eff_bits/8;
1323 ssl_debug_printf("ssl_generate_keyring_material PRF(key_s)\n");
1324 if(PRF(ssl_session,&k,"server write key",
1325 &ssl_session->client_random,
1326 &ssl_session->server_random, &key_s)) {
1327 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 client key \n");
1334 /* show key material info */
1335 ssl_print_data("Client MAC key",c_mk,ssl_session->cipher_suite.dig_len);
1336 ssl_print_data("Server MAC key",s_mk,ssl_session->cipher_suite.dig_len);
1337 ssl_print_data("Client Write key",c_wk,ssl_session->cipher_suite.bits/8);
1338 ssl_print_data("Server Write key",s_wk,ssl_session->cipher_suite.bits/8);
1340 if(ssl_session->cipher_suite.block>1) {
1341 ssl_print_data("Client Write IV",c_iv,ssl_session->cipher_suite.block);
1342 ssl_print_data("Server Write IV",s_iv,ssl_session->cipher_suite.block);
1345 ssl_print_data("Client Write IV",c_iv,8);
1346 ssl_print_data("Server Write IV",s_iv,8);
1349 /* create both client and server ciphers*/
1350 ssl_debug_printf("ssl_generate_keyring_material ssl_create_decoder(client)\n");
1351 ssl_session->client_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->compression, c_mk, c_wk, c_iv);
1352 if (!ssl_session->client_new) {
1353 ssl_debug_printf("ssl_generate_keyring_material can't init client decoder\n");
1356 ssl_debug_printf("ssl_generate_keyring_material ssl_create_decoder(server)\n");
1357 ssl_session->server_new = ssl_create_decoder(&ssl_session->cipher_suite, ssl_session->compression, s_mk, s_wk, s_iv);
1358 if (!ssl_session->server_new) {
1359 ssl_debug_printf("ssl_generate_keyring_material can't init client decoder\n");
1363 ssl_debug_printf("ssl_generate_keyring_material: client seq %d, server seq %d\n",
1364 ssl_session->client_new->seq, ssl_session->server_new->seq);
1365 g_free(key_block.data);
1369 g_free(key_block.data);
1374 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
1376 ssl_debug_printf("ssl_change_cipher %s\n", (server)?"SERVER":"CLIENT");
1378 ssl_session->server = ssl_session->server_new;
1379 ssl_session->server_new = NULL;
1381 ssl_session->client = ssl_session->client_new;
1382 ssl_session->client_new = NULL;
1387 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
1388 StringInfo* entrypted_pre_master, SSL_PRIVATE_KEY *pk)
1392 if(ssl_session->cipher_suite.kex!=KEX_RSA) {
1393 ssl_debug_printf("ssl_decrypt_pre_master_secret key %d diferent from KEX_RSA(%d)\n",
1394 ssl_session->cipher_suite.kex, KEX_RSA);
1398 /* with tls key loading will fail if not rsa type, so no need to check*/
1399 ssl_print_string("pre master encrypted",entrypted_pre_master);
1400 ssl_debug_printf("ssl_decrypt_pre_master_secret:RSA_private_decrypt\n");
1401 i=ssl_private_decrypt(entrypted_pre_master->data_len,
1402 entrypted_pre_master->data, pk);
1405 ssl_debug_printf("ssl_decrypt_pre_master_secret wrong "
1406 "pre_master_secret lenght (%d, expected %d)\n", i, 48);
1410 /* the decrypted data has been written into the pre_master key buffer */
1411 ssl_session->pre_master_secret.data = entrypted_pre_master->data;
1412 ssl_session->pre_master_secret.data_len=48;
1413 ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
1415 /* Remove the master secret if it was there.
1416 This force keying material regeneration in
1417 case we're renegotiating */
1418 ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
1422 /* convert network byte order 32 byte number to right-aligned host byte order *
1424 static gint fmt_seq(guint32 num, guint8* buf)
1429 netnum=g_htonl(num);
1430 memcpy(buf+4,&netnum,4);
1436 tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
1437 guint32 datalen, guint8* mac)
1444 md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
1445 ssl_debug_printf("tls_check_mac mac type:%s md %d\n",
1446 digests[decoder->cipher_suite->dig-0x40], md);
1448 ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md);
1450 /* hash sequence number */
1451 fmt_seq(decoder->seq,buf);
1455 ssl_hmac_update(&hm,buf,8);
1457 /* hash content type */
1459 ssl_hmac_update(&hm,buf,1);
1461 /* hash version,data lenght and data*/
1462 *((gint16*)buf) = g_htons(ver);
1463 ssl_hmac_update(&hm,buf,2);
1465 *((gint16*)buf) = g_htons(datalen);
1466 ssl_hmac_update(&hm,buf,2);
1467 ssl_hmac_update(&hm,data,datalen);
1469 /* get digest and digest len*/
1470 ssl_hmac_final(&hm,buf,&len);
1471 ssl_print_data("Mac", buf, len);
1472 if(memcmp(mac,buf,len))
1475 ssl_hmac_cleanup(&hm);
1480 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
1481 guint32 datalen, guint8* mac)
1486 guint8 buf[64],dgst[20];
1489 pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
1491 /* get cipher used for digest comptuation */
1492 md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
1493 ssl_md_init(&mc,md);
1495 /* do hash computation on data && padding */
1496 ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
1499 memset(buf,0x36,pad_ct);
1500 ssl_md_update(&mc,buf,pad_ct);
1502 /* hash sequence number */
1503 fmt_seq(decoder->seq,buf);
1505 ssl_md_update(&mc,buf,8);
1507 /* hash content type */
1509 ssl_md_update(&mc,buf,1);
1511 /* hash data lenght in network byte order and data*/
1512 *((gint16* )buf) = g_htons(datalen);
1513 ssl_md_update(&mc,buf,2);
1514 ssl_md_update(&mc,data,datalen);
1516 /* get partial digest */
1517 ssl_md_final(&mc,dgst,&len);
1518 ssl_md_cleanup(&mc);
1520 ssl_md_init(&mc,md);
1523 ssl_md_update(&mc,decoder->mac_key.data,decoder->mac_key.data_len);
1525 /* hash padding and partial digest*/
1526 memset(buf,0x5c,pad_ct);
1527 ssl_md_update(&mc,buf,pad_ct);
1528 ssl_md_update(&mc,dgst,len);
1530 ssl_md_final(&mc,dgst,&len);
1531 ssl_md_cleanup(&mc);
1533 if(memcmp(mac,dgst,len))
1541 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
1542 guint32 datalen, guint8* mac)
1549 md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
1550 ssl_debug_printf("dtls_check_mac mac type:%s md %d\n",
1551 digests[decoder->cipher_suite->dig-0x40], md);
1553 ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md);
1554 ssl_debug_printf("dtls_check_mac seq: %d epoch: %d\n",decoder->seq,decoder->epoch);
1555 /* hash sequence number */
1556 fmt_seq(decoder->seq,buf);
1557 buf[0]=decoder->epoch>>8;
1558 buf[1]=decoder->epoch;
1560 ssl_hmac_update(&hm,buf,8);
1562 /* hash content type */
1564 ssl_hmac_update(&hm,buf,1);
1566 /* hash version,data lenght and data */
1567 *((gint16*)buf) = g_htons(ver);
1568 ssl_hmac_update(&hm,buf,2);
1570 *((gint16*)buf) = g_htons(datalen);
1571 ssl_hmac_update(&hm,buf,2);
1572 ssl_hmac_update(&hm,data,datalen);
1573 /* get digest and digest len */
1574 ssl_hmac_final(&hm,buf,&len);
1575 ssl_print_data("Mac", buf, len);
1576 if(memcmp(mac,buf,len))
1579 ssl_hmac_cleanup(&hm);
1586 ssl_decompress_record(SslDecompress* decomp, const guchar* in, guint inl, StringInfo* out_str, guint* outl)
1590 switch (decomp->compression) {
1592 case 1: /* DEFLATE */
1594 if (out_str->data_len < 16384) { /* maximal plain length */
1595 ssl_data_realloc(out_str, 16384);
1597 decomp->istream.next_in = (guchar*)in;
1598 decomp->istream.avail_in = inl;
1599 decomp->istream.next_out = out_str->data;
1600 decomp->istream.avail_out = out_str->data_len;
1602 err = inflate(&decomp->istream, Z_SYNC_FLUSH);
1604 ssl_debug_printf("ssl_decompress_record: inflate() failed - %d\n", err);
1607 *outl = out_str->data_len - decomp->istream.avail_out;
1609 #endif /* HAVE_LIBZ */
1611 ssl_debug_printf("ssl_decompress_record: unsupported compression method %d\n", decomp->compression);
1618 ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
1619 const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl)
1621 guint pad, worklen, uncomplen;
1624 ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
1625 ssl_print_data("Ciphertext",in, inl);
1627 /* ensure we have enough storage space for decrypted data */
1628 if (inl > out_str->data_len)
1630 ssl_debug_printf("ssl_decrypt_record: allocating %d bytes for decrypt data (old len %d)\n",
1631 inl + 32, out_str->data_len);
1632 ssl_data_realloc(out_str, inl + 32);
1636 if ((pad = ssl_cipher_decrypt(&decoder->evp, out_str->data, out_str->data_len, in, inl))!= 0) {
1637 ssl_debug_printf("ssl_decrypt_record failed: ssl_cipher_decrypt: %s %s\n", gcry_strsource (pad),
1638 gcry_strerror (pad));
1642 ssl_print_data("Plaintext", out_str->data, inl);
1645 /* Now strip off the padding*/
1646 if(decoder->cipher_suite->block!=1) {
1647 pad=out_str->data[inl-1];
1649 ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
1654 if (decoder->cipher_suite->dig_len > (gint)worklen)
1656 ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
1659 worklen-=decoder->cipher_suite->dig_len;
1660 mac = out_str->data + worklen;
1662 /* if TLS 1.1 we use the transmitted IV and remove it after (to not modify dissector in others parts)*/
1663 if(ssl->version_netorder==TLSV1DOT1_VERSION){
1664 worklen=worklen-decoder->cipher_suite->block;
1665 memcpy(out_str->data,out_str->data+decoder->cipher_suite->block,worklen);
1667 if(ssl->version_netorder==DTLSV1DOT0_VERSION){
1668 worklen=worklen-decoder->cipher_suite->block;
1669 memcpy(out_str->data,out_str->data+decoder->cipher_suite->block,worklen);
1671 /* Now check the MAC */
1672 ssl_debug_printf("checking mac (len %d, version %X, ct %d seq %d)\n",
1673 worklen, ssl->version_netorder, ct, decoder->seq);
1674 if(ssl->version_netorder==SSLV3_VERSION){
1675 if(ssl3_check_mac(decoder,ct,out_str->data,worklen,mac) < 0) {
1676 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
1680 else if(ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION){
1681 if(tls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)< 0) {
1682 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
1686 else if(ssl->version_netorder==DTLSV1DOT0_VERSION){
1687 /* follow the openssl dtls errors the rigth test is : dtls_check_mac(decoder,ct,ssl->version_netorder,out,worklen,mac)< 0 */
1688 if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)< 0) {
1689 ssl_debug_printf("ssl_decrypt_record: mac failed\n");
1693 ssl_debug_printf("ssl_decrypt_record: mac ok\n");
1696 if (decoder->compression > 0) {
1697 ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
1698 ssl_data_copy(comp_str, out_str);
1699 ssl_print_data("Plaintext compressed", comp_str->data, worklen);
1700 if (!decoder->decomp) {
1701 ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
1704 if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
1705 ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
1713 ssl_get_version(gint* major, gint* minor, gint* patch)
1722 ssl_load_key(FILE* fp)
1724 /* gnutls make our work much harded, since we have to work internally with
1725 * s-exp formatted data, but PEM loader export only in "gnutls_datum"
1726 * format, and a datum -> s-exp convertion function does not exist.
1728 struct gnutls_x509_privkey_int* priv_key;
1730 gnutls_datum m, e, d, p,q, u;
1731 gint size, major, minor, patch;
1735 gcry_mpi_t* rsa_params = g_malloc(sizeof(gcry_mpi_t)*6);
1737 gcry_mpi_t rsa_params[6];
1739 gcry_sexp_t rsa_priv_key;
1741 /* init private key data*/
1742 gnutls_x509_privkey_init(&priv_key);
1744 /* compute file size and load all file contents into a datum buffer*/
1745 if (fseek(fp, 0, SEEK_END) < 0) {
1746 ssl_debug_printf("ssl_load_key: can't fseek file\n");
1749 if ((size = ftell(fp)) < 0) {
1750 ssl_debug_printf("ssl_load_key: can't ftell file\n");
1753 if (fseek(fp, 0, SEEK_SET) < 0) {
1754 ssl_debug_printf("ssl_load_key: can't refseek file\n");
1757 key.data = g_malloc(size);
1759 bytes = fread(key.data, 1, key.size, fp);
1760 if (bytes < key.size) {
1761 ssl_debug_printf("ssl_load_key: can't read from file %d bytes, got %d\n",
1766 /* import PEM data*/
1767 if (gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)!=0) {
1768 ssl_debug_printf("ssl_load_key: can't import pem data\n");
1773 /* RSA get parameter */
1774 if (gnutls_x509_privkey_export_rsa_raw(priv_key, &m, &e, &d, &p, &q, &u) != 0) {
1775 ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
1779 /* convert each rsa parameter to mpi format*/
1780 if (gcry_mpi_scan( &rsa_params[0], GCRYMPI_FMT_USG, m.data, m.size, &tmp_size) !=0) {
1781 ssl_debug_printf("ssl_load_key: can't convert m rsa param to int (size %d)\n", m.size);
1785 if (gcry_mpi_scan( &rsa_params[1], GCRYMPI_FMT_USG, e.data, e.size, &tmp_size) != 0) {
1786 ssl_debug_printf("ssl_load_key: can't convert e rsa param to int (size %d)\n", e.size);
1791 * note: openssl and gnutls use 'p' and 'q' with opposite meaning:
1792 * our 'p' must be equal to 'q' as provided from openssl and viceversa
1794 if (gcry_mpi_scan( &rsa_params[2], GCRYMPI_FMT_USG, d.data, d.size, &tmp_size) !=0) {
1795 ssl_debug_printf("ssl_load_key: can't convert d rsa param to int (size %d)\n", d.size);
1799 if (gcry_mpi_scan( &rsa_params[3], GCRYMPI_FMT_USG, q.data, q.size, &tmp_size) !=0) {
1800 ssl_debug_printf("ssl_load_key: can't convert q rsa param to int (size %d)\n", q.size);
1804 if (gcry_mpi_scan( &rsa_params[4], GCRYMPI_FMT_USG, p.data, p.size, &tmp_size) !=0) {
1805 ssl_debug_printf("ssl_load_key: can't convert p rsa param to int (size %d)\n", p.size);
1809 if (gcry_mpi_scan( &rsa_params[5], GCRYMPI_FMT_USG, u.data, u.size, &tmp_size) !=0) {
1810 ssl_debug_printf("ssl_load_key: can't convert u rsa param to int (size %d)\n", m.size);
1814 ssl_get_version(&major, &minor, &patch);
1816 /* certain versions of gnutls require swap of rsa params 'p' and 'q' */
1817 if ((major <= 1) && (minor <= 0) && (patch <=13))
1820 ssl_debug_printf("ssl_load_key: swapping p and q parametes\n");
1821 tmp = rsa_params[4];
1822 rsa_params[4] = rsa_params[3];
1823 rsa_params[3] = tmp;
1826 if (gcry_sexp_build( &rsa_priv_key, NULL,
1827 "(private-key(rsa((n%m)(e%m)(d%m)(p%m)(q%m)(u%m))))", rsa_params[0],
1828 rsa_params[1], rsa_params[2], rsa_params[3], rsa_params[4],
1829 rsa_params[5]) != 0) {
1830 ssl_debug_printf("ssl_load_key: can't built rsa private key s-exp\n");
1839 for (i=0; i< 6; i++)
1840 gcry_mpi_release(rsa_params[i]);
1842 return rsa_priv_key;
1846 void ssl_free_key(SSL_PRIVATE_KEY* key)
1850 for (i=0; i< 6; i++)
1851 gcry_mpi_release(key[i]);
1853 gcry_sexp_release(key);
1860 const gchar* str = gnutls_check_version(NULL);
1862 /* get library version */
1863 /* old relase of gnutls does not define the appropriate macros, so get
1864 * them from the string*/
1865 ssl_debug_printf("gnutls version: %s\n", str);
1866 sscanf(str, "%d.%d.%d", &ver_major, &ver_minor, &ver_patch);
1869 #else /* HAVE_LIBGNUTLS */
1870 /* no libgnutl: dummy operation to keep interface consistent*/
1877 ssl_load_key(FILE* fp)
1879 ssl_debug_printf("ssl_load_key: impossible without glutls. fp %p\n",fp);
1884 ssl_free_key(SSL_PRIVATE_KEY* key _U_)
1889 ssl_find_cipher(int num,SslCipherSuite* cs)
1891 ssl_debug_printf("ssl_find_cipher: dummy without glutls. num %d cs %p\n",
1896 ssl_generate_keyring_material(SslDecryptSession*ssl)
1898 ssl_debug_printf("ssl_generate_keyring_material: impossible without glutls. ssl %p\n",
1903 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server)
1905 ssl_debug_printf("ssl_change_cipher %s: makes no sence without gnutls. ssl %p\n", (server)?"SERVER":"CLIENT", ssl_session);
1909 ssl_decrypt_pre_master_secret(SslDecryptSession* ssl_session,
1910 StringInfo* entrypted_pre_master, SSL_PRIVATE_KEY *pk)
1912 ssl_debug_printf("ssl_decrypt_pre_master_secret: impossible without glutls."
1913 " ssl %p entrypted_pre_master %p pk %p\n", ssl_session,
1914 entrypted_pre_master, pk);
1919 ssl_decrypt_record(SslDecryptSession*ssl, SslDecoder* decoder, gint ct,
1920 const guchar* in, guint inl, StringInfo* comp_str _U_, StringInfo* out, guint* outl)
1922 ssl_debug_printf("ssl_decrypt_record: impossible without gnutls. ssl %p"
1923 "decoder %p ct %d, in %p inl %d out %p outl %p\n", ssl, decoder, ct,
1924 in, inl, out, outl);
1929 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher _U_, guchar* iv _U_, gint iv_len _U_)
1931 ssl_debug_printf("ssl_cipher_setiv: impossible without glutls.\n");
1935 #endif /* HAVE_LIBGNUTLS */
1937 /* get ssl data for this session. if no ssl data is found allocate a new one*/
1939 ssl_session_init(SslDecryptSession* ssl_session)
1941 ssl_debug_printf("ssl_session_init: initializing ptr %p size %lu\n",
1942 ssl_session, (gulong)sizeof(SslDecryptSession));
1944 ssl_session->master_secret.data = ssl_session->_master_secret;
1945 ssl_session->session_id.data = ssl_session->_session_id;
1946 ssl_session->client_random.data = ssl_session->_client_random;
1947 ssl_session->server_random.data = ssl_session->_server_random;
1948 ssl_session->master_secret.data_len = 48;
1949 ssl_session->server_data_for_iv.data = 0;
1950 ssl_session->server_data_for_iv.data = ssl_session->_server_data_for_iv;
1951 ssl_session->client_data_for_iv.data = 0;
1952 ssl_session->client_data_for_iv.data = ssl_session->_client_data_for_iv;
1953 ssl_session->app_data_segment.data=NULL;
1954 ssl_session->app_data_segment.data_len=0;
1957 /* Hash Functions for TLS/DTLS sessions table and private keys table*/
1959 ssl_equal (gconstpointer v, gconstpointer v2)
1961 const StringInfo *val1;
1962 const StringInfo *val2;
1963 val1 = (const StringInfo *)v;
1964 val2 = (const StringInfo *)v2;
1966 if (val1->data_len == val2->data_len &&
1967 !memcmp(val1->data, val2->data, val2->data_len)) {
1974 ssl_hash (gconstpointer v)
1977 const StringInfo* id;
1980 id = (const StringInfo*) v;
1981 cur = (const guint*) id->data;
1983 for (l=4; (l<id->data_len); l+=4, cur++)
1984 hash = hash ^ (*cur);
1990 ssl_private_key_equal (gconstpointer v, gconstpointer v2)
1992 const SslService *val1;
1993 const SslService *val2;
1994 val1 = (const SslService *)v;
1995 val2 = (const SslService *)v2;
1997 if ((val1->port == val2->port) &&
1998 ! CMP_ADDRESS(&val1->addr, &val2->addr)) {
2005 ssl_private_key_hash (gconstpointer v)
2007 const SslService *key;
2008 guint l, hash, len ;
2010 key = (const SslService *)v;
2012 len = key->addr.len;
2013 cur = (const guint*) key->addr.data;
2015 for (l=4; (l<len); l+=4, cur++)
2016 hash = hash ^ (*cur);
2021 /* private key table entries have a scope 'larger' then packet capture,
2022 * so we can't relay on se_alloc** function */
2024 ssl_private_key_free(gpointer id, gpointer key, gpointer dummy _U_)
2027 ssl_free_key((SSL_PRIVATE_KEY*) key);
2030 /* handling of association between tls/dtls ports and clear text protocol */
2032 ssl_association_add(GTree* associations, dissector_handle_t handle, guint port, const gchar *protocol, gboolean tcp, gboolean from_key_list)
2035 SslAssociation* assoc;
2036 assoc = g_malloc(sizeof(SslAssociation));
2039 assoc->ssl_port = port;
2040 assoc->info=g_malloc(strlen(protocol)+1);
2041 strcpy(assoc->info, protocol);
2042 assoc->handle = find_dissector(protocol);
2043 assoc->from_key_list = from_key_list;
2045 ssl_debug_printf("association_add %s port %d protocol %s handle %p\n",
2046 (assoc->tcp)?"TCP":"UDP", port, protocol, assoc->handle);
2049 if (!assoc->handle) {
2050 ssl_debug_printf("association_add could not find handle for protocol '%s', try to find 'data' dissector\n", protocol);
2051 assoc->handle = find_dissector("data");
2055 fprintf(stderr, "association_add() could not find handle for protocol:%s\n",protocol);
2058 dissector_add("tcp.port", port, handle);
2060 dissector_add("udp.port", port, handle);
2061 g_tree_insert(associations, assoc, assoc);
2066 ssl_association_remove(GTree* associations, SslAssociation *assoc)
2068 ssl_debug_printf("ssl_association_remove removing %s %u - %s handle %p\n",
2069 (assoc->tcp)?"TCP":"UDP", assoc->ssl_port, assoc->info, assoc->handle);
2071 dissector_delete((assoc->tcp)?"tcp.port":"udp.port", assoc->ssl_port, assoc->handle);
2073 g_tree_remove(associations, assoc);
2078 ssl_association_cmp(gconstpointer a, gconstpointer b)
2080 const SslAssociation *assoc_a=a, *assoc_b=b;
2081 if (assoc_a->tcp != assoc_b->tcp) return (assoc_a->tcp)?1:-1;
2082 return assoc_a->ssl_port - assoc_b->ssl_port;
2086 ssl_association_find(GTree * associations, guint port, gboolean tcp)
2088 register SslAssociation* ret;
2089 SslAssociation assoc_tmp;
2091 assoc_tmp.tcp = tcp;
2092 assoc_tmp.ssl_port = port;
2093 ret = g_tree_lookup(associations, &assoc_tmp);
2095 ssl_debug_printf("association_find: %s port %d found %p\n", (tcp)?"TCP":"UDP", port, ret);
2100 ssl_assoc_from_key_list(gpointer key _U_, gpointer data, gpointer user_data)
2102 if (((SslAssociation*)data)->from_key_list)
2103 ep_stack_push((ep_stack_t)user_data, data);
2108 ssl_packet_from_server(GTree* associations, guint port, gboolean tcp)
2111 ret = ssl_association_find(associations, port, tcp) != 0;
2113 ssl_debug_printf("packet_from_server: is from server - %s\n", (ret)?"TRUE":"FALSE");
2117 /* add to packet data a newly allocated tvb with the specified real data*/
2119 ssl_add_record_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint record_id)
2124 real_data = se_alloc(data_len);
2125 rec = se_alloc(sizeof(SslRecordInfo));
2126 pi = p_get_proto_data(pinfo->fd, proto);
2130 pi = se_alloc0(sizeof(SslPacketInfo));
2131 p_add_proto_data(pinfo->fd, proto,pi);
2134 rec->id = record_id;
2135 rec->tvb = tvb_new_real_data(real_data, data_len, data_len);
2136 memcpy(real_data, data, data_len);
2138 /* head insertion */
2139 rec->next= pi->handshake_data;
2140 pi->handshake_data = rec;
2144 /* search in packet data the tvbuff associated to the specified id */
2146 ssl_get_record_info(int proto, packet_info *pinfo, gint record_id)
2150 pi = p_get_proto_data(pinfo->fd, proto);
2155 for (rec = pi->handshake_data; rec; rec = rec->next)
2156 if (rec->id == record_id)
2163 ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint key, SslFlow *flow)
2165 SslDataInfo *rec, **prec;
2168 pi = p_get_proto_data(pinfo->fd, proto);
2171 pi = se_alloc0(sizeof(SslPacketInfo));
2172 p_add_proto_data(pinfo->fd, proto,pi);
2175 rec = se_alloc(sizeof(SslDataInfo)+data_len);
2177 rec->plain_data.data = (guchar*)(rec + 1);
2178 memcpy(rec->plain_data.data, data, data_len);
2179 rec->plain_data.data_len = data_len;
2180 rec->seq = flow->byte_seq;
2181 rec->nxtseq = flow->byte_seq + data_len;
2185 flow->byte_seq += data_len;
2188 prec = &pi->appl_data;
2189 while (*prec) prec = &(*prec)->next;
2192 ssl_debug_printf("ssl_add_data_info: new data inserted data_len = %d, seq = %u, nxtseq = %u\n",
2193 rec->plain_data.data_len, rec->seq, rec->nxtseq);
2197 ssl_get_data_info(int proto, packet_info *pinfo, gint key)
2201 pi = p_get_proto_data(pinfo->fd, proto);
2203 if (!pi) return NULL;
2205 rec = pi->appl_data;
2207 if (rec->key == key) return rec;
2214 /* initialize/reset per capture state data (ssl sessions cache) */
2216 ssl_common_init(GHashTable **session_hash, StringInfo *decrypted_data, StringInfo *compressed_data)
2219 g_hash_table_destroy(*session_hash);
2220 *session_hash = g_hash_table_new(ssl_hash, ssl_equal);
2222 if (decrypted_data->data)
2223 g_free(decrypted_data->data);
2224 ssl_data_alloc(decrypted_data, 32);
2226 if (compressed_data->data)
2227 g_free(compressed_data->data);
2228 ssl_data_alloc(compressed_data, 32);
2231 /* parse ssl related preferences (private keys and ports association strings) */
2233 ssl_parse_key_list(const gchar * keys_list, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp)
2239 SslService* service;
2240 SSL_PRIVATE_KEY * private_key;
2243 start = strdup(keys_list);
2245 ssl_debug_printf("ssl_init keys string:\n%s\n", start);
2247 gchar* addr, *port, *protocol, *filename;
2250 /* split ip/file couple with ';' separator*/
2251 end = strpbrk(start, ";\n\r");
2257 /* skip comments (in file) */
2258 if (addr[0] == '#') continue;
2260 /* for each entry split ip, port, protocol, filename with ',' separator */
2261 ssl_debug_printf("ssl_init found host entry %s\n", addr);
2262 port = strchr(addr, ',');
2265 ssl_debug_printf("ssl_init entry malformed can't find port in '%s'\n", addr);
2271 protocol = strchr(port,',');
2274 ssl_debug_printf("ssl_init entry malformed can't find protocol in %s\n", port);
2280 filename = strchr(protocol,',');
2283 ssl_debug_printf("ssl_init entry malformed can't find filename in %s\n", port);
2289 /* convert ip and port string to network rappresentation*/
2290 service = g_malloc(sizeof(SslService) + 4);
2291 service->addr.type = AT_IPv4;
2292 service->addr.len = 4;
2293 service->addr.data = ip = ((guchar*)service) + sizeof(SslService);
2294 sscanf(addr, "%hhu.%hhu.%hhu.%hhu", &ip[0], &ip[1], &ip[2], &ip[3]);
2295 service->port = atoi(port);
2296 ssl_debug_printf("ssl_init addr %hhu.%hhu.%hhu.%hhu port %d filename %s\n",
2297 ip[0], ip[1], ip[2], ip[3], service->port, filename);
2299 /* try to load pen file*/
2300 fp = fopen(filename, "rb");
2302 fprintf(stderr, "can't open file %s \n",filename);
2306 private_key = ssl_load_key(fp);
2308 fprintf(stderr,"can't load private key from %s\n",
2314 ssl_debug_printf("ssl_init private key file %s successfully loaded\n",
2316 g_hash_table_insert(key_hash, service, private_key);
2318 ssl_association_add(associations, handle, atoi(port), protocol, tcp, TRUE);
2320 } while (end != NULL);
2324 /* store master secret into session data cache */
2326 ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash)
2328 /* allocate stringinfo chunks for session id and master secret data*/
2329 StringInfo* session_id;
2330 StringInfo* master_secret;
2331 session_id = se_alloc0(sizeof(StringInfo) + ssl->session_id.data_len);
2332 master_secret = se_alloc0(48 + sizeof(StringInfo));
2334 master_secret->data = ((guchar*)master_secret+sizeof(StringInfo));
2335 session_id->data = ((guchar*)session_id+sizeof(StringInfo));
2337 ssl_data_set(session_id, ssl->session_id.data, ssl->session_id.data_len);
2338 ssl_data_set(master_secret, ssl->master_secret.data, ssl->master_secret.data_len);
2339 g_hash_table_insert(session_hash, session_id, master_secret);
2340 ssl_print_string("ssl_save_session stored session id", session_id);
2341 ssl_print_string("ssl_save_session stored master secret", master_secret);
2345 ssl_restore_session(SslDecryptSession* ssl, GHashTable *session_hash)
2348 ms = g_hash_table_lookup(session_hash, &ssl->session_id);
2351 ssl_debug_printf("ssl_restore_session can't find stored session\n");
2354 ssl_data_set(&ssl->master_secret, ms->data, ms->data_len);
2355 ssl->state |= SSL_MASTER_SECRET;
2356 ssl_debug_printf("ssl_restore_session master key retrived\n");
2360 ssl_is_valid_content_type(guint8 type)
2362 if (type >= 0x14 && type <= 0x17)
2370 #ifdef SSL_DECRYPT_DEBUG
2372 static FILE* ssl_debug_file=NULL;
2375 ssl_set_debug(char* name)
2377 static gint debug_file_must_be_closed;
2379 debug_file_must_be_closed = 0;
2380 use_stderr = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
2382 if (debug_file_must_be_closed)
2383 fclose(ssl_debug_file);
2385 ssl_debug_file = stderr;
2386 else if (!name || (strcmp(name, "") ==0))
2387 ssl_debug_file = NULL;
2389 ssl_debug_file = fopen(name, "w");
2390 if (!use_stderr && ssl_debug_file)
2391 debug_file_must_be_closed = 1;
2396 ssl_debug_printf(const gchar* fmt, ...)
2402 if (!ssl_debug_file)
2406 ret += vfprintf(ssl_debug_file, fmt, ap);
2408 fflush(ssl_debug_file);
2412 ssl_print_text_data(const gchar* name, const guchar* data, gint len)
2415 if (!ssl_debug_file)
2417 fprintf(ssl_debug_file,"%s: ",name);
2418 for (i=0; i< len; i++) {
2419 fprintf(ssl_debug_file,"%c",data[i]);
2421 fprintf(ssl_debug_file,"\n");
2422 fflush(ssl_debug_file);
2426 ssl_print_data(const gchar* name, const guchar* data, gint len)
2429 if (!ssl_debug_file)
2431 fprintf(ssl_debug_file,"%s[%d]:\n",name, len);
2432 for (i=0; i< len; i++) {
2433 if ((i>0) && (i%16 == 0))
2434 fprintf(ssl_debug_file,"\n");
2435 fprintf(ssl_debug_file,"%.2x ",data[i]&255);
2437 fprintf(ssl_debug_file,"\n");
2438 fflush(ssl_debug_file);
2442 ssl_print_string(const gchar* name, const StringInfo* data)
2444 ssl_print_data(name, data->data, data->data_len);
2446 #endif /* SSL_DECRYPT_DEBUG */