我想使用 SHA256 或 SHA512 生成自签名证书,但我遇到了问题。我创建了一个脚本,它应该自动执行此操作:
#!/bin/bash
set -e
echo "WORKSPACE: $WORKSPACE"
SSL_DIR=$(pwd)/httpd_ssl_certs
OPENSSL_CNF=$(pwd)/openssl.cnf
if [ -d "$SSL_DIR" ]; then
rm -rvf "$SSL_DIR"
fi
mkdir -vp "$SSL_DIR"
pushd "$SSL_DIR"
# check if openssl.cnf exists
if [ ! -f "$OPENSSL_CNF" ]; then
echo "Could not find $OPENSSL_CNF. Build will be exited."
exit 1
fi
echo " - create private key"
openssl genrsa -out server.key.template 2048
echo " - create signing request"
openssl req -nodes -new -sha256 -config $OPENSSL_CNF -key server.key.template -out server.csr.template
echo " - create certificate"
openssl x509 -req -in server.csr.template -signkey server.key.template -out server.crt.template -extfile $OPENSSL_CNF
我有一个openssl.cnf
包含其配置的文件:
[ ca ]
default_ca = CA_default
[ CA_default ]
# how long to certify
default_days = 365
# how long before next CRL
default_crl_days = 30
# use public key default MD
default_md = sha256
# keep passed DN ordering
preserve = no
policy = policy_anything
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = optional
emailAddress = optional
[ req ]
default_bits = 2048
default_keyfile = server.key.template
distinguished_name = req_distinguished_name
prompt = no
encrypt_key = no
# add default_md to [ req ] for creating certificates with SHA256
default_md = sha256
[ req_distinguished_name ]
countryName = "AB"
stateOrProvinceName = "CD"
localityName = "Some town"
organizationName = "XXX Y"
organizationalUnitName = "XXX Y"
commonName = "localhost"
emailAddress = "[email protected]"
当我使用这个 openssl.cnf 运行脚本时,我会得到一个证书,但该证书始终使用 SHA1 加密。我用这个命令检查了它:openssl x509 -in server.crt.template -text -noout | grep 'Signature
。我总是得到这样的输出:
Signature Algorithm: sha1WithRSAEncryption
Signature Algorithm: sha1WithRSAEncryption
有人可以给我提示吗,那里有什么假的?
答案1
以下是我关于使用 openssl 生成证书签名请求 (CSR)(尽管不是证书本身)的相关注释。
带有占位符的语法如下所示:
openssl req -new -newkey rsa:4096 -sha256 -nodes -out {DOMAIN.TLD}.rsa.csr -keyout {DOMAIN.TLD}.rsa.pkey -subj "/C={US}/ST={STATE| |普罗旺斯}/L={城市}/O={公司名称}/CN={域名.TLD}"
这正是我用来生成自己的 CSR 的命令,并将其提供给我的 SSL 证书提供商。
openssl req -new -newkey rsa:4096 -sha256 -nodes -out rustbeltrebellion.com.csr -keyout rustbeltrebellion.rsa.pkey -subj "/C=US/ST=OHIO/L=EASTLAKE/O=RUSTBELTREBELLION/CN=RUSTBELTREBELLION.COM"
CSR 的好处是您的私钥保持更加私密。我知道创建证书的方法只有两种——通过 openssl 库运行私钥或通过 openssl 库运行 CSR。当尘埃落定后,两者都会为您提供功能相同的证书。其中之一允许您的私钥保持私密(CSR 就是这样做的)。