forked from hyperledger-archives/fabric
/
node_tlsca.go
265 lines (199 loc) · 6.88 KB
/
node_tlsca.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 crypto
import (
obcca "github.com/openblockchain/obc-peer/obc-ca/protos"
"crypto/ecdsa"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"errors"
"google/protobuf"
"time"
"github.com/golang/protobuf/proto"
"github.com/openblockchain/obc-peer/openchain/crypto/utils"
"github.com/openblockchain/obc-peer/openchain/util"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func (node *nodeImpl) initTLS() error {
node.log.Debug("Initiliazing TLS...")
if node.conf.isTLSEnabled() {
pem, err := node.ks.loadExternalCert(node.conf.getTLSCACertsExternalPath())
if err != nil {
node.log.Error("Failed loading TLSCA certificates chain [%s].", err.Error())
return err
}
node.tlsCertPool = x509.NewCertPool()
ok := node.tlsCertPool.AppendCertsFromPEM(pem)
if !ok {
node.log.Error("Failed appending TLSCA certificates chain.")
return errors.New("Failed appending TLSCA certificates chain.")
}
node.log.Debug("Initiliazing TLS...Done")
} else {
node.log.Debug("Initiliazing TLS...Disabled!!!")
}
return nil
}
func (node *nodeImpl) retrieveTLSCertificate(id, affiliation string) error {
key, tlsCertRaw, err := node.getTLSCertificateFromTLSCA(id, affiliation)
if err != nil {
node.log.Error("Failed getting tls certificate [id=%s] %s", id, err)
return err
}
node.log.Debug("TLS Cert [%s]", utils.EncodeBase64(tlsCertRaw))
node.log.Info("Storing TLS key and certificate for user [%s]...", id)
// Store tls key.
if err := node.ks.storePrivateKeyInClear(node.conf.getTLSKeyFilename(), key); err != nil {
node.log.Error("Failed storing tls key [id=%s]: %s", id, err)
return err
}
// Store tls cert
if err := node.ks.storeCert(node.conf.getTLSCertFilename(), tlsCertRaw); err != nil {
node.log.Error("Failed storing tls certificate [id=%s]: %s", id, err)
return err
}
return nil
}
func (node *nodeImpl) loadTLSCertificate() error {
node.log.Debug("Loading tls certificate...")
cert, _, err := node.ks.loadCertX509AndDer(node.conf.getTLSCertFilename())
if err != nil {
node.log.Error("Failed parsing tls certificate [%s].", err.Error())
return err
}
node.tlsCert = cert
return nil
}
func (node *nodeImpl) loadTLSCACertsChain() error {
if node.conf.isTLSEnabled() {
node.log.Debug("Loading TLSCA certificates chain...")
pem, err := node.ks.loadExternalCert(node.conf.getTLSCACertsExternalPath())
if err != nil {
node.log.Error("Failed loading TLSCA certificates chain [%s].", err.Error())
return err
}
ok := node.tlsCertPool.AppendCertsFromPEM(pem)
if !ok {
node.log.Error("Failed appending TLSCA certificates chain.")
return errors.New("Failed appending TLSCA certificates chain.")
}
node.log.Debug("Loading TLSCA certificates chain...done")
return nil
} else {
node.log.Debug("TLS is disabled!!!")
}
return nil
}
func (node *nodeImpl) getTLSCertificateFromTLSCA(id, affiliation string) (interface{}, []byte, error) {
node.log.Info("getTLSCertificate...")
priv, err := utils.NewECDSAKey()
if err != nil {
node.log.Error("Failed generating key: %s", err)
return nil, nil, err
}
uuid := util.GenerateUUID()
// Prepare the request
pubraw, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
now := time.Now()
timestamp := google_protobuf.Timestamp{int64(now.Second()), int32(now.Nanosecond())}
req := &obcca.TLSCertCreateReq{
×tamp,
&obcca.Identity{Id: id + "-" + uuid},
&obcca.PublicKey{
Type: obcca.CryptoType_ECDSA,
Key: pubraw,
}, nil}
rawreq, _ := proto.Marshal(req)
r, s, err := ecdsa.Sign(rand.Reader, priv, utils.Hash(rawreq))
if err != nil {
panic(err)
}
R, _ := r.MarshalText()
S, _ := s.MarshalText()
req.Sig = &obcca.Signature{obcca.CryptoType_ECDSA, R, S}
pbCert, err := node.callTLSCACreateCertificate(context.Background(), req)
if err != nil {
node.log.Error("Failed requesting tls certificate: %s", err)
return nil, nil, err
}
node.log.Info("Verifing tls certificate...")
tlsCert, err := utils.DERToX509Certificate(pbCert.Cert.Cert)
certPK := tlsCert.PublicKey.(*ecdsa.PublicKey)
utils.VerifySignCapability(priv, certPK)
node.log.Info("Verifing tls certificate...done!")
return priv, pbCert.Cert.Cert, nil
}
func (node *nodeImpl) getClientConn(address string, serverName string) (*grpc.ClientConn, error) {
node.log.Debug("Getting Client Connection to [%s]...", serverName)
var conn *grpc.ClientConn
var err error
if node.conf.isTLSEnabled() {
node.log.Debug("TLS enabled...")
// setup tls options
var opts []grpc.DialOption
config := tls.Config{
InsecureSkipVerify: false,
RootCAs: node.tlsCertPool,
ServerName: serverName,
}
if node.conf.isTLSClientAuthEnabled() {
}
creds := credentials.NewTLS(&config)
opts = append(opts, grpc.WithTransportCredentials(creds))
opts = append(opts, grpc.WithTimeout(time.Second*3))
conn, err = grpc.Dial(address, opts...)
} else {
node.log.Debug("TLS disabled...")
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithTimeout(time.Second*3))
conn, err = grpc.Dial(address, opts...)
}
if err != nil {
node.log.Error("Failed dailing in [%s].", err.Error())
return nil, err
}
node.log.Debug("Getting Client Connection to [%s]...done", serverName)
return conn, nil
}
func (node *nodeImpl) getTLSCAClient() (*grpc.ClientConn, obcca.TLSCAPClient, error) {
node.log.Debug("Getting TLSCA client...")
conn, err := node.getClientConn(node.conf.getTLSCAPAddr(), node.conf.getTLSCAServerName())
if err != nil {
node.log.Error("Failed getting client connection: [%s]", err)
}
client := obcca.NewTLSCAPClient(conn)
node.log.Debug("Getting TLSCA client...done")
return conn, client, nil
}
func (node *nodeImpl) callTLSCACreateCertificate(ctx context.Context, in *obcca.TLSCertCreateReq, opts ...grpc.CallOption) (*obcca.TLSCertCreateResp, error) {
conn, tlscaP, err := node.getTLSCAClient()
if err != nil {
node.log.Error("Failed dialing in: %s", err)
return nil, err
}
defer conn.Close()
resp, err := tlscaP.CreateCertificate(ctx, in, opts...)
if err != nil {
node.log.Error("Failed requesting tls certificate: %s", err)
return nil, err
}
return resp, nil
}