2019-07-19 20:05:18 +00:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
|
|
|
|
2019-10-08 04:38:05 +00:00
|
|
|
// +build linux,!gofuzz
|
2019-07-19 20:05:18 +00:00
|
|
|
|
|
|
|
package attest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/go-tpm/tpm2"
|
|
|
|
"github.com/google/go-tpm/tpmutil"
|
|
|
|
"github.com/google/go-tspi/attestation"
|
|
|
|
)
|
|
|
|
|
|
|
|
// key12 represents a key bound to a TPM 1.2 device via tcsd.
|
|
|
|
type key12 struct {
|
|
|
|
blob []byte
|
|
|
|
public []byte
|
2019-07-22 16:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func newKey12(blob, public []byte) ak {
|
2019-07-22 16:18:51 +00:00
|
|
|
return &key12{
|
2019-07-30 17:27:57 +00:00
|
|
|
blob: blob,
|
|
|
|
public: public,
|
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal represents the key in a persistent format which may be
|
|
|
|
// loaded at a later time using tpm.LoadKey().
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key12) marshal() ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
out := serializedKey{
|
|
|
|
Encoding: keyEncodingEncrypted,
|
|
|
|
TPMVersion: TPMVersion12,
|
|
|
|
Blob: k.blob,
|
|
|
|
Public: k.public,
|
|
|
|
}
|
|
|
|
return out.Serialize()
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key12) close(tpm *platformTPM) error {
|
2019-07-19 20:05:18 +00:00
|
|
|
return nil // No state for tpm 1.2.
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key12) activateCredential(t *platformTPM, in EncryptedCredential) ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
cred, err := attestation.AIKChallengeResponse(t.ctx, k.blob, in.Credential, in.Secret)
|
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
return nil, fmt.Errorf("failed to activate ak: %v", err)
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
return cred, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key12) quote(t *platformTPM, nonce []byte, alg HashAlg) (*Quote, error) {
|
2019-09-18 17:16:00 +00:00
|
|
|
if alg != HashSHA1 {
|
2019-09-23 19:37:40 +00:00
|
|
|
return nil, fmt.Errorf("only SHA1 algorithms supported on TPM 1.2, not %v", alg)
|
2019-09-18 17:16:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 20:05:18 +00:00
|
|
|
quote, rawSig, err := attestation.GetQuote(t.ctx, k.blob, nonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Quote() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Quote{
|
|
|
|
Version: TPMVersion12,
|
|
|
|
Quote: quote,
|
|
|
|
Signature: rawSig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key12) attestationParameters() AttestationParameters {
|
2019-07-23 22:22:53 +00:00
|
|
|
return AttestationParameters{
|
|
|
|
Public: k.public,
|
|
|
|
UseTCSDActivationFormat: true,
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// key20 represents a key bound to a TPM 2.0.
|
|
|
|
type key20 struct {
|
|
|
|
hnd tpmutil.Handle
|
|
|
|
|
|
|
|
blob []byte
|
|
|
|
public []byte // used by both TPM1.2 and 2.0
|
|
|
|
createData []byte
|
|
|
|
createAttestation []byte
|
|
|
|
createSignature []byte
|
2019-07-22 16:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func newKey20(hnd tpmutil.Handle, blob, public, createData, createAttestation, createSig []byte) ak {
|
2019-07-22 16:18:51 +00:00
|
|
|
return &key20{
|
|
|
|
hnd: hnd,
|
|
|
|
blob: blob,
|
|
|
|
public: public,
|
|
|
|
createData: createData,
|
|
|
|
createAttestation: createAttestation,
|
|
|
|
createSignature: createSig,
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key20) marshal() ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
return (&serializedKey{
|
|
|
|
Encoding: keyEncodingEncrypted,
|
|
|
|
TPMVersion: TPMVersion20,
|
|
|
|
|
|
|
|
Blob: k.blob,
|
|
|
|
Public: k.public,
|
|
|
|
CreateData: k.createData,
|
|
|
|
CreateAttestation: k.createAttestation,
|
|
|
|
CreateSignature: k.createSignature,
|
|
|
|
}).Serialize()
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key20) close(tpm *platformTPM) error {
|
2019-07-19 20:05:18 +00:00
|
|
|
return tpm2.FlushContext(tpm.rwc, k.hnd)
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key20) activateCredential(t *platformTPM, in EncryptedCredential) ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
ekHnd, _, err := t.getPrimaryKeyHandle(commonEkEquivalentHandle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sessHandle, _, err := tpm2.StartAuthSession(
|
|
|
|
t.rwc,
|
|
|
|
tpm2.HandleNull, /*tpmKey*/
|
|
|
|
tpm2.HandleNull, /*bindKey*/
|
|
|
|
make([]byte, 16), /*nonceCaller*/
|
|
|
|
nil, /*secret*/
|
|
|
|
tpm2.SessionPolicy,
|
|
|
|
tpm2.AlgNull,
|
|
|
|
tpm2.AlgSHA256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating session: %v", err)
|
|
|
|
}
|
|
|
|
defer tpm2.FlushContext(t.rwc, sessHandle)
|
|
|
|
|
|
|
|
if _, err := tpm2.PolicySecret(t.rwc, tpm2.HandleEndorsement, tpm2.AuthCommand{Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession}, sessHandle, nil, nil, nil, 0); err != nil {
|
|
|
|
return nil, fmt.Errorf("tpm2.PolicySecret() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpm2.ActivateCredentialUsingAuth(t.rwc, []tpm2.AuthCommand{
|
|
|
|
{Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession},
|
|
|
|
{Session: sessHandle, Attributes: tpm2.AttrContinueSession},
|
|
|
|
}, k.hnd, ekHnd, in.Credential[2:], in.Secret[2:])
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key20) quote(t *platformTPM, nonce []byte, alg HashAlg) (*Quote, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
return quote20(t.rwc, k.hnd, tpm2.Algorithm(alg), nonce)
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
func (k *key20) attestationParameters() AttestationParameters {
|
2019-07-23 22:22:53 +00:00
|
|
|
return AttestationParameters{
|
2019-07-19 20:05:18 +00:00
|
|
|
Public: k.public,
|
|
|
|
CreateData: k.createData,
|
|
|
|
CreateAttestation: k.createAttestation,
|
|
|
|
CreateSignature: k.createSignature,
|
|
|
|
}
|
|
|
|
}
|