File: loadablekey.go

package info (click to toggle)
golang-github-foxboron-go-tpm-keyfiles 0.0~git20241207.04534a2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 9
file content (42 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download
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
package keyfile

import (
	"github.com/google/go-tpm/tpm2"
	"github.com/google/go-tpm/tpm2/transport"
)

// TODO: Do we want a new struct to represent these?
// type LoadableTPMKey struct {
// 	*LoadableTPMKey
// }

// NewLoadableKey creates a new LoadableKey
func NewLoadableKey(tpm transport.TPMCloser, alg tpm2.TPMAlgID, bits int, ownerauth []byte, fn ...TPMKeyOption) (*TPMKey, error) {
	tpmkey, _, err := NewLoadableKeyWithResponse(tpm, alg, bits, ownerauth, fn...)
	return tpmkey, err
}

// NewLoadableKeyWithResponse creates a new LoadableKey and returns the tpm2.CreateResponse
func NewLoadableKeyWithResponse(tpm transport.TPMCloser, alg tpm2.TPMAlgID, bits int, ownerauth []byte, fn ...TPMKeyOption) (*TPMKey, *tpm2.CreateResponse, error) {
	sess := NewTPMSession(tpm)
	key := NewTPMKey(OIDLoadableKey, tpm2.TPM2BPublic{}, tpm2.TPM2BPrivate{}, fn...)

	parenthandle, err := GetParentHandle(sess, key.Parent, ownerauth)
	if err != nil {
		return nil, nil, err
	}

	defer sess.FlushHandle()

	rsp, err := createKeyWithHandle(sess, *parenthandle, alg, bits, ownerauth, key.userAuth)
	if err != nil {
		return nil, nil, err
	}

	// Add the remaining options to complete the key
	key.AddOptions(
		WithPubkey(rsp.OutPublic),
		WithPrivkey(rsp.OutPrivate),
	)
	return key, rsp, nil
}