| 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 |
package attest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto/rsa" |
| 20 |
"crypto/sha256" |
| 21 |
"crypto/x509" |
| 22 |
"encoding/asn1" |
| 23 |
"encoding/base64" |
| 24 |
"encoding/binary" |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"io" |
| 28 |
"net/url" |
| 29 |
"strings" |
| 30 |
|
| 31 |
"github.com/google/go-tpm/legacy/tpm2" |
| 32 |
"github.com/google/go-tpm/tpmutil" |
| 33 |
) |
| 34 |
|
| 35 |
const ( |
| 36 |
tpmPtManufacturer = 0x00000100 + 5 // PT_FIXED + offset of 5 |
| 37 |
tpmPtVendorString = 0x00000100 + 6 // PT_FIXED + offset of 6 |
| 38 |
tpmPtFwVersion1 = 0x00000100 + 11 // PT_FIXED + offset of 11 |
| 39 |
|
| 40 |
// Defined in "Registry of reserved TPM 2.0 handles and localities". |
| 41 |
nvramRSACertIndex = 0x1c00002 |
| 42 |
nvramRSAEkNonceIndex = 0x1c00003 |
| 43 |
nvramECCCertIndex = 0x1c0000a |
| 44 |
nvramECCEkNonceIndex = 0x1c0000b |
| 45 |
|
| 46 |
// Defined in "Registry of reserved TPM 2.0 handles and localities", and checked on a glinux machine. |
| 47 |
commonRSAEkEquivalentHandle = 0x81010001 |
| 48 |
commonECCEkEquivalentHandle = 0x81010002 |
| 49 |
) |
| 50 |
|
| 51 |
var ( |
| 52 |
akTemplateRSA = tpm2.Public{ |
| 53 |
Type: tpm2.AlgRSA, |
| 54 |
NameAlg: tpm2.AlgSHA256, |
| 55 |
Attributes: tpm2.FlagSignerDefault | tpm2.FlagNoDA, |
| 56 |
RSAParameters: &tpm2.RSAParams{ |
| 57 |
Sign: &tpm2.SigScheme{ |
| 58 |
Alg: tpm2.AlgRSASSA, |
| 59 |
Hash: tpm2.AlgSHA256, |
| 60 |
}, |
| 61 |
KeyBits: 2048, |
| 62 |
}, |
| 63 |
} |
| 64 |
akTemplateECC = tpm2.Public{ |
| 65 |
Type: tpm2.AlgECC, |
| 66 |
NameAlg: tpm2.AlgSHA256, |
| 67 |
Attributes: tpm2.FlagSignerDefault | tpm2.FlagNoDA, |
| 68 |
ECCParameters: &tpm2.ECCParams{ |
| 69 |
Sign: &tpm2.SigScheme{ |
| 70 |
Alg: tpm2.AlgECDSA, |
| 71 |
Hash: tpm2.AlgSHA256, |
| 72 |
}, |
| 73 |
CurveID: tpm2.CurveNISTP256, |
| 74 |
Point: tpm2.ECPoint{ |
| 75 |
XRaw: make([]byte, 32), |
| 76 |
YRaw: make([]byte, 32), |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
defaultRSASRKTemplate = tpm2.Public{ |
| 81 |
Type: tpm2.AlgRSA, |
| 82 |
NameAlg: tpm2.AlgSHA256, |
| 83 |
Attributes: tpm2.FlagStorageDefault | tpm2.FlagNoDA, |
| 84 |
RSAParameters: &tpm2.RSAParams{ |
| 85 |
Symmetric: &tpm2.SymScheme{ |
| 86 |
Alg: tpm2.AlgAES, |
| 87 |
KeyBits: 128, |
| 88 |
Mode: tpm2.AlgCFB, |
| 89 |
}, |
| 90 |
ModulusRaw: make([]byte, 256), |
| 91 |
KeyBits: 2048, |
| 92 |
}, |
| 93 |
} |
| 94 |
defaultECCSRKTemplate = tpm2.Public{ |
| 95 |
Type: tpm2.AlgECC, |
| 96 |
NameAlg: tpm2.AlgSHA256, |
| 97 |
Attributes: tpm2.FlagStorageDefault | tpm2.FlagNoDA, |
| 98 |
ECCParameters: &tpm2.ECCParams{ |
| 99 |
Symmetric: &tpm2.SymScheme{ |
| 100 |
Alg: tpm2.AlgAES, |
| 101 |
KeyBits: 128, |
| 102 |
Mode: tpm2.AlgCFB, |
| 103 |
}, |
| 104 |
CurveID: tpm2.CurveNISTP256, |
| 105 |
Point: tpm2.ECPoint{ |
| 106 |
XRaw: make([]byte, 32), |
| 107 |
YRaw: make([]byte, 32), |
| 108 |
}, |
| 109 |
}, |
| 110 |
} |
| 111 |
// Default RSA and ECC EK templates defined in: |
| 112 |
// https://trustedcomputinggroup.org/wp-content/uploads/Credential_Profile_EK_V2.0_R14_published.pdf |
| 113 |
defaultRSAEKTemplate = tpm2.Public{ |
| 114 |
Type: tpm2.AlgRSA, |
| 115 |
NameAlg: tpm2.AlgSHA256, |
| 116 |
Attributes: tpm2.FlagFixedTPM | tpm2.FlagFixedParent | tpm2.FlagSensitiveDataOrigin | |
| 117 |
tpm2.FlagAdminWithPolicy | tpm2.FlagRestricted | tpm2.FlagDecrypt, |
| 118 |
AuthPolicy: []byte{ |
| 119 |
0x83, 0x71, 0x97, 0x67, 0x44, 0x84, |
| 120 |
0xB3, 0xF8, 0x1A, 0x90, 0xCC, 0x8D, |
| 121 |
0x46, 0xA5, 0xD7, 0x24, 0xFD, 0x52, |
| 122 |
0xD7, 0x6E, 0x06, 0x52, 0x0B, 0x64, |
| 123 |
0xF2, 0xA1, 0xDA, 0x1B, 0x33, 0x14, |
| 124 |
0x69, 0xAA, |
| 125 |
}, |
| 126 |
RSAParameters: &tpm2.RSAParams{ |
| 127 |
Symmetric: &tpm2.SymScheme{ |
| 128 |
Alg: tpm2.AlgAES, |
| 129 |
KeyBits: 128, |
| 130 |
Mode: tpm2.AlgCFB, |
| 131 |
}, |
| 132 |
KeyBits: 2048, |
| 133 |
ModulusRaw: make([]byte, 256), |
| 134 |
}, |
| 135 |
} |
| 136 |
defaultECCEKTemplate = tpm2.Public{ |
| 137 |
Type: tpm2.AlgECC, |
| 138 |
NameAlg: tpm2.AlgSHA256, |
| 139 |
Attributes: tpm2.FlagFixedTPM | tpm2.FlagFixedParent | tpm2.FlagSensitiveDataOrigin | |
| 140 |
tpm2.FlagAdminWithPolicy | tpm2.FlagRestricted | tpm2.FlagDecrypt, |
| 141 |
AuthPolicy: []byte{ |
| 142 |
0x83, 0x71, 0x97, 0x67, 0x44, 0x84, |
| 143 |
0xB3, 0xF8, 0x1A, 0x90, 0xCC, 0x8D, |
| 144 |
0x46, 0xA5, 0xD7, 0x24, 0xFD, 0x52, |
| 145 |
0xD7, 0x6E, 0x06, 0x52, 0x0B, 0x64, |
| 146 |
0xF2, 0xA1, 0xDA, 0x1B, 0x33, 0x14, |
| 147 |
0x69, 0xAA, |
| 148 |
}, |
| 149 |
ECCParameters: &tpm2.ECCParams{ |
| 150 |
Symmetric: &tpm2.SymScheme{ |
| 151 |
Alg: tpm2.AlgAES, |
| 152 |
KeyBits: 128, |
| 153 |
Mode: tpm2.AlgCFB, |
| 154 |
}, |
| 155 |
CurveID: tpm2.CurveNISTP256, |
| 156 |
Point: tpm2.ECPoint{ |
| 157 |
XRaw: make([]byte, 32), |
| 158 |
YRaw: make([]byte, 32), |
| 159 |
}, |
| 160 |
}, |
| 161 |
} |
| 162 |
// Basic template for an ECDSA key signing outside-TPM objects. Other |
| 163 |
// fields are populated depending on the key creation options. |
| 164 |
ecdsaKeyTemplate = tpm2.Public{ |
| 165 |
Type: tpm2.AlgECC, |
| 166 |
Attributes: tpm2.FlagSignerDefault ^ tpm2.FlagRestricted, |
| 167 |
ECCParameters: &tpm2.ECCParams{ |
| 168 |
Sign: &tpm2.SigScheme{ |
| 169 |
Alg: tpm2.AlgECDSA, |
| 170 |
}, |
| 171 |
}, |
| 172 |
} |
| 173 |
// Basic template for an RSA key signing outside-TPM objects. Other |
| 174 |
// fields are populated depending on the key creation options. |
| 175 |
rsaKeyTemplate = tpm2.Public{ |
| 176 |
Type: tpm2.AlgRSA, |
| 177 |
NameAlg: tpm2.AlgSHA256, |
| 178 |
Attributes: tpm2.FlagSignerDefault ^ tpm2.FlagRestricted | tpm2.FlagDecrypt, |
| 179 |
RSAParameters: &tpm2.RSAParams{}, |
| 180 |
} |
| 181 |
) |
| 182 |
|
| 183 |
type tpmInfo struct { |
| 184 |
vendor string |
| 185 |
manufacturer TCGVendorID |
| 186 |
fwMajor int |
| 187 |
fwMinor int |
| 188 |
} |
| 189 |
|
| 190 |
func readVendorAttributes(tpm io.ReadWriter) (tpmInfo, error) { |
| 191 |
var vendorInfo string |
| 192 |
// The Vendor String is split up into 4 sections of 4 bytes, |
| 193 |
// for a maximum length of 16 octets of ASCII text. We iterate |
| 194 |
// through the 4 indexes to get all 16 bytes & construct vendorInfo. |
| 195 |
// See: TPM_PT_VENDOR_STRING_1 in TPM 2.0 Structures reference. |
| 196 |
for i := 0; i < 4; i++ { |
| 197 |
caps, _, err := tpm2.GetCapability(tpm, tpm2.CapabilityTPMProperties, 1, tpmPtVendorString+uint32(i)) |
| 198 |
if err != nil { |
| 199 |
return tpmInfo{}, fmt.Errorf("tpm2.GetCapability(PT_VENDOR_STRING_%d) failed: %v", i+1, err) |
| 200 |
} |
| 201 |
subset, ok := caps[0].(tpm2.TaggedProperty) |
| 202 |
if !ok { |
| 203 |
return tpmInfo{}, fmt.Errorf("got capability of type %T, want tpm2.TaggedProperty", caps[0]) |
| 204 |
} |
| 205 |
// Reconstruct the 4 ASCII octets from the uint32 value. |
| 206 |
b := make([]byte, 4) |
| 207 |
binary.BigEndian.PutUint32(b, subset.Value) |
| 208 |
vendorInfo += string(b) |
| 209 |
} |
| 210 |
|
| 211 |
caps, _, err := tpm2.GetCapability(tpm, tpm2.CapabilityTPMProperties, 1, tpmPtManufacturer) |
| 212 |
if err != nil { |
| 213 |
return tpmInfo{}, fmt.Errorf("tpm2.GetCapability(PT_MANUFACTURER) failed: %v", err) |
| 214 |
} |
| 215 |
manu, ok := caps[0].(tpm2.TaggedProperty) |
| 216 |
if !ok { |
| 217 |
return tpmInfo{}, fmt.Errorf("got capability of type %T, want tpm2.TaggedProperty", caps[0]) |
| 218 |
} |
| 219 |
|
| 220 |
caps, _, err = tpm2.GetCapability(tpm, tpm2.CapabilityTPMProperties, 1, tpmPtFwVersion1) |
| 221 |
if err != nil { |
| 222 |
return tpmInfo{}, fmt.Errorf("tpm2.GetCapability(PT_FIRMWARE_VERSION_1) failed: %v", err) |
| 223 |
} |
| 224 |
fw, ok := caps[0].(tpm2.TaggedProperty) |
| 225 |
if !ok { |
| 226 |
return tpmInfo{}, fmt.Errorf("got capability of type %T, want tpm2.TaggedProperty", caps[0]) |
| 227 |
} |
| 228 |
|
| 229 |
return tpmInfo{ |
| 230 |
vendor: strings.Trim(vendorInfo, "\x00"), |
| 231 |
manufacturer: TCGVendorID(manu.Value), |
| 232 |
fwMajor: int((fw.Value & 0xffff0000) >> 16), |
| 233 |
fwMinor: int(fw.Value & 0x0000ffff), |
| 234 |
}, nil |
| 235 |
} |
| 236 |
|
| 237 |
// ParseEKCertificate parses a raw DER encoded EK certificate blob. |
| 238 |
func ParseEKCertificate(ekCert []byte) (*x509.Certificate, error) { |
| 239 |
var wasWrapped bool |
| 240 |
|
| 241 |
// TCG PC Specific Implementation section 7.3.2 specifies |
| 242 |
// a prefix when storing a certificate in NVRAM. We look |
| 243 |
// for and unwrap the certificate if its present. |
| 244 |
if len(ekCert) > 5 && bytes.Equal(ekCert[:3], []byte{0x10, 0x01, 0x00}) { |
| 245 |
certLen := int(binary.BigEndian.Uint16(ekCert[3:5])) |
| 246 |
if len(ekCert) < certLen+5 { |
| 247 |
return nil, fmt.Errorf("parsing nvram header: ekCert size %d smaller than specified cert length %d", len(ekCert), certLen) |
| 248 |
} |
| 249 |
ekCert = ekCert[5 : 5+certLen] |
| 250 |
wasWrapped = true |
| 251 |
} |
| 252 |
|
| 253 |
// If the cert parses fine without any changes, we are G2G. |
| 254 |
if c, err := x509.ParseCertificate(ekCert); err == nil { |
| 255 |
return c, nil |
| 256 |
} |
| 257 |
// There might be trailing nonsense in the cert, which Go |
| 258 |
// does not parse correctly. As ASN1 data is TLV encoded, we should |
| 259 |
// be able to just get the certificate, and then send that to Go's |
| 260 |
// certificate parser. |
| 261 |
var cert struct { |
| 262 |
Raw asn1.RawContent |
| 263 |
} |
| 264 |
if _, err := asn1.UnmarshalWithParams(ekCert, &cert, "lax"); err != nil { |
| 265 |
return nil, fmt.Errorf("asn1.Unmarshal() failed: %v, wasWrapped=%v", err, wasWrapped) |
| 266 |
} |
| 267 |
|
| 268 |
c, err := x509.ParseCertificate(cert.Raw) |
| 269 |
if err != nil { |
| 270 |
return nil, fmt.Errorf("x509.ParseCertificate() failed: %v", err) |
| 271 |
} |
| 272 |
return c, nil |
| 273 |
} |
| 274 |
|
| 275 |
const ( |
| 276 |
manufacturerIntel = "Intel" |
| 277 |
intelEKCertServiceURL = "https://ekop.intel.com/ekcertservice/" |
| 278 |
) |
| 279 |
|
| 280 |
func intelEKURL(ekPub *rsa.PublicKey) string { |
| 281 |
pubHash := sha256.New() |
| 282 |
pubHash.Write(ekPub.N.Bytes()) |
| 283 |
pubHash.Write([]byte{0x1, 0x00, 0x01}) |
| 284 |
|
| 285 |
return intelEKCertServiceURL + url.QueryEscape(base64.URLEncoding.EncodeToString(pubHash.Sum(nil))) |
| 286 |
} |
| 287 |
|
| 288 |
const ( |
| 289 |
manufacturerAMD = "AMD" |
| 290 |
amdEKCertServiceURL = "https://ftpm.amd.com/pki/aia/" |
| 291 |
) |
| 292 |
|
| 293 |
func amdEKURL(ekPub *rsa.PublicKey) string { |
| 294 |
pubHash := sha256.New() |
| 295 |
pubHash.Write([]byte{0x00, 0x00, 0x22, 0x22}) |
| 296 |
expBytes := make([]byte, 4) |
| 297 |
binary.BigEndian.PutUint32(expBytes, uint32(ekPub.E)) |
| 298 |
pubHash.Write(expBytes) |
| 299 |
pubHash.Write(ekPub.N.Bytes()) |
| 300 |
return amdEKCertServiceURL + url.QueryEscape(fmt.Sprintf("%X", pubHash.Sum(nil)[0:16])) |
| 301 |
} |
| 302 |
|
| 303 |
func ekCertURL(ekPub *rsa.PublicKey, manufacturer string) string { |
| 304 |
var CertURL string |
| 305 |
switch manufacturer { |
| 306 |
case manufacturerIntel: |
| 307 |
CertURL = intelEKURL(ekPub) |
| 308 |
case manufacturerAMD: |
| 309 |
CertURL = amdEKURL(ekPub) |
| 310 |
} |
| 311 |
return CertURL |
| 312 |
} |
| 313 |
|
| 314 |
func readEKCertFromNVRAM20(tpm io.ReadWriter, nvramCertIndex tpmutil.Handle) (*x509.Certificate, error) { |
| 315 |
// By passing nvramCertIndex as our auth handle we're using the NV index |
| 316 |
// itself as the auth hierarchy, which is the same approach |
| 317 |
// tpm2_getekcertificate takes. |
| 318 |
ekCert, err := tpm2.NVReadEx(tpm, nvramCertIndex, nvramCertIndex, "", 0) |
| 319 |
if err != nil { |
| 320 |
return nil, fmt.Errorf("reading EK cert: %v", err) |
| 321 |
} |
| 322 |
return ParseEKCertificate(ekCert) |
| 323 |
} |
| 324 |
|
| 325 |
func quote20(tpm io.ReadWriter, akHandle tpmutil.Handle, hashAlg tpm2.Algorithm, nonce []byte, selectedPCRs []int) (*Quote, error) { |
| 326 |
sel := tpm2.PCRSelection{Hash: hashAlg, |
| 327 |
PCRs: selectedPCRs} |
| 328 |
|
| 329 |
quote, sig, err := tpm2.Quote(tpm, akHandle, "", "", nonce, sel, tpm2.AlgNull) |
| 330 |
if err != nil { |
| 331 |
return nil, err |
| 332 |
} |
| 333 |
|
| 334 |
rawSig, err := sig.Encode() |
| 335 |
return &Quote{ |
| 336 |
Quote: quote, |
| 337 |
Signature: rawSig, |
| 338 |
}, err |
| 339 |
} |
| 340 |
|
| 341 |
func pcrbanks(tpm io.ReadWriter) ([]HashAlg, error) { |
| 342 |
vals, _, err := tpm2.GetCapability(tpm, tpm2.CapabilityPCRs, 1024, 0) |
| 343 |
if err != nil { |
| 344 |
return nil, fmt.Errorf("failed to get TPM available PCR banks: %w", err) |
| 345 |
} |
| 346 |
|
| 347 |
var hAlgs []HashAlg |
| 348 |
var errs error |
| 349 |
for i, v := range vals { |
| 350 |
pcrb, ok := v.(tpm2.PCRSelection) |
| 351 |
if !ok { |
| 352 |
errs = errors.Join(errs, fmt.Errorf("failed to convert value %d to tpm2.PCRSelection: %v", i, v)) |
| 353 |
continue |
| 354 |
} |
| 355 |
|
| 356 |
if len(pcrb.PCRs) == 0 { |
| 357 |
// ignore empty PCR banks. |
| 358 |
continue |
| 359 |
} |
| 360 |
hAlgs = append(hAlgs, HashAlg(pcrb.Hash)) |
| 361 |
} |
| 362 |
|
| 363 |
return hAlgs, errs |
| 364 |
} |
| 365 |
|
| 366 |
func readAllPCRs(tpm io.ReadWriter, alg tpm2.Algorithm) (map[uint32][]byte, error) { |
| 367 |
numPCRs := 24 |
| 368 |
out := map[uint32][]byte{} |
| 369 |
|
| 370 |
// The TPM 2.0 spec says that the TPM can partially fulfill the |
| 371 |
// request. As such, we repeat the command up to 24 times to get all |
| 372 |
// 24 PCRs. |
| 373 |
for i := 0; i < numPCRs; i++ { |
| 374 |
// Build a selection structure, specifying all PCRs we do |
| 375 |
// not have the value for. |
| 376 |
sel := tpm2.PCRSelection{Hash: alg} |
| 377 |
for pcr := 0; pcr < numPCRs; pcr++ { |
| 378 |
if _, present := out[uint32(pcr)]; !present { |
| 379 |
sel.PCRs = append(sel.PCRs, pcr) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// Ask the TPM for those PCR values. |
| 384 |
ret, err := tpm2.ReadPCRs(tpm, sel) |
| 385 |
if err != nil { |
| 386 |
return nil, fmt.Errorf("tpm2.ReadPCRs(%+v) failed with err: %v", sel, err) |
| 387 |
} |
| 388 |
// Keep track of the PCRs we were actually given. |
| 389 |
for pcr, digest := range ret { |
| 390 |
out[uint32(pcr)] = digest |
| 391 |
} |
| 392 |
if len(out) == numPCRs { |
| 393 |
break |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if len(out) != numPCRs { |
| 398 |
return nil, fmt.Errorf("failed to read all PCRs, only read %d", len(out)) |
| 399 |
} |
| 400 |
|
| 401 |
return out, nil |
| 402 |
} |
| 403 |
|
| 404 |
// tpmBase defines the implementation of a TPM invariant. |
| 405 |
type tpmBase interface { |
| 406 |
close() error |
| 407 |
eks() ([]EK, error) |
| 408 |
ekCertificates() ([]EK, error) |
| 409 |
info() (*TPMInfo, error) |
| 410 |
pcrbanks() ([]HashAlg, error) |
| 411 |
|
| 412 |
loadAK(opaqueBlob []byte) (*AK, error) |
| 413 |
loadAKWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*AK, error) |
| 414 |
newAK(opts *AKConfig) (*AK, error) |
| 415 |
loadKey(opaqueBlob []byte) (*Key, error) |
| 416 |
loadKeyWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*Key, error) |
| 417 |
newKey(ak *AK, opts *KeyConfig) (*Key, error) |
| 418 |
newKeyCertifiedByKey(ck certifyingKey, opts *KeyConfig) (*Key, error) |
| 419 |
pcrs(alg HashAlg) ([]PCR, error) |
| 420 |
measurementLog() ([]byte, error) |
| 421 |
} |
| 422 |
|
| 423 |
// TPM interfaces with a TPM device on the system. |
| 424 |
type TPM struct { |
| 425 |
// tpm refers to a concrete implementation of TPM logic, based on the current |
| 426 |
// platform and TPM version. |
| 427 |
tpm tpmBase |
| 428 |
} |
| 429 |
|
| 430 |
// Close shuts down the connection to the TPM. |
| 431 |
func (t *TPM) Close() error { |
| 432 |
return t.tpm.close() |
| 433 |
} |
| 434 |
|
| 435 |
// EKs returns the endorsement keys burned-in to the platform. |
| 436 |
// Note for Linux clients: for historical reasons, the method assumes that |
| 437 |
// the TPM has a single EK, and the EK's type is RSA. If the EK's type is ECC |
| 438 |
// and the TPM contains an ECC EK Certificate, the EKCertificates() method |
| 439 |
// should be used to retrieve the EKs. |
| 440 |
func (t *TPM) EKs() ([]EK, error) { |
| 441 |
return t.tpm.eks() |
| 442 |
} |
| 443 |
|
| 444 |
// EKCertificates returns the endorsement key certificates burned-in to the platform. |
| 445 |
// It is guaranteed that each EK.Certificate field will be populated. |
| 446 |
func (t *TPM) EKCertificates() ([]EK, error) { |
| 447 |
return t.tpm.ekCertificates() |
| 448 |
} |
| 449 |
|
| 450 |
// Info returns information about the TPM. |
| 451 |
func (t *TPM) Info() (*TPMInfo, error) { |
| 452 |
return t.tpm.info() |
| 453 |
} |
| 454 |
|
| 455 |
// LoadAK loads a previously-created ak into the TPM for use. |
| 456 |
// A key loaded via this function needs to be closed with .Close(). |
| 457 |
// Only blobs generated by calling AK.Marshal() are valid parameters |
| 458 |
// to this function. |
| 459 |
func (t *TPM) LoadAK(opaqueBlob []byte) (*AK, error) { |
| 460 |
return t.tpm.loadAK(opaqueBlob) |
| 461 |
} |
| 462 |
|
| 463 |
// LoadAKWithParent loads a previously-created ak into the TPM |
| 464 |
// under the given parent for use. |
| 465 |
func (t *TPM) LoadAKWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*AK, error) { |
| 466 |
return t.tpm.loadAKWithParent(opaqueBlob, parent) |
| 467 |
} |
| 468 |
|
| 469 |
// MeasurementLog returns the present value of the System Measurement Log. |
| 470 |
// |
| 471 |
// This is a low-level API. Consumers seeking to attest the state of the |
| 472 |
// platform should use tpm.AttestPlatform() instead. |
| 473 |
func (t *TPM) MeasurementLog() ([]byte, error) { |
| 474 |
el, err := t.tpm.measurementLog() |
| 475 |
if err != nil { |
| 476 |
return nil, err |
| 477 |
} |
| 478 |
|
| 479 |
// A valid event log contains at least one SpecID event header (28 bytes). |
| 480 |
if minValidSize := 28; len(el) < minValidSize { |
| 481 |
return nil, fmt.Errorf("event log too short: %d < %d", len(el), minValidSize) |
| 482 |
} |
| 483 |
return el, nil |
| 484 |
} |
| 485 |
|
| 486 |
// NewAK creates an attestation key. |
| 487 |
func (t *TPM) NewAK(opts *AKConfig) (*AK, error) { |
| 488 |
return t.tpm.newAK(opts) |
| 489 |
} |
| 490 |
|
| 491 |
// Return a KeyConfig with default values if appropriate. It never modifies the |
| 492 |
// incoming pointer. |
| 493 |
func optsSetDefault(opts *KeyConfig) *KeyConfig { |
| 494 |
if opts == nil { |
| 495 |
return defaultConfig |
| 496 |
} |
| 497 |
if opts.Algorithm == "" && opts.Size == 0 { |
| 498 |
optsCopy := *opts |
| 499 |
optsCopy.Algorithm = defaultConfig.Algorithm |
| 500 |
optsCopy.Size = defaultConfig.Size |
| 501 |
return &optsCopy |
| 502 |
} |
| 503 |
return opts |
| 504 |
} |
| 505 |
|
| 506 |
// NewKey creates an application key certified by the attestation key. If opts is nil |
| 507 |
// then DefaultConfig is used. |
| 508 |
func (t *TPM) NewKey(ak *AK, opts *KeyConfig) (*Key, error) { |
| 509 |
opts = optsSetDefault(opts) |
| 510 |
return t.tpm.newKey(ak, opts) |
| 511 |
} |
| 512 |
|
| 513 |
// NewKeyCertifiedByKey creates an application key certified by |
| 514 |
// the attestation key. Unlike NewKey(), this method does not require |
| 515 |
// an attest.AK object and only requires the AK handle and its algorithm. |
| 516 |
// Thus it can be used in cases where the attestation key was not created |
| 517 |
// by go-attestation library. If opts is nil then DefaultConfig is used. |
| 518 |
func (t *TPM) NewKeyCertifiedByKey(akHandle tpmutil.Handle, akAlg Algorithm, opts *KeyConfig) (*Key, error) { |
| 519 |
opts = optsSetDefault(opts) |
| 520 |
ck := certifyingKey{handle: akHandle, alg: akAlg} |
| 521 |
return t.tpm.newKeyCertifiedByKey(ck, opts) |
| 522 |
} |
| 523 |
|
| 524 |
// LoadKey loads a previously-created application key into the TPM for use. |
| 525 |
// A key loaded via this function needs to be closed with .Close(). |
| 526 |
// Only blobs generated by calling Key.Marshal() are valid parameters |
| 527 |
// to this function. |
| 528 |
func (t *TPM) LoadKey(opaqueBlob []byte) (*Key, error) { |
| 529 |
return t.tpm.loadKey(opaqueBlob) |
| 530 |
} |
| 531 |
|
| 532 |
// PCRs returns the present value of Platform Configuration Registers with |
| 533 |
// the given digest algorithm. |
| 534 |
// |
| 535 |
// This is a low-level API. Consumers seeking to attest the state of the |
| 536 |
// platform should use tpm.AttestPlatform() instead. |
| 537 |
func (t *TPM) PCRs(alg HashAlg) ([]PCR, error) { |
| 538 |
return t.tpm.pcrs(alg) |
| 539 |
} |
| 540 |
|
| 541 |
// PCRBanks returns the list of supported PCR banks on the TPM. |
| 542 |
// |
| 543 |
// This is a low-level API. Consumers seeking to attest the state of the |
| 544 |
// platform should use tpm.AttestPlatform() instead. |
| 545 |
func (t *TPM) PCRBanks() ([]HashAlg, error) { |
| 546 |
return t.tpm.pcrbanks() |
| 547 |
} |
| 548 |
|
| 549 |
func (t *TPM) attestPCRs(ak *AK, nonce []byte, alg HashAlg) (*Quote, []PCR, error) { |
| 550 |
pcrs, err := t.PCRs(alg) |
| 551 |
if err != nil { |
| 552 |
return nil, nil, fmt.Errorf("failed to read %v PCRs: %v", alg, err) |
| 553 |
} |
| 554 |
|
| 555 |
quote, err := ak.Quote(t, nonce, alg) |
| 556 |
if err != nil { |
| 557 |
return nil, nil, fmt.Errorf("failed to quote using %v: %v", alg, err) |
| 558 |
} |
| 559 |
|
| 560 |
// Make sure that the pcrs and quote values are consistent. See details in Section 17.6.2 of |
| 561 |
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_TPM2_r1p59_Part1_Architecture_pub.pdf |
| 562 |
pub, err := ParseAKPublic(ak.AttestationParameters().Public) |
| 563 |
if err != nil { |
| 564 |
return nil, nil, fmt.Errorf("failed to parse AK public: %v", err) |
| 565 |
} |
| 566 |
if err := pub.Verify(*quote, pcrs, nonce); err != nil { |
| 567 |
return nil, nil, fmt.Errorf("local quote verification failed: %v", err) |
| 568 |
} |
| 569 |
|
| 570 |
return quote, pcrs, nil |
| 571 |
} |
| 572 |
|
| 573 |
func (t *TPM) attestPlatform(ak *AK, nonce []byte, eventLog []byte) (*PlatformParameters, error) { |
| 574 |
out := PlatformParameters{ |
| 575 |
Public: ak.AttestationParameters().Public, |
| 576 |
EventLog: eventLog, |
| 577 |
} |
| 578 |
|
| 579 |
algs, err := t.PCRBanks() |
| 580 |
if err != nil { |
| 581 |
return nil, fmt.Errorf("failed to get PCR banks: %w", err) |
| 582 |
} |
| 583 |
|
| 584 |
var lastErr error |
| 585 |
for _, alg := range algs { |
| 586 |
quote, pcrs, err := t.attestPCRs(ak, nonce, alg) |
| 587 |
if err != nil { |
| 588 |
lastErr = err |
| 589 |
continue |
| 590 |
} |
| 591 |
out.Quotes = append(out.Quotes, *quote) |
| 592 |
out.PCRs = append(out.PCRs, pcrs...) |
| 593 |
} |
| 594 |
|
| 595 |
if len(out.Quotes) == 0 { |
| 596 |
return nil, lastErr |
| 597 |
} |
| 598 |
return &out, nil |
| 599 |
} |
| 600 |
|
| 601 |
// PlatformAttestConfig configures how attestations are generated through |
| 602 |
// tpm.AttestPlatform(). |
| 603 |
type PlatformAttestConfig struct { |
| 604 |
// If non-nil, the raw event log will be read from EventLog |
| 605 |
// instead of being obtained from the running system. |
| 606 |
EventLog []byte |
| 607 |
} |
| 608 |
|
| 609 |
// AttestPlatform computes the set of information necessary to attest the |
| 610 |
// state of the platform. For TPM 2.0 devices, AttestPlatform will attempt |
| 611 |
// to read all supported PCR banks and quote all of them, so bugs in |
| 612 |
// platform firmware which break replay for one PCR bank can be mitigated |
| 613 |
// using any other. |
| 614 |
// The provided config, if not nil, can be used to configure aspects of the |
| 615 |
// platform attestation. |
| 616 |
func (t *TPM) AttestPlatform(ak *AK, nonce []byte, config *PlatformAttestConfig) (*PlatformParameters, error) { |
| 617 |
if config == nil { |
| 618 |
config = &PlatformAttestConfig{} |
| 619 |
} |
| 620 |
|
| 621 |
var el []byte |
| 622 |
if config.EventLog != nil { |
| 623 |
el = config.EventLog |
| 624 |
} else { |
| 625 |
var err error |
| 626 |
if el, err = t.MeasurementLog(); err != nil { |
| 627 |
return nil, fmt.Errorf("failed to read event log: %v", err) |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
return t.attestPlatform(ak, nonce, el) |
| 632 |
} |