c53ab89f36962d21f7bbbfd46f8db89040b60fe7
[obnox/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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 TLS one (packet-ssl.c) because of the proximity of DTLS and TLS, decryption works like him with RSA key exchange.
32  * It uses the sames things (file, libraries) that SSL one (gnutls, packet-ssl-utils.h) to make it easily maintenable.
33  *
34  * It was developped to dissect and decrypt OpenSSL v 0.9.8f DTLS implementation.
35  * It is limited to this implementation  while there is no complete implementation.
36  *
37  * Implemented :
38  *  - DTLS dissection
39  *  - DTLS decryption (openssl one)
40  *
41  * Todo :
42  *  - activate correct Mac calculation when openssl will be corrected
43  *    (or if an other implementation works),
44  *    corrected code is ready and commented in packet-ssl-utils.h file.
45  *  - add missings things (desegmentation, reordering... that aren't present in actual OpenSSL implementation)
46  */
47
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include <sys/types.h>
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60
61 #ifdef HAVE_WINSOCK2_H
62 #include <winsock2.h>
63 #endif
64
65 #include <glib.h>
66
67 #include <epan/conversation.h>
68 #include <epan/prefs.h>
69 #include <epan/asn1.h>
70 #include <epan/dissectors/packet-x509af.h>
71 #include <epan/emem.h>
72 #include <epan/tap.h>
73 #include <epan/reassemble.h>
74 #include "inet_v6defs.h"
75 #include "packet-ssl-utils.h"
76
77 extern const gchar *ssl_version_short_names[];
78
79 /* we need to remember the top tree so that subdissectors we call are created
80  * at the root and not deep down inside the DTLS decode
81  */
82 static proto_tree *top_tree;
83
84 /*********************************************************************
85  *
86  * Protocol Constants, Variables, Data Structures
87  *
88  *********************************************************************/
89
90 /* Initialize the protocol and registered fields */
91 static gint dtls_tap                           = -1;
92 static gint proto_dtls                         = -1;
93 static gint hf_dtls_record                     = -1;
94 static gint hf_dtls_record_content_type        = -1;
95 static gint hf_dtls_record_version             = -1;
96 static gint hf_dtls_record_epoch               = -1;
97 static gint hf_dtls_record_sequence_number     = -1;
98 static gint hf_dtls_record_length              = -1;
99 static gint hf_dtls_record_appdata             = -1;
100 static gint hf_dtls_change_cipher_spec         = -1;
101 static gint hf_dtls_alert_message              = -1;
102 static gint hf_dtls_alert_message_level        = -1;
103 static gint hf_dtls_alert_message_description  = -1;
104 static gint hf_dtls_handshake_protocol         = -1;
105 static gint hf_dtls_handshake_type             = -1;
106 static gint hf_dtls_handshake_length           = -1;
107 static gint hf_dtls_handshake_message_seq      = -1;
108 static gint hf_dtls_handshake_fragment_offset  = -1;
109 static gint hf_dtls_handshake_fragment_length  = -1;
110 static gint hf_dtls_handshake_client_version   = -1;
111 static gint hf_dtls_handshake_server_version   = -1;
112 static gint hf_dtls_handshake_random_time      = -1;
113 static gint hf_dtls_handshake_random_bytes     = -1;
114 static gint hf_dtls_handshake_cookie_len       = -1;
115 static gint hf_dtls_handshake_cookie           = -1;
116 static gint hf_dtls_handshake_cipher_suites_len = -1;
117 static gint hf_dtls_handshake_cipher_suites    = -1;
118 static gint hf_dtls_handshake_cipher_suite     = -1;
119 static gint hf_dtls_handshake_session_id       = -1;
120 static gint hf_dtls_handshake_comp_methods_len = -1;
121 static gint hf_dtls_handshake_comp_methods     = -1;
122 static gint hf_dtls_handshake_comp_method      = -1;
123 static gint hf_dtls_handshake_extensions_len   = -1;
124 static gint hf_dtls_handshake_extension_type   = -1;
125 static gint hf_dtls_handshake_extension_len    = -1;
126 static gint hf_dtls_handshake_extension_data   = -1;
127 static gint hf_dtls_handshake_certificates_len = -1;
128 static gint hf_dtls_handshake_certificates     = -1;
129 static gint hf_dtls_handshake_certificate      = -1;
130 static gint hf_dtls_handshake_certificate_len  = -1;
131 static gint hf_dtls_handshake_cert_types_count = -1;
132 static gint hf_dtls_handshake_cert_types       = -1;
133 static gint hf_dtls_handshake_cert_type        = -1;
134 static gint hf_dtls_handshake_finished         = -1;
135 static gint hf_dtls_handshake_md5_hash         = -1;
136 static gint hf_dtls_handshake_sha_hash         = -1;
137 static gint hf_dtls_handshake_session_id_len   = -1;
138 static gint hf_dtls_handshake_dnames_len       = -1;
139 static gint hf_dtls_handshake_dnames           = -1;
140 static gint hf_dtls_handshake_dname_len        = -1;
141 static gint hf_dtls_handshake_dname            = -1;
142
143 static gint hf_dtls_fragments = -1;
144 static gint hf_dtls_fragment = -1;
145 static gint hf_dtls_fragment_overlap = -1;
146 static gint hf_dtls_fragment_overlap_conflicts = -1;
147 static gint hf_dtls_fragment_multiple_tails = -1;
148 static gint hf_dtls_fragment_too_long_fragment = -1;
149 static gint hf_dtls_fragment_error = -1;
150 static gint hf_dtls_reassembled_in = -1;
151
152 /* Initialize the subtree pointers */
153 static gint ett_dtls                   = -1;
154 static gint ett_dtls_record            = -1;
155 static gint ett_dtls_alert             = -1;
156 static gint ett_dtls_handshake         = -1;
157 static gint ett_dtls_cipher_suites     = -1;
158 static gint ett_dtls_comp_methods      = -1;
159 static gint ett_dtls_extension         = -1;
160 static gint ett_dtls_certs             = -1;
161 static gint ett_dtls_cert_types        = -1;
162 static gint ett_dtls_dnames            = -1;
163
164 static gint ett_dtls_fragment          = -1;
165 static gint ett_dtls_fragments         = -1;
166
167 static GHashTable *dtls_session_hash = NULL;
168 static GHashTable *dtls_key_hash = NULL;
169 static GHashTable *dtls_fragment_table = NULL;
170 static GHashTable *dtls_reassembled_table = NULL;
171 static GTree* dtls_associations = NULL;
172 static dissector_handle_t dtls_handle = NULL;
173 static StringInfo dtls_compressed_data = {NULL, 0};
174 static StringInfo dtls_decrypted_data = {NULL, 0};
175 static gint dtls_decrypted_data_avail = 0;
176
177 static gchar* dtls_keys_list = NULL;
178 #ifdef HAVE_LIBGNUTLS
179 static gchar* dtls_debug_file_name = NULL;
180 #endif
181
182 static const fragment_items dtls_frag_items = {
183         /* Fragment subtrees */
184         &ett_dtls_fragment,
185         &ett_dtls_fragments,
186         /* Fragment fields */
187         &hf_dtls_fragments,
188         &hf_dtls_fragment,
189         &hf_dtls_fragment_overlap,
190         &hf_dtls_fragment_overlap_conflicts,
191         &hf_dtls_fragment_multiple_tails,
192         &hf_dtls_fragment_too_long_fragment,
193         &hf_dtls_fragment_error,
194         /* Reassembled in field */
195         &hf_dtls_reassembled_in,
196         /* Tag */
197         "Message fragments"
198 };
199
200 /* initialize/reset per capture state data (dtls sessions cache) */
201 static void
202 dtls_init(void)
203 {
204   ssl_common_init(&dtls_session_hash, &dtls_decrypted_data, &dtls_compressed_data);
205   fragment_table_init (&dtls_fragment_table);
206   reassembled_table_init(&dtls_reassembled_table);  
207 }
208
209 /* parse dtls related preferences (private keys and ports association strings) */
210 static void
211 dtls_parse(void)
212 {
213   ep_stack_t tmp_stack;
214   SslAssociation *tmp_assoc;
215
216   if (dtls_key_hash)
217     {
218       g_hash_table_foreach(dtls_key_hash, ssl_private_key_free, NULL);
219       g_hash_table_destroy(dtls_key_hash);
220     }
221
222   /* remove only associations created from key list */
223   tmp_stack = ep_stack_new();
224   g_tree_foreach(dtls_associations, ssl_assoc_from_key_list, tmp_stack);
225   while ((tmp_assoc = ep_stack_pop(tmp_stack)) != NULL) {
226     ssl_association_remove(dtls_associations, tmp_assoc);
227   }
228
229   /* parse private keys string, load available keys and put them in key hash*/
230   dtls_key_hash = g_hash_table_new(ssl_private_key_hash, ssl_private_key_equal);
231
232   if (dtls_keys_list && (dtls_keys_list[0] != 0))
233     {
234       ssl_parse_key_list(dtls_keys_list,dtls_key_hash,dtls_associations,dtls_handle,FALSE);
235     }
236
237   ssl_set_debug(dtls_debug_file_name);
238
239   dissector_add_handle("sctp.port", dtls_handle);
240   dissector_add_handle("udp.port", dtls_handle);
241 }
242
243 /*
244  * DTLS Dissection Routines
245  *
246  */
247
248 /* record layer dissector */
249 static gint dissect_dtls_record(tvbuff_t *tvb, packet_info *pinfo,
250                                proto_tree *tree, guint32 offset,
251                                guint *conv_version,
252                                SslDecryptSession *conv_data);
253
254 /* change cipher spec dissector */
255 static void dissect_dtls_change_cipher_spec(tvbuff_t *tvb,
256                                             proto_tree *tree,
257                                             guint32 offset,
258                                             guint *conv_version, guint8 content_type);
259
260 /* alert message dissector */
261 static void dissect_dtls_alert(tvbuff_t *tvb, packet_info *pinfo,
262                                proto_tree *tree, guint32 offset,
263                                guint *conv_version);
264
265 /* handshake protocol dissector */
266 static void dissect_dtls_handshake(tvbuff_t *tvb, packet_info *pinfo,
267                                    proto_tree *tree, guint32 offset,
268                                    guint32 record_length,
269                                    guint *conv_version,
270                                    SslDecryptSession *conv_data, guint8 content_type);
271
272
273 static void dissect_dtls_hnd_cli_hello(tvbuff_t *tvb,
274                                        proto_tree *tree,
275                                        guint32 offset, guint32 length,
276                                        SslDecryptSession* ssl);
277
278 static void dissect_dtls_hnd_hello_verify_request(tvbuff_t *tvb,
279                                                   proto_tree *tree,
280                                                   guint32 offset,
281                                                   SslDecryptSession* ssl);
282
283 static void dissect_dtls_hnd_srv_hello(tvbuff_t *tvb,
284                                        proto_tree *tree,
285                                        guint32 offset, guint32 length,
286                                        SslDecryptSession* ssl);
287
288 static void dissect_dtls_hnd_cert(tvbuff_t *tvb,
289                                   proto_tree *tree, guint32 offset, packet_info *pinfo);
290
291 static void dissect_dtls_hnd_cert_req(tvbuff_t *tvb,
292                                       proto_tree *tree,
293                                       guint32 offset);
294
295 static void dissect_dtls_hnd_finished(tvbuff_t *tvb,
296                                       proto_tree *tree,
297                                       guint32 offset,
298                                       guint* conv_version);
299
300 /*
301  * Support Functions
302  *
303  */
304 /*static void ssl_set_conv_version(packet_info *pinfo, guint version);*/
305 static gint  dtls_is_valid_handshake_type(guint8 type);
306
307 static gint  dtls_is_authoritative_version_message(guint8 content_type,
308                                                   guint8 next_byte);
309 static gint  looks_like_dtls(tvbuff_t *tvb, guint32 offset);
310
311 /*********************************************************************
312  *
313  * Main dissector
314  *
315  *********************************************************************/
316 /*
317  * Code to actually dissect the packets
318  */
319 static void
320 dissect_dtls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
321 {
322
323   conversation_t *conversation;
324   void *conv_data;
325   proto_item *ti;
326   proto_tree *dtls_tree;
327   guint32 offset;
328   gboolean first_record_in_frame;
329   SslDecryptSession* ssl_session;
330   guint* conv_version;
331   ti = NULL;
332   dtls_tree = NULL;
333   offset = 0;
334   first_record_in_frame = TRUE;
335   ssl_session = NULL;
336   top_tree=tree;
337
338   /* Track the version using conversations allows
339    * us to more frequently set the protocol column properly
340    * for continuation data frames.
341    *
342    * Also: We use the copy in conv_version as our cached copy,
343    *       so that we don't have to search the conversation
344    *       table every time we want the version; when setting
345    *       the conv_version, must set the copy in the conversation
346    *       in addition to conv_version
347    */
348   conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
349                                    pinfo->srcport, pinfo->destport, 0);
350   if (!conversation)
351     {
352       /* create a new conversation */
353       conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
354                                       pinfo->srcport, pinfo->destport, 0);
355     }
356   conv_data = conversation_get_proto_data(conversation, proto_dtls);
357
358   /* manage dtls decryption data */
359   /*get a valid ssl session pointer*/
360   if (conv_data != NULL)
361     ssl_session = conv_data;
362   else {
363     SslService dummy;
364
365     ssl_session = se_alloc0(sizeof(SslDecryptSession));
366     ssl_session_init(ssl_session);
367     ssl_session->version = SSL_VER_UNKNOWN;
368     conversation_add_proto_data(conversation, proto_dtls, ssl_session);
369
370     /* we need to know witch side of conversation is speaking */
371     if (ssl_packet_from_server(dtls_associations, pinfo->srcport, pinfo->ptype == PT_TCP)) {
372       dummy.addr = pinfo->src;
373       dummy.port = pinfo->srcport;
374     }
375     else {
376       dummy.addr = pinfo->dst;
377       dummy.port = pinfo->destport;
378     }
379     ssl_debug_printf("dissect_dtls server %s:%d\n",
380                      address_to_str(&dummy.addr),dummy.port);
381
382     /* try to retrive private key for this service. Do it now 'cause pinfo
383      * is not always available
384      * Note that with HAVE_LIBGNUTLS undefined private_key is allways 0
385      * and thus decryption never engaged*/
386     ssl_session->private_key = g_hash_table_lookup(dtls_key_hash, &dummy);
387     if (!ssl_session->private_key)
388       ssl_debug_printf("dissect_dtls can't find private key for this server!\n");
389   }
390   conv_version= & ssl_session->version;
391
392   /* try decryption only the first time we see this packet
393    * (to keep cipher synchronized)and only if we have
394    * the server private key*/
395   if (!ssl_session->private_key || pinfo->fd->flags.visited)
396     ssl_session = NULL;
397
398   /* Initialize the protocol column; we'll set it later when we
399    * figure out what flavor of DTLS it is (actually only one
400    version exists). */
401   if (check_col(pinfo->cinfo, COL_PROTOCOL))
402     {
403       col_set_str(pinfo->cinfo, COL_PROTOCOL, "DTLS");
404     }
405
406   /* clear the the info column */
407   if (check_col(pinfo->cinfo, COL_INFO))
408     col_clear(pinfo->cinfo, COL_INFO);
409
410   /* Create display subtree for SSL as a whole */
411   if (tree)
412     {
413       ti = proto_tree_add_item(tree, proto_dtls, tvb, 0, -1, FALSE);
414       dtls_tree = proto_item_add_subtree(ti, ett_dtls);
415     }
416
417   /* iterate through the records in this tvbuff */
418   while (tvb_reported_length_remaining(tvb, offset) != 0)
419     {
420       /* on second and subsequent records per frame
421        * add a delimiter on info column
422        */
423       if (!first_record_in_frame
424           && check_col(pinfo->cinfo, COL_INFO))
425         {
426           col_append_str(pinfo->cinfo, COL_INFO, ", ");
427         }
428
429       /* first try to dispatch off the cached version
430        * known to be associated with the conversation
431        */
432       switch(*conv_version) {
433       case SSL_VER_DTLS:
434         offset = dissect_dtls_record(tvb, pinfo, dtls_tree,
435                                      offset, conv_version,
436                                      ssl_session);
437         break;
438
439         /* that failed, so apply some heuristics based
440          * on this individual packet
441          */
442       default:
443         if (looks_like_dtls(tvb, offset))
444           {
445             /* looks like dtls */
446             offset = dissect_dtls_record(tvb, pinfo, dtls_tree,
447                                          offset, conv_version,
448                                          ssl_session);
449           }
450         else
451           {
452             /* looks like something unknown, so lump into
453              * continuation data
454              */
455             offset = tvb_length(tvb);
456             if (check_col(pinfo->cinfo, COL_INFO))
457               col_append_str(pinfo->cinfo, COL_INFO,
458                              "Continuation Data");
459
460             /* Set the protocol column */
461             if (check_col(pinfo->cinfo, COL_PROTOCOL))
462               {
463                 col_set_str(pinfo->cinfo, COL_PROTOCOL,"DTLS");
464               }
465           }
466         break;
467       }
468
469       /* set up for next record in frame, if any */
470       first_record_in_frame = FALSE;
471     }
472
473   tap_queue_packet(dtls_tap, pinfo, NULL);
474 }
475
476 static gint
477 decrypt_dtls_record(tvbuff_t *tvb, packet_info *pinfo, guint32 offset,
478                     guint32 record_length, guint8 content_type, SslDecryptSession* ssl,
479                     gboolean save_plaintext)
480 {
481   gint ret;
482   gint direction;
483   SslDecoder* decoder;
484   ret = 0;
485
486   /* if we can decrypt and decryption have success
487    * add decrypted data to this packet info */
488   ssl_debug_printf("decrypt_dtls_record: app_data len %d ssl state %X\n",
489                    record_length, ssl->state);
490   if (!(ssl->state & SSL_HAVE_SESSION_KEY)) {
491     ssl_debug_printf("decrypt_dtls_record: no session key\n");
492     return ret;
493   }
494
495   /* retrive decoder for this packet direction */
496   if ((direction = ssl_packet_from_server(dtls_associations, pinfo->srcport, pinfo->ptype == PT_TCP)) != 0) {
497     ssl_debug_printf("decrypt_dtls_record: using server decoder\n");
498     decoder = ssl->server;
499   }
500   else {
501     ssl_debug_printf("decrypt_dtls_record: using client decoder\n");
502     decoder = ssl->client;
503   }
504
505   /* ensure we have enough storage space for decrypted data */
506   if (record_length > dtls_decrypted_data.data_len)
507     {
508       ssl_debug_printf("decrypt_dtls_record: allocating %d bytes"
509                        " for decrypt data (old len %d)\n",
510                        record_length + 32, dtls_decrypted_data.data_len);
511       dtls_decrypted_data.data = g_realloc(dtls_decrypted_data.data,
512                                            record_length + 32);
513       dtls_decrypted_data.data_len = record_length + 32;
514     }
515
516   /* run decryption and add decrypted payload to protocol data, if decryption
517    * is successful*/
518   dtls_decrypted_data_avail = dtls_decrypted_data.data_len;
519   if (ssl_decrypt_record(ssl, decoder,
520                          content_type, tvb_get_ptr(tvb, offset, record_length),
521                          record_length, &dtls_compressed_data, &dtls_decrypted_data, &dtls_decrypted_data_avail) == 0)
522     ret = 1;
523
524     if (ret && save_plaintext) {
525       ssl_add_data_info(proto_dtls, pinfo, dtls_decrypted_data.data, dtls_decrypted_data_avail,  TVB_RAW_OFFSET(tvb)+offset, 0);
526     }
527
528   return ret;
529 }
530
531
532
533
534
535 /*********************************************************************
536  *
537  * DTLS Dissection Routines
538  *
539  *********************************************************************/
540 static gint
541 dissect_dtls_record(tvbuff_t *tvb, packet_info *pinfo,
542                     proto_tree *tree, guint32 offset,
543                     guint *conv_version,
544                     SslDecryptSession* ssl)
545 {
546
547   /*
548    *    struct {
549    *        uint8 major, minor;
550    *    } ProtocolVersion;
551    *
552    *
553    *    enum {
554    *        change_cipher_spec(20), alert(21), handshake(22),
555    *        application_data(23), (255)
556    *    } ContentType;
557    *
558    *    struct {
559    *        ContentType type;
560    *        ProtocolVersion version;
561    *       uint16 epoch;               // New field
562    *       uint48 sequence_number;       // New field
563    *        uint16 length;
564    *        opaque fragment[TLSPlaintext.length];
565    *    } DTLSPlaintext;
566    */
567   guint32 record_length;
568   guint16 version;
569   guint16 epoch;
570   gdouble sequence_number;
571   gint64 sequence_number_temp;
572   guint8 content_type;
573   guint8 next_byte;
574   proto_tree *ti;
575   proto_tree *dtls_record_tree;
576   guint32 available_bytes;
577   SslAssociation* association;
578   SslDataInfo *appl_data;
579   ti              = NULL;
580   dtls_record_tree = NULL;
581   available_bytes = tvb_length_remaining(tvb, offset);
582
583   /*
584    * Get the record layer fields of interest
585    */
586   content_type  = tvb_get_guint8(tvb, offset);
587   version       = tvb_get_ntohs(tvb, offset + 1);
588   epoch       = tvb_get_ntohs(tvb, offset + 3);
589   sequence_number  = tvb_get_ntohl(tvb, offset + 7);
590   sequence_number_temp=tvb_get_ntohs(tvb, offset + 5);
591   sequence_number_temp=sequence_number_temp<<32;
592   sequence_number+=sequence_number_temp;
593   record_length = tvb_get_ntohs(tvb, offset + 11);
594
595   if(ssl){
596     if(ssl_packet_from_server(dtls_associations, pinfo->srcport, pinfo->ptype == PT_TCP)){
597      if (ssl->server) {
598       ssl->server->seq=(guint32)sequence_number;
599       ssl->server->epoch=epoch;
600      }
601     }
602     else{
603      if (ssl->client) {
604       ssl->client->seq=(guint32)sequence_number;
605       ssl->client->epoch=epoch;
606      }
607     }
608   }
609   if (!ssl_is_valid_content_type(content_type)) {
610
611     /* if we don't have a valid content_type, there's no sense
612      * continuing any further
613      */
614     if (check_col(pinfo->cinfo, COL_INFO))
615       col_append_str(pinfo->cinfo, COL_INFO, "Continuation Data");
616
617     /* Set the protocol column */
618     if (check_col(pinfo->cinfo, COL_PROTOCOL))
619       {
620         col_set_str(pinfo->cinfo, COL_PROTOCOL,"DTLS");
621       }
622     return offset + 13 + record_length;
623   }
624
625   /*
626    * If GUI, fill in record layer part of tree
627    */
628
629   if (tree)
630     {
631       /* add the record layer subtree header */
632       tvb_ensure_bytes_exist(tvb, offset, 13 + record_length);
633       ti = proto_tree_add_item(tree, hf_dtls_record, tvb,
634                                offset, 13 + record_length, 0);
635       dtls_record_tree = proto_item_add_subtree(ti, ett_dtls_record);
636     }
637
638   if (dtls_record_tree)
639     {
640
641       /* show the one-byte content type */
642       proto_tree_add_item(dtls_record_tree, hf_dtls_record_content_type,
643                           tvb, offset, 1, FALSE);
644       offset++;
645
646       /* add the version */
647       proto_tree_add_item(dtls_record_tree, hf_dtls_record_version, tvb,
648                           offset, 2, FALSE);
649       offset += 2;
650
651       /* show epoch */
652       proto_tree_add_uint(dtls_record_tree, hf_dtls_record_epoch, tvb, offset, 2, epoch);
653
654       offset += 2;
655
656       /* add sequence_number */
657
658       proto_tree_add_double(dtls_record_tree, hf_dtls_record_sequence_number, tvb, offset, 6, sequence_number);
659
660       offset += 6;
661
662       /* add the length */
663       proto_tree_add_uint(dtls_record_tree, hf_dtls_record_length, tvb,
664                           offset, 2, record_length);
665       offset += 2;    /* move past length field itself */
666
667     }
668   else
669     {
670       /* if no GUI tree, then just skip over those fields */
671       offset += 13;
672     }
673
674
675   /*
676    * if we don't already have a version set for this conversation,
677    * but this message's version is authoritative (i.e., it's
678    * not client_hello, then save the version to to conversation
679    * structure and print the column version
680    */
681   next_byte = tvb_get_guint8(tvb, offset);
682   if (*conv_version == SSL_VER_UNKNOWN
683       && dtls_is_authoritative_version_message(content_type, next_byte))
684     {
685       if (version == DTLSV1DOT0_VERSION ||
686           version == DTLSV1DOT0_VERSION_NOT)
687         {
688
689           *conv_version = SSL_VER_DTLS;
690           if (ssl) {
691             ssl->version_netorder = version;
692             ssl->state |= SSL_VERSION;
693           }
694           /*ssl_set_conv_version(pinfo, ssl->version);*/
695         }
696     }
697   if (check_col(pinfo->cinfo, COL_PROTOCOL))
698     {
699       if (version == DTLSV1DOT0_VERSION)
700         {
701           col_set_str(pinfo->cinfo, COL_PROTOCOL,
702                       ssl_version_short_names[SSL_VER_DTLS]);
703         }
704       else
705         {
706           col_set_str(pinfo->cinfo, COL_PROTOCOL,"DTLS");
707         }
708     }
709
710   /*
711    * now dissect the next layer
712    */
713   ssl_debug_printf("dissect_dtls_record: content_type %d\n",content_type);
714
715   /* PAOLO try to decrypt each record (we must keep ciphers "in sync")
716    * store plain text only for app data */
717
718   switch (content_type) {
719   case SSL_ID_CHG_CIPHER_SPEC:
720     if (check_col(pinfo->cinfo, COL_INFO))
721       col_append_str(pinfo->cinfo, COL_INFO, "Change Cipher Spec");
722     dissect_dtls_change_cipher_spec(tvb, dtls_record_tree,
723                                     offset, conv_version, content_type);
724     break;
725   case SSL_ID_ALERT:
726     {
727       tvbuff_t* decrypted;
728       decrypted = 0;
729       if (ssl&&decrypt_dtls_record(tvb, pinfo, offset,
730                                    record_length, content_type, ssl, FALSE))
731         ssl_add_record_info(proto_dtls, pinfo, dtls_decrypted_data.data,
732                             dtls_decrypted_data_avail, offset);
733
734       /* try to retrive and use decrypted alert record, if any. */
735       decrypted = ssl_get_record_info(proto_dtls, pinfo, offset);
736       if (decrypted)
737         dissect_dtls_alert(decrypted, pinfo, dtls_record_tree, 0,
738                            conv_version);
739       else
740         dissect_dtls_alert(tvb, pinfo, dtls_record_tree, offset,
741                            conv_version);
742       break;
743     }
744   case SSL_ID_HANDSHAKE:
745     {
746       tvbuff_t* decrypted;
747       decrypted = 0;
748       /* try to decrypt handshake record, if possible. Store decrypted
749        * record for later usage. The offset is used as 'key' to itentify
750        * this record into the packet (we can have multiple handshake records
751        * in the same frame) */
752       if (ssl && decrypt_dtls_record(tvb, pinfo, offset,
753                                      record_length, content_type, ssl, FALSE))
754         ssl_add_record_info(proto_dtls, pinfo, dtls_decrypted_data.data,
755                             dtls_decrypted_data_avail, offset);
756
757       /* try to retrive and use decrypted handshake record, if any. */
758       decrypted = ssl_get_record_info(proto_dtls, pinfo, offset);
759       if (decrypted)
760         dissect_dtls_handshake(decrypted, pinfo, dtls_record_tree, 0,
761                                decrypted->length, conv_version, ssl, content_type);
762       else
763         dissect_dtls_handshake(tvb, pinfo, dtls_record_tree, offset,
764                                record_length, conv_version, ssl, content_type);
765       break;
766     }
767   case SSL_ID_APP_DATA:
768     if (ssl)
769       decrypt_dtls_record(tvb, pinfo, offset,
770                           record_length, content_type, ssl, TRUE);
771
772     /* show on info colum what we are decoding */
773     if (check_col(pinfo->cinfo, COL_INFO))
774       col_append_str(pinfo->cinfo, COL_INFO, "Application Data");
775
776     if (!dtls_record_tree)
777       break;
778
779     /* we need dissector information when the selected packet is shown.
780      * ssl session pointer is NULL at that time, so we can't access
781      * info cached there*/
782     association = ssl_association_find(dtls_associations, pinfo->srcport, pinfo->ptype == PT_TCP);
783     association = association ? association: ssl_association_find(dtls_associations, pinfo->destport, pinfo->ptype == PT_TCP);
784
785     proto_item_set_text(dtls_record_tree,
786                         "%s Record Layer: %s Protocol: %s",
787                         ssl_version_short_names[*conv_version],
788                         val_to_str(content_type, ssl_31_content_type, "unknown"),
789                         association?association->info:"Application Data");
790
791     proto_tree_add_item(dtls_record_tree, hf_dtls_record_appdata, tvb,
792                         offset, record_length, 0);
793
794     /* show decrypted data info, if available */
795     appl_data = ssl_get_data_info(proto_dtls, pinfo, TVB_RAW_OFFSET(tvb)+offset);
796     if (appl_data && (appl_data->plain_data.data_len > 0))
797       {
798                 tvbuff_t *next_tvb;
799         /* try to dissect decrypted data*/
800         ssl_debug_printf("dissect_dtls_record decrypted len %d\n",
801                          appl_data->plain_data.data_len);
802
803                 /* create a new TVB structure for desegmented data */
804                 next_tvb = tvb_new_child_real_data(tvb, appl_data->plain_data.data, appl_data->plain_data.data_len, appl_data->plain_data.data_len);
805
806         add_new_data_source(pinfo, next_tvb, "Decrypted DTLS data");
807
808         /* find out a dissector using server port*/
809         if (association && association->handle) {
810           ssl_debug_printf("dissect_dtls_record found association %p\n", (void *)association);
811           ssl_print_text_data("decrypted app data",appl_data->plain_data.data, appl_data->plain_data.data_len);
812
813           call_dissector(association->handle, next_tvb, pinfo, top_tree);
814         }
815       }
816     break;
817
818   default:
819     /* shouldn't get here since we check above for valid types */
820     if (check_col(pinfo->cinfo, COL_INFO))
821       col_append_str(pinfo->cinfo, COL_INFO, "Bad DTLS Content Type");
822     break;
823   }
824   offset += record_length; /* skip to end of record */
825
826   return offset;
827 }
828
829 /* dissects the change cipher spec procotol, filling in the tree */
830 static void
831 dissect_dtls_change_cipher_spec(tvbuff_t *tvb,
832                                 proto_tree *tree, guint32 offset,
833                                 guint* conv_version, guint8 content_type)
834 {
835   /*
836    * struct {
837    *     enum { change_cipher_spec(1), (255) } type;
838    * } ChangeCipherSpec;
839    *
840    */
841   if (tree)
842     {
843       proto_item_set_text(tree,
844                           "%s Record Layer: %s Protocol: Change Cipher Spec",
845                           ssl_version_short_names[*conv_version],
846                           val_to_str(content_type, ssl_31_content_type, "unknown"));
847       proto_tree_add_item(tree, hf_dtls_change_cipher_spec, tvb,
848                           offset++, 1, FALSE);
849     }
850 }
851
852 /* dissects the alert message, filling in the tree */
853 static void
854 dissect_dtls_alert(tvbuff_t *tvb, packet_info *pinfo,
855                    proto_tree *tree, guint32 offset,
856                    guint* conv_version)
857 {
858   /*     struct {
859    *         AlertLevel level;
860    *         AlertDescription description;
861    *     } Alert;
862    */
863   proto_tree *ti;
864   proto_tree *ssl_alert_tree;
865   const gchar *level;
866   const gchar *desc;
867   guint8 byte;
868   ssl_alert_tree = NULL;
869
870   if (tree)
871     {
872       ti = proto_tree_add_item(tree, hf_dtls_alert_message, tvb,
873                                offset, 2, 0);
874       ssl_alert_tree = proto_item_add_subtree(ti, ett_dtls_alert);
875     }
876
877   /*
878    * set the record layer label
879    */
880
881   /* first lookup the names for the alert level and description */
882   byte = tvb_get_guint8(tvb, offset); /* grab the level byte */
883   level = match_strval(byte, ssl_31_alert_level);
884
885   byte = tvb_get_guint8(tvb, offset+1); /* grab the desc byte */
886   desc = match_strval(byte, ssl_31_alert_description);
887
888   /* now set the text in the record layer line */
889   if (level && desc)
890     {
891       if (check_col(pinfo->cinfo, COL_INFO))
892         col_append_fstr(pinfo->cinfo, COL_INFO,
893                         "Alert (Level: %s, Description: %s)",
894                         level, desc);
895     }
896   else
897     {
898       if (check_col(pinfo->cinfo, COL_INFO))
899         col_append_str(pinfo->cinfo, COL_INFO, "Encrypted Alert");
900     }
901
902   if (tree)
903     {
904       if (level && desc)
905         {
906           proto_item_set_text(tree, "%s Record Layer: Alert "
907                               "(Level: %s, Description: %s)",
908                               ssl_version_short_names[*conv_version],
909                               level, desc);
910           proto_tree_add_item(ssl_alert_tree, hf_dtls_alert_message_level,
911                               tvb, offset++, 1, FALSE);
912
913           proto_tree_add_item(ssl_alert_tree, hf_dtls_alert_message_description,
914                               tvb, offset++, 1, FALSE);
915         }
916       else
917         {
918           proto_item_set_text(tree,
919                               "%s Record Layer: Encrypted Alert",
920                               ssl_version_short_names[*conv_version]);
921           proto_item_set_text(ssl_alert_tree,
922                               "Alert Message: Encrypted Alert");
923         }
924     }
925 }
926
927
928 /* dissects the handshake protocol, filling the tree */
929 static void
930 dissect_dtls_handshake(tvbuff_t *tvb, packet_info *pinfo,
931                        proto_tree *tree, guint32 offset,
932                        guint32 record_length, guint *conv_version,
933                        SslDecryptSession* ssl, guint8 content_type)
934 {
935   /*     struct {
936    *         HandshakeType msg_type;
937    *         uint24 length;
938    *         uint16 message_seq;          //new field
939    *         uint24 fragment_offset;       //new field
940    *         uint24 fragment_length;        //new field
941    *         select (HandshakeType) {
942    *             case hello_request:       HelloRequest;
943    *             case client_hello:        ClientHello;
944    *             case server_hello:        ServerHello;
945    *             case hello_verify_request: HelloVerifyRequest;     //new field
946    *             case certificate:         Certificate;
947    *             case server_key_exchange: ServerKeyExchange;
948    *             case certificate_request: CertificateRequest;
949    *             case server_hello_done:   ServerHelloDone;
950    *             case certificate_verify:  CertificateVerify;
951    *             case client_key_exchange: ClientKeyExchange;
952    *             case finished:            Finished;
953    *         } body;
954    *     } Handshake;
955    */
956   proto_tree *ti;
957   proto_tree *ssl_hand_tree;
958   const gchar *msg_type_str;
959   guint8 msg_type;
960   guint32 length;
961   guint16 message_seq;
962   guint32 fragment_offset;
963   guint32 fragment_length;
964   gboolean first_iteration;
965   ti               = NULL;
966   ssl_hand_tree    = NULL;
967   msg_type_str     = NULL;
968   first_iteration  = TRUE;
969
970   /* just as there can be multiple records per packet, there
971    * can be multiple messages per record as long as they have
972    * the same content type
973    *
974    * we really only care about this for handshake messages
975    */
976
977   /* set record_length to the max offset */
978   record_length += offset;
979   for (; offset < record_length; offset += fragment_length,
980          first_iteration = FALSE) /* set up for next pass, if any */
981     {
982       fragment_data *frag_msg = NULL;
983       tvbuff_t *new_tvb = NULL;
984       const gchar *frag_str = NULL;
985       gboolean fragmented;
986
987       msg_type = tvb_get_guint8(tvb, offset);
988       msg_type_str = match_strval(msg_type, ssl_31_handshake_type);
989       length   = tvb_get_ntoh24(tvb, offset + 1);
990       message_seq = tvb_get_ntohs(tvb,offset + 4);
991       fragment_offset = tvb_get_ntoh24(tvb, offset + 6);
992       fragment_length = tvb_get_ntoh24(tvb, offset + 9);
993       fragmented = fragment_length != length;
994
995       if (!msg_type_str && !first_iteration)
996         {
997           /* only dissect / report messages if they're
998            * either the first message in this record
999            * or they're a valid message type
1000            */
1001           return;
1002         }
1003
1004       /* on second and later iterations, add comma to info col */
1005       if (!first_iteration)
1006         {
1007           if (check_col(pinfo->cinfo, COL_INFO))
1008             col_append_str(pinfo->cinfo, COL_INFO, ", ");
1009         }
1010
1011       /*
1012        * Update our info string
1013        */
1014       if (check_col(pinfo->cinfo, COL_INFO))
1015         col_append_str(pinfo->cinfo, COL_INFO, (msg_type_str != NULL)
1016                         ? msg_type_str : "Encrypted Handshake Message");
1017
1018       /* Handle fragments of known message type */
1019       if (fragmented)
1020         {
1021           gboolean frag_hand;
1022
1023           switch (msg_type) {
1024           case SSL_HND_HELLO_REQUEST:
1025           case SSL_HND_CLIENT_HELLO:
1026           case SSL_HND_HELLO_VERIFY_REQUEST:
1027           case SSL_HND_SERVER_HELLO:
1028           case SSL_HND_CERTIFICATE:
1029           case SSL_HND_SERVER_KEY_EXCHG:
1030           case SSL_HND_CERT_REQUEST:
1031           case SSL_HND_SVR_HELLO_DONE:
1032           case SSL_HND_CERT_VERIFY:
1033           case SSL_HND_CLIENT_KEY_EXCHG:
1034           case SSL_HND_FINISHED:
1035             frag_hand = TRUE;
1036             break;
1037           default:
1038             /* Ignore encrypted handshake messages */
1039             frag_hand = FALSE;
1040             break;
1041           }
1042
1043           if (frag_hand) {
1044             /* Fragmented handshake message */
1045             pinfo->fragmented = TRUE;
1046             frag_msg = fragment_add(tvb, offset+12, pinfo, message_seq,
1047                                     dtls_fragment_table,
1048                                     fragment_offset, fragment_length, TRUE);
1049             fragment_set_tot_len(pinfo, message_seq, dtls_fragment_table,
1050                                  length);
1051
1052             if (frag_msg && (fragment_length + fragment_offset) == length)
1053               {
1054                 /* Reassembled */
1055                 new_tvb = process_reassembled_data(tvb, offset+12, pinfo,
1056                                                    "Reassembled Message",
1057                                                    frag_msg,
1058                                                    &dtls_frag_items,
1059                                                    NULL, tree);
1060                 frag_str = " (Reassembled)";
1061               }
1062             else
1063               {
1064                 frag_str = " (Fragment)";
1065               }
1066
1067             if (check_col(pinfo->cinfo, COL_INFO))
1068               col_append_str(pinfo->cinfo, COL_INFO, frag_str);
1069           }
1070         }
1071
1072       if (tree)
1073         {
1074           /* set the label text on the record layer expanding node */
1075           if (first_iteration)
1076             {
1077               proto_item_set_text(tree, "%s Record Layer: %s Protocol: %s%s",
1078                                   ssl_version_short_names[*conv_version],
1079                                   val_to_str(content_type, ssl_31_content_type, "unknown"),
1080                                   (msg_type_str!=NULL) ? msg_type_str :
1081                                   "Encrypted Handshake Message",
1082                                   (frag_str!=NULL) ? frag_str : "");
1083             }
1084           else
1085             {
1086               proto_item_set_text(tree, "%s Record Layer: %s Protocol: %s%s",
1087                                   ssl_version_short_names[*conv_version],
1088                                   val_to_str(content_type, ssl_31_content_type, "unknown"),
1089                                   "Multiple Handshake Messages",
1090                                   (frag_str!=NULL) ? frag_str : "");
1091             }
1092
1093           /* add a subtree for the handshake protocol */
1094           ti = proto_tree_add_item(tree, hf_dtls_handshake_protocol, tvb,
1095                                    offset, fragment_length + 12, 0);
1096           ssl_hand_tree = proto_item_add_subtree(ti, ett_dtls_handshake);
1097
1098           if (ssl_hand_tree)
1099             {
1100               /* set the text label on the subtree node */
1101               proto_item_set_text(ssl_hand_tree, "Handshake Protocol: %s%s",
1102                                   (msg_type_str != NULL) ? msg_type_str :
1103                                   "Encrypted Handshake Message",
1104                                   (frag_str!=NULL) ? frag_str : "");
1105             }
1106         }
1107
1108       /* if we don't have a valid handshake type, just quit dissecting */
1109       if (!msg_type_str)
1110         return;
1111
1112       /* if we are doing ssl decryption we must dissect some requests type */
1113       if (ssl_hand_tree || ssl)
1114         {
1115           tvbuff_t *sub_tvb = NULL;
1116
1117           /* add nodes for the message type and message length */
1118           if (ssl_hand_tree)
1119             proto_tree_add_item(ssl_hand_tree, hf_dtls_handshake_type,
1120                                 tvb, offset, 1, msg_type);
1121           offset++;
1122           if (ssl_hand_tree)
1123             proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_length,
1124                                 tvb, offset, 3, length);
1125           offset += 3;
1126
1127           if (ssl_hand_tree)
1128             proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_message_seq,
1129                                 tvb, offset, 2, message_seq);
1130           offset += 2;
1131           if (ssl_hand_tree)
1132             proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_fragment_offset,
1133                                 tvb, offset, 3, fragment_offset);
1134           offset += 3;
1135           if (ssl_hand_tree)
1136             proto_tree_add_uint(ssl_hand_tree, hf_dtls_handshake_fragment_length,
1137                                 tvb, offset, 3, fragment_length);
1138           offset += 3;
1139
1140           if (fragmented && !new_tvb)
1141             {
1142               /* Skip fragmented messages not reassembled yet */
1143               continue;
1144             }
1145
1146           if (new_tvb)
1147             {
1148               sub_tvb = new_tvb;
1149             }
1150           else
1151             {
1152               sub_tvb = tvb_new_subset(tvb, offset, fragment_length,
1153                                         fragment_length);
1154             }
1155
1156           /* now dissect the handshake message, if necessary */
1157           switch (msg_type) {
1158           case SSL_HND_HELLO_REQUEST:
1159             /* hello_request has no fields, so nothing to do! */
1160             break;
1161
1162           case SSL_HND_CLIENT_HELLO:
1163             dissect_dtls_hnd_cli_hello(sub_tvb, ssl_hand_tree, 0, length, ssl);
1164             break;
1165
1166           case SSL_HND_HELLO_VERIFY_REQUEST:
1167             dissect_dtls_hnd_hello_verify_request(sub_tvb, ssl_hand_tree, 0,  ssl);
1168             break;
1169
1170           case SSL_HND_SERVER_HELLO:
1171             dissect_dtls_hnd_srv_hello(sub_tvb, ssl_hand_tree, 0, length, ssl);
1172             break;
1173
1174           case SSL_HND_CERTIFICATE:
1175             dissect_dtls_hnd_cert(sub_tvb, ssl_hand_tree, 0, pinfo);
1176             break;
1177
1178           case SSL_HND_SERVER_KEY_EXCHG:
1179             /* unimplemented */
1180             break;
1181
1182           case SSL_HND_CERT_REQUEST:
1183             dissect_dtls_hnd_cert_req(sub_tvb, ssl_hand_tree, 0);
1184             break;
1185
1186           case SSL_HND_SVR_HELLO_DONE:
1187             /* server_hello_done has no fields, so nothing to do! */
1188             break;
1189
1190           case SSL_HND_CERT_VERIFY:
1191             /* unimplemented */
1192             break;
1193
1194           case SSL_HND_CLIENT_KEY_EXCHG:
1195             {
1196               /* here we can have all the data to build session key */
1197               StringInfo encrypted_pre_master;
1198               gint ret;
1199               guint encrlen = length, skip;
1200               skip = 0;
1201
1202               if (!ssl)
1203                 break;
1204
1205               /* check for required session data */
1206               ssl_debug_printf("dissect_dtls_handshake found SSL_HND_CLIENT_KEY_EXCHG state %X\n",
1207                                ssl->state);
1208               if ((ssl->state & (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) !=
1209                   (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION)) {
1210                 ssl_debug_printf("dissect_dtls_handshake not enough data to generate key (required %X)\n",
1211                                  (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION));
1212                 break;
1213               }
1214
1215               encrypted_pre_master.data = se_alloc(encrlen);
1216               encrypted_pre_master.data_len = encrlen;
1217               tvb_memcpy(tvb, encrypted_pre_master.data, offset+skip, encrlen);
1218
1219               if (!ssl->private_key) {
1220                 ssl_debug_printf("dissect_dtls_handshake can't find private key\n");
1221                 break;
1222               }
1223
1224               /* go with ssl key processessing; encrypted_pre_master
1225                * will be used for master secret store*/
1226               ret = ssl_decrypt_pre_master_secret(ssl, &encrypted_pre_master, ssl->private_key);
1227               if (ret < 0) {
1228                 ssl_debug_printf("dissect_dtls_handshake can't decrypt pre master secret\n");
1229                 break;
1230               }
1231               if (ssl_generate_keyring_material(ssl)<0) {
1232                 ssl_debug_printf("dissect_dtls_handshake can't generate keyring material\n");
1233                 break;
1234               }
1235               ssl->state |= SSL_HAVE_SESSION_KEY;
1236               ssl_save_session(ssl, dtls_session_hash);
1237               ssl_debug_printf("dissect_dtls_handshake session keys successfully generated\n");
1238             }
1239             break;
1240
1241           case SSL_HND_FINISHED:
1242             dissect_dtls_hnd_finished(sub_tvb, ssl_hand_tree,
1243                                       0, conv_version);
1244             break;
1245           }
1246
1247         }
1248       else{
1249         offset += 12;        /* skip the handshake header when handshake is not processed*/
1250       }
1251     }
1252 }
1253
1254 static gint
1255 dissect_dtls_hnd_hello_common(tvbuff_t *tvb, proto_tree *tree,
1256                               guint32 offset, SslDecryptSession* ssl, gint from_server)
1257 {
1258   /* show the client's random challenge */
1259   nstime_t gmt_unix_time;
1260   guint8  session_id_length;
1261   session_id_length = 0;
1262   if (ssl)
1263     {
1264       /* get proper peer information*/
1265       StringInfo* rnd;
1266       if (from_server)
1267         rnd = &ssl->server_random;
1268       else
1269         rnd = &ssl->client_random;
1270
1271       /* get provided random for keyring generation*/
1272       tvb_memcpy(tvb, rnd->data, offset, 32);
1273       rnd->data_len = 32;
1274       if (from_server)
1275         ssl->state |= SSL_SERVER_RANDOM;
1276       else
1277         ssl->state |= SSL_CLIENT_RANDOM;
1278       ssl_debug_printf("dissect_dtls_hnd_hello_common found random state %X\n",
1279                        ssl->state);
1280
1281       session_id_length = tvb_get_guint8(tvb, offset + 32);
1282       /* check stored session id info */
1283       if (from_server && (session_id_length == ssl->session_id.data_len) &&
1284           (tvb_memeql(tvb, offset+33, ssl->session_id.data, session_id_length) == 0))
1285         {
1286           /* clinet/server id match: try to restore a previous cached session*/
1287           ssl_restore_session(ssl, dtls_session_hash);
1288         }
1289       else {
1290         tvb_memcpy(tvb,ssl->session_id.data, offset+33, session_id_length);
1291         ssl->session_id.data_len = session_id_length;
1292       }
1293     }
1294
1295   if (tree)
1296     {
1297       /* show the time */
1298       gmt_unix_time.secs = tvb_get_ntohl(tvb, offset);
1299       gmt_unix_time.nsecs = 0;
1300       proto_tree_add_time(tree, hf_dtls_handshake_random_time,
1301                           tvb, offset, 4, &gmt_unix_time);
1302       offset += 4;
1303
1304       /* show the random bytes */
1305       proto_tree_add_item(tree, hf_dtls_handshake_random_bytes,
1306                           tvb, offset, 28, 0);
1307       offset += 28;
1308
1309       /* show the session id */
1310       session_id_length = tvb_get_guint8(tvb, offset);
1311       proto_tree_add_item(tree, hf_dtls_handshake_session_id_len,
1312                           tvb, offset++, 1, 0);
1313       if (session_id_length > 0)
1314         {
1315           proto_tree_add_bytes_format(tree, hf_dtls_handshake_session_id,
1316                                       tvb, offset, session_id_length,
1317                                       tvb_get_ptr(tvb, offset, session_id_length),
1318                                       "Session ID (%u byte%s)",
1319                                       session_id_length,
1320                                       plurality(session_id_length, "", "s"));
1321           offset += session_id_length;
1322         }
1323
1324     }
1325
1326   /* XXXX */
1327   return session_id_length+33;
1328 }
1329
1330 static gint
1331 dissect_dtls_hnd_hello_ext(tvbuff_t *tvb,
1332                            proto_tree *tree, guint32 offset, guint32 left)
1333 {
1334   guint16 extension_length;
1335   guint16 ext_type;
1336   guint16 ext_len;
1337   proto_item *pi;
1338   proto_tree *ext_tree;
1339
1340   if (left < 2)
1341     return offset;
1342
1343   extension_length = tvb_get_ntohs(tvb, offset);
1344   proto_tree_add_uint(tree, hf_dtls_handshake_extensions_len,
1345                       tvb, offset, 2, extension_length);
1346   offset += 2;
1347   left -= 2;
1348
1349   while (left >= 4)
1350     {
1351       ext_type = tvb_get_ntohs(tvb, offset);
1352       ext_len = tvb_get_ntohs(tvb, offset + 2);
1353
1354       pi = proto_tree_add_text(tree, tvb, offset, 4 + ext_len,
1355                                "Extension: %s",
1356                                val_to_str(ext_type,
1357                                           tls_hello_extension_types,
1358                                           "Unknown %u"));
1359       ext_tree = proto_item_add_subtree(pi, ett_dtls_extension);
1360       if (!ext_tree)
1361         ext_tree = tree;
1362
1363       proto_tree_add_uint(ext_tree, hf_dtls_handshake_extension_type,
1364                           tvb, offset, 2, ext_type);
1365       offset += 2;
1366
1367       proto_tree_add_uint(ext_tree, hf_dtls_handshake_extension_len,
1368                           tvb, offset, 2, ext_len);
1369       offset += 2;
1370
1371       proto_tree_add_bytes_format(ext_tree, hf_dtls_handshake_extension_data,
1372                                   tvb, offset, ext_len,
1373                                   tvb_get_ptr(tvb, offset, ext_len),
1374                                   "Data (%u byte%s)",
1375                                   ext_len, plurality(ext_len, "", "s"));
1376       offset += ext_len;
1377       left -= 2 + 2 + ext_len;
1378     }
1379
1380   return offset;
1381 }
1382
1383 static void
1384 dissect_dtls_hnd_cli_hello(tvbuff_t *tvb,
1385                            proto_tree *tree, guint32 offset, guint32 length,
1386                            SslDecryptSession*ssl)
1387 {
1388   /* struct {
1389    *     ProtocolVersion client_version;
1390    *     Random random;
1391    *     SessionID session_id;
1392    *     opaque cookie<0..32>;                   //new field
1393    *     CipherSuite cipher_suites<2..2^16-1>;
1394    *     CompressionMethod compression_methods<1..2^8-1>;
1395    *     Extension client_hello_extension_list<0..2^16-1>;
1396    * } ClientHello;
1397    *
1398    */
1399   proto_tree *ti;
1400   proto_tree *cs_tree;
1401   guint16 cipher_suite_length;
1402   guint8  compression_methods_length;
1403   guint8  compression_method;
1404   guint16 start_offset = offset;
1405   guint8 cookie_length;
1406   cipher_suite_length = 0;
1407   compression_methods_length = 0;
1408   cookie_length = 0;
1409
1410   if (tree || ssl)
1411     {
1412       /* show the client version */
1413       if (tree)
1414         proto_tree_add_item(tree, hf_dtls_handshake_client_version, tvb,
1415                             offset, 2, FALSE);
1416       offset += 2;
1417
1418       /* show the fields in common with server hello */
1419       offset += dissect_dtls_hnd_hello_common(tvb, tree, offset, ssl, 0);
1420
1421       /* look for a cookie */
1422       cookie_length = tvb_get_guint8(tvb, offset);
1423       if (!tree)
1424         return;
1425
1426       proto_tree_add_uint(tree, hf_dtls_handshake_cookie_len,
1427                           tvb, offset, 1, cookie_length);
1428       offset ++;            /* skip opaque length */
1429
1430       if (cookie_length > 0)
1431         {
1432           proto_tree_add_bytes_format(tree, hf_dtls_handshake_cookie,
1433                                       tvb, offset, cookie_length,
1434                                       tvb_get_ptr(tvb, offset, cookie_length),
1435                                       "Cookie (%u byte%s)",
1436                                       cookie_length,
1437                                       plurality(cookie_length, "", "s"));
1438           offset += cookie_length;
1439         }
1440
1441       /* tell the user how many cipher suites there are */
1442       cipher_suite_length = tvb_get_ntohs(tvb, offset);
1443
1444       proto_tree_add_uint(tree, hf_dtls_handshake_cipher_suites_len,
1445                           tvb, offset, 2, cipher_suite_length);
1446       offset += 2;            /* skip opaque length */
1447
1448       if (cipher_suite_length > 0)
1449         {
1450           tvb_ensure_bytes_exist(tvb, offset, cipher_suite_length);
1451           ti = proto_tree_add_none_format(tree,
1452                                           hf_dtls_handshake_cipher_suites,
1453                                           tvb, offset, cipher_suite_length,
1454                                           "Cipher Suites (%u suite%s)",
1455                                           cipher_suite_length / 2,
1456                                           plurality(cipher_suite_length/2, "", "s"));
1457
1458           /* make this a subtree */
1459           cs_tree = proto_item_add_subtree(ti, ett_dtls_cipher_suites);
1460           if (!cs_tree)
1461             {
1462               cs_tree = tree; /* failsafe */
1463             }
1464
1465           while (cipher_suite_length > 0)
1466             {
1467               proto_tree_add_item(cs_tree, hf_dtls_handshake_cipher_suite,
1468                                   tvb, offset, 2, FALSE);
1469               offset += 2;
1470               cipher_suite_length -= 2;
1471             }
1472         }
1473
1474       /* tell the user how man compression methods there are */
1475       compression_methods_length = tvb_get_guint8(tvb, offset);
1476       proto_tree_add_uint(tree, hf_dtls_handshake_comp_methods_len,
1477                           tvb, offset, 1, compression_methods_length);
1478       offset++;
1479
1480       if (compression_methods_length > 0)
1481         {
1482           tvb_ensure_bytes_exist(tvb, offset, compression_methods_length);
1483           ti = proto_tree_add_none_format(tree,
1484                                           hf_dtls_handshake_comp_methods,
1485                                           tvb, offset, compression_methods_length,
1486                                           "Compression Methods (%u method%s)",
1487                                           compression_methods_length,
1488                                           plurality(compression_methods_length,
1489                                                     "", "s"));
1490
1491           /* make this a subtree */
1492           cs_tree = proto_item_add_subtree(ti, ett_dtls_comp_methods);
1493           if (!cs_tree)
1494             {
1495               cs_tree = tree; /* failsafe */
1496             }
1497
1498           while (compression_methods_length > 0)
1499             {
1500               compression_method = tvb_get_guint8(tvb, offset);
1501               if (compression_method < 64)
1502                 proto_tree_add_uint(cs_tree, hf_dtls_handshake_comp_method,
1503                                     tvb, offset, 1, compression_method);
1504               else if (compression_method > 63 && compression_method < 193)
1505                 proto_tree_add_text(cs_tree, tvb, offset, 1,
1506                                     "Compression Method: Reserved - to be assigned by IANA (%u)",
1507                                     compression_method);
1508               else
1509                 proto_tree_add_text(cs_tree, tvb, offset, 1,
1510                                     "Compression Method: Private use range (%u)",
1511                                     compression_method);
1512               offset++;
1513               compression_methods_length--;
1514             }
1515         }
1516
1517       if (length > offset - start_offset)
1518         {
1519           offset = dissect_dtls_hnd_hello_ext(tvb, tree, offset,
1520                                               length -
1521                                               (offset - start_offset));
1522         }
1523     }
1524 }
1525
1526
1527 static void dissect_dtls_hnd_hello_verify_request(tvbuff_t *tvb,
1528                                                   proto_tree *tree,
1529                                                   guint32 offset,
1530                                                   SslDecryptSession* ssl)
1531 {
1532   /*
1533    * struct {
1534    *    ProtocolVersion server_version;
1535    *    opaque cookie<0..32>;
1536    * } HelloVerifyRequest;
1537    */
1538
1539   guint8 cookie_length;
1540   cookie_length = 0;
1541
1542   if (tree || ssl)
1543     {
1544       /* show the client version */
1545       if (tree)
1546         proto_tree_add_item(tree, hf_dtls_handshake_server_version, tvb,
1547                             offset, 2, FALSE);
1548       offset += 2;
1549
1550
1551       /* look for a cookie */
1552       cookie_length = tvb_get_guint8(tvb, offset);
1553       if (!tree)
1554         return;
1555
1556       proto_tree_add_uint(tree, hf_dtls_handshake_cookie_len,
1557                           tvb, offset, 1, cookie_length);
1558       offset ++;            /* skip opaque length */
1559
1560       if (cookie_length > 0)
1561         {
1562           proto_tree_add_bytes_format(tree, hf_dtls_handshake_cookie,
1563                                       tvb, offset, cookie_length,
1564                                       tvb_get_ptr(tvb, offset, cookie_length),
1565                                       "Cookie (%u byte%s)",
1566                                       cookie_length,
1567                                       plurality(cookie_length, "", "s"));
1568           offset += cookie_length;
1569         }
1570     }
1571
1572 }
1573
1574 static void
1575 dissect_dtls_hnd_srv_hello(tvbuff_t *tvb,
1576                            proto_tree *tree, guint32 offset, guint32 length, SslDecryptSession* ssl)
1577 {
1578   /* struct {
1579    *     ProtocolVersion server_version;
1580    *     Random random;
1581    *     SessionID session_id;
1582    *     CipherSuite cipher_suite;
1583    *     CompressionMethod compression_method;
1584    *     Extension server_hello_extension_list<0..2^16-1>;
1585    * } ServerHello;
1586    */
1587   guint16 start_offset;
1588   start_offset = offset;
1589
1590   if (tree || ssl)
1591     {
1592       /* show the server version */
1593       if (tree)
1594         proto_tree_add_item(tree, hf_dtls_handshake_server_version, tvb,
1595                             offset, 2, FALSE);
1596       offset += 2;
1597
1598       /* first display the elements conveniently in
1599        * common with client hello
1600        */
1601       offset += dissect_dtls_hnd_hello_common(tvb, tree, offset, ssl, 1);
1602
1603       /* PAOLO: handle session cipher suite  */
1604       if (ssl) {
1605         /* store selected cipher suite for decryption */
1606         ssl->cipher = tvb_get_ntohs(tvb, offset);
1607         if (ssl_find_cipher(ssl->cipher,&ssl->cipher_suite) < 0) {
1608           ssl_debug_printf("dissect_dtls_hnd_srv_hello can't find cipher suite %X\n", ssl->cipher);
1609           goto no_cipher;
1610         }
1611
1612         ssl->state |= SSL_CIPHER;
1613         ssl_debug_printf("dissect_dtls_hnd_srv_hello found cipher %X, state %X\n",
1614                          ssl->cipher, ssl->state);
1615
1616         /* if we have restored a session now we can have enought material
1617          * to build session key, check it out*/
1618         if ((ssl->state &
1619              (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET)) !=
1620             (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET)) {
1621           ssl_debug_printf("dissect_dtls_hnd_srv_hello not enough data to generate key (required %X)\n",
1622                            (SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION|SSL_MASTER_SECRET));
1623           goto no_cipher;
1624         }
1625
1626         ssl_debug_printf("dissect_dtls_hnd_srv_hello trying to generate keys\n");
1627         if (ssl_generate_keyring_material(ssl)<0) {
1628           ssl_debug_printf("dissect_dtls_hnd_srv_hello can't generate keyring material\n");
1629           goto no_cipher;
1630         }
1631         ssl->state |= SSL_HAVE_SESSION_KEY;
1632       }
1633     no_cipher:
1634       if (!tree)
1635         return;
1636
1637       /* now the server-selected cipher suite */
1638       proto_tree_add_item(tree, hf_dtls_handshake_cipher_suite,
1639                           tvb, offset, 2, FALSE);
1640       offset += 2;
1641
1642       /* and the server-selected compression method */
1643       proto_tree_add_item(tree, hf_dtls_handshake_comp_method,
1644                           tvb, offset, 1, FALSE);
1645       offset++;
1646
1647       if (length > offset - start_offset)
1648         {
1649           offset = dissect_dtls_hnd_hello_ext(tvb, tree, offset,
1650                                               length -
1651                                               (offset - start_offset));
1652         }
1653     }
1654 }
1655
1656 static void
1657 dissect_dtls_hnd_cert(tvbuff_t *tvb,
1658                       proto_tree *tree, guint32 offset, packet_info *pinfo)
1659 {
1660
1661   /* opaque ASN.1Cert<2^24-1>;
1662    *
1663    * struct {
1664    *     ASN.1Cert certificate_list<1..2^24-1>;
1665    * } Certificate;
1666    */
1667   guint32 certificate_list_length;
1668   proto_tree *ti;
1669   proto_tree *subtree;
1670   asn1_ctx_t asn1_ctx;
1671   asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
1672
1673   if (tree)
1674     {
1675       certificate_list_length = tvb_get_ntoh24(tvb, offset);
1676       proto_tree_add_uint(tree, hf_dtls_handshake_certificates_len,
1677                           tvb, offset, 3, certificate_list_length);
1678       offset += 3;            /* 24-bit length value */
1679
1680       if (certificate_list_length > 0)
1681         {
1682           tvb_ensure_bytes_exist(tvb, offset, certificate_list_length);
1683           ti = proto_tree_add_none_format(tree,
1684                                           hf_dtls_handshake_certificates,
1685                                           tvb, offset, certificate_list_length,
1686                                           "Certificates (%u byte%s)",
1687                                           certificate_list_length,
1688                                           plurality(certificate_list_length,
1689                                                     "", "s"));
1690
1691           /* make it a subtree */
1692           subtree = proto_item_add_subtree(ti, ett_dtls_certs);
1693           if (!subtree)
1694             {
1695               subtree = tree; /* failsafe */
1696             }
1697
1698           /* iterate through each certificate */
1699           while (certificate_list_length > 0)
1700             {
1701               /* get the length of the current certificate */
1702               guint32 cert_length = tvb_get_ntoh24(tvb, offset);
1703               certificate_list_length -= 3 + cert_length;
1704
1705               proto_tree_add_item(subtree, hf_dtls_handshake_certificate_len,
1706                                   tvb, offset, 3, FALSE);
1707               offset += 3;
1708
1709               dissect_x509af_Certificate(FALSE, tvb, offset, &asn1_ctx, subtree, hf_dtls_handshake_certificate);
1710               offset += cert_length;
1711             }
1712         }
1713
1714     }
1715 }
1716
1717 static void
1718 dissect_dtls_hnd_cert_req(tvbuff_t *tvb,
1719                           proto_tree *tree, guint32 offset)
1720 {
1721   /*
1722    *    enum {
1723    *        rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
1724    *        (255)
1725    *    } ClientCertificateType;
1726    *
1727    *    opaque DistinguishedName<1..2^16-1>;
1728    *
1729    *    struct {
1730    *        ClientCertificateType certificate_types<1..2^8-1>;
1731    *        DistinguishedName certificate_authorities<3..2^16-1>;
1732    *    } CertificateRequest;
1733    *
1734    */
1735   proto_tree *ti;
1736   proto_tree *subtree;
1737   guint8      cert_types_count;
1738   gint         dnames_length;
1739   cert_types_count = 0;
1740   dnames_length = 0;
1741
1742   if (tree)
1743     {
1744       cert_types_count = tvb_get_guint8(tvb, offset);
1745       proto_tree_add_uint(tree, hf_dtls_handshake_cert_types_count,
1746                           tvb, offset, 1, cert_types_count);
1747       offset++;
1748
1749       if (cert_types_count > 0)
1750         {
1751           ti = proto_tree_add_none_format(tree,
1752                                           hf_dtls_handshake_cert_types,
1753                                           tvb, offset, cert_types_count,
1754                                           "Certificate types (%u type%s)",
1755                                           cert_types_count,
1756                                           plurality(cert_types_count, "", "s"));
1757           subtree = proto_item_add_subtree(ti, ett_dtls_cert_types);
1758           if (!subtree)
1759             {
1760               subtree = tree;
1761             }
1762
1763           while (cert_types_count > 0)
1764             {
1765               proto_tree_add_item(subtree, hf_dtls_handshake_cert_type,
1766                                   tvb, offset, 1, FALSE);
1767               offset++;
1768               cert_types_count--;
1769             }
1770         }
1771
1772       dnames_length = tvb_get_ntohs(tvb, offset);
1773       proto_tree_add_uint(tree, hf_dtls_handshake_dnames_len,
1774                           tvb, offset, 2, dnames_length);
1775       offset += 2;
1776
1777       if (dnames_length > 0)
1778         {
1779           tvb_ensure_bytes_exist(tvb, offset, dnames_length);
1780           ti = proto_tree_add_none_format(tree,
1781                                           hf_dtls_handshake_dnames,
1782                                           tvb, offset, dnames_length,
1783                                           "Distinguished Names (%d byte%s)",
1784                                           dnames_length,
1785                                           plurality(dnames_length, "", "s"));
1786           subtree = proto_item_add_subtree(ti, ett_dtls_dnames);
1787           if (!subtree)
1788             {
1789               subtree = tree;
1790             }
1791
1792           while (dnames_length > 0)
1793             {
1794               /* get the length of the current certificate */
1795               guint16 name_length = tvb_get_ntohs(tvb, offset);
1796               dnames_length -= 2 + name_length;
1797
1798               proto_tree_add_item(subtree, hf_dtls_handshake_dname_len,
1799                                   tvb, offset, 2, FALSE);
1800               offset += 2;
1801
1802               proto_tree_add_bytes_format(subtree,
1803                                           hf_dtls_handshake_dname,
1804                                           tvb, offset, name_length,
1805                                           tvb_get_ptr(tvb, offset, name_length),
1806                                           "Distinguished Name (%u byte%s)",
1807                                           name_length,
1808                                           plurality(name_length, "", "s"));
1809               offset += name_length;
1810             }
1811         }
1812     }
1813
1814 }
1815
1816 static void
1817 dissect_dtls_hnd_finished(tvbuff_t *tvb,
1818                           proto_tree *tree, guint32 offset,
1819                           guint* conv_version)
1820 {
1821   /*
1822    *     struct {
1823    *         opaque verify_data[12];
1824    *     } Finished;
1825    */
1826
1827   /* this all needs a tree, so bail if we don't have one */
1828   if (!tree)
1829     {
1830       return;
1831     }
1832
1833   switch(*conv_version) {
1834   case SSL_VER_DTLS:
1835     proto_tree_add_item(tree, hf_dtls_handshake_finished,
1836                         tvb, offset, 12, FALSE);
1837     break;
1838   }
1839 }
1840
1841 /*********************************************************************
1842  *
1843  * Support Functions
1844  *
1845  *********************************************************************/
1846 #if 0
1847 static void
1848 ssl_set_conv_version(packet_info *pinfo, guint version)
1849 {
1850   conversation_t *conversation;
1851
1852   if (pinfo->fd->flags.visited)
1853     {
1854       /* We've already processed this frame; no need to do any more
1855        * work on it.
1856        */
1857       return;
1858     }
1859
1860   conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
1861                                    pinfo->srcport, pinfo->destport, 0);
1862
1863   if (conversation == NULL)
1864     {
1865       /* create a new conversation */
1866       conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
1867                                       pinfo->srcport, pinfo->destport, 0);
1868     }
1869
1870   if (conversation_get_proto_data(conversation, proto_dtls) != NULL)
1871     {
1872       /* get rid of the current data */
1873       conversation_delete_proto_data(conversation, proto_dtls);
1874     }
1875   conversation_add_proto_data(conversation, proto_dtls, GINT_TO_POINTER(version));
1876 }
1877 #endif
1878
1879 static gint
1880 dtls_is_valid_handshake_type(guint8 type)
1881 {
1882
1883   switch (type) {
1884   case SSL_HND_HELLO_REQUEST:
1885   case SSL_HND_CLIENT_HELLO:
1886   case SSL_HND_SERVER_HELLO:
1887   case SSL_HND_HELLO_VERIFY_REQUEST:
1888   case SSL_HND_CERTIFICATE:
1889   case SSL_HND_SERVER_KEY_EXCHG:
1890   case SSL_HND_CERT_REQUEST:
1891   case SSL_HND_SVR_HELLO_DONE:
1892   case SSL_HND_CERT_VERIFY:
1893   case SSL_HND_CLIENT_KEY_EXCHG:
1894   case SSL_HND_FINISHED:
1895     return 1;
1896   }
1897   return 0;
1898 }
1899
1900 static gint
1901 dtls_is_authoritative_version_message(guint8 content_type,
1902                                       guint8 next_byte)
1903 {
1904   if (content_type == SSL_ID_HANDSHAKE
1905       && dtls_is_valid_handshake_type(next_byte))
1906     {
1907       return (next_byte != SSL_HND_CLIENT_HELLO);
1908     }
1909   else if (ssl_is_valid_content_type(content_type)
1910            && content_type != SSL_ID_HANDSHAKE)
1911     {
1912       return 1;
1913     }
1914   return 0;
1915 }
1916
1917 /* this applies a heuristic to determine whether
1918  * or not the data beginning at offset looks like a
1919  * valid dtls record.
1920  */
1921 static gint
1922 looks_like_dtls(tvbuff_t *tvb, guint32 offset)
1923 {
1924   /* have to have a valid content type followed by a valid
1925    * protocol version
1926    */
1927   guint8 byte;
1928   guint16 version;
1929
1930   /* see if the first byte is a valid content type */
1931   byte = tvb_get_guint8(tvb, offset);
1932   if (!ssl_is_valid_content_type(byte))
1933     {
1934       return 0;
1935     }
1936
1937   /* now check to see if the version byte appears valid */
1938   version = tvb_get_ntohs(tvb, offset + 1);
1939   if (version != DTLSV1DOT0_VERSION && version != DTLSV1DOT0_VERSION_NOT)
1940     {
1941       return 0;
1942     }
1943
1944   return 1;
1945 }
1946
1947 /*********************************************************************
1948  *
1949  * Standard Wireshark Protocol Registration and housekeeping
1950  *
1951  *********************************************************************/
1952 void
1953 proto_register_dtls(void)
1954 {
1955
1956   /* Setup list of header fields See Section 1.6.1 for details*/
1957   static hf_register_info hf[] = {
1958     { &hf_dtls_record,
1959       { "Record Layer", "dtls.record",
1960         FT_NONE, BASE_NONE, NULL, 0x0,
1961         "Record layer", HFILL }
1962     },
1963     { &hf_dtls_record_content_type,
1964       { "Content Type", "dtls.record.content_type",
1965         FT_UINT8, BASE_DEC, VALS(ssl_31_content_type), 0x0,
1966         "Content type", HFILL}
1967     },
1968     { &hf_dtls_record_version,
1969       { "Version", "dtls.record.version",
1970         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
1971         "Record layer version.", HFILL }
1972     },
1973     { &hf_dtls_record_epoch,
1974       { "Epoch", "dtls.record.epoch",
1975         FT_UINT16, BASE_DEC, NULL, 0x0,
1976         NULL, HFILL }
1977     },
1978     { &hf_dtls_record_sequence_number,
1979       { "Sequence Number", "dtls.record.sequence_number",
1980         FT_DOUBLE, BASE_NONE, NULL, 0x0,
1981         NULL, HFILL }
1982     },
1983     { &hf_dtls_record_length,
1984       { "Length", "dtls.record.length",
1985         FT_UINT16, BASE_DEC, NULL, 0x0,
1986         "Length of DTLS record data", HFILL }
1987     },
1988     { &hf_dtls_record_appdata,
1989       { "Encrypted Application Data", "dtls.app_data",
1990         FT_BYTES, BASE_NONE, NULL, 0x0,
1991         "Payload is encrypted application data", HFILL }
1992     },
1993     { &hf_dtls_change_cipher_spec,
1994       { "Change Cipher Spec Message", "dtls.change_cipher_spec",
1995         FT_NONE, BASE_NONE, NULL, 0x0,
1996         "Signals a change in cipher specifications", HFILL }
1997     },
1998     { & hf_dtls_alert_message,
1999       { "Alert Message", "dtls.alert_message",
2000         FT_NONE, BASE_NONE, NULL, 0x0,
2001         "Alert message", HFILL }
2002     },
2003     { & hf_dtls_alert_message_level,
2004       { "Level", "dtls.alert_message.level",
2005         FT_UINT8, BASE_DEC, VALS(ssl_31_alert_level), 0x0,
2006         "Alert message level", HFILL }
2007     },
2008     { &hf_dtls_alert_message_description,
2009       { "Description", "dtls.alert_message.desc",
2010         FT_UINT8, BASE_DEC, VALS(ssl_31_alert_description), 0x0,
2011         "Alert message description", HFILL }
2012     },
2013     { &hf_dtls_handshake_protocol,
2014       { "Handshake Protocol", "dtls.handshake",
2015         FT_NONE, BASE_NONE, NULL, 0x0,
2016         "Handshake protocol message", HFILL}
2017     },
2018     { &hf_dtls_handshake_type,
2019       { "Handshake Type", "dtls.handshake.type",
2020         FT_UINT8, BASE_DEC, VALS(ssl_31_handshake_type), 0x0,
2021         "Type of handshake message", HFILL}
2022     },
2023     { &hf_dtls_handshake_length,
2024       { "Length", "dtls.handshake.length",
2025         FT_UINT24, BASE_DEC, NULL, 0x0,
2026         "Length of handshake message", HFILL }
2027     },
2028     { &hf_dtls_handshake_message_seq,
2029       { "Message Sequence", "dtls.handshake.message_seq",
2030         FT_UINT16, BASE_DEC, NULL, 0x0,
2031         "Message sequence of handshake message", HFILL }
2032     },
2033     { &hf_dtls_handshake_fragment_offset,
2034       { "Fragment Offset", "dtls.handshake.fragment_offset",
2035         FT_UINT24, BASE_DEC, NULL, 0x0,
2036         "Fragment offset of handshake message", HFILL }
2037     },
2038     { &hf_dtls_handshake_fragment_length,
2039       { "Fragment Length", "dtls.handshake.fragment_length",
2040         FT_UINT24, BASE_DEC, NULL, 0x0,
2041         "Fragment length of handshake message", HFILL }
2042     },
2043     { &hf_dtls_handshake_client_version,
2044       { "Version", "dtls.handshake.version",
2045         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
2046         "Maximum version supported by client", HFILL }
2047     },
2048     { &hf_dtls_handshake_server_version,
2049       { "Version", "dtls.handshake.version",
2050         FT_UINT16, BASE_HEX, VALS(ssl_versions), 0x0,
2051         "Version selected by server", HFILL }
2052     },
2053     { &hf_dtls_handshake_random_time,
2054       { "Random.gmt_unix_time", "dtls.handshake.random_time",
2055         FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
2056         "Unix time field of random structure", HFILL }
2057     },
2058     { &hf_dtls_handshake_random_bytes,
2059       { "Random.bytes", "dtls.handshake.random",
2060         FT_NONE, BASE_NONE, NULL, 0x0,
2061         "Random challenge used to authenticate server", HFILL }
2062     },
2063     { &hf_dtls_handshake_cipher_suites_len,
2064       { "Cipher Suites Length", "dtls.handshake.cipher_suites_length",
2065         FT_UINT16, BASE_DEC, NULL, 0x0,
2066         "Length of cipher suites field", HFILL }
2067     },
2068     { &hf_dtls_handshake_cipher_suites,
2069       { "Cipher Suites", "dtls.handshake.ciphersuites",
2070         FT_NONE, BASE_NONE, NULL, 0x0,
2071         "List of cipher suites supported by client", HFILL }
2072     },
2073     { &hf_dtls_handshake_cipher_suite,
2074       { "Cipher Suite", "dtls.handshake.ciphersuite",
2075         FT_UINT16, BASE_HEX, VALS(ssl_31_ciphersuite), 0x0,
2076         "Cipher suite", HFILL }
2077     },
2078     { &hf_dtls_handshake_cookie_len,
2079       { "Cookie Length", "dtls.handshake.cookie_length",
2080         FT_UINT8, BASE_DEC, NULL, 0x0,
2081         "Length of the cookie field", HFILL }
2082     },
2083     { &hf_dtls_handshake_cookie,
2084       { "Cookie", "dtls.handshake.cookie",
2085         FT_BYTES, BASE_NONE, NULL, 0x0,
2086         NULL, HFILL }
2087     },
2088     { &hf_dtls_handshake_session_id,
2089       { "Session ID", "dtls.handshake.session_id",
2090         FT_BYTES, BASE_NONE, NULL, 0x0,
2091         "Identifies the DTLS session, allowing later resumption", HFILL }
2092     },
2093     { &hf_dtls_handshake_comp_methods_len,
2094       { "Compression Methods Length", "dtls.handshake.comp_methods_length",
2095         FT_UINT8, BASE_DEC, NULL, 0x0,
2096         "Length of compression methods field", HFILL }
2097     },
2098     { &hf_dtls_handshake_comp_methods,
2099       { "Compression Methods", "dtls.handshake.comp_methods",
2100         FT_NONE, BASE_NONE, NULL, 0x0,
2101         "List of compression methods supported by client", HFILL }
2102     },
2103     { &hf_dtls_handshake_comp_method,
2104       { "Compression Method", "dtls.handshake.comp_method",
2105         FT_UINT8, BASE_DEC, VALS(ssl_31_compression_method), 0x0,
2106         NULL, HFILL }
2107     },
2108     { &hf_dtls_handshake_extensions_len,
2109       { "Extensions Length", "dtls.handshake.extensions_length",
2110         FT_UINT16, BASE_DEC, NULL, 0x0,
2111         "Length of hello extensions", HFILL }
2112     },
2113     { &hf_dtls_handshake_extension_type,
2114       { "Type", "dtls.handshake.extension.type",
2115         FT_UINT16, BASE_HEX, VALS(tls_hello_extension_types), 0x0,
2116         "Hello extension type", HFILL }
2117     },
2118     { &hf_dtls_handshake_extension_len,
2119       { "Length", "dtls.handshake.extension.len",
2120         FT_UINT16, BASE_DEC, NULL, 0x0,
2121         "Length of a hello extension", HFILL }
2122     },
2123     { &hf_dtls_handshake_extension_data,
2124       { "Data", "dtls.handshake.extension.data",
2125         FT_BYTES, BASE_NONE, NULL, 0x0,
2126         "Hello Extension data", HFILL }
2127     },
2128     { &hf_dtls_handshake_certificates_len,
2129       { "Certificates Length", "dtls.handshake.certificates_length",
2130         FT_UINT24, BASE_DEC, NULL, 0x0,
2131         "Length of certificates field", HFILL }
2132     },
2133     { &hf_dtls_handshake_certificates,
2134       { "Certificates", "dtls.handshake.certificates",
2135         FT_NONE, BASE_NONE, NULL, 0x0,
2136         "List of certificates", HFILL }
2137     },
2138     { &hf_dtls_handshake_certificate,
2139       { "Certificate", "dtls.handshake.certificate",
2140         FT_BYTES, BASE_NONE, NULL, 0x0,
2141         NULL, HFILL }
2142     },
2143     { &hf_dtls_handshake_certificate_len,
2144       { "Certificate Length", "dtls.handshake.certificate_length",
2145         FT_UINT24, BASE_DEC, NULL, 0x0,
2146         "Length of certificate", HFILL }
2147     },
2148     { &hf_dtls_handshake_cert_types_count,
2149       { "Certificate types count", "dtls.handshake.cert_types_count",
2150         FT_UINT8, BASE_DEC, NULL, 0x0,
2151         "Count of certificate types", HFILL }
2152     },
2153     { &hf_dtls_handshake_cert_types,
2154       { "Certificate types", "dtls.handshake.cert_types",
2155         FT_NONE, BASE_NONE, NULL, 0x0,
2156         "List of certificate types", HFILL }
2157     },
2158     { &hf_dtls_handshake_cert_type,
2159       { "Certificate type", "dtls.handshake.cert_type",
2160         FT_UINT8, BASE_DEC, VALS(ssl_31_client_certificate_type), 0x0,
2161         NULL, HFILL }
2162     },
2163     { &hf_dtls_handshake_finished,
2164       { "Verify Data", "dtls.handshake.verify_data",
2165         FT_NONE, BASE_NONE, NULL, 0x0,
2166         "Opaque verification data", HFILL }
2167     },
2168     { &hf_dtls_handshake_md5_hash,
2169       { "MD5 Hash", "dtls.handshake.md5_hash",
2170         FT_NONE, BASE_NONE, NULL, 0x0,
2171         "Hash of messages, master_secret, etc.", HFILL }
2172     },
2173     { &hf_dtls_handshake_sha_hash,
2174       { "SHA-1 Hash", "dtls.handshake.sha_hash",
2175         FT_NONE, BASE_NONE, NULL, 0x0,
2176         "Hash of messages, master_secret, etc.", HFILL }
2177     },
2178     { &hf_dtls_handshake_session_id_len,
2179       { "Session ID Length", "dtls.handshake.session_id_length",
2180         FT_UINT8, BASE_DEC, NULL, 0x0,
2181         "Length of session ID field", HFILL }
2182     },
2183     { &hf_dtls_handshake_dnames_len,
2184       { "Distinguished Names Length", "dtls.handshake.dnames_len",
2185         FT_UINT16, BASE_DEC, NULL, 0x0,
2186         "Length of list of CAs that server trusts", HFILL }
2187     },
2188     { &hf_dtls_handshake_dnames,
2189       { "Distinguished Names", "dtls.handshake.dnames",
2190         FT_NONE, BASE_NONE, NULL, 0x0,
2191         "List of CAs that server trusts", HFILL }
2192     },
2193     { &hf_dtls_handshake_dname_len,
2194       { "Distinguished Name Length", "dtls.handshake.dname_len",
2195         FT_UINT16, BASE_DEC, NULL, 0x0,
2196         "Length of distinguished name", HFILL }
2197     },
2198     { &hf_dtls_handshake_dname,
2199       { "Distinguished Name", "dtls.handshake.dname",
2200         FT_BYTES, BASE_NONE, NULL, 0x0,
2201         "Distinguished name of a CA that server trusts", HFILL }
2202     },
2203     { &hf_dtls_fragments,
2204       { "Message fragments", "dtls.fragments",
2205         FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
2206     },
2207     { &hf_dtls_fragment,
2208       { "Message fragment", "dtls.fragment",
2209         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
2210     },
2211     { &hf_dtls_fragment_overlap,
2212       { "Message fragment overlap", "dtls.fragment.overlap",
2213         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
2214     },
2215     { &hf_dtls_fragment_overlap_conflicts,
2216       { "Message fragment overlapping with conflicting data",
2217         "dtls.fragment.overlap.conflicts",
2218        FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
2219     },
2220     { &hf_dtls_fragment_multiple_tails,
2221       { "Message has multiple tail fragments",
2222         "dtls.fragment.multiple_tails", 
2223         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
2224     },
2225     { &hf_dtls_fragment_too_long_fragment,
2226       { "Message fragment too long", "dtls.fragment.too_long_fragment",
2227         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
2228     },
2229     { &hf_dtls_fragment_error,
2230       { "Message defragmentation error", "dtls.fragment.error",
2231         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
2232     },
2233     { &hf_dtls_reassembled_in,
2234       { "Reassembled in", "dtls.reassembled.in",
2235         FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
2236     },
2237   };
2238
2239   /* Setup protocol subtree array */
2240   static gint *ett[] = {
2241     &ett_dtls,
2242     &ett_dtls_record,
2243     &ett_dtls_alert,
2244     &ett_dtls_handshake,
2245     &ett_dtls_cipher_suites,
2246     &ett_dtls_comp_methods,
2247     &ett_dtls_extension,
2248     &ett_dtls_certs,
2249     &ett_dtls_cert_types,
2250     &ett_dtls_dnames,
2251     &ett_dtls_fragment,
2252     &ett_dtls_fragments,
2253   };
2254
2255   /* Register the protocol name and description */
2256   proto_dtls = proto_register_protocol("Datagram Transport Layer Security",
2257                                        "DTLS", "dtls");
2258
2259   /* Required function calls to register the header fields and
2260    * subtrees used */
2261   proto_register_field_array(proto_dtls, hf, array_length(hf));
2262   proto_register_subtree_array(ett, array_length(ett));
2263
2264 #ifdef HAVE_LIBGNUTLS
2265   {
2266     module_t *dtls_module = prefs_register_protocol(proto_dtls, dtls_parse);
2267     prefs_register_string_preference(dtls_module, "keys_list", "RSA keys list",
2268                                      "semicolon separated list of private RSA keys used for DTLS decryption; "
2269                                      "each list entry must be in the form of <ip>,<port>,<protocol>,<key_file_name>"
2270                                      "<key_file_name>   is the local file name of the RSA private key used by the specified server\n",
2271                                      (const gchar **)&dtls_keys_list);
2272     prefs_register_string_preference(dtls_module, "debug_file", "DTLS debug file",
2273                                      "redirect dtls debug to file name; leave empty to disable debug, "
2274                                      "use \"" SSL_DEBUG_USE_STDERR "\" to redirect output to stderr\n",
2275                                      (const gchar **)&dtls_debug_file_name);
2276   }
2277 #endif
2278
2279   register_dissector("dtls", dissect_dtls, proto_dtls);
2280   dtls_handle = find_dissector("dtls");
2281
2282   dtls_associations = g_tree_new(ssl_association_cmp);
2283
2284   register_init_routine(dtls_init);
2285   ssl_lib_init();
2286   dtls_tap = register_tap("dtls");
2287   ssl_debug_printf("proto_register_dtls: registered tap %s:%d\n",
2288                    "dtls", dtls_tap);
2289 }
2290
2291 /* If this dissector uses sub-dissector registration add a registration
2292  * routine.  This format is required because a script is used to find
2293  * these routines and create the code that calls these routines.
2294  */
2295 void
2296 proto_reg_handoff_dtls(void)
2297 {
2298
2299   /* add now dissector to default ports.*/
2300   dtls_parse();
2301 }