-
Notifications
You must be signed in to change notification settings - Fork 711
/
util.go
222 lines (197 loc) · 6.81 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lib
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/cloudflare/cfssl/log"
"github.com/grantae/certinfo"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/lib/caerrors"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
var clientAuthTypes = map[string]tls.ClientAuthType{
"noclientcert": tls.NoClientCert,
"requestclientcert": tls.RequestClientCert,
"requireanyclientcert": tls.RequireAnyClientCert,
"verifyclientcertifgiven": tls.VerifyClientCertIfGiven,
"requireandverifyclientcert": tls.RequireAndVerifyClientCert,
}
// 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) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
if len(certFiles) > 0 {
for _, cert := range certFiles {
log.Debugf("Reading cert file: %s", cert)
pemCerts, err := os.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, caerrors.NewHTTPErr(400, caerrors.ErrInvalidMaxEnroll, "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, caerrors.NewHTTPErr(400, caerrors.ErrInvalidMaxEnroll, "Registration is disabled")
default:
switch userMaxEnrollments {
case -1:
// User requested infinite enrollments is not allowed
return 0, caerrors.NewHTTPErr(400, caerrors.ErrInvalidMaxEnroll, "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, caerrors.NewHTTPErr(400, caerrors.ErrInvalidMaxEnroll, "Requested enrollments (%d) exceeds maximum allowable enrollments (%d)", userMaxEnrollments, caMaxEnrollments)
}
// otherwise, use the requested maximum
return userMaxEnrollments, nil
}
}
}
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
}
// 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 = os.WriteFile(filePath, cert, 0644)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("Failed to store certificate at: %s", storePath))
}
return nil
}