Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Signer.cs #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions FirmaXadesNet/Crypto/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,38 @@ public void Dispose()

private void SetSigningKey(X509Certificate2 certificate)
{
var key = (RSACryptoServiceProvider)certificate.PrivateKey;

if (key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_STRONG_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_ENHANCED_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_RSA_SCHANNEL_PROV)
try // catch if the PrivateKey is not of RSACryptoServiceProvider type
{
Type CspKeyContainerInfo_Type = typeof(CspKeyContainerInfo);

FieldInfo CspKeyContainerInfo_m_parameters = CspKeyContainerInfo_Type.GetField("m_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
CspParameters parameters = (CspParameters)CspKeyContainerInfo_m_parameters.GetValue(key.CspKeyContainerInfo);

var cspparams = new CspParameters(CryptoConst.PROV_RSA_AES, CryptoConst.MS_ENH_RSA_AES_PROV, key.CspKeyContainerInfo.KeyContainerName);
cspparams.KeyNumber = parameters.KeyNumber;
cspparams.Flags = parameters.Flags;
_signingKey = new RSACryptoServiceProvider(cspparams);

_disposeCryptoProvider = true;
var key = (RSACryptoServiceProvider)certificate.PrivateKey;

if (key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_STRONG_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_ENHANCED_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_RSA_SCHANNEL_PROV)
{
Type CspKeyContainerInfo_Type = typeof(CspKeyContainerInfo);

FieldInfo CspKeyContainerInfo_m_parameters = CspKeyContainerInfo_Type.GetField("m_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
CspParameters parameters = (CspParameters)CspKeyContainerInfo_m_parameters.GetValue(key.CspKeyContainerInfo);

var cspparams = new CspParameters(CryptoConst.PROV_RSA_AES, CryptoConst.MS_ENH_RSA_AES_PROV, key.CspKeyContainerInfo.KeyContainerName);
cspparams.KeyNumber = parameters.KeyNumber;
cspparams.Flags = parameters.Flags;
_signingKey = new RSACryptoServiceProvider(cspparams);

_disposeCryptoProvider = true;
}
else
{
_signingKey = key;
_disposeCryptoProvider = false;
}
}
else
catch (InvalidCastException e)
{
_signingKey = key;
_signingKey = certificate.PrivateKey; // set the private key without casting
_disposeCryptoProvider = false;
}
}
}

#endregion
Expand Down