Path: blob/master/src/java.base/share/classes/sun/security/x509/CRLNumberExtension.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.OutputStream;29import java.math.BigInteger;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* Represent the CRL Number Extension.36*37* <p>This extension, if present, conveys a monotonically increasing38* sequence number for each CRL issued by a given CA through a specific39* CA X.500 Directory entry or CRL distribution point. This extension40* allows users to easily determine when a particular CRL supersedes41* another CRL.42*43* @author Hemma Prafullchandra44* @see Extension45* @see CertAttrSet46*/47public class CRLNumberExtension extends Extension48implements CertAttrSet<String> {4950/**51* Attribute name.52*/53public static final String NAME = "CRLNumber";54public static final String NUMBER = "value";5556private static final String LABEL = "CRL Number";5758private BigInteger crlNumber = null;59private String extensionName;60private String extensionLabel;6162// Encode this extension value63private void encodeThis() throws IOException {64if (crlNumber == null) {65this.extensionValue = null;66return;67}68DerOutputStream os = new DerOutputStream();69os.putInteger(this.crlNumber);70this.extensionValue = os.toByteArray();71}7273/**74* Create a CRLNumberExtension with the integer value .75* The criticality is set to false.76*77* @param crlNum the value to be set for the extension.78*/79public CRLNumberExtension(int crlNum) throws IOException {80this(PKIXExtensions.CRLNumber_Id, false, BigInteger.valueOf(crlNum),81NAME, LABEL);82}8384/**85* Create a CRLNumberExtension with the BigInteger value .86* The criticality is set to false.87*88* @param crlNum the value to be set for the extension.89*/90public CRLNumberExtension(BigInteger crlNum) throws IOException {91this(PKIXExtensions.CRLNumber_Id, false, crlNum, NAME, LABEL);92}9394/**95* Creates the extension (also called by the subclass).96*/97protected CRLNumberExtension(ObjectIdentifier extensionId,98boolean isCritical, BigInteger crlNum, String extensionName,99String extensionLabel) throws IOException {100101this.extensionId = extensionId;102this.critical = isCritical;103this.crlNumber = crlNum;104this.extensionName = extensionName;105this.extensionLabel = extensionLabel;106encodeThis();107}108109/**110* Create the extension from the passed DER encoded value of the same.111*112* @param critical true if the extension is to be treated as critical.113* @param value an array of DER encoded bytes of the actual value.114* @exception ClassCastException if value is not an array of bytes115* @exception IOException on error.116*/117public CRLNumberExtension(Boolean critical, Object value)118throws IOException {119this(PKIXExtensions.CRLNumber_Id, critical, value, NAME, LABEL);120}121122/**123* Creates the extension (also called by the subclass).124*/125protected CRLNumberExtension(ObjectIdentifier extensionId,126Boolean critical, Object value, String extensionName,127String extensionLabel) throws IOException {128129this.extensionId = extensionId;130this.critical = critical.booleanValue();131this.extensionValue = (byte[]) value;132DerValue val = new DerValue(this.extensionValue);133this.crlNumber = val.getBigInteger();134this.extensionName = extensionName;135this.extensionLabel = extensionLabel;136}137138/**139* Set the attribute value.140*/141public void set(String name, Object obj) throws IOException {142if (name.equalsIgnoreCase(NUMBER)) {143if (!(obj instanceof BigInteger)) {144throw new IOException("Attribute must be of type BigInteger.");145}146crlNumber = (BigInteger)obj;147} else {148throw new IOException("Attribute name not recognized by" +149" CertAttrSet:" + extensionName + '.');150}151encodeThis();152}153154/**155* Get the attribute value.156*/157public BigInteger get(String name) throws IOException {158if (name.equalsIgnoreCase(NUMBER)) {159return crlNumber;160} else {161throw new IOException("Attribute name not recognized by" +162" CertAttrSet:" + extensionName + '.');163}164}165166/**167* Delete the attribute value.168*/169public void delete(String name) throws IOException {170if (name.equalsIgnoreCase(NUMBER)) {171crlNumber = null;172} else {173throw new IOException("Attribute name not recognized by" +174" CertAttrSet:" + extensionName + '.');175}176encodeThis();177}178179/**180* Returns a printable representation of the CRLNumberExtension.181*/182public String toString() {183StringBuilder sb = new StringBuilder();184sb.append(super.toString())185.append(extensionLabel)186.append(": ");187if (crlNumber != null) {188sb.append(Debug.toHexString(crlNumber));189}190sb.append('\n');191return sb.toString();192}193194/**195* Write the extension to the DerOutputStream.196*197* @param out the DerOutputStream to write the extension to.198* @exception IOException on encoding errors.199*/200public void encode(OutputStream out) throws IOException {201DerOutputStream tmp = new DerOutputStream();202encode(out, PKIXExtensions.CRLNumber_Id, true);203}204205/**206* Write the extension to the DerOutputStream.207* (Also called by the subclass)208*/209protected void encode(OutputStream out, ObjectIdentifier extensionId,210boolean isCritical) throws IOException {211212DerOutputStream tmp = new DerOutputStream();213214if (this.extensionValue == null) {215this.extensionId = extensionId;216this.critical = isCritical;217encodeThis();218}219super.encode(tmp);220out.write(tmp.toByteArray());221}222223/**224* Return an enumeration of names of attributes existing within this225* attribute.226*/227public Enumeration<String> getElements() {228AttributeNameEnumeration elements = new AttributeNameEnumeration();229elements.addElement(NUMBER);230return (elements.elements());231}232233/**234* Return the name of this attribute.235*/236public String getName() {237return (extensionName);238}239}240241242