Updates for the endpoint talkers thing
[obnox/wireshark/wip.git] / packet-eapol.c
1 /* packet-eapol.c
2  * Routines for EAPOL 802.1X authentication header disassembly
3  * (From IEEE Draft P802.1X/D11; is there a later draft, or a
4  * final standard?  If so, check it.)
5  *
6  * $Id: packet-eapol.c,v 1.13 2003/06/03 01:20:14 gerald Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include "packet-ieee8023.h"
34 #include "packet-ipx.h"
35 #include "packet-llc.h"
36 #include "etypes.h"
37
38 static int proto_eapol = -1;
39 static int hf_eapol_version = -1;
40 static int hf_eapol_type = -1;
41 static int hf_eapol_len = -1;
42 static int hf_eapol_keydes_type = -1;
43 static int hf_eapol_keydes_keylen = -1;
44 static int hf_eapol_keydes_replay_counter = -1;
45 static int hf_eapol_keydes_key_iv = -1;
46 static int hf_eapol_keydes_key_index_keytype = -1;
47 static int hf_eapol_keydes_key_index_indexnum = -1;
48 static int hf_eapol_keydes_key_signature = -1;
49 static int hf_eapol_keydes_key = -1;
50
51 static int hf_eapol_wpa_keydes_keyinfo = -1;
52 static int hf_eapol_wpa_keydes_nonce = -1;
53 static int hf_eapol_wpa_keydes_rsc = -1;
54 static int hf_eapol_wpa_keydes_id = -1;
55 static int hf_eapol_wpa_keydes_mic = -1;
56 static int hf_eapol_wpa_keydes_datalen = -1;
57 static int hf_eapol_wpa_keydes_data = -1;
58
59 static int tag_number = -1;
60 static int tag_length = -1;
61 static int tag_interpretation = -1;
62
63 static gint ett_eapol = -1;
64 static gint ett_eapol_key_index = -1;
65
66 static dissector_handle_t eap_handle;
67 static dissector_handle_t data_handle;
68
69 #define EAPOL_HDR_LEN   4
70
71 #define EAP_PACKET              0
72 #define EAPOL_START             1
73 #define EAPOL_LOGOFF            2
74 #define EAPOL_KEY               3
75 #define EAPOL_ENCAP_ASF_ALERT   4
76
77 #define EAPOL_WPA_KEY           254
78
79 static const value_string eapol_type_vals[] = {
80     { EAP_PACKET,            "EAP Packet" },
81     { EAPOL_START,           "Start" },
82     { EAPOL_LOGOFF,          "Logoff" },
83     { EAPOL_KEY,             "Key" },
84     { EAPOL_ENCAP_ASF_ALERT, "Encapsulated ASF Alert" },
85     { 0,                     NULL }
86 };
87
88 static const value_string eapol_keydes_type_vals[] = {
89         { 1, "RC4 Descriptor" },
90         { EAPOL_WPA_KEY, "EAPOL WPA key" },
91         { 0, NULL }
92 };
93
94 static const true_false_string keytype_tfs =
95         { "Unicast", "Broadcast" };
96
97 extern void dissect_vendor_specific_ie(proto_tree * tree, tvbuff_t * tvb,
98         int offset, int tag_number, int tag_length, int tag_interpretation);
99
100 static void
101 dissect_eapol(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
102 {
103   int         offset = 0;
104   guint8      eapol_type;
105   guint8      keydesc_type;
106   guint16     eapol_len;
107   guint       len;
108   guint16     eapol_key_len, eapol_data_len;
109   guint8      key_index;
110   proto_tree *ti = NULL;
111   proto_tree *eapol_tree = NULL;
112   proto_tree *key_index_tree;
113   tvbuff_t   *next_tvb;
114
115   if (check_col(pinfo->cinfo, COL_PROTOCOL))
116     col_set_str(pinfo->cinfo, COL_PROTOCOL, "EAPOL");
117   if (check_col(pinfo->cinfo, COL_INFO))
118     col_clear(pinfo->cinfo, COL_INFO);
119
120   if (tree) {
121     ti = proto_tree_add_item(tree, proto_eapol, tvb, 0, -1, FALSE);
122     eapol_tree = proto_item_add_subtree(ti, ett_eapol);
123
124     proto_tree_add_item(eapol_tree, hf_eapol_version, tvb, offset, 1, FALSE);
125   }
126   offset++;
127
128   eapol_type = tvb_get_guint8(tvb, offset);
129   if (tree)
130     proto_tree_add_uint(eapol_tree, hf_eapol_type, tvb, offset, 1, eapol_type);
131   if (check_col(pinfo->cinfo, COL_INFO))
132     col_add_str(pinfo->cinfo, COL_INFO,
133                 val_to_str(eapol_type, eapol_type_vals, "Unknown type (0x%02X)"));
134   offset++;
135
136   eapol_len = tvb_get_ntohs(tvb, offset);
137   len = EAPOL_HDR_LEN + eapol_len;
138   set_actual_length(tvb, len);
139   if (tree) {
140     proto_item_set_len(ti, len);
141     proto_tree_add_uint(eapol_tree, hf_eapol_len, tvb, offset, 2, eapol_len);
142   }
143   offset += 2;
144
145   switch (eapol_type) {
146
147   case EAP_PACKET:
148     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
149     call_dissector(eap_handle, next_tvb, pinfo, eapol_tree);
150     break;
151
152   case EAPOL_KEY:
153     if (tree) {
154       keydesc_type = tvb_get_guint8(tvb, offset);
155       proto_tree_add_item(eapol_tree, hf_eapol_keydes_type, tvb, offset, 1, FALSE);
156       offset += 1;
157       if (keydesc_type == EAPOL_WPA_KEY) {
158         proto_tree_add_uint(eapol_tree, hf_eapol_wpa_keydes_keyinfo, tvb,
159                             offset, 2, tvb_get_ntohs(tvb, offset));
160         offset += 2;
161         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset,
162                             2, tvb_get_ntohs(tvb, offset));
163         offset += 2;
164         proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
165                             offset, 8, FALSE);
166         offset += 8;
167         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_nonce, tvb, offset,
168                             32, FALSE);
169         offset += 32;
170         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
171                             offset, 16, FALSE);
172         offset += 16;
173         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_rsc, tvb, offset,
174                             8, FALSE);
175         offset += 8;
176         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_id, tvb, offset, 8,
177                             FALSE);
178         offset += 8;
179         proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_mic, tvb, offset,
180                             16, FALSE);
181         offset += 16;
182         eapol_data_len = tvb_get_ntohs(tvb, offset);
183         proto_tree_add_uint(eapol_tree, hf_eapol_wpa_keydes_datalen, tvb,
184                             offset, 2, eapol_data_len);
185         offset += 2;
186         if (eapol_data_len != 0) {
187           proto_tree_add_item(eapol_tree, hf_eapol_wpa_keydes_data,
188                 tvb, offset, eapol_data_len, FALSE);
189           dissect_vendor_specific_ie(eapol_tree, tvb, offset,
190                 tag_number, tag_length, tag_interpretation);
191           offset += eapol_data_len;
192         }
193       }
194       else {
195         eapol_key_len = tvb_get_ntohs(tvb, offset);
196         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_keylen, tvb, offset, 2, eapol_key_len);
197         offset += 2;
198         proto_tree_add_item(eapol_tree, hf_eapol_keydes_replay_counter, tvb,
199                           offset, 8, FALSE);
200         offset += 8;
201         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_iv, tvb,
202                           offset, 16, FALSE);
203         offset += 16;
204         key_index = tvb_get_guint8(tvb, offset);
205         ti = proto_tree_add_text(eapol_tree, tvb, offset, 1,
206                                "Key Index: %s, index %u",
207                                (key_index & 0x80) ? "unicast" : "broadcast",
208                                key_index & 0x7F);
209         key_index_tree = proto_item_add_subtree(ti, ett_eapol_key_index);
210         proto_tree_add_boolean(eapol_tree, hf_eapol_keydes_key_index_keytype,
211                              tvb, offset, 1, key_index);
212         proto_tree_add_uint(eapol_tree, hf_eapol_keydes_key_index_indexnum,
213                              tvb, offset, 1, key_index);
214         offset += 1;
215         proto_tree_add_item(eapol_tree, hf_eapol_keydes_key_signature, tvb,
216                           offset, 16, FALSE);
217         offset += 16;
218         if (eapol_key_len != 0)
219           proto_tree_add_item(eapol_tree, hf_eapol_keydes_key, tvb, offset,
220                             eapol_key_len, FALSE);
221       }
222     }
223     break;
224
225   case EAPOL_ENCAP_ASF_ALERT:   /* XXX - is this an SNMP trap? */
226   default:
227     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
228     call_dissector(data_handle, next_tvb, pinfo, eapol_tree);
229     break;
230   }
231 }
232
233 void
234 proto_register_eapol(void)
235 {
236   static hf_register_info hf[] = {
237         { &hf_eapol_version, {
238                 "Version", "eapol.version", FT_UINT8, BASE_DEC,
239                 NULL, 0x0, "", HFILL }},
240         { &hf_eapol_type, {
241                 "Type", "eapol.type", FT_UINT8, BASE_DEC,
242                 VALS(eapol_type_vals), 0x0, "", HFILL }},
243         { &hf_eapol_len, {
244                 "Length", "eapol.len", FT_UINT16, BASE_DEC,
245                 NULL, 0x0, "Length", HFILL }},
246         { &hf_eapol_keydes_type, {
247                 "Descriptor Type", "eapol.keydes.type", FT_UINT8, BASE_DEC,
248                 VALS(eapol_keydes_type_vals), 0x0, "Key Descriptor Type", HFILL }},
249         { &hf_eapol_keydes_keylen, {
250                 "Key Length", "eapol.keydes.keylen", FT_UINT16, BASE_DEC,
251                 NULL, 0x0, "Key Length", HFILL }},
252         { &hf_eapol_keydes_replay_counter, {
253                 "Replay Counter", "eapol.keydes.replay_counter", FT_UINT64, BASE_DEC,
254                 NULL, 0x0, "Replay Counter", HFILL }},
255         { &hf_eapol_keydes_key_iv, {
256                 "Key IV", "eapol.keydes.key_iv", FT_BYTES, BASE_NONE,
257                 NULL, 0x0, "Key Initialization Vector", HFILL }},
258         { &hf_eapol_keydes_key_index_keytype, {
259                 "Key Type", "eapol.keydes.index.keytype", FT_BOOLEAN, 8,
260                 TFS(&keytype_tfs), 0x80, "Key Type (unicast/broadcast)", HFILL }},
261         { &hf_eapol_keydes_key_index_indexnum, {
262                 "Index Number", "eapol.keydes.index.indexnum", FT_UINT8, BASE_DEC,
263                 NULL, 0x7F, "Key Index number", HFILL }},
264         { &hf_eapol_keydes_key_signature, {
265                 "Key Signature", "eapol.keydes.key_signature", FT_BYTES, BASE_NONE,
266                 NULL, 0x0, "Key Signature", HFILL }},
267         { &hf_eapol_keydes_key, {
268                 "Key", "eapol.keydes.key", FT_BYTES, BASE_NONE,
269                 NULL, 0x0, "Key", HFILL }},
270
271         { &hf_eapol_wpa_keydes_keyinfo, {
272                 "Key Information", "eapol.keydes.key_info", FT_UINT16,
273                 BASE_HEX, NULL, 0x0, "WPA key info", HFILL }},
274         { &hf_eapol_wpa_keydes_nonce, {
275                 "Nonce", "eapol.keydes.nonce", FT_BYTES, BASE_NONE,
276                 NULL, 0x0, "WPA Key Nonce", HFILL }},
277         { &hf_eapol_wpa_keydes_rsc, {
278                 "WPA Key RSC", "eapol.keydes.rsc", FT_BYTES, BASE_NONE, NULL,
279                 0x0, "WPA Key Receive Sequence Counter", HFILL }},
280         { &hf_eapol_wpa_keydes_id, {
281                 "WPA Key ID", "eapol,keydes.id", FT_BYTES, BASE_NONE, NULL,
282                 0x0, "WPA Key ID", HFILL }},
283         { &hf_eapol_wpa_keydes_mic, {
284                 "WPA Key MIC", "eapol.keydes.mic", FT_BYTES, BASE_NONE, NULL,
285                 0x0, "WPA Key Message Integrity Check", HFILL }},
286         { &hf_eapol_wpa_keydes_datalen, {
287                 "WPA Key Length", "eapol.keydes.datalen", FT_UINT16, BASE_DEC,
288                 NULL, 0x0, "WPA Key Data Length", HFILL }},
289         { &hf_eapol_wpa_keydes_data, {
290                 "WPA Key", "eapol.keydes.data", FT_BYTES, BASE_NONE,
291                 NULL, 0x0, "WPA Key Data", HFILL }},
292
293         {&tag_number, {
294                 "WPA Key Interpretation", "eapol.keydes.data.wpaie",
295                 FT_UINT16, BASE_DEC, NULL, 0x0, "Element ID", HFILL }},
296         {&tag_length, {
297                 "WPA Key Interpretation", "eapol.keydes.data.wpaie",
298                 FT_UINT16, BASE_DEC, NULL, 0, "Length of tag", HFILL }},
299         {&tag_interpretation, {
300                 "WPA Key Interpretation", "eapol.keydes.data.wpaie",
301                 FT_STRING, BASE_NONE, NULL, 0, "Interpretation of tag", HFILL }}
302   };
303   static gint *ett[] = {
304         &ett_eapol,
305         &ett_eapol_key_index
306   };
307
308   proto_eapol = proto_register_protocol("802.1x Authentication", "EAPOL", "eapol");
309   proto_register_field_array(proto_eapol, hf, array_length(hf));
310   proto_register_subtree_array(ett, array_length(ett));
311 }
312
313 void
314 proto_reg_handoff_eapol(void)
315 {
316   dissector_handle_t eapol_handle;
317
318   /*
319    * Get handles for the EAP and raw data dissectors.
320    */
321   eap_handle = find_dissector("eap");
322   data_handle = find_dissector("data");
323
324   eapol_handle = create_dissector_handle(dissect_eapol, proto_eapol);
325   dissector_add("ethertype", ETHERTYPE_EAPOL, eapol_handle);
326 }