-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
115 lines (101 loc) · 3.55 KB
/
keys.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// 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 platformpolicy
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/platformpolicy/internal/sign"
"github.com/pkg/errors"
)
type keyProcessor struct {
curve elliptic.Curve
}
func NewKeyProcessor() insolar.KeyProcessor {
return &keyProcessor{
curve: elliptic.P256(),
}
}
func (kp *keyProcessor) GeneratePrivateKey() (crypto.PrivateKey, error) {
return ecdsa.GenerateKey(kp.curve, rand.Reader)
}
func (*keyProcessor) ExtractPublicKey(privateKey crypto.PrivateKey) crypto.PublicKey {
ecdsaPrivateKey := sign.MustConvertPrivateKeyToEcdsa(privateKey)
publicKey := ecdsaPrivateKey.PublicKey
return &publicKey
}
func (*keyProcessor) ImportPublicKeyPEM(pemEncoded []byte) (crypto.PublicKey, error) {
blockPub, _ := pem.Decode(pemEncoded)
if blockPub == nil {
return nil, fmt.Errorf("[ ImportPublicKey ] Problems with decoding. Key - %v", pemEncoded)
}
x509EncodedPub := blockPub.Bytes
publicKey, err := x509.ParsePKIXPublicKey(x509EncodedPub)
if err != nil {
return nil, fmt.Errorf("[ ImportPublicKey ] Problems with parsing. Key - %v", pemEncoded)
}
return publicKey, nil
}
func (*keyProcessor) ImportPrivateKeyPEM(pemEncoded []byte) (crypto.PrivateKey, error) {
block, _ := pem.Decode(pemEncoded)
if block == nil {
return nil, fmt.Errorf("[ ImportPrivateKey ] Problems with decoding. Key - %v", pemEncoded)
}
x509Encoded := block.Bytes
privateKey, err := x509.ParseECPrivateKey(x509Encoded)
if err != nil {
return nil, fmt.Errorf("[ ImportPrivateKey ] Problems with parsing. Key - %v", pemEncoded)
}
return privateKey, nil
}
func (*keyProcessor) ExportPublicKeyPEM(publicKey crypto.PublicKey) ([]byte, error) {
ecdsaPublicKey := sign.MustConvertPublicKeyToEcdsa(publicKey)
x509EncodedPub, err := x509.MarshalPKIXPublicKey(ecdsaPublicKey)
if err != nil {
return nil, errors.Wrap(err, "[ ExportPublicKey ]")
}
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: x509EncodedPub})
return pemEncoded, nil
}
func (*keyProcessor) ExportPrivateKeyPEM(privateKey crypto.PrivateKey) ([]byte, error) {
ecdsaPrivateKey := sign.MustConvertPrivateKeyToEcdsa(privateKey)
x509Encoded, err := x509.MarshalECPrivateKey(ecdsaPrivateKey)
if err != nil {
return nil, errors.Wrap(err, "[ ExportPrivateKey ]")
}
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
return pemEncoded, nil
}
func (kp *keyProcessor) ExportPublicKeyBinary(publicKey crypto.PublicKey) ([]byte, error) {
ecdsaPublicKey := sign.MustConvertPublicKeyToEcdsa(publicKey)
return sign.SerializeTwoBigInt(ecdsaPublicKey.X, ecdsaPublicKey.Y), nil
}
func (kp *keyProcessor) ImportPublicKeyBinary(data []byte) (crypto.PublicKey, error) {
x, y, err := sign.DeserializeTwoBigInt(data)
if err != nil {
return nil, errors.Wrap(err, "[ ImportPublicKeyBinary ]")
}
return &ecdsa.PublicKey{
Curve: kp.curve,
X: x,
Y: y,
}, nil
}