我正在使用本地 DNS 服务器
192.168.1.1
,它可以解析本地域 custom.local
及其所有子域。这本身就很好用。
为了建立安全连接,我有一个自签名根 CA 证书,如下所示:
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
*.custom.local
的通配符证书如下所示:
Bag Attributes
localKeyID: 01 00 00 00
friendlyName: Wildcard custom.local
...: ...
...: ...
subject=C = ..., ST = ..., L = ..., O = ..., OU = IT, CN = *.custom.local, emailAddress = ...
issuer=DC = local, DC = custom, CN = CUSTOM-ROOT-CA
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
以及通配符证书的私钥,如下所示:
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
现在我想做的是验证这三个组件的组合,这就是我到目前为止所做的:
# Check if the given private key belongs to the given wildcard certificate by comparing their derived public keys
[ "$(openssl x509 -pubkey -noout -in ./CUSTOM_WILDCARD.crt)" = "$(openssl rsa -pubout -outform PEM -in ./CUSTOM_WILDCARD.key 2&> /dev/null)" ]
# Verify the trust chain of the wildcard certificate by checking if it was signed by the private key corresponding to the given root CA certificate
openssl verify -CAfile ./CUSTOM_ROOT_CA.crt ./CUSTOM_WILDCARD.crt
但这有一个巨大的缺点,或者我应该说缺少了一个重要的部分。我从来没有根据实际服务器检查这些证书/密钥,我想知道如何做到这一点。
假设我想针对
test.custom.local:443
进行测试。我该怎么做?openssl s_client -connect -CAfile ... -cert ... -key ...
进行连接,如果它以代码 0 退出,我就知道我本地拥有的证书和密钥对于该服务器/域来说是正确的吗?
echo | openssl s_client -connect test.custom.local:443 -CAfile ./CUSTOM_ROOT_CA.crt -cert ./CUSTOM_WILDCARD.crt -key ./CUSTOM_WILDCARD.key
乍一看似乎可以工作,但如果例如我更改了密钥文件中的最后一个字母,该命令仍然返回相同的输出,退出代码为 0,这让我认为这种方法可能不安全。