| 1 |
// Copyright 2019 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
//go:build windows |
| 16 |
// +build windows |
| 17 |
|
| 18 |
package attest |
| 19 |
|
| 20 |
import ( |
| 21 |
"crypto" |
| 22 |
"fmt" |
| 23 |
|
| 24 |
"github.com/google/go-tpm/legacy/tpm2" |
| 25 |
) |
| 26 |
|
| 27 |
// windowsKey20 represents a key bound to a TPM 2.0. |
| 28 |
type windowsKey20 struct { |
| 29 |
hnd uintptr |
| 30 |
|
| 31 |
pcpKeyName string |
| 32 |
public []byte |
| 33 |
createData []byte |
| 34 |
createAttestation []byte |
| 35 |
createSignature []byte |
| 36 |
} |
| 37 |
|
| 38 |
func newWindowsKey20(hnd uintptr, pcpKeyName string, public, createData, createAttest, createSig []byte) ak { |
| 39 |
return &windowsKey20{ |
| 40 |
hnd: hnd, |
| 41 |
pcpKeyName: pcpKeyName, |
| 42 |
public: public, |
| 43 |
createData: createData, |
| 44 |
createAttestation: createAttest, |
| 45 |
createSignature: createSig, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (k *windowsKey20) marshal() ([]byte, error) { |
| 50 |
out := serializedKey{ |
| 51 |
Encoding: keyEncodingOSManaged, |
| 52 |
TPMVersion: 2, |
| 53 |
Name: k.pcpKeyName, |
| 54 |
|
| 55 |
Public: k.public, |
| 56 |
CreateData: k.createData, |
| 57 |
CreateAttestation: k.createAttestation, |
| 58 |
CreateSignature: k.createSignature, |
| 59 |
} |
| 60 |
return out.Serialize() |
| 61 |
} |
| 62 |
|
| 63 |
func (k *windowsKey20) activateCredential(t tpmBase, in EncryptedCredential, ek *EK) ([]byte, error) { |
| 64 |
tpm, ok := t.(*windowsTPM) |
| 65 |
if !ok { |
| 66 |
return nil, fmt.Errorf("expected *windowsTPM, got %T", t) |
| 67 |
} |
| 68 |
return tpm.pcp.ActivateCredential(k.hnd, append(in.Credential, in.Secret...)) |
| 69 |
} |
| 70 |
|
| 71 |
func (k *windowsKey20) quote(tb tpmBase, nonce []byte, alg HashAlg, selectedPCRs []int) (*Quote, error) { |
| 72 |
t, ok := tb.(*windowsTPM) |
| 73 |
if !ok { |
| 74 |
return nil, fmt.Errorf("expected *windowsTPM, got %T", tb) |
| 75 |
} |
| 76 |
tpmKeyHnd, err := t.pcp.TPMKeyHandle(k.hnd) |
| 77 |
if err != nil { |
| 78 |
return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err) |
| 79 |
} |
| 80 |
|
| 81 |
tpm, err := t.pcp.TPMCommandInterface() |
| 82 |
if err != nil { |
| 83 |
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err) |
| 84 |
} |
| 85 |
return quote20(tpm, tpmKeyHnd, alg.goTPMAlg(), nonce, selectedPCRs) |
| 86 |
} |
| 87 |
|
| 88 |
func (k *windowsKey20) close(tpm tpmBase) error { |
| 89 |
return closeNCryptObject(k.hnd) |
| 90 |
} |
| 91 |
|
| 92 |
func (k *windowsKey20) attestationParameters() AttestationParameters { |
| 93 |
return AttestationParameters{ |
| 94 |
Public: k.public, |
| 95 |
CreateData: k.createData, |
| 96 |
CreateAttestation: k.createAttestation, |
| 97 |
CreateSignature: k.createSignature, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func (k *windowsKey20) certify(tb tpmBase, handle any, _ CertifyOpts) (*CertificationParameters, error) { |
| 102 |
t, ok := tb.(*windowsTPM) |
| 103 |
if !ok { |
| 104 |
return nil, fmt.Errorf("expected *windowsTPM, got %T", tb) |
| 105 |
} |
| 106 |
h, ok := handle.(uintptr) |
| 107 |
if !ok { |
| 108 |
return nil, fmt.Errorf("expected uinptr, got %T", handle) |
| 109 |
} |
| 110 |
hnd, err := t.pcp.TPMKeyHandle(h) |
| 111 |
if err != nil { |
| 112 |
return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err) |
| 113 |
} |
| 114 |
akHnd, err := t.pcp.TPMKeyHandle(k.hnd) |
| 115 |
if err != nil { |
| 116 |
return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err) |
| 117 |
} |
| 118 |
tpm, err := t.pcp.TPMCommandInterface() |
| 119 |
if err != nil { |
| 120 |
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err) |
| 121 |
} |
| 122 |
scheme := tpm2.SigScheme{ |
| 123 |
Alg: tpm2.AlgRSASSA, |
| 124 |
Hash: tpm2.AlgSHA1, // PCP-created AK uses SHA1 |
| 125 |
} |
| 126 |
return certify(tpm, hnd, akHnd, nil, scheme) |
| 127 |
} |
| 128 |
|
| 129 |
func (k *windowsKey20) signMsg(tb tpmBase, msg []byte, pub crypto.PublicKey, opts crypto.SignerOpts) ([]byte, error) { |
| 130 |
return nil, fmt.Errorf("not implemented") |
| 131 |
} |