-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileks.go
185 lines (143 loc) · 3.99 KB
/
fileks.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package csp
import (
"io/ioutil"
"os"
"sync"
"errors"
"encoding/hex"
"fmt"
"path/filepath"
log "github.com/33cn/chain33/common/log/log15"
"github.com/GM-Publicchain/gm/plugin/dapp/cert/authority/tools/cryptogen/factory/utils"
auth "github.com/GM-Publicchain/gm/plugin/dapp/cert/authority/utils"
)
var logger = log.New("tools", "cryptogen")
// NewFileBasedKeyStore 创建key存储器
func NewFileBasedKeyStore(pwd []byte, path string, readOnly bool) (KeyStore, error) {
ks := &fileBasedKeyStore{}
return ks, ks.Init(pwd, path, readOnly)
}
type fileBasedKeyStore struct {
path string
readOnly bool
isOpen bool
pwd []byte
m sync.Mutex
}
func (ks *fileBasedKeyStore) Init(pwd []byte, path string, readOnly bool) error {
if len(path) == 0 {
return errors.New("An invalid KeyStore path provided. Path cannot be an empty string")
}
ks.m.Lock()
defer ks.m.Unlock()
if ks.isOpen {
return errors.New("KeyStore already initilized")
}
ks.path = path
ks.pwd = utils.Clone(pwd)
err := ks.createKeyStoreIfNotExists()
if err != nil {
return err
}
err = ks.openKeyStore()
if err != nil {
return err
}
ks.readOnly = readOnly
return nil
}
func (ks *fileBasedKeyStore) ReadOnly() bool {
return ks.readOnly
}
func (ks *fileBasedKeyStore) StoreKey(k Key) (err error) {
if ks.readOnly {
return errors.New("Read only KeyStore")
}
if k == nil {
return errors.New("Invalid key. It must be different from nil")
}
switch k.(type) {
case *ecdsaPrivateKey:
kk := k.(*ecdsaPrivateKey)
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.privKey)
if err != nil {
return fmt.Errorf("Failed storing ECDSA private key [%s]", err)
}
case *ecdsaPublicKey:
kk := k.(*ecdsaPublicKey)
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.pubKey)
if err != nil {
return fmt.Errorf("Failed storing ECDSA public key [%s]", err)
}
case *SM2PrivateKey:
kk := k.(*SM2PrivateKey)
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.PrivKey)
if err != nil {
return fmt.Errorf("Failed storing SM2 private key [%s]", err)
}
case *SM2PublicKey:
kk := k.(*SM2PublicKey)
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.PubKey)
if err != nil {
return fmt.Errorf("Failed storing SM2 public key [%s]", err)
}
default:
return fmt.Errorf("Key type not reconigned [%s]", k)
}
return
}
func (ks *fileBasedKeyStore) storePrivateKey(alias string, privateKey interface{}) error {
rawKey, err := utils.PrivateKeyToPEM(privateKey, ks.pwd)
if err != nil {
logger.Error("Failed converting private key to PEM [%s]: [%s]", alias, err)
return err
}
err = ioutil.WriteFile(ks.getPathForAlias(alias, "sk"), rawKey, 0700)
if err != nil {
logger.Error("Failed storing private key [%s]: [%s]", alias, err)
return err
}
return nil
}
func (ks *fileBasedKeyStore) storePublicKey(alias string, publicKey interface{}) error {
rawKey, err := utils.PublicKeyToPEM(publicKey, ks.pwd)
if err != nil {
logger.Error("Failed converting public key to PEM [%s]: [%s]", alias, err)
return err
}
err = ioutil.WriteFile(ks.getPathForAlias(alias, "pk"), rawKey, 0700)
if err != nil {
logger.Error("Failed storing private key [%s]: [%s]", alias, err)
return err
}
return nil
}
func (ks *fileBasedKeyStore) createKeyStoreIfNotExists() error {
ksPath := ks.path
missing, _ := auth.DirMissingOrEmpty(ksPath)
if missing {
err := ks.createKeyStore()
if err != nil {
logger.Error("Failed creating KeyStore At [%s]: [%s]", ksPath, err.Error())
return err
}
}
return nil
}
func (ks *fileBasedKeyStore) createKeyStore() error {
ksPath := ks.path
err := os.MkdirAll(ksPath, 0750)
return err
}
func (ks *fileBasedKeyStore) openKeyStore() error {
if ks.isOpen {
return nil
}
return nil
}
func (ks *fileBasedKeyStore) getPathForAlias(alias, suffix string) string {
return filepath.Join(ks.path, alias+"_"+suffix)
}