-
Notifications
You must be signed in to change notification settings - Fork 1
/
yubikey.go
192 lines (168 loc) · 4.75 KB
/
yubikey.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
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"log"
"strconv"
"time"
"github.com/go-piv/piv-go/piv"
"golang.org/x/crypto/ssh"
)
type Core struct {
YK *piv.YubiKey
ManagementKey [24]byte
Pin []byte
Pub *ecdsa.PublicKey
Priv *piv.ECDSAPrivateKey
}
func (core *Core) setPinToYubiKey() {
if err := ensureYK(core.YK); err != nil {
log.Fatal("Need Keep YubiKey inserted")
}
var managedKey [24]byte
if _, err := rand.Read(managedKey[:]); err != nil {
log.Fatal("Read random failed", err)
}
if err := core.YK.SetManagementKey(piv.DefaultManagementKey, managedKey); err != nil {
log.Fatalln("The default Management Key did not work", err)
}
if err := core.YK.SetMetadata(managedKey, &piv.Metadata{
ManagementKey: &managedKey,
}); err != nil {
log.Fatalln("Failed to store the Management Key on the device:", err)
}
if err := core.YK.SetPIN(piv.DefaultPIN, string(core.Pin)); err != nil {
log.Fatalln("The default PIN did not work", err)
}
if err := core.YK.SetPUK(piv.DefaultPUK, string(core.Pin)); err != nil {
log.Fatalln("The default PUK did not work", err)
}
core.ManagementKey = managedKey
}
func (core *Core) generateKeyPair() {
if err := ensureYK(core.YK); err != nil {
log.Fatal("Need Keep YubiKey inserted")
}
version := strconv.Itoa(core.YK.Version().Major) + "." + strconv.Itoa(core.YK.Version().Minor) + "." + strconv.Itoa(core.YK.Version().Patch)
pub, err := core.YK.GenerateKey(core.ManagementKey, piv.SlotAuthentication, piv.Key{
Algorithm: piv.AlgorithmEC256,
PINPolicy: piv.PINPolicyOnce,
TouchPolicy: piv.TouchPolicyAlways,
})
if err != nil {
log.Fatalln("Failed to generate key:", err)
}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalln("Failed to generate parent key:", err)
}
parent := &x509.Certificate{
Subject: pkix.Name{
Organization: []string{"timedia"},
OrganizationalUnit: []string{version},
},
PublicKey: priv.Public(),
}
template := &x509.Certificate{
Subject: pkix.Name{
CommonName: "SSH Standard Key Certificate",
},
NotAfter: time.Now().AddDate(10, 0, 0),
NotBefore: time.Now(),
SerialNumber: randomSerialNumber(),
KeyUsage: x509.KeyUsageKeyAgreement | x509.KeyUsageDigitalSignature,
}
certBytes, err := x509.CreateCertificate(rand.Reader, template, parent, pub, priv)
if err != nil {
log.Fatalln("Failed to generate certificate:", err)
}
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
log.Fatalln("Failed to parse certificate:", err)
}
if err := core.YK.SetCertificate(core.ManagementKey, piv.SlotAuthentication, cert); err != nil {
log.Fatalln("Failed to store certificate:", err)
}
}
func getCertificate(yk *piv.YubiKey) *x509.Certificate {
cert, err := yk.Attest(piv.SlotAuthentication)
if err != nil {
log.Fatal("could not get certificate from yubikey", err)
}
return cert
}
func (core *Core) GetECDSAPublicKey() {
if err := core.getECDSAPublicKey(); err != nil {
log.Fatal(err)
}
}
func (core *Core) getECDSAPublicKey() error {
cert := getCertificate(core.YK)
newPubKeyECDSA, ok := cert.PublicKey.(*ecdsa.PublicKey)
if !ok {
return errors.New("generated ECDSA public key on yubikey is invalid")
}
core.Pub = newPubKeyECDSA
return nil
}
func (core *Core) GetPrivateKey() {
if err := core.getPrivateKey(); err != nil {
log.Fatal(err)
}
}
func (core *Core) getPrivateKey() error {
cert := getCertificate(core.YK)
pub, err := ssh.NewPublicKey(cert.PublicKey)
if err != nil {
return errors.New("failed to process public key:" + err.Error())
}
priv, err := core.YK.PrivateKey(
piv.SlotAuthentication,
pub.(ssh.CryptoPublicKey).CryptoPublicKey(),
piv.KeyAuth{PIN: string(core.Pin)},
)
if err != nil {
return errors.New("failed to prepare private key:" + err.Error())
}
ykPriv, ok := priv.(*piv.ECDSAPrivateKey)
if !ok {
return errors.New("cannnot Transfrom this key")
}
core.Priv = ykPriv
return nil
}
func (core *Core) AuthenticatePin() {
if err := ensureYK(core.YK); err != nil {
log.Fatal("Need Keep YubiKey inserted")
}
core.GetManagementKey()
}
func (core *Core) GetManagementKey() {
meta, err := core.YK.Metadata(string(core.Pin))
if err != nil {
log.Fatal("cannot get metadata", err)
}
if meta.ManagementKey == nil {
log.Fatal("invalid Pin")
}
key := *meta.ManagementKey
core.ManagementKey = key
}
func (core *Core) GenerateAESKey() []byte {
log.Println("Press your Yubikey")
bytes, err := core.Priv.SharedKey(core.Pub)
if err != nil {
log.Fatal(err)
}
return bytes[:24]
}
func (core *Core) ResetPin() {
log.Println("Resetting YubiKey PIV applet...")
if err := core.YK.Reset(); err != nil {
log.Fatalln("Failed to reset YubiKey:", err)
}
}