| 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 |
"bytes" |
| 22 |
"crypto/rand" |
| 23 |
"crypto/rsa" |
| 24 |
"encoding/binary" |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"io" |
| 28 |
"math/big" |
| 29 |
|
| 30 |
tpmtbs "github.com/google/go-tpm/tpmutil/tbs" |
| 31 |
"golang.org/x/sys/windows" |
| 32 |
) |
| 33 |
|
| 34 |
var wellKnownAuth [20]byte |
| 35 |
|
| 36 |
type windowsTPM struct { |
| 37 |
pcp *winPCP |
| 38 |
} |
| 39 |
|
| 40 |
func probeSystemTPMs() ([]string, error) { |
| 41 |
// Initialize Tbs.dll here so that it's linked only when TPM support is required. |
| 42 |
if tbs == nil { |
| 43 |
tbs = windows.MustLoadDLL("Tbs.dll") |
| 44 |
tbsGetDeviceInfo = tbs.MustFindProc("Tbsi_GetDeviceInfo") |
| 45 |
} |
| 46 |
|
| 47 |
// Windows systems appear to only support a single abstracted TPM. |
| 48 |
// If we fail to initialize the Platform Crypto Provider, we assume |
| 49 |
// a TPM is not present. |
| 50 |
pcp, err := openPCP() |
| 51 |
if err != nil { |
| 52 |
return nil, nil |
| 53 |
} |
| 54 |
defer pcp.Close() |
| 55 |
|
| 56 |
info, err := pcp.TPMInfo() |
| 57 |
if err != nil { |
| 58 |
return nil, fmt.Errorf("TPMInfo() failed: %v", err) |
| 59 |
} |
| 60 |
|
| 61 |
if info.TBSInfo.TPMVersion != 2 { |
| 62 |
return nil, fmt.Errorf("TBSInfo.TPMVersion %d unsupported", info.TBSInfo.TPMVersion) |
| 63 |
} |
| 64 |
|
| 65 |
return []string{""}, nil |
| 66 |
} |
| 67 |
|
| 68 |
func openTPM(_ string) (*TPM, error) { |
| 69 |
pcp, err := openPCP() |
| 70 |
if err != nil { |
| 71 |
return nil, fmt.Errorf("openPCP() failed: %v", err) |
| 72 |
} |
| 73 |
|
| 74 |
info, err := pcp.TPMInfo() |
| 75 |
if err != nil { |
| 76 |
return nil, fmt.Errorf("TPMInfo() failed: %v", err) |
| 77 |
} |
| 78 |
if info.TBSInfo.TPMVersion != 2 { |
| 79 |
return nil, fmt.Errorf("TBSInfo.TPMVersion %d unsupported", info.TBSInfo.TPMVersion) |
| 80 |
} |
| 81 |
|
| 82 |
return &TPM{tpm: &windowsTPM{ |
| 83 |
pcp: pcp, |
| 84 |
}}, nil |
| 85 |
} |
| 86 |
|
| 87 |
func (t *windowsTPM) close() error { |
| 88 |
return t.pcp.Close() |
| 89 |
} |
| 90 |
|
| 91 |
func (t *windowsTPM) info() (*TPMInfo, error) { |
| 92 |
tInfo := TPMInfo{ |
| 93 |
Interface: TPMInterfaceKernelManaged, |
| 94 |
} |
| 95 |
tpm, err := t.pcp.TPMCommandInterface() |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
|
| 100 |
t2Info, err := readVendorAttributes(tpm) |
| 101 |
if err != nil { |
| 102 |
return nil, err |
| 103 |
} |
| 104 |
tInfo.Manufacturer = t2Info.manufacturer |
| 105 |
tInfo.VendorInfo = t2Info.vendor |
| 106 |
tInfo.FirmwareVersionMajor = t2Info.fwMajor |
| 107 |
tInfo.FirmwareVersionMinor = t2Info.fwMinor |
| 108 |
|
| 109 |
return &tInfo, nil |
| 110 |
} |
| 111 |
|
| 112 |
func (t *windowsTPM) ekCertificates() ([]EK, error) { |
| 113 |
ekCerts, err := t.pcp.EKCerts() |
| 114 |
if err != nil { |
| 115 |
return nil, fmt.Errorf("could not read EKCerts: %v", err) |
| 116 |
} |
| 117 |
var eks []EK |
| 118 |
for _, cert := range ekCerts { |
| 119 |
eks = append(eks, EK{Certificate: cert, Public: cert.PublicKey}) |
| 120 |
} |
| 121 |
return eks, nil |
| 122 |
} |
| 123 |
|
| 124 |
func (t *windowsTPM) eks() ([]EK, error) { |
| 125 |
ekCerts, err := t.pcp.EKCerts() |
| 126 |
if err != nil { |
| 127 |
return nil, fmt.Errorf("could not read EKCerts: %v", err) |
| 128 |
} |
| 129 |
if len(ekCerts) > 0 { |
| 130 |
var eks []EK |
| 131 |
for _, cert := range ekCerts { |
| 132 |
eks = append(eks, EK{Certificate: cert, Public: cert.PublicKey}) |
| 133 |
} |
| 134 |
return eks, nil |
| 135 |
} |
| 136 |
|
| 137 |
pub, err := t.ekPub() |
| 138 |
if err != nil { |
| 139 |
return nil, fmt.Errorf("could not read ek public key from tpm: %v", err) |
| 140 |
} |
| 141 |
ek := EK{Public: pub} |
| 142 |
|
| 143 |
i, err := t.info() |
| 144 |
if err != nil { |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
ek.CertificateURL = ekCertURL(pub, i.Manufacturer.String()) |
| 148 |
return []EK{ek}, nil |
| 149 |
} |
| 150 |
|
| 151 |
func (t *windowsTPM) ekPub() (*rsa.PublicKey, error) { |
| 152 |
p, err := t.pcp.EKPub() |
| 153 |
if err != nil { |
| 154 |
return nil, fmt.Errorf("could not read ekpub: %v", err) |
| 155 |
} |
| 156 |
ekPub, err := decodeWindowsBcryptRSABlob(p) |
| 157 |
if err != nil { |
| 158 |
return nil, fmt.Errorf("could not decode ekpub: %v", err) |
| 159 |
} |
| 160 |
return ekPub, nil |
| 161 |
} |
| 162 |
|
| 163 |
type bcryptRSABlobHeader struct { |
| 164 |
Magic uint32 |
| 165 |
BitLength uint32 |
| 166 |
ExponentLen uint32 |
| 167 |
ModulusLen uint32 |
| 168 |
Prime1Len uint32 |
| 169 |
Prime2Len uint32 |
| 170 |
} |
| 171 |
|
| 172 |
func decodeWindowsBcryptRSABlob(b []byte) (*rsa.PublicKey, error) { |
| 173 |
var ( |
| 174 |
r = &io.LimitedReader{R: bytes.NewReader(b), N: int64(len(b))} |
| 175 |
header = &bcryptRSABlobHeader{} |
| 176 |
) |
| 177 |
if err := binary.Read(r, binary.LittleEndian, header); err != nil { |
| 178 |
return nil, err |
| 179 |
} |
| 180 |
|
| 181 |
if header.Magic != 0x31415352 { // "RSA1" |
| 182 |
return nil, fmt.Errorf("invalid header magic %x", header.Magic) |
| 183 |
} |
| 184 |
if header.ExponentLen > 8 { |
| 185 |
return nil, errors.New("exponent too large") |
| 186 |
} |
| 187 |
|
| 188 |
if int64(header.ExponentLen) > r.N { |
| 189 |
return nil, fmt.Errorf("ExponentLen (%d bytes) larger than remaining capacity (%d bytes)", header.ExponentLen, r.N) |
| 190 |
} |
| 191 |
|
| 192 |
// Allocate an 8-byte buffer to right-align and zero-pad the big-endian exponent |
| 193 |
// so it can be directly decoded with binary.BigEndian.Uint64 (which requires 8 bytes). |
| 194 |
exp := make([]byte, 8) |
| 195 |
if _, err := r.Read(exp[8-header.ExponentLen:]); err != nil { |
| 196 |
return nil, fmt.Errorf("failed to read public exponent: %v", err) |
| 197 |
} |
| 198 |
|
| 199 |
if int64(header.ModulusLen) > r.N { |
| 200 |
return nil, fmt.Errorf("ModulusLen (%d bytes) larger than remaining capacity (%d bytes)", header.ModulusLen, r.N) |
| 201 |
} |
| 202 |
mod := make([]byte, header.ModulusLen) |
| 203 |
if n, err := r.Read(mod); n != int(header.ModulusLen) || err != nil { |
| 204 |
return nil, fmt.Errorf("failed to read modulus (%d, %v)", n, err) |
| 205 |
} |
| 206 |
|
| 207 |
return &rsa.PublicKey{ |
| 208 |
N: new(big.Int).SetBytes(mod), |
| 209 |
E: int(binary.BigEndian.Uint64(exp)), |
| 210 |
}, nil |
| 211 |
} |
| 212 |
|
| 213 |
func (t *windowsTPM) newAK(opts *AKConfig) (*AK, error) { |
| 214 |
var name string |
| 215 |
var alg Algorithm |
| 216 |
|
| 217 |
if opts != nil && opts.Name != "" { |
| 218 |
name = opts.Name |
| 219 |
} else { |
| 220 |
nameHex := make([]byte, 5) |
| 221 |
if n, err := rand.Read(nameHex); err != nil || n != len(nameHex) { |
| 222 |
return nil, fmt.Errorf("rand.Read() failed with %d/%d bytes read and error: %v", n, len(nameHex), err) |
| 223 |
} |
| 224 |
name = fmt.Sprintf("ak-%x", nameHex) |
| 225 |
} |
| 226 |
if opts != nil && opts.Algorithm != "" { |
| 227 |
alg = opts.Algorithm |
| 228 |
} else { |
| 229 |
// Default to RSA based AK. |
| 230 |
alg = RSA |
| 231 |
} |
| 232 |
|
| 233 |
kh, err := t.pcp.NewAK(name, alg) |
| 234 |
if err != nil { |
| 235 |
return nil, fmt.Errorf("pcp failed to mint attestation key: %v", err) |
| 236 |
} |
| 237 |
props, err := t.pcp.AKProperties(kh) |
| 238 |
if err != nil { |
| 239 |
closeNCryptObject(kh) |
| 240 |
return nil, fmt.Errorf("pcp failed to read attestation key properties: %v", err) |
| 241 |
} |
| 242 |
|
| 243 |
return &AK{ak: newWindowsKey20(kh, name, props.RawPublic, props.RawCreationData, props.RawAttest, props.RawSignature)}, nil |
| 244 |
} |
| 245 |
|
| 246 |
func (t *windowsTPM) loadAK(opaqueBlob []byte) (*AK, error) { |
| 247 |
sKey, err := deserializeKey(opaqueBlob) |
| 248 |
if err != nil { |
| 249 |
return nil, fmt.Errorf("deserializeKey() failed: %v", err) |
| 250 |
} |
| 251 |
if sKey.Encoding != keyEncodingOSManaged { |
| 252 |
return nil, fmt.Errorf("unsupported key encoding: %x", sKey.Encoding) |
| 253 |
} |
| 254 |
|
| 255 |
hnd, err := t.pcp.LoadKeyByName(sKey.Name) |
| 256 |
if err != nil { |
| 257 |
return nil, fmt.Errorf("pcp failed to load key: %v", err) |
| 258 |
} |
| 259 |
|
| 260 |
return &AK{ak: newWindowsKey20(hnd, sKey.Name, sKey.Public, sKey.CreateData, sKey.CreateAttestation, sKey.CreateSignature)}, nil |
| 261 |
} |
| 262 |
|
| 263 |
func (t *windowsTPM) loadAKWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*AK, error) { |
| 264 |
return nil, fmt.Errorf("not implemented") |
| 265 |
} |
| 266 |
|
| 267 |
func (t *windowsTPM) newKey(*AK, *KeyConfig) (*Key, error) { |
| 268 |
return nil, fmt.Errorf("not implemented") |
| 269 |
} |
| 270 |
|
| 271 |
func (t *windowsTPM) newKeyCertifiedByKey(ck certifyingKey, opts *KeyConfig) (*Key, error) { |
| 272 |
return nil, fmt.Errorf("not implemented") |
| 273 |
} |
| 274 |
|
| 275 |
func (t *windowsTPM) loadKey(opaqueBlob []byte) (*Key, error) { |
| 276 |
return nil, fmt.Errorf("not implemented") |
| 277 |
} |
| 278 |
|
| 279 |
func (t *windowsTPM) loadKeyWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*Key, error) { |
| 280 |
return nil, fmt.Errorf("not implemented") |
| 281 |
} |
| 282 |
|
| 283 |
func (t *windowsTPM) pcrbanks() ([]HashAlg, error) { |
| 284 |
tpm, err := t.pcp.TPMCommandInterface() |
| 285 |
if err != nil { |
| 286 |
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err) |
| 287 |
} |
| 288 |
return pcrbanks(tpm) |
| 289 |
} |
| 290 |
|
| 291 |
func (t *windowsTPM) pcrs(alg HashAlg) ([]PCR, error) { |
| 292 |
var PCRs map[uint32][]byte |
| 293 |
|
| 294 |
tpm, err := t.pcp.TPMCommandInterface() |
| 295 |
if err != nil { |
| 296 |
return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err) |
| 297 |
} |
| 298 |
PCRs, err = readAllPCRs(tpm, alg.goTPMAlg()) |
| 299 |
if err != nil { |
| 300 |
return nil, fmt.Errorf("failed to read PCRs: %v", err) |
| 301 |
} |
| 302 |
|
| 303 |
out := make([]PCR, len(PCRs)) |
| 304 |
for index, digest := range PCRs { |
| 305 |
digestAlg, err := alg.cryptoHash() |
| 306 |
if err != nil { |
| 307 |
return nil, fmt.Errorf("unknown algorithm ID %x: %v", alg, err) |
| 308 |
} |
| 309 |
out[int(index)] = PCR{ |
| 310 |
Index: int(index), |
| 311 |
Digest: digest, |
| 312 |
DigestAlg: digestAlg, |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return out, nil |
| 317 |
} |
| 318 |
|
| 319 |
func (t *windowsTPM) measurementLog() ([]byte, error) { |
| 320 |
context, err := tpmtbs.CreateContext(tpmtbs.TPMVersion20, tpmtbs.IncludeTPM20) |
| 321 |
if err != nil { |
| 322 |
return nil, err |
| 323 |
} |
| 324 |
defer context.Close() |
| 325 |
|
| 326 |
// Run command first with nil buffer to get required buffer size. |
| 327 |
logLen, err := context.GetTCGLog(nil) |
| 328 |
if err != nil { |
| 329 |
return nil, err |
| 330 |
} |
| 331 |
logBuffer := make([]byte, logLen) |
| 332 |
if _, err = context.GetTCGLog(logBuffer); err != nil { |
| 333 |
return nil, err |
| 334 |
} |
| 335 |
return logBuffer, nil |
| 336 |
} |