通过 java 使用 Windows 安全的确认证书弹出窗口显示有效证书
我只想显示 Windows 安全的确认证书弹出窗口中的有效证书,jna 库函数获取所有有效和无效证书。有什么办法可以解决这个问题吗? 列出 Windows 安全中心的证书
public static X509Certificate selectCertificate() throws CertificateException {
var handle = Crypt32.INSTANCE.CertOpenSystemStore(null, "MY");
try {
var certCtx = Cryptui.INSTANCE.CryptUIDlgSelectCertificateFromStore(handle, null,
null, null,16 , 0, null);
if (certCtx != null) {
try {
CertificateFactory fac = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) fac.generateCertificate(
new ByteArrayInputStream(certCtx.pbCertEncoded.getByteArray(0, certCtx.cbCertEncoded)));
return cert;
} finally {
Crypt32.INSTANCE.CertFreeCertificateContext(certCtx);
}
}
} finally {
if (handle != null)
Crypt32.INSTANCE.CertCloseStore(handle, 0);
}
return null;
}
I want to show only valid certificates from Windows Security's Confirm Certificate popup, jna library function get all valid and invalid certificates. Is there any way to solve this problem? list certificate from Windows Security
public static X509Certificate selectCertificate() throws CertificateException {
var handle = Crypt32.INSTANCE.CertOpenSystemStore(null, "MY");
try {
var certCtx = Cryptui.INSTANCE.CryptUIDlgSelectCertificateFromStore(handle, null,
null, null,16 , 0, null);
if (certCtx != null) {
try {
CertificateFactory fac = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) fac.generateCertificate(
new ByteArrayInputStream(certCtx.pbCertEncoded.getByteArray(0, certCtx.cbCertEncoded)));
return cert;
} finally {
Crypt32.INSTANCE.CertFreeCertificateContext(certCtx);
}
}
} finally {
if (handle != null)
Crypt32.INSTANCE.CertCloseStore(handle, 0);
}
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JNA 只是 Java 中到本机 API(在本例中为 Windows)的接口。如果您有一个证书列表并希望按有效性进行过滤,您只需要一个 WinAPI 函数来测试它。
CertVerifyCertificateChainPolicy
似乎满足此要求。从文档中:<一href="http://java-native-access.github.io/jna/5.10.0/javadoc/com/sun/jna/platform/win32/Crypt32.html#CertVerifyCertificateChainPolicy-com.sun.jna.platform.win32 .WTypes.LPSTR-c om.sun.jna.platform.win32.WinCrypt.CERT_CHAIN_CONTEXT-com.sun.jna.platform.win32.WinCrypt.CERT_CHAIN_POLICY_PARA-com.sun.jna.platform.win32.WinCrypt.CERT_CHAIN_POLICY_STATUS-” rel="nofollow noreferrer">此映射在
Crypt32.java
中的 JNA 用户贡献的映射 (jna-platform
) 中。应用这个布尔函数作为过滤器留给读者作为练习。
JNA is just an interface in Java to native APIs -- Windows in this case. If you have a list of certificates and want to filter by validity you just need a WinAPI function to test that.
CertVerifyCertificateChainPolicy
appears to meet this requirement. From the documentation:This is mapped in the JNA user-contributed mappings (
jna-platform
) inCrypt32.java
.Applying this boolean function as a filter is left as an exercise for the reader.