KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > AlgorithmIdentifier


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22 import com.maverick.crypto.asn1.ASN1Encodable;
23 import com.maverick.crypto.asn1.ASN1EncodableVector;
24 import com.maverick.crypto.asn1.ASN1Sequence;
25 import com.maverick.crypto.asn1.ASN1TaggedObject;
26 import com.maverick.crypto.asn1.DEREncodable;
27 import com.maverick.crypto.asn1.DERObject;
28 import com.maverick.crypto.asn1.DERObjectIdentifier;
29 import com.maverick.crypto.asn1.DERSequence;
30
31 public class AlgorithmIdentifier
32     extends ASN1Encodable
33 {
34     private DERObjectIdentifier objectId;
35     private DEREncodable parameters;
36     private boolean parametersDefined = false;
37
38     public static AlgorithmIdentifier getInstance(
39         ASN1TaggedObject obj,
40         boolean explicit)
41     {
42         return getInstance(ASN1Sequence.getInstance(obj, explicit));
43     }
44
45     public static AlgorithmIdentifier getInstance(
46         Object JavaDoc obj)
47     {
48         if (obj instanceof AlgorithmIdentifier)
49         {
50             return (AlgorithmIdentifier)obj;
51         }
52
53         if (obj instanceof DERObjectIdentifier)
54         {
55             return new AlgorithmIdentifier((DERObjectIdentifier)obj);
56         }
57
58         if (obj instanceof String JavaDoc)
59         {
60             return new AlgorithmIdentifier((String JavaDoc)obj);
61         }
62
63         if (obj instanceof ASN1Sequence)
64         {
65             return new AlgorithmIdentifier((ASN1Sequence)obj);
66         }
67
68         throw new IllegalArgumentException JavaDoc("unknown object in factory");
69     }
70
71     public AlgorithmIdentifier(
72         DERObjectIdentifier objectId)
73     {
74         this.objectId = objectId;
75     }
76
77     public AlgorithmIdentifier(
78         String JavaDoc objectId)
79     {
80         this.objectId = new DERObjectIdentifier(objectId);
81     }
82
83     public AlgorithmIdentifier(
84         DERObjectIdentifier objectId,
85         DEREncodable parameters)
86     {
87         parametersDefined = true;
88         this.objectId = objectId;
89         this.parameters = parameters;
90     }
91
92     public AlgorithmIdentifier(
93         ASN1Sequence seq)
94     {
95         objectId = (DERObjectIdentifier)seq.getObjectAt(0);
96
97         if (seq.size() == 2)
98         {
99             parametersDefined = true;
100             parameters = seq.getObjectAt(1);
101         }
102         else
103         {
104             parameters = null;
105         }
106     }
107
108     public DERObjectIdentifier getObjectId()
109     {
110         return objectId;
111     }
112
113     public DEREncodable getParameters()
114     {
115         return parameters;
116     }
117
118     /**
119      * Produce an object suitable for an ASN1OutputStream.
120      * <pre>
121      * AlgorithmIdentifier ::= SEQUENCE {
122      * algorithm OBJECT IDENTIFIER,
123      * parameters ANY DEFINED BY algorithm OPTIONAL }
124      * </pre>
125      */

126     public DERObject toASN1Object()
127     {
128         ASN1EncodableVector v = new ASN1EncodableVector();
129
130         v.add(objectId);
131
132         if (parametersDefined)
133         {
134             v.add(parameters);
135         }
136
137         return new DERSequence(v);
138     }
139 }
140
Popular Tags