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