KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > cmp > CertifiedKeyPair


1 // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
2
//
3
// Author: Maik Stohn
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
// software and associated documentation files (the "Software"), to deal in the Software
7
// without restriction, including without limitation the rights to use, copy, modify, merge,
8
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
// to whom the Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in all copies or
12
// substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19

20 package com.novosec.pkix.asn1.cmp;
21
22 import java.util.Enumeration JavaDoc;
23
24 import org.bouncycastle.asn1.ASN1EncodableVector;
25 import org.bouncycastle.asn1.ASN1Sequence;
26 import org.bouncycastle.asn1.ASN1TaggedObject;
27 import org.bouncycastle.asn1.DEREncodable;
28 import org.bouncycastle.asn1.DERObject;
29 import org.bouncycastle.asn1.DERSequence;
30 import org.bouncycastle.asn1.DERTaggedObject;
31
32 import com.novosec.pkix.asn1.crmf.EncryptedValue;
33 import com.novosec.pkix.asn1.crmf.PKIPublicationInfo;
34
35 /**
36  * ASN.1 structure DER En/DeCoder.
37  *
38  * <pre>
39  * CertifiedKeyPair ::= SEQUENCE {
40  * certOrEncCert CertOrEncCert,
41  * privateKey [0] EncryptedValue OPTIONAL,
42  * publicationInfo [1] PKIPublicationInfo OPTIONAL
43  * }
44  *
45  * </pre>
46  */

47 public class CertifiedKeyPair implements DEREncodable
48 {
49     CertOrEncCert certOrEncCert;
50     EncryptedValue privateKey;
51     PKIPublicationInfo publicationInfo;
52
53     public static CertifiedKeyPair getInstance( ASN1TaggedObject obj, boolean explicit )
54     {
55         return getInstance(ASN1Sequence.getInstance(obj, explicit));
56     }
57
58     public static CertifiedKeyPair getInstance( Object JavaDoc obj )
59     {
60         if (obj instanceof CertifiedKeyPair)
61         {
62             return (CertifiedKeyPair)obj;
63         }
64         else if (obj instanceof ASN1Sequence)
65         {
66             return new CertifiedKeyPair((ASN1Sequence)obj);
67         }
68
69         throw new IllegalArgumentException JavaDoc("unknown object in factory");
70     }
71     
72     public CertifiedKeyPair( ASN1Sequence seq )
73     {
74       Enumeration JavaDoc e = seq.getObjects();
75
76       certOrEncCert = CertOrEncCert.getInstance( (DERObject)e.nextElement() );
77       
78       while (e.hasMoreElements())
79       {
80         ASN1TaggedObject tagObj = (ASN1TaggedObject)e.nextElement();
81
82         switch (tagObj.getTagNo())
83         {
84           case 0: privateKey = EncryptedValue.getInstance( tagObj.getObject() ); break;
85           case 1: publicationInfo = PKIPublicationInfo.getInstance( tagObj.getObject() ); break;
86         }
87       }
88     }
89
90     public CertifiedKeyPair( CertOrEncCert certOrEncCert )
91     {
92       this.certOrEncCert = certOrEncCert;
93     }
94
95     public CertOrEncCert getCertOrEncCert()
96     {
97         return certOrEncCert;
98     }
99     
100     public void setPrivateKey( EncryptedValue privateKey )
101     {
102       this.privateKey = privateKey;
103     }
104
105     public EncryptedValue getPrivateKey()
106     {
107       return privateKey;
108     }
109
110     public void setPublicationInfo( PKIPublicationInfo publicationInfo )
111     {
112       this.publicationInfo = publicationInfo;
113     }
114
115     public PKIPublicationInfo getPublicationInfo()
116     {
117       return publicationInfo;
118     }
119
120     public DERObject getDERObject()
121     {
122       ASN1EncodableVector v = new ASN1EncodableVector();
123
124       v.add( certOrEncCert );
125       
126       if( privateKey != null )
127         v.add( new DERTaggedObject( true, 0, privateKey ) );
128
129       if( publicationInfo != null )
130         v.add( new DERTaggedObject( true, 1, publicationInfo ) );
131       
132       return new DERSequence(v);
133     }
134
135     public String JavaDoc toString()
136     {
137       String JavaDoc s = "CertifiedKeyPair: ( certOrEncCert: " + this.getCertOrEncCert() + ", ";
138
139       if( this.getPrivateKey() != null )
140         s += "privateKey: "+ this.getPrivateKey() + ", ";
141
142       if( this.getPublicationInfo() != null )
143         s += "publicationInfo: "+ this.getPublicationInfo() + ", ";
144
145       s += ")";
146       
147       return s;
148     }
149 }
150
Popular Tags