key-type.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* 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. #ifndef _LINUX_KEY_TYPE_H
  12. #define _LINUX_KEY_TYPE_H
  13. #include <linux/key.h>
  14. #include <linux/errno.h>
  15. #ifdef CONFIG_KEYS
  16. /*
  17. * key under-construction record
  18. * - passed to the request_key actor if supplied
  19. */
  20. struct key_construction {
  21. struct key *key; /* key being constructed */
  22. struct key *authkey;/* authorisation for key being constructed */
  23. };
  24. /*
  25. * Pre-parsed payload, used by key add, update and instantiate.
  26. *
  27. * This struct will be cleared and data and datalen will be set with the data
  28. * and length parameters from the caller and quotalen will be set from
  29. * def_datalen from the key type. Then if the preparse() op is provided by the
  30. * key type, that will be called. Then the struct will be passed to the
  31. * instantiate() or the update() op.
  32. *
  33. * If the preparse() op is given, the free_preparse() op will be called to
  34. * clear the contents.
  35. */
  36. struct key_preparsed_payload {
  37. char *description; /* Proposed key description (or NULL) */
  38. void *type_data[2]; /* Private key-type data */
  39. void *payload[2]; /* Proposed payload */
  40. const void *data; /* Raw data */
  41. size_t datalen; /* Raw datalen */
  42. size_t quotalen; /* Quota length for proposed payload */
  43. time_t expiry; /* Expiry time of key */
  44. bool trusted; /* True if key is trusted */
  45. };
  46. typedef int (*request_key_actor_t)(struct key_construction *key,
  47. const char *op, void *aux);
  48. /*
  49. * Preparsed matching criterion.
  50. */
  51. struct key_match_data {
  52. /* Comparison function, defaults to exact description match, but can be
  53. * overridden by type->match_preparse(). Should return true if a match
  54. * is found and false if not.
  55. */
  56. bool (*cmp)(const struct key *key,
  57. const struct key_match_data *match_data);
  58. const void *raw_data; /* Raw match data */
  59. void *preparsed; /* For ->match_preparse() to stash stuff */
  60. unsigned lookup_type; /* Type of lookup for this search. */
  61. #define KEYRING_SEARCH_LOOKUP_DIRECT 0x0000 /* Direct lookup by description. */
  62. #define KEYRING_SEARCH_LOOKUP_ITERATE 0x0001 /* Iterative search. */
  63. };
  64. /*
  65. * kernel managed key type definition
  66. */
  67. struct key_type {
  68. /* name of the type */
  69. const char *name;
  70. /* default payload length for quota precalculation (optional)
  71. * - this can be used instead of calling key_payload_reserve(), that
  72. * function only needs to be called if the real datalen is different
  73. */
  74. size_t def_datalen;
  75. /* vet a description */
  76. int (*vet_description)(const char *description);
  77. /* Preparse the data blob from userspace that is to be the payload,
  78. * generating a proposed description and payload that will be handed to
  79. * the instantiate() and update() ops.
  80. */
  81. int (*preparse)(struct key_preparsed_payload *prep);
  82. /* Free a preparse data structure.
  83. */
  84. void (*free_preparse)(struct key_preparsed_payload *prep);
  85. /* instantiate a key of this type
  86. * - this method should call key_payload_reserve() to determine if the
  87. * user's quota will hold the payload
  88. */
  89. int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
  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. */
  95. int (*update)(struct key *key, struct key_preparsed_payload *prep);
  96. /* Preparse the data supplied to ->match() (optional). The
  97. * data to be preparsed can be found in match_data->raw_data.
  98. * The lookup type can also be set by this function.
  99. */
  100. int (*match_preparse)(struct key_match_data *match_data);
  101. /* Free preparsed match data (optional). This should be supplied it
  102. * ->match_preparse() is supplied. */
  103. void (*match_free)(struct key_match_data *match_data);
  104. /* clear some of the data from a key on revokation (optional)
  105. * - the key's semaphore will be write-locked by the caller
  106. */
  107. void (*revoke)(struct key *key);
  108. /* clear the data from a key (optional) */
  109. void (*destroy)(struct key *key);
  110. /* describe a key */
  111. void (*describe)(const struct key *key, struct seq_file *p);
  112. /* read a key's data (optional)
  113. * - permission checks will be done by the caller
  114. * - the key's semaphore will be readlocked by the caller
  115. * - should return the amount of data that could be read, no matter how
  116. * much is copied into the buffer
  117. * - shouldn't do the copy if the buffer is NULL
  118. */
  119. long (*read)(const struct key *key, char __user *buffer, size_t buflen);
  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. /* internal fields */
  130. struct list_head link; /* link in types list */
  131. struct lock_class_key lock_class; /* key->sem lock class */
  132. };
  133. extern struct key_type key_type_keyring;
  134. extern int register_key_type(struct key_type *ktype);
  135. extern void unregister_key_type(struct key_type *ktype);
  136. extern int key_payload_reserve(struct key *key, size_t datalen);
  137. extern int key_instantiate_and_link(struct key *key,
  138. const void *data,
  139. size_t datalen,
  140. struct key *keyring,
  141. struct key *instkey);
  142. extern int key_reject_and_link(struct key *key,
  143. unsigned timeout,
  144. unsigned error,
  145. struct key *keyring,
  146. struct key *instkey);
  147. extern void complete_request_key(struct key_construction *cons, int error);
  148. static inline int key_negate_and_link(struct key *key,
  149. unsigned timeout,
  150. struct key *keyring,
  151. struct key *instkey)
  152. {
  153. return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
  154. }
  155. extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
  156. #endif /* CONFIG_KEYS */
  157. #endif /* _LINUX_KEY_TYPE_H */