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

using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Cmp
{
	public class CertifiedKeyPair
		: Asn1Encodable
	{
		private readonly CertOrEncCert certOrEncCert;
		private readonly EncryptedValue privateKey;
		private readonly PkiPublicationInfo publicationInfo;

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

			if (seq.Count >= 2)
			{
				if (seq.Count == 2)
				{
					Asn1TaggedObject tagged = Asn1TaggedObject.GetInstance(seq[1]);
					if (tagged.TagNo == 0)
					{
						privateKey = EncryptedValue.GetInstance(tagged.GetObject());
					}
					else
					{
						publicationInfo = PkiPublicationInfo.GetInstance(tagged.GetObject());
					}
				}
				else
				{
					privateKey = EncryptedValue.GetInstance(Asn1TaggedObject.GetInstance(seq[1]));
					publicationInfo = PkiPublicationInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[2]));
				}
			}
		}

		public static CertifiedKeyPair GetInstance(object obj)
		{
			if (obj is CertifiedKeyPair)
				return (CertifiedKeyPair)obj;

			if (obj is Asn1Sequence)
				return new CertifiedKeyPair((Asn1Sequence)obj);

            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
		}

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

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

			this.certOrEncCert = certOrEncCert;
			this.privateKey = privateKey;
			this.publicationInfo = publicationInfo;
		}

		public virtual CertOrEncCert CertOrEncCert
		{
			get { return certOrEncCert; }
		}

		public virtual EncryptedValue PrivateKey
		{
			get { return privateKey; }
		}

		public virtual PkiPublicationInfo PublicationInfo
		{
			get { return 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(certOrEncCert);
            v.AddOptionalTagged(true, 0, privateKey);
            v.AddOptionalTagged(true, 1, publicationInfo);
			return new DerSequence(v);
		}
	}
}