| 1 |
// Copyright (c) 2014, Google LLC All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of 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, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package tpm supports direct communication with a tpm device under Linux. |
| 16 |
package tpm |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"crypto" |
| 21 |
"crypto/aes" |
| 22 |
"crypto/cipher" |
| 23 |
"crypto/hmac" |
| 24 |
"crypto/rand" |
| 25 |
"crypto/rsa" |
| 26 |
"crypto/sha1" |
| 27 |
"encoding/binary" |
| 28 |
"errors" |
| 29 |
"fmt" |
| 30 |
"io" |
| 31 |
|
| 32 |
"github.com/google/go-tpm/tpmutil" |
| 33 |
) |
| 34 |
|
| 35 |
// GetKeys gets the list of handles for currently-loaded TPM keys. |
| 36 |
func GetKeys(rw io.ReadWriter) ([]tpmutil.Handle, error) { |
| 37 |
b, err := getCapability(rw, CapHandle, rtKey) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
var handles []tpmutil.Handle |
| 42 |
if _, err := tpmutil.Unpack(b, &handles); err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
return handles, err |
| 46 |
} |
| 47 |
|
| 48 |
// PcrExtend extends a value into the right PCR by index. |
| 49 |
func PcrExtend(rw io.ReadWriter, pcrIndex uint32, pcr pcrValue) ([]byte, error) { |
| 50 |
in := []interface{}{pcrIndex, pcr} |
| 51 |
var d pcrValue |
| 52 |
out := []interface{}{&d} |
| 53 |
if _, err := submitTPMRequest(rw, tagRQUCommand, ordExtend, in, out); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
return d[:], nil |
| 58 |
} |
| 59 |
|
| 60 |
// ReadPCR reads a PCR value from the TPM. |
| 61 |
func ReadPCR(rw io.ReadWriter, pcrIndex uint32) ([]byte, error) { |
| 62 |
in := []interface{}{pcrIndex} |
| 63 |
var v pcrValue |
| 64 |
out := []interface{}{&v} |
| 65 |
// There's no need to check the ret value here, since the err value contains |
| 66 |
// all the necessary information. |
| 67 |
if _, err := submitTPMRequest(rw, tagRQUCommand, ordPCRRead, in, out); err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
return v[:], nil |
| 72 |
} |
| 73 |
|
| 74 |
// FetchPCRValues gets a given sequence of PCR values. |
| 75 |
func FetchPCRValues(rw io.ReadWriter, pcrVals []int) ([]byte, error) { |
| 76 |
var pcrs []byte |
| 77 |
for _, v := range pcrVals { |
| 78 |
pcr, err := ReadPCR(rw, uint32(v)) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
pcrs = append(pcrs, pcr...) |
| 84 |
} |
| 85 |
|
| 86 |
return pcrs, nil |
| 87 |
} |
| 88 |
|
| 89 |
// GetRandom gets random bytes from the TPM. |
| 90 |
func GetRandom(rw io.ReadWriter, size uint32) ([]byte, error) { |
| 91 |
var b tpmutil.U32Bytes |
| 92 |
in := []interface{}{size} |
| 93 |
out := []interface{}{&b} |
| 94 |
// There's no need to check the ret value here, since the err value |
| 95 |
// contains all the necessary information. |
| 96 |
if _, err := submitTPMRequest(rw, tagRQUCommand, ordGetRandom, in, out); err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
|
| 100 |
return b, nil |
| 101 |
} |
| 102 |
|
| 103 |
// LoadKey2 loads a key blob (a serialized TPM_KEY or TPM_KEY12) into the TPM |
| 104 |
// and returns a handle for this key. |
| 105 |
func LoadKey2(rw io.ReadWriter, keyBlob []byte, srkAuth []byte) (tpmutil.Handle, error) { |
| 106 |
// Deserialize the keyBlob as a key |
| 107 |
var k key |
| 108 |
if _, err := tpmutil.Unpack(keyBlob, &k); err != nil { |
| 109 |
return 0, err |
| 110 |
} |
| 111 |
|
| 112 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 113 |
// command and getting back a secret and a handle. LoadKey2 needs an |
| 114 |
// OSAP session for the SRK because the private part of a TPM_KEY or |
| 115 |
// TPM_KEY12 is sealed against the SRK. |
| 116 |
sharedSecret, osapr, err := newOSAPSession(rw, etSRK, khSRK, srkAuth) |
| 117 |
if err != nil { |
| 118 |
return 0, err |
| 119 |
} |
| 120 |
defer osapr.Close(rw) |
| 121 |
defer zeroBytes(sharedSecret[:]) |
| 122 |
|
| 123 |
authIn := []interface{}{ordLoadKey2, k} |
| 124 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 125 |
if err != nil { |
| 126 |
return 0, err |
| 127 |
} |
| 128 |
|
| 129 |
handle, ra, ret, err := loadKey2(rw, &k, ca) |
| 130 |
if err != nil { |
| 131 |
return 0, err |
| 132 |
} |
| 133 |
|
| 134 |
// Check the response authentication. |
| 135 |
raIn := []interface{}{ret, ordLoadKey2} |
| 136 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 137 |
return 0, err |
| 138 |
} |
| 139 |
|
| 140 |
return handle, nil |
| 141 |
} |
| 142 |
|
| 143 |
// Quote2 performs a quote operation on the TPM for the given data, |
| 144 |
// under the key associated with the handle and for the pcr values |
| 145 |
// specified in the call. |
| 146 |
func Quote2(rw io.ReadWriter, handle tpmutil.Handle, data []byte, pcrVals []int, addVersion byte, aikAuth []byte) ([]byte, error) { |
| 147 |
// Run OSAP for the handle, reading a random OddOSAP for our initial |
| 148 |
// command and getting back a secret and a response. |
| 149 |
sharedSecret, osapr, err := newOSAPSession(rw, etKeyHandle, handle, aikAuth) |
| 150 |
if err != nil { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
defer osapr.Close(rw) |
| 154 |
defer zeroBytes(sharedSecret[:]) |
| 155 |
|
| 156 |
// Hash the data to get the value to pass to quote2. |
| 157 |
hash := sha1.Sum(data) |
| 158 |
pcrSel, err := newPCRSelection(pcrVals) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
authIn := []interface{}{ordQuote2, hash, pcrSel, addVersion} |
| 163 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 164 |
if err != nil { |
| 165 |
return nil, err |
| 166 |
} |
| 167 |
|
| 168 |
// TODO(tmroeder): use the returned CapVersion. |
| 169 |
pcrShort, _, capBytes, sig, ra, ret, err := quote2(rw, handle, hash, pcrSel, addVersion, ca) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
|
| 174 |
// Check response authentication. |
| 175 |
raIn := []interface{}{ret, ordQuote2, pcrShort, tpmutil.U32Bytes(capBytes), tpmutil.U32Bytes(sig)} |
| 176 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
|
| 180 |
return sig, nil |
| 181 |
} |
| 182 |
|
| 183 |
// GetPubKey retrieves an opaque blob containing a public key corresponding to |
| 184 |
// a handle from the TPM. |
| 185 |
func GetPubKey(rw io.ReadWriter, keyHandle tpmutil.Handle, srkAuth []byte) ([]byte, error) { |
| 186 |
// Run OSAP for the handle, reading a random OddOSAP for our initial |
| 187 |
// command and getting back a secret and a response. |
| 188 |
sharedSecret, osapr, err := newOSAPSession(rw, etKeyHandle, keyHandle, srkAuth) |
| 189 |
if err != nil { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
defer osapr.Close(rw) |
| 193 |
defer zeroBytes(sharedSecret[:]) |
| 194 |
|
| 195 |
authIn := []interface{}{ordGetPubKey} |
| 196 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
|
| 201 |
pk, ra, ret, err := getPubKey(rw, keyHandle, ca) |
| 202 |
if err != nil { |
| 203 |
return nil, err |
| 204 |
} |
| 205 |
|
| 206 |
// Check response authentication for TPM_GetPubKey. |
| 207 |
raIn := []interface{}{ret, ordGetPubKey, pk} |
| 208 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 209 |
return nil, err |
| 210 |
} |
| 211 |
|
| 212 |
b, err := tpmutil.Pack(*pk) |
| 213 |
if err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
return b, err |
| 217 |
} |
| 218 |
|
| 219 |
// newOSAPSession starts a new OSAP session and derives a shared key from it. |
| 220 |
func newOSAPSession(rw io.ReadWriter, entityType uint16, entityValue tpmutil.Handle, srkAuth []byte) ([20]byte, *osapResponse, error) { |
| 221 |
osapc := &osapCommand{ |
| 222 |
EntityType: entityType, |
| 223 |
EntityValue: entityValue, |
| 224 |
} |
| 225 |
|
| 226 |
var sharedSecret [20]byte |
| 227 |
if _, err := rand.Read(osapc.OddOSAP[:]); err != nil { |
| 228 |
return sharedSecret, nil, err |
| 229 |
} |
| 230 |
|
| 231 |
osapr, err := osap(rw, osapc) |
| 232 |
if err != nil { |
| 233 |
return sharedSecret, nil, err |
| 234 |
} |
| 235 |
|
| 236 |
// A shared secret is computed as |
| 237 |
// |
| 238 |
// sharedSecret = HMAC-SHA1(srkAuth, evenosap||oddosap) |
| 239 |
// |
| 240 |
// where srkAuth is the hash of the SRK authentication (which hash is all 0s |
| 241 |
// for the well-known SRK auth value) and even and odd OSAP are the |
| 242 |
// values from the OSAP protocol. |
| 243 |
osapData, err := tpmutil.Pack(osapr.EvenOSAP, osapc.OddOSAP) |
| 244 |
if err != nil { |
| 245 |
return sharedSecret, nil, err |
| 246 |
} |
| 247 |
|
| 248 |
hm := hmac.New(sha1.New, srkAuth) |
| 249 |
hm.Write(osapData) |
| 250 |
// Note that crypto/hash.Sum returns a slice rather than an array, so we |
| 251 |
// have to copy this into an array to make sure that serialization doesn't |
| 252 |
// prepend a length in tpmutil.Pack(). |
| 253 |
sharedSecretBytes := hm.Sum(nil) |
| 254 |
copy(sharedSecret[:], sharedSecretBytes) |
| 255 |
return sharedSecret, osapr, nil |
| 256 |
} |
| 257 |
|
| 258 |
// newCommandAuth creates a new commandAuth structure over the given |
| 259 |
// parameters, using the given secret and the given odd nonce, if provided, |
| 260 |
// for the HMAC. If no odd nonce is provided, one is randomly generated. |
| 261 |
func newCommandAuth(authHandle tpmutil.Handle, nonceEven Nonce, nonceOdd *Nonce, key []byte, params []interface{}) (*commandAuth, error) { |
| 262 |
// Auth = HMAC-SHA1(key, SHA1(params) || NonceEven || NonceOdd || ContSession) |
| 263 |
digestBytes, err := tpmutil.Pack(params...) |
| 264 |
if err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
digest := sha1.Sum(digestBytes) |
| 268 |
|
| 269 |
// Use the passed-in nonce if non-nil, otherwise generate it now. |
| 270 |
var odd Nonce |
| 271 |
if nonceOdd != nil { |
| 272 |
odd = *nonceOdd |
| 273 |
} else { |
| 274 |
if _, err := rand.Read(odd[:]); err != nil { |
| 275 |
return nil, err |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
ca := &commandAuth{ |
| 280 |
AuthHandle: authHandle, |
| 281 |
NonceOdd: odd, |
| 282 |
} |
| 283 |
|
| 284 |
authBytes, err := tpmutil.Pack(digest, nonceEven, ca.NonceOdd, ca.ContSession) |
| 285 |
if err != nil { |
| 286 |
return nil, err |
| 287 |
} |
| 288 |
|
| 289 |
hm2 := hmac.New(sha1.New, key) |
| 290 |
hm2.Write(authBytes) |
| 291 |
auth := hm2.Sum(nil) |
| 292 |
copy(ca.Auth[:], auth[:]) |
| 293 |
return ca, nil |
| 294 |
} |
| 295 |
|
| 296 |
// verify checks that the response authentication was correct. |
| 297 |
// It computes the SHA1 of params, and computes the HMAC-SHA1 of this digest |
| 298 |
// with the authentication parameters of ra along with the given odd nonce. |
| 299 |
func (ra *responseAuth) verify(nonceOdd Nonce, key []byte, params []interface{}) error { |
| 300 |
// Auth = HMAC-SHA1(key, SHA1(params) || ra.NonceEven || NonceOdd || ra.ContSession) |
| 301 |
digestBytes, err := tpmutil.Pack(params...) |
| 302 |
if err != nil { |
| 303 |
return err |
| 304 |
} |
| 305 |
|
| 306 |
digest := sha1.Sum(digestBytes) |
| 307 |
authBytes, err := tpmutil.Pack(digest, ra.NonceEven, nonceOdd, ra.ContSession) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
|
| 312 |
hm2 := hmac.New(sha1.New, key) |
| 313 |
hm2.Write(authBytes) |
| 314 |
auth := hm2.Sum(nil) |
| 315 |
|
| 316 |
if !hmac.Equal(ra.Auth[:], auth) { |
| 317 |
return errors.New("the computed response HMAC didn't match the provided HMAC") |
| 318 |
} |
| 319 |
|
| 320 |
return nil |
| 321 |
} |
| 322 |
|
| 323 |
// zeroBytes zeroes a byte array. |
| 324 |
func zeroBytes(b []byte) { |
| 325 |
for i := range b { |
| 326 |
b[i] = 0 |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
func sealHelper(rw io.ReadWriter, pcrInfo *pcrInfoLong, data []byte, srkAuth []byte) ([]byte, error) { |
| 331 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 332 |
// command and getting back a secret and a handle. |
| 333 |
sharedSecret, osapr, err := newOSAPSession(rw, etSRK, khSRK, srkAuth) |
| 334 |
if err != nil { |
| 335 |
return nil, err |
| 336 |
} |
| 337 |
defer osapr.Close(rw) |
| 338 |
defer zeroBytes(sharedSecret[:]) |
| 339 |
|
| 340 |
// EncAuth for a seal command is computed as |
| 341 |
// |
| 342 |
// encAuth = XOR(srkAuth, SHA1(sharedSecret || <lastEvenNonce>)) |
| 343 |
// |
| 344 |
// In this case, the last even nonce is NonceEven from OSAP. |
| 345 |
xorData, err := tpmutil.Pack(sharedSecret, osapr.NonceEven) |
| 346 |
if err != nil { |
| 347 |
return nil, err |
| 348 |
} |
| 349 |
defer zeroBytes(xorData) |
| 350 |
|
| 351 |
encAuthData := sha1.Sum(xorData) |
| 352 |
sc := &sealCommand{KeyHandle: khSRK} |
| 353 |
for i := range sc.EncAuth { |
| 354 |
sc.EncAuth[i] = srkAuth[i] ^ encAuthData[i] |
| 355 |
} |
| 356 |
|
| 357 |
// The digest input for seal authentication is |
| 358 |
// |
| 359 |
// digest = SHA1(ordSeal || encAuth || binary.Size(pcrInfo) || pcrInfo || |
| 360 |
// len(data) || data) |
| 361 |
// |
| 362 |
authIn := []interface{}{ordSeal, sc.EncAuth, uint32(binary.Size(pcrInfo)), pcrInfo, tpmutil.U32Bytes(data)} |
| 363 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 364 |
if err != nil { |
| 365 |
return nil, err |
| 366 |
} |
| 367 |
|
| 368 |
sealed, ra, ret, err := seal(rw, sc, pcrInfo, data, ca) |
| 369 |
if err != nil { |
| 370 |
return nil, err |
| 371 |
} |
| 372 |
|
| 373 |
// Check the response authentication. |
| 374 |
raIn := []interface{}{ret, ordSeal, sealed} |
| 375 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 376 |
return nil, err |
| 377 |
} |
| 378 |
|
| 379 |
sealedBytes, err := tpmutil.Pack(*sealed) |
| 380 |
if err != nil { |
| 381 |
return nil, err |
| 382 |
} |
| 383 |
|
| 384 |
return sealedBytes, nil |
| 385 |
} |
| 386 |
|
| 387 |
// Seal encrypts data against a given locality and PCRs and returns the sealed data. |
| 388 |
func Seal(rw io.ReadWriter, loc Locality, pcrs []int, data []byte, srkAuth []byte) ([]byte, error) { |
| 389 |
pcrInfo, err := newPCRInfoLong(rw, loc, pcrs) |
| 390 |
if err != nil { |
| 391 |
return nil, err |
| 392 |
} |
| 393 |
return sealHelper(rw, pcrInfo, data, srkAuth) |
| 394 |
} |
| 395 |
|
| 396 |
// Reseal takes a pre-calculated PCR map and locality in order to seal data |
| 397 |
// with a srkAuth. This function is necessary for PCR pre-calculation and later |
| 398 |
// sealing to provide a way of updating software which is part of a measured |
| 399 |
// boot process. |
| 400 |
func Reseal(rw io.ReadWriter, loc Locality, pcrs map[int][]byte, data []byte, srkAuth []byte) ([]byte, error) { |
| 401 |
pcrInfo, err := newPCRInfoLongWithHashes(loc, pcrs) |
| 402 |
if err != nil { |
| 403 |
return nil, err |
| 404 |
} |
| 405 |
return sealHelper(rw, pcrInfo, data, srkAuth) |
| 406 |
} |
| 407 |
|
| 408 |
// Unseal decrypts data encrypted by the TPM. |
| 409 |
func Unseal(rw io.ReadWriter, sealed []byte, srkAuth []byte) ([]byte, error) { |
| 410 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 411 |
// command and getting back a secret and a handle. |
| 412 |
sharedSecret, osapr, err := newOSAPSession(rw, etSRK, khSRK, srkAuth) |
| 413 |
if err != nil { |
| 414 |
return nil, err |
| 415 |
} |
| 416 |
defer osapr.Close(rw) |
| 417 |
defer zeroBytes(sharedSecret[:]) |
| 418 |
|
| 419 |
// The unseal command needs an OIAP session in addition to the OSAP session. |
| 420 |
oiapr, err := oiap(rw) |
| 421 |
if err != nil { |
| 422 |
return nil, err |
| 423 |
} |
| 424 |
defer oiapr.Close(rw) |
| 425 |
|
| 426 |
// Convert the sealed value into a tpmStoredData. |
| 427 |
var tsd tpmStoredData |
| 428 |
if _, err := tpmutil.Unpack(sealed, &tsd); err != nil { |
| 429 |
return nil, errors.New("couldn't convert the sealed data into a tpmStoredData struct") |
| 430 |
} |
| 431 |
|
| 432 |
// The digest for auth1 and auth2 for the unseal command is computed as |
| 433 |
// digest = SHA1(ordUnseal || tsd) |
| 434 |
authIn := []interface{}{ordUnseal, tsd} |
| 435 |
|
| 436 |
// The first commandAuth uses the shared secret as an HMAC key. |
| 437 |
ca1, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 438 |
if err != nil { |
| 439 |
return nil, err |
| 440 |
} |
| 441 |
|
| 442 |
// The second commandAuth is based on OIAP instead of OSAP and uses the |
| 443 |
// SRK auth value as an HMAC key instead of the shared secret. |
| 444 |
ca2, err := newCommandAuth(oiapr.AuthHandle, oiapr.NonceEven, nil, srkAuth, authIn) |
| 445 |
if err != nil { |
| 446 |
return nil, err |
| 447 |
} |
| 448 |
|
| 449 |
unsealed, ra1, ra2, ret, err := unseal(rw, khSRK, &tsd, ca1, ca2) |
| 450 |
if err != nil { |
| 451 |
return nil, err |
| 452 |
} |
| 453 |
|
| 454 |
// Check the response authentication. |
| 455 |
raIn := []interface{}{ret, ordUnseal, tpmutil.U32Bytes(unsealed)} |
| 456 |
if err := ra1.verify(ca1.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 457 |
return nil, err |
| 458 |
} |
| 459 |
|
| 460 |
if err := ra2.verify(ca2.NonceOdd, srkAuth, raIn); err != nil { |
| 461 |
return nil, err |
| 462 |
} |
| 463 |
|
| 464 |
return unsealed, nil |
| 465 |
} |
| 466 |
|
| 467 |
// Quote produces a TPM quote for the given data under the given PCRs. It uses |
| 468 |
// AIK auth and a given AIK handle. |
| 469 |
func Quote(rw io.ReadWriter, handle tpmutil.Handle, data []byte, pcrNums []int, aikAuth []byte) ([]byte, []byte, error) { |
| 470 |
// Run OSAP for the handle, reading a random OddOSAP for our initial |
| 471 |
// command and getting back a secret and a response. |
| 472 |
sharedSecret, osapr, err := newOSAPSession(rw, etKeyHandle, handle, aikAuth) |
| 473 |
if err != nil { |
| 474 |
return nil, nil, err |
| 475 |
} |
| 476 |
defer osapr.Close(rw) |
| 477 |
defer zeroBytes(sharedSecret[:]) |
| 478 |
|
| 479 |
// Hash the data to get the value to pass to quote2. |
| 480 |
hash := sha1.Sum(data) |
| 481 |
pcrSel, err := newPCRSelection(pcrNums) |
| 482 |
if err != nil { |
| 483 |
return nil, nil, err |
| 484 |
} |
| 485 |
authIn := []interface{}{ordQuote, hash, pcrSel} |
| 486 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 487 |
if err != nil { |
| 488 |
return nil, nil, err |
| 489 |
} |
| 490 |
|
| 491 |
pcrc, sig, ra, ret, err := quote(rw, handle, hash, pcrSel, ca) |
| 492 |
if err != nil { |
| 493 |
return nil, nil, err |
| 494 |
} |
| 495 |
|
| 496 |
// Check response authentication. |
| 497 |
raIn := []interface{}{ret, ordQuote, pcrc, tpmutil.U32Bytes(sig)} |
| 498 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 499 |
return nil, nil, err |
| 500 |
} |
| 501 |
|
| 502 |
return sig, pcrc.Values, nil |
| 503 |
} |
| 504 |
|
| 505 |
// MakeIdentity creates a new AIK with the given new auth value, and the given |
| 506 |
// parameters for the privacy CA that will be used to attest to it. |
| 507 |
// If both pk and label are nil, then the TPM_CHOSENID_HASH is set to all 0s as |
| 508 |
// a special case. MakeIdentity returns a key blob for the newly-created key. |
| 509 |
// The caller must be authorized to use the SRK, since the private part of the |
| 510 |
// AIK is sealed against the SRK. |
| 511 |
// TODO(tmroeder): currently, this code can only create 2048-bit RSA keys. |
| 512 |
func MakeIdentity(rw io.ReadWriter, srkAuth []byte, ownerAuth []byte, aikAuth []byte, pk crypto.PublicKey, label []byte) ([]byte, error) { |
| 513 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial command |
| 514 |
// and getting back a secret and a handle. |
| 515 |
sharedSecretSRK, osaprSRK, err := newOSAPSession(rw, etSRK, khSRK, srkAuth) |
| 516 |
if err != nil { |
| 517 |
return nil, err |
| 518 |
} |
| 519 |
defer osaprSRK.Close(rw) |
| 520 |
defer zeroBytes(sharedSecretSRK[:]) |
| 521 |
|
| 522 |
// Run OSAP for the Owner, reading a random OddOSAP for our initial command |
| 523 |
// and getting back a secret and a handle. |
| 524 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth) |
| 525 |
if err != nil { |
| 526 |
return nil, err |
| 527 |
} |
| 528 |
defer osaprOwn.Close(rw) |
| 529 |
defer zeroBytes(sharedSecretOwn[:]) |
| 530 |
|
| 531 |
// EncAuth for a MakeIdentity command is computed as |
| 532 |
// |
| 533 |
// encAuth = XOR(aikAuth, SHA1(sharedSecretOwn || <lastEvenNonce>)) |
| 534 |
// |
| 535 |
// In this case, the last even nonce is NonceEven from OSAP for the Owner. |
| 536 |
xorData, err := tpmutil.Pack(sharedSecretOwn, osaprOwn.NonceEven) |
| 537 |
if err != nil { |
| 538 |
return nil, err |
| 539 |
} |
| 540 |
defer zeroBytes(xorData) |
| 541 |
|
| 542 |
encAuthData := sha1.Sum(xorData) |
| 543 |
var encAuth Digest |
| 544 |
for i := range encAuth { |
| 545 |
encAuth[i] = aikAuth[i] ^ encAuthData[i] |
| 546 |
} |
| 547 |
|
| 548 |
var caDigest Digest |
| 549 |
if (pk != nil) != (label != nil) { |
| 550 |
return nil, errors.New("inconsistent null values between the pk and the label") |
| 551 |
} |
| 552 |
|
| 553 |
if pk != nil { |
| 554 |
pubKey, err := convertPubKey(pk) |
| 555 |
if err != nil { |
| 556 |
return nil, err |
| 557 |
} |
| 558 |
|
| 559 |
// We can't pack the pair of values directly, since the label is |
| 560 |
// included directly as bytes, without any length. |
| 561 |
fullpkb, err := tpmutil.Pack(pubKey) |
| 562 |
if err != nil { |
| 563 |
return nil, err |
| 564 |
} |
| 565 |
|
| 566 |
caDigestBytes := append(label, fullpkb...) |
| 567 |
caDigest = sha1.Sum(caDigestBytes) |
| 568 |
} |
| 569 |
|
| 570 |
rsaAIKParams := rsaKeyParams{ |
| 571 |
KeyLength: 2048, |
| 572 |
NumPrimes: 2, |
| 573 |
//Exponent: big.NewInt(0x10001).Bytes(), // 65537. Implicit? |
| 574 |
} |
| 575 |
packedParams, err := tpmutil.Pack(rsaAIKParams) |
| 576 |
if err != nil { |
| 577 |
return nil, err |
| 578 |
} |
| 579 |
|
| 580 |
aikParams := keyParams{ |
| 581 |
AlgID: AlgRSA, |
| 582 |
EncScheme: esNone, |
| 583 |
SigScheme: ssRSASaPKCS1v15SHA1, |
| 584 |
Params: packedParams, |
| 585 |
} |
| 586 |
|
| 587 |
aik := &key{ |
| 588 |
Version: 0x01010000, |
| 589 |
KeyUsage: keyIdentity, |
| 590 |
KeyFlags: 0, |
| 591 |
AuthDataUsage: authAlways, |
| 592 |
AlgorithmParams: aikParams, |
| 593 |
} |
| 594 |
|
| 595 |
// The digest input for MakeIdentity authentication is |
| 596 |
// |
| 597 |
// digest = SHA1(ordMakeIdentity || encAuth || caDigest || aik) |
| 598 |
// |
| 599 |
authIn := []interface{}{ordMakeIdentity, encAuth, caDigest, aik} |
| 600 |
ca1, err := newCommandAuth(osaprSRK.AuthHandle, osaprSRK.NonceEven, nil, sharedSecretSRK[:], authIn) |
| 601 |
if err != nil { |
| 602 |
return nil, err |
| 603 |
} |
| 604 |
|
| 605 |
ca2, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 606 |
if err != nil { |
| 607 |
return nil, err |
| 608 |
} |
| 609 |
|
| 610 |
k, sig, ra1, ra2, ret, err := makeIdentity(rw, encAuth, caDigest, aik, ca1, ca2) |
| 611 |
if err != nil { |
| 612 |
return nil, err |
| 613 |
} |
| 614 |
|
| 615 |
// Check response authentication. |
| 616 |
raIn := []interface{}{ret, ordMakeIdentity, k, tpmutil.U32Bytes(sig)} |
| 617 |
if err := ra1.verify(ca1.NonceOdd, sharedSecretSRK[:], raIn); err != nil { |
| 618 |
return nil, err |
| 619 |
} |
| 620 |
|
| 621 |
if err := ra2.verify(ca2.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 622 |
return nil, err |
| 623 |
} |
| 624 |
|
| 625 |
// TODO(tmroeder): check the signature against the pubEK. |
| 626 |
blob, err := tpmutil.Pack(k) |
| 627 |
if err != nil { |
| 628 |
return nil, err |
| 629 |
} |
| 630 |
|
| 631 |
return blob, nil |
| 632 |
} |
| 633 |
|
| 634 |
func unloadTrspiCred(blob []byte) ([]byte, error) { |
| 635 |
/* |
| 636 |
* Trousers expects the asym blob to have an additional data in the header. |
| 637 |
* The relevant data is duplicated in the TPM_SYMMETRIC_KEY struct so we parse |
| 638 |
* and throw the header away. |
| 639 |
* TODO(dkarch): Trousers is not doing credential activation correctly. We should |
| 640 |
* remove this and instead expose the asymmetric decryption and symmetric decryption |
| 641 |
* so that anyone generating a challenge for Trousers can unload the header themselves |
| 642 |
* and send us a correctly formatted challenge. |
| 643 |
*/ |
| 644 |
|
| 645 |
var header struct { |
| 646 |
Credsize uint32 |
| 647 |
AlgID uint32 |
| 648 |
EncScheme uint16 |
| 649 |
SigScheme uint16 |
| 650 |
Parmsize uint32 |
| 651 |
} |
| 652 |
|
| 653 |
symbuf := bytes.NewReader(blob) |
| 654 |
if err := binary.Read(symbuf, binary.BigEndian, &header); err != nil { |
| 655 |
return nil, err |
| 656 |
} |
| 657 |
// Unload the symmetric key parameters. |
| 658 |
parms := make([]byte, header.Parmsize) |
| 659 |
if err := binary.Read(symbuf, binary.BigEndian, parms); err != nil { |
| 660 |
return nil, err |
| 661 |
} |
| 662 |
// Unload and return the symmetrically encrypted secret. |
| 663 |
cred := make([]byte, header.Credsize) |
| 664 |
if err := binary.Read(symbuf, binary.BigEndian, cred); err != nil { |
| 665 |
return nil, err |
| 666 |
} |
| 667 |
return cred, nil |
| 668 |
} |
| 669 |
|
| 670 |
// ActivateIdentity asks the TPM to decrypt an EKPub encrypted symmetric session key |
| 671 |
// which it uses to decrypt the symmetrically encrypted secret. |
| 672 |
func ActivateIdentity(rw io.ReadWriter, aikAuth []byte, ownerAuth []byte, aik tpmutil.Handle, asym, sym []byte) ([]byte, error) { |
| 673 |
// Run OIAP for the AIK. |
| 674 |
oiaprAIK, err := oiap(rw) |
| 675 |
if err != nil { |
| 676 |
return nil, fmt.Errorf("failed to start OIAP session: %v", err) |
| 677 |
} |
| 678 |
|
| 679 |
// Run OSAP for the owner, reading a random OddOSAP for our initial command |
| 680 |
// and getting back a secret and a handle. |
| 681 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth) |
| 682 |
if err != nil { |
| 683 |
return nil, fmt.Errorf("failed to start OSAP session: %v", err) |
| 684 |
} |
| 685 |
defer osaprOwn.Close(rw) |
| 686 |
defer zeroBytes(sharedSecretOwn[:]) |
| 687 |
|
| 688 |
authIn := []interface{}{ordActivateIdentity, tpmutil.U32Bytes(asym)} |
| 689 |
ca1, err := newCommandAuth(oiaprAIK.AuthHandle, oiaprAIK.NonceEven, nil, aikAuth, authIn) |
| 690 |
if err != nil { |
| 691 |
return nil, fmt.Errorf("newCommandAuth failed: %v", err) |
| 692 |
} |
| 693 |
ca2, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 694 |
if err != nil { |
| 695 |
return nil, fmt.Errorf("newCommandAuth failed: %v", err) |
| 696 |
} |
| 697 |
|
| 698 |
symkey, ra1, ra2, ret, err := activateIdentity(rw, aik, asym, ca1, ca2) |
| 699 |
if err != nil { |
| 700 |
return nil, fmt.Errorf("activateIdentity failed: %v", err) |
| 701 |
} |
| 702 |
|
| 703 |
// Check response authentication. |
| 704 |
raIn := []interface{}{ret, ordActivateIdentity, symkey} |
| 705 |
if err := ra1.verify(ca1.NonceOdd, aikAuth, raIn); err != nil { |
| 706 |
return nil, fmt.Errorf("aik resAuth failed to verify: %v", err) |
| 707 |
} |
| 708 |
|
| 709 |
if err := ra2.verify(ca2.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 710 |
return nil, fmt.Errorf("owner resAuth failed to verify: %v", err) |
| 711 |
} |
| 712 |
|
| 713 |
cred, err := unloadTrspiCred(sym) |
| 714 |
if err != nil { |
| 715 |
return nil, fmt.Errorf("unloadTrspiCred failed: %v", err) |
| 716 |
} |
| 717 |
var ( |
| 718 |
block cipher.Block |
| 719 |
iv []byte |
| 720 |
ciphertxt []byte |
| 721 |
secret []byte |
| 722 |
) |
| 723 |
switch id := symkey.AlgID; id { |
| 724 |
case AlgAES128: |
| 725 |
block, err = aes.NewCipher(symkey.Key) |
| 726 |
if err != nil { |
| 727 |
return nil, fmt.Errorf("aes.NewCipher failed: %v", err) |
| 728 |
} |
| 729 |
iv = cred[:aes.BlockSize] |
| 730 |
ciphertxt = cred[aes.BlockSize:] |
| 731 |
secret = ciphertxt |
| 732 |
default: |
| 733 |
return nil, fmt.Errorf("%v is not a supported session key algorithm", id) |
| 734 |
} |
| 735 |
switch es := symkey.EncScheme; es { |
| 736 |
case esSymCTR: |
| 737 |
stream := cipher.NewCTR(block, iv) |
| 738 |
stream.XORKeyStream(secret, ciphertxt) |
| 739 |
case esSymOFB: |
| 740 |
stream := cipher.NewOFB(block, iv) |
| 741 |
stream.XORKeyStream(secret, ciphertxt) |
| 742 |
case esSymCBCPKCS5: |
| 743 |
mode := cipher.NewCBCDecrypter(block, iv) |
| 744 |
mode.CryptBlocks(secret, ciphertxt) |
| 745 |
// Remove PKCS5 padding. |
| 746 |
padlen := int(secret[len(secret)-1]) |
| 747 |
secret = secret[:len(secret)-padlen] |
| 748 |
default: |
| 749 |
return nil, fmt.Errorf("%v is not a supported encryption scheme", es) |
| 750 |
} |
| 751 |
|
| 752 |
return secret, nil |
| 753 |
} |
| 754 |
|
| 755 |
// ResetLockValue resets the dictionary-attack value in the TPM; this allows the |
| 756 |
// TPM to start working again after authentication errors without waiting for |
| 757 |
// the dictionary-attack defenses to time out. This requires owner |
| 758 |
// authentication. |
| 759 |
func ResetLockValue(rw io.ReadWriter, ownerAuth Digest) error { |
| 760 |
// Run OSAP for the Owner, reading a random OddOSAP for our initial command |
| 761 |
// and getting back a secret and a handle. |
| 762 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth[:]) |
| 763 |
if err != nil { |
| 764 |
return err |
| 765 |
} |
| 766 |
defer osaprOwn.Close(rw) |
| 767 |
defer zeroBytes(sharedSecretOwn[:]) |
| 768 |
|
| 769 |
// The digest input for ResetLockValue auth is |
| 770 |
// |
| 771 |
// digest = SHA1(ordResetLockValue) |
| 772 |
// |
| 773 |
authIn := []interface{}{ordResetLockValue} |
| 774 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 775 |
if err != nil { |
| 776 |
return err |
| 777 |
} |
| 778 |
|
| 779 |
ra, ret, err := resetLockValue(rw, ca) |
| 780 |
if err != nil { |
| 781 |
return err |
| 782 |
} |
| 783 |
|
| 784 |
// Check response authentication. |
| 785 |
raIn := []interface{}{ret, ordResetLockValue} |
| 786 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 787 |
return err |
| 788 |
} |
| 789 |
|
| 790 |
return nil |
| 791 |
} |
| 792 |
|
| 793 |
// ownerReadInternalHelper sets up command auth and checks response auth for |
| 794 |
// OwnerReadInternalPub. It's not exported because OwnerReadInternalPub only |
| 795 |
// supports two fixed key handles: khEK and khSRK. |
| 796 |
func ownerReadInternalHelper(rw io.ReadWriter, kh tpmutil.Handle, ownerAuth Digest) (*pubKey, error) { |
| 797 |
// Run OSAP for the Owner, reading a random OddOSAP for our initial command |
| 798 |
// and getting back a secret and a handle. |
| 799 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth[:]) |
| 800 |
if err != nil { |
| 801 |
return nil, err |
| 802 |
} |
| 803 |
defer osaprOwn.Close(rw) |
| 804 |
defer zeroBytes(sharedSecretOwn[:]) |
| 805 |
|
| 806 |
// The digest input for OwnerReadInternalPub is |
| 807 |
// |
| 808 |
// digest = SHA1(ordOwnerReadInternalPub || kh) |
| 809 |
// |
| 810 |
authIn := []interface{}{ordOwnerReadInternalPub, kh} |
| 811 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 812 |
if err != nil { |
| 813 |
return nil, err |
| 814 |
} |
| 815 |
|
| 816 |
pk, ra, ret, err := ownerReadInternalPub(rw, kh, ca) |
| 817 |
if err != nil { |
| 818 |
return nil, err |
| 819 |
} |
| 820 |
|
| 821 |
// Check response authentication. |
| 822 |
raIn := []interface{}{ret, ordOwnerReadInternalPub, pk} |
| 823 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 824 |
return nil, err |
| 825 |
} |
| 826 |
|
| 827 |
return pk, nil |
| 828 |
} |
| 829 |
|
| 830 |
// OwnerReadSRK uses owner auth to get a blob representing the SRK. |
| 831 |
func OwnerReadSRK(rw io.ReadWriter, ownerAuth Digest) ([]byte, error) { |
| 832 |
pk, err := ownerReadInternalHelper(rw, khSRK, ownerAuth) |
| 833 |
if err != nil { |
| 834 |
return nil, err |
| 835 |
} |
| 836 |
|
| 837 |
return tpmutil.Pack(pk) |
| 838 |
} |
| 839 |
|
| 840 |
// ReadEKCert reads the EKCert from the NVRAM. |
| 841 |
// The TCG PC Client specifies additional headers that are to be stored with the EKCert, we parse them |
| 842 |
// here and return only the DER encoded certificate. |
| 843 |
// TCG PC Client Specific Implementation Specification for Conventional BIOS 7.4.4 |
| 844 |
// https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientImplementation_1-21_1_00.pdf |
| 845 |
func ReadEKCert(rw io.ReadWriter, ownAuth Digest) ([]byte, error) { |
| 846 |
const ( |
| 847 |
certIndex = 0x1000f000 // TPM_NV_INDEX_EKCert (TPM Main Part 2 TPM Structures 19.1.2) |
| 848 |
certTagPCClientStoredCert = 0x1001 // TCG_TAG_PCCLIENT_STORED_CERT |
| 849 |
certTagPCClientFullCert = 0x1002 // TCG_TAG_PCCLIENT_FULL_CERT |
| 850 |
tcgFullCert = 0 // TCG_FULL_CERT |
| 851 |
tcgPartialSmallCert = 1 // TCG_PARTIAL_SMALL_CERT |
| 852 |
) |
| 853 |
offset := uint32(0) |
| 854 |
var header struct { |
| 855 |
Tag uint16 |
| 856 |
CertType uint8 |
| 857 |
CertSize uint16 |
| 858 |
} |
| 859 |
|
| 860 |
data, err := NVReadValue(rw, certIndex, offset, uint32(binary.Size(header)), []byte(ownAuth[:])) |
| 861 |
if err != nil { |
| 862 |
return nil, err |
| 863 |
} |
| 864 |
offset = offset + uint32(binary.Size(header)) |
| 865 |
buff := bytes.NewReader(data) |
| 866 |
|
| 867 |
if err := binary.Read(buff, binary.BigEndian, &header); err != nil { |
| 868 |
return nil, err |
| 869 |
} |
| 870 |
|
| 871 |
if header.Tag != certTagPCClientStoredCert { |
| 872 |
return nil, fmt.Errorf("invalid certificate") |
| 873 |
} |
| 874 |
|
| 875 |
var bufSize uint32 |
| 876 |
switch header.CertType { |
| 877 |
case tcgFullCert: |
| 878 |
var tag uint16 |
| 879 |
data, err := NVReadValue(rw, certIndex, offset, uint32(binary.Size(tag)), []byte(ownAuth[:])) |
| 880 |
if err != nil { |
| 881 |
return nil, err |
| 882 |
} |
| 883 |
bufSize = uint32(header.CertSize) + offset |
| 884 |
offset = offset + uint32(binary.Size(tag)) |
| 885 |
buff = bytes.NewReader(data) |
| 886 |
|
| 887 |
if err := binary.Read(buff, binary.BigEndian, &tag); err != nil { |
| 888 |
return nil, err |
| 889 |
} |
| 890 |
|
| 891 |
if tag != certTagPCClientFullCert { |
| 892 |
return nil, fmt.Errorf("certificate type and tag do not match") |
| 893 |
} |
| 894 |
case tcgPartialSmallCert: |
| 895 |
return nil, fmt.Errorf("certType is not TCG_FULL_CERT: currently do not support partial certs") |
| 896 |
default: |
| 897 |
return nil, fmt.Errorf("invalid certType: 0x%x", header.CertType) |
| 898 |
} |
| 899 |
|
| 900 |
var ekbuf []byte |
| 901 |
for offset < bufSize { |
| 902 |
length := bufSize - offset |
| 903 |
// TPMs can only read so much memory per command so we read in 128byte chunks. |
| 904 |
// 128 was taken from go-tspi. The actual max read seems to be platform dependent |
| 905 |
// but cannot be queried on TPM1.2 (and does not seem to appear in any documentation). |
| 906 |
if length > 128 { |
| 907 |
length = 128 |
| 908 |
} |
| 909 |
data, err = NVReadValue(rw, certIndex, offset, length, []byte(ownAuth[:])) |
| 910 |
if err != nil { |
| 911 |
return nil, err |
| 912 |
} |
| 913 |
|
| 914 |
ekbuf = append(ekbuf, data...) |
| 915 |
offset += length |
| 916 |
} |
| 917 |
|
| 918 |
return ekbuf, nil |
| 919 |
} |
| 920 |
|
| 921 |
// NVDefineSpace implements the reservation of NVRAM as specified in: |
| 922 |
// TPM-Main-Part-3-Commands_v1.2_rev116_01032011, P. 212 |
| 923 |
func NVDefineSpace(rw io.ReadWriter, nvData NVDataPublic, ownAuth []byte) error { |
| 924 |
var ra *responseAuth |
| 925 |
var ret uint32 |
| 926 |
if ownAuth == nil { |
| 927 |
} else { |
| 928 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownAuth[:]) |
| 929 |
if err != nil { |
| 930 |
return fmt.Errorf("failed to start new auth session: %v", err) |
| 931 |
} |
| 932 |
defer osaprOwn.Close(rw) |
| 933 |
defer zeroBytes(sharedSecretOwn[:]) |
| 934 |
|
| 935 |
// encAuth: NV_Define_Space is a special case where no encryption is used. |
| 936 |
// See spec: TPM-Main-Part-1-Design-Principles_v1.2_rev116_01032011, P. 81 |
| 937 |
xorData, err := tpmutil.Pack(sharedSecretOwn, osaprOwn.NonceEven) |
| 938 |
if err != nil { |
| 939 |
return err |
| 940 |
} |
| 941 |
defer zeroBytes(xorData) |
| 942 |
|
| 943 |
encAuthData := sha1.Sum(xorData) |
| 944 |
|
| 945 |
authIn := []interface{}{ordNVDefineSpace, nvData, encAuthData} |
| 946 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 947 |
if err != nil { |
| 948 |
return err |
| 949 |
} |
| 950 |
ra, ret, err = nvDefineSpace(rw, nvData, encAuthData, ca) |
| 951 |
if err != nil { |
| 952 |
return fmt.Errorf("failed to define space in NVRAM: %v", err) |
| 953 |
} |
| 954 |
raIn := []interface{}{ret, ordNVDefineSpace} |
| 955 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 956 |
return fmt.Errorf("failed to verify authenticity of response: %v", err) |
| 957 |
} |
| 958 |
} |
| 959 |
return nil |
| 960 |
} |
| 961 |
|
| 962 |
// NVReadValue returns the value from a given index, offset, and length in NVRAM. |
| 963 |
// See TPM-Main-Part-2-TPM-Structures 19.1. |
| 964 |
// If TPM isn't locked, no authentication is needed. |
| 965 |
// This is for platform suppliers only. |
| 966 |
// See TPM-Main-Part-3-Commands-20.4 |
| 967 |
func NVReadValue(rw io.ReadWriter, index, offset, len uint32, ownAuth []byte) ([]byte, error) { |
| 968 |
if ownAuth == nil { |
| 969 |
data, _, _, err := nvReadValue(rw, index, offset, len, nil) |
| 970 |
if err != nil { |
| 971 |
return nil, fmt.Errorf("failed to read from NVRAM: %v", err) |
| 972 |
} |
| 973 |
return data, nil |
| 974 |
} |
| 975 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownAuth[:]) |
| 976 |
if err != nil { |
| 977 |
return nil, fmt.Errorf("failed to start new auth session: %v", err) |
| 978 |
} |
| 979 |
defer osaprOwn.Close(rw) |
| 980 |
defer zeroBytes(sharedSecretOwn[:]) |
| 981 |
authIn := []interface{}{ordNVReadValue, index, offset, len} |
| 982 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 983 |
if err != nil { |
| 984 |
return nil, fmt.Errorf("failed to construct owner auth fields: %v", err) |
| 985 |
} |
| 986 |
data, ra, ret, err := nvReadValue(rw, index, offset, len, ca) |
| 987 |
if err != nil { |
| 988 |
return nil, fmt.Errorf("failed to read from NVRAM: %v", err) |
| 989 |
} |
| 990 |
raIn := []interface{}{ret, ordNVReadValue, tpmutil.U32Bytes(data)} |
| 991 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 992 |
return nil, fmt.Errorf("failed to verify authenticity of response: %v", err) |
| 993 |
} |
| 994 |
|
| 995 |
return data, nil |
| 996 |
} |
| 997 |
|
| 998 |
// NVReadValueAuth returns the value from a given index, offset, and length in NVRAM. |
| 999 |
// See TPM-Main-Part-2-TPM-Structures 19.1. |
| 1000 |
// If TPM is locked, authentication is mandatory. |
| 1001 |
// See TPM-Main-Part-3-Commands-20.5 |
| 1002 |
func NVReadValueAuth(rw io.ReadWriter, index, offset, len uint32, auth []byte) ([]byte, error) { |
| 1003 |
if auth == nil { |
| 1004 |
return nil, fmt.Errorf("no auth value given but mandatory") |
| 1005 |
} |
| 1006 |
sharedSecret, osapr, err := newOSAPSession(rw, etOwner, khOwner, auth[:]) |
| 1007 |
if err != nil { |
| 1008 |
return nil, fmt.Errorf("failed to start new auth session: %v", err) |
| 1009 |
} |
| 1010 |
defer osapr.Close(rw) |
| 1011 |
defer zeroBytes(sharedSecret[:]) |
| 1012 |
authIn := []interface{}{ordNVReadValueAuth, index, offset, len} |
| 1013 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 1014 |
if err != nil { |
| 1015 |
return nil, fmt.Errorf("failed to construct auth fields: %v", err) |
| 1016 |
} |
| 1017 |
data, ra, ret, err := nvReadValue(rw, index, offset, len, ca) |
| 1018 |
if err != nil { |
| 1019 |
return nil, fmt.Errorf("failed to read from NVRAM: %v", err) |
| 1020 |
} |
| 1021 |
raIn := []interface{}{ret, ordNVReadValueAuth, tpmutil.U32Bytes(data)} |
| 1022 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 1023 |
return nil, fmt.Errorf("failed to verify authenticity of response: %v", err) |
| 1024 |
} |
| 1025 |
|
| 1026 |
return data, nil |
| 1027 |
} |
| 1028 |
|
| 1029 |
// NVWriteValue for writing to the NVRAM. Needs a index for a defined space in NVRAM. |
| 1030 |
// See TPM-Main-Part-3-Commands_v1.2_rev116_01032011, P216 |
| 1031 |
func NVWriteValue(rw io.ReadWriter, index, offset uint32, data []byte, ownAuth []byte) error { |
| 1032 |
if ownAuth == nil { |
| 1033 |
if _, _, _, err := nvWriteValue(rw, index, offset, uint32(len(data)), data, nil); err != nil { |
| 1034 |
return fmt.Errorf("failed to write to NVRAM: %v", err) |
| 1035 |
} |
| 1036 |
return nil |
| 1037 |
} |
| 1038 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownAuth[:]) |
| 1039 |
if err != nil { |
| 1040 |
return fmt.Errorf("failed to start new auth session: %v", err) |
| 1041 |
} |
| 1042 |
defer osaprOwn.Close(rw) |
| 1043 |
defer zeroBytes(sharedSecretOwn[:]) |
| 1044 |
authIn := []interface{}{ordNVWriteValue, index, offset, len(data), data} |
| 1045 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 1046 |
if err != nil { |
| 1047 |
return fmt.Errorf("failed to construct owner auth fields: %v", err) |
| 1048 |
} |
| 1049 |
data, ra, ret, err := nvWriteValue(rw, index, offset, uint32(len(data)), data, ca) |
| 1050 |
if err != nil { |
| 1051 |
return fmt.Errorf("failed to write to NVRAM: %v", err) |
| 1052 |
} |
| 1053 |
raIn := []interface{}{ret, ordNVWriteValue, tpmutil.U32Bytes(data)} |
| 1054 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 1055 |
return fmt.Errorf("failed to verify authenticity of response: %v", err) |
| 1056 |
} |
| 1057 |
return nil |
| 1058 |
} |
| 1059 |
|
| 1060 |
// NVWriteValueAuth for authenticated writing to the NVRAM. |
| 1061 |
// Needs a index of a defined space in NVRAM. |
| 1062 |
// See TPM-Main-Part-2-TPM-Structures 19.1. |
| 1063 |
// If TPM is locked, authentification is mandatory. |
| 1064 |
// See TPM-Main-Part-3-Commands_v1.2_rev116_01032011, P216 |
| 1065 |
func NVWriteValueAuth(rw io.ReadWriter, index, offset uint32, data []byte, auth []byte) error { |
| 1066 |
if auth == nil { |
| 1067 |
return fmt.Errorf("no auth value given but mandatory") |
| 1068 |
} |
| 1069 |
sharedSecret, osapr, err := newOSAPSession(rw, etOwner, khOwner, auth[:]) |
| 1070 |
if err != nil { |
| 1071 |
return fmt.Errorf("failed to start new auth session: %v", err) |
| 1072 |
} |
| 1073 |
defer osapr.Close(rw) |
| 1074 |
defer zeroBytes(sharedSecret[:]) |
| 1075 |
authIn := []interface{}{ordNVWriteValueAuth, index, offset, len(data), data} |
| 1076 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 1077 |
if err != nil { |
| 1078 |
return fmt.Errorf("failed to construct auth fields: %v", err) |
| 1079 |
} |
| 1080 |
data, ra, ret, err := nvWriteValue(rw, index, offset, uint32(len(data)), data, ca) |
| 1081 |
if err != nil { |
| 1082 |
return fmt.Errorf("failed to write to NVRAM: %v", err) |
| 1083 |
} |
| 1084 |
raIn := []interface{}{ret, ordNVWriteValueAuth, tpmutil.U32Bytes(data)} |
| 1085 |
if err := ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 1086 |
return fmt.Errorf("failed to verify authenticity of response: %v", err) |
| 1087 |
} |
| 1088 |
return nil |
| 1089 |
} |
| 1090 |
|
| 1091 |
// OwnerReadPubEK uses owner auth to get a blob representing the public part of the |
| 1092 |
// endorsement key. |
| 1093 |
func OwnerReadPubEK(rw io.ReadWriter, ownerAuth Digest) ([]byte, error) { |
| 1094 |
pk, err := ownerReadInternalHelper(rw, khEK, ownerAuth) |
| 1095 |
if err != nil { |
| 1096 |
return nil, err |
| 1097 |
} |
| 1098 |
|
| 1099 |
return tpmutil.Pack(pk) |
| 1100 |
} |
| 1101 |
|
| 1102 |
// ReadPubEK reads the public part of the endorsement key when no owner is |
| 1103 |
// established. |
| 1104 |
func ReadPubEK(rw io.ReadWriter) ([]byte, error) { |
| 1105 |
var n Nonce |
| 1106 |
if _, err := rand.Read(n[:]); err != nil { |
| 1107 |
return nil, err |
| 1108 |
} |
| 1109 |
|
| 1110 |
pk, d, _, err := readPubEK(rw, n) |
| 1111 |
if err != nil { |
| 1112 |
return nil, err |
| 1113 |
} |
| 1114 |
|
| 1115 |
// Recompute the hash of the pk and the nonce to defend against replay |
| 1116 |
// attacks. |
| 1117 |
b, err := tpmutil.Pack(pk, n) |
| 1118 |
if err != nil { |
| 1119 |
return nil, err |
| 1120 |
} |
| 1121 |
|
| 1122 |
s := sha1.Sum(b) |
| 1123 |
// There's no need for constant-time comparison of these hash values, |
| 1124 |
// since no secret is involved. |
| 1125 |
if !bytes.Equal(s[:], d[:]) { |
| 1126 |
return nil, errors.New("the ReadPubEK operation failed the replay check") |
| 1127 |
} |
| 1128 |
|
| 1129 |
return tpmutil.Pack(pk) |
| 1130 |
} |
| 1131 |
|
| 1132 |
// GetManufacturer returns the manufacturer ID |
| 1133 |
func GetManufacturer(rw io.ReadWriter) ([]byte, error) { |
| 1134 |
return getCapability(rw, CapProperty, SubCapPropManufacturer) |
| 1135 |
} |
| 1136 |
|
| 1137 |
// GetPermanentFlags returns the TPM_PERMANENT_FLAGS structure. |
| 1138 |
func GetPermanentFlags(rw io.ReadWriter) (PermanentFlags, error) { |
| 1139 |
var ret PermanentFlags |
| 1140 |
|
| 1141 |
raw, err := getCapability(rw, CapFlag, SubCapFlagPermanent) |
| 1142 |
if err != nil { |
| 1143 |
return ret, err |
| 1144 |
} |
| 1145 |
|
| 1146 |
_, err = tpmutil.Unpack(raw, &ret) |
| 1147 |
return ret, err |
| 1148 |
} |
| 1149 |
|
| 1150 |
// GetAlgs returns a list of algorithms supported by the TPM device. |
| 1151 |
func GetAlgs(rw io.ReadWriter) ([]Algorithm, error) { |
| 1152 |
var algs []Algorithm |
| 1153 |
for i := AlgRSA; i <= AlgXOR; i++ { |
| 1154 |
buf, err := getCapability(rw, CapAlg, uint32(i)) |
| 1155 |
if err != nil { |
| 1156 |
return nil, err |
| 1157 |
} |
| 1158 |
if uint8(buf[0]) > 0 { |
| 1159 |
algs = append(algs, Algorithm(i)) |
| 1160 |
} |
| 1161 |
|
| 1162 |
} |
| 1163 |
return algs, nil |
| 1164 |
} |
| 1165 |
|
| 1166 |
// GetCapVersionVal returns the decoded contents of TPM_CAP_VERSION_INFO. |
| 1167 |
func GetCapVersionVal(rw io.ReadWriter) (*CapVersionInfo, error) { |
| 1168 |
var capVer CapVersionInfo |
| 1169 |
buf, err := getCapability(rw, CapVersion, 0) |
| 1170 |
if err != nil { |
| 1171 |
return nil, err |
| 1172 |
} |
| 1173 |
if err := capVer.Decode(buf); err != nil { |
| 1174 |
return nil, err |
| 1175 |
} |
| 1176 |
return &capVer, nil |
| 1177 |
} |
| 1178 |
|
| 1179 |
// GetNVList returns a list of TPM_NV_INDEX values that |
| 1180 |
// are currently allocated NV storage through TPM_NV_DefineSpace. |
| 1181 |
func GetNVList(rw io.ReadWriter) ([]uint32, error) { |
| 1182 |
buf, err := getCapability(rw, CapNVList, 0) |
| 1183 |
if err != nil { |
| 1184 |
return nil, err |
| 1185 |
} |
| 1186 |
nvList := make([]uint32, len(buf)/4) |
| 1187 |
for i := range nvList { |
| 1188 |
nvList[i] = uint32(binary.BigEndian.Uint32(buf[i*4 : (i+1)*4])) |
| 1189 |
} |
| 1190 |
|
| 1191 |
return nvList, err |
| 1192 |
} |
| 1193 |
|
| 1194 |
// GetNVIndex returns the structure of NVDataPublic which contains |
| 1195 |
// information about the requested NV Index. |
| 1196 |
// See: TPM-Main-Part-2-TPM-Structures_v1.2_rev116_01032011, P.167 |
| 1197 |
func GetNVIndex(rw io.ReadWriter, nvIndex uint32) (*NVDataPublic, error) { |
| 1198 |
var nvInfo NVDataPublic |
| 1199 |
buf, _ := getCapability(rw, CapNVIndex, nvIndex) |
| 1200 |
if _, err := tpmutil.Unpack(buf, &nvInfo); err != nil { |
| 1201 |
return &nvInfo, err |
| 1202 |
} |
| 1203 |
return &nvInfo, nil |
| 1204 |
} |
| 1205 |
|
| 1206 |
// GetCapabilityRaw reads the requested capability and sub-capability from the |
| 1207 |
// TPM and returns it as a []byte. Where possible, prefer the convenience |
| 1208 |
// functions above, which return higher-level structs for easier handling. |
| 1209 |
func GetCapabilityRaw(rw io.ReadWriter, cap, subcap uint32) ([]byte, error) { |
| 1210 |
return getCapability(rw, cap, subcap) |
| 1211 |
} |
| 1212 |
|
| 1213 |
// OwnerClear uses owner auth to clear the TPM. After this operation, the TPM |
| 1214 |
// can change ownership. |
| 1215 |
func OwnerClear(rw io.ReadWriter, ownerAuth Digest) error { |
| 1216 |
// Run OSAP for the Owner, reading a random OddOSAP for our initial command |
| 1217 |
// and getting back a secret and a handle. |
| 1218 |
sharedSecretOwn, osaprOwn, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth[:]) |
| 1219 |
if err != nil { |
| 1220 |
return err |
| 1221 |
} |
| 1222 |
defer osaprOwn.Close(rw) |
| 1223 |
defer zeroBytes(sharedSecretOwn[:]) |
| 1224 |
|
| 1225 |
// The digest input for OwnerClear is |
| 1226 |
// |
| 1227 |
// digest = SHA1(ordOwnerClear) |
| 1228 |
// |
| 1229 |
authIn := []interface{}{ordOwnerClear} |
| 1230 |
ca, err := newCommandAuth(osaprOwn.AuthHandle, osaprOwn.NonceEven, nil, sharedSecretOwn[:], authIn) |
| 1231 |
if err != nil { |
| 1232 |
return err |
| 1233 |
} |
| 1234 |
|
| 1235 |
ra, ret, err := ownerClear(rw, ca) |
| 1236 |
if err != nil { |
| 1237 |
return err |
| 1238 |
} |
| 1239 |
|
| 1240 |
// Check response authentication. |
| 1241 |
raIn := []interface{}{ret, ordOwnerClear} |
| 1242 |
if err := ra.verify(ca.NonceOdd, sharedSecretOwn[:], raIn); err != nil { |
| 1243 |
return err |
| 1244 |
} |
| 1245 |
|
| 1246 |
return nil |
| 1247 |
} |
| 1248 |
|
| 1249 |
// TakeOwnership takes over a TPM and inserts a new owner auth value and |
| 1250 |
// generates a new SRK, associating it with a new SRK auth value. This |
| 1251 |
// operation can only be performed if there isn't already an owner for the TPM. |
| 1252 |
// The pub EK blob can be acquired by calling ReadPubEK if there is no owner, or |
| 1253 |
// OwnerReadPubEK if there is. |
| 1254 |
func TakeOwnership(rw io.ReadWriter, newOwnerAuth Digest, newSRKAuth Digest, pubEK []byte) error { |
| 1255 |
|
| 1256 |
// Encrypt the owner and SRK auth with the endorsement key. |
| 1257 |
ek, err := UnmarshalPubRSAPublicKey(pubEK) |
| 1258 |
if err != nil { |
| 1259 |
return err |
| 1260 |
} |
| 1261 |
encOwnerAuth, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, ek, newOwnerAuth[:], oaepLabel) |
| 1262 |
if err != nil { |
| 1263 |
return err |
| 1264 |
} |
| 1265 |
encSRKAuth, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, ek, newSRKAuth[:], oaepLabel) |
| 1266 |
if err != nil { |
| 1267 |
return err |
| 1268 |
} |
| 1269 |
|
| 1270 |
// The params for the SRK have very tight requirements: |
| 1271 |
// - KeyLength must be 2048 |
| 1272 |
// - alg must be RSA |
| 1273 |
// - Enc must be OAEP SHA1 MGF1 |
| 1274 |
// - Sig must be None |
| 1275 |
// - Key usage must be Storage |
| 1276 |
// - Key must not be migratable |
| 1277 |
srkRSAParams := rsaKeyParams{ |
| 1278 |
KeyLength: 2048, |
| 1279 |
NumPrimes: 2, |
| 1280 |
} |
| 1281 |
srkpb, err := tpmutil.Pack(srkRSAParams) |
| 1282 |
if err != nil { |
| 1283 |
return err |
| 1284 |
} |
| 1285 |
srkParams := keyParams{ |
| 1286 |
AlgID: AlgRSA, |
| 1287 |
EncScheme: esRSAEsOAEPSHA1MGF1, |
| 1288 |
SigScheme: ssNone, |
| 1289 |
Params: srkpb, |
| 1290 |
} |
| 1291 |
srk := &key{ |
| 1292 |
Version: 0x01010000, |
| 1293 |
KeyUsage: keyStorage, |
| 1294 |
KeyFlags: 0, |
| 1295 |
AuthDataUsage: authAlways, |
| 1296 |
AlgorithmParams: srkParams, |
| 1297 |
} |
| 1298 |
|
| 1299 |
// Get command auth using OIAP with the new owner auth. |
| 1300 |
oiapr, err := oiap(rw) |
| 1301 |
if err != nil { |
| 1302 |
return err |
| 1303 |
} |
| 1304 |
defer oiapr.Close(rw) |
| 1305 |
|
| 1306 |
// The digest for TakeOwnership is |
| 1307 |
// |
| 1308 |
// SHA1(ordTakeOwnership || pidOwner || encOwnerAuth || encSRKAuth || srk) |
| 1309 |
authIn := []interface{}{ordTakeOwnership, pidOwner, tpmutil.U32Bytes(encOwnerAuth), tpmutil.U32Bytes(encSRKAuth), srk} |
| 1310 |
ca, err := newCommandAuth(oiapr.AuthHandle, oiapr.NonceEven, nil, newOwnerAuth[:], authIn) |
| 1311 |
if err != nil { |
| 1312 |
return err |
| 1313 |
} |
| 1314 |
|
| 1315 |
k, ra, ret, err := takeOwnership(rw, encOwnerAuth, encSRKAuth, srk, ca) |
| 1316 |
if err != nil { |
| 1317 |
return err |
| 1318 |
} |
| 1319 |
|
| 1320 |
raIn := []interface{}{ret, ordTakeOwnership, k} |
| 1321 |
return ra.verify(ca.NonceOdd, newOwnerAuth[:], raIn) |
| 1322 |
} |
| 1323 |
|
| 1324 |
func createWrapKeyHelper(rw io.ReadWriter, srkAuth []byte, keyFlags KeyFlags, usageAuth Digest, migrationAuth Digest, pcrs []int) (*key, error) { |
| 1325 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 1326 |
// command and getting back a secret and a handle. |
| 1327 |
sharedSecret, osapr, err := newOSAPSession(rw, etSRK, khSRK, srkAuth) |
| 1328 |
if err != nil { |
| 1329 |
return nil, err |
| 1330 |
} |
| 1331 |
defer osapr.Close(rw) |
| 1332 |
defer zeroBytes(sharedSecret[:]) |
| 1333 |
|
| 1334 |
xorData, err := tpmutil.Pack(sharedSecret, osapr.NonceEven) |
| 1335 |
if err != nil { |
| 1336 |
return nil, err |
| 1337 |
} |
| 1338 |
defer zeroBytes(xorData) |
| 1339 |
|
| 1340 |
// We have to come up with NonceOdd early to encrypt the migration auth. |
| 1341 |
var nonceOdd Nonce |
| 1342 |
if _, err := rand.Read(nonceOdd[:]); err != nil { |
| 1343 |
return nil, err |
| 1344 |
} |
| 1345 |
|
| 1346 |
// ADIP (Authorization Data Insertion Protocol) is based on NonceEven for the first auth value |
| 1347 |
// encrypted by the protocol, and NonceOdd for the second auth value. This is so that the two |
| 1348 |
// keystreams are independent - otherwise, an eavesdropping attacker could XOR the two encrypted |
| 1349 |
// values together to cancel out the key and calculate (usageAuth ^ migrationAuth). |
| 1350 |
xorData2, err := tpmutil.Pack(sharedSecret, nonceOdd) |
| 1351 |
if err != nil { |
| 1352 |
return nil, err |
| 1353 |
} |
| 1354 |
defer zeroBytes(xorData2) |
| 1355 |
|
| 1356 |
encAuthDataKey := sha1.Sum(xorData) |
| 1357 |
defer zeroBytes(encAuthDataKey[:]) |
| 1358 |
encAuthDataKey2 := sha1.Sum(xorData2) |
| 1359 |
defer zeroBytes(encAuthDataKey2[:]) |
| 1360 |
|
| 1361 |
var encUsageAuth Digest |
| 1362 |
for i := range usageAuth { |
| 1363 |
encUsageAuth[i] = encAuthDataKey[i] ^ usageAuth[i] |
| 1364 |
} |
| 1365 |
var encMigrationAuth Digest |
| 1366 |
for i := range migrationAuth { |
| 1367 |
encMigrationAuth[i] = encAuthDataKey2[i] ^ migrationAuth[i] |
| 1368 |
} |
| 1369 |
|
| 1370 |
rParams := rsaKeyParams{ |
| 1371 |
KeyLength: 2048, |
| 1372 |
NumPrimes: 2, |
| 1373 |
} |
| 1374 |
rParamsPacked, err := tpmutil.Pack(&rParams) |
| 1375 |
if err != nil { |
| 1376 |
return nil, err |
| 1377 |
} |
| 1378 |
|
| 1379 |
var pcrInfoBytes []byte |
| 1380 |
if len(pcrs) > 0 { |
| 1381 |
pcrInfo, err := newPCRInfo(rw, pcrs) |
| 1382 |
if err != nil { |
| 1383 |
return nil, err |
| 1384 |
} |
| 1385 |
pcrInfoBytes, err = tpmutil.Pack(pcrInfo) |
| 1386 |
if err != nil { |
| 1387 |
return nil, err |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
keyInfo := &key{ |
| 1392 |
Version: 0x01010000, |
| 1393 |
KeyUsage: keySigning, |
| 1394 |
KeyFlags: keyFlags, |
| 1395 |
AuthDataUsage: authAlways, |
| 1396 |
AlgorithmParams: keyParams{ |
| 1397 |
AlgID: AlgRSA, |
| 1398 |
EncScheme: esNone, |
| 1399 |
SigScheme: ssRSASaPKCS1v15DER, |
| 1400 |
Params: rParamsPacked, |
| 1401 |
}, |
| 1402 |
PCRInfo: pcrInfoBytes, |
| 1403 |
} |
| 1404 |
|
| 1405 |
authIn := []interface{}{ordCreateWrapKey, encUsageAuth, encMigrationAuth, keyInfo} |
| 1406 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, &nonceOdd, sharedSecret[:], authIn) |
| 1407 |
if err != nil { |
| 1408 |
return nil, err |
| 1409 |
} |
| 1410 |
|
| 1411 |
k, ra, ret, err := createWrapKey(rw, encUsageAuth, encMigrationAuth, keyInfo, ca) |
| 1412 |
if err != nil { |
| 1413 |
return nil, err |
| 1414 |
} |
| 1415 |
|
| 1416 |
raIn := []interface{}{ret, ordCreateWrapKey, k} |
| 1417 |
if err = ra.verify(ca.NonceOdd, sharedSecret[:], raIn); err != nil { |
| 1418 |
return nil, err |
| 1419 |
} |
| 1420 |
|
| 1421 |
return k, nil |
| 1422 |
} |
| 1423 |
|
| 1424 |
// CreateWrapKey creates a new RSA key for signatures inside the TPM. It is |
| 1425 |
// wrapped by the SRK (which is to say, the SRK is the parent key). The key can |
| 1426 |
// be bound to the specified PCR numbers so that it can only be used for |
| 1427 |
// signing if the PCR values of those registers match. The pcrs parameter can |
| 1428 |
// be nil in which case the key is not bound to any PCRs. The usageAuth |
| 1429 |
// parameter defines the auth key for using this new key. The migrationAuth |
| 1430 |
// parameter would be used for authorizing migration of the key (although this |
| 1431 |
// code currently disables migration). |
| 1432 |
func CreateWrapKey(rw io.ReadWriter, srkAuth []byte, usageAuth Digest, migrationAuth Digest, pcrs []int) ([]byte, error) { |
| 1433 |
k, err := createWrapKeyHelper(rw, srkAuth, 0, usageAuth, migrationAuth, pcrs) |
| 1434 |
if err != nil { |
| 1435 |
return nil, err |
| 1436 |
} |
| 1437 |
keyblob, err := tpmutil.Pack(k) |
| 1438 |
if err != nil { |
| 1439 |
return nil, err |
| 1440 |
} |
| 1441 |
return keyblob, nil |
| 1442 |
} |
| 1443 |
|
| 1444 |
// CreateMigratableWrapKey creates a new RSA key as in CreateWrapKey, but the |
| 1445 |
// key is migratable (with the given migration auth). |
| 1446 |
// Returns the loadable KeyBlob as well as just the encrypted private part, for |
| 1447 |
// migration. |
| 1448 |
func CreateMigratableWrapKey(rw io.ReadWriter, srkAuth []byte, usageAuth Digest, migrationAuth Digest, pcrs []int) ([]byte, []byte, error) { |
| 1449 |
k, err := createWrapKeyHelper(rw, srkAuth, keyMigratable, usageAuth, migrationAuth, pcrs) |
| 1450 |
if err != nil { |
| 1451 |
return nil, nil, err |
| 1452 |
} |
| 1453 |
keyblob, err := tpmutil.Pack(k) |
| 1454 |
if err != nil { |
| 1455 |
return nil, nil, err |
| 1456 |
} |
| 1457 |
return keyblob, k.EncData, nil |
| 1458 |
} |
| 1459 |
|
| 1460 |
// AuthorizeMigrationKey authorizes a given public key for use in migrating |
| 1461 |
// migratable keys. The scheme is REWRAP. |
| 1462 |
func AuthorizeMigrationKey(rw io.ReadWriter, ownerAuth Digest, migrationKey crypto.PublicKey) ([]byte, error) { |
| 1463 |
// Run OSAP for the OwnerAuth, reading a random OddOSAP for our initial |
| 1464 |
// command and getting back a secret and a handle. |
| 1465 |
sharedSecret, osapr, err := newOSAPSession(rw, etOwner, khOwner, ownerAuth[:]) |
| 1466 |
if err != nil { |
| 1467 |
return nil, err |
| 1468 |
} |
| 1469 |
defer osapr.Close(rw) |
| 1470 |
defer zeroBytes(sharedSecret[:]) |
| 1471 |
|
| 1472 |
var pub *pubKey |
| 1473 |
if migrationKey != nil { |
| 1474 |
pub, err = convertPubKey(migrationKey) |
| 1475 |
if err != nil { |
| 1476 |
return nil, err |
| 1477 |
} |
| 1478 |
// convertPubKey is designed for signing keys. |
| 1479 |
pub.AlgorithmParams.EncScheme = esRSAEsOAEPSHA1MGF1 |
| 1480 |
pub.AlgorithmParams.SigScheme = ssNone |
| 1481 |
rsaParams := rsaKeyParams{ |
| 1482 |
KeyLength: 2048, |
| 1483 |
NumPrimes: 2, |
| 1484 |
//Exponent: default (omit) |
| 1485 |
} |
| 1486 |
pub.AlgorithmParams.Params, err = tpmutil.Pack(rsaParams) |
| 1487 |
if err != nil { |
| 1488 |
return nil, err |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
scheme := msRewrap |
| 1493 |
|
| 1494 |
// The digest for auth for the authorizeMigrationKey command is computed as |
| 1495 |
// SHA1(ordAuthorizeMigrationkey || migrationScheme || migrationKey) |
| 1496 |
authIn := []interface{}{ordAuthorizeMigrationKey, scheme, pub} |
| 1497 |
|
| 1498 |
// The commandAuth uses the shared secret as an HMAC key. |
| 1499 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 1500 |
if err != nil { |
| 1501 |
return nil, err |
| 1502 |
} |
| 1503 |
|
| 1504 |
migrationAuth, _, _, err := authorizeMigrationKey(rw, scheme, *pub, ca) |
| 1505 |
if err != nil { |
| 1506 |
return nil, err |
| 1507 |
} |
| 1508 |
|
| 1509 |
// For now, ignore the response authentication. |
| 1510 |
return migrationAuth, nil |
| 1511 |
} |
| 1512 |
|
| 1513 |
// CreateMigrationBlob performs a Rewrap migration of the given key blob. |
| 1514 |
func CreateMigrationBlob(rw io.ReadWriter, srkAuth Digest, migrationAuth Digest, keyBlob []byte, migrationKeyBlob []byte) ([]byte, error) { |
| 1515 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 1516 |
// command and getting back a secret and a handle. |
| 1517 |
sharedSecret, osapr, err := newOSAPSession(rw, etSRK, khSRK, srkAuth[:]) |
| 1518 |
if err != nil { |
| 1519 |
return nil, err |
| 1520 |
} |
| 1521 |
defer osapr.Close(rw) |
| 1522 |
defer zeroBytes(sharedSecret[:]) |
| 1523 |
|
| 1524 |
// The createMigrationBlob command needs an OIAP session in addition to the |
| 1525 |
// OSAP session. |
| 1526 |
oiapr, err := oiap(rw) |
| 1527 |
if err != nil { |
| 1528 |
return nil, err |
| 1529 |
} |
| 1530 |
defer oiapr.Close(rw) |
| 1531 |
|
| 1532 |
encData := tpmutil.U32Bytes(keyBlob) |
| 1533 |
|
| 1534 |
// The digest for auth1 and auth2 for the createMigrationBlob command is |
| 1535 |
// SHA1(ordCreateMigrationBlob || migrationScheme || migrationKeyBlob || encData) |
| 1536 |
authIn := []interface{}{ordCreateMigrationBlob, msRewrap, migrationKeyBlob, encData} |
| 1537 |
|
| 1538 |
// The first commandAuth uses the shared secret as an HMAC key. |
| 1539 |
ca1, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 1540 |
if err != nil { |
| 1541 |
return nil, err |
| 1542 |
} |
| 1543 |
|
| 1544 |
// The second commandAuth is based on OIAP instead of OSAP and uses the |
| 1545 |
// migration auth as the HMAC key. |
| 1546 |
ca2, err := newCommandAuth(oiapr.AuthHandle, oiapr.NonceEven, nil, migrationAuth[:], authIn) |
| 1547 |
if err != nil { |
| 1548 |
return nil, err |
| 1549 |
} |
| 1550 |
|
| 1551 |
_, outData, _, _, _, err := createMigrationBlob(rw, khSRK, msRewrap, migrationKeyBlob, encData, ca1, ca2) |
| 1552 |
if err != nil { |
| 1553 |
return nil, err |
| 1554 |
} |
| 1555 |
|
| 1556 |
// For now, ignore the response authenticatino. |
| 1557 |
return outData, nil |
| 1558 |
} |
| 1559 |
|
| 1560 |
// https://golang.org/src/crypto/rsa/pkcs1v15.go?s=8762:8862#L204 |
| 1561 |
var hashPrefixes = map[crypto.Hash][]byte{ |
| 1562 |
crypto.MD5: {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10}, |
| 1563 |
crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14}, |
| 1564 |
crypto.SHA224: {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}, |
| 1565 |
crypto.SHA256: {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}, |
| 1566 |
crypto.SHA384: {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}, |
| 1567 |
crypto.SHA512: {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}, |
| 1568 |
crypto.MD5SHA1: {}, // A special TLS case which doesn't use an ASN1 prefix. |
| 1569 |
crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14}, |
| 1570 |
} |
| 1571 |
|
| 1572 |
// Sign will sign a digest using the supplied key handle. Uses PKCS1v15 signing, which means the hash OID is prefixed to the |
| 1573 |
// hash before it is signed. Therefore the hash used needs to be passed as the hash parameter to determine the right |
| 1574 |
// prefix. |
| 1575 |
func Sign(rw io.ReadWriter, keyAuth []byte, keyHandle tpmutil.Handle, hash crypto.Hash, hashed []byte) ([]byte, error) { |
| 1576 |
prefix, ok := hashPrefixes[hash] |
| 1577 |
if !ok { |
| 1578 |
return nil, errors.New("Unsupported hash") |
| 1579 |
} |
| 1580 |
data := append(prefix, hashed...) |
| 1581 |
|
| 1582 |
// Run OSAP for the SRK, reading a random OddOSAP for our initial |
| 1583 |
// command and getting back a secret and a handle. |
| 1584 |
sharedSecret, osapr, err := newOSAPSession(rw, etKeyHandle, keyHandle, keyAuth) |
| 1585 |
if err != nil { |
| 1586 |
return nil, err |
| 1587 |
} |
| 1588 |
defer osapr.Close(rw) |
| 1589 |
defer zeroBytes(sharedSecret[:]) |
| 1590 |
|
| 1591 |
authIn := []interface{}{ordSign, tpmutil.U32Bytes(data)} |
| 1592 |
ca, err := newCommandAuth(osapr.AuthHandle, osapr.NonceEven, nil, sharedSecret[:], authIn) |
| 1593 |
if err != nil { |
| 1594 |
return nil, err |
| 1595 |
} |
| 1596 |
|
| 1597 |
signature, ra, ret, err := sign(rw, keyHandle, data, ca) |
| 1598 |
if err != nil { |
| 1599 |
return nil, err |
| 1600 |
} |
| 1601 |
|
| 1602 |
raIn := []interface{}{ret, ordSign, tpmutil.U32Bytes(signature)} |
| 1603 |
err = ra.verify(ca.NonceOdd, sharedSecret[:], raIn) |
| 1604 |
if err != nil { |
| 1605 |
return nil, err |
| 1606 |
} |
| 1607 |
|
| 1608 |
return signature, nil |
| 1609 |
} |
| 1610 |
|
| 1611 |
// PcrReset resets the given PCRs. Given typical locality restrictions, this can usually only be 16 or 23. |
| 1612 |
func PcrReset(rw io.ReadWriter, pcrs []int) error { |
| 1613 |
pcrSelect, err := newPCRSelection(pcrs) |
| 1614 |
if err != nil { |
| 1615 |
return err |
| 1616 |
} |
| 1617 |
err = pcrReset(rw, pcrSelect) |
| 1618 |
if err != nil { |
| 1619 |
return err |
| 1620 |
} |
| 1621 |
return nil |
| 1622 |
} |
| 1623 |
|
| 1624 |
// ForceClear is normally used by firmware but on some platforms |
| 1625 |
// vendors got it wrong and didn't call TPM_DisableForceClear. |
| 1626 |
// It removes forcefully the ownership of the TPM. |
| 1627 |
func ForceClear(rw io.ReadWriter) error { |
| 1628 |
in := []interface{}{} |
| 1629 |
out := []interface{}{} |
| 1630 |
_, err := submitTPMRequest(rw, tagRQUCommand, ordForceClear, in, out) |
| 1631 |
|
| 1632 |
return err |
| 1633 |
} |
| 1634 |
|
| 1635 |
// Startup performs TPM_Startup(TPM_ST_CLEAR) to initialize the TPM. |
| 1636 |
func startup(rw io.ReadWriter) error { |
| 1637 |
var typ uint16 = 0x0001 // TPM_ST_CLEAR |
| 1638 |
in := []interface{}{typ} |
| 1639 |
out := []interface{}{} |
| 1640 |
_, err := submitTPMRequest(rw, tagRQUCommand, ordStartup, in, out) |
| 1641 |
|
| 1642 |
return err |
| 1643 |
} |
| 1644 |
|
| 1645 |
// createEK performs TPM_CreateEndorsementKeyPair to create the EK in the TPM. |
| 1646 |
func createEK(rw io.ReadWriter) error { |
| 1647 |
antiReplay := Nonce{} |
| 1648 |
keyInfo := []byte{ |
| 1649 |
0x00, 0x00, 0x00, 0x01, // Algorithm = RSA |
| 1650 |
0x00, 0x03, // EncScheme = OAEP |
| 1651 |
0x00, 0x01, // SigScheme = None |
| 1652 |
0x00, 0x00, 0x00, 0x0c, // ParamsSize = 12 |
| 1653 |
0x00, 0x00, 0x08, 0x00, // KeyLength = 2048 |
| 1654 |
0x00, 0x00, 0x00, 0x02, // NumPrimes = 2 |
| 1655 |
0x00, 0x00, 0x00, 0x00, // ExponentSize = 0 (default 65537 exponent) |
| 1656 |
} |
| 1657 |
in := []interface{}{antiReplay, keyInfo} |
| 1658 |
out := []interface{}{} |
| 1659 |
_, err := submitTPMRequest(rw, tagRQUCommand, ordCreateEndorsementKeyPair, in, out) |
| 1660 |
|
| 1661 |
return err |
| 1662 |
} |