OpenSSL自建CA和CA链,给主机签发证书的批处理(使用
x509命令和CA命令都能以CA身份给客户签发证书,本文介绍前者,CA命令的用法见另一篇博文。
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的儋州网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
当使用-CA infile选项时,x509命令的行为就像是一个“迷你CA”,对输入的文件进行签名,它不像CA命令那样需要预先建立配置文件定义的目录结构,也不把曾经签署的证书信息写入数据库,使用上相对方便一些。
把openssl.exe所在文件夹加入PATH环境变量,就可以在任何位置执行批处理(不建议安装于C盘,因为在生成文件的过程中可能会遇到的权限问题)。
为了防止浏览器弹出“没有主题备用名称”的警告信息,需要将配置文件"C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf"拷贝两份到D盘根目录,分别改名为01.ext和02.ext,在01.ext的[usr_cert]一节添加subjectAltName = DNS:host1,在02.ext的[usr_cert]一节添加subjectAltName = DNS:host2,请确保这两个文件存在。
复制下列代码粘贴到DOS窗口执行即可,或者保存为批处理文件,注意倒数第一行需要打回车。为了保证干净的实验环境,每次执行都会先删除之前建立的目录然后重建,所以不要在这些目录里保存重要资料。切记!
OpenSSL版本号为Windows版1.1.1c 28 May 2019。
用x509命令签发证书
根CA签发
实验场景:先建立根CA:RCA,再由RCA签发主机HOST1和HOST2的证书
批处理在D盘下建立目录RCA、HOST1、HOST2,各目录存放的文件顾名思义,其中RCA保留曾签发的所有证书的备份。
:: 根CA签发 :: 删除之前所有的文件 d:&cd\&rd/s/q host1&rd/s/q host2&rd/s/q rca&md host1&md host2&md rca&cd rca :: 生成自签名的根证书,私钥和公钥: openssl req -x509 -newkey rsa:8192 -keyout rca.key -out rca.cer -days 3650 -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-R/CN=RCA/emailAddress=ca@tiger.com -passout pass:abcd openssl rsa -in rca.key -pubout -out rca.pub -passin pass:abcd :: 把RCA的证书和公钥拷贝到HOST1和HOST2 copy rca.pub d:\host1© rca.cer d:\host1© rca.pub d:\host2© rca.cer d:\host2 :: 生成host1与host2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-1/CN=host1 -addext "subjectAltName = DNS:host1" -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-2/CN=host2 -addext "subjectAltName = DNS: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 :: 用RCA的私钥签署用户请求 Openssl x509 -req -days 1095 -in host1.csr -CA rca.cer -CAkey rca.key -out host1.cer -CAcreateserial -passin pass:abcd -extfile "d:\01.ext" -extensions usr_cert Openssl x509 -req -days 1095 -in host2.csr -CA rca.cer -CAkey rca.key -out host2.cer -CAcreateserial -passin pass:abcd -extfile "d:\02.ext" -extensions usr_cert :: 把HOST1和HOST2的所属文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 :: 验证证书链 openssl verify -show_chain -CAfile rca.cer host1.cer openssl verify -show_chain -CAfile rca.cer host2.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保留曾签发的所有证书的备份。
:: 二级CA签发 :: 删除之前所有的文件 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 :: 生成自签名的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=CA-1/CN=CA1/emailAddress=ca1@tiger.com -passout pass:abcd openssl rsa -in ca1.key -pubout -out ca1.pub -passin pass:abcd :: 把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 :: 生成CA2的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca2.key -out ca2.csr -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-2/CN=CA2/emailAddress=ca2@tiger.com -passout pass:abcd openssl rsa -in ca2.key -pubout -out ca2.pub -passin pass:abcd :: 用CA1的私钥签署CA2的请求 Openssl x509 -req -days 1095 -in ca2.csr -CA ca1.cer -CAkey ca1.key -out ca2.cer -days 3650 -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial :: 把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 :: 生成HOST1与HOST2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-1/CN=host1 -addext "subjectAltName = DNS:host1" -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-2/CN=host2 -addext "subjectAltName = DNS: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 :: 用CA2的私钥签署用户证书: Openssl x509 -req -days 1095 -in host1.csr -CA ca2.cer -CAkey ca2.key -out host1.cer -days 3650 -passin pass:abcd -CAcreateserial -extfile "d:\01.ext" -extensions usr_cert Openssl x509 -req -days 1095 -in host2.csr -CA ca2.cer -CAkey ca2.key -out host2.cer -days 3650 -passin pass:abcd -CAcreateserial -extfile "d:\02.ext" -extensions usr_cert echo 把HOST1和HOST2的所有文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 :: 验证证书链 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保留曾签发的所有证书的备份。
:: 三级CA签发 :: 删除之前所有的文件 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 :: 生成自签名的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=CA-1/CN=CA1/emailAddress=ca1@tiger.com -passout pass:abcd openssl rsa -in ca1.key -pubout -out ca1.pub -passin pass:abcd :: 把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 :: 生成CA2的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca2.key -out ca2.csr -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-2/CN=CA2/emailAddress=ca2@tiger.com -passout pass:abcd openssl rsa -in ca2.key -pubout -out ca2.pub -passin pass:abcd :: 用CA1的私钥签署CA2的请求 Openssl x509 -req -days 1095 -in ca2.csr -CA ca1.cer -CAkey ca1.key -out ca2.cer -days 3650 -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial :: 把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 :: 生成CA3的请求,私钥和公钥 openssl req -newkey rsa:8192 -keyout ca3.key -out ca3.csr -subj /C=CN/ST=jiangsu/L=nanjing/O=Tiger/OU=CA-3/CN=CA3/emailAddress=ca3@tiger.com -passout pass:abcd openssl rsa -in ca3.key -pubout -out ca3.pub -passin pass:abcd :: 用CA2的私钥签署CA3的请求 Openssl x509 -req -days 1095 -in ca3.csr -CA ca2.cer -CAkey ca2.key -out ca3.cer -days 3650 -passin pass:abcd -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf" -extensions v3_ca -CAcreateserial :: 把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 :: 生成HOST1与HOST2的证书请求、私钥和公钥 openssl req -newkey rsa:8192 -keyout host1.key -out host1.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-1/CN=host1 -addext "subjectAltName = DNS:host1" -passout pass:abcd openssl req -newkey rsa:8192 -keyout host2.key -out host2.csr -subj /C=CN/ST=guangdong/L=shenzhen/O=SUN/OU=Office-2/CN=host2 -addext "subjectAltName = DNS: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 :: 用CA3的私钥签署用户证书: Openssl x509 -req -days 1095 -in host1.csr -CA ca3.cer -CAkey ca3.key -out host1.cer -days 3650 -passin pass:abcd -CAcreateserial -extfile "d:\01.ext" -extensions usr_cert Openssl x509 -req -days 1095 -in host2.csr -CA ca3.cer -CAkey ca3.key -out host2.cer -days 3650 -passin pass:abcd -CAcreateserial -extfile "d:\02.ext" -extensions usr_cert :: 把HOST1和HOST2的所有文件拷贝到对应目录 copy host1.* d:\host1© host2.* d:\host2 :: 验证证书链: 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"
网站标题:OpenSSL自建CA和CA链,给主机签发证书的批处理(使用
网页地址:http://ybzwz.com/article/gggope.html