Lines Matching +full:- +full:eproto
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kerberos-based RxRPC security
18 #include <linux/key-type.h>
21 #include <keys/rxrpc-type.h>
22 #include "ar-internal.h"
57 * The data should be the 8-byte secret key.
63 if (prep->datalen != 8) in rxkad_preparse_server_key()
64 return -EINVAL; in rxkad_preparse_server_key()
66 memcpy(&prep->payload.data[2], prep->data, 8); in rxkad_preparse_server_key()
74 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0) in rxkad_preparse_server_key()
77 prep->payload.data[0] = ci; in rxkad_preparse_server_key()
85 if (prep->payload.data[0]) in rxkad_free_preparse_server_key()
86 crypto_free_skcipher(prep->payload.data[0]); in rxkad_free_preparse_server_key()
91 if (key->payload.data[0]) { in rxkad_destroy_server_key()
92 crypto_free_skcipher(key->payload.data[0]); in rxkad_destroy_server_key()
93 key->payload.data[0] = NULL; in rxkad_destroy_server_key()
106 _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key)); in rxkad_init_connection_security()
108 conn->security_ix = token->security_index; in rxkad_init_connection_security()
117 if (crypto_sync_skcipher_setkey(ci, token->kad->session_key, in rxkad_init_connection_security()
118 sizeof(token->kad->session_key)) < 0) in rxkad_init_connection_security()
121 switch (conn->security_level) { in rxkad_init_connection_security()
127 ret = -EKEYREJECTED; in rxkad_init_connection_security()
135 conn->rxkad.cipher = ci; in rxkad_init_connection_security()
153 remain = min(remain, 65535 - sizeof(struct rxrpc_wire_header)); in rxkad_alloc_txbuf()
155 switch (call->conn->security_level) { in rxkad_alloc_txbuf()
174 txb->offset += shdr; in rxkad_alloc_txbuf()
175 txb->space -= shdr; in rxkad_alloc_txbuf()
195 if (!conn->key) in rxkad_prime_packet_security()
200 return -ENOMEM; in rxkad_prime_packet_security()
202 req = skcipher_request_alloc(&ci->base, GFP_NOFS); in rxkad_prime_packet_security()
205 return -ENOMEM; in rxkad_prime_packet_security()
208 token = conn->key->payload.data[0]; in rxkad_prime_packet_security()
209 memcpy(&iv, token->kad->session_key, sizeof(iv)); in rxkad_prime_packet_security()
211 tmpbuf[0] = htonl(conn->proto.epoch); in rxkad_prime_packet_security()
212 tmpbuf[1] = htonl(conn->proto.cid); in rxkad_prime_packet_security()
214 tmpbuf[3] = htonl(conn->security_ix); in rxkad_prime_packet_security()
223 memcpy(&conn->rxkad.csum_iv, tmpbuf + 2, sizeof(conn->rxkad.csum_iv)); in rxkad_prime_packet_security()
235 struct crypto_skcipher *tfm = &call->conn->rxkad.cipher->base; in rxkad_get_call_crypto()
254 struct rxrpc_wire_header *whdr = txb->kvec[0].iov_base; in rxkad_secure_packet_auth()
263 check = txb->seq ^ call->call_id; in rxkad_secure_packet_auth()
264 hdr->data_size = htonl((u32)check << 16 | txb->len); in rxkad_secure_packet_auth()
266 txb->len += sizeof(struct rxkad_level1_hdr); in rxkad_secure_packet_auth()
267 pad = txb->len; in rxkad_secure_packet_auth()
268 pad = RXKAD_ALIGN - pad; in rxkad_secure_packet_auth()
269 pad &= RXKAD_ALIGN - 1; in rxkad_secure_packet_auth()
271 memset(txb->kvec[0].iov_base + txb->offset, 0, pad); in rxkad_secure_packet_auth()
272 txb->len += pad; in rxkad_secure_packet_auth()
279 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_secure_packet_auth()
297 struct rxrpc_wire_header *whdr = txb->kvec[0].iov_base; in rxkad_secure_packet_encrypt()
307 check = txb->seq ^ call->call_id; in rxkad_secure_packet_encrypt()
309 rxkhdr->data_size = htonl(txb->len | (u32)check << 16); in rxkad_secure_packet_encrypt()
310 rxkhdr->checksum = 0; in rxkad_secure_packet_encrypt()
312 txb->len += sizeof(struct rxkad_level2_hdr); in rxkad_secure_packet_encrypt()
313 pad = txb->len; in rxkad_secure_packet_encrypt()
314 pad = RXKAD_ALIGN - pad; in rxkad_secure_packet_encrypt()
315 pad &= RXKAD_ALIGN - 1; in rxkad_secure_packet_encrypt()
317 memset(txb->kvec[0].iov_base + txb->offset, 0, pad); in rxkad_secure_packet_encrypt()
318 txb->len += pad; in rxkad_secure_packet_encrypt()
322 token = call->conn->key->payload.data[0]; in rxkad_secure_packet_encrypt()
323 memcpy(&iv, token->kad->session_key, sizeof(iv)); in rxkad_secure_packet_encrypt()
325 sg_init_one(&sg, rxkhdr, txb->len); in rxkad_secure_packet_encrypt()
326 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_secure_packet_encrypt()
328 skcipher_request_set_crypt(req, &sg, &sg, txb->len, iv.x); in rxkad_secure_packet_encrypt()
349 call->debug_id, key_serial(call->conn->key), in rxkad_secure_packet()
350 txb->seq, txb->len); in rxkad_secure_packet()
352 if (!call->conn->rxkad.cipher) in rxkad_secure_packet()
355 ret = key_validate(call->conn->key); in rxkad_secure_packet()
361 return -ENOMEM; in rxkad_secure_packet()
364 memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv)); in rxkad_secure_packet()
367 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); in rxkad_secure_packet()
368 x |= txb->seq & 0x3fffffff; in rxkad_secure_packet()
369 crypto.buf[0] = htonl(call->call_id); in rxkad_secure_packet()
373 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_secure_packet()
383 txb->cksum = htons(y); in rxkad_secure_packet()
385 switch (call->conn->security_level) { in rxkad_secure_packet()
396 ret = -EPERM; in rxkad_secure_packet()
422 if (sp->len < 8) in rxkad_verify_packet_1()
426 /* Decrypt the skbuff in-place. TODO: We really want to decrypt in rxkad_verify_packet_1()
430 ret = skb_to_sgvec(skb, sg, sp->offset, 8); in rxkad_verify_packet_1()
437 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_verify_packet_1()
444 if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) in rxkad_verify_packet_1()
447 sp->offset += sizeof(sechdr); in rxkad_verify_packet_1()
448 sp->len -= sizeof(sechdr); in rxkad_verify_packet_1()
454 check ^= seq ^ call->call_id; in rxkad_verify_packet_1()
459 if (data_size > sp->len) in rxkad_verify_packet_1()
462 sp->len = data_size; in rxkad_verify_packet_1()
484 _enter(",{%d}", sp->len); in rxkad_verify_packet_2()
486 if (sp->len < 8) in rxkad_verify_packet_2()
490 /* Decrypt the skbuff in-place. TODO: We really want to decrypt in rxkad_verify_packet_2()
494 nsg = skb_shinfo(skb)->nr_frags + 1; in rxkad_verify_packet_2()
500 return -ENOMEM; in rxkad_verify_packet_2()
504 ret = skb_to_sgvec(skb, sg, sp->offset, sp->len); in rxkad_verify_packet_2()
512 token = call->conn->key->payload.data[0]; in rxkad_verify_packet_2()
513 memcpy(&iv, token->kad->session_key, sizeof(iv)); in rxkad_verify_packet_2()
515 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_verify_packet_2()
517 skcipher_request_set_crypt(req, sg, sg, sp->len, iv.x); in rxkad_verify_packet_2()
524 if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) in rxkad_verify_packet_2()
527 sp->offset += sizeof(sechdr); in rxkad_verify_packet_2()
528 sp->len -= sizeof(sechdr); in rxkad_verify_packet_2()
534 check ^= seq ^ call->call_id; in rxkad_verify_packet_2()
540 if (data_size > sp->len) in rxkad_verify_packet_2()
544 sp->len = data_size; in rxkad_verify_packet_2()
561 rxrpc_seq_t seq = sp->hdr.seq; in rxkad_verify_packet()
567 call->debug_id, key_serial(call->conn->key), seq); in rxkad_verify_packet()
569 if (!call->conn->rxkad.cipher) in rxkad_verify_packet()
574 return -ENOMEM; in rxkad_verify_packet()
577 memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv)); in rxkad_verify_packet()
580 x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); in rxkad_verify_packet()
582 crypto.buf[0] = htonl(call->call_id); in rxkad_verify_packet()
586 skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher); in rxkad_verify_packet()
597 if (cksum != sp->hdr.cksum) { in rxkad_verify_packet()
603 switch (call->conn->security_level) { in rxkad_verify_packet()
614 ret = -ENOANO; in rxkad_verify_packet()
636 _enter("{%d}", conn->debug_id); in rxkad_issue_challenge()
638 get_random_bytes(&conn->rxkad.nonce, sizeof(conn->rxkad.nonce)); in rxkad_issue_challenge()
641 challenge.nonce = htonl(conn->rxkad.nonce); in rxkad_issue_challenge()
645 msg.msg_name = &conn->peer->srx.transport; in rxkad_issue_challenge()
646 msg.msg_namelen = conn->peer->srx.transport_len; in rxkad_issue_challenge()
651 whdr.epoch = htonl(conn->proto.epoch); in rxkad_issue_challenge()
652 whdr.cid = htonl(conn->proto.cid); in rxkad_issue_challenge()
656 whdr.flags = conn->out_clientflag; in rxkad_issue_challenge()
658 whdr.securityIndex = conn->security_ix; in rxkad_issue_challenge()
660 whdr.serviceId = htons(conn->service_id); in rxkad_issue_challenge()
672 ret = kernel_sendmsg(conn->local->socket, &msg, iov, 2, len); in rxkad_issue_challenge()
674 trace_rxrpc_tx_fail(conn->debug_id, serial, ret, in rxkad_issue_challenge()
676 return -EAGAIN; in rxkad_issue_challenge()
679 conn->peer->last_tx_at = ktime_get_seconds(); in rxkad_issue_challenge()
680 trace_rxrpc_tx_packet(conn->debug_id, &whdr, in rxkad_issue_challenge()
703 msg.msg_name = &conn->peer->srx.transport; in rxkad_send_response()
704 msg.msg_namelen = conn->peer->srx.transport_len; in rxkad_send_response()
710 whdr.epoch = htonl(hdr->epoch); in rxkad_send_response()
711 whdr.cid = htonl(hdr->cid); in rxkad_send_response()
713 whdr.flags = conn->out_clientflag; in rxkad_send_response()
714 whdr.securityIndex = hdr->securityIndex; in rxkad_send_response()
715 whdr.serviceId = htons(hdr->serviceId); in rxkad_send_response()
721 iov[2].iov_base = (void *)s2->ticket; in rxkad_send_response()
722 iov[2].iov_len = s2->ticket_len; in rxkad_send_response()
729 rxrpc_local_dont_fragment(conn->local, false); in rxkad_send_response()
730 ret = kernel_sendmsg(conn->local->socket, &msg, iov, 3, len); in rxkad_send_response()
732 trace_rxrpc_tx_fail(conn->debug_id, serial, ret, in rxkad_send_response()
734 return -EAGAIN; in rxkad_send_response()
737 conn->peer->last_tx_at = ktime_get_seconds(); in rxkad_send_response()
751 for (loop = sizeof(*response); loop > 0; loop--) in rxkad_calc_response_checksum()
754 response->encrypted.checksum = htonl(csum); in rxkad_calc_response_checksum()
768 req = skcipher_request_alloc(&conn->rxkad.cipher->base, GFP_NOFS); in rxkad_encrypt_response()
770 return -ENOMEM; in rxkad_encrypt_response()
773 memcpy(&iv, s2->session_key, sizeof(iv)); in rxkad_encrypt_response()
776 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); in rxkad_encrypt_response()
777 skcipher_request_set_sync_tfm(req, conn->rxkad.cipher); in rxkad_encrypt_response()
779 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); in rxkad_encrypt_response()
796 int ret = -EPROTO; in rxkad_respond_to_challenge()
798 _enter("{%d,%x}", conn->debug_id, key_serial(conn->key)); in rxkad_respond_to_challenge()
800 if (!conn->key) in rxkad_respond_to_challenge()
801 return rxrpc_abort_conn(conn, skb, RX_PROTOCOL_ERROR, -EPROTO, in rxkad_respond_to_challenge()
804 ret = key_validate(conn->key); in rxkad_respond_to_challenge()
811 return rxrpc_abort_conn(conn, skb, RXKADPACKETSHORT, -EPROTO, in rxkad_respond_to_challenge()
818 trace_rxrpc_rx_challenge(conn, sp->hdr.serial, version, nonce, min_level); in rxkad_respond_to_challenge()
821 return rxrpc_abort_conn(conn, skb, RXKADINCONSISTENCY, -EPROTO, in rxkad_respond_to_challenge()
824 if (conn->security_level < min_level) in rxkad_respond_to_challenge()
825 return rxrpc_abort_conn(conn, skb, RXKADLEVELFAIL, -EACCES, in rxkad_respond_to_challenge()
828 token = conn->key->payload.data[0]; in rxkad_respond_to_challenge()
833 return -ENOMEM; in rxkad_respond_to_challenge()
835 resp->version = htonl(RXKAD_VERSION); in rxkad_respond_to_challenge()
836 resp->encrypted.epoch = htonl(conn->proto.epoch); in rxkad_respond_to_challenge()
837 resp->encrypted.cid = htonl(conn->proto.cid); in rxkad_respond_to_challenge()
838 resp->encrypted.securityIndex = htonl(conn->security_ix); in rxkad_respond_to_challenge()
839 resp->encrypted.inc_nonce = htonl(nonce + 1); in rxkad_respond_to_challenge()
840 resp->encrypted.level = htonl(conn->security_level); in rxkad_respond_to_challenge()
841 resp->kvno = htonl(token->kad->kvno); in rxkad_respond_to_challenge()
842 resp->ticket_len = htonl(token->kad->ticket_len); in rxkad_respond_to_challenge()
843 resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter); in rxkad_respond_to_challenge()
844 resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter); in rxkad_respond_to_challenge()
845 resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter); in rxkad_respond_to_challenge()
846 resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter); in rxkad_respond_to_challenge()
850 ret = rxkad_encrypt_response(conn, resp, token->kad); in rxkad_respond_to_challenge()
852 ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad); in rxkad_respond_to_challenge()
876 _enter("{%d},{%x}", conn->debug_id, key_serial(server_key)); in rxkad_decrypt_ticket()
880 ASSERT(server_key->payload.data[0] != NULL); in rxkad_decrypt_ticket()
883 memcpy(&iv, &server_key->payload.data[2], sizeof(iv)); in rxkad_decrypt_ticket()
885 req = skcipher_request_alloc(server_key->payload.data[0], GFP_NOFS); in rxkad_decrypt_ticket()
887 return -ENOMEM; in rxkad_decrypt_ticket()
901 q = memchr(p, 0, end - p); \ in rxkad_decrypt_ticket()
902 if (!q || q - p > field##_SZ) \ in rxkad_decrypt_ticket()
904 conn, skb, RXKADBADTICKET, -EPROTO, \ in rxkad_decrypt_ticket()
909 conn, skb, RXKADBADTICKET, -EPROTO, \ in rxkad_decrypt_ticket()
932 if (end - p < 4 + 8 + 4 + 2) in rxkad_decrypt_ticket()
933 return rxrpc_abort_conn(conn, skb, RXKADBADTICKET, -EPROTO, in rxkad_decrypt_ticket()
967 return rxrpc_abort_conn(conn, skb, RXKADNOAUTH, -EKEYREJECTED, in rxkad_decrypt_ticket()
969 if (issue < now - life) in rxkad_decrypt_ticket()
970 return rxrpc_abort_conn(conn, skb, RXKADEXPIRED, -EKEYEXPIRED, in rxkad_decrypt_ticket()
997 ntohl(session_key->n[0]), ntohl(session_key->n[1])); in rxkad_decrypt_response()
1000 if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x, in rxkad_decrypt_response()
1007 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); in rxkad_decrypt_response()
1010 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); in rxkad_decrypt_response()
1035 _enter("{%d}", conn->debug_id); in rxkad_verify_response()
1041 case -ENOKEY: in rxkad_verify_response()
1044 case -EKEYEXPIRED: in rxkad_verify_response()
1053 ret = -ENOMEM; in rxkad_verify_response()
1060 rxrpc_abort_conn(conn, skb, RXKADPACKETSHORT, -EPROTO, in rxkad_verify_response()
1065 version = ntohl(response->version); in rxkad_verify_response()
1066 ticket_len = ntohl(response->ticket_len); in rxkad_verify_response()
1067 kvno = ntohl(response->kvno); in rxkad_verify_response()
1069 trace_rxrpc_rx_response(conn, sp->hdr.serial, version, kvno, ticket_len); in rxkad_verify_response()
1072 rxrpc_abort_conn(conn, skb, RXKADINCONSISTENCY, -EPROTO, in rxkad_verify_response()
1078 rxrpc_abort_conn(conn, skb, RXKADTICKETLEN, -EPROTO, in rxkad_verify_response()
1084 rxrpc_abort_conn(conn, skb, RXKADUNKNOWNKEY, -EPROTO, in rxkad_verify_response()
1090 ret = -ENOMEM; in rxkad_verify_response()
1097 rxrpc_abort_conn(conn, skb, RXKADPACKETSHORT, -EPROTO, in rxkad_verify_response()
1111 if (ntohl(response->encrypted.epoch) != conn->proto.epoch || in rxkad_verify_response()
1112 ntohl(response->encrypted.cid) != conn->proto.cid || in rxkad_verify_response()
1113 ntohl(response->encrypted.securityIndex) != conn->security_ix) { in rxkad_verify_response()
1114 rxrpc_abort_conn(conn, skb, RXKADSEALEDINCON, -EPROTO, in rxkad_verify_response()
1119 csum = response->encrypted.checksum; in rxkad_verify_response()
1120 response->encrypted.checksum = 0; in rxkad_verify_response()
1122 if (response->encrypted.checksum != csum) { in rxkad_verify_response()
1123 rxrpc_abort_conn(conn, skb, RXKADSEALEDINCON, -EPROTO, in rxkad_verify_response()
1129 u32 call_id = ntohl(response->encrypted.call_id[i]); in rxkad_verify_response()
1130 u32 counter = READ_ONCE(conn->channels[i].call_counter); in rxkad_verify_response()
1133 rxrpc_abort_conn(conn, skb, RXKADSEALEDINCON, -EPROTO, in rxkad_verify_response()
1139 rxrpc_abort_conn(conn, skb, RXKADSEALEDINCON, -EPROTO, in rxkad_verify_response()
1145 if (conn->channels[i].call) { in rxkad_verify_response()
1146 rxrpc_abort_conn(conn, skb, RXKADSEALEDINCON, -EPROTO, in rxkad_verify_response()
1150 conn->channels[i].call_counter = call_id; in rxkad_verify_response()
1154 if (ntohl(response->encrypted.inc_nonce) != conn->rxkad.nonce + 1) { in rxkad_verify_response()
1155 rxrpc_abort_conn(conn, skb, RXKADOUTOFSEQUENCE, -EPROTO, in rxkad_verify_response()
1160 level = ntohl(response->encrypted.level); in rxkad_verify_response()
1162 rxrpc_abort_conn(conn, skb, RXKADLEVELFAIL, -EPROTO, in rxkad_verify_response()
1166 conn->security_level = level; in rxkad_verify_response()
1168 /* create a key to hold the security data and expiration time - after in rxkad_verify_response()
1185 return -EPROTO; in rxkad_verify_response()
1207 if (conn->rxkad.cipher) in rxkad_clear()
1208 crypto_free_sync_skcipher(conn->rxkad.cipher); in rxkad_clear()
1225 req = skcipher_request_alloc(&tfm->base, GFP_KERNEL); in rxkad_init()
1235 return -ENOMEM; in rxkad_init()
1248 * RxRPC Kerberos-based security