X509Certificate2访问安全令牌中的私钥

3

我必须使用存储在安全令牌中的证书。我可以从Windows证书存储中访问它,但该设备有一个密码,因此会显示一个带有输入字段的弹出窗口。

以下是我用于加载证书的代码:

static X509Certificate2 BuscarCertificado
    (StoreLocation location, StoreName name, 
    X509FindType findType, string findValue)
{
    X509Store store = new X509Store(name, location);
    try{
        store.Open(OpenFlags.ReadOnly);

        X509Certificate2Collection col = store.Certificates.Find
            (findType, findValue, true);

        return col[0];
    }
    finally { store.Close(); }
}

设备是ACS CryptoMate64 0。
是否可能在代码中发送密码,以避免显示此消息?
感谢任何帮助。

不,这是不可能的。 - Crypt32
@CryptoGuy,那是一个有趣的理论,但你能证明它吗? - jariq
请忽略我的评论。Pepo的代码应该适用于您。但是,它需要在代码中存储PIN(不建议这样做)。 - Crypt32
1个回答

5

我没有ACS CryptoMate64 0。但是这段代码适用于西门子CardOS v4.3B(驱动程序CardOS API v5.2 build 15)。您需要检查它是否也适用于您的设备。

using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace SignWithToken
{
    class Program
    {
        static void Main(string[] args)
        {
            // ------ select certificate for signing ---------
            // open store
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.MaxAllowed);

            // find cert by thumbprint
            var foundCerts = store.Certificates.Find(X509FindType.FindByThumbprint, "44 df b8 96 73 55 e4 e2 56 3a c0 a2 e0 66 8e 52 8a 3a 4a f4", true);

            if (foundCerts.Count == 0)
                return;

            var certForSigning = foundCerts[0];
            store.Close();

            // -------- prepare private key with password --------
            // prepare password
            var pass = new SecureString();
            for(var i=0;i<8;i++)
                pass.AppendChar('1');

            // take private key
            var privateKey = certForSigning.PrivateKey as RSACryptoServiceProvider;

            // make new CSP parameters based on parameters from current private key but throw in password
            CspParameters cspParameters = new CspParameters(privateKey.CspKeyContainerInfo.ProviderType,
                privateKey.CspKeyContainerInfo.ProviderName,
                privateKey.CspKeyContainerInfo.KeyContainerName,
                null,
                pass);

            // make RSA crypto provider based on given CSP parameters
            var rsaCsp = new RSACryptoServiceProvider(cspParameters);

            // set modified RSA crypto provider back
            certForSigning.PrivateKey = rsaCsp;

            // ---- Sign -----
            // prepare content to be signed
            ContentInfo content = new ContentInfo(new byte[] {0x01, 0x02, 0x03});
            SignedCms cms = new SignedCms(content);

            // prepare CMS signer 
            CmsSigner signer = new CmsSigner(certForSigning);

            // sign to PKCS#7
            cms.ComputeSignature(signer);

            // get encoded PKCS#7 value
            var result = cms.Encode();

            // ------ Verify signature ------
            SignedCms cmsToVerify = new SignedCms();
            // decode signed PKCS#7
            cmsToVerify.Decode(result);

            // check signature of PKCS#7
            cmsToVerify.CheckSignature(true);
        }
    }
}

1
eToken Pro是否有CSP或者minidriver?西门子5.2有一个minidriver。但是西门子CardOS API v3.2有一个CSP。我必须添加以下内容才能使其工作:在cspParameters初始化后,cspParameters.KeyNumber = privateKey.CspKeyContainerInfo.KeyNumber; cspParameters.Flags = CspProviderFlags.UseExistingKey; - pepo
如果在cspParameters中设置KeyNumber和Flags,它是否有效?我只有西门子CardOS... - pepo
我很高兴。但是仅当它是CSP时,才应设置其他属性。使用minidriver时,可能会突然停止工作。 - pepo
这段代码在执行以下这行代码时会生成“System.Security.Cryptography.CryptographicException: Access is denied.”的错误:certForSigning.PrivateKey = rsaCsp; 请问需要哪些权限来解决这个问题? - Shashi Jeevan M. P.
@pepo 谢谢您的回复。我已尝试使用管理员特权运行它,但没有成功。这个错误出现在重新分配PrivateKey的那一行。我能否使用克隆方法重新创建X509Certificate2对象?如果可以,正确的克隆方法是什么? - Shashi Jeevan M. P.
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接