-
Notifications
You must be signed in to change notification settings - Fork 0
/
yubihsm2.go
235 lines (207 loc) · 5.55 KB
/
yubihsm2.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
package hsm
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"errors"
"fmt"
"io"
"log"
"math/big"
"os"
"sync"
"syscall"
"github.com/KarpelesLab/hsm/yubihsm2"
"golang.org/x/crypto/ssh/terminal"
)
type YubiHSM2 struct {
sm *yubihsm2.SessionManager
}
type YubiHSM2Key struct {
parent *YubiHSM2
kid yubihsm2.ObjectID
info *yubihsm2.ObjectInfoResponse
getInfo sync.Once
}
func NewYubiHSM2() (HSM, error) {
c := yubihsm2.NewHTTPConnector("localhost:12345")
status, err := c.GetStatus()
if err != nil {
return nil, err
}
log.Printf("Connected to YubiHSM manager v%s", status.Version)
if status.Status != "OK" {
log.Printf("Key status invalid: %s", status.Status)
return nil, fmt.Errorf("unable to access key: %s", status.Status)
}
attempt := 1
for {
fmt.Print("Enter passphrase for YubiHSM2 Key 1: ")
pwd, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
// failed to read from terminal → fail now
return nil, err
}
fmt.Printf("\n")
sm, err := yubihsm2.NewSessionManager(c, 1, string(pwd))
if err != nil {
fmt.Printf("Failed to unlock YubiHSM2: %s\n", err)
attempt += 1
if attempt <= 3 {
continue
}
return nil, err
}
return &YubiHSM2{sm}, nil
}
}
func (h *YubiHSM2) Ready() bool {
return h.sm != nil
}
func (h *YubiHSM2) ListKeys() ([]Key, error) {
res, err := h.sm.ListObjects(yubihsm2.AsymmetricKey)
if err != nil {
return nil, err
}
var f []Key
for _, i := range res {
f = append(f, &YubiHSM2Key{parent: h, kid: i.ObjectID})
}
return f, nil
}
func (h *YubiHSM2) ListKeysByName(name string) ([]Key, error) {
res, err := h.sm.ListObjects(yubihsm2.AsymmetricKey, yubihsm2.Label(name))
if err != nil {
return nil, err
}
var f []Key
for _, i := range res {
f = append(f, &YubiHSM2Key{parent: h, kid: i.ObjectID})
}
return f, nil
}
func (h *YubiHSM2) PutCertificate(name string, cert *x509.Certificate) error {
res, err := h.sm.ListObjects(yubihsm2.TypeOpaque, yubihsm2.OpaqueX509Cert, yubihsm2.Label(name))
if err != nil {
return err
}
var id yubihsm2.ObjectID
if len(res) > 0 {
id = res[0].ObjectID
}
// send certificate
_, err = h.sm.PutOpaque(id, []byte(name), 1, 0, yubihsm2.OpaqueX509Cert, cert.Raw)
return err
}
func (h *YubiHSM2) GetCertificate(name string) (*x509.Certificate, error) {
res, err := h.sm.ListObjects(yubihsm2.TypeOpaque, yubihsm2.OpaqueX509Cert, yubihsm2.Label(name))
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, os.ErrNotExist
}
// grab data
der, err := h.sm.GetOpaque(res[0].ObjectID)
if err != nil {
return nil, err
}
return x509.ParseCertificate(der)
}
func (k *YubiHSM2Key) Public() crypto.PublicKey {
key, err := k.parent.sm.GetPubKey(k.kid)
if err != nil {
return nil
}
switch key.Algorithm {
case yubihsm2.Ed25519:
return ed25519.PublicKey(key.KeyData)
case yubihsm2.Secp256r1:
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: new(big.Int).SetBytes(key.KeyData[:32]),
Y: new(big.Int).SetBytes(key.KeyData[32:]),
}
case yubihsm2.Secp384r1:
return &ecdsa.PublicKey{
Curve: elliptic.P384(),
X: new(big.Int).SetBytes(key.KeyData[:48]),
Y: new(big.Int).SetBytes(key.KeyData[48:]),
}
case yubihsm2.Secp521r1:
// key size, 64 or 66?
return &ecdsa.PublicKey{
Curve: elliptic.P521(),
X: new(big.Int).SetBytes(key.KeyData[:66]),
Y: new(big.Int).SetBytes(key.KeyData[66:]),
}
case yubihsm2.Rsa2048, yubihsm2.Rsa3072, yubihsm2.Rsa4096:
return &rsa.PublicKey{
N: big.NewInt(0).SetBytes(key.KeyData),
E: 65537, // YubiHSM2 has a fixed value for RSA e
}
default:
return key.KeyData
}
}
func (k *YubiHSM2Key) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
k.getInfo.Do(k.doGetInfo)
switch k.info.Algorithm {
case yubihsm2.Ed25519:
if opts.HashFunc() != crypto.Hash(0) {
return nil, errors.New("ed25519: cannot sign hashed message")
}
if len(digest) > 2000 { // give or take
return nil, errors.New("ed25519: message too large")
}
return k.parent.sm.SignDataEddsa(k.kid, digest)
case yubihsm2.Secp256r1, yubihsm2.Secp384r1, yubihsm2.Secp521r1:
return k.parent.sm.SignDataEcdsa(k.kid, digest)
case yubihsm2.Rsa2048, yubihsm2.Rsa3072, yubihsm2.Rsa4096:
if pssO, ok := opts.(*rsa.PSSOptions); ok {
// this uses PSS. Algo needs to match the current algo
var algo yubihsm2.Algorithm
switch pssO.Hash {
case crypto.SHA1:
algo = yubihsm2.RsaPssSha1
case crypto.SHA256:
algo = yubihsm2.RsaPssSha256
case crypto.SHA384:
algo = yubihsm2.RsaPssSha384
case crypto.SHA512:
algo = yubihsm2.RsaPssSha512
default:
return nil, errors.New("unsupported hashing algorithm")
}
return k.parent.sm.SignDataPss(k.kid, algo, uint16(pssO.SaltLength), digest)
}
return k.parent.sm.SignDataPkcs1(k.kid, digest)
}
// Depend on type of key!
return nil, errors.New("todo")
}
func (k *YubiHSM2Key) String() string {
k.getInfo.Do(k.doGetInfo)
return fmt.Sprintf("YubiHSM2 Key(0x%x Cap=0x%x Algo=%s Label=%s)", k.kid, k.info.Capabilities, k.info.Algorithm.String(), k.info.Label)
}
func (k *YubiHSM2Key) doGetInfo() {
// grab info from yubihsm
info, err := k.parent.sm.GetObjectInfo(k.kid, yubihsm2.AsymmetricKey)
if err != nil {
log.Printf("Get info failed: %s", err)
k.info = &yubihsm2.ObjectInfoResponse{}
} else {
k.info = info
}
}
func (k *YubiHSM2Key) PublicBlob() ([]byte, error) {
key, err := k.parent.sm.GetPubKey(k.kid)
if err != nil {
return nil, err
}
// we have key.Algorithm too
return key.KeyData, nil
}