/
eca.go
223 lines (194 loc) · 5.3 KB
/
eca.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ca
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"database/sql"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"io/ioutil"
"strconv"
"strings"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/flogging"
pb "github.com/hyperledger/fabric/membersrvc/protos"
"github.com/op/go-logging"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
var ecaLogger = logging.MustGetLogger("eca")
var (
// ECertSubjectRole is the ASN1 object identifier of the subject's role.
//
ECertSubjectRole = asn1.ObjectIdentifier{2, 1, 3, 4, 5, 6, 7}
)
// ECA is the enrollment certificate authority.
//
type ECA struct {
*CA
aca *ACA
obcKey []byte
obcPriv, obcPub []byte
gRPCServer *grpc.Server
}
func initializeECATables(db *sql.DB) error {
return initializeCommonTables(db)
}
// NewECA sets up a new ECA.
//
func NewECA(aca *ACA) *ECA {
eca := &ECA{CA: NewCA("eca", initializeECATables), aca: aca}
flogging.LoggingInit("eca")
{
// read or create global symmetric encryption key
var cooked string
var l = logging.MustGetLogger("ECA")
raw, err := ioutil.ReadFile(eca.path + "/obc.aes")
if err != nil {
rand := rand.Reader
key := make([]byte, 32) // AES-256
rand.Read(key)
cooked = base64.StdEncoding.EncodeToString(key)
err = ioutil.WriteFile(eca.path+"/obc.aes", []byte(cooked), 0644)
if err != nil {
l.Panic(err)
}
} else {
cooked = string(raw)
}
eca.obcKey, err = base64.StdEncoding.DecodeString(cooked)
if err != nil {
l.Panic(err)
}
}
{
// read or create global ECDSA key pair for ECIES
var priv *ecdsa.PrivateKey
cooked, err := ioutil.ReadFile(eca.path + "/obc.ecies")
if err == nil {
block, _ := pem.Decode(cooked)
priv, err = x509.ParseECPrivateKey(block.Bytes)
if err != nil {
ecaLogger.Panic(err)
}
} else {
priv, err = ecdsa.GenerateKey(primitives.GetDefaultCurve(), rand.Reader)
if err != nil {
ecaLogger.Panic(err)
}
raw, _ := x509.MarshalECPrivateKey(priv)
cooked = pem.EncodeToMemory(
&pem.Block{
Type: "ECDSA PRIVATE KEY",
Bytes: raw,
})
err := ioutil.WriteFile(eca.path+"/obc.ecies", cooked, 0644)
if err != nil {
ecaLogger.Panic(err)
}
}
eca.obcPriv = cooked
raw, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
eca.obcPub = pem.EncodeToMemory(
&pem.Block{
Type: "ECDSA PUBLIC KEY",
Bytes: raw,
})
}
eca.populateAffiliationGroupsTable()
eca.populateUsersTable()
return eca
}
// populateUsersTable populates the users table.
//
func (eca *ECA) populateUsersTable() {
// populate user table
users := viper.GetStringMapString("eca.users")
for id, flds := range users {
vals := strings.Fields(flds)
role, err := strconv.Atoi(vals[0])
if err != nil {
ecaLogger.Panic(err)
}
var affiliation, memberMetadata, registrar string
if len(vals) >= 3 {
affiliation = vals[2]
if len(vals) >= 4 {
memberMetadata = vals[3]
if len(vals) >= 5 {
registrar = vals[4]
}
}
}
eca.registerUser(id, affiliation, pb.Role(role), nil, eca.aca, registrar, memberMetadata, vals[1])
}
}
// populateAffiliationGroup populates the affiliation groups table.
//
func (eca *ECA) populateAffiliationGroup(name, parent, key string, level int) {
eca.registerAffiliationGroup(name, parent)
newKey := key + "." + name
if level == 0 {
affiliationGroups := viper.GetStringSlice(newKey)
for ci := range affiliationGroups {
eca.registerAffiliationGroup(affiliationGroups[ci], name)
}
} else {
affiliationGroups := viper.GetStringMapString(newKey)
for childName := range affiliationGroups {
eca.populateAffiliationGroup(childName, name, newKey, level-1)
}
}
}
// populateAffiliationGroupsTable populates affiliation groups table.
//
func (eca *ECA) populateAffiliationGroupsTable() {
key := "eca.affiliations"
affiliationGroups := viper.GetStringMapString(key)
for name := range affiliationGroups {
eca.populateAffiliationGroup(name, "", key, 1)
}
}
// Start starts the ECA.
//
func (eca *ECA) Start(srv *grpc.Server) {
ecaLogger.Info("Starting ECA...")
eca.startECAP(srv)
eca.startECAA(srv)
eca.gRPCServer = srv
ecaLogger.Info("ECA started.")
}
// Stop stops the ECA services.
func (eca *ECA) Stop() {
ecaLogger.Info("Stopping ECA services...")
if eca.gRPCServer != nil {
eca.gRPCServer.Stop()
}
err := eca.CA.Stop()
if err != nil {
ecaLogger.Errorf("ECA Error stopping services: %s", err)
} else {
ecaLogger.Info("ECA stopped")
}
}
func (eca *ECA) startECAP(srv *grpc.Server) {
pb.RegisterECAPServer(srv, &ECAP{eca})
ecaLogger.Info("ECA PUBLIC gRPC API server started")
}
func (eca *ECA) startECAA(srv *grpc.Server) {
pb.RegisterECAAServer(srv, &ECAA{eca})
ecaLogger.Info("ECA ADMIN gRPC API server started")
}