CA签发工具
#!/bin/bash
#author Sun Ying
#date:2015-12-17
if [ $# -lt 1 ];then
echo -e "\033[34mUsage: `basename $0` -h|--help for help\033[0m"
exit 0
fi
[ -e ./CA_config ] && source ./CA_config
ERRLOG=`date +%Y%H%M`.log
PASSWORD=${PASSWORD-"1234"}
CO_S=${CN_S-"CN"}
ST_S=${ST_S-"BJ"}
LO_S=${LO_S-"BJ"}
OG_S=${OG_S-"Centos"}
OU_S=${OU_S-"Linux"}
HS_S=${HS_S-"centos.example.com"}
CO_C=${CN_C-"CN"}
ST_C=${ST_C-"LN"}
LO_C=${LO_C-"SY"}
OG_C=${OG_C-"Client1"}
OU_C=${OU_C-"Operation System"}
HS_C=${HS_C-"client1.example.com"}
DAYS=${DAYS-"365"}
P_KEY=${P_KEY-/etc/pki/CA/private/cakey.pem}
CACERT=${CACERT-/etc/pki/CA/cacert.pem}
M_SIZE=${M_SIZE-"2048"}
CSR_pem=${CSR_pem-/etc/pki/CA/newcerts/${HOSTNAME}.csr}
S_DAYS=${S_DAYS-365}
DIR_F=/etc/pki/CA
umask=077
[ ! -d /tmp/CA ] && mkdir -p /tmp/CA/
touch /tmp/CA/$ERRLOG
V_Key(){
if [ -f $temp ]; then
openssl rsa -noout -text -in $temp
else
echo -e "\033[32mYou must assign a private Key with parameter -v|--verify\033[0m"
exit 3
fi
}
V_Cert(){
if [ -f $temp ]; then
openssl x509 -noout -text -in $temp
else
echo -e "\033[32mYou must assign a cert file with parameter -k|--check\033[0m"
exit 3
fi
}
V_Csr(){
if [ -f $temp ]; then
openssl req -noout -text -in $temp
else
echo -e "\033[32mYou must assign a CSR file with parameter -i|--identify\033[0m"
exit 3
fi
}
genPriKey(){
[ -e $P_KEY ] && rm -f $P_KEY
openssl genrsa -des3 -passout pass:$PASSWORD -out $P_KEY $M_SIZE >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Create private key in $P_KEY \033[033msuccess\033[0m"
else
echo "please check the error log under /tmp/CA/$ERRLOG"
exit 1
fi
}
selfSign(){
[ -e $CACERT ] && rm -f $CACERT
if [ ! -e $P_KEY ] ;then
echo "Please create the private key first"
exit 1
fi
openssl req -new -x509 -passin pass:$PASSWORD -key $P_KEY -out $CACERT -days $DAYS -subj "/C=${CO_S}/ST=${ST_S}/L=${LO_S}/O=${OG_S}/OU=${OU_S} Department/CN=$HS_S" >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Self Sign ROOT CA in $CACERT \033[033msuccess\033[0m"
else
echo "please check the error og under /tmp/CA/$ERRLOG"
exit 2
fi
}
CSR_P(){
[ -e $CSR_pem ] && rm -f $CSR_pem
openssl req -new -passin pass:$PASSWORD -key $P_KEY -out $CSR_pem -subj "/C=${CO_C}/ST=${ST_C}/L=${LO_C}/O=${OG_C}/OU=${OU_C} Department/CN=${HS_C}" >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Create the CSR under $CSR_pem \033[033msuccess\033[0m"
else
echo "please check the error log under /tmp/CA/$ERRLOG"
exit 3
fi
}
Cat_CA(){
[ -e /root/ca.config ] && rm -f /root/ca.config
cat > /root/ca.config <<EOF
[ ca ]
default_ca = CA_own
[ CA_own ]
dir = /etc/pki/CA
certs = \$dir/certs
new_certs_dir = \$dir/newcerts
database = \$dir/index.txt
serial = \$dir/serial
RANDFILE = \$dir/private/.rand
certificate = \$dir/cacert.pem
private_key = \$dir/private/cakey.pem
default_days = 3650
default_crl_days = 30
default_md = md5
preserve = no
policy = policy_anything
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[req]
x509_extensions =v3_ca
[v3_ca]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
EOF if [ $? -eq 0 ]; then
echo -e "Create configuration file on /root/ca.config \033[033msuccess\033[0m"
else
echo -e "Create configuration file on /root/ca.config \033[031failed\033[0m"
fi
}
Sign_v3(){
openssl req -in $temp -noout -text >> /tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ] ;then
Cat_CA
[ ! -f $DIR_F/index.txt ] && touch $DIR_F/index.txt
if [ ! -f $DIR_F/serial ];then
touch $DIR_F/serial
echo 01 > $DIR_F/serial
fi
openssl ca -extensions v3_ca -passin pass:$PASSWORD -in $temp -config /root/ca.config -days $S_DAYS -out ${temp}_V3.crt -batch >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Create the cert file on ${temp}_V3.crt \033[033msuccess\033[0m"
else
echo -e "Cannot Create the Cert file. Please check the error under /tmp/CA/$ERRLOG"
exit 4
fi
else
echo -e "\033[32mYou must assign a CSR file with parameter -s3|--sign3\033[0m"
exit 5
fi
}
Trans_key(){
openssl rsa -passin pass:$PASSWORD -noout -text -in $temp > /dev/null 2>/tmp/CA/$ERRLOG
if [ $? -eq 0 ];then
openssl rsa -passin pass:$PASSWORD -in $temp -out ${temp}.key
echo -e "Create the ${temp}.key \033[033msuccess\033[0m"
else
echo -e "\033[32mYou must assign a CSR file with parameter -t|--trans\033[0m"
exit 5
fi }
Sign_CSR(){
openssl req -in $temp -noout -text >> /tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ]; then
[ ! -f $DIR_F/index.txt ] && touch $DIR_F/index.txt
if [ ! -f $DIR_F/serial ];then
touch $DIR_F/serial
echo 01 > $DIR_F/serial
fi
openssl x509 -req -in $temp -CA $CACERT -CAkey $P_KEY -CAcreateserial -passin pass:$PASSWORD -out $temp.crt >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Create the cert file on $temp.crt \033[033msucess\033[0m"
else
echo -e "Cannot Create the Cert file. Please check the error under /tmp/CA/$ERRLOG"
exit 4
fi
else
echo -e "\033[32mYou must assign a CSR file with parameter -s|--sign\033[0m"
exit 6
fi }
Revoke_cert (){
if [ -f $temp ];then
openssl ca -revoke $temp >>/tmp/CA/$ERRLOG 2>&1
if [ $? -eq 0 ];then
echo -e "Revoke the cert file $temp \033[033msucess\033[0m"
else
echo -e "Cannot Revoke the cert file,please check the /tmp/CA/$ERRLOG"
exit 4
fi
else
echo -e "\033[32mYou must assign a Cert file with parameter -e|--revoke\033[0m"
exit 6
fi
} help_P(){
echo -e "\033[34mUsage: `basename $0`\033[031m [option] [file]\033[0m"
echo -e " \033[32m-h|--help : usage for help page\033[0m"
echo -e " \033[32m-c|--ca: usage to sign self root CA certification\033[0m"
echo -e " \033[32m-p|--private: usage to generate a private key\033[0m"
echo -e " \033[32m-r|--request: usage to create a CSR request\033[0m"
echo -e " \033[32m-s|--sign: usage to sign a Certification\033[0m"
echo -e " \033[32m-s3|--sign3: usage to sign v3 Certification\033[0m"
echo -e " \033[32m-v|--verify: usage to verify a private key\033[0m"
echo -e " \033[32m-i|--identify: usage to verify a CSR file\033[0m"
echo -e " \033[32m-k|--check: usage to verify a Certication file\033[0m"
echo -e " \033[32m-t|--trans: usage to trans a private key with pass to no pass\033[0m"
exit 0
}
while [ $# -ge 1 ]; do
case $1 in
-p|--private)
genPriKey
shift
;;
-c|--ca)
selfSign
shift
;;
-r|--request)
CSR_P
shift
;;
-s3|--sign3)
temp=$2
Sign_v3
rm /root/ca.config
shift 2
;;
-s|--sign)
temp=$2
Sign_CSR
shift 2
;;
-v|--verfiy)
temp=$2
temp=${temp:-null}
V_Key
shift 2
;;
-i|--identify)
temp=$2
temp=${temp:-null}
V_Csr
shift 2
;;
-k|--check)
temp=$2
temp=${temp:-null}
V_Cert
shift 2
;;
-e|--revoke)
temp=$2
Revoke_cert
shift 2
;;
-h|--help)
help_P
shift
;;
-t|--trans)
temp=$2
Trans_key
shift 2
;;
*)
echo -e "\033[34mUsage: `basename $0` -h|--help for help\033[0m"
exit 0
;;
esac
done [ -e /tmp/CA/$ERRLOG ] && rm /tmp/CA/$ERRLOG
默认配置
############configuration for the server side###########
##define private Key password
PASSWORD=1234
##define the valid days
DAYS=3650
##define the DN info
CO_S=CN
ST_S=BJ
LO_S=BJ
OG_S=Centos
OU_S=Linux
HS_S=centos.example.com
##define the Private Key Path
P_Key=/etc/pki/CA/private/cakey.pem
##define the CA Path
CACERT=/etc/pki/CA/cacert.pem ############configuration for the request CSR############
##define the DN info########
CO_C=CN
ST_C=LN
LO_C=DL
OG_C=server
OU_C="Operation System"
HS_C=server.example.com
CA签发工具的更多相关文章
- 使用CA签发的服务器证书搭建Tomcat双向SSL认证服务
第一部分,先说证书的申请. 这步是要到正规的CA公司申请正式的设备证书必须走的步骤. 1.先生成证书的密钥对 打开命令行,切换到某个自己新建的目录下,执行如下命令 keytool -genkey -k ...
- openssl 自建CA签发证书 网站https的ssl通信
<<COMMENTX509 文件扩展名 首先我们要理解文件的扩展名代表什么.DER.PEM.CRT和CER这些扩展名经常令人困惑.很多人错误地认为这些扩展名可以互相代替.尽管的确有时候有些 ...
- 使用openssl模拟CA和CA证书的签发
使用openssl模拟CA和CA证书的签发 当使用ssl/tls进行加密通信时,必须要有数字证书.若通信只限制在局域网内,可以不向第三方机构申请签发证书,可以通过openssl模拟CA(Cer ...
- 使用keytool工具产生带根CA和二级CA的用户证书
使用keytool工具产生带根CA和二级CA的用户证书 1 生成根CA 1.1 生成根CA证书 根CA实际是一张自签CA,自签CA的使用者和颁发者都是它自己.使用下面的命令生成根证书,如果没有指定 ...
- 基于CFSSL工具创建CA证书,服务端证书,客户端证书
背景描述 在局域网中部署组件时,想要通过证书来实现身份的认证,确保通信的安全性,可以通过cfssl工具来进行CA证书,服务端证书,客户端证书的创建. 目录 背景描述 部署cfssl工具 下载,上传cf ...
- 基于 OpenSSL 的 CA 建立及证书签发 【转】
建立 CA 建立 CA 目录结构 按照 OpenSSL 的默认配置建立 CA ,需要在文件系统中建立相应的目录结构.相关的配置内容一般位于 /usr/ssl/openssl.cnf 内,详情可参见 c ...
- 基于 OpenSSL 的 CA 建立及证书签发
http://rhythm-zju.blog.163.com/blog/static/310042008015115718637/ 建立 CA 建立 CA 目录结构 按照 OpenSSL 的默认配置建 ...
- 自建 CA 中心并签发 CA 证书
目录 文章目录 目录 CA 认证原理浅析 基本概念 PKI CA 认证中心(证书签发) X.509 标准 证书 证书的签发过程 自建 CA 签发证书并认证 HTTPS 网站的过程 使用 OpenSSL ...
- 通过Go语言创建CA与签发证书
本篇文章中,将描述如何使用go创建CA,并使用CA签署证书.在使用openssl创建证书时,遵循的步骤是 创建秘钥 > 创建CA > 生成要颁发证书的秘钥 > 使用CA签发证书.这种 ...
随机推荐
- Mysql 声明变量
Mysql 声明变量 Mysql中声明变量有两种方式 第一种: set @num=1; 或set @num:=1; //这里要使用变量来保存数据,直接使用@num变量 第二种: select @num ...
- 树莓派(Raspberry Pi)日期时间不准的修正方法
在树莓派上,打date命令可以看到系统的日期时间: 后面的CST表示中国标准时间 小知识: 树莓派没有电池,断电后无法保存时间. 树莓派默认安装了NTP(Network Time Protocol)服 ...
- VB CreateObject转C#
C#调用方法.函数获取属性大致流程如下: System.Type oType = System.Type.GetTypeFromProgID("SomeClass"); objec ...
- 为什么C#中ref和out 关键字 ?
需求假设:现需要通过一个叫Swap的方法交换a,b两个变量的值.交换前a=1,b=2,断言:交换后a=2,b=1. 现编码如下: class Program { static void ...
- XFire最佳实践
前言:XFire是新一代WebService框架,同时也支持与Spring集成,帮助我们方便快速地在Spring框架中开发WebService应用. 本节主要介绍XFire+Spring集成的2种常用 ...
- AJAX-创建XMLHttpRequest对象
AJAX-创建XMLHttpRequest对象 1.XMLHttpRequest是AJAX的基础,所有现在浏览器都支持,用于在后台与服务器交换数据,也就意味着可以在不加载整个页面的情况下对整个页面进 ...
- Python 网络编程(二)
Python 网络编程 上一篇博客介绍了socket的基本概念以及实现了简单的TCP和UDP的客户端.服务器程序,本篇博客主要对socket编程进行更深入的讲解 一.简化版ssh实现 这是一个极其简单 ...
- (C++) LNK2019: unresolved external symbol.
Error 33 error LNK2019: unresolved external symbol "\xxx.obj yyy.Native 仔细看看错误信息,后来发现尽然是构造函数的一个 ...
- vuejs,router
接下来我们要做的是vue的路由处理,首先当然是安装: npm install vue-router 接下打开我们的main.js,引入我们vue-router,然后在告诉vue我们要使用我们的vue- ...
- Visual Studio 设置多核编译
1.选择一个指定项目右击 -> 属性 -> 配置属性 -> C/C++ -> 命令行 在附加选项中输入:/MP4 或者/MP8 等,后面的那个数字是指定CPU 核的数量,可以自 ...