Update Free Software Foundation address.
[metze/wireshark/wip.git] / epan / dissectors / packet-ssl-utils.h
1 /* packet-ssl-utils.h
2  * ssl manipulation functions
3  * By Paolo Abeni <paolo.abeni@email.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifndef __SSL_UTILS_H_
27 #define __SSL_UTILS_H_
28
29 #include <stdio.h>      /* some APIs we declare take a stdio stream as an argument */
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/emem.h>
34
35 #ifdef HAVE_LIBGNUTLS
36 #include <gcrypt.h>
37 #include <gnutls/x509.h>
38 #include <gnutls/pkcs12.h>
39
40 #include <epan/conversation.h>
41
42 /* #define SSL_FAST 1 */
43 #define SSL_DECRYPT_DEBUG
44
45 #define SSL_CIPHER_CTX gcry_cipher_hd_t
46 #define SSL_PSK_KEY guchar
47 #ifdef SSL_FAST
48 #define SSL_PRIVATE_KEY gcry_mpi_t
49 #else /* SSL_FAST */
50 #define SSL_PRIVATE_KEY struct gcry_sexp
51 #endif /* SSL_FAST */
52 #else  /* HAVE_LIBGNUTLS */
53 #define SSL_CIPHER_CTX void*
54 #define SSL_PRIVATE_KEY void
55 #define SSL_PSK_KEY void
56 #endif /* HAVE_LIBGNUTLS */
57
58
59 /* version state tables */
60 #define SSL_VER_UNKNOWN                   0
61 #define SSL_VER_SSLv2                     1
62 #define SSL_VER_SSLv3                     2
63 #define SSL_VER_TLS                       3
64 #define SSL_VER_TLSv1DOT1                 4
65 #define SSL_VER_DTLS                      5
66 #define SSL_VER_DTLS1DOT2                 8
67 #define SSL_VER_PCT                       6
68 #define SSL_VER_TLSv1DOT2                 7
69
70 /* other defines */
71 #define SSL_ID_CHG_CIPHER_SPEC         0x14
72 #define SSL_ID_ALERT                   0x15
73 #define SSL_ID_HANDSHAKE               0x16
74 #define SSL_ID_APP_DATA                0x17
75 #define SSL_ID_HEARTBEAT               0x18
76
77 #define SSL_HND_HELLO_REQUEST          0
78 #define SSL_HND_CLIENT_HELLO           1
79 #define SSL_HND_SERVER_HELLO           2
80 #define SSL_HND_HELLO_VERIFY_REQUEST   3
81 #define SSL_HND_NEWSESSION_TICKET      4
82 #define SSL_HND_CERTIFICATE            11
83 #define SSL_HND_SERVER_KEY_EXCHG       12
84 #define SSL_HND_CERT_REQUEST           13
85 #define SSL_HND_SVR_HELLO_DONE         14
86 #define SSL_HND_CERT_VERIFY            15
87 #define SSL_HND_CLIENT_KEY_EXCHG       16
88 #define SSL_HND_FINISHED               20
89 #define SSL_HND_CERT_STATUS            22
90
91 #define SSL2_HND_ERROR                 0x00
92 #define SSL2_HND_CLIENT_HELLO          0x01
93 #define SSL2_HND_CLIENT_MASTER_KEY     0x02
94 #define SSL2_HND_CLIENT_FINISHED       0x03
95 #define SSL2_HND_SERVER_HELLO          0x04
96 #define SSL2_HND_SERVER_VERIFY         0x05
97 #define SSL2_HND_SERVER_FINISHED       0x06
98 #define SSL2_HND_REQUEST_CERTIFICATE   0x07
99 #define SSL2_HND_CLIENT_CERTIFICATE    0x08
100
101 #define PCT_VERSION_1                  0x8001
102
103 #define PCT_MSG_CLIENT_HELLO           0x01
104 #define PCT_MSG_SERVER_HELLO           0x02
105 #define PCT_MSG_CLIENT_MASTER_KEY      0x03
106 #define PCT_MSG_SERVER_VERIFY          0x04
107 #define PCT_MSG_ERROR                  0x05
108
109 #define PCT_CH_OFFSET_V1               0xa
110
111 #define PCT_CIPHER_DES                 0x01
112 #define PCT_CIPHER_IDEA                0x02
113 #define PCT_CIPHER_RC2                 0x03
114 #define PCT_CIPHER_RC4                 0x04
115 #define PCT_CIPHER_DES_112             0x05
116 #define PCT_CIPHER_DES_168             0x06
117
118 #define PCT_HASH_MD5                   0x0001
119 #define PCT_HASH_MD5_TRUNC_64          0x0002
120 #define PCT_HASH_SHA                   0x0003
121 #define PCT_HASH_SHA_TRUNC_80          0x0004
122 #define PCT_HASH_DES_DM                0x0005
123
124 #define PCT_CERT_NONE                  0x00
125 #define PCT_CERT_X509                  0x01
126 #define PCT_CERT_PKCS7                 0x02
127
128 #define PCT_SIG_NONE                   0x0000
129 #define PCT_SIG_RSA_MD5                0x0001
130 #define PCT_SIG_RSA_SHA                0x0002
131 #define PCT_SIG_DSA_SHA                0x0003
132
133 #define PCT_EXCH_RSA_PKCS1             0x01
134 #define PCT_EXCH_RSA_PKCS1_TOKEN_DES   0x02
135 #define PCT_EXCH_RSA_PKCS1_TOKEN_DES3  0x03
136 #define PCT_EXCH_RSA_PKCS1_TOKEN_RC2   0x04
137 #define PCT_EXCH_RSA_PKCS1_TOKEN_RC4   0x05
138 #define PCT_EXCH_DH_PKCS3              0x06
139 #define PCT_EXCH_DH_PKCS3_TOKEN_DES    0x07
140 #define PCT_EXCH_DH_PKCS3_TOKEN_DES3   0x08
141 #define PCT_EXCH_FORTEZZA_TOKEN        0x09
142
143 #define PCT_ERR_BAD_CERTIFICATE        0x01
144 #define PCT_ERR_CLIENT_AUTH_FAILED     0x02
145 #define PCT_ERR_ILLEGAL_MESSAGE        0x03
146 #define PCT_ERR_INTEGRITY_CHECK_FAILED 0x04
147 #define PCT_ERR_SERVER_AUTH_FAILED     0x05
148 #define PCT_ERR_SPECS_MISMATCH         0x06
149
150 #define SSL_HND_HELLO_EXT_SERVER_NAME        0x0
151 #define SSL_HND_HELLO_EXT_ELLIPTIC_CURVES    0x000a
152 #define SSL_HND_HELLO_EXT_EC_POINT_FORMATS   0x000b
153 #define SSL_HND_HELLO_EXT_HEARTBEAT          0x000f
154 #define SSL_HND_HELLO_EXT_RENEG_INFO         0xff01
155 #define SSL_HND_HELLO_EXT_NPN                0x3374
156 #define SSL_HND_CERT_STATUS_TYPE_OCSP  1
157
158 /*
159  * Lookup tables
160  */
161 extern const value_string ssl_version_short_names[];
162 extern const value_string ssl_20_msg_types[];
163 extern value_string_ext ssl_20_cipher_suites_ext;
164 extern const value_string ssl_20_certificate_type[];
165 extern const value_string ssl_31_content_type[];
166 extern const value_string ssl_versions[];
167 extern const value_string ssl_31_change_cipher_spec[];
168 extern const value_string ssl_31_alert_level[];
169 extern const value_string ssl_31_alert_description[];
170 extern const value_string ssl_31_handshake_type[];
171 extern const value_string tls_heartbeat_type[];
172 extern const value_string tls_heartbeat_mode[];
173 extern const value_string ssl_31_compression_method[];
174 extern const value_string ssl_31_key_exchange_algorithm[];
175 extern const value_string ssl_31_signature_algorithm[];
176 extern const value_string ssl_31_client_certificate_type[];
177 extern const value_string ssl_31_public_value_encoding[];
178 extern value_string_ext ssl_31_ciphersuite_ext;
179 extern const value_string pct_msg_types[];
180 extern const value_string pct_cipher_type[];
181 extern const value_string pct_hash_type[];
182 extern const value_string pct_cert_type[];
183 extern const value_string pct_sig_type[];
184 extern const value_string pct_exch_type[];
185 extern const value_string pct_error_code[];
186 extern const value_string tls_hello_extension_types[];
187 extern const value_string tls_hash_algorithm[];
188 extern const value_string tls_signature_algorithm[];
189 extern const value_string tls_certificate_type[];
190 extern const value_string tls_cert_status_type[];
191 extern const value_string ssl_extension_curves[];
192 extern const value_string ssl_extension_ec_point_formats[];
193 extern const value_string ssl_curve_types[];
194 extern const value_string tls_hello_ext_server_name_type_vs[];
195
196 /* XXX Should we use GByteArray instead? */
197 typedef struct _StringInfo {
198     guchar* data;
199     guint data_len;
200 } StringInfo;
201
202 #define SSL_WRITE_KEY           1
203
204 #define SSLV3_VERSION          0x300
205 #define TLSV1_VERSION          0x301
206 #define TLSV1DOT1_VERSION      0x302
207 #define TLSV1DOT2_VERSION      0x303
208 #define DTLSV1DOT0_VERSION     0xfeff
209 #define DTLSV1DOT0_VERSION_NOT 0x100
210 #define DTLSV1DOT2_VERSION     0xfefd
211
212 #define SSL_CLIENT_RANDOM       (1<<0)
213 #define SSL_SERVER_RANDOM       (1<<1)
214 #define SSL_CIPHER              (1<<2)
215 #define SSL_HAVE_SESSION_KEY    (1<<3)
216 #define SSL_VERSION             (1<<4)
217 #define SSL_MASTER_SECRET       (1<<5)
218 #define SSL_PRE_MASTER_SECRET   (1<<6)
219
220 #define SSL_CIPHER_MODE_STREAM  0
221 #define SSL_CIPHER_MODE_CBC     1
222
223 #define SSL_DEBUG_USE_STDERR "-"
224
225 #define SSLV2_MAX_SESSION_ID_LENGTH_IN_BYTES 16
226
227 typedef struct _SslCipherSuite {
228     gint number;
229     gint kex;
230     gint sig;
231     gint enc;
232     gint block;
233     gint bits;
234     gint eff_bits;
235     gint dig;
236     gint dig_len;
237     gint export;
238     gint mode;
239 } SslCipherSuite;
240
241 typedef struct _SslFlow {
242     guint32 byte_seq;
243     guint16 flags;
244     emem_tree_t *multisegment_pdus;
245 } SslFlow;
246
247 typedef struct _SslDecompress SslDecompress;
248
249 typedef struct _SslDecoder {
250     SslCipherSuite* cipher_suite;
251     gint compression;
252     guchar _mac_key[48];
253     StringInfo mac_key;
254     SSL_CIPHER_CTX evp;
255     SslDecompress *decomp;
256     guint32 seq;
257     guint16 epoch;
258     SslFlow *flow;
259 } SslDecoder;
260
261 #define KEX_RSA         0x10
262 #define KEX_DH          0x11
263 #define KEX_PSK         0x12
264 #define KEX_ECDH        0x13
265
266 #define SIG_RSA         0x20
267 #define SIG_DSS         0x21
268 #define SIG_NONE        0x22
269
270 #define ENC_DES         0x30
271 #define ENC_3DES        0x31
272 #define ENC_RC4         0x32
273 #define ENC_RC2         0x33
274 #define ENC_IDEA        0x34
275 #define ENC_AES         0x35
276 #define ENC_AES256      0x36
277 #define ENC_NULL        0x37
278
279 #define DIG_MD5         0x40
280 #define DIG_SHA         0x41
281 #define DIG_SHA256      0x42
282 #define DIG_SHA384      0x43
283
284 typedef struct _SslRecordInfo {
285     guchar *real_data;
286     gint data_len;
287     gint id;
288     struct _SslRecordInfo* next;
289 } SslRecordInfo;
290
291 typedef struct _SslDataInfo {
292     gint key;
293     StringInfo plain_data;
294     guint32 seq;
295     guint32 nxtseq;
296     SslFlow *flow;
297     struct _SslDataInfo *next;
298 } SslDataInfo;
299
300 typedef struct {
301     SslDataInfo *appl_data;
302     SslRecordInfo* handshake_data;
303 } SslPacketInfo;
304
305 typedef struct _SslDecryptSession {
306     guchar _master_secret[48];
307     guchar _session_id[256];
308     guchar _client_random[32];
309     guchar _server_random[32];
310     StringInfo session_id;
311     StringInfo server_random;
312     StringInfo client_random;
313     StringInfo master_secret;
314     /* the data store for this StringInfo must be allocated explicitly with a capture lifetime scope */
315     StringInfo pre_master_secret;
316     guchar _server_data_for_iv[24];
317     StringInfo server_data_for_iv;
318     guchar _client_data_for_iv[24];
319     StringInfo client_data_for_iv;
320
321     gint cipher;
322     gint compression;
323     gint state;
324     SslCipherSuite cipher_suite;
325     SslDecoder *server;
326     SslDecoder *client;
327     SslDecoder *server_new;
328     SslDecoder *client_new;
329     SSL_PRIVATE_KEY* private_key;
330     SSL_PSK_KEY* psk;
331     guint32 version;
332     guint16 version_netorder;
333     StringInfo app_data_segment;
334
335     address srv_addr;
336     port_type srv_ptype;
337     guint srv_port;
338
339 } SslDecryptSession;
340
341 typedef struct _SslAssociation {
342     gboolean tcp;
343     guint ssl_port;
344     dissector_handle_t handle;
345     gchar* info;
346     gboolean from_key_list;
347 } SslAssociation;
348
349 typedef struct _SslService {
350     address addr;
351     guint port;
352 } SslService;
353
354 typedef struct _Ssl_private_key {
355 #ifdef HAVE_LIBGNUTLS
356     gnutls_x509_crt_t     x509_cert;
357     gnutls_x509_privkey_t x509_pkey;
358 #endif
359     SSL_PRIVATE_KEY       *sexp_pkey;
360 } Ssl_private_key_t;
361
362 /* User Access Table */
363 typedef struct _ssldecrypt_assoc_t {
364     char* ipaddr;
365     char* port;
366     char* protocol;
367     char* keyfile;
368     char* password;
369 } ssldecrypt_assoc_t;
370
371 gint ssl_get_keyex_alg(gint cipher);
372
373 gboolean ssldecrypt_uat_fld_ip_chk_cb(void*, const char*, unsigned, const void*, const void*, const char** err);
374 gboolean ssldecrypt_uat_fld_port_chk_cb(void*, const char*, unsigned, const void*, const void*, const char** err);
375 gboolean ssldecrypt_uat_fld_protocol_chk_cb(void*, const char*, unsigned, const void*, const void*, const char** err);
376 gboolean ssldecrypt_uat_fld_fileopen_chk_cb(void*, const char*, unsigned, const void*, const void*, const char** err);
377 gboolean ssldecrypt_uat_fld_password_chk_cb(void*, const char*, unsigned, const void*, const void*, const char** err);
378
379 /** Initialize decryption engine/ssl layer. To be called once per execution */
380 extern void
381 ssl_lib_init(void);
382
383 /** Initialize an ssl session struct
384  @param ssl pointer to ssl session struct to be initialized */
385 extern void
386 ssl_session_init(SslDecryptSession* ssl);
387
388 /** Set server address and port */
389 extern void
390 ssl_set_server(SslDecryptSession* ssl, address *addr, port_type ptype, guint32 port);
391
392 /** set the data and len for the stringInfo buffer. buf should be big enough to
393  * contain the provided data
394  @param buf the buffer to update
395  @param src the data source
396  @param len the source data len */
397 extern void
398 ssl_data_set(StringInfo* buf, const guchar* src, guint len);
399
400 extern gint
401 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len);
402
403 /** Load an RSA private key from specified file
404  @param fp the file that contain the key data
405  @return a pointer to the loaded key on success, or NULL */
406 extern Ssl_private_key_t *
407 ssl_load_key(FILE* fp);
408
409 extern Ssl_private_key_t *
410 ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd);
411
412 /** Deallocate the memory used for specified key
413  @param key pointer to the key to be freed */
414 extern void
415 ssl_free_key(Ssl_private_key_t* key);
416
417 /* Find private key in associations */
418 extern gint
419 ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree* associations, packet_info *pinfo);
420
421 /** Search for the specified cipher suite id
422  @param num the id of the cipher suite to be searched
423  @param cs pointer to the cipher suite struct to be filled
424  @return 0 if the cipher suite is found, -1 elsewhere */
425 extern gint
426 ssl_find_cipher(int num,SslCipherSuite* cs);
427
428 /** Expand the pre_master_secret to generate all the session information
429  * (master secret, session keys, ivs)
430  @param ssl_session the store for all the session data
431  @return 0 on success */
432 extern gint
433 ssl_generate_keyring_material(SslDecryptSession*ssl_session);
434
435 extern void
436 ssl_change_cipher(SslDecryptSession *ssl_session, gboolean server);
437
438 /** Try to find the pre-master secret for the given encrypted pre-master secret
439     from a log of secrets.
440  @param ssl_session the store for the decrypted pre_master_secret
441  @param ssl_keylog_filename a file that contains a log of secrets (may be NULL)
442  @param encrypted_pre_master the rsa encrypted pre_master_secret (may be NULL)
443  @return 0 on success */
444 int
445 ssl_keylog_lookup(SslDecryptSession* ssl_session,
446                   const gchar* ssl_keylog_filename,
447                   StringInfo* encrypted_pre_master);
448
449 /** Try to decrypt in place the encrypted pre_master_secret
450  @param ssl_session the store for the decrypted pre_master_secret
451  @param encrypted_pre_master the rsa encrypted pre_master_secret
452  @param pk the private key to be used for decryption
453  @return 0 on success */
454 extern gint
455 ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
456     StringInfo* encrypted_pre_master, SSL_PRIVATE_KEY *pk);
457
458 /** Try to decrypt an ssl record
459  @param ssl ssl_session the store all the session data
460  @param decoder the stream decoder to be used
461  @param ct the content type of this ssl record
462  @param in a pinter to the ssl record to be decrypted
463  @param inl the record length
464  @param comp_str
465  @param out_str a pointer to the store for the decrypted data
466  @param outl the decrypted data len
467  @return 0 on success */
468 extern gint
469 ssl_decrypt_record(SslDecryptSession* ssl,SslDecoder* decoder, gint ct,
470         const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl);
471
472
473 /* Common part bitween SSL and DTLS dissectors */
474 /* Hash Functions for TLS/DTLS sessions table and private keys table */
475 extern gint
476 ssl_equal (gconstpointer v, gconstpointer v2);
477
478 extern guint
479 ssl_hash  (gconstpointer v);
480
481 extern gint
482 ssl_private_key_equal (gconstpointer v, gconstpointer v2);
483
484 extern guint
485 ssl_private_key_hash  (gconstpointer v);
486
487 /* private key table entries have a scope 'larger' then packet capture,
488  * so we can't relay on se_alloc** function */
489 extern void
490 ssl_private_key_free(gpointer id, gpointer key, gpointer dummy _U_);
491
492 /* handling of association between tls/dtls ports and clear text protocol */
493 extern void
494 ssl_association_add(GTree* associations, dissector_handle_t handle, guint port, const gchar *protocol, gboolean tcp, gboolean from_key_list);
495
496 extern void
497 ssl_association_remove(GTree* associations, SslAssociation *assoc);
498
499 extern gint
500 ssl_association_cmp(gconstpointer a, gconstpointer b);
501
502 extern SslAssociation*
503 ssl_association_find(GTree * associations, guint port, gboolean tcp);
504
505 extern gint
506 ssl_assoc_from_key_list(gpointer key _U_, gpointer data, gpointer user_data);
507
508 extern gint
509 ssl_packet_from_server(SslDecryptSession* ssl, GTree* associations, packet_info *pinfo);
510
511 /* add to packet data a copy of the specified real data */
512 extern void
513 ssl_add_record_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint record_id);
514
515 /* search in packet data for the specified id; return a newly created tvb for the associated data */
516 extern tvbuff_t*
517 ssl_get_record_info(tvbuff_t *parent_tvb, gint proto, packet_info *pinfo, gint record_id);
518
519 void
520 ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint key, SslFlow *flow);
521
522 SslDataInfo*
523 ssl_get_data_info(int proto, packet_info *pinfo, gint key);
524
525 /* initialize/reset per capture state data (ssl sessions cache) */
526 extern void
527 ssl_common_init(GHashTable **session_hash, StringInfo *decrypted_data, StringInfo *compressed_data);
528
529 /* parse ssl related preferences (private keys and ports association strings) */
530 extern void
531 ssl_parse_key_list(const ssldecrypt_assoc_t * uats, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp);
532
533 /* store master secret into session data cache */
534 extern void
535 ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash);
536
537 extern gboolean
538 ssl_restore_session(SslDecryptSession* ssl, GHashTable *session_hash);
539
540 extern gint
541 ssl_is_valid_content_type(guint8 type);
542
543 #ifdef SSL_DECRYPT_DEBUG
544 extern void
545 ssl_debug_printf(const gchar* fmt,...) G_GNUC_PRINTF(1,2);
546 extern void
547 ssl_print_data(const gchar* name, const guchar* data, size_t len);
548 extern void
549 ssl_print_string(const gchar* name, const StringInfo* data);
550 extern void
551 ssl_print_text_data(const gchar* name, const guchar* data, size_t len);
552 extern void
553 ssl_set_debug(const gchar* name);
554 extern void
555 ssl_debug_flush(void);
556 #else
557
558 /* No debug: nullify debug operation*/
559 static inline void G_GNUC_PRINTF(1,2)
560 ssl_debug_printf(const gchar* fmt _U_,...)
561 {
562 }
563 #define ssl_print_data(a, b, c)
564 #define ssl_print_string(a, b)
565 #define ssl_print_text_data(a, b, c)
566 #define ssl_set_debug(name)
567 #define ssl_debug_flush()
568
569 #endif /* SSL_DECRYPT_DEBUG */
570
571 #endif /* SSL_UTILS_H */
572
573 /*
574  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
575  *
576  * Local variables:
577  * c-basic-offset: 4
578  * tab-width: 8
579  * indent-tabs-mode: nil
580  * End:
581  *
582  * vi: set shiftwidth=4 tabstop=8 expandtab:
583  * :indentSize=4:tabSize=8:noTabs=true:
584  */