KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.DERBitString;
28 import org.bouncycastle.asn1.DEREncodable;
29 import org.bouncycastle.asn1.DERObject;
30 import org.bouncycastle.asn1.DEROctetString;
31 import org.bouncycastle.asn1.DERSequence;
32 import org.bouncycastle.asn1.DERTaggedObject;
33 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
34
35 /**
36  * ASN.1 structure DER En/DeCoder.
37  *
38  * <pre>
39  * EncryptedValue ::= SEQUENCE {
40  * intendedAlg [0] AlgorithmIdentifier OPTIONAL, -- the intended algorithm for which the value will be used
41  * symmAlg [1] AlgorithmIdentifier OPTIONAL, -- the symmetric algorithm used to encrypt the value
42  * encSymmKey [2] BIT STRING OPTIONAL, -- the (encrypted) symmetric key used to encrypt the value
43  * keyAlg [3] AlgorithmIdentifier OPTIONAL, -- algorithm used to encrypt the symmetric key
44  * valueHint [4] OCTET STRING OPTIONAL, -- a brief description or identifier of the encValue content (may be meaningful only to the sending entity, and used only if EncryptedValue might be re-examined by the sending entity in the future)
45  * encValue BIT STRING } -- the encrypted value itself
46  *
47  * </pre>
48  */

49  
50 public class EncryptedValue implements DEREncodable
51 {
52     AlgorithmIdentifier intendedAlg;
53     AlgorithmIdentifier symmAlg;
54     DERBitString encSymmKey;
55     AlgorithmIdentifier keyAlg;
56     DEROctetString valueHint;
57     DERBitString encValue;
58
59     public static EncryptedValue getInstance( ASN1TaggedObject obj, boolean explicit )
60     {
61         return getInstance(ASN1Sequence.getInstance(obj, explicit));
62     }
63
64     public static EncryptedValue getInstance( Object JavaDoc obj )
65     {
66         if (obj instanceof EncryptedValue)
67         {
68             return (EncryptedValue)obj;
69         }
70         else if (obj instanceof ASN1Sequence)
71         {
72             return new EncryptedValue((ASN1Sequence)obj);
73         }
74
75         throw new IllegalArgumentException JavaDoc("unknown object in factory");
76     }
77     
78     public EncryptedValue( ASN1Sequence seq )
79     {
80       Enumeration JavaDoc e = seq.getObjects();
81       while (e.hasMoreElements())
82       {
83         Object JavaDoc obj = e.nextElement();
84         
85         if( obj instanceof DERTaggedObject )
86         {
87           DERTaggedObject tagObj = (DERTaggedObject)obj;
88           
89           switch( tagObj.getTagNo() )
90           {
91             case 0: this.intendedAlg = AlgorithmIdentifier.getInstance( tagObj.getObject() ); break;
92             case 1: this.symmAlg = AlgorithmIdentifier.getInstance( tagObj.getObject() ); break;
93             case 2: this.encSymmKey = DERBitString.getInstance( tagObj.getObject() ); break;
94             case 3: this.keyAlg = AlgorithmIdentifier.getInstance( tagObj.getObject() ); break;
95             case 4: this.valueHint = (DEROctetString)DEROctetString.getInstance( tagObj.getObject() ); break;
96           }
97         }
98         else
99         {
100           encValue = DERBitString.getInstance( obj );
101           break;
102         }
103       }
104     }
105
106     public EncryptedValue(DERBitString encValue)
107     {
108       this.encValue = encValue;
109     }
110
111     public AlgorithmIdentifier getIntendedAlg()
112     {
113       return intendedAlg;
114     }
115
116     public void setIntendedAlg(AlgorithmIdentifier intendedAlg)
117     {
118       this.intendedAlg = intendedAlg;
119     }
120
121     public AlgorithmIdentifier getSymmAlg()
122     {
123       return symmAlg;
124     }
125
126     public void setSymmAlg(AlgorithmIdentifier symmAlg)
127     {
128       this.symmAlg = symmAlg;
129     }
130
131     public DERBitString getEncSymmKey()
132     {
133       return encSymmKey;
134     }
135
136     public void setEncSymmKey(DERBitString encSymmKey)
137     {
138       this.encSymmKey = encSymmKey;
139     }
140
141     public AlgorithmIdentifier getKeyAlg()
142     {
143       return keyAlg;
144     }
145
146     public void setKeyAlg(AlgorithmIdentifier keyAlg)
147     {
148       this.keyAlg = keyAlg;
149     }
150
151     public DEROctetString getValueHint()
152     {
153       return valueHint;
154     }
155
156     public void setValueHint(DEROctetString valueHint)
157     {
158       this.valueHint = valueHint;
159     }
160
161     public DERBitString getEncValue()
162     {
163       return encValue;
164     }
165
166     public DERObject getDERObject()
167     {
168         ASN1EncodableVector v = new ASN1EncodableVector();
169
170         if( intendedAlg != null )
171           v.add( new DERTaggedObject( false, 0, intendedAlg ) );
172         if( symmAlg != null )
173           v.add( new DERTaggedObject( false, 1, symmAlg ) );
174         if( encSymmKey != null )
175           v.add( new DERTaggedObject( false, 2, encSymmKey ) );
176         if( keyAlg != null )
177           v.add( new DERTaggedObject( false, 3, keyAlg ) );
178         if( valueHint != null )
179           v.add( new DERTaggedObject( false, 4, valueHint ) );
180
181         v.add( encValue );
182
183         return new DERSequence(v);
184     }
185
186     public String JavaDoc toString()
187     {
188       String JavaDoc s = "EncryptedValue: (";
189       
190       if( this.getIntendedAlg() != null )
191         s += "intendedAlg: " + this.getIntendedAlg() + ", ";
192
193       if( this.getSymmAlg() != null )
194         s += "symmAlg: " + this.getSymmAlg() + ", ";
195
196       if( this.getEncSymmKey() != null )
197         s += "encSymmKey: " + this.getEncSymmKey() + ", ";
198
199       if( this.getKeyAlg() != null )
200         s += "keyAlg: " + this.getKeyAlg() + ", ";
201
202       if( this.getValueHint() != null )
203         s += "valueHint: " + this.getValueHint() + ", ";
204
205       s += "encValue: " + this.getEncValue() + ")";
206       
207       return s;
208     }
209 }
210
Popular Tags