asymmetric-keys.txt 15 KB

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