Hyperledger Fabric中使用第三方CA的教程及源码

Hyperledger Fabric(HF)为终端用户使用自有CA提供了fabric-ca 工具。然而在生产环境中应当尽可能保证根CA的安全性,例如让 根CA离线,而将Hyperledger Fabric环境中的证书签发代理给中间 CA。在本文中,我们将介绍如何使用第三方CA作为根CA,使用fabric-ca 作为中间CA,以及如何在CA信任链中整合第三方CA与fabric-ca。

1、准备工作

为了演示外部CA和中间CA的使用,我们将部署一个根CA,它只负责 签发中间CA的证书。这个中间CA采用Fabric CA,负责签发用户和节点 证书。出于简化考虑,在这个教程中,我们将使用OpenSSL。

Fabric第三方CA

在开始之前,请参考相应文档先完成以下前置环节的部署和知识准备:

  • 安装docker、docker-compose、git和curl
  • Hyperledger Fabric和bash/sheel脚本基础知识
  • PKI基础知识

克隆本教程演示代码仓库,其中包含了所有用到的脚本:

1
2
git clone https://github.com/aldredb/external-ca
cd external-ca

下载Hyperledger Fabric预编译程序并删除不需要的文件。我们只用到 cryptogen, fabric-ca-client和configtx。

1
2
curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.1 -d -s
rm -f config/configtx.yaml config/core.yaml config/orderer.yaml

使用cryptogen为排序节点机构生成证书和密钥:

1
2
3
export PATH=$PWD/bin:$PATH
export FABRIC_CFG_PATH=${PWD}
cryptogen generate --config=./crypto-config.yaml

创建用于保存对等节点机构org1.example.com的证书和密钥的目录结构。 如下所示,该机构包含一个节点peer0.org1.example.com和两个用户: adminAdmin@org1.example.com。中间CA的证书和密钥将保存在ca 文件夹里。

1
2
3
4
5
ORG_DIR=$PWD/crypto-config/peerOrganizations/org1.example.com
PEER_DIR=$ORG_DIR/peers/peer0.org1.example.com
REGISTRAR_DIR=$ORG_DIR/users/admin
ADMIN_DIR=$ORG_DIR/users/Admin@org1.example.com
mkdir -p $ORG_DIR/ca $ORG_DIR/msp $PEER_DIR $REGISTRAR_DIR $ADMIN_DIR

2、创建中间CA

生成私钥和证书签名请求/CSR。注意机构的值与根CA相同。

1
2
3
4
5
openssl ecparam -name prime256v1 -genkey -noout -out \
$ORG_DIR/ca/ica.org1.example.com.key.pem
openssl req -new -sha256 -key $ORG_DIR/ca/ica.org1.example.com.key.pem \
-out $ORG_DIR/ca/ica.org1.example.com.csr \
-subj "/C=SG/ST=Singapore/L=Singapore/O=org1.example.com/OU=/CN=ica.org1.example.com"

根CA负责签名中间CA的CSR并签发证书,中间CA证书的有效期是根CA 证书的一半。注意我们使用v3_intermediate_ca扩展。

1
2
3
openssl ca -batch -config openssl_root.cnf -extensions v3_intermediate_ca \
-days 1825 -notext -md sha256 -in $ORG_DIR/ca/ica.org1.example.com.csr \
-out $ORG_DIR/ca/ica.org1.example.com.crt.pem

现在让我们看一下得到的证书:

1
openssl x509 -in $ORG_DIR/ca/ica.org1.example.com.crt.pem -text -noout

如下所示,可以看到证书的签发机构是: rca.org1.example.com:

1
2
3
4
5
Issuer: C=SG, ST=Singapore, L=Singapore, O=org1.example.com, CN=rca.org1.example.com
Validity
Not Before: May 3 10:16:44 2019 GMT
Not After : Apr 30 10:16:44 2029 GMT
Subject: C=SG, ST=Singapore, O=org1.example.com, CN=ica.org1.example.com

一旦我们签发了中间CA的证书,就不再需要根CA了,除非需要创建另一个中间CA或者 回收中间CA的证书。

现在创建CA链文件,其中包含了中间CA和genCA的证书:

1
2
3
cat $ORG_DIR/ca/ica.org1.example.com.crt.pem \
$PWD/rca/certs/rca.org1.example.com.crt.pem > \
$ORG_DIR/ca/chain.org1.example.com.crt.pem

最后启动中间CA,中间CA的配置文件指向我们前面创建的证书、密钥和CA链。 你可以参考ca-config/fabric-ca-server-config.yaml文件:

1
docker-compose up -d ica.org1.example.com

3、签发peer节点证书和用户证书

中间CA就绪后,我们现在可以签发用户证书和peer节点证书了。

首先加入(enroll)admin用户,该用户有权限注册(register)其他用户:

1
2
3
export FABRIC_CA_CLIENT_HOME=$REGISTRAR_DIR
fabric-ca-client enroll --csr.names C=SG,ST=Singapore,L=Singapore,O=org1.example.com \
-m admin -u http://admin:adminpw@localhost:7054

现在用admin注册机构org1.example.com的管理员Admin@org1.example.com, 以及peer节点peer0.org1.example.com

1
2
3
4
5
fabric-ca-client register --id.name Admin@org1.example.com \
--id.secret mysecret --id.type client --id.affiliation org1 \
-u http://localhost:7054fabric-ca-client register \
--id.name peer0.org1.example.com --id.secret mysecret \
--id.type peer --id.affiliation org1 -u http://localhost:7054

加入Admin@org1.example.com

1
2
3
4
5
6
7
export FABRIC_CA_CLIENT_HOME=$ADMIN_DIR
fabric-ca-client enroll \
--csr.names C=SG,ST=Singapore,L=Singapore,O=org1.example.com \
-m Admin@org1.example.com \
-u http://Admin@org1.example.com:mysecret@localhost:7054
mkdir -p $ADMIN_DIR/msp/admincerts && \
cp $ADMIN_DIR/msp/signcerts/*.pem $ADMIN_DIR/msp/admincerts/

加入peer0.org1.example.com

1
2
3
4
5
6
export FABRIC_CA_CLIENT_HOME=$PEER_DIRfabric-ca-client enroll \
--csr.names C=SG,ST=Singapore,L=Singapore,O=org1.example.com \
-m peer0.org1.example.com \
-u http://peer0.org1.example.com:mysecret@localhost:7054
mkdir -p $PEER_DIR/msp/admincerts && \
cp $ADMIN_DIR/msp/signcerts/*.pem $PEER_DIR/msp/admincerts/

现在让我们看一下其中某个证书,例如peer0.org1.example.com的证书:

1
openssl x509 -in $PEER_DIR/msp/signcerts/cert.pem -text -noout

证书的签发者应当是ica.org1.example.com:

1
2
3
4
5
Issuer: C=SG, ST=Singapore, O=org1.example.com, CN=ica.org1.example.com
Validity
Not Before: May 3 10:27:59 2019 GMT
Not After : May 2 10:28:00 2020 GMT
Subject: C=SG, ST=Singapore, L=Singapore, O=org1.example.com, OU=peer, OU=org1, CN=peer0.org1.example.com

恭喜!我们已经完成了第三方CA和Fabric CA的整合!

Fabric第三方CAFabric第三方CA


原文链接:Using 3rd Party Root CAs in Hyperledger Fabric

汇智网翻译整理,转载请标明出处