【问题标题】:How to enumerate all possible keys that exist on a smartcard that is present in a reader with just UWP APIs?如何枚举存在于仅具有 UWP API 的阅读器中的智能卡上的所有可能密钥?
【发布时间】:2022-04-24 19:22:41
【问题描述】:

以下是我的问题的限制条件:如何枚举存在于具有 UWP API 的阅读器中的智能卡上的所有可能密钥?

  1. 仅使用通用 Windows 程序 (UWP) API

我确实有使用 NCryptOpenStorageProvider(MS_SMART_CARD_KEY_STORAGE_PROVIDER) 和 NCryptEnumKeys https://docs.microsoft.com/en-us/windows/win32/api/ncrypt/nf-ncrypt-ncryptenumkeys 的 C++ 方法

  1. 我想排除属于智能卡但实际上不存在于读卡器中的密钥。

    一般来说,遍历证书存储区中的证书的方法对我来说都失败了,因为 Windows 会弹出一个对话框,要求插入一张不再可用的卡。也许有办法排除这样的证书,但我不知道方法。

因此,通过提供者迭代密钥的方法似乎更有意义。

oldnewthing(值得感谢多年来提供有用的帖子)有一个https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/SmartCard,但它没有显示这个确切的功能。

https://docs.microsoft.com/en-us/windows/uwp/security/smart-cards 解释了 UWP 如何与智能卡交互,包括 APDU 传输,但没有解释如何在高级别迭代非对称密钥。

提前感谢您的建议...

附言这是查看 Cert Store 并获取密钥的代码类型。

            X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection certs = (X509Certificate2Collection)store.Certificates;
            int n = certs.Count;
            msg += " has " + n.ToString() + " keys: ";

            foreach (X509Certificate2 cert in certs)
            {
                if (!cert.HasPrivateKey) continue;

                RSACng rsaCng = (RSACng)cert.GetRSAPrivateKey();
                if (null == rsaCng) continue;
                if (rsaCng.Key.Provider != CngProvider.MicrosoftSmartCardKeyStorageProvider) continue;
                msg += rsaCng.Key.KeyName + ", ";
            }

但我不知道如何将其限制为当前存在的键。

【问题讨论】:

  • 你用过什么类型的智能卡,视觉智能卡还是物理智能卡?
  • 我使用的是物理智能卡,而不是虚拟的。
  • 看起来只有你提到的 NCryptOpenStorageProvider api 可以访问公钥并从它可以为 UWP 平台工作的文档派生。

标签: c# windows api uwp smartcard


【解决方案1】:

您可以使用 SCard* 函数枚举读取器,然后将 NCryptEnumKeys() 调用的 pszScope 参数设置为 L"\\\\.\\<ReaderName>\\"

注意:此参数在 MSDN 中记录为“未使用;设置为 NULL”!

https://social.msdn.microsoft.com/Forums/en-US/e99d3980-ae4f-4632-8dad-565e098dc6dd/how-to-enumerate-keys-on-all-connected-smart-cards-using-cng

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多