-
Notifications
You must be signed in to change notification settings - Fork 500
/
pivkey.go
284 lines (238 loc) · 5.79 KB
/
pivkey.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
//go:build pivkey && cgo
// +build pivkey,cgo
// Copyright 2021 The Sigstore Authors.
//
// 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 pivkey
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"errors"
"fmt"
"io"
"os"
"syscall"
"github.com/go-piv/piv-go/piv"
"golang.org/x/term"
"github.com/sigstore/sigstore/pkg/signature"
)
var (
KeyNotInitialized error = errors.New("key not initialized")
SlotNotSet error = errors.New("slot not set")
)
type Key struct {
Pub crypto.PublicKey
Priv crypto.PrivateKey
card *piv.YubiKey
slot *piv.Slot
pin string
}
func GetKey() (*Key, error) {
cards, err := piv.Cards()
if err != nil {
return nil, err
}
if len(cards) == 0 {
return nil, errors.New("no cards found")
}
if len(cards) > 1 {
return nil, fmt.Errorf("found %d cards, please attach only one", len(cards))
}
yk, err := piv.Open(cards[0])
if err != nil {
return nil, err
}
return &Key{card: yk}, nil
}
func GetKeyWithSlot(slot string) (*Key, error) {
card, err := GetKey()
if err != nil {
return nil, fmt.Errorf("open key: %w", err)
}
card.slot = SlotForName(slot)
return card, nil
}
func (k *Key) Close() {
k.Pub = nil
k.Priv = nil
k.slot = nil
k.pin = ""
k.card.Close()
}
func (k *Key) Authenticate(pin string) {
k.pin = pin
}
func (k *Key) SetSlot(slot string) {
k.slot = SlotForName(slot)
}
func (k *Key) Attest() (*x509.Certificate, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
return k.card.Attest(*k.slot)
}
func (k *Key) GetAttestationCertificate() (*x509.Certificate, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
return k.card.AttestationCertificate()
}
func (k *Key) SetManagementKey(old, new [24]byte) error {
if k.card == nil {
return KeyNotInitialized
}
return k.card.SetManagementKey(old, new)
}
func (k *Key) SetPIN(old, new string) error {
if k.card == nil {
return KeyNotInitialized
}
return k.card.SetPIN(old, new)
}
func (k *Key) SetPUK(old, new string) error {
if k.card == nil {
return KeyNotInitialized
}
return k.card.SetPUK(old, new)
}
func (k *Key) Reset() error {
if k.card == nil {
return KeyNotInitialized
}
return k.card.Reset()
}
func (k *Key) Unblock(puk, newPIN string) error {
if k.card == nil {
return KeyNotInitialized
}
return k.card.Unblock(puk, newPIN)
}
func (k *Key) GenerateKey(mgmtKey [24]byte, slot piv.Slot, opts piv.Key) (crypto.PublicKey, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
return k.card.GenerateKey(mgmtKey, slot, opts)
}
func (k *Key) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) {
return k.Pub, nil
}
func (k *Key) VerifySignature(signature, message io.Reader, opts ...signature.VerifyOption) error {
sig, err := io.ReadAll(signature)
if err != nil {
return fmt.Errorf("read signature: %w", err)
}
msg, err := io.ReadAll(message)
if err != nil {
return fmt.Errorf("read message: %w", err)
}
digest := sha256.Sum256(msg)
att, err := k.Attest()
if err != nil {
return fmt.Errorf("get attestation: %w", err)
}
switch kt := att.PublicKey.(type) {
case *ecdsa.PublicKey:
if ecdsa.VerifyASN1(kt, digest[:], sig) {
return nil
}
return errors.New("invalid ecdsa signature")
case *rsa.PublicKey:
return rsa.VerifyPKCS1v15(kt, crypto.SHA256, digest[:], sig)
}
return fmt.Errorf("unsupported key type: %T", att.PublicKey)
}
func getPin() (string, error) {
fmt.Fprint(os.Stderr, "Enter PIN for security key: ")
// Unnecessary convert of syscall.Stdin on *nix, but Windows is a uintptr
// nolint:unconvert
b, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
fmt.Fprintln(os.Stderr, "\nPlease tap security key...")
return string(b), err
}
func (k *Key) Verifier() (signature.Verifier, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
if k.slot == nil {
return nil, SlotNotSet
}
cert, err := k.card.Attest(*k.slot)
if err != nil {
return nil, err
}
k.Pub = cert.PublicKey
return k, nil
}
func (k *Key) Certificate() (*x509.Certificate, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
if k.slot == nil {
return nil, SlotNotSet
}
return k.card.Certificate(*k.slot)
}
func (k *Key) SignerVerifier() (signature.SignerVerifier, error) {
if k.card == nil {
return nil, KeyNotInitialized
}
if k.slot == nil {
return nil, SlotNotSet
}
cert, err := k.card.Attest(*k.slot)
if err != nil {
return nil, err
}
k.Pub = cert.PublicKey
var auth piv.KeyAuth
if k.pin == "" {
auth.PINPrompt = getPin
} else {
auth.PIN = k.pin
}
privKey, err := k.card.PrivateKey(*k.slot, cert.PublicKey, auth)
if err != nil {
return nil, err
}
k.Priv = privKey
return k, nil
}
func (k *Key) Sign(ctx context.Context, rawPayload []byte) ([]byte, []byte, error) {
signer := k.Priv.(crypto.Signer)
h := sha256.Sum256(rawPayload)
sig, err := signer.Sign(rand.Reader, h[:], crypto.SHA256)
if err != nil {
return nil, nil, err
}
return sig, h[:], err
}
func (k *Key) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) {
signer := k.Priv.(crypto.Signer)
h := sha256.New()
if _, err := io.Copy(h, message); err != nil {
return nil, err
}
sig, err := signer.Sign(rand.Reader, h.Sum(nil), crypto.SHA256)
if err != nil {
return nil, err
}
return sig, err
}