KickJava   Java API By Example, From Geeks To Geeks.

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


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.DERBitString;
28 import org.bouncycastle.asn1.DEREncodable;
29 import org.bouncycastle.asn1.DERObject;
30 import org.bouncycastle.asn1.DERSequence;
31 import org.bouncycastle.asn1.DERTaggedObject;
32 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
33 import com.novosec.pkix.asn1.crmf.CertId;
34
35 /**
36  * ASN.1 structure DER En/DeCoder.
37  *
38  * <pre>
39  * OOBCertHash ::= SEQUENCE {
40  * hashAlg [0] AlgorithmIdentifier OPTIONAL,
41  * certId [1] CertId OPTIONAL,
42  * hashVal BIT STRING -- hashVal is calculated over DER encoding of the subjectPublicKey field of the corresponding cert.
43  * }
44  *
45  * </pre>
46  */

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