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 struct kernel_pkey_query; 21 struct kernel_pkey_params; 22 23 /* 24 * Pre-parsed payload, used by key add, update and instantiate. 25 * 26 * This struct will be cleared and data and datalen will be set with the data 27 * and length parameters from the caller and quotalen will be set from 28 * def_datalen from the key type. Then if the preparse() op is provided by the 29 * key type, that will be called. Then the struct will be passed to the 30 * instantiate() or the update() op. 31 * 32 * If the preparse() op is given, the free_preparse() op will be called to 33 * clear the contents. 34 */ 35 struct key_preparsed_payload { 36 char *description; /* Proposed key description (or NULL) */ 37 union key_payload payload; /* Proposed payload */ 38 const void *data; /* Raw data */ 39 size_t datalen; /* Raw datalen */ 40 size_t quotalen; /* Quota length for proposed payload */ 41 time64_t expiry; /* Expiry time of key */ 42 } __randomize_layout; 43 44 typedef int (*request_key_actor_t)(struct key *auth_key, void *aux); 45 46 /* 47 * Preparsed matching criterion. 48 */ 49 struct key_match_data { 50 /* Comparison function, defaults to exact description match, but can be 51 * overridden by type->match_preparse(). Should return true if a match 52 * is found and false if not. 53 */ 54 bool (*cmp)(const struct key *key, 55 const struct key_match_data *match_data); 56 57 const void *raw_data; /* Raw match data */ 58 void *preparsed; /* For ->match_preparse() to stash stuff */ 59 unsigned lookup_type; /* Type of lookup for this search. */ 60 #define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */ 61 #define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */ 62 }; 63 64 /* 65 * kernel managed key type definition 66 */ 67 struct key_type { 68 /* name of the type */ 69 const char *name; 70 71 /* default payload length for quota precalculation (optional) 72 * - this can be used instead of calling key_payload_reserve(), that 73 * function only needs to be called if the real datalen is different 74 */ 75 size_t def_datalen; 76 77 unsigned int flags; 78 #define KEY_TYPE_NET_DOMAIN 0x00000001 /* Keys of this type have a net namespace domain */ 79 80 /* vet a description */ 81 int (*vet_description)(const char *description); 82 83 /* Preparse the data blob from userspace that is to be the payload, 84 * generating a proposed description and payload that will be handed to 85 * the instantiate() and update() ops. 86 */ 87 int (*preparse)(struct key_preparsed_payload *prep); 88 89 /* Free a preparse data structure. 90 */ 91 void (*free_preparse)(struct key_preparsed_payload *prep); 92 93 /* instantiate a key of this type 94 * - this method should call key_payload_reserve() to determine if the 95 * user's quota will hold the payload 96 */ 97 int (*instantiate)(struct key *key, struct key_preparsed_payload *prep); 98 99 /* update a key of this type (optional) 100 * - this method should call key_payload_reserve() to recalculate the 101 * quota consumption 102 * - the key must be locked against read when modifying 103 */ 104 int (*update)(struct key *key, struct key_preparsed_payload *prep); 105 106 /* Preparse the data supplied to ->match() (optional). The 107 * data to be preparsed can be found in match_data->raw_data. 108 * The lookup type can also be set by this function. 109 */ 110 int (*match_preparse)(struct key_match_data *match_data); 111 112 /* Free preparsed match data (optional). This should be supplied it 113 * ->match_preparse() is supplied. */ 114 void (*match_free)(struct key_match_data *match_data); 115 116 /* clear some of the data from a key on revokation (optional) 117 * - the key's semaphore will be write-locked by the caller 118 */ 119 void (*revoke)(struct key *key); 120 121 /* clear the data from a key (optional) */ 122 void (*destroy)(struct key *key); 123 124 /* describe a key */ 125 void (*describe)(const struct key *key, struct seq_file *p); 126 127 /* read a key's data (optional) 128 * - permission checks will be done by the caller 129 * - the key's semaphore will be readlocked by the caller 130 * - should return the amount of data that could be read, no matter how 131 * much is copied into the buffer 132 * - shouldn't do the copy if the buffer is NULL 133 */ 134 long (*read)(const struct key *key, char __user *buffer, size_t buflen); 135 136 /* handle request_key() for this type instead of invoking 137 * /sbin/request-key (optional) 138 * - key is the key to instantiate 139 * - authkey is the authority to assume when instantiating this key 140 * - op is the operation to be done, usually "create" 141 * - the call must not return until the instantiation process has run 142 * its course 143 */ 144 request_key_actor_t request_key; 145 146 /* Look up a keyring access restriction (optional) 147 * 148 * - NULL is a valid return value (meaning the requested restriction 149 * is known but will never block addition of a key) 150 * - should return -EINVAL if the restriction is unknown 151 */ 152 struct key_restriction *(*lookup_restriction)(const char *params); 153 154 /* Asymmetric key accessor functions. */ 155 int (*asym_query)(const struct kernel_pkey_params *params, 156 struct kernel_pkey_query *info); 157 int (*asym_eds_op)(struct kernel_pkey_params *params, 158 const void *in, void *out); 159 int (*asym_verify_signature)(struct kernel_pkey_params *params, 160 const void *in, const void *in2); 161 162 /* internal fields */ 163 struct list_head link; /* link in types list */ 164 struct lock_class_key lock_class; /* key->sem lock class */ 165 } __randomize_layout; 166 167 extern struct key_type key_type_keyring; 168 169 extern int register_key_type(struct key_type *ktype); 170 extern void unregister_key_type(struct key_type *ktype); 171 172 extern int key_payload_reserve(struct key *key, size_t datalen); 173 extern int key_instantiate_and_link(struct key *key, 174 const void *data, 175 size_t datalen, 176 struct key *keyring, 177 struct key *authkey); 178 extern int key_reject_and_link(struct key *key, 179 unsigned timeout, 180 unsigned error, 181 struct key *keyring, 182 struct key *authkey); 183 extern void complete_request_key(struct key *authkey, int error); 184 185 static inline int key_negate_and_link(struct key *key, 186 unsigned timeout, 187 struct key *keyring, 188 struct key *authkey) 189 { 190 return key_reject_and_link(key, timeout, ENOKEY, keyring, authkey); 191 } 192 193 extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep); 194 195 #endif /* CONFIG_KEYS */ 196 #endif /* _LINUX_KEY_TYPE_H */ 197