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