summary refs log tree commit diff
path: root/crypto/src/asn1/tsp/EncryptionInfo.cs
blob: af3741ca913f6a37fdfdd998ad897fcb449ad132 (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
using System;

namespace Org.BouncyCastle.Asn1.Tsp
{
    /**
     * Implementation of the EncryptionInfo element defined in RFC 4998:
     * <p/>
     * 1988 ASN.1 EncryptionInfo
     * <p/>
     * EncryptionInfo       ::=     SEQUENCE {
     * encryptionInfoType     OBJECT IDENTIFIER,
     * encryptionInfoValue    ANY DEFINED BY encryptionInfoType
     * }
     * <p/>
     * 1997-ASN.1 EncryptionInfo
     * <p/>
     * EncryptionInfo       ::=     SEQUENCE {
     * encryptionInfoType   ENCINFO-TYPE.&amp;id
     * ({SupportedEncryptionAlgorithms}),
     * encryptionInfoValue  ENCINFO-TYPE.&amp;Type
     * ({SupportedEncryptionAlgorithms}{&#064;encryptionInfoType})
     * }
     * <p/>
     * ENCINFO-TYPE ::= TYPE-IDENTIFIER
     * <p/>
     * SupportedEncryptionAlgorithms ENCINFO-TYPE ::= {...}
     */
    public class EncryptionInfo
        : Asn1Encodable
    {
        public static EncryptionInfo GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is EncryptionInfo encryptionInfo)
                return encryptionInfo;
            return new EncryptionInfo(Asn1Sequence.GetInstance(obj));
        }

        public static EncryptionInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new EncryptionInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));

        public static EncryptionInfo GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new EncryptionInfo(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));

        /**
         * The OID for EncryptionInfo type.
         */
        private readonly DerObjectIdentifier m_encryptionInfoType;

        /**
         * The value of EncryptionInfo
         */
        private readonly Asn1Encodable m_encryptionInfoValue;

        private EncryptionInfo(Asn1Sequence seq)
        {
            int count = seq.Count;
            if (count != 2)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            m_encryptionInfoType = DerObjectIdentifier.GetInstance(seq[0]);
            m_encryptionInfoValue = seq[1];
        }

        public EncryptionInfo(DerObjectIdentifier encryptionInfoType, Asn1Encodable encryptionInfoValue)
        {
            m_encryptionInfoType = encryptionInfoType ?? throw new ArgumentNullException(nameof(encryptionInfoType));
            m_encryptionInfoValue = encryptionInfoValue ?? throw new ArgumentNullException(nameof(encryptionInfoValue));
        }

        public virtual DerObjectIdentifier EncryptionInfoType => m_encryptionInfoType;

        public virtual Asn1Encodable EncryptionInfoValue => m_encryptionInfoValue;

        public override Asn1Object ToAsn1Object() => new DLSequence(m_encryptionInfoType, m_encryptionInfoValue);
    }
}