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

using Org.BouncyCastle.Asn1.Crmf;

namespace Org.BouncyCastle.Asn1.Cmp
{
	public class CertifiedKeyPair
		: Asn1Encodable
	{
        public static CertifiedKeyPair GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is CertifiedKeyPair certifiedKeyPair)
                return certifiedKeyPair;
            return new CertifiedKeyPair(Asn1Sequence.GetInstance(obj));
        }

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

        private readonly CertOrEncCert m_certOrEncCert;
		private readonly EncryptedKey m_privateKey;
		private readonly PkiPublicationInfo m_publicationInfo;

        private CertifiedKeyPair(Asn1Sequence seq)
		{
			m_certOrEncCert = CertOrEncCert.GetInstance(seq[0]);

			if (seq.Count >= 2)
			{
				if (seq.Count == 2)
				{
					Asn1TaggedObject tagged = Asn1TaggedObject.GetInstance(seq[1]);
					if (tagged.TagNo == 0)
					{
						m_privateKey = EncryptedKey.GetInstance(tagged.GetObject());
					}
					else
					{
						m_publicationInfo = PkiPublicationInfo.GetInstance(tagged.GetObject());
					}
				}
				else
				{
                    m_privateKey = EncryptedKey.GetInstance(Asn1TaggedObject.GetInstance(seq[1]).GetObject());
                    m_publicationInfo = PkiPublicationInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[2]).GetObject());
				}
			}
		}

		public CertifiedKeyPair(CertOrEncCert certOrEncCert)
			: this(certOrEncCert, (EncryptedKey)null, null)
		{
		}

        public CertifiedKeyPair(CertOrEncCert certOrEncCert, EncryptedValue privateKey,
            PkiPublicationInfo publicationInfo)
            : this(certOrEncCert, privateKey == null ? null : new EncryptedKey(privateKey), publicationInfo)
        {
        }

        public CertifiedKeyPair(CertOrEncCert certOrEncCert, EncryptedKey privateKey,
			PkiPublicationInfo publicationInfo)
        {
			if (certOrEncCert == null)
				throw new ArgumentNullException(nameof(certOrEncCert));

            m_certOrEncCert = certOrEncCert;
            m_privateKey = privateKey;
            m_publicationInfo = publicationInfo;
        }

		public virtual CertOrEncCert CertOrEncCert => m_certOrEncCert;

		public virtual EncryptedKey PrivateKey => m_privateKey;

		public virtual PkiPublicationInfo PublicationInfo => m_publicationInfo;

		/**
		 * <pre>
		 * CertifiedKeyPair ::= SEQUENCE {
		 *                                  certOrEncCert       CertOrEncCert,
		 *                                  privateKey      [0] EncryptedValue      OPTIONAL,
		 *                                  -- see [CRMF] for comment on encoding
		 *                                  publicationInfo [1] PKIPublicationInfo  OPTIONAL
		 *       }
		 * </pre>
		 * @return a basic ASN.1 object representation.
		 */
		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(m_certOrEncCert);
            v.AddOptionalTagged(true, 0, m_privateKey);
            v.AddOptionalTagged(true, 1, m_publicationInfo);
			return new DerSequence(v);
		}
	}
}