Merge remote-tracking branch 'vfio/next'
[deliverable/linux.git] / include / linux / key-type.h
1 /* Definitions for key type implementations
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12 #ifndef _LINUX_KEY_TYPE_H
13 #define _LINUX_KEY_TYPE_H
14
15 #include <linux/key.h>
16 #include <linux/errno.h>
17
18 #ifdef CONFIG_KEYS
19
20 struct kernel_pkey_query;
21 struct kernel_pkey_params;
22
23 /*
24 * key under-construction record
25 * - passed to the request_key actor if supplied
26 */
27 struct key_construction {
28 struct key *key; /* key being constructed */
29 struct key *authkey;/* authorisation for key being constructed */
30 };
31
32 /*
33 * Pre-parsed payload, used by key add, update and instantiate.
34 *
35 * This struct will be cleared and data and datalen will be set with the data
36 * and length parameters from the caller and quotalen will be set from
37 * def_datalen from the key type. Then if the preparse() op is provided by the
38 * key type, that will be called. Then the struct will be passed to the
39 * instantiate() or the update() op.
40 *
41 * If the preparse() op is given, the free_preparse() op will be called to
42 * clear the contents.
43 */
44 struct key_preparsed_payload {
45 char *description; /* Proposed key description (or NULL) */
46 union key_payload payload; /* Proposed payload */
47 const void *data; /* Raw data */
48 size_t datalen; /* Raw datalen */
49 size_t quotalen; /* Quota length for proposed payload */
50 time_t expiry; /* Expiry time of key */
51 };
52
53 typedef int (*request_key_actor_t)(struct key_construction *key,
54 const char *op, void *aux);
55
56 /*
57 * Preparsed matching criterion.
58 */
59 struct key_match_data {
60 /* Comparison function, defaults to exact description match, but can be
61 * overridden by type->match_preparse(). Should return true if a match
62 * is found and false if not.
63 */
64 bool (*cmp)(const struct key *key,
65 const struct key_match_data *match_data);
66
67 const void *raw_data; /* Raw match data */
68 void *preparsed; /* For ->match_preparse() to stash stuff */
69 unsigned lookup_type; /* Type of lookup for this search. */
70 #define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
71 #define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
72 };
73
74 /*
75 * kernel managed key type definition
76 */
77 struct key_type {
78 /* name of the type */
79 const char *name;
80
81 /* default payload length for quota precalculation (optional)
82 * - this can be used instead of calling key_payload_reserve(), that
83 * function only needs to be called if the real datalen is different
84 */
85 size_t def_datalen;
86
87 /* vet a description */
88 int (*vet_description)(const char *description);
89
90 /* Preparse the data blob from userspace that is to be the payload,
91 * generating a proposed description and payload that will be handed to
92 * the instantiate() and update() ops.
93 */
94 int (*preparse)(struct key_preparsed_payload *prep);
95
96 /* Free a preparse data structure.
97 */
98 void (*free_preparse)(struct key_preparsed_payload *prep);
99
100 /* instantiate a key of this type
101 * - this method should call key_payload_reserve() to determine if the
102 * user's quota will hold the payload
103 */
104 int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
105
106 /* update a key of this type (optional)
107 * - this method should call key_payload_reserve() to recalculate the
108 * quota consumption
109 * - the key must be locked against read when modifying
110 */
111 int (*update)(struct key *key, struct key_preparsed_payload *prep);
112
113 /* Preparse the data supplied to ->match() (optional). The
114 * data to be preparsed can be found in match_data->raw_data.
115 * The lookup type can also be set by this function.
116 */
117 int (*match_preparse)(struct key_match_data *match_data);
118
119 /* Free preparsed match data (optional). This should be supplied it
120 * ->match_preparse() is supplied. */
121 void (*match_free)(struct key_match_data *match_data);
122
123 /* clear some of the data from a key on revokation (optional)
124 * - the key's semaphore will be write-locked by the caller
125 */
126 void (*revoke)(struct key *key);
127
128 /* clear the data from a key (optional) */
129 void (*destroy)(struct key *key);
130
131 /* describe a key */
132 void (*describe)(const struct key *key, struct seq_file *p);
133
134 /* read a key's data (optional)
135 * - permission checks will be done by the caller
136 * - the key's semaphore will be readlocked by the caller
137 * - should return the amount of data that could be read, no matter how
138 * much is copied into the buffer
139 * - shouldn't do the copy if the buffer is NULL
140 */
141 long (*read)(const struct key *key, char __user *buffer, size_t buflen);
142
143 /* handle request_key() for this type instead of invoking
144 * /sbin/request-key (optional)
145 * - key is the key to instantiate
146 * - authkey is the authority to assume when instantiating this key
147 * - op is the operation to be done, usually "create"
148 * - the call must not return until the instantiation process has run
149 * its course
150 */
151 request_key_actor_t request_key;
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 /* internal fields */
162 struct list_head link; /* link in types list */
163 struct lock_class_key lock_class; /* key->sem lock class */
164 };
165
166 extern struct key_type key_type_keyring;
167
168 extern int register_key_type(struct key_type *ktype);
169 extern void unregister_key_type(struct key_type *ktype);
170
171 extern int key_payload_reserve(struct key *key, size_t datalen);
172 extern int key_instantiate_and_link(struct key *key,
173 const void *data,
174 size_t datalen,
175 struct key *keyring,
176 struct key *instkey);
177 extern int key_reject_and_link(struct key *key,
178 unsigned timeout,
179 unsigned error,
180 struct key *keyring,
181 struct key *instkey);
182 extern void complete_request_key(struct key_construction *cons, int error);
183
184 static inline int key_negate_and_link(struct key *key,
185 unsigned timeout,
186 struct key *keyring,
187 struct key *instkey)
188 {
189 return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
190 }
191
192 extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
193
194 #endif /* CONFIG_KEYS */
195 #endif /* _LINUX_KEY_TYPE_H */
This page took 0.034668 seconds and 5 git commands to generate.