18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * COPYRIGHT (c) 2008 38c2ecf20Sopenharmony_ci * The Regents of the University of Michigan 48c2ecf20Sopenharmony_ci * ALL RIGHTS RESERVED 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Permission is granted to use, copy, create derivative works 78c2ecf20Sopenharmony_ci * and redistribute this software and such derivative works 88c2ecf20Sopenharmony_ci * for any purpose, so long as the name of The University of 98c2ecf20Sopenharmony_ci * Michigan is not used in any advertising or publicity 108c2ecf20Sopenharmony_ci * pertaining to the use of distribution of this software 118c2ecf20Sopenharmony_ci * without specific, written prior authorization. If the 128c2ecf20Sopenharmony_ci * above copyright notice or any other identification of the 138c2ecf20Sopenharmony_ci * University of Michigan is included in any copy of any 148c2ecf20Sopenharmony_ci * portion of this software, then the disclaimer below must 158c2ecf20Sopenharmony_ci * also be included. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION 188c2ecf20Sopenharmony_ci * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY 198c2ecf20Sopenharmony_ci * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF 208c2ecf20Sopenharmony_ci * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING 218c2ecf20Sopenharmony_ci * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF 228c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE 238c2ecf20Sopenharmony_ci * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE 248c2ecf20Sopenharmony_ci * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR 258c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING 268c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN 278c2ecf20Sopenharmony_ci * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF 288c2ecf20Sopenharmony_ci * SUCH DAMAGES. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include <crypto/skcipher.h> 328c2ecf20Sopenharmony_ci#include <linux/types.h> 338c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 348c2ecf20Sopenharmony_ci#include <linux/sunrpc/gss_krb5.h> 358c2ecf20Sopenharmony_ci#include <linux/random.h> 368c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 398c2ecf20Sopenharmony_ci# define RPCDBG_FACILITY RPCDBG_AUTH 408c2ecf20Sopenharmony_ci#endif 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic inline int 438c2ecf20Sopenharmony_cigss_krb5_padding(int blocksize, int length) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci return blocksize - (length % blocksize); 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic inline void 498c2ecf20Sopenharmony_cigss_krb5_add_padding(struct xdr_buf *buf, int offset, int blocksize) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci int padding = gss_krb5_padding(blocksize, buf->len - offset); 528c2ecf20Sopenharmony_ci char *p; 538c2ecf20Sopenharmony_ci struct kvec *iov; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (buf->page_len || buf->tail[0].iov_len) 568c2ecf20Sopenharmony_ci iov = &buf->tail[0]; 578c2ecf20Sopenharmony_ci else 588c2ecf20Sopenharmony_ci iov = &buf->head[0]; 598c2ecf20Sopenharmony_ci p = iov->iov_base + iov->iov_len; 608c2ecf20Sopenharmony_ci iov->iov_len += padding; 618c2ecf20Sopenharmony_ci buf->len += padding; 628c2ecf20Sopenharmony_ci memset(p, padding, padding); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic inline int 668c2ecf20Sopenharmony_cigss_krb5_remove_padding(struct xdr_buf *buf, int blocksize) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci u8 *ptr; 698c2ecf20Sopenharmony_ci u8 pad; 708c2ecf20Sopenharmony_ci size_t len = buf->len; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (len <= buf->head[0].iov_len) { 738c2ecf20Sopenharmony_ci pad = *(u8 *)(buf->head[0].iov_base + len - 1); 748c2ecf20Sopenharmony_ci if (pad > buf->head[0].iov_len) 758c2ecf20Sopenharmony_ci return -EINVAL; 768c2ecf20Sopenharmony_ci buf->head[0].iov_len -= pad; 778c2ecf20Sopenharmony_ci goto out; 788c2ecf20Sopenharmony_ci } else 798c2ecf20Sopenharmony_ci len -= buf->head[0].iov_len; 808c2ecf20Sopenharmony_ci if (len <= buf->page_len) { 818c2ecf20Sopenharmony_ci unsigned int last = (buf->page_base + len - 1) 828c2ecf20Sopenharmony_ci >>PAGE_SHIFT; 838c2ecf20Sopenharmony_ci unsigned int offset = (buf->page_base + len - 1) 848c2ecf20Sopenharmony_ci & (PAGE_SIZE - 1); 858c2ecf20Sopenharmony_ci ptr = kmap_atomic(buf->pages[last]); 868c2ecf20Sopenharmony_ci pad = *(ptr + offset); 878c2ecf20Sopenharmony_ci kunmap_atomic(ptr); 888c2ecf20Sopenharmony_ci goto out; 898c2ecf20Sopenharmony_ci } else 908c2ecf20Sopenharmony_ci len -= buf->page_len; 918c2ecf20Sopenharmony_ci BUG_ON(len > buf->tail[0].iov_len); 928c2ecf20Sopenharmony_ci pad = *(u8 *)(buf->tail[0].iov_base + len - 1); 938c2ecf20Sopenharmony_ciout: 948c2ecf20Sopenharmony_ci /* XXX: NOTE: we do not adjust the page lengths--they represent 958c2ecf20Sopenharmony_ci * a range of data in the real filesystem page cache, and we need 968c2ecf20Sopenharmony_ci * to know that range so the xdr code can properly place read data. 978c2ecf20Sopenharmony_ci * However adjusting the head length, as we do above, is harmless. 988c2ecf20Sopenharmony_ci * In the case of a request that fits into a single page, the server 998c2ecf20Sopenharmony_ci * also uses length and head length together to determine the original 1008c2ecf20Sopenharmony_ci * start of the request to copy the request for deferal; so it's 1018c2ecf20Sopenharmony_ci * easier on the server if we adjust head and tail length in tandem. 1028c2ecf20Sopenharmony_ci * It's not really a problem that we don't fool with the page and 1038c2ecf20Sopenharmony_ci * tail lengths, though--at worst badly formed xdr might lead the 1048c2ecf20Sopenharmony_ci * server to attempt to parse the padding. 1058c2ecf20Sopenharmony_ci * XXX: Document all these weird requirements for gss mechanism 1068c2ecf20Sopenharmony_ci * wrap/unwrap functions. */ 1078c2ecf20Sopenharmony_ci if (pad > blocksize) 1088c2ecf20Sopenharmony_ci return -EINVAL; 1098c2ecf20Sopenharmony_ci if (buf->len > pad) 1108c2ecf20Sopenharmony_ci buf->len -= pad; 1118c2ecf20Sopenharmony_ci else 1128c2ecf20Sopenharmony_ci return -EINVAL; 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_civoid 1178c2ecf20Sopenharmony_cigss_krb5_make_confounder(char *p, u32 conflen) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci static u64 i = 0; 1208c2ecf20Sopenharmony_ci u64 *q = (u64 *)p; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* rfc1964 claims this should be "random". But all that's really 1238c2ecf20Sopenharmony_ci * necessary is that it be unique. And not even that is necessary in 1248c2ecf20Sopenharmony_ci * our case since our "gssapi" implementation exists only to support 1258c2ecf20Sopenharmony_ci * rpcsec_gss, so we know that the only buffers we will ever encrypt 1268c2ecf20Sopenharmony_ci * already begin with a unique sequence number. Just to hedge my bets 1278c2ecf20Sopenharmony_ci * I'll make a half-hearted attempt at something unique, but ensuring 1288c2ecf20Sopenharmony_ci * uniqueness would mean worrying about atomicity and rollover, and I 1298c2ecf20Sopenharmony_ci * don't care enough. */ 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* initialize to random value */ 1328c2ecf20Sopenharmony_ci if (i == 0) { 1338c2ecf20Sopenharmony_ci i = prandom_u32(); 1348c2ecf20Sopenharmony_ci i = (i << 32) | prandom_u32(); 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci switch (conflen) { 1388c2ecf20Sopenharmony_ci case 16: 1398c2ecf20Sopenharmony_ci *q++ = i++; 1408c2ecf20Sopenharmony_ci fallthrough; 1418c2ecf20Sopenharmony_ci case 8: 1428c2ecf20Sopenharmony_ci *q++ = i++; 1438c2ecf20Sopenharmony_ci break; 1448c2ecf20Sopenharmony_ci default: 1458c2ecf20Sopenharmony_ci BUG(); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci/* Assumptions: the head and tail of inbuf are ours to play with. 1508c2ecf20Sopenharmony_ci * The pages, however, may be real pages in the page cache and we replace 1518c2ecf20Sopenharmony_ci * them with scratch pages from **pages before writing to them. */ 1528c2ecf20Sopenharmony_ci/* XXX: obviously the above should be documentation of wrap interface, 1538c2ecf20Sopenharmony_ci * and shouldn't be in this kerberos-specific file. */ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/* XXX factor out common code with seal/unseal. */ 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic u32 1588c2ecf20Sopenharmony_cigss_wrap_kerberos_v1(struct krb5_ctx *kctx, int offset, 1598c2ecf20Sopenharmony_ci struct xdr_buf *buf, struct page **pages) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; 1628c2ecf20Sopenharmony_ci struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), 1638c2ecf20Sopenharmony_ci .data = cksumdata}; 1648c2ecf20Sopenharmony_ci int blocksize = 0, plainlen; 1658c2ecf20Sopenharmony_ci unsigned char *ptr, *msg_start; 1668c2ecf20Sopenharmony_ci time64_t now; 1678c2ecf20Sopenharmony_ci int headlen; 1688c2ecf20Sopenharmony_ci struct page **tmp_pages; 1698c2ecf20Sopenharmony_ci u32 seq_send; 1708c2ecf20Sopenharmony_ci u8 *cksumkey; 1718c2ecf20Sopenharmony_ci u32 conflen = kctx->gk5e->conflen; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci dprintk("RPC: %s\n", __func__); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci blocksize = crypto_sync_skcipher_blocksize(kctx->enc); 1788c2ecf20Sopenharmony_ci gss_krb5_add_padding(buf, offset, blocksize); 1798c2ecf20Sopenharmony_ci BUG_ON((buf->len - offset) % blocksize); 1808c2ecf20Sopenharmony_ci plainlen = conflen + buf->len - offset; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci headlen = g_token_size(&kctx->mech_used, 1838c2ecf20Sopenharmony_ci GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength + plainlen) - 1848c2ecf20Sopenharmony_ci (buf->len - offset); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci ptr = buf->head[0].iov_base + offset; 1878c2ecf20Sopenharmony_ci /* shift data to make room for header. */ 1888c2ecf20Sopenharmony_ci xdr_extend_head(buf, offset, headlen); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* XXX Would be cleverer to encrypt while copying. */ 1918c2ecf20Sopenharmony_ci BUG_ON((buf->len - offset - headlen) % blocksize); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci g_make_token_header(&kctx->mech_used, 1948c2ecf20Sopenharmony_ci GSS_KRB5_TOK_HDR_LEN + 1958c2ecf20Sopenharmony_ci kctx->gk5e->cksumlength + plainlen, &ptr); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci /* ptr now at header described in rfc 1964, section 1.2.1: */ 1998c2ecf20Sopenharmony_ci ptr[0] = (unsigned char) ((KG_TOK_WRAP_MSG >> 8) & 0xff); 2008c2ecf20Sopenharmony_ci ptr[1] = (unsigned char) (KG_TOK_WRAP_MSG & 0xff); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci msg_start = ptr + GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* 2058c2ecf20Sopenharmony_ci * signalg and sealalg are stored as if they were converted from LE 2068c2ecf20Sopenharmony_ci * to host endian, even though they're opaque pairs of bytes according 2078c2ecf20Sopenharmony_ci * to the RFC. 2088c2ecf20Sopenharmony_ci */ 2098c2ecf20Sopenharmony_ci *(__le16 *)(ptr + 2) = cpu_to_le16(kctx->gk5e->signalg); 2108c2ecf20Sopenharmony_ci *(__le16 *)(ptr + 4) = cpu_to_le16(kctx->gk5e->sealalg); 2118c2ecf20Sopenharmony_ci ptr[6] = 0xff; 2128c2ecf20Sopenharmony_ci ptr[7] = 0xff; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci gss_krb5_make_confounder(msg_start, conflen); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci if (kctx->gk5e->keyed_cksum) 2178c2ecf20Sopenharmony_ci cksumkey = kctx->cksum; 2188c2ecf20Sopenharmony_ci else 2198c2ecf20Sopenharmony_ci cksumkey = NULL; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* XXXJBF: UGH!: */ 2228c2ecf20Sopenharmony_ci tmp_pages = buf->pages; 2238c2ecf20Sopenharmony_ci buf->pages = pages; 2248c2ecf20Sopenharmony_ci if (make_checksum(kctx, ptr, 8, buf, offset + headlen - conflen, 2258c2ecf20Sopenharmony_ci cksumkey, KG_USAGE_SEAL, &md5cksum)) 2268c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 2278c2ecf20Sopenharmony_ci buf->pages = tmp_pages; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci memcpy(ptr + GSS_KRB5_TOK_HDR_LEN, md5cksum.data, md5cksum.len); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci seq_send = atomic_fetch_inc(&kctx->seq_send); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci /* XXX would probably be more efficient to compute checksum 2348c2ecf20Sopenharmony_ci * and encrypt at the same time: */ 2358c2ecf20Sopenharmony_ci if ((krb5_make_seq_num(kctx, kctx->seq, kctx->initiate ? 0 : 0xff, 2368c2ecf20Sopenharmony_ci seq_send, ptr + GSS_KRB5_TOK_HDR_LEN, ptr + 8))) 2378c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (gss_encrypt_xdr_buf(kctx->enc, buf, 2408c2ecf20Sopenharmony_ci offset + headlen - conflen, pages)) 2418c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic u32 2478c2ecf20Sopenharmony_cigss_unwrap_kerberos_v1(struct krb5_ctx *kctx, int offset, int len, 2488c2ecf20Sopenharmony_ci struct xdr_buf *buf, unsigned int *slack, 2498c2ecf20Sopenharmony_ci unsigned int *align) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci int signalg; 2528c2ecf20Sopenharmony_ci int sealalg; 2538c2ecf20Sopenharmony_ci char cksumdata[GSS_KRB5_MAX_CKSUM_LEN]; 2548c2ecf20Sopenharmony_ci struct xdr_netobj md5cksum = {.len = sizeof(cksumdata), 2558c2ecf20Sopenharmony_ci .data = cksumdata}; 2568c2ecf20Sopenharmony_ci time64_t now; 2578c2ecf20Sopenharmony_ci int direction; 2588c2ecf20Sopenharmony_ci s32 seqnum; 2598c2ecf20Sopenharmony_ci unsigned char *ptr; 2608c2ecf20Sopenharmony_ci int bodysize; 2618c2ecf20Sopenharmony_ci void *data_start, *orig_start; 2628c2ecf20Sopenharmony_ci int data_len; 2638c2ecf20Sopenharmony_ci int blocksize; 2648c2ecf20Sopenharmony_ci u32 conflen = kctx->gk5e->conflen; 2658c2ecf20Sopenharmony_ci int crypt_offset; 2668c2ecf20Sopenharmony_ci u8 *cksumkey; 2678c2ecf20Sopenharmony_ci unsigned int saved_len = buf->len; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci dprintk("RPC: gss_unwrap_kerberos\n"); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci ptr = (u8 *)buf->head[0].iov_base + offset; 2728c2ecf20Sopenharmony_ci if (g_verify_token_header(&kctx->mech_used, &bodysize, &ptr, 2738c2ecf20Sopenharmony_ci len - offset)) 2748c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci if ((ptr[0] != ((KG_TOK_WRAP_MSG >> 8) & 0xff)) || 2778c2ecf20Sopenharmony_ci (ptr[1] != (KG_TOK_WRAP_MSG & 0xff))) 2788c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci /* XXX sanity-check bodysize?? */ 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* get the sign and seal algorithms */ 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci signalg = ptr[2] + (ptr[3] << 8); 2858c2ecf20Sopenharmony_ci if (signalg != kctx->gk5e->signalg) 2868c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci sealalg = ptr[4] + (ptr[5] << 8); 2898c2ecf20Sopenharmony_ci if (sealalg != kctx->gk5e->sealalg) 2908c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci if ((ptr[6] != 0xff) || (ptr[7] != 0xff)) 2938c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci /* 2968c2ecf20Sopenharmony_ci * Data starts after token header and checksum. ptr points 2978c2ecf20Sopenharmony_ci * to the beginning of the token header 2988c2ecf20Sopenharmony_ci */ 2998c2ecf20Sopenharmony_ci crypt_offset = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) - 3008c2ecf20Sopenharmony_ci (unsigned char *)buf->head[0].iov_base; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci buf->len = len; 3038c2ecf20Sopenharmony_ci if (gss_decrypt_xdr_buf(kctx->enc, buf, crypt_offset)) 3048c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci if (kctx->gk5e->keyed_cksum) 3078c2ecf20Sopenharmony_ci cksumkey = kctx->cksum; 3088c2ecf20Sopenharmony_ci else 3098c2ecf20Sopenharmony_ci cksumkey = NULL; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci if (make_checksum(kctx, ptr, 8, buf, crypt_offset, 3128c2ecf20Sopenharmony_ci cksumkey, KG_USAGE_SEAL, &md5cksum)) 3138c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci if (memcmp(md5cksum.data, ptr + GSS_KRB5_TOK_HDR_LEN, 3168c2ecf20Sopenharmony_ci kctx->gk5e->cksumlength)) 3178c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci /* it got through unscathed. Make sure the context is unexpired */ 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci if (now > kctx->endtime) 3248c2ecf20Sopenharmony_ci return GSS_S_CONTEXT_EXPIRED; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* do sequencing checks */ 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci if (krb5_get_seq_num(kctx, ptr + GSS_KRB5_TOK_HDR_LEN, 3298c2ecf20Sopenharmony_ci ptr + 8, &direction, &seqnum)) 3308c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci if ((kctx->initiate && direction != 0xff) || 3338c2ecf20Sopenharmony_ci (!kctx->initiate && direction != 0)) 3348c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci /* Copy the data back to the right position. XXX: Would probably be 3378c2ecf20Sopenharmony_ci * better to copy and encrypt at the same time. */ 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci blocksize = crypto_sync_skcipher_blocksize(kctx->enc); 3408c2ecf20Sopenharmony_ci data_start = ptr + (GSS_KRB5_TOK_HDR_LEN + kctx->gk5e->cksumlength) + 3418c2ecf20Sopenharmony_ci conflen; 3428c2ecf20Sopenharmony_ci orig_start = buf->head[0].iov_base + offset; 3438c2ecf20Sopenharmony_ci data_len = (buf->head[0].iov_base + buf->head[0].iov_len) - data_start; 3448c2ecf20Sopenharmony_ci memmove(orig_start, data_start, data_len); 3458c2ecf20Sopenharmony_ci buf->head[0].iov_len -= (data_start - orig_start); 3468c2ecf20Sopenharmony_ci buf->len = len - (data_start - orig_start); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci if (gss_krb5_remove_padding(buf, blocksize)) 3498c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci /* slack must include room for krb5 padding */ 3528c2ecf20Sopenharmony_ci *slack = XDR_QUADLEN(saved_len - buf->len); 3538c2ecf20Sopenharmony_ci /* The GSS blob always precedes the RPC message payload */ 3548c2ecf20Sopenharmony_ci *align = *slack; 3558c2ecf20Sopenharmony_ci return GSS_S_COMPLETE; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci/* 3598c2ecf20Sopenharmony_ci * We can shift data by up to LOCAL_BUF_LEN bytes in a pass. If we need 3608c2ecf20Sopenharmony_ci * to do more than that, we shift repeatedly. Kevin Coffman reports 3618c2ecf20Sopenharmony_ci * seeing 28 bytes as the value used by Microsoft clients and servers 3628c2ecf20Sopenharmony_ci * with AES, so this constant is chosen to allow handling 28 in one pass 3638c2ecf20Sopenharmony_ci * without using too much stack space. 3648c2ecf20Sopenharmony_ci * 3658c2ecf20Sopenharmony_ci * If that proves to a problem perhaps we could use a more clever 3668c2ecf20Sopenharmony_ci * algorithm. 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_ci#define LOCAL_BUF_LEN 32u 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic void rotate_buf_a_little(struct xdr_buf *buf, unsigned int shift) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci char head[LOCAL_BUF_LEN]; 3738c2ecf20Sopenharmony_ci char tmp[LOCAL_BUF_LEN]; 3748c2ecf20Sopenharmony_ci unsigned int this_len, i; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci BUG_ON(shift > LOCAL_BUF_LEN); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci read_bytes_from_xdr_buf(buf, 0, head, shift); 3798c2ecf20Sopenharmony_ci for (i = 0; i + shift < buf->len; i += LOCAL_BUF_LEN) { 3808c2ecf20Sopenharmony_ci this_len = min(LOCAL_BUF_LEN, buf->len - (i + shift)); 3818c2ecf20Sopenharmony_ci read_bytes_from_xdr_buf(buf, i+shift, tmp, this_len); 3828c2ecf20Sopenharmony_ci write_bytes_to_xdr_buf(buf, i, tmp, this_len); 3838c2ecf20Sopenharmony_ci } 3848c2ecf20Sopenharmony_ci write_bytes_to_xdr_buf(buf, buf->len - shift, head, shift); 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_cistatic void _rotate_left(struct xdr_buf *buf, unsigned int shift) 3888c2ecf20Sopenharmony_ci{ 3898c2ecf20Sopenharmony_ci int shifted = 0; 3908c2ecf20Sopenharmony_ci int this_shift; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci shift %= buf->len; 3938c2ecf20Sopenharmony_ci while (shifted < shift) { 3948c2ecf20Sopenharmony_ci this_shift = min(shift - shifted, LOCAL_BUF_LEN); 3958c2ecf20Sopenharmony_ci rotate_buf_a_little(buf, this_shift); 3968c2ecf20Sopenharmony_ci shifted += this_shift; 3978c2ecf20Sopenharmony_ci } 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cistatic void rotate_left(u32 base, struct xdr_buf *buf, unsigned int shift) 4018c2ecf20Sopenharmony_ci{ 4028c2ecf20Sopenharmony_ci struct xdr_buf subbuf; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci xdr_buf_subsegment(buf, &subbuf, base, buf->len - base); 4058c2ecf20Sopenharmony_ci _rotate_left(&subbuf, shift); 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_cistatic u32 4098c2ecf20Sopenharmony_cigss_wrap_kerberos_v2(struct krb5_ctx *kctx, u32 offset, 4108c2ecf20Sopenharmony_ci struct xdr_buf *buf, struct page **pages) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci u8 *ptr, *plainhdr; 4138c2ecf20Sopenharmony_ci time64_t now; 4148c2ecf20Sopenharmony_ci u8 flags = 0x00; 4158c2ecf20Sopenharmony_ci __be16 *be16ptr; 4168c2ecf20Sopenharmony_ci __be64 *be64ptr; 4178c2ecf20Sopenharmony_ci u32 err; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci dprintk("RPC: %s\n", __func__); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci if (kctx->gk5e->encrypt_v2 == NULL) 4228c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci /* make room for gss token header */ 4258c2ecf20Sopenharmony_ci if (xdr_extend_head(buf, offset, GSS_KRB5_TOK_HDR_LEN)) 4268c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci /* construct gss token header */ 4298c2ecf20Sopenharmony_ci ptr = plainhdr = buf->head[0].iov_base + offset; 4308c2ecf20Sopenharmony_ci *ptr++ = (unsigned char) ((KG2_TOK_WRAP>>8) & 0xff); 4318c2ecf20Sopenharmony_ci *ptr++ = (unsigned char) (KG2_TOK_WRAP & 0xff); 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if ((kctx->flags & KRB5_CTX_FLAG_INITIATOR) == 0) 4348c2ecf20Sopenharmony_ci flags |= KG2_TOKEN_FLAG_SENTBYACCEPTOR; 4358c2ecf20Sopenharmony_ci if ((kctx->flags & KRB5_CTX_FLAG_ACCEPTOR_SUBKEY) != 0) 4368c2ecf20Sopenharmony_ci flags |= KG2_TOKEN_FLAG_ACCEPTORSUBKEY; 4378c2ecf20Sopenharmony_ci /* We always do confidentiality in wrap tokens */ 4388c2ecf20Sopenharmony_ci flags |= KG2_TOKEN_FLAG_SEALED; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci *ptr++ = flags; 4418c2ecf20Sopenharmony_ci *ptr++ = 0xff; 4428c2ecf20Sopenharmony_ci be16ptr = (__be16 *)ptr; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci *be16ptr++ = 0; 4458c2ecf20Sopenharmony_ci /* "inner" token header always uses 0 for RRC */ 4468c2ecf20Sopenharmony_ci *be16ptr++ = 0; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci be64ptr = (__be64 *)be16ptr; 4498c2ecf20Sopenharmony_ci *be64ptr = cpu_to_be64(atomic64_fetch_inc(&kctx->seq_send64)); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci err = (*kctx->gk5e->encrypt_v2)(kctx, offset, buf, pages); 4528c2ecf20Sopenharmony_ci if (err) 4538c2ecf20Sopenharmony_ci return err; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 4568c2ecf20Sopenharmony_ci return (kctx->endtime < now) ? GSS_S_CONTEXT_EXPIRED : GSS_S_COMPLETE; 4578c2ecf20Sopenharmony_ci} 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_cistatic u32 4608c2ecf20Sopenharmony_cigss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, int len, 4618c2ecf20Sopenharmony_ci struct xdr_buf *buf, unsigned int *slack, 4628c2ecf20Sopenharmony_ci unsigned int *align) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci time64_t now; 4658c2ecf20Sopenharmony_ci u8 *ptr; 4668c2ecf20Sopenharmony_ci u8 flags = 0x00; 4678c2ecf20Sopenharmony_ci u16 ec, rrc; 4688c2ecf20Sopenharmony_ci int err; 4698c2ecf20Sopenharmony_ci u32 headskip, tailskip; 4708c2ecf20Sopenharmony_ci u8 decrypted_hdr[GSS_KRB5_TOK_HDR_LEN]; 4718c2ecf20Sopenharmony_ci unsigned int movelen; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci dprintk("RPC: %s\n", __func__); 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci if (kctx->gk5e->decrypt_v2 == NULL) 4778c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci ptr = buf->head[0].iov_base + offset; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci if (be16_to_cpu(*((__be16 *)ptr)) != KG2_TOK_WRAP) 4828c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci flags = ptr[2]; 4858c2ecf20Sopenharmony_ci if ((!kctx->initiate && (flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR)) || 4868c2ecf20Sopenharmony_ci (kctx->initiate && !(flags & KG2_TOKEN_FLAG_SENTBYACCEPTOR))) 4878c2ecf20Sopenharmony_ci return GSS_S_BAD_SIG; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci if ((flags & KG2_TOKEN_FLAG_SEALED) == 0) { 4908c2ecf20Sopenharmony_ci dprintk("%s: token missing expected sealed flag\n", __func__); 4918c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 4928c2ecf20Sopenharmony_ci } 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci if (ptr[3] != 0xff) 4958c2ecf20Sopenharmony_ci return GSS_S_DEFECTIVE_TOKEN; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci ec = be16_to_cpup((__be16 *)(ptr + 4)); 4988c2ecf20Sopenharmony_ci rrc = be16_to_cpup((__be16 *)(ptr + 6)); 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci /* 5018c2ecf20Sopenharmony_ci * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss 5028c2ecf20Sopenharmony_ci * doesn't want it checked; see page 6 of rfc 2203. 5038c2ecf20Sopenharmony_ci */ 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci if (rrc != 0) 5068c2ecf20Sopenharmony_ci rotate_left(offset + 16, buf, rrc); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci err = (*kctx->gk5e->decrypt_v2)(kctx, offset, len, buf, 5098c2ecf20Sopenharmony_ci &headskip, &tailskip); 5108c2ecf20Sopenharmony_ci if (err) 5118c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci /* 5148c2ecf20Sopenharmony_ci * Retrieve the decrypted gss token header and verify 5158c2ecf20Sopenharmony_ci * it against the original 5168c2ecf20Sopenharmony_ci */ 5178c2ecf20Sopenharmony_ci err = read_bytes_from_xdr_buf(buf, 5188c2ecf20Sopenharmony_ci len - GSS_KRB5_TOK_HDR_LEN - tailskip, 5198c2ecf20Sopenharmony_ci decrypted_hdr, GSS_KRB5_TOK_HDR_LEN); 5208c2ecf20Sopenharmony_ci if (err) { 5218c2ecf20Sopenharmony_ci dprintk("%s: error %u getting decrypted_hdr\n", __func__, err); 5228c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 5238c2ecf20Sopenharmony_ci } 5248c2ecf20Sopenharmony_ci if (memcmp(ptr, decrypted_hdr, 6) 5258c2ecf20Sopenharmony_ci || memcmp(ptr + 8, decrypted_hdr + 8, 8)) { 5268c2ecf20Sopenharmony_ci dprintk("%s: token hdr, plaintext hdr mismatch!\n", __func__); 5278c2ecf20Sopenharmony_ci return GSS_S_FAILURE; 5288c2ecf20Sopenharmony_ci } 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci /* do sequencing checks */ 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci /* it got through unscathed. Make sure the context is unexpired */ 5338c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 5348c2ecf20Sopenharmony_ci if (now > kctx->endtime) 5358c2ecf20Sopenharmony_ci return GSS_S_CONTEXT_EXPIRED; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci /* 5388c2ecf20Sopenharmony_ci * Move the head data back to the right position in xdr_buf. 5398c2ecf20Sopenharmony_ci * We ignore any "ec" data since it might be in the head or 5408c2ecf20Sopenharmony_ci * the tail, and we really don't need to deal with it. 5418c2ecf20Sopenharmony_ci * Note that buf->head[0].iov_len may indicate the available 5428c2ecf20Sopenharmony_ci * head buffer space rather than that actually occupied. 5438c2ecf20Sopenharmony_ci */ 5448c2ecf20Sopenharmony_ci movelen = min_t(unsigned int, buf->head[0].iov_len, len); 5458c2ecf20Sopenharmony_ci movelen -= offset + GSS_KRB5_TOK_HDR_LEN + headskip; 5468c2ecf20Sopenharmony_ci BUG_ON(offset + GSS_KRB5_TOK_HDR_LEN + headskip + movelen > 5478c2ecf20Sopenharmony_ci buf->head[0].iov_len); 5488c2ecf20Sopenharmony_ci memmove(ptr, ptr + GSS_KRB5_TOK_HDR_LEN + headskip, movelen); 5498c2ecf20Sopenharmony_ci buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip; 5508c2ecf20Sopenharmony_ci buf->len = len - (GSS_KRB5_TOK_HDR_LEN + headskip); 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci /* Trim off the trailing "extra count" and checksum blob */ 5538c2ecf20Sopenharmony_ci xdr_buf_trim(buf, ec + GSS_KRB5_TOK_HDR_LEN + tailskip); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci *align = XDR_QUADLEN(GSS_KRB5_TOK_HDR_LEN + headskip); 5568c2ecf20Sopenharmony_ci *slack = *align + XDR_QUADLEN(ec + GSS_KRB5_TOK_HDR_LEN + tailskip); 5578c2ecf20Sopenharmony_ci return GSS_S_COMPLETE; 5588c2ecf20Sopenharmony_ci} 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ciu32 5618c2ecf20Sopenharmony_cigss_wrap_kerberos(struct gss_ctx *gctx, int offset, 5628c2ecf20Sopenharmony_ci struct xdr_buf *buf, struct page **pages) 5638c2ecf20Sopenharmony_ci{ 5648c2ecf20Sopenharmony_ci struct krb5_ctx *kctx = gctx->internal_ctx_id; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci switch (kctx->enctype) { 5678c2ecf20Sopenharmony_ci default: 5688c2ecf20Sopenharmony_ci BUG(); 5698c2ecf20Sopenharmony_ci case ENCTYPE_DES_CBC_RAW: 5708c2ecf20Sopenharmony_ci case ENCTYPE_DES3_CBC_RAW: 5718c2ecf20Sopenharmony_ci return gss_wrap_kerberos_v1(kctx, offset, buf, pages); 5728c2ecf20Sopenharmony_ci case ENCTYPE_AES128_CTS_HMAC_SHA1_96: 5738c2ecf20Sopenharmony_ci case ENCTYPE_AES256_CTS_HMAC_SHA1_96: 5748c2ecf20Sopenharmony_ci return gss_wrap_kerberos_v2(kctx, offset, buf, pages); 5758c2ecf20Sopenharmony_ci } 5768c2ecf20Sopenharmony_ci} 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ciu32 5798c2ecf20Sopenharmony_cigss_unwrap_kerberos(struct gss_ctx *gctx, int offset, 5808c2ecf20Sopenharmony_ci int len, struct xdr_buf *buf) 5818c2ecf20Sopenharmony_ci{ 5828c2ecf20Sopenharmony_ci struct krb5_ctx *kctx = gctx->internal_ctx_id; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci switch (kctx->enctype) { 5858c2ecf20Sopenharmony_ci default: 5868c2ecf20Sopenharmony_ci BUG(); 5878c2ecf20Sopenharmony_ci case ENCTYPE_DES_CBC_RAW: 5888c2ecf20Sopenharmony_ci case ENCTYPE_DES3_CBC_RAW: 5898c2ecf20Sopenharmony_ci return gss_unwrap_kerberos_v1(kctx, offset, len, buf, 5908c2ecf20Sopenharmony_ci &gctx->slack, &gctx->align); 5918c2ecf20Sopenharmony_ci case ENCTYPE_AES128_CTS_HMAC_SHA1_96: 5928c2ecf20Sopenharmony_ci case ENCTYPE_AES256_CTS_HMAC_SHA1_96: 5938c2ecf20Sopenharmony_ci return gss_unwrap_kerberos_v2(kctx, offset, len, buf, 5948c2ecf20Sopenharmony_ci &gctx->slack, &gctx->align); 5958c2ecf20Sopenharmony_ci } 5968c2ecf20Sopenharmony_ci} 597