我在将自定义客户端证书从java密钥库导出到pem文件时遇到问题。我希望我的C ++ SSL服务器程序验证此自签名证书。如果我通过openssl创建自签名证书,它就可以工作,即服务器验证证书。如果我使用从JKS导出的自签名证书,则无法验证并且握手失败。我在服务器上使用以下SSL函数进行客户端证书验证。
SSL_CTX_load_verify_locations(*ctx, szloc, NULL)
SSL_CTX_set_verify(*ctx, SSL_VERIFY_PEER, NULL);
SSL_CTX_set_verify_depth( *ctx, 0 );
我打印出两个客户证书的内容。
证书有效:
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 530171964 (0x1f99c83c)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=US, ST=OR, L=Salem, O=mahaus, OU=maroom client, CN=lenovo-pc
Validity
Not Before: Feb 27 13:21:37 2015 GMT
Not After : May 28 13:21:37 2015 GMT
Subject: C=US, ST=OR, L=Salem, O=mahaus, OU=maroom client, CN=lenovo-pc
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:d2:65:56:a8:1e:66:7b:70:c9:a2:73:ae:77:58:
cb:bf:bf:0a:62:66:5a:5d:34:3b:22:32:75:82:73:
b6:08:01:e8:0d:ab:d0:03:d8:3b:82:9d:84:2d:d0:
6c:86:82:f9:00:ea:19:57:2c:89:51:7d:6a:b3:f9:
75:4c:a2:57:df:9c:f5:96:bd:ff:f1:8c:4d:1d:17:
f5:b8:95:22:f2:9c:91:92:20:38:9e:b3:99:0a:80:
96:51:35:12:5b:4e:88:43:f0:0b:d5:02:b9:80:00:
1c:9a:af:42:9a:9e:44:e3:d4:c8:04:cd:11:d2:8f:
7f:78:90:58:bd:15:22:e1:d3:54:fd:c9:b0:65:20:
c4:f2:0b:37:62:40:f6:13:ab:ef:ce:c9:8a:1a:9a:
07:95:ad:96:9e:e4:48:a1:fd:32:a0:2a:02:5c:74:
4c:fd:ef:d6:13:af:a2:bb:00:f3:5c:c8:ee:a8:26:
fb:63:a3:50:81:45:88:44:03:dc:99:60:81:52:6b:
c7:51:e2:30:fd:68:90:f1:95:74:f8:72:23:56:3d:
35:16:18:a1:03:e0:87:36:d0:8d:ce:99:19:b0:57:
c7:3d:2a:86:d0:78:91:d8:2c:df:a2:1b:42:34:3b:
d1:cc:6d:96:34:73:b3:1b:31:ce:1b:36:7a:43:38:
45:95
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
EA:E7:82:0D:CA:DD:8A:F6:89:99:04:65:5F:F4:6E:30:91:FE:80:41
Signature Algorithm: sha256WithRSAEncryption
6a:a1:2c:42:57:7b:9c:18:88:e4:98:8d:e3:a8:9b:ca:c9:5f:
9e:5a:c0:23:95:f6:e8:b3:44:0e:70:77:11:52:b2:de:a6:e1:
a9:0a:66:fc:3a:b2:e9:f2:53:14:fa:5e:7b:69:c9:fe:de:64:
4d:40:b3:a3:c7:cb:c7:cd:87:79:0c:2d:f4:f1:8c:54:fa:44:
75:17:16:05:a7:14:d8:60:fd:20:3a:ec:49:9c:27:96:53:8e:
9d:93:24:2d:cb:f2:f9:1c:ee:da:90:75:6b:5f:9e:f4:fd:fc:
60:97:ab:d6:7a:61:89:25:26:a0:15:ed:9e:02:ba:4e:02:50:
83:bc:06:2b:23:6a:74:50:87:2e:17:8d:3f:1f:f0:c9:4f:08:
36:b8:4d:62:53:e6:5a:f5:d3:c2:71:11:58:5d:4f:91:24:32:
c9:ee:33:8a:c3:24:64:e3:80:7b:28:e1:5c:ca:84:02:ef:71:
d0:0a:a0:09:95:82:34:f1:3a:06:19:9e:01:fe:6c:92:a4:a5:
da:d7:05:29:90:3b:26:7a:b9:19:33:7e:be:fc:59:cb:d0:9e:
28:80:57:ed:e4:b6:95:19:31:c7:4b:62:26:16:53:0b:fc:ef:
d9:88:9b:1a:a6:99:d7:4b:73:ad:15:97:c2:6e:12:75:77:d1:
60:5f:bb:41
从JKS导出的证书不起作用:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
cd:19:9c:68:d1:c5:ca:c2
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=US, ST=IL, L=Springfield, O=macompany, OU=yourus, CN=ABC
Validity
Not Before: Feb 27 19:23:33 2015 GMT
Not After : Feb 27 19:23:33 2016 GMT
Subject: C=US, ST=IL, L=Springfield, O=macompany, OU=yourus, CN=ABC
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:db:ef:24:8e:ee:3e:b1:14:18:8e:ba:17:ee:68:
34:d2:ac:98:29:53:97:f0:a4:21:c7:82:4c:10:ab:
97:17:dd:b6:88:ca:63:ad:68:7b:85:ed:94:93:64:
b4:53:f2:d4:ce:2f:9e:a2:3b:87:70:66:20:f3:ad:
30:e1:a0:31:0d:80:dd:c6:38:ed:5d:ec:d7:5c:a2:
89:d6:c6:ad:a1:7d:63:1a:df:5b:8b:6d:c5:94:f1:
fc:58:0e:a1:6f:f5:24:11:ed:be:06:f3:f1:dc:a7:
d3:43:c6:a2:b4:68:e5:20:ba:bd:1b:7c:f8:29:69:
79:f5:69:07:c1:ec:a8:78:dc:65:74:ed:bd:72:b1:
72:36:5b:b7:2a:a7:d3:2b:f2:45:af:64:62:de:e4:
4d:8b:2f:68:f3:24:bd:33:38:dd:17:00:64:e6:c2:
c8:2e:85:36:a2:52:6d:f7:dc:59:70:84:a2:23:7e:
f3:29:d6:ee:1e:07:06:c5:2a:37:3d:b2:05:b4:44:
82:c4:7c:a6:30:a4:90:96:f6:7a:1a:58:3c:e6:11:
ee:17:3d:3c:6c:b5:0d:46:b1:71:78:b5:b1:99:73:
aa:05:aa:a3:9f:10:ed:03:63:ae:30:c8:b2:62:a5:
77:d2:a0:b4:1f:f7:db:ca:bf:02:a2:f6:5a:91:91:
e2:9f
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
D5:FA:5F:9D:75:7A:DB:6F:D5:4B:62:CF:59:6D:BC:B5:AE:63:78:8D
X509v3 Authority Key Identifier:
keyid:D5:FA:5F:9D:75:7A:DB:6F:D5:4B:62:CF:59:6D:BC:B5:AE:63:78:8D
X509v3 Basic Constraints:
CA:TRUE
Signature Algorithm: sha1WithRSAEncryption
4f:c4:26:bf:4d:04:09:30:4b:75:31:25:73:48:15:aa:34:7c:
fa:86:b1:63:49:e2:4d:67:5c:9b:de:4a:39:f4:a3:58:fe:f5:
13:bf:ac:b6:81:9f:37:f4:db:c9:80:e8:fe:3b:f4:2a:54:5c:
95:1d:56:9a:1e:10:10:c9:7e:12:05:b6:f6:17:60:1a:b1:8e:
33:49:56:87:9d:ba:7d:56:02:7b:2d:82:81:98:12:a5:b2:b1:
33:db:ef:e5:b0:f3:96:c0:65:0d:6f:a9:5d:e9:81:fc:78:a5:
07:f1:3f:f7:64:0c:13:58:25:ff:b1:73:b7:bf:d3:a2:ac:f3:
d4:c9:e4:ed:1c:92:30:ef:00:d8:e8:ba:a2:69:6a:e0:07:18:
a1:20:e5:e4:fd:1a:c9:91:0e:7b:7b:e7:81:e3:ad:8f:b2:88:
5e:ee:dc:a9:36:e5:75:a4:0d:26:26:2c:f2:8e:99:de:0f:5e:
0b:a6:85:c4:b8:88:ca:43:43:b7:27:23:f0:d6:94:b6:e9:8d:
b2:ad:a1:f4:d5:62:ff:4e:ec:9a:4f:3b:73:a0:32:0c:3a:cb:
dc:3b:7c:26:72:bf:68:39:da:44:eb:95:ef:02:86:f6:be:46:
8d:62:82:d9:59:6d:1a:ce:a8:09:ed:16:fe:e1:12:72:9b:0c:
a6:10:22:47
BTW此证书在C ++客户端成功加载,这意味着客户端上的私钥没有不匹配。
我看到的一个差异可能与此问题有关,导出的证书缺少X509v3 extensions
部分下的CA的以下信息:
X509v3 Authority Key Identifier:
keyid:D5:FA:5F:9D:75:7A:DB:6F:D5:4B:62:CF:59:6D:BC:B5:AE:63:78:8D
X509v3 Basic Constraints:
CA:TRUE
这是服务器上的第二个(从JKS导出)证书验证不起作用的原因吗?如果是这样,我将如何导出必要的扩展?如果没有,那还有什么呢?
我将不胜感激任何帮助。感谢。
答案 0 :(得分:1)
您总是需要权限密钥标识符(AKI)才能成功验证x509证书。详细信息在RFC中,但我认为自签名证书(包括根CA)不需要它。此外,您上面列出的两个证书似乎都有有效的AKI值,所以我认为这不是您的问题。如果这些证书是使用openssl或Java keytool创建的,则AKI值非常好。
我的猜测是,您的信任链因未成功验证的证书而中断。您的服务器(C ++程序)从某个地方获取它的信任链 - 在Java中,信任链通常来自位于jre/lib/security/cacerts
的cacerts文件。要成功验证,您的自签名证书必须列为受信任的颁发者。您可以使用keytool命令keytool -import -trustcacerts -alias yourCert -file file.cer
在Java中添加受信任的颁发者。我不确定您的C ++信任存储位于何处或等效命令是什么。
查看列出的函数调用,看起来这些是OpenSSL函数,因此您的OpenSSL信任库必须包含您的自签名证书作为受信任的根CA.看起来有各种系统的在线说明,只需查看“OpenSSL信任存储”。