温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

超级账本Fabric如何使用第三方CA

发布时间:2021-12-22 14:51:53 来源:亿速云 阅读:253 作者:柒染 栏目:互联网科技

本篇文章给大家分享的是有关超级账本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基础知识

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

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

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

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为排序节点机构生成证书和密钥:

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文件夹里。

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相同。

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扩展。

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

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

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

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

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的证书:

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文件:

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

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

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

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

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

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

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

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的证书:

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

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

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如何使用第三方CA,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI