aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Bottomley <James.Bottomley@HansenPartnership.com>2018-11-07 08:15:39 -0800
committerJames Bottomley <James.Bottomley@HansenPartnership.com>2018-11-12 20:57:05 -0800
commit5823c84636fa6fadbf13a675625fad80bcbf3e1a (patch)
tree829df3ddfb80e9ab10e69efdb1b5e797c3d1bf6c
parent1b6fde3fae5242fb5216afb2bd61c8d359b70d7a (diff)
downloadopenssl_tpm2_engine-5823c84636fa6fadbf13a675625fad80bcbf3e1a.tar.gz
create_tpm2_key: policy should use the name algorithm
For all keys, policy must use the same algorithm as the name algorithm, so fix that and add checks for different algorithms. This also fixes a bug in the PCR policy where we were accidentally getting 32 from the size of the PCR unmarshal selection rather than setting the correct PCR hash algorithm size Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
-rw-r--r--create_tpm2_key.c6
-rw-r--r--e_tpm2-ecc.c20
-rw-r--r--e_tpm2-rsa.c20
-rw-r--r--e_tpm2.c4
-rw-r--r--e_tpm2.h1
-rw-r--r--tpm2-common.c13
-rw-r--r--tpm2-common.h6
7 files changed, 45 insertions, 25 deletions
diff --git a/create_tpm2_key.c b/create_tpm2_key.c
index 3c71d50..2c7f3c1 100644
--- a/create_tpm2_key.c
+++ b/create_tpm2_key.c
@@ -791,7 +791,7 @@ int main(int argc, char **argv)
phandle = parent;
}
- digest.hashAlg = TPM_ALG_SHA256;
+ digest.hashAlg = name_alg;
sizeInBytes = TSS_GetDigestSize(digest.hashAlg);
memset((uint8_t *)&digest.digest, 0, sizeInBytes);
@@ -896,7 +896,7 @@ int main(int argc, char **argv)
/* use salted parameter encryption to hide the key */
rc = tpm2_get_session_handle(tssContext, &authHandle, phandle,
- TPM_SE_HMAC);
+ TPM_SE_HMAC, name_alg);
if (rc) {
reason = "get session handle";
goto out_flush;
@@ -960,7 +960,7 @@ int main(int argc, char **argv)
/* use salted parameter encryption to hide the key */
rc = tpm2_get_session_handle(tssContext, &authHandle, phandle,
- TPM_SE_HMAC);
+ TPM_SE_HMAC, name_alg);
if (rc) {
reason = "get session handle";
goto out_flush;
diff --git a/e_tpm2-ecc.c b/e_tpm2-ecc.c
index dcb951e..e0b0c47 100644
--- a/e_tpm2-ecc.c
+++ b/e_tpm2-ecc.c
@@ -66,7 +66,8 @@ static int ec_app_data = TPM2_ENGINE_EX_DATA_UNINIT;
static TPM_HANDLE tpm2_load_key_from_ecc(const EC_KEY *eck,
TSS_CONTEXT **tssContext, char **auth,
TPM_SE *sessionType, int *num_commands,
- struct policy_command **commands)
+ struct policy_command **commands,
+ TPM_ALG_ID *nameAlg)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000
/* const mess up in openssl 1.0.2 */
@@ -84,6 +85,7 @@ static TPM_HANDLE tpm2_load_key_from_ecc(const EC_KEY *eck,
TPM_SE_POLICY : TPM_SE_HMAC;
*commands = app_data->commands;
*num_commands = app_data->num_commands;
+ *nameAlg = app_data->name_alg;
return tpm2_load_key(tssContext, app_data);
}
@@ -136,6 +138,7 @@ static ECDSA_SIG *tpm2_ecdsa_sign(const unsigned char *dgst, int dgst_len,
BIGNUM *r, *s;
int num_commands;
struct policy_command *commands;
+ TPM_ALG_ID nameAlg;
/* The TPM insists on knowing the digest type, so
* calculate that from the size */
@@ -161,7 +164,7 @@ static ECDSA_SIG *tpm2_ecdsa_sign(const unsigned char *dgst, int dgst_len,
in.keyHandle = tpm2_load_key_from_ecc(eck, &tssContext, &auth,
&sessionType, &num_commands,
- &commands);
+ &commands, &nameAlg);
if (in.keyHandle == 0) {
fprintf(stderr, "Failed to get Key Handle in TPM EC key routines\n");
return NULL;
@@ -175,13 +178,14 @@ static ECDSA_SIG *tpm2_ecdsa_sign(const unsigned char *dgst, int dgst_len,
in.validation.digest.t.size = 0;
sig = NULL;
- rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType);
+ rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType,
+ nameAlg);
if (rc)
goto out;
if (sessionType == TPM_SE_POLICY) {
rc = tpm2_init_session(tssContext, authHandle,
- num_commands, commands);
+ num_commands, commands, nameAlg);
if (rc)
goto out;
}
@@ -237,6 +241,7 @@ static int tpm2_ecc_compute_key(unsigned char **psec, size_t *pseclen,
unsigned char point[MAX_ECC_KEY_BYTES*2 + 1];
int num_commands;
struct policy_command *commands;
+ TPM_ALG_ID nameAlg;
int ret;
group = EC_KEY_get0_group(eck);
@@ -253,7 +258,7 @@ static int tpm2_ecc_compute_key(unsigned char **psec, size_t *pseclen,
in.keyHandle = tpm2_load_key_from_ecc(eck, &tssContext, &auth,
&sessionType, &num_commands,
- &commands);
+ &commands, &nameAlg);
if (in.keyHandle == 0) {
fprintf(stderr, "Failed to get Key Handle in TPM EC key routines\n");
return 0;
@@ -264,13 +269,14 @@ static int tpm2_ecc_compute_key(unsigned char **psec, size_t *pseclen,
in.inPoint.point.y.t.size = len;
ret = 0;
- rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType);
+ rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType,
+ nameAlg);
if (rc)
goto out;
if (sessionType == TPM_SE_POLICY) {
rc = tpm2_init_session(tssContext, authHandle,
- num_commands, commands);
+ num_commands, commands, nameAlg);
if (rc)
goto out;
}
diff --git a/e_tpm2-rsa.c b/e_tpm2-rsa.c
index 35b865b..3be6302 100644
--- a/e_tpm2-rsa.c
+++ b/e_tpm2-rsa.c
@@ -104,7 +104,8 @@ static int tpm2_rsa_pub_enc(int flen,
static TPM_HANDLE tpm2_load_key_from_rsa(RSA *rsa, TSS_CONTEXT **tssContext,
char **auth, TPM_SE *sessionType,
int *num_commands,
- struct policy_command **commands)
+ struct policy_command **commands,
+ TPM_ALG_ID *nameAlg)
{
struct app_data *app_data = RSA_get_ex_data(rsa, ex_app_data);
@@ -116,6 +117,7 @@ static TPM_HANDLE tpm2_load_key_from_rsa(RSA *rsa, TSS_CONTEXT **tssContext,
TPM_SE_POLICY : TPM_SE_HMAC;
*commands = app_data->commands;
*num_commands = app_data->num_commands;
+ *nameAlg = app_data->name_alg;
return tpm2_load_key(tssContext, app_data);
}
@@ -165,10 +167,11 @@ static int tpm2_rsa_priv_dec(int flen,
TPM_SE sessionType;
int num_commands;
struct policy_command *commands;
+ TPM_ALG_ID nameAlg;
in.keyHandle = tpm2_load_key_from_rsa(rsa, &tssContext, &auth,
&sessionType, &num_commands,
- &commands);
+ &commands, &nameAlg);
if (in.keyHandle == 0) {
fprintf(stderr, "Failed to get Key Handle in TPM RSA key routines\n");
@@ -187,13 +190,14 @@ static int tpm2_rsa_priv_dec(int flen,
memcpy(in.cipherText.t.buffer, from, flen);
in.label.t.size = 0;
- rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType);
+ rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType,
+ nameAlg);
if (rc)
goto out;
if (sessionType == TPM_SE_POLICY) {
rc = tpm2_init_session(tssContext, authHandle,
- num_commands, commands);
+ num_commands, commands, nameAlg);
if (rc)
goto out;
}
@@ -237,6 +241,7 @@ static int tpm2_rsa_priv_enc(int flen,
TPM_SE sessionType;
int num_commands;
struct policy_command *commands;
+ TPM_ALG_ID nameAlg;
if (padding != RSA_PKCS1_PADDING) {
fprintf(stderr, "Non PKCS1 padding asked for\n");
@@ -245,7 +250,7 @@ static int tpm2_rsa_priv_enc(int flen,
in.keyHandle = tpm2_load_key_from_rsa(rsa, &tssContext, &auth,
&sessionType, &num_commands,
- &commands);
+ &commands, &nameAlg);
if (in.keyHandle == 0) {
fprintf(stderr, "Failed to get Key Handle in TPM RSA routines\n");
@@ -254,13 +259,14 @@ static int tpm2_rsa_priv_enc(int flen,
}
rv = -1;
- rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType);
+ rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType,
+ nameAlg);
if (rc)
goto out;
if (sessionType == TPM_SE_POLICY) {
rc = tpm2_init_session(tssContext, authHandle,
- num_commands, commands);
+ num_commands, commands, nameAlg);
if (rc)
goto out;
}
diff --git a/e_tpm2.c b/e_tpm2.c
index 833dc7c..39026e9 100644
--- a/e_tpm2.c
+++ b/e_tpm2.c
@@ -218,6 +218,7 @@ static int tpm2_engine_load_nvkey(ENGINE *e, EVP_PKEY **ppkey,
rc = tpm2_readpublic(tssContext, key, &p);
if (rc)
goto err_del;
+ app_data->name_alg = p.nameAlg;
pkey = tpm2_to_openssl_public(&p);
if (!pkey) {
fprintf(stderr, "Failed to allocate a new EVP_KEY\n");
@@ -448,6 +449,7 @@ static int tpm2_engine_load_key_core(ENGINE *e, EVP_PKEY **ppkey,
buffer = app_data->pub;
size = app_data->pub_len;
TPM2B_PUBLIC_Unmarshal(&p, &buffer, &size, FALSE);
+ app_data->name_alg = p.publicArea.nameAlg;
/* create the new objects to return */
pkey = tpm2_to_openssl_public(&p.publicArea);
if (!pkey) {
@@ -578,7 +580,7 @@ TPM_HANDLE tpm2_load_key(TSS_CONTEXT **tsscp, struct app_data *app_data)
goto out;
}
rc = tpm2_get_session_handle(tssContext, &session, in.parentHandle,
- TPM_SE_HMAC);
+ TPM_SE_HMAC, app_data->name_alg);
if (rc)
goto out_flush_srk;
rc = TSS_Execute(tssContext,
diff --git a/e_tpm2.h b/e_tpm2.h
index 253231f..5e843d2 100644
--- a/e_tpm2.h
+++ b/e_tpm2.h
@@ -21,6 +21,7 @@ struct app_data {
const char *dir;
int req_policy_session;
int num_commands;
+ unsigned int name_alg;
struct policy_command *commands;
};
diff --git a/tpm2-common.c b/tpm2-common.c
index 6aef4e3..1b1eead 100644
--- a/tpm2-common.c
+++ b/tpm2-common.c
@@ -570,7 +570,8 @@ TPM_RC tpm2_get_bound_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
}
TPM_RC tpm2_get_session_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
- TPM_HANDLE salt_key, TPM_SE sessionType)
+ TPM_HANDLE salt_key, TPM_SE sessionType,
+ TPM_ALG_ID name_alg)
{
TPM_RC rc;
StartAuthSession_In in;
@@ -581,7 +582,7 @@ TPM_RC tpm2_get_session_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
memset(&extra, 0 , sizeof(extra));
in.bind = TPM_RH_NULL;
in.sessionType = sessionType;
- in.authHash = TPM_ALG_SHA256;
+ in.authHash = name_alg;
in.tpmKey = TPM_RH_NULL;
in.symmetric.algorithm = TPM_ALG_AES;
in.symmetric.keyBits.aes = 128;
@@ -614,7 +615,8 @@ TPM_RC tpm2_get_session_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
}
TPM_RC tpm2_init_session(TSS_CONTEXT *tssContext, TPM_HANDLE handle,
- int num_commands, struct policy_command *commands)
+ int num_commands, struct policy_command *commands,
+ TPM_ALG_ID name_alg)
{
INT32 size;
BYTE *policy;
@@ -622,6 +624,7 @@ TPM_RC tpm2_init_session(TSS_CONTEXT *tssContext, TPM_HANDLE handle,
COMMAND_PARAMETERS in;
int i;
char reason[256];
+ int name_alg_size = TSS_GetDigestSize(name_alg);
reason[0] = '\0';
/* pick a random policy type: they all have the handle first */
@@ -637,9 +640,9 @@ TPM_RC tpm2_init_session(TSS_CONTEXT *tssContext, TPM_HANDLE handle,
rc = TPML_PCR_SELECTION_Unmarshal(
&ppcrin->pcrs, &policy, &size);
- ppcrin->pcrDigest.b.size = size;
+ ppcrin->pcrDigest.b.size = name_alg_size;
memcpy(ppcrin->pcrDigest.b.buffer,
- policy, size);
+ policy, name_alg_size);
sprintf(reason, "PCR Mismatch");
reason_rc = TPM_RC_VALUE;
diff --git a/tpm2-common.h b/tpm2-common.h
index f442c94..14aae40 100644
--- a/tpm2-common.h
+++ b/tpm2-common.h
@@ -16,9 +16,11 @@ void tpm2_flush_handle(TSS_CONTEXT *tssContext, TPM_HANDLE h);
EVP_PKEY *tpm2_to_openssl_public(TPMT_PUBLIC *pub);
void tpm2_flush_srk(TSS_CONTEXT *tssContext, TPM_HANDLE hSRK);
TPM_RC tpm2_get_session_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
- TPM_HANDLE salt_key, TPM_SE sessionType);
+ TPM_HANDLE salt_key, TPM_SE sessionType,
+ TPM_ALG_ID name_alg);
TPM_RC tpm2_init_session(TSS_CONTEXT *tssContext, TPM_HANDLE handle,
- int num_commands, struct policy_command *commands);
+ int num_commands, struct policy_command *commands,
+ TPM_ALG_ID name_alg);
TPM_RC tpm2_get_bound_handle(TSS_CONTEXT *tssContext, TPM_HANDLE *handle,
TPM_HANDLE bind, const char *auth);
TPM_RC tpm2_SensitiveToDuplicate(TPMT_SENSITIVE *s,