Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #define pr_fmt(fmt) "TPM-PARSER: "fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <keys/asymmetric-subtype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <keys/asymmetric-parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <crypto/asym_tpm_subtype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "tpm.asn1.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct tpm_parse_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	const void	*blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	u32		blob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Note the key data of the ASN.1 blob.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) int tpm_note_key(void *context, size_t hdrlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		   unsigned char tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		   const void *value, size_t vlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct tpm_parse_context *ctx = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	ctx->blob = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	ctx->blob_len = vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Parse a TPM-encrypted private key blob.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static struct tpm_key *tpm_parse(const void *data, size_t datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct tpm_parse_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	memset(&ctx, 0, sizeof(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	/* Attempt to decode the private key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	ret = asn1_ber_decoder(&tpm_decoder, &ctx, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return tpm_key_create(ctx.blob, ctx.blob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Attempt to parse a data blob for a key as a TPM private key blob.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int tpm_key_preparse(struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct tpm_key *tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 * TPM 1.2 keys are max 2048 bits long, so assume the blob is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * more than 4x that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (prep->datalen > 256 * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	tk = tpm_parse(prep->data, prep->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (IS_ERR(tk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return PTR_ERR(tk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* We're pinning the module by being linked against it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	__module_get(asym_tpm_subtype.owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	prep->payload.data[asym_subtype] = &asym_tpm_subtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	prep->payload.data[asym_key_ids] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	prep->payload.data[asym_crypto] = tk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	prep->payload.data[asym_auth] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	prep->quotalen = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static struct asymmetric_key_parser tpm_key_parser = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	.name	= "tpm_parser",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	.parse	= tpm_key_preparse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int __init tpm_key_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return register_asymmetric_key_parser(&tpm_key_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void __exit tpm_key_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	unregister_asymmetric_key_parser(&tpm_key_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) module_init(tpm_key_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) module_exit(tpm_key_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) MODULE_DESCRIPTION("TPM private key-blob parser");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) MODULE_LICENSE("GPL v2");