xref: /linux-6.15/include/linux/key-type.h (revision e8fa600e)
1 /* Definitions for key type implementations
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells ([email protected])
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 /*
21  * key under-construction record
22  * - passed to the request_key actor if supplied
23  */
24 struct key_construction {
25 	struct key	*key;	/* key being constructed */
26 	struct key	*authkey;/* authorisation for key being constructed */
27 };
28 
29 typedef int (*request_key_actor_t)(struct key_construction *key,
30 				   const char *op, void *aux);
31 
32 /*
33  * kernel managed key type definition
34  */
35 struct key_type {
36 	/* name of the type */
37 	const char *name;
38 
39 	/* default payload length for quota precalculation (optional)
40 	 * - this can be used instead of calling key_payload_reserve(), that
41 	 *   function only needs to be called if the real datalen is different
42 	 */
43 	size_t def_datalen;
44 
45 	/* vet a description */
46 	int (*vet_description)(const char *description);
47 
48 	/* instantiate a key of this type
49 	 * - this method should call key_payload_reserve() to determine if the
50 	 *   user's quota will hold the payload
51 	 */
52 	int (*instantiate)(struct key *key, const void *data, size_t datalen);
53 
54 	/* update a key of this type (optional)
55 	 * - this method should call key_payload_reserve() to recalculate the
56 	 *   quota consumption
57 	 * - the key must be locked against read when modifying
58 	 */
59 	int (*update)(struct key *key, const void *data, size_t datalen);
60 
61 	/* match a key against a description */
62 	int (*match)(const struct key *key, const void *desc);
63 
64 	/* clear some of the data from a key on revokation (optional)
65 	 * - the key's semaphore will be write-locked by the caller
66 	 */
67 	void (*revoke)(struct key *key);
68 
69 	/* clear the data from a key (optional) */
70 	void (*destroy)(struct key *key);
71 
72 	/* describe a key */
73 	void (*describe)(const struct key *key, struct seq_file *p);
74 
75 	/* read a key's data (optional)
76 	 * - permission checks will be done by the caller
77 	 * - the key's semaphore will be readlocked by the caller
78 	 * - should return the amount of data that could be read, no matter how
79 	 *   much is copied into the buffer
80 	 * - shouldn't do the copy if the buffer is NULL
81 	 */
82 	long (*read)(const struct key *key, char __user *buffer, size_t buflen);
83 
84 	/* handle request_key() for this type instead of invoking
85 	 * /sbin/request-key (optional)
86 	 * - key is the key to instantiate
87 	 * - authkey is the authority to assume when instantiating this key
88 	 * - op is the operation to be done, usually "create"
89 	 * - the call must not return until the instantiation process has run
90 	 *   its course
91 	 */
92 	request_key_actor_t request_key;
93 
94 	/* internal fields */
95 	struct list_head	link;		/* link in types list */
96 	struct lock_class_key	lock_class;	/* key->sem lock class */
97 };
98 
99 extern struct key_type key_type_keyring;
100 
101 extern int register_key_type(struct key_type *ktype);
102 extern void unregister_key_type(struct key_type *ktype);
103 
104 extern int key_payload_reserve(struct key *key, size_t datalen);
105 extern int key_instantiate_and_link(struct key *key,
106 				    const void *data,
107 				    size_t datalen,
108 				    struct key *keyring,
109 				    struct key *instkey);
110 extern int key_reject_and_link(struct key *key,
111 			       unsigned timeout,
112 			       unsigned error,
113 			       struct key *keyring,
114 			       struct key *instkey);
115 extern void complete_request_key(struct key_construction *cons, int error);
116 
117 static inline int key_negate_and_link(struct key *key,
118 				      unsigned timeout,
119 				      struct key *keyring,
120 				      struct key *instkey)
121 {
122 	return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
123 }
124 
125 #endif /* CONFIG_KEYS */
126 #endif /* _LINUX_KEY_TYPE_H */
127