Complete the dissection of NetWkstaSetInfo, wheeee, this is fun, plonk.
[obnox/wireshark/wip.git] / packet-kerberos.c
1 /* packet-kerberos.c
2  * Routines for Kerberos
3  * Wes Hardaker (c) 2000
4  * wjhardaker@ucdavis.edu
5  * Richard Sharpe (C) 2002, rsharpe@samba.org, modularized a bit more and
6  *                          added AP-REQ and AP-REP dissection
7  *
8  * See RFC 1510, and various I-Ds and other documents showing additions,
9  * e.g. ones listed under
10  *
11  *      http://www.isi.edu/people/bcn/krb-revisions/
12  *
13  * and
14  *
15  *      http://www.ietf.org/internet-drafts/draft-ietf-krb-wg-kerberos-clarifications-03.txt
16  *
17  * $Id: packet-kerberos.c,v 1.38 2003/04/25 21:30:42 guy Exp $
18  *
19  * Ethereal - Network traffic analyzer
20  * By Gerald Combs <gerald@ethereal.com>
21  * Copyright 1998 Gerald Combs
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public License
25  * as published by the Free Software Foundation; either version 2
26  * of the License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <ctype.h>
45
46 #include <glib.h>
47
48 #include <epan/packet.h>
49
50 #include <epan/strutil.h>
51
52 #include "asn1.h"
53 #include "packet-netbios.h"
54 #include "packet-gssapi.h"
55 #include "packet-tcp.h"
56 #include "prefs.h"
57
58 #define UDP_PORT_KERBEROS               88
59 #define TCP_PORT_KERBEROS               88
60
61 /* Desegment Kerberos over TCP messages */
62 static gboolean krb_desegment = TRUE;
63
64 static gint proto_kerberos = -1;
65 static gint hf_krb_rm_reserved = -1;
66 static gint hf_krb_rm_reclen = -1;
67
68 static gint ett_kerberos = -1;
69 static gint ett_preauth = -1;
70 static gint ett_addresses = -1;
71 static gint ett_request = -1;
72 static gint ett_princ = -1;
73 static gint ett_ticket = -1;
74 static gint ett_encrypted = -1;
75 static gint ett_etype = -1;
76 static gint ett_additional_tickets = -1;
77 static gint ett_recordmark = -1;
78
79 /* TCP Record Mark */
80 #define KRB_RM_RESERVED 0x80000000L
81 #define KRB_RM_RECLEN   0x7fffffffL
82
83 #define KRB5_MSG_AS_REQ   10    /* AS-REQ type */
84 #define KRB5_MSG_AS_REP   11    /* AS-REP type */
85 #define KRB5_MSG_TGS_REQ  12    /* TGS-REQ type */
86 #define KRB5_MSG_TGS_REP  13    /* TGS-REP type */
87 #define KRB5_MSG_AP_REQ   14    /* AP-REQ type */
88 #define KRB5_MSG_AP_REP   15    /* AP-REP type */
89
90 #define KRB5_MSG_SAFE     20    /* KRB-SAFE type */
91 #define KRB5_MSG_PRIV     21    /* KRB-PRIV type */
92 #define KRB5_MSG_CRED     22    /* KRB-CRED type */
93 #define KRB5_MSG_ERROR    30    /* KRB-ERROR type */
94
95 /* Type tags within KDC-REQ */
96 #define KRB5_KDC_REQ_PVNO     1
97 #define KRB5_KDC_REQ_MSG_TYPE 2
98 #define KRB5_KDC_REQ_PADATA   3
99 #define KRB5_KDC_REQ_REQBODY  4
100
101 /* Type tags within KDC-REP */
102 #define KRB5_KDC_REP_PVNO     0
103 #define KRB5_KDC_REP_MSG_TYPE 1
104 #define KRB5_KDC_REP_PADATA   2
105 #define KRB5_KDC_REP_CREALM   3
106 #define KRB5_KDC_REP_CNAME    4
107 #define KRB5_KDC_REP_TICKET   5
108 #define KRB5_KDC_REP_ENC_PART 6
109
110 /* Type tags within KDC-REQ-BODY */
111 #define KRB5_BODY_KDC_OPTIONS            0
112 #define KRB5_BODY_CNAME                  1
113 #define KRB5_BODY_REALM                  2
114 #define KRB5_BODY_SNAME                  3
115 #define KRB5_BODY_FROM                   4
116 #define KRB5_BODY_TILL                   5
117 #define KRB5_BODY_RTIME                  6
118 #define KRB5_BODY_NONCE                  7
119 #define KRB5_BODY_ENCTYPE                8
120 #define KRB5_BODY_ADDRESSES              9
121 #define KRB5_BODY_ENC_AUTHORIZATION_DATA 10
122 #define KRB5_BODY_ADDITIONAL_TICKETS     11
123
124 /* TAGs within AP-REQ */
125 #define KRB5_AP_REQ_APOPTIONS             2
126 #define KRB5_AP_REQ_TICKET                3
127 #define KRB5_AP_REQ_ENC_DATA              4
128
129 /* TAGs within AP-REP */
130 #define KRB5_AP_REP_ENC_DATA              2
131
132 /* Type tags within KRB-ERROR */
133 #define KRB5_ERROR_PVNO       0
134 #define KRB5_ERROR_MSG_TYPE   1
135 #define KRB5_ERROR_CTIME      2
136 #define KRB5_ERROR_CUSEC      3
137 #define KRB5_ERROR_STIME      4
138 #define KRB5_ERROR_SUSEC      5
139 #define KRB5_ERROR_ERROR_CODE 6
140 #define KRB5_ERROR_CREALM     7
141 #define KRB5_ERROR_CNAME      8
142 #define KRB5_ERROR_REALM      9
143 #define KRB5_ERROR_SNAME      10
144 #define KRB5_ERROR_ETEXT      11
145 #define KRB5_ERROR_EDATA      12
146
147 /* address type constants */
148 #define KRB5_ADDR_IPv4       0x02
149 #define KRB5_ADDR_CHAOS      0x05
150 #define KRB5_ADDR_XEROX      0x06
151 #define KRB5_ADDR_ISO        0x07
152 #define KRB5_ADDR_DECNET     0x0c
153 #define KRB5_ADDR_APPLETALK  0x10
154 #define KRB5_ADDR_NETBIOS    0x14
155 #define KRB5_ADDR_IPv6       0x18
156
157 /* encryption type constants */
158 #define KRB5_ENCTYPE_NULL                0
159 #define KRB5_ENCTYPE_DES_CBC_CRC         1
160 #define KRB5_ENCTYPE_DES_CBC_MD4         2
161 #define KRB5_ENCTYPE_DES_CBC_MD5         3
162 #define KRB5_ENCTYPE_DES_CBC_RAW         4
163 #define KRB5_ENCTYPE_DES3_CBC_SHA        5
164 #define KRB5_ENCTYPE_DES3_CBC_RAW        6
165 #define KRB5_ENCTYPE_DES_HMAC_SHA1       8
166 #define KRB5_ENCTYPE_DES3_CBC_SHA1       16
167 #define KERB_ENCTYPE_RC4_HMAC            23 
168 #define KERB_ENCTYPE_RC4_HMAC_EXP        24
169 #define KRB5_ENCTYPE_UNKNOWN                0x1ff
170 #define KRB5_ENCTYPE_LOCAL_DES3_HMAC_SHA1   0x7007
171
172 /*
173  * For KERB_ENCTYPE_RC4_HMAC and KERB_ENCTYPE_RC4_HMAC_EXP, see
174  *
175  *      http://www.ietf.org/internet-drafts/draft-brezak-win2k-krb-rc4-hmac-04.txt
176  *
177  * unless it's expired.
178  */
179
180 /* pre-authentication type constants */
181 #define KRB5_PA_TGS_REQ                1
182 #define KRB5_PA_ENC_TIMESTAMP          2
183 #define KRB5_PA_PW_SALT                3
184 #define KRB5_PA_ENC_ENCKEY             4
185 #define KRB5_PA_ENC_UNIX_TIME          5
186 #define KRB5_PA_ENC_SANDIA_SECURID     6
187 #define KRB5_PA_SESAME                 7
188 #define KRB5_PA_OSF_DCE                8
189 #define KRB5_PA_CYBERSAFE_SECUREID     9
190 #define KRB5_PA_AFS3_SALT              10
191 #define KRB5_PA_ENCTYPE_INFO           11
192 #define KRB5_PA_SAM_CHALLENGE          12
193 #define KRB5_PA_SAM_RESPONSE           13
194 #define KRB5_PA_DASS                   16
195 #define KRB5_PA_USE_SPECIFIED_KVNO     20
196 #define KRB5_PA_SAM_REDIRECT           21
197 #define KRB5_PA_GET_FROM_TYPED_DATA    22
198 #define KRB5_PA_SAM_ETYPE_INFO         23
199 #define KRB5_PA_ALT_PRINC              24
200 #define KRB5_PA_SAM_CHALLENGE2         30
201 #define KRB5_PA_SAM_RESPONSE2          31
202 #define KRB5_PA_PAC_REQUEST            128
203
204 /* Type tags within Ticket */
205 #define KRB5_TKT_TKT_VNO  0
206 #define KRB5_TKT_REALM    1
207 #define KRB5_TKT_SNAME    2
208 #define KRB5_TKT_ENC_PART 3
209
210 /* Principal name-type */
211 #define KRB5_NT_UNKNOWN     0
212 #define KRB5_NT_PRINCIPAL   1
213 #define KRB5_NT_SRV_INST    2
214 #define KRB5_NT_SRV_HST     3
215 #define KRB5_NT_SRV_XHST    4
216 #define KRB5_NT_UID     5
217
218 /* error table constants */
219 /* I prefixed the krb5_err.et constant names with KRB5_ET_ for these */
220 #define KRB5_ET_KRB5KDC_ERR_NONE                         0
221 #define KRB5_ET_KRB5KDC_ERR_NAME_EXP                     1
222 #define KRB5_ET_KRB5KDC_ERR_SERVICE_EXP                  2
223 #define KRB5_ET_KRB5KDC_ERR_BAD_PVNO                     3
224 #define KRB5_ET_KRB5KDC_ERR_C_OLD_MAST_KVNO              4
225 #define KRB5_ET_KRB5KDC_ERR_S_OLD_MAST_KVNO              5
226 #define KRB5_ET_KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN          6
227 #define KRB5_ET_KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN          7
228 #define KRB5_ET_KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE         8
229 #define KRB5_ET_KRB5KDC_ERR_NULL_KEY                     9
230 #define KRB5_ET_KRB5KDC_ERR_CANNOT_POSTDATE              10
231 #define KRB5_ET_KRB5KDC_ERR_NEVER_VALID                  11
232 #define KRB5_ET_KRB5KDC_ERR_POLICY                       12
233 #define KRB5_ET_KRB5KDC_ERR_BADOPTION                    13
234 #define KRB5_ET_KRB5KDC_ERR_ETYPE_NOSUPP                 14
235 #define KRB5_ET_KRB5KDC_ERR_SUMTYPE_NOSUPP               15
236 #define KRB5_ET_KRB5KDC_ERR_PADATA_TYPE_NOSUPP           16
237 #define KRB5_ET_KRB5KDC_ERR_TRTYPE_NOSUPP                17
238 #define KRB5_ET_KRB5KDC_ERR_CLIENT_REVOKED               18
239 #define KRB5_ET_KRB5KDC_ERR_SERVICE_REVOKED              19
240 #define KRB5_ET_KRB5KDC_ERR_TGT_REVOKED                  20
241 #define KRB5_ET_KRB5KDC_ERR_CLIENT_NOTYET                21
242 #define KRB5_ET_KRB5KDC_ERR_SERVICE_NOTYET               22
243 #define KRB5_ET_KRB5KDC_ERR_KEY_EXP                      23
244 #define KRB5_ET_KRB5KDC_ERR_PREAUTH_FAILED               24
245 #define KRB5_ET_KRB5KDC_ERR_PREAUTH_REQUIRED             25
246 #define KRB5_ET_KRB5KDC_ERR_SERVER_NOMATCH               26
247 #define KRB5_ET_KRB5KRB_AP_ERR_BAD_INTEGRITY             31
248 #define KRB5_ET_KRB5KRB_AP_ERR_TKT_EXPIRED               32
249 #define KRB5_ET_KRB5KRB_AP_ERR_TKT_NYV                   33
250 #define KRB5_ET_KRB5KRB_AP_ERR_REPEAT                    34
251 #define KRB5_ET_KRB5KRB_AP_ERR_NOT_US                    35
252 #define KRB5_ET_KRB5KRB_AP_ERR_BADMATCH                  36
253 #define KRB5_ET_KRB5KRB_AP_ERR_SKEW                      37
254 #define KRB5_ET_KRB5KRB_AP_ERR_BADADDR                   38
255 #define KRB5_ET_KRB5KRB_AP_ERR_BADVERSION                39
256 #define KRB5_ET_KRB5KRB_AP_ERR_MSG_TYPE                  40
257 #define KRB5_ET_KRB5KRB_AP_ERR_MODIFIED                  41
258 #define KRB5_ET_KRB5KRB_AP_ERR_BADORDER                  42
259 #define KRB5_ET_KRB5KRB_AP_ERR_ILL_CR_TKT                43
260 #define KRB5_ET_KRB5KRB_AP_ERR_BADKEYVER                 44
261 #define KRB5_ET_KRB5KRB_AP_ERR_NOKEY                     45
262 #define KRB5_ET_KRB5KRB_AP_ERR_MUT_FAIL                  46
263 #define KRB5_ET_KRB5KRB_AP_ERR_BADDIRECTION              47
264 #define KRB5_ET_KRB5KRB_AP_ERR_METHOD                    48
265 #define KRB5_ET_KRB5KRB_AP_ERR_BADSEQ                    49
266 #define KRB5_ET_KRB5KRB_AP_ERR_INAPP_CKSUM               50
267 #define KRB5_ET_KRB5KRB_ERR_RESPONSE_TOO_BIG             52
268 #define KRB5_ET_KRB5KRB_ERR_GENERIC                      60
269 #define KRB5_ET_KRB5KRB_ERR_FIELD_TOOLONG                61
270
271 static const value_string krb5_error_codes[] = {
272         { KRB5_ET_KRB5KDC_ERR_NONE, "KRB5KDC_ERR_NONE" },
273         { KRB5_ET_KRB5KDC_ERR_NAME_EXP, "KRB5KDC_ERR_NAME_EXP" },
274         { KRB5_ET_KRB5KDC_ERR_SERVICE_EXP, "KRB5KDC_ERR_SERVICE_EXP" },
275         { KRB5_ET_KRB5KDC_ERR_BAD_PVNO, "KRB5KDC_ERR_BAD_PVNO" },
276         { KRB5_ET_KRB5KDC_ERR_C_OLD_MAST_KVNO, "KRB5KDC_ERR_C_OLD_MAST_KVNO" },
277         { KRB5_ET_KRB5KDC_ERR_S_OLD_MAST_KVNO, "KRB5KDC_ERR_S_OLD_MAST_KVNO" },
278         { KRB5_ET_KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN, "KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN" },
279         { KRB5_ET_KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN, "KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN" },
280         { KRB5_ET_KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE, "KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE" },
281         { KRB5_ET_KRB5KDC_ERR_NULL_KEY, "KRB5KDC_ERR_NULL_KEY" },
282         { KRB5_ET_KRB5KDC_ERR_CANNOT_POSTDATE, "KRB5KDC_ERR_CANNOT_POSTDATE" },
283         { KRB5_ET_KRB5KDC_ERR_NEVER_VALID, "KRB5KDC_ERR_NEVER_VALID" },
284         { KRB5_ET_KRB5KDC_ERR_POLICY, "KRB5KDC_ERR_POLICY" },
285         { KRB5_ET_KRB5KDC_ERR_BADOPTION, "KRB5KDC_ERR_BADOPTION" },
286         { KRB5_ET_KRB5KDC_ERR_ETYPE_NOSUPP, "KRB5KDC_ERR_ETYPE_NOSUPP" },
287         { KRB5_ET_KRB5KDC_ERR_SUMTYPE_NOSUPP, "KRB5KDC_ERR_SUMTYPE_NOSUPP" },
288         { KRB5_ET_KRB5KDC_ERR_PADATA_TYPE_NOSUPP, "KRB5KDC_ERR_PADATA_TYPE_NOSUPP" },
289         { KRB5_ET_KRB5KDC_ERR_TRTYPE_NOSUPP, "KRB5KDC_ERR_TRTYPE_NOSUPP" },
290         { KRB5_ET_KRB5KDC_ERR_CLIENT_REVOKED, "KRB5KDC_ERR_CLIENT_REVOKED" },
291         { KRB5_ET_KRB5KDC_ERR_SERVICE_REVOKED, "KRB5KDC_ERR_SERVICE_REVOKED" },
292         { KRB5_ET_KRB5KDC_ERR_TGT_REVOKED, "KRB5KDC_ERR_TGT_REVOKED" },
293         { KRB5_ET_KRB5KDC_ERR_CLIENT_NOTYET, "KRB5KDC_ERR_CLIENT_NOTYET" },
294         { KRB5_ET_KRB5KDC_ERR_SERVICE_NOTYET, "KRB5KDC_ERR_SERVICE_NOTYET" },
295         { KRB5_ET_KRB5KDC_ERR_KEY_EXP, "KRB5KDC_ERR_KEY_EXP" },
296         { KRB5_ET_KRB5KDC_ERR_PREAUTH_FAILED, "KRB5KDC_ERR_PREAUTH_FAILED" },
297         { KRB5_ET_KRB5KDC_ERR_PREAUTH_REQUIRED, "KRB5KDC_ERR_PREAUTH_REQUIRED" },
298         { KRB5_ET_KRB5KDC_ERR_SERVER_NOMATCH, "KRB5KDC_ERR_SERVER_NOMATCH" },
299         { KRB5_ET_KRB5KRB_AP_ERR_BAD_INTEGRITY, "KRB5KRB_AP_ERR_BAD_INTEGRITY" },
300         { KRB5_ET_KRB5KRB_AP_ERR_TKT_EXPIRED, "KRB5KRB_AP_ERR_TKT_EXPIRED" },
301         { KRB5_ET_KRB5KRB_AP_ERR_TKT_NYV, "KRB5KRB_AP_ERR_TKT_NYV" },
302         { KRB5_ET_KRB5KRB_AP_ERR_REPEAT, "KRB5KRB_AP_ERR_REPEAT" },
303         { KRB5_ET_KRB5KRB_AP_ERR_NOT_US, "KRB5KRB_AP_ERR_NOT_US" },
304         { KRB5_ET_KRB5KRB_AP_ERR_BADMATCH, "KRB5KRB_AP_ERR_BADMATCH" },
305         { KRB5_ET_KRB5KRB_AP_ERR_SKEW, "KRB5KRB_AP_ERR_SKEW" },
306         { KRB5_ET_KRB5KRB_AP_ERR_BADADDR, "KRB5KRB_AP_ERR_BADADDR" },
307         { KRB5_ET_KRB5KRB_AP_ERR_BADVERSION, "KRB5KRB_AP_ERR_BADVERSION" },
308         { KRB5_ET_KRB5KRB_AP_ERR_MSG_TYPE, "KRB5KRB_AP_ERR_MSG_TYPE" },
309         { KRB5_ET_KRB5KRB_AP_ERR_MODIFIED, "KRB5KRB_AP_ERR_MODIFIED" },
310         { KRB5_ET_KRB5KRB_AP_ERR_BADORDER, "KRB5KRB_AP_ERR_BADORDER" },
311         { KRB5_ET_KRB5KRB_AP_ERR_ILL_CR_TKT, "KRB5KRB_AP_ERR_ILL_CR_TKT" },
312         { KRB5_ET_KRB5KRB_AP_ERR_BADKEYVER, "KRB5KRB_AP_ERR_BADKEYVER" },
313         { KRB5_ET_KRB5KRB_AP_ERR_NOKEY, "KRB5KRB_AP_ERR_NOKEY" },
314         { KRB5_ET_KRB5KRB_AP_ERR_MUT_FAIL, "KRB5KRB_AP_ERR_MUT_FAIL" },
315         { KRB5_ET_KRB5KRB_AP_ERR_BADDIRECTION, "KRB5KRB_AP_ERR_BADDIRECTION" },
316         { KRB5_ET_KRB5KRB_AP_ERR_METHOD, "KRB5KRB_AP_ERR_METHOD" },
317         { KRB5_ET_KRB5KRB_AP_ERR_BADSEQ, "KRB5KRB_AP_ERR_BADSEQ" },
318         { KRB5_ET_KRB5KRB_AP_ERR_INAPP_CKSUM, "KRB5KRB_AP_ERR_INAPP_CKSUM" },
319         { KRB5_ET_KRB5KRB_ERR_RESPONSE_TOO_BIG, "KRB5KRB_ERR_RESPONSE_TOO_BIG"},
320         { KRB5_ET_KRB5KRB_ERR_GENERIC, "KRB5KRB_ERR_GENERIC" },
321         { KRB5_ET_KRB5KRB_ERR_FIELD_TOOLONG, "KRB5KRB_ERR_FIELD_TOOLONG" },
322         { 0, NULL }
323 };
324
325
326 static const value_string krb5_princ_types[] = {
327     { KRB5_NT_UNKNOWN              , "Unknown" },
328     { KRB5_NT_PRINCIPAL            , "Principal" },
329     { KRB5_NT_SRV_INST             , "Service and Instance" },
330     { KRB5_NT_SRV_HST              , "Service and Host" },
331     { KRB5_NT_SRV_XHST             , "Service and Host Components" },
332     { KRB5_NT_UID                  , "Unique ID" },
333     { 0                            , NULL },
334 };
335
336 static const value_string krb5_preauthentication_types[] = {
337     { KRB5_PA_TGS_REQ              , "PA-TGS-REQ" },
338     { KRB5_PA_ENC_TIMESTAMP        , "PA-ENC-TIMESTAMP" },
339     { KRB5_PA_PW_SALT              , "PA-PW-SALT" },
340     { KRB5_PA_ENC_ENCKEY           , "PA-ENC-ENCKEY" },
341     { KRB5_PA_ENC_UNIX_TIME        , "PA-ENC-UNIX-TIME" },
342     { KRB5_PA_ENC_SANDIA_SECURID   , "PA-PW-SALT" },
343     { KRB5_PA_SESAME               , "PA-SESAME" },
344     { KRB5_PA_OSF_DCE              , "PA-OSF-DCE" },
345     { KRB5_PA_CYBERSAFE_SECUREID   , "PA-CYBERSAFE-SECURID" },
346     { KRB5_PA_AFS3_SALT            , "PA-AFS3-SALT" },
347     { KRB5_PA_ENCTYPE_INFO         , "PA-ENCTYPE-INFO" },
348     { KRB5_PA_SAM_CHALLENGE        , "PA-SAM-CHALLENGE" },
349     { KRB5_PA_SAM_RESPONSE         , "PA-SAM-RESPONSE" },
350     { KRB5_PA_DASS                 , "PA-DASS" },
351     { KRB5_PA_USE_SPECIFIED_KVNO   , "PA-USE-SPECIFIED-KVNO" },
352     { KRB5_PA_SAM_REDIRECT         , "PA-SAM-REDIRECT" },
353     { KRB5_PA_GET_FROM_TYPED_DATA  , "PA-GET-FROM-TYPED-DATA" },
354     { KRB5_PA_SAM_ETYPE_INFO       , "PA-SAM-ETYPE-INFO" },
355     { KRB5_PA_ALT_PRINC            , "PA-ALT-PRINC" },
356     { KRB5_PA_SAM_CHALLENGE2       , "PA-SAM-CHALLENGE2" },
357     { KRB5_PA_SAM_RESPONSE2        , "PA-SAM-RESPONSE2" },
358     { KRB5_PA_PAC_REQUEST          , "PA-PAC-REQUEST" },
359     { 0                            , NULL },
360 };
361
362 static const value_string krb5_encryption_types[] = {
363     { KRB5_ENCTYPE_NULL           , "NULL" },
364     { KRB5_ENCTYPE_DES_CBC_CRC    , "des-cbc-crc" },
365     { KRB5_ENCTYPE_DES_CBC_MD4    , "des-cbc-md4" },
366     { KRB5_ENCTYPE_DES_CBC_MD5    , "des-cbc-md5" },
367     { KRB5_ENCTYPE_DES_CBC_RAW    , "des-cbc-raw" },
368     { KRB5_ENCTYPE_DES3_CBC_SHA   , "des3-cbc-sha" },
369     { KRB5_ENCTYPE_DES3_CBC_RAW   , "des3-cbc-raw" },
370     { KRB5_ENCTYPE_DES_HMAC_SHA1  , "des-hmac-sha1" },
371     { KRB5_ENCTYPE_DES3_CBC_SHA1  , "des3-cbc-sha1" },
372     { KERB_ENCTYPE_RC4_HMAC       , "rc4-hmac" },
373     { KERB_ENCTYPE_RC4_HMAC_EXP   , "rc4-hmac-exp" },
374     { KRB5_ENCTYPE_UNKNOWN        , "unknown" },
375     { KRB5_ENCTYPE_LOCAL_DES3_HMAC_SHA1    , "local-des3-hmac-sha1" },
376     { 0                            , NULL },
377 };
378
379 static const value_string krb5_address_types[] = {
380     { KRB5_ADDR_IPv4,           "IPv4"},
381     { KRB5_ADDR_CHAOS,          "CHAOS"},
382     { KRB5_ADDR_XEROX,          "XEROX"},
383     { KRB5_ADDR_ISO,            "ISO"},
384     { KRB5_ADDR_DECNET,         "DECNET"},
385     { KRB5_ADDR_APPLETALK,      "APPLETALK"},
386     { KRB5_ADDR_NETBIOS,        "NETBIOS"},
387     { KRB5_ADDR_IPv6,           "IPv6"},
388     { 0,                        NULL },
389 };
390
391 static const value_string krb5_msg_types[] = {
392         { KRB5_MSG_TGS_REQ,     "TGS-REQ" },
393         { KRB5_MSG_TGS_REP,     "TGS-REP" },
394         { KRB5_MSG_AS_REQ,      "AS-REQ" },
395         { KRB5_MSG_AS_REP,      "AS-REP" },
396         { KRB5_MSG_AP_REQ,      "AP-REQ" },
397         { KRB5_MSG_AP_REP,      "AP-REP" },
398         { KRB5_MSG_SAFE,        "KRB-SAFE" },
399         { KRB5_MSG_PRIV,        "KRB-PRIV" },
400         { KRB5_MSG_CRED,        "KRB-CRED" },
401         { KRB5_MSG_ERROR,       "KRB-ERROR" },
402         { 0,                    NULL },
403 };
404
405 static struct { char *set; char *unset; } bitval = { "Set", "Not set" };
406
407 static int dissect_PrincipalName(char *title, ASN1_SCK *asn1p,
408                                  packet_info *pinfo, proto_tree *tree,
409                                  int start_offset);
410 static int dissect_Ticket(ASN1_SCK *asn1p, packet_info *pinfo,
411                           proto_tree *tree, int start_offset);
412 static int dissect_EncryptedData(char *title, ASN1_SCK *asn1p,
413                                  packet_info *pinfo, proto_tree *tree,
414                                  int start_offset);
415 static int dissect_Addresses(ASN1_SCK *asn1p, packet_info *pinfo,
416                              proto_tree *tree, int start_offset);
417 static void dissect_kerberos_udp(tvbuff_t *tvb, packet_info *pinfo,
418                                  proto_tree *tree);
419 static void dissect_kerberos_tcp(tvbuff_t *tvb, packet_info *pinfo,
420                                  proto_tree *tree);
421 static gint dissect_kerberos_common(tvbuff_t *tvb, packet_info *pinfo,
422                                         proto_tree *tree, int do_col_info,
423                                         gboolean have_rm);
424 static void show_krb_recordmark(proto_tree *kerberos_tree, tvbuff_t *tvb,
425                                 gint start, guint32 krb_rm);
426 static gint kerberos_rm_to_reclen(guint krb_rm);
427 static void dissect_kerberos_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo,
428                                 proto_tree *tree);
429 static guint get_krb_pdu_len(tvbuff_t *tvb, int offset);
430
431 static const char *
432 to_error_str(int ret) {
433     switch (ret) {
434
435         case ASN1_ERR_EOC_MISMATCH:
436             return("EOC mismatch");
437
438         case ASN1_ERR_WRONG_TYPE:
439             return("Wrong type for that item");
440
441         case ASN1_ERR_LENGTH_NOT_DEFINITE:
442             return("Length was indefinite");
443
444         case ASN1_ERR_LENGTH_MISMATCH:
445             return("Length mismatch");
446
447         case ASN1_ERR_WRONG_LENGTH_FOR_TYPE:
448             return("Wrong length for that item's type");
449
450     }
451     return("Unknown error");
452 }
453
454 static void
455 krb_proto_tree_add_time(proto_tree *tree, tvbuff_t *tvb, int offset,
456                         int str_len, char *name, guchar *str)
457 {
458     if (tree)
459         proto_tree_add_text(tree, tvb, offset, str_len,
460                             "%s: %.4s-%.2s-%.2s %.2s:%.2s:%.2s (%.1s)",
461                             name, str, str+4, str+6,
462                             str+8, str+10, str+12,
463                             str+14);
464 }
465
466
467 /*
468  * You must be kidding.  I'm going to actually use a macro to do something?
469  *   bad me.  Bad me.
470  */
471
472 #define KRB_HEAD_DECODE_OR_DIE(token) \
473    start = asn1p->offset; \
474    ret = asn1_header_decode (asn1p, &cls, &con, &tag, &def, &item_len); \
475    if (ret != ASN1_ERR_NOERROR) {\
476        if (check_col(pinfo->cinfo, COL_INFO)) \
477            col_add_fstr(pinfo->cinfo, COL_INFO, "ERROR: Problem at %s: %s, offset: %d", \
478                     token, to_error_str(ret), start); \
479        return -1; \
480    } \
481    if (!def) {\
482        if (check_col(pinfo->cinfo, COL_INFO)) \
483            col_add_fstr(pinfo->cinfo, COL_INFO, "not definite: %s", token); \
484        fprintf(stderr,"not definite: %s\n", token); \
485        return -1; \
486    } \
487    offset += (asn1p->offset - start);
488
489 #define CHECK_APPLICATION_TYPE(expected_tag) \
490     (cls == ASN1_APL && con == ASN1_CON && tag == expected_tag)
491
492 #define DIE_IF_NOT_APPLICATION_TYPE(token, expected_tag) \
493     if (!CHECK_APPLICATION_TYPE(expected_tag)) \
494         DIE_WITH_BAD_TYPE(token, expected_tag);
495
496 #define CHECK_CONTEXT_TYPE(expected_tag) \
497     (cls == ASN1_CTX && con == ASN1_CON && tag == expected_tag)
498
499 #define DIE_IF_NOT_CONTEXT_TYPE(token, expected_tag) \
500     if (!CHECK_CONTEXT_TYPE(expected_tag)) \
501         DIE_WITH_BAD_TYPE(token, expected_tag);
502
503 #define DIE_WITH_BAD_TYPE(token, expected_tag) \
504     { \
505       if (check_col(pinfo->cinfo, COL_INFO)) \
506          col_add_fstr(pinfo->cinfo, COL_INFO, "ERROR: Problem at %s: %s (tag=%d exp=%d, con=%d, cls=%d, offset=%0x)", \
507                       token, to_error_str(ASN1_ERR_WRONG_TYPE), tag, expected_tag, con, cls, start); \
508       return -1; \
509     }
510
511 #define KRB_DECODE_APPLICATION_TAGGED_HEAD_OR_DIE(token, expected_tag) \
512     KRB_HEAD_DECODE_OR_DIE(token); \
513     DIE_IF_NOT_APPLICATION_TYPE(token, expected_tag);
514
515 #define KRB_DECODE_CONTEXT_HEAD_OR_DIE(token, expected_tag) \
516     KRB_HEAD_DECODE_OR_DIE(token); \
517     DIE_IF_NOT_CONTEXT_TYPE(token, expected_tag);
518
519 #define KRB_SEQ_HEAD_DECODE_OR_DIE(token) \
520    ret = asn1_sequence_decode (asn1p, &item_len, &header_len); \
521    if (ret != ASN1_ERR_NOERROR) {\
522        if (check_col(pinfo->cinfo, COL_INFO)) \
523            col_add_fstr(pinfo->cinfo, COL_INFO, "ERROR: Problem at %s: %s", \
524                     token, to_error_str(ret)); \
525        return -1; \
526    } \
527    offset += header_len;
528
529 #define KRB_DECODE_OR_DIE(token, fn, val) \
530     ret = fn (asn1p, &val, &length); \
531     if (ret != ASN1_ERR_NOERROR) { \
532        if (check_col(pinfo->cinfo, COL_INFO)) \
533          col_add_fstr(pinfo->cinfo, COL_INFO, "ERROR: Problem at %s: %s", \
534                      token, to_error_str(ret)); \
535         return -1; \
536     } \
537
538 #define KRB_DECODE_UINT32_OR_DIE(token, val) \
539     KRB_DECODE_OR_DIE(token, asn1_uint32_decode, val);
540
541 #define KRB_DECODE_STRING_OR_DIE(token, expected_tag, val, val_len, item_len) \
542     ret = asn1_string_decode (asn1p, &val, &val_len, &item_len, expected_tag); \
543     if (ret != ASN1_ERR_NOERROR) { \
544        if (check_col(pinfo->cinfo, COL_INFO)) \
545          col_add_fstr(pinfo->cinfo, COL_INFO, "ERROR: Problem at %s: %s", \
546                      token, to_error_str(ret)); \
547         return -1; \
548     }
549
550 #define KRB_DECODE_OCTET_STRING_OR_DIE(token, val, val_len, item_len) \
551     KRB_DECODE_STRING_OR_DIE(token, ASN1_OTS, val, val_len, item_len)
552
553 #define KRB_DECODE_GENERAL_STRING_OR_DIE(token, val, val_len, item_len) \
554     KRB_DECODE_STRING_OR_DIE(token, ASN1_GENSTR, val, val_len, item_len)
555
556 #define KRB_DECODE_GENERAL_TIME_OR_DIE(token, val, val_len, item_len) \
557     KRB_DECODE_STRING_OR_DIE(token, ASN1_GENTIM, val, val_len, item_len)
558
559 /* dissect_type_value_pair decodes (roughly) this:
560
561     SEQUENCE  {
562                         INTEGER,
563                         OCTET STRING
564     }
565
566     which is all over the place in krb5 */
567
568 static void
569 dissect_type_value_pair(ASN1_SCK *asn1p, int *inoff,
570                         guint32 *type, int *type_len, int *type_off,
571                         guchar **val, int *val_len, int *val_off) {
572     int offset = *inoff;
573     guint cls, con, tag;
574     gboolean def;
575     int start;
576     guint tmp_len;
577     int ret;
578
579     /* SEQUENCE */
580     start = asn1p->offset;
581     asn1_header_decode (asn1p, &cls, &con, &tag, &def, &tmp_len);
582     offset += (asn1p->offset - start);
583
584     /* INT */
585     /* wrapper */
586     start = asn1p->offset;
587     asn1_header_decode (asn1p, &cls, &con, &tag, &def, &tmp_len);
588     offset += (asn1p->offset - start);
589
590     if (type_off)
591         *type_off = offset;
592
593     /* value */
594     ret =  asn1_uint32_decode(asn1p, type, type_len);
595     if (ret != ASN1_ERR_NOERROR) {
596         fprintf(stderr,"die: type_value_pair: type, %s\n", to_error_str(ret));
597         return;
598     }
599     offset += tmp_len;
600
601     /* OCTET STRING (or generic data) */
602     /* wrapper */
603     start = asn1p->offset;
604     asn1_header_decode (asn1p, &cls, &con, &tag, &def, val_len);
605     asn1_header_decode (asn1p, &cls, &con, &tag, &def, val_len);
606     offset += asn1p->offset - start;
607
608     if (val_off)
609         *val_off = offset;
610
611     /* value */
612     asn1_string_value_decode (asn1p, *val_len, val);
613
614     *inoff = offset + *val_len;
615 }
616
617 gint
618 dissect_kerberos_main(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int do_col_info)
619 {
620     return (dissect_kerberos_common(tvb, pinfo, tree, do_col_info, FALSE));
621 }
622
623 static void
624 dissect_kerberos_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
625 {
626     if (check_col(pinfo->cinfo, COL_PROTOCOL))
627         col_set_str(pinfo->cinfo, COL_PROTOCOL, "KRB5");
628
629     (void)dissect_kerberos_common(tvb, pinfo, tree, TRUE, FALSE);
630 }
631
632 static gint
633 kerberos_rm_to_reclen(guint krb_rm)
634 {
635     return (krb_rm & KRB_RM_RECLEN);
636 }
637
638 static guint
639 get_krb_pdu_len(tvbuff_t *tvb, int offset)
640 {
641     guint krb_rm;
642     gint pdulen;
643
644     krb_rm = tvb_get_ntohl(tvb, offset);
645     pdulen = kerberos_rm_to_reclen(krb_rm);
646     return (pdulen + 4);
647 }
648
649 static void
650 dissect_kerberos_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
651 {
652     pinfo->fragmented = TRUE;
653     if (dissect_kerberos_common(tvb, pinfo, tree, TRUE, TRUE) < 0) {
654         /*
655          * The dissector failed to recognize this as a valid
656          * Kerberos message.  Mark it as a continuation packet.
657          */
658         if (check_col(pinfo->cinfo, COL_INFO)) {
659                 col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
660         }
661     }
662 }
663
664 static void
665 dissect_kerberos_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
666 {
667     if (check_col(pinfo->cinfo, COL_PROTOCOL))
668         col_set_str(pinfo->cinfo, COL_PROTOCOL, "KRB5");
669
670     tcp_dissect_pdus(tvb, pinfo, tree, krb_desegment, 4, get_krb_pdu_len,
671         dissect_kerberos_tcp_pdu);
672 }
673
674 /*
675  * Display the TCP record mark.
676  */
677 static void
678 show_krb_recordmark(proto_tree *tree, tvbuff_t *tvb, gint start, guint32 krb_rm)
679 {
680     gint rec_len;
681     proto_item *rm_item;
682     proto_tree *rm_tree;
683
684     if (tree == NULL)
685         return;
686
687     rec_len = kerberos_rm_to_reclen(krb_rm);
688     rm_item = proto_tree_add_text(tree, tvb, start, 4,
689         "Record Mark: %u %s", rec_len, plurality(rec_len, "byte", "bytes"));
690     rm_tree = proto_item_add_subtree(rm_item, ett_recordmark);
691     proto_tree_add_boolean(rm_tree, hf_krb_rm_reserved, tvb, start, 4, krb_rm);
692     proto_tree_add_uint(rm_tree, hf_krb_rm_reclen, tvb, start, 4, krb_rm);
693 }
694
695 static gint
696 dissect_kerberos_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
697     int do_col_info, gboolean have_rm)
698 {
699     int offset = 0;
700     proto_tree *kerberos_tree = NULL;
701     proto_tree *etype_tree = NULL;
702     proto_tree *preauth_tree = NULL;
703     proto_tree *request_tree = NULL;
704     proto_tree *additional_tickets_tree = NULL;
705     ASN1_SCK asn1, *asn1p = &asn1;
706     proto_item *item = NULL;
707
708     gint length;
709     guint cls, con, tag;
710     gboolean def;
711     gint item_len;
712     guint total_len;
713     int start, end, message_end, sequence_end;
714
715     int ret;
716
717     guint protocol_message_type;
718
719     guint32 version;
720     guint32 msg_type;
721     guint32 preauth_type;
722     guint32 tmp_int;
723
724     /* simple holders */
725     int str_len;
726     guchar *str;
727     int tmp_pos1, tmp_pos2;
728
729     /* TCP record mark and length */
730     guint32 krb_rm = 0;
731     gint krb_reclen = 0;
732
733     if (have_rm) {
734         krb_rm = tvb_get_ntohl(tvb, offset);
735         krb_reclen = kerberos_rm_to_reclen(krb_rm);
736
737         /*
738          * What is a reasonable size limit?
739          */
740         if (krb_reclen > 10 * 1024 * 1024) {
741             return (-1);
742         }
743         offset += 4;
744     }
745
746     asn1_open(&asn1, tvb, offset);
747
748     /* top header */
749     KRB_HEAD_DECODE_OR_DIE("top");
750     protocol_message_type = tag;
751     if (tree) {
752         item = proto_tree_add_item(tree, proto_kerberos, tvb, 0, -1, FALSE);
753         kerberos_tree = proto_item_add_subtree(item, ett_kerberos);
754     }
755     /*
756      * If item_len and krb_reclen disagree, which should we trust?  Stick
757      * with item_len for now.
758      */
759     message_end = asn1p->offset + item_len;
760
761     /* second header */
762     KRB_HEAD_DECODE_OR_DIE("top2");
763
764     /* version number */
765     KRB_HEAD_DECODE_OR_DIE("version-wrap");
766     KRB_DECODE_UINT32_OR_DIE("version", version);
767
768     if (kerberos_tree) {
769         if (have_rm) {
770             show_krb_recordmark(kerberos_tree, tvb, 0, krb_rm);
771         }
772         proto_tree_add_text(kerberos_tree, tvb, offset, length,
773                             "Version: %d",
774                             version);
775     }
776     offset += length;
777
778     /* message type */
779     KRB_HEAD_DECODE_OR_DIE("message-type-wrap");
780     KRB_DECODE_UINT32_OR_DIE("message-type", msg_type);
781
782     if (kerberos_tree) {
783         proto_tree_add_text(kerberos_tree, tvb, offset, length,
784                             "MSG Type: %s",
785                             val_to_str(msg_type, krb5_msg_types,
786                                        "Unknown msg type %#x"));
787     }
788     offset += length;
789
790     if (do_col_info & check_col(pinfo->cinfo, COL_INFO))
791         col_add_str(pinfo->cinfo, COL_INFO, val_to_str(msg_type, krb5_msg_types,
792                                              "Unknown msg type %#x"));
793     
794         /* is preauthentication present? */
795     KRB_HEAD_DECODE_OR_DIE("padata-or-body");
796     if (((protocol_message_type == KRB5_MSG_AS_REQ ||
797           protocol_message_type == KRB5_MSG_TGS_REQ) &&
798          tag == KRB5_KDC_REQ_PADATA) ||
799         ((protocol_message_type == KRB5_MSG_AS_REP ||
800           protocol_message_type == KRB5_MSG_TGS_REP) &&
801          tag == KRB5_KDC_REP_PADATA)) {
802         /* pre-authentication supplied */
803
804         if (kerberos_tree) {
805             item = proto_tree_add_text(kerberos_tree, tvb, offset,
806                                        item_len, "Pre-Authentication");
807             preauth_tree = proto_item_add_subtree(item, ett_preauth);
808         }
809
810         KRB_HEAD_DECODE_OR_DIE("sequence of pa-data");
811         end = asn1p->offset + item_len;
812
813         while(asn1p->offset < end) {
814             dissect_type_value_pair(asn1p, &offset,
815                                     &preauth_type, &item_len, &tmp_pos1,
816                                     &str, &str_len, &tmp_pos2);
817
818             if (preauth_tree) {
819                 proto_tree_add_text(preauth_tree, tvb, tmp_pos1,
820                                     item_len, "Type: %s",
821                                     val_to_str(preauth_type,
822                                                krb5_preauthentication_types,
823                                                "Unknown preauth type %#x"));
824                 proto_tree_add_text(preauth_tree, tvb, tmp_pos2,
825                                     str_len, "Value: %s",
826                                     bytes_to_str(str, str_len));
827             }
828         }
829         KRB_HEAD_DECODE_OR_DIE("message-body");
830     }
831
832     switch (protocol_message_type) {
833
834     case KRB5_MSG_AS_REQ:
835     case KRB5_MSG_TGS_REQ:
836 /*
837   AS-REQ ::=         [APPLICATION 10] KDC-REQ
838   TGS-REQ ::=        [APPLICATION 12] KDC-REQ
839
840   KDC-REQ ::=        SEQUENCE {
841            pvno[1]               INTEGER,
842            msg-type[2]           INTEGER,
843            padata[3]             SEQUENCE OF PA-DATA OPTIONAL,
844            req-body[4]           KDC-REQ-BODY
845   }
846
847   KDC-REQ-BODY ::=   SEQUENCE {
848             kdc-options[0]       KDCOptions,
849             cname[1]             PrincipalName OPTIONAL,
850                          -- Used only in AS-REQ
851             realm[2]             Realm, -- Server's realm
852                          -- Also client's in AS-REQ
853             sname[3]             PrincipalName OPTIONAL,
854             from[4]              KerberosTime OPTIONAL,
855             till[5]              KerberosTime,
856             rtime[6]             KerberosTime OPTIONAL,
857             nonce[7]             INTEGER,
858             etype[8]             SEQUENCE OF INTEGER, -- EncryptionType,
859                          -- in preference order
860             addresses[9]         HostAddresses OPTIONAL,
861             enc-authorization-data[10]   EncryptedData OPTIONAL,
862                          -- Encrypted AuthorizationData encoding
863             additional-tickets[11]       SEQUENCE OF Ticket OPTIONAL
864   }
865
866 */
867         /* request body */
868         KRB_HEAD_DECODE_OR_DIE("body-sequence");
869         if (kerberos_tree) {
870             item = proto_tree_add_text(kerberos_tree, tvb, offset,
871                                        item_len, "Request");
872             request_tree = proto_item_add_subtree(item, ett_request);
873         }
874         sequence_end = asn1p->offset + item_len;
875
876         /* kdc options */
877         KRB_HEAD_DECODE_OR_DIE("kdc options");
878
879         KRB_HEAD_DECODE_OR_DIE("kdc options:bits");
880
881         if (request_tree) {
882                 proto_tree_add_text(request_tree, tvb, offset, item_len,
883                                     "Options: %s",
884                                     tvb_bytes_to_str(asn1p->tvb, asn1p->offset,
885                                                      item_len));
886         }
887         offset += item_len;
888         asn1p->offset += item_len;
889
890         KRB_HEAD_DECODE_OR_DIE("Client Name or Realm");
891
892         if (CHECK_CONTEXT_TYPE(KRB5_BODY_CNAME)) {
893             item_len = dissect_PrincipalName("Client Name", asn1p, pinfo,
894                                              request_tree, offset);
895             if (item_len == -1)
896                 return -1;
897             offset += item_len;
898             KRB_HEAD_DECODE_OR_DIE("Realm");
899         }
900
901         DIE_IF_NOT_CONTEXT_TYPE("Realm", KRB5_BODY_REALM);
902         KRB_DECODE_GENERAL_STRING_OR_DIE("Realm", str, str_len, item_len);
903         if (request_tree) {
904             proto_tree_add_text(request_tree, tvb, offset, item_len,
905                                 "Realm: %.*s", str_len, str);
906         }
907         offset += item_len;
908
909         KRB_HEAD_DECODE_OR_DIE("Server Name");
910         if (CHECK_CONTEXT_TYPE(KRB5_BODY_SNAME)) {
911             item_len = dissect_PrincipalName("Server Name", asn1p, pinfo,
912                                              request_tree, offset);
913             if (item_len == -1)
914                 return -1;
915             offset += item_len;
916             KRB_HEAD_DECODE_OR_DIE("From or Till");
917         }
918
919         if (CHECK_CONTEXT_TYPE(KRB5_BODY_FROM)) {
920             KRB_DECODE_GENERAL_TIME_OR_DIE("From", str, str_len, item_len);
921             krb_proto_tree_add_time(request_tree, asn1p->tvb, offset, item_len,
922                                     "Start Time", str);
923             offset += item_len;
924             KRB_HEAD_DECODE_OR_DIE("Till");
925         }
926
927         DIE_IF_NOT_CONTEXT_TYPE("Till", KRB5_BODY_TILL);
928         KRB_DECODE_GENERAL_TIME_OR_DIE("Till", str, str_len, item_len);
929         krb_proto_tree_add_time(request_tree, asn1p->tvb, offset, item_len,
930                                 "End Time", str);
931         offset += item_len;
932
933         KRB_HEAD_DECODE_OR_DIE("Renewable Until or Nonce");
934         if (CHECK_CONTEXT_TYPE(KRB5_BODY_RTIME)) {
935             KRB_DECODE_GENERAL_TIME_OR_DIE("Renewable Until", str, str_len, item_len);
936             krb_proto_tree_add_time(request_tree, asn1p->tvb, offset, item_len,
937                                     "Renewable Until", str);
938             offset += item_len;
939             KRB_HEAD_DECODE_OR_DIE("Nonce");
940         }
941
942         DIE_IF_NOT_CONTEXT_TYPE("Nonce", KRB5_BODY_NONCE);
943         KRB_DECODE_UINT32_OR_DIE("Nonce", tmp_int);
944         if (request_tree) {
945             proto_tree_add_text(request_tree, tvb, offset, length,
946                                 "Random Number: %u",
947                                 tmp_int);
948         }
949         offset += length;
950
951         KRB_DECODE_CONTEXT_HEAD_OR_DIE("encryption type spot",
952                                               KRB5_BODY_ENCTYPE);
953         KRB_HEAD_DECODE_OR_DIE("encryption type list");
954         if (kerberos_tree) {
955             item = proto_tree_add_text(request_tree, tvb, offset,
956                                        item_len, "Encryption Types");
957             etype_tree = proto_item_add_subtree(item, ett_etype);
958         }
959         total_len = item_len;
960         while(total_len > 0) {
961             KRB_DECODE_UINT32_OR_DIE("encryption type", tmp_int);
962             if (etype_tree) {
963                 proto_tree_add_text(etype_tree, tvb, offset, length,
964                                     "Type: %s",
965                                     val_to_str(tmp_int,
966                                                krb5_encryption_types,
967                                                "Unknown encryption type %#x"));
968             }
969             offset += length;
970             total_len -= length;
971         }
972
973         if (asn1p->offset >= sequence_end)
974             break;
975         KRB_HEAD_DECODE_OR_DIE("addresses or enc-authorization-data");
976         if (CHECK_CONTEXT_TYPE(KRB5_BODY_ADDRESSES)) {
977             /* addresses supplied */
978
979             length = dissect_Addresses(asn1p, pinfo, kerberos_tree,
980                                        offset);
981             if (offset == -1)
982                 return -1;
983             offset += length;
984             if (asn1p->offset >= sequence_end)
985                 break;
986             KRB_HEAD_DECODE_OR_DIE("enc-authorization-data or additional-tickets");
987         }
988
989         if (CHECK_CONTEXT_TYPE(KRB5_BODY_ENC_AUTHORIZATION_DATA)) {
990             /* enc-authorization-data supplied */
991             length = dissect_EncryptedData("Encrypted Payload", asn1p, pinfo,
992                                            kerberos_tree, offset);
993             if (length == -1)
994                 return -1;
995             offset += length;
996             if (asn1p->offset >= sequence_end)
997                 break;
998             KRB_HEAD_DECODE_OR_DIE("additional-tickets");
999         }
1000
1001         /* additional-tickets supplied */
1002         if (kerberos_tree) {
1003             item = proto_tree_add_text(kerberos_tree, tvb, offset,
1004                                        item_len, "Additional Tickets");
1005             additional_tickets_tree = proto_item_add_subtree(item, ett_additional_tickets);
1006         }
1007         end = asn1p->offset + item_len;
1008         while(asn1p->offset < end) {
1009             KRB_DECODE_CONTEXT_HEAD_OR_DIE("ticket", KRB5_KDC_REP_TICKET);
1010             length = dissect_Ticket(asn1p, pinfo, additional_tickets_tree,
1011                                     offset);
1012             if (length == -1)
1013                 return -1;
1014             offset += length;
1015         }
1016
1017         break;
1018
1019     case KRB5_MSG_AS_REP:
1020     case KRB5_MSG_TGS_REP:
1021 /*
1022    AS-REP ::=    [APPLICATION 11] KDC-REP
1023    TGS-REP ::=   [APPLICATION 13] KDC-REP
1024
1025    KDC-REP ::=   SEQUENCE {
1026                  pvno[0]                    INTEGER,
1027                  msg-type[1]                INTEGER,
1028                  padata[2]                  SEQUENCE OF PA-DATA OPTIONAL,
1029                  crealm[3]                  Realm,
1030                  cname[4]                   PrincipalName,
1031                  ticket[5]                  Ticket,
1032                  enc-part[6]                EncryptedData
1033    }
1034 */
1035
1036         DIE_IF_NOT_CONTEXT_TYPE("crealm", KRB5_KDC_REP_CREALM);
1037         KRB_DECODE_GENERAL_STRING_OR_DIE("realm name", str, str_len, item_len);
1038         if (kerberos_tree) {
1039             proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1040                                 "Realm: %.*s", str_len, str);
1041         }
1042         offset += item_len;
1043
1044         KRB_DECODE_CONTEXT_HEAD_OR_DIE("cname", KRB5_KDC_REP_CNAME);
1045         item_len = dissect_PrincipalName("Client Name", asn1p, pinfo,
1046                                          kerberos_tree, offset);
1047         if (item_len == -1)
1048             return -1;
1049         offset += item_len;
1050
1051         KRB_DECODE_CONTEXT_HEAD_OR_DIE("ticket", KRB5_KDC_REP_TICKET);
1052         length = dissect_Ticket(asn1p, pinfo, kerberos_tree, offset);
1053         if (length == -1)
1054             return -1;
1055         offset += length;
1056
1057         KRB_DECODE_CONTEXT_HEAD_OR_DIE("enc-msg-part",
1058                                               KRB5_KDC_REP_ENC_PART);
1059         length = dissect_EncryptedData("Encrypted Payload", asn1p, pinfo,
1060                                        kerberos_tree, offset);
1061         if (length == -1)
1062             return -1;
1063         offset += length;
1064         break;
1065
1066     case KRB5_MSG_AP_REQ:
1067
1068         /* ap options */
1069         /* We pulled the header above */
1070         
1071         KRB_HEAD_DECODE_OR_DIE("ap options:bits");
1072
1073         if (kerberos_tree) {
1074                 proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1075                                     "APOptions: %s",
1076                                     tvb_bytes_to_str(asn1p->tvb, asn1p->offset,
1077                                                      item_len));
1078         }
1079         offset += item_len;
1080         asn1p->offset += item_len;
1081
1082         KRB_DECODE_CONTEXT_HEAD_OR_DIE("ticket", KRB5_AP_REQ_TICKET);
1083         length = dissect_Ticket(asn1p, pinfo, kerberos_tree, offset);
1084         if (length == -1)
1085             return -1;
1086         offset += length;
1087
1088         KRB_DECODE_CONTEXT_HEAD_OR_DIE("authenticator",
1089                                               KRB5_AP_REQ_ENC_DATA);
1090         length = dissect_EncryptedData("Authenticator", asn1p, pinfo,
1091                                        kerberos_tree, offset);
1092         if (length == -1)
1093             return -1;
1094         offset += length;
1095         break;
1096
1097     case KRB5_MSG_AP_REP:
1098         length = dissect_EncryptedData("EncPart", asn1p, pinfo,
1099                                        kerberos_tree, offset);
1100         if (length == -1)
1101             return -1;
1102         offset += length;
1103         break;
1104
1105     case KRB5_MSG_ERROR:
1106 /*
1107   KRB-ERROR ::=   [APPLICATION 30] SEQUENCE {
1108                    pvno[0]               INTEGER,
1109                    msg-type[1]           INTEGER,
1110                    ctime[2]              KerberosTime OPTIONAL,
1111                    cusec[3]              INTEGER OPTIONAL,
1112                    stime[4]              KerberosTime,
1113                    susec[5]              INTEGER,
1114                    error-code[6]         INTEGER,
1115                    crealm[7]             Realm OPTIONAL,
1116                    cname[8]              PrincipalName OPTIONAL,
1117                    realm[9]              Realm, -- Correct realm
1118                    sname[10]             PrincipalName, -- Correct name
1119                    e-text[11]            GeneralString OPTIONAL,
1120                    e-data[12]            OCTET STRING OPTIONAL
1121    }
1122   }
1123
1124 */
1125
1126         /* ctime */
1127         if (CHECK_CONTEXT_TYPE(KRB5_ERROR_CTIME)) {
1128             KRB_DECODE_GENERAL_TIME_OR_DIE("ctime", str, str_len, item_len);
1129             krb_proto_tree_add_time(kerberos_tree, asn1p->tvb, offset, item_len,
1130                                     "ctime", str);
1131             offset += item_len;
1132                         KRB_HEAD_DECODE_OR_DIE("cusec");
1133         }
1134
1135         /* cusec */
1136         if (CHECK_CONTEXT_TYPE(KRB5_ERROR_CUSEC)) {
1137                         KRB_DECODE_UINT32_OR_DIE("cusec", tmp_int);
1138             if (kerberos_tree) {
1139                 proto_tree_add_text(kerberos_tree, tvb, offset, length,
1140                                     "cusec: %u",
1141                                     tmp_int);
1142             }
1143
1144             offset += item_len;
1145                         KRB_HEAD_DECODE_OR_DIE("sutime");
1146         }
1147
1148         DIE_IF_NOT_CONTEXT_TYPE("sutime", KRB5_ERROR_STIME);
1149         KRB_DECODE_GENERAL_TIME_OR_DIE("stime", str, str_len, item_len);
1150         krb_proto_tree_add_time(kerberos_tree, asn1p->tvb, offset, item_len,
1151                                     "stime", str);
1152         offset += item_len;
1153
1154         KRB_HEAD_DECODE_OR_DIE("susec");
1155         DIE_IF_NOT_CONTEXT_TYPE("susec", KRB5_ERROR_SUSEC);
1156         KRB_DECODE_UINT32_OR_DIE("susec", tmp_int);
1157         if (kerberos_tree) {
1158                 proto_tree_add_text(kerberos_tree, tvb, offset, length,
1159                                     "susec: %u",
1160                                     tmp_int);
1161         }
1162         offset += item_len;
1163
1164         KRB_HEAD_DECODE_OR_DIE("errcode");
1165         DIE_IF_NOT_CONTEXT_TYPE("errcode", KRB5_ERROR_ERROR_CODE);
1166         KRB_DECODE_UINT32_OR_DIE("errcode", tmp_int);
1167         if (kerberos_tree) {
1168             proto_tree_add_text(kerberos_tree, tvb, offset, length,
1169                                 "Error Code: %s",
1170                                 val_to_str(tmp_int, krb5_error_codes,
1171                                            "Unknown error code %#x"));
1172         }
1173         offset += item_len;
1174         KRB_HEAD_DECODE_OR_DIE("crealm");
1175
1176         if (CHECK_CONTEXT_TYPE(KRB5_ERROR_CREALM)) {
1177                 KRB_DECODE_GENERAL_STRING_OR_DIE("crealm", str, str_len, item_len);
1178                 if (kerberos_tree) {
1179                     proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1180                                         "crealm: %.*s", str_len, str);
1181                 }
1182                 offset += item_len;
1183                 KRB_HEAD_DECODE_OR_DIE("cname");
1184         }
1185
1186         if (CHECK_CONTEXT_TYPE(KRB5_ERROR_CNAME)) {
1187             item_len = dissect_PrincipalName("cname", asn1p, pinfo,
1188                                          kerberos_tree, offset);
1189             if (item_len == -1)
1190                 return -1;
1191             offset += item_len;
1192             KRB_HEAD_DECODE_OR_DIE("realm");
1193         }
1194
1195         DIE_IF_NOT_CONTEXT_TYPE("realm", KRB5_ERROR_REALM);
1196         KRB_DECODE_GENERAL_STRING_OR_DIE("realm", str, str_len, item_len);
1197         if (kerberos_tree) {
1198             proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1199                                 "realm: %.*s", str_len, str);
1200         }
1201         offset += item_len;
1202         KRB_HEAD_DECODE_OR_DIE("sname");
1203
1204         DIE_IF_NOT_CONTEXT_TYPE("sname", KRB5_ERROR_SNAME);
1205         item_len = dissect_PrincipalName("sname", asn1p, pinfo,
1206                                          kerberos_tree, offset);
1207         if (item_len == -1)
1208                 return -1;
1209         offset += item_len;
1210
1211         if (asn1p->offset >= message_end)
1212             break;
1213         KRB_HEAD_DECODE_OR_DIE("e-text");
1214         if ( CHECK_CONTEXT_TYPE(KRB5_ERROR_ETEXT) ) {
1215             KRB_DECODE_GENERAL_STRING_OR_DIE("etext", str, str_len, item_len);
1216             if (kerberos_tree) {
1217                 proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1218                                         "etext: %.*s", str_len, str);
1219             }
1220             offset += item_len;
1221             if (asn1p->offset >= message_end)
1222                 break;
1223             KRB_HEAD_DECODE_OR_DIE("e-data");
1224         }
1225
1226         if ( CHECK_CONTEXT_TYPE(KRB5_ERROR_EDATA) ) {
1227             guchar *data;
1228             guint data_len;
1229
1230             KRB_DECODE_OCTET_STRING_OR_DIE("e-data", data, data_len, item_len);
1231
1232             if (kerberos_tree) {
1233                 proto_tree_add_text(kerberos_tree, tvb, offset, item_len,
1234                             "Error Data: %s", bytes_to_str(data, data_len));
1235             }
1236             offset += item_len;
1237         }
1238
1239         break;
1240     }
1241     return offset;
1242 }
1243
1244 static int
1245 dissect_PrincipalName(char *title, ASN1_SCK *asn1p, packet_info *pinfo,
1246                        proto_tree *tree, int start_offset)
1247 {
1248 /*
1249    PrincipalName ::=   SEQUENCE {
1250                        name-type[0]     INTEGER,
1251                        name-string[1]   SEQUENCE OF GeneralString
1252    }
1253 */
1254     proto_tree *princ_tree = NULL;
1255     int offset = start_offset;
1256
1257     guint32 princ_type;
1258
1259     int start;
1260     guint cls, con, tag;
1261     guint header_len, item_len, total_len, type_len;
1262     int ret;
1263
1264     proto_item *item = NULL;
1265     guint length;
1266     gboolean def;
1267
1268     int type_offset;
1269
1270     guchar *name;
1271     guint name_len;
1272
1273     /* principal name */
1274     KRB_SEQ_HEAD_DECODE_OR_DIE("principal section");
1275
1276     if (tree) {
1277       item = proto_tree_add_text(tree, asn1p->tvb, start_offset,
1278                                  (offset - start_offset) + item_len, "%s",
1279                                  title);
1280       princ_tree = proto_item_add_subtree(item, ett_princ);
1281     } else {
1282       item = NULL;
1283       princ_tree = NULL;
1284     }
1285
1286     KRB_DECODE_CONTEXT_HEAD_OR_DIE("principal type", 0);
1287     KRB_DECODE_UINT32_OR_DIE("princ-type", princ_type);
1288     type_offset = offset;
1289     type_len = item_len;
1290     offset += length;
1291
1292     if (princ_tree) {
1293       proto_tree_add_text(princ_tree, asn1p->tvb, type_offset, type_len,
1294                                                 "Type: %s",
1295                                                 val_to_str(princ_type, krb5_princ_types,
1296                                            "Unknown name type %#x"));
1297     }
1298
1299     KRB_DECODE_CONTEXT_HEAD_OR_DIE("principal name-string", 1);
1300     KRB_SEQ_HEAD_DECODE_OR_DIE("principal name-string sequence-of");
1301     total_len = item_len;
1302     if (total_len == 0) {
1303       /* There are no name strings in this PrincipalName, so we can't
1304          put any in the top-level item. */
1305       return offset - start_offset;
1306     }
1307
1308     /* Put the first name string in the top-level item. */
1309     KRB_DECODE_GENERAL_STRING_OR_DIE("principal name", name, name_len, item_len);
1310     if (princ_tree) {
1311         proto_item_set_text(item, "%s: %.*s", title, (int) name_len, name);
1312         proto_tree_add_text(princ_tree, asn1p->tvb, offset, item_len,
1313                             "Name: %.*s", (int) name_len, name);
1314     }
1315     total_len -= item_len;
1316     offset += item_len;
1317
1318     /* Now process the rest of the strings.
1319        XXX - put them in the item as well? */
1320     while (total_len > 0) {
1321         KRB_DECODE_GENERAL_STRING_OR_DIE("principal name", name, name_len, item_len);
1322         if (princ_tree) {
1323             proto_tree_add_text(princ_tree, asn1p->tvb, offset, item_len,
1324                                 "Name: %.*s", (int) name_len, name);
1325         }
1326         total_len -= item_len;
1327         offset += item_len;
1328     }
1329     return offset - start_offset;
1330 }
1331
1332 static int
1333 dissect_Addresses(ASN1_SCK *asn1p, packet_info *pinfo,
1334                   proto_tree *tree, int start_offset) {
1335     proto_tree *address_tree = NULL;
1336     int offset = start_offset;
1337
1338     int start, end;
1339     guint cls, con, tag;
1340     guint item_len;
1341     int ret;
1342
1343     proto_item *item = NULL;
1344     gboolean def;
1345
1346     int tmp_pos1, tmp_pos2;
1347     guint32 address_type;
1348
1349     int str_len;
1350     guchar *str;
1351
1352     char netbios_name[(NETBIOS_NAME_LEN - 1)*4 + 1];
1353     int netbios_name_type;
1354
1355     KRB_HEAD_DECODE_OR_DIE("sequence of addresses");
1356     if (tree) {
1357         item = proto_tree_add_text(tree, asn1p->tvb, offset,
1358                                    item_len, "Addresses");
1359         address_tree = proto_item_add_subtree(item, ett_addresses);
1360     }
1361
1362     start = offset;
1363     end = asn1p->offset + item_len;
1364
1365     while(asn1p->offset < end) {
1366         dissect_type_value_pair(asn1p, &offset,
1367                                 &address_type, &item_len, &tmp_pos1,
1368                                 &str, &str_len, &tmp_pos2);
1369
1370         if (address_tree) {
1371             proto_tree_add_text(address_tree, asn1p->tvb, tmp_pos1,
1372                                 item_len, "Type: %s",
1373                                 val_to_str(address_type, krb5_address_types,
1374                                            "Unknown address type %#x"));
1375             switch(address_type) {
1376                 case KRB5_ADDR_IPv4:
1377                     proto_tree_add_text(address_tree, asn1p->tvb, tmp_pos2,
1378                                         str_len, "Value: %d.%d.%d.%d",
1379                                         str[0], str[1], str[2], str[3]);
1380                     break;
1381
1382                 case KRB5_ADDR_NETBIOS:
1383                     if (str_len == NETBIOS_NAME_LEN) {
1384                         netbios_name_type = process_netbios_name(str,
1385                                                                  netbios_name);
1386                         proto_tree_add_text(address_tree, asn1p->tvb, tmp_pos2,
1387                                             str_len,
1388                                             "Value: %s<%02x> (%s)",
1389                                             netbios_name, netbios_name_type,
1390                                             netbios_name_type_descr(netbios_name_type));
1391                     } else {
1392                         proto_tree_add_text(address_tree, asn1p->tvb, tmp_pos2,
1393                                             str_len,
1394                                             "Value (Invalid length %d, should be 16): \"%s\"",
1395                                             str_len, format_text(str, str_len));
1396                     }
1397                     break;
1398
1399                 /* XXX - do KRB5_ADDR_IPv6 */
1400
1401                 default:
1402                     proto_tree_add_text(address_tree, asn1p->tvb, tmp_pos2,
1403                                         str_len, "Value: %s",
1404                                         bytes_to_str(str, str_len));
1405             }
1406         }
1407     }
1408
1409     return offset - start_offset;
1410 }
1411
1412 static int
1413 dissect_EncryptedData(char *title, ASN1_SCK *asn1p, packet_info *pinfo,
1414                       proto_tree *tree, int start_offset)
1415 {
1416 /*
1417    EncryptedData ::=   SEQUENCE {
1418                        etype[0]     INTEGER, -- EncryptionType
1419                        kvno[1]      INTEGER OPTIONAL,
1420                        cipher[2]    OCTET STRING -- ciphertext
1421    }
1422 */
1423     proto_tree *encr_tree = NULL;
1424     int offset = start_offset;
1425
1426     int start;
1427     guint cls, con, tag;
1428     guint header_len, item_len, data_len;
1429     int ret;
1430
1431     proto_item *item = NULL;
1432     guint length;
1433     gboolean def;
1434     guint32 val;
1435
1436     guchar *data;
1437
1438     KRB_SEQ_HEAD_DECODE_OR_DIE("encrypted data section");
1439
1440     if (tree) {
1441         item = proto_tree_add_text(tree, asn1p->tvb, start_offset,
1442                                    (offset - start_offset) + item_len,
1443                                    "Encrypted Data: %s", title);
1444         encr_tree = proto_item_add_subtree(item, ett_princ);
1445     }
1446
1447     /* type */
1448     KRB_DECODE_CONTEXT_HEAD_OR_DIE("encryption type", 0);
1449     KRB_DECODE_UINT32_OR_DIE("encr-type", val);
1450     if (encr_tree) {
1451         proto_tree_add_text(encr_tree, asn1p->tvb, offset, length,
1452                             "Type: %s",
1453                             val_to_str(val, krb5_encryption_types,
1454                                        "Unknown encryption type %#x"));
1455     }
1456     offset += length;
1457
1458     /* kvno */
1459     KRB_HEAD_DECODE_OR_DIE("kvno-wrap or cipher-wrap");
1460     if (CHECK_CONTEXT_TYPE(1)) {
1461       KRB_DECODE_UINT32_OR_DIE("kvno", val);
1462       if (encr_tree) {
1463           proto_tree_add_text(encr_tree, asn1p->tvb, offset, length,
1464                               "KVNO: %u", val);
1465       }
1466       offset += length;
1467       KRB_HEAD_DECODE_OR_DIE("cipher-wrap");
1468     }
1469
1470     DIE_IF_NOT_CONTEXT_TYPE("cipher-wrap", 2);
1471     KRB_DECODE_OCTET_STRING_OR_DIE("cipher", data, data_len, item_len);
1472
1473     if (encr_tree) {
1474         proto_tree_add_text(encr_tree, asn1p->tvb, offset, item_len,
1475                             "CipherText: %s", bytes_to_str(data, data_len));
1476     }
1477     offset += item_len;
1478
1479     return offset - start_offset;
1480 }
1481
1482 static int
1483 dissect_Ticket(ASN1_SCK *asn1p, packet_info *pinfo,
1484                proto_tree *tree, int start_offset)
1485 {
1486 /*
1487    Ticket ::=                    [APPLICATION 1] SEQUENCE {
1488                                  tkt-vno[0]                   INTEGER,
1489                                  realm[1]                     Realm,
1490                                  sname[2]                     PrincipalName,
1491                                  enc-part[3]                  EncryptedData
1492    }
1493 */
1494     proto_tree *ticket_tree = NULL;
1495     int offset = start_offset;
1496
1497     int start;
1498     guint cls, con, tag;
1499     guint header_len, total_len;
1500     gint item_len;
1501     int ret;
1502
1503     proto_item *item = NULL;
1504     gint length;
1505     gboolean def;
1506     guint32 val;
1507
1508     int str_len;
1509     guchar *str;
1510
1511     KRB_DECODE_APPLICATION_TAGGED_HEAD_OR_DIE("Ticket section", 1);
1512     KRB_SEQ_HEAD_DECODE_OR_DIE("Ticket sequence");
1513     total_len = item_len;
1514
1515     if (tree) {
1516         item = proto_tree_add_text(tree, asn1p->tvb, start_offset,
1517                                    (offset - start_offset) + item_len,
1518                                    "Ticket");
1519         ticket_tree = proto_item_add_subtree(item, ett_ticket);
1520     }
1521
1522     /* type */
1523     KRB_DECODE_CONTEXT_HEAD_OR_DIE("Ticket tkt-vno", KRB5_TKT_TKT_VNO);
1524     KRB_DECODE_UINT32_OR_DIE("Ticket tkt-vno", val);
1525     if (ticket_tree) {
1526         proto_tree_add_text(ticket_tree, asn1p->tvb, offset, length,
1527                             "Version: %u", val);
1528     }
1529     offset += length;
1530     total_len -= length;
1531
1532     /* realm name */
1533     KRB_DECODE_CONTEXT_HEAD_OR_DIE("Ticket realm", KRB5_TKT_REALM);
1534     KRB_DECODE_GENERAL_STRING_OR_DIE("Ticket realm string", str, str_len, item_len);
1535     if (ticket_tree) {
1536         proto_tree_add_text(ticket_tree, asn1p->tvb, offset, item_len,
1537                             "Realm: %.*s", str_len, str);
1538     }
1539     offset += item_len;
1540     total_len -= item_len;
1541
1542     /* server name (sname) */
1543     KRB_DECODE_CONTEXT_HEAD_OR_DIE("Ticket sname", KRB5_TKT_SNAME);
1544     item_len = dissect_PrincipalName("Service Name", asn1p, pinfo, ticket_tree,
1545                                      offset);
1546     if (item_len == -1)
1547         return -1;
1548     offset += item_len;
1549
1550     /* encrypted part */
1551     KRB_DECODE_CONTEXT_HEAD_OR_DIE("enc-part", KRB5_TKT_ENC_PART);
1552     length = dissect_EncryptedData("Ticket data", asn1p, pinfo, ticket_tree,
1553                                    offset);
1554     if (length == -1)
1555         return -1;
1556     offset += length;
1557
1558     return offset - start_offset;
1559 }
1560
1561
1562 void
1563 proto_register_kerberos(void)
1564 {
1565     static hf_register_info hf[] = {
1566         { &hf_krb_rm_reserved, {
1567             "Reserved", "kerberos.rm.reserved", FT_BOOLEAN, 32,
1568             &bitval, KRB_RM_RESERVED, "Record mark reserved bit", HFILL }},
1569         { &hf_krb_rm_reclen, {
1570             "Record Length", "kerberos.rm.length", FT_UINT32, BASE_DEC,
1571             NULL, KRB_RM_RECLEN, "Record length", HFILL }},
1572     };
1573
1574     static gint *ett[] = {
1575         &ett_kerberos,
1576         &ett_preauth,
1577         &ett_request,
1578         &ett_princ,
1579         &ett_encrypted,
1580         &ett_ticket,
1581         &ett_addresses,
1582         &ett_etype,
1583         &ett_additional_tickets,
1584         &ett_recordmark,
1585     };
1586     module_t *krb_module;
1587
1588     proto_kerberos = proto_register_protocol("Kerberos", "KRB5", "kerberos");
1589     proto_register_field_array(proto_kerberos, hf, array_length(hf));
1590     proto_register_subtree_array(ett, array_length(ett));
1591
1592     /* Register preferences */
1593     krb_module = prefs_register_protocol(proto_kerberos, NULL);
1594     prefs_register_bool_preference(krb_module, "desegment",
1595         "Desegment Kerberos over TCP messages",
1596         "Whether the dissector should desegment "
1597         "multi-segment Kerberos messages", &krb_desegment);
1598 }
1599
1600 void
1601 proto_reg_handoff_kerberos(void)
1602 {
1603     dissector_handle_t kerberos_handle_udp;
1604     dissector_handle_t kerberos_handle_tcp;
1605
1606     kerberos_handle_udp = create_dissector_handle(dissect_kerberos_udp,
1607         proto_kerberos);
1608     kerberos_handle_tcp = create_dissector_handle(dissect_kerberos_tcp,
1609         proto_kerberos);
1610     dissector_add("udp.port", UDP_PORT_KERBEROS, kerberos_handle_udp);
1611     dissector_add("tcp.port", TCP_PORT_KERBEROS, kerberos_handle_tcp);
1612
1613 }
1614
1615 /*
1616
1617   MISC definitions from RFC1510:
1618
1619    Realm ::=           GeneralString
1620
1621    KerberosTime ::=   GeneralizedTime
1622
1623    HostAddress ::=    SEQUENCE  {
1624                       addr-type[0]             INTEGER,
1625                       address[1]               OCTET STRING
1626    }
1627
1628    HostAddresses ::=   SEQUENCE OF SEQUENCE {
1629                        addr-type[0]             INTEGER,
1630                        address[1]               OCTET STRING
1631    }
1632
1633    AuthorizationData ::=   SEQUENCE OF SEQUENCE {
1634                            ad-type[0]               INTEGER,
1635                            ad-data[1]               OCTET STRING
1636    }
1637                    APOptions ::=   BIT STRING {
1638                                    reserved(0),
1639                                    use-session-key(1),
1640                                    mutual-required(2)
1641                    }
1642
1643
1644                    TicketFlags ::=   BIT STRING {
1645                                      reserved(0),
1646                                      forwardable(1),
1647                                      forwarded(2),
1648                                      proxiable(3),
1649                                      proxy(4),
1650                                      may-postdate(5),
1651                                      postdated(6),
1652                                      invalid(7),
1653                                      renewable(8),
1654                                      initial(9),
1655                                      pre-authent(10),
1656                                      hw-authent(11)
1657                    }
1658
1659                   KDCOptions ::=   BIT STRING {
1660                                    reserved(0),
1661                                    forwardable(1),
1662                                    forwarded(2),
1663                                    proxiable(3),
1664                                    proxy(4),
1665                                    allow-postdate(5),
1666                                    postdated(6),
1667                                    unused7(7),
1668                                    renewable(8),
1669                                    unused9(9),
1670                                    unused10(10),
1671                                    unused11(11),
1672                                    renewable-ok(27),
1673                                    enc-tkt-in-skey(28),
1674                                    renew(30),
1675                                    validate(31)
1676                   }
1677
1678
1679             LastReq ::=   SEQUENCE OF SEQUENCE {
1680                           lr-type[0]               INTEGER,
1681                           lr-value[1]              KerberosTime
1682             }
1683
1684    Ticket ::=                    [APPLICATION 1] SEQUENCE {
1685                                  tkt-vno[0]                   INTEGER,
1686                                  realm[1]                     Realm,
1687                                  sname[2]                     PrincipalName,
1688                                  enc-part[3]                  EncryptedData
1689    }
1690
1691   -- Encrypted part of ticket
1692   EncTicketPart ::=     [APPLICATION 3] SEQUENCE {
1693                         flags[0]             TicketFlags,
1694                         key[1]               EncryptionKey,
1695                         crealm[2]            Realm,
1696                         cname[3]             PrincipalName,
1697                         transited[4]         TransitedEncoding,
1698                         authtime[5]          KerberosTime,
1699                         starttime[6]         KerberosTime OPTIONAL,
1700                         endtime[7]           KerberosTime,
1701                         renew-till[8]        KerberosTime OPTIONAL,
1702                         caddr[9]             HostAddresses OPTIONAL,
1703                         authorization-data[10]   AuthorizationData OPTIONAL
1704   }
1705
1706   -- encoded Transited field
1707   TransitedEncoding ::=         SEQUENCE {
1708                                 tr-type[0]  INTEGER, -- must be registered
1709                                 contents[1]          OCTET STRING
1710   }
1711
1712   -- Unencrypted authenticator
1713   Authenticator ::=    [APPLICATION 2] SEQUENCE    {
1714                  authenticator-vno[0]          INTEGER,
1715                  crealm[1]                     Realm,
1716                  cname[2]                      PrincipalName,
1717                  cksum[3]                      Checksum OPTIONAL,
1718                  cusec[4]                      INTEGER,
1719                  ctime[5]                      KerberosTime,
1720                  subkey[6]                     EncryptionKey OPTIONAL,
1721                  seq-number[7]                 INTEGER OPTIONAL,
1722                  authorization-data[8]         AuthorizationData OPTIONAL
1723   }
1724
1725   PA-DATA ::=        SEQUENCE {
1726            padata-type[1]        INTEGER,
1727            padata-value[2]       OCTET STRING,
1728                          -- might be encoded AP-REQ
1729   }
1730
1731    padata-type     ::= PA-ENC-TIMESTAMP
1732    padata-value    ::= EncryptedData -- PA-ENC-TS-ENC
1733
1734    PA-ENC-TS-ENC   ::= SEQUENCE {
1735            patimestamp[0]               KerberosTime, -- client's time
1736            pausec[1]                    INTEGER OPTIONAL
1737    }
1738
1739    EncASRepPart ::=    [APPLICATION 25[25]] EncKDCRepPart
1740    EncTGSRepPart ::=   [APPLICATION 26] EncKDCRepPart
1741
1742    EncKDCRepPart ::=   SEQUENCE {
1743                key[0]                       EncryptionKey,
1744                last-req[1]                  LastReq,
1745                nonce[2]                     INTEGER,
1746                key-expiration[3]            KerberosTime OPTIONAL,
1747                flags[4]                     TicketFlags,
1748                authtime[5]                  KerberosTime,
1749                starttime[6]                 KerberosTime OPTIONAL,
1750                endtime[7]                   KerberosTime,
1751                renew-till[8]                KerberosTime OPTIONAL,
1752                srealm[9]                    Realm,
1753                sname[10]                    PrincipalName,
1754                caddr[11]                    HostAddresses OPTIONAL
1755    }
1756
1757    AP-REQ ::=      [APPLICATION 14] SEQUENCE {
1758                    pvno[0]                       INTEGER,
1759                    msg-type[1]                   INTEGER,
1760                    ap-options[2]                 APOptions,
1761                    ticket[3]                     Ticket,
1762                    authenticator[4]              EncryptedData
1763    }
1764
1765    APOptions ::=   BIT STRING {
1766                    reserved(0),
1767                    use-session-key(1),
1768                    mutual-required(2)
1769    }
1770
1771    AP-REP ::=         [APPLICATION 15] SEQUENCE {
1772               pvno[0]                   INTEGER,
1773               msg-type[1]               INTEGER,
1774               enc-part[2]               EncryptedData
1775    }
1776
1777    EncAPRepPart ::=   [APPLICATION 27]     SEQUENCE {
1778               ctime[0]                  KerberosTime,
1779               cusec[1]                  INTEGER,
1780               subkey[2]                 EncryptionKey OPTIONAL,
1781               seq-number[3]             INTEGER OPTIONAL
1782    }
1783
1784    KRB-SAFE ::=        [APPLICATION 20] SEQUENCE {
1785                pvno[0]               INTEGER,
1786                msg-type[1]           INTEGER,
1787                safe-body[2]          KRB-SAFE-BODY,
1788                cksum[3]              Checksum
1789    }
1790
1791    KRB-SAFE-BODY ::=   SEQUENCE {
1792                user-data[0]          OCTET STRING,
1793                timestamp[1]          KerberosTime OPTIONAL,
1794                usec[2]               INTEGER OPTIONAL,
1795                seq-number[3]         INTEGER OPTIONAL,
1796                s-address[4]          HostAddress,
1797                r-address[5]          HostAddress OPTIONAL
1798    }
1799
1800    KRB-PRIV ::=         [APPLICATION 21] SEQUENCE {
1801                 pvno[0]                   INTEGER,
1802                 msg-type[1]               INTEGER,
1803                 enc-part[3]               EncryptedData
1804    }
1805
1806    EncKrbPrivPart ::=   [APPLICATION 28] SEQUENCE {
1807                 user-data[0]              OCTET STRING,
1808                 timestamp[1]              KerberosTime OPTIONAL,
1809                 usec[2]                   INTEGER OPTIONAL,
1810                 seq-number[3]             INTEGER OPTIONAL,
1811                 s-address[4]              HostAddress, -- sender's addr
1812                 r-address[5]              HostAddress OPTIONAL
1813                                                       -- recip's addr
1814    }
1815
1816    KRB-CRED         ::= [APPLICATION 22]   SEQUENCE {
1817                     pvno[0]                INTEGER,
1818                     msg-type[1]            INTEGER, -- KRB_CRED
1819                     tickets[2]             SEQUENCE OF Ticket,
1820                     enc-part[3]            EncryptedData
1821    }
1822
1823    EncKrbCredPart   ::= [APPLICATION 29]   SEQUENCE {
1824                     ticket-info[0]         SEQUENCE OF KrbCredInfo,
1825                     nonce[1]               INTEGER OPTIONAL,
1826                     timestamp[2]           KerberosTime OPTIONAL,
1827                     usec[3]                INTEGER OPTIONAL,
1828                     s-address[4]           HostAddress OPTIONAL,
1829                     r-address[5]           HostAddress OPTIONAL
1830    }
1831
1832    KrbCredInfo      ::=                    SEQUENCE {
1833                     key[0]                 EncryptionKey,
1834                     prealm[1]              Realm OPTIONAL,
1835                     pname[2]               PrincipalName OPTIONAL,
1836                     flags[3]               TicketFlags OPTIONAL,
1837                     authtime[4]            KerberosTime OPTIONAL,
1838                     starttime[5]           KerberosTime OPTIONAL,
1839                     endtime[6]             KerberosTime OPTIONAL
1840                     renew-till[7]          KerberosTime OPTIONAL,
1841                     srealm[8]              Realm OPTIONAL,
1842                     sname[9]               PrincipalName OPTIONAL,
1843                     caddr[10]              HostAddresses OPTIONAL
1844    }
1845
1846    KRB-ERROR ::=   [APPLICATION 30] SEQUENCE {
1847                    pvno[0]               INTEGER,
1848                    msg-type[1]           INTEGER,
1849                    ctime[2]              KerberosTime OPTIONAL,
1850                    cusec[3]              INTEGER OPTIONAL,
1851                    stime[4]              KerberosTime,
1852                    susec[5]              INTEGER,
1853                    error-code[6]         INTEGER,
1854                    crealm[7]             Realm OPTIONAL,
1855                    cname[8]              PrincipalName OPTIONAL,
1856                    realm[9]              Realm, -- Correct realm
1857                    sname[10]             PrincipalName, -- Correct name
1858                    e-text[11]            GeneralString OPTIONAL,
1859                    e-data[12]            OCTET STRING OPTIONAL
1860    }
1861
1862    e-data    This field contains additional data about the error for use
1863              by the application to help it recover from or handle the
1864              error.  If the errorcode is KDC_ERR_PREAUTH_REQUIRED, then
1865              the e-data field will contain an encoding of a sequence of
1866              padata fields, each corresponding to an acceptable pre-
1867              authentication method and optionally containing data for
1868              the method:
1869
1870       METHOD-DATA ::=    SEQUENCE of PA-DATA
1871
1872    If the error-code is KRB_AP_ERR_METHOD, then the e-data field will
1873    contain an encoding of the following sequence:
1874
1875       METHOD-DATA ::=    SEQUENCE {
1876                          method-type[0]   INTEGER,
1877                          method-data[1]   OCTET STRING OPTIONAL
1878       }
1879
1880       EncryptionKey ::=   SEQUENCE {
1881                          keytype[0]    INTEGER,
1882                          keyvalue[1]   OCTET STRING
1883       }
1884
1885       Checksum ::=   SEQUENCE {
1886                          cksumtype[0]   INTEGER,
1887                          checksum[1]    OCTET STRING
1888       }
1889
1890 */