Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-kerberos4.c
1 /* packet-kerberos4.c
2  * Routines for Kerberos v4 packet dissection
3  *
4  * Ronnie Sahlberg 2004
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 /*
25  * PDU structure based on the document :
26  *                                                              Section E.2.1
27  *
28  *                            Kerberos Authentication and Authorization System
29  *
30  *            by S. P. Miller, B. C. Neuman, J. I. Schiller, and J. H. Saltzer
31  *
32  */
33
34 #include "config.h"
35
36 #include <glib.h>
37 #include <epan/packet.h>
38
39 void proto_register_krb4(void);
40 void proto_reg_handoff_krb4(void);
41
42 static int proto_krb4 = -1;
43 static int hf_krb4_version = -1;
44 static int hf_krb4_auth_msg_type = -1;
45 static int hf_krb4_m_type = -1;
46 static int hf_krb4_byte_order = -1;
47 static int hf_krb4_name = -1;
48 static int hf_krb4_instance = -1;
49 static int hf_krb4_realm = -1;
50 static int hf_krb4_time_sec = -1;
51 static int hf_krb4_exp_date = -1;
52 static int hf_krb4_req_date = -1;
53 static int hf_krb4_lifetime = -1;
54 static int hf_krb4_s_name = -1;
55 static int hf_krb4_s_instance = -1;
56 static int hf_krb4_kvno = -1;
57 static int hf_krb4_length = -1;
58 static int hf_krb4_ticket_length = -1;
59 static int hf_krb4_request_length = -1;
60 static int hf_krb4_ticket_blob = -1;
61 static int hf_krb4_request_blob = -1;
62 static int hf_krb4_encrypted_blob = -1;
63 static int hf_krb4_unknown_transarc_blob = -1;
64
65 static gint ett_krb4 = -1;
66 static gint ett_krb4_auth_msg_type = -1;
67
68 #define UDP_PORT_KRB4    750
69 #define TRANSARC_SPECIAL_VERSION 0x63
70
71 static const value_string byte_order_vals[] = {
72         { 0,    "Big Endian" },
73         { 1,    "Little Endian" },
74         { 0,    NULL }
75 };
76
77 #define AUTH_MSG_KDC_REQUEST            1
78 #define AUTH_MSG_KDC_REPLY              2
79 #define AUTH_MSG_APPL_REQUEST           3
80 #define AUTH_MSG_APPL_REQUEST_MUTUAL    4
81 #define AUTH_MSG_ERR_REPLY              5
82 #define AUTH_MSG_PRIVATE                6
83 #define AUTH_MSG_SAFE                   7
84 #define AUTH_MSG_APPL_ERR               8
85 #define AUTH_MSG_DIE                    63
86 static const value_string m_type_vals[] = {
87         { AUTH_MSG_KDC_REQUEST,         "KDC Request" },
88         { AUTH_MSG_KDC_REPLY,           "KDC Reply" },
89         { AUTH_MSG_APPL_REQUEST,        "Appl Request" },
90         { AUTH_MSG_APPL_REQUEST_MUTUAL, "Appl Request Mutual" },
91         { AUTH_MSG_ERR_REPLY,           "Err Reply" },
92         { AUTH_MSG_PRIVATE,             "Private" },
93         { AUTH_MSG_SAFE,                "Safe" },
94         { AUTH_MSG_APPL_ERR,            "Appl Err" },
95         { AUTH_MSG_DIE,                 "Die" },
96         { 0,    NULL }
97 };
98
99
100 static int
101 dissect_krb4_string(packet_info *pinfo _U_, int hf_index, proto_tree *tree, tvbuff_t *tvb, int offset)
102 {
103         proto_tree_add_item(tree, hf_index, tvb, offset, -1, ENC_ASCII|ENC_NA);
104         while(tvb_get_guint8(tvb, offset)!=0){
105                 offset++;
106         }
107         offset++;
108
109         return offset;
110 }
111
112 static int
113 dissect_krb4_kdc_request(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean little_endian, int version)
114 {
115         nstime_t time_sec;
116         guint8 lifetime;
117
118         if(version==TRANSARC_SPECIAL_VERSION){
119                 proto_tree_add_item(tree, hf_krb4_unknown_transarc_blob, tvb, offset, 8, ENC_NA);
120                 offset+=8;
121         }
122
123         /* Name */
124         offset=dissect_krb4_string(pinfo, hf_krb4_name, tree, tvb, offset);
125
126         /* Instance */
127         offset=dissect_krb4_string(pinfo, hf_krb4_instance, tree, tvb, offset);
128
129         /* Realm */
130         offset=dissect_krb4_string(pinfo, hf_krb4_realm, tree, tvb, offset);
131
132         /* Time sec */
133         time_sec.secs=little_endian?tvb_get_letohl(tvb, offset):tvb_get_ntohl(tvb, offset);
134         time_sec.nsecs=0;
135         proto_tree_add_time(tree, hf_krb4_time_sec, tvb, offset, 4, &time_sec);
136         offset+=4;
137
138         /* lifetime */
139         lifetime=tvb_get_guint8(tvb, offset);
140         proto_tree_add_uint_format_value(tree, hf_krb4_lifetime, tvb, offset, 1, lifetime, "%d (%d minutes)", lifetime, lifetime*5);
141         offset++;
142
143         /* service Name */
144         offset=dissect_krb4_string(pinfo, hf_krb4_s_name, tree, tvb, offset);
145
146         /* service Instance */
147         offset=dissect_krb4_string(pinfo, hf_krb4_s_instance, tree, tvb, offset);
148
149         return offset;
150 }
151
152
153 static int
154 dissect_krb4_kdc_reply(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean little_endian)
155 {
156         nstime_t time_sec;
157         guint32 length;
158
159         /* Name */
160         offset=dissect_krb4_string(pinfo, hf_krb4_name, tree, tvb, offset);
161
162         /* Instance */
163         offset=dissect_krb4_string(pinfo, hf_krb4_instance, tree, tvb, offset);
164
165         /* Realm */
166         offset=dissect_krb4_string(pinfo, hf_krb4_realm, tree, tvb, offset);
167
168         /* Time sec */
169         time_sec.secs=little_endian?tvb_get_letohl(tvb, offset):tvb_get_ntohl(tvb, offset);
170         time_sec.nsecs=0;
171         proto_tree_add_time(tree, hf_krb4_time_sec, tvb, offset, 4, &time_sec);
172         offset+=4;
173
174         /*XXX unknown byte here */
175         offset++;
176
177         /* exp date */
178         time_sec.secs=little_endian?tvb_get_letohl(tvb, offset):tvb_get_ntohl(tvb, offset);
179         time_sec.nsecs=0;
180         proto_tree_add_time(tree, hf_krb4_exp_date, tvb, offset, 4, &time_sec);
181         offset+=4;
182
183         /* kvno */
184         proto_tree_add_item(tree, hf_krb4_kvno, tvb, offset, 1, ENC_BIG_ENDIAN);
185         offset++;
186
187         /* length2 */
188         length=little_endian?tvb_get_letohs(tvb, offset):tvb_get_ntohs(tvb, offset);
189         proto_tree_add_uint_format_value(tree, hf_krb4_length, tvb, offset, 2, length, "%d", length);
190         offset+=2;
191
192         /* encrypted blob */
193         proto_tree_add_item(tree, hf_krb4_encrypted_blob, tvb, offset, length, ENC_NA);
194         offset+=length;
195
196         return offset;
197 }
198
199
200 static int
201 dissect_krb4_appl_request(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean little_endian)
202 {
203         guint8 tlen, rlen;
204         nstime_t time_sec;
205         guint8 lifetime;
206
207         /* kvno */
208         proto_tree_add_item(tree, hf_krb4_kvno, tvb, offset, 1, ENC_BIG_ENDIAN);
209         offset++;
210
211         /* Realm */
212         offset=dissect_krb4_string(pinfo, hf_krb4_realm, tree, tvb, offset);
213
214         /* ticket length */
215         tlen=tvb_get_guint8(tvb, offset);
216         proto_tree_add_item(tree, hf_krb4_ticket_length, tvb, offset, 1, ENC_BIG_ENDIAN);
217         offset++;
218
219         /* request length */
220         rlen=tvb_get_guint8(tvb, offset);
221         proto_tree_add_item(tree, hf_krb4_request_length, tvb, offset, 1, ENC_BIG_ENDIAN);
222         offset++;
223
224         /* ticket */
225         proto_tree_add_item(tree, hf_krb4_ticket_blob, tvb, offset, tlen, ENC_NA);
226         offset+=tlen;
227
228         /* request */
229         proto_tree_add_item(tree, hf_krb4_request_blob, tvb, offset, rlen, ENC_NA);
230         offset+=rlen;
231
232         /* request time */
233         time_sec.secs=little_endian?tvb_get_letohl(tvb, offset):tvb_get_ntohl(tvb, offset);
234         time_sec.nsecs=0;
235         proto_tree_add_time(tree, hf_krb4_req_date, tvb, offset, 4, &time_sec);
236         offset+=4;
237
238         /* lifetime */
239         lifetime=tvb_get_guint8(tvb, offset);
240         proto_tree_add_uint_format_value(tree, hf_krb4_lifetime, tvb, offset, 1, lifetime, "%d (%d minutes)", lifetime, lifetime*5);
241         offset++;
242
243         /* service Name */
244         offset=dissect_krb4_string(pinfo, hf_krb4_s_name, tree, tvb, offset);
245
246         /* service Instance */
247         offset=dissect_krb4_string(pinfo, hf_krb4_s_instance, tree, tvb, offset);
248
249         return offset;
250 }
251
252
253
254 static int
255 dissect_krb4_auth_msg_type(packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int version)
256 {
257         proto_tree *tree;
258         proto_item *item;
259         guint8 auth_msg_type;
260
261         auth_msg_type=tvb_get_guint8(tvb, offset);
262         item = proto_tree_add_item(parent_tree, hf_krb4_auth_msg_type, tvb, offset, 1, ENC_BIG_ENDIAN);
263         tree = proto_item_add_subtree(item, ett_krb4_auth_msg_type);
264
265         /* m_type */
266         proto_tree_add_item(tree, hf_krb4_m_type, tvb, offset, 1, ENC_BIG_ENDIAN);
267         col_append_fstr(pinfo->cinfo, COL_INFO, "%s%s",
268            (version==TRANSARC_SPECIAL_VERSION)?"TRANSARC-":"",
269             val_to_str(auth_msg_type>>1, m_type_vals, "Unknown (0x%04x)"));
270         proto_item_append_text(item, " %s%s",
271            (version==TRANSARC_SPECIAL_VERSION)?"TRANSARC-":"",
272            val_to_str(auth_msg_type>>1, m_type_vals, "Unknown (0x%04x)"));
273
274         /* byte order */
275         proto_tree_add_item(tree, hf_krb4_byte_order, tvb, offset, 1, ENC_BIG_ENDIAN);
276         proto_item_append_text(item, " (%s)", val_to_str(auth_msg_type&0x01, byte_order_vals, "Unknown (0x%04x)"));
277
278         offset++;
279         return offset;
280 }
281
282 static gboolean
283 dissect_krb4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void *data _U_)
284 {
285         proto_tree *tree;
286         proto_item *item;
287         guint8 version, opcode;
288         int offset=0;
289
290         /* this should better have the value 4 or it might be a weirdo
291          * Transarc AFS special unknown thing.
292          */
293         version=tvb_get_guint8(tvb, offset);
294         if((version!=4)&&(version!=TRANSARC_SPECIAL_VERSION)){
295                 return FALSE;
296         }
297
298         opcode=tvb_get_guint8(tvb, offset+1);
299         switch(opcode>>1){
300         case AUTH_MSG_KDC_REQUEST:
301         case AUTH_MSG_KDC_REPLY:
302         case AUTH_MSG_APPL_REQUEST:
303         case AUTH_MSG_APPL_REQUEST_MUTUAL:
304         case AUTH_MSG_ERR_REPLY:
305         case AUTH_MSG_PRIVATE:
306         case AUTH_MSG_SAFE:
307         case AUTH_MSG_APPL_ERR:
308         case AUTH_MSG_DIE:
309                 break;
310         default:
311                 return FALSE;
312         }
313
314         /* create a tree for krb4 */
315         item = proto_tree_add_item(parent_tree, proto_krb4, tvb, offset, -1, ENC_NA);
316         tree = proto_item_add_subtree(item, ett_krb4);
317
318         col_set_str(pinfo->cinfo, COL_PROTOCOL, "KRB4");
319         col_clear(pinfo->cinfo, COL_INFO);
320
321         /* version */
322         proto_tree_add_item(tree, hf_krb4_version, tvb, offset, 1, ENC_BIG_ENDIAN);
323         offset++;
324
325         /* auth_msg_type */
326         offset = dissect_krb4_auth_msg_type(pinfo, tree, tvb, offset, version);
327
328         switch(opcode>>1){
329         case AUTH_MSG_KDC_REQUEST:
330                 /*offset =*/ dissect_krb4_kdc_request(pinfo, tree, tvb, offset, opcode&0x01, version);
331                 break;
332         case AUTH_MSG_KDC_REPLY:
333                 /*offset =*/ dissect_krb4_kdc_reply(pinfo, tree, tvb, offset, opcode&0x01);
334                 break;
335         case AUTH_MSG_APPL_REQUEST:
336                 /*offset =*/ dissect_krb4_appl_request(pinfo, tree, tvb, offset, opcode&0x01);
337                 break;
338         case AUTH_MSG_APPL_REQUEST_MUTUAL:
339         case AUTH_MSG_ERR_REPLY:
340         case AUTH_MSG_PRIVATE:
341         case AUTH_MSG_SAFE:
342         case AUTH_MSG_APPL_ERR:
343         case AUTH_MSG_DIE:
344                 break;
345         }
346         return TRUE;
347 }
348
349 void
350 proto_register_krb4(void)
351 {
352   static hf_register_info hf[] = {
353     { &hf_krb4_version,
354       { "Version", "krb4.version",
355         FT_UINT8, BASE_DEC, NULL, 0x0,
356         "Kerberos(v4) version number", HFILL }},
357     { &hf_krb4_auth_msg_type,
358       { "Msg Type", "krb4.auth_msg_type",
359         FT_UINT8, BASE_HEX, NULL, 0x0,
360         "Message Type/Byte Order", HFILL }},
361     { &hf_krb4_m_type,
362       { "M Type", "krb4.m_type",
363         FT_UINT8, BASE_HEX, VALS(m_type_vals), 0xfe,
364         "Message Type", HFILL }},
365     { &hf_krb4_byte_order,
366       { "Byte Order", "krb4.byte_order",
367         FT_UINT8, BASE_HEX, VALS(byte_order_vals), 0x01,
368         NULL, HFILL }},
369     { &hf_krb4_name,
370       { "Name", "krb4.name",
371         FT_STRINGZ, BASE_NONE, NULL, 0x00,
372         NULL, HFILL }},
373     { &hf_krb4_instance,
374       { "Instance", "krb4.instance",
375         FT_STRINGZ, BASE_NONE, NULL, 0x00,
376         NULL, HFILL }},
377     { &hf_krb4_realm,
378       { "Realm", "krb4.realm",
379         FT_STRINGZ, BASE_NONE, NULL, 0x00,
380         NULL, HFILL }},
381     { &hf_krb4_time_sec,
382       { "Time Sec", "krb4.time_sec",
383         FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
384         NULL, HFILL }},
385     { &hf_krb4_exp_date,
386       { "Exp Date", "krb4.exp_date",
387         FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
388         NULL, HFILL }},
389     { &hf_krb4_req_date,
390       { "Req Date", "krb4.req_date",
391         FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
392         NULL, HFILL }},
393     { &hf_krb4_lifetime,
394       { "Lifetime", "krb4.lifetime",
395         FT_UINT8, BASE_DEC, NULL, 0x00,
396         "Lifetime (in 5 min units)", HFILL }},
397     { &hf_krb4_s_name,
398       { "Service Name", "krb4.s_name",
399         FT_STRINGZ, BASE_NONE, NULL, 0x00,
400         NULL, HFILL }},
401     { &hf_krb4_s_instance,
402       { "Service Instance", "krb4.s_instance",
403         FT_STRINGZ, BASE_NONE, NULL, 0x00,
404         NULL, HFILL }},
405     { &hf_krb4_kvno,
406       { "Kvno", "krb4.kvno",
407         FT_UINT8, BASE_DEC, NULL, 0x00,
408         "Key Version No", HFILL }},
409     { &hf_krb4_length,
410       { "Length", "krb4.length",
411         FT_UINT32, BASE_DEC, NULL, 0x00,
412         "Length of encrypted blob", HFILL }},
413     { &hf_krb4_ticket_length,
414       { "Ticket Length", "krb4.ticket.length",
415         FT_UINT8, BASE_DEC, NULL, 0x00,
416         "Length of ticket", HFILL }},
417     { &hf_krb4_request_length,
418       { "Request Length", "krb4.request.length",
419         FT_UINT8, BASE_DEC, NULL, 0x00,
420         "Length of request", HFILL }},
421     { &hf_krb4_ticket_blob,
422       { "Ticket Blob", "krb4.ticket.blob",
423         FT_BYTES, BASE_NONE, NULL, 0x00,
424         NULL, HFILL }},
425     { &hf_krb4_request_blob,
426       { "Request Blob", "krb4.request.blob",
427         FT_BYTES, BASE_NONE, NULL, 0x00,
428         NULL, HFILL }},
429     { &hf_krb4_encrypted_blob,
430       { "Encrypted Blob", "krb4.encrypted_blob",
431         FT_BYTES, BASE_NONE, NULL, 0x00,
432         NULL, HFILL }},
433     { &hf_krb4_unknown_transarc_blob,
434       { "Unknown Transarc Blob", "krb4.unknown_transarc_blob",
435         FT_BYTES, BASE_NONE, NULL, 0x00,
436         "Unknown blob only present in Transarc packets", HFILL }},
437   };
438   static gint *ett[] = {
439     &ett_krb4,
440     &ett_krb4_auth_msg_type,
441   };
442
443   proto_krb4 = proto_register_protocol("Kerberos v4",
444                                        "KRB4", "krb4");
445   new_register_dissector("krb4", dissect_krb4, proto_krb4);
446   proto_register_field_array(proto_krb4, hf, array_length(hf));
447   proto_register_subtree_array(ett, array_length(ett));
448 }
449
450 void
451 proto_reg_handoff_krb4(void)
452 {
453   dissector_handle_t krb4_handle;
454
455   krb4_handle = find_dissector("krb4");
456   dissector_add_uint("udp.port", UDP_PORT_KRB4, krb4_handle);
457 }