第三方CA在hyperledger fabric中的使用方法

简介:

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

Hyperledger Fabric区块链开发教程: Node.js | Java | Golang

1、准备工作

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FG5jPuCg-1582423441932)(fabric-external-root-ca-tutorial/demo.png)]

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

  • 安装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的整合!

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-haYESz6V-1582423441933)(fabric-external-root-ca-tutorial/ca-chain-done.png)]


原文链接:Hyperledger Fabric中使用第三方CA的教程及源码 — 汇智网

目录
相关文章
|
6月前
|
开发框架 .NET 区块链
Hyperledger fabric部署链码(五)初始化与链码升级
fabric部署chaincode-go(智能合约)系列之五
|
6月前
|
JavaScript 测试技术 Go
Hyperledger fabric部署链码(一)打包链码
fabric部署chaincode-go(智能合约)系列之一
|
6月前
|
测试技术 Go 区块链
Hyperledger fabric 测试环境部署
Hyperledger fabric 测试环境部署及相关问题解答
109 3
|
6月前
|
存储 JSON 安全
Hyperledger fabric智能合约编写(一)
本篇文章主要对链码编写的主要思路和部分API进行梳理。
|
6月前
|
Go API 区块链
Hyperledger Fabric相关概念介绍
在学习Hyperledger Fabric的过程中,初步对相关概念的了解。
Hyperledger Fabric相关概念介绍
|
6月前
|
JSON 区块链 数据格式
Hyperledger fabric部署链码(四)提交链码定义到channel
fabric部署chaincode-go(智能合约)系列之四
|
6月前
|
测试技术 API 区块链
Hyperledger fabric部署链码(三)批准链码定义
fabric部署chaincode-go(智能合约)系列之三
|
6月前
|
区块链
Hyperledger fabric部署链码(二)安装链码到fabric
fabric部署chaincode-go(智能合约)系列之二
|
8月前
|
消息中间件 Java Kafka
Hyperledger Fabric 通道配置文件和容器环境变量详解
Fabric 节点的主配置路径为 FABRIC_CFG_PATH 环境变量所指向路径(默认为 /etc/hyperledger/fabric)。在不显式指定配置路径时,会尝试从主配置路径下查找相关的配置文件。
213 0
|
Java API 区块链
Hyperledger Fabric 2.x Java区块链应用
在上一篇文章中分享了智能合约的安装并使用cli客户端进行合约的调用;本文将使用Java代码基于fabric-gateway-java进行区块链网络的访问与交易,并集成SpringBoot框架。
738 0
Hyperledger Fabric 2.x Java区块链应用