使用Openssl 自签发IP证书

使用Openssl 自签发IP证书

日常交付项目中,总是有这样的场景,再使用一些融合通讯的业务时,需要HTTPS 环境,那就涉及到SSL 证书的签发

考虑到项目成本的问题,往往都是本地自签发IP 证书使用;使用openssl生成根证书,签发服务端证书,安装根证书使浏览器信任自签证书。

  • 环境: centos7.9
  • IP: 172.16.10.110 111.111.111.111

创建证书脚本:ssl.sh

[root@all ~]# mkdir ssl
[root@all ~]# cd ssl
[root@all ssl]# vim ssl.sh

证书脚本内容:

#!/bin/sh

# Generate the openssl configuration files.
echo "创建openssl.cnf------------------->"

cat > openssl.cnf << EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
[req_distinguished_name]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = HN
localityName = Locality Name (eg, city)
localityName_default = ZZ
organizationalUnitName  = Organizational Unit Name (eg, section)
organizationalUnitName_default  = tx
commonName = commonName
commonName_default = tx
commonName_max  = 64
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[alt_names]
IP.1 = 172.16.10.110
IP.2 = 111.111.111.111
EOF

echo "创建v3.ext------------------->"
cat > v3.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage=digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName=@alt_names
[alt_names]
IP.1 = 172.16.10.110
IP.2 = 111.111.111.111
EOF

echo "创建CA 根证书------------------------->"
echo "创建私钥 ca.key"
openssl genrsa -out ca.key 2048

echo "创建CA证书 ca.crt"
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt

echo "生成服务器证书----------------->"
echo "创建私钥 server.key"
openssl genrsa -out server.key 2048

echo "创建服务器证书请求文件 server.csr"
openssl req -new -days 3650 -key server.key -out server.csr -config openssl.cnf

echo "创建服务器证书 server.crt"
openssl x509 -days 3650 -req -sha256 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

echo "创建pem------------------------>"
cat server.crt server.key > server.pem

echo "创建p12----------------------->"
openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name "server"

签发证书

#!/bin/sh

# Generate the openssl configuration files.
echo "创建openssl.cnf------------------->"

cat > openssl.cnf << EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
[req_distinguished_name]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = HN
localityName = Locality Name (eg, city)
localityName_default = ZZ
organizationalUnitName  = Organizational Unit Name (eg, section)
organizationalUnitName_default  = tx
commonName = commonName
commonName_default = tx
commonName_max  = 64
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[alt_names]
IP.1 = 172.16.10.110
IP.2 = 111.111.111.111
EOF

echo "创建v3.ext------------------->"
cat > v3.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage=digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName=@alt_names
[alt_names]
IP.1 = 172.16.10.110
IP.2 = 111.111.111.111
EOF

echo "创建CA 根证书------------------------->"
echo "创建私钥 ca.key"
openssl genrsa -out ca.key 2048

echo "创建CA证书 ca.crt"
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt

echo "生成服务器证书----------------->"
echo "创建私钥 server.key"
openssl genrsa -out server.key 2048

echo "创建服务器证书请求文件 server.csr"
openssl req -new -days 3650 -key server.key -out server.csr -config openssl.cnf

echo "创建服务器证书 server.crt"
openssl x509 -days 3650 -req -sha256 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

echo "创建pem------------------------>"
cat server.crt server.key > server.pem

echo "创建p12----------------------->"
openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name "server"

签发好的证书

image-20230427093926460

安装根证书使浏览器信任自签证书。

把刚刚生成的server.crt 导入到本地

导入帮助手册: https://jingyan.baidu.com/article/ca41422fda393f5faf99ed0d.html

文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/8242

赞 (0)
郭靖的头像郭靖
上一篇 2023年5月25日 下午5:18
下一篇 2023年5月26日 下午5:29

相关推荐

  • IIS发布网站时报403错误的所有原因及解决方案

    在使用IIS的时候,如果遇到403相关的错误,我们往往束手无策,不知道是什么权限的原因。现总结如下,供以后参考。 1、403.1 – 执行访问被禁止 没有足够的执行权限,例如你访问的index.php文件,但是该文件并没有执行权限,那么浏览器在访问时就会出现该错误,然后你就需要去修改目录的权限了 2、403.2 – 读访问被禁止验证是…

    2022年6月21日
    3.2K00
  • 用 Nginx 禁止国外 IP 访问我的网站

    看了下 Nginx 的访问日志,发现每天有好多国外的 IP 地址来访问我的网站,并且访问的内容基本上都是恶意的。因此我决定禁止国外 IP 来访问我的网站。 想要实现这个功能有很多方法,下面我就来介绍基于 Nginx 的 ngx_http_geoip2 模块来禁止国外 IP 访问网站。 ①安装 geoip2 扩展依赖: ②下载 ngx_http_geoip2_…

    2023年4月24日
    1.7K00
  • ThinkPHP 出现“您浏览的页面暂时发生了异常!请稍后再试~”的解决方法

    ThinkPHP3.0 第一次运行入口文件,之后在修改Action只要是有数据库操作,就会出现“您浏览的页面暂时发生了错误!请稍后再试~”的系统提示错误。 解决办法是: 把项目里自动生成的runtime文件夹下~runtime.php文件删掉,再刷新浏览器页面就会正常显示数据库提取的信息

    2024年2月28日
    3.2K00
  • sl认证 Processing, The CA is processing your order, please just wait.

    执行操作: 如果ssl认证报如下错误 Processing, The CA is processing your order, please just wait. (29/30) 请添加 注意:如果是centos系统一定要关闭防火墙:systemctl stop firewalld.service

    2023年3月11日
    2.4K00
  • Centos7.6下宝塔安装及资产管理系统部署

    一:宝塔安装 1、连接linux服务器 2、执行命令开始安装 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ed8484bec 安装成功出现下方界面 3、访问外网地址…

    2022年6月8日
    6.0K90

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

在线咨询: QQ交谈

邮件:712342017@qq.com

工作时间:周一至周五,8:30-17:30,节假日休息

关注微信