summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorKonstantin Kretov <konstantin.kretov@jetbrains.com>2023-12-28 15:23:55 +0100
committerAlexander Scheel <alexander.scheel@keyfactor.com>2024-02-12 11:08:13 -0500
commit2a508f3ffe7843efcd188f6349f3125a25158351 (patch)
tree517e04edb9060d26aae2719d6b71dbf832c4923f /crypto/src
parentTest invalid DerGeneralizedTime with explicit zone (diff)
downloadBouncyCastle.NET-ed25519-2a508f3ffe7843efcd188f6349f3125a25158351.tar.xz
Added support for PKCS7 signed content in CMS. Port from Java BC. #310 #232
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/cms/CMSSignedData.cs13
-rw-r--r--crypto/src/cms/Pkcs7ProcessableObject.cs48
2 files changed, 59 insertions, 2 deletions
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