Path: blob/master/src/java.base/share/classes/sun/security/x509/CRLReasonCodeExtension.java
41159 views
/*1* Copyright (c) 1997, 2014, 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.security.cert.CRLReason;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* The reasonCode is a non-critical CRL entry extension that identifies36* the reason for the certificate revocation.37* @author Hemma Prafullchandra38* @see java.security.cert.CRLReason39* @see Extension40* @see CertAttrSet41*/42public class CRLReasonCodeExtension extends Extension43implements CertAttrSet<String> {4445/**46* Attribute name47*/48public static final String NAME = "CRLReasonCode";49public static final String REASON = "reason";5051private static CRLReason[] values = CRLReason.values();5253private int reasonCode = 0;5455private void encodeThis() throws IOException {56if (reasonCode == 0) {57this.extensionValue = null;58return;59}60DerOutputStream dos = new DerOutputStream();61dos.putEnumerated(reasonCode);62this.extensionValue = dos.toByteArray();63}6465/**66* Create a CRLReasonCodeExtension with the passed in reason.67* Criticality automatically set to false.68*69* @param reason the enumerated value for the reason code.70*/71public CRLReasonCodeExtension(int reason) throws IOException {72this(false, reason);73}7475/**76* Create a CRLReasonCodeExtension with the passed in reason.77*78* @param critical true if the extension is to be treated as critical.79* @param reason the enumerated value for the reason code.80*/81public CRLReasonCodeExtension(boolean critical, int reason)82throws IOException {83this.extensionId = PKIXExtensions.ReasonCode_Id;84this.critical = critical;85this.reasonCode = reason;86encodeThis();87}8889/**90* Create the extension from the passed DER encoded value of the same.91*92* @param critical true if the extension is to be treated as critical.93* @param value an array of DER encoded bytes of the actual value.94* @exception ClassCastException if value is not an array of bytes95* @exception IOException on error.96*/97public CRLReasonCodeExtension(Boolean critical, Object value)98throws IOException {99this.extensionId = PKIXExtensions.ReasonCode_Id;100this.critical = critical.booleanValue();101this.extensionValue = (byte[]) value;102DerValue val = new DerValue(this.extensionValue);103this.reasonCode = val.getEnumerated();104}105106/**107* Set the attribute value.108*/109public void set(String name, Object obj) throws IOException {110if (!(obj instanceof Integer)) {111throw new IOException("Attribute must be of type Integer.");112}113if (name.equalsIgnoreCase(REASON)) {114reasonCode = ((Integer)obj).intValue();115} else {116throw new IOException117("Name not supported by CRLReasonCodeExtension");118}119encodeThis();120}121122/**123* Get the attribute value.124*/125public Integer get(String name) throws IOException {126if (name.equalsIgnoreCase(REASON)) {127return reasonCode;128} else {129throw new IOException130("Name not supported by CRLReasonCodeExtension");131}132}133134/**135* Delete the attribute value.136*/137public void delete(String name) throws IOException {138if (name.equalsIgnoreCase(REASON)) {139reasonCode = 0;140} else {141throw new IOException142("Name not supported by CRLReasonCodeExtension");143}144encodeThis();145}146147/**148* Returns a printable representation of the Reason code.149*/150public String toString() {151return super.toString() + " Reason Code: " + getReasonCode();152}153154/**155* Write the extension to the DerOutputStream.156*157* @param out the DerOutputStream to write the extension to.158* @exception IOException on encoding errors.159*/160public void encode(OutputStream out) throws IOException {161DerOutputStream tmp = new DerOutputStream();162163if (this.extensionValue == null) {164this.extensionId = PKIXExtensions.ReasonCode_Id;165this.critical = false;166encodeThis();167}168super.encode(tmp);169out.write(tmp.toByteArray());170}171172/**173* Return an enumeration of names of attributes existing within this174* attribute.175*/176public Enumeration<String> getElements() {177AttributeNameEnumeration elements = new AttributeNameEnumeration();178elements.addElement(REASON);179180return elements.elements();181}182183/**184* Return the name of this attribute.185*/186public String getName() {187return NAME;188}189190/**191* Return the reason as a CRLReason enum.192*/193public CRLReason getReasonCode() {194// if out-of-range, return UNSPECIFIED195if (reasonCode > 0 && reasonCode < values.length) {196return values[reasonCode];197} else {198return CRLReason.UNSPECIFIED;199}200}201}202203204