-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
86 lines (73 loc) · 2.13 KB
/
loader.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
/*
* Copyright 2019 Insolar Technologies
*
* 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 privatekey
import (
"crypto"
"crypto/x509"
"encoding/json"
"encoding/pem"
"io/ioutil"
"path/filepath"
"github.com/pkg/errors"
)
type keyLoader struct {
parseFunc func(key []byte) (crypto.PrivateKey, error)
}
func NewLoader() Loader {
return &keyLoader{
parseFunc: pemParse,
}
}
func (p *keyLoader) Load(file string) (crypto.PrivateKey, error) {
key, err := readJSON(file)
if err != nil {
return nil, errors.Wrap(err, "[ Load ] Could't read private key")
}
signer, err := p.parseFunc(key)
if err != nil {
return nil, errors.Wrap(err, "[ Load ] Could't parse private key")
}
return signer, nil
}
// TODO: deprecated, use PEM format
func readJSON(path string) ([]byte, error) {
data, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return nil, errors.Wrap(err, "[ read ] couldn't read keys from: "+path)
}
var keys map[string]string
err = json.Unmarshal(data, &keys)
if err != nil {
return nil, errors.Wrap(err, "[ read ] failed to parse json.")
}
key, ok := keys["private_key"]
if !ok {
return nil, errors.Errorf("[ read ] couldn't read keys from: %s", path)
}
return []byte(key), nil
}
func pemParse(key []byte) (crypto.PrivateKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return nil, errors.Errorf("[ Parse ] Problems with decoding. Key - %v", key)
}
x509Encoded := block.Bytes
privateKey, err := x509.ParseECPrivateKey(x509Encoded)
if err != nil {
return nil, errors.Errorf("[ Parse ] Problems with parsing. Key - %v", key)
}
return privateKey, nil
}