This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
loader.go
142 lines (112 loc) · 2.81 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
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
package identity
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"path"
"github.com/pkg/errors"
"github.com/atomyze-ru/hlf-sdk-go/api"
)
var (
ErrNoPEMContent = errors.New("no pem content")
ErrKeyNotFound = errors.New("key not found")
)
func FirstFromPath(mspID string, certDir, keyDir string) (*identity, error) {
certFile, err := readFirstFile(certDir)
if err != nil {
return nil, err
}
cert, key, err := KeyPairForCert(certFile, keyDir)
if err != nil {
return nil, err
}
return New(mspID, cert, key), nil
}
func ListFromPath(mspID string, certDir, keyDir string) ([]api.Identity, error) {
var identities []api.Identity
certFiles, err := readFiles(certDir)
if err != nil {
return nil, err
}
for _, certRaw := range certFiles {
cert, key, err := KeyPairForCert(certRaw, keyDir)
if err != nil {
return nil, err
}
identities = append(identities, New(mspID, cert, key))
}
return identities, nil
}
func CertificatesFromPath(certDir string) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
certFiles, err := readFiles(certDir)
if err != nil {
return nil, err
}
for _, certRaw := range certFiles {
cert, err := Certificate(certRaw)
if err != nil {
return nil, err
}
certs = append(certs, cert)
}
return certs, nil
}
func Certificate(certRaw []byte) (*x509.Certificate, error) {
certPEM, _ := pem.Decode(certRaw)
if certPEM == nil {
return nil, ErrNoPEMContent
}
cert, err := x509.ParseCertificate(certPEM.Bytes)
if err != nil {
return nil, fmt.Errorf(`parse certificate: %w`, err)
}
return cert, nil
}
// Key parses raw key bytes
func Key(keyRaw []byte) (interface{}, error) {
keyPEM, _ := pem.Decode(keyRaw)
if keyPEM == nil {
return nil, ErrNoPEMContent
}
key, err := x509.ParsePKCS8PrivateKey(keyPEM.Bytes)
if err != nil {
return nil, fmt.Errorf(`parse key: %w`, err)
}
return key, nil
}
func KeyPairForCert(certRaw []byte, keyDir string) (*x509.Certificate, interface{}, error) {
key, err := KeyForCert(certRaw, keyDir)
if err != nil {
return nil, nil, err
}
cert, err := Certificate(certRaw)
if err != nil {
return nil, nil, err
}
return cert, key, nil
}
// KeyForCert returns private key for certificate from keyDir
func KeyForCert(certRaw []byte, keyDir string) (interface{}, error) {
files, err := ioutil.ReadDir(keyDir)
if err != nil {
return nil, fmt.Errorf(`read key dir: %w`, err)
}
for _, f := range files {
if f.IsDir() {
continue
}
keyRaw, err := ioutil.ReadFile(path.Join(keyDir, f.Name()))
if err != nil {
return nil, fmt.Errorf(`read key file: %w`, err)
}
// match public/private keys
if _, err = tls.X509KeyPair(certRaw, keyRaw); err != nil {
continue
}
return Key(keyRaw)
}
return nil, fmt.Errorf(`key dir = %s, files: %d: %w`, keyDir, len(files), ErrKeyNotFound)
}