1

2 つのファイル (.p12 および .p7b) からロードされた証明書を使用して SSL 接続を作成しようとしています。
.p12 ファイルをロードするために次のコードを試しました

char []passwKey = "1234567".toCharArray();
        KeyStore ts = KeyStore.getInstance("PKCS12");
        ts.load(new FileInputStream("/home/user/Desktop/file.p12"), passwKey);
        KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
        tmf.init(ts,passwKey);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(tmf.getKeyManagers(), null, null);
        SSLSocketFactory factory =sslContext.getSocketFactory();
        HttpsURLConnection.setDefaultSSLSocketFactory(factory);
        SSLSocket socket = (SSLSocket) factory.createSocket("www.host.com", 8883); // Create the ServerSocket
        String[] suites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(suites);
        socket.startHandshake();

しかし、私は例外を受け取ります:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX パスの構築に失敗しました: sun.security.provider.certpath.SunCertPathBuilderException: 要求されたターゲットへの有効な証明書パスが見つかりません

.p12 および .p7b ファイル (CA チェーン全体を含む) から .jks ファイルを作成する必要があると思いますが、私はこれに慣れておらず、その方法がわかりません。私が見つけた例は、単一のファイル/証明書に基づいていました。

アップデート:

証明書ファイルを使用して単一のキーストアを作成しました (必要なのは .p12 ファイルだけだったと思います) が、うまくいきませんでした。そのため、サイトに直接アクセスし、証明書を .pem としてエクスポートしてキーストアに追加しました。デバッグ情報で「ServerHello」を受け取るようになりましたが、最後にまだ取得します

handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

たとえば、いくつかのソリューションを試しました。 HTTPS/SSL 経由の Java クライアント証明書 または javax.net.ssl.SSLHandshakeException の取得: 受信した致命的なアラート: handshake_failure 受信した .p12 ファイルからの証明書とブラウザーからエクスポートされた証明書でエラーが発生しましたが、どれも機能しません...

更新 2:

私はこれを試しました: https://stackoverflow.com/a/11908693/1215791そしてなんとか ServerHelloDone にたどり着きました(そして信頼できる証明書が見つかりました...)。

しかし、私が今やろうとしているのは、SOAPリクエストでログインすることで、これを取得します:

com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
    at SoapT.login(SoapT.java:241)
    at SoapT.main(SoapT.java:75)

付属の証明書の問題ではなく、soap リクエスト作成時のエラーか、サーバーのエラー (html) だと思います。

4

1 に答える 1

1

not-yet-commons-ssl を試してください。

非常に使いやすい SSL-Library。SSLClient を作成し、Trust マテリアルを追加します

ウェブページの例:

Client Example:

SSLClient client = new SSLClient();

// Let's trust usual "cacerts" that come with Java.  Plus, let's also trust a self-signed cert
// we know of.  We have some additional certs to trust inside a java keystore file.
client.addTrustMaterial( TrustMaterial.DEFAULT );
client.addTrustMaterial( new TrustMaterial( "/path/to/self-signed.pem" ) );
client.addTrustMaterial( new KeyMaterial( "/path/to/keystore.jks", "changeit".toCharArray() ) );

// To be different, let's allow for expired certificates (not recommended).
client.setCheckHostname( true );  // default setting is "true" for SSLClient
client.setCheckExpiry( false );   // default setting is "true" for SSLClient
client.setCheckCRL( true );       // default setting is "true" for SSLClient

// Let's load a client certificate (max: 1 per SSLClient instance).
client.setKeyMaterial( new KeyMaterial( "/path/to/client.pfx", "secret".toCharArray() ) );
SSLSocket s = (SSLSocket) client.createSocket( "www.cucbc.com", 443 );
于 2013-04-09T08:56:53.617 に答える