首页 > 其他分享 >TLS 证书生成方法

TLS 证书生成方法

时间:2023-08-12 16:22:36浏览次数:37  
标签:TLS 证书 ca 生成 CertPath CertPD key server

##############################################


#!/bin/bash

function tls3.encry.ext(){
# 签发加密类型的X509证书文件
####################################################
#
# 创建CA(X509 version 3.0)加密根证书
#
####################################################

CertPath=/k8s/tlsv3
CertPD=huawei@123
DomainName=ca.huawei.com

#1、创建证书存放目录
mkdir -p ${CertPath} && cd ${CertPath}


#2、创建CA证书的私钥"cacert-key.pem"
openssl genrsa -des3 -out  ${CertPath}/ca.key -passout pass:${CertPD} 2048


#3、生产X509 Version3类型证书
openssl req -x509 -new -nodes \
-key  ${CertPath}/ca.key \
-sha256 \
-subj "/C=CN/ST=GuangDong/L=ShenZhen/O=HW/OU=IT/CN=${DomainName}" \
-days 7300 \
-out ${CertPath}/ca.crt \
-passin pass:${CertPD}

# 4、查看证书文件
openssl x509 -in  ${CertPath}/ca.crt  -text -noout


#####################################################
#
# 生成X509 3.0加密证书
# CA签署的服务器证书
#
#####################################################

# 服务器证书存放路径,需与CA证书存放路径保持一致
CertPath=/k8s/tlsv3
# 证书明文密码
CertPD=huawei@123
# 服务器证书域名
DomainName=www.huawei.com


# 1、创建服务器证书的私钥"server.key"
openssl genrsa -des3 -out ${CertPath}/server.key  -passout pass:${CertPD} 2048

# 2、创建服务器证书请求文件"server.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=ShenZhen/O=HW/OU=IT/CN=${DomainName}" \
-key ${CertPath}/server.key \
-out ${CertPath}/server.csr \
-passin pass:${CertPD}

# 3、创建证书扩展文件"my-ssl.conf"
# 更改相应IP和DNS地址
#
cat > ${CertPath}/my-ssl.conf <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster
DNS.5 = kubernetes.default.svc.cluster.local
DNS.6 = www.huawei.com
DNS.7 = localhost
IP.1 = 168.7.10.201
IP.2 = 168.7.10.202
IP.3 = 168.7.10.203
IP.4 = 168.7.10.204
IP.4 = 127.0.0.1
EOF

# 4、签发X509 v3.0证书文件
openssl x509 -req \
-in ${CertPath}/server.csr \
-out ${CertPath}/server.crt \
-days 3650 \
-CAcreateserial \
-CA ${CertPath}/ca.crt \
-CAkey ${CertPath}/ca.key \
-CAserial serial \
-extfile ${CertPath}/my-ssl.conf \
-passin pass:${CertPD}


# 5、查看证书文件
openssl x509 -in ${CertPath}/server.crt -text -noout
chmod 777 ${CertPath}/ca* ${CertPath}/ser* && ls -l ${CertPath}/

}



function tls1{
#
# 签发X509 V1.0加密证书,不支持扩展属性
#####################################################
#
#  创建CA X509 version 1.0根证书
#  X509 1.0 无扩展属性
#
#####################################################

#创建证书存放目录
CertPath=/k8s/tlsv1
DomainName=ca.huawei.com

# 1、创建证书文件存放目录
mkdir -p ${CertPath} && cd ${CertPath}
     
# 2、创建CA证书的私钥"ca.key"
openssl genrsa -out  ${CertPath}/ca.key

# 3、 创建CA证书请求"ca.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=DongGuan/O=HW/OU=IT/CN=${DomainName}"  \
-key  ${CertPath}/ca.key \
-out  ${CertPath}/ca.csr

# 4、 创建3年有效期的CA证书"ca.crt"
openssl x509 -req \
-days  3650 \
-in ${CertPath}/ca.csr \
-signkey ${CertPath}/ca.key \
-out ${CertPath}/ca.crt

# 5、查看证书文件
openssl x509 -in ${CertPath}/ca.crt -text -noout
chmod 777 ${CertPath}/ca* && ls -l ${CertPath}/


#####################################################
# 
# 生成X509 1.0证书
# CA签署的服务器证书
#
#####################################################

CertPath=/k8s/tlsv1
ServerName=server
DomainName=huawei.com

# 1、创建服务证书的私钥"xxx.key"
openssl genrsa -out ${CertPath}/${ServerName}.key


# 2、创建服务器证书请求文件 "xxx.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=DongGuan/O=HW/OU=IT/CN=${ServerName}.${DomainName}"  \
-key ${CertPath}/${ServerName}.key \
-out ${CertPath}/${ServerName}.csr

# 3、CA签署X509 V1.0服务器证书,有效期3年,即: "xxx.crt"
openssl x509 -req \
-in  ${CertPath}/${ServerName}.csr \
-out  ${CertPath}/${ServerName}.crt \
-days 3650 \
-CAcreateserial -CA ${CertPath}/ca.crt \
-CAkey ${CertPath}/ca.key

# 4、查看证书文件
openssl x509 -in  ${CertPath}/${ServerName}.crt  -text -noout
chmod 777 ${CertPath}/${ServerName}.* && ls -l ${CertPath}/

}

function tls3.ext(){

#####################################################
#
# 创建CA X509 version 1.0根证书
#
#####################################################

#创建证书存放目录
CertPath=/k8s/tlsv2
DomainName=ca.huawei.com

# 1、创建证书文件存放目录
mkdir -p ${CertPath} && cd ${CertPath}
     
# 2、创建CA证书的私钥"ca.key"
openssl genrsa -out  ${CertPath}/ca.key

# 3、 创建CA证书请求"ca.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=DongGuan/O=HW/OU=IT/CN=${DomainName}"  \
-key  ${CertPath}/ca.key \
-out  ${CertPath}/ca.csr

# 4、 创建3年有效期的CA证书"ca.crt"
openssl x509 -req \
-days  3650 \
-in ${CertPath}/ca.csr \
-signkey ${CertPath}/ca.key \
-out ${CertPath}/ca.crt

# 5、查看证书文件
openssl x509 -in ${CertPath}/ca.crt -text -noout
chmod 777 ${CertPath}/ca* && ls -l ${CertPath}/


#####################################################
# 
# 生成X509 3.0证书,证书key文件不加密
# CA签署的服务器证书
#
#####################################################

ServerName=server
DomainName=huawei.com

# 1、创建服务证书的私钥"xxx.key"
openssl genrsa -out ${CertPath}/${ServerName}.key


# 2、创建服务器证书请求文件 "xxx.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=DongGuan/O=HW/OU=IT/CN=${ServerName}.${DomainName}"  \
-key ${CertPath}/${ServerName}.key \
-out ${CertPath}/${ServerName}.csr


# 3、创建证书扩展文件
cat > ${CertPath}/my-ssl.conf <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster
DNS.5 = kubernetes.default.svc.cluster.local
DNS.6 = www.huawei.com
DNS.7 = localhost
IP.1 = 168.7.10.201
IP.2 = 168.7.10.202
IP.3 = 168.7.10.203
IP.4 = 168.7.10.204
IP.4 = 127.0.0.1
EOF


# 4、签发X509 3.0服务器证书文件
openssl x509 -req \
-in  ${CertPath}/${ServerName}.csr \
-out  ${CertPath}/${ServerName}.crt \
-days 3650 \
-CAcreateserial -CA ${CertPath}/ca.crt \
-CAkey ${CertPath}/ca.key \
-CAserial serial \
-extfile ${CertPath}/my-ssl.conf




# 4、签发X509 1.0服务器证书文件,即: "xxx.crt"
# openssl x509 -req \
# -in  ${CertPath}/${ServerName}.csr \
# -out  ${CertPath}/${ServerName}.crt \
# -days 3650 \
# -CAcreateserial -CA ${CertPath}/ca.crt \
# -CAkey ${CertPath}/ca.key

# 5、查看证书文件
openssl x509 -in  ${CertPath}/${ServerName}.crt  -text -noout
chmod 777 ${CertPath}/${ServerName}.* && ls -l ${CertPath}/
}

 


function tls3.encry.ext(){

####################################################
#
# 创建CA(X509 version 3.0)加密根证书
#
####################################################

CertPath=/k8s/tlsv4
CertPD=huawei@123
DomainName=ca.huawei.com

#1、创建证书存放目录
mkdir -p ${CertPath} && cd ${CertPath}


#2、创建CA证书的私钥"cacert-key.pem"
openssl genrsa -des3 -out  ${CertPath}/ca.key -passout pass:${CertPD} 2048


#3、生产X509 Version3类型证书
openssl req -x509 -new -nodes \
-key  ${CertPath}/ca.key \
-sha256 \
-subj "/C=CN/ST=GuangDong/L=ShenZhen/O=HW/OU=IT/CN=${DomainName}" \
-days 7300 \
-out ${CertPath}/ca.crt \
-passin pass:${CertPD}

# 4、查看证书文件
openssl x509 -in  ${CertPath}/ca.crt  -text -noout


#####################################################
#
# 生成X509 3.0证书,证书key文件不加密
# CA签署的服务器证书
#
#####################################################

# 服务器证书存放路径,需与CA证书存放路径保持一致

CertPath=/k8s/tlsv4
# 证书明文密码
CertPD=huawei@123
# 服务器证书域名
DomainName=www.huawei.com


# 1、创建服务器证书的私钥"server.key"
openssl genrsa -out ${CertPath}/server.key 2048

# 2、创建服务器证书请求文件"server.csr"
openssl req -new \
-subj "/C=CN/ST=GuangDong/L=ShenZhen/O=HW/OU=IT/CN=${DomainName}" \
-key ${CertPath}/server.key \
-out ${CertPath}/server.csr \


# 3、创建证书扩展文件"my-ssl.conf"
# 更改相应IP和DNS地址
#
cat > ${CertPath}/my-ssl.conf <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster
DNS.5 = kubernetes.default.svc.cluster.local
DNS.6 = www.huawei.com
DNS.7 = localhost
IP.1 = 168.7.10.201
IP.2 = 168.7.10.202
IP.3 = 168.7.10.203
IP.4 = 168.7.10.204
IP.4 = 127.0.0.1
EOF

# 4、签发X509 v3.0证书文件
openssl x509 -req \
-in ${CertPath}/server.csr \
-out ${CertPath}/server.crt \
-days 3650 \
-CAcreateserial \
-CA ${CertPath}/ca.crt \
-CAkey ${CertPath}/ca.key \
-CAserial serial \
-extfile ${CertPath}/my-ssl.conf \
-passin pass:${CertPD}


# 5、查看证书文件
openssl x509 -in ${CertPath}/server.crt -text -noout
chmod 777 ${CertPath}/ca* ${CertPath}/ser* && ls -l ${CertPath}/

}


***********************************************************************************************************
*
***********************************************************************************************************

 

标签:TLS,证书,ca,生成,CertPath,CertPD,key,server
From: https://www.cnblogs.com/vmsysjack/p/17624967.html

相关文章

  • ICCV 2023 | Actformer:从单人到多人,迈向更加通用的3D人体动作生成
    前言 本文的主要贡献为:1.提出了基于GAN和Transformer混合架构的通用3D人体动作生成框;2.不仅能够实现单人动作生成,还能拓展到多人交互式动作生成;3.基于GTA游戏引擎构造了一个合成的多人打架数据集,包括2~5个人同时交互,现已开源。本文转载自PaperWeekly作者|徐良仅用......
  • 2023-08-12 记录一则随机密码生成脚本
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=......
  • X509 TLS
    !/bin/bashfunctiontls3(){######################################################创建CAX509version3.0根证书#####################################################CertPath=/k8s/tlsv3CertPD=huawei@123DomainName=ca.huawei.com#1、创建证书存放目录......
  • 3.0 Python 迭代器与生成器
    当我们需要处理一个大量的数据集合时,一次性将其全部读入内存并处理可能会导致内存溢出。此时,我们可以采用迭代器Iterator和生成器Generator的方法,逐个地处理数据,从而避免内存溢出的问题。迭代器是一个可以逐个访问元素的对象,它实现了python的迭代协议,即实现了__iter__()和__next_......
  • 3.0 Python 迭代器与生成器
    当我们需要处理一个大量的数据集合时,一次性将其全部读入内存并处理可能会导致内存溢出。此时,我们可以采用迭代器Iterator和生成器Generator的方法,逐个地处理数据,从而避免内存溢出的问题。迭代器是一个可以逐个访问元素的对象,它实现了python的迭代协议,即实现了__iter__()和__next__......
  • 最小生成树模板
    prim算法算法思想:每次选定未进入集合中和集合距离最小的点,用该点更新其他点到集合的距离,直到可以判断出不存在最小生成树或所有点均已进入集合下面虽然是两种写法,但是记忆时最好还是按照算法的思路来实现,也就是第2个代码。虽然会多一些边界处理,但是只要我们理解了算法思想即使......
  • 设置随机数的生成起点
    代码结果展示......
  • emmet快速生成html标签和css样式
    emmet快速生成html标签语法1.生成标签,直接输入标签名,按下tab键即可;2.生成多个相同标签,加上即可,如生成3个div标签,div3;3.生成父子级的标签,使用>号,如ul>li;4.生成兄弟标签,使用+号,如div+p;5.生成带有类名或者id名的标签,直接写.demo或者#id按下tab键即可;6.如果生成的div类名是......
  • 视频生成缩略图或pdf文件生成缩略图
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingMicrosoft.WindowsAPICodePack.Shell;usingSystem.Drawing;//usingPdfiumViewer;usingGhostscriptSharp;//usingSystem.IO;//usingiTextSharp;usingSyst......
  • 6529: 构造完全图 最小生成树
    描述 对于完全图G,若有且仅有一棵最小生成树为T,则称完全图G是树T的扩展出的。给你一棵树T,找出T能扩展出的边权和最小的完全图G。 输入 第一行N表示树T的点数。接下来N-1行:Si,Ti,Di;描述一条边(Si,Ti)权值为Di。保证输入数据构成一棵树。对于20%的数据,N<=10对于50%的......