I am attempting to create a HttpRequestStream with PKCS#7 SignedData MIME entity. However, I have not been able to create it with a valid signature.
// first, create the root MIME entity and add the payload entity to it
MimeEntity meMultipartSigned = new MimeEntity();
meMultipartSigned.ContentType = new ContentType(MediaTypeNames.Multipart.Signed);
meMultipartSigned.ContentType.Parameters.Add("protocol", "application/pkcs7-signature");
meMultipartSigned.ContentType.Parameters.Add("micalg", "sha-1");
MimeEntity mePayload = new MimeEntity();
_payload.Seek(0, 0);
mePayload.SetContent(_payload, "", "application/xml", TransferEncoding.Binary);
meMultipartSigned.Parts.Add(mePayload);
mePayload.ReadOnly = true;
// now sign the data in the payload entity
ContentInfo contentInfo = new ContentInfo(Utility.IO.DataHandling.ReadFully(mePayload.GetContentStream())); // is there a more efficient way?
SignedData signedData = new SignedData(contentInfo, true);
signedData.IncludeOption = CertificateIncludeOption.EndCertificateOnly;
SignerInfo signer = new SignerInfo(_certificate, SubjectIdentifierType.IssuerAndSerialNumber, SignatureHashAlgorithm.SHA1);
signedData.SignerInfos.Add(signer);
signedData.Sign(SignatureOptions.SkipCertificateUsageCheck);
// store the signed data
MimeEntity meSignedData = new MimeEntity();
meSignedData.ContentType = new ContentType("application/pkcs7-signature");
meSignedData.SetContent(new MemoryStream(signedData.Encode()),"smime.7ps","application/pkcs7-signature",TransferEncoding.Base64);
meSignedData.ContentType.Parameters.Add("smime-type","signed-data");
meMultipartSigned.Parts.Add(meSignedData);
MemoryStream ms = new MemoryStream();
meMultipartSigned.Save(ms);
ms.Seek(0, 0);
this.Content = new StreamContent(ms);
I am then validating, by using this function, which I have verified works with an object signed by an external process.
public bool ValidateSignature()
{
MimeEntity me = new MimeEntity();
me.Load(this.Content.ReadAsStreamAsync().Result);
ValidationOptions vo = ValidationOptions.IgnoreWrongUsage;
SignatureValidationResult validationResult = me.ValidateSignature(false,vo);
return validationResult.Valid;
}
What is the correct way to ensemble this multipart/signed entity and sign it correctly?