I'm getting a PKCS#7/CMS container from a service I'm using that includes a user certificate and a signature.
I am able to see the single certificate using SignedCms
var cert = GetCertFromServer();
SignedCms signedCms = new SignedCms();
signedCms.Decode(cert);
// The user certificate
X509Certificate2 userCert = signedCms.Certificates[0];
But I need to add the intermediate and root certificate to that chain. SignedCms.Certificates
seems to be immutable, so I can't add the certificates directly to the collection and I am not able to replace the certificates collection the following way
// root, inter and leaf certs are X509Certificate2 objects read from files
X509Certificate2[] certArr = new[] {rootCert, interCert, leafCert, userCert};
X509Certificate2Collection chain = new X509Certificate2Collection(certArr);
signedCms.Certificates = chain; // Error: No setter
Since SignedCms.Certificates
has no setter. I haven't been able to find a way to create a new container from this one. I have also not had any luck finding info on how to do this in bouncy castle or any other library.
I need to have a container containing the certificate chain and a signature and write the bytes from that container to a PDF file I'm signing. Is there a way to add certificates to the chain in the container?