简体   繁体   English

如何在java中进行相互SSL认证

[英]How to do mutual SSL authentication in java

I want to do mutual SSL authentication using java code but I didn't get success I have key.pem and cert.pem file to do authentication I have tried it with Curl我想使用 java 代码进行相互 SSL 身份验证,但我没有成功 我有key.pemcert.pem文件来进行身份验证我已经尝试过使用 Curl

curl -X POST -d '{ "Channel": "....}' -H "Content-Type: application/json" -H "Auth1: ***" -H "Auth2: ***" -k https://******/webservices/JSON/Default.aspx --cert "cert.pem" --key "Key.pem"

and it was working fine then I trying to create java program reference Send https request in java using .pem file but server returns "CERT_MISSING".并且它工作正常然后我尝试使用.pem文件在java中创建java程序引用发送https请求但服务器返回“CERT_MISSING”。 I also tried with this https://www.naschenweng.info/2018/02/01/java-mutual-ssl-authentication-2-way-ssl-authentication/ this code creating p12, crt and jks file as above link says but still getting same error "CERT_MISSING".我也尝试过使用此https://www.naschenweng.info/2018/02/01/java-mutual-ssl-authentication-2-way-ssl-authentication/这段代码创建 p12、crt 和 jks 文件,如上链接所述但仍然收到相同的错误“CERT_MISSING”。 This is working NodeJS example:这是有效的 NodeJS 示例:

var https = require("https");
var fs = require("fs");

var jsonData = {
    "Channel": ....
}

var options = {
    hostname: "****",
    port: 443,
    path: '/webservices/JSON/Default.aspx',
    method: 'POST',
    timeout: this.TimeOut,
    headers: {'Content-Type':'application/json',"Auth1": "****","Auth2": "*****"},
    json: true,
    key: fs.readFileSync('Key.pem'),
    cert: fs.readFileSync('cert.pem')
}

var req = https.request(options, function(res) {
    res.on('data', function(data) {
        var response = JSON.parse(data)
            console.log(response)

            req.end();
    });
});
req.on('error', function(e) {
    console.log("ERROR:");

    })


req.write(JSON.stringify(jsonData));
req.end();

Please help me in this.请帮助我。

I think you need "internediate-cert" file to Concatenate all certificates into one PEM file Like cat "internediate-cert.pem" "codika_cert.pem" "Key.pem" > full-chain.keycert.pem then Generate the PKCS12(.p12) keystore with the alias and password Like pkcs12 -export -in "full-chain.keycert.pem" -out full-chain.keycert.p12 -name alias -noiter -nomaciter then use full-chain.keycert.p12 as KeyStore with password.我认为您需要“internediate-cert”文件将所有证书连接到一个 PEM 文件中,例如cat "internediate-cert.pem" "codika_cert.pem" "Key.pem" > full-chain.keycert.pem然后生成 PKCS12( .p12) 带有别名和密码的密钥库 像pkcs12 -export -in "full-chain.keycert.pem" -out full-chain.keycert.p12 -name alias -noiter -nomaciter然后使用full-chain.keycert.p12作为带密码的密钥库。 It should work.它应该工作。

I'm just going to try to recall a few things to perhaps complete your checklist.我只是想试着回忆一些事情来完成你的清单。 I can't give a full example.我不能举一个完整的例子。 I presume you control both client and server code.我假设您同时控制客户端和服务器代码。 Or at least the client code, and full tuning of server config.或者至少是客户端代码,以及服务器配置的全面调整。

0-use wireshark to see what going on. 0-使用wireshark看看发生了什么。

1-make sure your server side sends the accept-client-cert or requires-client-cert in the serverhello. 1-确保您的服务器端在 serverhello 中发送 accept-client-cert 或 requires-client-cert。 Curl might be working only because the server isn't "requiring" it, only requesting it. Curl 可能工作只是因为服务器不是“要求”它,只是请求它。

2-make sure your client cert signer will be trusted by the server. 2-确保服务器信任您的客户端证书签名者。 This means sometimes to hack the server's certificate authorities (CA) store if you will be making self-signed client cert when testing.这意味着如果您在测试时制作自签名客户端证书,有时会破解服务器的证书颁发机构 (CA) 存储。

3-with wireshark, watch ssl/tls dialog and verify both clienthello and serverhello are as expected (mostly if serverhello does request/require client auth and if client even tries to send it). 3-使用wireshark,观察ssl/tls 对话框并验证clienthello 和serverhello 是否符合预期(主要是如果serverhello 确实请求/需要客户端身份验证,并且客户端甚至尝试发送它)。 If anything, that is quite educative.如果有的话,那是很有教育意义的。

4-write trivial code using jsse tutorials from bottom up. 4 使用 jsse 教程自下而上编写琐碎的代码。 make code kata to see the keystore/castore are there, contains what you expect.制作代码 kata 以查看 keystore/castore 是否在那里,包含您期望的内容。 Then proceed with secure [server]socketfactory setup, keymanager, trustmanager and hostnameverifier (with or without the dangerous bypass of the later two some people dare suggest).然后继续进行安全的 [server]socketfactory 设置、keymanager、trustmanager 和 hostnameverifier(有或没有有人敢建议的后两者的危险绕过)。 When these 4 pieces are setup, you are 99% done and your ssl/tls socket should work.设置好这 4 个部分后,您就完成了 99%,您的 ssl/tls 套接字应该可以工作了。 The few things that could typically still prevent your from a ssl/tls connection are incompatible cipher suite or tls version, or certs with invalid dates.通常仍会阻止您进行 ssl/tls 连接的少数事情是不兼容的密码套件或 tls 版本,或日期无效的证书。 Since you control both client and server, this should not be an issue.由于您同时控制客户端和服务器,因此这应该不是问题。

5-once you can trust your client side behavior, you may try to connect to an https server like tomcat or jetty (I dont know what you use) and send a crude GET on the sslsocket with you java client. 5-一旦您可以信任您的客户端行为,您可以尝试连接到像 tomcat 或 jetty 这样的 https 服务器(我不知道您使用的是什么),并在您的 java 客户端的 sslsocket 上发送一个粗略的 GET。

6-When that works, now you can pass your sslsocketfactory to many http[s] stack (smtp, httplient, even core jdk httpurlconnection). 6-当它起作用时,现在您可以将您的 sslsocketfactory 传递给许多 http[s] 堆栈(smtp、httplient,甚至核心 jdk httpurlconnection)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM