OpenSSL 自签名根 CA 证书:设置开始日期

OpenSSL Self-signed Root CA certificate: Set a start date

我正在使用以下设置(使用 OpenSSL 1.0.1 2012 年 3 月 14 日)创建一个带有自己的自签名证书的小型测试 CA。我遇到的问题是,如果我查看 CA 自己的证书的开始日期,它会为明天创建它(我想今天使用它)。

> openssl x509 -noout -startdate -enddate -in ~/my_little_ca/cacert.pem 
notBefore=Jan  2 16:05:52 2015 GMT
notAfter=Feb  1 16:05:52 2015 GMT

所以我仔细研究了一下,因为我在使用 CA 签署的证书上遇到了同样的问题。对于这些证书,我可以使用 --startdate 设置开始日期,但我看不到 CA 根证书的类似选项。我试过在生成 CA 时使用的 openssl 配置文件中使用 default_startdate,但由于某种原因似乎被忽略了?

我按如下方式创建 CA,它基本上几乎是逐字逐句地从 Pravir Chandra 等人的 "Network Security with OpenSSL" 中提取的,如下所示。

使用的命令是req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -verbose,但我想知道的是在生成根CA证书和私钥时如何设置开始日期? 提前谢谢你。

生成 CA 的完整脚本如下:

MYDIR=$(pwd -P)
BASEDIR=~/enigma_ca


mkdir -pv $BASEDIR
cd $BASEDIR

mkdir -pv private
chmod g-rwx,o-rwx private
mkdir -pv certs
touch index.txt
echo '01' > serial

DEFAULT_STARTDATE=$(date +'%y%m01000000Z')

cat <<EOF >openssl.cnf
[ ca ]
default_ca = my_test_ca

[ my_test_ca ]
certificate       = $BASEDIR/cacert.pem
database          = $BASEDIR/index.txt
new_certs_dir     = $BASEDIR/certs
private_key       = $BASEDIR/private/cakey.pem
serial            = $BASEDIR/serial

default_crl_days  = 7
default_days      = 356
default_md        = md5
default_startdate = $DEFAULT_STARTDATE

policy            = my_test_ca_policy
x509_extensions   = certificate_extensions

[ my_test_ca_policy ]
commonName              = supplied
stateOrProvinceName     = supplied
countryName             = supplied
emailAddress            = supplied
organizationName        = supplied
organizationalUnitName  = optional

[ certificate_extensions ]
basicConstraints  = CA:false

[ req ]
default_bits      = 2048
default_keyfile   = $BASEDIR/private/cakey.pem
default_md        = md5
default_startdate = $DEFAULT_STARTDATE
default_days      = 356

prompt              = no
distinguished_name  = root_ca_distinguished_name
x509_extensions     = root_ca_extensions

[ root_ca_distinguished_name ]
commonName           = My Mini CA
stateOrProvinceName  = Hampshire
countryName          = UK
emailAddress         = ca@myminica.com
organizationName     = My Mini CA Ltd

[ root_ca_extensions ]
basicConstraints = CA:true

EOF


OPENSSL_CONF=$BASEDIR/openssl.cnf
export OPENSSL_CONF

# Now generate self-signed certificate and generate key pair to go with it...
expect - <<EOF >> $MYDIR/ca_debug.txt
puts [concat "OPENSSL_CONF =" $::env(OPENSSL_CONF)]
spawn openssl req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -verbose
expect "PEM pass phrase:"
send "junk\r"
expect "PEM pass phrase:"
send "junk\r"
expect eof
EOF

只需在 openssl apps/req.capps/req.c

的第 843 行中将硬编码的零替换为以秒为单位的时移

https://github.com/openssl/openssl/blob/master/apps/req.c#L843

if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end; 

使用日期(现在 - 10 天)创建请求的示例:

if (!X509_gmtime_adj(X509_get_notBefore(x509ss),-10*24*3600)) goto end;

您可以使用 openssl ca-selfsign 选项来创建您的 CA 自签名证书。 此命令允许设置特定的 -startdate-enddate

例如:

  • 为您的 CA 创建私钥:

openssl genrsa -out cakey.pem 2048

  • 为此密钥创建 CSR:

openssl req -new -key cakey.pem -out ca.csr

  • 创建自签名证书

openssl ca -config openssl.cnf -selfsign -keyfile cakey.pem -startdate 20150214120000Z -enddate 20160214120000Z