2 * Dissector for GSS-API tokens as described in rfc2078, section 3.1
3 * Copyright 2002, Tim Potter <tpot@samba.org>
4 * Copyright 2002, Richard Sharpe <rsharpe@samba.org> Added a few
7 * $Id: packet-gssapi.c,v 1.26 2002/12/02 20:04:07 guy Exp $
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@ethereal.com>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
39 #include <epan/packet.h>
42 #include "format-oid.h"
43 #include "packet-gssapi.h"
44 #include "packet-frame.h"
45 #include "epan/conversation.h"
47 static int proto_gssapi = -1;
49 static int hf_gssapi = -1;
51 static gint ett_gssapi = -1;
57 static GHashTable *gssapi_oids;
59 static gint gssapi_oid_equal(gconstpointer k1, gconstpointer k2)
61 const char *key1 = (const char *)k1;
62 const char *key2 = (const char *)k2;
64 return strcmp(key1, key2) == 0;
68 gssapi_oid_hash(gconstpointer k)
70 const char *key = (const char *)k;
73 for (i = 0; i < strlen(key); i++)
80 gssapi_init_oid(char *oid, int proto, int ett, dissector_handle_t handle,
81 dissector_handle_t wrap_handle, gchar *comment)
83 char *key = g_strdup(oid);
84 gssapi_oid_value *value = g_malloc(sizeof(*value));
88 value->handle = handle;
89 value->wrap_handle = wrap_handle;
90 value->comment = comment;
92 g_hash_table_insert(gssapi_oids, key, value);
96 * This takes an OID in binary form, not an OID as a text string, as
100 gssapi_lookup_oid(subid_t *oid, guint oid_len)
106 gssapi_oid_value *value;
109 * Convert the OID to a string, as text strings are used as
110 * keys in the OID hash table.
112 oid_key = g_malloc(oid_len * 22 + 1);
114 len = sprintf(p, "%lu", (unsigned long)oid[0]);
116 for (i = 1; i < oid_len;i++) {
117 len = sprintf(p, ".%lu", (unsigned long)oid[i]);
121 value = g_hash_table_lookup(gssapi_oids, oid_key);
126 /* Display an ASN1 parse error. Taken from packet-snmp.c */
128 static dissector_handle_t data_handle;
131 dissect_parse_error(tvbuff_t *tvb, int offset, packet_info *pinfo,
132 proto_tree *tree, const char *field_name, int ret)
136 errstr = asn1_err_to_str(ret);
139 proto_tree_add_text(tree, tvb, offset, 0,
140 "ERROR: Couldn't parse %s: %s", field_name, errstr);
141 call_dissector(data_handle,
142 tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
147 dissect_gssapi_work(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
148 gboolean is_verifier)
154 volatile int return_offset = 0;
156 guint len1, oid_len, cls, con, tag, nbytes;
159 gssapi_oid_value *value;
160 volatile dissector_handle_t handle;
161 conversation_t *volatile conversation;
166 * We need this later, so lets get it now ...
169 conversation = find_conversation(&pinfo->src, &pinfo->dst,
170 pinfo->ptype, pinfo->srcport,
173 item = proto_tree_add_item(
174 tree, hf_gssapi, tvb, offset, -1, FALSE);
176 subtree = proto_item_add_subtree(item, ett_gssapi);
179 * Catch the ReportedBoundsError exception; the stuff we've been
180 * handed doesn't necessarily run to the end of the packet, it's
181 * an item inside a packet, so if it happens to be malformed (or
182 * we, or a dissector we call, has a bug), so that an exception
183 * is thrown, we want to report the error, but return and let
184 * our caller dissect the rest of the packet.
186 * If it gets a BoundsError, we can stop, as there's nothing more
187 * in the packet after our blob to see, so we just re-throw the
193 asn1_open(&hnd, tvb, offset);
195 ret = asn1_header_decode(&hnd, &cls, &con, &tag, &def, &len1);
197 if (ret != ASN1_ERR_NOERROR) {
198 dissect_parse_error(tvb, offset, pinfo, subtree,
199 "GSS-API header", ret);
200 return_offset = tvb_length(tvb);
204 if (!(cls == ASN1_APL && con == ASN1_CON && tag == 0)) {
206 * If we do not recognise an Application class,
207 * then we are probably dealing with an inner context
208 * token or a wrap token, and we should retrieve the
209 * gssapi_oid_value pointer from the per-frame data or,
210 * if there is no per-frame data (as would be the case
211 * the first time we dissect this frame), from the
212 * conversation that exists or that we created from
213 * pinfo (and then make it per-frame data).
214 * We need to make it per-frame data as there can be
215 * more than one GSS-API negotiation in a conversation.
217 * Note! We "cheat". Since we only need the pointer,
218 * we store that as the data. (That's not really
219 * "cheating" - the per-frame data and per-conversation
220 * data code doesn't care what you supply as a data
221 * pointer; it just treats it as an opaque pointer, it
222 * doesn't dereference it or free what it points to.)
224 value = p_get_proto_data(pinfo->fd, proto_gssapi);
225 if (!value && !pinfo->fd->flags.visited)
227 /* No handle attached to this frame, but it's the first */
228 /* pass, so it'd be attached to the conversation. */
229 /* If we have a conversation, try to get the handle, */
230 /* and if we get one, attach it to the frame. */
233 value = conversation_get_proto_data(conversation,
236 p_add_proto_data(pinfo->fd, proto_gssapi, value);
241 proto_tree_add_text(subtree, tvb, offset, 0,
242 "Unknown header (cls=%d, con=%d, tag=%d)",
244 return_offset = tvb_length(tvb);
251 /* Naughty ... no way to reset the offset */
252 /* Account for the fact we have consumed part of the */
253 /* ASN.1 and we want to get it back */
256 oid_tvb = tvb_new_subset(tvb, offset, -1, -1);
258 handle = value->wrap_handle;
260 handle = value->handle;
261 len = call_dissector(handle, oid_tvb, pinfo, subtree);
263 return_offset = tvb_length(tvb);
265 return_offset = offset + len;
266 goto done; /* We are finished here */
274 ret = asn1_oid_decode(&hnd, &oid, &oid_len, &nbytes);
276 if (ret != ASN1_ERR_NOERROR) {
277 dissect_parse_error(tvb, offset, pinfo, subtree,
278 "GSS-API token", ret);
279 return_offset = tvb_length(tvb);
283 oid_string = format_oid(oid, oid_len);
286 * Hand off to subdissector.
289 if (((value = gssapi_lookup_oid(oid, oid_len)) == NULL) ||
290 !proto_is_protocol_enabled(value->proto)) {
292 proto_tree_add_text(subtree, tvb, offset, nbytes,
300 /* No dissector for this oid */
302 proto_tree_add_text(subtree, tvb, offset, -1,
305 return_offset = tvb_length(tvb);
310 proto_tree_add_text(subtree, tvb, offset, nbytes,
312 oid_string, value->comment);
314 proto_tree_add_text(subtree, tvb, offset, nbytes, "OID: %s",
322 * This is not needed, as the sub-dissector adds a tree
323 sub_item = proto_tree_add_item(subtree, value->proto, tvb,
326 oid_subtree = proto_item_add_subtree(sub_item, value->ett);
330 * Here we should create a conversation if needed and
331 * save a pointer to the data for that OID for the
335 if (!conversation) { /* Create one */
336 conversation = conversation_new(&pinfo->src,
344 * Now add the proto data ...
345 * but only if it is not already there.
348 if (!conversation_get_proto_data(conversation,
350 conversation_add_proto_data(conversation,
351 proto_gssapi, value);
355 handle = value->wrap_handle;
356 if (handle != NULL) {
357 oid_tvb = tvb_new_subset(tvb, offset, -1, -1);
358 len = call_dissector(handle, oid_tvb, pinfo,
361 return_offset = tvb_length(tvb);
363 return_offset = offset + len;
365 proto_tree_add_text(subtree, tvb, offset, -1,
366 "Authentication verifier");
367 return_offset = tvb_length(tvb);
370 handle = value->handle;
371 if (handle != NULL) {
372 oid_tvb = tvb_new_subset(tvb, offset, -1, -1);
373 len = call_dissector(handle, oid_tvb, pinfo,
376 return_offset = tvb_length(tvb);
378 return_offset = offset + len;
380 proto_tree_add_text(subtree, tvb, offset, -1,
381 "Authentication credentials");
382 return_offset = tvb_length(tvb);
387 asn1_close(&hnd, &offset);
388 } CATCH(BoundsError) {
390 } CATCH(ReportedBoundsError) {
391 show_reported_bounds_error(tvb, pinfo, tree);
394 proto_item_set_len(item, return_offset);
395 return return_offset;
399 dissect_gssapi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
401 dissect_gssapi_work(tvb, pinfo, tree, FALSE);
405 dissect_gssapi_wrap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
407 return dissect_gssapi_work(tvb, pinfo, tree, TRUE);
411 proto_register_gssapi(void)
413 static hf_register_info hf[] = {
415 { "GSS-API", "gss-api", FT_NONE, BASE_NONE, NULL, 0x0,
419 static gint *ett[] = {
423 proto_gssapi = proto_register_protocol(
424 "Generic Security Service Application Program Interface",
425 "GSS-API", "gss-api");
427 proto_register_field_array(proto_gssapi, hf, array_length(hf));
428 proto_register_subtree_array(ett, array_length(ett));
430 register_dissector("gssapi", dissect_gssapi, proto_gssapi);
431 new_register_dissector("gssapi_verf", dissect_gssapi_wrap, proto_gssapi);
433 gssapi_oids = g_hash_table_new(gssapi_oid_hash, gssapi_oid_equal);
437 proto_reg_handoff_gssapi(void)
439 data_handle = find_dissector("data");