-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented and tested TPM2_Certify.
- Loading branch information
Matthew Tsai
committed
Jul 12, 2022
1 parent
d20ef6e
commit 9d0b8e9
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package tpm2 | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"testing" | ||
|
||
"github.com/google/go-tpm/direct/helpers" | ||
"github.com/google/go-tpm/direct/structures/tpm" | ||
"github.com/google/go-tpm/direct/structures/tpm2b" | ||
"github.com/google/go-tpm/direct/structures/tpma" | ||
"github.com/google/go-tpm/direct/structures/tpml" | ||
"github.com/google/go-tpm/direct/structures/tpms" | ||
"github.com/google/go-tpm/direct/structures/tpmt" | ||
"github.com/google/go-tpm/direct/structures/tpmu" | ||
"github.com/google/go-tpm/direct/transport/simulator" | ||
) | ||
|
||
func TestCertify(t *testing.T) { | ||
|
||
thetpm, err := simulator.OpenSimulator() | ||
if err != nil { | ||
t.Fatalf("could not connect to TPM simulator: %v", err) | ||
} | ||
defer thetpm.Close() | ||
|
||
Auth := []byte("password") | ||
|
||
PCR7, err := CreatePCRSelection([]int{7}) | ||
if err != nil { | ||
t.Fatalf("Failed to create PCRSelection") | ||
} | ||
public := tpm2b.Public{ | ||
PublicArea: tpmt.Public{ | ||
Type: tpm.AlgRSA, | ||
NameAlg: tpm.AlgSHA256, | ||
ObjectAttributes: tpma.Object{ | ||
SignEncrypt: true, | ||
Restricted: true, | ||
FixedTPM: true, | ||
FixedParent: true, | ||
SensitiveDataOrigin: true, | ||
UserWithAuth: true, | ||
}, | ||
Parameters: tpmu.PublicParms{ | ||
RSADetail: &tpms.RSAParms{ | ||
Scheme: tpmt.RSAScheme{ | ||
Scheme: tpm.AlgRSASSA, | ||
Details: tpmu.AsymScheme{ | ||
RSASSA: &tpms.SigSchemeRSASSA{ | ||
HashAlg: tpm.AlgSHA256, | ||
}, | ||
}, | ||
}, | ||
KeyBits: 2048, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
pcrSelection := tpml.PCRSelection{ | ||
PCRSelections: []tpms.PCRSelection{ | ||
{ | ||
Hash: tpm.AlgSHA256, | ||
PCRSelect: PCR7, | ||
}, | ||
}, | ||
} | ||
|
||
createPrimarySigner := CreatePrimary{ | ||
PrimaryHandle: tpm.RHOwner, | ||
InSensitive: tpm2b.SensitiveCreate{ | ||
Sensitive: tpms.SensitiveCreate{ | ||
UserAuth: tpm2b.Auth{ | ||
Buffer: Auth, | ||
}, | ||
}, | ||
}, | ||
InPublic: public, | ||
CreationPCR: pcrSelection, | ||
} | ||
rspSigner, err := createPrimarySigner.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("Failed to create primary: %v", err) | ||
} | ||
flushContextSigner := FlushContext{FlushHandle: rspSigner.ObjectHandle} | ||
defer flushContextSigner.Execute(thetpm) | ||
|
||
createPrimarySubject := CreatePrimary{ | ||
PrimaryHandle: tpm.RHPlatform, | ||
InSensitive: tpm2b.SensitiveCreate{ | ||
Sensitive: tpms.SensitiveCreate{ | ||
UserAuth: tpm2b.Auth{ | ||
Buffer: Auth, | ||
}, | ||
}, | ||
}, | ||
InPublic: public, | ||
CreationPCR: pcrSelection, | ||
} | ||
rspSubject, err := createPrimarySubject.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("Failed to create primary: %v", err) | ||
} | ||
flushContextSubject := FlushContext{FlushHandle: rspSubject.ObjectHandle} | ||
defer flushContextSubject.Execute(thetpm) | ||
|
||
certify := Certify{ | ||
ObjectHandle: AuthHandle{ | ||
Handle: rspSubject.ObjectHandle, | ||
Name: rspSubject.Name, | ||
Auth: PasswordAuth(Auth), | ||
}, | ||
SignHandle: AuthHandle{ | ||
Handle: rspSigner.ObjectHandle, | ||
Name: rspSigner.Name, | ||
Auth: PasswordAuth(Auth), | ||
}, | ||
QualifyingData: tpm2b.Data{ | ||
Buffer: nil, | ||
}, | ||
InScheme: tpmt.SigScheme{ | ||
Scheme: tpm.AlgNull, | ||
}, | ||
} | ||
|
||
rspCert, err := certify.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("Failed to certify: %v", err) | ||
} | ||
|
||
info, err := Marshal(rspCert.CertifyInfo.AttestationData) | ||
if err != nil { | ||
t.Fatalf("Failed to marshal: %v", err) | ||
} | ||
attestHash := sha256.Sum256(info) | ||
pub := rspSigner.OutPublic.PublicArea | ||
rsaPub, err := helpers.RSAPub(pub.Parameters.RSADetail, pub.Unique.RSA) | ||
if err != nil { | ||
t.Fatalf("Failed to retrive Public Key: %v", err) | ||
} | ||
|
||
if err := rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, attestHash[:], rspCert.Signature.Signature.RSASSA.Sig.Buffer); err != nil { | ||
t.Errorf("Signature verification failed: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters