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

namespace Org.BouncyCastle.Asn1.Cmp
{
    /**
     * <pre>
     *      ErrorMsgContent ::= SEQUENCE {
     *          pKIStatusInfo          PKIStatusInfo,
     *          errorCode              INTEGER           OPTIONAL,
     *          -- implementation-specific error codes
     *          errorDetails           PKIFreeText       OPTIONAL
     *          -- implementation-specific error details
     *      }
     * </pre>
     */
    public class ErrorMsgContent
		: Asn1Encodable
	{
        public static ErrorMsgContent GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is ErrorMsgContent errorMsgContent)
                return errorMsgContent;
            return new ErrorMsgContent(Asn1Sequence.GetInstance(obj));
        }

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

        private readonly PkiStatusInfo m_pkiStatusInfo;
		private readonly DerInteger m_errorCode;
		private readonly PkiFreeText m_errorDetails;

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

			m_pkiStatusInfo = PkiStatusInfo.GetInstance(seq[pos++]);
			m_errorCode = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional);
            m_errorDetails = Asn1Utilities.ReadOptional(seq, ref pos, PkiFreeText.GetOptional);

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

		public ErrorMsgContent(PkiStatusInfo pkiStatusInfo)
			: this(pkiStatusInfo, null, null)
		{
		}

		public ErrorMsgContent(PkiStatusInfo pkiStatusInfo, DerInteger errorCode, PkiFreeText errorDetails)
		{
			m_pkiStatusInfo = pkiStatusInfo ?? throw new ArgumentNullException(nameof(pkiStatusInfo));
			m_errorCode = errorCode;
			m_errorDetails = errorDetails;
		}

		public virtual PkiStatusInfo PkiStatusInfo => m_pkiStatusInfo;

		public virtual DerInteger ErrorCode => m_errorCode;

		public virtual PkiFreeText ErrorDetails => m_errorDetails;

		/**
		 * <pre>
		 * ErrorMsgContent ::= SEQUENCE {
		 *                        pKIStatusInfo          PKIStatusInfo,
		 *                        errorCode              INTEGER           OPTIONAL,
		 *                        -- implementation-specific error codes
		 *                        errorDetails           PKIFreeText       OPTIONAL
		 *                        -- implementation-specific error details
		 * }
		 * </pre>
		 * @return a basic ASN.1 object representation.
		 */
		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(3);
			v.Add(m_pkiStatusInfo);
			v.AddOptional(m_errorCode, m_errorDetails);
			return new DerSequence(v);
		}
	}
}