1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC security handling
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/udp.h>
12 #include <linux/crypto.h>
14 #include <net/af_rxrpc.h>
15 #include <keys/rxrpc-type.h>
16 #include "ar-internal.h"
18 static const struct rxrpc_security *rxrpc_security_types[] = {
19 [RXRPC_SECURITY_NONE] = &rxrpc_no_security,
21 [RXRPC_SECURITY_RXKAD] = &rxkad,
25 int __init rxrpc_init_security(void)
29 for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) {
30 if (rxrpc_security_types[i]) {
31 ret = rxrpc_security_types[i]->init();
40 for (i--; i >= 0; i--)
41 if (rxrpc_security_types[i])
42 rxrpc_security_types[i]->exit();
46 void rxrpc_exit_security(void)
50 for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++)
51 if (rxrpc_security_types[i])
52 rxrpc_security_types[i]->exit();
56 * look up an rxrpc security module
58 const struct rxrpc_security *rxrpc_security_lookup(u8 security_index)
60 if (security_index >= ARRAY_SIZE(rxrpc_security_types))
62 return rxrpc_security_types[security_index];
66 * Initialise the security on a client call.
68 int rxrpc_init_client_call_security(struct rxrpc_call *call)
70 const struct rxrpc_security *sec = &rxrpc_no_security;
71 struct rxrpc_key_token *token;
72 struct key *key = call->key;
78 ret = key_validate(key);
82 for (token = key->payload.data[0]; token; token = token->next) {
83 sec = rxrpc_security_lookup(token->security_index);
91 call->security_ix = sec->security_index;
96 * initialise the security on a client connection
98 int rxrpc_init_client_conn_security(struct rxrpc_connection *conn)
100 struct rxrpc_key_token *token;
101 struct key *key = conn->key;
104 _enter("{%d},{%x}", conn->debug_id, key_serial(key));
106 for (token = key->payload.data[0]; token; token = token->next) {
107 if (token->security_index == conn->security->security_index)
110 return -EKEYREJECTED;
113 mutex_lock(&conn->security_lock);
114 if (conn->state == RXRPC_CONN_CLIENT_UNSECURED) {
115 ret = conn->security->init_connection_security(conn, token);
117 spin_lock(&conn->state_lock);
118 if (conn->state == RXRPC_CONN_CLIENT_UNSECURED)
119 conn->state = RXRPC_CONN_CLIENT;
120 spin_unlock(&conn->state_lock);
123 mutex_unlock(&conn->security_lock);
128 * Set the ops a server connection.
130 const struct rxrpc_security *rxrpc_get_incoming_security(struct rxrpc_sock *rx,
133 const struct rxrpc_security *sec;
134 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
138 sec = rxrpc_security_lookup(sp->hdr.securityIndex);
140 rxrpc_direct_abort(skb, rxrpc_abort_unsupported_security,
141 RX_INVALID_OPERATION, -EKEYREJECTED);
145 if (sp->hdr.securityIndex != RXRPC_SECURITY_NONE &&
147 rxrpc_direct_abort(skb, rxrpc_abort_no_service_key,
148 sec->no_key_abort, -EKEYREJECTED);
156 * Find the security key for a server connection.
158 struct key *rxrpc_look_up_server_security(struct rxrpc_connection *conn,
160 u32 kvno, u32 enctype)
162 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
163 struct rxrpc_sock *rx;
164 struct key *key = ERR_PTR(-EKEYREJECTED);
165 key_ref_t kref = NULL;
166 char kdesc[5 + 1 + 3 + 1 + 12 + 1 + 12 + 1];
172 sprintf(kdesc, "%u:%u:%u:%u",
173 sp->hdr.serviceId, sp->hdr.securityIndex, kvno, enctype);
175 sprintf(kdesc, "%u:%u:%u",
176 sp->hdr.serviceId, sp->hdr.securityIndex, kvno);
178 sprintf(kdesc, "%u:%u",
179 sp->hdr.serviceId, sp->hdr.securityIndex);
181 read_lock(&conn->local->services_lock);
183 rx = conn->local->service;
187 /* look through the service's keyring */
188 kref = keyring_search(make_key_ref(rx->securities, 1UL),
189 &key_type_rxrpc_s, kdesc, true);
191 key = ERR_CAST(kref);
195 key = key_ref_to_ptr(kref);
197 ret = key_validate(key);
205 read_unlock(&conn->local->services_lock);