自动创建颁发CA证书

#! /bin/bash

DOMAIN=test1.zerchin.xyz
DOMAIN_EXT=
IP=172.16.1.188
DATE=3650

## generate CA : cakey.pem && cacerts.pem
if [[ ! -e "cacerts.pem" || ! -e "cakey.pem" ]]
then
  openssl genrsa -out cakey.pem 2048
  openssl req -x509 -new -nodes -key cakey.pem -subj "/CN=zerchin" -days ${DATE} -out cacerts.pem 
fi


## generate server tls
mkdir ${DOMAIN}
openssl genrsa -out ${DOMAIN}/tls.key 2048

cat > ${DOMAIN}/csr.conf << EOF
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn
[ dn ]
C = CN
ST = GD
L = SZ
O = zerchin
OU = zerchin
CN = ${DOMAIN}
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
EOF
if [[ -n ${DOMAIN_EXT} ]]
then
    IFS=","
    DNS=(${DOMAIN})
    DNS+=(${DOMAIN_EXT})
    for i in ${!DNS[@]} 
    do
        echo DNS.${i} "=" ${DNS[$i]} >> ${DOMAIN}/csr.conf
    done
    echo DNS.
fi
if [[ -n ${IP} ]]
then
    IFS=","
    ip=(${IP})
    for i in ${!ip[@]} 
    do
        echo IP.${i} "=" ${ip[$i]} >> ${DOMAIN}/csr.conf
    done
    echo DNS.
fi
cat >> ${DOMAIN}/csr.conf << EOF
[ v3_ext ]
authorityKeyIdentifier=keyid,issuer:always
basicConstraints=CA:FALSE
keyUsage=keyEncipherment,dataEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=@alt_names
EOF


# 
openssl req -new -key ${DOMAIN}/tls.key -out ${DOMAIN}/tls.csr -config ${DOMAIN}/csr.conf

#
openssl x509 -req -in ${DOMAIN}/tls.csr -CA cacerts.pem  -CAkey cakey.pem \
  -CAcreateserial -out ${DOMAIN}/tls.crt -days ${DATE} \
  -extensions v3_ext -extfile ${DOMAIN}/csr.conf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值