-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpm.go
232 lines (188 loc) · 5.03 KB
/
tpm.go
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
package tpm
import (
"bytes"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"strings"
"github.com/google/go-attestation/attest"
"github.com/google/go-tpm-tools/simulator"
"github.com/kairos-io/tpm-helpers/backend"
"github.com/pkg/errors"
)
// GenerateChallenge generates a challenge from attestation data and a public endorsed key
func GenerateChallenge(ek *attest.EK, attestationData *AttestationData) ([]byte, []byte, error) {
ap := attest.ActivationParameters{
TPMVersion: attest.TPMVersion20,
EK: ek.Public,
AK: *attestationData.AK,
}
secret, ec, err := ap.Generate()
if err != nil {
return nil, nil, fmt.Errorf("generating challenge: %w", err)
}
challengeBytes, err := json.Marshal(Challenge{EC: ec})
if err != nil {
return nil, nil, fmt.Errorf("marshalling challenge: %w", err)
}
return secret, challengeBytes, nil
}
// ResolveToken is just syntax sugar around GetPubHash.
// If the token provided is in EK's form it just returns it, otherwise
// retrieves the pubhash
func ResolveToken(token string, opts ...Option) (bool, string, error) {
if !strings.HasPrefix(token, "tpm://") {
return false, token, nil
}
hash, err := GetPubHash(opts...)
return true, hash, err
}
// GetPubHash returns the EK's pub hash
func GetPubHash(opts ...Option) (string, error) {
c := newConfig()
c.apply(opts...)
ek, err := getEK(c)
if err != nil {
return "", fmt.Errorf("getting EK: %w", err)
}
hash, err := DecodePubHash(ek)
if err != nil {
return "", fmt.Errorf("hashing EK: %w", err)
}
return hash, nil
}
func getTPM(c *config) (*attest.TPM, error) {
cfg := &attest.OpenConfig{
TPMVersion: attest.TPMVersion20,
}
if c.commandChannel != nil {
cfg.CommandChannel = c.commandChannel
}
if c.emulated {
var sim *simulator.Simulator
var err error
if c.seed != 0 {
sim, err = simulator.GetWithFixedSeedInsecure(c.seed)
} else {
sim, err = simulator.Get()
}
if err != nil {
return nil, err
}
cfg.CommandChannel = backend.Fake(sim)
}
return attest.OpenTPM(cfg)
}
func getEK(c *config) (*attest.EK, error) {
var err error
tpm, err := getTPM(c)
if err != nil {
return nil, fmt.Errorf("opening tpm for decoding EK: %w", err)
}
defer tpm.Close()
eks, err := tpm.EKs()
if err != nil {
return nil, fmt.Errorf("getting eks: %w", err)
}
if len(eks) == 0 {
return nil, fmt.Errorf("failed to find EK")
}
return &eks[0], nil
}
func getToken(data *AttestationData) (string, error) {
bytes, err := json.Marshal(data)
if err != nil {
return "", fmt.Errorf("marshalling attestation data: %w", err)
}
return "Bearer TPM" + base64.StdEncoding.EncodeToString(bytes), nil
}
func getAttestationData(c *config) (*AttestationData, []byte, error) {
var err error
tpm, err := getTPM(c)
if err != nil {
return nil, nil, fmt.Errorf("opening tpm for getting attestation data: %w", err)
}
defer tpm.Close()
eks, err := tpm.EKs()
if err != nil {
return nil, nil, err
}
ak, err := tpm.NewAK(nil)
if err != nil {
return nil, nil, err
}
defer ak.Close(tpm)
params := ak.AttestationParameters()
if len(eks) == 0 {
return nil, nil, fmt.Errorf("failed to find EK")
}
ek := &eks[0]
ekBytes, err := encodeEK(ek)
if err != nil {
return nil, nil, err
}
aikBytes, err := ak.Marshal()
if err != nil {
return nil, nil, fmt.Errorf("marshaling AK: %w", err)
}
return &AttestationData{
EK: ekBytes,
AK: ¶ms,
}, aikBytes, nil
}
// DecodeEK decodes EK pem bytes to attest.EK
func DecodeEK(pemBytes []byte) (*attest.EK, error) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("invalid pemBytes")
}
switch block.Type {
case "CERTIFICATE":
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing certificate: %v", err)
}
return &attest.EK{
Certificate: cert,
Public: cert.PublicKey,
}, nil
case "PUBLIC KEY":
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing ecdsa public key: %v", err)
}
return &attest.EK{
Public: pub,
}, nil
}
return nil, fmt.Errorf("invalid pem type: %s", block.Type)
}
// GetAttestationData returns attestation data from a TPM bearer token
func GetAttestationData(header string) (*attest.EK, *AttestationData, error) {
tpmBytes, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(header, "Bearer TPM"))
if err != nil {
return nil, nil, err
}
var attestationData AttestationData
if err := json.Unmarshal(tpmBytes, &attestationData); err != nil {
return nil, nil, err
}
ek, err := DecodeEK(attestationData.EK)
if err != nil {
return nil, nil, err
}
return ek, &attestationData, nil
}
// ValidateChallenge validates a challange against a secret
func ValidateChallenge(secret, resp []byte) error {
var response ChallengeResponse
if err := json.Unmarshal(resp, &response); err != nil {
return fmt.Errorf("unmarshalling challenge response: %w", err)
}
if !bytes.Equal(secret, response.Secret) {
return fmt.Errorf("invalid challenge response")
}
return nil
}