1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "TPM-PARSER: "fmt
3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/export.h>
6 #include <linux/slab.h>
8 #include <keys/asymmetric-subtype.h>
9 #include <keys/asymmetric-parser.h>
10 #include <crypto/asym_tpm_subtype.h>
13 struct tpm_parse_context {
19 * Note the key data of the ASN.1 blob.
21 int tpm_note_key(void *context, size_t hdrlen,
23 const void *value, size_t vlen)
25 struct tpm_parse_context *ctx = context;
34 * Parse a TPM-encrypted private key blob.
36 static struct tpm_key *tpm_parse(const void *data, size_t datalen)
38 struct tpm_parse_context ctx;
41 memset(&ctx, 0, sizeof(ctx));
43 /* Attempt to decode the private key */
44 ret = asn1_ber_decoder(&tpm_decoder, &ctx, data, datalen);
48 return tpm_key_create(ctx.blob, ctx.blob_len);
54 * Attempt to parse a data blob for a key as a TPM private key blob.
56 static int tpm_key_preparse(struct key_preparsed_payload *prep)
61 * TPM 1.2 keys are max 2048 bits long, so assume the blob is no
64 if (prep->datalen > 256 * 4)
67 tk = tpm_parse(prep->data, prep->datalen);
72 /* We're pinning the module by being linked against it */
73 __module_get(asym_tpm_subtype.owner);
74 prep->payload.data[asym_subtype] = &asym_tpm_subtype;
75 prep->payload.data[asym_key_ids] = NULL;
76 prep->payload.data[asym_crypto] = tk;
77 prep->payload.data[asym_auth] = NULL;
82 static struct asymmetric_key_parser tpm_key_parser = {
85 .parse = tpm_key_preparse,
88 static int __init tpm_key_init(void)
90 return register_asymmetric_key_parser(&tpm_key_parser);
93 static void __exit tpm_key_exit(void)
95 unregister_asymmetric_key_parser(&tpm_key_parser);
98 module_init(tpm_key_init);
99 module_exit(tpm_key_exit);
101 MODULE_DESCRIPTION("TPM private key-blob parser");
102 MODULE_LICENSE("GPL v2");