联盟链系列 - 用Openssl颁发X.509证书

交互式 生成 X.509证书

(1)Root CA

  1. 生成CA的私钥
openssl genrsa -out ca/ca-prikey.pem 2048
  1. 生成CA的公钥
openssl rsa -in ca/ca-prikey.pem -pubout -out ca/ca-pubkey.pem
  1. 生成CA的根证书
# 创建证书请求 
openssl req -new -out ca/ca-req.csr -key ca/ca-prikey.pem
      # 证书内容可选填 (其它 一路回车, 使用默认值)
      Organization Name (eg, company) : XXX
      Common Name (eg, YOUR name) []: root 
      
# 自签署证书 (Create a self-signed certificate)
openssl x509 -req -in ca/ca-req.csr -out ca/ca-cert.pem -signkey ca/ca-prikey.pem -days 3650 

# (可选)将证书导出成浏览器支持的.p12格式, 密码: 123456       
openssl pkcs12 -export -clcerts -in ca/ca-cert.pem -inkey ca/ca-prikey.pem -out ca/ca.p12 

(2)使用根证书生成 一个node(即服务器)的证书

  1. 生成node的私钥
openssl genrsa -out ca/node-prikey.pem 2048
  1. 生成node的公钥
openssl rsa -in ca/node-prikey.pem -pubout -out ca/node-pubkey.pem
  1. 使用根证书生成node的证书
# 创建证书请求
openssl req -new -out ca/node-req.csr -key ca/node-prikey.pem
      # 一路回车, 使用默认值, 直到 Common Name (注意: 一定要写服务器所在的ip地址)
      Organization Name (eg, company) : CMCC
      Common Name (eg, YOUR name) []: 127.0.0.1
      # 后续一路回车
      
# 使用CA证书及密钥对服务器证书进行签名
openssl x509 -req -in ca/node-req.csr -out ca/node-cert.pem -CA ca/ca-cert.pem -CAkey ca/ca-prikey.pem -CAcreateserial -days 3650 

# (可选)将证书导出成浏览器支持的.p12格式, 密码: 123456       
openssl pkcs12 -export -clcerts -in ca/node-cert.pem -inkey ca/node-prikey.pem -out ca/node.p12 

(3)使用根证书生成 一个客户端的证书

  1. 生成client的私钥
openssl genrsa -out ca/client-prikey.pem 2048
  1. 生成client的公钥
openssl rsa -in ca/client-prikey.pem -pubout -out ca/client-pubkey.pem

openssl x509 -in ca/client-cert.pem -pubkey  -noout > ca/client-pubkey.pem
  1. 使用根证书生成client的证书
# 创建证书请求 
openssl req -new -out ca/client-req.csr -key ca/client-prikey.pem
      # 一路回车, 使用默认值, 直到 Common Name (注意: 可以填平台用户名)
      Organization Name (eg, company) : General Motors
      Common Name (eg, YOUR name) []: voter1
      Email Address []: [默认]

      Please enter the following 'extra' attributes 
      to be sent with your certificate request 
      A challenge password []:123456 
      An optional company name []: testing  

# 自签署证书
openssl x509 -req -in ca/client-req.csr -out ca/client-cert.pem -CA ca/ca-cert.pem -CAkey ca/ca-prikey.pem -CAcreateserial -days 3650  

# (可选)将证书导出成浏览器支持的.p12格式, 密码: 123456       
openssl pkcs12 -export -clcerts -in ca/client-cert.pem -inkey ca/client-prikey.pem -out ca/client.p12 

非交互式生成 X.509证书

(1)Root CA

# CA证书及密钥生成方法一----直接生成CA密钥及其自签名证书
# 如果想以后读取私钥文件ca_rsa_private.pem时不需要输入密码,亦即不对私钥进行加密存储,那么将-passout pass:123456替换成-nodes
openssl req -newkey rsa:2048 -passout pass:123456 -keyout ca_rsa_private.pem -x509 -days 365 -out ca.crt -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=CA/[email protected]"

# CA证书及密钥生成方法二----分步生成CA密钥及其自签名证书:
# openssl genrsa -aes256 -passout pass:123456 -out ca_rsa_private.pem 2048
# openssl req -new -x509 -days 365 -key ca_rsa_private.pem -passin pass:123456 -out ca.crt -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=CA/[email protected]"
# 如果生成证书时报错: Error opening file "../crypto/rand/randfile.c", 没关系, 版本大于1.1.1就不报了

(2)使用根证书生成 一个服务器证书

# 服务器证书及密钥生成方法一----直接生成服务器密钥及待签名证书
# 如果想以后读取私钥文件server_rsa_private.pem时不需要输入密码,亦即不对私钥进行加密存储,那么将-passout pass:server替换成-nodes
openssl req -newkey rsa:2048 -passout pass:server -keyout server_rsa_private.pem  -out server.csr -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=SERVER/[email protected]"

# 服务器证书及密钥生成方法二----分步生成服务器密钥及待签名证书
# openssl genrsa -aes256 -passout pass:server -out server_rsa_private.pem 2048
# openssl req -new -key server_rsa_private.pem -passin pass:server -out server.csr -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=SERVER/[email protected]"

# 使用CA证书及密钥对服务器证书进行签名:
openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca_rsa_private.pem -passin pass:123456 -CAcreateserial -out server.crt

# 将加密的RSA密钥转成未加密的RSA密钥,避免每次读取都要求输入解密密码
# 密码就是生成私钥文件时设置的passout、读取私钥文件时要输入的passin,比如这里要输入“server”
openssl rsa -in server_rsa_private.pem -out server_rsa_private.pem.unsecure

(3)使用根证书生成 一个客户端的证书

# 客户端证书及密钥生成方法一----直接生成客户端密钥及待签名证书
# 如果想以后读取私钥文件client_rsa_private.pem时不需要输入密码,亦即不对私钥进行加密存储,那么将-passout pass:client替换成-nodes
openssl req -newkey rsa:2048 -passout pass:client -keyout client_rsa_private.pem -out client.csr -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=CLIENT/[email protected]"

# 客户端证书及密钥生成方法二----分步生成客户端密钥及待签名证书:
# openssl genrsa -aes256 -passout pass:client -out client_rsa_private.pem 2048
# openssl req -new -key client_rsa_private.pem -passin pass:client -out client.csr -subj "/C=CN/ST=GD/L=SZ/O=COM/OU=NSP/CN=CLIENT/[email protected]"

# 使用CA证书及密钥对客户端证书进行签名:
openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca_rsa_private.pem -passin pass:123456 -CAcreateserial -out client.crt

# 将加密的RSA密钥转成未加密的RSA密钥,避免每次读取都要求输入解密密码
# 密码就是生成私钥文件时设置的passout、读取私钥文件时要输入的passin,比如这里要输入“client”
openssl rsa -in client_rsa_private.pem -out client_rsa_private.pem.unsecure

往期精彩回顾:
区块链知识系列
密码学系列
零知识证明系列
共识系列
公链调研系列
比特币系列
以太坊系列
EOS系列
Filecoin系列
联盟链系列
Fabric系列
智能合约系列
Token系列

猜你喜欢

转载自blog.csdn.net/wcc19840827/article/details/120280799