Skip to contentSkip to conversation

Conflicting TPM2 engines and storage formats

tpm2 David Woodhouse 2 participants 5 messages Oct 2, 2018 – Oct 4, 2018 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

David Woodhouse

author
#1 lore raw

OK.. I think this makes a difference:

src/tpm2-tss-engine-common.h +1 -2
@@ -55,8 +55,7 @@ TSS2_RC init_tpm_key(ESYS_CONTEXT **ctx, ESYS_TR *keyHandle,
         .objectAttributes = (TPMA_OBJECT_USERWITHAUTH | \
                              TPMA_OBJECT_RESTRICTED | \
                              TPMA_OBJECT_DECRYPT | \
-                             TPMA_OBJECT_FIXEDTPM | \
-                             TPMA_OBJECT_FIXEDPARENT | \
+                             TPMA_OBJECT_NODA | \
                              TPMA_OBJECT_SENSITIVEDATAORIGIN), \
         .authPolicy = { \
              .size = 0, \

Now, I have two PEM files, one 'machine.tss' created with James's tpm2 engine, and a second 'tpm2-engine.tss' which is converted manually as described in issue #11 to be used with the tpm2tss engine.

$ openssl rsautl -sign -engine tpm2tss -inkey tpm2-engine.tss -keyform engine < testfile > testsig.tpm2tss
$ openssl rsautl -sign -engine tpm2 -inkey machine.tss -keyform engine < testfile > testsig.tpm2
$ sha1sum testsig.tpm*
fb9e8c1f4a1872f4144fd760af1740af75a9a8b2  testsig.tpm2
fb9e8c1f4a1872f4144fd760af1740af75a9a8b2  testsig.tpm2tss

So... that now *is* working for signing. However, while the corresponding 'rsautl -verify' works fine with James's tpm2 engine, it fails with tpm2tss:

$ openssl rsautl -verify -engine tpm2tss -inkey ../openssl_tpm2_engine/tpm2-engine.tss -keyform engine -in testsig.tpm2tss
Initializing
engine "tpm2tss" set.
Loading private key ../openssl_tpm2_engine/tpm2-engine.tss
get_auth called for object user key with ui_method 0x55ced253fdc0
Enter password for user key:
password is 
Loaded key uses alg-id 1
Creating RSA key object.
Created RSA key object.
TPM2 Key loaded
RSA operation error
140407559393728:error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01:../crypto/rsa/rsa_pk1.c:75:
140407559393728:error:04067072:rsa routines:rsa_ossl_public_decrypt:padding check failed:../crypto/rsa/rsa_ossl.c:586:

So... is it the public key that I've misconverted somehow?

 $ openssl asn1parse -in machine.tss 
    0:d=0  hl=4 l= 503 cons: SEQUENCE          
    4:d=1  hl=2 l=   5 prim: OBJECT            :2.23.133.10.2
   11:d=1  hl=2 l=   3 cons: cont [ 0 ]        
   13:d=2  hl=2 l=   1 prim: BOOLEAN           :1
   16:d=1  hl=2 l=   6 cons: cont [ 1 ]        
   18:d=2  hl=2 l=   4 prim: INTEGER           :40000001
   24:d=1  hl=4 l= 284 cons: cont [ 2 ]        
   28:d=2  hl=4 l= 280 prim: OCTET STRING      [HEX DUMP]:01160001000B000204400000001000100800000000000100C696B23A22C1516E22352CD1309A48BD7E6F054EE10BB7265BFC3BC85B622E75B658A03E15BB74CD9C542C4CEA22337467913058B3711E6EF6CB4C8DA9B496E5888346B058426D645398613BF9BCE907947E63B5B4CD4ABA1FFC2EA396BEC0A7F3F1FEDF022966CED43C070B2000BEEEE7BB82375A4E1C1558E7FC7139DCEE9C6055C3332D50572E14A0747B907440598C93B77F356689EE71ACDAECA9C1FF33C74106CD8C23ACA4613F4D234A4C10A1D7AA3E469493A541838D40A065B17FD23F7D75A5F9BD29F59566BB7D450E970269FD90F914FFF0CE44D526D4914C32F35AD9A2C6BAF7F77BAAA0B4B77F80C29C683764BAFADEAFC397D344DB2ABF996D
  312:d=1  hl=3 l= 192 prim: OCTET STRING      [HEX DUMP]:00BE00204463515A27FC3B773B53F28C04C147D78991B02E7B2FAFE0E96FA11E79692FCA0010DE02ADCCECCADBA51D1C71283A37FFD608D0979B994054031FF7475505EBC0D45CB6808A93D83C64051CA06043721D55D8FE514BDD997CB8B26199B62D2E8517C1DE5293AC6ED97EF278F648B072855B73B2D32CB2B39315FA40FA6AAF618038D6F880465083A1140DF9792448F6D888C912A797EE943912E0EC78B9576748822580ABECE5D02B973DAF1C0F7CE15DC56D3BB15E9AE7FADDB7C2
 $ tail -8 tpm2-engine.tss | openssl asn1parse
    0:d=0  hl=4 l= 280 prim: OCTET STRING      [HEX DUMP]:01160001000B000204400000001000100800000000000100C696B23A22C1516E22352CD1309A48BD7E6F054EE10BB7265BFC3BC85B622E75B658A03E15BB74CD9C542C4CEA22337467913058B3711E6EF6CB4C8DA9B496E5888346B058426D645398613BF9BCE907947E63B5B4CD4ABA1FFC2EA396BEC0A7F3F1FEDF022966CED43C070B2000BEEEE7BB82375A4E1C1558E7FC7139DCEE9C6055C3332D50572E14A0747B907440598C93B77F356689EE71ACDAECA9C1FF33C74106CD8C23ACA4613F4D234A4C10A1D7AA3E469493A541838D40A065B17FD23F7D75A5F9BD29F59566BB7D450E970269FD90F914FFF0CE44D526D4914C32F35AD9A2C6BAF7F77BAAA0B4B77F80C29C683764BAFADEAFC397D344DB2ABF996D

I note that 'tpm2tss-genkey' stopped working with the change I made at the beginning of this mail. I confess I haven't even looked up what any of those flags mean...

$ ./tpm2tss-genkey  new.tss
Initializing
Setting owner auth to empty auth.
Generating RSA key for 2048 bits keysize.
Establishing connection with TPM.
WARNING:esys:src/tss2-esys/esys_tcti_default.c:137:tcti_from_file() Could not load TCTI file: libtss2-tcti-default.so 
WARNING:esys:src/tss2-esys/esys_tcti_default.c:137:tcti_from_file() Could not load TCTI file: libtss2-tcti-tabrmd.so 
Creating primary key under owner.
Generating the RSA key inside the TPM.
WARNING:esys:src/tss2-esys/api/Esys_Create.c:411:Esys_Create_Finish() Received TPM Error 
ERROR:esys:src/tss2-esys/api/Esys_Create.c:155:Esys_Create() Esys Finish ErrorCode (0x000002c2) 
Error: Generating key failed
Key could not be generated.
author
#2 lore raw
Quoted 21 lines · David Woodhouse

So... that now *is* working for signing. However, while the corresponding 'rsautl -verify' works fine with James's tpm2 engine, it fails with tpm2tss:

$ openssl rsautl -verify -engine tpm2tss -inkey ../openssl_tpm2_engine/tpm2-engine.tss -keyform engine -in testsig.tpm2tss
Initializing
engine "tpm2tss" set.
Loading private key ../openssl_tpm2_engine/tpm2-engine.tss
get_auth called for object user key with ui_method 0x55ced253fdc0
Enter password for user key:
password is 
Loaded key uses alg-id 1
Creating RSA key object.
Created RSA key object.
TPM2 Key loaded
RSA operation error
140407559393728:error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01:../crypto/rsa/rsa_pk1.c:75:
140407559393728:error:04067072:rsa routines:rsa_ossl_public_decrypt:padding check failed:../crypto/rsa/rsa_ossl.c:586:
Quoted 1 line

So... is it the public key that I've misconverted somehow?

I've resolved that one too. James explicitly sets the exponent in tpm2Data->pub.publicArea.parameters.rsaDetail.exponent to zero if it's 0x10001, with a comment saying "zero means standard exponent. Some TPM chips will reject a non standard exponent".

So now I do have OpenConnect using my wrapped VPN key, with both engines (although tpm2-tss-engine stills asks me for an empty password).

By setting objectAttributes when creating a key, I can make tpm2tss-genkey work again too.

Full patch below, for reference. Don't heckle too hard; it exists mostly to document the current incompatibilities. I'll let the two of you come to an agreement on the correct way to resolve them, while I throw together some GnuTLS code to use the same PEM files.

(Since the tpm2-tss-engine code is under a licence which lets me crib from it, and the libraries actually have pkgconfig etc., I suppose that's made this application author's implementation choice fairly much a no-brainer...)

src/tpm2-tss-engine-common.h +1 -2
@@ -55,8 +55,7 @@ TSS2_RC init_tpm_key(ESYS_CONTEXT **ctx, ESYS_TR *keyHandle,
         .objectAttributes = (TPMA_OBJECT_USERWITHAUTH | \
                              TPMA_OBJECT_RESTRICTED | \
                              TPMA_OBJECT_DECRYPT | \
-                             TPMA_OBJECT_FIXEDTPM | \
-                             TPMA_OBJECT_FIXEDPARENT | \
+                             TPMA_OBJECT_NODA | \
                              TPMA_OBJECT_SENSITIVEDATAORIGIN), \
         .authPolicy = { \
              .size = 0, \
src/tpm2-tss-engine-rsa.c +4 -2
@@ -304,7 +304,7 @@ populate_rsa(RSA *rsa) {
         ERR(populate_rsa, ERR_R_MALLOC_FAILURE);
         goto error;
     }
-    BN_set_word(rsa->e, tpm2Data->pub.publicArea.parameters.rsaDetail.exponent);
+    BN_set_word(rsa->e, tpm2Data->pub.publicArea.parameters.rsaDetail.exponent ? : 0x10001);
 
     /* Setting private portions to 0 values so the public key can be extracted
        from the keyfile if this is desired. */
@@ -366,7 +366,7 @@ populate_rsa(RSA *rsa) {
         goto error;
     }
 
-    BN_set_word(e, tpm2Data->pub.publicArea.parameters.rsaDetail.exponent);
+    BN_set_word(e, tpm2Data->pub.publicArea.parameters.rsaDetail.exponent ? : 0x10001);
     BN_set_word(d, 0);
     BN_set_word(p, 0);
     BN_set_word(q, 0);
@@ -485,6 +485,8 @@ tpm2tss_rsa_genkey(RSA *rsa, int bits, BIGNUM *e, char *password)
     if (e)
         inPublic.publicArea.parameters.rsaDetail.exponent = BN_get_word(e);
 
+    inPublic.publicArea.objectAttributes = TPMA_OBJECT_SENSITIVEDATAORIGIN | TPMA_OBJECT_NODA;
+
     if (password) {
         DBG("Setting a password for the created key.\n");
         if (strlen(password) > sizeof(tpm2Data->userauth.buffer) - 1) {
author
#3 lore raw
Quoted 6 lines · David Woodhouse

Full patch below, for reference. Don't heckle too hard; it exists mostly to document the current incompatibilities. I'll let the two of you come to an agreement on the correct way to resolve them, while I throw together some GnuTLS code to use the same PEM files.

OK... OpenConnect now supports both OpenSSL engines, and I've thrown together some GnuTLS code cribbing from tpm2-tss-engine, implementing a gnutls_privkey_t with some caveats (RSA only, PKCS#1 padding only, no auth, no policy, only the default parent, ...).

http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2_esys.c
http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2.c

I'd love *not* to have to fix all those caveats, and for this to live in a library somewhere instead. :)

#4 lore raw

Should we try to setup a wiki or markdown to start converging into a single form ? I think we can also easily set NODA for the primary, because they have to auth value anyways.

@James: how do you handle the key-ids ? Allways assume them to be files ?
I have a PR for persistent TPM keys, where all key ids starting with 0x are interpreted as TPM keys. For the future I'll also want to reference FAPI keys (path-like format).
Thus, any clues on how to handle things consistently here ?

tpm2-tss-engine will propably not support policies from your format then, but wait until FAPI (with integrated policy engine) is available.
________________________________________
From: David Woodhouse [dwmw2(a)infradead.org]
Sent: Wednesday, October 03, 2018 22:47
To: James Bottomley; Fuchs, Andreas; tpm2(a)lists.01.org; Nikos Mavrogiannopoulos
Subject: Re: [tpm2] Conflicting TPM2 engines and storage formats

Quoted 6 lines · David Woodhouse

Full patch below, for reference. Don't heckle too hard; it exists mostly to document the current incompatibilities. I'll let the two of you come to an agreement on the correct way to resolve them, while I throw together some GnuTLS code to use the same PEM files.

OK... OpenConnect now supports both OpenSSL engines, and I've thrown together some GnuTLS code cribbing from tpm2-tss-engine, implementing a gnutls_privkey_t with some caveats (RSA only, PKCS#1 padding only, no auth, no policy, only the default parent, ...).

http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2_esys.c
http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2.c

I'd love *not* to have to fix all those caveats, and for this to live in a library somewhere instead. :)

author
#5 lore raw

FWIW I have the GnuTLS code also working, including with EC keys (and I think I know how to tell GnuTLS not to ask me to do SHA512). I should be able to adjust it to conform to whatever consensus we reach.

cf. https://tools.ietf.org/html/draft-mavrogiannopoulos-tpmuri-01

Quoted 33 lines · Andreas

Should we try to setup a wiki or markdown to start converging into a single form ? I think we can also easily set NODA for the primary, because they have to auth value anyways.

@James: how do you handle the key-ids ? Allways assume them to be files ?
I have a PR for persistent TPM keys, where all key ids starting with 0x are interpreted as TPM keys. For the future I'll also want to reference FAPI keys (path-like format).
Thus, any clues on how to handle things consistently here ?

tpm2-tss-engine will propably not support policies from your format then, but wait until FAPI (with integrated policy engine) is available.
________________________________________
From: David Woodhouse [dwmw2(a)infradead.org]
Sent: Wednesday, October 03, 2018 22:47
To: James Bottomley; Fuchs, Andreas; tpm2(a)lists.01.org; Nikos Mavrogiannopoulos
Subject: Re: [tpm2] Conflicting TPM2 engines and storage formats

Quoted 6 lines · David Woodhouse

Full patch below, for reference. Don't heckle too hard; it exists mostly to document the current incompatibilities. I'll let the two of you come to an agreement on the correct way to resolve them, while I throw together some GnuTLS code to use the same PEM files.

OK... OpenConnect now supports both OpenSSL engines, and I've thrown together some GnuTLS code cribbing from tpm2-tss-engine, implementing a gnutls_privkey_t with some caveats (RSA only, PKCS#1 padding only, no auth, no policy, only the default parent, ...).

http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2_esys.c
http://git.infradead.org/users/dwmw2/openconnect.git/blob/tpm2:/gnutls_tpm2.c

I'd love *not* to have to fix all those caveats, and for this to live in a library somewhere instead. :)