java - Java中的客户端证书身份验证

标签 java ssl smartcard pkcs#11

我需要使用智能卡登录网站。我可以从智能卡成功获取 keystore ,其中包含用户的证书和不可导出的私钥(它是一个常规的 PrivateKey 对象,但“getEncoded”方法返回 null)。

本站:https://pst.giustizia.it/PST/authentication/it/pst_ar.wp 有一个登录链接,每次访问时都会更改。因此,作为用户,我会在我的 Java 应用程序中做同样的事情:我访问该页面一次以获取该链接,然后我对该链接执行 SSL 身份验证(有点像模拟访问该页面并单击该链接) .

这是我使用的代码:

public class SSLAuth
{
    private static String LOGIN_PAGE = "https://pst.giustizia.it/PST/authentication/it/pst_ar.wp";
    private static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0";

    private TrustStrategy trustStrategy = new TrustStrategy()
    {
        public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
        {
            // Temporary work-around. I already know how to fix this
            return true;
        }
    };

    public String authenticate(String pin) throws Exception
    {
        // Request KeyStore from smart card
        KeyStore keyStore = Utility.digitalSigner.loadKeyStorePKCS11();
        SSLContext sslContext = SSLContexts.custom().useProtocol("TLSv1.2").loadTrustMaterial(keyStore, trustStrategy).build();

        // Get login token first
        String loginToken = null;
        {
            Document document = Jsoup.connect(LOGIN_PAGE).ignoreContentType(true).userAgent(USER_AGENT).timeout(10000).followRedirects(true).get();
            Elements link = document.select("div > fieldset > p > a");
            loginToken = link.get(0).attr("abs:href");
        }

        // Try to authenticate
        HttpClient httpClient = HttpClients.custom().setUserAgent(USER_AGENT).setSSLContext(sslContext).build();
        HttpResponse response = httpClient.execute(new HttpGet(loginToken));
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            return null;

        return response.toString();
    }
}

我只需要在第一次进行身份验证,因为一旦登录,网站只会检查名为“JSESSIONID”的 cookie 和客户端的用户代理字符串。我已经测试过了。获得这两个有效参数后,您甚至可以从其他浏览器访问该页面。

无论如何,“loadKeyStorePKCS11”方法给你上面提到的 keystore ,其中包含证书链(99%,也许100%只是一个证书,因为我尝试了26种不同的智能卡,它们只有一个证书:用户的)和不可导出的私钥。 我试图在互联网上寻找解决方案,但它们都是关于 PKCS#12 的,我不需要这个。

我尝试使用不同的协议(protocol)(SSL 和 TLS)和它的不同版本,但一无所获!

Firefox 可以进行智能卡身份验证,我确定我在这个过程中遗漏了一些东西!当我在“httpClient”对象上调用“execute”方法时,它给了我一个异常:“handshake_failure”(SSLHandshakeException)。

如果我使用“loadKeyMaterial”而不是“loadTrustMaterial”,我会得到“unsupported_certificate”。

我现在真的不知道我必须做什么! 你有什么建议吗? 提前致谢!

最佳答案

感谢您发布日志,我会把它作为一个答案,因为评论太多了。

简短回答:服务器要求提供客户端证书,而您没有或未配置为提供可接受的证书。

在你的日志中搜索:

main, READ: TLSv1 Handshake, length = 11296
*** CertificateRequest
Cert Types: RSA, DSS
Cert Authorities:

这是服务器要求您提供客户端证书。以下是可接受的 CA 列表。您的 keystore 中必须有这些 CA 之一颁发的证书,并且必须在客户端为 session 启用客户端证书。

日志的下一部分是这样的:

*** ServerHelloDone
Warning: no suitable certificate found - continuing without client 
authentication

不幸的是,您似乎没有服务器可接受的证书,或者没有为客户端证书模式配置,这就是它切断您并中止握手的原因。

关于java - Java中的客户端证书身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636631/

相关文章:

java - 删除通过 Java 连接到 MySQL 时的输出消息

ios - 在 iOS 上使用 Grabba 从军事 CAC 读取 CHUID?

php - 如何使php应用程序需要智能卡身份验证

java - JPA加载实体,但在@OneToMany列表中,仅加载特定的

java - Protobuf - 缺少 toByteArray()?

图片未显示在安全页面上?

nfc - 如何将任何 APDU 命令从当前选定的小程序传递到另一个小程序?

java - 应用程序在 1 小时后失去当前 Activity

java 获取插入的行ID

javascript - nodejs请求包中的SSL错误