/
securitycmd.go
219 lines (208 loc) · 5.27 KB
/
securitycmd.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
package main
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"github.com/MeshBoxFoundation/meshbox/accounts/keystore"
"github.com/MeshBoxFoundation/meshbox/cmd/utils"
"github.com/MeshBoxFoundation/meshbox/crypto"
"github.com/MeshBoxFoundation/meshbox/node"
"github.com/pborman/uuid"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"os"
"path/filepath"
)
const PWD_MIN_LEN = 6
var (
security = new(Security)
securityCommand = cli.Command{
Before: func(ctx *cli.Context) error {
fmt.Println("-----------------------")
if ctx.Bool(utils.TestnetFlag.Name) {
fmt.Println("- TESTNET ")
os.Setenv("TESTNET", "1")
} else if ctx.Bool(utils.DevnetFlag.Name) {
fmt.Println("- DEVNET ")
os.Setenv("DEVNET", "1")
} else {
fmt.Println("- MAINNET ")
}
security.homeDir = node.DefaultNodekeyDir()
fmt.Println("home_dir :", security.homeDir)
fmt.Println("-----------------------")
security.nodekey = filepath.Join(security.homeDir, "nodekey")
security.nodekeyPrv = filepath.Join(security.homeDir, "nodekey.prv")
return nil
},
Action: security.run,
Name: "security",
Usage: "encrypt / decrypt nodekey, params mutual exclusion, only one choice",
ArgsUsage: "<TODO>",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "testnet,t",
Usage: "",
},
cli.BoolFlag{
Name: "unlock,l",
Usage: "unlock nodekey with password",
Destination: &security.l,
},
cli.BoolFlag{
Name: "passwd,p",
Usage: "set or reset password",
Destination: &security.p,
},
},
Category: "BLOCKCHAIN COMMANDS",
Description: `TODO`,
}
)
type Security struct {
homeDir, nodekey, nodekeyPrv string
l, p bool
prv *ecdsa.PrivateKey
}
func (self *Security) run(ctx *cli.Context) error {
if security.l && security.p {
fmt.Println("================================================================================")
fmt.Println(" INPUT : unlock=", security.l, " ; passwd=", security.p)
fmt.Println(" ERROR : params mutual exclusion, only one choice")
fmt.Println("================================================================================")
}
switch {
case security.l:
self.unlockCmd()
case security.p:
self.passwdCmd()
}
return nil
}
func (self *Security) unlockCmd() {
var (
pwd string
counter int
pwdfile = filepath.Join(security.homeDir, "nodekey.pwd")
)
data, err := ioutil.ReadFile(self.nodekeyPrv)
if err != nil {
fmt.Println("Please set password first.")
fmt.Println("ERROR :", err)
}
kjson, err := hex.DecodeString(string(data))
if err != nil {
fmt.Println("ERROR:", err)
return
}
INPUT:
fmt.Println("Please input password : ")
fmt.Scanln(&pwd)
if _, err := keystore.DecryptKey(kjson, pwd); err != nil {
counter++
fmt.Println(counter, "❌ Wrong password .")
if counter < 3 {
goto INPUT
}
return
}
os.Remove(pwdfile)
pwdhex := hex.EncodeToString([]byte(pwd))
if err := ioutil.WriteFile(pwdfile, []byte(pwdhex), 0666); err == nil {
fmt.Println("😊 Success.")
} else {
fmt.Println("😢 Error :", err)
}
}
func (self *Security) passwdCmd() {
var (
oldpwd, pwd string
)
data, err := ioutil.ReadFile(self.nodekeyPrv)
if err != nil {
counter := 0
// set password and move nodekey to nodekey.prv
if err := self.loadNodekey(self.nodekey); err != nil {
fmt.Println("ERROR ::", err)
return
}
defer os.Remove(self.nodekey)
INPUT:
fmt.Println("Please input password : ")
fmt.Scanln(&pwd)
if len(pwd) < PWD_MIN_LEN {
counter++
fmt.Println(counter, "❌ The password length needs to be greater than", PWD_MIN_LEN)
if counter < 3 {
goto INPUT
}
return
}
self.storeKey(self.nodekeyPrv, pwd)
} else {
// reset password
counter := 0
fmt.Println("Please input old password : ")
fmt.Scanln(&oldpwd)
kjson, err := hex.DecodeString(string(data))
if err != nil {
fmt.Println("ERROR:", err)
return
}
key, err := keystore.DecryptKey(kjson, oldpwd)
if err != nil {
fmt.Println("ERROR:", err)
return
}
NEWPWD:
fmt.Println("Please input new password : ")
fmt.Scanln(&pwd)
if len(pwd) < PWD_MIN_LEN {
counter++
fmt.Println(counter, "❌ The new password length needs to be greater than", PWD_MIN_LEN)
if counter < 3 {
goto NEWPWD
}
return
}
self.prv = key.PrivateKey
os.Remove(self.nodekeyPrv)
self.storeKey(self.nodekeyPrv, pwd)
}
}
func (self *Security) loadNodekey(path string) error {
f, err := os.Open(path)
if err != nil {
prv, err := crypto.GenerateKey()
if err != nil {
return err
}
self.prv = prv
}
buf := make([]byte, 512)
if n, err := f.Read(buf); err == nil {
k := string(buf[0:n])
data, _ := hex.DecodeString(k)
prv, err := crypto.ToECDSA(data)
if err != nil {
return err
}
self.prv = prv
}
return nil
}
func (self *Security) storeKey(path, passphrase string) {
id := uuid.NewRandom()
key := &keystore.Key{
Id: id,
Address: crypto.PubkeyToAddress(self.prv.PublicKey),
PrivateKey: self.prv,
}
keyjson, _ := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP)
hexkey := hex.EncodeToString(keyjson)
if err := ioutil.WriteFile(path, []byte(hexkey), 0666); err == nil {
fmt.Println("😊 Success.")
} else {
fmt.Println("😢 Error :", err)
}
}