18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * linux/net/sunrpc/gss_generic_token.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/generic/util_token.c 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (c) 2000 The Regents of the University of Michigan. 78c2ecf20Sopenharmony_ci * All rights reserved. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Andy Adamson <andros@umich.edu> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/* 138c2ecf20Sopenharmony_ci * Copyright 1993 by OpenVision Technologies, Inc. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software 168c2ecf20Sopenharmony_ci * and its documentation for any purpose is hereby granted without fee, 178c2ecf20Sopenharmony_ci * provided that the above copyright notice appears in all copies and 188c2ecf20Sopenharmony_ci * that both that copyright notice and this permission notice appear in 198c2ecf20Sopenharmony_ci * supporting documentation, and that the name of OpenVision not be used 208c2ecf20Sopenharmony_ci * in advertising or publicity pertaining to distribution of the software 218c2ecf20Sopenharmony_ci * without specific, written prior permission. OpenVision makes no 228c2ecf20Sopenharmony_ci * representations about the suitability of this software for any 238c2ecf20Sopenharmony_ci * purpose. It is provided "as is" without express or implied warranty. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 268c2ecf20Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 278c2ecf20Sopenharmony_ci * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR 288c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 298c2ecf20Sopenharmony_ci * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 308c2ecf20Sopenharmony_ci * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 318c2ecf20Sopenharmony_ci * PERFORMANCE OF THIS SOFTWARE. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#include <linux/types.h> 358c2ecf20Sopenharmony_ci#include <linux/module.h> 368c2ecf20Sopenharmony_ci#include <linux/string.h> 378c2ecf20Sopenharmony_ci#include <linux/sunrpc/sched.h> 388c2ecf20Sopenharmony_ci#include <linux/sunrpc/gss_asn1.h> 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 428c2ecf20Sopenharmony_ci# define RPCDBG_FACILITY RPCDBG_AUTH 438c2ecf20Sopenharmony_ci#endif 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* TWRITE_STR from gssapiP_generic.h */ 478c2ecf20Sopenharmony_ci#define TWRITE_STR(ptr, str, len) \ 488c2ecf20Sopenharmony_ci memcpy((ptr), (char *) (str), (len)); \ 498c2ecf20Sopenharmony_ci (ptr) += (len); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* XXXX this code currently makes the assumption that a mech oid will 528c2ecf20Sopenharmony_ci never be longer than 127 bytes. This assumption is not inherent in 538c2ecf20Sopenharmony_ci the interfaces, so the code can be fixed if the OSI namespace 548c2ecf20Sopenharmony_ci balloons unexpectedly. */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* Each token looks like this: 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci0x60 tag for APPLICATION 0, SEQUENCE 598c2ecf20Sopenharmony_ci (constructed, definite-length) 608c2ecf20Sopenharmony_ci <length> possible multiple bytes, need to parse/generate 618c2ecf20Sopenharmony_ci 0x06 tag for OBJECT IDENTIFIER 628c2ecf20Sopenharmony_ci <moid_length> compile-time constant string (assume 1 byte) 638c2ecf20Sopenharmony_ci <moid_bytes> compile-time constant string 648c2ecf20Sopenharmony_ci <inner_bytes> the ANY containing the application token 658c2ecf20Sopenharmony_ci bytes 0,1 are the token type 668c2ecf20Sopenharmony_ci bytes 2,n are the token data 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciFor the purposes of this abstraction, the token "header" consists of 698c2ecf20Sopenharmony_cithe sequence tag and length octets, the mech OID DER encoding, and the 708c2ecf20Sopenharmony_cifirst two inner bytes, which indicate the token type. The token 718c2ecf20Sopenharmony_ci"body" consists of everything else. 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci*/ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic int 768c2ecf20Sopenharmony_cider_length_size( int length) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci if (length < (1<<7)) 798c2ecf20Sopenharmony_ci return 1; 808c2ecf20Sopenharmony_ci else if (length < (1<<8)) 818c2ecf20Sopenharmony_ci return 2; 828c2ecf20Sopenharmony_ci#if (SIZEOF_INT == 2) 838c2ecf20Sopenharmony_ci else 848c2ecf20Sopenharmony_ci return 3; 858c2ecf20Sopenharmony_ci#else 868c2ecf20Sopenharmony_ci else if (length < (1<<16)) 878c2ecf20Sopenharmony_ci return 3; 888c2ecf20Sopenharmony_ci else if (length < (1<<24)) 898c2ecf20Sopenharmony_ci return 4; 908c2ecf20Sopenharmony_ci else 918c2ecf20Sopenharmony_ci return 5; 928c2ecf20Sopenharmony_ci#endif 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic void 968c2ecf20Sopenharmony_cider_write_length(unsigned char **buf, int length) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci if (length < (1<<7)) { 998c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) length; 1008c2ecf20Sopenharmony_ci } else { 1018c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) (der_length_size(length)+127); 1028c2ecf20Sopenharmony_ci#if (SIZEOF_INT > 2) 1038c2ecf20Sopenharmony_ci if (length >= (1<<24)) 1048c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) (length>>24); 1058c2ecf20Sopenharmony_ci if (length >= (1<<16)) 1068c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) ((length>>16)&0xff); 1078c2ecf20Sopenharmony_ci#endif 1088c2ecf20Sopenharmony_ci if (length >= (1<<8)) 1098c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) ((length>>8)&0xff); 1108c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) (length&0xff); 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/* returns decoded length, or < 0 on failure. Advances buf and 1158c2ecf20Sopenharmony_ci decrements bufsize */ 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int 1188c2ecf20Sopenharmony_cider_read_length(unsigned char **buf, int *bufsize) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci unsigned char sf; 1218c2ecf20Sopenharmony_ci int ret; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci if (*bufsize < 1) 1248c2ecf20Sopenharmony_ci return -1; 1258c2ecf20Sopenharmony_ci sf = *(*buf)++; 1268c2ecf20Sopenharmony_ci (*bufsize)--; 1278c2ecf20Sopenharmony_ci if (sf & 0x80) { 1288c2ecf20Sopenharmony_ci if ((sf &= 0x7f) > ((*bufsize)-1)) 1298c2ecf20Sopenharmony_ci return -1; 1308c2ecf20Sopenharmony_ci if (sf > SIZEOF_INT) 1318c2ecf20Sopenharmony_ci return -1; 1328c2ecf20Sopenharmony_ci ret = 0; 1338c2ecf20Sopenharmony_ci for (; sf; sf--) { 1348c2ecf20Sopenharmony_ci ret = (ret<<8) + (*(*buf)++); 1358c2ecf20Sopenharmony_ci (*bufsize)--; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci } else { 1388c2ecf20Sopenharmony_ci ret = sf; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return ret; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/* returns the length of a token, given the mech oid and the body size */ 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ciint 1478c2ecf20Sopenharmony_cig_token_size(struct xdr_netobj *mech, unsigned int body_size) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci /* set body_size to sequence contents size */ 1508c2ecf20Sopenharmony_ci body_size += 2 + (int) mech->len; /* NEED overflow check */ 1518c2ecf20Sopenharmony_ci return 1 + der_length_size(body_size) + body_size; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(g_token_size); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci/* fills in a buffer with the token header. The buffer is assumed to 1578c2ecf20Sopenharmony_ci be the right size. buf is advanced past the token header */ 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_civoid 1608c2ecf20Sopenharmony_cig_make_token_header(struct xdr_netobj *mech, int body_size, unsigned char **buf) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci *(*buf)++ = 0x60; 1638c2ecf20Sopenharmony_ci der_write_length(buf, 2 + mech->len + body_size); 1648c2ecf20Sopenharmony_ci *(*buf)++ = 0x06; 1658c2ecf20Sopenharmony_ci *(*buf)++ = (unsigned char) mech->len; 1668c2ecf20Sopenharmony_ci TWRITE_STR(*buf, mech->data, ((int) mech->len)); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(g_make_token_header); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * Given a buffer containing a token, reads and verifies the token, 1738c2ecf20Sopenharmony_ci * leaving buf advanced past the token header, and setting body_size 1748c2ecf20Sopenharmony_ci * to the number of remaining bytes. Returns 0 on success, 1758c2ecf20Sopenharmony_ci * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the 1768c2ecf20Sopenharmony_ci * mechanism in the token does not match the mech argument. buf and 1778c2ecf20Sopenharmony_ci * *body_size are left unmodified on error. 1788c2ecf20Sopenharmony_ci */ 1798c2ecf20Sopenharmony_ciu32 1808c2ecf20Sopenharmony_cig_verify_token_header(struct xdr_netobj *mech, int *body_size, 1818c2ecf20Sopenharmony_ci unsigned char **buf_in, int toksize) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci unsigned char *buf = *buf_in; 1848c2ecf20Sopenharmony_ci int seqsize; 1858c2ecf20Sopenharmony_ci struct xdr_netobj toid; 1868c2ecf20Sopenharmony_ci int ret = 0; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci if ((toksize-=1) < 0) 1898c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 1908c2ecf20Sopenharmony_ci if (*buf++ != 0x60) 1918c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci if ((seqsize = der_read_length(&buf, &toksize)) < 0) 1948c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci if (seqsize != toksize) 1978c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci if ((toksize-=1) < 0) 2008c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 2018c2ecf20Sopenharmony_ci if (*buf++ != 0x06) 2028c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci if ((toksize-=1) < 0) 2058c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 2068c2ecf20Sopenharmony_ci toid.len = *buf++; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if ((toksize-=toid.len) < 0) 2098c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 2108c2ecf20Sopenharmony_ci toid.data = buf; 2118c2ecf20Sopenharmony_ci buf+=toid.len; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (! g_OID_equal(&toid, mech)) 2148c2ecf20Sopenharmony_ci ret = G_WRONG_MECH; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* G_WRONG_MECH is not returned immediately because it's more important 2178c2ecf20Sopenharmony_ci to return G_BAD_TOK_HEADER if the token header is in fact bad */ 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if ((toksize-=2) < 0) 2208c2ecf20Sopenharmony_ci return G_BAD_TOK_HEADER; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci if (ret) 2238c2ecf20Sopenharmony_ci return ret; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (!ret) { 2268c2ecf20Sopenharmony_ci *buf_in = buf; 2278c2ecf20Sopenharmony_ci *body_size = toksize; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci return ret; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(g_verify_token_header); 234