asymmetric-keys.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============================================
  3. Asymmetric / Public-key Cryptography Key Type
  4. =============================================
  5. .. Contents:
  6. - Overview.
  7. - Key identification.
  8. - Accessing asymmetric keys.
  9. - Signature verification.
  10. - Asymmetric key subtypes.
  11. - Instantiation data parsers.
  12. - Keyring link restrictions.
  13. Overview
  14. ========
  15. The "asymmetric" key type is designed to be a container for the keys used in
  16. public-key cryptography, without imposing any particular restrictions on the
  17. form or mechanism of the cryptography or form of the key.
  18. The asymmetric key is given a subtype that defines what sort of data is
  19. associated with the key and provides operations to describe and destroy it.
  20. However, no requirement is made that the key data actually be stored in the
  21. key.
  22. A completely in-kernel key retention and operation subtype can be defined, but
  23. it would also be possible to provide access to cryptographic hardware (such as
  24. a TPM) that might be used to both retain the relevant key and perform
  25. operations using that key. In such a case, the asymmetric key would then
  26. merely be an interface to the TPM driver.
  27. Also provided is the concept of a data parser. Data parsers are responsible
  28. for extracting information from the blobs of data passed to the instantiation
  29. function. The first data parser that recognises the blob gets to set the
  30. subtype of the key and define the operations that can be done on that key.
  31. A data parser may interpret the data blob as containing the bits representing a
  32. key, or it may interpret it as a reference to a key held somewhere else in the
  33. system (for example, a TPM).
  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 asymmetric modsign.0: DSA 5acc2142 []
  58. Accessing Asymmetric Keys
  59. =========================
  60. For general access to asymmetric keys from within the kernel, the following
  61. inclusion is required::
  62. #include <crypto/public_key.h>
  63. This gives access to functions for dealing with asymmetric / public keys.
  64. Three enums are defined there for representing public-key cryptography
  65. algorithms::
  66. enum pkey_algo
  67. digest algorithms used by those::
  68. enum pkey_hash_algo
  69. and key identifier representations::
  70. enum pkey_id_type
  71. Note that the key type representation types are required because key
  72. identifiers from different standards aren't necessarily compatible. For
  73. instance, PGP generates key identifiers by hashing the key data plus some
  74. PGP-specific metadata, whereas X.509 has arbitrary certificate identifiers.
  75. The operations defined upon a key are:
  76. 1) Signature verification.
  77. Other operations are possible (such as encryption) with the same key data
  78. required for verification, but not currently supported, and others
  79. (eg. decryption and signature generation) require extra key data.
  80. Signature Verification
  81. ----------------------
  82. An operation is provided to perform cryptographic signature verification, using
  83. an asymmetric key to provide or to provide access to the public key::
  84. int verify_signature(const struct key *key,
  85. const struct public_key_signature *sig);
  86. The caller must have already obtained the key from some source and can then use
  87. it to check the signature. The caller must have parsed the signature and
  88. transferred the relevant bits to the structure pointed to by sig::
  89. struct public_key_signature {
  90. u8 *digest;
  91. u8 digest_size;
  92. enum pkey_hash_algo pkey_hash_algo : 8;
  93. u8 nr_mpi;
  94. union {
  95. MPI mpi[2];
  96. ...
  97. };
  98. };
  99. The algorithm used must be noted in sig->pkey_hash_algo, and all the MPIs that
  100. make up the actual signature must be stored in sig->mpi[] and the count of MPIs
  101. placed in sig->nr_mpi.
  102. In addition, the data must have been digested by the caller and the resulting
  103. hash must be pointed to by sig->digest and the size of the hash be placed in
  104. sig->digest_size.
  105. The function will return 0 upon success or -EKEYREJECTED if the signature
  106. doesn't match.
  107. The function may also return -ENOTSUPP if an unsupported public-key algorithm
  108. or public-key/hash algorithm combination is specified or the key doesn't
  109. support the operation; -EBADMSG or -ERANGE if some of the parameters have weird
  110. data; or -ENOMEM if an allocation can't be performed. -EINVAL can be returned
  111. if the key argument is the wrong type or is incompletely set up.
  112. Asymmetric Key Subtypes
  113. =======================
  114. Asymmetric keys have a subtype that defines the set of operations that can be
  115. performed on that key and that determines what data is attached as the key
  116. payload. The payload format is entirely at the whim of the subtype.
  117. The subtype is selected by the key data parser and the parser must initialise
  118. the data required for it. The asymmetric key retains a reference on the
  119. subtype module.
  120. The subtype definition structure can be found in::
  121. #include <keys/asymmetric-subtype.h>
  122. and looks like the following::
  123. struct asymmetric_key_subtype {
  124. struct module *owner;
  125. const char *name;
  126. void (*describe)(const struct key *key, struct seq_file *m);
  127. void (*destroy)(void *payload);
  128. int (*query)(const struct kernel_pkey_params *params,
  129. struct kernel_pkey_query *info);
  130. int (*eds_op)(struct kernel_pkey_params *params,
  131. const void *in, void *out);
  132. int (*verify_signature)(const struct key *key,
  133. const struct public_key_signature *sig);
  134. };
  135. Asymmetric keys point to this with their payload[asym_subtype] member.
  136. The owner and name fields should be set to the owning module and the name of
  137. the subtype. Currently, the name is only used for print statements.
  138. There are a number of operations defined by the subtype:
  139. 1) describe().
  140. Mandatory. This allows the subtype to display something in /proc/keys
  141. against the key. For instance the name of the public key algorithm type
  142. could be displayed. The key type will display the tail of the key
  143. identity string after this.
  144. 2) destroy().
  145. Mandatory. This should free the memory associated with the key. The
  146. asymmetric key will look after freeing the fingerprint and releasing the
  147. reference on the subtype module.
  148. 3) query().
  149. Mandatory. This is a function for querying the capabilities of a key.
  150. 4) eds_op().
  151. Optional. This is the entry point for the encryption, decryption and
  152. signature creation operations (which are distinguished by the operation ID
  153. in the parameter struct). The subtype may do anything it likes to
  154. implement an operation, including offloading to hardware.
  155. 5) verify_signature().
  156. Optional. This is the entry point for signature verification. The
  157. subtype may do anything it likes to implement an operation, including
  158. offloading to hardware.
  159. Instantiation Data Parsers
  160. ==========================
  161. The asymmetric key type doesn't generally want to store or to deal with a raw
  162. blob of data that holds the key data. It would have to parse it and error
  163. check it each time it wanted to use it. Further, the contents of the blob may
  164. have various checks that can be performed on it (eg. self-signatures, validity
  165. dates) and may contain useful data about the key (identifiers, capabilities).
  166. Also, the blob may represent a pointer to some hardware containing the key
  167. rather than the key itself.
  168. Examples of blob formats for which parsers could be implemented include:
  169. - OpenPGP packet stream [RFC 4880].
  170. - X.509 ASN.1 stream.
  171. - Pointer to TPM key.
  172. - Pointer to UEFI key.
  173. - PKCS#8 private key [RFC 5208].
  174. - PKCS#5 encrypted private key [RFC 2898].
  175. During key instantiation each parser in the list is tried until one doesn't
  176. return -EBADMSG.
  177. The parser definition structure can be found in::
  178. #include <keys/asymmetric-parser.h>
  179. and looks like the following::
  180. struct asymmetric_key_parser {
  181. struct module *owner;
  182. const char *name;
  183. int (*parse)(struct key_preparsed_payload *prep);
  184. };
  185. The owner and name fields should be set to the owning module and the name of
  186. the parser.
  187. There is currently only a single operation defined by the parser, and it is
  188. mandatory:
  189. 1) parse().
  190. This is called to preparse the key from the key creation and update paths.
  191. In particular, it is called during the key creation _before_ a key is
  192. allocated, and as such, is permitted to provide the key's description in
  193. the case that the caller declines to do so.
  194. The caller passes a pointer to the following struct with all of the fields
  195. cleared, except for data, datalen and quotalen [see
  196. Documentation/security/keys/core.rst]::
  197. struct key_preparsed_payload {
  198. char *description;
  199. void *payload[4];
  200. const void *data;
  201. size_t datalen;
  202. size_t quotalen;
  203. };
  204. The instantiation data is in a blob pointed to by data and is datalen in
  205. size. The parse() function is not permitted to change these two values at
  206. all, and shouldn't change any of the other values _unless_ they are
  207. recognise the blob format and will not return -EBADMSG to indicate it is
  208. not theirs.
  209. If the parser is happy with the blob, it should propose a description for
  210. the key and attach it to ->description, ->payload[asym_subtype] should be
  211. set to point to the subtype to be used, ->payload[asym_crypto] should be
  212. set to point to the initialised data for that subtype,
  213. ->payload[asym_key_ids] should point to one or more hex fingerprints and
  214. quotalen should be updated to indicate how much quota this key should
  215. account for.
  216. When clearing up, the data attached to ->payload[asym_key_ids] and
  217. ->description will be kfree()'d and the data attached to
  218. ->payload[asm_crypto] will be passed to the subtype's ->destroy() method
  219. to be disposed of. A module reference for the subtype pointed to by
  220. ->payload[asym_subtype] will be put.
  221. If the data format is not recognised, -EBADMSG should be returned. If it
  222. is recognised, but the key cannot for some reason be set up, some other
  223. negative error code should be returned. On success, 0 should be returned.
  224. The key's fingerprint string may be partially matched upon. For a
  225. public-key algorithm such as RSA and DSA this will likely be a printable
  226. hex version of the key's fingerprint.
  227. Functions are provided to register and unregister parsers::
  228. int register_asymmetric_key_parser(struct asymmetric_key_parser *parser);
  229. void unregister_asymmetric_key_parser(struct asymmetric_key_parser *subtype);
  230. Parsers may not have the same name. The names are otherwise only used for
  231. displaying in debugging messages.
  232. Keyring Link Restrictions
  233. =========================
  234. Keyrings created from userspace using add_key can be configured to check the
  235. signature of the key being linked. Keys without a valid signature are not
  236. allowed to link.
  237. Several restriction methods are available:
  238. 1) Restrict using the kernel builtin trusted keyring
  239. - Option string used with KEYCTL_RESTRICT_KEYRING:
  240. - "builtin_trusted"
  241. The kernel builtin trusted keyring will be searched for the signing key.
  242. If the builtin trusted keyring is not configured, all links will be
  243. rejected. The ca_keys kernel parameter also affects which keys are used
  244. for signature verification.
  245. 2) Restrict using the kernel builtin and secondary trusted keyrings
  246. - Option string used with KEYCTL_RESTRICT_KEYRING:
  247. - "builtin_and_secondary_trusted"
  248. The kernel builtin and secondary trusted keyrings will be searched for the
  249. signing key. If the secondary trusted keyring is not configured, this
  250. restriction will behave like the "builtin_trusted" option. The ca_keys
  251. kernel parameter also affects which keys are used for signature
  252. verification.
  253. 3) Restrict using a separate key or keyring
  254. - Option string used with KEYCTL_RESTRICT_KEYRING:
  255. - "key_or_keyring:<key or keyring serial number>[:chain]"
  256. Whenever a key link is requested, the link will only succeed if the key
  257. being linked is signed by one of the designated keys. This key may be
  258. specified directly by providing a serial number for one asymmetric key, or
  259. a group of keys may be searched for the signing key by providing the
  260. serial number for a keyring.
  261. When the "chain" option is provided at the end of the string, the keys
  262. within the destination keyring will also be searched for signing keys.
  263. This allows for verification of certificate chains by adding each
  264. certificate in order (starting closest to the root) to a keyring. For
  265. instance, one keyring can be populated with links to a set of root
  266. certificates, with a separate, restricted keyring set up for each
  267. certificate chain to be validated::
  268. # Create and populate a keyring for root certificates
  269. root_id=`keyctl add keyring root-certs "" @s`
  270. keyctl padd asymmetric "" $root_id < root1.cert
  271. keyctl padd asymmetric "" $root_id < root2.cert
  272. # Create and restrict a keyring for the certificate chain
  273. chain_id=`keyctl add keyring chain "" @s`
  274. keyctl restrict_keyring $chain_id asymmetric key_or_keyring:$root_id:chain
  275. # Attempt to add each certificate in the chain, starting with the
  276. # certificate closest to the root.
  277. keyctl padd asymmetric "" $chain_id < intermediateA.cert
  278. keyctl padd asymmetric "" $chain_id < intermediateB.cert
  279. keyctl padd asymmetric "" $chain_id < end-entity.cert
  280. If the final end-entity certificate is successfully added to the "chain"
  281. keyring, we can be certain that it has a valid signing chain going back to
  282. one of the root certificates.
  283. A single keyring can be used to verify a chain of signatures by
  284. restricting the keyring after linking the root certificate::
  285. # Create a keyring for the certificate chain and add the root
  286. chain2_id=`keyctl add keyring chain2 "" @s`
  287. keyctl padd asymmetric "" $chain2_id < root1.cert
  288. # Restrict the keyring that already has root1.cert linked. The cert
  289. # will remain linked by the keyring.
  290. keyctl restrict_keyring $chain2_id asymmetric key_or_keyring:0:chain
  291. # Attempt to add each certificate in the chain, starting with the
  292. # certificate closest to the root.
  293. keyctl padd asymmetric "" $chain2_id < intermediateA.cert
  294. keyctl padd asymmetric "" $chain2_id < intermediateB.cert
  295. keyctl padd asymmetric "" $chain2_id < end-entity.cert
  296. If the final end-entity certificate is successfully added to the "chain2"
  297. keyring, we can be certain that there is a valid signing chain going back
  298. to the root certificate that was added before the keyring was restricted.
  299. In all of these cases, if the signing key is found the signature of the key to
  300. be linked will be verified using the signing key. The requested key is added
  301. to the keyring only if the signature is successfully verified. -ENOKEY is
  302. returned if the parent certificate could not be found, or -EKEYREJECTED is
  303. returned if the signature check fails or the key is blacklisted. Other errors
  304. may be returned if the signature check could not be performed.