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 07:06:24 +00:00
|
|
|
// +build windows
|
2019-07-19 20:05:18 +00:00
|
|
|
|
|
|
|
package attest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
tpm1 "github.com/google/go-tpm/tpm"
|
|
|
|
)
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
// windowsKey12 represents a Windows-managed key on a TPM1.2 TPM.
|
|
|
|
type windowsKey12 struct {
|
2019-07-19 20:05:18 +00:00
|
|
|
hnd uintptr
|
|
|
|
pcpKeyName string
|
|
|
|
public []byte
|
2019-07-22 16:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func newWindowsKey12(hnd uintptr, pcpKeyName string, public []byte) ak {
|
|
|
|
return &windowsKey12{
|
2019-07-22 16:18:51 +00:00
|
|
|
hnd: hnd,
|
|
|
|
pcpKeyName: pcpKeyName,
|
|
|
|
public: public,
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey12) marshal() ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
out := serializedKey{
|
|
|
|
Encoding: keyEncodingOSManaged,
|
|
|
|
TPMVersion: TPMVersion12,
|
|
|
|
Name: k.pcpKeyName,
|
|
|
|
Public: k.public,
|
|
|
|
}
|
|
|
|
return out.Serialize()
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey12) activateCredential(t tpmBase, in EncryptedCredential) ([]byte, error) {
|
2020-05-05 21:56:40 +00:00
|
|
|
tpm, ok := t.(*windowsTPM)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected *windowsTPM, got %T", t)
|
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
secretKey, err := tpm.pcp.ActivateCredential(k.hnd, in.Credential)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return decryptCredential(secretKey, in.Secret)
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey12) quote(tb tpmBase, 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
|
|
|
}
|
2020-05-05 21:56:40 +00:00
|
|
|
t, ok := tb.(*windowsTPM)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected *windowsTPM, got %T", tb)
|
|
|
|
}
|
2019-09-18 17:16:00 +00:00
|
|
|
|
2019-07-19 20:05:18 +00:00
|
|
|
tpmKeyHnd, err := t.pcp.TPMKeyHandle(k.hnd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tpm, err := t.pcp.TPMCommandInterface()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedPCRs := make([]int, 24)
|
|
|
|
for pcr, _ := range selectedPCRs {
|
|
|
|
selectedPCRs[pcr] = pcr
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, pcrc, err := tpm1.Quote(tpm, tpmKeyHnd, nonce, selectedPCRs[:], wellKnownAuth[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Quote() failed: %v", err)
|
|
|
|
}
|
|
|
|
// Construct and return TPM_QUOTE_INFO
|
|
|
|
// Returning TPM_QUOTE_INFO allows us to verify the Quote at a higher resolution
|
|
|
|
// and matches what go-tspi returns.
|
|
|
|
quote, err := tpm1.NewQuoteInfo(nonce, selectedPCRs[:], pcrc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct Quote Info: %v", err)
|
|
|
|
}
|
|
|
|
return &Quote{
|
2019-09-13 20:03:26 +00:00
|
|
|
Version: TPMVersion12,
|
2019-07-19 20:05:18 +00:00
|
|
|
Quote: quote,
|
|
|
|
Signature: sig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey12) close(tpm tpmBase) error {
|
2019-07-19 20:05:18 +00:00
|
|
|
return closeNCryptObject(k.hnd)
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey12) attestationParameters() AttestationParameters {
|
2019-07-23 22:22:53 +00:00
|
|
|
return AttestationParameters{
|
2019-07-19 20:05:18 +00:00
|
|
|
Public: k.public,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
// windowsKey20 represents a key bound to a TPM 2.0.
|
|
|
|
type windowsKey20 struct {
|
2019-07-19 20:05:18 +00:00
|
|
|
hnd uintptr
|
|
|
|
|
|
|
|
pcpKeyName string
|
|
|
|
public []byte
|
|
|
|
createData []byte
|
|
|
|
createAttestation []byte
|
|
|
|
createSignature []byte
|
2019-07-22 16:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func newWindowsKey20(hnd uintptr, pcpKeyName string, public, createData, createAttest, createSig []byte) ak {
|
|
|
|
return &windowsKey20{
|
2019-07-22 16:18:51 +00:00
|
|
|
hnd: hnd,
|
|
|
|
pcpKeyName: pcpKeyName,
|
|
|
|
public: public,
|
|
|
|
createData: createData,
|
|
|
|
createAttestation: createAttest,
|
|
|
|
createSignature: createSig,
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey20) marshal() ([]byte, error) {
|
2019-07-19 20:05:18 +00:00
|
|
|
out := serializedKey{
|
|
|
|
Encoding: keyEncodingOSManaged,
|
|
|
|
TPMVersion: TPMVersion20,
|
|
|
|
Name: k.pcpKeyName,
|
|
|
|
|
|
|
|
Public: k.public,
|
|
|
|
CreateData: k.createData,
|
|
|
|
CreateAttestation: k.createAttestation,
|
|
|
|
CreateSignature: k.createSignature,
|
|
|
|
}
|
|
|
|
return out.Serialize()
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey20) activateCredential(t tpmBase, in EncryptedCredential) ([]byte, error) {
|
2020-05-05 21:56:40 +00:00
|
|
|
tpm, ok := t.(*windowsTPM)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected *windowsTPM, got %T", t)
|
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
return tpm.pcp.ActivateCredential(k.hnd, append(in.Credential, in.Secret...))
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey20) quote(tb tpmBase, nonce []byte, alg HashAlg) (*Quote, error) {
|
2020-05-05 21:56:40 +00:00
|
|
|
t, ok := tb.(*windowsTPM)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected *windowsTPM, got %T", tb)
|
|
|
|
}
|
2019-07-19 20:05:18 +00:00
|
|
|
tpmKeyHnd, err := t.pcp.TPMKeyHandle(k.hnd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tpm, err := t.pcp.TPMCommandInterface()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
|
|
|
|
}
|
2019-09-18 17:16:00 +00:00
|
|
|
return quote20(tpm, tpmKeyHnd, alg.goTPMAlg(), nonce)
|
2019-07-19 20:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey20) close(tpm tpmBase) error {
|
2019-07-19 20:05:18 +00:00
|
|
|
return closeNCryptObject(k.hnd)
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func (k *windowsKey20) 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,
|
|
|
|
}
|
|
|
|
}
|