使用Shell脚本自动构建OpenSSL的CA等
副标题[/!--empirenews.page--] 其实此前也写过,但当时仅实现了自动创建CA自签发证书等文件,近来时间颇为宽裕,加上这个东东的用处又几乎无所不在,因此,又在其基础上做了些修修补补。 实现功能: 1、全自动创建CA私有机构和测试证书一套 2、在已有的CA自签发证书或测试证书的基础上,进行几项调整或重新创建或给其他申请签发等 考虑到代码略多,因此本文先做详细使用介绍,然后再粘贴代码及上传附件,心急代码的同学,可以直接向下拉查看或下载附件。 环境: rhel5.8 32bit openssl为光盘yum源安装 用法: command [option] 可以不用选项,且选项也不多,否则就失去脚本的意义了,使用部分有详细介绍 /etc/sysconfig/mos_ca.conf :配置文件,可做自定义调整,也可不用,脚本皆有默认值 一、获取脚本,给执行权限,移动到系统路径,直接执行即可生成CA自签发和演示证书:其会在/etc/pki/CA/里面生成自签发所需的所有文件,会在/opt/mos_demo目录内生成一套演示证书(其功能完全可用),操作如下: [root@test scripts]# chmod 700 mos_ca.sh [root@test scripts]# ll mos_ca.sh -rwx------ 1 root root 3.3K Jan 8 12:24 mos_ca.sh [root@test scripts]# cp mos_ca.sh /bin/mos_ca [root@test scripts]# mos_ca # 执行到此已OK,下列操主要就是检查结果 [root@test scripts]# ls /etc/pki/CA/ #查看生成的CA自签署文件 cacert.pem crl/ index.txt.attr newcerts/ serial certs/ index.txt index.txt.old private/ serial.old [root@test scripts]# openssl x509 -text -in /opt/mos_demo/demo. #查看生成的演示证书 demo.crt demo.csr demo.key [root@test scripts]# openssl x509 -text -in /opt/mos_demo/demo.crt #查看刚才脚本签署的证书 Certificate: Data: Version: 3 (0x2) Serial Number: 1 (0x1) Signature Algorithm: sha1WithRSAEncryption Issuer: C=CN, ST=Henan, L=Zhengzhou, O=Youguess, OU=Tech, CN=ca.mos.com/emailAddress=root@mos.com Validity Not Before: Jan 8 05:54:52 2013 GMT Not After : Jan 8 05:54:52 2014 GMT Subject: C=CN, ST=Henan, O=Youguess, OU=Tech, CN=demo.mos.com ....... 二、当系统已有CA自签署和演示证书已存在时,再次执行脚本,会提示是否将原来的文件,以时间命名备份到其当前目录,具体如下 [root@test opt]# mos_ca CA existe,Continue? y: Move file; n-> Quit. [y|n] y #输入y移动文件,n退出 Demo certificate existe, Continue? y: Move file; n: Quit. [y|n] y #同上 [root@test opt]# ls /etc/pki/CA/ 2013-01-08-13:54:48tmp/ crl/ index.txt.old serial cacert.pem index.txt newcerts/ serial.old certs/ index.txt.attr private/ [root@test opt]# ls /etc/pki/CA/2013-01-08-13:54:48tmp/ cacert.pem crl/ index.txt.attr newcerts/ serial certs/ index.txt index.txt.old private/ serial.old [root@test opt]# ls /opt/mos_demo/ 2013-01-08-13:54:51tmp/ demo.crt demo.csr demo.key [root@test opt]# ls /opt/mos_demo/2013-01-08-13:54:51tmp/demo. demo.crt demo.csr demo.key 三、给一个自定义域名签发证书,使用 -n 选项,需要注意的是,操作之前,一定要修改/etc/sysconfig/mos_ca.conf中的Dhost=“你的域名”,若直接使用 -n 的话,当前CA是无法签发的,因为其和演示证书的域名内容相同是不被允许的,就像真正的CA不能为一个域名颁发两个证书给不同的客户。演示如下: [root@test scripts]# grep -Ev "^$|^#" /etc/sysconfig/mos_ca.conf Dname=mos # 新创建的证书,其名字已mos开头 Dhost=www.mos.com # 为这个域名颁发 De=root@mos.com # 定义其邮件地址 [root@test scripts]# ll /opt/mos_demo/ [root@test scripts]# mos_ca -n 2013-01-08-13:54:51tmp/ demo.csr mos.crt mos.key demo.crt demo.key mos.csr [root@test scripts]# openssl x509 -text -in /opt/mos_demo/mos.crt #查看刚才签发新证书 Certificate: Data: Version: 3 (0x2) Serial Number: 2 (0x2) Signature Algorithm: sha1WithRSAEncryption Issuer: C=CN, ST=Henan, L=Zhengzhou, O=Youguess, OU=Tech, CN=ca.mos.com/emailAddress=root@mos.com Validity Not Before: Jan 8 06:14:00 2013 GMT Not After : Jan 8 06:14:00 2014 GMT Subject: C=CN, ST=Henan, O=Youguess, OU=Tech, CN=www.mos.com .................. 四、使用 -s 选项,生成不签署的证书,用以给向别的CA机构申请,需要在配置文件中修改其国家、省份州、城市、公司、部分、域名、邮件。操作如下: [root@test scripts]# grep -Ev "^$|^#" /etc/sysconfig/mos_ca.conf #配置文件,做如下修改 Cny=CN #国家 Pve=Henan #省份/州 Cty=Zhengzhou #城市 Bis=Youguess #公司 Bnh=Tech #部门 Dname=tech #证书名字以此开头 Dhost=tech.test.com # 要申请的证书域名 De=root@test.com # 其邮件地址 [root@test scripts]# mos_ca -s [root@test scripts]# ls /opt/mos_demo/tech. tech.csr tech.key 五、使用 -o 给已有的csr证书签发请求文件签发,需要再配置文件中指定路径和部分名字,例如,证书的绝对路径为:/opt/mos_demo/tech.csr ,在配置文件中,去掉.csr即可,如:/opt/mos_demo/tech。为刚才没签发的证书签发。(再提醒一下,私有CA签发需要合csr中填写的国家、省份、城市、公司、部门保持一致。)操作如下: [root@test scripts]# grep Other /etc/sysconfig/mos_ca.conf Other=/opt/mos_demo/tech [root@test scripts]# mos_ca -o [root@test scripts]# ll /opt/mos_demo/tech. tech.crt tech.csr tech.key # 签发后生成的crt文件已出现,查看内容如下: [root@test scripts]# openssl x509 -text -in /opt/mos_demo/tech.crt Certificate: Data: Version: 3 (0x2) Serial Number: 3 (0x3) Signature Algorithm: sha1WithRSAEncryption Issuer: C=CN, ST=Henan, L=Zhengzhou, O=Youguess, OU=Tech, CN=ca.mos.com/emailAddress=root@mos.com Validity Not Before: Jan 8 06:49:53 2013 GMT Not After : Jan 8 06:49:53 2014 GMT Subject: C=CN, ST=Henan, O=Youguess, OU=Tech, CN=tech.test.com ................... (编辑:滁州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |