xref: /netbsd-src/external/bsd/tcpdump/dist/print-krb.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 1995, 1996, 1997
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  *
210f74e101Schristos  * Initial contribution from John Hawkinson (jhawk@mit.edu).
220f74e101Schristos  */
230f74e101Schristos 
2411b3aaa1Schristos #include <sys/cdefs.h>
250f74e101Schristos #ifndef lint
26*26ba0b50Schristos __RCSID("$NetBSD: print-krb.c,v 1.9 2024/09/02 16:15:31 christos Exp $");
270f74e101Schristos #endif
280f74e101Schristos 
29dc860a36Sspz /* \summary: Kerberos printer */
30dc860a36Sspz 
31c74ad251Schristos #include <config.h>
320f74e101Schristos 
33c74ad251Schristos #include "netdissect-stdinc.h"
340f74e101Schristos 
35fdccd7e4Schristos #include "netdissect.h"
360f74e101Schristos #include "extract.h"
370f74e101Schristos 
38c74ad251Schristos /*
39c74ad251Schristos  * Kerberos 4:
40c74ad251Schristos  *
41c74ad251Schristos  * Athena Technical Plan
42c74ad251Schristos  * Section E.2.1
43c74ad251Schristos  * Kerberos Authentication and Authorization System
44c74ad251Schristos  * by S. P. Miller, B. C. Neuman, J. I. Schiller, and J. H. Saltzer
45c74ad251Schristos  *
46c74ad251Schristos  * https://web.mit.edu/Saltzer/www/publications/athenaplan/e.2.1.pdf
47c74ad251Schristos  *
48c74ad251Schristos  * 7. Appendix I Design Specifications
49c74ad251Schristos  *
50c74ad251Schristos  * Kerberos 5:
51c74ad251Schristos  *
52c74ad251Schristos  * RFC 1510, RFC 2630, etc.
53c74ad251Schristos  */
54b3a00663Schristos 
55c74ad251Schristos 
56c74ad251Schristos static const u_char *c_print(netdissect_options *, const u_char *, const u_char *);
57b3a00663Schristos static const u_char *krb4_print_hdr(netdissect_options *, const u_char *);
58b3a00663Schristos static void krb4_print(netdissect_options *, const u_char *);
590f74e101Schristos 
600f74e101Schristos #define AUTH_MSG_KDC_REQUEST			1<<1
610f74e101Schristos #define AUTH_MSG_KDC_REPLY			2<<1
620f74e101Schristos #define AUTH_MSG_APPL_REQUEST			3<<1
630f74e101Schristos #define AUTH_MSG_APPL_REQUEST_MUTUAL		4<<1
640f74e101Schristos #define AUTH_MSG_ERR_REPLY			5<<1
650f74e101Schristos #define AUTH_MSG_PRIVATE			6<<1
660f74e101Schristos #define AUTH_MSG_SAFE				7<<1
670f74e101Schristos #define AUTH_MSG_APPL_ERR			8<<1
680f74e101Schristos #define AUTH_MSG_DIE				63<<1
690f74e101Schristos 
700f74e101Schristos #define KERB_ERR_OK				0
710f74e101Schristos #define KERB_ERR_NAME_EXP			1
720f74e101Schristos #define KERB_ERR_SERVICE_EXP			2
730f74e101Schristos #define KERB_ERR_AUTH_EXP			3
740f74e101Schristos #define KERB_ERR_PKT_VER			4
750f74e101Schristos #define KERB_ERR_NAME_MAST_KEY_VER		5
760f74e101Schristos #define KERB_ERR_SERV_MAST_KEY_VER		6
770f74e101Schristos #define KERB_ERR_BYTE_ORDER			7
780f74e101Schristos #define KERB_ERR_PRINCIPAL_UNKNOWN		8
790f74e101Schristos #define KERB_ERR_PRINCIPAL_NOT_UNIQUE		9
800f74e101Schristos #define KERB_ERR_NULL_KEY			10
810f74e101Schristos 
820f74e101Schristos struct krb {
83c74ad251Schristos 	nd_uint8_t pvno;	/* Protocol Version */
84c74ad251Schristos 	nd_uint8_t type;	/* Type+B */
850f74e101Schristos };
860f74e101Schristos 
87870189d2Schristos static const struct tok type2str[] = {
880f74e101Schristos 	{ AUTH_MSG_KDC_REQUEST,		"KDC_REQUEST" },
890f74e101Schristos 	{ AUTH_MSG_KDC_REPLY,		"KDC_REPLY" },
900f74e101Schristos 	{ AUTH_MSG_APPL_REQUEST,	"APPL_REQUEST" },
910f74e101Schristos 	{ AUTH_MSG_APPL_REQUEST_MUTUAL,	"APPL_REQUEST_MUTUAL" },
920f74e101Schristos 	{ AUTH_MSG_ERR_REPLY,		"ERR_REPLY" },
930f74e101Schristos 	{ AUTH_MSG_PRIVATE,		"PRIVATE" },
940f74e101Schristos 	{ AUTH_MSG_SAFE,		"SAFE" },
950f74e101Schristos 	{ AUTH_MSG_APPL_ERR,		"APPL_ERR" },
960f74e101Schristos 	{ AUTH_MSG_DIE,			"DIE" },
970f74e101Schristos 	{ 0,				NULL }
980f74e101Schristos };
990f74e101Schristos 
100870189d2Schristos static const struct tok kerr2str[] = {
1010f74e101Schristos 	{ KERB_ERR_OK,			"OK" },
1020f74e101Schristos 	{ KERB_ERR_NAME_EXP,		"NAME_EXP" },
1030f74e101Schristos 	{ KERB_ERR_SERVICE_EXP,		"SERVICE_EXP" },
1040f74e101Schristos 	{ KERB_ERR_AUTH_EXP,		"AUTH_EXP" },
1050f74e101Schristos 	{ KERB_ERR_PKT_VER,		"PKT_VER" },
1060f74e101Schristos 	{ KERB_ERR_NAME_MAST_KEY_VER,	"NAME_MAST_KEY_VER" },
1070f74e101Schristos 	{ KERB_ERR_SERV_MAST_KEY_VER,	"SERV_MAST_KEY_VER" },
1080f74e101Schristos 	{ KERB_ERR_BYTE_ORDER,		"BYTE_ORDER" },
1090f74e101Schristos 	{ KERB_ERR_PRINCIPAL_UNKNOWN,	"PRINCIPAL_UNKNOWN" },
1100f74e101Schristos 	{ KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" },
1110f74e101Schristos 	{ KERB_ERR_NULL_KEY,		"NULL_KEY"},
1120f74e101Schristos 	{ 0,				NULL}
1130f74e101Schristos };
1140f74e101Schristos 
1150f74e101Schristos static const u_char *
116b3a00663Schristos c_print(netdissect_options *ndo,
117c74ad251Schristos         const u_char *s, const u_char *ep)
1180f74e101Schristos {
119c74ad251Schristos 	u_char c;
120c74ad251Schristos 	int flag;
1210f74e101Schristos 
1220f74e101Schristos 	flag = 1;
1230f74e101Schristos 	while (s < ep) {
124c74ad251Schristos 		c = GET_U_1(s);
125c74ad251Schristos 		s++;
1260f74e101Schristos 		if (c == '\0') {
1270f74e101Schristos 			flag = 0;
1280f74e101Schristos 			break;
1290f74e101Schristos 		}
130c74ad251Schristos 		fn_print_char(ndo, c);
1310f74e101Schristos 	}
1320f74e101Schristos 	if (flag)
1330f74e101Schristos 		return NULL;
1340f74e101Schristos 	return (s);
1350f74e101Schristos }
1360f74e101Schristos 
1370f74e101Schristos static const u_char *
138b3a00663Schristos krb4_print_hdr(netdissect_options *ndo,
139b3a00663Schristos                const u_char *cp)
1400f74e101Schristos {
1410f74e101Schristos 	cp += 2;
1420f74e101Schristos 
143b3a00663Schristos #define PRINT		if ((cp = c_print(ndo, cp, ndo->ndo_snapend)) == NULL) goto trunc
1440f74e101Schristos 
1450f74e101Schristos 	PRINT;
146c74ad251Schristos 	ND_PRINT(".");
1470f74e101Schristos 	PRINT;
148c74ad251Schristos 	ND_PRINT("@");
1490f74e101Schristos 	PRINT;
1500f74e101Schristos 	return (cp);
1510f74e101Schristos 
1520f74e101Schristos trunc:
153c74ad251Schristos 	nd_print_trunc(ndo);
1540f74e101Schristos 	return (NULL);
1550f74e101Schristos 
1560f74e101Schristos #undef PRINT
1570f74e101Schristos }
1580f74e101Schristos 
1590f74e101Schristos static void
160b3a00663Schristos krb4_print(netdissect_options *ndo,
161b3a00663Schristos            const u_char *cp)
1620f74e101Schristos {
163c74ad251Schristos 	const struct krb *kp;
1640f74e101Schristos 	u_char type;
1650f74e101Schristos 	u_short len;
1660f74e101Schristos 
167b3a00663Schristos #define PRINT		if ((cp = c_print(ndo, cp, ndo->ndo_snapend)) == NULL) goto trunc
1680f74e101Schristos /*  True if struct krb is little endian */
169c74ad251Schristos #define IS_LENDIAN(kp)	((GET_U_1((kp)->type) & 0x01) != 0)
170c74ad251Schristos #define KTOHSP(kp, cp)	(IS_LENDIAN(kp) ? GET_LE_U_2(cp) : GET_BE_U_2(cp))
1710f74e101Schristos 
172fdccd7e4Schristos 	kp = (const struct krb *)cp;
1730f74e101Schristos 
174c74ad251Schristos 	type = GET_U_1(kp->type) & (0xFF << 1);
1750f74e101Schristos 
176c74ad251Schristos 	ND_PRINT(" %s %s: ",
177c74ad251Schristos 	    IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type));
1780f74e101Schristos 
1790f74e101Schristos 	switch (type) {
1800f74e101Schristos 
1810f74e101Schristos 	case AUTH_MSG_KDC_REQUEST:
182b3a00663Schristos 		if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
1830f74e101Schristos 			return;
1840f74e101Schristos 		cp += 4;	/* ctime */
185c74ad251Schristos 		ND_PRINT(" %umin ", GET_U_1(cp) * 5);
186c74ad251Schristos 		cp++;
1870f74e101Schristos 		PRINT;
188c74ad251Schristos 		ND_PRINT(".");
1890f74e101Schristos 		PRINT;
1900f74e101Schristos 		break;
1910f74e101Schristos 
1920f74e101Schristos 	case AUTH_MSG_APPL_REQUEST:
1930f74e101Schristos 		cp += 2;
194c74ad251Schristos 		ND_PRINT("v%u ", GET_U_1(cp));
195c74ad251Schristos 		cp++;
1960f74e101Schristos 		PRINT;
197c74ad251Schristos 		ND_PRINT(" (%u)", GET_U_1(cp));
198c74ad251Schristos 		cp++;
199c74ad251Schristos 		ND_PRINT(" (%u)", GET_U_1(cp));
2000f74e101Schristos 		break;
2010f74e101Schristos 
2020f74e101Schristos 	case AUTH_MSG_KDC_REPLY:
203b3a00663Schristos 		if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
2040f74e101Schristos 			return;
2050f74e101Schristos 		cp += 10;	/* timestamp + n + exp + kvno */
2060f74e101Schristos 		len = KTOHSP(kp, cp);
207c74ad251Schristos 		ND_PRINT(" (%u)", len);
2080f74e101Schristos 		break;
2090f74e101Schristos 
2100f74e101Schristos 	case AUTH_MSG_ERR_REPLY:
211b3a00663Schristos 		if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
2120f74e101Schristos 			return;
2130f74e101Schristos 		cp += 4;	  /* timestamp */
214c74ad251Schristos 		ND_PRINT(" %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp)));
2150f74e101Schristos 		cp += 4;
2160f74e101Schristos 		PRINT;
2170f74e101Schristos 		break;
2180f74e101Schristos 
2190f74e101Schristos 	default:
220c74ad251Schristos 		ND_PRINT("(unknown)");
2210f74e101Schristos 		break;
2220f74e101Schristos 	}
2230f74e101Schristos 
2240f74e101Schristos 	return;
2250f74e101Schristos trunc:
226c74ad251Schristos 	nd_print_trunc(ndo);
2270f74e101Schristos }
2280f74e101Schristos 
2290f74e101Schristos void
230b3a00663Schristos krb_print(netdissect_options *ndo,
231b3a00663Schristos           const u_char *dat)
2320f74e101Schristos {
233c74ad251Schristos 	const struct krb *kp;
2340f74e101Schristos 
235*26ba0b50Schristos 	ndo->ndo_protocol = "kerberos";
236*26ba0b50Schristos 	nd_print_protocol(ndo);
2370f74e101Schristos 
238*26ba0b50Schristos 	kp = (const struct krb *)dat;
2390f74e101Schristos 
240c74ad251Schristos 	switch (GET_U_1(kp->pvno)) {
2410f74e101Schristos 
2420f74e101Schristos 	case 1:
2430f74e101Schristos 	case 2:
2440f74e101Schristos 	case 3:
245c74ad251Schristos 		ND_PRINT(" v%u", GET_U_1(kp->pvno));
2460f74e101Schristos 		break;
2470f74e101Schristos 
2480f74e101Schristos 	case 4:
249c74ad251Schristos 		ND_PRINT(" v%u", GET_U_1(kp->pvno));
250b3a00663Schristos 		krb4_print(ndo, (const u_char *)kp);
2510f74e101Schristos 		break;
2520f74e101Schristos 
2530f74e101Schristos 	case 106:
2540f74e101Schristos 	case 107:
255c74ad251Schristos 		ND_PRINT(" v5");
2560f74e101Schristos 		/* Decode ASN.1 here "someday" */
2570f74e101Schristos 		break;
2580f74e101Schristos 	}
2590f74e101Schristos }
260