x509命令和CA命令都能以CA身份给客户签发证书,本文介绍前者,CA命令的用法见另一篇博文。
当使用-CA infile选项时,x509命令的行为就像是一个“迷你CA”,对输入的文件进行签名,它不像CA命令那样需要预先建立配置文件定义的目录结构,也不把曾经签署的证书信息写入数据库,使用上相对方便一些。
把openssl.exe所在文件夹加入PATH环境变量,就可以在任何位置执行批处理(不建议安装于C盘,因为在生成文件的过程中可能会遇到的权限问题),本实验会用到随OpenSSL默认安装的配置文件"C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf",请确保该文件存在。
为了保证干净的实验环境,每次执行批处理都会先删除它们然后重建,所以不要在这些目录里保存重要资料。切记!
OpenSSL版本号为Windows版1.1.1c 28 May 2019。
用x509命令签发证书
根CA签发
实验场景:先建立根CA:RCA,再由RCA签发主机HOST1和HOST2的证书
echo 删除之前所有的文件 d:&cd\&rd/s/q host1&rd/s/q host2&rd/s/q rca&md host1&md host2&md rca&cd rca echo 生成自签名的根证书,私钥和公钥: openssl req -x509 -newkey rsa:8192 -keyout rca.key -out rca.cer -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=RCA/[email protected] -passout pass:abcd openssl rsa -in rca.key -pubout -out rca.pub -passin pass:abcd echo 把RCA的证书和公钥拷贝到HOST1和HOST2 copy rca.pub d:\host1© rca.cer d:\host1© rca.pub d:\host2© rca.cer d:\host2 echo 生成host1与host2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=SUN-A/CN=host1 -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/O=Tiger/ST=jiangsu/CN=host2 -passout pass:abcd openssl rsa -in host1.key -pubout -out host1.pub -passin pass:abcd openssl rsa -in host2.key -pubout -out host2.pub -passin pass:abcd echo 用RCA的私钥签署用户请求 Openssl x509 -req -days 1095 -in host1.csr -CA rca.cer -CAkey rca.key -out host1.cer -passin pass:abcd -CAcreateserial Openssl x509 -req -days 1095 -in host2.csr -CA rca.cer -CAkey rca.key -out host2.cer -passin pass:abcd -CAcreateserial echo 把HOST1和HOST2的所属文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 echo 验证证书链 openssl verify -show_chain -CAfile rca.cer host1.cer openssl x509 -in rca.cer -noout -text|find "CA:TRUE" openssl x509 -in host1.cer -noout -text|find "CA:TRUE" openssl x509 -in host2.cer -noout -text|find "CA:TRUE"
二级CA签发
根CA:CA1
中间CA:CA2
CA1签发CA2的证书,CA2给HOST1和HOST2签发证书。
批处理在D盘根目录下建立目录CA1、CA2、HOST1、HOST2,各目录存放的文件顾名思义,其中CA2保留曾签发的所有证书的备份。
echo 删除之前所有的文件 d:&cd\&rd/s/q host1&rd/s/q host2&rd/s/q ca1&rd/s/q ca2&md host1&md host2&md ca1&md ca2&cd ca1 echo 生成自签名的CA1根证书、私钥和公钥: openssl req -x509 -newkey rsa:8192 -keyout ca1.key -out ca1.cer -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=CA1/[email protected] -passout pass:abcd openssl rsa -in ca1.key -pubout -out ca1.pub -passin pass:abcd echo 把CA1的证书和公钥拷贝到CA2,HOST1和HOST2 copy ca1.cer d:\host1© ca1.pub d:\host1© ca1.cer d:\host2© ca1.pub d:\host2© ca1.cer d:\ca2© ca1.pub d:\ca2 echo 生成CA2的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca2.key -out ca2.csr -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=CA2/[email protected] -passout pass:abcd openssl rsa -in ca2.key -pubout -out ca2.pub -passin pass:abcd echo 用CA1的私钥签署CA2的请求 Openssl x509 -req -days 1095 -in ca2.csr -CA ca1.cer -CAkey ca1.key -out ca2.cer -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial echo 把CA2的证书和公钥拷贝到HOST1和HOST2,把CA2所属文件都拷贝到CA2 copy ca2.cer d:\host1© ca2.pub d:\host1© ca2.cer d:\host2© ca2.pub d:\host2© ca2.* \ca2&cd\ca2 echo 生成HOST1与HOST2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=SUN-A/CN=host1 -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/O=Tiger/ST=jiangsu/CN=host2 -passout pass:abcd openssl rsa -in host1.key -pubout -out host1.pub -passin pass:abcd openssl rsa -in host2.key -pubout -out host2.pub -passin pass:abcd echo 用CA2的私钥签署用户证书: Openssl x509 -req -days 1095 -in host1.csr -CA ca2.cer -CAkey ca2.key -out host1.cer -passin pass:abcd -CAcreateserial Openssl x509 -req -days 1095 -in host2.csr -CA ca2.cer -CAkey ca2.key -out host2.cer -passin pass:abcd -CAcreateserial echo 把HOST1和HOST2的所有文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 echo 验证证书链 copy ca2.cer+ca1.cer ca-chain.cer openssl verify -show_chain -CAfile ca-chain.cer host1.cer openssl verify -show_chain -CAfile ca-chain.cer host2.cer openssl x509 -in ca1.cer -noout -text|find "CA:TRUE" openssl x509 -in ca2.cer -noout -text|find "CA:TRUE" openssl x509 -in host1.cer -noout -text|find "CA:TRUE" openssl x509 -in host2.cer -noout -text|find "CA:TRUE"
三级CA签发
根CA:CA1
中间CA:CA2,CA3
CA1签发CA2的证书,CA2签发CA3的证书,CA3给HOST1和HOST2签发证书。
批处理在D盘根目录下建立目录CA1、CA2、CA3、HOST1、HOST2,各目录存放的文件顾名思义,其中CA3保留曾签发的所有证书的备份。
echo 删除之前所有的文件 d:&cd\&rd/s/q host1&rd/s/q host2&rd/s/q ca1&rd/s/q ca2&rd/s/q ca3&md host1&md host2&md ca1&md ca2&md ca3&cd ca1 echo 生成自签名的CA1根证书、私钥和公钥: openssl req -x509 -newkey rsa:8192 -keyout ca1.key -out ca1.cer -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=CA1/[email protected] -passout pass:abcd openssl rsa -in ca1.key -pubout -out ca1.pub -passin pass:abcd echo 把CA1的证书和公钥拷贝到CA2,CA3,HOST1,HOST2 copy ca1.cer d:\ca2© ca1.pub d:\ca2© ca1.cer d:\ca3© ca1.pub d:\ca3© ca1.cer d:\host1© ca1.pub d:\host1© ca1.cer d:\host2© ca1.pub d:\host2 echo 生成CA2的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca2.key -out ca2.csr -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=CA2/[email protected] -passout pass:abcd openssl rsa -in ca2.key -pubout -out ca2.pub -passin pass:abcd echo 用CA1的私钥签署CA2的请求 Openssl x509 -req -days 1095 -in ca2.csr -CA ca1.cer -CAkey ca1.key -out ca2.cer -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial echo 把CA2的证书和公钥拷贝到CA3,HOST1和HOST2,把CA2所属文件都拷贝到CA2 copy ca2.cer d:\ca3© ca2.pub d:\ca3© ca2.cer d:\host1© ca2.pub d:\host1© ca2.cer d:\host2© ca2.pub d:\host2© ca2.* \ca2&cd\ca2 echo 生成CA3的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca3.key -out ca3.csr -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=T-CA/CN=CA3/[email protected] -passout pass:abcd openssl rsa -in ca3.key -pubout -out ca3.pub -passin pass:abcd echo 用CA2的私钥签署CA3的请求 Openssl x509 -req -days 1095 -in ca3.csr -CA ca2.cer -CAkey ca2.key -out ca3.cer -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial echo 把CA3的证书和公钥拷贝到HOST1和HOST2,把CA3所属文件都拷贝到CA3 copy ca3.cer d:\host1© ca3.pub d:\host1© ca3.cer d:\host2© ca3.pub d:\host2© ca3.* \ca3&cd\ca3 echo 生成HOST1与HOST2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=SUN-A/CN=host1 -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/O=Tiger/ST=jiangsu/CN=host2 -passout pass:abcd openssl rsa -in host1.key -pubout -out host1.pub -passin pass:abcd openssl rsa -in host2.key -pubout -out host2.pub -passin pass:abcd echo 用CA3的私钥签署用户证书: Openssl x509 -req -days 1095 -in host1.csr -CA ca3.cer -CAkey ca3.key -out host1.cer -passin pass:abcd -CAcreateserial Openssl x509 -req -days 1095 -in host2.csr -CA ca3.cer -CAkey ca3.key -out host2.cer -passin pass:abcd -CAcreateserial echo 把HOST1和HOST2的所有文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 echo 验证证书链: copy ca3.cer+ca2.cer+ca1.cer ca-chain.cer openssl verify -show_chain -CAfile ca-chain.cer host1.cer openssl verify -show_chain -CAfile ca-chain.cer host2.cer openssl x509 -in ca1.cer -noout -text|find "CA:TRUE" openssl x509 -in ca2.cer -noout -text|find "CA:TRUE" openssl x509 -in ca3.cer -noout -text|find "CA:TRUE" openssl x509 -in host1.cer -noout -text|find "CA:TRUE" openssl x509 -in host2.cer -noout -text|find "CA:TRUE"