summary refs log tree commit diff
path: root/crypto/src/asn1/cms/RecipientInfo.cs
blob: ff93258b650652309379f33127291ef49dcf3ccf (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Cms
{
    public class RecipientInfo
        : Asn1Encodable, IAsn1Choice
    {
        public static RecipientInfo GetInstance(object o)
        {
            if (o == null)
                return null;

            if (o is RecipientInfo recipientInfo)
                return recipientInfo;

            if (o is Asn1Sequence sequence)
                return new RecipientInfo(sequence);

            if (o is Asn1TaggedObject taggedObject)
                return new RecipientInfo(taggedObject);

            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(o), nameof(o));
        }

        public static RecipientInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return Asn1Utilities.GetInstanceFromChoice(taggedObject, declaredExplicit, GetInstance);
        }

        internal Asn1Encodable info;

		public RecipientInfo(
            KeyTransRecipientInfo info)
        {
            this.info = info;
        }

		public RecipientInfo(
            KeyAgreeRecipientInfo info)
        {
            this.info = new DerTaggedObject(false, 1, info);
        }

		public RecipientInfo(
            KekRecipientInfo info)
        {
            this.info = new DerTaggedObject(false, 2, info);
        }

		public RecipientInfo(
            PasswordRecipientInfo info)
        {
            this.info = new DerTaggedObject(false, 3, info);
        }

		public RecipientInfo(
            OtherRecipientInfo info)
        {
            this.info = new DerTaggedObject(false, 4, info);
        }

		public RecipientInfo(
            Asn1Object   info)
        {
            this.info = info;
        }

		public DerInteger Version
        {
			get
			{
				if (info is Asn1TaggedObject o)
				{
					switch (o.TagNo)
					{
					case 1:
						return KeyAgreeRecipientInfo.GetInstance(o, false).Version;
					case 2:
						return GetKekInfo(o).Version;
					case 3:
						return PasswordRecipientInfo.GetInstance(o, false).Version;
					case 4:
						return new DerInteger(0);    // no syntax version for OtherRecipientInfo
					default:
						throw new InvalidOperationException("unknown tag");
					}
				}

				return KeyTransRecipientInfo.GetInstance(info).Version;
			}
        }

		public bool IsTagged
		{
			get { return info is Asn1TaggedObject; }
		}

		public Asn1Encodable Info
        {
			get
			{
				if (info is Asn1TaggedObject o)
				{
					switch (o.TagNo)
					{
					case 1:
						return KeyAgreeRecipientInfo.GetInstance(o, false);
					case 2:
						return GetKekInfo(o);
					case 3:
						return PasswordRecipientInfo.GetInstance(o, false);
					case 4:
						return OtherRecipientInfo.GetInstance(o, false);
					default:
						throw new InvalidOperationException("unknown tag");
					}
				}

				return KeyTransRecipientInfo.GetInstance(info);
			}
        }

		private KekRecipientInfo GetKekInfo(
			Asn1TaggedObject o)
		{
			// For compatibility with erroneous version, we don't always pass 'false' here
			return KekRecipientInfo.GetInstance(o, o.IsExplicit());
		}

		/**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         * RecipientInfo ::= CHOICE {
         *     ktri KeyTransRecipientInfo,
         *     kari [1] KeyAgreeRecipientInfo,
         *     kekri [2] KekRecipientInfo,
         *     pwri [3] PasswordRecipientInfo,
         *     ori [4] OtherRecipientInfo }
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            return info.ToAsn1Object();
        }
    }
}