/
csp.go
254 lines (228 loc) · 7.91 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
package util
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/local"
"github.com/pkg/errors"
"github.com/rkcloudchain/cccsp"
"github.com/rkcloudchain/cccsp/importer"
"github.com/rkcloudchain/cccsp/keygen"
"github.com/rkcloudchain/cccsp/provider"
"github.com/rkcloudchain/cccsp/util"
)
// InitCCCSP initializes CCCSP
func InitCCCSP(path string) (cccsp.CCCSP, error) {
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, err
}
}
ks, err := provider.NewFileKeyStore(path)
if err != nil {
return nil, err
}
return provider.New(ks), nil
}
// ImportCCCSPKeyFromPEM attempts to create a private CCCSP key from a pem file keyFile
func ImportCCCSPKeyFromPEM(keyFile string, csp cccsp.CCCSP, temporary bool) (cccsp.Key, error) {
keyBuffer, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
key, err := util.PEMToPrivateKey(keyBuffer)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed parsing private key from %s", keyFile))
}
switch key.(type) {
case *ecdsa.PrivateKey:
priv, err := util.PrivateKeyToDER(key.(*ecdsa.PrivateKey))
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to convert ECDSA private key for '%s'", keyFile))
}
sk, err := csp.KeyImport(priv, importer.ECDSAPRIKEY, 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)
}
}
// GetSignerFromCertFile load skifile and load private key represented by ski and return cccsp signer that conforms to crypto.Signer
func GetSignerFromCertFile(certFile string, csp cccsp.CCCSP) (cccsp.Key, crypto.Signer, *x509.Certificate, error) {
certBytes, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "Could not read certificate file '%s'", certFile)
}
parsedCA, err := helpers.ParseCertificatePEM(certBytes)
if err != nil {
return nil, nil, nil, err
}
key, cspSigner, err := GetSignerFromCert(parsedCA, csp)
return key, cspSigner, parsedCA, err
}
// GetSignerFromCert load private key represented by ski and return cccsp signer that conforms to crypto.Signer
func GetSignerFromCert(cert *x509.Certificate, csp cccsp.CCCSP) (cccsp.Key, crypto.Signer, error) {
if csp == nil {
return nil, nil, errors.New("CSP was not initialized")
}
certPubK, err := csp.KeyImport(cert, importer.X509CERT, true)
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed to import certificate's public key")
}
ski := certPubK.Identifier()
privateKey, err := csp.GetKey(ski)
if err != nil {
return nil, nil, errors.WithMessage(err, "Could not find matching private key for SKI")
}
if !privateKey.Private() {
return nil, nil, errors.Errorf("The private key associated with the certificate with SKI '%s' was not found", hex.EncodeToString(ski))
}
signer, err := provider.NewSigner(csp, privateKey)
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed to create signer from cccsp")
}
return privateKey, signer, nil
}
// LoadX509KeyPair reads and parses a public/private key pair from a pair of files.
func LoadX509KeyPair(certFile, keyFile string, csp cccsp.CCCSP) (*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 CCCSP: %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 CCCSP")
}
}
return cert, nil
}
// CCCSPKeyRequestGenerate generates keys through CCCSP
// somewhat mirroring to cfssl/req.KeyRequest.Generate()
func CCCSPKeyRequestGenerate(req *csr.CertificateRequest, csp cccsp.CCCSP) (cccsp.Key, crypto.Signer, error) {
log.Infof("generating key %+v", req.KeyRequest)
algorithm, err := getCCCSPKeyGenAlgo(req.KeyRequest)
if err != nil {
return nil, nil, err
}
key, err := csp.KeyGenerate(string(algorithm), false)
if err != nil {
return nil, nil, err
}
cspSigner, err := provider.NewSigner(csp, key)
if err != nil {
return nil, nil, errors.WithMessage(err, "Failed initializing CryptoSigner")
}
return key, cspSigner, nil
}
// getCCCSPKeyGenAlgo generates a key as specified in the request.
// This supports ECDSA and RSA
func getCCCSPKeyGenAlgo(kr csr.KeyRequest) (opts string, err error) {
if kr == nil {
return keygen.ECDSA256, nil
}
log.Debugf("generate key from request: algo=%s, size=%d", kr.Algo(), kr.Size())
switch kr.Algo() {
case "rsa":
switch kr.Size() {
case 2048:
return keygen.RSA2048, nil
case 3072:
return keygen.RSA3072, nil
case 4096:
return keygen.RSA4096, nil
default:
return "", errors.Errorf("Invalid RSA key size: %d", kr.Size())
}
case "ecdsa":
switch kr.Size() {
case 256:
return keygen.ECDSA256, nil
case 384:
return keygen.ECDSA384, nil
case 521:
return keygen.ECDSA521, nil
default:
return "", errors.Errorf("Invalid ECDSA key size: %d", kr.Size())
}
default:
return "", errors.Errorf("Invalid algorithm: %s", kr.Algo())
}
}
// CCCSPBackedSigner attempts to create a signer using csp cccsp.CCCSP.
func CCCSPBackedSigner(caFile, keyFile string, policy *config.Signing, csp cccsp.CCCSP) (signer.Signer, error) {
_, cspSigner, parsedCA, err := GetSignerFromCertFile(caFile, csp)
if err != nil {
log.Debugf("No key found in CCCSP keystore, attempting fallback")
var key cccsp.Key
var signer crypto.Signer
key, err = ImportCCCSPKeyFromPEM(keyFile, csp, false)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Could not find the private key in CCCSP keystore nor in keyfile '%s'", keyFile))
}
signer, err = provider.NewSigner(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
}