forked from hyperledger-archives/fabric
/
node_tca.go
executable file
·124 lines (93 loc) · 3.39 KB
/
node_tca.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
/*
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"
"errors"
"github.com/openblockchain/obc-peer/openchain/crypto/utils"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
func (node *nodeImpl) retrieveTCACertsChain(userID string) error {
// Retrieve TCA certificate and verify it
tcaCertRaw, err := node.getTCACertificate()
if err != nil {
node.log.Error("Failed getting TCA certificate [%s].", err.Error())
return err
}
node.log.Debug("TCA certificate [%s]", utils.EncodeBase64(tcaCertRaw))
// TODO: Test TCA cert againt root CA
_, err = utils.DERToX509Certificate(tcaCertRaw)
if err != nil {
node.log.Error("Failed parsing TCA certificate [%s].", err.Error())
return err
}
// Store TCA cert
node.log.Debug("Storing TCA certificate for [%s]...", userID)
if err := node.ks.storeCert(node.conf.getTCACertsChainFilename(), tcaCertRaw); err != nil {
node.log.Error("Failed storing tca certificate [%s].", err.Error())
return err
}
return nil
}
func (node *nodeImpl) loadTCACertsChain() error {
// Load TCA certs chain
node.log.Debug("Loading TCA certificates chain...")
cert, err := node.ks.loadCert(node.conf.getTCACertsChainFilename())
if err != nil {
node.log.Error("Failed loading TCA certificates chain [%s].", err.Error())
return err
}
// Prepare ecaCertPool
ok := node.tcaCertPool.AppendCertsFromPEM(cert)
if !ok {
node.log.Error("Failed appending TCA certificates chain.")
return errors.New("Failed appending TCA certificates chain.")
}
return nil
}
func (node *nodeImpl) getTCAClient() (*grpc.ClientConn, obcca.TCAPClient, error) {
node.log.Debug("Getting TCA client...")
conn, err := node.getClientConn(node.conf.getTCAPAddr(), node.conf.getTCAServerName())
if err != nil {
node.log.Error("Failed getting client connection: [%s]", err)
}
client := obcca.NewTCAPClient(conn)
node.log.Debug("Getting TCA client...done")
return conn, client, nil
}
func (node *nodeImpl) callTCAReadCACertificate(ctx context.Context, opts ...grpc.CallOption) (*obcca.Cert, error) {
// Get a TCA Client
sock, tcaP, err := node.getTCAClient()
defer sock.Close()
// Issue the request
cert, err := tcaP.ReadCACertificate(ctx, &obcca.Empty{}, opts...)
if err != nil {
node.log.Error("Failed requesting tca read certificate [%s].", err.Error())
return nil, err
}
return cert, nil
}
func (node *nodeImpl) getTCACertificate() ([]byte, error) {
response, err := node.callTCAReadCACertificate(context.Background())
if err != nil {
node.log.Error("Failed requesting TCA certificate [%s].", err.Error())
return nil, err
}
// TODO: check response.Cert against rootCA
return response.Cert, nil
}