summary refs log tree commit diff
path: root/crypto/src/cms/Pkcs7ProcessableObject.cs
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/cms/Pkcs7ProcessableObject.cs
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 '')
-rw-r--r--crypto/src/cms/Pkcs7ProcessableObject.cs48
1 files changed, 48 insertions, 0 deletions
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