Debian Codegraph @

attest/key_windows.go

raw
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 "io"
24
25 "github.com/google/go-tpm/legacy/tpm2"
26 "github.com/google/go-tpm/tpmutil"
27 )
28
29 // windowsKey20 represents a key bound to a TPM 2.0.
30 type windowsKey20 struct {
31 hnd uintptr
32
33 pcpKeyName string
34 public []byte
35 createData []byte
36 createAttestation []byte
37 createSignature []byte
38 }
39
40 func newWindowsKey20(hnd uintptr, pcpKeyName string, public, createData, createAttest, createSig []byte) ak {
41 return &windowsKey20{
42 hnd: hnd,
43 pcpKeyName: pcpKeyName,
44 public: public,
45 createData: createData,
46 createAttestation: createAttest,
47 createSignature: createSig,
48 }
49 }
50
51 func (k *windowsKey20) marshal() ([]byte, error) {
52 out := serializedKey{
53 Encoding: keyEncodingOSManaged,
54 TPMVersion: 2,
55 Name: k.pcpKeyName,
56
57 Public: k.public,
58 CreateData: k.createData,
59 CreateAttestation: k.createAttestation,
60 CreateSignature: k.createSignature,
61 }
62 return out.Serialize()
63 }
64
65 func (k *windowsKey20) activateCredential(t tpmBase, in EncryptedCredential, ek *EK) ([]byte, error) {
66 tpm, ok := t.(*windowsTPM)
67 if !ok {
68 return nil, fmt.Errorf("expected *windowsTPM, got %T", t)
69 }
70 return tpm.pcp.ActivateCredential(k.hnd, append(in.Credential, in.Secret...))
71 }
72
73 func (k *windowsKey20) quote(tb tpmBase, nonce []byte, alg HashAlg, selectedPCRs []int) (*Quote, error) {
74 t, ok := tb.(*windowsTPM)
75 if !ok {
76 return nil, fmt.Errorf("expected *windowsTPM, got %T", tb)
77 }
78 tpmKeyHnd, err := t.pcp.TPMKeyHandle(k.hnd)
79 if err != nil {
80 return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
81 }
82
83 tpm, err := t.pcp.TPMCommandInterface()
84 if err != nil {
85 return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
86 }
87 return quote20(tpm, tpmKeyHnd, alg.goTPMAlg(), nonce, selectedPCRs)
88 }
89
90 func (k *windowsKey20) close(tpm tpmBase) error {
91 return closeNCryptObject(k.hnd)
92 }
93
94 func (k *windowsKey20) attestationParameters() AttestationParameters {
95 return AttestationParameters{
96 Public: k.public,
97 CreateData: k.createData,
98 CreateAttestation: k.createAttestation,
99 CreateSignature: k.createSignature,
100 }
101 }
102
103 func (k *windowsKey20) certify(tb tpmBase, handle any, _ CertifyOpts) (*CertificationParameters, error) {
104 t, ok := tb.(*windowsTPM)
105 if !ok {
106 return nil, fmt.Errorf("expected *windowsTPM, got %T", tb)
107 }
108 h, ok := handle.(uintptr)
109 if !ok {
110 return nil, fmt.Errorf("expected uinptr, got %T", handle)
111 }
112 hnd, err := t.pcp.TPMKeyHandle(h)
113 if err != nil {
114 return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
115 }
116 akHnd, err := t.pcp.TPMKeyHandle(k.hnd)
117 if err != nil {
118 return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
119 }
120 tpm, err := t.pcp.TPMCommandInterface()
121 if err != nil {
122 return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
123 }
124 scheme := tpm2.SigScheme{
125 Alg: tpm2.AlgRSASSA,
126 Hash: tpm2.AlgSHA1, // PCP-created AK uses SHA1
127 }
128 return certify(tpm, hnd, akHnd, nil, scheme)
129 }
130
131 func getWindowsEndorsementKeyHandle(rwc io.ReadWriter, ek *EK) (tpmutil.Handle, error) {
132 hnd, _, err := getEndorsementKeyHandle(rwc, ek)
133 return hnd, err
134 }
135
136 func (k *windowsKey20) certifyWithDecryptionEk(tb tpmBase, ek *EK, challenge DecryptionEkChallenge) (*CertificationParameters, error) {
137 t, ok := tb.(*windowsTPM)
138 if !ok {
139 return nil, fmt.Errorf("expected *windowsTPM, got %T", tb)
140 }
141 if ek == nil {
142 return nil, fmt.Errorf("ek is nil")
143 }
144 rwc, err := t.pcp.TPMCommandInterface()
145 if err != nil {
146 return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
147 }
148 ekHnd, err := getWindowsEndorsementKeyHandle(rwc, ek)
149 if err != nil {
150 return nil, fmt.Errorf("failed to get EK handle: %v", err)
151 }
152 akHnd, err := t.pcp.TPMKeyHandle(k.hnd)
153 if err != nil {
154 return nil, fmt.Errorf("TPMKeyHandle() failed: %v", err)
155 }
156
157 return certifyAKWithDecryptionEk(rwc, ekHnd, akHnd, k.public, challenge)
158 }
159
160 func (k *windowsKey20) signMsg(tb tpmBase, msg []byte, pub crypto.PublicKey, opts crypto.SignerOpts) ([]byte, error) {
161 return nil, fmt.Errorf("not implemented")
162 }