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

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Cms
{
    /**
     *   KEMRecipientInfo ::= SEQUENCE {
     *     version CMSVersion,  -- always set to 0
     *     rid RecipientIdentifier,
     *     kem KEMAlgorithmIdentifier,
     *     kemct OCTET STRING,
     *     kdf KeyDerivationAlgorithmIdentifier,
     *     kekLength INTEGER (1..MAX),
     *     ukm [0] EXPLICIT UserKeyingMaterial OPTIONAL,
     *     wrap KeyEncryptionAlgorithmIdentifier,
     *     encryptedKey EncryptedKey }
     */
    public sealed class KemRecipientInfo
        : Asn1Encodable
    {
        public static KemRecipientInfo GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is KemRecipientInfo kemRecipientInfo)
                return kemRecipientInfo;
            return new KemRecipientInfo(Asn1Sequence.GetInstance(obj));
        }

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

        private static readonly DerInteger V1 = DerInteger.Zero;

        private readonly DerInteger m_cmsVersion;
        private readonly RecipientIdentifier m_rid;
        private readonly AlgorithmIdentifier m_kem;
        private readonly Asn1OctetString m_kemct;
        private readonly AlgorithmIdentifier m_kdf;
        private readonly DerInteger m_kekLength;
        private readonly Asn1OctetString m_ukm;
        private readonly AlgorithmIdentifier m_wrap;
        private readonly Asn1OctetString m_encryptedKey;

        public KemRecipientInfo(RecipientIdentifier rid, AlgorithmIdentifier kem, Asn1OctetString kemct,
            AlgorithmIdentifier kdf, DerInteger kekLength, Asn1OctetString ukm, AlgorithmIdentifier wrap,
            Asn1OctetString encryptedKey)
        {
            m_cmsVersion = V1;
            m_rid = rid ?? throw new ArgumentNullException(nameof(rid));
            m_kem = kem ?? throw new ArgumentNullException(nameof(kem));
            m_kemct = kemct ?? throw new ArgumentNullException(nameof(kemct));
            m_kdf = kdf ?? throw new ArgumentNullException(nameof(kdf));
            m_kekLength = kekLength ?? throw new ArgumentNullException(nameof(kekLength));
            m_ukm = ukm;
            m_wrap = wrap ?? throw new ArgumentNullException(nameof(wrap));
            m_encryptedKey = encryptedKey ?? throw new ArgumentNullException(nameof(encryptedKey));
        }

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

            m_cmsVersion = DerInteger.GetInstance(seq[pos++]);
            m_rid = RecipientIdentifier.GetInstance(seq[pos++]);
            m_kem = AlgorithmIdentifier.GetInstance(seq[pos++]);
            m_kemct = Asn1OctetString.GetInstance(seq[pos++]);
            m_kdf = AlgorithmIdentifier.GetInstance(seq[pos++]);
            m_kekLength = DerInteger.GetInstance(seq[pos++]);
            m_ukm = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1OctetString.GetInstance);
            m_wrap = AlgorithmIdentifier.GetInstance(seq[pos++]);
            m_encryptedKey = Asn1OctetString.GetInstance(seq[pos++]);

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

            if (!m_cmsVersion.HasValue(0))
                throw new ArgumentException("Unsupported version (hex): " + m_cmsVersion.Value.ToString(16));
        }

        public RecipientIdentifier RecipientIdentifier => m_rid;

        public AlgorithmIdentifier Kem => m_kem;

        public Asn1OctetString Kemct => m_kemct;

        public AlgorithmIdentifier Kdf => m_kdf;

        public AlgorithmIdentifier Wrap => m_wrap;

        public Asn1OctetString Ukm => m_ukm;

        public Asn1OctetString EncryptedKey => m_encryptedKey;

        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(9);
            v.Add(m_cmsVersion, m_rid, m_kem, m_kemct, m_kdf, m_kekLength);
            v.AddOptionalTagged(true, 0, m_ukm);
            v.Add(m_wrap, m_encryptedKey);
            return new DerSequence(v);
        }
    }
}