快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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"

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:OpenSSL自建CA和CA链,给主机签发证书的批处理(使用-创新互联
文章网址:http://6mz.cn/article/dcgoep.html

其他资讯