简体   繁体   English

如何从SSLSocket中提取X509Certificate []实例?

[英]How do I pull an instance of X509Certificate[] out of an SSLSocket?

Suppose I have the following code, which creates an SSL Socket to google.com , and I would like to view Google's certificate. 假设我有以下代码,该代码为google.com创建了一个SSL套接字,并且我想查看Google的证书。 How can I get an instance of javax.security.cert.X509Certificate[] ? 如何获取javax.security.cert.X509Certificate[]的实例?

import java.io.*;
import java.math.*;
import java.net.*;
import java.security.*;

import javax.net.ssl.*;
import javax.net.*;


public class MWE{
    public static void main(String[] args) throws Exception{
        SSLContext sslContext = SSLContext.getDefault();
        SocketFactory clientSocketFactory = sslContext.getSocketFactory();

        String remoteHost = "google.com";
        int remotePort = 443;
        SSLSocket socket = null;
        try {
            //Lookup the "common name" field of the certificate from the remote server:
            socket = (SSLSocket) clientSocketFactory.createSocket(remoteHost, remotePort);
            socket.setEnabledCipherSuites(socket.getSupportedCipherSuites());
            socket.startHandshake();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }
}

I have looked at the documentation for each of classes involved, but I have yet to find any references to any data structures containing the certificate. 我已经查看了所涉及的每个类的文档,但是还没有找到对包含证书的任何数据结构的引用。

X509Certificate[] c = socket.getSession().getPeerCertificateChain();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM