/
resources.go
119 lines (102 loc) · 2.65 KB
/
resources.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
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
//
// 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 devid
import (
"io"
"log"
"github.com/cray-hpe/tpm-provisioner/third_party/devid-provisioning-tool/pkg/agent/keygen"
"github.com/google/go-tpm/legacy/tpm2"
"github.com/google/go-tpm/tpmutil"
)
type RequestResources struct {
Attestation *keygen.KeyInfo
Endorsement *keygen.KeyInfo
DevID *keygen.KeyInfo
rw io.ReadWriter
}
func (rh *RequestResources) Flush() {
if rh.Attestation != nil && rh.Attestation.Handle != 0 {
err := tpm2.FlushContext(rh.rw, rh.Attestation.Handle)
if err != nil {
log.Fatalf("Failed to flush Attestation key: %v", err)
}
rh.Attestation.Handle = 0
}
if rh.Endorsement != nil && rh.Endorsement.Handle != 0 {
err := tpm2.FlushContext(rh.rw, rh.Endorsement.Handle)
if err != nil {
log.Fatalf("Failed to flush Endorsement key: %v", err)
}
rh.Endorsement.Handle = 0
}
if rh.DevID != nil && rh.DevID.Handle != 0 {
err := tpm2.FlushContext(rh.rw, rh.DevID.Handle)
if err != nil {
log.Fatalf("Failed to flush DevID key: %v", err)
}
rh.DevID.Handle = 0
}
}
func (rh *RequestResources) Activate(credentialBlob, secret []byte) ([]byte, error) {
hSession, err := createPolicySession(rh.rw)
if err != nil {
return nil, err
}
defer tpm2.FlushContext(rh.rw, hSession)
return tpm2.ActivateCredentialUsingAuth(
rh.rw,
[]tpm2.AuthCommand{
{Session: tpm2.HandlePasswordSession},
{Session: hSession},
},
rh.Attestation.Handle,
rh.Endorsement.Handle,
credentialBlob,
secret,
)
}
func createPolicySession(rw io.ReadWriter) (tpmutil.Handle, error) {
var nonceCaller [32]byte
hSession, _, err := tpm2.StartAuthSession(
rw,
tpm2.HandleNull,
tpm2.HandleNull,
nonceCaller[:],
nil,
tpm2.SessionPolicy,
tpm2.AlgNull,
tpm2.AlgSHA256,
)
if err != nil {
return 0, err
}
_, _, err = tpm2.PolicySecret(
rw,
tpm2.HandleEndorsement,
tpm2.AuthCommand{Session: tpm2.HandlePasswordSession},
hSession,
nil,
nil,
nil,
0,
)
if err != nil {
err = tpm2.FlushContext(rw, hSession)
if err != nil {
return 0, err
}
return 0, err
}
return hSession, nil
}