2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.x509;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.InputStream;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.util.Date;
|
|
32 |
import java.util.Enumeration;
|
|
33 |
|
|
34 |
import sun.security.util.*;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* This class defines the version of the X509 Certificate.
|
|
38 |
*
|
|
39 |
* @author Amit Kapoor
|
|
40 |
* @author Hemma Prafullchandra
|
|
41 |
* @see CertAttrSet
|
|
42 |
*/
|
|
43 |
public class CertificateVersion implements CertAttrSet<String> {
|
|
44 |
/**
|
|
45 |
* X509Certificate Version 1
|
|
46 |
*/
|
|
47 |
public static final int V1 = 0;
|
|
48 |
/**
|
|
49 |
* X509Certificate Version 2
|
|
50 |
*/
|
|
51 |
public static final int V2 = 1;
|
|
52 |
/**
|
|
53 |
* X509Certificate Version 3
|
|
54 |
*/
|
|
55 |
public static final int V3 = 2;
|
|
56 |
/**
|
|
57 |
* Identifier for this attribute, to be used with the
|
|
58 |
* get, set, delete methods of Certificate, x509 type.
|
|
59 |
*/
|
|
60 |
public static final String IDENT = "x509.info.version";
|
|
61 |
/**
|
|
62 |
* Sub attributes name for this CertAttrSet.
|
|
63 |
*/
|
|
64 |
public static final String NAME = "version";
|
|
65 |
public static final String VERSION = "number";
|
|
66 |
|
|
67 |
// Private data members
|
|
68 |
int version = V1;
|
|
69 |
|
|
70 |
// Returns the version number.
|
|
71 |
private int getVersion() {
|
|
72 |
return(version);
|
|
73 |
}
|
|
74 |
|
|
75 |
// Construct the class from the passed DerValue
|
|
76 |
private void construct(DerValue derVal) throws IOException {
|
|
77 |
if (derVal.isConstructed() && derVal.isContextSpecific()) {
|
|
78 |
derVal = derVal.data.getDerValue();
|
|
79 |
version = derVal.getInteger();
|
|
80 |
if (derVal.data.available() != 0) {
|
|
81 |
throw new IOException("X.509 version, bad format");
|
|
82 |
}
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* The default constructor for this class,
|
|
88 |
* sets the version to 0 (i.e. X.509 version 1).
|
|
89 |
*/
|
|
90 |
public CertificateVersion() {
|
|
91 |
version = V1;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* The constructor for this class for the required version.
|
|
96 |
*
|
|
97 |
* @param version the version for the certificate.
|
|
98 |
* @exception IOException if the version is not valid.
|
|
99 |
*/
|
|
100 |
public CertificateVersion(int version) throws IOException {
|
|
101 |
|
|
102 |
// check that it is a valid version
|
|
103 |
if (version == V1 || version == V2 || version == V3)
|
|
104 |
this.version = version;
|
|
105 |
else {
|
|
106 |
throw new IOException("X.509 Certificate version " +
|
|
107 |
version + " not supported.\n");
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Create the object, decoding the values from the passed DER stream.
|
|
113 |
*
|
|
114 |
* @param in the DerInputStream to read the CertificateVersion from.
|
|
115 |
* @exception IOException on decoding errors.
|
|
116 |
*/
|
|
117 |
public CertificateVersion(DerInputStream in) throws IOException {
|
|
118 |
version = V1;
|
|
119 |
DerValue derVal = in.getDerValue();
|
|
120 |
|
|
121 |
construct(derVal);
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Create the object, decoding the values from the passed stream.
|
|
126 |
*
|
|
127 |
* @param in the InputStream to read the CertificateVersion from.
|
|
128 |
* @exception IOException on decoding errors.
|
|
129 |
*/
|
|
130 |
public CertificateVersion(InputStream in) throws IOException {
|
|
131 |
version = V1;
|
|
132 |
DerValue derVal = new DerValue(in);
|
|
133 |
|
|
134 |
construct(derVal);
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Create the object, decoding the values from the passed DerValue.
|
|
139 |
*
|
|
140 |
* @param val the Der encoded value.
|
|
141 |
* @exception IOException on decoding errors.
|
|
142 |
*/
|
|
143 |
public CertificateVersion(DerValue val) throws IOException {
|
|
144 |
version = V1;
|
|
145 |
|
|
146 |
construct(val);
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Return the version number of the certificate.
|
|
151 |
*/
|
|
152 |
public String toString() {
|
|
153 |
return("Version: V" + (version+1));
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Encode the CertificateVersion period in DER form to the stream.
|
|
158 |
*
|
|
159 |
* @param out the OutputStream to marshal the contents to.
|
|
160 |
* @exception IOException on errors.
|
|
161 |
*/
|
|
162 |
public void encode(OutputStream out) throws IOException {
|
|
163 |
// Nothing for default
|
|
164 |
if (version == V1) {
|
|
165 |
return;
|
|
166 |
}
|
|
167 |
DerOutputStream tmp = new DerOutputStream();
|
|
168 |
tmp.putInteger(version);
|
|
169 |
|
|
170 |
DerOutputStream seq = new DerOutputStream();
|
|
171 |
seq.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),
|
|
172 |
tmp);
|
|
173 |
|
|
174 |
out.write(seq.toByteArray());
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Set the attribute value.
|
|
179 |
*/
|
|
180 |
public void set(String name, Object obj) throws IOException {
|
|
181 |
if (!(obj instanceof Integer)) {
|
|
182 |
throw new IOException("Attribute must be of type Integer.");
|
|
183 |
}
|
|
184 |
if (name.equalsIgnoreCase(VERSION)) {
|
|
185 |
version = ((Integer)obj).intValue();
|
|
186 |
} else {
|
|
187 |
throw new IOException("Attribute name not recognized by " +
|
|
188 |
"CertAttrSet: CertificateVersion.");
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Get the attribute value.
|
|
194 |
*/
|
|
195 |
public Object get(String name) throws IOException {
|
|
196 |
if (name.equalsIgnoreCase(VERSION)) {
|
|
197 |
return(new Integer(getVersion()));
|
|
198 |
} else {
|
|
199 |
throw new IOException("Attribute name not recognized by " +
|
|
200 |
"CertAttrSet: CertificateVersion.");
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Delete the attribute value.
|
|
206 |
*/
|
|
207 |
public void delete(String name) throws IOException {
|
|
208 |
if (name.equalsIgnoreCase(VERSION)) {
|
|
209 |
version = V1;
|
|
210 |
} else {
|
|
211 |
throw new IOException("Attribute name not recognized by " +
|
|
212 |
"CertAttrSet: CertificateVersion.");
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Return an enumeration of names of attributes existing within this
|
|
218 |
* attribute.
|
|
219 |
*/
|
|
220 |
public Enumeration<String> getElements() {
|
|
221 |
AttributeNameEnumeration elements = new AttributeNameEnumeration();
|
|
222 |
elements.addElement(VERSION);
|
|
223 |
|
|
224 |
return (elements.elements());
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Return the name of this attribute.
|
|
229 |
*/
|
|
230 |
public String getName() {
|
|
231 |
return(NAME);
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Compare versions.
|
|
236 |
*/
|
|
237 |
public int compare(int vers) {
|
|
238 |
return(version - vers);
|
|
239 |
}
|
|
240 |
}
|