1

我正在尝试使用 Java-pns 向 iPhone 发送推送通知,但出现以下错误 - javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

我的代码是 -

String token="95076d2846e8979b46efd1884206a590d99d0f3f6139d947635ac4186cdc5942";
String host = "gateway.sandbox.push.apple.com";
int port = 2195;
String payload = "{\"aps\":{\"alert\":\"Message from Java o_O\"}}";

NotificationTest.verifyKeystore("res/myFile.p12", "password", false);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(getClass().getResourceAsStream("res/myFile.p12"), "password".toCharArray());

KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509");
keyMgrFactory.init(keyStore, "password".toCharArray());

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
String[] cipherSuites = sslSocket.getSupportedCipherSuites();
sslSocket.setEnabledCipherSuites(cipherSuites);
sslSocket.startHandshake();

char[] t = token.toCharArray();
byte[] b = Hex.decodeHex(t);

OutputStream outputstream = sslSocket.getOutputStream();

outputstream.write(0);
outputstream.write(0);
outputstream.write(32);
outputstream.write(b);
outputstream.write(0);
outputstream.write(payload.length());
outputstream.write(payload.getBytes());

outputstream.flush();
outputstream.close();

System.out.println("Message sent .... ");

对于NotificationTest.verifyKeystore ,我知道这个有效的是文件和密钥库。

我不明白为什么会收到此错误。

请问有人可以帮助我吗?

提前致谢 ...

在我的日志中,我看到了

** 证书请求

证书类型:RSA、DSS、ECDSA

证书颁发机构:

[阅读] MD5 和 SHA1 哈希:len = 10

0000: 0D 00 00 06 03 01 02 40 00 00 ....@..

** 服务器HelloDone

[阅读] MD5 和 SHA1 哈希:len = 4

0000: 0E 00 00 00 ....

** 证书链

**

** ClientKeyExchange、RSA PreMasterSecret、TLSv1

[写入] MD5 和 SHA1 哈希:len = 269

.

.

.

.

.

主要,阅读:TLSv1 警报,长度 = 2

主要,RECV TLSv1 ALERT:致命,handshake_failure

主要的,称为 closeSocket()

主要,处理异常:javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure

我不明白为什么Cert Authorities:是空的?

4

1 回答 1

3

无法在没有看到堆栈跟踪以及运行客户端或服务器时产生的输出的情况下回答-Djavax.net.debug=ssl,handshake.但是:

String[] cipherSuites = sslSocket.getSupportedCipherSuites(); sslSocket.setEnabledCipherSuites(cipherSuites);

不要那样做。它根本不安全,它只是隐藏了真正的问题,这几乎总是一个证书信任链问题:要么客户端不信任服务器的证书,反之亦然。

于 2012-06-15T12:02:26.850 回答