0

我已经读过,可以在不竞争握手的情况下获取证书数据,这对于减少时间是可取的。下面的代码用于获取证书信息,但代码完成了 ssl 握手。如果任何一位网络编程专家可以告诉我如何在不完成 ssl 握手的情况下获取远程主机的证书信息?

package com.kushal.security;

import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class JavaSSLCertificate {

public static void main(String[] argv) throws Exception {

int port = 443;

String hostname = "gmail.com";

SSLSocketFactory factory = HttpsURLConnection
.getDefaultSSLSocketFactory();

System.out.println("Creating a SSL Socket For "+hostname+" on port "+port);

SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);

socket.startHandshake();
System.out.println("Handshaking Complete");

Certificate[] serverCerts = socket.getSession().getPeerCertificates();
System.out.println("Retreived Server's Certificate Chain");

System.out.println(serverCerts.length + "Certifcates Found\n\n\n");
for (int i = 0; i < serverCerts.length; i++) {
Certificate myCert = serverCerts[i];
System.out.println("====Certificate:" + (i+1) + "====");
System.out.println("-Public Key-\n" + myCert.getPublicKey());
System.out.println("-Certificate Type-\n " + myCert.getType());

System.out.println();
}

socket.close();
}

}
4

1 回答 1

-1

证书交换是握手的一部分。我不知道您在哪里读到无需等待握手即可获得证书,但这不是真的,至少在 JSSE 中是这样。这对我来说甚至没有意义。

于 2012-07-20T19:36:54.953 回答