I have to digitally sign some data in C# using a certificate and private key. The certificate that I'm using has a custom root CA as well as a custom intermediate CA.
I can use code like this to do the signing without any problem, if the root and intermediate CAs are installed into the Windows certificate stores:
var content = new ContentInfo(manifest);
var cms = new SignedCms(content, true);
var signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, myCertificate);
signer.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now));
cms.ComputeSignature(signer);
return cms.Encode();
Unfortunately, howeber, I am not able to install the root and intermediate CAs into the server's certificate store (I'm using Azure Web Apps). I'm trying to work out a way to sign by using the root and intermediate CA certificates if they are stored on disk instead. I thought I might be able to do something like this before calling cms.Encode()
:
// add CAs from disk
var intermediateCACertificate = new X509Certificate2(@"pathToIntermediateCertificate.cer");
signer.Certificates.Add(intermediateCACertificate);
var rootCACertificate = new X509Certificate2(@"pathToRootCertificate.cer");
signer.Certificates.Add(rootCACertificate);
But when I execute this I get a CryptographicException thrown "A certificate chain could not be built to a trusted root authority."
.
Is it possible to digitally sign using non-standard CAs without installing them into the Windows certificate store?