1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright 1993 by OpenVision Technologies, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear in
9 * supporting documentation, and that the name of OpenVision not be used
10 * in advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. OpenVision makes no
12 * representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
24 #include "gssapiP_generic.h"
34 /* XXXX this code currently makes the assumption that a mech oid will
35 never be longer than 127 bytes. This assumption is not inherent in
36 the interfaces, so the code can be fixed if the OSI namespace
37 balloons unexpectedly. */
40 * Each token looks like this:
41 * 0x60 tag for APPLICATION 0, SEQUENCE
42 * (constructed, definite-length)
43 * <length> possible multiple bytes, need to parse/generate
44 * 0x06 tag for OBJECT IDENTIFIER
45 * <moid_length> compile-time constant string (assume 1 byte)
46 * <moid_bytes> compile-time constant string
47 * <inner_bytes> the ANY containing the application token
48 * bytes 0,1 are the token type
49 * bytes 2,n are the token data
51 * Note that the token type field is a feature of RFC 1964 mechanisms and
52 * is not used by other GSSAPI mechanisms. As such, a token type of -1
53 * is interpreted to mean that no token type should be expected or
56 * For the purposes of this abstraction, the token "header" consists of
57 * the sequence tag and length octets, the mech OID DER encoding, and the
58 * first two inner bytes, which indicate the token type. The token
59 * "body" consists of everything else.
62 der_length_size(int length)
66 else if (length < (1<<8))
72 else if (length < (1<<16))
74 else if (length < (1<<24))
82 der_write_length(unsigned char **buf, int length)
84 if (length < (1<<7)) {
85 *(*buf)++ = (unsigned char) length;
87 *(*buf)++ = (unsigned char) (der_length_size(length)+127);
89 if (length >= (1<<24))
90 *(*buf)++ = (unsigned char) (length>>24);
91 if (length >= (1<<16))
92 *(*buf)++ = (unsigned char) ((length>>16)&0xff);
95 *(*buf)++ = (unsigned char) ((length>>8)&0xff);
96 *(*buf)++ = (unsigned char) (length&0xff);
100 /* returns decoded length, or < 0 on failure. Advances buf and
101 decrements bufsize */
104 der_read_length(unsigned char **buf, int *bufsize)
114 if ((sf &= 0x7f) > ((*bufsize)-1))
116 if (sf > sizeof(int))
120 ret = (ret<<8) + (*(*buf)++);
130 /* returns the length of a token, given the mech oid and the body size */
133 g_token_size(const gss_OID_desc * mech, unsigned int body_size)
135 /* set body_size to sequence contents size */
136 body_size += 4 + (unsigned int)mech->length; /* NEED overflow check */
137 return(1 + der_length_size(body_size) + body_size);
140 /* fills in a buffer with the token header. The buffer is assumed to
141 be the right size. buf is advanced past the token header */
145 const gss_OID_desc * mech,
146 unsigned int body_size,
151 der_write_length(buf, ((tok_type == -1) ? 2 : 4) + mech->length + body_size);
153 *(*buf)++ = (unsigned char) mech->length;
154 TWRITE_STR(*buf, mech->elements, mech->length);
155 if (tok_type != -1) {
156 *(*buf)++ = (unsigned char) ((tok_type>>8)&0xff);
157 *(*buf)++ = (unsigned char) (tok_type&0xff);
162 * Given a buffer containing a token, reads and verifies the token,
163 * leaving buf advanced past the token header, and setting body_size
164 * to the number of remaining bytes. Returns 0 on success,
165 * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
166 * mechanism in the token does not match the mech argument. buf and
167 * *body_size are left unmodified on error.
171 g_verify_token_header(
172 const gss_OID_desc * mech,
173 unsigned int *body_size,
174 unsigned char **buf_in,
176 unsigned int toksize_in,
179 unsigned char *buf = *buf_in;
182 int toksize = toksize_in;
184 if ((toksize-=1) < 0)
185 return(G_BAD_TOK_HEADER);
186 if (*buf++ != 0x60) {
187 if (flags & G_VFY_TOKEN_HDR_WRAPPER_REQUIRED)
188 return(G_BAD_TOK_HEADER);
194 if ((seqsize = der_read_length(&buf, &toksize)) < 0)
195 return(G_BAD_TOK_HEADER);
197 if (seqsize != toksize)
198 return(G_BAD_TOK_HEADER);
200 if ((toksize-=1) < 0)
201 return(G_BAD_TOK_HEADER);
203 return(G_BAD_TOK_HEADER);
205 if ((toksize-=1) < 0)
206 return(G_BAD_TOK_HEADER);
207 toid.length = *buf++;
209 if ((toksize-=toid.length) < 0)
210 return(G_BAD_TOK_HEADER);
214 if (! g_OID_equal(&toid, mech))
217 if (tok_type != -1) {
218 if ((toksize-=2) < 0)
219 return(G_BAD_TOK_HEADER);
221 if ((*buf++ != ((tok_type>>8)&0xff)) ||
222 (*buf++ != (tok_type&0xff)))
223 return(G_WRONG_TOKID);
226 *body_size = toksize;