首页 > 解决方案 > 更改 openssl 自签名证书的开始和结束日期

问题描述

我有这三个语句来生成带有我拥有的根证书的自签名证书。

openssl genrsa -out domain.org.key
openssl req -newkey rsa:2048 -nodes -keyout domain.org.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=*.domain.org" -out domain.org.csr
openssl x509 -req -extfile <(printf "subjectAltName=DNS:domain.org,DNS:www.domain.org") -days 1 -in domain.org.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out domain.org.crt

但是,在这里只能将有效期更改为一天。有谁知道我是否可以添加一个参数来让我将开始日期和结束日期更改为秒或分钟的粒度。出于测试目的,我需要创建几个有效期仅几分钟变化的证书。我查看了其他答案,他们建议使用以下ca命令:

openssl ca -config /path/to/myca.conf -in req.csr -out ourdomain.pem \
-startdate 0801010000Z -enddate 1001010000Z

但我不确定如何将两者结合起来,因为第二个命令似乎只是为域生成一个密钥。任何人都可以帮助组合这两个命令,或者以其他不涉及更改我的系统时间的方式更改证书时间。

标签: openssl

解决方案


openssl 命令行不提供命令行选项来设置“x509 -req”选项的开始和结束日期。

如果你真的需要这样做,你可以修改 openssl 源代码来做你想做的事。

在 app\req.c 中,您需要修改“set_cert_times”调用:

    if (days == 0) {
        /* set default days if it's not specified */
        days = 30;
    }
    if (!set_cert_times(x509ss, NULL, NULL, days))
        goto end;

int set_cert_times(X509 *x, const char *startdate, const char *enddate,
                   int days)

如果您提供 startdate 和 enddate,它将覆盖 days 参数,因此您可以这样做:

if (!set_cert_times(x509ss, "0801010000Z", "1001010000Z", days))
    goto end;

这将对开始和结束日期进行硬编码,或者通过更多的工作,您可以在 x509 -req 处理中添加对 -startdate 和 -enddate 参数的支持。


推荐阅读