summary refs log tree commit diff
path: root/crypto/src/asn1/tsp/EncryptionInfo.cs
blob: e712c6a000de8d9dd21820aec6c70d161f5d3fa2 (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
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)
        {
            return new EncryptionInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

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

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

        private EncryptionInfo(Asn1Sequence sequence)
        {
            if (sequence.Count != 2)
                throw new ArgumentException("wrong sequence size in constructor: " + sequence.Count, nameof(sequence));

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

        public EncryptionInfo(DerObjectIdentifier encryptionInfoType, Asn1Encodable encryptionInfoValue)
        {
            m_encryptionInfoType = encryptionInfoType;
            m_encryptionInfoValue = encryptionInfoValue;
        }

        public virtual DerObjectIdentifier EncryptionInfoType => m_encryptionInfoType;

        public virtual Asn1Encodable EncryptionInfoValue => m_encryptionInfoValue;

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