CMS Signature Example with BouncyCastle for .NET
You can use the BouncyCastle crypto library for .NET, which contains its own X509 certificate and CMS signing mechanisms. Many examples and documentation on the Internet are for Java, since BouncyCastle was originally a Java library. I used https://stackoverflow.com/a/3609777/ ... as a starting point for downloading the certificate and key and added the CMS signature. You may need to adjust the settings to get the results that you want to use to use.
I made the signature function about the same as yours, but note that the private key is now a separate parameter.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Org.BouncyCastle.Cms; using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.X509.Store; class Program { protected static byte[] SignWithSystem(byte[] data, AsymmetricKeyParameter key, X509Certificate cert, X509Certificate[] chain) { var generator = new CmsSignedDataGenerator();
.NET CMS (Quick-fix with the rest of the chain skipped from the signature)
I can reproduce your problem with a certificate whose root is not in the trust store, and make sure that adding a certificate chain to the cmsSigner / signedCms Certificates collection does not avoid the error A certificate chain could not be built to a trusted root authority .
You can successfully complete the registration by setting cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;
However, if you do this, you will not get the rest of the chain in the signature. This is probably not what you want.
As an aside, in your example, you use X509Certificate for the array of certificates in the chain, but pass them to X509Certificate2Collection (note the β2β there). X509Certificate2 comes from X509Certificate , but if it really is not X509Certificate2 , which you put in one of these collections, you will receive a cast error if something iterates over the collection (you do not get an error when adding, unfortunately, the certificate is of the wrong type, because that X509Certificate2Collection also comes from X509CertificateCollection and inherits its add methods).
softwariness
source share