Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateVersion.java
41159 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* This class defines the version of the X509 Certificate.36*37* @author Amit Kapoor38* @author Hemma Prafullchandra39* @see CertAttrSet40*/41public class CertificateVersion implements CertAttrSet<String> {42/**43* X509Certificate Version 144*/45public static final int V1 = 0;46/**47* X509Certificate Version 248*/49public static final int V2 = 1;50/**51* X509Certificate Version 352*/53public static final int V3 = 2;54/**55* Identifier for this attribute, to be used with the56* get, set, delete methods of Certificate, x509 type.57*/58public static final String IDENT = "x509.info.version";59/**60* Sub attributes name for this CertAttrSet.61*/62public static final String NAME = "version";63public static final String VERSION = "number";6465// Private data members66int version = V1;6768// Returns the version number.69private int getVersion() {70return(version);71}7273// Construct the class from the passed DerValue74private void construct(DerValue derVal) throws IOException {75if (derVal.isConstructed() && derVal.isContextSpecific()) {76derVal = derVal.data.getDerValue();77version = derVal.getInteger();78if (derVal.data.available() != 0) {79throw new IOException("X.509 version, bad format");80}81}82}8384/**85* The default constructor for this class,86* sets the version to 0 (i.e. X.509 version 1).87*/88public CertificateVersion() {89version = V1;90}9192/**93* The constructor for this class for the required version.94*95* @param version the version for the certificate.96* @exception IOException if the version is not valid.97*/98public CertificateVersion(int version) throws IOException {99100// check that it is a valid version101if (version == V1 || version == V2 || version == V3)102this.version = version;103else {104throw new IOException("X.509 Certificate version " +105version + " not supported.\n");106}107}108109/**110* Create the object, decoding the values from the passed DER stream.111*112* @param in the DerInputStream to read the CertificateVersion from.113* @exception IOException on decoding errors.114*/115public CertificateVersion(DerInputStream in) throws IOException {116version = V1;117DerValue derVal = in.getDerValue();118119construct(derVal);120}121122/**123* Create the object, decoding the values from the passed stream.124*125* @param in the InputStream to read the CertificateVersion from.126* @exception IOException on decoding errors.127*/128public CertificateVersion(InputStream in) throws IOException {129version = V1;130DerValue derVal = new DerValue(in);131132construct(derVal);133}134135/**136* Create the object, decoding the values from the passed DerValue.137*138* @param val the Der encoded value.139* @exception IOException on decoding errors.140*/141public CertificateVersion(DerValue val) throws IOException {142version = V1;143144construct(val);145}146147/**148* Return the version number of the certificate.149*/150public String toString() {151return("Version: V" + (version+1));152}153154/**155* Encode the CertificateVersion period in DER form to the stream.156*157* @param out the OutputStream to marshal the contents to.158* @exception IOException on errors.159*/160public void encode(OutputStream out) throws IOException {161// Nothing for default162if (version == V1) {163return;164}165DerOutputStream tmp = new DerOutputStream();166tmp.putInteger(version);167168DerOutputStream seq = new DerOutputStream();169seq.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),170tmp);171172out.write(seq.toByteArray());173}174175/**176* Set the attribute value.177*/178public void set(String name, Object obj) throws IOException {179if (!(obj instanceof Integer)) {180throw new IOException("Attribute must be of type Integer.");181}182if (name.equalsIgnoreCase(VERSION)) {183version = ((Integer)obj).intValue();184} else {185throw new IOException("Attribute name not recognized by " +186"CertAttrSet: CertificateVersion.");187}188}189190/**191* Get the attribute value.192*/193public Integer get(String name) throws IOException {194if (name.equalsIgnoreCase(VERSION)) {195return(getVersion());196} else {197throw new IOException("Attribute name not recognized by " +198"CertAttrSet: CertificateVersion.");199}200}201202/**203* Delete the attribute value.204*/205public void delete(String name) throws IOException {206if (name.equalsIgnoreCase(VERSION)) {207version = V1;208} else {209throw new IOException("Attribute name not recognized by " +210"CertAttrSet: CertificateVersion.");211}212}213214/**215* Return an enumeration of names of attributes existing within this216* attribute.217*/218public Enumeration<String> getElements() {219AttributeNameEnumeration elements = new AttributeNameEnumeration();220elements.addElement(VERSION);221222return (elements.elements());223}224225/**226* Return the name of this attribute.227*/228public String getName() {229return(NAME);230}231232/**233* Compare versions.234*/235public int compare(int vers) {236return(version - vers);237}238}239240241