2 * Copyright 1993 by OpenVision Technologies, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without fee,
6 * provided that the above copyright notice appears in all copies and
7 * that both that copyright notice and this permission notice appear in
8 * supporting documentation, and that the name of OpenVision not be used
9 * in advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. OpenVision makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
24 #include <gssapi/gssapi.h>
25 #include "gs2_token.h"
28 * $Id: util_token.c 23457 2009-12-08 00:04:48Z tlyu $
31 /* XXXX this code currently makes the assumption that a mech oid will
32 never be longer than 127 bytes. This assumption is not inherent in
33 the interfaces, so the code can be fixed if the OSI namespace
34 balloons unexpectedly. */
37 * Each token looks like this:
38 * 0x60 tag for APPLICATION 0, SEQUENCE
39 * (constructed, definite-length)
40 * <length> possible multiple bytes, need to parse/generate
41 * 0x06 tag for OBJECT IDENTIFIER
42 * <moid_length> compile-time constant string (assume 1 byte)
43 * <moid_bytes> compile-time constant string
44 * <inner_bytes> the ANY containing the application token
45 * bytes 0,1 are the token type
46 * bytes 2,n are the token data
48 * Note that the token type field is a feature of RFC 1964 mechanisms and
49 * is not used by other GSSAPI mechanisms. As such, a token type of -1
50 * is interpreted to mean that no token type should be expected or
53 * For the purposes of this abstraction, the token "header" consists of
54 * the sequence tag and length octets, the mech OID DER encoding, and the
55 * first two inner bytes, which indicate the token type. The token
56 * "body" consists of everything else.
60 der_length_size(size_t length)
64 else if (length < (1<<8))
70 else if (length < (1<<16))
72 else if (length < (1<<24))
80 der_write_length(unsigned char **buf, size_t length)
82 if (length < (1<<7)) {
83 *(*buf)++ = (unsigned char)length;
85 *(*buf)++ = (unsigned char)(der_length_size(length)+127);
87 if (length >= (1<<24))
88 *(*buf)++ = (unsigned char)(length>>24);
89 if (length >= (1<<16))
90 *(*buf)++ = (unsigned char)((length>>16)&0xff);
93 *(*buf)++ = (unsigned char)((length>>8)&0xff);
94 *(*buf)++ = (unsigned char)(length&0xff);
98 /* returns decoded length, or < 0 on failure. Advances buf and
102 der_read_length(unsigned char **buf, ssize_t *bufsize)
113 if ((sf &= 0x7f) > ((*bufsize)-1))
115 if (sf > sizeof(int))
119 ret = (ret<<8) + (*(*buf)++);
129 /* returns the length of a token, given the mech oid and the body size */
132 gs2_token_size(const gss_OID_desc *mech, size_t body_size)
134 /* set body_size to sequence contents size */
135 body_size += 2 + (size_t) mech->length; /* NEED overflow check */
136 return 1 + der_length_size(body_size) + body_size;
139 /* fills in a buffer with the token header. The buffer is assumed to
140 be the right size. buf is advanced past the token header */
143 gs2_make_token_header(
144 const gss_OID_desc *mech,
149 der_write_length(buf, 2 + mech->length + body_size);
151 *(*buf)++ = (unsigned char)mech->length;
152 memcpy(*buf, mech->elements, mech->length);
153 *buf += mech->length;
157 * Given a buffer containing a token, reads and verifies the token,
158 * leaving buf advanced past the token header, and setting body_size
159 * to the number of remaining bytes. Returns 0 on success,
160 * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
161 * mechanism in the token does not match the mech argument. buf and
162 * *body_size are left unmodified on error.
166 gs2_verify_token_header(OM_uint32 *minor,
169 unsigned char **buf_in,
172 unsigned char *buf = *buf_in;
175 ssize_t toksize = (ssize_t)toksize_in;
179 if ((toksize -= 1) < 0)
180 return GSS_S_DEFECTIVE_TOKEN;
183 return GSS_S_DEFECTIVE_TOKEN;
185 seqsize = der_read_length(&buf, &toksize);
187 return GSS_S_DEFECTIVE_TOKEN;
189 if (seqsize != toksize)
190 return GSS_S_DEFECTIVE_TOKEN;
192 if ((toksize -= 1) < 0)
193 return GSS_S_DEFECTIVE_TOKEN;
196 return GSS_S_DEFECTIVE_TOKEN;
198 if ((toksize -= 1) < 0)
199 return GSS_S_DEFECTIVE_TOKEN;
201 toid.length = *buf++;
203 if ((toksize -= toid.length) < 0)
204 return GSS_S_DEFECTIVE_TOKEN;
209 if (!g_OID_equal(&toid, mech))
210 return GSS_S_BAD_MECH;
213 *body_size = toksize;
215 return GSS_S_COMPLETE;