-
Notifications
You must be signed in to change notification settings - Fork 3
/
pseudohsm.go
179 lines (156 loc) · 4.62 KB
/
pseudohsm.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
// Package pseudohsm provides a pseudo HSM for development environments.
package pseudohsm
import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/errors"
"github.com/pborman/uuid"
)
// pre-define errors for supporting bytom errorFormatter
var (
ErrDuplicateKeyAlias = errors.New("duplicate key alias")
ErrLoadKey = errors.New("key not found or wrong password ")
ErrDecrypt = errors.New("could not decrypt key with given passphrase")
)
// HSM type for storing pubkey and privatekey
type HSM struct {
cacheMu sync.Mutex
keyStore keyStore
cache *keyCache
//kdCache map[chainkd.XPub]chainkd.XPrv
}
// XPub type for pubkey for anyone can see
type XPub struct {
Alias string `json:"alias"`
XPub chainkd.XPub `json:"xpub"`
File string `json:"file"`
}
// New method for HSM struct
func New(keypath string) (*HSM, error) {
keydir, _ := filepath.Abs(keypath)
return &HSM{
keyStore: &keyStorePassphrase{keydir, LightScryptN, LightScryptP},
cache: newKeyCache(keydir),
//kdCache: make(map[chainkd.XPub]chainkd.XPrv),
}, nil
}
// XCreate produces a new random xprv and stores it in the db.
func (h *HSM) XCreate(alias string, auth string) (*XPub, error) {
h.cacheMu.Lock()
defer h.cacheMu.Unlock()
normalizedAlias := strings.ToLower(strings.TrimSpace(alias))
if ok := h.cache.hasAlias(normalizedAlias); ok {
return nil, ErrDuplicateKeyAlias
}
xpub, _, err := h.createChainKDKey(auth, normalizedAlias, false)
if err != nil {
return nil, err
}
h.cache.add(*xpub)
return xpub, err
}
func (h *HSM) createChainKDKey(auth string, alias string, get bool) (*XPub, bool, error) {
xprv, xpub, err := chainkd.NewXKeys(nil)
if err != nil {
return nil, false, err
}
id := uuid.NewRandom()
key := &XKey{
ID: id,
KeyType: "bytom_kd",
XPub: xpub,
XPrv: xprv,
Alias: alias,
}
file := h.keyStore.JoinPath(keyFileName(key.ID.String()))
if err := h.keyStore.StoreKey(file, key, auth); err != nil {
return nil, false, errors.Wrap(err, "storing keys")
}
return &XPub{XPub: xpub, Alias: alias, File: file}, true, nil
}
// ListKeys returns a list of all xpubs from the store
func (h *HSM) ListKeys() []XPub {
xpubs := h.cache.keys()
return xpubs
}
// XSign looks up the xprv given the xpub, optionally derives a new
// xprv with the given path (but does not store the new xprv), and
// signs the given msg.
func (h *HSM) XSign(xpub chainkd.XPub, path [][]byte, msg []byte, auth string) ([]byte, error) {
xprv, err := h.LoadChainKDKey(xpub, auth)
if err != nil {
return nil, err
}
if len(path) > 0 {
xprv = xprv.Derive(path)
}
return xprv.Sign(msg), nil
}
//LoadChainKDKey get xprv from xpub
func (h *HSM) LoadChainKDKey(xpub chainkd.XPub, auth string) (xprv chainkd.XPrv, err error) {
h.cacheMu.Lock()
defer h.cacheMu.Unlock()
//if xprv, ok := h.kdCache[xpub]; ok {
// return xprv, nil
//}
_, xkey, err := h.loadDecryptedKey(xpub, auth)
if err != nil {
return xprv, ErrLoadKey
}
//h.kdCache[xpb.XPub] = xkey.XPrv
return xkey.XPrv, nil
}
// XDelete deletes the key matched by xpub if the passphrase is correct.
// If a contains no filename, the address must match a unique key.
func (h *HSM) XDelete(xpub chainkd.XPub, auth string) error {
// Decrypting the key isn't really necessary, but we do
// it anyway to check the password and zero out the key
// immediately afterwards.
xpb, xkey, err := h.loadDecryptedKey(xpub, auth)
if xkey != nil {
zeroKey(xkey)
}
if err != nil {
return err
}
h.cacheMu.Lock()
// The order is crucial here. The key is dropped from the
// cache after the file is gone so that a reload happening in
// between won't insert it into the cache again.
err = os.Remove(xpb.File)
if err == nil {
h.cache.delete(xpb)
}
h.cacheMu.Unlock()
return err
}
func (h *HSM) loadDecryptedKey(xpub chainkd.XPub, auth string) (XPub, *XKey, error) {
h.cache.maybeReload()
h.cache.mu.Lock()
xpb, err := h.cache.find(XPub{XPub: xpub})
h.cache.mu.Unlock()
if err != nil {
return xpb, nil, err
}
xkey, err := h.keyStore.GetKey(xpb.Alias, xpb.File, auth)
return xpb, xkey, err
}
// ResetPassword reset passphrase for an existing xpub
func (h *HSM) ResetPassword(xpub chainkd.XPub, oldAuth, newAuth string) error {
xpb, xkey, err := h.loadDecryptedKey(xpub, oldAuth)
if err != nil {
return err
}
return h.keyStore.StoreKey(xpb.File, xkey, newAuth)
}
// HasAlias check whether the key alias exists
func (h *HSM) HasAlias(alias string) bool {
return h.cache.hasAlias(alias)
}
// HasKey check whether the private key exists
func (h *HSM) HasKey(xprv chainkd.XPrv) bool {
return h.cache.hasKey(xprv.XPub())
}