PageRenderTime 74ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/include/linux/key-type.h

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