summary refs log tree commit diff
path: root/crypto/src/cms/Pkcs7ProcessableObject.cs
blob: 1042b6ff5d779fe575ed2d1066b83f8a1dd8e406 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
  }
}