4

我正在尝试使用 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我知道这个有效的是Fileand Keystore

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

这是我的错误日志...

** CertificateRequest
Cert Types: RSA, DSS, ECDSA
Cert Authorities:
<empty>
[read] MD5 and SHA1 hashes: len = 10
0000: 0D 00 00 06 03 01 02 40 00 00 .......@..
** ServerHelloDone
[read] MD5 and SHA1 hashes: len = 4
0000: 0E 00 00 00 ....
** Certificate chain
**
** ClientKeyExchange, RSA PreMasterSecret, TLSv1
[write] MD5 and SHA1 hashes: len = 269
...
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1 ALERT: fatal, handshake_failure
main, called closeSocket()
main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

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

4

3 回答 3

5

我建议您使用 keytool -list 将客户端上的密钥库与服务器已知的密钥库进行比较。您收到的握手错误是因为服务器已经完成了它的问候,并期待客户端证书作为回复。你没有发送一个。要解决此问题,应将 PKCS12 证书转换为 PEM 格式(使用 openssl 是一种方法),然后使用 keytool 将其导入密钥库。

我怀疑如果您通过将客户端证书导入密钥库来解决此问题,那么您将遇到第二个错误。第二个错误与空 CA 证书有关 - 可能是因为您的密钥库中没有服务器已知的 CA 证书。导入您的 CA 并重试。

于 2012-08-07T20:43:07.307 回答
0

看起来您需要安装“Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files”。这为我解决了这个问题。

于 2014-12-11T13:38:14.243 回答
-4

要向 iPhone/iPad 发送推送通知,我使用了 JavaPNS

它非常易于使用,并且对我有用。

我们可以简单地按照这个来使用它。

于 2013-12-31T05:35:59.933 回答