1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
5 * Copyright 2004, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
11 * linux/net/sunrpc/gss_generic_token.c
13 * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c
15 * Copyright (c) 2000 The Regents of the University of Michigan.
16 * All rights reserved.
18 * Andy Adamson <andros@umich.edu>
22 * Copyright 1993 by OpenVision Technologies, Inc.
24 * Permission to use, copy, modify, distribute, and sell this software
25 * and its documentation for any purpose is hereby granted without fee,
26 * provided that the above copyright notice appears in all copies and
27 * that both that copyright notice and this permission notice appear in
28 * supporting documentation, and that the name of OpenVision not be used
29 * in advertising or publicity pertaining to distribution of the software
30 * without specific, written prior permission. OpenVision makes no
31 * representations about the suitability of this software for any
32 * purpose. It is provided "as is" without express or implied warranty.
34 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
35 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
36 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
37 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
38 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
39 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
40 * PERFORMANCE OF THIS SOFTWARE.
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_SEC
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
52 #include <liblustre.h>
55 #include <libcfs/kp30.h>
56 #include <linux/obd.h>
57 #include <linux/obd_class.h>
58 #include <linux/obd_support.h>
59 #include <linux/lustre_idl.h>
60 #include <linux/lustre_net.h>
61 #include <linux/lustre_import.h>
62 #include <linux/lustre_sec.h>
65 #include "gss_internal.h"
71 /* TWRITE_STR from gssapiP_generic.h */
72 #define TWRITE_STR(ptr, str, len) \
73 memcpy((ptr), (char *) (str), (len)); \
76 /* XXXX this code currently makes the assumption that a mech oid will
77 never be longer than 127 bytes. This assumption is not inherent in
78 the interfaces, so the code can be fixed if the OSI namespace
79 balloons unexpectedly. */
81 /* Each token looks like this:
83 0x60 tag for APPLICATION 0, SEQUENCE
84 (constructed, definite-length)
85 <length> possible multiple bytes, need to parse/generate
86 0x06 tag for OBJECT IDENTIFIER
87 <moid_length> compile-time constant string (assume 1 byte)
88 <moid_bytes> compile-time constant string
89 <inner_bytes> the ANY containing the application token
90 bytes 0,1 are the token type
91 bytes 2,n are the token data
93 For the purposes of this abstraction, the token "header" consists of
94 the sequence tag and length octets, the mech OID DER encoding, and the
95 first two inner bytes, which indicate the token type. The token
96 "body" consists of everything else.
101 der_length_size( int length)
105 else if (length < (1<<8))
107 #if (SIZEOF_INT == 2)
111 else if (length < (1<<16))
113 else if (length < (1<<24))
121 der_write_length(unsigned char **buf, int length)
123 if (length < (1<<7)) {
124 *(*buf)++ = (unsigned char) length;
126 *(*buf)++ = (unsigned char) (der_length_size(length)+127);
128 if (length >= (1<<24))
129 *(*buf)++ = (unsigned char) (length>>24);
130 if (length >= (1<<16))
131 *(*buf)++ = (unsigned char) ((length>>16)&0xff);
133 if (length >= (1<<8))
134 *(*buf)++ = (unsigned char) ((length>>8)&0xff);
135 *(*buf)++ = (unsigned char) (length&0xff);
139 /* returns decoded length, or < 0 on failure. Advances buf and
140 decrements bufsize */
143 der_read_length(unsigned char **buf, int *bufsize)
153 if ((sf &= 0x7f) > ((*bufsize)-1))
159 ret = (ret<<8) + (*(*buf)++);
169 /* returns the length of a token, given the mech oid and the body size */
172 g_token_size(rawobj_t *mech, unsigned int body_size)
174 /* set body_size to sequence contents size */
175 body_size += 4 + (int) mech->len; /* NEED overflow check */
176 return(1 + der_length_size(body_size) + body_size);
179 //EXPORT_SYMBOL(g_token_size);
181 /* fills in a buffer with the token header. The buffer is assumed to
182 be the right size. buf is advanced past the token header */
185 g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf)
188 der_write_length(buf, 4 + mech->len + body_size);
190 *(*buf)++ = (unsigned char) mech->len;
191 TWRITE_STR(*buf, mech->data, ((int) mech->len));
194 //EXPORT_SYMBOL(g_make_token_header);
197 * Given a buffer containing a token, reads and verifies the token,
198 * leaving buf advanced past the token header, and setting body_size
199 * to the number of remaining bytes. Returns 0 on success,
200 * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
201 * mechanism in the token does not match the mech argument. buf and
202 * *body_size are left unmodified on error.
205 g_verify_token_header(rawobj_t *mech, int *body_size,
206 unsigned char **buf_in, int toksize)
208 unsigned char *buf = *buf_in;
213 if ((toksize-=1) < 0)
214 return(G_BAD_TOK_HEADER);
216 return(G_BAD_TOK_HEADER);
218 if ((seqsize = der_read_length(&buf, &toksize)) < 0)
219 return(G_BAD_TOK_HEADER);
221 if (seqsize != toksize)
222 return(G_BAD_TOK_HEADER);
224 if ((toksize-=1) < 0)
225 return(G_BAD_TOK_HEADER);
227 return(G_BAD_TOK_HEADER);
229 if ((toksize-=1) < 0)
230 return(G_BAD_TOK_HEADER);
233 if ((toksize-=toid.len) < 0)
234 return(G_BAD_TOK_HEADER);
238 if (! g_OID_equal(&toid, mech))
241 /* G_WRONG_MECH is not returned immediately because it's more important
242 to return G_BAD_TOK_HEADER if the token header is in fact bad */
244 if ((toksize-=2) < 0)
245 return(G_BAD_TOK_HEADER);
252 *body_size = toksize;
258 //EXPORT_SYMBOL(g_verify_token_header);
260 /* Given a buffer containing a token, returns a copy of the mech oid in
261 * the parameter mech. */
263 g_get_mech_oid(rawobj_t *mech, rawobj_t * in_buf)
265 unsigned char *buf = in_buf->data;
266 int len = in_buf->len;
271 return(G_BAD_TOK_HEADER);
273 return(G_BAD_TOK_HEADER);
275 if ((seqsize = der_read_length(&buf, &len)) < 0)
276 return(G_BAD_TOK_HEADER);
279 return(G_BAD_TOK_HEADER);
281 return(G_BAD_TOK_HEADER);
284 return(G_BAD_TOK_HEADER);
287 if ((len-=mech->len) < 0)
288 return(G_BAD_TOK_HEADER);
289 OBD_ALLOC(mech->data, mech->len);
291 return(G_BUFFER_ALLOC);
292 memcpy(mech->data, buf, mech->len);