|
其实此前也写过,但当时仅实现了自动创建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
- ...................
六、 使用CA选项,重新配置并生成CA,但不创建测试证书,需要先在配置文件中修改自签发CA的各项属性(国家、省份、城市、公司、部门、域名、邮件等,当然,也可不配置,仍会重新生成,旧的被移动保存)。操作如下:
- [root@test mos_demo]# grep -Ev "^$|^#" /etc/sysconfig/mos_ca.conf
- Dir=/etc/pki/CA/ # 自签发CA文件所在的目录,一般不要改动
- Cny=CN #国家
- Pve=Henan #省份/州
- Cty=Zhengzhou #城市
- Bis=Youguess #公司
- Bnh=Tech #部门
- Host=ca.mos.com #私有CA中心的域名
- Em=root@mos.com #私有CA中心的邮件地址
- [root@test mos_demo]# mos_ca CA
- CA existe,Continue? y: Move file; n-> Quit. [y|n] y #移动原CA中心文件
- [root@test mos_demo]# ll /etc/pki/CA/ #可以看到,原CA文件,以被到时间临时目录内
- 2013-01-08-15:01:12tmp/ certs/ index.txt private/
- cacert.pem crl/ newcerts/ serial
七、其实没了,但还有配置文件没说,脚本有默认值,配置文件可以没有,但是如果需要更详细的调整,需要手动创建配置文件,以及里面的具体参数。若不需要使用其相关参数,使用#注释即可;若要使用,需删除行首井号"#"。操作和注解如下:
- [root@test mos_demo]# cat /etc/sysconfig/mos_ca.conf
- #----------------------------------#
- #Cnf=/etc/pki/tls/openssl.cnf #openssl的配置文件,对于下面的国家,省份的信息,就是使用sed对配置文件做的修改,一般默认即可, #Dir=/etc/pki/CA/ # CA私有机构所需文件所在目录,一般默认即可
- Cny=CN # 国家,对CA自签发证书剩下,也对创建申请证书生效,脚本内默认为CN
- Pve=Henan # 省份/州,默认Henan(河南)同上
- Cty=Zhengzhou # 城市,默认Zhengzhou(郑州), 同上
- Bis=Youguess # 公司名字,默认 Youguess(你猜), 同上
- Bnh=Tech # 部门,默认Tech(技术部),同上
- #----------------------------------#
- Host=ca.mos.com # CA私有机构的域名,其实上面已经介绍过了
- Em=root@mos.com # CA私有机构的邮箱
- #----------------------------------#
- #Ddir=/opt/mos_demo/ # 创建证书的默认位置,不存在的话,会自动创建
- #Dname=tech # 以tech为名字创建证书相关文件
- #Dhost=tech.test.com # 请求签发证书的域名
- #De=root@test.com # 请求签发证书的邮箱
- #----------------------------------#
- #Other=/opt/mos_demo/tech #申请签发证书tech.csr所在的绝对路径,但不包含扩展名。
---------------------------------华丽的分割线------------------------------
代码如下:
- #!/bin/bash
- # Author: MOS
- # Script name: mos_ca.sh
- # Date & Time: 2013-01-06/23:05:35
- # Version: 1.0.2
- # Description:
- #
- [ -f /etc/sysconfig/mos_ca.conf ] && . /etc/sysconfig/mos_ca.conf
- Config(){
- Cnf=${Cnf:-/etc/pki/tls/openssl.cnf}
- cp $Cnf $Cnf.`date +%F-%T`.bak
- Dir=${Dir:-/etc/pki/CA/}
- Cny=${Cny:-CN}
- Pve=${Pve:-Henan}
- Cty=${Cty:-Zhengzhou}
- Bis=${Bis:-Youguess}
- Bnh=${Bnh:-Tech}
- sed -i "s@\(^dir.*=[[:space:]]\).*@\1$Dir@g" $Cnf
- sed -i "s@\(^countryName_default.*=[[:space:]]\).*@\1$Cny@g" $Cnf
- sed -i "s@\(^stateOrProvinceName_default.*=[[:space:]]\).*@\1$Pve@g" $Cnf
- sed -i "s@\(^localityName_default.*=[[:space:]]\).*@\1$Cty@g" $Cnf
- sed -i "s@\(^0.organizationName_default.*=[[:space:]]\).*@\1$Bis@g" $Cnf
- sed -i "s@^#\(organizationalUnitName_default\([[:space:]]\)=\)@\1 $Bnh@g" $Cnf
- }
- Create_CA(){
- Dir=${Dir:-/etc/pki/CA/}
- [ ! -d ${Dir}crl ] && mkdir -pm 700 ${Dir}crl
- [ ! -d ${Dir}newcerts ] && mkdir -pm 700 ${Dir}newcerts
- [ ! -d ${Dir}certs ] && mkdir -pm 700 ${Dir}certs
- [ ! -f ${Dir}index.txt ] && touch ${Dir}index.txt
- [ ! -f ${Dir}serial ] && echo 01 > ${Dir}serial
- [ ! -d ${Dir}private ] && mkdir -pm 700 ${Dir}private
- (umask 077; openssl genrsa -out ${Dir}private/cakey.pem 2048 &> /dev/null )
- #read -p "Please input CA hostname [default:ca.mos.com]: " Host
- Host=${Host:-ca.mos.com}
- #read -p "Please input CA E-mail [default]:root@mos.com]: " Em
- Em=${Em:-root@mos.com}
- echo -e "\n\n\n\n\n${Host}\n${Em}\n"|openssl req -x509 -new -key ${Dir}private/cakey.pem -out ${Dir}cacert.pem -days 3650 &> /dev/null
- }
- Create(){
- Dir=${Dir:-/etc/pki/CA/}
- Date=`date +%F-%H:%M:%S`
- [ ! -f /etc/pki/CA/private/cakey.pem ] && Create_CA && return 0
- if [ -f /etc/pki/CA/private/cakey.pem ];then
- read -p "CA existe,Continue? y: Move file; n-> Quit. [y|n] " Choice
- if [[ "$Choice" == "y" ]];then
- [ ! -d "$Dir""$Date"tmp ] && mkdir -p "$Dir""$Date"tmp
- mv "$Dir"* "$Dir""$Date"tmp/ &> /dev/null
- Create_CA
- elif [[ "$Choice" == "n" ]];then
- exit 0
- else
- echo "Error input..."
- exit 1
- fi
- fi
- }
- Create_crt(){
- (umask 077;openssl genrsa 1024 > "$Ddir""$Dname".key) &> /dev/null
- echo -e "\n\n\n\n\n"$Dhost"\n"$DE"\n\n\n"|openssl req -new -key "$Ddir""$Dname".key -out "$Ddir""$Dname".csr &> /dev/null
- [[ $Set != "-s" ]] && echo -e "y\ny\n"|openssl ca -in "$Ddir""$Dname".csr -out "$Ddir""$Dname".crt -days 365 &> /dev/null && exit 0
- [[ $Set == "-s" ]] && exit 0
- }
- Demo_crt(){
- Ddir=${Ddir:-/opt/mos_demo/}
- Dname=${Dname:-demo}
- Dhost=${Dhost:-demo.mos.com}
- De=${De:-root@mos.com}
- Date=`date +%F-%H:%M:%S`
- [ ! -d $Ddir ] && mkdir -p $Ddir &> /dev/null
- [[ ! -f "$Ddir""$Dname".key && ! -f "$Ddir""$Dname".csr ]] && Create_crt && exit 0
- if [[ -f "$Ddir""$Dname".key || -f "$Ddir""$Dname".csr || -f "$Ddir""$Dname".crt ]];then
- read -p "Demo certificate existe, Continue? y: Move file; n: Quit. [y|n] " Cie
- if [[ "$Cie" == "y" ]];then
- [ ! -d "$Ddir""$Date"tmp ] && mkdir -p "$Ddir""$Date"tmp && mv "$Ddir""$Dname"* "$Ddir""$Date"tmp/ &> /dev/null
- Create_crt
- exit 0
- elif [[ "$Cie" == "n" ]];then
- exit 0
- else
- echo "Error input..."
- fi
- fi
- }
- Set=$1
- if [[ $Set =~ (-n)|(-o)|(-s)|(CA) && -z $2 || -z $Set ]];then
- [ -z $Set ] && Config && Create && Demo_crt && exit 0
- [ $Set = -n ] && Demo_crt && exit 0
- [ $Set = -s ] && Config && Demo_crt && exit 0
- [ $Set = CA ] && Config && Create && exit 0
- [ $Set = -o ] && echo -e "y\ny\n"|openssl ca -in "$Other".csr -out "$Other".crt -days 365 &> /dev/null
- else
- echo "Error,Invalid option!"
- exit 1
- fi
写东西不易,列位看官,觉得还凑合的话,来个赞吧 O(∩_∩)O.
补充一点:昨天在centos6.0 32bit系统上使用时,发现无法签发crt,检查得知,配置文件(/etc/pki/tls/openssl.cnf)的 stateOrProvinceName_default(州或省的默认名字)行,在rhel5.8中未被注释,因此脚本正常,而在centos6.0中貌似被默认注释了,因此,导致脚本在6.0使用时出现异常:
解决方法:在修改此项时,做逻辑判断即可,代码就不列了,直接上传于附件了,有兴趣的同学,自行下载哈。O(∩_∩)O.
|
|
|