summary refs log tree commit diff
path: root/crypto/src/asn1/cms/SignerInfo.cs
blob: 5866f98da222140d8a4f4a64d24c62483435d9bb (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Cms
{
    public class SignerInfo
        : Asn1Encodable
    {
        public static SignerInfo GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is SignerInfo signerInfo)
                return signerInfo;
            return new SignerInfo(Asn1Sequence.GetInstance(obj));
        }

        public static SignerInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new SignerInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        private readonly DerInteger m_version;
        private readonly SignerIdentifier m_sid;
        private readonly AlgorithmIdentifier m_digAlgorithm;
        private readonly Asn1Set m_authenticatedAttributes;
        private readonly AlgorithmIdentifier m_digEncryptionAlgorithm;
        private readonly Asn1OctetString m_encryptedDigest;
        private readonly Asn1Set m_unauthenticatedAttributes;

        public SignerInfo(SignerIdentifier sid, AlgorithmIdentifier digAlgorithm, Attributes authenticatedAttributes,
            AlgorithmIdentifier digEncryptionAlgorithm, Asn1OctetString encryptedDigest,
            Attributes unauthenticatedAttributes)
            : this(sid, digAlgorithm, Asn1Set.GetInstance(authenticatedAttributes), digEncryptionAlgorithm,
                  encryptedDigest, Asn1Set.GetInstance(unauthenticatedAttributes))
        {
        }

        public SignerInfo(SignerIdentifier sid, AlgorithmIdentifier digAlgorithm, Asn1Set authenticatedAttributes,
            AlgorithmIdentifier digEncryptionAlgorithm, Asn1OctetString encryptedDigest,
            Asn1Set unauthenticatedAttributes)
        {
            m_sid = sid ?? throw new ArgumentNullException(nameof(sid));
            m_digAlgorithm = digAlgorithm ?? throw new ArgumentNullException(nameof(digAlgorithm));
            m_authenticatedAttributes = authenticatedAttributes;
            m_digEncryptionAlgorithm = digEncryptionAlgorithm ?? throw new ArgumentNullException(nameof(digEncryptionAlgorithm));
            m_encryptedDigest = encryptedDigest ?? throw new ArgumentNullException(nameof(encryptedDigest));
            m_unauthenticatedAttributes = unauthenticatedAttributes;
            m_version = sid.IsTagged ? DerInteger.Three : DerInteger.One;
        }

        private SignerInfo(Asn1Sequence seq)
        {
            int count = seq.Count, pos = 0;
            if (count < 5 || count > 7)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            m_version = DerInteger.GetInstance(seq[pos++]);
            m_sid = SignerIdentifier.GetInstance(seq[pos++]);
            m_digAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]);
            m_authenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1Set.GetTagged);
            m_digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]);
            m_encryptedDigest = Asn1OctetString.GetInstance(seq[pos++]);
            m_unauthenticatedAttributes = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, Asn1Set.GetTagged);

            if (pos != count)
                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
        }

        public DerInteger Version => m_version;

        public SignerIdentifier SignerID => m_sid;

        public Asn1Set AuthenticatedAttributes => m_authenticatedAttributes;

        public AlgorithmIdentifier DigestAlgorithm => m_digAlgorithm;

        public Asn1OctetString EncryptedDigest => m_encryptedDigest;

        public AlgorithmIdentifier DigestEncryptionAlgorithm => m_digEncryptionAlgorithm;

        public Asn1Set UnauthenticatedAttributes => m_unauthenticatedAttributes;

        /**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         *  SignerInfo ::= Sequence {
         *      version Version,
         *      SignerIdentifier sid,
         *      digestAlgorithm DigestAlgorithmIdentifier,
         *      authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
         *      digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
         *      encryptedDigest EncryptedDigest,
         *      unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
         *  }
         *
         *  EncryptedDigest ::= OCTET STRING
         *
         *  DigestAlgorithmIdentifier ::= AlgorithmIdentifier
         *
         *  DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(7);
            v.Add(m_version, m_sid, m_digAlgorithm);
            v.AddOptionalTagged(false, 0, m_authenticatedAttributes);
            v.Add(m_digEncryptionAlgorithm, m_encryptedDigest);
            v.AddOptionalTagged(false, 1, m_unauthenticatedAttributes);
            return new DerSequence(v);
        }
    }
}