KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > crmf > EncryptedKey


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.crmf;
21
22 import org.bouncycastle.asn1.ASN1TaggedObject;
23 import org.bouncycastle.asn1.DEREncodable;
24 import org.bouncycastle.asn1.DERObject;
25 import org.bouncycastle.asn1.DERTaggedObject;
26 import org.bouncycastle.asn1.cms.EnvelopedData;
27
28 /**
29  * ASN.1 structure DER En/DeCoder.
30  *
31  * <pre>
32  *
33  * EncryptedKey ::= CHOICE {
34  * encryptedValue EncryptedValue,
35  * envelopedData [0] EnvelopedData } -- The encrypted private key MUST be placed in the envelopedData encryptedContentInfo encryptedContent OCTET STRING.
36  *
37  * </pre>
38  */

39 public class EncryptedKey implements DEREncodable
40 {
41     public static final int TAGNO_ENV_DATA = 0;
42     public static final int TAGNO_ENC_VALUE = 1;
43
44     private int tagNo = -1;
45     private DEREncodable obj = null;
46     private EncryptedValue encryptedValue = null;
47
48     public static EncryptedKey getInstance( DEREncodable derObj )
49     {
50         if(derObj instanceof EnvelopedData)
51             return new EncryptedKey( (EnvelopedData)derObj );
52         else if(derObj instanceof EncryptedValue)
53             return new EncryptedKey( (EncryptedValue)derObj );
54         else if(derObj instanceof ASN1TaggedObject)
55             return getInstance( (ASN1TaggedObject)derObj, false );
56         else
57             return new EncryptedKey( EncryptedValue.getInstance(derObj) ); // last try ;-)
58
}
59
60     public static EncryptedKey getInstance( ASN1TaggedObject tagObj, boolean explicit )
61     {
62         int tag = (tagObj == null ? -1 : tagObj.getTagNo());
63         switch (tag)
64         {
65           case TAGNO_ENV_DATA: return new EncryptedKey(EnvelopedData.getInstance(tagObj, explicit));
66           default: return new EncryptedKey(EncryptedValue.getInstance(tagObj, explicit));
67         }
68     }
69
70     public EncryptedKey( DEREncodable derObj, int tag )
71     {
72         this.tagNo = tag;
73
74         if(derObj instanceof EnvelopedData)
75             this.obj = (EnvelopedData)derObj;
76         else if(derObj instanceof EncryptedValue)
77             this.encryptedValue = (EncryptedValue)derObj;
78         else {
79             switch( this.tagNo ) {
80                 case TAGNO_ENV_DATA: this.obj = EnvelopedData.getInstance(derObj); break;
81                 default: this.encryptedValue = EncryptedValue.getInstance(derObj); break;
82             }
83         }
84     }
85
86     public EncryptedKey( EnvelopedData envelopedData )
87     {
88         this( envelopedData, TAGNO_ENV_DATA );
89     }
90
91     public EncryptedKey( EncryptedValue encryptedValue )
92     {
93         this( encryptedValue, TAGNO_ENC_VALUE );
94     }
95
96     public void setEncryptedValue(EncryptedValue value) {
97         encryptedValue = value;
98     }
99
100     public EncryptedValue getEncryptedValue()
101     {
102       return encryptedValue;
103     }
104
105     public void setTagNo(int tn) {
106         this.tagNo = tn;
107     }
108
109     public int getTagNo() {
110         return this.tagNo;
111     }
112
113     public EnvelopedData getEnvelopedData() {
114         return EnvelopedData.getInstance(obj);
115     }
116
117     public DERObject getDERObject()
118     {
119         if( this.encryptedValue != null )
120             return encryptedValue.getDERObject();
121         else if( this.obj != null )
122             return new DERTaggedObject(true, this.tagNo, this.obj); // choice is allways explictly tagged
123
else
124             return null;
125     }
126
127     public String JavaDoc toString() {
128         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.getClass().getName());
129         sb.append(" (");
130
131         sb.append("tagNo: " + this.tagNo + ", ");
132
133         if( this.encryptedValue != null )
134             sb.append("encryptedValue: " + this.encryptedValue + ", ");
135
136         if( this.obj != null ) {
137             sb.append("envelopedData: " + this.obj + ", ");
138         }
139
140         sb.append("hashCode: " + Integer.toHexString(this.hashCode()) + ")");
141         return sb.toString();
142     }
143 }
144
Popular Tags