162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * linux/net/sunrpc/gss_generic_token.c 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright (c) 2000 The Regents of the University of Michigan. 762306a36Sopenharmony_ci * All rights reserved. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Andy Adamson <andros@umich.edu> 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci/* 1362306a36Sopenharmony_ci * Copyright 1993 by OpenVision Technologies, Inc. 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software 1662306a36Sopenharmony_ci * and its documentation for any purpose is hereby granted without fee, 1762306a36Sopenharmony_ci * provided that the above copyright notice appears in all copies and 1862306a36Sopenharmony_ci * that both that copyright notice and this permission notice appear in 1962306a36Sopenharmony_ci * supporting documentation, and that the name of OpenVision not be used 2062306a36Sopenharmony_ci * in advertising or publicity pertaining to distribution of the software 2162306a36Sopenharmony_ci * without specific, written prior permission. OpenVision makes no 2262306a36Sopenharmony_ci * representations about the suitability of this software for any 2362306a36Sopenharmony_ci * purpose. It is provided "as is" without express or implied warranty. 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 2662306a36Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 2762306a36Sopenharmony_ci * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR 2862306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 2962306a36Sopenharmony_ci * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 3062306a36Sopenharmony_ci * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 3162306a36Sopenharmony_ci * PERFORMANCE OF THIS SOFTWARE. 3262306a36Sopenharmony_ci */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#include <linux/types.h> 3562306a36Sopenharmony_ci#include <linux/module.h> 3662306a36Sopenharmony_ci#include <linux/string.h> 3762306a36Sopenharmony_ci#include <linux/sunrpc/sched.h> 3862306a36Sopenharmony_ci#include <linux/sunrpc/gss_asn1.h> 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 4262306a36Sopenharmony_ci# define RPCDBG_FACILITY RPCDBG_AUTH 4362306a36Sopenharmony_ci#endif 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci/* TWRITE_STR from gssapiP_generic.h */ 4762306a36Sopenharmony_ci#define TWRITE_STR(ptr, str, len) \ 4862306a36Sopenharmony_ci memcpy((ptr), (char *) (str), (len)); \ 4962306a36Sopenharmony_ci (ptr) += (len); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci/* XXXX this code currently makes the assumption that a mech oid will 5262306a36Sopenharmony_ci never be longer than 127 bytes. This assumption is not inherent in 5362306a36Sopenharmony_ci the interfaces, so the code can be fixed if the OSI namespace 5462306a36Sopenharmony_ci balloons unexpectedly. */ 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci/* Each token looks like this: 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci0x60 tag for APPLICATION 0, SEQUENCE 5962306a36Sopenharmony_ci (constructed, definite-length) 6062306a36Sopenharmony_ci <length> possible multiple bytes, need to parse/generate 6162306a36Sopenharmony_ci 0x06 tag for OBJECT IDENTIFIER 6262306a36Sopenharmony_ci <moid_length> compile-time constant string (assume 1 byte) 6362306a36Sopenharmony_ci <moid_bytes> compile-time constant string 6462306a36Sopenharmony_ci <inner_bytes> the ANY containing the application token 6562306a36Sopenharmony_ci bytes 0,1 are the token type 6662306a36Sopenharmony_ci bytes 2,n are the token data 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciFor the purposes of this abstraction, the token "header" consists of 6962306a36Sopenharmony_cithe sequence tag and length octets, the mech OID DER encoding, and the 7062306a36Sopenharmony_cifirst two inner bytes, which indicate the token type. The token 7162306a36Sopenharmony_ci"body" consists of everything else. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci*/ 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic int 7662306a36Sopenharmony_cider_length_size( int length) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci if (length < (1<<7)) 7962306a36Sopenharmony_ci return 1; 8062306a36Sopenharmony_ci else if (length < (1<<8)) 8162306a36Sopenharmony_ci return 2; 8262306a36Sopenharmony_ci#if (SIZEOF_INT == 2) 8362306a36Sopenharmony_ci else 8462306a36Sopenharmony_ci return 3; 8562306a36Sopenharmony_ci#else 8662306a36Sopenharmony_ci else if (length < (1<<16)) 8762306a36Sopenharmony_ci return 3; 8862306a36Sopenharmony_ci else if (length < (1<<24)) 8962306a36Sopenharmony_ci return 4; 9062306a36Sopenharmony_ci else 9162306a36Sopenharmony_ci return 5; 9262306a36Sopenharmony_ci#endif 9362306a36Sopenharmony_ci} 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_cistatic void 9662306a36Sopenharmony_cider_write_length(unsigned char **buf, int length) 9762306a36Sopenharmony_ci{ 9862306a36Sopenharmony_ci if (length < (1<<7)) { 9962306a36Sopenharmony_ci *(*buf)++ = (unsigned char) length; 10062306a36Sopenharmony_ci } else { 10162306a36Sopenharmony_ci *(*buf)++ = (unsigned char) (der_length_size(length)+127); 10262306a36Sopenharmony_ci#if (SIZEOF_INT > 2) 10362306a36Sopenharmony_ci if (length >= (1<<24)) 10462306a36Sopenharmony_ci *(*buf)++ = (unsigned char) (length>>24); 10562306a36Sopenharmony_ci if (length >= (1<<16)) 10662306a36Sopenharmony_ci *(*buf)++ = (unsigned char) ((length>>16)&0xff); 10762306a36Sopenharmony_ci#endif 10862306a36Sopenharmony_ci if (length >= (1<<8)) 10962306a36Sopenharmony_ci *(*buf)++ = (unsigned char) ((length>>8)&0xff); 11062306a36Sopenharmony_ci *(*buf)++ = (unsigned char) (length&0xff); 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci/* returns decoded length, or < 0 on failure. Advances buf and 11562306a36Sopenharmony_ci decrements bufsize */ 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_cistatic int 11862306a36Sopenharmony_cider_read_length(unsigned char **buf, int *bufsize) 11962306a36Sopenharmony_ci{ 12062306a36Sopenharmony_ci unsigned char sf; 12162306a36Sopenharmony_ci int ret; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci if (*bufsize < 1) 12462306a36Sopenharmony_ci return -1; 12562306a36Sopenharmony_ci sf = *(*buf)++; 12662306a36Sopenharmony_ci (*bufsize)--; 12762306a36Sopenharmony_ci if (sf & 0x80) { 12862306a36Sopenharmony_ci if ((sf &= 0x7f) > ((*bufsize)-1)) 12962306a36Sopenharmony_ci return -1; 13062306a36Sopenharmony_ci if (sf > SIZEOF_INT) 13162306a36Sopenharmony_ci return -1; 13262306a36Sopenharmony_ci ret = 0; 13362306a36Sopenharmony_ci for (; sf; sf--) { 13462306a36Sopenharmony_ci ret = (ret<<8) + (*(*buf)++); 13562306a36Sopenharmony_ci (*bufsize)--; 13662306a36Sopenharmony_ci } 13762306a36Sopenharmony_ci } else { 13862306a36Sopenharmony_ci ret = sf; 13962306a36Sopenharmony_ci } 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci return ret; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/* returns the length of a token, given the mech oid and the body size */ 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ciint 14762306a36Sopenharmony_cig_token_size(struct xdr_netobj *mech, unsigned int body_size) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci /* set body_size to sequence contents size */ 15062306a36Sopenharmony_ci body_size += 2 + (int) mech->len; /* NEED overflow check */ 15162306a36Sopenharmony_ci return 1 + der_length_size(body_size) + body_size; 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(g_token_size); 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci/* fills in a buffer with the token header. The buffer is assumed to 15762306a36Sopenharmony_ci be the right size. buf is advanced past the token header */ 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_civoid 16062306a36Sopenharmony_cig_make_token_header(struct xdr_netobj *mech, int body_size, unsigned char **buf) 16162306a36Sopenharmony_ci{ 16262306a36Sopenharmony_ci *(*buf)++ = 0x60; 16362306a36Sopenharmony_ci der_write_length(buf, 2 + mech->len + body_size); 16462306a36Sopenharmony_ci *(*buf)++ = 0x06; 16562306a36Sopenharmony_ci *(*buf)++ = (unsigned char) mech->len; 16662306a36Sopenharmony_ci TWRITE_STR(*buf, mech->data, ((int) mech->len)); 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(g_make_token_header); 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci/* 17262306a36Sopenharmony_ci * Given a buffer containing a token, reads and verifies the token, 17362306a36Sopenharmony_ci * leaving buf advanced past the token header, and setting body_size 17462306a36Sopenharmony_ci * to the number of remaining bytes. Returns 0 on success, 17562306a36Sopenharmony_ci * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the 17662306a36Sopenharmony_ci * mechanism in the token does not match the mech argument. buf and 17762306a36Sopenharmony_ci * *body_size are left unmodified on error. 17862306a36Sopenharmony_ci */ 17962306a36Sopenharmony_ciu32 18062306a36Sopenharmony_cig_verify_token_header(struct xdr_netobj *mech, int *body_size, 18162306a36Sopenharmony_ci unsigned char **buf_in, int toksize) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci unsigned char *buf = *buf_in; 18462306a36Sopenharmony_ci int seqsize; 18562306a36Sopenharmony_ci struct xdr_netobj toid; 18662306a36Sopenharmony_ci int ret = 0; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci if ((toksize-=1) < 0) 18962306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 19062306a36Sopenharmony_ci if (*buf++ != 0x60) 19162306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if ((seqsize = der_read_length(&buf, &toksize)) < 0) 19462306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci if (seqsize != toksize) 19762306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci if ((toksize-=1) < 0) 20062306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 20162306a36Sopenharmony_ci if (*buf++ != 0x06) 20262306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci if ((toksize-=1) < 0) 20562306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 20662306a36Sopenharmony_ci toid.len = *buf++; 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci if ((toksize-=toid.len) < 0) 20962306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 21062306a36Sopenharmony_ci toid.data = buf; 21162306a36Sopenharmony_ci buf+=toid.len; 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci if (! g_OID_equal(&toid, mech)) 21462306a36Sopenharmony_ci ret = G_WRONG_MECH; 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci /* G_WRONG_MECH is not returned immediately because it's more important 21762306a36Sopenharmony_ci to return G_BAD_TOK_HEADER if the token header is in fact bad */ 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci if ((toksize-=2) < 0) 22062306a36Sopenharmony_ci return G_BAD_TOK_HEADER; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci if (ret) 22362306a36Sopenharmony_ci return ret; 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci *buf_in = buf; 22662306a36Sopenharmony_ci *body_size = toksize; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci return ret; 22962306a36Sopenharmony_ci} 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(g_verify_token_header); 232