drm/amdgpu/swSMU/navi: add feature toggles for more things
[linux-2.6-block.git] / Documentation / crypto / asymmetric-keys.txt
CommitLineData
9a83b465
DH
1 =============================================
2 ASYMMETRIC / PUBLIC-KEY CRYPTOGRAPHY KEY TYPE
3 =============================================
4
5Contents:
6
7 - Overview.
8 - Key identification.
9 - Accessing asymmetric keys.
10 - Signature verification.
11 - Asymmetric key subtypes.
12 - Instantiation data parsers.
7228b66a 13 - Keyring link restrictions.
9a83b465
DH
14
15
16========
17OVERVIEW
18========
19
20The "asymmetric" key type is designed to be a container for the keys used in
21public-key cryptography, without imposing any particular restrictions on the
22form or mechanism of the cryptography or form of the key.
23
24The asymmetric key is given a subtype that defines what sort of data is
25associated with the key and provides operations to describe and destroy it.
26However, no requirement is made that the key data actually be stored in the
27key.
28
29A completely in-kernel key retention and operation subtype can be defined, but
30it would also be possible to provide access to cryptographic hardware (such as
31a TPM) that might be used to both retain the relevant key and perform
32operations using that key. In such a case, the asymmetric key would then
33merely be an interface to the TPM driver.
34
35Also provided is the concept of a data parser. Data parsers are responsible
36for extracting information from the blobs of data passed to the instantiation
37function. The first data parser that recognises the blob gets to set the
38subtype of the key and define the operations that can be done on that key.
39
40A data parser may interpret the data blob as containing the bits representing a
41key, or it may interpret it as a reference to a key held somewhere else in the
42system (for example, a TPM).
43
44
45==================
46KEY IDENTIFICATION
47==================
48
49If a key is added with an empty name, the instantiation data parsers are given
50the opportunity to pre-parse a key and to determine the description the key
51should be given from the content of the key.
52
53This can then be used to refer to the key, either by complete match or by
54partial match. The key type may also use other criteria to refer to a key.
55
56The asymmetric key type's match function can then perform a wider range of
57comparisons than just the straightforward comparison of the description with
58the criterion string:
59
60 (1) If the criterion string is of the form "id:<hexdigits>" then the match
61 function will examine a key's fingerprint to see if the hex digits given
62 after the "id:" match the tail. For instance:
63
64 keyctl search @s asymmetric id:5acc2142
65
66 will match a key with fingerprint:
67
68 1A00 2040 7601 7889 DE11 882C 3823 04AD 5ACC 2142
69
70 (2) If the criterion string is of the form "<subtype>:<hexdigits>" then the
71 match will match the ID as in (1), but with the added restriction that
72 only keys of the specified subtype (e.g. tpm) will be matched. For
73 instance:
74
75 keyctl search @s asymmetric tpm:5acc2142
76
77Looking in /proc/keys, the last 8 hex digits of the key fingerprint are
78displayed, along with the subtype:
79
b142f54e 80 1a39e171 I----- 1 perm 3f010000 0 0 asymmetric modsign.0: DSA 5acc2142 []
9a83b465
DH
81
82
83=========================
84ACCESSING ASYMMETRIC KEYS
85=========================
86
87For general access to asymmetric keys from within the kernel, the following
88inclusion is required:
89
90 #include <crypto/public_key.h>
91
92This gives access to functions for dealing with asymmetric / public keys.
93Three enums are defined there for representing public-key cryptography
94algorithms:
95
96 enum pkey_algo
97
98digest algorithms used by those:
99
100 enum pkey_hash_algo
101
102and key identifier representations:
103
104 enum pkey_id_type
105
106Note that the key type representation types are required because key
107identifiers from different standards aren't necessarily compatible. For
108instance, PGP generates key identifiers by hashing the key data plus some
109PGP-specific metadata, whereas X.509 has arbitrary certificate identifiers.
110
111The operations defined upon a key are:
112
113 (1) Signature verification.
114
115Other operations are possible (such as encryption) with the same key data
116required for verification, but not currently supported, and others
117(eg. decryption and signature generation) require extra key data.
118
119
120SIGNATURE VERIFICATION
121----------------------
122
123An operation is provided to perform cryptographic signature verification, using
124an asymmetric key to provide or to provide access to the public key.
125
126 int verify_signature(const struct key *key,
127 const struct public_key_signature *sig);
128
129The caller must have already obtained the key from some source and can then use
130it to check the signature. The caller must have parsed the signature and
131transferred the relevant bits to the structure pointed to by sig.
132
133 struct public_key_signature {
134 u8 *digest;
135 u8 digest_size;
136 enum pkey_hash_algo pkey_hash_algo : 8;
137 u8 nr_mpi;
138 union {
139 MPI mpi[2];
140 ...
141 };
142 };
143
144The algorithm used must be noted in sig->pkey_hash_algo, and all the MPIs that
145make up the actual signature must be stored in sig->mpi[] and the count of MPIs
146placed in sig->nr_mpi.
147
148In addition, the data must have been digested by the caller and the resulting
149hash must be pointed to by sig->digest and the size of the hash be placed in
150sig->digest_size.
151
152The function will return 0 upon success or -EKEYREJECTED if the signature
153doesn't match.
154
155The function may also return -ENOTSUPP if an unsupported public-key algorithm
156or public-key/hash algorithm combination is specified or the key doesn't
157support the operation; -EBADMSG or -ERANGE if some of the parameters have weird
158data; or -ENOMEM if an allocation can't be performed. -EINVAL can be returned
159if the key argument is the wrong type or is incompletely set up.
160
161
162=======================
163ASYMMETRIC KEY SUBTYPES
164=======================
165
166Asymmetric keys have a subtype that defines the set of operations that can be
167performed on that key and that determines what data is attached as the key
168payload. The payload format is entirely at the whim of the subtype.
169
170The subtype is selected by the key data parser and the parser must initialise
171the data required for it. The asymmetric key retains a reference on the
172subtype module.
173
174The subtype definition structure can be found in:
175
176 #include <keys/asymmetric-subtype.h>
177
178and looks like the following:
179
180 struct asymmetric_key_subtype {
181 struct module *owner;
182 const char *name;
183
184 void (*describe)(const struct key *key, struct seq_file *m);
185 void (*destroy)(void *payload);
5a307718
DH
186 int (*query)(const struct kernel_pkey_params *params,
187 struct kernel_pkey_query *info);
188 int (*eds_op)(struct kernel_pkey_params *params,
189 const void *in, void *out);
9a83b465
DH
190 int (*verify_signature)(const struct key *key,
191 const struct public_key_signature *sig);
192 };
193
146aa8b1 194Asymmetric keys point to this with their payload[asym_subtype] member.
9a83b465
DH
195
196The owner and name fields should be set to the owning module and the name of
197the subtype. Currently, the name is only used for print statements.
198
199There are a number of operations defined by the subtype:
200
201 (1) describe().
202
203 Mandatory. This allows the subtype to display something in /proc/keys
204 against the key. For instance the name of the public key algorithm type
205 could be displayed. The key type will display the tail of the key
206 identity string after this.
207
208 (2) destroy().
209
210 Mandatory. This should free the memory associated with the key. The
211 asymmetric key will look after freeing the fingerprint and releasing the
212 reference on the subtype module.
213
5a307718 214 (3) query().
9a83b465 215
5a307718
DH
216 Mandatory. This is a function for querying the capabilities of a key.
217
218 (4) eds_op().
219
220 Optional. This is the entry point for the encryption, decryption and
221 signature creation operations (which are distinguished by the operation ID
222 in the parameter struct). The subtype may do anything it likes to
223 implement an operation, including offloading to hardware.
224
225 (5) verify_signature().
226
227 Optional. This is the entry point for signature verification. The
228 subtype may do anything it likes to implement an operation, including
229 offloading to hardware.
9a83b465
DH
230
231
232==========================
233INSTANTIATION DATA PARSERS
234==========================
235
236The asymmetric key type doesn't generally want to store or to deal with a raw
237blob of data that holds the key data. It would have to parse it and error
238check it each time it wanted to use it. Further, the contents of the blob may
239have various checks that can be performed on it (eg. self-signatures, validity
240dates) and may contain useful data about the key (identifiers, capabilities).
241
242Also, the blob may represent a pointer to some hardware containing the key
243rather than the key itself.
244
245Examples of blob formats for which parsers could be implemented include:
246
247 - OpenPGP packet stream [RFC 4880].
248 - X.509 ASN.1 stream.
249 - Pointer to TPM key.
250 - Pointer to UEFI key.
3c58b236
DH
251 - PKCS#8 private key [RFC 5208].
252 - PKCS#5 encrypted private key [RFC 2898].
9a83b465
DH
253
254During key instantiation each parser in the list is tried until one doesn't
255return -EBADMSG.
256
257The parser definition structure can be found in:
258
259 #include <keys/asymmetric-parser.h>
260
261and looks like the following:
262
263 struct asymmetric_key_parser {
264 struct module *owner;
265 const char *name;
266
267 int (*parse)(struct key_preparsed_payload *prep);
268 };
269
270The owner and name fields should be set to the owning module and the name of
271the parser.
272
273There is currently only a single operation defined by the parser, and it is
274mandatory:
275
276 (1) parse().
277
278 This is called to preparse the key from the key creation and update paths.
279 In particular, it is called during the key creation _before_ a key is
280 allocated, and as such, is permitted to provide the key's description in
281 the case that the caller declines to do so.
282
283 The caller passes a pointer to the following struct with all of the fields
284 cleared, except for data, datalen and quotalen [see
b68101a1 285 Documentation/security/keys/core.rst].
9a83b465
DH
286
287 struct key_preparsed_payload {
288 char *description;
146aa8b1 289 void *payload[4];
9a83b465
DH
290 const void *data;
291 size_t datalen;
292 size_t quotalen;
293 };
294
295 The instantiation data is in a blob pointed to by data and is datalen in
296 size. The parse() function is not permitted to change these two values at
297 all, and shouldn't change any of the other values _unless_ they are
298 recognise the blob format and will not return -EBADMSG to indicate it is
299 not theirs.
300
301 If the parser is happy with the blob, it should propose a description for
146aa8b1
DH
302 the key and attach it to ->description, ->payload[asym_subtype] should be
303 set to point to the subtype to be used, ->payload[asym_crypto] should be
304 set to point to the initialised data for that subtype,
305 ->payload[asym_key_ids] should point to one or more hex fingerprints and
306 quotalen should be updated to indicate how much quota this key should
307 account for.
308
309 When clearing up, the data attached to ->payload[asym_key_ids] and
310 ->description will be kfree()'d and the data attached to
311 ->payload[asm_crypto] will be passed to the subtype's ->destroy() method
312 to be disposed of. A module reference for the subtype pointed to by
313 ->payload[asym_subtype] will be put.
9a83b465
DH
314
315
316 If the data format is not recognised, -EBADMSG should be returned. If it
317 is recognised, but the key cannot for some reason be set up, some other
318 negative error code should be returned. On success, 0 should be returned.
319
320 The key's fingerprint string may be partially matched upon. For a
321 public-key algorithm such as RSA and DSA this will likely be a printable
322 hex version of the key's fingerprint.
323
324Functions are provided to register and unregister parsers:
325
326 int register_asymmetric_key_parser(struct asymmetric_key_parser *parser);
327 void unregister_asymmetric_key_parser(struct asymmetric_key_parser *subtype);
328
329Parsers may not have the same name. The names are otherwise only used for
330displaying in debugging messages.
97d3aa0f
MM
331
332
333=========================
334KEYRING LINK RESTRICTIONS
335=========================
336
337Keyrings created from userspace using add_key can be configured to check the
7228b66a
MM
338signature of the key being linked. Keys without a valid signature are not
339allowed to link.
97d3aa0f
MM
340
341Several restriction methods are available:
342
343 (1) Restrict using the kernel builtin trusted keyring
344
345 - Option string used with KEYCTL_RESTRICT_KEYRING:
346 - "builtin_trusted"
347
7228b66a
MM
348 The kernel builtin trusted keyring will be searched for the signing key.
349 If the builtin trusted keyring is not configured, all links will be
350 rejected. The ca_keys kernel parameter also affects which keys are used
351 for signature verification.
97d3aa0f
MM
352
353 (2) Restrict using the kernel builtin and secondary trusted keyrings
354
355 - Option string used with KEYCTL_RESTRICT_KEYRING:
356 - "builtin_and_secondary_trusted"
357
358 The kernel builtin and secondary trusted keyrings will be searched for the
7228b66a
MM
359 signing key. If the secondary trusted keyring is not configured, this
360 restriction will behave like the "builtin_trusted" option. The ca_keys
361 kernel parameter also affects which keys are used for signature
362 verification.
97d3aa0f 363
7e3c4d22
MM
364 (3) Restrict using a separate key or keyring
365
366 - Option string used with KEYCTL_RESTRICT_KEYRING:
8e323a02 367 - "key_or_keyring:<key or keyring serial number>[:chain]"
7e3c4d22
MM
368
369 Whenever a key link is requested, the link will only succeed if the key
7228b66a 370 being linked is signed by one of the designated keys. This key may be
7e3c4d22
MM
371 specified directly by providing a serial number for one asymmetric key, or
372 a group of keys may be searched for the signing key by providing the
373 serial number for a keyring.
374
8e323a02
MM
375 When the "chain" option is provided at the end of the string, the keys
376 within the destination keyring will also be searched for signing keys.
377 This allows for verification of certificate chains by adding each
7228b66a
MM
378 certificate in order (starting closest to the root) to a keyring. For
379 instance, one keyring can be populated with links to a set of root
380 certificates, with a separate, restricted keyring set up for each
381 certificate chain to be validated:
382
383 # Create and populate a keyring for root certificates
384 root_id=`keyctl add keyring root-certs "" @s`
385 keyctl padd asymmetric "" $root_id < root1.cert
386 keyctl padd asymmetric "" $root_id < root2.cert
387
388 # Create and restrict a keyring for the certificate chain
389 chain_id=`keyctl add keyring chain "" @s`
390 keyctl restrict_keyring $chain_id asymmetric key_or_keyring:$root_id:chain
391
392 # Attempt to add each certificate in the chain, starting with the
393 # certificate closest to the root.
394 keyctl padd asymmetric "" $chain_id < intermediateA.cert
395 keyctl padd asymmetric "" $chain_id < intermediateB.cert
396 keyctl padd asymmetric "" $chain_id < end-entity.cert
397
398 If the final end-entity certificate is successfully added to the "chain"
399 keyring, we can be certain that it has a valid signing chain going back to
400 one of the root certificates.
401
402 A single keyring can be used to verify a chain of signatures by
403 restricting the keyring after linking the root certificate:
404
405 # Create a keyring for the certificate chain and add the root
406 chain2_id=`keyctl add keyring chain2 "" @s`
407 keyctl padd asymmetric "" $chain2_id < root1.cert
408
409 # Restrict the keyring that already has root1.cert linked. The cert
410 # will remain linked by the keyring.
411 keyctl restrict_keyring $chain2_id asymmetric key_or_keyring:0:chain
412
413 # Attempt to add each certificate in the chain, starting with the
414 # certificate closest to the root.
415 keyctl padd asymmetric "" $chain2_id < intermediateA.cert
416 keyctl padd asymmetric "" $chain2_id < intermediateB.cert
417 keyctl padd asymmetric "" $chain2_id < end-entity.cert
418
419 If the final end-entity certificate is successfully added to the "chain2"
420 keyring, we can be certain that there is a valid signing chain going back
421 to the root certificate that was added before the keyring was restricted.
422
8e323a02 423
97d3aa0f
MM
424In all of these cases, if the signing key is found the signature of the key to
425be linked will be verified using the signing key. The requested key is added
426to the keyring only if the signature is successfully verified. -ENOKEY is
427returned if the parent certificate could not be found, or -EKEYREJECTED is
428returned if the signature check fails or the key is blacklisted. Other errors
429may be returned if the signature check could not be performed.