From 2a508f3ffe7843efcd188f6349f3125a25158351 Mon Sep 17 00:00:00 2001 From: Konstantin Kretov Date: Thu, 28 Dec 2023 15:23:55 +0100 Subject: Added support for PKCS7 signed content in CMS. Port from Java BC. #310 #232 --- crypto/src/cms/CMSSignedData.cs | 13 +++++++-- crypto/src/cms/Pkcs7ProcessableObject.cs | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 crypto/src/cms/Pkcs7ProcessableObject.cs (limited to 'crypto/src') diff --git a/crypto/src/cms/CMSSignedData.cs b/crypto/src/cms/CMSSignedData.cs index a19fe27ab..77da9b95d 100644 --- a/crypto/src/cms/CMSSignedData.cs +++ b/crypto/src/cms/CMSSignedData.cs @@ -5,6 +5,7 @@ using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.cms; using Org.BouncyCastle.Operators.Utilities; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.X509; @@ -115,8 +116,16 @@ namespace Org.BouncyCastle.Cms // if (signedData.EncapContentInfo.Content != null) { - this.signedContent = new CmsProcessableByteArray( - ((Asn1OctetString)signedData.EncapContentInfo.Content).GetOctets()); + if (signedData.EncapContentInfo.Content is Asn1OctetString) + { + signedContent = new CmsProcessableByteArray( + ((Asn1OctetString)(signedData.EncapContentInfo.Content)).GetOctets()); + } + else + { + signedContent = new Pkcs7ProcessableObject(signedData.EncapContentInfo.ContentType, + signedData.EncapContentInfo.Content); + } } // else // { diff --git a/crypto/src/cms/Pkcs7ProcessableObject.cs b/crypto/src/cms/Pkcs7ProcessableObject.cs new file mode 100644 index 000000000..1042b6ff5 --- /dev/null +++ b/crypto/src/cms/Pkcs7ProcessableObject.cs @@ -0,0 +1,48 @@ +using System.IO; +using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Cms; + +namespace Org.BouncyCastle.cms +{ + public class Pkcs7ProcessableObject : CmsProcessable + { + public DerObjectIdentifier ContentType { get; } + public Asn1Encodable Content { get; } + + public Pkcs7ProcessableObject(DerObjectIdentifier contentType, Asn1Encodable content) + { + ContentType = contentType; + Content = content; + } + + public void Write(Stream outStream) + { + using (var sw = new BinaryWriter(outStream)) + { + if (Content is Asn1Sequence) + { + Asn1Sequence seq = Asn1Sequence.GetInstance(Content); + + foreach (Asn1Encodable enc in seq) + { + sw.Write(enc.ToAsn1Object().GetEncoded(Asn1Encodable.Der)); + } + } + else + { + byte[] encoded = Content.ToAsn1Object().GetEncoded(Asn1Encodable.Der); + int index = 1; + while ((encoded[index] & 0xff) > 127) + { + index++; + } + + index++; + sw.Write(encoded, index, encoded.Length - index); + } + } + } + + public object GetContent() => Content; + } +} \ No newline at end of file -- cgit 1.4.1