1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
package tpm
import (
"context"
"crypto"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/smallstep/go-attestation/attest"
internalkey "go.step.sm/crypto/tpm/internal/key"
"go.step.sm/crypto/tpm/storage"
)
// Key models a TPM 2.0 Key. A Key can be used
// to sign data. When a Key is created, it can be
// attested by an AK, to be able to prove that it
// was created by a specific TPM.
type Key struct {
name string
data []byte
attestedBy string
chain []*x509.Certificate
createdAt time.Time
blobs *Blobs
tpm *TPM
}
// Name returns the Key name. The name uniquely
// identifies the Key if a TPM with persistent
// storage is used.
func (k *Key) Name() string {
return k.name
}
// Data returns the Key data blob. The data blob
// contains all information required for the Key
// to be loaded into the TPM that created it again,
// so that it can be used for signing data.
func (k *Key) Data() []byte {
return k.data
}
// AttestedBy returns the name of the AK the Key was
// attested (certified) by at creation time.
func (k *Key) AttestedBy() string {
return k.attestedBy
}
// WasAttested returns whether or not the Key was
// attested (certified) by an AK at creation time.
func (k *Key) WasAttested() bool {
return k.attestedBy != ""
}
// WasAttestedBy returns whether or not the Key
// was attested (certified) by the provided AK
// at creation time.
func (k *Key) WasAttestedBy(ak *AK) bool {
return k.attestedBy == ak.name
}
// Certificate returns the certificate for the Key, if set.
// Will return nil in case no AK certificate is available.
func (k *Key) Certificate() *x509.Certificate {
if len(k.chain) == 0 {
return nil
}
return k.chain[0]
}
// CertificateChain returns the certificate chain for the Key.
// It can return an empty chain.
func (k *Key) CertificateChain() []*x509.Certificate {
return k.chain
}
// CreatedAt returns the the creation time of the Key.
func (k *Key) CreatedAt() time.Time {
return k.createdAt.Truncate(time.Second)
}
// MarshalJSON marshals the Key to JSON.
func (k *Key) MarshalJSON() ([]byte, error) {
chain := make([][]byte, len(k.chain))
for i, cert := range k.chain {
chain[i] = cert.Raw
}
o := struct {
Name string `json:"name"`
Data []byte `json:"data"`
AttestedBy string `json:"attestedBy,omitempty"`
Chain [][]byte `json:"chain,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}{
Name: k.name,
Data: k.data,
AttestedBy: k.attestedBy,
Chain: chain,
CreatedAt: k.createdAt,
}
return json.Marshal(o)
}
// comparablePublicKey is an interface that allows a crypto.PublicKey to be
// compared to another crypto.PublicKey.
type comparablePublicKey interface {
Equal(crypto.PublicKey) bool
}
// CreateKeyConfig is used to pass configuration
// when creating Keys.
type CreateKeyConfig struct {
// Algorithm to be used, either RSA or ECDSA.
Algorithm string
// Size is used to specify the bit size of the key or elliptic curve. For
// example, '256' is used to specify curve P-256.
Size int
// TODO(hs): move key name to this struct?
}
// AttestKeyConfig is used to pass configuration
// when creating Keys that are to be attested by
// an AK.
type AttestKeyConfig struct {
// Algorithm to be used, either RSA or ECDSA.
Algorithm string
// Size is used to specify the bit size of the key or elliptic curve. For
// example, '256' is used to specify curve P-256.
Size int
// QualifyingData is additional data that is passed to the TPM.
// It can be used as a nonce to ensure freshness of an attestation.
// When used with ACME `device-attest-01`, this contains a hash of
// the key authorization.
QualifyingData []byte
// TODO(hs): add akName and key name to this struct?
}
// CreateKey creates a new Key identified by `name`. If no name is provided,
// a random 10 character name is generated. If a Key with the same name exists,
// `ErrExists` is returned. The Key won't be attested by an AK.
func (t *TPM) CreateKey(ctx context.Context, name string, config CreateKeyConfig) (key *Key, err error) {
if err = t.open(goTPMCall(ctx)); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
now := time.Now()
if name, err = processName(name); err != nil {
return nil, err
}
_, err = t.store.GetKey(name)
switch {
case err == nil:
return nil, fmt.Errorf("failed creating key %q: %w", name, ErrExists)
case errors.Is(err, storage.ErrNoStorageConfigured):
return nil, fmt.Errorf("failed creating key %q: %w", name, err)
}
createConfig := internalkey.CreateConfig{
Algorithm: config.Algorithm,
Size: config.Size,
}
if err := t.validate(&createConfig); err != nil {
return nil, fmt.Errorf("invalid key creation parameters: %w", err)
}
data, err := internalkey.Create(t.rwc, prefixKey(name), createConfig)
if err != nil {
return nil, fmt.Errorf("failed creating key %q: %w", name, err)
}
key = &Key{
name: name,
data: data,
createdAt: now,
tpm: t,
}
if err := t.store.AddKey(key.toStorage()); err != nil {
return nil, fmt.Errorf("failed adding key %q to storage: %w", name, err)
}
if err := t.store.Persist(); err != nil {
return nil, fmt.Errorf("failed persisting key %q to storage: %w", name, err)
}
return
}
type attestValidationWrapper attest.KeyConfig
func (w attestValidationWrapper) Validate() error {
switch w.Algorithm {
case "RSA":
if w.Size > 2048 {
return fmt.Errorf("%d bits RSA keys are (currently) not supported in go.step.sm/crypto; maximum is 2048", w.Size)
}
case "ECDSA":
break
default:
return fmt.Errorf("unsupported algorithm %q", w.Algorithm)
}
return nil
}
// AttestKey creates a new Key identified by `name` and attested by the AK
// identified by `akName`. If no name is provided, a random 10 character
// name is generated. If a Key with the same name exists, `ErrExists` is
// returned.
func (t *TPM) AttestKey(ctx context.Context, akName, name string, config AttestKeyConfig) (key *Key, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
now := time.Now()
if name, err = processName(name); err != nil {
return nil, err
}
_, err = t.store.GetKey(name)
switch {
case err == nil:
return nil, fmt.Errorf("failed creating key %q: %w", name, ErrExists)
case errors.Is(err, storage.ErrNoStorageConfigured):
return nil, fmt.Errorf("failed creating key %q: %w", name, err)
}
ak, err := t.store.GetAK(akName)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting AK %q: %w", akName, ErrNotFound)
}
return nil, fmt.Errorf("failed getting AK %q: %w", akName, err)
}
loadedAK, err := t.attestTPM.LoadAK(ak.Data)
if err != nil {
return nil, fmt.Errorf("failed loading AK %q: %w", akName, err)
}
defer loadedAK.Close(t.attestTPM)
keyConfig := attest.KeyConfig{
Algorithm: attest.Algorithm(config.Algorithm),
Size: config.Size,
QualifyingData: config.QualifyingData,
Name: prefixKey(name),
}
if err := t.validate(attestValidationWrapper(keyConfig)); err != nil {
return nil, fmt.Errorf("invalid key attestation parameters: %w", err)
}
akey, err := t.attestTPM.NewKey(loadedAK, &keyConfig)
if err != nil {
return nil, fmt.Errorf("failed creating key %q: %w", name, err)
}
defer akey.Close()
data, err := akey.Marshal()
if err != nil {
return nil, fmt.Errorf("failed marshaling key %q: %w", name, err)
}
key = &Key{
name: name,
data: data,
attestedBy: akName,
createdAt: now,
tpm: t,
}
if err := t.store.AddKey(key.toStorage()); err != nil {
return nil, fmt.Errorf("failed adding key %q to storage: %w", name, err)
}
if err := t.store.Persist(); err != nil {
return nil, fmt.Errorf("failed persisting key %q: %w", name, err)
}
return
}
// GetKey returns the Key identified by `name`. It returns `ErrNotfound`
// if it doesn't exist.
func (t *TPM) GetKey(ctx context.Context, name string) (key *Key, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
skey, err := t.store.GetKey(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting key %q: %w", name, ErrNotFound)
}
return nil, fmt.Errorf("failed getting key %q: %w", name, err)
}
return keyFromStorage(skey, t), nil
}
// ListKeys returns a slice of Keys. The result is (currently)
// not ordered.
func (t *TPM) ListKeys(ctx context.Context) (keys []*Key, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
skeys, err := t.store.ListKeys()
if err != nil {
return nil, fmt.Errorf("failed listing keys: %w", err)
}
keys = make([]*Key, 0, len(skeys))
for _, skey := range skeys {
keys = append(keys, keyFromStorage(skey, t))
}
return
}
// GetKeysAttestedBy returns a slice of Keys attested by the AK
// identified by `akName`. The result is (currently) not ordered.
func (t *TPM) GetKeysAttestedBy(ctx context.Context, akName string) (keys []*Key, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
skeys, err := t.store.ListKeys()
if err != nil {
return nil, fmt.Errorf("failed listing keys: %w", err)
}
keys = make([]*Key, 0, len(skeys))
for _, skey := range skeys {
if skey.AttestedBy == akName {
keys = append(keys, keyFromStorage(skey, t))
}
}
return
}
// DeleteKey removes the Key identified by `name`. It returns `ErrNotfound`
// if it doesn't exist.
func (t *TPM) DeleteKey(ctx context.Context, name string) (err error) {
if err := t.open(ctx); err != nil {
return fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, t, &err)
key, err := t.store.GetKey(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("failed getting key %q: %w", name, ErrNotFound)
}
return fmt.Errorf("failed getting key %q: %w", name, err)
}
if err := t.attestTPM.DeleteKey(key.Data); err != nil {
return fmt.Errorf("failed deleting key %q: %w", name, err)
}
if err := t.store.DeleteKey(name); err != nil {
return fmt.Errorf("failed deleting key %q from storage: %w", name, err)
}
if err := t.store.Persist(); err != nil {
return fmt.Errorf("failed persisting storage: %w", err)
}
return
}
// Signer returns a crypto.Signer backed by the Key.
func (k *Key) Signer(ctx context.Context) (crypto.Signer, error) {
return k.tpm.GetSigner(ctx, k.name)
}
// CertificationParameters returns information about the key that can be used to
// verify key certification.
func (k *Key) CertificationParameters(ctx context.Context) (params attest.CertificationParameters, err error) {
if err = k.tpm.open(ctx); err != nil {
return params, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, k.tpm, &err)
loadedKey, err := k.tpm.attestTPM.LoadKey(k.data)
if err != nil {
return attest.CertificationParameters{}, fmt.Errorf("failed loading key %q: %w", k.name, err)
}
defer loadedKey.Close()
params = loadedKey.CertificationParameters()
return
}
// Blobs returns a container for the private and public key blobs.
// The resulting blobs are compatible with tpm2-tools, so can be used
// like this (after having been written to key.priv and key.pub):
//
// tpm2_load -C 0x81000001 -u key.pub -r key.priv -c key.ctx
func (k *Key) Blobs(ctx context.Context) (blobs *Blobs, err error) {
if k.blobs != nil {
return k.blobs, nil
}
if err = k.tpm.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, k.tpm, &err)
key, err := k.tpm.attestTPM.LoadKey(k.data)
if err != nil {
return nil, fmt.Errorf("failed loading key: %w", err)
}
defer key.Close()
public, private, err := key.Blobs()
if err != nil {
return nil, fmt.Errorf("failed getting key blobs: %w", err)
}
k.setBlobs(private, public)
return k.blobs, nil
}
// SetCertificateChain associates an X.509 certificate chain with the Key.
// If the public key doesn't match the public key in the first certificate
// in the chain (the leaf), an error is returned.
func (k *Key) SetCertificateChain(ctx context.Context, chain []*x509.Certificate) (err error) {
if err = k.tpm.open(ctx); err != nil {
return fmt.Errorf("failed opening TPM: %w", err)
}
defer closeTPM(ctx, k.tpm, &err)
signer, err := k.Signer(internalCall(ctx)) // TODO: cache the crypto.PublicKey after its first load instead?
if err != nil {
return fmt.Errorf("failed getting signer for key: %w", err)
}
if len(chain) > 0 {
leaf := chain[0]
leafPK, ok := leaf.PublicKey.(crypto.PublicKey)
if !ok {
return fmt.Errorf("unexpected type for certificate public key: %T", leaf.PublicKey)
}
publicKey, ok := leafPK.(comparablePublicKey)
if !ok {
return errors.New("certificate public key can't be compared to a crypto.PublicKey")
}
if !publicKey.Equal(signer.Public()) {
return errors.New("public key does not match the leaf certificate public key")
}
}
k.chain = chain // TODO(hs): deep copy, so that certs can't be changed by pointer?
if err := k.tpm.store.UpdateKey(k.toStorage()); err != nil {
return fmt.Errorf("failed updating key %q: %w", k.name, err)
}
return
}
// toStorage transforms the Key to the struct used for
// persisting Keys.
func (k *Key) toStorage() *storage.Key {
return &storage.Key{
Name: k.name,
Data: k.data,
AttestedBy: k.attestedBy,
Chain: k.chain,
CreatedAt: k.createdAt.UTC(),
}
}
// keyFromStorage recreates a Key from the struct used for
// persisting Keys.
func keyFromStorage(sk *storage.Key, t *TPM) *Key {
return &Key{
name: sk.Name,
data: sk.Data,
attestedBy: sk.AttestedBy,
chain: sk.Chain,
createdAt: sk.CreatedAt.Local(),
tpm: t,
}
}
|