blob: f58737bcb05008a555ca7acc63f0dbf2272b131f [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 */
44 void *payload; /* Proposed payload */
45 const void *data; /* Raw data */
46 size_t datalen; /* Raw datalen */
47 size_t quotalen; /* Quota length for proposed payload */
48};
49
David Howells76181c12007-10-16 23:29:46 -070050typedef int (*request_key_actor_t)(struct key_construction *key,
51 const char *op, void *aux);
52
53/*
54 * kernel managed key type definition
55 */
56struct key_type {
57 /* name of the type */
58 const char *name;
59
60 /* default payload length for quota precalculation (optional)
61 * - this can be used instead of calling key_payload_reserve(), that
62 * function only needs to be called if the real datalen is different
63 */
64 size_t def_datalen;
65
David Howells4bdf0bc2013-09-24 10:35:15 +010066 /* Default key search algorithm. */
67 unsigned def_lookup_type;
68#define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
69#define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
70
David Howellsb9fffa32011-03-07 15:05:59 +000071 /* vet a description */
72 int (*vet_description)(const char *description);
73
David Howellscf7f6012012-09-13 13:06:29 +010074 /* Preparse the data blob from userspace that is to be the payload,
75 * generating a proposed description and payload that will be handed to
76 * the instantiate() and update() ops.
77 */
78 int (*preparse)(struct key_preparsed_payload *prep);
79
80 /* Free a preparse data structure.
81 */
82 void (*free_preparse)(struct key_preparsed_payload *prep);
83
David Howells76181c12007-10-16 23:29:46 -070084 /* instantiate a key of this type
85 * - this method should call key_payload_reserve() to determine if the
86 * user's quota will hold the payload
87 */
David Howellscf7f6012012-09-13 13:06:29 +010088 int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
David Howells76181c12007-10-16 23:29:46 -070089
90 /* update a key of this type (optional)
91 * - this method should call key_payload_reserve() to recalculate the
92 * quota consumption
93 * - the key must be locked against read when modifying
94 */
David Howellscf7f6012012-09-13 13:06:29 +010095 int (*update)(struct key *key, struct key_preparsed_payload *prep);
David Howells76181c12007-10-16 23:29:46 -070096
97 /* match a key against a description */
98 int (*match)(const struct key *key, const void *desc);
99
100 /* clear some of the data from a key on revokation (optional)
101 * - the key's semaphore will be write-locked by the caller
102 */
103 void (*revoke)(struct key *key);
104
105 /* clear the data from a key (optional) */
106 void (*destroy)(struct key *key);
107
108 /* describe a key */
109 void (*describe)(const struct key *key, struct seq_file *p);
110
111 /* read a key's data (optional)
112 * - permission checks will be done by the caller
113 * - the key's semaphore will be readlocked by the caller
114 * - should return the amount of data that could be read, no matter how
115 * much is copied into the buffer
116 * - shouldn't do the copy if the buffer is NULL
117 */
118 long (*read)(const struct key *key, char __user *buffer, size_t buflen);
119
120 /* handle request_key() for this type instead of invoking
121 * /sbin/request-key (optional)
122 * - key is the key to instantiate
123 * - authkey is the authority to assume when instantiating this key
124 * - op is the operation to be done, usually "create"
125 * - the call must not return until the instantiation process has run
126 * its course
127 */
128 request_key_actor_t request_key;
129
130 /* internal fields */
131 struct list_head link; /* link in types list */
David Howells7845bc392011-11-16 11:15:54 +0000132 struct lock_class_key lock_class; /* key->sem lock class */
David Howells76181c12007-10-16 23:29:46 -0700133};
134
135extern struct key_type key_type_keyring;
136
137extern int register_key_type(struct key_type *ktype);
138extern void unregister_key_type(struct key_type *ktype);
139
140extern int key_payload_reserve(struct key *key, size_t datalen);
141extern int key_instantiate_and_link(struct key *key,
142 const void *data,
143 size_t datalen,
144 struct key *keyring,
145 struct key *instkey);
David Howellsfdd1b942011-03-07 15:06:09 +0000146extern int key_reject_and_link(struct key *key,
David Howells76181c12007-10-16 23:29:46 -0700147 unsigned timeout,
David Howellsfdd1b942011-03-07 15:06:09 +0000148 unsigned error,
David Howells76181c12007-10-16 23:29:46 -0700149 struct key *keyring,
150 struct key *instkey);
151extern void complete_request_key(struct key_construction *cons, int error);
152
David Howellsfdd1b942011-03-07 15:06:09 +0000153static inline int key_negate_and_link(struct key *key,
154 unsigned timeout,
155 struct key *keyring,
156 struct key *instkey)
157{
158 return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
159}
160
David Howells76181c12007-10-16 23:29:46 -0700161#endif /* CONFIG_KEYS */
162#endif /* _LINUX_KEY_TYPE_H */