summary refs log tree commit diff
path: root/crypto/src/asn1/esf/RevocationValues.cs
blob: f3b36f1d1f651e40509c9f2fe6839dd7e3f394bc (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
using System;
using System.Collections.Generic;

using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Esf
{
	/// <remarks>
	/// RFC 5126: 6.3.4.  revocation-values Attribute Definition
	/// <code>
	/// RevocationValues ::=  SEQUENCE {
	///		crlVals			[0] SEQUENCE OF CertificateList     OPTIONAL,
	///		ocspVals		[1] SEQUENCE OF BasicOCSPResponse   OPTIONAL,
	///		otherRevVals	[2] OtherRevVals OPTIONAL
	/// }
	/// </code>
	/// </remarks>
	public class RevocationValues
		: Asn1Encodable
	{
		private readonly Asn1Sequence m_crlVals;
		private readonly Asn1Sequence m_ocspVals;
		private readonly OtherRevVals m_otherRevVals;

		public static RevocationValues GetInstance(object obj)
		{
            if (obj == null)
                return null;

            if (obj is RevocationValues revocationValues)
				return revocationValues;

			return new RevocationValues(Asn1Sequence.GetInstance(obj));
		}

		private RevocationValues(Asn1Sequence seq)
		{
			if (seq == null)
				throw new ArgumentNullException(nameof(seq));
			if (seq.Count > 3)
				throw new ArgumentException("Bad sequence size: " + seq.Count, nameof(seq));

			foreach (Asn1TaggedObject taggedObj in seq)
			{
				Asn1Object asn1Obj = taggedObj.GetObject();
				switch (taggedObj.TagNo)
				{
				case 0:
					Asn1Sequence crlValsSeq = (Asn1Sequence)asn1Obj;

					// Validate
					crlValsSeq.MapElements(element => CertificateList.GetInstance(element.ToAsn1Object()));

					m_crlVals = crlValsSeq;
					break;
				case 1:
					Asn1Sequence ocspValsSeq = (Asn1Sequence)asn1Obj;

					// Validate
					ocspValsSeq.MapElements(element => BasicOcspResponse.GetInstance(element.ToAsn1Object()));

					m_ocspVals = ocspValsSeq;
					break;
				case 2:
					m_otherRevVals = OtherRevVals.GetInstance(asn1Obj);
					break;
				default:
					throw new ArgumentException("Illegal tag in RevocationValues", nameof(seq));
				}
			}
		}

		public RevocationValues(CertificateList[] crlVals, BasicOcspResponse[] ocspVals, OtherRevVals otherRevVals)
		{
			if (crlVals != null)
			{
				m_crlVals = new DerSequence(crlVals);
			}

			if (ocspVals != null)
			{
				m_ocspVals = new DerSequence(ocspVals);
			}

			m_otherRevVals = otherRevVals;
		}

		public RevocationValues(IEnumerable<CertificateList> crlVals, IEnumerable<BasicOcspResponse> ocspVals,
			OtherRevVals otherRevVals)
		{
			if (crlVals != null)
			{
				m_crlVals = new DerSequence(Asn1EncodableVector.FromEnumerable(crlVals));
			}

			if (ocspVals != null)
			{
				m_ocspVals = new DerSequence(Asn1EncodableVector.FromEnumerable(ocspVals));
			}

			m_otherRevVals = otherRevVals;
		}

		public CertificateList[] GetCrlVals()
		{
			return m_crlVals.MapElements(element => CertificateList.GetInstance(element.ToAsn1Object()));
		}

		public BasicOcspResponse[] GetOcspVals()
		{
            return m_ocspVals.MapElements(element => BasicOcspResponse.GetInstance(element.ToAsn1Object()));
		}

		public OtherRevVals OtherRevVals
		{
			get { return m_otherRevVals; }
		}

		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(3);
            v.AddOptionalTagged(true, 0, m_crlVals);
            v.AddOptionalTagged(true, 1, m_ocspVals);

            if (m_otherRevVals != null)
			{
				v.Add(new DerTaggedObject(true, 2, m_otherRevVals.ToAsn1Object()));
			}

            return new DerSequence(v);
		}
	}
}