no need for check_col()
[metze/wireshark/wip.git] / epan / dissectors / packet-dtls.c
1 /* packet-dtls.c
2  * Routines for dtls dissection
3  * Copyright (c) 2006, Authesserre Samuel <sauthess@gmail.com>
4  * Copyright (c) 2007, Mikael Magnusson <mikma@users.sourceforge.net>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  *
27  * DTLS dissection and decryption.
28  * See RFC 4347 for details about DTLS specs.
29  *
30  * Notes :
31  * This dissector is based on the TLS dissector (packet-ssl.c); Because of the similarity
32  *   of DTLS and TLS, decryption works like TLS with RSA key exchange.
33  * This dissector uses the sames things (file, libraries) as the SSL dissector (gnutls, packet-ssl-utils.h)
34  *  to make it easily maintainable.
35  *
36  * It was developed to dissect and decrypt the OpenSSL v 0.9.8f DTLS implementation.
37  * It is limited to this implementation; there is no complete implementation.
38  *
39  * Implemented :
40  *  - DTLS dissection
41  *  - DTLS decryption (openssl one)
42  *
43  * Todo :
44  *  - activate correct Mac calculation when openssl will be corrected
45  *    (or if an other implementation works),
46  *    corrected code is ready and commented in packet-ssl-utils.h file.
47  *  - add missing things (desegmentation, reordering... that aren't present in actual OpenSSL implementation)
48  */
49
50 #include "config.h"
51
52 #include <glib.h>
53
54 #include <epan/packet.h>
55 #include <epan/conversation.h>
56 #include <epan/expert.h>
57 #include <epan/prefs.h>
58 #include <epan/asn1.h>
59 #include <epan/dissectors/packet-x509af.h>
60 #include <epan/emem.h>
61 #include <epan/tap.h>
62 #include <epan/reassemble.h>
63 #include "packet-x509if.h"
64 #include "packet-ssl-utils.h"
65 #include <wsutil/file_util.h>
66 #include <epan/uat.h>
67 #include <epan/sctpppids.h>
68
69 void proto_register_dtls(void);
70
71 /* DTLS User Access Table */
72 static ssldecrypt_assoc_t *dtlskeylist_uats = NULL;
73 static guint ndtlsdecrypt = 0;
74
75 /* we need to remember the top tree so that subdissectors we call are created
76  * at the root and not deep down inside the DTLS decode
77  */
78 static proto_tree *top_tree;
79
80 /*********************************************************************
81  *
82  * Protocol Constants, Variables, Data Structures
83  *
84  *********************************************************************/
85
86 /* Initialize the protocol and registered fields */
87 static gint dtls_tap                            = -1;
88 static gint proto_dtls                          = -1;
89 static gint hf_dtls_record                      = -1;
90 static gint hf_dtls_record_content_type         = -1;
91 static gint hf_dtls_record_version              = -1;
92 static gint hf_dtls_record_epoch                = -1;
93 static gint hf_dtls_record_sequence_number      = -1;
94 static gint hf_dtls_record_length               = -1;
95 static gint hf_dtls_record_appdata              = -1;
96 static gint hf_dtls_change_cipher_spec          = -1;
97 static gint hf_dtls_alert_message               = -1;
98 static gint hf_dtls_alert_message_level         = -1;
99 static gint hf_dtls_alert_message_description   = -1;
100 static gint hf_dtls_handshake_protocol          = -1;
101 static gint hf_dtls_handshake_type              = -1;
102 static gint hf_dtls_handshake_length            = -1;
103 static gint hf_dtls_handshake_message_seq       = -1;
104 static gint hf_dtls_handshake_fragment_offset   = -1;
105 static gint hf_dtls_handshake_fragment_length   = -1;
106 static gint hf_dtls_handshake_client_version    = -1;
107 static gint hf_dtls_handshake_server_version    = -1;
108 static gint hf_dtls_handshake_random_time       = -1;
109 static gint hf_dtls_handshake_random_bytes      = -1;
110 static gint hf_dtls_handshake_cookie_len        = -1;
111 static gint hf_dtls_handshake_cookie            = -1;
112 static gint hf_dtls_handshake_cipher_suites_len = -1;
113 static gint hf_dtls_handshake_cipher_suites     = -1;
114 static gint hf_dtls_handshake_cipher_suite      = -1;
115 static gint hf_dtls_handshake_session_id        = -1;
116 static gint hf_dtls_handshake_comp_methods_len  = -1;
117 static gint hf_dtls_handshake_comp_methods      = -1;
118 static gint hf_dtls_handshake_comp_method       = -1;
119 static gint hf_dtls_handshake_extensions_len    = -1;
120 static gint hf_dtls_handshake_extension_type    = -1;
121 static gint hf_dtls_handshake_extension_len     = -1;
122 static gint hf_dtls_handshake_extension_data    = -1;
123 static gint hf_dtls_handshake_session_ticket_lifetime_hint = -1;
124 static gint hf_dtls_handshake_session_ticket_len = -1;
125 static gint hf_dtls_handshake_session_ticket    = -1;
126 static gint hf_dtls_handshake_certificates_len  = -1;
127 static gint hf_dtls_handshake_certificates      = -1;
128 static gint hf_dtls_handshake_certificate       = -1;
129 static gint hf_dtls_handshake_certificate_len   = -1;
130 static gint hf_dtls_handshake_cert_types_count  = -1;
131 static gint hf_dtls_handshake_cert_types        = -1;
132 static gint hf_dtls_handshake_cert_type         = -1;
133 static gint hf_dtls_handshake_server_keyex_p_len     = -1;
134 static gint hf_dtls_handshake_server_keyex_g_len     = -1;
135 static gint hf_dtls_handshake_server_keyex_ys_len    = -1;
136 static gint hf_dtls_handshake_server_keyex_point_len = -1;
137 static gint hf_dtls_handshake_client_keyex_yc_len    = -1;
138 static gint hf_dtls_handshake_client_keyex_point_len = -1;
139 static gint hf_dtls_handshake_client_keyex_epms_len  = -1;
140 static gint hf_dtls_handshake_server_keyex_modulus_len = -1;
141 static gint hf_dtls_handshake_server_keyex_exponent_len = -1;
142 static gint hf_dtls_handshake_server_keyex_sig_len   = -1;
143 static gint hf_dtls_handshake_server_keyex_p         = -1;
144 static gint hf_dtls_handshake_server_keyex_g         = -1;
145 static gint hf_dtls_handshake_server_keyex_ys        = -1;
146 static gint hf_dtls_handshake_client_keyex_yc        = -1;
147 static gint hf_dtls_handshake_server_keyex_curve_type = -1;
148 static gint hf_dtls_handshake_server_keyex_named_curve = -1;
149 static gint hf_dtls_handshake_server_keyex_point     = -1;
150 static gint hf_dtls_handshake_client_keyex_epms      = -1;
151 static gint hf_dtls_handshake_client_keyex_point     = -1;
152 static gint hf_dtls_handshake_server_keyex_modulus   = -1;
153 static gint hf_dtls_handshake_server_keyex_exponent  = -1;
154 static gint hf_dtls_handshake_server_keyex_sig       = -1;
155 static gint hf_dtls_handshake_server_keyex_hint_len  = -1;
156 static gint hf_dtls_handshake_server_keyex_hint      = -1;
157 static gint hf_dtls_handshake_client_keyex_identity_len = -1;
158 static gint hf_dtls_handshake_client_keyex_identity  = -1;
159 static gint hf_dtls_handshake_sig_hash_alg_len = -1;
160 static gint hf_dtls_handshake_sig_hash_algs    = -1;
161 static gint hf_dtls_handshake_sig_hash_alg     = -1;
162 static gint hf_dtls_handshake_sig_hash_hash    = -1;
163 static gint hf_dtls_handshake_sig_hash_sig     = -1;
164 static gint hf_dtls_handshake_finished          = -1;
165 /* static gint hf_dtls_handshake_md5_hash          = -1; */
166 /* static gint hf_dtls_handshake_sha_hash          = -1; */
167 static gint hf_dtls_handshake_session_id_len    = -1;
168 static gint hf_dtls_handshake_dnames_len        = -1;
169 static gint hf_dtls_handshake_dnames            = -1;
170 static gint hf_dtls_handshake_dname_len         = -1;
171 static gint hf_dtls_handshake_dname             = -1;
172
173 static gint hf_dtls_heartbeat_extension_mode          = -1;
174 static gint hf_dtls_heartbeat_message                 = -1;
175 static gint hf_dtls_heartbeat_message_type            = -1;
176 static gint hf_dtls_heartbeat_message_payload_length  = -1;
177 static gint hf_dtls_heartbeat_message_payload         = -1;
178 static gint hf_dtls_heartbeat_message_padding         = -1;
179
180 static gint hf_dtls_fragments                   = -1;
181 static gint hf_dtls_fragment                    = -1;
182 static gint hf_dtls_fragment_overlap            = -1;
183 static gint hf_dtls_fragment_overlap_conflicts  = -1;
184 static gint hf_dtls_fragment_multiple_tails     = -1;
185 static gint hf_dtls_fragment_too_long_fragment  = -1;
186 static gint hf_dtls_fragment_error              = -1;
187 static gint hf_dtls_fragment_count              = -1;
188 static gint hf_dtls_reassembled_in              = -1;
189 static gint hf_dtls_reassembled_length          = -1;
190
191 /* Initialize the subtree pointers */
192 static gint ett_dtls                   = -1;
193 static gint ett_dtls_record            = -1;
194 static gint ett_dtls_alert             = -1;
195 static gint ett_dtls_handshake         = -1;
196 static gint ett_dtls_heartbeat         = -1;
197 static gint ett_dtls_cipher_suites     = -1;
198 static gint ett_dtls_comp_methods      = -1;
199 static gint ett_dtls_extension         = -1;
200 static gint ett_dtls_random            = -1;
201 static gint ett_dtls_new_ses_ticket    = -1;
202 static gint ett_dtls_keyex_params      = -1;
203 static gint ett_dtls_certs             = -1;
204 static gint ett_dtls_cert_types        = -1;
205 static gint ett_dtls_sig_hash_algs     = -1;
206 static gint ett_dtls_sig_hash_alg      = -1;
207 static gint ett_dtls_dnames            = -1;
208
209 static gint ett_dtls_fragment          = -1;
210 static gint ett_dtls_fragments         = -1;
211
212 static expert_field ei_dtls_handshake_fragment_length_too_long = EI_INIT;
213 static expert_field ei_dtls_handshake_fragment_past_end_msg = EI_INIT;
214 static expert_field ei_dtls_msg_len_diff_fragment = EI_INIT;
215 static expert_field ei_dtls_handshake_sig_hash_alg_len_bad = EI_INIT;
216
217 static GHashTable         *dtls_session_hash         = NULL;
218 static GHashTable         *dtls_key_hash             = NULL;
219 static reassembly_table    dtls_reassembly_table;
220 static GTree*              dtls_associations         = NULL;
221 static dissector_handle_t  dtls_handle               = NULL;
222 static StringInfo          dtls_compressed_data      = {NULL, 0};
223 static StringInfo          dtls_decrypted_data       = {NULL, 0};
224 static gint                dtls_decrypted_data_avail = 0;
225
226 static uat_t *dtlsdecrypt_uat      = NULL;
227 static const gchar *dtls_keys_list = NULL;
228 #ifdef HAVE_LIBGNUTLS
229 static const gchar *dtls_debug_file_name = NULL;
230 #endif
231
232 static heur_dissector_list_t heur_subdissector_list;
233
234 static const fragment_items dtls_frag_items = {
235   /* Fragment subtrees */
236   &ett_dtls_fragment,
237   &ett_dtls_fragments,
238   /* Fragment fields */
239   &hf_dtls_fragments,
240   &hf_dtls_fragment,
241   &hf_dtls_fragment_overlap,
242   &hf_dtls_fragment_overlap_conflicts,
243   &hf_dtls_fragment_multiple_tails,
244   &hf_dtls_fragment_too_long_fragment,
245   &hf_dtls_fragment_error,
246   &hf_dtls_fragment_count,
247   /* Reassembled in field */
248   &hf_dtls_reassembled_in,
249   /* Reassembled length field */
250   &hf_dtls_reassembled_length,
251   /* Reassembled data field */
252   NULL,
253   /* Tag */
254   "Message fragments"
255 };
256
257 /* initialize/reset per capture state data (dtls sessions cache) */
258 static void
259 dtls_init(void)
260 {
261   module_t *dtls_module = prefs_find_module("dtls");
262   pref_t   *keys_list_pref;
263
264   ssl_common_init(&dtls_session_hash, &dtls_decrypted_data, &dtls_compressed_data);
265   reassembly_table_init (&dtls_reassembly_table, &addresses_reassembly_table_functions);
266
267   /* We should have loaded "keys_list" by now. Mark it obsolete */
268   if (dtls_module) {
269     keys_list_pref = prefs_find_preference(dtls_module, "keys_list");
270     if (! prefs_get_preference_obsolete(keys_list_pref)) {
271       prefs_set_preference_obsolete(keys_list_pref);
272     }
273   }
274 }
275
276 /* parse dtls related preferences (private keys and ports association strings) */
277 static void
278 dtls_parse_uat(void)
279 {
280   ep_stack_t       tmp_stack;
281   SslAssociation  *tmp_assoc;
282   guint            i;
283
284   if (dtls_key_hash)
285   {
286       g_hash_table_foreach(dtls_key_hash, ssl_private_key_free, NULL);
287       g_hash_table_destroy(dtls_key_hash);
288   }
289
290   /* remove only associations created from key list */
291   tmp_stack = ep_stack_new();
292   g_tree_foreach(dtls_associations, ssl_assoc_from_key_list, tmp_stack);
293   while ((tmp_assoc = (SslAssociation *)ep_stack_pop(tmp_stack)) != NULL) {
294     ssl_association_remove(dtls_associations, tmp_assoc);
295   }
296
297   /* parse private keys string, load available keys and put them in key hash*/
298   dtls_key_hash = g_hash_table_new(ssl_private_key_hash, ssl_private_key_equal);
299
300   ssl_set_debug(dtls_debug_file_name);
301
302   if (ndtlsdecrypt > 0)
303   {
304     for (i = 0; i < ndtlsdecrypt; i++)
305     {
306       ssldecrypt_assoc_t *d = &(dtlskeylist_uats[i]);
307       ssl_parse_key_list(d, dtls_key_hash, dtls_associations, dtls_handle, FALSE);
308     }
309   }
310
311   dissector_add_handle("sctp.port", dtls_handle);
312   dissector_add_handle("udp.port", dtls_handle);
313 }
314
315 static void
316 dtls_parse_old_keys(void)
317 {
318   gchar          **old_keys, **parts, *err;
319   guint            i;
320   gchar          *uat_entry;
321
322   /* Import old-style keys */
323   if (dtlsdecrypt_uat && dtls_keys_list && dtls_keys_list[0]) {
324     old_keys = ep_strsplit(dtls_keys_list, ";", 0);
325     for (i = 0; old_keys[i] != NULL; i++) {
326       parts = ep_strsplit(old_keys[i], ",", 4);
327       if (parts[0] && parts[1] && parts[2] && parts[3]) {
328         uat_entry = ep_strdup_printf("\"%s\",\"%s\",\"%s\",\"%s\",\"\"",
329                         parts[0], parts[1], parts[2], parts[3]);
330         if (!uat_load_str(dtlsdecrypt_uat, uat_entry, &err)) {
331           ssl_debug_printf("dtls_parse: Can't load UAT string %s: %s\n",
332                            uat_entry, err);
333         }
334       }
335     }
336   }
337 }
338
339 /*
340  * DTLS Dissection Routines
341  *
342  */
343
344 /* record layer dissector */
345 static gint dissect_dtls_record(tvbuff_t *tvb, packet_info *pinfo,
346                                 proto_tree *tree, guint32 offset,
347                                 guint *conv_version, guint conv_cipher,
348                                 SslDecryptSession *conv_data);
349
350 /* change cipher spec dissector */
351 static void dissect_dtls_change_cipher_spec(tvbuff_t *tvb,
352                                             proto_tree *tree,
353                                             guint32 offset,
354                                             guint *conv_version, guint8 content_type);
355
356 /* alert message dissector */
357 static void dissect_dtls_alert(tvbuff_t *tvb, packet_info *pinfo,
358                                proto_tree *tree, guint32 offset,
359                                guint *conv_version);
360
361 /* handshake protocol dissector */
362 static void dissect_dtls_handshake(tvbuff_t *tvb, packet_info *pinfo,
363                                    proto_tree *tree, guint32 offset,
364                                    guint32 record_length,
365                                    guint *conv_version, guint conv_cipher,
366                                    SslDecryptSession *conv_data, guint8 content_type);
367
368 /* heartbeat message dissector */
369 static void dissect_dtls_heartbeat(tvbuff_t *tvb, packet_info *pinfo,
370                                    proto_tree *tree, guint32 offset,
371                                    guint *conv_version, guint32 record_length);
372
373
374 static void dissect_dtls_hnd_cli_hello(tvbuff_t *tvb,
375                                        proto_tree *tree,
376                                        guint32 offset, guint32 length,
377                                        SslDecryptSession* ssl);
378
379 static int dissect_dtls_hnd_srv_hello(tvbuff_t *tvb,
380                                        proto_tree *tree,
381                                        guint32 offset, guint32 length,
382                                        SslDecryptSession* ssl);
383
384 static int dissect_dtls_hnd_hello_verify_request(tvbuff_t *tvb,
385                                                   proto_tree *tree,
386                                                   guint32 offset,
387                                                   SslDecryptSession* ssl);
388
389 static void dissect_dtls_hnd_new_ses_ticket(tvbuff_t *tvb,
390                                        proto_tree *tree,
391                                        guint32 offset, guint32 length);
392
393 static void dissect_dtls_hnd_cert(tvbuff_t *tvb,
394                                   proto_tree *tree, guint32 offset, packet_info *pinfo);
395
396 static void dissect_dtls_hnd_cert_req(tvbuff_t *tvb,
397                                       proto_tree *tree,
398                                       guint32 offset,
399                                       packet_info *pinfo,
400                                       const guint *conv_version);
401
402 static void dissect_dtls_hnd_srv_keyex_ecdh(tvbuff_t *tvb,
403                                           proto_tree *tree,
404                                           guint32 offset, guint32 length);
405
406 static void dissect_dtls_hnd_srv_keyex_dh(tvbuff_t *tvb,
407                                           proto_tree *tree,
408                                           guint32 offset, guint32 length);
409
410 static void dissect_dtls_hnd_srv_keyex_rsa(tvbuff_t *tvb,
411                                           proto_tree *tree,
412                                           guint32 offset, guint32 length);
413
414 static void dissect_dtls_hnd_srv_keyex_psk(tvbuff_t *tvb,
415                                           proto_tree *tree,
416                                           guint32 offset, guint32 length);
417
418 static void dissect_dtls_hnd_cli_keyex_ecdh(tvbuff_t *tvb,
419                                           proto_tree *tree,
420                                           guint32 offset, guint32 length);
421
422 static void dissect_dtls_hnd_cli_keyex_dh(tvbuff_t *tvb,
423                                           proto_tree *tree,
424                                           guint32 offset, guint32 length);
425
426 static void dissect_dtls_hnd_cli_keyex_rsa(tvbuff_t *tvb,
427                                           proto_tree *tree,
428                                           guint32 offset, guint32 length);
429
430 static void dissect_dtls_hnd_cli_keyex_psk(tvbuff_t *tvb,
431                                           proto_tree *tree,
432                                           guint32 offset, guint32 length);
433
434 static void dissect_dtls_hnd_cli_keyex_rsa_psk(tvbuff_t *tvb,
435                                                proto_tree *tree,
436                                                guint32 offset, guint32 length);
437
438 static void dissect_dtls_hnd_finished(tvbuff_t *tvb,
439                                       proto_tree *tree,
440                                       guint32 offset,
441                                       guint* conv_version);
442
443 /*
444  * Support Functions
445  *
446  */
447 /*static void ssl_set_conv_version(packet_info *pinfo, guint version);*/
448 static gint  dtls_is_valid_handshake_type(guint8 type);
449
450 static gint  dtls_is_authoritative_version_message(guint8 content_type,
451                                                    guint8 next_byte);
452 static gint  looks_like_dtls(tvbuff_t *tvb, guint32 offset);
453
454 /*********************************************************************
455  *
456  * Main dissector
457  *
458  *********************************************************************/
459 /*
460  * Code to actually dissect the packets
461  */
462 static void
463 dissect_dtls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
464 {
465
466   conversation_t    *conversation;
467   void              *conv_data;
468   proto_item        *ti;
469   proto_tree        *dtls_tree;
470   guint32            offset;
471   gboolean           first_record_in_frame;
472   SslDecryptSession *ssl_session;
473   guint*             conv_version;
474   guint              conv_cipher;
475   Ssl_private_key_t *private_key;
476
477   ti                    = NULL;
478   dtls_tree             = NULL;
479   offset                = 0;
480   first_record_in_frame = TRUE;
481   ssl_session           = NULL;
482   top_tree              = tree;
483
484   /* Track the version using conversations allows
485    * us to more frequently set the protocol column properly
486    * for continuation data frames.
487    *
488    * Also: We use the copy in conv_version as our cached copy,
489    *       so that we don't have to search the conversation
490    *       table every time we want the version; when setting
491    *       the conv_version, must set the copy in the conversation
492    *       in addition to conv_version
493    */
494   conversation = find_or_create_conversation(pinfo);
495   conv_data    = conversation_get_proto_data(conversation, proto_dtls);
496
497   /* manage dtls decryption data */
498   /*get a valid ssl session pointer*/
499   if (conv_data != NULL)
500     ssl_session = (SslDecryptSession *)conv_data;
501   else {
502     SslService dummy;
503
504     ssl_session = se_new0(SslDecryptSession);
505     ssl_session_init(ssl_session);
506     ssl_session->version = SSL_VER_UNKNOWN;
507     conversation_add_proto_data(conversation, proto_dtls, ssl_session);
508
509     /* we need to know witch side of conversation is speaking */
510     if (ssl_packet_from_server(ssl_session, dtls_associations, pinfo)) {
511       dummy.addr = pinfo->src;
512       dummy.port = pinfo->srcport;
513     }
514     else {
515       dummy.addr = pinfo->dst;
516       dummy.port = pinfo->destport;
517     }
518     ssl_debug_printf("dissect_dtls server %s:%d\n",
519                      ep_address_to_str(&dummy.addr),dummy.port);
520
521     /* try to retrieve private key for this service. Do it now 'cause pinfo
522      * is not always available
523      * Note that with HAVE_LIBGNUTLS undefined private_key is always 0
524      * and thus decryption never engaged*/
525     private_key = (Ssl_private_key_t *)g_hash_table_lookup(dtls_key_hash, &dummy);
526     if (!private_key) {
527       ssl_debug_printf("dissect_dtls can't find private key for this server!\n");
528     }
529     else {
530       ssl_session->private_key = private_key->sexp_pkey;
531     }
532   }
533   conv_version= & ssl_session->version;
534   conv_cipher  =  ssl_session->cipher;
535
536   /* try decryption only the first time we see this packet
537    * (to keep cipher synchronized) */
538   if (pinfo->fd->flags.visited)
539     ssl_session = NULL;
540
541   /* Initialize the protocol column; we'll set it later when we
542    * figure out what flavor of DTLS it is (actually only one
543    version exists). */
544   col_set_str(pinfo->cinfo, COL_PROTOCOL, "DTLS");
545
546   /* clear the the info column */
547   col_clear(pinfo->cinfo, COL_INFO);
548
549   /* Create display subtree for SSL as a whole */
550   if (tree)
551     {
552       ti = proto_tree_add_item(tree, proto_dtls, tvb, 0, -1, ENC_NA);
553       dtls_tree = proto_item_add_subtree(ti, ett_dtls);
554     }
555
556   /* iterate through the records in this tvbuff */
557   while (tvb_reported_length_remaining(tvb, offset) != 0)
558     {
559       /* on second and subsequent records per frame
560        * add a delimiter on info column
561        */
562       if (!first_record_in_frame)
563         {
564           col_append_str(pinfo->cinfo, COL_INFO, ", ");
565         }
566
567       /* first try to dispatch off the cached version
568        * known to be associated with the conversation
569        */
570       switch(*conv_version) {
571       case SSL_VER_DTLS:
572         offset = dissect_dtls_record(tvb, pinfo, dtls_tree,
573                                      offset, conv_version, conv_cipher,
574                                      ssl_session);
575         break;
576       case SSL_VER_DTLS1DOT2:
577         offset = dissect_dtls_record(tvb, pinfo, dtls_tree,
578                                      offset, conv_version, conv_cipher,
579                                      ssl_session);
580         break;
581
582         /* that failed, so apply some heuristics based
583          * on this individual packet
584          */
585       default:
586         if (looks_like_dtls(tvb, offset))
587           {
588             /* looks like dtls */
589             offset = dissect_dtls_record(tvb, pinfo, dtls_tree,
590                                          offset, conv_version, conv_cipher,
591                                          ssl_session);
592           }
593         else
594           {
595             /* looks like something unknown, so lump into
596              * continuation data
597              */
598             offset = tvb_length(tvb);
599             col_append_str(pinfo->cinfo, COL_INFO,
600                              "Continuation Data");
601
602             /* Set the protocol column */
603             col_set_str(pinfo->cinfo, COL_PROTOCOL, "DTLS");
604           }
605         break;
606       }
607
608       /* set up for next record in frame, if any */
609       first_record_in_frame = FALSE;
610     }
611
612   tap_queue_packet(dtls_tap, pinfo, NULL);
613 }
614
615 static gboolean
616 dissect_dtls_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
617
618 {
619   /* Stronger confirmation of DTLS packet is provided by verifying the
620    * captured payload length against the remainder of the UDP packet size. */
621   guint length = tvb_length(tvb);
622   guint offset = 0;
623
624   if (tvb_reported_length(tvb) == length) {
625     /* The entire payload was captured. */
626     while (offset + 13 <= length && looks_like_dtls(tvb, offset)) {
627       /* Advance offset to the end of the current DTLS record */
628       offset += tvb_get_ntohs(tvb, offset + 11) + 13;
629       if (offset == length) {
630         dissect_dtls(tvb, pinfo, tree);
631         return TRUE;
632       }
633     }
634
635     if (pinfo->fragmented && offset >= 13) {
636       dissect_dtls(tvb, pinfo, tree);
637       return TRUE;
638     }
639     return FALSE;
640   }
641
642   /* This packet was truncated by the capture process due to a snapshot
643    * length - do our best with what we've got. */
644   while (tvb_length_remaining(tvb, offset) >= 3) {
645     if (!looks_like_dtls(tvb, offset))
646       return FALSE;
647
648     offset += 3;
649     if (tvb_length_remaining(tvb, offset) >= 10 ) {
650       offset += tvb_get_ntohs(tvb, offset + 8) + 10;
651     } else {
652       /* Dissect what we've got, which might be as little as 3 bytes. */
653       dissect_dtls(tvb, pinfo, tree);
654       return TRUE;
655     }
656     if (offset == length) {
657       /* Can this ever happen?  Well, just in case ... */
658       dissect_dtls(tvb, pinfo, tree);
659       return TRUE;
660     }
661   }
662
663   /* One last check to see if the current offset is at least less than the
664    * original number of bytes present before truncation or we're dealing with
665    * a packet fragment that's also been truncated. */
666   if ((length >= 3) && (offset <= tvb_reported_length(tvb) || pinfo->fragmented)) {
667     dissect_dtls(tvb, pinfo, tree);
668     return TRUE;
669   }
670   return FALSE;
671 }
672
673 static gint
674 decrypt_dtls_record(tvbuff_t *tvb, packet_info *pinfo, guint32 offset,
675                     guint32 record_length, guint8 content_type, SslDecryptSession* ssl,
676                     gboolean save_plaintext)
677 {
678   gint        ret;
679   SslDecoder *decoder;
680
681   ret = 0;
682
683   /* if we can decrypt and decryption have success
684    * add decrypted data to this packet info */
685   if (!ssl || (!save_plaintext && !(ssl->state & SSL_HAVE_SESSION_KEY))) {
686     ssl_debug_printf("decrypt_dtls_record: no session key\n");
687     return ret;
688   }
689   ssl_debug_printf("decrypt_dtls_record: app_data len %d, ssl state %X\n",
690                    record_length, ssl->state);
691
692   /* retrieve decoder for this packet direction */
693   if (ssl_packet_from_server(ssl, dtls_associations, pinfo) != 0) {
694     ssl_debug_printf("decrypt_dtls_record: using server decoder\n");
695     decoder = ssl->server;
696   }
697   else {
698     ssl_debug_printf("decrypt_dtls_record: using client decoder\n");
699     decoder = ssl->client;
700   }
701
702   if (!decoder && ssl->cipher != 0x0001 && ssl->cipher != 0x0002) {
703     ssl_debug_printf("decrypt_dtls_record: no decoder available\n");
704     return ret;
705   }
706
707   /* ensure we have enough storage space for decrypted data */
708   if (record_length > dtls_decrypted_data.data_len)
709     {
710       ssl_debug_printf("decrypt_dtls_record: allocating %d bytes"
711                        " for decrypt data (old len %d)\n",
712                        record_length + 32, dtls_decrypted_data.data_len);
713       dtls_decrypted_data.data = (guchar *)g_realloc(dtls_decrypted_data.data,
714                                            record_length + 32);
715       dtls_decrypted_data.data_len = record_length + 32;
716     }
717
718   /* run decryption and add decrypted payload to protocol data, if decryption
719    * is successful*/
720   dtls_decrypted_data_avail = dtls_decrypted_data.data_len;
721   if (ssl->state & SSL_HAVE_SESSION_KEY) {
722     if (!decoder) {
723       ssl_debug_printf("decrypt_dtls_record: no decoder available\n");
724       return ret;
725     }
726     if (ssl_decrypt_record(ssl, decoder, content_type, tvb_get_ptr(tvb, offset, record_length), record_length,
727                            &dtls_compressed_data, &dtls_decrypted_data, &dtls_decrypted_data_avail) == 0)
728       ret = 1;
729   }
730   else if (ssl->cipher == 0x0001 || ssl->cipher == 0x0002) {
731     /* Non-encrypting cipher RSA-NULL-MD5 or RSA-NULL-SHA */
732     memcpy(dtls_decrypted_data.data, tvb_get_ptr(tvb, offset, record_length), record_length);
733     dtls_decrypted_data_avail = dtls_decrypted_data.data_len = record_length;
734     ret = 1;
735   }
736
737   if (ret && save_plaintext) {
738     ssl_add_data_info(proto_dtls, pinfo, dtls_decrypted_data.data, dtls_decrypted_data_avail,
739                       tvb_raw_offset(tvb)+offset, 0);
740   }
741
742   return ret;
743 }
744
745
746
747
748
749 /*********************************************************************
750  *
751  * DTLS Dissection Routines
752  *
753  *********************************************************************/
754 static gint
755 dissect_dtls_record(tvbuff_t *tvb, packet_info *pinfo,
756                     proto_tree *tree, guint32 offset,
757                     guint *conv_version, guint conv_cipher,
758                     SslDecryptSession* ssl)
759 {
760
761   /*
762    *    struct {
763    *        uint8 major, minor;
764    *    } ProtocolVersion;
765    *
766    *
767    *    enum {
768    *        change_cipher_spec(20), alert(21), handshake(22),
769    *        application_data(23), (255)
770    *    } ContentType;
771    *
772    *    struct {
773    *        ContentType type;
774    *        ProtocolVersion version;
775    *        uint16 epoch;               // New field
776    *        uint48 sequence_number;     // New field
777    *        uint16 length;
778    *        opaque fragment[TLSPlaintext.length];
779    *    } DTLSPlaintext;
780    */
781
782   guint32         record_length;
783   guint16         version;
784   guint16         epoch;
785   gdouble         sequence_number;
786   gint64          sequence_number_temp;
787   guint8          content_type;
788   guint8          next_byte;
789   proto_tree     *ti;
790   proto_tree     *dtls_record_tree;
791   SslAssociation *association;
792   SslDataInfo    *appl_data;
793
794   ti               = NULL;
795   dtls_record_tree = NULL;
796
797   /*
798    * Get the record layer fields of interest
799    */
800   content_type          = tvb_get_guint8(tvb, offset);
801   version               = tvb_get_ntohs(tvb, offset + 1);
802   epoch                 = tvb_get_ntohs(tvb, offset + 3);
803   sequence_number       = tvb_get_ntohl(tvb, offset + 7);
804   sequence_number_temp  = tvb_get_ntohs(tvb, offset + 5);
805   sequence_number_temp  = sequence_number_temp<<32;
806   sequence_number      += sequence_number_temp;
807   record_length         = tvb_get_ntohs(tvb, offset + 11);
808
809   if(ssl){
810     if(ssl_packet_from_server(ssl, dtls_associations, pinfo)){
811      if (ssl->server) {
812       ssl->server->seq=(guint32)sequence_number;
813       ssl->server->epoch=epoch;
814      }
815     }
816     else{
817      if (ssl->client) {
818       ssl->client->seq=(guint32)sequence_number;
819       ssl->client->epoch=epoch;
820      }
821     }
822   }
823   if (!ssl_is_valid_content_type(content_type)) {
824
825     /* if we don't have a valid content_type, there's no sense
826      * continuing any further
827      */
828     col_append_str(pinfo->cinfo, COL_INFO, "Continuation Data");
829
830     /* Set the protocol column */
831     col_set_str(pinfo->cinfo, COL_PROTOCOL, "DTLS");
832     return offset + 13 + record_length;
833   }
834
835   /*
836    * If GUI, fill in record layer part of tree
837    */
838
839   if (tree)
840     {
841       /* add the record layer subtree header */
842       tvb_ensure_bytes_exist(tvb, offset, 13 + record_length);
843       ti = proto_tree_add_item(tree, hf_dtls_record, tvb,
844                                offset, 13 + record_length, ENC_NA);
845       dtls_record_tree = proto_item_add_subtree(ti, ett_dtls_record);
846     }
847
848   if (dtls_record_tree)
849     {
850
851       /* show the one-byte content type */
852       proto_tree_add_item(dtls_record_tree, hf_dtls_record_content_type,
853                           tvb, offset, 1, ENC_BIG_ENDIAN);
854       offset++;
855
856       /* add the version */
857       proto_tree_add_item(dtls_record_tree, hf_dtls_record_version, tvb,
858                           offset, 2, ENC_BIG_ENDIAN);
859       offset += 2;
860
861       /* show epoch */
862       proto_tree_add_uint(dtls_record_tree, hf_dtls_record_epoch, tvb, offset, 2, epoch);
863
864       offset += 2;
865
866       /* add sequence_number */
867
868       proto_tree_add_double(dtls_record_tree, hf_dtls_record_sequence_number, tvb, offset, 6, sequence_number);
869
870       offset += 6;
871
872       /* add the length */
873       proto_tree_add_uint(dtls_record_tree, hf_dtls_record_length, tvb,
874                           offset, 2, record_length);
875       offset += 2;    /* move past length field itself */
876
877     }
878   else
879     {
880       /* if no GUI tree, then just skip over those fields */
881       offset += 13;
882     }
883
884
885   /*
886    * if we don't already have a version set for this conversation,
887    * but this message's version is authoritative (i.e., it's
888    * not client_hello, then save the version to to conversation
889    * structure and print the column version
890    */
891   next_byte = tvb_get_guint8(tvb, offset);
892   if (*conv_version == SSL_VER_UNKNOWN
893       && dtls_is_authoritative_version_message(content_type, next_byte))
894     {
895       if (version == DTLSV1DOT0_VERSION ||
896           version == DTLSV1DOT0_VERSION_NOT)
897         {
898
899           *conv_version = SSL_VER_DTLS;
900           if (ssl) {
901             ssl->version_netorder = version;
902             ssl->state |= SSL_VERSION;
903           }
904           /*ssl_set_conv_version(pinfo, ssl->version);*/
905         }
906       if (version == DTLSV1DOT2_VERSION)
907         {
908
909           *conv_version = SSL_VER_DTLS1DOT2;
910           if (ssl) {
911             ssl->version_netorder = version;
912             ssl->state |= SSL_VERSION;
913           }
914           /*ssl_set_conv_version(pinfo, ssl->version);*/
915         }
916     }
917   if (version == DTLSV1DOT0_VERSION)
918   {
919      col_set_str(pinfo->cinfo, COL_PROTOCOL,
920            val_to_str_const(SSL_VER_DTLS, ssl_version_short_names, "SSL"));
921   }
922   else if (version == DTLSV1DOT2_VERSION)
923   {
924      col_set_str(pinfo->cinfo, COL_PROTOCOL,
925            val_to_str_const(SSL_VER_DTLS1DOT2, ssl_version_short_names, "SSL"));
926   }
927   else
928   {
929      col_set_str(pinfo->cinfo, COL_PROTOCOL,"DTLS");
930   }
931
932   /*
933    * now dissect the next layer
934    */
935   ssl_debug_printf("dissect_dtls_record: content_type %d\n",content_type);
936
937   /* PAOLO try to decrypt each record (we must keep ciphers "in sync")
938    * store plain text only for app data */
939
940   switch (content_type) {
941   case SSL_ID_CHG_CIPHER_SPEC:
942     col_append_str(pinfo->cinfo, COL_INFO, "Change Cipher Spec");
943     dissect_dtls_change_cipher_spec(tvb, dtls_record_tree,
944                                     offset, conv_version, content_type);
945     if (ssl) ssl_change_cipher(ssl, ssl_packet_from_server(ssl, dtls_associations, pinfo));
946     break;
947   case SSL_ID_ALERT:
948     {
949       tvbuff_t* decrypted;
950       decrypted = 0;
951       if (ssl&&decrypt_dtls_record(tvb, pinfo, offset,
952                                    record_length, content_type, ssl, FALSE))
953         ssl_add_record_info(proto_dtls, pinfo, dtls_decrypted_data.data,
954                             dtls_decrypted_data_avail, offset);
955
956       /* try to retrieve and use decrypted alert record, if any. */
957       decrypted = ssl_get_record_info(tvb, proto_dtls, pinfo, offset);
958       if (decrypted) {
959         dissect_dtls_alert(decrypted, pinfo, dtls_record_tree, 0,
960                            conv_version);
961         add_new_data_source(pinfo, decrypted, "Decrypted SSL record");
962       } else {
963         dissect_dtls_alert(tvb, pinfo, dtls_record_tree, offset,
964                            conv_version);
965       }
966       break;
967     }
968   case SSL_ID_HANDSHAKE:
969     {
970       tvbuff_t* decrypted;
971       decrypted = 0;
972       /* try to decrypt handshake record, if possible. Store decrypted
973        * record for later usage. The offset is used as 'key' to identify
974        * this record into the packet (we can have multiple handshake records
975        * in the same frame) */
976       if (ssl && decrypt_dtls_record(tvb, pinfo, offset,
977                                      record_length, content_type, ssl, FALSE))
978         ssl_add_record_info(proto_dtls, pinfo, dtls_decrypted_data.data,
979                             dtls_decrypted_data_avail, offset);
980
981       /* try to retrieve and use decrypted handshake record, if any. */
982       decrypted = ssl_get_record_info(tvb, proto_dtls, pinfo, offset);
983       if (decrypted) {
984         dissect_dtls_handshake(decrypted, pinfo, dtls_record_tree, 0,
985                                tvb_length(decrypted), conv_version, conv_cipher, ssl, content_type);
986         add_new_data_source(pinfo, decrypted, "Decrypted SSL record");
987       } else {
988         dissect_dtls_handshake(tvb, pinfo, dtls_record_tree, offset,
989                                record_length, conv_version, conv_cipher, ssl, content_type);
990       }
991       break;
992     }
993   case SSL_ID_APP_DATA:
994     if (ssl)
995       decrypt_dtls_record(tvb, pinfo, offset,
996                           record_length, content_type, ssl, TRUE);
997
998     /* show on info column what we are decoding */
999     col_append_str(pinfo->cinfo, COL_INFO, "Application Data");
1000
1001     if (!dtls_record_tree)
1002       break;
1003
1004     /* we need dissector information when the selected packet is shown.
1005      * ssl session pointer is NULL at that time, so we can't access
1006      * info cached there*/
1007     association = ssl_association_find(dtls_associations, pinfo->srcport, pinfo->ptype == PT_TCP);
1008     association = association ? association : ssl_association_find(dtls_associations, pinfo->destport, pinfo->ptype == PT_TCP);
1009
1010     proto_item_set_text(dtls_record_tree,
1011                         "%s Record Layer: %s Protocol: %s",
1012                         val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1013                         val_to_str_const(content_type, ssl_31_content_type, "unknown"),
1014                         association?association->info:"Application Data");
1015
1016     /* show decrypted data info, if available */
1017     appl_data = ssl_get_data_info(proto_dtls, pinfo, tvb_raw_offset(tvb)+offset);
1018     if (appl_data && (appl_data->plain_data.data_len > 0))
1019       {
1020         tvbuff_t *next_tvb;
1021         gboolean  dissected;
1022         /* try to dissect decrypted data*/
1023         ssl_debug_printf("dissect_dtls_record decrypted len %d\n",
1024                          appl_data->plain_data.data_len);
1025
1026         /* create a new TVB structure for desegmented data */
1027         next_tvb = tvb_new_child_real_data(tvb,
1028                                            appl_data->plain_data.data,
1029                                            appl_data->plain_data.data_len,
1030                                            appl_data->plain_data.data_len);
1031
1032         add_new_data_source(pinfo, next_tvb, "Decrypted DTLS data");
1033
1034         /* find out a dissector using server port*/
1035         if (association && association->handle) {
1036           ssl_debug_printf("dissect_dtls_record found association %p\n", (void *)association);
1037           ssl_print_data("decrypted app data",appl_data->plain_data.data, appl_data->plain_data.data_len);
1038
1039           dissected = call_dissector_only(association->handle, next_tvb, pinfo, top_tree, NULL);
1040         }
1041         else {
1042           /* try heuristic subdissectors */
1043           dissected = dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, top_tree, NULL);
1044         }
1045         if (dissected)
1046           break;
1047       }
1048
1049     proto_tree_add_item(dtls_record_tree, hf_dtls_record_appdata, tvb,
1050                         offset, record_length, ENC_NA);
1051     break;
1052   case SSL_ID_HEARTBEAT:
1053   {
1054     tvbuff_t* decrypted;
1055
1056     if (ssl && decrypt_dtls_record(tvb, pinfo, offset,
1057                                    record_length, content_type, ssl, FALSE))
1058       ssl_add_record_info(proto_dtls, pinfo, dtls_decrypted_data.data,
1059                           dtls_decrypted_data_avail, offset);
1060
1061     /* try to retrieve and use decrypted alert record, if any. */
1062     decrypted = ssl_get_record_info(tvb, proto_dtls, pinfo, offset);
1063     if (decrypted) {
1064       dissect_dtls_heartbeat(decrypted, pinfo, dtls_record_tree, 0,
1065                              conv_version, record_length);
1066       add_new_data_source(pinfo, decrypted, "Decrypted SSL record");
1067     } else {
1068       dissect_dtls_heartbeat(tvb, pinfo, dtls_record_tree, offset,
1069                              conv_version, record_length);
1070     }
1071     break;
1072   }
1073
1074   default:
1075     /* shouldn't get here since we check above for valid types */
1076     col_append_str(pinfo->cinfo, COL_INFO, "Bad DTLS Content Type");
1077     break;
1078   }
1079   offset += record_length; /* skip to end of record */
1080
1081   return offset;
1082 }
1083
1084 /* dissects the change cipher spec protocol, filling in the tree */
1085 static void
1086 dissect_dtls_change_cipher_spec(tvbuff_t *tvb,
1087                                 proto_tree *tree, guint32 offset,
1088                                 guint* conv_version, guint8 content_type)
1089 {
1090   /*
1091    * struct {
1092    *     enum { change_cipher_spec(1), (255) } type;
1093    * } ChangeCipherSpec;
1094    *
1095    */
1096   if (tree)
1097     {
1098       proto_item_set_text(tree,
1099                           "%s Record Layer: %s Protocol: Change Cipher Spec",
1100                           val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1101                           val_to_str_const(content_type, ssl_31_content_type, "unknown"));
1102       proto_tree_add_item(tree, hf_dtls_change_cipher_spec, tvb,
1103                           offset, 1, ENC_NA);
1104     }
1105 }
1106
1107 /* dissects the alert message, filling in the tree */
1108 static void
1109 dissect_dtls_alert(tvbuff_t *tvb, packet_info *pinfo,
1110                    proto_tree *tree, guint32 offset,
1111                    guint* conv_version)
1112 {
1113   /*     struct {
1114    *         AlertLevel level;
1115    *         AlertDescription description;
1116    *     } Alert;
1117    */
1118
1119   proto_tree  *ti;
1120   proto_tree  *ssl_alert_tree;
1121   const gchar *level;
1122   const gchar *desc;
1123   guint8       byte;
1124
1125   ssl_alert_tree = NULL;
1126
1127   if (tree)
1128     {
1129       ti = proto_tree_add_item(tree, hf_dtls_alert_message, tvb,
1130                                offset, 2, ENC_NA);
1131       ssl_alert_tree = proto_item_add_subtree(ti, ett_dtls_alert);
1132     }
1133
1134   /*
1135    * set the record layer label
1136    */
1137
1138   /* first lookup the names for the alert level and description */
1139   byte  = tvb_get_guint8(tvb, offset); /* grab the level byte */
1140   level = try_val_to_str(byte, ssl_31_alert_level);
1141
1142   byte  = tvb_get_guint8(tvb, offset+1); /* grab the desc byte */
1143   desc  = try_val_to_str(byte, ssl_31_alert_description);
1144
1145   /* now set the text in the record layer line */
1146   if (level && desc)
1147     {
1148        col_append_fstr(pinfo->cinfo, COL_INFO,
1149              "Alert (Level: %s, Description: %s)",
1150              level, desc);
1151     }
1152   else
1153     {
1154       col_append_str(pinfo->cinfo, COL_INFO, "Encrypted Alert");
1155     }
1156
1157   if (tree)
1158     {
1159       if (level && desc)
1160         {
1161           proto_item_set_text(tree, "%s Record Layer: Alert "
1162                               "(Level: %s, Description: %s)",
1163                               val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1164                               level, desc);
1165           proto_tree_add_item(ssl_alert_tree, hf_dtls_alert_message_level,
1166                               tvb, offset++, 1, ENC_BIG_ENDIAN);
1167
1168           proto_tree_add_item(ssl_alert_tree, hf_dtls_alert_message_description,
1169                               tvb, offset, 1, ENC_BIG_ENDIAN);
1170         }
1171       else
1172         {
1173           proto_item_set_text(tree,
1174                               "%s Record Layer: Encrypted Alert",
1175                               val_to_str_const(*conv_version, ssl_version_short_names, "SSL"));
1176           proto_item_set_text(ssl_alert_tree,
1177                               "Alert Message: Encrypted Alert");
1178         }
1179     }
1180 }
1181
1182
1183 /* dissects the handshake protocol, filling the tree */
1184 static void
1185 dissect_dtls_handshake(tvbuff_t *tvb, packet_info *pinfo,
1186                        proto_tree *tree, guint32 offset,
1187                        guint32 record_length, guint *conv_version, guint conv_cipher,
1188                        SslDecryptSession* ssl, guint8 content_type)
1189 {
1190   /*     struct {
1191    *         HandshakeType msg_type;
1192    *         uint24 length;
1193    *         uint16 message_seq;          //new field
1194    *         uint24 fragment_offset;      //new field
1195    *         uint24 fragment_length;      //new field
1196    *         select (HandshakeType) {
1197    *             case hello_request:       HelloRequest;
1198    *             case client_hello:        ClientHello;
1199    *             case server_hello:        ServerHello;
1200    *             case hello_verify_request: HelloVerifyRequest;     //new field
1201    *             case certificate:         Certificate;
1202    *             case server_key_exchange: ServerKeyExchange;
1203    *             case certificate_request: CertificateRequest;
1204    *             case server_hello_done:   ServerHelloDone;
1205    *             case certificate_verify:  CertificateVerify;
1206    *             case client_key_exchange: ClientKeyExchange;
1207    *             case finished:            Finished;
1208    *         } body;
1209    *     } Handshake;
1210    */
1211
1212   proto_tree  *ti, *length_item = NULL, *fragment_length_item = NULL;
1213   proto_tree  *ssl_hand_tree;
1214   const gchar *msg_type_str;
1215   guint8       msg_type;
1216   guint32      length;
1217   guint16      message_seq;
1218   guint32      fragment_offset;
1219   guint32      fragment_length;
1220   gboolean     first_iteration;
1221   gboolean     frag_hand;
1222   guint32      reassembled_length;
1223
1224   ti              = NULL;
1225   ssl_hand_tree   = NULL;
1226   msg_type_str    = NULL;
1227   first_iteration = TRUE;
1228
1229   /* just as there can be multiple records per packet, there
1230    * can be multiple messages per record as long as they have
1231    * the same content type
1232    *
1233    * we really only care about this for handshake messages
1234    */
1235
1236   /* set record_length to the max offset */
1237   record_length += offset;
1238   for (; offset < record_length; offset += fragment_length,
1239          first_iteration = FALSE) /* set up for next pass, if any */
1240     {
1241       fragment_data *frag_msg = NULL;
1242       tvbuff_t      *new_tvb  = NULL;
1243       const gchar   *frag_str = NULL;
1244       gboolean       fragmented;
1245
1246       if (tree)
1247         {
1248           /* add a subtree for the handshake protocol */
1249           ti = proto_tree_add_item(tree, hf_dtls_handshake_protocol, tvb,
1250                                    offset, -1, ENC_NA);
1251           ssl_hand_tree = proto_item_add_subtree(ti, ett_dtls_handshake);
1252         }
1253
1254       msg_type = tvb_get_guint8(tvb, offset);
1255       msg_type_str = try_val_to_str(msg_type, ssl_31_handshake_type);
1256
1257       if (!msg_type_str && !first_iteration)
1258         {
1259           /* only dissect / report messages if they're
1260            * either the first message in this record
1261            * or they're a valid message type
1262            */
1263           return;
1264         }
1265
1266       /* on second and later iterations, add comma to info col */
1267       if (!first_iteration)
1268         {
1269           col_append_str(pinfo->cinfo, COL_INFO, ", ");
1270         }
1271
1272       /*
1273        * Update our info string
1274        */
1275       col_append_str(pinfo->cinfo, COL_INFO, (msg_type_str != NULL)
1276             ? msg_type_str : "Encrypted Handshake Message");
1277
1278       if (ssl_hand_tree)
1279         proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_type,
1280                             tvb, offset, 1, msg_type);
1281       offset++;
1282
1283       length = tvb_get_ntoh24(tvb, offset);
1284       if (ssl_hand_tree)
1285         length_item = proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_length,
1286                                           tvb, offset, 3, length);
1287       offset += 3;
1288
1289       message_seq = tvb_get_ntohs(tvb,offset);
1290       if (ssl_hand_tree)
1291         proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_message_seq,
1292                             tvb, offset, 2, message_seq);
1293       offset += 2;
1294
1295       fragment_offset = tvb_get_ntoh24(tvb, offset);
1296       if (ssl_hand_tree)
1297         proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_fragment_offset,
1298                             tvb, offset, 3, fragment_offset);
1299       offset += 3;
1300
1301       fragment_length = tvb_get_ntoh24(tvb, offset);
1302       if (ssl_hand_tree)
1303         fragment_length_item = proto_tree_add_uint(ssl_hand_tree,
1304                                                    hf_dtls_handshake_fragment_length,
1305                                                    tvb, offset, 3,
1306                                                    fragment_length);
1307       offset += 3;
1308       proto_item_set_len(ti, fragment_length + 12);
1309
1310       fragmented = FALSE;
1311       if (fragment_length + fragment_offset > length)
1312         {
1313           if (fragment_offset == 0)
1314             {
1315               expert_add_info(pinfo, fragment_length_item, &ei_dtls_handshake_fragment_length_too_long);
1316             }
1317           else
1318             {
1319               fragmented = TRUE;
1320               expert_add_info(pinfo, fragment_length_item, &ei_dtls_handshake_fragment_past_end_msg);
1321             }
1322         }
1323       else if (fragment_length < length)
1324         {
1325           fragmented = TRUE;
1326
1327           /* Handle fragments of known message type */
1328           switch (msg_type) {
1329           case SSL_HND_HELLO_REQUEST:
1330           case SSL_HND_CLIENT_HELLO:
1331           case SSL_HND_HELLO_VERIFY_REQUEST:
1332           case SSL_HND_NEWSESSION_TICKET:
1333           case SSL_HND_SERVER_HELLO:
1334           case SSL_HND_CERTIFICATE:
1335           case SSL_HND_SERVER_KEY_EXCHG:
1336           case SSL_HND_CERT_REQUEST:
1337           case SSL_HND_SVR_HELLO_DONE:
1338           case SSL_HND_CERT_VERIFY:
1339           case SSL_HND_CLIENT_KEY_EXCHG:
1340           case SSL_HND_FINISHED:
1341             frag_hand = TRUE;
1342             break;
1343           default:
1344             /* Ignore encrypted handshake messages */
1345             frag_hand = FALSE;
1346             break;
1347           }
1348
1349           if (frag_hand)
1350             {
1351               /* Fragmented handshake message */
1352               pinfo->fragmented = TRUE;
1353
1354               /* Don't pass the reassembly code data that doesn't exist */
1355               tvb_ensure_bytes_exist(tvb, offset, fragment_length);
1356
1357               frag_msg = fragment_add(&dtls_reassembly_table,
1358                                       tvb, offset, pinfo, message_seq, NULL,
1359                                       fragment_offset, fragment_length, TRUE);
1360               /*
1361                * Do we already have a length for this reassembly?
1362                */
1363               reassembled_length = fragment_get_tot_len(&dtls_reassembly_table,
1364                                                         pinfo, message_seq, NULL);
1365               if (reassembled_length == 0)
1366                 {
1367                   /* No - set it to the length specified by this packet. */
1368                   fragment_set_tot_len(&dtls_reassembly_table,
1369                                        pinfo, message_seq, NULL, length);
1370                 }
1371               else
1372                 {
1373                   /* Yes - if this packet specifies a different length,
1374                      report an error. */
1375                   if (reassembled_length != length)
1376                     {
1377                       expert_add_info(pinfo, length_item, &ei_dtls_msg_len_diff_fragment);
1378                     }
1379                 }
1380
1381               if (frag_msg && (fragment_length + fragment_offset) == reassembled_length)
1382                 {
1383                   /* Reassembled */
1384                   new_tvb = process_reassembled_data(tvb, offset, pinfo,
1385                                                      "Reassembled DTLS",
1386                                                      frag_msg,
1387                                                      &dtls_frag_items,
1388                                                      NULL, tree);
1389                   frag_str = " (Reassembled)";
1390                 }
1391               else
1392                 {
1393                   frag_str = " (Fragment)";
1394                 }
1395
1396               col_append_str(pinfo->cinfo, COL_INFO, frag_str);
1397             }
1398         }
1399
1400       if (tree)
1401         {
1402           /* set the label text on the record layer expanding node */
1403           if (first_iteration)
1404             {
1405               proto_item_set_text(tree, "%s Record Layer: %s Protocol: %s%s",
1406                                   val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1407                                   val_to_str_const(content_type, ssl_31_content_type, "unknown"),
1408                                   (msg_type_str!=NULL) ? msg_type_str :
1409                                   "Encrypted Handshake Message",
1410                                   (frag_str!=NULL) ? frag_str : "");
1411             }
1412           else
1413             {
1414               proto_item_set_text(tree, "%s Record Layer: %s Protocol: %s%s",
1415                                   val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1416                                   val_to_str_const(content_type, ssl_31_content_type, "unknown"),
1417                                   "Multiple Handshake Messages",
1418                                   (frag_str!=NULL) ? frag_str : "");
1419             }
1420
1421           if (ssl_hand_tree)
1422             {
1423               /* set the text label on the subtree node */
1424               proto_item_set_text(ssl_hand_tree, "Handshake Protocol: %s%s",
1425                                   (msg_type_str != NULL) ? msg_type_str :
1426                                   "Encrypted Handshake Message",
1427                                   (frag_str!=NULL) ? frag_str : "");
1428             }
1429         }
1430
1431       /* if we don't have a valid handshake type, just quit dissecting */
1432       if (!msg_type_str)
1433         return;
1434
1435       if (ssl_hand_tree || ssl)
1436         {
1437           tvbuff_t *sub_tvb = NULL;
1438
1439           if (fragmented && !new_tvb)
1440             {
1441               /* Skip fragmented messages not reassembled yet */
1442               continue;
1443             }
1444
1445           if (new_tvb)
1446             {
1447               sub_tvb = new_tvb;
1448             }
1449           else
1450             {
1451               sub_tvb = tvb_new_subset(tvb, offset, fragment_length,
1452                                        fragment_length);
1453             }
1454
1455           /* now dissect the handshake message, if necessary */
1456           switch (msg_type) {
1457           case SSL_HND_HELLO_REQUEST:
1458             /* hello_request has no fields, so nothing to do! */
1459             break;
1460
1461           case SSL_HND_CLIENT_HELLO:
1462             dissect_dtls_hnd_cli_hello(sub_tvb, ssl_hand_tree, 0, length, ssl);
1463             break;
1464
1465           case SSL_HND_SERVER_HELLO:
1466             dissect_dtls_hnd_srv_hello(sub_tvb, ssl_hand_tree, 0, length, ssl);
1467             break;
1468
1469           case SSL_HND_HELLO_VERIFY_REQUEST:
1470             dissect_dtls_hnd_hello_verify_request(sub_tvb, ssl_hand_tree, 0,  ssl);
1471             break;
1472
1473           case SSL_HND_NEWSESSION_TICKET:
1474             dissect_dtls_hnd_new_ses_ticket(sub_tvb, ssl_hand_tree, 0, length);
1475             break;
1476
1477           case SSL_HND_CERTIFICATE:
1478             dissect_dtls_hnd_cert(sub_tvb, ssl_hand_tree, 0, pinfo);
1479             break;
1480
1481           case SSL_HND_SERVER_KEY_EXCHG:
1482             switch (ssl_get_keyex_alg(conv_cipher)) {
1483             case KEX_DH:
1484                 dissect_dtls_hnd_srv_keyex_dh(tvb, ssl_hand_tree, offset, length);
1485                 break;
1486             case KEX_RSA:
1487                 dissect_dtls_hnd_srv_keyex_rsa(tvb, ssl_hand_tree, offset, length);
1488                 break;
1489             case KEX_ECDH:
1490                 dissect_dtls_hnd_srv_keyex_ecdh(tvb, ssl_hand_tree, offset, length);
1491                 break;
1492             case KEX_RSA_PSK:
1493             case KEX_PSK:
1494                 dissect_dtls_hnd_srv_keyex_psk(tvb, ssl_hand_tree, offset, length);
1495                 break;
1496             default:
1497                 break;
1498             }
1499             break;
1500
1501           case SSL_HND_CERT_REQUEST:
1502             dissect_dtls_hnd_cert_req(sub_tvb, ssl_hand_tree, 0, pinfo, conv_version);
1503             break;
1504
1505           case SSL_HND_SVR_HELLO_DONE:
1506             /* server_hello_done has no fields, so nothing to do! */
1507             break;
1508
1509           case SSL_HND_CERT_VERIFY:
1510             /* unimplemented */
1511             break;
1512
1513           case SSL_HND_CLIENT_KEY_EXCHG:
1514             switch (ssl_get_keyex_alg(conv_cipher)) {
1515             case KEX_DH:
1516                     dissect_dtls_hnd_cli_keyex_dh(tvb, ssl_hand_tree, offset, length);
1517                     break;
1518             case KEX_RSA:
1519                     dissect_dtls_hnd_cli_keyex_rsa(tvb, ssl_hand_tree, offset, length);
1520                     break;
1521             case KEX_ECDH:
1522                     dissect_dtls_hnd_cli_keyex_ecdh(tvb, ssl_hand_tree, offset, length);
1523                     break;
1524             case KEX_PSK:
1525                     dissect_dtls_hnd_cli_keyex_psk(tvb, ssl_hand_tree, offset, length);
1526                     break;
1527             case KEX_RSA_PSK:
1528                     dissect_dtls_hnd_cli_keyex_rsa_psk(tvb, ssl_hand_tree, offset, length);
1529                     break;
1530             default:
1531                     break;
1532             }
1533             {
1534               /* here we can have all the data to build session key */
1535               StringInfo encrypted_pre_master;
1536               gint ret;
1537               guint encrlen = length, skip;
1538               skip = 0;
1539
1540               if (!ssl)
1541                 break;
1542
1543               /* check for required session data */
1544               ssl_debug_printf("dissect_dtls_handshake found SSL_HND_CLIENT_KEY_EXCHG, state %X\n",
1545                                ssl->state);
1546               if ((ssl->state & (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) !=
1547                   (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) {
1548                 ssl_debug_printf("dissect_dtls_handshake not enough data to generate key (required state %X)\n",
1549                                  (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION));
1550                 break;
1551               }
1552
1553               /* Skip leading two bytes length field. Older openssl's DTLS implementation seems not to have this field.
1554                * See implementation note in RFC 4346 section 7.4.7.1
1555                */
1556               if (ssl->cipher_suite.kex==KEX_RSA && ssl->version_netorder != DTLSV1DOT0_VERSION_NOT) {
1557                 encrlen = tvb_get_ntohs(tvb, offset);
1558                 skip = 2;
1559                 if (encrlen > length - 2) {
1560                   ssl_debug_printf("dissect_dtls_handshake wrong encrypted length (%d max %d)\n", encrlen, length);
1561                   break;
1562                 }
1563               }
1564
1565               encrypted_pre_master.data = (guchar *)se_alloc(encrlen);
1566               encrypted_pre_master.data_len = encrlen;
1567               tvb_memcpy(tvb, encrypted_pre_master.data, offset+skip, encrlen);
1568
1569               if (!ssl->private_key) {
1570                 ssl_debug_printf("dissect_dtls_handshake can't find private key\n");
1571                 break;
1572               }
1573
1574               /* go with ssl key processing; encrypted_pre_master
1575                * will be used for master secret store*/
1576               ret = ssl_decrypt_pre_master_secret(ssl, &encrypted_pre_master, ssl->private_key);
1577               if (ret < 0) {
1578                 ssl_debug_printf("dissect_dtls_handshake can't decrypt pre master secret\n");
1579                 break;
1580               }
1581               if (ssl_generate_keyring_material(ssl)<0) {
1582                 ssl_debug_printf("dissect_dtls_handshake can't generate keyring material\n");
1583                 break;
1584               }
1585               ssl->state |= SSL_HAVE_SESSION_KEY;
1586               ssl_save_session(ssl, dtls_session_hash);
1587               ssl_debug_printf("dissect_dtls_handshake session keys successfully generated\n");
1588             }
1589             break;
1590
1591           case SSL_HND_FINISHED:
1592             dissect_dtls_hnd_finished(sub_tvb, ssl_hand_tree,
1593                                       0, conv_version);
1594             break;
1595           }
1596
1597         }
1598     }
1599 }
1600
1601 /* dissects the heartbeat message, filling in the tree */
1602 static void
1603 dissect_dtls_heartbeat(tvbuff_t *tvb, packet_info *pinfo,
1604                        proto_tree *tree, guint32 offset,
1605                        guint* conv_version, guint32 record_length)
1606 {
1607   /*     struct {
1608    *         HeartbeatMessageType type;
1609    *         uint16 payload_length;
1610    *         opaque payload;
1611    *         opaque padding;
1612    *     } HeartbeatMessage;
1613    */
1614
1615   proto_tree  *ti;
1616   proto_tree  *dtls_heartbeat_tree;
1617   const gchar *type;
1618   guint8       byte;
1619   guint16      payload_length;
1620   guint16      padding_length;
1621
1622   dtls_heartbeat_tree = NULL;
1623
1624   if (tree) {
1625     ti = proto_tree_add_item(tree, hf_dtls_heartbeat_message, tvb,
1626                              offset, record_length - 32, ENC_NA);
1627     dtls_heartbeat_tree = proto_item_add_subtree(ti, ett_dtls_heartbeat);
1628   }
1629
1630   /*
1631    * set the record layer label
1632    */
1633
1634   /* first lookup the names for the message type and the payload length */
1635   byte = tvb_get_guint8(tvb, offset);
1636   type = try_val_to_str(byte, tls_heartbeat_type);
1637
1638   payload_length = tvb_get_ntohs(tvb, offset + 1);
1639   padding_length = record_length - 3 - payload_length;
1640
1641   /* now set the text in the record layer line */
1642   if (type && (payload_length <= record_length - 16 - 3)) {
1643     col_append_fstr(pinfo->cinfo, COL_INFO, "Heartbeat %s", type);
1644   } else {
1645     col_append_str(pinfo->cinfo, COL_INFO, "Encrypted Heartbeat");
1646   }
1647
1648   if (tree) {
1649     if (type && (payload_length <= record_length - 16 - 3)) {
1650       proto_item_set_text(tree, "%s Record Layer: Heartbeat "
1651                                 "%s",
1652                                 val_to_str_const(*conv_version, ssl_version_short_names, "SSL"),
1653                                 type);
1654       proto_tree_add_item(dtls_heartbeat_tree, hf_dtls_heartbeat_message_type,
1655                           tvb, offset, 1, ENC_BIG_ENDIAN);
1656       offset += 1;
1657       proto_tree_add_uint(dtls_heartbeat_tree, hf_dtls_heartbeat_message_payload_length,
1658                           tvb, offset, 2, payload_length);
1659       offset += 2;
1660       proto_tree_add_bytes_format(dtls_heartbeat_tree, hf_dtls_heartbeat_message_payload,
1661                                   tvb, offset, payload_length,
1662                                   NULL, "Payload (%u byte%s)",
1663                                   payload_length,
1664                                   plurality(payload_length, "", "s"));
1665       offset += payload_length;
1666       proto_tree_add_bytes_format(dtls_heartbeat_tree, hf_dtls_heartbeat_message_padding,
1667                                   tvb, offset, padding_length,
1668                                   NULL, "Padding and HMAC (%u byte%s)",
1669                                   padding_length,
1670                                   plurality(padding_length, "", "s"));
1671     } else {
1672       proto_item_set_text(tree,
1673                          "%s Record Layer: Encrypted Heartbeat",
1674                          val_to_str_const(*conv_version, ssl_version_short_names, "SSL"));
1675       proto_item_set_text(dtls_heartbeat_tree,
1676                           "Encrypted Heartbeat Message");
1677     }
1678   }
1679 }
1680
1681 static gint
1682 dissect_dtls_hnd_hello_common(tvbuff_t *tvb, proto_tree *tree,
1683                               guint32 offset, SslDecryptSession* ssl, gint from_server)
1684 {
1685   /* show the client's random challenge */
1686   nstime_t gmt_unix_time;
1687   guint8   session_id_length;
1688   proto_item *ti_rnd;
1689   proto_tree *dtls_rnd_tree = NULL;
1690
1691   if (tree || ssl)
1692   {
1693     if (ssl)
1694     {
1695       /* get proper peer information*/
1696       StringInfo* rnd;
1697       if (from_server)
1698         rnd = &ssl->server_random;
1699       else
1700         rnd = &ssl->client_random;
1701
1702       /* get provided random for keyring generation*/
1703       tvb_memcpy(tvb, rnd->data, offset, 32);
1704       rnd->data_len = 32;
1705       if (from_server)
1706         ssl->state |= SSL_SERVER_RANDOM;
1707       else
1708         ssl->state |= SSL_CLIENT_RANDOM;
1709       ssl_debug_printf("dissect_dtls_hnd_hello_common found random state %X\n",
1710                        ssl->state);
1711     }
1712
1713     if (tree)
1714     {
1715         ti_rnd = proto_tree_add_text(tree, tvb, offset, 32, "Random");
1716         dtls_rnd_tree = proto_item_add_subtree(ti_rnd, ett_dtls_random);
1717     }
1718
1719     /* show the time */
1720     if (dtls_rnd_tree)
1721     {
1722       gmt_unix_time.secs  = tvb_get_ntohl(tvb, offset);
1723       gmt_unix_time.nsecs = 0;
1724       proto_tree_add_time(dtls_rnd_tree, hf_dtls_handshake_random_time,
1725                           tvb, offset, 4, &gmt_unix_time);
1726     }
1727     offset += 4;
1728
1729     /* show the random bytes */
1730     if (dtls_rnd_tree)
1731       proto_tree_add_item(dtls_rnd_tree, hf_dtls_handshake_random_bytes,
1732                           tvb, offset, 28, ENC_NA);
1733     offset += 28;
1734
1735     /* show the session id */
1736     session_id_length = tvb_get_guint8(tvb, offset);
1737     if (tree)
1738       proto_tree_add_item(tree, hf_dtls_handshake_session_id_len,
1739                           tvb, offset, 1, ENC_BIG_ENDIAN);
1740     offset++;
1741     if (ssl)
1742     {
1743       /* check stored session id info */
1744       if (from_server && (session_id_length == ssl->session_id.data_len) &&
1745           (tvb_memeql(tvb, offset, ssl->session_id.data, session_id_length) == 0))
1746       {
1747         /* client/server id match: try to restore a previous cached session*/
1748         ssl_restore_session(ssl, dtls_session_hash);
1749       }
1750       else {
1751         tvb_memcpy(tvb,ssl->session_id.data, offset, session_id_length);
1752         ssl->session_id.data_len = session_id_length;
1753       }
1754     }
1755     if (tree && session_id_length > 0)
1756       proto_tree_add_bytes_format(tree, hf_dtls_handshake_session_id,
1757                                   tvb, offset, session_id_length,
1758                                   NULL, "Session ID (%u byte%s)",
1759                                   session_id_length,
1760                                   plurality(session_id_length, "", "s"));
1761     offset += session_id_length;
1762   }
1763
1764   /* XXXX */
1765   return offset;
1766 }
1767
1768 static gint
1769 dissect_dtls_hnd_hello_ext(tvbuff_t *tvb,
1770                            proto_tree *tree, guint32 offset, guint32 left)
1771 {
1772   guint16     extension_length;
1773   guint16     ext_type;
1774   guint16     ext_len;
1775   proto_item *pi;
1776   proto_tree *ext_tree;
1777
1778   if (left < 2)
1779     return offset;
1780
1781   extension_length = tvb_get_ntohs(tvb, offset);
1782   proto_tree_add_uint(tree, hf_dtls_handshake_extensions_len,
1783                       tvb, offset, 2, extension_length);
1784   offset += 2;
1785   left   -= 2;
1786
1787   while (left >= 4)
1788     {
1789       ext_type = tvb_get_ntohs(tvb, offset);
1790       ext_len  = tvb_get_ntohs(tvb, offset + 2);
1791
1792       pi = proto_tree_add_text(tree, tvb, offset, 4 + ext_len,
1793                                "Extension: %s",
1794                                val_to_str(ext_type,
1795                                           tls_hello_extension_types,
1796                                           "Unknown %u"));
1797       ext_tree = proto_item_add_subtree(pi, ett_dtls_extension);
1798       if (!ext_tree)
1799         ext_tree = tree;
1800
1801       proto_tree_add_uint(ext_tree, hf_dtls_handshake_extension_type,
1802                           tvb, offset, 2, ext_type);
1803       offset += 2;
1804
1805       proto_tree_add_uint(ext_tree, hf_dtls_handshake_extension_len,
1806                           tvb, offset, 2, ext_len);
1807       offset += 2;
1808
1809       switch (ext_type) {
1810       case SSL_HND_HELLO_EXT_HEARTBEAT:
1811           proto_tree_add_item(ext_tree, hf_dtls_heartbeat_extension_mode,
1812                               tvb, offset, 1, ENC_BIG_ENDIAN);
1813           offset += ext_len;
1814           break;
1815       default:
1816           proto_tree_add_bytes_format(ext_tree, hf_dtls_handshake_extension_data,
1817                                       tvb, offset, ext_len, NULL,
1818                                       "Data (%u byte%s)",
1819                                       ext_len, plurality(ext_len, "", "s"));
1820           offset += ext_len;
1821           break;
1822       }
1823
1824       left   -= 2 + 2 + ext_len;
1825     }
1826
1827   return offset;
1828 }
1829
1830 static void
1831 dissect_dtls_hnd_cli_hello(tvbuff_t *tvb,
1832                            proto_tree *tree, guint32 offset, guint32 length,
1833                            SslDecryptSession*ssl)
1834 {
1835   /* struct {
1836    *     ProtocolVersion client_version;
1837    *     Random random;
1838    *     SessionID session_id;
1839    *     opaque cookie<0..32>;                   //new field
1840    *     CipherSuite cipher_suites<2..2^16-1>;
1841    *     CompressionMethod compression_methods<1..2^8-1>;
1842    *     Extension client_hello_extension_list<0..2^16-1>;
1843    * } ClientHello;
1844    *
1845    */
1846
1847   proto_tree *ti;
1848   proto_tree *cs_tree;
1849   guint16     cipher_suite_length;
1850   guint8      compression_methods_length;
1851   guint8      compression_method;
1852   guint16     start_offset   = offset;
1853   guint8      cookie_length;
1854
1855   if (tree || ssl)
1856     {
1857       /* show the client version */
1858       if (tree)
1859         proto_tree_add_item(tree, hf_dtls_handshake_client_version, tvb,
1860                             offset, 2, ENC_BIG_ENDIAN);
1861       offset += 2;
1862
1863       /* show the fields in common with server hello */
1864       offset = dissect_dtls_hnd_hello_common(tvb, tree, offset, ssl, 0);
1865
1866       /* look for a cookie */
1867       cookie_length = tvb_get_guint8(tvb, offset);
1868       if (!tree)
1869         return;
1870
1871       proto_tree_add_uint(tree, hf_dtls_handshake_cookie_len,
1872                           tvb, offset, 1, cookie_length);
1873       offset ++;            /* skip opaque length */
1874
1875       if (cookie_length > 0)
1876         {
1877           proto_tree_add_item(tree, hf_dtls_handshake_cookie, tvb, offset,
1878                               cookie_length, ENC_NA);
1879           offset += cookie_length;
1880         }
1881
1882       /* tell the user how many cipher suites there are */
1883       cipher_suite_length = tvb_get_ntohs(tvb, offset);
1884
1885       proto_tree_add_uint(tree, hf_dtls_handshake_cipher_suites_len,
1886                           tvb, offset, 2, cipher_suite_length);
1887       offset += 2;            /* skip opaque length */
1888
1889       if (cipher_suite_length > 0)
1890         {
1891           tvb_ensure_bytes_exist(tvb, offset, cipher_suite_length);
1892           ti = proto_tree_add_none_format(tree,
1893                                           hf_dtls_handshake_cipher_suites,
1894                                           tvb, offset, cipher_suite_length,
1895                                           "Cipher Suites (%u suite%s)",
1896                                           cipher_suite_length / 2,
1897                                           plurality(cipher_suite_length/2, "", "s"));
1898
1899           /* make this a subtree */
1900           cs_tree = proto_item_add_subtree(ti, ett_dtls_cipher_suites);
1901           if (!cs_tree)
1902             {
1903               cs_tree = tree; /* failsafe */
1904             }
1905
1906           while (cipher_suite_length > 0)
1907             {
1908               proto_tree_add_item(cs_tree, hf_dtls_handshake_cipher_suite,
1909                                   tvb, offset, 2, ENC_BIG_ENDIAN);
1910               offset += 2;
1911               cipher_suite_length -= 2;
1912             }
1913         }
1914
1915       /* tell the user how man compression methods there are */
1916       compression_methods_length = tvb_get_guint8(tvb, offset);
1917       proto_tree_add_uint(tree, hf_dtls_handshake_comp_methods_len,
1918                           tvb, offset, 1, compression_methods_length);
1919       offset++;
1920
1921       if (compression_methods_length > 0)
1922         {
1923           tvb_ensure_bytes_exist(tvb, offset, compression_methods_length);
1924           ti = proto_tree_add_none_format(tree,
1925                                           hf_dtls_handshake_comp_methods,
1926                                           tvb, offset, compression_methods_length,
1927                                           "Compression Methods (%u method%s)",
1928                                           compression_methods_length,
1929                                           plurality(compression_methods_length,
1930                                                     "", "s"));
1931
1932           /* make this a subtree */
1933           cs_tree = proto_item_add_subtree(ti, ett_dtls_comp_methods);
1934           if (!cs_tree)
1935             {
1936               cs_tree = tree; /* failsafe */
1937             }
1938
1939           while (compression_methods_length > 0)
1940             {
1941               compression_method = tvb_get_guint8(tvb, offset);
1942               if (compression_method < 64)
1943                 proto_tree_add_uint(cs_tree, hf_dtls_handshake_comp_method,
1944                                     tvb, offset, 1, compression_method);
1945               else if (compression_method > 63 && compression_method < 193)
1946                 proto_tree_add_text(cs_tree, tvb, offset, 1,
1947                                     "Compression Method: Reserved - to be assigned by IANA (%u)",
1948                                     compression_method);
1949               else
1950                 proto_tree_add_text(cs_tree, tvb, offset, 1,
1951                                     "Compression Method: Private use range (%u)",
1952                                     compression_method);
1953               offset++;
1954               compression_methods_length--;
1955             }
1956         }
1957
1958       if (length > offset - start_offset)
1959         {
1960           dissect_dtls_hnd_hello_ext(tvb, tree, offset,
1961                                               length -
1962                                               (offset - start_offset));
1963         }
1964     }
1965 }
1966
1967 static int
1968 dissect_dtls_hnd_srv_hello(tvbuff_t *tvb,
1969                            proto_tree *tree, guint32 offset, guint32 length, SslDecryptSession* ssl)
1970 {
1971   /* struct {
1972    *     ProtocolVersion server_version;
1973    *     Random random;
1974    *     SessionID session_id;
1975    *     CipherSuite cipher_suite;
1976    *     CompressionMethod compression_method;
1977    *     Extension server_hello_extension_list<0..2^16-1>;
1978    * } ServerHello;
1979    */
1980
1981   guint16 start_offset;
1982
1983   start_offset = offset;
1984
1985   if (tree || ssl)
1986     {
1987       /* show the server version */
1988       if (tree)
1989         proto_tree_add_item(tree, hf_dtls_handshake_server_version, tvb,
1990                             offset, 2, ENC_BIG_ENDIAN);
1991       offset += 2;
1992
1993       /* first display the elements conveniently in
1994        * common with client hello
1995        */
1996       offset = dissect_dtls_hnd_hello_common(tvb, tree, offset, ssl, 1);
1997
1998       /* PAOLO: handle session cipher suite  */
1999       if (ssl) {
2000         /* store selected cipher suite for decryption */
2001         ssl->cipher = tvb_get_ntohs(tvb, offset);
2002         if (ssl_find_cipher(ssl->cipher,&ssl->cipher_suite) < 0) {
2003           ssl_debug_printf("dissect_dtls_hnd_srv_hello can't find cipher suite %X\n", ssl->cipher);
2004           goto no_cipher;
2005         }
2006
2007         ssl->state |= SSL_CIPHER;
2008         ssl_debug_printf("dissect_dtls_hnd_srv_hello found cipher %X, state %X\n",
2009                          ssl->cipher, ssl->state);
2010
2011         /* if we have restored a session now we can have enough material
2012          * to build session key, check it out*/
2013         if ((ssl->state &
2014              (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET)) !=
2015             (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET)) {
2016           ssl_debug_printf("dissect_dtls_hnd_srv_hello not enough data to generate key (required state %X)\n",
2017                            (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET));
2018           goto no_cipher;
2019         }
2020
2021         ssl_debug_printf("dissect_dtls_hnd_srv_hello trying to generate keys\n");
2022         if (ssl_generate_keyring_material(ssl)<0) {
2023           ssl_debug_printf("dissect_dtls_hnd_srv_hello can't generate keyring material\n");
2024           goto no_cipher;
2025         }
2026         ssl->state |= SSL_HAVE_SESSION_KEY;
2027       }
2028     no_cipher:
2029       if (ssl) {
2030         /* store selected compression method for decompression */
2031         ssl->compression = tvb_get_guint8(tvb, offset+2);
2032       }
2033       if (!tree)
2034         return offset;
2035
2036       /* now the server-selected cipher suite */
2037       proto_tree_add_item(tree, hf_dtls_handshake_cipher_suite,
2038                           tvb, offset, 2, ENC_BIG_ENDIAN);
2039       offset += 2;
2040
2041       /* and the server-selected compression method */
2042       proto_tree_add_item(tree, hf_dtls_handshake_comp_method,
2043                           tvb, offset, 1, ENC_BIG_ENDIAN);
2044       offset++;
2045
2046       if (length > offset - start_offset)
2047         {
2048           offset = dissect_dtls_hnd_hello_ext(tvb, tree, offset,
2049                                               length -
2050                                               (offset - start_offset));
2051         }
2052     }
2053     return offset;
2054 }
2055
2056 static int
2057 dissect_dtls_hnd_hello_verify_request(tvbuff_t *tvb, proto_tree *tree,
2058                                       guint32 offset, SslDecryptSession* ssl)
2059 {
2060   /*
2061    * struct {
2062    *    ProtocolVersion server_version;
2063    *    opaque cookie<0..32>;
2064    * } HelloVerifyRequest;
2065    */
2066
2067   guint8 cookie_length;
2068
2069
2070   if (tree || ssl)
2071     {
2072       /* show the client version */
2073       if (tree)
2074         proto_tree_add_item(tree, hf_dtls_handshake_server_version, tvb,
2075                             offset, 2, ENC_BIG_ENDIAN);
2076       offset += 2;
2077
2078
2079       /* look for a cookie */
2080       cookie_length = tvb_get_guint8(tvb, offset);
2081       if (!tree)
2082         return offset;
2083
2084       proto_tree_add_uint(tree, hf_dtls_handshake_cookie_len,
2085                           tvb, offset, 1, cookie_length);
2086       offset ++;            /* skip opaque length */
2087
2088       if (cookie_length > 0)
2089         {
2090           proto_tree_add_bytes_format(tree, hf_dtls_handshake_cookie,
2091                                       tvb, offset, cookie_length,
2092                                       NULL, "Cookie (%u byte%s)",
2093                                       cookie_length,
2094                                       plurality(cookie_length, "", "s"));
2095           offset += cookie_length;
2096         }
2097     }
2098     return offset;
2099 }
2100
2101 static void
2102 dissect_dtls_hnd_new_ses_ticket(tvbuff_t *tvb,
2103                            proto_tree *tree, guint32 offset, guint32 length)
2104 {
2105     guint nst_len;
2106     proto_item *ti;
2107     proto_tree *subtree;
2108
2109
2110     nst_len = tvb_get_ntohs(tvb, offset+4);
2111     if (6 + nst_len != length) {
2112         return;
2113     }
2114
2115     ti = proto_tree_add_text(tree, tvb, offset, 6+nst_len, "TLS Session Ticket");
2116     subtree = proto_item_add_subtree(ti, ett_dtls_new_ses_ticket);
2117
2118     proto_tree_add_item(subtree, hf_dtls_handshake_session_ticket_lifetime_hint,
2119                         tvb, offset, 4, ENC_BIG_ENDIAN);
2120     offset += 4;
2121
2122     proto_tree_add_uint(subtree, hf_dtls_handshake_session_ticket_len,
2123         tvb, offset, 2, nst_len);
2124     /* Content depends on implementation, so just show data! */
2125     proto_tree_add_item(subtree, hf_dtls_handshake_session_ticket,
2126             tvb, offset + 2, nst_len, ENC_NA);
2127 }
2128
2129 static void
2130 dissect_dtls_hnd_cert(tvbuff_t *tvb,
2131                       proto_tree *tree, guint32 offset, packet_info *pinfo)
2132 {
2133
2134   /* opaque ASN.1Cert<2^24-1>;
2135    *
2136    * struct {
2137    *     ASN.1Cert certificate_list<1..2^24-1>;
2138    * } Certificate;
2139    */
2140
2141   guint32     certificate_list_length;
2142   proto_tree *ti;
2143   proto_tree *subtree;
2144   asn1_ctx_t  asn1_ctx;
2145
2146   asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
2147
2148   if (tree)
2149     {
2150       certificate_list_length = tvb_get_ntoh24(tvb, offset);
2151       proto_tree_add_uint(tree, hf_dtls_handshake_certificates_len,
2152                           tvb, offset, 3, certificate_list_length);
2153       offset += 3;            /* 24-bit length value */
2154
2155       if (certificate_list_length > 0)
2156         {
2157           tvb_ensure_bytes_exist(tvb, offset, certificate_list_length);
2158           ti = proto_tree_add_none_format(tree,
2159                                           hf_dtls_handshake_certificates,
2160                                           tvb, offset, certificate_list_length,
2161                                           "Certificates (%u byte%s)",
2162                                           certificate_list_length,
2163                                           plurality(certificate_list_length,
2164                                                     "", "s"));
2165
2166           /* make it a subtree */
2167           subtree = proto_item_add_subtree(ti, ett_dtls_certs);
2168           if (!subtree)
2169             {
2170               subtree = tree; /* failsafe */
2171             }
2172
2173           /* iterate through each certificate */
2174           while (certificate_list_length > 0)
2175             {
2176               /* get the length of the current certificate */
2177               guint32 cert_length = tvb_get_ntoh24(tvb, offset);
2178               certificate_list_length -= 3 + cert_length;
2179
2180               proto_tree_add_item(subtree, hf_dtls_handshake_certificate_len,
2181                                   tvb, offset, 3, ENC_BIG_ENDIAN);
2182               offset += 3;
2183
2184               dissect_x509af_Certificate(FALSE, tvb, offset, &asn1_ctx, subtree, hf_dtls_handshake_certificate);
2185               offset += cert_length;
2186             }
2187         }
2188
2189     }
2190 }
2191
2192 static void
2193 dissect_dtls_hnd_cert_req(tvbuff_t *tvb,
2194                           proto_tree *tree, guint32 offset, packet_info *pinfo,
2195                           const guint *conv_version)
2196 {
2197   /*
2198    *    enum {
2199    *        rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
2200    *        (255)
2201    *    } ClientCertificateType;
2202    *
2203    *    opaque DistinguishedName<1..2^16-1>;
2204    *
2205    *    struct {
2206    *        ClientCertificateType certificate_types<1..2^8-1>;
2207    *        DistinguishedName certificate_authorities<3..2^16-1>;
2208    *    } CertificateRequest;
2209    *
2210    *
2211    * As per TLSv1.2 (RFC 5246) the format has changed to:
2212    *
2213    *    enum {
2214    *        rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
2215    *        rsa_ephemeral_dh_RESERVED(5), dss_ephemeral_dh_RESERVED(6),
2216    *        fortezza_dms_RESERVED(20), (255)
2217    *    } ClientCertificateType;
2218    *
2219    *    enum {
2220    *        none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
2221    *        sha512(6), (255)
2222    *    } HashAlgorithm;
2223    *
2224    *    enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
2225    *      SignatureAlgorithm;
2226    *
2227    *    struct {
2228    *          HashAlgorithm hash;
2229    *          SignatureAlgorithm signature;
2230    *    } SignatureAndHashAlgorithm;
2231    *
2232    *    SignatureAndHashAlgorithm
2233    *      supported_signature_algorithms<2..2^16-2>;
2234    *
2235    *    opaque DistinguishedName<1..2^16-1>;
2236    *
2237    *    struct {
2238    *        ClientCertificateType certificate_types<1..2^8-1>;
2239    *        SignatureAndHashAlgorithm
2240    *          supported_signature_algorithms<2^16-1>;
2241    *        DistinguishedName certificate_authorities<0..2^16-1>;
2242    *    } CertificateRequest;
2243    *
2244    */
2245
2246   proto_tree *ti, *ti2;
2247   proto_tree *subtree;
2248   proto_tree *saved_subtree;
2249   guint8      cert_types_count;
2250   gint        sh_alg_length;
2251   guint16     sig_hash_alg;
2252   gint        dnames_length;
2253   asn1_ctx_t  asn1_ctx;
2254
2255   asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
2256
2257   if (tree)
2258     {
2259       cert_types_count = tvb_get_guint8(tvb, offset);
2260       proto_tree_add_uint(tree, hf_dtls_handshake_cert_types_count,
2261                           tvb, offset, 1, cert_types_count);
2262       offset++;
2263
2264       if (cert_types_count > 0)
2265         {
2266           ti = proto_tree_add_none_format(tree,
2267                                           hf_dtls_handshake_cert_types,
2268                                           tvb, offset, cert_types_count,
2269                                           "Certificate types (%u type%s)",
2270                                           cert_types_count,
2271                                           plurality(cert_types_count, "", "s"));
2272           subtree = proto_item_add_subtree(ti, ett_dtls_cert_types);
2273           if (!subtree)
2274             {
2275               subtree = tree;
2276             }
2277
2278           while (cert_types_count > 0)
2279             {
2280               proto_tree_add_item(subtree, hf_dtls_handshake_cert_type,
2281                                   tvb, offset, 1, ENC_BIG_ENDIAN);
2282               offset++;
2283               cert_types_count--;
2284             }
2285         }
2286
2287       switch (*conv_version) {
2288       case SSL_VER_DTLS1DOT2:
2289           sh_alg_length = tvb_get_ntohs(tvb, offset);
2290           ti2 = proto_tree_add_uint(tree, hf_dtls_handshake_sig_hash_alg_len,
2291                               tvb, offset, 2, sh_alg_length);
2292           offset += 2;
2293
2294           if (sh_alg_length > 0)
2295             {
2296               ti = proto_tree_add_none_format(tree,
2297                                               hf_dtls_handshake_sig_hash_algs,
2298                                               tvb, offset, sh_alg_length,
2299                                               "Signature Hash Algorithms (%u algorithm%s)",
2300                                               sh_alg_length/2,
2301                                               plurality(sh_alg_length/2, "", "s"));
2302               subtree = proto_item_add_subtree(ti, ett_dtls_sig_hash_algs);
2303               if (!subtree)
2304                 {
2305                   subtree = tree;
2306                 }
2307
2308               if (sh_alg_length % 2) {
2309                   expert_add_info_format_text(pinfo, ti2, &ei_dtls_handshake_sig_hash_alg_len_bad,
2310                       "Signature Hash Algorithm length (%d) must be a multiple of 2",
2311                       sh_alg_length);
2312                   return;
2313                 }
2314
2315
2316               while (sh_alg_length > 0)
2317                 {
2318                   saved_subtree = subtree;
2319
2320                   sig_hash_alg = tvb_get_ntohs(tvb, offset);
2321                   ti = proto_tree_add_uint(subtree, hf_dtls_handshake_sig_hash_alg,
2322                                       tvb, offset, 2, sig_hash_alg);
2323                   subtree = proto_item_add_subtree(ti, ett_dtls_sig_hash_alg);
2324                   if (!subtree)
2325                   {
2326                       subtree = saved_subtree;
2327                   }
2328
2329                   proto_tree_add_item(subtree, hf_dtls_handshake_sig_hash_hash,
2330                                   tvb, offset, 1, ENC_BIG_ENDIAN);
2331                   proto_tree_add_item(subtree, hf_dtls_handshake_sig_hash_sig,
2332                                   tvb, offset+1, 1, ENC_BIG_ENDIAN);
2333
2334                   subtree = saved_subtree;
2335
2336                   offset += 2;
2337                   sh_alg_length -= 2;
2338                 }
2339             }
2340           break;
2341
2342       default:
2343           break;
2344         }
2345
2346       dnames_length = tvb_get_ntohs(tvb, offset);
2347       proto_tree_add_uint(tree, hf_dtls_handshake_dnames_len,
2348                           tvb, offset, 2, dnames_length);
2349       offset += 2;
2350
2351       if (dnames_length > 0)
2352         {
2353           tvb_ensure_bytes_exist(tvb, offset, dnames_length);
2354           ti = proto_tree_add_none_format(tree,
2355                                           hf_dtls_handshake_dnames,
2356                                           tvb, offset, dnames_length,
2357                                           "Distinguished Names (%d byte%s)",
2358                                           dnames_length,
2359                                           plurality(dnames_length, "", "s"));
2360           subtree = proto_item_add_subtree(ti, ett_dtls_dnames);
2361           if (!subtree)
2362             {
2363               subtree = tree;
2364             }
2365
2366           while (dnames_length > 0)
2367             {
2368               /* get the length of the current certificate */
2369               guint16 name_length;
2370               name_length = tvb_get_ntohs(tvb, offset);
2371               dnames_length -= 2 + name_length;
2372
2373               proto_tree_add_item(subtree, hf_dtls_handshake_dname_len,
2374                                   tvb, offset, 2, ENC_BIG_ENDIAN);
2375               offset += 2;
2376
2377               tvb_ensure_bytes_exist(tvb, offset, name_length);
2378
2379               (void)dissect_x509if_DistinguishedName(FALSE, tvb, offset, &asn1_ctx, subtree, hf_dtls_handshake_dname);
2380
2381               offset += name_length;
2382             }
2383         }
2384     }
2385
2386 }
2387
2388
2389 static void
2390 dissect_dtls_hnd_srv_keyex_ecdh(tvbuff_t *tvb, proto_tree *tree,
2391                               guint32 offset, guint32 length)
2392 {
2393     gint        curve_type, curve_type_offset;
2394     gint        named_curve, named_curve_offset;
2395     gint        point_len, point_len_offset;
2396     gint        sig_len, sig_len_offset;
2397     proto_item *ti_ecdh;
2398     proto_tree *ssl_ecdh_tree;
2399     guint32     orig_offset;
2400
2401     orig_offset = offset;
2402
2403     curve_type_offset = offset;
2404     curve_type = tvb_get_guint8(tvb, offset);
2405     if (curve_type != 3)
2406         return; /* only named_curves are supported */
2407     offset += 1;
2408     if ((offset - orig_offset) > length) {
2409         return;
2410     }
2411
2412     named_curve_offset = offset;
2413     named_curve = tvb_get_ntohs(tvb, offset);
2414     offset += 2;
2415     if ((offset - orig_offset) > length) {
2416         return;
2417     }
2418
2419     point_len_offset = offset;
2420     point_len = tvb_get_guint8(tvb, offset);
2421     if ((offset + point_len - orig_offset) > length) {
2422         return;
2423     }
2424     offset += 1 + point_len;
2425
2426     sig_len_offset = offset;
2427     sig_len = tvb_get_ntohs(tvb, offset);
2428     offset += 2 + sig_len;
2429     if ((offset - orig_offset) != length) {
2430         /* Lengths don't line up (wasn't what we expected?) */
2431         return;
2432     }
2433
2434     ti_ecdh = proto_tree_add_text(tree, tvb, orig_offset,
2435                 (offset - orig_offset), "EC Diffie-Hellman Server Params");
2436     ssl_ecdh_tree = proto_item_add_subtree(ti_ecdh, ett_dtls_keyex_params);
2437
2438     /* curve_type */
2439     proto_tree_add_uint(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_curve_type,
2440         tvb, curve_type_offset, 1, curve_type);
2441
2442     /* named_curve */
2443     proto_tree_add_uint(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_named_curve,
2444         tvb, named_curve_offset, 2, named_curve);
2445
2446     /* point */
2447     proto_tree_add_uint(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_point_len,
2448         tvb, point_len_offset, 1, point_len);
2449     proto_tree_add_item(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_point,
2450             tvb, point_len_offset+1, point_len, ENC_NA);
2451
2452     /* Sig */
2453     proto_tree_add_uint(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_sig_len,
2454         tvb, sig_len_offset, 2, sig_len);
2455     proto_tree_add_item(ssl_ecdh_tree, hf_dtls_handshake_server_keyex_sig,
2456             tvb, sig_len_offset + 2, sig_len, ENC_NA);
2457
2458 }
2459
2460 static void
2461 dissect_dtls_hnd_srv_keyex_dh(tvbuff_t *tvb, proto_tree *tree,
2462                               guint32 offset, guint32 length)
2463 {
2464     gint        p_len, p_len_offset;
2465     gint        g_len, g_len_offset;
2466     gint        ys_len, ys_len_offset;
2467     gint        sig_len, sig_len_offset;
2468     proto_item *ti_dh;
2469     proto_tree *ssl_dh_tree;
2470     guint32     orig_offset;
2471
2472     orig_offset = offset;
2473
2474     p_len_offset = offset;
2475     p_len = tvb_get_ntohs(tvb, offset);
2476     offset += 2 + p_len;
2477     if ((offset - orig_offset) > length) {
2478         return;
2479     }
2480
2481     g_len_offset = offset;
2482     g_len = tvb_get_ntohs(tvb, offset);
2483     offset += 2 + g_len;
2484     if ((offset - orig_offset) > length) {
2485         return;
2486     }
2487
2488     ys_len_offset = offset;
2489     ys_len = tvb_get_ntohs(tvb, offset);
2490     offset += 2 + ys_len;
2491     if ((offset - orig_offset) > length) {
2492         return;
2493     }
2494
2495     sig_len_offset = offset;
2496     sig_len = tvb_get_ntohs(tvb, offset);
2497     offset += 2 + sig_len;
2498     if ((offset - orig_offset) != length) {
2499         /* Lengths don't line up (wasn't what we expected?) */
2500         return;
2501     }
2502
2503     ti_dh = proto_tree_add_text(tree, tvb, orig_offset,
2504                 (offset - orig_offset), "Diffie-Hellman Server Params");
2505     ssl_dh_tree = proto_item_add_subtree(ti_dh, ett_dtls_keyex_params);
2506
2507     /* p */
2508     proto_tree_add_uint(ssl_dh_tree, hf_dtls_handshake_server_keyex_p_len,
2509         tvb, p_len_offset, 2, p_len);
2510     proto_tree_add_item(ssl_dh_tree, hf_dtls_handshake_server_keyex_p,
2511             tvb, p_len_offset + 2, p_len, ENC_NA);
2512
2513     /* g */
2514     proto_tree_add_uint(ssl_dh_tree, hf_dtls_handshake_server_keyex_g_len,
2515         tvb, g_len_offset, 2, g_len);
2516     proto_tree_add_item(ssl_dh_tree, hf_dtls_handshake_server_keyex_g,
2517             tvb, g_len_offset + 2, g_len, ENC_NA);
2518
2519     /* Ys */
2520     proto_tree_add_uint(ssl_dh_tree, hf_dtls_handshake_server_keyex_ys_len,
2521         tvb, ys_len_offset, 2, ys_len);
2522     proto_tree_add_item(ssl_dh_tree, hf_dtls_handshake_server_keyex_ys,
2523             tvb, ys_len_offset + 2, ys_len, ENC_NA);
2524
2525     /* Sig */
2526     proto_tree_add_uint(ssl_dh_tree, hf_dtls_handshake_server_keyex_sig_len,
2527         tvb, sig_len_offset, 2, sig_len);
2528     proto_tree_add_item(ssl_dh_tree, hf_dtls_handshake_server_keyex_sig,
2529             tvb, sig_len_offset + 2, sig_len, ENC_NA);
2530
2531 }
2532
2533 /* Used in RSA PSK cipher suites */
2534 static void
2535 dissect_dtls_hnd_srv_keyex_rsa(tvbuff_t *tvb, proto_tree *tree,
2536                               guint32 offset, guint32 length)
2537 {
2538     gint        modulus_len, modulus_len_offset;
2539     gint        exponent_len, exponent_len_offset;
2540     gint        sig_len, sig_len_offset;
2541     proto_item *ti_rsa;
2542     proto_tree *ssl_rsa_tree;
2543     guint32     orig_offset;
2544
2545     orig_offset = offset;
2546
2547     modulus_len_offset = offset;
2548     modulus_len = tvb_get_ntohs(tvb, offset);
2549     offset += 2 + modulus_len;
2550     if ((offset - orig_offset) > length) {
2551         return;
2552     }
2553
2554     exponent_len_offset = offset;
2555     exponent_len = tvb_get_ntohs(tvb, offset);
2556     offset += 2 + exponent_len;
2557     if ((offset - orig_offset) > length) {
2558         return;
2559     }
2560
2561     sig_len_offset = offset;
2562     sig_len = tvb_get_ntohs(tvb, offset);
2563     offset += 2 + sig_len;
2564     if ((offset - orig_offset) != length) {
2565         /* Lengths don't line up (wasn't what we expected?) */
2566         return;
2567     }
2568
2569     ti_rsa = proto_tree_add_text(tree, tvb, orig_offset,
2570                 (offset - orig_offset), "RSA-EXPORT Server Params");
2571     ssl_rsa_tree = proto_item_add_subtree(ti_rsa, ett_dtls_keyex_params);
2572
2573     /* modulus */
2574     proto_tree_add_uint(ssl_rsa_tree, hf_dtls_handshake_server_keyex_modulus_len,
2575         tvb, modulus_len_offset, 2, modulus_len);
2576     proto_tree_add_item(ssl_rsa_tree, hf_dtls_handshake_server_keyex_modulus,
2577             tvb, modulus_len_offset + 2, modulus_len, ENC_NA);
2578
2579     /* exponent */
2580     proto_tree_add_uint(ssl_rsa_tree, hf_dtls_handshake_server_keyex_exponent_len,
2581         tvb, exponent_len_offset, 2, exponent_len);
2582     proto_tree_add_item(ssl_rsa_tree, hf_dtls_handshake_server_keyex_exponent,
2583             tvb, exponent_len_offset + 2, exponent_len, ENC_NA);
2584
2585     /* Sig */
2586     proto_tree_add_uint(ssl_rsa_tree, hf_dtls_handshake_server_keyex_sig_len,
2587         tvb, sig_len_offset, 2, sig_len);
2588     proto_tree_add_item(ssl_rsa_tree, hf_dtls_handshake_server_keyex_sig,
2589             tvb, sig_len_offset + 2, sig_len, ENC_NA);
2590
2591 }
2592
2593 /* Used in RSA PSK and PSK cipher suites */
2594 static void
2595 dissect_dtls_hnd_srv_keyex_psk(tvbuff_t *tvb, proto_tree *tree,
2596                               guint32 offset, guint32 length)
2597 {
2598     guint        hint_len;
2599     proto_item *ti_psk;
2600     proto_tree *ssl_psk_tree;
2601
2602     hint_len = tvb_get_ntohs(tvb, offset);
2603     if ((2 + hint_len) != length) {
2604         /* Lengths don't line up (wasn't what we expected?) */
2605         return;
2606     }
2607
2608     ti_psk = proto_tree_add_text(tree, tvb, offset,
2609                                  length, "PSK Server Params");
2610     ssl_psk_tree = proto_item_add_subtree(ti_psk, ett_dtls_keyex_params);
2611
2612     /* hint */
2613     proto_tree_add_uint(ssl_psk_tree, hf_dtls_handshake_server_keyex_hint_len,
2614                         tvb, offset, 2, hint_len);
2615     proto_tree_add_item(ssl_psk_tree, hf_dtls_handshake_server_keyex_hint,
2616                         tvb, offset + 2, hint_len, ENC_NA);
2617 }
2618
2619 static void
2620 dissect_dtls_hnd_cli_keyex_ecdh(tvbuff_t *tvb, proto_tree *tree,
2621                               guint32 offset, guint32 length)
2622 {
2623     gint        point_len, point_len_offset;
2624     proto_item *ti_ecdh;
2625     proto_tree *ssl_ecdh_tree;
2626     guint32     orig_offset;
2627
2628     orig_offset = offset;
2629
2630     point_len_offset = offset;
2631     point_len = tvb_get_guint8(tvb, offset);
2632     if ((offset + point_len - orig_offset) > length) {
2633         return;
2634     }
2635     offset += 1 + point_len;
2636
2637     ti_ecdh = proto_tree_add_text(tree, tvb, orig_offset,
2638                 (offset - orig_offset), "EC Diffie-Hellman Client Params");
2639     ssl_ecdh_tree = proto_item_add_subtree(ti_ecdh, ett_dtls_keyex_params);
2640
2641     /* point */
2642     proto_tree_add_uint(ssl_ecdh_tree, hf_dtls_handshake_client_keyex_point_len,
2643         tvb, point_len_offset, 1, point_len);
2644     proto_tree_add_item(ssl_ecdh_tree, hf_dtls_handshake_client_keyex_point,
2645             tvb, point_len_offset+1, point_len, ENC_NA);
2646
2647 }
2648
2649 static void
2650 dissect_dtls_hnd_cli_keyex_dh(tvbuff_t *tvb, proto_tree *tree,
2651                               guint32 offset, guint32 length)
2652 {
2653     gint        yc_len, yc_len_offset;
2654     proto_item *ti_dh;
2655     proto_tree *ssl_dh_tree;
2656     guint32     orig_offset;
2657
2658     orig_offset = offset;
2659
2660     yc_len_offset = offset;
2661     yc_len  = tvb_get_ntohs(tvb, offset);
2662     offset += 2 + yc_len;
2663     if ((offset - orig_offset) != length) {
2664         return;
2665     }
2666
2667     ti_dh = proto_tree_add_text(tree, tvb, orig_offset,
2668                 (offset - orig_offset), "Diffie-Hellman Client Params");
2669     ssl_dh_tree = proto_item_add_subtree(ti_dh, ett_dtls_keyex_params);
2670
2671     /* encrypted PreMaster secret */
2672     proto_tree_add_uint(ssl_dh_tree, hf_dtls_handshake_client_keyex_yc_len,
2673         tvb, yc_len_offset, 2, yc_len);
2674     proto_tree_add_item(ssl_dh_tree, hf_dtls_handshake_client_keyex_yc,
2675             tvb, yc_len_offset + 2, yc_len, ENC_NA);
2676 }
2677
2678 static void
2679 dissect_dtls_hnd_cli_keyex_rsa(tvbuff_t *tvb, proto_tree *tree,
2680                               guint32 offset, guint32 length)
2681 {
2682     gint        epms_len, epms_len_offset;
2683     proto_item *ti_rsa;
2684     proto_tree *ssl_rsa_tree;
2685     guint32     orig_offset;
2686
2687     orig_offset = offset;
2688
2689     epms_len_offset = offset;
2690     epms_len = tvb_get_ntohs(tvb, offset);
2691     offset += 2 + epms_len;
2692     if ((offset - orig_offset) != length) {
2693         return;
2694     }
2695
2696     ti_rsa = proto_tree_add_text(tree, tvb, orig_offset,
2697                 (offset - orig_offset), "RSA Encrypted PreMaster Secret");
2698     ssl_rsa_tree = proto_item_add_subtree(ti_rsa, ett_dtls_keyex_params);
2699
2700     /* Yc */
2701     proto_tree_add_uint(ssl_rsa_tree, hf_dtls_handshake_client_keyex_epms_len,
2702         tvb, epms_len_offset, 2, epms_len);
2703     proto_tree_add_item(ssl_rsa_tree, hf_dtls_handshake_client_keyex_epms,
2704             tvb, epms_len_offset + 2, epms_len, ENC_NA);
2705 }
2706
2707 /* Used in PSK cipher suites */
2708 static void
2709 dissect_dtls_hnd_cli_keyex_psk(tvbuff_t *tvb, proto_tree *tree,
2710                               guint32 offset, guint32 length)
2711 {
2712     guint        identity_len;
2713     proto_item *ti_psk;
2714     proto_tree *ssl_psk_tree;
2715
2716     identity_len = tvb_get_ntohs(tvb, offset);
2717     if ((2 + identity_len) != length) {
2718         /* Lengths don't line up (wasn't what we expected?) */
2719         return;
2720     }
2721
2722     ti_psk = proto_tree_add_text(tree, tvb, offset,
2723                 length, "PSK Client Params");
2724     ssl_psk_tree = proto_item_add_subtree(ti_psk, ett_dtls_keyex_params);
2725
2726     /* identity */
2727     proto_tree_add_uint(ssl_psk_tree, hf_dtls_handshake_client_keyex_identity_len,
2728         tvb, offset, 2, identity_len);
2729     proto_tree_add_item(ssl_psk_tree, hf_dtls_handshake_client_keyex_identity,
2730             tvb, offset + 2, identity_len, ENC_NA);
2731 }
2732
2733 /* Used in RSA PSK cipher suites */
2734 static void
2735 dissect_dtls_hnd_cli_keyex_rsa_psk(tvbuff_t *tvb, proto_tree *tree,
2736                                    guint32 offset, guint32 length)
2737 {
2738     gint        identity_len, identity_len_offset;
2739     gint        epms_len, epms_len_offset;
2740     proto_item *ti_psk;
2741     proto_tree *ssl_psk_tree;
2742     guint32     orig_offset;
2743
2744     orig_offset = offset;
2745
2746     identity_len_offset = offset;
2747     identity_len = tvb_get_ntohs(tvb, offset);
2748     offset += 2 + identity_len;
2749     if ((offset - orig_offset) > length) {
2750         return;
2751     }
2752
2753     epms_len_offset = offset;
2754     epms_len = tvb_get_ntohs(tvb, offset);
2755     offset += 2 + epms_len;
2756     if ((offset - orig_offset) != length) {
2757         /* Lengths don't line up (wasn't what we expected?) */
2758         return;
2759     }
2760
2761     ti_psk = proto_tree_add_text(tree, tvb, orig_offset,
2762                                  (offset - orig_offset), "RSA PSK Client Params");
2763     ssl_psk_tree = proto_item_add_subtree(ti_psk, ett_dtls_keyex_params);
2764
2765     /* identity */
2766     proto_tree_add_uint(ssl_psk_tree, hf_dtls_handshake_client_keyex_identity_len,
2767                         tvb, identity_len_offset, 2, identity_len);
2768     proto_tree_add_item(ssl_psk_tree, hf_dtls_handshake_client_keyex_identity,
2769                         tvb, identity_len_offset + 2, identity_len, ENC_NA);
2770
2771     /* Yc */
2772     proto_tree_add_uint(ssl_psk_tree, hf_dtls_handshake_client_keyex_epms_len,
2773         tvb, epms_len_offset, 2, epms_len);
2774     proto_tree_add_item(ssl_psk_tree, hf_dtls_handshake_client_keyex_epms,
2775             tvb, epms_len_offset + 2, epms_len, ENC_NA);
2776 }
2777
2778 static void
2779 dissect_dtls_hnd_finished(tvbuff_t *tvb, proto_tree *tree, guint32 offset,
2780                           guint* conv_version)
2781 {
2782   /*
2783    *     struct {
2784    *         opaque verify_data[12];
2785    *     } Finished;
2786    */
2787
2788   /* this all needs a tree, so bail if we don't have one */
2789   if (!tree)
2790     {
2791       return;
2792     }
2793
2794   switch(*conv_version) {
2795   case SSL_VER_DTLS:
2796     proto_tree_add_item(tree, hf_dtls_handshake_finished,
2797                         tvb, offset, 12, ENC_NA);
2798     break;
2799   case SSL_VER_DTLS1DOT2:
2800     proto_tree_add_item(tree, hf_dtls_handshake_finished,
2801                         tvb, offset, 12, ENC_NA);
2802     break;
2803   }
2804 }
2805
2806 /*********************************************************************
2807  *
2808  * Support Functions
2809  *
2810  *********************************************************************/
2811 #if 0
2812 static void
2813 ssl_set_conv_version(packet_info *pinfo, guint version)
2814 {
2815   conversation_t *conversation;
2816
2817   if (pinfo->fd->flags.visited)
2818     {
2819       /* We've already processed this frame; no need to do any more
2820        * work on it.
2821        */
2822       return;
2823     }
2824
2825   conversation = find_or_create_conversation(pinfo);
2826
2827   if (conversation_get_proto_data(conversation, proto_dtls) != NULL)
2828     {
2829       /* get rid of the current data */
2830       conversation_delete_proto_data(conversation, proto_dtls);
2831     }
2832   conversation_add_proto_data(conversation, proto_dtls, GINT_TO_POINTER(version));
2833 }
2834 #endif
2835
2836 static gint
2837 dtls_is_valid_handshake_type(guint8 type)
2838 {
2839
2840   switch (type) {
2841   case SSL_HND_HELLO_REQUEST:
2842   case SSL_HND_CLIENT_HELLO:
2843   case SSL_HND_SERVER_HELLO:
2844   case SSL_HND_HELLO_VERIFY_REQUEST:
2845   case SSL_HND_NEWSESSION_TICKET:
2846   case SSL_HND_CERTIFICATE:
2847   case SSL_HND_SERVER_KEY_EXCHG:
2848   case SSL_HND_CERT_REQUEST:
2849   case SSL_HND_SVR_HELLO_DONE:
2850   case SSL_HND_CERT_VERIFY:
2851   case SSL_HND_CLIENT_KEY_EXCHG:
2852   case SSL_HND_FINISHED:
2853     return 1;
2854   }
2855   return 0;
2856 }
2857
2858 static gint
2859 dtls_is_authoritative_version_message(guint8 content_type, guint8 next_byte)
2860 {
2861   if (content_type == SSL_ID_HANDSHAKE
2862       && dtls_is_valid_handshake_type(next_byte))
2863     {
2864       return (next_byte != SSL_HND_CLIENT_HELLO);
2865     }
2866   else if (ssl_is_valid_content_type(content_type)
2867            && content_type != SSL_ID_HANDSHAKE)
2868     {
2869       return 1;
2870     }
2871   return 0;
2872 }
2873
2874 /* this applies a heuristic to determine whether
2875  * or not the data beginning at offset looks like a
2876  * valid dtls record.
2877  */
2878 static gint
2879 looks_like_dtls(tvbuff_t *tvb, guint32 offset)
2880 {
2881   /* have to have a valid content type followed by a valid
2882    * protocol version
2883    */
2884   guint8  byte;
2885   guint16 version;
2886
2887   /* see if the first byte is a valid content type */
2888   byte = tvb_get_guint8(tvb, offset);
2889   if (!ssl_is_valid_content_type(byte))
2890     {
2891       return 0;
2892     }
2893
2894   /* now check to see if the version byte appears valid */
2895   version = tvb_get_ntohs(tvb, offset + 1);
2896   if (version != DTLSV1DOT0_VERSION && version != DTLSV1DOT2_VERSION &&
2897       version != DTLSV1DOT0_VERSION_NOT)
2898     {
2899       return 0;
2900     }
2901
2902   return 1;
2903 }
2904
2905 /* UAT */
2906
2907 #ifdef HAVE_LIBGNUTLS
2908 static void
2909 dtlsdecrypt_free_cb(void* r)
2910 {
2911   ssldecrypt_assoc_t* h = (ssldecrypt_assoc_t*)r;
2912
2913   g_free(h->ipaddr);
2914   g_free(h->port);
2915   g_free(h->protocol);
2916   g_free(h->keyfile);
2917   g_free(h->password);
2918 }
2919 #endif
2920
2921 #if 0
2922 static void
2923 dtlsdecrypt_update_cb(void* r _U_, const char** err _U_)
2924 {
2925   return;
2926 }
2927 #endif
2928
2929 #ifdef HAVE_LIBGNUTLS
2930 static void *
2931 dtlsdecrypt_copy_cb(void* dest, const void* orig, size_t len _U_)
2932 {
2933   const ssldecrypt_assoc_t* o = (const ssldecrypt_assoc_t*)orig;
2934   ssldecrypt_assoc_t*       d = (ssldecrypt_assoc_t*)dest;
2935
2936   d->ipaddr    = g_strdup(o->ipaddr);
2937   d->port      = g_strdup(o->port);
2938   d->protocol  = g_strdup(o->protocol);
2939   d->keyfile   = g_strdup(o->keyfile);
2940   d->password  = g_strdup(o->password);
2941
2942   return d;
2943 }
2944
2945 UAT_CSTRING_CB_DEF(sslkeylist_uats,ipaddr,ssldecrypt_assoc_t)
2946 UAT_CSTRING_CB_DEF(sslkeylist_uats,port,ssldecrypt_assoc_t)
2947 UAT_CSTRING_CB_DEF(sslkeylist_uats,protocol,ssldecrypt_assoc_t)
2948 UAT_FILENAME_CB_DEF(sslkeylist_uats,keyfile,ssldecrypt_assoc_t)
2949 UAT_CSTRING_CB_DEF(sslkeylist_uats,password,ssldecrypt_assoc_t)
2950 #endif
2951
2952 void proto_reg_handoff_dtls(void);
2953
2954 /*********************************************************************
2955  *
2956  * Standard Wireshark Protocol Registration and housekeeping
2957  *
2958  *********************************************************************/
2959 void
2960 proto_register_dtls(void)
2961 {
2962
2963   /* Setup list of header fields See Section 1.6.1 for details*/
2964   static hf_register_info hf[] = {
2965     { &hf_dtls_record,
2966       { "Record Layer", "dtls.record",
2967         FT_NONE, BASE_NONE, NULL, 0x0,
2968         NULL, HFILL }
2969     },
2970     { &hf_dtls_record_content_type,
2971       { "Content Type", "dtls.record.content_type",
2972         FT_UINT8, BASE_DEC, VALS(ssl_31_content_type), 0x0,
2973         NULL, HFILL}
2974     },
2975     { &hf_dtls_record_version,
2976       { "Version", "dtls.record.version",
2977         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
2978         "Record layer version.", HFILL }
2979     },
2980     { &hf_dtls_record_epoch,
2981       { "Epoch", "dtls.record.epoch",
2982         FT_UINT16, BASE_DEC, NULL, 0x0,
2983         NULL, HFILL }
2984     },
2985     { &hf_dtls_record_sequence_number,
2986       { "Sequence Number", "dtls.record.sequence_number",
2987         FT_DOUBLE, BASE_NONE, NULL, 0x0,
2988         NULL, HFILL }
2989     },
2990     { &hf_dtls_record_length,
2991       { "Length", "dtls.record.length",
2992         FT_UINT16, BASE_DEC, NULL, 0x0,
2993         "Length of DTLS record data", HFILL }
2994     },
2995     { &hf_dtls_record_appdata,
2996       { "Encrypted Application Data", "dtls.app_data",
2997         FT_BYTES, BASE_NONE, NULL, 0x0,
2998         "Payload is encrypted application data", HFILL }
2999     },
3000     { &hf_dtls_change_cipher_spec,
3001       { "Change Cipher Spec Message", "dtls.change_cipher_spec",
3002         FT_NONE, BASE_NONE, NULL, 0x0,
3003         "Signals a change in cipher specifications", HFILL }
3004     },
3005     { & hf_dtls_alert_message,
3006       { "Alert Message", "dtls.alert_message",
3007         FT_NONE, BASE_NONE, NULL, 0x0,
3008         NULL, HFILL }
3009     },
3010     { & hf_dtls_alert_message_level,
3011       { "Level", "dtls.alert_message.level",
3012         FT_UINT8, BASE_DEC, VALS(ssl_31_alert_level), 0x0,
3013         "Alert message level", HFILL }
3014     },
3015     { &hf_dtls_alert_message_description,
3016       { "Description", "dtls.alert_message.desc",
3017         FT_UINT8, BASE_DEC, VALS(ssl_31_alert_description), 0x0,
3018         "Alert message description", HFILL }
3019     },
3020     { &hf_dtls_handshake_protocol,
3021       { "Handshake Protocol", "dtls.handshake",
3022         FT_NONE, BASE_NONE, NULL, 0x0,
3023         "Handshake protocol message", HFILL}
3024     },
3025     { &hf_dtls_handshake_type,
3026       { "Handshake Type", "dtls.handshake.type",
3027         FT_UINT8, BASE_DEC, VALS(ssl_31_handshake_type), 0x0,
3028         "Type of handshake message", HFILL}
3029     },
3030     { &hf_dtls_handshake_length,
3031       { "Length", "dtls.handshake.length",
3032         FT_UINT24, BASE_DEC, NULL, 0x0,
3033         "Length of handshake message", HFILL }
3034     },
3035     { &hf_dtls_handshake_message_seq,
3036       { "Message Sequence", "dtls.handshake.message_seq",
3037         FT_UINT16, BASE_DEC, NULL, 0x0,
3038         "Message sequence of handshake message", HFILL }
3039     },
3040     { &hf_dtls_handshake_fragment_offset,
3041       { "Fragment Offset", "dtls.handshake.fragment_offset",
3042         FT_UINT24, BASE_DEC, NULL, 0x0,
3043         "Fragment offset of handshake message", HFILL }
3044     },
3045     { &hf_dtls_handshake_fragment_length,
3046       { "Fragment Length", "dtls.handshake.fragment_length",
3047         FT_UINT24, BASE_DEC, NULL, 0x0,
3048         "Fragment length of handshake message", HFILL }
3049     },
3050     { &hf_dtls_handshake_client_version,
3051       { "Version", "dtls.handshake.client_version",
3052         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
3053         "Maximum version supported by client", HFILL }
3054     },
3055     { &hf_dtls_handshake_server_version,
3056       { "Version", "dtls.handshake.server_version",
3057         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
3058         "Version selected by server", HFILL }
3059     },
3060     { &hf_dtls_handshake_random_time,
3061       { "GMT Unix Time", "dtls.handshake.random_time",
3062         FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0,
3063         "Unix time field of random structure", HFILL }
3064     },
3065     { &hf_dtls_handshake_random_bytes,
3066       { "Random Bytes", "dtls.handshake.random",
3067         FT_BYTES, BASE_NONE, NULL, 0x0,
3068         "Random challenge used to authenticate server", HFILL }
3069     },
3070     { &hf_dtls_handshake_cipher_suites_len,
3071       { "Cipher Suites Length", "dtls.handshake.cipher_suites_length",
3072         FT_UINT16, BASE_DEC, NULL, 0x0,
3073         "Length of cipher suites field", HFILL }
3074     },
3075     { &hf_dtls_handshake_cipher_suites,
3076       { "Cipher Suites", "dtls.handshake.ciphersuites",
3077         FT_NONE, BASE_NONE, NULL, 0x0,
3078         "List of cipher suites supported by client", HFILL }
3079     },
3080     { &hf_dtls_handshake_cipher_suite,
3081       { "Cipher Suite", "dtls.handshake.ciphersuite",
3082         FT_UINT16, BASE_HEX|BASE_EXT_STRING, &ssl_31_ciphersuite_ext, 0x0,
3083         NULL, HFILL }
3084     },
3085     { &hf_dtls_handshake_cookie_len,
3086       { "Cookie Length", "dtls.handshake.cookie_length",
3087         FT_UINT8, BASE_DEC, NULL, 0x0,
3088         "Length of the cookie field", HFILL }
3089     },
3090     { &hf_dtls_handshake_cookie,
3091       { "Cookie", "dtls.handshake.cookie",
3092         FT_BYTES, BASE_NONE, NULL, 0x0,
3093         NULL, HFILL }
3094     },
3095     { &hf_dtls_handshake_session_id,
3096       { "Session ID", "dtls.handshake.session_id",
3097         FT_BYTES, BASE_NONE, NULL, 0x0,
3098         "Identifies the DTLS session, allowing later resumption", HFILL }
3099     },
3100     { &hf_dtls_handshake_comp_methods_len,
3101       { "Compression Methods Length", "dtls.handshake.comp_methods_length",
3102         FT_UINT8, BASE_DEC, NULL, 0x0,
3103         "Length of compression methods field", HFILL }
3104     },
3105     { &hf_dtls_handshake_comp_methods,
3106       { "Compression Methods", "dtls.handshake.comp_methods",
3107         FT_NONE, BASE_NONE, NULL, 0x0,
3108         "List of compression methods supported by client", HFILL }
3109     },
3110     { &hf_dtls_handshake_comp_method,
3111       { "Compression Method", "dtls.handshake.comp_method",
3112         FT_UINT8, BASE_DEC, VALS(ssl_31_compression_method), 0x0,
3113         NULL, HFILL }
3114     },
3115     { &hf_dtls_handshake_extensions_len,
3116       { "Extensions Length", "dtls.handshake.extensions_length",
3117         FT_UINT16, BASE_DEC, NULL, 0x0,
3118         "Length of hello extensions", HFILL }
3119     },
3120     { &hf_dtls_handshake_extension_type,
3121       { "Type", "dtls.handshake.extension.type",
3122         FT_UINT16, BASE_HEX, VALS(tls_hello_extension_types), 0x0,
3123         "Hello extension type", HFILL }
3124     },
3125     { &hf_dtls_handshake_extension_len,
3126       { "Length", "dtls.handshake.extension.len",
3127         FT_UINT16, BASE_DEC, NULL, 0x0,
3128         "Length of a hello extension", HFILL }
3129     },
3130     { &hf_dtls_handshake_extension_data,
3131       { "Data", "dtls.handshake.extension.data",
3132         FT_BYTES, BASE_NONE, NULL, 0x0,
3133         "Hello Extension data", HFILL }
3134     },
3135     { &hf_dtls_handshake_session_ticket_lifetime_hint,
3136       { "Session Ticket Lifetime Hint", "dtls.handshake.session_ticket_lifetime_hint",
3137         FT_UINT32, BASE_DEC, NULL, 0x0,
3138         "New DTLS Session Ticket Lifetime Hint", HFILL }
3139     },
3140     { &hf_dtls_handshake_session_ticket_len,
3141       { "Session Ticket Length", "dtls.handshake.session_ticket_length",
3142         FT_UINT16, BASE_DEC, NULL, 0x0,
3143         "New DTLS Session Ticket Length", HFILL }
3144     },
3145     { &hf_dtls_handshake_session_ticket,
3146       { "Session Ticket", "dtls.handshake.session_ticket",
3147         FT_BYTES, BASE_NONE, NULL, 0x0,
3148         "New DTLS Session Ticket", HFILL }
3149     },
3150     { &hf_dtls_handshake_certificates_len,
3151       { "Certificates Length", "dtls.handshake.certificates_length",
3152         FT_UINT24, BASE_DEC, NULL, 0x0,
3153         "Length of certificates field", HFILL }
3154     },
3155     { &hf_dtls_handshake_certificates,
3156       { "Certificates", "dtls.handshake.certificates",
3157         FT_NONE, BASE_NONE, NULL, 0x0,
3158         "List of certificates", HFILL }
3159     },
3160     { &hf_dtls_handshake_certificate,
3161       { "Certificate", "dtls.handshake.certificate",
3162         FT_BYTES, BASE_NONE, NULL, 0x0,
3163         NULL, HFILL }
3164     },
3165     { &hf_dtls_handshake_certificate_len,
3166       { "Certificate Length", "dtls.handshake.certificate_length",
3167         FT_UINT24, BASE_DEC, NULL, 0x0,
3168         "Length of certificate", HFILL }
3169     },
3170     { &hf_dtls_handshake_cert_types_count,
3171       { "Certificate types count", "dtls.handshake.cert_types_count",
3172         FT_UINT8, BASE_DEC, NULL, 0x0,
3173         "Count of certificate types", HFILL }
3174     },
3175     { &hf_dtls_handshake_cert_types,
3176       { "Certificate types", "dtls.handshake.cert_types",
3177         FT_NONE, BASE_NONE, NULL, 0x0,
3178         "List of certificate types", HFILL }
3179     },
3180     { &hf_dtls_handshake_cert_type,
3181       { "Certificate type", "dtls.handshake.cert_type",
3182         FT_UINT8, BASE_DEC, VALS(ssl_31_client_certificate_type), 0x0,
3183         NULL, HFILL }
3184     },
3185     { &hf_dtls_handshake_server_keyex_p_len,
3186       { "p Length", "dtls.handshake.p_len",
3187         FT_UINT16, BASE_DEC, NULL, 0x0,
3188         "Length of p", HFILL }
3189     },
3190     { &hf_dtls_handshake_server_keyex_g_len,
3191       { "g Length", "dtls.handshake.g_len",
3192         FT_UINT16, BASE_DEC, NULL, 0x0,
3193         "Length of g", HFILL }
3194     },
3195     { &hf_dtls_handshake_server_keyex_ys_len,
3196       { "Pubkey Length", "dtls.handshake.ys_len",
3197         FT_UINT16, BASE_DEC, NULL, 0x0,
3198         "Length of server's Diffie-Hellman public key", HFILL }
3199     },
3200     { &hf_dtls_handshake_client_keyex_yc_len,
3201       { "Pubkey Length", "dtls.handshake.yc_len",
3202         FT_UINT16, BASE_DEC, NULL, 0x0,
3203         "Length of client's Diffie-Hellman public key", HFILL }
3204     },
3205     { &hf_dtls_handshake_client_keyex_point_len,
3206       { "Pubkey Length", "dtls.handshake.client_point_len",
3207         FT_UINT8, BASE_DEC, NULL, 0x0,
3208         "Length of client's EC Diffie-Hellman public key", HFILL }
3209     },
3210     { &hf_dtls_handshake_server_keyex_point_len,
3211       { "Pubkey Length", "dtls.handshake.server_point_len",
3212         FT_UINT8, BASE_DEC, NULL, 0x0,
3213         "Length of server's EC Diffie-Hellman public key", HFILL }
3214     },
3215     { &hf_dtls_handshake_client_keyex_epms_len,
3216       { "Encrypted PreMaster length", "dtls.handshake.epms_len",
3217         FT_UINT16, BASE_DEC, NULL, 0x0,
3218         "Length of encrypted PreMaster secret", HFILL }
3219     },
3220     { &hf_dtls_handshake_client_keyex_epms,
3221       { "Encrypted PreMaster", "dtls.handshake.epms",
3222         FT_BYTES, BASE_NONE, NULL, 0x0,
3223         "Encrypted PreMaster secret", HFILL }
3224     },
3225     { &hf_dtls_handshake_server_keyex_modulus_len,
3226       { "Modulus Length", "dtls.handshake.modulus_len",
3227         FT_UINT16, BASE_DEC, NULL, 0x0,
3228         "Length of RSA-EXPORT modulus", HFILL }
3229     },
3230     { &hf_dtls_handshake_server_keyex_exponent_len,
3231       { "Exponent Length", "dtls.handshake.exponent_len",
3232         FT_UINT16, BASE_DEC, NULL, 0x0,
3233         "Length of RSA-EXPORT exponent", HFILL }
3234     },
3235     { &hf_dtls_handshake_server_keyex_sig_len,
3236       { "Signature Length", "dtls.handshake.sig_len",
3237         FT_UINT16, BASE_DEC, NULL, 0x0,
3238         "Length of Signature", HFILL }
3239     },
3240     { &hf_dtls_handshake_server_keyex_p,
3241       { "p", "dtls.handshake.p",
3242         FT_BYTES, BASE_NONE, NULL, 0x0,
3243         "Diffie-Hellman p", HFILL }
3244     },
3245     { &hf_dtls_handshake_server_keyex_g,
3246       { "g", "dtls.handshake.g",
3247         FT_BYTES, BASE_NONE, NULL, 0x0,
3248         "Diffie-Hellman g", HFILL }
3249     },
3250     { &hf_dtls_handshake_server_keyex_curve_type,
3251         { "Curve Type", "dtls.handshake.server_curve_type",
3252         FT_UINT8, BASE_HEX, VALS(ssl_curve_types), 0x0,
3253         "Server curve_type", HFILL }
3254     },
3255     { &hf_dtls_handshake_server_keyex_named_curve,
3256         { "Named Curve", "dtls.handshake.server_named_curve",
3257         FT_UINT16, BASE_HEX, VALS(ssl_extension_curves), 0x0,
3258         "Server named_curve", HFILL }
3259     },
3260     { &hf_dtls_handshake_server_keyex_ys,
3261       { "Pubkey", "dtls.handshake.ys",
3262         FT_BYTES, BASE_NONE, NULL, 0x0,
3263         "Diffie-Hellman server pubkey", HFILL }
3264     },
3265     { &hf_dtls_handshake_client_keyex_yc,
3266       { "Pubkey", "dtls.handshake.yc",
3267         FT_BYTES, BASE_NONE, NULL, 0x0,
3268         "Diffie-Hellman client pubkey", HFILL }
3269     },
3270     { &hf_dtls_handshake_server_keyex_point,
3271       { "Pubkey", "dtls.handshake.server_point",
3272         FT_BYTES, BASE_NONE, NULL, 0x0,
3273         "EC Diffie-Hellman server pubkey", HFILL }
3274     },
3275     { &hf_dtls_handshake_client_keyex_point,
3276       { "Pubkey", "dtls.handshake.client_point",
3277         FT_BYTES, BASE_NONE, NULL, 0x0,
3278         "EC Diffie-Hellman client pubkey", HFILL }
3279     },
3280     { &hf_dtls_handshake_server_keyex_modulus,
3281       { "Modulus", "dtls.handshake.modulus",
3282         FT_BYTES, BASE_NONE, NULL, 0x0,
3283         "RSA-EXPORT modulus", HFILL }
3284     },
3285     { &hf_dtls_handshake_server_keyex_exponent,
3286       { "Exponent", "dtls.handshake.exponent",
3287         FT_BYTES, BASE_NONE, NULL, 0x0,
3288         "RSA-EXPORT exponent", HFILL }
3289     },
3290     { &hf_dtls_handshake_server_keyex_sig,
3291       { "Signature", "dtls.handshake.sig",
3292         FT_BYTES, BASE_NONE, NULL, 0x0,
3293         "Diffie-Hellman server signature", HFILL }
3294     },
3295     { &hf_dtls_handshake_server_keyex_hint_len,
3296       { "Hint Length", "dtls.handshake.hint_len",
3297         FT_UINT16, BASE_DEC, NULL, 0x0,
3298         "Length of PSK Hint", HFILL }
3299     },
3300     { &hf_dtls_handshake_server_keyex_hint,
3301       { "Hint", "dtls.handshake.hint",
3302         FT_BYTES, BASE_NONE, NULL, 0x0,
3303         "PSK Hint", HFILL }
3304     },
3305     { &hf_dtls_handshake_client_keyex_identity_len,
3306       { "Identity Length", "dtls.handshake.identity_len",
3307         FT_UINT16, BASE_DEC, NULL, 0x0,
3308         "Length of PSK Identity", HFILL }
3309     },
3310     { &hf_dtls_handshake_client_keyex_identity,
3311       { "Identity", "dtls.handshake.identity",
3312         FT_BYTES, BASE_NONE, NULL, 0x0,
3313         "PSK Identity", HFILL }
3314     },
3315     { &hf_dtls_handshake_sig_hash_alg_len,
3316       { "Signature Hash Algorithms Length", "dtls.handshake.sig_hash_alg_len",
3317         FT_UINT16, BASE_DEC, NULL, 0x0,
3318         "Length of Signature Hash Algorithms", HFILL }
3319     },
3320     { &hf_dtls_handshake_sig_hash_algs,
3321       { "Signature Hash Algorithms", "dtls.handshake.sig_hash_algs",
3322         FT_NONE, BASE_NONE, NULL, 0x0,
3323         "List of Signature Hash Algorithms", HFILL }
3324     },
3325     { &hf_dtls_handshake_sig_hash_alg,
3326       { "Signature Hash Algorithm", "dtls.handshake.sig_hash_alg",
3327         FT_UINT16, BASE_HEX, NULL, 0x0,
3328         NULL, HFILL }
3329     },
3330     { &hf_dtls_handshake_sig_hash_hash,
3331       { "Signature Hash Algorithm Hash", "dtls.handshake.sig_hash_hash",
3332         FT_UINT8, BASE_DEC, VALS(tls_hash_algorithm), 0x0,
3333         NULL, HFILL }
3334     },
3335     { &hf_dtls_handshake_sig_hash_sig,
3336       { "Signature Hash Algorithm Signature", "dtls.handshake.sig_hash_sig",
3337         FT_UINT8, BASE_DEC, VALS(tls_signature_algorithm), 0x0,
3338         NULL, HFILL }
3339      },
3340     { &hf_dtls_handshake_finished,
3341       { "Verify Data", "dtls.handshake.verify_data",
3342         FT_NONE, BASE_NONE, NULL, 0x0,
3343         "Opaque verification data", HFILL }
3344     },
3345 #if 0
3346     { &hf_dtls_handshake_md5_hash,
3347       { "MD5 Hash", "dtls.handshake.md5_hash",
3348         FT_NONE, BASE_NONE, NULL, 0x0,
3349         "Hash of messages, master_secret, etc.", HFILL }
3350     },
3351     { &hf_dtls_handshake_sha_hash,
3352       { "SHA-1 Hash", "dtls.handshake.sha_hash",
3353         FT_NONE, BASE_NONE, NULL, 0x0,
3354         "Hash of messages, master_secret, etc.", HFILL }
3355     },
3356 #endif
3357     { &hf_dtls_handshake_session_id_len,
3358       { "Session ID Length", "dtls.handshake.session_id_length",
3359         FT_UINT8, BASE_DEC, NULL, 0x0,
3360         "Length of session ID field", HFILL }
3361     },
3362     { &hf_dtls_handshake_dnames_len,
3363       { "Distinguished Names Length", "dtls.handshake.dnames_len",
3364         FT_UINT16, BASE_DEC, NULL, 0x0,
3365         "Length of list of CAs that server trusts", HFILL }
3366     },
3367     { &hf_dtls_handshake_dnames,
3368       { "Distinguished Names", "dtls.handshake.dnames",
3369         FT_NONE, BASE_NONE, NULL, 0x0,
3370         "List of CAs that server trusts", HFILL }
3371     },
3372     { &hf_dtls_handshake_dname_len,
3373       { "Distinguished Name Length", "dtls.handshake.dname_len",
3374         FT_UINT16, BASE_DEC, NULL, 0x0,
3375         "Length of distinguished name", HFILL }
3376     },
3377     { &hf_dtls_handshake_dname,
3378       { "Distinguished Name", "dtls.handshake.dname",
3379         FT_BYTES, BASE_NONE, NULL, 0x0,
3380         "Distinguished name of a CA that server trusts", HFILL }
3381     },
3382     { &hf_dtls_heartbeat_extension_mode,
3383       { "Mode", "dtls.handshake.extension.heartbeat.mode",
3384         FT_UINT8, BASE_DEC, VALS(tls_heartbeat_mode), 0x0,
3385         "Heartbeat extension mode", HFILL }
3386     },
3387     { &hf_dtls_heartbeat_message,
3388       { "Heartbeat Message", "dtls.heartbeat_message",
3389         FT_NONE, BASE_NONE, NULL, 0x0,
3390         NULL, HFILL }
3391     },
3392     { &hf_dtls_heartbeat_message_type,
3393       { "Type", "dtls.heartbeat_message.type",
3394         FT_UINT8, BASE_DEC, VALS(tls_heartbeat_type), 0x0,
3395         "Heartbeat message type", HFILL }
3396     },
3397     { &hf_dtls_heartbeat_message_payload_length,
3398       { "Payload Length", "dtls.heartbeat_message.payload_length",
3399         FT_UINT16, BASE_DEC, NULL, 0x00, NULL, HFILL }
3400     },
3401     { &hf_dtls_heartbeat_message_payload,
3402       { "Payload Length", "dtls.heartbeat_message.payload",
3403         FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL }
3404     },
3405     { &hf_dtls_heartbeat_message_padding,
3406       { "Payload Length", "dtls.heartbeat_message.padding",
3407         FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL }
3408     },
3409     { &hf_dtls_fragments,
3410       { "Message fragments", "dtls.fragments",
3411         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
3412     },
3413     { &hf_dtls_fragment,
3414       { "Message fragment", "dtls.fragment",
3415         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
3416     },
3417     { &hf_dtls_fragment_overlap,
3418       { "Message fragment overlap", "dtls.fragment.overlap",
3419         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
3420     },
3421     { &hf_dtls_fragment_overlap_conflicts,
3422       { "Message fragment overlapping with conflicting data",
3423         "dtls.fragment.overlap.conflicts",
3424        FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
3425     },
3426     { &hf_dtls_fragment_multiple_tails,
3427       { "Message has multiple tail fragments",
3428         "dtls.fragment.multiple_tails",
3429         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
3430     },
3431     { &hf_dtls_fragment_too_long_fragment,
3432       { "Message fragment too long", "dtls.fragment.too_long_fragment",
3433         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
3434     },
3435     { &hf_dtls_fragment_error,
3436       { "Message defragmentation error", "dtls.fragment.error",
3437         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
3438     },
3439     { &hf_dtls_fragment_count,
3440       { "Message fragment count", "dtls.fragment.count",
3441         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
3442     },
3443     { &hf_dtls_reassembled_in,
3444       { "Reassembled in", "dtls.reassembled.in",
3445         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
3446     },
3447     { &hf_dtls_reassembled_length,
3448       { "Reassembled DTLS length", "dtls.reassembled.length",
3449         FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
3450     },
3451   };
3452
3453   /* Setup protocol subtree array */
3454   static gint *ett[] = {
3455     &ett_dtls,
3456     &ett_dtls_record,
3457     &ett_dtls_alert,
3458     &ett_dtls_handshake,
3459     &ett_dtls_heartbeat,
3460     &ett_dtls_cipher_suites,
3461     &ett_dtls_comp_methods,
3462     &ett_dtls_extension,
3463     &ett_dtls_random,
3464     &ett_dtls_new_ses_ticket,
3465     &ett_dtls_keyex_params,
3466     &ett_dtls_certs,
3467     &ett_dtls_cert_types,
3468     &ett_dtls_sig_hash_algs,
3469     &ett_dtls_sig_hash_alg,
3470     &ett_dtls_dnames,
3471     &ett_dtls_fragment,
3472     &ett_dtls_fragments,
3473   };
3474
3475   static ei_register_info ei[] = {
3476      { &ei_dtls_handshake_fragment_length_too_long, { "dtls.handshake.fragment_length.too_long", PI_PROTOCOL, PI_ERROR, "Fragment length is larger than message length", EXPFILL }},
3477      { &ei_dtls_handshake_fragment_past_end_msg, { "dtls.handshake.fragment_past_end_msg", PI_PROTOCOL, PI_ERROR, "Fragment runs past the end of the message", EXPFILL }},
3478      { &ei_dtls_msg_len_diff_fragment, { "dtls.msg_len_diff_fragment", PI_PROTOCOL, PI_ERROR, "Message length differs from value in earlier fragment", EXPFILL }},
3479      { &ei_dtls_handshake_sig_hash_alg_len_bad, { "dtls.handshake.sig_hash_alg_len.bad", PI_MALFORMED, PI_ERROR, "Signature Hash Algorithm length must be a multiple of 2", EXPFILL }},
3480   };
3481
3482   expert_module_t* expert_dtls;
3483
3484   /* Register the protocol name and description */
3485   proto_dtls = proto_register_protocol("Datagram Transport Layer Security",
3486                                        "DTLS", "dtls");
3487
3488   /* Required function calls to register the header fields and
3489    * subtrees used */
3490   proto_register_field_array(proto_dtls, hf, array_length(hf));
3491   proto_register_subtree_array(ett, array_length(ett));
3492   expert_dtls = expert_register_protocol(proto_dtls);
3493   expert_register_field_array(expert_dtls, ei, array_length(ei));
3494
3495 #ifdef HAVE_LIBGNUTLS
3496   {
3497     module_t *dtls_module = prefs_register_protocol(proto_dtls, proto_reg_handoff_dtls);
3498
3499     static uat_field_t dtlskeylist_uats_flds[] = {
3500       UAT_FLD_CSTRING_OTHER(sslkeylist_uats, ipaddr, "IP address", ssldecrypt_uat_fld_ip_chk_cb, "IPv4 or IPv6 address"),
3501       UAT_FLD_CSTRING_OTHER(sslkeylist_uats, port, "Port", ssldecrypt_uat_fld_port_chk_cb, "Port Number"),
3502       UAT_FLD_CSTRING_OTHER(sslkeylist_uats, protocol, "Protocol", ssldecrypt_uat_fld_protocol_chk_cb, "Protocol"),
3503       UAT_FLD_FILENAME_OTHER(sslkeylist_uats, keyfile, "Key File", ssldecrypt_uat_fld_fileopen_chk_cb, "Path to the keyfile."),
3504       UAT_FLD_CSTRING_OTHER(sslkeylist_uats, password," Password (p12 file)", ssldecrypt_uat_fld_password_chk_cb, "Password"),
3505       UAT_END_FIELDS
3506     };
3507
3508     dtlsdecrypt_uat = uat_new("DTLS RSA Keylist",
3509                               sizeof(ssldecrypt_assoc_t),
3510                               "dtlsdecrypttablefile",         /* filename */
3511                               TRUE,                           /* from_profile */
3512                               (void**) &dtlskeylist_uats,     /* data_ptr */
3513                               &ndtlsdecrypt,                  /* numitems_ptr */
3514                               UAT_AFFECTS_DISSECTION,         /* affects dissection of packets, but not set of named fields */
3515                               "ChK12ProtocolsSection",        /* TODO, need revision - help */
3516                               dtlsdecrypt_copy_cb,
3517                               NULL, /* dtlsdecrypt_update_cb? */
3518                               dtlsdecrypt_free_cb,
3519                               dtls_parse_uat,
3520                               dtlskeylist_uats_flds);
3521
3522     prefs_register_uat_preference(dtls_module, "cfg",
3523                                   "RSA keys list",
3524                                   "A table of RSA keys for DTLS decryption",
3525                                   dtlsdecrypt_uat);
3526
3527     prefs_register_filename_preference(dtls_module, "debug_file", "DTLS debug file",
3528                                        "redirect dtls debug to file name; leave empty to disable debug, "
3529                                        "use \"" SSL_DEBUG_USE_STDERR "\" to redirect output to stderr\n",
3530                                        &dtls_debug_file_name);
3531
3532     prefs_register_string_preference(dtls_module, "keys_list", "RSA keys list (deprecated)",
3533                                      "Semicolon-separated list of private RSA keys used for DTLS decryption. "
3534                                      "Used by versions of Wireshark prior to 1.6",
3535                                      &dtls_keys_list);
3536
3537   }
3538 #endif
3539
3540   register_dissector("dtls", dissect_dtls, proto_dtls);
3541   dtls_handle = find_dissector("dtls");
3542
3543   dtls_associations = g_tree_new(ssl_association_cmp);
3544
3545   register_init_routine(dtls_init);
3546   ssl_lib_init();
3547   dtls_tap = register_tap("dtls");
3548   ssl_debug_printf("proto_register_dtls: registered tap %s:%d\n",
3549                    "dtls", dtls_tap);
3550
3551   register_heur_dissector_list("dtls", &heur_subdissector_list);
3552 }
3553
3554
3555 /* If this dissector uses sub-dissector registration add a registration
3556  * routine.  This format is required because a script is used to find
3557  * these routines and create the code that calls these routines.
3558  */
3559 void
3560 proto_reg_handoff_dtls(void)
3561 {
3562   static gboolean initialized = FALSE;
3563
3564   /* add now dissector to default ports.*/
3565   dtls_parse_uat();
3566   dtls_parse_old_keys();
3567
3568   if (initialized == FALSE) {
3569     heur_dissector_add("udp", dissect_dtls_heur, proto_dtls);
3570     dissector_add_uint("sctp.ppi", DIAMETER_DTLS_PROTOCOL_ID, find_dissector("dtls"));
3571   }
3572
3573   initialized = TRUE;
3574 }