【发布时间】:2023-04-10 04:52:01
【问题描述】:
我需要使用智能卡登录网站。我可以成功地从智能卡中获取密钥库,其中包含用户的证书和不可导出的私钥(它是一个常规的 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”方法给你上面提到的密钥库,其中包含证书链(99%,也许 100% 只有一个证书,因为我尝试了 26 种不同的智能卡,它们只有一个证书:用户的)和一个不可导出的私钥。 我试图在互联网上寻找解决方案,但它们都是关于 PKCS#12,我不需要这个。
我尝试使用不同的协议(SSL 和 TLS)和它的不同版本,但没有!
Firefox 可以进行智能卡身份验证,而且我确定我在此过程中遗漏了一些东西!当我在“httpClient”对象上调用“execute”方法时,它给了我一个异常:“handshake_failure”(SSLHandshakeException)。
如果我使用“loadKeyMaterial”而不是“loadTrustMaterial”,我会得到“unsupported_certificate”。
我现在真的不知道我要做什么! 你有什么建议吗? 提前致谢!
【问题讨论】:
标签: java ssl smartcard pkcs#11