asymmetric-keys.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. =============================================
  2. ASYMMETRIC / PUBLIC-KEY CRYPTOGRAPHY KEY TYPE
  3. =============================================
  4. Contents:
  5. - Overview.
  6. - Key identification.
  7. - Accessing asymmetric keys.
  8. - Signature verification.
  9. - Asymmetric key subtypes.
  10. - Instantiation data parsers.
  11. ========
  12. OVERVIEW
  13. ========
  14. The "asymmetric" key type is designed to be a container for the keys used in
  15. public-key cryptography, without imposing any particular restrictions on the
  16. form or mechanism of the cryptography or form of the key.
  17. The asymmetric key is given a subtype that defines what sort of data is
  18. associated with the key and provides operations to describe and destroy it.
  19. However, no requirement is made that the key data actually be stored in the
  20. key.
  21. A completely in-kernel key retention and operation subtype can be defined, but
  22. it would also be possible to provide access to cryptographic hardware (such as
  23. a TPM) that might be used to both retain the relevant key and perform
  24. operations using that key. In such a case, the asymmetric key would then
  25. merely be an interface to the TPM driver.
  26. Also provided is the concept of a data parser. Data parsers are responsible
  27. for extracting information from the blobs of data passed to the instantiation
  28. function. The first data parser that recognises the blob gets to set the
  29. subtype of the key and define the operations that can be done on that key.
  30. A data parser may interpret the data blob as containing the bits representing a
  31. key, or it may interpret it as a reference to a key held somewhere else in the
  32. system (for example, a TPM).
  33. ==================
  34. KEY IDENTIFICATION
  35. ==================
  36. If a key is added with an empty name, the instantiation data parsers are given
  37. the opportunity to pre-parse a key and to determine the description the key
  38. should be given from the content of the key.
  39. This can then be used to refer to the key, either by complete match or by
  40. partial match. The key type may also use other criteria to refer to a key.
  41. The asymmetric key type's match function can then perform a wider range of
  42. comparisons than just the straightforward comparison of the description with
  43. the criterion string:
  44. (1) If the criterion string is of the form "id:<hexdigits>" then the match
  45. function will examine a key's fingerprint to see if the hex digits given
  46. after the "id:" match the tail. For instance:
  47. keyctl search @s asymmetric id:5acc2142
  48. will match a key with fingerprint:
  49. 1A00 2040 7601 7889 DE11 882C 3823 04AD 5ACC 2142
  50. (2) If the criterion string is of the form "<subtype>:<hexdigits>" then the
  51. match will match the ID as in (1), but with the added restriction that
  52. only keys of the specified subtype (e.g. tpm) will be matched. For
  53. instance:
  54. keyctl search @s asymmetric tpm:5acc2142
  55. Looking in /proc/keys, the last 8 hex digits of the key fingerprint are
  56. displayed, along with the subtype:
  57. 1a39e171 I----- 1 perm 3f010000 0 0 asymmetri modsign.0: DSA 5acc2142 []
  58. =========================
  59. ACCESSING ASYMMETRIC KEYS
  60. =========================
  61. For general access to asymmetric keys from within the kernel, the following
  62. inclusion is required:
  63. #include <crypto/public_key.h>
  64. This gives access to functions for dealing with asymmetric / public keys.
  65. Three enums are defined there for representing public-key cryptography
  66. algorithms:
  67. enum pkey_algo
  68. digest algorithms used by those:
  69. enum pkey_hash_algo
  70. and key identifier representations:
  71. enum pkey_id_type
  72. Note that the key type representation types are required because key
  73. identifiers from different standards aren't necessarily compatible. For
  74. instance, PGP generates key identifiers by hashing the key data plus some
  75. PGP-specific metadata, whereas X.509 has arbitrary certificate identifiers.
  76. The operations defined upon a key are:
  77. (1) Signature verification.
  78. Other operations are possible (such as encryption) with the same key data
  79. required for verification, but not currently supported, and others
  80. (eg. decryption and signature generation) require extra key data.
  81. SIGNATURE VERIFICATION
  82. ----------------------
  83. An operation is provided to perform cryptographic signature verification, using
  84. an asymmetric key to provide or to provide access to the public key.
  85. int verify_signature(const struct key *key,
  86. const struct public_key_signature *sig);
  87. The caller must have already obtained the key from some source and can then use
  88. it to check the signature. The caller must have parsed the signature and
  89. transferred the relevant bits to the structure pointed to by sig.
  90. struct public_key_signature {
  91. u8 *digest;
  92. u8 digest_size;
  93. enum pkey_hash_algo pkey_hash_algo : 8;
  94. u8 nr_mpi;
  95. union {
  96. MPI mpi[2];
  97. ...
  98. };
  99. };
  100. The algorithm used must be noted in sig->pkey_hash_algo, and all the MPIs that
  101. make up the actual signature must be stored in sig->mpi[] and the count of MPIs
  102. placed in sig->nr_mpi.
  103. In addition, the data must have been digested by the caller and the resulting
  104. hash must be pointed to by sig->digest and the size of the hash be placed in
  105. sig->digest_size.
  106. The function will return 0 upon success or -EKEYREJECTED if the signature
  107. doesn't match.
  108. The function may also return -ENOTSUPP if an unsupported public-key algorithm
  109. or public-key/hash algorithm combination is specified or the key doesn't
  110. support the operation; -EBADMSG or -ERANGE if some of the parameters have weird
  111. data; or -ENOMEM if an allocation can't be performed. -EINVAL can be returned
  112. if the key argument is the wrong type or is incompletely set up.
  113. =======================
  114. ASYMMETRIC KEY SUBTYPES
  115. =======================
  116. Asymmetric keys have a subtype that defines the set of operations that can be
  117. performed on that key and that determines what data is attached as the key
  118. payload. The payload format is entirely at the whim of the subtype.
  119. The subtype is selected by the key data parser and the parser must initialise
  120. the data required for it. The asymmetric key retains a reference on the
  121. subtype module.
  122. The subtype definition structure can be found in:
  123. #include <keys/asymmetric-subtype.h>
  124. and looks like the following:
  125. struct asymmetric_key_subtype {
  126. struct module *owner;
  127. const char *name;
  128. void (*describe)(const struct key *key, struct seq_file *m);
  129. void (*destroy)(void *payload);
  130. int (*verify_signature)(const struct key *key,
  131. const struct public_key_signature *sig);
  132. };
  133. Asymmetric keys point to this with their payload[asym_subtype] member.
  134. The owner and name fields should be set to the owning module and the name of
  135. the subtype. Currently, the name is only used for print statements.
  136. There are a number of operations defined by the subtype:
  137. (1) describe().
  138. Mandatory. This allows the subtype to display something in /proc/keys
  139. against the key. For instance the name of the public key algorithm type
  140. could be displayed. The key type will display the tail of the key
  141. identity string after this.
  142. (2) destroy().
  143. Mandatory. This should free the memory associated with the key. The
  144. asymmetric key will look after freeing the fingerprint and releasing the
  145. reference on the subtype module.
  146. (3) verify_signature().
  147. Optional. These are the entry points for the key usage operations.
  148. Currently there is only the one defined. If not set, the caller will be
  149. given -ENOTSUPP. The subtype may do anything it likes to implement an
  150. operation, including offloading to hardware.
  151. ==========================
  152. INSTANTIATION DATA PARSERS
  153. ==========================
  154. The asymmetric key type doesn't generally want to store or to deal with a raw
  155. blob of data that holds the key data. It would have to parse it and error
  156. check it each time it wanted to use it. Further, the contents of the blob may
  157. have various checks that can be performed on it (eg. self-signatures, validity
  158. dates) and may contain useful data about the key (identifiers, capabilities).
  159. Also, the blob may represent a pointer to some hardware containing the key
  160. rather than the key itself.
  161. Examples of blob formats for which parsers could be implemented include:
  162. - OpenPGP packet stream [RFC 4880].
  163. - X.509 ASN.1 stream.
  164. - Pointer to TPM key.
  165. - Pointer to UEFI key.
  166. During key instantiation each parser in the list is tried until one doesn't
  167. return -EBADMSG.
  168. The parser definition structure can be found in:
  169. #include <keys/asymmetric-parser.h>
  170. and looks like the following:
  171. struct asymmetric_key_parser {
  172. struct module *owner;
  173. const char *name;
  174. int (*parse)(struct key_preparsed_payload *prep);
  175. };
  176. The owner and name fields should be set to the owning module and the name of
  177. the parser.
  178. There is currently only a single operation defined by the parser, and it is
  179. mandatory:
  180. (1) parse().
  181. This is called to preparse the key from the key creation and update paths.
  182. In particular, it is called during the key creation _before_ a key is
  183. allocated, and as such, is permitted to provide the key's description in
  184. the case that the caller declines to do so.
  185. The caller passes a pointer to the following struct with all of the fields
  186. cleared, except for data, datalen and quotalen [see
  187. Documentation/security/keys.txt].
  188. struct key_preparsed_payload {
  189. char *description;
  190. void *payload[4];
  191. const void *data;
  192. size_t datalen;
  193. size_t quotalen;
  194. };
  195. The instantiation data is in a blob pointed to by data and is datalen in
  196. size. The parse() function is not permitted to change these two values at
  197. all, and shouldn't change any of the other values _unless_ they are
  198. recognise the blob format and will not return -EBADMSG to indicate it is
  199. not theirs.
  200. If the parser is happy with the blob, it should propose a description for
  201. the key and attach it to ->description, ->payload[asym_subtype] should be
  202. set to point to the subtype to be used, ->payload[asym_crypto] should be
  203. set to point to the initialised data for that subtype,
  204. ->payload[asym_key_ids] should point to one or more hex fingerprints and
  205. quotalen should be updated to indicate how much quota this key should
  206. account for.
  207. When clearing up, the data attached to ->payload[asym_key_ids] and
  208. ->description will be kfree()'d and the data attached to
  209. ->payload[asm_crypto] will be passed to the subtype's ->destroy() method
  210. to be disposed of. A module reference for the subtype pointed to by
  211. ->payload[asym_subtype] will be put.
  212. If the data format is not recognised, -EBADMSG should be returned. If it
  213. is recognised, but the key cannot for some reason be set up, some other
  214. negative error code should be returned. On success, 0 should be returned.
  215. The key's fingerprint string may be partially matched upon. For a
  216. public-key algorithm such as RSA and DSA this will likely be a printable
  217. hex version of the key's fingerprint.
  218. Functions are provided to register and unregister parsers:
  219. int register_asymmetric_key_parser(struct asymmetric_key_parser *parser);
  220. void unregister_asymmetric_key_parser(struct asymmetric_key_parser *subtype);
  221. Parsers may not have the same name. The names are otherwise only used for
  222. displaying in debugging messages.