又一篇关于自签名证书的问题,但我已经尝试了几天来找到在最新版本的Chrome、Android和iOS开发环境中创建可用的自签名证书的最佳/正确方式。
我在这里和其他地方找到的说明已经过时,至少对其中一个平台而言是如此。
以下是我找到的最佳方法,但它只适用于Chrome和Android。
openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -subj "/C=US/ST=Oklahoma/L=Stillwater/O=My Company/OU=Engineering" -keyout ca.key -out ca.crt
openssl genrsa -out "test.key" 2048
openssl req -new -key test.key -out test.csr -config openssl.cnf
openssl x509 -req -days 3650 -in test.csr -CA ca.crt -CAkey ca.key -CAcreateserial -extensions v3_req -extfile openssl.cnf -out test.crt
openssl x509 -inform PEM -outform DER -in test.crt -out test.der.crt
openssl.cnf的内容:
[req]
default_bits = 2048
encrypt_key = no # Change to encrypt the private key using des3 or similar
default_md = sha256
prompt = no
utf8 = yes
# Specify the DN here so we aren't prompted (along with prompt = no above).
distinguished_name = req_distinguished_name
# Extensions for SAN IP and SAN DNS
req_extensions = v3_req
# Be sure to update the subject to match your organization.
[req_distinguished_name]
C = US
ST = Oklahoma
L = Stillwater
O = My Company
OU = Engineering
CN = test.com
# Allow client and server auth. You may want to only allow server auth.
# Link to SAN names.
[v3_req]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, serverAuth
subjectAltName = @alt_names
# Alternative names are specified as IP.# and DNS.# for IP addresses and
# DNS accordingly.
[alt_names]
DNS.1 = test.com
在我开发服务器上安装了test.crt和test.key之后,这种方法对于Chrome非常有效:只需将test.crt添加到我的Mac钥匙链中并为其打开“始终信任”即可。
对于Android也非常有效:将test.der.crt发送到设备并点击安装。最重要的是:它出现在设置/加密和凭据/受信任的凭据下的“用户”选项卡中。这对于在我的Android应用程序中使用networkSecurityConfig至关重要。
不幸的是,它在iOS上没有起作用:
- 我通过将证书拖到Xcode模拟器中来安装它。 - 我必须安装test.crt和ca.crt两个证书。如果我只安装了test.crt,则保持“未验证”状态,因为ca.crt是根证书。 - 它未出现在“设置/关于/证书信任设置”下,这是我打开它所需的。 - 当我的应用程序尝试使用NSMutableURLRequest访问我的服务器时,它会收到“TIC SSL Trust Error”的错误消息,并包括10个键值对:
- NSURLErrorFailingURLPeerTrustErrorKey= - _kCFStreamErrorDomainKey=3 - _kCFStreamErrorCodeKey=-9813 - NSErrorPeerCertificateChainKey=1 element, and NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “test.com” which could put your confidential information at risk.
有什么办法可以更改我所做的内容,以便我可以在iOS下打开“证书信任设置”?
注意1:由于其他关于-9813错误代码的问题的答案表明可能缺少中间证书,因此我将ca.crt添加到我的Apache配置中以设置SSLCaCertificateFile。对于Chrome和Android仍然可以正常工作,但在iOS中出现了完全相同的错误。
谢谢!
DNS.2 = localhost
的情况下测试 OsX,以查看是否是因为它被忽略了,他们与移动平台相比,在不安全的本地服务器方面存在更多问题。 - lossleader