/
util.go
201 lines (175 loc) · 5.11 KB
/
util.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
// Copyright © 2018 Immutability, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/hashicorp/vault/sdk/logical"
)
const (
// JWTAlgorithm is for secp256k1
JWTAlgorithm string = "ES256"
// ProtocolKeystore JSON keystore URLs start with this
ProtocolKeystore string = "keystore://"
// MaxKeystoreSize is a heuristic to prevent reading stupid big files
MaxKeystoreSize int64 = 1024
// RequestPathImport is the part of the path for import
RequestPathImport string = "import"
// RequestPathTrustees is the part of the path for trustees
RequestPathTrustees string = "trustees"
// PassphraseWords is the default number of words in a passphrase
PassphraseWords int = 9
// PassphraseSeparator separates PassphraseWords
PassphraseSeparator string = "-"
)
func prettyPrint(v interface{}) string {
jsonString, _ := json.Marshal(v)
var out bytes.Buffer
json.Indent(&out, jsonString, "", " ")
return out.String()
}
func (b *PluginBackend) writeTemporaryKeystoreFile(path string, filename string, data []byte) (string, error) {
keystorePath := path + "/" + filename
err := ioutil.WriteFile(keystorePath, data, 0644)
return keystorePath, err
}
func (b *PluginBackend) createTemporaryKeystoreDirectory() (string, error) {
dir, err := ioutil.TempDir("", "keystore")
return dir, err
}
func (b *PluginBackend) removeTemporaryKeystore(path string) error {
return os.RemoveAll(path)
}
func convertMapToStringValue(initial map[string]interface{}) map[string]string {
result := map[string]string{}
for key, value := range initial {
result[key] = fmt.Sprintf("%v", value)
}
return result
}
func parseURL(url string) (accounts.URL, error) {
parts := strings.Split(url, "://")
if len(parts) != 2 || parts[0] == "" {
return accounts.URL{}, errors.New("protocol scheme missing")
}
return accounts.URL{
Scheme: parts[0],
Path: parts[1],
}, nil
}
func (b *PluginBackend) importJSONKeystore(ctx context.Context, keystorePath string, passphrase string) (*ecdsa.PrivateKey, error) {
var key *keystore.Key
jsonKeystore, err := b.readJSONKeystore(keystorePath)
if err != nil {
return nil, err
}
key, err = keystore.DecryptKey(jsonKeystore, passphrase)
if err != nil {
return nil, err
}
if key == nil {
return nil, fmt.Errorf("failed to decrypt key")
}
return key.PrivateKey, err
}
func pathExists(ctx context.Context, req *logical.Request, path string) (bool, error) {
out, err := req.Storage.Get(ctx, path)
if err != nil {
return false, fmt.Errorf("existence check failed for %s: %v", path, err)
}
return out != nil, nil
}
func (b *PluginBackend) readJSONKeystore(keystorePath string) ([]byte, error) {
var jsonKeystore []byte
file, err := os.Open(keystorePath)
defer file.Close()
defer b.removeTemporaryKeystore(keystorePath)
stat, err := file.Stat()
if err != nil {
return nil, err
}
if stat.Size() > MaxKeystoreSize {
err = fmt.Errorf("keystore is suspiciously large at %d bytes", stat.Size())
return nil, err
}
jsonKeystore, err = ioutil.ReadFile(keystorePath)
if err != nil {
return nil, err
}
return jsonKeystore, nil
}
func (b *PluginBackend) readTrustee(ctx context.Context, req *logical.Request, name string) (*Trustee, error) {
path := fmt.Sprintf("trustees/%s", name)
entry, err := req.Storage.Get(ctx, path)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var trustee Trustee
err = entry.DecodeJSON(&trustee)
if entry == nil {
return nil, fmt.Errorf("failed to deserialize trustee at %s", path)
}
return &trustee, nil
}
func (b *PluginBackend) contains(stringSlice []string, searchString string) bool {
for _, value := range stringSlice {
if value == searchString {
return true
}
}
return false
}
func contains(stringSlice []string, searchString string) bool {
for _, value := range stringSlice {
if value == searchString {
return true
}
}
return false
}
func dedup(stringSlice []string) []string {
var returnSlice []string
for _, value := range stringSlice {
if !contains(returnSlice, value) {
returnSlice = append(returnSlice, value)
}
}
return returnSlice
}
func hashKeccak256(data string) []byte {
input := []byte(data)
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(input), input)
hash := crypto.Keccak256([]byte(msg))
return hash
}
// ZeroKey removes the key from memory
func ZeroKey(k *ecdsa.PrivateKey) {
b := k.D.Bits()
for i := range b {
b[i] = 0
}
}