如何判断证书使用的是椭圆曲线还是 RSA 密钥

你可以使用 openssl 通过以下命令判断你的证书使用的是椭圆曲线(例如 ECDSA)还是 RSA 密钥,将 cert.pem 替换为你的证书路径:

example-8.sh

如果证书的密钥是椭圆曲线密钥,它将打印:

example-7.txt

如果证书的密钥是其他类型的密钥如 RSA 密钥,它将打印:

example-6.txt

工作原理

首先我们告诉 OpenSSL 打印有关证书的信息:

example-5.sh

然后我们 grep ecPublicKey。这是因为对于椭圆曲线密钥,上述 openssl 命令的输出包含

example-4.txt
    Public Key Algorithm: id-ecPublicKey
        Public-Key: (384 bit)
        pub:

而对于 RSA 密钥,它看起来像这样:

example-3.txt
    Public Key Algorithm: rsaEncryption
        RSA Public-Key: (2048 bit)
        Modulus:

grep 命令通过管道传递到 /dev/null,因为我们对它的输出不感兴趣,只对它的返回码感兴趣(在 shell 中可用 $? 获取)。grep 当且仅当在输入中找到至少一个匹配时返回返回码 0。否则,返回码为 1。在我们的情况下,这意味着如果 ecPublicKey 出现在输出的任何地方,我们将获得返回码 0。我们假设此字符串只会出现在 Subject Public Key Info: 部分。虽然理论上 ecPublicKey 可能出现在证书的某些用户定义字段中,但这极不可能发生,可以通过在 grep 中使用正则表达式来缓解

然后我们可以使用此 bash 代码片段:

example-2.sh
then
    # TODO insert code if grep does NOT find anything
else
    # TODO insert code if grep finds at least one line
fi

我们这样使用它:

example-1.sh

即根据 grep 的返回码,我们将打印正确的消息。


Check out similar posts by category: Networking