CertifiedKeyPair.cs
2.74 KB
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
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);
if (privateKey != null)
{
v.Add(new DerTaggedObject(true, 0, privateKey));
}
if (publicationInfo != null)
{
v.Add(new DerTaggedObject(true, 1, publicationInfo));
}
return new DerSequence(v);
}
}
}