• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Definitions for key type implementations
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #ifndef _LINUX_KEY_TYPE_H
9 #define _LINUX_KEY_TYPE_H
10 
11 #include <linux/key.h>
12 #include <linux/errno.h>
13 #include <linux/android_kabi.h>
14 
15 #ifdef CONFIG_KEYS
16 
17 struct kernel_pkey_query;
18 struct kernel_pkey_params;
19 
20 /*
21  * Pre-parsed payload, used by key add, update and instantiate.
22  *
23  * This struct will be cleared and data and datalen will be set with the data
24  * and length parameters from the caller and quotalen will be set from
25  * def_datalen from the key type.  Then if the preparse() op is provided by the
26  * key type, that will be called.  Then the struct will be passed to the
27  * instantiate() or the update() op.
28  *
29  * If the preparse() op is given, the free_preparse() op will be called to
30  * clear the contents.
31  */
32 struct key_preparsed_payload {
33 	const char	*orig_description; /* Actual or proposed description (maybe NULL) */
34 	char		*description;	/* Proposed key description (or NULL) */
35 	union key_payload payload;	/* Proposed payload */
36 	const void	*data;		/* Raw data */
37 	size_t		datalen;	/* Raw datalen */
38 	size_t		quotalen;	/* Quota length for proposed payload */
39 	time64_t	expiry;		/* Expiry time of key */
40 } __randomize_layout;
41 
42 typedef int (*request_key_actor_t)(struct key *auth_key, void *aux);
43 
44 /*
45  * Preparsed matching criterion.
46  */
47 struct key_match_data {
48 	/* Comparison function, defaults to exact description match, but can be
49 	 * overridden by type->match_preparse().  Should return true if a match
50 	 * is found and false if not.
51 	 */
52 	bool (*cmp)(const struct key *key,
53 		    const struct key_match_data *match_data);
54 
55 	const void	*raw_data;	/* Raw match data */
56 	void		*preparsed;	/* For ->match_preparse() to stash stuff */
57 	unsigned	lookup_type;	/* Type of lookup for this search. */
58 #define KEYRING_SEARCH_LOOKUP_DIRECT	0x0000	/* Direct lookup by description. */
59 #define KEYRING_SEARCH_LOOKUP_ITERATE	0x0001	/* Iterative search. */
60 };
61 
62 /*
63  * kernel managed key type definition
64  */
65 struct key_type {
66 	/* name of the type */
67 	const char *name;
68 
69 	/* default payload length for quota precalculation (optional)
70 	 * - this can be used instead of calling key_payload_reserve(), that
71 	 *   function only needs to be called if the real datalen is different
72 	 */
73 	size_t def_datalen;
74 
75 	unsigned int flags;
76 #define KEY_TYPE_NET_DOMAIN	0x00000001 /* Keys of this type have a net namespace domain */
77 #define KEY_TYPE_INSTANT_REAP	0x00000002 /* Keys of this type don't have a delay after expiring */
78 
79 	/* vet a description */
80 	int (*vet_description)(const char *description);
81 
82 	/* Preparse the data blob from userspace that is to be the payload,
83 	 * generating a proposed description and payload that will be handed to
84 	 * the instantiate() and update() ops.
85 	 */
86 	int (*preparse)(struct key_preparsed_payload *prep);
87 
88 	/* Free a preparse data structure.
89 	 */
90 	void (*free_preparse)(struct key_preparsed_payload *prep);
91 
92 	/* instantiate a key of this type
93 	 * - this method should call key_payload_reserve() to determine if the
94 	 *   user's quota will hold the payload
95 	 */
96 	int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
97 
98 	/* update a key of this type (optional)
99 	 * - this method should call key_payload_reserve() to recalculate the
100 	 *   quota consumption
101 	 * - the key must be locked against read when modifying
102 	 */
103 	int (*update)(struct key *key, struct key_preparsed_payload *prep);
104 
105 	/* Preparse the data supplied to ->match() (optional).  The
106 	 * data to be preparsed can be found in match_data->raw_data.
107 	 * The lookup type can also be set by this function.
108 	 */
109 	int (*match_preparse)(struct key_match_data *match_data);
110 
111 	/* Free preparsed match data (optional).  This should be supplied it
112 	 * ->match_preparse() is supplied. */
113 	void (*match_free)(struct key_match_data *match_data);
114 
115 	/* clear some of the data from a key on revokation (optional)
116 	 * - the key's semaphore will be write-locked by the caller
117 	 */
118 	void (*revoke)(struct key *key);
119 
120 	/* clear the data from a key (optional) */
121 	void (*destroy)(struct key *key);
122 
123 	/* describe a key */
124 	void (*describe)(const struct key *key, struct seq_file *p);
125 
126 	/* read a key's data (optional)
127 	 * - permission checks will be done by the caller
128 	 * - the key's semaphore will be readlocked by the caller
129 	 * - should return the amount of data that could be read, no matter how
130 	 *   much is copied into the buffer
131 	 * - shouldn't do the copy if the buffer is NULL
132 	 */
133 	long (*read)(const struct key *key, char *buffer, size_t buflen);
134 
135 	/* handle request_key() for this type instead of invoking
136 	 * /sbin/request-key (optional)
137 	 * - key is the key to instantiate
138 	 * - authkey is the authority to assume when instantiating this key
139 	 * - op is the operation to be done, usually "create"
140 	 * - the call must not return until the instantiation process has run
141 	 *   its course
142 	 */
143 	request_key_actor_t request_key;
144 
145 	/* Look up a keyring access restriction (optional)
146 	 *
147 	 * - NULL is a valid return value (meaning the requested restriction
148 	 *   is known but will never block addition of a key)
149 	 * - should return -EINVAL if the restriction is unknown
150 	 */
151 	struct key_restriction *(*lookup_restriction)(const char *params);
152 
153 	/* Asymmetric key accessor functions. */
154 	int (*asym_query)(const struct kernel_pkey_params *params,
155 			  struct kernel_pkey_query *info);
156 	int (*asym_eds_op)(struct kernel_pkey_params *params,
157 			   const void *in, void *out);
158 	int (*asym_verify_signature)(struct kernel_pkey_params *params,
159 				     const void *in, const void *in2);
160 
161 	ANDROID_KABI_RESERVE(1);
162 	ANDROID_KABI_RESERVE(2);
163 
164 	/* internal fields */
165 	struct list_head	link;		/* link in types list */
166 	struct lock_class_key	lock_class;	/* key->sem lock class */
167 } __randomize_layout;
168 
169 extern struct key_type key_type_keyring;
170 
171 extern int register_key_type(struct key_type *ktype);
172 extern void unregister_key_type(struct key_type *ktype);
173 
174 extern int key_payload_reserve(struct key *key, size_t datalen);
175 extern int key_instantiate_and_link(struct key *key,
176 				    const void *data,
177 				    size_t datalen,
178 				    struct key *keyring,
179 				    struct key *authkey);
180 extern int key_reject_and_link(struct key *key,
181 			       unsigned timeout,
182 			       unsigned error,
183 			       struct key *keyring,
184 			       struct key *authkey);
185 extern void complete_request_key(struct key *authkey, int error);
186 
key_negate_and_link(struct key * key,unsigned timeout,struct key * keyring,struct key * authkey)187 static inline int key_negate_and_link(struct key *key,
188 				      unsigned timeout,
189 				      struct key *keyring,
190 				      struct key *authkey)
191 {
192 	return key_reject_and_link(key, timeout, ENOKEY, keyring, authkey);
193 }
194 
195 extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
196 
197 #endif /* CONFIG_KEYS */
198 #endif /* _LINUX_KEY_TYPE_H */
199