其实此前也写过,但当时仅实现了自动创建CA自签发证书等文件,近来时间颇为宽裕,加上这个东东的用处又几乎无所不在,因此,又在其基础上做了些修修补补。
- [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/[email protected]
- 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
- .......
- [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
- [root@test scripts]# grep -Ev "^$|^#" /etc/sysconfig/mos_ca.conf
- Dname=mos # 新创建的证书,其名字已mos开头
- Dhost=www.mos.com # 为这个域名颁发
- [email protected] # 定义其邮件地址
- [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/[email protected]
- 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
- ..................
- [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 # 要申请的证书域名
- [email protected] # 其邮件地址
- [root@test scripts]# mos_ca -s
- [root@test scripts]# ls /opt/mos_demo/tech.
- tech.csr tech.key
- [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/[email protected]
- 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
- ...................
- [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中心的域名
- [email protected] #私有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私有机构的域名,其实上面已经介绍过了
- [email protected] # CA私有机构的邮箱
- #----------------------------------#
- #Ddir=/opt/mos_demo/ # 创建证书的默认位置,不存在的话,会自动创建
- #Dname=tech # 以tech为名字创建证书相关文件
- #Dhost=tech.test.com # 请求签发证书的域名
- #[email protected] # 请求签发证书的邮箱
- #----------------------------------#
- #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]:[email protected]]: " Em
- Em=${Em:[email protected]}
- 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:[email protected]}
- 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