001: /*
002: * @(#)CertificateVersion.java 1.19 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Date;
034: import java.util.Enumeration;
035:
036: import sun.security.util.*;
037:
038: /**
039: * This class defines the version of the X509 Certificate.
040: *
041: * @author Amit Kapoor
042: * @author Hemma Prafullchandra
043: * @version 1.12
044: * @see CertAttrSet
045: */
046: public class CertificateVersion implements CertAttrSet {
047: /**
048: * X509Certificate Version 1
049: */
050: public static final int V1 = 0;
051: /**
052: * X509Certificate Version 2
053: */
054: public static final int V2 = 1;
055: /**
056: * X509Certificate Version 3
057: */
058: public static final int V3 = 2;
059: /**
060: * Identifier for this attribute, to be used with the
061: * get, set, delete methods of Certificate, x509 type.
062: */
063: public static final String IDENT = "x509.info.version";
064: /**
065: * Sub attributes name for this CertAttrSet.
066: */
067: public static final String NAME = "version";
068: public static final String VERSION = "number";
069:
070: // Private data members
071: int version = V1;
072:
073: // Returns the version number.
074: private int getVersion() {
075: return (version);
076: }
077:
078: // Construct the class from the passed DerValue
079: private void construct(DerValue derVal) throws IOException {
080: if (derVal.isConstructed() && derVal.isContextSpecific()) {
081: derVal = derVal.data.getDerValue();
082: version = derVal.getInteger();
083: if (derVal.data.available() != 0) {
084: throw new IOException("X.509 version, bad format");
085: }
086: }
087: }
088:
089: /**
090: * The default constructor for this class,
091: * sets the version to 0 (i.e. X.509 version 1).
092: */
093: public CertificateVersion() {
094: version = V1;
095: }
096:
097: /**
098: * The constructor for this class for the required version.
099: *
100: * @param version the version for the certificate.
101: * @exception IOException if the version is not valid.
102: */
103: public CertificateVersion(int version) throws IOException {
104:
105: // check that it is a valid version
106: if (version == V1 || version == V2 || version == V3)
107: this .version = version;
108: else {
109: throw new IOException("X.509 Certificate version "
110: + version + " not supported.\n");
111: }
112: }
113:
114: /**
115: * Create the object, decoding the values from the passed DER stream.
116: *
117: * @param in the DerInputStream to read the CertificateVersion from.
118: * @exception IOException on decoding errors.
119: */
120: public CertificateVersion(DerInputStream in) throws IOException {
121: version = V1;
122: DerValue derVal = in.getDerValue();
123:
124: construct(derVal);
125: }
126:
127: /**
128: * Create the object, decoding the values from the passed stream.
129: *
130: * @param in the InputStream to read the CertificateVersion from.
131: * @exception IOException on decoding errors.
132: */
133: public CertificateVersion(InputStream in) throws IOException {
134: version = V1;
135: DerValue derVal = new DerValue(in);
136:
137: construct(derVal);
138: }
139:
140: /**
141: * Create the object, decoding the values from the passed DerValue.
142: *
143: * @param val the Der encoded value.
144: * @exception IOException on decoding errors.
145: */
146: public CertificateVersion(DerValue val) throws IOException {
147: version = V1;
148:
149: construct(val);
150: }
151:
152: /**
153: * Return the version number of the certificate.
154: */
155: public String toString() {
156: return ("Version: V" + (version + 1));
157: }
158:
159: /**
160: * Encode the CertificateVersion period in DER form to the stream.
161: *
162: * @param out the OutputStream to marshal the contents to.
163: * @exception IOException on errors.
164: */
165: public void encode(OutputStream out) throws IOException {
166: // Nothing for default
167: if (version == V1) {
168: return;
169: }
170: DerOutputStream tmp = new DerOutputStream();
171: tmp.putInteger(version);
172:
173: DerOutputStream seq = new DerOutputStream();
174: seq.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
175: (byte) 0), tmp);
176:
177: out.write(seq.toByteArray());
178: }
179:
180: /**
181: * Decode the CertificateVersion period in DER form from the stream.
182: *
183: * @param in the InputStream to unmarshal the contents from.
184: * @exception IOException on errors.
185: */
186: public void decode(InputStream in) throws IOException {
187: DerValue derVal = new DerValue(in);
188: construct(derVal);
189: }
190:
191: /**
192: * Set the attribute value.
193: */
194: public void set(String name, Object obj) throws IOException {
195: if (!(obj instanceof Integer)) {
196: throw new IOException("Attribute must be of type Integer.");
197: }
198: if (name.equalsIgnoreCase(VERSION)) {
199: version = ((Integer) obj).intValue();
200: } else {
201: throw new IOException("Attribute name not recognized by "
202: + "CertAttrSet: CertificateVersion.");
203: }
204: }
205:
206: /**
207: * Get the attribute value.
208: */
209: public Object get(String name) throws IOException {
210: if (name.equalsIgnoreCase(VERSION)) {
211: return (new Integer(getVersion()));
212: } else {
213: throw new IOException("Attribute name not recognized by "
214: + "CertAttrSet: CertificateVersion.");
215: }
216: }
217:
218: /**
219: * Delete the attribute value.
220: */
221: public void delete(String name) throws IOException {
222: if (name.equalsIgnoreCase(VERSION)) {
223: version = V1;
224: } else {
225: throw new IOException("Attribute name not recognized by "
226: + "CertAttrSet: CertificateVersion.");
227: }
228: }
229:
230: /**
231: * Return an enumeration of names of attributes existing within this
232: * attribute.
233: */
234: public Enumeration getElements() {
235: AttributeNameEnumeration elements = new AttributeNameEnumeration();
236: elements.addElement(VERSION);
237:
238: return (elements.elements());
239: }
240:
241: /**
242: * Return the name of this attribute.
243: */
244: public String getName() {
245: return (NAME);
246: }
247:
248: /**
249: * Compare versions.
250: */
251: public int compare(int vers) {
252: return (version - vers);
253: }
254: }
|