0

当我使用openssl命令行生成 RSA-2048 密钥对,然后使用相同的私钥为相同的域名构造两个证书签名请求 (CSR) 时,我得到相同的输出。

$ openssl genrsa -f4 -out rsa.key | head -1
Generating RSA private key, 2048 bit long modulus
$ openssl req -new -sha256 -key rsa.key -out rsa1.csr -subj "/CN=example.com"
$ openssl req -new -sha256 -key rsa.key -out rsa2.csr -subj "/CN=example.com"
$ diff rsa1.csr rsa2.csr

但是,当我生成一个椭圆曲线 (P-256) 密钥对,并使用相同的私钥为相同的域名创建两个 CSR 时,我会得到两个不同的输出!

$ openssl ecparam -genkey -name prime256v1 -noout -out p256.key
$ openssl req -new -sha256 -key p256.key -out ec1.csr -subj "/CN=example.com"
$ openssl req -new -sha256 -key p256.key -out ec2.csr -subj "/CN=example.com"
$ diff -U999 ec1.csr ec2.csr 
--- ec1.csr 2019-08-14 12:20:55.000000000 -0400
+++ ec2.csr 2019-08-14 12:20:59.000000000 -0400
@@ -1,7 +1,7 @@
 -----BEGIN CERTIFICATE REQUEST-----
-MIHRMHgCAQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wWTATBgcqhkjOPQIBBggq
+MIHPMHgCAQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wWTATBgcqhkjOPQIBBggq
 hkjOPQMBBwNCAASKkrbzoJCjHgvI95U1ZYPG5AQUtN+ImrutI2KNAne/BvktGaHW
-ep2CEc5bliuYzxeC68cUG0MBmDrLZbRwaMS7oAAwCgYIKoZIzj0EAwIDSQAwRgIh
-AO1VziY7sHIKNFvCQnm+g7fguFSPoopHw+Jh3CKpjTKYAiEAmvlilKQkiN134T07
-LCDWfF/IlGeWv6nv1VhgsD3SEBU=
+ep2CEc5bliuYzxeC68cUG0MBmDrLZbRwaMS7oAAwCgYIKoZIzj0EAwIDRwAwRAIg
+AxHKjgwyXbeMqWK8XF/F6KztweW/tpY1U55pXyHeKgECID9jgdAQp7FetjbRGY7A
+GY0Y37x8XY5O3o5rEZSnsA1C
 -----END CERTIFICATE REQUEST-----

这不是一次性的事情。我使用这个 P-256 密钥生成 1000 个 CSR,我得到 1000 个不同的输出。我使用这个 RSA-2048 密钥生成 1000 个 CSR,得到 1000 个相同的输出。

$ for i in `seq 1 1000`; do openssl req -new -sha256 -key p256.key -out ec$i.csr -subj "/CN=example.com"; done
real    0m8.147s
user    0m5.972s
sys     0m1.810s
$ md5sum ec*.csr | cut -f 1 -d ' ' | sort | uniq | wc -l
1000

$ time for i in `seq 1 1000`; do openssl req -new -sha256 -key rsa.key -out rsa$i.csr -subj "/CN=example.com"; done
real    0m43.940s
user    0m41.386s
sys     0m2.049s
$ md5sum rsa*.csr | cut -f 1 -d ' ' | sort | uniq | wc -l
1

这到底是怎么回事?有什么方法可以强制 OpenSSL 生成可重现的输出?有什么理由我不希望 OpenSSL 生成可重现的输出?

是否有任何理由希望OpenSSL 在使用 RSA 密钥时生成可重现的输出,但在使用 EC 密钥时不生成?


我应该补充一点,我使用https://certlogik.com/decoder/来查看正在生成的 CSR,它们看起来完全一样,除了最后BIT STRING,我假设它应该是 SHA-256 签名?

我还看到 P-384 键发生了同样的不确定性:

$ openssl ecparam -genkey -name secp384r1 -noout -out p384.key
$ openssl req -new -sha256 -key p384.key -out ec1.csr -subj "/CN=example.com"
$ openssl req -new -sha256 -key p384.key -out ec2.csr -subj "/CN=example.com"
$ cmp ec1.csr ec2.csr 
ec1.csr ec2.csr differ: char 275, line 5
4

1 回答 1

2

标准 ECDSA 签名本质上是不确定的。这是算法的基本特征。另一方面,RSA 签名是确定性的。所以这就是您看到这些类型的键之间差异的原因。

一个RFC 描述了生成确定性 ECDSA 签名的过程(参见RFC6979)。但是 OpenSSL 目前不支持它。有一个开放的拉取请求在此处添加该功能。

于 2019-08-14T21:20:11.673 回答