Skip to content

Instantly share code, notes, and snippets.

@cismous
Forked from liuguangw/make_cert.md
Last active January 28, 2024 03:47
Show Gist options
  • Save cismous/8af4c751c09f3066cb123b7d2433272d to your computer and use it in GitHub Desktop.
Save cismous/8af4c751c09f3066cb123b7d2433272d to your computer and use it in GitHub Desktop.
使用openssl制作自定义CA、自签名ssl证书

自签名ssl证书生成

生成CA私钥

# 创建私钥
输入密码创建,RSA密钥长度不能小于 2048。
openssl genrsa -des3 -out ca.key 2048

生成如下

Generating RSA private key, 2048 bit long modulus
.............................................+++
................................+++
e is 65537 (0x010001)
Enter pass phrase for ca.key:
Verifying - Enter pass phrase for ca.key:

生成CA证书

# 20 年有效期
通过配置文件生成证书
openssl req -x509 -new -nodes -key ca.key -sha256 -days 7300 -out ca.crt -config ./ca.cnf

把此证书导入需要部署的PC中即可,以后用此CA签署的证书都可以使用

查看证书信息命令 openssl x509 -in ca.crt -noout -text

创建ssl证书私钥

cd ..
# 此文件夹存放待签名的证书
mkdir certs && cd certs
openssl genrsa -out domain.key 2048

输出信息

Generating RSA private key, 2048 bit long modulus

创建ssl证书CSR

openssl req -new -key domain.key -out domain.csr -config domain.cnf

使用CA签署ssl证书

# ssl证书有效期825天,自签名证书最长的有效期不能超过825天
openssl x509 -req -in domain.csr -out domain.crt -days 825 \
  -CAcreateserial -CA ca.crt -CAkey ca.key \
  -CAserial serial -extfile cert.ext

此步骤需要输入CA私钥的密码

其它

查看签署的证书信息

root@ubuntu:# openssl x509 -in domain.crt -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:

使用CA验证一下证书是否通过

root@ubuntu:# openssl verify -CAfile ca.crt domain.crt
localhost.crt: OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment