From 6dc8703b0a5b5e5ced063dfc5813d331328c7912 Mon Sep 17 00:00:00 2001 From: Mate Kukri Date: Thu, 30 Jan 2025 15:25:48 +0000 Subject: [PATCH] tpm2: Make it possible to customize the name algorithm for TPM sealed keys This is accomplished by adding the new field 'NameAlg' to the structure 'ProtectKeyParams'. This means callers of these external APIs will need to fill in the new field (affected tests): - NewExternalTPMProtectedKey (platform_test.go) - NewTPMProtectedKey (seal_test.go, update_test.go, platform_test.go) - NewTPMPassphraseProtectedKey (platform_test.go) Signed-off-by: Mate Kukri --- tpm2/platform_test.go | 26 ++++++++++++-- tpm2/seal.go | 16 +++++++-- tpm2/seal_test.go | 84 +++++++++++++++++++++++++++++++++++-------- tpm2/update_test.go | 10 ++++-- 4 files changed, 112 insertions(+), 24 deletions(-) diff --git a/tpm2/platform_test.go b/tpm2/platform_test.go index 956f049b..344e264d 100644 --- a/tpm2/platform_test.go +++ b/tpm2/platform_test.go @@ -77,6 +77,7 @@ func (s *platformSuite) TestRecoverKeysIntegrated(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } k, primaryKey, unlockKey, err := NewTPMProtectedKey(s.TPM(), params) @@ -95,6 +96,7 @@ func (s *platformSuite) TestRecoverKeysWithPassphraseIntegrated(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "bar", + NameAlg: tpm2.HashAlgorithmSHA256, } passphraseParams := &PassphraseProtectKeyParams{ @@ -117,6 +119,7 @@ func (s *platformSuite) TestRecoverKeysWithPassphraseIntegratedPBKDF2(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } passphraseParams := &PassphraseProtectKeyParams{ @@ -143,6 +146,7 @@ func (s *platformSuite) TestRecoverKeysWithBadPassphraseIntegrated(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } passphraseParams := &PassphraseProtectKeyParams{ @@ -163,6 +167,7 @@ func (s *platformSuite) TestChangePassphraseIntegrated(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } passphraseParams := &PassphraseProtectKeyParams{ @@ -187,6 +192,7 @@ func (s *platformSuite) TestChangePassphraseWithBadPassphraseIntegrated(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } passphraseParams := &PassphraseProtectKeyParams{ @@ -255,6 +261,7 @@ func (s *platformSuite) TestRecoverKeysSimplePCRProfile(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -262,6 +269,7 @@ func (s *platformSuite) TestRecoverKeysNilPCRProfile(c *C) { s.testRecoverKeys(c, &ProtectKeyParams{ PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -270,6 +278,7 @@ func (s *platformSuite) TestRecoverKeysNoPCRPolicyCounter(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: tpm2.HandleNull, Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -278,6 +287,7 @@ func (s *platformSuite) TestRecoverKeysDifferentRole(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "bar", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -289,6 +299,7 @@ func (s *platformSuite) TestRecoverKeysTPMLockout(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -297,6 +308,7 @@ func (s *platformSuite) testRecoverKeysNoValidSRK(c *C, prepareSrk func()) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } k, primaryKey, unlockKey, err := NewTPMProtectedKey(s.TPM(), params) @@ -387,13 +399,15 @@ func (s *platformSuite) testRecoverKeysImportable(c *C, params *ProtectKeyParams func (s *platformSuite) TestRecoverKeysImportableSimplePCRProfile(c *C) { s.testRecoverKeysImportable(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewResolvedPCRProfileFromCurrentValues(c, s.TPM().TPMContext, tpm2.HashAlgorithmSHA256, []int{7}), - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *platformSuite) TestRecoverKeysImportableNilPCRProfile(c *C) { s.testRecoverKeysImportable(c, &ProtectKeyParams{ PCRPolicyCounterHandle: tpm2.HandleNull, - Role: ""}) + Role: "", + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *platformSuite) TestRecoverKeysNoTPMConnection(c *C) { @@ -404,6 +418,7 @@ func (s *platformSuite) TestRecoverKeysNoTPMConnection(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: tpm2.HandleNull, Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, }) c.Check(err, IsNil) @@ -431,7 +446,8 @@ func (s *platformSuite) testRecoverKeysUnsealErrorHandling(c *C, prepare func(*s params := &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), - Role: "foo"} + Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256} k, primaryKey, _, err := NewTPMProtectedKey(s.TPM(), params) c.Assert(err, IsNil) @@ -581,6 +597,7 @@ func (s *platformSuite) TestRecoverKeysWithAuthKey(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, } k, primaryKey, unlockKey, err := NewTPMProtectedKey(s.TPM(), params) @@ -671,6 +688,7 @@ func (s *platformSuite) TestRecoverKeysWithIncorrectAuthKey(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, } k, _, _, err := NewTPMProtectedKey(s.TPM(), params) @@ -754,6 +772,7 @@ func (s *platformSuite) TestChangeAuthKeyWithIncorrectAuthKey(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, } k, _, _, err := NewTPMProtectedKey(s.TPM(), params) @@ -839,6 +858,7 @@ func (s *platformSuite) TestRecoverKeysWithAuthKeyTPMLockout(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, } k, _, _, err := NewTPMProtectedKey(s.TPM(), params) diff --git a/tpm2/seal.go b/tpm2/seal.go index 10f27fa6..fee171db 100644 --- a/tpm2/seal.go +++ b/tpm2/seal.go @@ -59,6 +59,8 @@ type ProtectKeyParams struct { PCRPolicyCounterHandle tpm2.Handle PrimaryKey secboot.PrimaryKey + + NameAlg tpm2.HashAlgorithmId } type PassphraseProtectKeyParams struct { @@ -102,6 +104,7 @@ type makeSealedKeyDataParams struct { PcrPolicyCounterHandle tpm2.Handle PrimaryKey secboot.PrimaryKey AuthMode secboot.AuthMode + NameAlg tpm2.HashAlgorithmId } // makeSealedKeyData makes a sealed key data using the supplied parameters, keySealer implementation, @@ -111,6 +114,11 @@ type makeSealedKeyDataParams struct { // used for authenticating the storage hierarchy in order to avoid trasmitting the cleartext authorization // value. var makeSealedKeyData = func(tpm *tpm2.TPMContext, params *makeSealedKeyDataParams, sealer keySealer, constructor keyDataConstructor, session tpm2.SessionContext) (*secboot.KeyData, secboot.PrimaryKey, secboot.DiskUnlockKey, error) { + // Make sure the requested name algorithm is available. + if !params.NameAlg.Available() { + return nil, nil, nil, xerrors.Errorf("chosen name algorithm %v is not available", params.NameAlg) + } + // Create a primary key, if required. primaryKey := params.PrimaryKey if primaryKey == nil { @@ -146,10 +154,9 @@ var makeSealedKeyData = func(tpm *tpm2.TPMContext, params *makeSealedKeyDataPara } // Create the initial policy data. - nameAlg := tpm2.HashAlgorithmSHA256 requireAuthValue := params.AuthMode != secboot.AuthModeNone - policyData, authPolicyDigest, err := newKeyDataPolicy(nameAlg, authPublicKey, params.Role, pcrPolicyCounterPub, requireAuthValue) + policyData, authPolicyDigest, err := newKeyDataPolicy(params.NameAlg, authPublicKey, params.Role, pcrPolicyCounterPub, requireAuthValue) if err != nil { return nil, nil, nil, xerrors.Errorf("cannot create initial policy data: %w", err) } @@ -163,7 +170,7 @@ var makeSealedKeyData = func(tpm *tpm2.TPMContext, params *makeSealedKeyDataPara // Seal the symmetric key and nonce. The final boolean argument is set to true in order // to disable dictionary attack protection (ie, adding the noDA attribute). We want this // when no user auth value is required. - priv, pub, importSymSeed, err := sealer.CreateSealedObject(symKey[:], nameAlg, authPolicyDigest, !requireAuthValue) + priv, pub, importSymSeed, err := sealer.CreateSealedObject(symKey[:], params.NameAlg, authPolicyDigest, !requireAuthValue) if err != nil { return nil, nil, nil, err } @@ -253,6 +260,7 @@ func NewExternalTPMProtectedKey(tpmKey *tpm2.Public, params *ProtectKeyParams) ( AuthMode: secboot.AuthModeNone, Role: params.Role, PcrProfile: params.PCRProfile, + NameAlg: params.NameAlg, }, sealer, makeKeyDataNoAuth, nil) } @@ -291,6 +299,7 @@ func NewTPMProtectedKey(tpm *Connection, params *ProtectKeyParams) (protectedKey PcrPolicyCounterHandle: params.PCRPolicyCounterHandle, PrimaryKey: params.PrimaryKey, AuthMode: secboot.AuthModeNone, + NameAlg: params.NameAlg, }, sealer, makeKeyDataNoAuth, tpm.HmacSession()) } @@ -308,5 +317,6 @@ func NewTPMPassphraseProtectedKey(tpm *Connection, params *PassphraseProtectKeyP AuthMode: secboot.AuthModePassphrase, Role: params.Role, PcrProfile: params.PCRProfile, + NameAlg: params.NameAlg, }, sealer, makeKeyDataWithPassphraseConstructor(tpm, params.KDFOptions, passphrase), tpm.HmacSession()) } diff --git a/tpm2/seal_test.go b/tpm2/seal_test.go index dd4640e7..0177dd2f 100644 --- a/tpm2/seal_test.go +++ b/tpm2/seal_test.go @@ -127,10 +127,10 @@ func (s *sealSuite) testProtectKeyWithTPM(c *C, params *ProtectKeyParams) { } - expectedPolicyData, expectedPolicyDigest, err := NewKeyDataPolicy(tpm2.HashAlgorithmSHA256, policyAuthPublicKey, params.Role, pcrPolicyCounterPub, false) + expectedPolicyData, expectedPolicyDigest, err := NewKeyDataPolicy(params.NameAlg, policyAuthPublicKey, params.Role, pcrPolicyCounterPub, false) c.Assert(err, IsNil) - c.Check(skd.Data().Public().NameAlg, Equals, tpm2.HashAlgorithmSHA256) + c.Check(skd.Data().Public().NameAlg, Equals, params.NameAlg) c.Check(skd.Data().Public().Attrs, Equals, tpm2.AttrFixedTPM|tpm2.AttrFixedParent|tpm2.AttrNoDA) c.Check(skd.Data().Public().AuthPolicy, DeepEquals, expectedPolicyDigest) c.Check(skd.Data().Policy().(*KeyDataPolicy_v3).StaticData, tpm2_testutil.TPMValueDeepEquals, expectedPolicyData.(*KeyDataPolicy_v3).StaticData) @@ -166,6 +166,7 @@ func (s *sealSuite) TestProtectKeyWithTPM(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -174,6 +175,7 @@ func (s *sealSuite) TestProtectKeyWithTPMDifferentPCRPolicyCounterHandle(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181fff0), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -186,6 +188,7 @@ func (s *sealSuite) TestProtectKeyWithTPMWithNewConnection(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) s.validateSRK(c) @@ -203,6 +206,7 @@ func (s *sealSuite) TestProtectKeyWithTPMMissingSRK(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) s.validateSRK(c) @@ -245,6 +249,7 @@ func (s *sealSuite) TestProtectKeyWithTPMMissingCustomSRK(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) s.validatePrimaryKeyAgainstTemplate(c, tpm2.HandleOwner, tcg.SRKHandle, template) @@ -290,6 +295,7 @@ func (s *sealSuite) TestProtectKeyWithTPMMissingSRKWithInvalidCustomTemplate(c * PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) s.validateSRK(c) @@ -300,6 +306,7 @@ func (s *sealSuite) TestProtectKeyWithTPMNoPCRPolicyCounterHandle(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: tpm2.HandleNull, Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -311,20 +318,30 @@ func (s *sealSuite) TestProtectKeyWithTPMWithProvidedPrimaryKey(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", - PrimaryKey: primaryKey}) + PrimaryKey: primaryKey, + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *sealSuite) TestProtectKeyWithTPMWithDifferentRole(c *C) { s.testProtectKeyWithTPM(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), - Role: "bar"}) + Role: "bar", + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *sealSuite) TestProtectKeyWithTPMWithNoRole(c *C) { s.testProtectKeyWithTPM(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), - PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000)}) + PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), + NameAlg: tpm2.HashAlgorithmSHA256}) +} + +func (s *sealSuite) TestProtectKeyWithTPMNameAlgSHA384(c *C) { + s.testProtectKeyWithTPM(c, &ProtectKeyParams{ + PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA384, []int{7, 23}), + PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), + NameAlg: tpm2.HashAlgorithmSHA384}) } func (s *sealSuite) testProtectKeyWithTPMErrorHandling(c *C, params *ProtectKeyParams) error { @@ -376,7 +393,8 @@ func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingOwnerAuthFail(c *C) { s.ReinitTPMConnectionFromExisting(c) err := s.testProtectKeyWithTPMErrorHandling(c, &ProtectKeyParams{ - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) c.Assert(err, testutil.ConvertibleTo, AuthFailError{}) c.Check(err.(AuthFailError).Handle, Equals, tpm2.HandleOwner) } @@ -388,6 +406,7 @@ func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingInvalidPCRProfile(c *C) { NewPCRProtectionProfile(), NewPCRProtectionProfile().AddPCRValueFromTPM(tpm2.HashAlgorithmSHA256, 8)), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x0181ffff), // verify that this gets undefined on error + NameAlg: tpm2.HashAlgorithmSHA256, }) c.Check(err, ErrorMatches, "cannot set initial PCR policy: cannot compute PCR digests from protection profile: "+ "not all branches contain values for the same sets of PCRs") @@ -396,7 +415,8 @@ func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingInvalidPCRProfile(c *C) { func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingInvalidPCRProfileSelection(c *C) { err := s.testProtectKeyWithTPMErrorHandling(c, &ProtectKeyParams{ PCRProfile: NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 50, make([]byte, tpm2.HashAlgorithmSHA256.Size())), - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, ErrorMatches, "cannot set initial PCR policy: PCR protection profile contains digests for unsupported PCRs") } @@ -404,17 +424,27 @@ func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingInvalidRole(c *C) { err := s.testProtectKeyWithTPMErrorHandling(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: tpm2.HandleNull, - Role: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}) + Role: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, ErrorMatches, `cannot create initial policy data: invalid role: too large`) } +func (s *sealSuite) TestProtectKeyWithTPMErrorHandlingInvalidNameAlg(c *C) { + err := s.testProtectKeyWithTPMErrorHandling(c, &ProtectKeyParams{ + PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), + PCRPolicyCounterHandle: tpm2.HandleNull, + Role: "foobar", + NameAlg: 0}) + c.Check(err, ErrorMatches, `chosen name algorithm 0x0000 is not available`) +} + func (s *sealSuite) testPassphraseProtectKeyWithTPM(c *C, params *PassphraseProtectKeyParams, passphrase string) { s.AddCleanup(MockSecbootNewKeyDataWithPassphrase(func(keyParams *secboot.KeyWithPassphraseParams, keyPassphrase string) (*secboot.KeyData, error) { c.Check(keyParams.Role, Equals, params.Role) c.Check(keyParams.PlatformName, Equals, "tpm2") c.Check(keyParams.KDFAlg, Equals, crypto.SHA256) c.Check(keyParams.KDFOptions, DeepEquals, params.KDFOptions) - c.Check(keyParams.AuthKeySize, Equals, 32) + c.Check(keyParams.AuthKeySize, Equals, params.NameAlg.GetHash().Size()) c.Check(keyPassphrase, Equals, passphrase) // TODO: Check EncryptedPayload and Handle fields @@ -447,10 +477,10 @@ func (s *sealSuite) testPassphraseProtectKeyWithTPM(c *C, params *PassphraseProt } - expectedPolicyData, expectedPolicyDigest, err := NewKeyDataPolicy(tpm2.HashAlgorithmSHA256, policyAuthPublicKey, params.Role, pcrPolicyCounterPub, true) + expectedPolicyData, expectedPolicyDigest, err := NewKeyDataPolicy(params.NameAlg, policyAuthPublicKey, params.Role, pcrPolicyCounterPub, true) c.Assert(err, IsNil) - c.Check(skd.Data().Public().NameAlg, Equals, tpm2.HashAlgorithmSHA256) + c.Check(skd.Data().Public().NameAlg, Equals, params.NameAlg) c.Check(skd.Data().Public().Attrs, Equals, tpm2.AttrFixedTPM|tpm2.AttrFixedParent) c.Check(skd.Data().Public().AuthPolicy, DeepEquals, expectedPolicyDigest) c.Check(skd.Data().Policy().(*KeyDataPolicy_v3).StaticData, tpm2_testutil.TPMValueDeepEquals, expectedPolicyData.(*KeyDataPolicy_v3).StaticData) @@ -487,6 +517,18 @@ func (s *sealSuite) TestPassphraseProtectKeyWithTPM(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, + }, + }, "Jg4zg4GF9WGL") +} + +func (s *sealSuite) TestPassphraseProtectKeyWithTPMNameAlgSHA384(c *C) { + s.testPassphraseProtectKeyWithTPM(c, &PassphraseProtectKeyParams{ + ProtectKeyParams: ProtectKeyParams{ + PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA384, []int{7, 23}), + PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), + Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA384, }, }, "Jg4zg4GF9WGL") } @@ -497,6 +539,7 @@ func (s *sealSuite) TestPassphraseProtectKeyWithTPMSuppliedKDFOptions(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }, KDFOptions: &secboot.Argon2Options{ Mode: secboot.Argon2id, @@ -513,6 +556,7 @@ func (s *sealSuite) TestPassphraseProtectKeyWithTPMDifferentSuppliedKDFOptions(c PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }, KDFOptions: &secboot.PBKDF2Options{ ForceIterations: 100000, @@ -527,6 +571,7 @@ func (s *sealSuite) TestPassphraseProtectKeyWithTPMDifferentPassphrase(c *C) { PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256, }, }, "uWjzz3MURKUS") } @@ -587,12 +632,14 @@ func (s *sealSuite) TestProtectKeyWithExternalStorageKey(c *C) { s.testProtectKeyWithExternalStorageKey(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewResolvedPCRProfileFromCurrentValues(c, s.TPM().TPMContext, tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256, }) } func (s *sealSuite) TestProtectKeyWithExternalStorageKeyNilPCRProfileAndNoAuthorizedSnapModels(c *C) { s.testProtectKeyWithExternalStorageKey(c, &ProtectKeyParams{ - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *sealSuite) TestProtectKeyWithExternalStorageKeyWithProvidedPrimaryKey(c *C) { @@ -602,7 +649,8 @@ func (s *sealSuite) TestProtectKeyWithExternalStorageKeyWithProvidedPrimaryKey(c s.testProtectKeyWithExternalStorageKey(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewResolvedPCRProfileFromCurrentValues(c, s.TPM().TPMContext, tpm2.HashAlgorithmSHA256, []int{7, 23}), PCRPolicyCounterHandle: tpm2.HandleNull, - PrimaryKey: primaryKey}) + PrimaryKey: primaryKey, + NameAlg: tpm2.HashAlgorithmSHA256}) } func (s *sealSuite) testProtectKeyWithExternalStorageKeyErrorHandling(c *C, params *ProtectKeyParams) error { @@ -626,7 +674,8 @@ func (s *sealSuite) TestProtectKeyWithExternalStorageKeyErrorHandlingNilParams(c func (s *sealSuite) TestProtectKeyWithExternalStorageKeyErrorHandlingInvalidPCRProfile(c *C) { err := s.testProtectKeyWithExternalStorageKeyErrorHandling(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7}), - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, ErrorMatches, "cannot set initial PCR policy: cannot compute PCR digests from protection profile: "+ "cannot read current PCR values from TPM: no context") } @@ -634,7 +683,8 @@ func (s *sealSuite) TestProtectKeyWithExternalStorageKeyErrorHandlingInvalidPCRP func (s *sealSuite) TestProtectKeyWithExternalStorageKeyErrorHandlingInvalidPCRProfileSelection(c *C) { err := s.testProtectKeyWithExternalStorageKeyErrorHandling(c, &ProtectKeyParams{ PCRProfile: NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 50, make([]byte, tpm2.HashAlgorithmSHA256.Size())), - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, ErrorMatches, "cannot set initial PCR policy: PCR protection profile contains digests for unsupported PCRs") } @@ -698,6 +748,7 @@ type testMakeSealedKeyDataData struct { Role string PCRPolicyCounterHandle tpm2.Handle PrimaryKey secboot.PrimaryKey + NameAlg tpm2.HashAlgorithmId } func (s *sealSuiteNoTPM) testMakeSealedKeyData(c *C, data *testMakeSealedKeyDataData) { @@ -781,6 +832,7 @@ func (s *sealSuiteNoTPM) testMakeSealedKeyData(c *C, data *testMakeSealedKeyData Role: data.Role, PcrPolicyCounterHandle: data.PCRPolicyCounterHandle, PrimaryKey: primaryKey, + NameAlg: data.NameAlg, } constructor := MakeKeyDataNoAuth @@ -839,6 +891,7 @@ func (s *sealSuiteNoTPM) TestMakeSealedKeyData(c *C) { PCRProfile: NewPCRProtectionProfile(), PCRPolicyCounterHandle: 0x01800000, Role: "", + NameAlg: tpm2.HashAlgorithmSHA256, }) } @@ -847,5 +900,6 @@ func (s *sealSuiteNoTPM) TestMakeSealedKeyData2(c *C) { PCRProfile: NewPCRProtectionProfile(), PCRPolicyCounterHandle: 0x01800000, Role: "test", + NameAlg: tpm2.HashAlgorithmSHA256, }) } diff --git a/tpm2/update_test.go b/tpm2/update_test.go index 5e006f42..f1551ea6 100644 --- a/tpm2/update_test.go +++ b/tpm2/update_test.go @@ -69,7 +69,8 @@ func (s *updateSuite) testUpdatePCRProtectionPolicy(c *C, data *testUpdatePCRPro PCRProfile: NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.DecodeHexString(c, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")), PCRPolicyCounterHandle: data.pcrPolicyCounterHandle, PrimaryKey: data.primaryKey, - Role: "foo"} + Role: "foo", + NameAlg: tpm2.HashAlgorithmSHA256} k, primaryKey, _, err := NewTPMProtectedKey(s.TPM(), params) c.Assert(err, IsNil) @@ -170,14 +171,16 @@ func (s *updateSuite) testRevokeOldPCRProtectionPolicies(c *C, params *ProtectKe func (s *updateSuite) TestRevokeOldPCRProtectionPoliciesWithPCRPolicyCounter(c *C) { err := s.testRevokeOldPCRProtectionPolicies(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), - PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000)}) + PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, ErrorMatches, "invalid key data: cannot complete authorization policy assertions: the PCR policy has been revoked") } func (s *updateSuite) TestRevokeOldPCRProtectionPoliciesWithoutPCRPolicyCounter(c *C) { err := s.testRevokeOldPCRProtectionPolicies(c, &ProtectKeyParams{ PCRProfile: tpm2test.NewPCRProfileFromCurrentValues(tpm2.HashAlgorithmSHA256, []int{7, 23}), - PCRPolicyCounterHandle: tpm2.HandleNull}) + PCRPolicyCounterHandle: tpm2.HandleNull, + NameAlg: tpm2.HashAlgorithmSHA256}) c.Check(err, IsNil) } @@ -191,6 +194,7 @@ func (s *updateSuite) TestUpdateKeyDataPCRProtectionPolicy(c *C) { PCRPolicyCounterHandle: s.NextAvailableHandle(c, 0x01810000), PrimaryKey: primaryKey, Role: "bar", + NameAlg: tpm2.HashAlgorithmSHA256, } var keys []*secboot.KeyData