1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Definitions for key type implementations
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #ifndef _LINUX_KEY_TYPE_H
9 #define _LINUX_KEY_TYPE_H
11 #include <linux/key.h>
12 #include <linux/errno.h>
16 struct kernel_pkey_query
;
17 struct kernel_pkey_params
;
20 * Pre-parsed payload, used by key add, update and instantiate.
22 * This struct will be cleared and data and datalen will be set with the data
23 * and length parameters from the caller and quotalen will be set from
24 * def_datalen from the key type. Then if the preparse() op is provided by the
25 * key type, that will be called. Then the struct will be passed to the
26 * instantiate() or the update() op.
28 * If the preparse() op is given, the free_preparse() op will be called to
31 struct key_preparsed_payload
{
32 char *description
; /* Proposed key description (or NULL) */
33 union key_payload payload
; /* Proposed payload */
34 const void *data
; /* Raw data */
35 size_t datalen
; /* Raw datalen */
36 size_t quotalen
; /* Quota length for proposed payload */
37 time64_t expiry
; /* Expiry time of key */
40 typedef int (*request_key_actor_t
)(struct key
*auth_key
, void *aux
);
43 * Preparsed matching criterion.
45 struct key_match_data
{
46 /* Comparison function, defaults to exact description match, but can be
47 * overridden by type->match_preparse(). Should return true if a match
48 * is found and false if not.
50 bool (*cmp
)(const struct key
*key
,
51 const struct key_match_data
*match_data
);
53 const void *raw_data
; /* Raw match data */
54 void *preparsed
; /* For ->match_preparse() to stash stuff */
55 unsigned lookup_type
; /* Type of lookup for this search. */
56 #define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
57 #define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
61 * kernel managed key type definition
64 /* name of the type */
67 /* default payload length for quota precalculation (optional)
68 * - this can be used instead of calling key_payload_reserve(), that
69 * function only needs to be called if the real datalen is different
73 /* vet a description */
74 int (*vet_description
)(const char *description
);
76 /* Preparse the data blob from userspace that is to be the payload,
77 * generating a proposed description and payload that will be handed to
78 * the instantiate() and update() ops.
80 int (*preparse
)(struct key_preparsed_payload
*prep
);
82 /* Free a preparse data structure.
84 void (*free_preparse
)(struct key_preparsed_payload
*prep
);
86 /* instantiate a key of this type
87 * - this method should call key_payload_reserve() to determine if the
88 * user's quota will hold the payload
90 int (*instantiate
)(struct key
*key
, struct key_preparsed_payload
*prep
);
92 /* update a key of this type (optional)
93 * - this method should call key_payload_reserve() to recalculate the
95 * - the key must be locked against read when modifying
97 int (*update
)(struct key
*key
, struct key_preparsed_payload
*prep
);
99 /* Preparse the data supplied to ->match() (optional). The
100 * data to be preparsed can be found in match_data->raw_data.
101 * The lookup type can also be set by this function.
103 int (*match_preparse
)(struct key_match_data
*match_data
);
105 /* Free preparsed match data (optional). This should be supplied it
106 * ->match_preparse() is supplied. */
107 void (*match_free
)(struct key_match_data
*match_data
);
109 /* clear some of the data from a key on revokation (optional)
110 * - the key's semaphore will be write-locked by the caller
112 void (*revoke
)(struct key
*key
);
114 /* clear the data from a key (optional) */
115 void (*destroy
)(struct key
*key
);
118 void (*describe
)(const struct key
*key
, struct seq_file
*p
);
120 /* read a key's data (optional)
121 * - permission checks will be done by the caller
122 * - the key's semaphore will be readlocked by the caller
123 * - should return the amount of data that could be read, no matter how
124 * much is copied into the buffer
125 * - shouldn't do the copy if the buffer is NULL
127 long (*read
)(const struct key
*key
, char __user
*buffer
, size_t buflen
);
129 /* handle request_key() for this type instead of invoking
130 * /sbin/request-key (optional)
131 * - key is the key to instantiate
132 * - authkey is the authority to assume when instantiating this key
133 * - op is the operation to be done, usually "create"
134 * - the call must not return until the instantiation process has run
137 request_key_actor_t request_key
;
139 /* Look up a keyring access restriction (optional)
141 * - NULL is a valid return value (meaning the requested restriction
142 * is known but will never block addition of a key)
143 * - should return -EINVAL if the restriction is unknown
145 struct key_restriction
*(*lookup_restriction
)(const char *params
);
147 /* Asymmetric key accessor functions. */
148 int (*asym_query
)(const struct kernel_pkey_params
*params
,
149 struct kernel_pkey_query
*info
);
150 int (*asym_eds_op
)(struct kernel_pkey_params
*params
,
151 const void *in
, void *out
);
152 int (*asym_verify_signature
)(struct kernel_pkey_params
*params
,
153 const void *in
, const void *in2
);
155 /* internal fields */
156 struct list_head link
; /* link in types list */
157 struct lock_class_key lock_class
; /* key->sem lock class */
158 } __randomize_layout
;
160 extern struct key_type key_type_keyring
;
162 extern int register_key_type(struct key_type
*ktype
);
163 extern void unregister_key_type(struct key_type
*ktype
);
165 extern int key_payload_reserve(struct key
*key
, size_t datalen
);
166 extern int key_instantiate_and_link(struct key
*key
,
170 struct key
*authkey
);
171 extern int key_reject_and_link(struct key
*key
,
175 struct key
*authkey
);
176 extern void complete_request_key(struct key
*authkey
, int error
);
178 static inline int key_negate_and_link(struct key
*key
,
183 return key_reject_and_link(key
, timeout
, ENOKEY
, keyring
, authkey
);
186 extern int generic_key_instantiate(struct key
*key
, struct key_preparsed_payload
*prep
);
188 #endif /* CONFIG_KEYS */
189 #endif /* _LINUX_KEY_TYPE_H */