18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* Kerberos-based RxRPC security 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <crypto/skcipher.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/net.h> 138c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 148c2ecf20Sopenharmony_ci#include <linux/udp.h> 158c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 168c2ecf20Sopenharmony_ci#include <linux/ctype.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <net/sock.h> 198c2ecf20Sopenharmony_ci#include <net/af_rxrpc.h> 208c2ecf20Sopenharmony_ci#include <keys/rxrpc-type.h> 218c2ecf20Sopenharmony_ci#include "ar-internal.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define RXKAD_VERSION 2 248c2ecf20Sopenharmony_ci#define MAXKRB5TICKETLEN 1024 258c2ecf20Sopenharmony_ci#define RXKAD_TKT_TYPE_KERBEROS_V5 256 268c2ecf20Sopenharmony_ci#define ANAME_SZ 40 /* size of authentication name */ 278c2ecf20Sopenharmony_ci#define INST_SZ 40 /* size of principal's instance */ 288c2ecf20Sopenharmony_ci#define REALM_SZ 40 /* size of principal's auth domain */ 298c2ecf20Sopenharmony_ci#define SNAME_SZ 40 /* size of service name */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct rxkad_level1_hdr { 328c2ecf20Sopenharmony_ci __be32 data_size; /* true data size (excluding padding) */ 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistruct rxkad_level2_hdr { 368c2ecf20Sopenharmony_ci __be32 data_size; /* true data size (excluding padding) */ 378c2ecf20Sopenharmony_ci __be32 checksum; /* decrypted data checksum */ 388c2ecf20Sopenharmony_ci}; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* 418c2ecf20Sopenharmony_ci * this holds a pinned cipher so that keventd doesn't get called by the cipher 428c2ecf20Sopenharmony_ci * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE 438c2ecf20Sopenharmony_ci * packets 448c2ecf20Sopenharmony_ci */ 458c2ecf20Sopenharmony_cistatic struct crypto_sync_skcipher *rxkad_ci; 468c2ecf20Sopenharmony_cistatic struct skcipher_request *rxkad_ci_req; 478c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(rxkad_ci_mutex); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* 508c2ecf20Sopenharmony_ci * initialise connection security 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_cistatic int rxkad_init_connection_security(struct rxrpc_connection *conn) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct crypto_sync_skcipher *ci; 558c2ecf20Sopenharmony_ci struct rxrpc_key_token *token; 568c2ecf20Sopenharmony_ci int ret; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key)); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci token = conn->params.key->payload.data[0]; 618c2ecf20Sopenharmony_ci conn->security_ix = token->security_index; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0); 648c2ecf20Sopenharmony_ci if (IS_ERR(ci)) { 658c2ecf20Sopenharmony_ci _debug("no cipher"); 668c2ecf20Sopenharmony_ci ret = PTR_ERR(ci); 678c2ecf20Sopenharmony_ci goto error; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci if (crypto_sync_skcipher_setkey(ci, token->kad->session_key, 718c2ecf20Sopenharmony_ci sizeof(token->kad->session_key)) < 0) 728c2ecf20Sopenharmony_ci BUG(); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci switch (conn->params.security_level) { 758c2ecf20Sopenharmony_ci case RXRPC_SECURITY_PLAIN: 768c2ecf20Sopenharmony_ci break; 778c2ecf20Sopenharmony_ci case RXRPC_SECURITY_AUTH: 788c2ecf20Sopenharmony_ci conn->size_align = 8; 798c2ecf20Sopenharmony_ci conn->security_size = sizeof(struct rxkad_level1_hdr); 808c2ecf20Sopenharmony_ci break; 818c2ecf20Sopenharmony_ci case RXRPC_SECURITY_ENCRYPT: 828c2ecf20Sopenharmony_ci conn->size_align = 8; 838c2ecf20Sopenharmony_ci conn->security_size = sizeof(struct rxkad_level2_hdr); 848c2ecf20Sopenharmony_ci break; 858c2ecf20Sopenharmony_ci default: 868c2ecf20Sopenharmony_ci ret = -EKEYREJECTED; 878c2ecf20Sopenharmony_ci goto error; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci conn->cipher = ci; 918c2ecf20Sopenharmony_ci ret = 0; 928c2ecf20Sopenharmony_cierror: 938c2ecf20Sopenharmony_ci _leave(" = %d", ret); 948c2ecf20Sopenharmony_ci return ret; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* 988c2ecf20Sopenharmony_ci * prime the encryption state with the invariant parts of a connection's 998c2ecf20Sopenharmony_ci * description 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistatic int rxkad_prime_packet_security(struct rxrpc_connection *conn) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct skcipher_request *req; 1048c2ecf20Sopenharmony_ci struct rxrpc_key_token *token; 1058c2ecf20Sopenharmony_ci struct scatterlist sg; 1068c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 1078c2ecf20Sopenharmony_ci __be32 *tmpbuf; 1088c2ecf20Sopenharmony_ci size_t tmpsize = 4 * sizeof(__be32); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci _enter(""); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (!conn->params.key) 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci tmpbuf = kmalloc(tmpsize, GFP_KERNEL); 1168c2ecf20Sopenharmony_ci if (!tmpbuf) 1178c2ecf20Sopenharmony_ci return -ENOMEM; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS); 1208c2ecf20Sopenharmony_ci if (!req) { 1218c2ecf20Sopenharmony_ci kfree(tmpbuf); 1228c2ecf20Sopenharmony_ci return -ENOMEM; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci token = conn->params.key->payload.data[0]; 1268c2ecf20Sopenharmony_ci memcpy(&iv, token->kad->session_key, sizeof(iv)); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci tmpbuf[0] = htonl(conn->proto.epoch); 1298c2ecf20Sopenharmony_ci tmpbuf[1] = htonl(conn->proto.cid); 1308c2ecf20Sopenharmony_ci tmpbuf[2] = 0; 1318c2ecf20Sopenharmony_ci tmpbuf[3] = htonl(conn->security_ix); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci sg_init_one(&sg, tmpbuf, tmpsize); 1348c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, conn->cipher); 1358c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 1368c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x); 1378c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 1388c2ecf20Sopenharmony_ci skcipher_request_free(req); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv)); 1418c2ecf20Sopenharmony_ci kfree(tmpbuf); 1428c2ecf20Sopenharmony_ci _leave(" = 0"); 1438c2ecf20Sopenharmony_ci return 0; 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci/* 1478c2ecf20Sopenharmony_ci * Allocate and prepare the crypto request on a call. For any particular call, 1488c2ecf20Sopenharmony_ci * this is called serially for the packets, so no lock should be necessary. 1498c2ecf20Sopenharmony_ci */ 1508c2ecf20Sopenharmony_cistatic struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = &call->conn->cipher->base; 1538c2ecf20Sopenharmony_ci struct skcipher_request *cipher_req = call->cipher_req; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (!cipher_req) { 1568c2ecf20Sopenharmony_ci cipher_req = skcipher_request_alloc(tfm, GFP_NOFS); 1578c2ecf20Sopenharmony_ci if (!cipher_req) 1588c2ecf20Sopenharmony_ci return NULL; 1598c2ecf20Sopenharmony_ci call->cipher_req = cipher_req; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return cipher_req; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* 1668c2ecf20Sopenharmony_ci * Clean up the crypto on a call. 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_cistatic void rxkad_free_call_crypto(struct rxrpc_call *call) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci if (call->cipher_req) 1718c2ecf20Sopenharmony_ci skcipher_request_free(call->cipher_req); 1728c2ecf20Sopenharmony_ci call->cipher_req = NULL; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci/* 1768c2ecf20Sopenharmony_ci * partially encrypt a packet (level 1 security) 1778c2ecf20Sopenharmony_ci */ 1788c2ecf20Sopenharmony_cistatic int rxkad_secure_packet_auth(const struct rxrpc_call *call, 1798c2ecf20Sopenharmony_ci struct sk_buff *skb, 1808c2ecf20Sopenharmony_ci u32 data_size, 1818c2ecf20Sopenharmony_ci void *sechdr, 1828c2ecf20Sopenharmony_ci struct skcipher_request *req) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 1858c2ecf20Sopenharmony_ci struct rxkad_level1_hdr hdr; 1868c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 1878c2ecf20Sopenharmony_ci struct scatterlist sg; 1888c2ecf20Sopenharmony_ci u16 check; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci _enter(""); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci check = sp->hdr.seq ^ call->call_id; 1938c2ecf20Sopenharmony_ci data_size |= (u32)check << 16; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci hdr.data_size = htonl(data_size); 1968c2ecf20Sopenharmony_ci memcpy(sechdr, &hdr, sizeof(hdr)); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci /* start the encryption afresh */ 1998c2ecf20Sopenharmony_ci memset(&iv, 0, sizeof(iv)); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci sg_init_one(&sg, sechdr, 8); 2028c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 2038c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 2048c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); 2058c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 2068c2ecf20Sopenharmony_ci skcipher_request_zero(req); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci _leave(" = 0"); 2098c2ecf20Sopenharmony_ci return 0; 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/* 2138c2ecf20Sopenharmony_ci * wholly encrypt a packet (level 2 security) 2148c2ecf20Sopenharmony_ci */ 2158c2ecf20Sopenharmony_cistatic int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, 2168c2ecf20Sopenharmony_ci struct sk_buff *skb, 2178c2ecf20Sopenharmony_ci u32 data_size, 2188c2ecf20Sopenharmony_ci void *sechdr, 2198c2ecf20Sopenharmony_ci struct skcipher_request *req) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci const struct rxrpc_key_token *token; 2228c2ecf20Sopenharmony_ci struct rxkad_level2_hdr rxkhdr; 2238c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp; 2248c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 2258c2ecf20Sopenharmony_ci struct scatterlist sg[16]; 2268c2ecf20Sopenharmony_ci unsigned int len; 2278c2ecf20Sopenharmony_ci u16 check; 2288c2ecf20Sopenharmony_ci int err; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci sp = rxrpc_skb(skb); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci _enter(""); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci check = sp->hdr.seq ^ call->call_id; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci rxkhdr.data_size = htonl(data_size | (u32)check << 16); 2378c2ecf20Sopenharmony_ci rxkhdr.checksum = 0; 2388c2ecf20Sopenharmony_ci memcpy(sechdr, &rxkhdr, sizeof(rxkhdr)); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci /* encrypt from the session key */ 2418c2ecf20Sopenharmony_ci token = call->conn->params.key->payload.data[0]; 2428c2ecf20Sopenharmony_ci memcpy(&iv, token->kad->session_key, sizeof(iv)); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci sg_init_one(&sg[0], sechdr, sizeof(rxkhdr)); 2458c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 2468c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 2478c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x); 2488c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci /* we want to encrypt the skbuff in-place */ 2518c2ecf20Sopenharmony_ci err = -EMSGSIZE; 2528c2ecf20Sopenharmony_ci if (skb_shinfo(skb)->nr_frags > 16) 2538c2ecf20Sopenharmony_ci goto out; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci len = data_size + call->conn->size_align - 1; 2568c2ecf20Sopenharmony_ci len &= ~(call->conn->size_align - 1); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci sg_init_table(sg, ARRAY_SIZE(sg)); 2598c2ecf20Sopenharmony_ci err = skb_to_sgvec(skb, sg, 0, len); 2608c2ecf20Sopenharmony_ci if (unlikely(err < 0)) 2618c2ecf20Sopenharmony_ci goto out; 2628c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, len, iv.x); 2638c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci _leave(" = 0"); 2668c2ecf20Sopenharmony_ci err = 0; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ciout: 2698c2ecf20Sopenharmony_ci skcipher_request_zero(req); 2708c2ecf20Sopenharmony_ci return err; 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci/* 2748c2ecf20Sopenharmony_ci * checksum an RxRPC packet header 2758c2ecf20Sopenharmony_ci */ 2768c2ecf20Sopenharmony_cistatic int rxkad_secure_packet(struct rxrpc_call *call, 2778c2ecf20Sopenharmony_ci struct sk_buff *skb, 2788c2ecf20Sopenharmony_ci size_t data_size, 2798c2ecf20Sopenharmony_ci void *sechdr) 2808c2ecf20Sopenharmony_ci{ 2818c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp; 2828c2ecf20Sopenharmony_ci struct skcipher_request *req; 2838c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 2848c2ecf20Sopenharmony_ci struct scatterlist sg; 2858c2ecf20Sopenharmony_ci u32 x, y; 2868c2ecf20Sopenharmony_ci int ret; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci sp = rxrpc_skb(skb); 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci _enter("{%d{%x}},{#%u},%zu,", 2918c2ecf20Sopenharmony_ci call->debug_id, key_serial(call->conn->params.key), 2928c2ecf20Sopenharmony_ci sp->hdr.seq, data_size); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci if (!call->conn->cipher) 2958c2ecf20Sopenharmony_ci return 0; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci ret = key_validate(call->conn->params.key); 2988c2ecf20Sopenharmony_ci if (ret < 0) 2998c2ecf20Sopenharmony_ci return ret; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci req = rxkad_get_call_crypto(call); 3028c2ecf20Sopenharmony_ci if (!req) 3038c2ecf20Sopenharmony_ci return -ENOMEM; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci /* continue encrypting from where we left off */ 3068c2ecf20Sopenharmony_ci memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci /* calculate the security checksum */ 3098c2ecf20Sopenharmony_ci x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); 3108c2ecf20Sopenharmony_ci x |= sp->hdr.seq & 0x3fffffff; 3118c2ecf20Sopenharmony_ci call->crypto_buf[0] = htonl(call->call_id); 3128c2ecf20Sopenharmony_ci call->crypto_buf[1] = htonl(x); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci sg_init_one(&sg, call->crypto_buf, 8); 3158c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 3168c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 3178c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); 3188c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 3198c2ecf20Sopenharmony_ci skcipher_request_zero(req); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci y = ntohl(call->crypto_buf[1]); 3228c2ecf20Sopenharmony_ci y = (y >> 16) & 0xffff; 3238c2ecf20Sopenharmony_ci if (y == 0) 3248c2ecf20Sopenharmony_ci y = 1; /* zero checksums are not permitted */ 3258c2ecf20Sopenharmony_ci sp->hdr.cksum = y; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci switch (call->conn->params.security_level) { 3288c2ecf20Sopenharmony_ci case RXRPC_SECURITY_PLAIN: 3298c2ecf20Sopenharmony_ci ret = 0; 3308c2ecf20Sopenharmony_ci break; 3318c2ecf20Sopenharmony_ci case RXRPC_SECURITY_AUTH: 3328c2ecf20Sopenharmony_ci ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr, 3338c2ecf20Sopenharmony_ci req); 3348c2ecf20Sopenharmony_ci break; 3358c2ecf20Sopenharmony_ci case RXRPC_SECURITY_ENCRYPT: 3368c2ecf20Sopenharmony_ci ret = rxkad_secure_packet_encrypt(call, skb, data_size, 3378c2ecf20Sopenharmony_ci sechdr, req); 3388c2ecf20Sopenharmony_ci break; 3398c2ecf20Sopenharmony_ci default: 3408c2ecf20Sopenharmony_ci ret = -EPERM; 3418c2ecf20Sopenharmony_ci break; 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci _leave(" = %d [set %hx]", ret, y); 3458c2ecf20Sopenharmony_ci return ret; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci/* 3498c2ecf20Sopenharmony_ci * decrypt partial encryption on a packet (level 1 security) 3508c2ecf20Sopenharmony_ci */ 3518c2ecf20Sopenharmony_cistatic int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb, 3528c2ecf20Sopenharmony_ci unsigned int offset, unsigned int len, 3538c2ecf20Sopenharmony_ci rxrpc_seq_t seq, 3548c2ecf20Sopenharmony_ci struct skcipher_request *req) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct rxkad_level1_hdr sechdr; 3578c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 3588c2ecf20Sopenharmony_ci struct scatterlist sg[16]; 3598c2ecf20Sopenharmony_ci bool aborted; 3608c2ecf20Sopenharmony_ci u32 data_size, buf; 3618c2ecf20Sopenharmony_ci u16 check; 3628c2ecf20Sopenharmony_ci int ret; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci _enter(""); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci if (len < 8) { 3678c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H", 3688c2ecf20Sopenharmony_ci RXKADSEALEDINCON); 3698c2ecf20Sopenharmony_ci goto protocol_error; 3708c2ecf20Sopenharmony_ci } 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci /* Decrypt the skbuff in-place. TODO: We really want to decrypt 3738c2ecf20Sopenharmony_ci * directly into the target buffer. 3748c2ecf20Sopenharmony_ci */ 3758c2ecf20Sopenharmony_ci sg_init_table(sg, ARRAY_SIZE(sg)); 3768c2ecf20Sopenharmony_ci ret = skb_to_sgvec(skb, sg, offset, 8); 3778c2ecf20Sopenharmony_ci if (unlikely(ret < 0)) 3788c2ecf20Sopenharmony_ci return ret; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci /* start the decryption afresh */ 3818c2ecf20Sopenharmony_ci memset(&iv, 0, sizeof(iv)); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 3848c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 3858c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, 8, iv.x); 3868c2ecf20Sopenharmony_ci crypto_skcipher_decrypt(req); 3878c2ecf20Sopenharmony_ci skcipher_request_zero(req); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci /* Extract the decrypted packet length */ 3908c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) { 3918c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1", 3928c2ecf20Sopenharmony_ci RXKADDATALEN); 3938c2ecf20Sopenharmony_ci goto protocol_error; 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci offset += sizeof(sechdr); 3968c2ecf20Sopenharmony_ci len -= sizeof(sechdr); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci buf = ntohl(sechdr.data_size); 3998c2ecf20Sopenharmony_ci data_size = buf & 0xffff; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci check = buf >> 16; 4028c2ecf20Sopenharmony_ci check ^= seq ^ call->call_id; 4038c2ecf20Sopenharmony_ci check &= 0xffff; 4048c2ecf20Sopenharmony_ci if (check != 0) { 4058c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C", 4068c2ecf20Sopenharmony_ci RXKADSEALEDINCON); 4078c2ecf20Sopenharmony_ci goto protocol_error; 4088c2ecf20Sopenharmony_ci } 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci if (data_size > len) { 4118c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L", 4128c2ecf20Sopenharmony_ci RXKADDATALEN); 4138c2ecf20Sopenharmony_ci goto protocol_error; 4148c2ecf20Sopenharmony_ci } 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci _leave(" = 0 [dlen=%x]", data_size); 4178c2ecf20Sopenharmony_ci return 0; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ciprotocol_error: 4208c2ecf20Sopenharmony_ci if (aborted) 4218c2ecf20Sopenharmony_ci rxrpc_send_abort_packet(call); 4228c2ecf20Sopenharmony_ci return -EPROTO; 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci/* 4268c2ecf20Sopenharmony_ci * wholly decrypt a packet (level 2 security) 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_cistatic int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb, 4298c2ecf20Sopenharmony_ci unsigned int offset, unsigned int len, 4308c2ecf20Sopenharmony_ci rxrpc_seq_t seq, 4318c2ecf20Sopenharmony_ci struct skcipher_request *req) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci const struct rxrpc_key_token *token; 4348c2ecf20Sopenharmony_ci struct rxkad_level2_hdr sechdr; 4358c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 4368c2ecf20Sopenharmony_ci struct scatterlist _sg[4], *sg; 4378c2ecf20Sopenharmony_ci bool aborted; 4388c2ecf20Sopenharmony_ci u32 data_size, buf; 4398c2ecf20Sopenharmony_ci u16 check; 4408c2ecf20Sopenharmony_ci int nsg, ret; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci _enter(",{%d}", skb->len); 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci if (len < 8) { 4458c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H", 4468c2ecf20Sopenharmony_ci RXKADSEALEDINCON); 4478c2ecf20Sopenharmony_ci goto protocol_error; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci /* Decrypt the skbuff in-place. TODO: We really want to decrypt 4518c2ecf20Sopenharmony_ci * directly into the target buffer. 4528c2ecf20Sopenharmony_ci */ 4538c2ecf20Sopenharmony_ci sg = _sg; 4548c2ecf20Sopenharmony_ci nsg = skb_shinfo(skb)->nr_frags + 1; 4558c2ecf20Sopenharmony_ci if (nsg <= 4) { 4568c2ecf20Sopenharmony_ci nsg = 4; 4578c2ecf20Sopenharmony_ci } else { 4588c2ecf20Sopenharmony_ci sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO); 4598c2ecf20Sopenharmony_ci if (!sg) 4608c2ecf20Sopenharmony_ci goto nomem; 4618c2ecf20Sopenharmony_ci } 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci sg_init_table(sg, nsg); 4648c2ecf20Sopenharmony_ci ret = skb_to_sgvec(skb, sg, offset, len); 4658c2ecf20Sopenharmony_ci if (unlikely(ret < 0)) { 4668c2ecf20Sopenharmony_ci if (sg != _sg) 4678c2ecf20Sopenharmony_ci kfree(sg); 4688c2ecf20Sopenharmony_ci return ret; 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci /* decrypt from the session key */ 4728c2ecf20Sopenharmony_ci token = call->conn->params.key->payload.data[0]; 4738c2ecf20Sopenharmony_ci memcpy(&iv, token->kad->session_key, sizeof(iv)); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 4768c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 4778c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, len, iv.x); 4788c2ecf20Sopenharmony_ci crypto_skcipher_decrypt(req); 4798c2ecf20Sopenharmony_ci skcipher_request_zero(req); 4808c2ecf20Sopenharmony_ci if (sg != _sg) 4818c2ecf20Sopenharmony_ci kfree(sg); 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci /* Extract the decrypted packet length */ 4848c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) { 4858c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2", 4868c2ecf20Sopenharmony_ci RXKADDATALEN); 4878c2ecf20Sopenharmony_ci goto protocol_error; 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci offset += sizeof(sechdr); 4908c2ecf20Sopenharmony_ci len -= sizeof(sechdr); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci buf = ntohl(sechdr.data_size); 4938c2ecf20Sopenharmony_ci data_size = buf & 0xffff; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci check = buf >> 16; 4968c2ecf20Sopenharmony_ci check ^= seq ^ call->call_id; 4978c2ecf20Sopenharmony_ci check &= 0xffff; 4988c2ecf20Sopenharmony_ci if (check != 0) { 4998c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C", 5008c2ecf20Sopenharmony_ci RXKADSEALEDINCON); 5018c2ecf20Sopenharmony_ci goto protocol_error; 5028c2ecf20Sopenharmony_ci } 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci if (data_size > len) { 5058c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L", 5068c2ecf20Sopenharmony_ci RXKADDATALEN); 5078c2ecf20Sopenharmony_ci goto protocol_error; 5088c2ecf20Sopenharmony_ci } 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci _leave(" = 0 [dlen=%x]", data_size); 5118c2ecf20Sopenharmony_ci return 0; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ciprotocol_error: 5148c2ecf20Sopenharmony_ci if (aborted) 5158c2ecf20Sopenharmony_ci rxrpc_send_abort_packet(call); 5168c2ecf20Sopenharmony_ci return -EPROTO; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_cinomem: 5198c2ecf20Sopenharmony_ci _leave(" = -ENOMEM"); 5208c2ecf20Sopenharmony_ci return -ENOMEM; 5218c2ecf20Sopenharmony_ci} 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci/* 5248c2ecf20Sopenharmony_ci * Verify the security on a received packet or subpacket (if part of a 5258c2ecf20Sopenharmony_ci * jumbo packet). 5268c2ecf20Sopenharmony_ci */ 5278c2ecf20Sopenharmony_cistatic int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb, 5288c2ecf20Sopenharmony_ci unsigned int offset, unsigned int len, 5298c2ecf20Sopenharmony_ci rxrpc_seq_t seq, u16 expected_cksum) 5308c2ecf20Sopenharmony_ci{ 5318c2ecf20Sopenharmony_ci struct skcipher_request *req; 5328c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 5338c2ecf20Sopenharmony_ci struct scatterlist sg; 5348c2ecf20Sopenharmony_ci bool aborted; 5358c2ecf20Sopenharmony_ci u16 cksum; 5368c2ecf20Sopenharmony_ci u32 x, y; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci _enter("{%d{%x}},{#%u}", 5398c2ecf20Sopenharmony_ci call->debug_id, key_serial(call->conn->params.key), seq); 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci if (!call->conn->cipher) 5428c2ecf20Sopenharmony_ci return 0; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci req = rxkad_get_call_crypto(call); 5458c2ecf20Sopenharmony_ci if (!req) 5468c2ecf20Sopenharmony_ci return -ENOMEM; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci /* continue encrypting from where we left off */ 5498c2ecf20Sopenharmony_ci memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci /* validate the security checksum */ 5528c2ecf20Sopenharmony_ci x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); 5538c2ecf20Sopenharmony_ci x |= seq & 0x3fffffff; 5548c2ecf20Sopenharmony_ci call->crypto_buf[0] = htonl(call->call_id); 5558c2ecf20Sopenharmony_ci call->crypto_buf[1] = htonl(x); 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci sg_init_one(&sg, call->crypto_buf, 8); 5588c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, call->conn->cipher); 5598c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 5608c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); 5618c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 5628c2ecf20Sopenharmony_ci skcipher_request_zero(req); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci y = ntohl(call->crypto_buf[1]); 5658c2ecf20Sopenharmony_ci cksum = (y >> 16) & 0xffff; 5668c2ecf20Sopenharmony_ci if (cksum == 0) 5678c2ecf20Sopenharmony_ci cksum = 1; /* zero checksums are not permitted */ 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci if (cksum != expected_cksum) { 5708c2ecf20Sopenharmony_ci aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK", 5718c2ecf20Sopenharmony_ci RXKADSEALEDINCON); 5728c2ecf20Sopenharmony_ci goto protocol_error; 5738c2ecf20Sopenharmony_ci } 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci switch (call->conn->params.security_level) { 5768c2ecf20Sopenharmony_ci case RXRPC_SECURITY_PLAIN: 5778c2ecf20Sopenharmony_ci return 0; 5788c2ecf20Sopenharmony_ci case RXRPC_SECURITY_AUTH: 5798c2ecf20Sopenharmony_ci return rxkad_verify_packet_1(call, skb, offset, len, seq, req); 5808c2ecf20Sopenharmony_ci case RXRPC_SECURITY_ENCRYPT: 5818c2ecf20Sopenharmony_ci return rxkad_verify_packet_2(call, skb, offset, len, seq, req); 5828c2ecf20Sopenharmony_ci default: 5838c2ecf20Sopenharmony_ci return -ENOANO; 5848c2ecf20Sopenharmony_ci } 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ciprotocol_error: 5878c2ecf20Sopenharmony_ci if (aborted) 5888c2ecf20Sopenharmony_ci rxrpc_send_abort_packet(call); 5898c2ecf20Sopenharmony_ci return -EPROTO; 5908c2ecf20Sopenharmony_ci} 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci/* 5938c2ecf20Sopenharmony_ci * Locate the data contained in a packet that was partially encrypted. 5948c2ecf20Sopenharmony_ci */ 5958c2ecf20Sopenharmony_cistatic void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb, 5968c2ecf20Sopenharmony_ci unsigned int *_offset, unsigned int *_len) 5978c2ecf20Sopenharmony_ci{ 5988c2ecf20Sopenharmony_ci struct rxkad_level1_hdr sechdr; 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0) 6018c2ecf20Sopenharmony_ci BUG(); 6028c2ecf20Sopenharmony_ci *_offset += sizeof(sechdr); 6038c2ecf20Sopenharmony_ci *_len = ntohl(sechdr.data_size) & 0xffff; 6048c2ecf20Sopenharmony_ci} 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci/* 6078c2ecf20Sopenharmony_ci * Locate the data contained in a packet that was completely encrypted. 6088c2ecf20Sopenharmony_ci */ 6098c2ecf20Sopenharmony_cistatic void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb, 6108c2ecf20Sopenharmony_ci unsigned int *_offset, unsigned int *_len) 6118c2ecf20Sopenharmony_ci{ 6128c2ecf20Sopenharmony_ci struct rxkad_level2_hdr sechdr; 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0) 6158c2ecf20Sopenharmony_ci BUG(); 6168c2ecf20Sopenharmony_ci *_offset += sizeof(sechdr); 6178c2ecf20Sopenharmony_ci *_len = ntohl(sechdr.data_size) & 0xffff; 6188c2ecf20Sopenharmony_ci} 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci/* 6218c2ecf20Sopenharmony_ci * Locate the data contained in an already decrypted packet. 6228c2ecf20Sopenharmony_ci */ 6238c2ecf20Sopenharmony_cistatic void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb, 6248c2ecf20Sopenharmony_ci unsigned int *_offset, unsigned int *_len) 6258c2ecf20Sopenharmony_ci{ 6268c2ecf20Sopenharmony_ci switch (call->conn->params.security_level) { 6278c2ecf20Sopenharmony_ci case RXRPC_SECURITY_AUTH: 6288c2ecf20Sopenharmony_ci rxkad_locate_data_1(call, skb, _offset, _len); 6298c2ecf20Sopenharmony_ci return; 6308c2ecf20Sopenharmony_ci case RXRPC_SECURITY_ENCRYPT: 6318c2ecf20Sopenharmony_ci rxkad_locate_data_2(call, skb, _offset, _len); 6328c2ecf20Sopenharmony_ci return; 6338c2ecf20Sopenharmony_ci default: 6348c2ecf20Sopenharmony_ci return; 6358c2ecf20Sopenharmony_ci } 6368c2ecf20Sopenharmony_ci} 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci/* 6398c2ecf20Sopenharmony_ci * issue a challenge 6408c2ecf20Sopenharmony_ci */ 6418c2ecf20Sopenharmony_cistatic int rxkad_issue_challenge(struct rxrpc_connection *conn) 6428c2ecf20Sopenharmony_ci{ 6438c2ecf20Sopenharmony_ci struct rxkad_challenge challenge; 6448c2ecf20Sopenharmony_ci struct rxrpc_wire_header whdr; 6458c2ecf20Sopenharmony_ci struct msghdr msg; 6468c2ecf20Sopenharmony_ci struct kvec iov[2]; 6478c2ecf20Sopenharmony_ci size_t len; 6488c2ecf20Sopenharmony_ci u32 serial; 6498c2ecf20Sopenharmony_ci int ret; 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key)); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci ret = key_validate(conn->server_key); 6548c2ecf20Sopenharmony_ci if (ret < 0) 6558c2ecf20Sopenharmony_ci return ret; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce)); 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci challenge.version = htonl(2); 6608c2ecf20Sopenharmony_ci challenge.nonce = htonl(conn->security_nonce); 6618c2ecf20Sopenharmony_ci challenge.min_level = htonl(0); 6628c2ecf20Sopenharmony_ci challenge.__padding = 0; 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci msg.msg_name = &conn->params.peer->srx.transport; 6658c2ecf20Sopenharmony_ci msg.msg_namelen = conn->params.peer->srx.transport_len; 6668c2ecf20Sopenharmony_ci msg.msg_control = NULL; 6678c2ecf20Sopenharmony_ci msg.msg_controllen = 0; 6688c2ecf20Sopenharmony_ci msg.msg_flags = 0; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci whdr.epoch = htonl(conn->proto.epoch); 6718c2ecf20Sopenharmony_ci whdr.cid = htonl(conn->proto.cid); 6728c2ecf20Sopenharmony_ci whdr.callNumber = 0; 6738c2ecf20Sopenharmony_ci whdr.seq = 0; 6748c2ecf20Sopenharmony_ci whdr.type = RXRPC_PACKET_TYPE_CHALLENGE; 6758c2ecf20Sopenharmony_ci whdr.flags = conn->out_clientflag; 6768c2ecf20Sopenharmony_ci whdr.userStatus = 0; 6778c2ecf20Sopenharmony_ci whdr.securityIndex = conn->security_ix; 6788c2ecf20Sopenharmony_ci whdr._rsvd = 0; 6798c2ecf20Sopenharmony_ci whdr.serviceId = htons(conn->service_id); 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci iov[0].iov_base = &whdr; 6828c2ecf20Sopenharmony_ci iov[0].iov_len = sizeof(whdr); 6838c2ecf20Sopenharmony_ci iov[1].iov_base = &challenge; 6848c2ecf20Sopenharmony_ci iov[1].iov_len = sizeof(challenge); 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci len = iov[0].iov_len + iov[1].iov_len; 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci serial = atomic_inc_return(&conn->serial); 6898c2ecf20Sopenharmony_ci whdr.serial = htonl(serial); 6908c2ecf20Sopenharmony_ci _proto("Tx CHALLENGE %%%u", serial); 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); 6938c2ecf20Sopenharmony_ci if (ret < 0) { 6948c2ecf20Sopenharmony_ci trace_rxrpc_tx_fail(conn->debug_id, serial, ret, 6958c2ecf20Sopenharmony_ci rxrpc_tx_point_rxkad_challenge); 6968c2ecf20Sopenharmony_ci return -EAGAIN; 6978c2ecf20Sopenharmony_ci } 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci conn->params.peer->last_tx_at = ktime_get_seconds(); 7008c2ecf20Sopenharmony_ci trace_rxrpc_tx_packet(conn->debug_id, &whdr, 7018c2ecf20Sopenharmony_ci rxrpc_tx_point_rxkad_challenge); 7028c2ecf20Sopenharmony_ci _leave(" = 0"); 7038c2ecf20Sopenharmony_ci return 0; 7048c2ecf20Sopenharmony_ci} 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci/* 7078c2ecf20Sopenharmony_ci * send a Kerberos security response 7088c2ecf20Sopenharmony_ci */ 7098c2ecf20Sopenharmony_cistatic int rxkad_send_response(struct rxrpc_connection *conn, 7108c2ecf20Sopenharmony_ci struct rxrpc_host_header *hdr, 7118c2ecf20Sopenharmony_ci struct rxkad_response *resp, 7128c2ecf20Sopenharmony_ci const struct rxkad_key *s2) 7138c2ecf20Sopenharmony_ci{ 7148c2ecf20Sopenharmony_ci struct rxrpc_wire_header whdr; 7158c2ecf20Sopenharmony_ci struct msghdr msg; 7168c2ecf20Sopenharmony_ci struct kvec iov[3]; 7178c2ecf20Sopenharmony_ci size_t len; 7188c2ecf20Sopenharmony_ci u32 serial; 7198c2ecf20Sopenharmony_ci int ret; 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci _enter(""); 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci msg.msg_name = &conn->params.peer->srx.transport; 7248c2ecf20Sopenharmony_ci msg.msg_namelen = conn->params.peer->srx.transport_len; 7258c2ecf20Sopenharmony_ci msg.msg_control = NULL; 7268c2ecf20Sopenharmony_ci msg.msg_controllen = 0; 7278c2ecf20Sopenharmony_ci msg.msg_flags = 0; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci memset(&whdr, 0, sizeof(whdr)); 7308c2ecf20Sopenharmony_ci whdr.epoch = htonl(hdr->epoch); 7318c2ecf20Sopenharmony_ci whdr.cid = htonl(hdr->cid); 7328c2ecf20Sopenharmony_ci whdr.type = RXRPC_PACKET_TYPE_RESPONSE; 7338c2ecf20Sopenharmony_ci whdr.flags = conn->out_clientflag; 7348c2ecf20Sopenharmony_ci whdr.securityIndex = hdr->securityIndex; 7358c2ecf20Sopenharmony_ci whdr.serviceId = htons(hdr->serviceId); 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci iov[0].iov_base = &whdr; 7388c2ecf20Sopenharmony_ci iov[0].iov_len = sizeof(whdr); 7398c2ecf20Sopenharmony_ci iov[1].iov_base = resp; 7408c2ecf20Sopenharmony_ci iov[1].iov_len = sizeof(*resp); 7418c2ecf20Sopenharmony_ci iov[2].iov_base = (void *)s2->ticket; 7428c2ecf20Sopenharmony_ci iov[2].iov_len = s2->ticket_len; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci serial = atomic_inc_return(&conn->serial); 7478c2ecf20Sopenharmony_ci whdr.serial = htonl(serial); 7488c2ecf20Sopenharmony_ci _proto("Tx RESPONSE %%%u", serial); 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len); 7518c2ecf20Sopenharmony_ci if (ret < 0) { 7528c2ecf20Sopenharmony_ci trace_rxrpc_tx_fail(conn->debug_id, serial, ret, 7538c2ecf20Sopenharmony_ci rxrpc_tx_point_rxkad_response); 7548c2ecf20Sopenharmony_ci return -EAGAIN; 7558c2ecf20Sopenharmony_ci } 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci conn->params.peer->last_tx_at = ktime_get_seconds(); 7588c2ecf20Sopenharmony_ci _leave(" = 0"); 7598c2ecf20Sopenharmony_ci return 0; 7608c2ecf20Sopenharmony_ci} 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci/* 7638c2ecf20Sopenharmony_ci * calculate the response checksum 7648c2ecf20Sopenharmony_ci */ 7658c2ecf20Sopenharmony_cistatic void rxkad_calc_response_checksum(struct rxkad_response *response) 7668c2ecf20Sopenharmony_ci{ 7678c2ecf20Sopenharmony_ci u32 csum = 1000003; 7688c2ecf20Sopenharmony_ci int loop; 7698c2ecf20Sopenharmony_ci u8 *p = (u8 *) response; 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci for (loop = sizeof(*response); loop > 0; loop--) 7728c2ecf20Sopenharmony_ci csum = csum * 0x10204081 + *p++; 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci response->encrypted.checksum = htonl(csum); 7758c2ecf20Sopenharmony_ci} 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci/* 7788c2ecf20Sopenharmony_ci * encrypt the response packet 7798c2ecf20Sopenharmony_ci */ 7808c2ecf20Sopenharmony_cistatic int rxkad_encrypt_response(struct rxrpc_connection *conn, 7818c2ecf20Sopenharmony_ci struct rxkad_response *resp, 7828c2ecf20Sopenharmony_ci const struct rxkad_key *s2) 7838c2ecf20Sopenharmony_ci{ 7848c2ecf20Sopenharmony_ci struct skcipher_request *req; 7858c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 7868c2ecf20Sopenharmony_ci struct scatterlist sg[1]; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS); 7898c2ecf20Sopenharmony_ci if (!req) 7908c2ecf20Sopenharmony_ci return -ENOMEM; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci /* continue encrypting from where we left off */ 7938c2ecf20Sopenharmony_ci memcpy(&iv, s2->session_key, sizeof(iv)); 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci sg_init_table(sg, 1); 7968c2ecf20Sopenharmony_ci sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); 7978c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, conn->cipher); 7988c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 7998c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); 8008c2ecf20Sopenharmony_ci crypto_skcipher_encrypt(req); 8018c2ecf20Sopenharmony_ci skcipher_request_free(req); 8028c2ecf20Sopenharmony_ci return 0; 8038c2ecf20Sopenharmony_ci} 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci/* 8068c2ecf20Sopenharmony_ci * respond to a challenge packet 8078c2ecf20Sopenharmony_ci */ 8088c2ecf20Sopenharmony_cistatic int rxkad_respond_to_challenge(struct rxrpc_connection *conn, 8098c2ecf20Sopenharmony_ci struct sk_buff *skb, 8108c2ecf20Sopenharmony_ci u32 *_abort_code) 8118c2ecf20Sopenharmony_ci{ 8128c2ecf20Sopenharmony_ci const struct rxrpc_key_token *token; 8138c2ecf20Sopenharmony_ci struct rxkad_challenge challenge; 8148c2ecf20Sopenharmony_ci struct rxkad_response *resp; 8158c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 8168c2ecf20Sopenharmony_ci const char *eproto; 8178c2ecf20Sopenharmony_ci u32 version, nonce, min_level, abort_code; 8188c2ecf20Sopenharmony_ci int ret; 8198c2ecf20Sopenharmony_ci 8208c2ecf20Sopenharmony_ci _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_ci eproto = tracepoint_string("chall_no_key"); 8238c2ecf20Sopenharmony_ci abort_code = RX_PROTOCOL_ERROR; 8248c2ecf20Sopenharmony_ci if (!conn->params.key) 8258c2ecf20Sopenharmony_ci goto protocol_error; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci abort_code = RXKADEXPIRED; 8288c2ecf20Sopenharmony_ci ret = key_validate(conn->params.key); 8298c2ecf20Sopenharmony_ci if (ret < 0) 8308c2ecf20Sopenharmony_ci goto other_error; 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci eproto = tracepoint_string("chall_short"); 8338c2ecf20Sopenharmony_ci abort_code = RXKADPACKETSHORT; 8348c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), 8358c2ecf20Sopenharmony_ci &challenge, sizeof(challenge)) < 0) 8368c2ecf20Sopenharmony_ci goto protocol_error; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci version = ntohl(challenge.version); 8398c2ecf20Sopenharmony_ci nonce = ntohl(challenge.nonce); 8408c2ecf20Sopenharmony_ci min_level = ntohl(challenge.min_level); 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }", 8438c2ecf20Sopenharmony_ci sp->hdr.serial, version, nonce, min_level); 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci eproto = tracepoint_string("chall_ver"); 8468c2ecf20Sopenharmony_ci abort_code = RXKADINCONSISTENCY; 8478c2ecf20Sopenharmony_ci if (version != RXKAD_VERSION) 8488c2ecf20Sopenharmony_ci goto protocol_error; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci abort_code = RXKADLEVELFAIL; 8518c2ecf20Sopenharmony_ci ret = -EACCES; 8528c2ecf20Sopenharmony_ci if (conn->params.security_level < min_level) 8538c2ecf20Sopenharmony_ci goto other_error; 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci token = conn->params.key->payload.data[0]; 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci /* build the response packet */ 8588c2ecf20Sopenharmony_ci resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS); 8598c2ecf20Sopenharmony_ci if (!resp) 8608c2ecf20Sopenharmony_ci return -ENOMEM; 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci resp->version = htonl(RXKAD_VERSION); 8638c2ecf20Sopenharmony_ci resp->encrypted.epoch = htonl(conn->proto.epoch); 8648c2ecf20Sopenharmony_ci resp->encrypted.cid = htonl(conn->proto.cid); 8658c2ecf20Sopenharmony_ci resp->encrypted.securityIndex = htonl(conn->security_ix); 8668c2ecf20Sopenharmony_ci resp->encrypted.inc_nonce = htonl(nonce + 1); 8678c2ecf20Sopenharmony_ci resp->encrypted.level = htonl(conn->params.security_level); 8688c2ecf20Sopenharmony_ci resp->kvno = htonl(token->kad->kvno); 8698c2ecf20Sopenharmony_ci resp->ticket_len = htonl(token->kad->ticket_len); 8708c2ecf20Sopenharmony_ci resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter); 8718c2ecf20Sopenharmony_ci resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter); 8728c2ecf20Sopenharmony_ci resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter); 8738c2ecf20Sopenharmony_ci resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter); 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci /* calculate the response checksum and then do the encryption */ 8768c2ecf20Sopenharmony_ci rxkad_calc_response_checksum(resp); 8778c2ecf20Sopenharmony_ci ret = rxkad_encrypt_response(conn, resp, token->kad); 8788c2ecf20Sopenharmony_ci if (ret == 0) 8798c2ecf20Sopenharmony_ci ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad); 8808c2ecf20Sopenharmony_ci kfree(resp); 8818c2ecf20Sopenharmony_ci return ret; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ciprotocol_error: 8848c2ecf20Sopenharmony_ci trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); 8858c2ecf20Sopenharmony_ci ret = -EPROTO; 8868c2ecf20Sopenharmony_ciother_error: 8878c2ecf20Sopenharmony_ci *_abort_code = abort_code; 8888c2ecf20Sopenharmony_ci return ret; 8898c2ecf20Sopenharmony_ci} 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_ci/* 8928c2ecf20Sopenharmony_ci * decrypt the kerberos IV ticket in the response 8938c2ecf20Sopenharmony_ci */ 8948c2ecf20Sopenharmony_cistatic int rxkad_decrypt_ticket(struct rxrpc_connection *conn, 8958c2ecf20Sopenharmony_ci struct sk_buff *skb, 8968c2ecf20Sopenharmony_ci void *ticket, size_t ticket_len, 8978c2ecf20Sopenharmony_ci struct rxrpc_crypt *_session_key, 8988c2ecf20Sopenharmony_ci time64_t *_expiry, 8998c2ecf20Sopenharmony_ci u32 *_abort_code) 9008c2ecf20Sopenharmony_ci{ 9018c2ecf20Sopenharmony_ci struct skcipher_request *req; 9028c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 9038c2ecf20Sopenharmony_ci struct rxrpc_crypt iv, key; 9048c2ecf20Sopenharmony_ci struct scatterlist sg[1]; 9058c2ecf20Sopenharmony_ci struct in_addr addr; 9068c2ecf20Sopenharmony_ci unsigned int life; 9078c2ecf20Sopenharmony_ci const char *eproto; 9088c2ecf20Sopenharmony_ci time64_t issue, now; 9098c2ecf20Sopenharmony_ci bool little_endian; 9108c2ecf20Sopenharmony_ci int ret; 9118c2ecf20Sopenharmony_ci u32 abort_code; 9128c2ecf20Sopenharmony_ci u8 *p, *q, *name, *end; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key)); 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ci *_expiry = 0; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci ret = key_validate(conn->server_key); 9198c2ecf20Sopenharmony_ci if (ret < 0) { 9208c2ecf20Sopenharmony_ci switch (ret) { 9218c2ecf20Sopenharmony_ci case -EKEYEXPIRED: 9228c2ecf20Sopenharmony_ci abort_code = RXKADEXPIRED; 9238c2ecf20Sopenharmony_ci goto other_error; 9248c2ecf20Sopenharmony_ci default: 9258c2ecf20Sopenharmony_ci abort_code = RXKADNOAUTH; 9268c2ecf20Sopenharmony_ci goto other_error; 9278c2ecf20Sopenharmony_ci } 9288c2ecf20Sopenharmony_ci } 9298c2ecf20Sopenharmony_ci 9308c2ecf20Sopenharmony_ci ASSERT(conn->server_key->payload.data[0] != NULL); 9318c2ecf20Sopenharmony_ci ASSERTCMP((unsigned long) ticket & 7UL, ==, 0); 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv)); 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci ret = -ENOMEM; 9368c2ecf20Sopenharmony_ci req = skcipher_request_alloc(conn->server_key->payload.data[0], 9378c2ecf20Sopenharmony_ci GFP_NOFS); 9388c2ecf20Sopenharmony_ci if (!req) 9398c2ecf20Sopenharmony_ci goto temporary_error; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci sg_init_one(&sg[0], ticket, ticket_len); 9428c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 9438c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x); 9448c2ecf20Sopenharmony_ci crypto_skcipher_decrypt(req); 9458c2ecf20Sopenharmony_ci skcipher_request_free(req); 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci p = ticket; 9488c2ecf20Sopenharmony_ci end = p + ticket_len; 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci#define Z(field) \ 9518c2ecf20Sopenharmony_ci ({ \ 9528c2ecf20Sopenharmony_ci u8 *__str = p; \ 9538c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_bad_"#field); \ 9548c2ecf20Sopenharmony_ci q = memchr(p, 0, end - p); \ 9558c2ecf20Sopenharmony_ci if (!q || q - p > (field##_SZ)) \ 9568c2ecf20Sopenharmony_ci goto bad_ticket; \ 9578c2ecf20Sopenharmony_ci for (; p < q; p++) \ 9588c2ecf20Sopenharmony_ci if (!isprint(*p)) \ 9598c2ecf20Sopenharmony_ci goto bad_ticket; \ 9608c2ecf20Sopenharmony_ci p++; \ 9618c2ecf20Sopenharmony_ci __str; \ 9628c2ecf20Sopenharmony_ci }) 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci /* extract the ticket flags */ 9658c2ecf20Sopenharmony_ci _debug("KIV FLAGS: %x", *p); 9668c2ecf20Sopenharmony_ci little_endian = *p & 1; 9678c2ecf20Sopenharmony_ci p++; 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci /* extract the authentication name */ 9708c2ecf20Sopenharmony_ci name = Z(ANAME); 9718c2ecf20Sopenharmony_ci _debug("KIV ANAME: %s", name); 9728c2ecf20Sopenharmony_ci 9738c2ecf20Sopenharmony_ci /* extract the principal's instance */ 9748c2ecf20Sopenharmony_ci name = Z(INST); 9758c2ecf20Sopenharmony_ci _debug("KIV INST : %s", name); 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci /* extract the principal's authentication domain */ 9788c2ecf20Sopenharmony_ci name = Z(REALM); 9798c2ecf20Sopenharmony_ci _debug("KIV REALM: %s", name); 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_bad_len"); 9828c2ecf20Sopenharmony_ci if (end - p < 4 + 8 + 4 + 2) 9838c2ecf20Sopenharmony_ci goto bad_ticket; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci /* get the IPv4 address of the entity that requested the ticket */ 9868c2ecf20Sopenharmony_ci memcpy(&addr, p, sizeof(addr)); 9878c2ecf20Sopenharmony_ci p += 4; 9888c2ecf20Sopenharmony_ci _debug("KIV ADDR : %pI4", &addr); 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci /* get the session key from the ticket */ 9918c2ecf20Sopenharmony_ci memcpy(&key, p, sizeof(key)); 9928c2ecf20Sopenharmony_ci p += 8; 9938c2ecf20Sopenharmony_ci _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1])); 9948c2ecf20Sopenharmony_ci memcpy(_session_key, &key, sizeof(key)); 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci /* get the ticket's lifetime */ 9978c2ecf20Sopenharmony_ci life = *p++ * 5 * 60; 9988c2ecf20Sopenharmony_ci _debug("KIV LIFE : %u", life); 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci /* get the issue time of the ticket */ 10018c2ecf20Sopenharmony_ci if (little_endian) { 10028c2ecf20Sopenharmony_ci __le32 stamp; 10038c2ecf20Sopenharmony_ci memcpy(&stamp, p, 4); 10048c2ecf20Sopenharmony_ci issue = rxrpc_u32_to_time64(le32_to_cpu(stamp)); 10058c2ecf20Sopenharmony_ci } else { 10068c2ecf20Sopenharmony_ci __be32 stamp; 10078c2ecf20Sopenharmony_ci memcpy(&stamp, p, 4); 10088c2ecf20Sopenharmony_ci issue = rxrpc_u32_to_time64(be32_to_cpu(stamp)); 10098c2ecf20Sopenharmony_ci } 10108c2ecf20Sopenharmony_ci p += 4; 10118c2ecf20Sopenharmony_ci now = ktime_get_real_seconds(); 10128c2ecf20Sopenharmony_ci _debug("KIV ISSUE: %llx [%llx]", issue, now); 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci /* check the ticket is in date */ 10158c2ecf20Sopenharmony_ci if (issue > now) { 10168c2ecf20Sopenharmony_ci abort_code = RXKADNOAUTH; 10178c2ecf20Sopenharmony_ci ret = -EKEYREJECTED; 10188c2ecf20Sopenharmony_ci goto other_error; 10198c2ecf20Sopenharmony_ci } 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci if (issue < now - life) { 10228c2ecf20Sopenharmony_ci abort_code = RXKADEXPIRED; 10238c2ecf20Sopenharmony_ci ret = -EKEYEXPIRED; 10248c2ecf20Sopenharmony_ci goto other_error; 10258c2ecf20Sopenharmony_ci } 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci *_expiry = issue + life; 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ci /* get the service name */ 10308c2ecf20Sopenharmony_ci name = Z(SNAME); 10318c2ecf20Sopenharmony_ci _debug("KIV SNAME: %s", name); 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_ci /* get the service instance name */ 10348c2ecf20Sopenharmony_ci name = Z(INST); 10358c2ecf20Sopenharmony_ci _debug("KIV SINST: %s", name); 10368c2ecf20Sopenharmony_ci return 0; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_cibad_ticket: 10398c2ecf20Sopenharmony_ci trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); 10408c2ecf20Sopenharmony_ci abort_code = RXKADBADTICKET; 10418c2ecf20Sopenharmony_ci ret = -EPROTO; 10428c2ecf20Sopenharmony_ciother_error: 10438c2ecf20Sopenharmony_ci *_abort_code = abort_code; 10448c2ecf20Sopenharmony_ci return ret; 10458c2ecf20Sopenharmony_citemporary_error: 10468c2ecf20Sopenharmony_ci return ret; 10478c2ecf20Sopenharmony_ci} 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci/* 10508c2ecf20Sopenharmony_ci * decrypt the response packet 10518c2ecf20Sopenharmony_ci */ 10528c2ecf20Sopenharmony_cistatic void rxkad_decrypt_response(struct rxrpc_connection *conn, 10538c2ecf20Sopenharmony_ci struct rxkad_response *resp, 10548c2ecf20Sopenharmony_ci const struct rxrpc_crypt *session_key) 10558c2ecf20Sopenharmony_ci{ 10568c2ecf20Sopenharmony_ci struct skcipher_request *req = rxkad_ci_req; 10578c2ecf20Sopenharmony_ci struct scatterlist sg[1]; 10588c2ecf20Sopenharmony_ci struct rxrpc_crypt iv; 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci _enter(",,%08x%08x", 10618c2ecf20Sopenharmony_ci ntohl(session_key->n[0]), ntohl(session_key->n[1])); 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci mutex_lock(&rxkad_ci_mutex); 10648c2ecf20Sopenharmony_ci if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x, 10658c2ecf20Sopenharmony_ci sizeof(*session_key)) < 0) 10668c2ecf20Sopenharmony_ci BUG(); 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci memcpy(&iv, session_key, sizeof(iv)); 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci sg_init_table(sg, 1); 10718c2ecf20Sopenharmony_ci sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); 10728c2ecf20Sopenharmony_ci skcipher_request_set_sync_tfm(req, rxkad_ci); 10738c2ecf20Sopenharmony_ci skcipher_request_set_callback(req, 0, NULL, NULL); 10748c2ecf20Sopenharmony_ci skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); 10758c2ecf20Sopenharmony_ci crypto_skcipher_decrypt(req); 10768c2ecf20Sopenharmony_ci skcipher_request_zero(req); 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_ci mutex_unlock(&rxkad_ci_mutex); 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci _leave(""); 10818c2ecf20Sopenharmony_ci} 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci/* 10848c2ecf20Sopenharmony_ci * verify a response 10858c2ecf20Sopenharmony_ci */ 10868c2ecf20Sopenharmony_cistatic int rxkad_verify_response(struct rxrpc_connection *conn, 10878c2ecf20Sopenharmony_ci struct sk_buff *skb, 10888c2ecf20Sopenharmony_ci u32 *_abort_code) 10898c2ecf20Sopenharmony_ci{ 10908c2ecf20Sopenharmony_ci struct rxkad_response *response; 10918c2ecf20Sopenharmony_ci struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 10928c2ecf20Sopenharmony_ci struct rxrpc_crypt session_key; 10938c2ecf20Sopenharmony_ci const char *eproto; 10948c2ecf20Sopenharmony_ci time64_t expiry; 10958c2ecf20Sopenharmony_ci void *ticket; 10968c2ecf20Sopenharmony_ci u32 abort_code, version, kvno, ticket_len, level; 10978c2ecf20Sopenharmony_ci __be32 csum; 10988c2ecf20Sopenharmony_ci int ret, i; 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ci _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key)); 11018c2ecf20Sopenharmony_ci 11028c2ecf20Sopenharmony_ci ret = -ENOMEM; 11038c2ecf20Sopenharmony_ci response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS); 11048c2ecf20Sopenharmony_ci if (!response) 11058c2ecf20Sopenharmony_ci goto temporary_error; 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_short"); 11088c2ecf20Sopenharmony_ci abort_code = RXKADPACKETSHORT; 11098c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), 11108c2ecf20Sopenharmony_ci response, sizeof(*response)) < 0) 11118c2ecf20Sopenharmony_ci goto protocol_error; 11128c2ecf20Sopenharmony_ci if (!pskb_pull(skb, sizeof(*response))) 11138c2ecf20Sopenharmony_ci BUG(); 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci version = ntohl(response->version); 11168c2ecf20Sopenharmony_ci ticket_len = ntohl(response->ticket_len); 11178c2ecf20Sopenharmony_ci kvno = ntohl(response->kvno); 11188c2ecf20Sopenharmony_ci _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }", 11198c2ecf20Sopenharmony_ci sp->hdr.serial, version, kvno, ticket_len); 11208c2ecf20Sopenharmony_ci 11218c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_ver"); 11228c2ecf20Sopenharmony_ci abort_code = RXKADINCONSISTENCY; 11238c2ecf20Sopenharmony_ci if (version != RXKAD_VERSION) 11248c2ecf20Sopenharmony_ci goto protocol_error; 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_tktlen"); 11278c2ecf20Sopenharmony_ci abort_code = RXKADTICKETLEN; 11288c2ecf20Sopenharmony_ci if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN) 11298c2ecf20Sopenharmony_ci goto protocol_error; 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_unkkey"); 11328c2ecf20Sopenharmony_ci abort_code = RXKADUNKNOWNKEY; 11338c2ecf20Sopenharmony_ci if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5) 11348c2ecf20Sopenharmony_ci goto protocol_error; 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_ci /* extract the kerberos ticket and decrypt and decode it */ 11378c2ecf20Sopenharmony_ci ret = -ENOMEM; 11388c2ecf20Sopenharmony_ci ticket = kmalloc(ticket_len, GFP_NOFS); 11398c2ecf20Sopenharmony_ci if (!ticket) 11408c2ecf20Sopenharmony_ci goto temporary_error_free_resp; 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_tkt_short"); 11438c2ecf20Sopenharmony_ci abort_code = RXKADPACKETSHORT; 11448c2ecf20Sopenharmony_ci if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), 11458c2ecf20Sopenharmony_ci ticket, ticket_len) < 0) 11468c2ecf20Sopenharmony_ci goto protocol_error_free; 11478c2ecf20Sopenharmony_ci 11488c2ecf20Sopenharmony_ci ret = rxkad_decrypt_ticket(conn, skb, ticket, ticket_len, &session_key, 11498c2ecf20Sopenharmony_ci &expiry, _abort_code); 11508c2ecf20Sopenharmony_ci if (ret < 0) 11518c2ecf20Sopenharmony_ci goto temporary_error_free_ticket; 11528c2ecf20Sopenharmony_ci 11538c2ecf20Sopenharmony_ci /* use the session key from inside the ticket to decrypt the 11548c2ecf20Sopenharmony_ci * response */ 11558c2ecf20Sopenharmony_ci rxkad_decrypt_response(conn, response, &session_key); 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_param"); 11588c2ecf20Sopenharmony_ci abort_code = RXKADSEALEDINCON; 11598c2ecf20Sopenharmony_ci if (ntohl(response->encrypted.epoch) != conn->proto.epoch) 11608c2ecf20Sopenharmony_ci goto protocol_error_free; 11618c2ecf20Sopenharmony_ci if (ntohl(response->encrypted.cid) != conn->proto.cid) 11628c2ecf20Sopenharmony_ci goto protocol_error_free; 11638c2ecf20Sopenharmony_ci if (ntohl(response->encrypted.securityIndex) != conn->security_ix) 11648c2ecf20Sopenharmony_ci goto protocol_error_free; 11658c2ecf20Sopenharmony_ci csum = response->encrypted.checksum; 11668c2ecf20Sopenharmony_ci response->encrypted.checksum = 0; 11678c2ecf20Sopenharmony_ci rxkad_calc_response_checksum(response); 11688c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_csum"); 11698c2ecf20Sopenharmony_ci if (response->encrypted.checksum != csum) 11708c2ecf20Sopenharmony_ci goto protocol_error_free; 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci spin_lock(&conn->bundle->channel_lock); 11738c2ecf20Sopenharmony_ci for (i = 0; i < RXRPC_MAXCALLS; i++) { 11748c2ecf20Sopenharmony_ci struct rxrpc_call *call; 11758c2ecf20Sopenharmony_ci u32 call_id = ntohl(response->encrypted.call_id[i]); 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_callid"); 11788c2ecf20Sopenharmony_ci if (call_id > INT_MAX) 11798c2ecf20Sopenharmony_ci goto protocol_error_unlock; 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_callctr"); 11828c2ecf20Sopenharmony_ci if (call_id < conn->channels[i].call_counter) 11838c2ecf20Sopenharmony_ci goto protocol_error_unlock; 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_callst"); 11868c2ecf20Sopenharmony_ci if (call_id > conn->channels[i].call_counter) { 11878c2ecf20Sopenharmony_ci call = rcu_dereference_protected( 11888c2ecf20Sopenharmony_ci conn->channels[i].call, 11898c2ecf20Sopenharmony_ci lockdep_is_held(&conn->bundle->channel_lock)); 11908c2ecf20Sopenharmony_ci if (call && call->state < RXRPC_CALL_COMPLETE) 11918c2ecf20Sopenharmony_ci goto protocol_error_unlock; 11928c2ecf20Sopenharmony_ci conn->channels[i].call_counter = call_id; 11938c2ecf20Sopenharmony_ci } 11948c2ecf20Sopenharmony_ci } 11958c2ecf20Sopenharmony_ci spin_unlock(&conn->bundle->channel_lock); 11968c2ecf20Sopenharmony_ci 11978c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_seq"); 11988c2ecf20Sopenharmony_ci abort_code = RXKADOUTOFSEQUENCE; 11998c2ecf20Sopenharmony_ci if (ntohl(response->encrypted.inc_nonce) != conn->security_nonce + 1) 12008c2ecf20Sopenharmony_ci goto protocol_error_free; 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci eproto = tracepoint_string("rxkad_rsp_level"); 12038c2ecf20Sopenharmony_ci abort_code = RXKADLEVELFAIL; 12048c2ecf20Sopenharmony_ci level = ntohl(response->encrypted.level); 12058c2ecf20Sopenharmony_ci if (level > RXRPC_SECURITY_ENCRYPT) 12068c2ecf20Sopenharmony_ci goto protocol_error_free; 12078c2ecf20Sopenharmony_ci conn->params.security_level = level; 12088c2ecf20Sopenharmony_ci 12098c2ecf20Sopenharmony_ci /* create a key to hold the security data and expiration time - after 12108c2ecf20Sopenharmony_ci * this the connection security can be handled in exactly the same way 12118c2ecf20Sopenharmony_ci * as for a client connection */ 12128c2ecf20Sopenharmony_ci ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno); 12138c2ecf20Sopenharmony_ci if (ret < 0) 12148c2ecf20Sopenharmony_ci goto temporary_error_free_ticket; 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci kfree(ticket); 12178c2ecf20Sopenharmony_ci kfree(response); 12188c2ecf20Sopenharmony_ci _leave(" = 0"); 12198c2ecf20Sopenharmony_ci return 0; 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ciprotocol_error_unlock: 12228c2ecf20Sopenharmony_ci spin_unlock(&conn->bundle->channel_lock); 12238c2ecf20Sopenharmony_ciprotocol_error_free: 12248c2ecf20Sopenharmony_ci kfree(ticket); 12258c2ecf20Sopenharmony_ciprotocol_error: 12268c2ecf20Sopenharmony_ci kfree(response); 12278c2ecf20Sopenharmony_ci trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); 12288c2ecf20Sopenharmony_ci *_abort_code = abort_code; 12298c2ecf20Sopenharmony_ci return -EPROTO; 12308c2ecf20Sopenharmony_ci 12318c2ecf20Sopenharmony_citemporary_error_free_ticket: 12328c2ecf20Sopenharmony_ci kfree(ticket); 12338c2ecf20Sopenharmony_citemporary_error_free_resp: 12348c2ecf20Sopenharmony_ci kfree(response); 12358c2ecf20Sopenharmony_citemporary_error: 12368c2ecf20Sopenharmony_ci /* Ignore the response packet if we got a temporary error such as 12378c2ecf20Sopenharmony_ci * ENOMEM. We just want to send the challenge again. Note that we 12388c2ecf20Sopenharmony_ci * also come out this way if the ticket decryption fails. 12398c2ecf20Sopenharmony_ci */ 12408c2ecf20Sopenharmony_ci return ret; 12418c2ecf20Sopenharmony_ci} 12428c2ecf20Sopenharmony_ci 12438c2ecf20Sopenharmony_ci/* 12448c2ecf20Sopenharmony_ci * clear the connection security 12458c2ecf20Sopenharmony_ci */ 12468c2ecf20Sopenharmony_cistatic void rxkad_clear(struct rxrpc_connection *conn) 12478c2ecf20Sopenharmony_ci{ 12488c2ecf20Sopenharmony_ci _enter(""); 12498c2ecf20Sopenharmony_ci 12508c2ecf20Sopenharmony_ci if (conn->cipher) 12518c2ecf20Sopenharmony_ci crypto_free_sync_skcipher(conn->cipher); 12528c2ecf20Sopenharmony_ci} 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_ci/* 12558c2ecf20Sopenharmony_ci * Initialise the rxkad security service. 12568c2ecf20Sopenharmony_ci */ 12578c2ecf20Sopenharmony_cistatic int rxkad_init(void) 12588c2ecf20Sopenharmony_ci{ 12598c2ecf20Sopenharmony_ci struct crypto_sync_skcipher *tfm; 12608c2ecf20Sopenharmony_ci struct skcipher_request *req; 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ci /* pin the cipher we need so that the crypto layer doesn't invoke 12638c2ecf20Sopenharmony_ci * keventd to go get it */ 12648c2ecf20Sopenharmony_ci tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0); 12658c2ecf20Sopenharmony_ci if (IS_ERR(tfm)) 12668c2ecf20Sopenharmony_ci return PTR_ERR(tfm); 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci req = skcipher_request_alloc(&tfm->base, GFP_KERNEL); 12698c2ecf20Sopenharmony_ci if (!req) 12708c2ecf20Sopenharmony_ci goto nomem_tfm; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci rxkad_ci_req = req; 12738c2ecf20Sopenharmony_ci rxkad_ci = tfm; 12748c2ecf20Sopenharmony_ci return 0; 12758c2ecf20Sopenharmony_ci 12768c2ecf20Sopenharmony_cinomem_tfm: 12778c2ecf20Sopenharmony_ci crypto_free_sync_skcipher(tfm); 12788c2ecf20Sopenharmony_ci return -ENOMEM; 12798c2ecf20Sopenharmony_ci} 12808c2ecf20Sopenharmony_ci 12818c2ecf20Sopenharmony_ci/* 12828c2ecf20Sopenharmony_ci * Clean up the rxkad security service. 12838c2ecf20Sopenharmony_ci */ 12848c2ecf20Sopenharmony_cistatic void rxkad_exit(void) 12858c2ecf20Sopenharmony_ci{ 12868c2ecf20Sopenharmony_ci crypto_free_sync_skcipher(rxkad_ci); 12878c2ecf20Sopenharmony_ci skcipher_request_free(rxkad_ci_req); 12888c2ecf20Sopenharmony_ci} 12898c2ecf20Sopenharmony_ci 12908c2ecf20Sopenharmony_ci/* 12918c2ecf20Sopenharmony_ci * RxRPC Kerberos-based security 12928c2ecf20Sopenharmony_ci */ 12938c2ecf20Sopenharmony_ciconst struct rxrpc_security rxkad = { 12948c2ecf20Sopenharmony_ci .name = "rxkad", 12958c2ecf20Sopenharmony_ci .security_index = RXRPC_SECURITY_RXKAD, 12968c2ecf20Sopenharmony_ci .no_key_abort = RXKADUNKNOWNKEY, 12978c2ecf20Sopenharmony_ci .init = rxkad_init, 12988c2ecf20Sopenharmony_ci .exit = rxkad_exit, 12998c2ecf20Sopenharmony_ci .init_connection_security = rxkad_init_connection_security, 13008c2ecf20Sopenharmony_ci .prime_packet_security = rxkad_prime_packet_security, 13018c2ecf20Sopenharmony_ci .secure_packet = rxkad_secure_packet, 13028c2ecf20Sopenharmony_ci .verify_packet = rxkad_verify_packet, 13038c2ecf20Sopenharmony_ci .free_call_crypto = rxkad_free_call_crypto, 13048c2ecf20Sopenharmony_ci .locate_data = rxkad_locate_data, 13058c2ecf20Sopenharmony_ci .issue_challenge = rxkad_issue_challenge, 13068c2ecf20Sopenharmony_ci .respond_to_challenge = rxkad_respond_to_challenge, 13078c2ecf20Sopenharmony_ci .verify_response = rxkad_verify_response, 13088c2ecf20Sopenharmony_ci .clear = rxkad_clear, 13098c2ecf20Sopenharmony_ci}; 1310