Debian Codegraph @

client/session.go

raw
1 package client
2
3 import (
4 "io"
5
6 "github.com/google/go-tpm/legacy/tpm2"
7 "github.com/google/go-tpm/tpmutil"
8 )
9
10 // Session is an interface for TPM sessions.
11 type Session interface {
12 io.Closer
13 Auth() (tpm2.AuthCommand, error)
14 }
15
16 func startAuthSession(rw io.ReadWriter) (session tpmutil.Handle, err error) {
17 // This session assumes the bus is trusted, so we:
18 // - use nil for tpmKey, encrypted salt, and symmetric
19 // - use and all-zeros caller nonce, and ignore the returned nonce
20 // As we are creating a plain TPM session, we:
21 // - setup a policy session
22 // - don't bind the session to any particular key
23 session, _, err = tpm2.StartAuthSession(
24 rw,
25 /*tpmKey=*/ tpm2.HandleNull,
26 /*bindKey=*/ tpm2.HandleNull,
27 /*nonceCaller=*/ make([]byte, SessionHashAlg.Size()),
28 /*encryptedSalt=*/ nil,
29 /*sessionType=*/ tpm2.SessionPolicy,
30 /*symmetric=*/ tpm2.AlgNull,
31 /*authHash=*/ SessionHashAlgTpm)
32 return
33 }
34
35 // PCRSession is a TPM session that is bound to a set of PCRs.
36 type PCRSession struct {
37 rw io.ReadWriter
38 session tpmutil.Handle
39 sel tpm2.PCRSelection
40 }
41
42 // NewPCRSession creates a new PCRSession.
43 func NewPCRSession(rw io.ReadWriter, sel tpm2.PCRSelection) (Session, error) {
44 if len(sel.PCRs) == 0 {
45 return NullSession{}, nil
46 }
47 session, err := startAuthSession(rw)
48 return PCRSession{rw, session, sel}, err
49 }
50
51 // Auth returns the AuthCommand for the session.
52 func (p PCRSession) Auth() (auth tpm2.AuthCommand, err error) {
53 if err = tpm2.PolicyPCR(p.rw, p.session, nil, p.sel); err != nil {
54 return
55 }
56 return tpm2.AuthCommand{Session: p.session, Attributes: tpm2.AttrContinueSession}, nil
57 }
58
59 // Close closes the session.
60 func (p PCRSession) Close() error {
61 return tpm2.FlushContext(p.rw, p.session)
62 }
63
64 // EKSession is a TPM session that is bound to the EK.
65 type EKSession struct {
66 rw io.ReadWriter
67 session tpmutil.Handle
68 }
69
70 // NewEKSession creates a new EKSession.
71 func NewEKSession(rw io.ReadWriter) (Session, error) {
72 session, err := startAuthSession(rw)
73 return EKSession{rw, session}, err
74 }
75
76 // Auth returns the AuthCommand for the session.
77 func (e EKSession) Auth() (auth tpm2.AuthCommand, err error) {
78 nullAuth := tpm2.AuthCommand{Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession}
79 if _, _, err = tpm2.PolicySecret(e.rw, tpm2.HandleEndorsement, nullAuth, e.session, nil, nil, nil, 0); err != nil {
80 return
81 }
82 return tpm2.AuthCommand{Session: e.session, Attributes: tpm2.AttrContinueSession}, nil
83 }
84
85 // Close closes the session.
86 func (e EKSession) Close() error {
87 return tpm2.FlushContext(e.rw, e.session)
88 }
89
90 // NullSession is a TPM session that is not bound to anything.
91 type NullSession struct{}
92
93 // Auth returns the AuthCommand for the session.
94 func (n NullSession) Auth() (auth tpm2.AuthCommand, err error) {
95 return tpm2.AuthCommand{Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession}, nil
96 }
97
98 // Close closes the session.
99 func (n NullSession) Close() error {
100 return nil
101 }