1b4d0d230SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
276181c13SDavid Howells /* Definitions for key type implementations
376181c13SDavid Howells *
476181c13SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
576181c13SDavid Howells * Written by David Howells ([email protected])
676181c13SDavid Howells */
776181c13SDavid Howells
876181c13SDavid Howells #ifndef _LINUX_KEY_TYPE_H
976181c13SDavid Howells #define _LINUX_KEY_TYPE_H
1076181c13SDavid Howells
1176181c13SDavid Howells #include <linux/key.h>
125935e6dcSDavid Howells #include <linux/errno.h>
1376181c13SDavid Howells
1476181c13SDavid Howells #ifdef CONFIG_KEYS
1576181c13SDavid Howells
1670025f84SDavid Howells struct kernel_pkey_query;
1770025f84SDavid Howells struct kernel_pkey_params;
1870025f84SDavid Howells
1976181c13SDavid Howells /*
20cf7f601cSDavid Howells * Pre-parsed payload, used by key add, update and instantiate.
21cf7f601cSDavid Howells *
22cf7f601cSDavid Howells * This struct will be cleared and data and datalen will be set with the data
23cf7f601cSDavid Howells * and length parameters from the caller and quotalen will be set from
24cf7f601cSDavid Howells * def_datalen from the key type. Then if the preparse() op is provided by the
25cf7f601cSDavid Howells * key type, that will be called. Then the struct will be passed to the
26cf7f601cSDavid Howells * instantiate() or the update() op.
27cf7f601cSDavid Howells *
28cf7f601cSDavid Howells * If the preparse() op is given, the free_preparse() op will be called to
29cf7f601cSDavid Howells * clear the contents.
30cf7f601cSDavid Howells */
31cf7f601cSDavid Howells struct key_preparsed_payload {
328eb62169SDavid Howells const char *orig_description; /* Actual or proposed description (maybe NULL) */
33cf7f601cSDavid Howells char *description; /* Proposed key description (or NULL) */
34146aa8b1SDavid Howells union key_payload payload; /* Proposed payload */
35cf7f601cSDavid Howells const void *data; /* Raw data */
36cf7f601cSDavid Howells size_t datalen; /* Raw datalen */
37cf7f601cSDavid Howells size_t quotalen; /* Quota length for proposed payload */
380a9dd0e0SBaolin Wang time64_t expiry; /* Expiry time of key */
393859a271SKees Cook } __randomize_layout;
40cf7f601cSDavid Howells
41822ad64dSDavid Howells typedef int (*request_key_actor_t)(struct key *auth_key, void *aux);
4276181c13SDavid Howells
4376181c13SDavid Howells /*
4446291959SDavid Howells * Preparsed matching criterion.
4546291959SDavid Howells */
4646291959SDavid Howells struct key_match_data {
470c903ab6SDavid Howells /* Comparison function, defaults to exact description match, but can be
480c903ab6SDavid Howells * overridden by type->match_preparse(). Should return true if a match
490c903ab6SDavid Howells * is found and false if not.
500c903ab6SDavid Howells */
510c903ab6SDavid Howells bool (*cmp)(const struct key *key,
5246291959SDavid Howells const struct key_match_data *match_data);
5346291959SDavid Howells
5446291959SDavid Howells const void *raw_data; /* Raw match data */
5546291959SDavid Howells void *preparsed; /* For ->match_preparse() to stash stuff */
5646291959SDavid Howells unsigned lookup_type; /* Type of lookup for this search. */
5746291959SDavid Howells #define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
5846291959SDavid Howells #define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
5946291959SDavid Howells };
6046291959SDavid Howells
6146291959SDavid Howells /*
6276181c13SDavid Howells * kernel managed key type definition
6376181c13SDavid Howells */
6476181c13SDavid Howells struct key_type {
6576181c13SDavid Howells /* name of the type */
6676181c13SDavid Howells const char *name;
6776181c13SDavid Howells
6876181c13SDavid Howells /* default payload length for quota precalculation (optional)
6976181c13SDavid Howells * - this can be used instead of calling key_payload_reserve(), that
7076181c13SDavid Howells * function only needs to be called if the real datalen is different
7176181c13SDavid Howells */
7276181c13SDavid Howells size_t def_datalen;
7376181c13SDavid Howells
749b242610SDavid Howells unsigned int flags;
759b242610SDavid Howells #define KEY_TYPE_NET_DOMAIN 0x00000001 /* Keys of this type have a net namespace domain */
76*39299bddSDavid Howells #define KEY_TYPE_INSTANT_REAP 0x00000002 /* Keys of this type don't have a delay after expiring */
779b242610SDavid Howells
78b9fffa38SDavid Howells /* vet a description */
79b9fffa38SDavid Howells int (*vet_description)(const char *description);
80b9fffa38SDavid Howells
81cf7f601cSDavid Howells /* Preparse the data blob from userspace that is to be the payload,
82cf7f601cSDavid Howells * generating a proposed description and payload that will be handed to
83cf7f601cSDavid Howells * the instantiate() and update() ops.
84cf7f601cSDavid Howells */
85cf7f601cSDavid Howells int (*preparse)(struct key_preparsed_payload *prep);
86cf7f601cSDavid Howells
87cf7f601cSDavid Howells /* Free a preparse data structure.
88cf7f601cSDavid Howells */
89cf7f601cSDavid Howells void (*free_preparse)(struct key_preparsed_payload *prep);
90cf7f601cSDavid Howells
9176181c13SDavid Howells /* instantiate a key of this type
9276181c13SDavid Howells * - this method should call key_payload_reserve() to determine if the
9376181c13SDavid Howells * user's quota will hold the payload
9476181c13SDavid Howells */
95cf7f601cSDavid Howells int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
9676181c13SDavid Howells
9776181c13SDavid Howells /* update a key of this type (optional)
9876181c13SDavid Howells * - this method should call key_payload_reserve() to recalculate the
9976181c13SDavid Howells * quota consumption
10076181c13SDavid Howells * - the key must be locked against read when modifying
10176181c13SDavid Howells */
102cf7f601cSDavid Howells int (*update)(struct key *key, struct key_preparsed_payload *prep);
10376181c13SDavid Howells
10446291959SDavid Howells /* Preparse the data supplied to ->match() (optional). The
10546291959SDavid Howells * data to be preparsed can be found in match_data->raw_data.
10646291959SDavid Howells * The lookup type can also be set by this function.
10746291959SDavid Howells */
10846291959SDavid Howells int (*match_preparse)(struct key_match_data *match_data);
10946291959SDavid Howells
11046291959SDavid Howells /* Free preparsed match data (optional). This should be supplied it
11146291959SDavid Howells * ->match_preparse() is supplied. */
11246291959SDavid Howells void (*match_free)(struct key_match_data *match_data);
11376181c13SDavid Howells
11476181c13SDavid Howells /* clear some of the data from a key on revokation (optional)
11576181c13SDavid Howells * - the key's semaphore will be write-locked by the caller
11676181c13SDavid Howells */
11776181c13SDavid Howells void (*revoke)(struct key *key);
11876181c13SDavid Howells
11976181c13SDavid Howells /* clear the data from a key (optional) */
12076181c13SDavid Howells void (*destroy)(struct key *key);
12176181c13SDavid Howells
12276181c13SDavid Howells /* describe a key */
12376181c13SDavid Howells void (*describe)(const struct key *key, struct seq_file *p);
12476181c13SDavid Howells
12576181c13SDavid Howells /* read a key's data (optional)
12676181c13SDavid Howells * - permission checks will be done by the caller
12776181c13SDavid Howells * - the key's semaphore will be readlocked by the caller
12876181c13SDavid Howells * - should return the amount of data that could be read, no matter how
12976181c13SDavid Howells * much is copied into the buffer
13076181c13SDavid Howells * - shouldn't do the copy if the buffer is NULL
13176181c13SDavid Howells */
132d3ec10aaSWaiman Long long (*read)(const struct key *key, char *buffer, size_t buflen);
13376181c13SDavid Howells
13476181c13SDavid Howells /* handle request_key() for this type instead of invoking
13576181c13SDavid Howells * /sbin/request-key (optional)
13676181c13SDavid Howells * - key is the key to instantiate
13776181c13SDavid Howells * - authkey is the authority to assume when instantiating this key
13876181c13SDavid Howells * - op is the operation to be done, usually "create"
13976181c13SDavid Howells * - the call must not return until the instantiation process has run
14076181c13SDavid Howells * its course
14176181c13SDavid Howells */
14276181c13SDavid Howells request_key_actor_t request_key;
14376181c13SDavid Howells
144efba797bSMat Martineau /* Look up a keyring access restriction (optional)
145efba797bSMat Martineau *
146efba797bSMat Martineau * - NULL is a valid return value (meaning the requested restriction
147efba797bSMat Martineau * is known but will never block addition of a key)
148efba797bSMat Martineau * - should return -EINVAL if the restriction is unknown
149efba797bSMat Martineau */
150efba797bSMat Martineau struct key_restriction *(*lookup_restriction)(const char *params);
151efba797bSMat Martineau
15270025f84SDavid Howells /* Asymmetric key accessor functions. */
15370025f84SDavid Howells int (*asym_query)(const struct kernel_pkey_params *params,
15470025f84SDavid Howells struct kernel_pkey_query *info);
15570025f84SDavid Howells int (*asym_eds_op)(struct kernel_pkey_params *params,
15670025f84SDavid Howells const void *in, void *out);
15770025f84SDavid Howells int (*asym_verify_signature)(struct kernel_pkey_params *params,
15870025f84SDavid Howells const void *in, const void *in2);
15970025f84SDavid Howells
16076181c13SDavid Howells /* internal fields */
16176181c13SDavid Howells struct list_head link; /* link in types list */
1627845bc39SDavid Howells struct lock_class_key lock_class; /* key->sem lock class */
1633859a271SKees Cook } __randomize_layout;
16476181c13SDavid Howells
16576181c13SDavid Howells extern struct key_type key_type_keyring;
16676181c13SDavid Howells
16776181c13SDavid Howells extern int register_key_type(struct key_type *ktype);
16876181c13SDavid Howells extern void unregister_key_type(struct key_type *ktype);
16976181c13SDavid Howells
17076181c13SDavid Howells extern int key_payload_reserve(struct key *key, size_t datalen);
17176181c13SDavid Howells extern int key_instantiate_and_link(struct key *key,
17276181c13SDavid Howells const void *data,
17376181c13SDavid Howells size_t datalen,
17476181c13SDavid Howells struct key *keyring,
175822ad64dSDavid Howells struct key *authkey);
176fdd1b945SDavid Howells extern int key_reject_and_link(struct key *key,
17776181c13SDavid Howells unsigned timeout,
178fdd1b945SDavid Howells unsigned error,
17976181c13SDavid Howells struct key *keyring,
180822ad64dSDavid Howells struct key *authkey);
181822ad64dSDavid Howells extern void complete_request_key(struct key *authkey, int error);
18276181c13SDavid Howells
key_negate_and_link(struct key * key,unsigned timeout,struct key * keyring,struct key * authkey)183fdd1b945SDavid Howells static inline int key_negate_and_link(struct key *key,
184fdd1b945SDavid Howells unsigned timeout,
185fdd1b945SDavid Howells struct key *keyring,
186822ad64dSDavid Howells struct key *authkey)
187fdd1b945SDavid Howells {
188822ad64dSDavid Howells return key_reject_and_link(key, timeout, ENOKEY, keyring, authkey);
189fdd1b945SDavid Howells }
190fdd1b945SDavid Howells
1916a09d17bSDavid Howells extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
1926a09d17bSDavid Howells
19376181c13SDavid Howells #endif /* CONFIG_KEYS */
19476181c13SDavid Howells #endif /* _LINUX_KEY_TYPE_H */
195