Skip to content

Commit 24ae211

Browse files
committed
update docs
Signed-off-by: sal rashid <[email protected]>
1 parent f51cb2e commit 24ae211

File tree

1 file changed

+117
-97
lines changed

1 file changed

+117
-97
lines changed

README.md

Lines changed: 117 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
where private keys as embedded inside `Trusted Platform Module (TPM)`
44

5-
Basically, you will get a [crypto.Signer](https://pkg.go.dev/crypto#Signer) interface for the private key..
5+
Basically, you will get a [crypto.Signer](https://pkg.go.dev/crypto#Signer) interface for the private key.
66

77
Use the signer to create a TLS session, sign CA/CSRs, or just sign anything.
88

@@ -54,87 +54,13 @@ import (
5454

5555
### Sign/Verify
5656

57-
see `example/sign_verify_tpm` folder and the Example Setup section below
57+
see `example/sign_verify_tpm` folder.
5858

59-
### Usage TLS
60-
61-
* for tpm see [mTLS with TPM bound private key](https://github.com/salrashid123/go_tpm_https_embed)
62-
63-
### Sign/Verify ECC
64-
65-
The default output signature format for ECC based keys is ASN1 format as described in [ecdsa.SignASN1](https://pkg.go.dev/crypto/ecdsa#Sign)
66-
67-
If you need the raw output format, set `ECCRawOutput: true` in the config.
68-
69-
See the examples folder for usage
70-
71-
### Usage: Generate CSR
72-
73-
The following will generate a TPM based key and then issue a CSR against it.
74-
75-
```bash
76-
### create key, rsassa
77-
# using H2 template ( https://gist.github.com/salrashid123/9822b151ebb66f4083c5f71fd4cdbe40 )
78-
printf '\x00\x00' > unique.dat
79-
tpm2_createprimary -C o -G ecc -g sha256 \
80-
-c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
81-
82-
tpm2_create -G rsa2048:rsassa:null -g sha256 -u key.pub -r key.priv -C primary.ctx
83-
tpm2_flushcontext -t
84-
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
85-
tpm2_evictcontrol -C o -c key.ctx 0x81008001
86-
tpm2_flushcontext -t
87-
tpm2_encodeobject -C primary.ctx -u key.pub -r key.priv -o private.pem
88-
89-
cd util/csrgen/
90-
go run csrgen/csrgen.go -cn server.domain.com --persistentHandle 0x81008001
91-
```
59+
To use this, the key must be first created on the TPM and accessed as a PersistentHandle or TPM PEM file
9260

93-
### Usage: Generate self-signed certificate
94-
95-
The following will generate a key on the tpm, then use that RSA key to issue a CSR and then sign that CSR with by itself to get an x509.
96-
97-
You can ofcourse modify it to just sign any csr with a TPM backed key
61+
You can create these keys using `go-tpm` or using `tpm2_tools`. The example below uses tpm2_tools but for others languages and standalone applicatoins, see [openssl tpm2 provider](https://github.com/salrashid123/tpm2?tab=readme-ov-file#tpm-based-private-key) or [tpm2genkey](https://github.com/salrashid123/tpm2genkey)
9862

99-
100-
```bash
101-
# using H2 template ( https://gist.github.com/salrashid123/9822b151ebb66f4083c5f71fd4cdbe40 )
102-
printf '\x00\x00' > unique.dat
103-
tpm2_createprimary -C o -G ecc -g sha256 \
104-
-c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
105-
106-
tpm2_create -G rsa2048:rsassa:null -g sha256 -u key.pub -r key.priv -C primary.ctx
107-
tpm2_flushcontext -t
108-
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
109-
tpm2_evictcontrol -C o -c key.ctx 0x81008002
110-
tpm2_flushcontext -t
111-
tpm2_encodeobject -C primary.ctx -u key.pub -r key.priv -o private.pem
112-
113-
go run certgen/certgen.go -cn server.domain.com --persistentHandle 0x81008002
114-
```
115-
116-
---
117-
118-
If you just want to issue JWT's, see
119-
120-
* [https://github.com/salrashid123/golang-jwt-tpm](https://github.com/salrashid123/golang-jwt-tpm)
121-
* [https://github.com/salrashid123/golang-jwt-pkcs11](https://github.com/salrashid123/golang-jwt-pkcs11)
122-
123-
or real random:
124-
125-
* [TPM backed crypto/rand Reader](https://github.com/salrashid123/tpmrand)
126-
127-
---
128-
129-
### Example Setup - TPM
130-
131-
132-
example usage generates a new TPM unrestricted RSA key and sign,verify some data.
133-
134-
135-
You can create the persistent handles using go-tpm or using `tpm2_tools` and make it persistent,
136-
137-
First install latest [tpm2_tools](https://tpm2-tools.readthedocs.io/en/latest/INSTALL/)
63+
For this, install latest [tpm2_tools](https://tpm2-tools.readthedocs.io/en/latest/INSTALL/)
13864

13965
```bash
14066
cd example/
@@ -147,11 +73,12 @@ cd example/
14773
## and for tpm2_tools, export the following var
14874
# export TPM2TOOLS_TCTI="swtpm:port=2321"
14975

150-
## note if you want, the primary can be the "H2" profile from https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#name-parent
76+
## if you are using a real tpm set --tpm-path=/dev/tpmrm0
77+
78+
## note the primary can be the "H2" profile from https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#name-parent
15179
# printf '\x00\x00' > unique.dat
15280
# tpm2_createprimary -C o -G ecc -g sha256 -c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
15381

154-
15582
## RSA - no password
15683

15784
tpm2_createprimary -C o -G rsa2048:aes128cfb -g sha256 -c primary.ctx -a 'restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda'
@@ -208,39 +135,132 @@ go run sign_verify_tpm/ecc/main.go --tpm-path="127.0.0.1:2321" --handle 0x810080
208135
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
209136
tpm2_evictcontrol -C o -c key.ctx 0x81008006
210137

138+
go run sign_verify_tpm/policy_pcr/main.go --handle=0x81008006 --tpm-path="127.0.0.1:2321"
139+
211140
## for policyPassword
212141

213142
tpm2_createprimary -C o -G rsa2048:aes128cfb -g sha256 -c primary.ctx -a 'restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda'
214143
tpm2_create -G rsa2048:rsassa:null -p testpwd -g sha256 -u key.pub -r key.priv -C primary.ctx
215144
tpm2_flushcontext -t && tpm2_flushcontext -s && tpm2_flushcontext -l
216145
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
217146
tpm2_evictcontrol -C o -c key.ctx 0x81008007
218-
219-
## =====
220147

221-
tpm2_flushcontext -t && tpm2_flushcontext -s && tpm2_flushcontext -l
148+
go run sign_verify_tpm/policy_password/main.go --handle=0x81008007 --tpm-path="127.0.0.1:2321"
222149

223-
cd example/
150+
```
224151

225-
## RSA-SSA managed externally
226-
go run sign_verify_tpm/rsassa/main.go --handle=0x81008001 --tpm-path="127.0.0.1:2321"
227152

228-
## RSA with PEM KeyFile
229-
go run sign_verify_tpm/keyfile/main.go --pemFile=/path/to/key.pem --tpm-path="127.0.0.1:2321"
153+
### Usage TLS
230154

231-
## RSA-PSS
232-
go run sign_verify_tpm/rsapss/main.go --handle=0x81008004 --tpm-path="127.0.0.1:2321"
155+
* for tpm see [mTLS with TPM bound private key](https://github.com/salrashid123/go_tpm_https_embed)
233156

234-
## ECC
235-
go run sign_verify_tpm/ecc/main.go --handle=0x81008005 --tpm-path="127.0.0.1:2321"
157+
### Sign/Verify ECC
236158

237-
## RSA with pcr policy
238-
go run sign_verify_tpm/policy_pcr/main.go --handle=0x81008006 --tpm-path="127.0.0.1:2321"
159+
The default output signature format for ECC based keys is ASN1 format as described in [ecdsa.SignASN1](https://pkg.go.dev/crypto/ecdsa#Sign)
239160

240-
## RSA with password policy
241-
go run sign_verify_tpm/policy_password/main.go --handle=0x81008007 --tpm-path="127.0.0.1:2321"
161+
If you need the raw output format, set `ECCRawOutput: true` in the config.
162+
163+
See the examples folder for usage
164+
165+
### Usage: Generate CSR
166+
167+
The following will generate a TPM based key and then issue a CSR against it.
168+
169+
```bash
170+
### create key, rsassa
171+
# using H2 template ( https://gist.github.com/salrashid123/9822b151ebb66f4083c5f71fd4cdbe40 )
172+
printf '\x00\x00' > unique.dat
173+
tpm2_createprimary -C o -G ecc -g sha256 \
174+
-c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
175+
176+
tpm2_create -G rsa2048:rsassa:null -g sha256 -u key.pub -r key.priv -C primary.ctx
177+
tpm2_flushcontext -t
178+
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
179+
tpm2_evictcontrol -C o -c key.ctx 0x81008001
180+
tpm2_flushcontext -t
181+
tpm2_encodeobject -C primary.ctx -u key.pub -r key.priv -o private.pem
182+
183+
cd util/csrgen/
184+
go run csrgen/csrgen.go -cn server.domain.com --persistentHandle 0x81008001
242185
```
243186

187+
### Usage: Generate self-signed certificate
188+
189+
The following will generate a key on the tpm, then use that RSA key to issue a CSR and then sign that CSR with by itself to get an x509.
190+
191+
You can ofcourse modify it to just sign any csr with a TPM backed key
192+
193+
194+
```bash
195+
# using H2 template ( https://gist.github.com/salrashid123/9822b151ebb66f4083c5f71fd4cdbe40 )
196+
printf '\x00\x00' > unique.dat
197+
tpm2_createprimary -C o -G ecc -g sha256 \
198+
-c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda|restricted|decrypt" -u unique.dat
199+
200+
tpm2_create -G rsa2048:rsassa:null -g sha256 -u key.pub -r key.priv -C primary.ctx
201+
tpm2_flushcontext -t
202+
tpm2_load -C primary.ctx -u key.pub -r key.priv -c key.ctx
203+
tpm2_evictcontrol -C o -c key.ctx 0x81008002
204+
tpm2_flushcontext -t
205+
tpm2_encodeobject -C primary.ctx -u key.pub -r key.priv -o private.pem
206+
207+
go run certgen/certgen.go -cn server.domain.com --persistentHandle 0x81008002
208+
```
209+
210+
---
211+
212+
If you just want to issue JWT's, see
213+
214+
* [https://github.com/salrashid123/golang-jwt-tpm](https://github.com/salrashid123/golang-jwt-tpm)
215+
* [https://github.com/salrashid123/golang-jwt-pkcs11](https://github.com/salrashid123/golang-jwt-pkcs11)
216+
217+
or real random:
218+
219+
* [TPM backed crypto/rand Reader](https://github.com/salrashid123/tpmrand)
220+
221+
---
222+
223+
#### Keys with Auth Policy
224+
225+
If the key is setup with an AuthPolicy (eg, a policy that requires a passphrase or a predefined PCR values to exist), you can specify those in code or define your own
226+
227+
228+
##### PasswordPolicy
229+
230+
If the key requires a password, initialize a `NewPasswordSession`
231+
232+
```golang
233+
se, err := saltpm.NewPasswordSession(rwr, []byte(*keyPass))
234+
235+
rr, err := saltpm.NewTPMCrypto(&saltpm.TPM{
236+
TpmDevice: rwc,
237+
Handle: tpm2.TPMHandle(*handle),
238+
AuthSession: se,
239+
})
240+
```
241+
242+
##### PCRPolicy
243+
244+
If the key requires a password, initialize a `NewPCRSession`
245+
246+
```golang
247+
se, err := saltpm.NewPCRSession(rwr, []tpm2.TPMSPCRSelection{
248+
{
249+
Hash: tpm2.TPMAlgSHA256,
250+
PCRSelect: tpm2.PCClientCompatible.PCRs(uint(*pcr)),
251+
},
252+
})
253+
254+
rr, err := saltpm.NewTPMCrypto(&saltpm.TPM{
255+
TpmDevice: rwc,
256+
Handle: tpm2.TPMHandle(*handle),
257+
AuthSession: se,
258+
})
259+
260+
```
261+
262+
##### CustomPolicy
263+
244264
Note, you can define your own policy for import too...just implement the "session" interface from the signer:
245265

246266
```golang

0 commit comments

Comments
 (0)