Path: blob/master/security/keys/trusted-keys/trusted_tpm2.c
50904 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2004 IBM Corporation3* Copyright (C) 2014 Intel Corporation4*/56#include <linux/asn1_encoder.h>7#include <linux/oid_registry.h>8#include <linux/string.h>9#include <linux/err.h>10#include <linux/tpm.h>11#include <linux/tpm_command.h>1213#include <keys/trusted-type.h>14#include <keys/trusted_tpm.h>1516#include <linux/unaligned.h>1718#include "tpm2key.asn1.h"1920static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };2122static int tpm2_key_encode(struct trusted_key_payload *payload,23struct trusted_key_options *options,24u8 *src, u32 len)25{26const int SCRATCH_SIZE = PAGE_SIZE;27u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);28u8 *work = scratch, *work1;29u8 *end_work = scratch + SCRATCH_SIZE;30u8 *priv, *pub;31u16 priv_len, pub_len;32int ret;3334priv_len = get_unaligned_be16(src) + 2;35priv = src;3637src += priv_len;3839pub_len = get_unaligned_be16(src) + 2;40pub = src;4142if (!scratch)43return -ENOMEM;4445work = asn1_encode_oid(work, end_work, tpm2key_oid,46asn1_oid_len(tpm2key_oid));4748if (options->blobauth_len == 0) {49unsigned char bool[3], *w = bool;50/* tag 0 is emptyAuth */51w = asn1_encode_boolean(w, w + sizeof(bool), true);52if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {53ret = PTR_ERR(w);54goto err;55}56work = asn1_encode_tag(work, end_work, 0, bool, w - bool);57}5859/*60* Assume both octet strings will encode to a 2 byte definite length61*62* Note: For a well behaved TPM, this warning should never63* trigger, so if it does there's something nefarious going on64*/65if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,66"BUG: scratch buffer is too small")) {67ret = -EINVAL;68goto err;69}7071work = asn1_encode_integer(work, end_work, options->keyhandle);72work = asn1_encode_octet_string(work, end_work, pub, pub_len);73work = asn1_encode_octet_string(work, end_work, priv, priv_len);7475work1 = payload->blob;76work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),77scratch, work - scratch);78if (IS_ERR(work1)) {79ret = PTR_ERR(work1);80pr_err("BUG: ASN.1 encoder failed with %d\n", ret);81goto err;82}8384kfree(scratch);85return work1 - payload->blob;8687err:88kfree(scratch);89return ret;90}9192struct tpm2_key_context {93u32 parent;94const u8 *pub;95u32 pub_len;96const u8 *priv;97u32 priv_len;98};99100static int tpm2_key_decode(struct trusted_key_payload *payload,101struct trusted_key_options *options,102u8 **buf)103{104int ret;105struct tpm2_key_context ctx;106u8 *blob;107108memset(&ctx, 0, sizeof(ctx));109110ret = asn1_ber_decoder(&tpm2key_decoder, &ctx, payload->blob,111payload->blob_len);112if (ret < 0)113return ret;114115if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)116return -EINVAL;117118blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);119if (!blob)120return -ENOMEM;121122*buf = blob;123options->keyhandle = ctx.parent;124125memcpy(blob, ctx.priv, ctx.priv_len);126blob += ctx.priv_len;127128memcpy(blob, ctx.pub, ctx.pub_len);129130return 0;131}132133int tpm2_key_parent(void *context, size_t hdrlen,134unsigned char tag,135const void *value, size_t vlen)136{137struct tpm2_key_context *ctx = context;138const u8 *v = value;139int i;140141ctx->parent = 0;142for (i = 0; i < vlen; i++) {143ctx->parent <<= 8;144ctx->parent |= v[i];145}146147return 0;148}149150int tpm2_key_type(void *context, size_t hdrlen,151unsigned char tag,152const void *value, size_t vlen)153{154enum OID oid = look_up_OID(value, vlen);155156if (oid != OID_TPMSealedData) {157char buffer[50];158159sprint_oid(value, vlen, buffer, sizeof(buffer));160pr_debug("OID is \"%s\" which is not TPMSealedData\n",161buffer);162return -EINVAL;163}164165return 0;166}167168int tpm2_key_pub(void *context, size_t hdrlen,169unsigned char tag,170const void *value, size_t vlen)171{172struct tpm2_key_context *ctx = context;173174ctx->pub = value;175ctx->pub_len = vlen;176177return 0;178}179180int tpm2_key_priv(void *context, size_t hdrlen,181unsigned char tag,182const void *value, size_t vlen)183{184struct tpm2_key_context *ctx = context;185186ctx->priv = value;187ctx->priv_len = vlen;188189return 0;190}191192/**193* tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.194*195* @buf: an allocated tpm_buf instance196* @session_handle: session handle197* @nonce: the session nonce, may be NULL if not used198* @nonce_len: the session nonce length, may be 0 if not used199* @attributes: the session attributes200* @hmac: the session HMAC or password, may be NULL if not used201* @hmac_len: the session HMAC or password length, maybe 0 if not used202*/203static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,204const u8 *nonce, u16 nonce_len,205u8 attributes,206const u8 *hmac, u16 hmac_len)207{208tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);209tpm_buf_append_u32(buf, session_handle);210tpm_buf_append_u16(buf, nonce_len);211212if (nonce && nonce_len)213tpm_buf_append(buf, nonce, nonce_len);214215tpm_buf_append_u8(buf, attributes);216tpm_buf_append_u16(buf, hmac_len);217218if (hmac && hmac_len)219tpm_buf_append(buf, hmac, hmac_len);220}221222/**223* tpm2_seal_trusted() - seal the payload of a trusted key224*225* @chip: TPM chip to use226* @payload: the key data in clear and encrypted form227* @options: authentication values and other options228*229* Return: < 0 on error and 0 on success.230*/231int tpm2_seal_trusted(struct tpm_chip *chip,232struct trusted_key_payload *payload,233struct trusted_key_options *options)234{235off_t offset = TPM_HEADER_SIZE;236struct tpm_buf buf, sized;237int blob_len = 0;238int hash;239u32 flags;240int rc;241242hash = tpm2_find_hash_alg(options->hash);243if (hash < 0)244return hash;245246if (!options->keyhandle)247return -EINVAL;248249rc = tpm_try_get_ops(chip);250if (rc)251return rc;252253rc = tpm2_start_auth_session(chip);254if (rc)255goto out_put;256257rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);258if (rc) {259tpm2_end_auth_session(chip);260goto out_put;261}262263rc = tpm_buf_init_sized(&sized);264if (rc) {265tpm_buf_destroy(&buf);266tpm2_end_auth_session(chip);267goto out_put;268}269270rc = tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);271if (rc)272goto out;273274tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_DECRYPT,275options->keyauth, TPM_DIGEST_SIZE);276277/* sensitive */278tpm_buf_append_u16(&sized, options->blobauth_len);279280if (options->blobauth_len)281tpm_buf_append(&sized, options->blobauth, options->blobauth_len);282283tpm_buf_append_u16(&sized, payload->key_len);284tpm_buf_append(&sized, payload->key, payload->key_len);285tpm_buf_append(&buf, sized.data, sized.length);286287/* public */288tpm_buf_reset_sized(&sized);289tpm_buf_append_u16(&sized, TPM_ALG_KEYEDHASH);290tpm_buf_append_u16(&sized, hash);291292/* key properties */293flags = 0;294flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;295flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);296tpm_buf_append_u32(&sized, flags);297298/* policy */299tpm_buf_append_u16(&sized, options->policydigest_len);300if (options->policydigest_len)301tpm_buf_append(&sized, options->policydigest, options->policydigest_len);302303/* public parameters */304tpm_buf_append_u16(&sized, TPM_ALG_NULL);305tpm_buf_append_u16(&sized, 0);306307tpm_buf_append(&buf, sized.data, sized.length);308309/* outside info */310tpm_buf_append_u16(&buf, 0);311312/* creation PCR */313tpm_buf_append_u32(&buf, 0);314315if (buf.flags & TPM_BUF_OVERFLOW) {316rc = -E2BIG;317tpm2_end_auth_session(chip);318goto out;319}320321rc = tpm_buf_fill_hmac_session(chip, &buf);322if (rc)323goto out;324325rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");326rc = tpm_buf_check_hmac_response(chip, &buf, rc);327if (rc)328goto out;329330blob_len = tpm_buf_read_u32(&buf, &offset);331if (blob_len > MAX_BLOB_SIZE || buf.flags & TPM_BUF_BOUNDARY_ERROR) {332rc = -E2BIG;333goto out;334}335if (buf.length - offset < blob_len) {336rc = -EFAULT;337goto out;338}339340blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);341if (blob_len < 0)342rc = blob_len;343344out:345tpm_buf_destroy(&sized);346tpm_buf_destroy(&buf);347348if (!rc)349payload->blob_len = blob_len;350351out_put:352tpm_put_ops(chip);353return tpm_ret_to_err(rc);354}355356/**357* tpm2_load_cmd() - execute a TPM2_Load command358*359* @chip: TPM chip to use360* @payload: the key data in clear and encrypted form361* @options: authentication values and other options362* @blob_handle: returned blob handle363*364* Return: 0 on success.365* -E2BIG on wrong payload size.366* -EPERM on tpm error status.367* < 0 error from tpm_send.368*/369static int tpm2_load_cmd(struct tpm_chip *chip,370struct trusted_key_payload *payload,371struct trusted_key_options *options,372u32 *blob_handle)373{374u8 *blob_ref __free(kfree) = NULL;375struct tpm_buf buf;376unsigned int private_len;377unsigned int public_len;378unsigned int blob_len;379u8 *blob, *pub;380int rc;381u32 attrs;382383rc = tpm2_key_decode(payload, options, &blob);384if (rc) {385/* old form */386blob = payload->blob;387payload->old_format = 1;388} else {389/* Bind for cleanup: */390blob_ref = blob;391}392393/* new format carries keyhandle but old format doesn't */394if (!options->keyhandle)395return -EINVAL;396397/* must be big enough for at least the two be16 size counts */398if (payload->blob_len < 4)399return -EINVAL;400401private_len = get_unaligned_be16(blob);402403/* must be big enough for following public_len */404if (private_len + 2 + 2 > (payload->blob_len))405return -E2BIG;406407public_len = get_unaligned_be16(blob + 2 + private_len);408if (private_len + 2 + public_len + 2 > payload->blob_len)409return -E2BIG;410411pub = blob + 2 + private_len + 2;412/* key attributes are always at offset 4 */413attrs = get_unaligned_be32(pub + 4);414415if ((attrs & (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT)) ==416(TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT))417payload->migratable = 0;418else419payload->migratable = 1;420421blob_len = private_len + public_len + 4;422if (blob_len > payload->blob_len)423return -E2BIG;424425rc = tpm2_start_auth_session(chip);426if (rc)427return rc;428429rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);430if (rc) {431tpm2_end_auth_session(chip);432return rc;433}434435rc = tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);436if (rc)437goto out;438439tpm_buf_append_hmac_session(chip, &buf, 0, options->keyauth,440TPM_DIGEST_SIZE);441442tpm_buf_append(&buf, blob, blob_len);443444if (buf.flags & TPM_BUF_OVERFLOW) {445rc = -E2BIG;446tpm2_end_auth_session(chip);447goto out;448}449450rc = tpm_buf_fill_hmac_session(chip, &buf);451if (rc)452goto out;453454rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");455rc = tpm_buf_check_hmac_response(chip, &buf, rc);456if (!rc)457*blob_handle = be32_to_cpup(458(__be32 *) &buf.data[TPM_HEADER_SIZE]);459460out:461tpm_buf_destroy(&buf);462463return tpm_ret_to_err(rc);464}465466/**467* tpm2_unseal_cmd() - execute a TPM2_Unseal command468*469* @chip: TPM chip to use470* @payload: the key data in clear and encrypted form471* @options: authentication values and other options472* @blob_handle: blob handle473*474* Return: 0 on success475* -EPERM on tpm error status476* < 0 error from tpm_send477*/478static int tpm2_unseal_cmd(struct tpm_chip *chip,479struct trusted_key_payload *payload,480struct trusted_key_options *options,481u32 blob_handle)482{483struct tpm_header *head;484struct tpm_buf buf;485u16 data_len;486int offset;487u8 *data;488int rc;489490rc = tpm2_start_auth_session(chip);491if (rc)492return rc;493494rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);495if (rc) {496tpm2_end_auth_session(chip);497return rc;498}499500rc = tpm_buf_append_name(chip, &buf, blob_handle, NULL);501if (rc)502goto out;503504if (!options->policyhandle) {505tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT,506options->blobauth,507options->blobauth_len);508} else {509/*510* FIXME: The policy session was generated outside the511* kernel so we don't known the nonce and thus can't512* calculate a HMAC on it. Therefore, the user can513* only really use TPM2_PolicyPassword and we must514* send down the plain text password, which could be515* intercepted. We can still encrypt the returned516* key, but that's small comfort since the interposer517* could repeat our actions with the exfiltrated518* password.519*/520tpm2_buf_append_auth(&buf, options->policyhandle,521NULL /* nonce */, 0, 0,522options->blobauth, options->blobauth_len);523if (tpm2_chip_auth(chip)) {524tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT, NULL, 0);525} else {526offset = buf.handles * 4 + TPM_HEADER_SIZE;527head = (struct tpm_header *)buf.data;528if (tpm_buf_length(&buf) == offset)529head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);530}531}532533rc = tpm_buf_fill_hmac_session(chip, &buf);534if (rc)535goto out;536537rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");538rc = tpm_buf_check_hmac_response(chip, &buf, rc);539540if (!rc) {541data_len = be16_to_cpup(542(__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);543if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE) {544rc = -EFAULT;545goto out;546}547548if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {549rc = -EFAULT;550goto out;551}552data = &buf.data[TPM_HEADER_SIZE + 6];553554if (payload->old_format) {555/* migratable flag is at the end of the key */556memcpy(payload->key, data, data_len - 1);557payload->key_len = data_len - 1;558payload->migratable = data[data_len - 1];559} else {560/*561* migratable flag already collected from key562* attributes563*/564memcpy(payload->key, data, data_len);565payload->key_len = data_len;566}567}568569out:570tpm_buf_destroy(&buf);571return tpm_ret_to_err(rc);572}573574/**575* tpm2_unseal_trusted() - unseal the payload of a trusted key576*577* @chip: TPM chip to use578* @payload: the key data in clear and encrypted form579* @options: authentication values and other options580*581* Return: Same as with tpm_send.582*/583int tpm2_unseal_trusted(struct tpm_chip *chip,584struct trusted_key_payload *payload,585struct trusted_key_options *options)586{587u32 blob_handle;588int rc;589590rc = tpm_try_get_ops(chip);591if (rc)592return rc;593594rc = tpm2_load_cmd(chip, payload, options, &blob_handle);595if (rc)596goto out;597598rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);599tpm2_flush_context(chip, blob_handle);600601out:602tpm_put_ops(chip);603return tpm_ret_to_err(rc);604}605606607