ssl安全证书制作过程

#!/bin/bash

function read_node_info_config

{

key=$1

grep -v '^#' crt_node_info.conf | grep $key | cut -d'=' -f2

}

function check_config

{

test -n "$2" || { test -n "$3" && { echo "Set $1 to default value $3"; eval "$1=$3"; } || { echo "Missing $1 config"; exit 9; } }

}

function create_xxx_ca

{

create ca.key

openssl genrsa -aes256 -passout pass:${CA_pass} -out ${ca_dir}/ca.key 2048

openssl req -subj /C=CN/ST=ZJ/L=HZ/O=companyname/OU=DT/CN=${project_name}/emailAddress=ca@xxx.com -new -key {ca_dir}/ca.key -passin pass:{CA_pass} -out ${ca_dir}/ca.req

create ca.crt

openssl x509 -req -in ${ca_dir}/ca.req -signkey {ca_dir}/ca.key -passin pass:{CA_pass} -days 3650 -out ${ca_dir}/ca.crt

}

function init_env

{

if [ ! -r ./crt_node_info.conf ]; then

echo "Create crt_node_info.conf first."

return 1

fi

crt_node_type=`read_node_info_config crt_node_type`

project_name=`read_node_info_config project_name`

xxx_type=`read_node_info_config xxx_type`

node_names=`read_node_info_config node_name`

prefix_name=`read_node_info_config prefix_name`

name_count=`read_node_info_config name_count`

crt_expiration_days=`read_node_info_config crt_expiration_days`

CA_pass=`read_node_info_config CA_pass`

CA_path=`read_node_info_config CA_path`

cert_ST=`read_node_info_config cert_ST`

cert_L=`read_node_info_config cert_L`

cert_O=`read_node_info_config cert_O`

cert_CN=`read_node_info_config cert_CN`

#cert_Email=`read_node_info_config cert_Email`

cert_Title=`read_node_info_config cert_Title`

check_config CA_pass "$CA_pass" "123xxx"

check_config prefix_name "prefix_name" "crt_node_type"

check_config name_count "$name_count" "0"

cert_Email="@xxxpri.com"

if [ ${xxx_type} == "XXX_Public" ]; then

cert_Email="@xxxpub.com"

fi

#parse node names to a list

OLD_IFS="$IFS"

IFS=","

node_name_list=(${node_names})

IFS="$OLD_IFS"

echo ${node_name_list[*]}

ca_dir=${CA_path}

if [ ! -f ${ca_dir}/ca.key ]; then

if [ ! -d ${ca_dir} ]; then

mkdir -p ${ca_dir} >/dev/null 2>>/dev/null

fi

create_xxx_ca

if [ $? -ne 0 ]; then

echo "create xxx ca failed."

return 1

fi

fi

return 0

}

function create_xxx_server_cert

{

cert_dir=`pwd`

create server.key

openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -out ${cert_dir}/xx_server_$1.key

create csr file

openssl req -subj /C=CN/ST={cert_ST}/L={cert_L}/O={cert_O}/OU={crt_node_type}/CN=$1/emailAddress=1{cert_Email} -new -key ${cert_dir}/xx_server_$1.key -out ${cert_dir}/xx_server_$1.csr

create server.crt

openssl x509 -req -days ${crt_expiration_days} -in ${cert_dir}/xx_server_$1.csr -CA ${ca_dir}/ca.crt -CAkey {ca_dir}/ca.key -CAcreateserial -passin pass:{CA_pass} -out ${cert_dir}/xx_server_$1.crt

}

function create_chain_node_cert

{

chain_cert_dir=`pwd`

create chain_node.key

openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -out ${chain_cert_dir}/tn_client_chain_node_$1.key

create csr file

openssl req -subj /C=CN/ST={cert_ST}/L={cert_L}/O={cert_O}/OU={crt_node_type}/title=${cert_Title}/CN=$1/emailAddress=1{cert_Email} -new -key ${chain_cert_dir}/xx_client_chain_node_$1.key -out ${chain_cert_dir}/xx_client_chain_node_$1.csr

create chain_node.crt

openssl x509 -req -days ${crt_expiration_days} -in ${chain_cert_dir}/xx_client_chain_node_$1.csr -CA ${ca_dir}/ca.crt -CAkey {ca_dir}/ca.key -CAcreateserial -passin pass:{CA_pass} -out ${chain_cert_dir}/xx_client_chain_node_$1.crt

}

function create_xxx_agent_cert

{

ba_cert_dir=`pwd`

create ect key

openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -out ${ba_cert_dir}/xx_client_ba_$1.key

create csr file

openssl req -subj /C=CN/ST={cert_ST}/L={cert_L}/O={cert_O}/OU={crt_node_type}/title=${cert_Title}/CN=$1/emailAddress=1{cert_Email} -new -key ${ba_cert_dir}/xx_client_ba_$1.key -out ${ba_cert_dir}/xx_client_ba_$1.csr

create ect crt

openssl x509 -req -days ${crt_expiration_days} -in ${ba_cert_dir}/xx_client_ba_$1.csr -CA ${ca_dir}/ca.crt -CAkey {ca_dir}/ca.key -CAcreateserial -passin pass:{CA_pass} -out ${ba_cert_dir}/xx_client_ba_$1.crt

}

function main {

1 init dir, var, CA

echo "1) init environment."

init_env

if [ $? -ne 0 ]; then

return 1

fi

echo "2) create crt..."

#xxx_server, xxx_agent, chain_node;

if [ ${crt_node_type} == "xxx_server" ]; then

for((i=0; i<${#node_name_list[@]}; ++i)); do

create_xxx_server_cert {node_name_list\[{i}]}

if [ $? -ne 0 ]; then

echo "create {node_name_list\[{i}]} crt failed."

fi

done

elif [ ${crt_node_type} == "chain_node" ]; then

for((i=0; i<${#node_name_list[@]}; ++i)); do

create_chain_node_cert {node_name_list\[{i}]}

if [ $? -ne 0 ]; then

echo "create {node_name_list\[{i}]} crt failed."

fi

done

elif [ ${crt_node_type} == "xxx_agent" ]; then

if [ ${name_count} -gt 0 ]; then

for((i=1; i<=${name_count}; ++i)); do

node_name={prefix_name}i

create_xxx_agent_cert ${node_name}

if [ $? -ne 0 ]; then

echo "create ${node_name} crt failed."

fi

done

else

for((i=0; i<${#node_name_list[@]}; ++i)); do

create_xxx_agent_cert {node_name_list\[{i}]}

if [ $? -ne 0 ]; then

echo "create {node_name_list\[{i}]} crt failed."

fi

done

fi

fi

echo "create crt completed."

return 0

}

main $*

相关推荐
枫叶丹418 分钟前
【HarmonyOS之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(三)
开发语言·前端·javascript·华为·harmonyos
酷爱码19 分钟前
HTML5+SVG+CSS3实现雪中点亮的圣诞树动画效果源码
前端·css3·html5
有杨既安然31 分钟前
Vue.js组件开发深度指南:从零到可复用的艺术
前端·javascript·vue.js·npm
萌新小码农‍1 小时前
回顾:Maven的环境搭建
java·maven
gentle_ice2 小时前
leetcode——删除链表的倒数第N个节点(java)
java·leetcode·链表
小韩学长yyds2 小时前
前端实战:小程序搭建商品购物全流程
javascript·css·vue.js·小程序·前端框架·node.js·html5
wanghao6664552 小时前
Java基础面试题总结(题目来源JavaGuide)
java·开发语言
HYUJKI3 小时前
自定义注解
java·开发语言·spring
bing_1583 小时前
【某大厂一面】ThreadLocal如何实现主子线程之间的数据同步
java
gentle coder3 小时前
JVM_程序计数器的作用、特点、线程私有、本地方法的概述
java·jvm