【问题标题】:Why does the serial number on Google's certificate as returned by Java's SSL differ from that returned from Firefox and Chrome?为什么 Java 的 SSL 返回的 Google 证书上的序列号与 Firefox 和 Chrome 返回的不同?
【发布时间】:2013-03-05 07:23:44
【问题描述】:

我在 Java 中使用以下代码打印出 Google 证书的各种属性。

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.SocketFactory;

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

import javax.net.*;
import javax.security.cert.X509Certificate;

/*
 * Start an connection with google.com and submit to Google to figure out how to get the certificate.
 * Should not pull from artificial context.
 */
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();
        }
        X509Certificate[] c = socket.getSession().getPeerCertificateChain();
        X509Certificate serverCertificate = c[0]; //can I control which instance of this is used?
        Principal serverDN = serverCertificate.getSubjectDN();
        BigInteger serverSerialNumber = serverCertificate.getSerialNumber();

        System.out.println(serverCertificate.getClass());
        System.out.println(serverDN);
        System.out.println(serverSerialNumber.toString(16));
        System.out.println(serverCertificate.getSigAlgName());

        System.out.println(serverCertificate.getNotBefore());
        System.out.println(serverCertificate.getNotAfter());
    }
}

我得到的输出是这样的:

CN=*.google.com, O=Google Inc, L=Mountain View, ST=California, C=US
1484d9a3000000007d35
SHA1withRSA
Wed Feb 20 05:34:43 PST 2013
Fri Jun 07 12:43:27 PDT 2013

但是,当我从 Firefox 或 Chrome 中查看证书时,除了序列号之外,所有内容都匹配。

【问题讨论】:

    标签: java firefox ssl ssl-certificate


    【解决方案1】:

    您的 Firefox 证书信息显示 www.google.com 的证书,而您的 Java 代码显示 google.com 的证书。

    这两个网站有不同的证书,因此也有不同的序列号。

    【讨论】:

      猜你喜欢
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2019-10-31
      相关资源
      最近更新 更多