【问题标题】:2 way TLS (1.2) between Java client and Apple pay serverJava 客户端和 Apple 支付服务器之间的 2 路 TLS (1.2)
【发布时间】:2017-01-25 16:05:35
【问题描述】:

努力实现: 根据 Apple Pay for Web 的要求,需要 2 路 TLS 1.2。在我的情况下,我试图从我的 java 中使用 json 有效负载(你可以说是客户端)访问苹果支付服务器。 https://developer.apple.com/reference/applepayjs/applepaysession#2166532

我的关注点:

1) 有列出所有支持的密码套件的命令吗?在 java 1.8 上运行。

2) 将 HttpsURLconnection.openconnection 设为 TLS1.2 需要什么?

3) 我可以通过它实现此连接的任何示例代码。

4) 证书和私钥等需要哪些设置?

任何帮助都会很有用。 我知道我已经提出了非常直接的问题,但我会继续在此添加更具体的问题。

更新:前 3 点已完成。 现在只关于第 4 点:我通过转换为 cert.p12 文件并保存为新的密钥库来发送文件密钥和证书。

openssl x509 -inform der -in merchant_id.cer -out merchant_id.pem
openssl pkcs12 -nodes -export -in merchant_id.pem -inkey clientprivate.key -out cert.p12 -name "Certificate"

之后在 eclipse 中运行带有 VM 参数的 java 代码:

-Djavax.net.ssl.keyStoreType=pkcs12 -Djavax.net.ssl.keyStore=cert.p12 -Djavax.net.debug=ssl

我可以在控制台中看到以下错误:

*ServerHelloDone
Warning: no suitable certificate found - continuing without client authentication
* Certificate chain

** ECDHClientKeyExchange
main, WRITE: TLSv1.2 Change Cipher Spec, length = 1
*** Finished
verify_data: 
*
main, WRITE: TLSv1.2 Handshake, length = 64
main, handling exception: java.net.SocketException: Connection reset
%% Invalidated:  [Session-1, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA]
main, SEND TLSv1.2 ALERT:  fatal, description = unexpected_message
main, WRITE: TLSv1.2 Alert, length = 48
main, Exception sending alert: java.net.SocketException: Connection reset by peer: socket write error
main, called closeSocket()
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)

这里是java代码:

HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();  
conn.setSSLSocketFactory(factory);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
os.write(jsonInString.getBytes("UTF-8"));

失败

conn.getOutputStream();

使用 java 1.8

【问题讨论】:

    标签: java-8 tomcat7 tls1.2 applepay applepayjs


    【解决方案1】:

    按询问顺序的一般答案..

    1. 我发现这对列出所有密码很有帮助:https://confluence.atlassian.com/stashkb/list-ciphers-used-by-jvm-679609085.html

    2. 我使用此System.setProperty("https.protocols", "TLSv1.2"); 将其设置为 TLS 1.2。

    3. 我无法在此处发布我的整个项目,代码太多...这是一个基本的 http 请求,同时发送了所有必填字段。
    4. 首先在苹果网站上创建一个“apple pay 商家 ID”证书,下载它,将证书和密钥从其中提取到 2 个单独的文件中,连同您的请求一起发送(当然还有密码)。李>

    【讨论】:

    • 谢谢 yyf。以上 3 点已解决。但我严重卡在第 4 点。我通过添加密钥库来发送密钥和证书。但是在 ServerHello 之后,我收到以下错误:WRITE: TLSv1.2 Handshake,length = 64 main, handling exception: java.net.SocketException: Connection reset %% Invalidated: [Session-1, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA] main, SEND TLSv1.2 ALERT:致命,description = unexpected_message main,WRITE:TLSv1.2 Alert,length = 48 main,异常发送警报:java.net.SocketException:对等方重置连接:套接字写入错误 main,调用 closeSocket()
    • 我也很努力,直到我让它工作......我从苹果下载了证书并从中提取一个证书openssl pkcs12 -nodes --nokeys -in merchant_id.pem -inkey clientprivate.key -out cert.p12和一个密钥openssl pkcs12 -nodes --nocerts -in merchant_id.pem -inkey clientprivate.key -out key.p12然后将两者都设置为请求的密钥库。
    • 我试图弄清楚有什么区别。所以,假设你从苹果那里得到了merchant_id.cer。那么您必须在此之前创建 csr 和私钥。之后从 .cer 文件您也已转换为 Mercer_id.pem。之后,正如您所提到的,创建了证书和密钥。那么你如何将两者都设置为密钥库?你也可以分享一下这一步吗?
    • 如果您列出您已遵循的步骤,那就太好了。我仍在努力让它发挥作用。谢谢yyf。
    • 在 Apple Developer Portal 中上传 CSR 文件会生成一个证书文件 (merchant_id.cer) 供您下载。这充当您的商户身份证书的公钥。私钥是您为生成该证书而提供的 CSR。为了创建到 Apple Pay 商家验证服务器的有效 TLS 连接,您需要使用 CSR 和 CER 文件创建一个公私密钥对,例如使用 OpenSSL 等工具创建一个 .p12 文件。
    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多