Path: blob/master/net/sunrpc/auth_gss/gss_generic_token.c
15112 views
/*1* linux/net/sunrpc/gss_generic_token.c2*3* Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c4*5* Copyright (c) 2000 The Regents of the University of Michigan.6* All rights reserved.7*8* Andy Adamson <[email protected]>9*/1011/*12* Copyright 1993 by OpenVision Technologies, Inc.13*14* Permission to use, copy, modify, distribute, and sell this software15* and its documentation for any purpose is hereby granted without fee,16* provided that the above copyright notice appears in all copies and17* that both that copyright notice and this permission notice appear in18* supporting documentation, and that the name of OpenVision not be used19* in advertising or publicity pertaining to distribution of the software20* without specific, written prior permission. OpenVision makes no21* representations about the suitability of this software for any22* purpose. It is provided "as is" without express or implied warranty.23*24* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,25* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO26* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR27* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF28* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR29* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR30* PERFORMANCE OF THIS SOFTWARE.31*/3233#include <linux/types.h>34#include <linux/module.h>35#include <linux/string.h>36#include <linux/sunrpc/sched.h>37#include <linux/sunrpc/gss_asn1.h>383940#ifdef RPC_DEBUG41# define RPCDBG_FACILITY RPCDBG_AUTH42#endif434445/* TWRITE_STR from gssapiP_generic.h */46#define TWRITE_STR(ptr, str, len) \47memcpy((ptr), (char *) (str), (len)); \48(ptr) += (len);4950/* XXXX this code currently makes the assumption that a mech oid will51never be longer than 127 bytes. This assumption is not inherent in52the interfaces, so the code can be fixed if the OSI namespace53balloons unexpectedly. */5455/* Each token looks like this:56570x60 tag for APPLICATION 0, SEQUENCE58(constructed, definite-length)59<length> possible multiple bytes, need to parse/generate600x06 tag for OBJECT IDENTIFIER61<moid_length> compile-time constant string (assume 1 byte)62<moid_bytes> compile-time constant string63<inner_bytes> the ANY containing the application token64bytes 0,1 are the token type65bytes 2,n are the token data6667For the purposes of this abstraction, the token "header" consists of68the sequence tag and length octets, the mech OID DER encoding, and the69first two inner bytes, which indicate the token type. The token70"body" consists of everything else.7172*/7374static int75der_length_size( int length)76{77if (length < (1<<7))78return 1;79else if (length < (1<<8))80return 2;81#if (SIZEOF_INT == 2)82else83return 3;84#else85else if (length < (1<<16))86return 3;87else if (length < (1<<24))88return 4;89else90return 5;91#endif92}9394static void95der_write_length(unsigned char **buf, int length)96{97if (length < (1<<7)) {98*(*buf)++ = (unsigned char) length;99} else {100*(*buf)++ = (unsigned char) (der_length_size(length)+127);101#if (SIZEOF_INT > 2)102if (length >= (1<<24))103*(*buf)++ = (unsigned char) (length>>24);104if (length >= (1<<16))105*(*buf)++ = (unsigned char) ((length>>16)&0xff);106#endif107if (length >= (1<<8))108*(*buf)++ = (unsigned char) ((length>>8)&0xff);109*(*buf)++ = (unsigned char) (length&0xff);110}111}112113/* returns decoded length, or < 0 on failure. Advances buf and114decrements bufsize */115116static int117der_read_length(unsigned char **buf, int *bufsize)118{119unsigned char sf;120int ret;121122if (*bufsize < 1)123return -1;124sf = *(*buf)++;125(*bufsize)--;126if (sf & 0x80) {127if ((sf &= 0x7f) > ((*bufsize)-1))128return -1;129if (sf > SIZEOF_INT)130return -1;131ret = 0;132for (; sf; sf--) {133ret = (ret<<8) + (*(*buf)++);134(*bufsize)--;135}136} else {137ret = sf;138}139140return ret;141}142143/* returns the length of a token, given the mech oid and the body size */144145int146g_token_size(struct xdr_netobj *mech, unsigned int body_size)147{148/* set body_size to sequence contents size */149body_size += 2 + (int) mech->len; /* NEED overflow check */150return 1 + der_length_size(body_size) + body_size;151}152153EXPORT_SYMBOL_GPL(g_token_size);154155/* fills in a buffer with the token header. The buffer is assumed to156be the right size. buf is advanced past the token header */157158void159g_make_token_header(struct xdr_netobj *mech, int body_size, unsigned char **buf)160{161*(*buf)++ = 0x60;162der_write_length(buf, 2 + mech->len + body_size);163*(*buf)++ = 0x06;164*(*buf)++ = (unsigned char) mech->len;165TWRITE_STR(*buf, mech->data, ((int) mech->len));166}167168EXPORT_SYMBOL_GPL(g_make_token_header);169170/*171* Given a buffer containing a token, reads and verifies the token,172* leaving buf advanced past the token header, and setting body_size173* to the number of remaining bytes. Returns 0 on success,174* G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the175* mechanism in the token does not match the mech argument. buf and176* *body_size are left unmodified on error.177*/178u32179g_verify_token_header(struct xdr_netobj *mech, int *body_size,180unsigned char **buf_in, int toksize)181{182unsigned char *buf = *buf_in;183int seqsize;184struct xdr_netobj toid;185int ret = 0;186187if ((toksize-=1) < 0)188return G_BAD_TOK_HEADER;189if (*buf++ != 0x60)190return G_BAD_TOK_HEADER;191192if ((seqsize = der_read_length(&buf, &toksize)) < 0)193return G_BAD_TOK_HEADER;194195if (seqsize != toksize)196return G_BAD_TOK_HEADER;197198if ((toksize-=1) < 0)199return G_BAD_TOK_HEADER;200if (*buf++ != 0x06)201return G_BAD_TOK_HEADER;202203if ((toksize-=1) < 0)204return G_BAD_TOK_HEADER;205toid.len = *buf++;206207if ((toksize-=toid.len) < 0)208return G_BAD_TOK_HEADER;209toid.data = buf;210buf+=toid.len;211212if (! g_OID_equal(&toid, mech))213ret = G_WRONG_MECH;214215/* G_WRONG_MECH is not returned immediately because it's more important216to return G_BAD_TOK_HEADER if the token header is in fact bad */217218if ((toksize-=2) < 0)219return G_BAD_TOK_HEADER;220221if (ret)222return ret;223224if (!ret) {225*buf_in = buf;226*body_size = toksize;227}228229return ret;230}231232EXPORT_SYMBOL_GPL(g_verify_token_header);233234235236