Skip to content

Commit fad3918

Browse files
committed
revert internal signer for now
1 parent caf928d commit fad3918

File tree

5 files changed

+24
-374
lines changed

5 files changed

+24
-374
lines changed

README.md

+1-23
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ see the [example/](example/) folder for more information.
2121

2222
---
2323

24-
25-
2624
>> this library is not supported by google
2725
2826
---
@@ -77,11 +75,8 @@ If you just want to issue JWT's, see
7775

7876
### TPM Signer Device management
7977

80-
>> **NOTE** there will be a breaking change if you are using this library for TPM based signature after `v0.8.0`. The new structure uses the [tpm-direct](https://github.com/google/go-tpm/releases/tag/v0.9.0) API. If you would rather use the tpm2/legacy branch, please use the signer at [v0.7.2](https://github.com/salrashid123/signer/releases/tag/v0.7.2). While this repo still retain managed and unmanaged handles to the TPM device, its recommended to to manage it externally if you need complex authorization...if its simple authorization like pcr and password or if you need concurrent, non blocking of the TPM device, use library managed handle. For externally manged, just remember to open-sign-close as the device is locking.
78+
>> **NOTE** there will be a breaking change if you are using this library for TPM based signature after `v0.8.0`. The new structure uses the [tpm-direct](https://github.com/google/go-tpm/releases/tag/v0.9.0) API. If you would rather use the tpm2/legacy branch, please use the signer at [v0.7.2](https://github.com/salrashid123/signer/releases/tag/v0.7.2). Library managed device was removed (it seems tpm resource managers work well enough...I'm clearly on the fence here given the recent commits..)
8179
82-
For TPM Signer, there are two modes of operation:
83-
84-
* managed externally
8580

8681
The TPM device is managed externally outside of the signer. You have to instantiate the TPM device ReadWriteCloser and client.Key outside of the library and pass that in.
8782

@@ -107,23 +102,6 @@ For TPM Signer, there are two modes of operation:
107102
s, err := r.Sign(rand.Reader, digest, crypto.SHA256)
108103
```
109104

110-
* managed by library
111-
112-
This is the preferred mode: you just pass the uint32 handle for the key and the path to the tpm device as string and the library opens/closes it as needed.
113-
114-
If the device is busy or the TPM is in use during invocation, the operation will fail.
115-
116-
```golang
117-
r, err := saltpm.NewTPMCrypto(&saltpm.TPM{
118-
TpmPath: *tpmPath,
119-
KeyHandle: tpm2.TPMHandle(*handle).HandleValue(),
120-
PCRs: []uint{},
121-
AuthPassword: []byte(""),
122-
})
123-
124-
// the tpm is opened and then closed after every sign operation
125-
s, err := r.Sign(rand.Reader, digest, crypto.SHA256)
126-
```
127105

128106
TODO use a backoff retry similar to [tpmrand](https://github.com/salrashid123/tpmrand) to prevent contention.
129107

example/README.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ First install latest `tpm2_tools`
3939
```bash
4040
cd example/
4141

42+
## if you want to use a software TPM,
4243
# rm -rf /tmp/myvtpm && mkdir /tmp/myvtpm
4344
# sudo swtpm socket --tpmstate dir=/tmp/myvtpm --tpm2 --server type=tcp,port=2321 --ctrl type=tcp,port=2322 --flags not-need-init,startup-clear
45+
46+
## then specify "127.0.0.1:2321" as the TPM device path in the examples
47+
## then for tpm2_tools, export the following var
4448
# export TPM2TOOLS_TCTI="swtpm:port=2321"
4549

50+
## note if you want, the primary can be the "H2" profile from https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#name-parent
51+
## see https://gist.github.com/salrashid123/9822b151ebb66f4083c5f71fd4cdbe40
52+
# printf '\x00\x00' > unique.dat
53+
# tpm2_createprimary -C o -G ecc -g sha256 -c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
54+
4655

4756
## RSA - no password
4857
tpm2_createprimary -C o -G rsa2048:aes128cfb -g sha256 -c primary.ctx -a 'restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda'
@@ -92,19 +101,17 @@ cd example/
92101
cd example/
93102

94103
## RSA-SSA managed externally
95-
go run sign_verify_tpm/rsassa/main.go --handle=0x81008001
96-
97-
## RSA-SSA managed by library
98-
go run sign_verify_tpm/rsassa_managed/main.go --handle=0x81008001
104+
go run sign_verify_tpm/rsassa/main.go --handle=0x81008001 --tpm-path="127.0.0.1:2321"
99105

100106
## RSA-PSS
101-
go run sign_verify_tpm/rsapss/main.go --handle=0x81008004
107+
go run sign_verify_tpm/rsapss/main.go --handle=0x81008004 --tpm-path="127.0.0.1:2321"
102108

103109
## ECC
104-
go run sign_verify_tpm/ecc/main.go --handle=0x81008005
110+
go run sign_verify_tpm/ecc/main.go --handle=0x81008005 --tpm-path="127.0.0.1:2321"
105111

106112
## RSA with policy
107-
go run sign_verify_tpm/policy/main.go --handle=0x81008006
113+
go run sign_verify_tpm/policy/main.go --handle=0x81008006 --tpm-path="127.0.0.1:2321"
108114
```
109115

116+
---
110117

example/sign_verify_tpm/rsassa_managed/main.go

-87
This file was deleted.

tpm/tpm.go

+9-156
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,10 @@ type TPM struct {
4646
publicKey crypto.PublicKey
4747
tpmPublic tpm2.TPMTPublic
4848

49-
// for externally managed device
5049
AuthHandle *tpm2.AuthHandle // load a key from handle
5150
TpmDevice io.ReadWriteCloser // TPM read closer
5251
EncryptionHandle tpm2.TPMHandle // (optional) handle to use for transit encryption
5352
EncryptionPub *tpm2.TPMTPublic // (optional) public key to use for transit encryption
54-
55-
// for library managed device
56-
TpmPath string // string path to the tpm ("eg: /dev/tpm0")
57-
KeyHandle uint32 // uint value for the persistent handle
58-
PCRs []uint // pcrs to bind to
59-
AuthPassword []byte // auth password
6053
}
6154

6255
var TPMDEVICES = []string{"/dev/tpm0", "/dev/tpmrm0"}
@@ -75,99 +68,17 @@ func OpenTPM(path string) (io.ReadWriteCloser, error) {
7568

7669
func NewTPMCrypto(conf *TPM) (TPM, error) {
7770

78-
if conf.TpmDevice == nil && conf.TpmPath == "" {
79-
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: TpmDevice or TpmPath must be specified")
80-
}
81-
82-
if conf.TpmDevice != nil && conf.TpmPath != "" {
83-
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: only one of TpmDevice or TpmPath must be specified")
71+
if conf.TpmDevice == nil {
72+
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: TpmDevice must be specified")
8473
}
85-
86-
var rwr transport.TPM
87-
var ah *tpm2.AuthHandle
88-
89-
// if an actual device is specified, its externally managed
90-
// so the auth handle shoud've been initialzied before this
91-
if conf.TpmDevice != nil {
92-
if conf.AuthHandle == nil {
93-
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: AuthHandle and TpmDevice must be specified")
94-
}
95-
rwr = transport.FromReadWriter(conf.TpmDevice)
96-
ah = conf.AuthHandle
97-
} else {
98-
// otherwise, its a library managed call
99-
// here we'll open up the tpm and read in the
100-
// persistent handle
101-
// after enabling for if any password or pcr policies, we'll read the public key..then
102-
// wer'e going to close the tpm after this function call
103-
rwc, err := OpenTPM(conf.TpmPath)
104-
if err != nil {
105-
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: TpmDevice or TpmPath must be specified")
106-
}
107-
defer rwc.Close()
108-
109-
if len(conf.AuthPassword) > 0 && len(conf.PCRs) > 0 {
110-
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: only auth or pcr policy is supported...")
111-
}
112-
113-
rwr = transport.FromReadWriter(rwc)
114-
115-
h := tpm2.TPMHandle(conf.KeyHandle)
116-
defer func() {
117-
flushContextCmd := tpm2.FlushContext{
118-
FlushHandle: h,
119-
}
120-
_, _ = flushContextCmd.Execute(rwr)
121-
}()
122-
123-
pub, err := tpm2.ReadPublic{
124-
ObjectHandle: tpm2.TPMHandle(conf.KeyHandle),
125-
}.Execute(rwr)
126-
if err != nil {
127-
return TPM{}, fmt.Errorf("error reading public %v", err)
128-
}
129-
130-
if len(conf.PCRs) > 0 {
131-
sess, cleanup, err := tpm2.PolicySession(rwr, tpm2.TPMAlgSHA256, 16)
132-
if err != nil {
133-
return TPM{}, fmt.Errorf("error creating policy session %v", err)
134-
}
135-
defer cleanup()
136-
137-
_, err = tpm2.PolicyPCR{
138-
PolicySession: sess.Handle(),
139-
Pcrs: tpm2.TPMLPCRSelection{
140-
PCRSelections: []tpm2.TPMSPCRSelection{
141-
{
142-
Hash: tpm2.TPMAlgSHA256,
143-
PCRSelect: tpm2.PCClientCompatible.PCRs(conf.PCRs...),
144-
},
145-
},
146-
},
147-
}.Execute(rwr)
148-
if err != nil {
149-
return TPM{}, fmt.Errorf("error creating policy pcr %v", err)
150-
}
151-
152-
ah = &tpm2.AuthHandle{
153-
Handle: h,
154-
Name: pub.Name,
155-
Auth: sess,
156-
}
157-
158-
} else {
159-
ah = &tpm2.AuthHandle{
160-
Handle: h,
161-
Name: pub.Name,
162-
Auth: tpm2.PasswordAuth(conf.AuthPassword),
163-
}
164-
}
165-
74+
if conf.AuthHandle == nil {
75+
return TPM{}, fmt.Errorf("salrashid123/x/oauth2/google: AuthHandle and TpmDevice must be specified")
16676
}
77+
rwr := transport.FromReadWriter(conf.TpmDevice)
16778

16879
// todo: we should supply the encrypted session here, if set
16980
pub, err := tpm2.ReadPublic{
170-
ObjectHandle: ah.Handle,
81+
ObjectHandle: conf.AuthHandle.Handle,
17182
}.Execute(rwr)
17283
if err != nil {
17384
return TPM{}, fmt.Errorf("google: Unable to Read Public data from TPM: %v", err)
@@ -227,65 +138,7 @@ func (t TPM) Sign(rr io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte,
227138
t.refreshMutex.Lock()
228139
defer t.refreshMutex.Unlock()
229140

230-
var rwr transport.TPM
231-
var ah *tpm2.AuthHandle
232-
233-
// for each signature, check if the device is externally
234-
// managed or not
235-
if t.TpmDevice != nil {
236-
rwr = transport.FromReadWriter(t.TpmDevice)
237-
ah = t.AuthHandle
238-
} else {
239-
// since its internally managed, open, sign and then close
240-
// the device
241-
// we need to reload the key from the handle and apply
242-
// any password or pcr policies before the signature
243-
rwc, err := OpenTPM(t.TpmPath)
244-
if err != nil {
245-
return nil, fmt.Errorf("salrashid123/x/oauth2/google: error opening tpm %v", err)
246-
}
247-
defer rwc.Close()
248-
249-
rwr = transport.FromReadWriter(rwc)
250-
251-
pub, err := tpm2.ReadPublic{
252-
ObjectHandle: tpm2.TPMHandle(t.KeyHandle),
253-
}.Execute(rwr)
254-
if err != nil {
255-
return nil, fmt.Errorf("salrashid123/x/oauth2/google: error executing tpm2.ReadPublic %v", err)
256-
}
257-
258-
if len(t.PCRs) > 0 {
259-
sess, cleanup, err := tpm2.PolicySession(rwr, tpm2.TPMAlgSHA256, 16)
260-
if err != nil {
261-
return nil, fmt.Errorf("error creating policy session %v", err)
262-
}
263-
defer cleanup()
264-
265-
_, err = tpm2.PolicyPCR{
266-
PolicySession: sess.Handle(),
267-
Pcrs: tpm2.TPMLPCRSelection{
268-
PCRSelections: []tpm2.TPMSPCRSelection{
269-
{
270-
Hash: tpm2.TPMAlgSHA256,
271-
PCRSelect: tpm2.PCClientCompatible.PCRs(t.PCRs...),
272-
},
273-
},
274-
},
275-
}.Execute(rwr)
276-
if err != nil {
277-
return nil, fmt.Errorf("error creating policy pcr %v", err)
278-
}
279-
} else {
280-
281-
ah = &tpm2.AuthHandle{
282-
Handle: tpm2.TPMHandle(t.KeyHandle),
283-
Name: pub.Name,
284-
Auth: tpm2.PasswordAuth(t.AuthPassword),
285-
}
286-
}
287-
288-
}
141+
rwr := transport.FromReadWriter(t.TpmDevice)
289142

290143
var sess tpm2.Session
291144

@@ -319,7 +172,7 @@ func (t TPM) Sign(rr io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte,
319172
return nil, fmt.Errorf("tpmjwt: can't error getting rsa details %v", err)
320173
}
321174
rspSign, err := tpm2.Sign{
322-
KeyHandle: *ah,
175+
KeyHandle: *t.AuthHandle,
323176
Digest: tpm2.TPM2BDigest{
324177
Buffer: digest[:],
325178
},
@@ -359,7 +212,7 @@ func (t TPM) Sign(rr io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte,
359212
return nil, fmt.Errorf("tpmjwt: can't error getting rsa details %v", err)
360213
}
361214
rspSign, err := tpm2.Sign{
362-
KeyHandle: *ah,
215+
KeyHandle: *t.AuthHandle,
363216
Digest: tpm2.TPM2BDigest{
364217
Buffer: digest[:],
365218
},

0 commit comments

Comments
 (0)