-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathder.go
390 lines (335 loc) · 10.6 KB
/
der.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package keyfile
import (
encasn1 "encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"io"
"unicode/utf8"
"github.com/google/go-tpm/tpm2"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/crypto/cryptobyte/asn1"
)
var (
// id-tpmkey OBJECT IDENTIFIER ::=
// {joint-iso-itu-t(2) international-organizations(23) 133 10 1}
// Probably not used, but here for reference
oidTPMKey = encasn1.ObjectIdentifier{2, 23, 133, 10, 2}
OIDOldLoadableKey = encasn1.ObjectIdentifier{2, 23, 133, 10, 2}
// id-loadablekey OBJECT IDENTIFIER ::= {id-tpmkey 3}
OIDLoadableKey = encasn1.ObjectIdentifier{2, 23, 133, 10, 1, 3}
// id-importablekey OBJECT IDENTIFIER ::= {id-tpmkey 4}
OIDImportableKey = encasn1.ObjectIdentifier{2, 23, 133, 10, 1, 4}
// id-sealedkey OBJECT IDENTIFIER ::= {id-tpmkey 5}
OIDSealedKey = encasn1.ObjectIdentifier{2, 23, 133, 10, 1, 5}
)
func readOptional(der *cryptobyte.String, tag int) (out cryptobyte.String, ok bool) {
der.ReadOptionalASN1(&out, &ok, asn1.Tag(tag).ContextSpecific().Constructed())
return
}
func parseTPMPolicy(der *cryptobyte.String) ([]*TPMPolicy, error) {
var tpmpolicies []*TPMPolicy
// policy [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL,
policy, ok := readOptional(der, 1)
if !ok {
return []*TPMPolicy{}, nil
}
// read outer sequence
var policySequence cryptobyte.String
if !policy.ReadASN1(&policySequence, asn1.SEQUENCE) {
return nil, errors.New("malformed policy sequence")
}
for !policySequence.Empty() {
// TPMPolicy ::= SEQUENCE
var policyBytes cryptobyte.String
if !policySequence.ReadASN1(&policyBytes, asn1.SEQUENCE) {
return nil, errors.New("malformed policy sequence")
}
// commandCode [0] EXPLICIT INTEGER,
var ccBytes cryptobyte.String
if !policyBytes.ReadASN1(&ccBytes, asn1.Tag(0).ContextSpecific().Constructed()) {
return nil, errors.New("strip tag from commandCode")
}
var tpmpolicy TPMPolicy
if !ccBytes.ReadASN1Integer(&tpmpolicy.CommandCode) {
return nil, errors.New("malformed policy commandCode")
}
// commandPolicy [1] EXPLICIT OCTET STRING
var cpBytes cryptobyte.String
if !policyBytes.ReadASN1(&cpBytes, asn1.Tag(1).ContextSpecific().Constructed()) {
return nil, errors.New("strip tag from commandPolicy")
}
if !cpBytes.ReadASN1Bytes(&tpmpolicy.CommandPolicy, asn1.OCTET_STRING) {
return nil, errors.New("malformed policy commandPolicy")
}
tpmpolicies = append(tpmpolicies, &tpmpolicy)
}
return tpmpolicies, nil
}
func parseTPMAuthPolicy(der *cryptobyte.String) ([]*TPMAuthPolicy, error) {
var authPolicy []*TPMAuthPolicy
var sAuthPolicy cryptobyte.String
// authPolicy [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
sAuthPolicy, ok := readOptional(der, 3)
if !ok {
return authPolicy, nil
}
// read outer sequence
var authPolicySequence cryptobyte.String
if !sAuthPolicy.ReadASN1(&authPolicySequence, asn1.SEQUENCE) {
return nil, errors.New("malformed auth policy sequence")
}
for !authPolicySequence.Empty() {
// TPMAuthPolicy ::= SEQUENCE
var authPolicyBytes cryptobyte.String
if !authPolicySequence.ReadASN1(&authPolicyBytes, asn1.SEQUENCE) {
return nil, errors.New("malformed auth policy sequence")
}
var tpmAuthPolicy TPMAuthPolicy
// name [0] EXPLICIT UTF8String OPTIONAL,
nameBytes, ok := readOptional(&authPolicyBytes, 0)
if ok {
var nameB cryptobyte.String
if !nameBytes.ReadASN1(&nameB, asn1.UTF8String) {
return nil, errors.New("malformed utf8string in auth policy name")
}
if !utf8.Valid(nameB) {
return nil, errors.New("invalid utf8 bytes in name of auth policy")
}
tpmAuthPolicy.Name = string(nameB)
}
// policy [1] EXPLICIT SEQUENCE OF TPMPolicy
tpmpolicies, err := parseTPMPolicy(&authPolicyBytes)
if err != nil {
return nil, fmt.Errorf("failed parsing tpm policies in auth policy: %v", err)
}
if len(tpmpolicies) == 0 {
return nil, errors.New("tpm policies in auth policy is empty")
}
tpmAuthPolicy.Policy = tpmpolicies
authPolicy = append(authPolicy, &tpmAuthPolicy)
}
return authPolicy, nil
}
func Parse(b []byte) (*TPMKey, error) {
var tkey TPMKey
var err error
// TPMKey ::= SEQUENCE
s := cryptobyte.String(b)
if !s.ReadASN1(&s, asn1.SEQUENCE) {
return nil, errors.New("no sequence")
}
// type TPMKeyType,
var oid encasn1.ObjectIdentifier
if !s.ReadASN1ObjectIdentifier(&oid) {
return nil, errors.New("no contentinfo oid")
}
// Check if we know the keytype
// TPMKeyType ::= OBJECT IDENTIFIER (
// id-loadablekey |
// id-importablekey |
// id-sealedkey
// )
switch {
case oid.Equal(OIDLoadableKey):
fallthrough
case oid.Equal(OIDImportableKey):
fallthrough
case oid.Equal(OIDSealedKey):
fallthrough
case oid.Equal(OIDOldLoadableKey):
tkey.Keytype = oid
default:
return nil, errors.New("unknown key type")
}
// emptyAuth [0] EXPLICIT BOOLEAN OPTIONAL,
if emptyAuthbytes, ok := readOptional(&s, 0); ok {
var auth bool
var bytes cryptobyte.String
if !emptyAuthbytes.ReadASN1(&bytes, asn1.BOOLEAN) || len(bytes) != 1 {
return nil, errors.New("no emptyAuth bool")
}
switch bytes[0] {
case 0:
auth = false
case 1:
auth = true
case 0xff:
auth = true
default:
auth = false
}
tkey.EmptyAuth = auth
}
policy, err := parseTPMPolicy(&s)
if err != nil {
return nil, fmt.Errorf("failed reading TPMPolicy: %v", err)
}
tkey.Policy = policy
// secret [2] EXPLICIT OCTET STRING OPTIONAL,
if secretbytes, ok := readOptional(&s, 2); ok {
var secretb cryptobyte.String
if !secretbytes.ReadASN1(&secretb, asn1.OCTET_STRING) {
return nil, errors.New("could not parse secret")
}
secret, err := tpm2.Unmarshal[tpm2.TPM2BEncryptedSecret](secretb)
if err != nil {
return nil, errors.New("could not parse public section of key 1")
}
tkey.Secret = *secret
}
// authPolicy [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
authPolicy, err := parseTPMAuthPolicy(&s)
if err != nil {
return nil, fmt.Errorf("failed reading TPMAuthPolicy: %v", err)
}
tkey.AuthPolicy = authPolicy
// description [4] EXPLICIT OCTET STRING OPTIONAL,
if descriptionBytes, ok := readOptional(&s, 4); ok {
var description cryptobyte.String
if !descriptionBytes.ReadASN1(&description, asn1.UTF8String) {
return nil, errors.New("could not parse description bytes")
}
if !utf8.Valid(description) {
return nil, errors.New("description is not a valid UTF8 string")
}
tkey.Description = string(description)
}
// Consume optional directives we don't support
// TODO: Bump this number when we support more things
i := 5
for {
if _, ok := readOptional(&s, i); !ok {
break
}
i++
}
// parent INTEGER,
var parent int
if !s.ReadASN1Integer(&parent) {
return nil, errors.New("failed reading parent")
}
tkey.Parent = tpm2.TPMHandle(parent)
// pubkey OCTET STRING,
var pubkey cryptobyte.String
if !s.ReadASN1(&pubkey, asn1.OCTET_STRING) {
return nil, errors.New("could not parse pubkey")
}
public, err := tpm2.Unmarshal[tpm2.TPM2BPublic](pubkey)
if err != nil {
return nil, errors.New("could not parse public section of key 1")
}
tkey.Pubkey = *public
// privkey OCTET STRING
var privkey cryptobyte.String
if !s.ReadASN1(&privkey, asn1.OCTET_STRING) {
return nil, errors.New("could not parse privkey")
}
private, err := tpm2.Unmarshal[tpm2.TPM2BPrivate](privkey)
if err != nil {
return nil, errors.New("could not parse public section of key")
}
tkey.Privkey = *private
return &tkey, nil
}
func Marshal(key *TPMKey) []byte {
var b cryptobyte.Builder
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
b.AddASN1ObjectIdentifier(key.Keytype)
if key.EmptyAuth {
b.AddASN1(asn1.Tag(0).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1Boolean(true)
})
}
if len(key.Policy) != 0 {
b.AddASN1(asn1.Tag(1).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
for _, policy := range key.Policy {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
b.AddASN1(asn1.Tag(0).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1Int64(int64(policy.CommandCode))
})
b.AddASN1(asn1.Tag(1).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1OctetString(policy.CommandPolicy)
})
})
}
})
})
}
if len(key.Secret.Buffer) != 0 {
b.AddASN1(asn1.Tag(2).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1OctetString(tpm2.Marshal(key.Secret))
})
}
if len(key.AuthPolicy) != 0 {
b.AddASN1(asn1.Tag(3).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
for _, authpolicy := range key.AuthPolicy {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
if authpolicy.Name != "" {
b.AddASN1(asn1.Tag(0).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1(asn1.UTF8String, func(b *cryptobyte.Builder) {
// TODO: Is this correct?
b.AddBytes([]byte(authpolicy.Name))
})
})
}
// Copy of the policy writing
b.AddASN1(asn1.Tag(1).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
for _, policy := range authpolicy.Policy {
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
b.AddASN1(asn1.Tag(0).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1Int64(int64(policy.CommandCode))
})
b.AddASN1(asn1.Tag(1).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1OctetString(policy.CommandPolicy)
})
})
}
})
})
})
}
})
})
}
if len(key.Description) != 0 {
b.AddASN1(asn1.Tag(4).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
b.AddASN1(asn1.UTF8String, func(b *cryptobyte.Builder) {
b.AddBytes([]byte(key.Description))
})
})
}
b.AddASN1Int64(int64(key.Parent))
b.AddASN1OctetString(tpm2.Marshal(key.Pubkey))
b.AddASN1OctetString(tpm2.Marshal(key.Privkey))
})
return b.BytesOrPanic()
}
// Errors
var (
ErrNotTPMKey = errors.New("not a TSS2 PRIVATE KEY")
)
var (
pemType = "TSS2 PRIVATE KEY"
)
func Encode(out io.Writer, key *TPMKey) error {
return pem.Encode(out, &pem.Block{
Type: pemType,
Bytes: Marshal(key),
})
}
func Decode(b []byte) (*TPMKey, error) {
block, _ := pem.Decode(b)
if block == nil {
return nil, fmt.Errorf("not an armored key")
}
switch block.Type {
case pemType:
return Parse(block.Bytes)
default:
return nil, ErrNotTPMKey
}
}