/
util.go
314 lines (274 loc) · 9.5 KB
/
util.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lib
import (
tls "github.com/tjfoc/gmtls"
"crypto/x509"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/cloudflare/cfssl/log"
"github.com/grantae/certinfo"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/lib/spi"
"github.com/hyperledger/fabric-ca/util"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/tjfoc/gmsm/sm2"
)
var clientAuthTypes = map[string]tls.ClientAuthType{
"noclientcert": tls.NoClientCert,
"requestclientcert": tls.RequestClientCert,
"requireanyclientcert": tls.RequireAnyClientCert,
"verifyclientcertifgiven": tls.VerifyClientCertIfGiven,
"requireandverifyclientcert": tls.RequireAndVerifyClientCert,
}
// GetCertID returns both the serial number and AKI (Authority Key ID) for the certificate
func GetCertID(bytes []byte) (string, string, error) {
cert, err := BytesToX509Cert(bytes)
if err != nil {
return "", "", err
}
serial := util.GetSerialAsHex(cert.SerialNumber)
aki := hex.EncodeToString(cert.AuthorityKeyId)
return serial, aki, nil
}
// BytesToX509Cert converts bytes (PEM or DER) to an X509 certificate
func BytesToX509Cert(bytes []byte) (*x509.Certificate, error) {
dcert, _ := pem.Decode(bytes)
if dcert != nil {
bytes = dcert.Bytes
}
cert, err := x509.ParseCertificate(bytes)
if err != nil {
return nil, errors.Wrap(err, "Buffer was neither PEM nor DER encoding")
}
return cert, err
}
// LoadPEMCertPool loads a pool of PEM certificates from list of files
func LoadPEMCertPool(certFiles []string) (*sm2.CertPool, error) {
certPool := sm2.NewCertPool()
if len(certFiles) > 0 {
for _, cert := range certFiles {
log.Debugf("Reading cert file: %s", cert)
pemCerts, err := ioutil.ReadFile(cert)
if err != nil {
return nil, err
}
log.Debugf("Appending cert %s to pool", cert)
if !certPool.AppendCertsFromPEM(pemCerts) {
return nil, errors.New("Failed to load cert pool")
}
}
}
return certPool, nil
}
// UnmarshalConfig unmarshals a configuration file
func UnmarshalConfig(config interface{}, vp *viper.Viper, configFile string,
server bool) error {
vp.SetConfigFile(configFile)
err := vp.ReadInConfig()
if err != nil {
return errors.Wrapf(err, "Failed to read config file '%s'", configFile)
}
err = vp.Unmarshal(config)
if err != nil {
return errors.Wrapf(err, "Incorrect format in file '%s'", configFile)
}
if server {
serverCfg := config.(*ServerConfig)
err = vp.Unmarshal(&serverCfg.CAcfg)
if err != nil {
return errors.Wrapf(err, "Incorrect format in file '%s'", configFile)
}
}
return nil
}
func getMaxEnrollments(userMaxEnrollments int, caMaxEnrollments int) (int, error) {
log.Debugf("Max enrollment value verification - User specified max enrollment: %d, CA max enrollment: %d", userMaxEnrollments, caMaxEnrollments)
if userMaxEnrollments < -1 {
return 0, errors.Errorf("Max enrollment in registration request may not be less than -1, but was %d", userMaxEnrollments)
}
switch caMaxEnrollments {
case -1:
if userMaxEnrollments == 0 {
// The user is requesting the matching limit of the CA, so gets infinite
return caMaxEnrollments, nil
}
// There is no CA max enrollment limit, so simply use the user requested value
return userMaxEnrollments, nil
case 0:
// The CA max enrollment is 0, so registration is disabled.
return 0, errors.New("Registration is disabled")
default:
switch userMaxEnrollments {
case -1:
// User requested infinite enrollments is not allowed
return 0, errors.New("Registration for infinite enrollments is not allowed")
case 0:
// User is requesting the current CA maximum
return caMaxEnrollments, nil
default:
// User is requesting a specific positive value; make sure it doesn't exceed the CA maximum.
if userMaxEnrollments > caMaxEnrollments {
return 0, errors.Errorf("Requested enrollments (%d) exceeds maximum allowable enrollments (%d)",
userMaxEnrollments, caMaxEnrollments)
}
// otherwise, use the requested maximum
return userMaxEnrollments, nil
}
}
}
// GetUserAffiliation return a joined version version of the affiliation path with '.' as the seperator
func GetUserAffiliation(user spi.User) string {
return strings.Join(user.GetAffiliationPath(), ".")
}
func addQueryParm(req *http.Request, name, value string) {
url := req.URL.Query()
url.Add(name, value)
req.URL.RawQuery = url.Encode()
}
// IdentityDecoder decodes streams of data coming from the server into an Identity object
func IdentityDecoder(decoder *json.Decoder) error {
var id api.IdentityInfo
err := decoder.Decode(&id)
if err != nil {
return err
}
fmt.Printf("Name: %s, Type: %s, Affiliation: %s, Max Enrollments: %d, Attributes: %+v\n", id.ID, id.Type, id.Affiliation, id.MaxEnrollments, id.Attributes)
return nil
}
// AffiliationDecoder decodes streams of data coming from the server into an Affiliation object
func AffiliationDecoder(decoder *json.Decoder) error {
var aff api.AffiliationInfo
err := decoder.Decode(&aff)
if err != nil {
return err
}
fmt.Printf("%s\n", aff.Name)
return nil
}
// CertificateDecoder is needed to keep track of state, to see how many certificates
// have been returned for each enrollment ID.
type CertificateDecoder struct {
certIDCount map[string]int
storePath string
}
// NewCertificateDecoder returns decoder for certificates
func NewCertificateDecoder(storePath string) *CertificateDecoder {
cd := &CertificateDecoder{}
cd.certIDCount = make(map[string]int)
cd.storePath = storePath
return cd
}
// CertificateDecoder decodes streams of data coming from the server
func (cd *CertificateDecoder) CertificateDecoder(decoder *json.Decoder) error {
var cert certPEM
err := decoder.Decode(&cert)
if err != nil {
return err
}
block, rest := pem.Decode([]byte(cert.PEM))
if block == nil || len(rest) > 0 {
return errors.New("Certificate decoding error")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
enrollmentID := certificate.Subject.CommonName
if cd.storePath != "" {
err = cd.StoreCert(enrollmentID, cd.storePath, []byte(cert.PEM))
if err != nil {
return err
}
}
result, err := certinfo.CertificateText(certificate)
if err != nil {
return err
}
fmt.Printf(result)
return nil
}
// StoreCert stores the certificate on the file system
func (cd *CertificateDecoder) StoreCert(enrollmentID, storePath string, cert []byte) error {
cd.certIDCount[enrollmentID] = cd.certIDCount[enrollmentID] + 1
err := os.MkdirAll(storePath, os.ModePerm)
if err != nil {
return err
}
var filePath string
singleCertName := fmt.Sprintf("%s.pem", enrollmentID)
switch cd.certIDCount[enrollmentID] {
case 1: // Only one certificate returned, don't need to append number to certificate file name
filePath = filepath.Join(storePath, singleCertName)
case 2: // Two certificates returned, rename the old certificate to have number at the end
err := os.Rename(filepath.Join(storePath, singleCertName), filepath.Join(storePath, fmt.Sprintf("%s-1.pem", enrollmentID)))
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("Failed to rename certificate: %s", singleCertName))
}
filePath = filepath.Join(storePath, fmt.Sprintf("%s-%d.pem", enrollmentID, cd.certIDCount[enrollmentID]))
default:
filePath = filepath.Join(storePath, fmt.Sprintf("%s-%d.pem", enrollmentID, cd.certIDCount[enrollmentID]))
}
err = ioutil.WriteFile(filePath, cert, 0644)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("Failed to store certificate at: %s", storePath))
}
return nil
}
// SM2证书请求 转换 X509 证书请求
func ParseSm2CertificateRequest2X509(sm2req *sm2.CertificateRequest) *x509.CertificateRequest {
x509req := &x509.CertificateRequest{
Raw: sm2req.Raw, // Complete ASN.1 DER content (CSR, signature algorithm and signature).
RawTBSCertificateRequest: sm2req.RawTBSCertificateRequest, // Certificate request info part of raw ASN.1 DER content.
RawSubjectPublicKeyInfo: sm2req.RawSubjectPublicKeyInfo, // DER encoded SubjectPublicKeyInfo.
RawSubject: sm2req.RawSubject, // DER encoded Subject.
Version: sm2req.Version,
Signature: sm2req.Signature,
SignatureAlgorithm: x509.SignatureAlgorithm(sm2req.SignatureAlgorithm),
PublicKeyAlgorithm: x509.PublicKeyAlgorithm(sm2req.PublicKeyAlgorithm),
PublicKey: sm2req.PublicKey,
Subject: sm2req.Subject,
// Attributes is the dried husk of a bug and shouldn't be used.
Attributes: sm2req.Attributes,
// Extensions contains raw X.509 extensions. When parsing CSRs, this
// can be used to extract extensions that are not parsed by this
// package.
Extensions: sm2req.Extensions,
// ExtraExtensions contains extensions to be copied, raw, into any
// marshaled CSR. Values override any extensions that would otherwise
// be produced based on the other fields but are overridden by any
// extensions specified in Attributes.
//
// The ExtraExtensions field is not populated when parsing CSRs, see
// Extensions.
ExtraExtensions: sm2req.ExtraExtensions,
// Subject Alternate Name values.
DNSNames: sm2req.DNSNames,
EmailAddresses: sm2req.EmailAddresses,
IPAddresses: sm2req.IPAddresses,
}
return x509req
}
var providerName string
func IsGMConfig() bool {
if providerName == "" {
return false
}
if strings.ToUpper(providerName) == "GM" {
return true
}
return false
}
func SetProviderName(name string) {
providerName = name
}