blob: 8aba688a451a08a5f5fd6bba16870c6522cd3995 [file] [log] [blame]
David Howells76181c12007-10-16 23:29:46 -07001/* 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>
David Howells5935e6d2012-07-23 15:37:31 +010016#include <linux/errno.h>
David Howells76181c12007-10-16 23:29:46 -070017
18#ifdef CONFIG_KEYS
19
20/*
21 * key under-construction record
22 * - passed to the request_key actor if supplied
23 */
24struct key_construction {
25 struct key *key; /* key being constructed */
26 struct key *authkey;/* authorisation for key being constructed */
27};
28
David Howellscf7f6012012-09-13 13:06:29 +010029/*
30 * Pre-parsed payload, used by key add, update and instantiate.
31 *
32 * This struct will be cleared and data and datalen will be set with the data
33 * and length parameters from the caller and quotalen will be set from
34 * def_datalen from the key type. Then if the preparse() op is provided by the
35 * key type, that will be called. Then the struct will be passed to the
36 * instantiate() or the update() op.
37 *
38 * If the preparse() op is given, the free_preparse() op will be called to
39 * clear the contents.
40 */
41struct key_preparsed_payload {
42 char *description; /* Proposed key description (or NULL) */
43 void *type_data[2]; /* Private key-type data */
David Howellsfc7c70e2014-07-18 18:56:34 +010044 void *payload[2]; /* Proposed payload */
David Howellscf7f6012012-09-13 13:06:29 +010045 const void *data; /* Raw data */
46 size_t datalen; /* Raw datalen */
47 size_t quotalen; /* Quota length for proposed payload */
David Howells7dfa0ca2014-07-18 18:56:34 +010048 time_t expiry; /* Expiry time of key */
David Howells008643b2013-08-30 16:07:37 +010049 bool trusted; /* True if key is trusted */
David Howellscf7f6012012-09-13 13:06:29 +010050};
51
David Howells76181c12007-10-16 23:29:46 -070052typedef int (*request_key_actor_t)(struct key_construction *key,
53 const char *op, void *aux);
54
55/*
David Howells46291952014-09-16 17:36:02 +010056 * Preparsed matching criterion.
57 */
58struct key_match_data {
59 /* Comparison function, defaults to type->match, but can be replaced by
60 * type->match_preparse(). */
61 int (*cmp)(const struct key *key,
62 const struct key_match_data *match_data);
63
64 const void *raw_data; /* Raw match data */
65 void *preparsed; /* For ->match_preparse() to stash stuff */
66 unsigned lookup_type; /* Type of lookup for this search. */
67#define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
68#define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
69};
70
71/*
David Howells76181c12007-10-16 23:29:46 -070072 * kernel managed key type definition
73 */
74struct key_type {
75 /* name of the type */
76 const char *name;
77
78 /* default payload length for quota precalculation (optional)
79 * - this can be used instead of calling key_payload_reserve(), that
80 * function only needs to be called if the real datalen is different
81 */
82 size_t def_datalen;
83
David Howells4bdf0bc2013-09-24 10:35:15 +010084 /* Default key search algorithm. */
85 unsigned def_lookup_type;
David Howells4bdf0bc2013-09-24 10:35:15 +010086
David Howellsb9fffa32011-03-07 15:05:59 +000087 /* vet a description */
88 int (*vet_description)(const char *description);
89
David Howellscf7f6012012-09-13 13:06:29 +010090 /* 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
David Howells76181c12007-10-16 23:29:46 -0700100 /* 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 */
David Howellscf7f6012012-09-13 13:06:29 +0100104 int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
David Howells76181c12007-10-16 23:29:46 -0700105
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 */
David Howellscf7f6012012-09-13 13:06:29 +0100111 int (*update)(struct key *key, struct key_preparsed_payload *prep);
David Howells76181c12007-10-16 23:29:46 -0700112
David Howells46291952014-09-16 17:36:02 +0100113 /* 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
David Howells76181c12007-10-16 23:29:46 -0700119 /* match a key against a description */
David Howells46291952014-09-16 17:36:02 +0100120 int (*match)(const struct key *key,
121 const struct key_match_data *match_data);
122
123 /* Free preparsed match data (optional). This should be supplied it
124 * ->match_preparse() is supplied. */
125 void (*match_free)(struct key_match_data *match_data);
David Howells76181c12007-10-16 23:29:46 -0700126
127 /* clear some of the data from a key on revokation (optional)
128 * - the key's semaphore will be write-locked by the caller
129 */
130 void (*revoke)(struct key *key);
131
132 /* clear the data from a key (optional) */
133 void (*destroy)(struct key *key);
134
135 /* describe a key */
136 void (*describe)(const struct key *key, struct seq_file *p);
137
138 /* read a key's data (optional)
139 * - permission checks will be done by the caller
140 * - the key's semaphore will be readlocked by the caller
141 * - should return the amount of data that could be read, no matter how
142 * much is copied into the buffer
143 * - shouldn't do the copy if the buffer is NULL
144 */
145 long (*read)(const struct key *key, char __user *buffer, size_t buflen);
146
147 /* handle request_key() for this type instead of invoking
148 * /sbin/request-key (optional)
149 * - key is the key to instantiate
150 * - authkey is the authority to assume when instantiating this key
151 * - op is the operation to be done, usually "create"
152 * - the call must not return until the instantiation process has run
153 * its course
154 */
155 request_key_actor_t request_key;
156
157 /* internal fields */
158 struct list_head link; /* link in types list */
David Howells7845bc392011-11-16 11:15:54 +0000159 struct lock_class_key lock_class; /* key->sem lock class */
David Howells76181c12007-10-16 23:29:46 -0700160};
161
162extern struct key_type key_type_keyring;
163
164extern int register_key_type(struct key_type *ktype);
165extern void unregister_key_type(struct key_type *ktype);
166
167extern int key_payload_reserve(struct key *key, size_t datalen);
168extern int key_instantiate_and_link(struct key *key,
169 const void *data,
170 size_t datalen,
171 struct key *keyring,
172 struct key *instkey);
David Howellsfdd1b942011-03-07 15:06:09 +0000173extern int key_reject_and_link(struct key *key,
David Howells76181c12007-10-16 23:29:46 -0700174 unsigned timeout,
David Howellsfdd1b942011-03-07 15:06:09 +0000175 unsigned error,
David Howells76181c12007-10-16 23:29:46 -0700176 struct key *keyring,
177 struct key *instkey);
178extern void complete_request_key(struct key_construction *cons, int error);
179
David Howellsfdd1b942011-03-07 15:06:09 +0000180static inline int key_negate_and_link(struct key *key,
181 unsigned timeout,
182 struct key *keyring,
183 struct key *instkey)
184{
185 return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
186}
187
David Howells6a09d172014-07-18 18:56:34 +0100188extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
189
David Howells76181c12007-10-16 23:29:46 -0700190#endif /* CONFIG_KEYS */
191#endif /* _LINUX_KEY_TYPE_H */