-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_public.go
239 lines (192 loc) · 6.63 KB
/
read_public.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
package main
import (
"crypto/x509"
"encoding/binary"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpmutil"
"github.com/paulgriffiths/pgtpm"
)
// readPublic reads a TPM object's public area.
func readPublic() error {
var pub tpm2.Public
var nameAlg pgtpm.Algorithm
var qnameAlg pgtpm.Algorithm
var nameHash []byte
var qnameHash []byte
err := ensureExactlyOnePassed(fReadPublicSet, inFlagName, handleFlagName)
if err != nil {
return err
}
// Read a public area from a file, or from a TPM.
if *fReadPublicIn == "" {
var handle = pgtpm.Handle(fReadPublicHandle)
t, err := getTPM(*fReadPublicTPM)
if err != nil {
return err
}
defer t.Close()
pub, nameHash, qnameHash, err = tpm2.ReadPublic(t, tpmutil.Handle(handle))
if err != nil {
return fmt.Errorf("failed to read public area: %v", err)
}
nameAlg = pgtpm.Algorithm(binary.BigEndian.Uint16(nameHash))
nameHash = nameHash[2:]
qnameAlg = pgtpm.Algorithm(binary.BigEndian.Uint16(qnameHash))
qnameHash = qnameHash[2:]
} else {
data, err := ioutil.ReadFile(*fReadPublicIn)
if err != nil {
return fmt.Errorf("failed to read public area: %v", err)
}
pub, err = tpm2.DecodePublic(data)
if err != nil {
return fmt.Errorf("failed to decode public area: %v", err)
}
name, err := pub.Name()
if err != nil {
return fmt.Errorf("failed to get name from public area: %v", err)
}
if name.Digest != nil {
nameHash = name.Digest.Value
nameAlg = pgtpm.Algorithm(name.Digest.Alg)
}
}
// Write the raw public area, if requested.
if *fReadPublicOut != "" || (!*fReadPublicText && !*fReadPublicPubOut) {
var f *os.File
var err error
if *fReadPublicOut != "" {
f, err = os.Create(*fReadPublicOut)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer f.Close()
} else {
f = os.Stdout
}
data, err := pub.Encode()
if err != nil {
return fmt.Errorf("failed to encode public area: %v", err)
}
_, err = f.Write(data)
if err != nil {
return fmt.Errorf("failed to write public area: %v", err)
}
}
// Write the public area as text, if requested.
if *fReadPublicText {
const fw = 21
fmt.Printf("%-*s: %s\n", fw, "Type", pgtpm.Algorithm(pub.Type).String())
fmt.Printf("%-*s: %s\n", fw, "Name algorithm", pgtpm.Algorithm(pub.NameAlg).String())
if nameHash != nil {
fmt.Printf("%-*s: %s (%s)\n", fw, "Name", hexEncodeBytes(nameHash[2:]), nameAlg.String())
}
if qnameHash != nil {
fmt.Printf("%-*s: %s (%s)\n", fw, "Qualified name", hexEncodeBytes(qnameHash[2:]), qnameAlg.String())
}
if pub.Attributes != 0 {
var first = true
for _, a := range []pgtpm.ObjectAttribute{
pgtpm.TPMA_OBJECT_FIXEDTPM,
pgtpm.TPMA_OBJECT_STCLEAR,
pgtpm.TPMA_OBJECT_FIXEDPARENT,
pgtpm.TPMA_OBJECT_SENSITIVEDATAORIGIN,
pgtpm.TPMA_OBJECT_USERWITHAUTH,
pgtpm.TPMA_OBJECT_ADMINWITHPOLICY,
pgtpm.TPMA_OBJECT_NODA,
pgtpm.TPMA_OBJECT_ENCRYPTEDDUPLICATION,
pgtpm.TPMA_OBJECT_RESTRICTED,
pgtpm.TPMA_OBJECT_DECRYPT,
pgtpm.TPMA_OBJECT_SIGN_ENCRYPT,
} {
if pgtpm.ObjectAttribute(pub.Attributes)&a != 0 {
var label string
if first {
label = "Attributes"
first = false
}
fmt.Printf("%-*s: %s\n", fw, label, a.String())
}
}
}
if len(pub.AuthPolicy) > 0 {
fmt.Printf("%-*s: %s\n", fw, "Auth policy", hexEncodeBytes([]byte(pub.AuthPolicy)))
}
switch {
case pub.RSAParameters != nil:
param := pub.RSAParameters
if sym := param.Symmetric; sym != nil {
fmt.Printf("%-*s: %s\n", fw, "Symmetric algorithm", pgtpm.Algorithm(sym.Alg).String())
fmt.Printf("%-*s: %d\n", fw, "Symmetric key bits", sym.KeyBits)
fmt.Printf("%-*s: %s\n", fw, "Symmetric mode", pgtpm.Algorithm(sym.Mode).String())
}
if sig := param.Sign; sig != nil {
fmt.Printf("%-*s: %s\n", fw, "Signature algorithm", pgtpm.Algorithm(sig.Alg).String())
fmt.Printf("%-*s: %s\n", fw, "Signature hash", pgtpm.Algorithm(sig.Hash).String())
}
fmt.Printf("%-*s: %d\n", fw, "Key bits", param.KeyBits)
var e = param.Exponent()
fmt.Printf("%-*s: %d (0x%x)\n", fw, "Exponent", e, e)
outputBigInt("Modulus", fw, param.Modulus())
case pub.ECCParameters != nil:
param := pub.ECCParameters
if sym := param.Symmetric; sym != nil {
fmt.Printf("%-*s: %s\n", fw, "Symmetric algorithm", pgtpm.Algorithm(sym.Alg).String())
fmt.Printf("%-*s: %d\n", fw, "Symmetric key bits", sym.KeyBits)
fmt.Printf("%-*s: %s\n", fw, "Symmetric mode", pgtpm.Algorithm(sym.Mode).String())
}
if sig := param.Sign; sig != nil {
fmt.Printf("%-*s: %s\n", fw, "Signature algorithm", pgtpm.Algorithm(sig.Alg).String())
fmt.Printf("%-*s: %s\n", fw, "Signature hash", pgtpm.Algorithm(sig.Hash).String())
if param.Sign.Alg.UsesCount() {
fmt.Printf("%-*s: %d\n", fw, "Signature count", pgtpm.Algorithm(sig.Count))
}
}
fmt.Printf("%-*s: %s\n", fw, "Elliptic curve", pgtpm.EllipticCurve(param.CurveID).String())
if kdf := param.KDF; kdf != nil {
fmt.Printf("%-*s: %s\n", fw, "KDF scheme algorithm", pgtpm.Algorithm(kdf.Alg).String())
fmt.Printf("%-*s: %s\n", fw, "KDF scheme hash", pgtpm.Algorithm(kdf.Hash).String())
}
outputBigInt("X point", fw, param.Point.X())
outputBigInt("Y point", fw, param.Point.Y())
case pub.SymCipherParameters != nil:
param := pub.SymCipherParameters
if sym := param.Symmetric; sym != nil {
fmt.Printf("%-*s: %s\n", fw, "Symmetric algorithm", pgtpm.Algorithm(sym.Alg).String())
fmt.Printf("%-*s: %d\n", fw, "Symmetric key bits", sym.KeyBits)
fmt.Printf("%-*s: %s\n", fw, "Symmetric mode", pgtpm.Algorithm(sym.Mode).String())
}
if uniq := param.Unique; len(uniq) > 0 {
fmt.Printf("%-*s: %s\n", fw, "Unique", hexEncodeBytes(uniq))
}
case pub.KeyedHashParameters != nil:
param := pub.KeyedHashParameters
fmt.Printf("%-*s: %s\n", fw, "Keyed hash algorithm", pgtpm.Algorithm(param.Alg).String())
fmt.Printf("%-*s: %s\n", fw, "Keyed hash hash", pgtpm.Algorithm(param.Hash).String())
fmt.Printf("%-*s: %s\n", fw, "Keyed hash KDF", pgtpm.Algorithm(param.KDF).String())
if uniq := param.Unique; len(uniq) > 0 {
fmt.Printf("%-*s: %s\n", fw, "Unique", hexEncodeBytes(uniq))
}
}
}
// Write the PEM-encoded public key, if requested.
if *fReadPublicPubOut {
key, err := pub.Key()
if err != nil {
return fmt.Errorf("failed to get public key from public area: %v", err)
}
der, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return fmt.Errorf("failed to marshal public key: %v", err)
}
fmt.Printf("%s", pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: der,
}))
}
return nil
}