blob: ead3471307ee52e113eefa7ff67821bb8b77b0ab [file] [log] [blame]
David Howellsca7fb102020-09-16 08:25:08 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2/* RxRPC key management
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
7 * RxRPC keys should have a description of describing their purpose:
8 * "afs@CAMBRIDGE.REDHAT.COM>
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <crypto/skcipher.h>
14#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/key-type.h>
18#include <linux/ctype.h>
19#include <linux/slab.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include <keys/rxrpc-type.h>
23#include <keys/user-type.h>
24#include "ar-internal.h"
25
26static int rxrpc_vet_description_s(const char *);
27static int rxrpc_preparse_s(struct key_preparsed_payload *);
28static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
29static void rxrpc_destroy_s(struct key *);
30static void rxrpc_describe_s(const struct key *, struct seq_file *);
31
32/*
David Howells12da59f2020-09-16 08:37:29 +010033 * rxrpc server keys take "<serviceId>:<securityIndex>[:<sec-specific>]" as the
34 * description and the key material as the payload.
David Howellsca7fb102020-09-16 08:25:08 +010035 */
36struct key_type key_type_rxrpc_s = {
37 .name = "rxrpc_s",
38 .flags = KEY_TYPE_NET_DOMAIN,
39 .vet_description = rxrpc_vet_description_s,
40 .preparse = rxrpc_preparse_s,
41 .free_preparse = rxrpc_free_preparse_s,
42 .instantiate = generic_key_instantiate,
43 .destroy = rxrpc_destroy_s,
44 .describe = rxrpc_describe_s,
45};
46
47/*
David Howells12da59f2020-09-16 08:37:29 +010048 * Vet the description for an RxRPC server key.
David Howellsca7fb102020-09-16 08:25:08 +010049 */
50static int rxrpc_vet_description_s(const char *desc)
51{
David Howells12da59f2020-09-16 08:37:29 +010052 unsigned long service, sec_class;
David Howellsca7fb102020-09-16 08:25:08 +010053 char *p;
54
David Howells12da59f2020-09-16 08:37:29 +010055 service = simple_strtoul(desc, &p, 10);
56 if (*p != ':' || service > 65535)
David Howellsca7fb102020-09-16 08:25:08 +010057 return -EINVAL;
David Howells12da59f2020-09-16 08:37:29 +010058 sec_class = simple_strtoul(p + 1, &p, 10);
59 if ((*p && *p != ':') || sec_class < 1 || sec_class > 255)
David Howellsca7fb102020-09-16 08:25:08 +010060 return -EINVAL;
61 return 0;
62}
63
64/*
65 * Preparse a server secret key.
David Howellsca7fb102020-09-16 08:25:08 +010066 */
67static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
68{
David Howells12da59f2020-09-16 08:37:29 +010069 const struct rxrpc_security *sec;
70 unsigned int service, sec_class;
71 int n;
David Howellsca7fb102020-09-16 08:25:08 +010072
73 _enter("%zu", prep->datalen);
74
David Howells12da59f2020-09-16 08:37:29 +010075 if (!prep->orig_description)
David Howellsca7fb102020-09-16 08:25:08 +010076 return -EINVAL;
77
David Howells12da59f2020-09-16 08:37:29 +010078 if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
79 return -EINVAL;
David Howellsca7fb102020-09-16 08:25:08 +010080
David Howells12da59f2020-09-16 08:37:29 +010081 sec = rxrpc_security_lookup(sec_class);
82 if (!sec)
83 return -ENOPKG;
David Howellsca7fb102020-09-16 08:25:08 +010084
David Howells12da59f2020-09-16 08:37:29 +010085 prep->payload.data[1] = (struct rxrpc_security *)sec;
David Howellsca7fb102020-09-16 08:25:08 +010086
David Howells12da59f2020-09-16 08:37:29 +010087 return sec->preparse_server_key(prep);
David Howellsca7fb102020-09-16 08:25:08 +010088}
89
90static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
91{
David Howells12da59f2020-09-16 08:37:29 +010092 const struct rxrpc_security *sec = prep->payload.data[1];
93
94 if (sec)
95 sec->free_preparse_server_key(prep);
David Howellsca7fb102020-09-16 08:25:08 +010096}
97
98static void rxrpc_destroy_s(struct key *key)
99{
David Howells12da59f2020-09-16 08:37:29 +0100100 const struct rxrpc_security *sec = key->payload.data[1];
101
102 if (sec)
103 sec->destroy_server_key(key);
David Howellsca7fb102020-09-16 08:25:08 +0100104}
105
106static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
107{
David Howellsd5953f62020-09-27 11:13:04 +0100108 const struct rxrpc_security *sec = key->payload.data[1];
109
David Howellsca7fb102020-09-16 08:25:08 +0100110 seq_puts(m, key->description);
David Howellsd5953f62020-09-27 11:13:04 +0100111 if (sec && sec->describe_server_key)
112 sec->describe_server_key(key, m);
David Howellsca7fb102020-09-16 08:25:08 +0100113}
114
115/*
116 * grab the security keyring for a server socket
117 */
118int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
119{
120 struct key *key;
121 char *description;
122
123 _enter("");
124
125 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
126 return -EINVAL;
127
128 description = memdup_sockptr_nul(optval, optlen);
129 if (IS_ERR(description))
130 return PTR_ERR(description);
131
132 key = request_key(&key_type_keyring, description, NULL);
133 if (IS_ERR(key)) {
134 kfree(description);
135 _leave(" = %ld", PTR_ERR(key));
136 return PTR_ERR(key);
137 }
138
139 rx->securities = key;
140 kfree(description);
141 _leave(" = 0 [key %x]", key->serial);
142 return 0;
143}