通过 java 使用 Windows 安全的确认证书弹出窗口显示有效证书

发布于 2025-01-15 00:26:04 字数 1079 浏览 5 评论 0原文

我只想显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2025-01-22 00:26:04

JNA 只是 Java 中到本机 API(在本例中为 Windows)的接口。如果您有一个证书列表并希望按有效性进行过滤,您只需要一个 WinAPI 函数来测试它。

CertVerifyCertificateChainPolicy 似乎满足此要求。从文档中:

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:

The CertVerifyCertificateChainPolicy function checks a certificate chain to verify its validity, including its compliance with any specified validity policy criteria.

This is mapped in the JNA user-contributed mappings (jna-platform) in Crypt32.java.

Applying this boolean function as a filter is left as an exercise for the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文