为域名快速生成证书

为域名快速生成证书

1. 下面这个脚本不能,有 bug

会出现这种问题:

x509 cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs.

原因是脚本里面的命令没有把 IP 地址写到证书里面。

若想生成没有错误的证书可以参考这个: https://blog.csdn.net/wan212000/article/details/120762210

2. 用法

./gencert.sh example.com

3. 脚本 gencert.sh

#!/usr/bin/env bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# example: ./gencert.sh example.com

set -e

DOMAIN="$1"
WORK_DIR="$(mktemp -d)"

if [ -z "$DOMAIN" ]; then
  echo "Domain name needed."
  exit 1
fi

echo "Temporary working dir is $WORK_DIR "
echo "Gernerating cert for $DOMAIN ..."

#
# Fix the following error:
# --------------------------
# Cannot write random bytes:
# 139695180550592:error:24070079:random number generator:RAND_write_file:Cannot open file:../crypto/rand/randfile.c:213:Filename=/home/eliu/.rnd
#
[ -f $HOME/.rnd ] || dd if=/dev/urandom of=$HOME/.rnd bs=256 count=1

openssl genrsa -out $WORK_DIR/ca.key 4096

openssl req -x509 -new -nodes -sha512 -days 3650 \
  -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=$DOMAIN" \
  -key $WORK_DIR/ca.key \
  -out $WORK_DIR/ca.crt

openssl genrsa -out $WORK_DIR/server.key 4096

openssl req -sha512 -new \
  -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=$DOMAIN" \
  -key $WORK_DIR/server.key \
  -out $WORK_DIR/server.csr

cat > $WORK_DIR/v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=$DOMAIN
DNS.2=*.$DOMAIN
EOF

openssl x509 -req -sha512 -days 3650 \
  -extfile $WORK_DIR/v3.ext \
  -CA $WORK_DIR/ca.crt -CAkey $WORK_DIR/ca.key -CAcreateserial \
  -in $WORK_DIR/server.csr \
  -out $WORK_DIR/server.crt

openssl x509 -inform PEM -in $WORK_DIR/server.crt -out $WORK_DIR/$DOMAIN.cert

mkdir -p ./$DOMAIN
cp $WORK_DIR/ca.key $WORK_DIR/ca.crt $WORK_DIR/server.key $WORK_DIR/server.crt ./$DOMAIN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 macOS 上,你可以使用 `mkcert` 工具来生成自签名的证书。`mkcert` 是一个简单易用的工具,可以快速生成本地开发环境所需的证书。以下是使用 `mkcert` 工具生成证书的步骤: 1. 安装 `mkcert` 工具:你可以使用 Homebrew 包管理器来安装 `mkcert`。打开终端,并执行以下命令: ``` brew install mkcert ``` 2. 创建证书存储库:在终端中执行以下命令来创建一个新的证书存储库: ``` mkcert -install ``` 这将在系统中创建一个新的证书存储库,并生成一个根证书。 3. 生成证书:在终端中,导航到你想要生成证书的目录,并执行以下命令: ``` mkcert localhost ``` 这将生成一个名为 `localhost.pem` 的证书文件和一个名为 `localhost-key.pem` 的私钥文件。 现在,你可以在本地开发环境中使用生成证书文件来启用 HTTPS。例如,如果你使用 Node.js 的 Express 框架,可以像这样设置 HTTPS 服务器: ```javascript const fs = require('fs'); const https = require('https'); const express = require('express'); const app = express(); const options = { key: fs.readFileSync('localhost-key.pem'), cert: fs.readFileSync('localhost.pem') }; https.createServer(options, app).listen(3000, () => { console.log('Server running on https://localhost:3000'); }); ``` 请注意,`mkcert` 工具默认只会生成针对 `localhost` 域名证书。如果你需要为其他域名生成证书,可以在命令中指定多个域名,例如 `mkcert localhost mydomain.com`。 希望这个指南对你有帮助!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云满笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值