-
Notifications
You must be signed in to change notification settings - Fork 90
/
csp.go
300 lines (274 loc) · 10.2 KB
/
csp.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package util
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"strings"
_ "time" // for ocspSignerFromConfig
_ "github.com/cloudflare/cfssl/cli" // for ocspSignerFromConfig
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/log"
_ "github.com/cloudflare/cfssl/ocsp" // for ocspSignerFromConfig
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/local"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/factory"
cspsigner "github.com/hyperledger/fabric/bccsp/signer"
"github.com/hyperledger/fabric/bccsp/utils"
"github.com/pkg/errors"
)
// GetDefaultBCCSP returns the default BCCSP
func GetDefaultBCCSP() bccsp.BCCSP {
return factory.GetDefault()
}
// InitBCCSP initializes BCCSP
func InitBCCSP(optsPtr **factory.FactoryOpts, mspDir, homeDir string) (bccsp.BCCSP, error) {
err := ConfigureBCCSP(optsPtr, mspDir, homeDir)
if err != nil {
return nil, err
}
csp, err := GetBCCSP(*optsPtr, homeDir)
if err != nil {
return nil, err
}
return csp, nil
}
// GetBCCSP returns BCCSP
func GetBCCSP(opts *factory.FactoryOpts, homeDir string) (bccsp.BCCSP, error) {
// Get BCCSP from the opts
csp, err := factory.GetBCCSPFromOpts(opts)
if err != nil {
return nil, errors.WithMessage(err, "Failed to get BCCSP with opts")
}
return csp, nil
}
// makeFileNamesAbsolute makes all relative file names associated with CSP absolute,
// relative to 'homeDir'.
func makeFileNamesAbsolute(opts *factory.FactoryOpts, homeDir string) error {
var err error
if opts != nil && opts.SwOpts != nil && opts.SwOpts.FileKeystore != nil {
fks := opts.SwOpts.FileKeystore
fks.KeyStorePath, err = MakeFileAbs(fks.KeyStorePath, homeDir)
}
return err
}
// BccspBackedSigner attempts to create a signer using csp bccsp.BCCSP. This csp could be SW (golang crypto)
// PKCS11 or whatever BCCSP-conformant library is configured
func BccspBackedSigner(caFile, keyFile string, policy *config.Signing, csp bccsp.BCCSP) (signer.Signer, error) {
_, cspSigner, parsedCa, err := GetSignerFromCertFile(caFile, csp)
if err != nil {
// Fallback: attempt to read out of keyFile and import
log.Debugf("No key found in BCCSP keystore, attempting fallback")
var key bccsp.Key
var signer crypto.Signer
key, err = ImportBCCSPKeyFromPEM(keyFile, csp, false)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Could not find the private key in BCCSP keystore nor in keyfile '%s'", keyFile))
}
signer, err = cspsigner.New(csp, key)
if err != nil {
return nil, errors.WithMessage(err, "Failed initializing CryptoSigner")
}
cspSigner = signer
}
signer, err := local.NewSigner(cspSigner, parsedCa, signer.DefaultSigAlgo(cspSigner), policy)
if err != nil {
return nil, errors.Wrap(err, "Failed to create new signer")
}
return signer, nil
}
// getBCCSPKeyOpts generates a key as specified in the request.
// This supports ECDSA and RSA.
func getBCCSPKeyOpts(kr *csr.KeyRequest, ephemeral bool) (opts bccsp.KeyGenOpts, err error) {
if kr == nil {
return &bccsp.ECDSAKeyGenOpts{Temporary: ephemeral}, nil
}
log.Debugf("generate key from request: algo=%s, size=%d", kr.Algo(), kr.Size())
switch kr.Algo() {
// commented since hyperledger fabric 2.0 does not support RSA
//case "rsa":
// switch kr.Size() {
// case 2048:
// return &bccsp.RSA2048KeyGenOpts{Temporary: ephemeral}, nil
// case 3072:
// return &bccsp.RSA3072KeyGenOpts{Temporary: ephemeral}, nil
// case 4096:
// return &bccsp.RSA4096KeyGenOpts{Temporary: ephemeral}, nil
// default:
// // Need to add a way to specify arbitrary RSA key size to bccsp
// return nil, errors.Errorf("Invalid RSA key size: %d", kr.Size())
// }
case "ecdsa":
switch kr.Size() {
case 256:
return &bccsp.ECDSAP256KeyGenOpts{Temporary: ephemeral}, nil
case 384:
return &bccsp.ECDSAP384KeyGenOpts{Temporary: ephemeral}, nil
case 521:
// Need to add curve P521 to bccsp
// return &bccsp.ECDSAP512KeyGenOpts{Temporary: false}, nil
return nil, errors.New("Unsupported ECDSA key size: 521")
default:
return nil, errors.Errorf("Invalid ECDSA key size: %d", kr.Size())
}
default:
return nil, errors.Errorf("Invalid algorithm: %s", kr.Algo())
}
}
// GetSignerFromCert load private key represented by ski and return bccsp signer that conforms to crypto.Signer
func GetSignerFromCert(cert *x509.Certificate, csp bccsp.BCCSP) (bccsp.Key, crypto.Signer, error) {
if csp == nil {
return nil, nil, errors.New("CSP was not initialized")
}
// get the public key in the right format
certPubK, err := csp.KeyImport(cert, &bccsp.X509PublicKeyImportOpts{Temporary: true})
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed to import certificate's public key")
}
// Get the key given the SKI value
ski := certPubK.SKI()
privateKey, err := csp.GetKey(ski)
if err != nil {
return nil, nil, errors.WithMessage(err, "Could not find matching private key for SKI")
}
// BCCSP returns a public key if the private key for the SKI wasn't found, so
// we need to return an error in that case.
if !privateKey.Private() {
return nil, nil, errors.Errorf("The private key associated with the certificate with SKI '%s' was not found", hex.EncodeToString(ski))
}
// Construct and initialize the signer
signer, err := cspsigner.New(csp, privateKey)
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed to load ski from bccsp")
}
return privateKey, signer, nil
}
// GetSignerFromCertFile load skiFile and load private key represented by ski and return bccsp signer that conforms to crypto.Signer
func GetSignerFromCertFile(certFile string, csp bccsp.BCCSP) (bccsp.Key, crypto.Signer, *x509.Certificate, error) {
// Load cert file
certBytes, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "Could not read certFile '%s'", certFile)
}
// Parse certificate
parsedCa, err := helpers.ParseCertificatePEM(certBytes)
if err != nil {
return nil, nil, nil, err
}
// Get the signer from the cert
key, cspSigner, err := GetSignerFromCert(parsedCa, csp)
return key, cspSigner, parsedCa, err
}
// BCCSPKeyRequestGenerate generates keys through BCCSP
// somewhat mirroring to cfssl/req.KeyRequest.Generate()
func BCCSPKeyRequestGenerate(req *csr.CertificateRequest, myCSP bccsp.BCCSP) (bccsp.Key, crypto.Signer, error) {
log.Infof("generating key: %+v", req.KeyRequest)
keyOpts, err := getBCCSPKeyOpts(req.KeyRequest, false)
if err != nil {
return nil, nil, err
}
key, err := myCSP.KeyGen(keyOpts)
if err != nil {
return nil, nil, err
}
cspSigner, err := cspsigner.New(myCSP, key)
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed initializing CryptoSigner")
}
return key, cspSigner, nil
}
// ImportBCCSPKeyFromPEM attempts to create a private BCCSP key from a pem file keyFile
func ImportBCCSPKeyFromPEM(keyFile string, myCSP bccsp.BCCSP, temporary bool) (bccsp.Key, error) {
keyBuff, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
key, err := utils.PEMtoPrivateKey(keyBuff, nil)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed parsing private key from %s", keyFile))
}
switch key := key.(type) {
case *ecdsa.PrivateKey:
priv, err := utils.PrivateKeyToDER(key)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to convert ECDSA private key for '%s'", keyFile))
}
sk, err := myCSP.KeyImport(priv, &bccsp.ECDSAPrivateKeyImportOpts{Temporary: temporary})
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to import ECDSA private key for '%s'", keyFile))
}
return sk, nil
case *rsa.PrivateKey:
return nil, errors.Errorf("Failed to import RSA key from %s; RSA private key import is not supported", keyFile)
default:
return nil, errors.Errorf("Failed to import key from %s: invalid secret key type", keyFile)
}
}
// LoadX509KeyPair reads and parses a public/private key pair from a pair
// of files. The files must contain PEM encoded data. The certificate file
// may contain intermediate certificates following the leaf certificate to
// form a certificate chain. On successful return, Certificate.Leaf will
// be nil because the parsed form of the certificate is not retained.
//
// This function originated from crypto/tls/tls.go and was adapted to use a
// BCCSP Signer
func LoadX509KeyPair(certFile, keyFile string, csp bccsp.BCCSP) (*tls.Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, err
}
cert := &tls.Certificate{}
var skippedBlockTypes []string
for {
var certDERBlock *pem.Block
certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
if certDERBlock == nil {
break
}
if certDERBlock.Type == "CERTIFICATE" {
cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
} else {
skippedBlockTypes = append(skippedBlockTypes, certDERBlock.Type)
}
}
if len(cert.Certificate) == 0 {
if len(skippedBlockTypes) == 0 {
return nil, errors.Errorf("Failed to find PEM block in file %s", certFile)
}
if len(skippedBlockTypes) == 1 && strings.HasSuffix(skippedBlockTypes[0], "PRIVATE KEY") {
return nil, errors.Errorf("Failed to find certificate PEM data in file %s, but did find a private key; PEM inputs may have been switched", certFile)
}
return nil, errors.Errorf("Failed to find \"CERTIFICATE\" PEM block in file %s after skipping PEM blocks of the following types: %v", certFile, skippedBlockTypes)
}
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, err
}
_, cert.PrivateKey, err = GetSignerFromCert(x509Cert, csp)
if err != nil {
if keyFile != "" {
log.Debugf("Could not load TLS certificate with BCCSP: %s", err)
log.Debugf("Attempting fallback with certfile %s and keyfile %s", certFile, keyFile)
fallbackCerts, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Wrapf(err, "Could not get the private key %s that matches %s", keyFile, certFile)
}
cert = &fallbackCerts
} else {
return nil, errors.WithMessage(err, "Could not load TLS certificate with BCCSP")
}
}
return cert, nil
}