-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpcpcrypto_utils.go
493 lines (430 loc) · 17.3 KB
/
pcpcrypto_utils.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) 2020-2025, El Mostafa IDRASSI.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pcpcrypto
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"unsafe"
"github.com/ElMostafaIdrassi/goncrypt"
"github.com/google/go-tpm/legacy/tpm2"
"golang.org/x/sys/windows"
)
type KeyUsage uint32
const (
KeyUsageDefault KeyUsage = 0x00000000
KeyUsageAllowDecrypt KeyUsage = 0x00000001 // NcryptAllowDecryptFlag
KeyUsageAllowSigning KeyUsage = 0x00000002 // NcryptAllowSigningFlag
KeyUsageAllowKeyAgreement KeyUsage = 0x00000004 // NcryptAllowKeyAgreementFlag
KeyUsageAllowAllUsages KeyUsage = 0x00ffffff // NcryptAllowAllUsages
)
func (u *KeyUsage) fromNcryptFlag(flag goncrypt.NcryptKeyUsagePropertyFlag) {
*u = KeyUsageDefault
if flag&goncrypt.NcryptAllowDecryptFlag != 0 {
*u |= KeyUsageAllowDecrypt
}
if flag&goncrypt.NcryptAllowSigningFlag != 0 {
*u |= KeyUsageAllowSigning
}
if flag&goncrypt.NcryptAllowKeyAgreementFlag != 0 {
*u |= KeyUsageAllowKeyAgreement
}
if flag&goncrypt.NcryptAllowAllUsages != 0 {
*u |= KeyUsageAllowAllUsages
}
}
func (u *KeyUsage) Value() uint32 {
return uint32(*u)
}
type UIPolicy uint32
const (
UIPolicyNoConsent UIPolicy = 0x00000000
UIPolicyConsentWithOptionalPIN UIPolicy = 0x00000001
UIPolicyConsentWithMandatoryPIN UIPolicy = 0x00000002
UIPolicyConsentWithMandatoryFingerprint UIPolicy = 0x00000004
)
func (p *UIPolicy) fromNcryptFlag(flag goncrypt.NcryptUiPolicyPropertyFlag) {
*p = UIPolicyNoConsent
if flag == goncrypt.NcryptUiProtectKeyFlag {
*p = UIPolicyConsentWithOptionalPIN
} else if flag == goncrypt.NcryptUiForceHighProtectionFlag {
*p = UIPolicyConsentWithMandatoryPIN
} else if flag == goncrypt.NcryptUiFingerprintProtectionFlag {
*p = UIPolicyConsentWithMandatoryFingerprint
}
}
func (p *UIPolicy) Value() uint32 {
return uint32(*p)
}
// utf16BytesToString transforms a []byte which contains a wide char string in LE
// into its []uint16 corresponding representation,
// then returns the UTF-8 encoding of the UTF-16 sequence,
// with a terminating NUL removed. If after converting the []byte into
// a []uint16, there is a NUL uint16, the conversion to string stops
// at that NUL uint16.
func utf16BytesToString(buf []byte) (string, error) {
if len(buf)%2 != 0 {
return "", fmt.Errorf("input is not a valid byte representation of a wide char string in LE")
}
b := make([]uint16, len(buf)/2)
// LPCSTR (Windows' representation of utf16) is always little endian.
if err := binary.Read(bytes.NewReader(buf), binary.LittleEndian, b); err != nil {
return "", err
}
return windows.UTF16ToString(b), nil
}
// stringToUtf16Bytes returns the UTF-16 encoding of the UTF-8 string
// str, as a byte array with a terminating NUL added. If str contains a NUL byte at any
// location, it returns (nil, EINVAL).
func stringToUtf16Bytes(str string) ([]byte, error) {
if str == "" {
return nil, nil
}
utf16Str, err := windows.UTF16FromString(str)
if err != nil {
return nil, err
}
bytesStr := make([]byte, len(utf16Str)*2)
j := 0
for _, utf16 := range utf16Str {
b := make([]byte, 2)
// LPCSTR (Windows' representation of utf16) is always little endian.
binary.LittleEndian.PutUint16(b, utf16)
bytesStr[j] = b[0]
bytesStr[j+1] = b[1]
j += 2
}
return bytesStr, nil
}
// See https://github.com/microsoft/TSS.MSR/blob/main/PCPTool.v11/inc/TpmAtt.h
const BcryptPcpKeyMagic uint32 = 0x4D504350 // PCPM
const PCPTypeTpm12 uint32 = 0x00000001
const PCPTypeTpm20 uint32 = 0x00000002
type PCPKeyBlobWin8Header struct {
Magic uint32
HeaderLength uint32
PCPType uint32
Flags uint32
PublicLength uint32
PrivateLength uint32
MigrationPublicLength uint32
MigrationPrivateLength uint32
PolicyDigestListLength uint32
PCRBindingLength uint32
PCRDigestLength uint32
EncryptedSecretLength uint32
Tpm12HostageBlobLength uint32
}
type PCPKeyBlobWin8 struct {
PCPKeyBlobWin8Header PCPKeyBlobWin8Header
Public []byte // TPM2B_PUBLIC
Private []byte // TPM2B_PRIVATE
MigrationPublic []byte
MigrationPrivate []byte
PolicyDigestList []byte // TPML_DIGEST
PCRBinding []byte
PCRDigest []byte
EncryptedSecret []byte
Tpm12HostageBlob []byte
}
func (k *PCPKeyBlobWin8) fromBlobData(blobData []byte) error {
if len(blobData) > int(unsafe.Sizeof(k.PCPKeyBlobWin8Header)) &&
binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(k.PCPKeyBlobWin8Header)) {
pcpKeyBlobWin8Header := *(*PCPKeyBlobWin8Header)(unsafe.Pointer(&blobData[0]))
blobDataExpectedLength := uint32(unsafe.Sizeof(pcpKeyBlobWin8Header)) +
pcpKeyBlobWin8Header.PublicLength +
pcpKeyBlobWin8Header.PrivateLength +
pcpKeyBlobWin8Header.MigrationPublicLength +
pcpKeyBlobWin8Header.MigrationPrivateLength +
pcpKeyBlobWin8Header.PolicyDigestListLength +
pcpKeyBlobWin8Header.PCRBindingLength +
pcpKeyBlobWin8Header.PCRDigestLength +
pcpKeyBlobWin8Header.EncryptedSecretLength +
pcpKeyBlobWin8Header.Tpm12HostageBlobLength
if uint32(len(blobData)) == blobDataExpectedLength {
k.PCPKeyBlobWin8Header = pcpKeyBlobWin8Header
currentDataPosition := uint32(unsafe.Sizeof(k.PCPKeyBlobWin8Header))
k.Public = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.PublicLength]
currentDataPosition += k.PCPKeyBlobWin8Header.PublicLength
k.Private = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.PrivateLength]
currentDataPosition += k.PCPKeyBlobWin8Header.PrivateLength
k.MigrationPublic = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.MigrationPublicLength]
currentDataPosition += k.PCPKeyBlobWin8Header.MigrationPublicLength
k.MigrationPrivate = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.MigrationPrivateLength]
currentDataPosition += k.PCPKeyBlobWin8Header.MigrationPrivateLength
k.PolicyDigestList = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.PolicyDigestListLength]
currentDataPosition += k.PCPKeyBlobWin8Header.PolicyDigestListLength
k.PCRBinding = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.PCRBindingLength]
currentDataPosition += k.PCPKeyBlobWin8Header.PCRBindingLength
k.PCRDigest = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.PCRDigestLength]
currentDataPosition += k.PCPKeyBlobWin8Header.PCRDigestLength
k.EncryptedSecret = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.EncryptedSecretLength]
currentDataPosition += k.PCPKeyBlobWin8Header.EncryptedSecretLength
k.Tpm12HostageBlob = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobWin8Header.Tpm12HostageBlobLength]
return nil
} else {
return fmt.Errorf("invalid PCP Key Blob (expected blob length %d, got %d)", blobDataExpectedLength, len(blobData))
}
} else {
return fmt.Errorf("invalid PCP Key Blob (expected header length %d, got %d)", unsafe.Sizeof(k.PCPKeyBlobWin8Header), len(blobData))
}
}
type PCP20KeyBlobHeader struct {
Magic uint32
HeaderLength uint32
PCPType uint32
Flags uint32
PublicLength uint32
PrivateLength uint32
MigrationPublicLength uint32
MigrationPrivateLength uint32
PolicyDigestListLength uint32
PCRBindingLength uint32
PCRDigestLength uint32
EncryptedSecretLength uint32
Tpm12HostageBlobLength uint32
PCRAlgId uint16
}
type PCP20KeyBlob struct {
PCP20KeyBlobHeader PCP20KeyBlobHeader
Public []byte // TPM2B_PUBLIC
Private []byte // TPM2B_PRIVATE
MigrationPublic []byte
MigrationPrivate []byte
PolicyDigestList []byte // TPML_DIGEST
PCRBinding []byte
PCRDigest []byte
EncryptedSecret []byte
Tpm12HostageBlob []byte
}
func (k *PCP20KeyBlob) fromBlobData(blobData []byte) error {
if len(blobData) > int(unsafe.Sizeof(k.PCP20KeyBlobHeader)) &&
binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(k.PCP20KeyBlobHeader)) {
pcp20KeyBlobHeader := *(*PCP20KeyBlobHeader)(unsafe.Pointer(&blobData[0]))
blobDataExpectedLength := uint32(unsafe.Sizeof(pcp20KeyBlobHeader)) +
pcp20KeyBlobHeader.PublicLength +
pcp20KeyBlobHeader.PrivateLength +
pcp20KeyBlobHeader.MigrationPublicLength +
pcp20KeyBlobHeader.MigrationPrivateLength +
pcp20KeyBlobHeader.PolicyDigestListLength +
pcp20KeyBlobHeader.PCRBindingLength +
pcp20KeyBlobHeader.PCRDigestLength +
pcp20KeyBlobHeader.EncryptedSecretLength +
pcp20KeyBlobHeader.Tpm12HostageBlobLength
if uint32(len(blobData)) == blobDataExpectedLength {
k.PCP20KeyBlobHeader = pcp20KeyBlobHeader
currentDataPosition := uint32(unsafe.Sizeof(k.PCP20KeyBlobHeader))
k.Public = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.PublicLength]
currentDataPosition += k.PCP20KeyBlobHeader.PublicLength
k.Private = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.PrivateLength]
currentDataPosition += k.PCP20KeyBlobHeader.PrivateLength
k.MigrationPublic = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.MigrationPublicLength]
currentDataPosition += k.PCP20KeyBlobHeader.MigrationPublicLength
k.MigrationPrivate = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.MigrationPrivateLength]
currentDataPosition += k.PCP20KeyBlobHeader.MigrationPrivateLength
k.PolicyDigestList = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.PolicyDigestListLength]
currentDataPosition += k.PCP20KeyBlobHeader.PolicyDigestListLength
k.PCRBinding = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.PCRBindingLength]
currentDataPosition += k.PCP20KeyBlobHeader.PCRBindingLength
k.PCRDigest = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.PCRDigestLength]
currentDataPosition += k.PCP20KeyBlobHeader.PCRDigestLength
k.EncryptedSecret = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.EncryptedSecretLength]
currentDataPosition += k.PCP20KeyBlobHeader.EncryptedSecretLength
k.Tpm12HostageBlob = blobData[currentDataPosition : currentDataPosition+k.PCP20KeyBlobHeader.Tpm12HostageBlobLength]
return nil
} else {
return fmt.Errorf("invalid PCP Key Blob (expected blob length %d, got %d)", blobDataExpectedLength, len(blobData))
}
} else {
return fmt.Errorf("invalid PCP Key Blob (expected header length %d, got %d)", unsafe.Sizeof(k.PCP20KeyBlobHeader), len(blobData))
}
}
type PCPKeyBlobHeader struct {
Magic uint32
HeaderLength uint32
PCPType uint32
Flags uint32
TPKKeyLength uint32
}
type PCPKeyBlob struct {
PCPKeyBlobHeader PCPKeyBlobHeader
TPMKey []byte
}
func (k *PCPKeyBlob) fromBlobData(blobData []byte) error {
if len(blobData) > int(unsafe.Sizeof(k.PCPKeyBlobHeader)) &&
binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(k.PCPKeyBlobHeader)) {
pcpKeyBlobHeader := *(*PCPKeyBlobHeader)(unsafe.Pointer(&blobData[0]))
blobDataExpectedLength := uint32(unsafe.Sizeof(pcpKeyBlobHeader)) + pcpKeyBlobHeader.TPKKeyLength
if uint32(len(blobData)) == blobDataExpectedLength {
k.PCPKeyBlobHeader = pcpKeyBlobHeader
currentDataPosition := uint32(unsafe.Sizeof(k.PCPKeyBlobHeader))
k.TPMKey = blobData[currentDataPosition : currentDataPosition+k.PCPKeyBlobHeader.TPKKeyLength]
return nil
} else {
return fmt.Errorf("invalid PCP 2.0 Key Blob (expected blob length %d, got %d)", blobDataExpectedLength, len(blobData))
}
} else {
return fmt.Errorf("invalid PCP 2.0 Key Blob (expected header length %d, got %d)", unsafe.Sizeof(k.PCPKeyBlobHeader), len(blobData))
}
}
type TlvRecord struct {
Tag uint32
Len uint32
Value []byte
}
func GetPCPKeyFileTLVRecords(pcpKeyFilePath string) ([]TlvRecord, error) {
// Read the file into a byte array
data, err := os.ReadFile(pcpKeyFilePath)
if err != nil {
return nil, err
}
// Check that the PCP Key File is at least 8 bytes long.
if len(data) <= 8 {
return nil, fmt.Errorf("invalid PCP Key File (too short)")
}
// Check that the PCP Key File starts with "PKSP" and ends with "PKSP".
if !bytes.HasPrefix(data, []byte("PKSP")) || !bytes.HasSuffix(data, []byte("PKSP")) {
return nil, fmt.Errorf("invalid PCP Key File (missing header or footer)")
}
// Move the data past the "PKSP" header and before the "PKSP" footer.
data = data[4 : len(data)-4]
// Decode the PCP Key File data as a list of TLV records.
var tlvRecords []TlvRecord
for len(data) > 0 {
if len(data) < 8 {
return nil, fmt.Errorf("invalid PCP Key File (too short)")
}
tag := binary.LittleEndian.Uint32(data[0:4])
length := binary.LittleEndian.Uint32(data[4:8])
if uint32(len(data)) < length+8 {
return nil, fmt.Errorf("invalid PCP Key File (too short)")
}
value := data[8 : length+8]
tlvRecords = append(tlvRecords, TlvRecord{tag, length, value})
data = data[length+8:]
}
return tlvRecords, nil
}
func ParsePCPKeyFile(pcpKeyFilePath string) (
public *tpm2.Public,
private []byte,
policyDigest *tpm2.TPMLDigest,
keyName string,
keyAlgorithm string,
err error,
) {
var tlvRecords []TlvRecord
var blobData []byte
var pcpKeyBlobWin8 PCPKeyBlobWin8
var pcp20KeyBlob PCP20KeyBlob
var tpm2Public tpm2.Public
tlvRecords, err = GetPCPKeyFileTLVRecords(pcpKeyFilePath)
if err != nil {
return
}
for _, tlvRecord := range tlvRecords {
if tlvRecord.Tag == 0x02000000 {
keyName, err = utf16BytesToString(tlvRecord.Value)
if err != nil {
return
}
} else if tlvRecord.Tag == 0x02000004 {
keyAlgorithm, err = utf16BytesToString(tlvRecord.Value)
if err != nil {
return
}
} else if tlvRecord.Tag == 0x01000002 || tlvRecord.Tag == 0x01000003 {
blobData = tlvRecord.Value
if tlvRecord.Tag == 0x01000003 {
inDataBlob := &windows.DataBlob{
Data: &blobData[0],
Size: uint32(len(blobData)),
}
outDataBllob := &windows.DataBlob{}
err = windows.CryptUnprotectData(inDataBlob, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, outDataBllob)
if err != nil {
return
}
defer windows.LocalFree(windows.Handle(unsafe.Pointer(outDataBllob.Data)))
blobData = make([]byte, outDataBllob.Size)
copy(blobData, unsafe.Slice(outDataBllob.Data, outDataBllob.Size))
}
if len(blobData) <= 8 {
err = fmt.Errorf("invalid PCP Key File (too short)")
return
}
if binary.LittleEndian.Uint32(blobData[0:4]) != BcryptPcpKeyMagic {
err = fmt.Errorf("invalid PCP Key File (invalid magic, expected 0x%08x, got 0x%08x)", BcryptPcpKeyMagic, binary.LittleEndian.Uint32(blobData[0:4]))
return
}
if binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(PCPKeyBlob{}.PCPKeyBlobHeader)) {
err = fmt.Errorf("invalid PCP Key File (unsupported PCP 1.2 Key Blob version)")
return
} else if binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(pcpKeyBlobWin8.PCPKeyBlobWin8Header)) {
err = pcpKeyBlobWin8.fromBlobData(blobData)
if err != nil {
return
}
// pcpKeyBlobWin8.Public is a TPM2B_PUBLIC structure, which is a UINT16 size followed by a TPMT_PUBLIC structure.
// We need to strip the UINT16 size from the beginning of the blob before we can decode the TPMT_PUBLIC structure.
if len(pcpKeyBlobWin8.Public) < 2 {
err = fmt.Errorf("invalid PCP Key File (public key blob too short)")
return
}
tpm2Public, err = tpm2.DecodePublic(pcpKeyBlobWin8.Public[2:])
if err != nil {
return
}
public = &tpm2Public
policyDigest, err = tpm2.DecodeTPMLDigest(pcpKeyBlobWin8.PolicyDigestList)
if err != nil {
return
}
if len(pcpKeyBlobWin8.Private) < 2 {
err = fmt.Errorf("invalid PCP Key File (private key blob too short)")
return
}
private = pcpKeyBlobWin8.Private
} else if binary.LittleEndian.Uint32(blobData[4:8]) == uint32(unsafe.Sizeof(pcp20KeyBlob.PCP20KeyBlobHeader)) {
err = pcp20KeyBlob.fromBlobData(blobData)
if err != nil {
return
}
// pcp20KeyBlob.Public is a TPM2B_PUBLIC structure, which is a UINT16 size followed by a TPMT_PUBLIC structure.
// We need to strip the UINT16 size from the beginning of the blob before we can decode the TPMT_PUBLIC structure.
if len(pcp20KeyBlob.Public) < 2 {
err = fmt.Errorf("invalid PCP Key File (public key blob too short)")
return
}
tpm2Public, err = tpm2.DecodePublic(pcp20KeyBlob.Public[2:])
policyDigest, err = tpm2.DecodeTPMLDigest(pcp20KeyBlob.PolicyDigestList)
if err != nil {
return
}
public = &tpm2Public
if len(pcp20KeyBlob.Private) < 2 {
err = fmt.Errorf("invalid PCP Key File (private key blob too short)")
return
}
private = pcp20KeyBlob.Private
} else {
err = fmt.Errorf("invalid PCP Key File (invalid blob header size)")
return
}
break
}
}
return
}