18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * linux/net/sunrpc/gss_krb5_unseal.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/k5unseal.c 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (c) 2000-2008 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/* 358c2ecf20Sopenharmony_ci * Copyright (C) 1998 by the FundsXpress, INC. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * All rights reserved. 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci * Export of this software from the United States of America may require 408c2ecf20Sopenharmony_ci * a specific license from the United States Government. It is the 418c2ecf20Sopenharmony_ci * responsibility of any person or organization contemplating export to 428c2ecf20Sopenharmony_ci * obtain such a license before exporting. 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 458c2ecf20Sopenharmony_ci * distribute this software and its documentation for any purpose and 468c2ecf20Sopenharmony_ci * without fee is hereby granted, provided that the above copyright 478c2ecf20Sopenharmony_ci * notice appear in all copies and that both that copyright notice and 488c2ecf20Sopenharmony_ci * this permission notice appear in supporting documentation, and that 498c2ecf20Sopenharmony_ci * the name of FundsXpress. not be used in advertising or publicity pertaining 508c2ecf20Sopenharmony_ci * to distribution of the software without specific, written prior 518c2ecf20Sopenharmony_ci * permission. FundsXpress makes no representations about the suitability of 528c2ecf20Sopenharmony_ci * this software for any purpose. It is provided "as is" without express 538c2ecf20Sopenharmony_ci * or implied warranty. 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 568c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 578c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#include <linux/types.h> 618c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 628c2ecf20Sopenharmony_ci#include <linux/sunrpc/gss_krb5.h> 638c2ecf20Sopenharmony_ci#include <linux/crypto.h> 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 668c2ecf20Sopenharmony_ci# define RPCDBG_FACILITY RPCDBG_AUTH 678c2ecf20Sopenharmony_ci#endif 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* read_token is a mic token, and message_buffer is the data that the mic was 718c2ecf20Sopenharmony_ci * supposedly taken over. */ 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic u32 748c2ecf20Sopenharmony_cigss_verify_mic_v1(struct krb5_ctx *ctx, 758c2ecf20Sopenharmony_ci struct xdr_buf *message_buffer, struct xdr_netobj *read_token) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci int signalg; 788c2ecf20Sopenharmony_ci int sealalg; 798c2ecf20Sopenharmony_ci char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; 808c2ecf20Sopenharmony_ci struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), 818c2ecf20Sopenharmony_ci .data = cksumdata}; 828c2ecf20Sopenharmony_ci s32 now; 838c2ecf20Sopenharmony_ci int direction; 848c2ecf20Sopenharmony_ci u32 seqnum; 858c2ecf20Sopenharmony_ci unsigned char *ptr = (unsigned char *)read_token->data; 868c2ecf20Sopenharmony_ci int bodysize; 878c2ecf20Sopenharmony_ci u8 *cksumkey; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci dprintk("RPC: krb5_read_token\n"); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (g_verify_token_header(&ctx->mech_used, &bodysize, &ptr, 928c2ecf20Sopenharmony_ci read_token->len)) 938c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if ((ptr[0] != ((KG_TOK_MIC_MSG >> 8) & 0xff)) || 968c2ecf20Sopenharmony_ci (ptr[1] != (KG_TOK_MIC_MSG & 0xff))) 978c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* XXX sanity-check bodysize?? */ 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci signalg = ptr[2] + (ptr[3] << 8); 1028c2ecf20Sopenharmony_ci if (signalg != ctx->gk5e->signalg) 1038c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci sealalg = ptr[4] + (ptr[5] << 8); 1068c2ecf20Sopenharmony_ci if (sealalg != SEAL_ALG_NONE) 1078c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if ((ptr[6] != 0xff) || (ptr[7] != 0xff)) 1108c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (ctx->gk5e->keyed_cksum) 1138c2ecf20Sopenharmony_ci cksumkey = ctx->cksum; 1148c2ecf20Sopenharmony_ci else 1158c2ecf20Sopenharmony_ci cksumkey = NULL; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (make_checksum(ctx, ptr, 8, message_buffer, 0, 1188c2ecf20Sopenharmony_ci cksumkey, KG_USAGE_SIGN, &md5cksum)) 1198c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN, 1228c2ecf20Sopenharmony_ci ctx->gk5e->cksumlength)) 1238c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci /* it got through unscathed. Make sure the context is unexpired */ 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (now > ctx->endtime) 1308c2ecf20Sopenharmony_ci return GSS_S_CONTEXT_EXPIRED; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* do sequencing checks */ 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci if (krb5_get_seq_num(ctx, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8, 1358c2ecf20Sopenharmony_ci &direction, &seqnum)) 1368c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if ((ctx->initiate && direction != 0xff) || 1398c2ecf20Sopenharmony_ci (!ctx->initiate && direction != 0)) 1408c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return GSS_S_COMPLETE; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic u32 1468c2ecf20Sopenharmony_cigss_verify_mic_v2(struct krb5_ctx *ctx, 1478c2ecf20Sopenharmony_ci struct xdr_buf *message_buffer, struct xdr_netobj *read_token) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; 1508c2ecf20Sopenharmony_ci struct xdr_netobj cksumobj = {.len = sizeof(cksumdata), 1518c2ecf20Sopenharmony_ci .data = cksumdata}; 1528c2ecf20Sopenharmony_ci time64_t now; 1538c2ecf20Sopenharmony_ci u8 *ptr = read_token->data; 1548c2ecf20Sopenharmony_ci u8 *cksumkey; 1558c2ecf20Sopenharmony_ci u8 flags; 1568c2ecf20Sopenharmony_ci int i; 1578c2ecf20Sopenharmony_ci unsigned int cksum_usage; 1588c2ecf20Sopenharmony_ci __be16 be16_ptr; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci dprintk("RPC: %s\n", __func__); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci memcpy(&be16_ptr, (char *) ptr, 2); 1638c2ecf20Sopenharmony_ci if (be16_to_cpu(be16_ptr) != KG2_TOK_MIC) 1648c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci flags = ptr[2]; 1678c2ecf20Sopenharmony_ci if ((!ctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) || 1688c2ecf20Sopenharmony_ci (ctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR))) 1698c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (flags & KG2_TOKEN_FLAG_SEALED) { 1728c2ecf20Sopenharmony_ci dprintk("%s: token has unexpected sealed flag\n", __func__); 1738c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci for (i = 3; i < 8; i++) 1778c2ecf20Sopenharmony_ci if (ptr[i] != 0xff) 1788c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci if (ctx->initiate) { 1818c2ecf20Sopenharmony_ci cksumkey = ctx->acceptor_sign; 1828c2ecf20Sopenharmony_ci cksum_usage = KG_USAGE_ACCEPTOR_SIGN; 1838c2ecf20Sopenharmony_ci } else { 1848c2ecf20Sopenharmony_ci cksumkey = ctx->initiator_sign; 1858c2ecf20Sopenharmony_ci cksum_usage = KG_USAGE_INITIATOR_SIGN; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci if (make_checksum_v2(ctx, ptr, GSS_KRB5_TOK_HDR_LEN, message_buffer, 0, 1898c2ecf20Sopenharmony_ci cksumkey, cksum_usage, &cksumobj)) 1908c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (memcmp(cksumobj.data, ptr + GSS_KRB5_TOK_HDR_LEN, 1938c2ecf20Sopenharmony_ci ctx->gk5e->cksumlength)) 1948c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci /* it got through unscathed. Make sure the context is unexpired */ 1978c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 1988c2ecf20Sopenharmony_ci if (now > ctx->endtime) 1998c2ecf20Sopenharmony_ci return GSS_S_CONTEXT_EXPIRED; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci /* 2028c2ecf20Sopenharmony_ci * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss 2038c2ecf20Sopenharmony_ci * doesn't want it checked; see page 6 of rfc 2203. 2048c2ecf20Sopenharmony_ci */ 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return GSS_S_COMPLETE; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ciu32 2108c2ecf20Sopenharmony_cigss_verify_mic_kerberos(struct gss_ctx *gss_ctx, 2118c2ecf20Sopenharmony_ci struct xdr_buf *message_buffer, 2128c2ecf20Sopenharmony_ci struct xdr_netobj *read_token) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci struct krb5_ctx *ctx = gss_ctx->internal_ctx_id; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci switch (ctx->enctype) { 2178c2ecf20Sopenharmony_ci default: 2188c2ecf20Sopenharmony_ci BUG(); 2198c2ecf20Sopenharmony_ci case ENCTYPE_DES_CBC_RAW: 2208c2ecf20Sopenharmony_ci case ENCTYPE_DES3_CBC_RAW: 2218c2ecf20Sopenharmony_ci return gss_verify_mic_v1(ctx, message_buffer, read_token); 2228c2ecf20Sopenharmony_ci case ENCTYPE_AES128_CTS_HMAC_SHA1_96: 2238c2ecf20Sopenharmony_ci case ENCTYPE_AES256_CTS_HMAC_SHA1_96: 2248c2ecf20Sopenharmony_ci return gss_verify_mic_v2(ctx, message_buffer, read_token); 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci} 227