summary refs log tree commit diff
path: root/crypto/src/cms/Pkcs7ProcessableObject.cs
blob: 2fe37632a98df065f9640f955a629067b98603e5 (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;
  }
}