0 环境
openssl 1.1.1b https://www.cnblogs.com/jsjliyang/p/10606908.html
pkcs11-tool (由sudo apt-get install opensc 安装)
libpksc11 https://github.com/OpenSC/libp11
softhsm2 https://github.com/opendnssec/SoftHSMv2
1 证书生成
1.1 CA 自签署证书:
// 生成根证书私钥(pem文件)
openssl genrsa -out root.key 2048
// 生成根证书签发申请文件(csr文件)
openssl req -new -key root.key -out root.csr -subj "/CN=localhost/C=CN/ST=rootprovince/L=rootcity/O=rootorganization/OU=rootgroup"
// 自签发根证书(cer文件)
openssl x509 -req -days 365 -extensions v3_ca -signkey root.key -in root.csr -out root.crt
1.2 CA签发Server 证书:
// 生成服务端私钥
openssl genrsa -out server.key 2048
// 生成证书请求文件
openssl req -new -key server.key -out server.csr -subj "/CN=localhost/C=CN/ST=serverprovince/L=servercity/O=serverorganization/OU=servergroup"
// 使用根证书签发服务端证书
openssl x509 -req -days 365 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.crt
// 使用CA证书验证服务端证书
openssl verify -CAfile root.crt server.crt
1.3 CA签发Client证书,Client证书保存于pkcs11中:
//须先执行softhsm的步骤
//生成client密钥对 //module 为pkcs11格式的硬件驱动 id、label和pin要记住
pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so -l -k --key-type rsa:2048 --id 4144 --label testn --pin 56789
// 生成请求文件
openssl req -new -days 365 -subj "/CN=localhost/C=CN/ST=clientprovince/L=clientcity/O=clientorganization/OU=clientgroup" -engine pkcs11 -keyform engine -key "pkcs11:token=test;object=testn;type=private;pin-value=56789" -out client.csr
// 使用根证书签发客户端证书
openssl x509 -req -days 365 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.csr -out client.crt
// 使用CA证书验证客户端证书
openssl verify -CAfile root.crt client.crt
//将cert写入softhsm中
pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so -l --id 4144 --label testn -y cert -w client.crt --pin 56789
2 openssl双向认证
//服务器开启认证
openssl s_server -accept 8090 -key server.key -cert server.crt -CAfile root.crt -Verify 1
//客户端开启认证 此处的token为softhsm进行init时输入的label,而object为创建客户端密钥对时的label
openssl s_client -connect localhost:8090 -engine pkcs11 -keyform engine -key "pkcs11:token=test;object=testn;type=private;pin-value=56789" -cert client.crt -CAfile root.crt
//接下来双方就可以传数据了
3 softhsm2相关
可以通过配置文件配置so加载:(未测试)
openssl_conf = openssl_init //放在conf文件的顶行
/**
*
*
**/
//以下放在底部
[openssl_init]
engines=engine_section
[engine_section]
pkcs11 = pkcs11_section
[pkcs11_section]
engine_id = pkcs11
dynamic_path = /usr/local/lib/engines-1.1/libpkcs11.so
MODULE_PATH = /usr/local/lib/softhsm/libsofthsm2.so
init = 0
或者命令行:
//加载softhsm
OpenSSL> engine -t dynamic -pre SO_PATH:/usr/local/lib/engines-1.1/libpkcs11.so
-pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD
-pre MODULE_PATH:/usr/local/lib/softhsm/libsofthsm2.so
//检测是否运行
openssl engine pkcs11 -t
//初始化softhsm
softhsm2-util --init-token --slot 0 --label "test"
//根据提示输入pin和user pin
=== SO PIN (4-255 characters) ===
Please enter SO PIN: ****
Please reenter SO PIN: ****
=== User PIN (4-255 characters) ===
Please enter user PIN: *****
Please reenter user PIN: *****
The token has been initialized and is reassigned to slot 394926501