CryptoPro / corefx

This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others. We welcome contributions.
https://github.com/dotnet/core
MIT License
27 stars 7 forks source link

Getting SignerInfo.SignedAttributes throws an exception #23

Closed AlexeyAKat closed 4 years ago

AlexeyAKat commented 4 years ago

The following test code works on Windows and throws an exception on linux:

var contentInfo = new ContentInfo(new Oid("1.2.840.113549.1.7.1"), data);
var signedCms = new SignedCms(contentInfo, true);

// signerCert - X509Certificate2 object of Gost certificate
var cmsSigner = new CmsSigner(signerCert) { IncludeOption = X509IncludeOption.EndCertOnly };

cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime());
signedCms.ComputeSignature(cmsSigner);

var signerInfo = signedCms.SignerInfos[0];

// throws an exception
var signedAttributes = signerInfo.SignedAttributes;
Exception stack trace:
"System.Reflection.TargetInvocationException" in System.Private.CoreLib.dll: 'Exception has been thrown by the target of an invocation.'
---> System.Security.Cryptography.CryptographicException: The OID value is invalid.
   at System.Security.Cryptography.Oid.FromOidValue(String oidValue, OidGroup group)
   at System.Security.Cryptography.Pkcs.Pkcs9ContentType..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance[T]()
   at Internal.Cryptography.PkcsHelpers.Upgrade[T](Pkcs9AttributeObject basicAttribute)
   at Internal.Cryptography.PkcsHelpers.CreateBestPkcs9AttributeObjectAvailable(Oid oid, Byte[] encodedAttribute)
   at System.Security.Cryptography.Pkcs.SignerInfo.MakeAttribute(AttributeAsn attribute)
   at System.Security.Cryptography.Pkcs.SignerInfo.MakeAttributeCollection(AttributeAsn[] attributes)
   at System.Security.Cryptography.Pkcs.SignerInfo.get_SignedAttributes()

Also for this code:

var contentInfo = new ContentInfo(data);
"System.Security.Cryptography.CryptographicException" in System.Security.Cryptography.Encoding.dll: 'The OID value is invalid.'
   в System.Security.Cryptography.Oid.FromOidValue(String oidValue, OidGroup group)
   в System.Security.Cryptography.Pkcs.ContentInfo..ctor(Byte[] content)

and this:

var signedCms = new SignedCms();
"System.TypeInitializationException" in System.Security.Cryptography.Pkcs.dll: 'The type initializer for 'System.Security.Cryptography.Pkcs.SignedCms' threw an exception.'
---> System.Security.Cryptography.CryptographicException : The OID value is invalid.
   в System.Security.Cryptography.Oid.FromOidValue(String oidValue, OidGroup group)
   в System.Security.Cryptography.Pkcs.SignedCms..cctor()

OS: Ubuntu 18.04.4 LTS, .net core 3.1 (SDK 3.1.202, runtime 3.1.4), Crypto-Pro CSP 5.0.11455 KC1, instruction Linux.md was used, corefx ver.3.1.1.241 (4.7.0-dev.20175.1) from link .

tolyanich commented 4 years ago

To work with constructors with implicit oids (like ContentInfo(), SignedCMS()) you need to update cryptoprovider to lates one. You can download it from this page

AlexeyAKat commented 4 years ago

Thanks, it worked! Installed version of Crypto-Pro CSP: 5.0.11732 .