gpt4 book ai didi

wcf - 如何提供 PIN 以编程方式访问智能卡?

转载 作者:行者123 更新时间:2023-12-01 04:20:29 27 4
gpt4 key购买 nike

我正在使用证书来保护客户端和服务器之间的通信(没有代码,只是端点配置)。证书目前存储在 ACOS5 智能卡中。一切都很好,只是每次当 WCF 创建一个新的 channel 来访问服务器时,ACOS5 驱动程序要求用户输入“用户 PIN”。不幸的是,它经常发生。

有没有办法配置驱动程序来缓存用户已经在当前进程中输入的 PIN 至少一段时间,或者我如何缓存 pin 并在同一 session 中每次以编程方式提供它?

我在这个 article 中发现了一些有用的东西:

This is because in previous versions of Windows each CSP would cache the PIN you entered, but Windows 7 actually converts the PIN to a secure token and caches that. Unfortunately there’s only one global token cache but the CSPs can’t use tokens generated by others, so first the smart card CSP prompts you and caches a token, then SSL prompts you and caches its own token (overwriting the first one), then the smart card system prompts you again (because its cached token is gone).



但是我不能使用作者提出的解决方案。所以我该怎么做?

最佳答案

这是我们多年来在主要应用程序中发现和使用的一种方式:

static class X509Certificate2Extension
{
public static void SetPinForPrivateKey(this X509Certificate2 certificate, string pin)
{
if (certificate == null) throw new ArgumentNullException("certificate");
var key = (RSACryptoServiceProvider)certificate.PrivateKey;

var providerHandle = IntPtr.Zero;
var pinBuffer = Encoding.ASCII.GetBytes(pin);

// provider handle is implicitly released when the certificate handle is released.
SafeNativeMethods.Execute(() => SafeNativeMethods.CryptAcquireContext(ref providerHandle,
key.CspKeyContainerInfo.KeyContainerName,
key.CspKeyContainerInfo.ProviderName,
key.CspKeyContainerInfo.ProviderType,
SafeNativeMethods.CryptContextFlags.Silent));
SafeNativeMethods.Execute(() => SafeNativeMethods.CryptSetProvParam(providerHandle,
SafeNativeMethods.CryptParameter.KeyExchangePin,
pinBuffer, 0));
SafeNativeMethods.Execute(() => SafeNativeMethods.CertSetCertificateContextProperty(
certificate.Handle,
SafeNativeMethods.CertificateProperty.CryptoProviderHandle,
0, providerHandle));
}
}

internal static class SafeNativeMethods
{
internal enum CryptContextFlags
{
None = 0,
Silent = 0x40
}

internal enum CertificateProperty
{
None = 0,
CryptoProviderHandle = 0x1
}

internal enum CryptParameter
{
None = 0,
KeyExchangePin = 0x20
}

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
string containerName,
string providerName,
int providerType,
CryptContextFlags flags
);

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CryptSetProvParam(
IntPtr hProv,
CryptParameter dwParam,
[In] byte[] pbData,
uint dwFlags);

[DllImport("CRYPT32.DLL", SetLastError = true)]
internal static extern bool CertSetCertificateContextProperty(
IntPtr pCertContext,
CertificateProperty propertyId,
uint dwFlags,
IntPtr pvData
);

public static void Execute(Func<bool> action)
{
if (!action())
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}

完整的帖子和作者在这里:
http://www.infinitec.de/post/2010/11/22/Setting-the-PIN-of-a-smartcard-programmatically.aspx

关于wcf - 如何提供 PIN 以编程方式访问智能卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com