aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb4/prot_common.c
blob: 3e36de129139e886ca88823859ad67028fd9df49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * lib/krb4/prot_common.c
 *
 * Copyright 2001 by the Massachusetts Institute of Technology.  All
 * Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * Contains some common code used by multiple encoders/decoders.
 */

#include "krb.h"
#include "prot.h"
#include <string.h>

/*
 * encode_naminstrlm
 *
 * Takes input string triplet of a principal, encodes into PKT.
 * Assumes that input strings are properly terminated.  If CHKLEN is
 * non-zero, validate input string lengths against their respective
 * limits.  The pointer P is the address of the moving pointer used by
 * the caller, and is updated here.
 *
 * Returns zero on success, non-zero on failure.
 *
 * PKT->LENGTH is NOT updated.  The caller must update it.
 */
int KRB5_CALLCONV
krb4prot_encode_naminstrlm(char *name, char *inst, char *realm,
			   int chklen, /* check input str len? */
			   KTEXT pkt, /* buffer to encode into */
			   unsigned char **p /* moving pointer */)
{
    size_t namelen, instlen, realmlen;

    namelen = strlen(name) + 1;
    instlen = strlen(inst) + 1;
    realmlen = strlen(realm) + 1;
    if (chklen && (namelen > ANAME_SZ || instlen > INST_SZ
		   || realmlen > REALM_SZ))
	return KRB4PROT_ERR_OVERRUN;
    if (*p - pkt->dat < namelen + instlen + realmlen)
	return KRB4PROT_ERR_OVERRUN;
    memcpy(*p, name, namelen);
    *p += namelen;
    memcpy(*p, inst, instlen);
    *p += namelen;
    memcpy(*p, realm, realmlen);
    *p += namelen;
    return KRB4PROT_OK;
}

/*
 * decode_naminstrlm
 *
 * Grabs a string triplet corresponding to a principal.  The input
 * buffer PKT should have its length properly set.  The pointer P is
 * the address of the moving pointer used by the caller, and will be
 * updated.  If any input pointer is NULL, merely skip the string.
 *
 * The output strings NAME, INST, and REALM are assumed to be of the
 * correct sizes (ANAME_SZ, INST_SZ, REALM_SZ).
 *
 * Returns 0 on success, non-zero on failure.
 */
int KRB5_CALLCONV
krb4prot_decode_naminstrlm(KTEXT pkt, /* buffer to decode from */
			   unsigned char **p, /* moving pointer */
			   char *name, char *inst, char *realm)
{
    int len;

#define PKT_REMAIN (pkt->length - (*p - pkt->dat))
    if (PKT_REMAIN <= 0)
	return KRB4PROT_ERR_UNDERRUN;
    len = krb4int_strnlen((char *)*p, PKT_REMAIN) + 1;
    if (len == 0 || len > ANAME_SZ)
	return KRB4PROT_ERR_OVERRUN;
    if (name != NULL)
	memcpy(name, *p, (size_t)len);
    *p += len;

    if (PKT_REMAIN <= 0)
	return KRB4PROT_ERR_UNDERRUN;
    len = krb4int_strnlen((char *)*p, PKT_REMAIN) + 1;
    if (len <= 0 || len > INST_SZ)
	return KRB4PROT_ERR_OVERRUN;
    if (name != NULL)
	memcpy(inst, *p, (size_t)len);
    *p += len;

    if (PKT_REMAIN <= 0)
	return KRB4PROT_ERR_UNDERRUN;
    len = krb4int_strnlen((char *)*p, PKT_REMAIN) + 1;
    if (len <= 0 || len > REALM_SZ)
	return KRB4PROT_ERR_OVERRUN;
    if (realm != NULL)
	memcpy(realm, *p, (size_t)len);
    *p += len;
    return KRB4PROT_OK;
#undef PKT_REMAIN
}

int KRB5_CALLCONV
krb4prot_decode_header(KTEXT pkt,
		       int *pver, int *msgtype, int *le)
{
    unsigned char *p;

    p = pkt->dat;
    if (pkt->length < 2)
	return KRB4PROT_ERR_UNDERRUN;
    *pver = *p++;
    *msgtype = *p++;
    *le = *msgtype & 1;
    *msgtype &= ~1;
    return KRB4PROT_OK;
}