01: package org.bouncycastle.asn1.cmp;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Sequence;
06: import org.bouncycastle.asn1.ASN1TaggedObject;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DERSequence;
09: import org.bouncycastle.asn1.DERTaggedObject;
10: import org.bouncycastle.asn1.crmf.EncryptedValue;
11: import org.bouncycastle.asn1.crmf.PKIPublicationInfo;
12:
13: public class CertifiedKeyPair extends ASN1Encodable {
14: private CertOrEncCert certOrEncCert;
15: private EncryptedValue privateKey;
16: private PKIPublicationInfo publicationInfo;
17:
18: private CertifiedKeyPair(ASN1Sequence seq) {
19: certOrEncCert = CertOrEncCert.getInstance(seq.getObjectAt(0));
20:
21: if (seq.size() >= 2) {
22: if (seq.size() == 2) {
23: ASN1TaggedObject tagged = ASN1TaggedObject
24: .getInstance(seq.getObjectAt(1));
25: if (tagged.getTagNo() == 0) {
26: privateKey = EncryptedValue.getInstance(tagged
27: .getObject());
28: } else {
29: publicationInfo = PKIPublicationInfo
30: .getInstance(tagged.getObject());
31: }
32: } else {
33: privateKey = EncryptedValue
34: .getInstance(ASN1TaggedObject.getInstance(seq
35: .getObjectAt(1)));
36: publicationInfo = PKIPublicationInfo
37: .getInstance(ASN1TaggedObject.getInstance(seq
38: .getObjectAt(2)));
39: }
40: }
41: }
42:
43: public static CertifiedKeyPair getInstance(Object o) {
44: if (o instanceof CertifiedKeyPair) {
45: return (CertifiedKeyPair) o;
46: }
47:
48: if (o instanceof ASN1Sequence) {
49: return new CertifiedKeyPair((ASN1Sequence) o);
50: }
51:
52: throw new IllegalArgumentException("Invalid object: "
53: + o.getClass().getName());
54: }
55:
56: public CertOrEncCert getCertOrEncCert() {
57: return certOrEncCert;
58: }
59:
60: public EncryptedValue getPrivateKey() {
61: return privateKey;
62: }
63:
64: public PKIPublicationInfo getPublicationInfo() {
65: return publicationInfo;
66: }
67:
68: /**
69: * <pre>
70: * CertifiedKeyPair ::= SEQUENCE {
71: * certOrEncCert CertOrEncCert,
72: * privateKey [0] EncryptedValue OPTIONAL,
73: * -- see [CRMF] for comment on encoding
74: * publicationInfo [1] PKIPublicationInfo OPTIONAL
75: * }
76: * </pre>
77: * @return a basic ASN.1 object representation.
78: */
79: public DERObject toASN1Object() {
80: ASN1EncodableVector v = new ASN1EncodableVector();
81:
82: v.add(certOrEncCert);
83:
84: if (privateKey != null) {
85: v.add(new DERTaggedObject(true, 0, privateKey));
86: }
87:
88: if (publicationInfo != null) {
89: v.add(new DERTaggedObject(true, 1, publicationInfo));
90: }
91:
92: return new DERSequence(v);
93: }
94: }
|