| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/rand" |
| 6 |
"testing" |
| 7 |
|
| 8 |
. "github.com/google/go-tpm/tpm2" |
| 9 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 10 |
) |
| 11 |
|
| 12 |
// p384Template is an SRK-like ECDH-P384 key based on the P384 EK template. |
| 13 |
// The main purpose of this key is to have a different Name algorithm than the other keys involved in this test. |
| 14 |
var p384Template = TPMTPublic{ |
| 15 |
Type: TPMAlgECC, |
| 16 |
NameAlg: TPMAlgSHA384, |
| 17 |
ObjectAttributes: TPMAObject{ |
| 18 |
FixedTPM: true, |
| 19 |
STClear: false, |
| 20 |
FixedParent: true, |
| 21 |
SensitiveDataOrigin: true, |
| 22 |
UserWithAuth: true, |
| 23 |
AdminWithPolicy: false, |
| 24 |
NoDA: false, |
| 25 |
EncryptedDuplication: false, |
| 26 |
Restricted: true, |
| 27 |
Decrypt: true, |
| 28 |
SignEncrypt: false, |
| 29 |
}, |
| 30 |
Parameters: NewTPMUPublicParms( |
| 31 |
TPMAlgECC, |
| 32 |
&TPMSECCParms{ |
| 33 |
Symmetric: TPMTSymDefObject{ |
| 34 |
Algorithm: TPMAlgAES, |
| 35 |
KeyBits: NewTPMUSymKeyBits( |
| 36 |
TPMAlgAES, |
| 37 |
TPMKeyBits(256), |
| 38 |
), |
| 39 |
Mode: NewTPMUSymMode( |
| 40 |
TPMAlgAES, |
| 41 |
TPMAlgCFB, |
| 42 |
), |
| 43 |
}, |
| 44 |
CurveID: TPMECCNistP384, |
| 45 |
}, |
| 46 |
), |
| 47 |
Unique: NewTPMUPublicID( |
| 48 |
TPMAlgECC, |
| 49 |
&TPMSECCPoint{ |
| 50 |
X: TPM2BECCParameter{ |
| 51 |
Buffer: nil, |
| 52 |
}, |
| 53 |
Y: TPM2BECCParameter{ |
| 54 |
Buffer: nil, |
| 55 |
}, |
| 56 |
}, |
| 57 |
), |
| 58 |
} |
| 59 |
|
| 60 |
// This test checks that ActivateCredential can decrypt a credential created by the TPM in MakeCredential. |
| 61 |
func TestActivateTPMCredential(t *testing.T) { |
| 62 |
thetpm, err := simulator.OpenSimulator() |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 65 |
} |
| 66 |
defer thetpm.Close() |
| 67 |
|
| 68 |
ekCreate := CreatePrimary{ |
| 69 |
PrimaryHandle: TPMRHEndorsement, |
| 70 |
InPublic: New2B(ECCEKTemplate), |
| 71 |
} |
| 72 |
|
| 73 |
ekCreateRsp, err := ekCreate.Execute(thetpm) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("could not generate EK: %v", err) |
| 76 |
} |
| 77 |
defer func() { |
| 78 |
flush := FlushContext{ |
| 79 |
FlushHandle: ekCreateRsp.ObjectHandle, |
| 80 |
} |
| 81 |
_, err := flush.Execute(thetpm) |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("could not flush EK: %v", err) |
| 84 |
} |
| 85 |
}() |
| 86 |
|
| 87 |
srkCreate := CreatePrimary{ |
| 88 |
PrimaryHandle: TPMRHOwner, |
| 89 |
InPublic: New2B(ECCSRKTemplate), |
| 90 |
} |
| 91 |
|
| 92 |
srkCreateRsp, err := srkCreate.Execute(thetpm) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("could not generate SRK: %v", err) |
| 95 |
} |
| 96 |
defer func() { |
| 97 |
flush := FlushContext{ |
| 98 |
FlushHandle: srkCreateRsp.ObjectHandle, |
| 99 |
} |
| 100 |
_, err := flush.Execute(thetpm) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("could not flush SRK: %v", err) |
| 103 |
} |
| 104 |
}() |
| 105 |
|
| 106 |
secret := TPM2BDigest{Buffer: []byte("Secrets!!!")} |
| 107 |
|
| 108 |
mc := MakeCredential{ |
| 109 |
Handle: ekCreateRsp.ObjectHandle, |
| 110 |
Credential: secret, |
| 111 |
ObjectName: srkCreateRsp.Name, |
| 112 |
} |
| 113 |
mcRsp, err := mc.Execute(thetpm) |
| 114 |
if err != nil { |
| 115 |
t.Fatalf("could not make credential: %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
ac := ActivateCredential{ |
| 119 |
ActivateHandle: NamedHandle{ |
| 120 |
Handle: srkCreateRsp.ObjectHandle, |
| 121 |
Name: srkCreateRsp.Name, |
| 122 |
}, |
| 123 |
KeyHandle: AuthHandle{ |
| 124 |
Handle: ekCreateRsp.ObjectHandle, |
| 125 |
Name: ekCreateRsp.Name, |
| 126 |
Auth: Policy(TPMAlgSHA256, 16, ekPolicy), |
| 127 |
}, |
| 128 |
CredentialBlob: mcRsp.CredentialBlob, |
| 129 |
Secret: mcRsp.Secret, |
| 130 |
} |
| 131 |
acRsp, err := ac.Execute(thetpm) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("could not activate credential: %v", err) |
| 134 |
} |
| 135 |
|
| 136 |
if !bytes.Equal(acRsp.CertInfo.Buffer, secret.Buffer) { |
| 137 |
t.Errorf("want %x got %x", secret.Buffer, acRsp.CertInfo.Buffer) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// This test checks that ActivateCredential can decrypt a credential created by a remote server using CreateCredential. |
| 142 |
func TestActivateSWCredential(t *testing.T) { |
| 143 |
tpm, err := simulator.OpenSimulator() |
| 144 |
if err != nil { |
| 145 |
t.Fatalf("OpenSimulator() = %v", err) |
| 146 |
} |
| 147 |
defer tpm.Close() |
| 148 |
|
| 149 |
for _, tc := range []struct { |
| 150 |
// Name of the test case |
| 151 |
name string |
| 152 |
// Storage key template |
| 153 |
pubTemplate TPMTPublic |
| 154 |
// Credentialed object template |
| 155 |
subTemplate TPMTPublic |
| 156 |
}{ |
| 157 |
{ |
| 158 |
name: "ECDH-P256 SRK activating RSA SRK", |
| 159 |
pubTemplate: ECCSRKTemplate, |
| 160 |
subTemplate: RSASRKTemplate, |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "RSA-2048 SRK activating P256 SRK", |
| 164 |
pubTemplate: RSASRKTemplate, |
| 165 |
subTemplate: ECCSRKTemplate, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "ECDH-P256 SRK activating P384 key", |
| 169 |
pubTemplate: ECCSRKTemplate, |
| 170 |
subTemplate: p384Template, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "RSA-2048 SRK activating P384 key", |
| 174 |
pubTemplate: RSASRKTemplate, |
| 175 |
subTemplate: p384Template, |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "P384 key activating P256 SRK", |
| 179 |
pubTemplate: p384Template, |
| 180 |
subTemplate: ECCSRKTemplate, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "P384 key activating RSA SRK", |
| 184 |
pubTemplate: p384Template, |
| 185 |
subTemplate: RSASRKTemplate, |
| 186 |
}, |
| 187 |
} { |
| 188 |
t.Run(tc.name, func(t *testing.T) { |
| 189 |
// Create the key that is going to decrypt the credential challenge. |
| 190 |
primary, err := CreatePrimary{ |
| 191 |
PrimaryHandle: TPMRHOwner, |
| 192 |
InPublic: New2B(tc.pubTemplate), |
| 193 |
}.Execute(tpm) |
| 194 |
if err != nil { |
| 195 |
t.Fatalf("CreatePrimary() = %v", err) |
| 196 |
} |
| 197 |
defer FlushContext{FlushHandle: primary.ObjectHandle}.Execute(tpm) |
| 198 |
|
| 199 |
public, err := primary.OutPublic.Contents() |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("OutPublic.Contents() = %v", err) |
| 202 |
} |
| 203 |
|
| 204 |
key, err := ImportEncapsulationKey(public) |
| 205 |
if err != nil { |
| 206 |
t.Fatalf("ImportEncapsulationKey() = %v", err) |
| 207 |
} |
| 208 |
|
| 209 |
// Create the key that is going to be named in the challenge. |
| 210 |
subject, err := CreatePrimary{ |
| 211 |
PrimaryHandle: TPMRHOwner, |
| 212 |
InPublic: New2B(tc.subTemplate), |
| 213 |
}.Execute(tpm) |
| 214 |
if err != nil { |
| 215 |
t.Fatalf("CreatePrimary() = %v", err) |
| 216 |
} |
| 217 |
defer FlushContext{FlushHandle: subject.ObjectHandle}.Execute(tpm) |
| 218 |
|
| 219 |
// Create the challenge. |
| 220 |
plaintext := []byte("hello, credential") |
| 221 |
idObject, encSecret, err := CreateCredential(rand.Reader, key, subject.Name.Buffer, plaintext) |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("CreateCredential() = %v", err) |
| 224 |
} |
| 225 |
|
| 226 |
// Get the challenge decrypted. |
| 227 |
activate, err := ActivateCredential{ |
| 228 |
KeyHandle: NamedHandle{ |
| 229 |
Handle: primary.ObjectHandle, |
| 230 |
Name: primary.Name, |
| 231 |
}, |
| 232 |
ActivateHandle: NamedHandle{ |
| 233 |
Handle: subject.ObjectHandle, |
| 234 |
Name: subject.Name, |
| 235 |
}, |
| 236 |
CredentialBlob: TPM2BIDObject{ |
| 237 |
Buffer: idObject, |
| 238 |
}, |
| 239 |
Secret: TPM2BEncryptedSecret{ |
| 240 |
Buffer: encSecret, |
| 241 |
}, |
| 242 |
}.Execute(tpm) |
| 243 |
if err != nil { |
| 244 |
t.Fatalf("ActivateCredential() = %v", err) |
| 245 |
} |
| 246 |
|
| 247 |
if !bytes.Equal(activate.CertInfo.Buffer, plaintext) { |
| 248 |
t.Errorf("ActivateCredential() = %x, want %x", activate.CertInfo.Buffer, plaintext) |
| 249 |
} |
| 250 |
}) |
| 251 |
} |
| 252 |
} |