Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/x509/CertificateSerialNumber.java
38831 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*/24package sun.security.x509;2526import java.io.IOException;27import java.io.InputStream;28import java.io.OutputStream;29import java.math.BigInteger;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* This class defines the SerialNumber attribute for the Certificate.36*37* @author Amit Kapoor38* @author Hemma Prafullchandra39* @see CertAttrSet40*/41public class CertificateSerialNumber implements CertAttrSet<String> {42/**43* Identifier for this attribute, to be used with the44* get, set, delete methods of Certificate, x509 type.45*/46public static final String IDENT = "x509.info.serialNumber";4748/**49* Sub attributes name for this CertAttrSet.50*/51public static final String NAME = "serialNumber";52public static final String NUMBER = "number";5354private SerialNumber serial;5556/**57* Default constructor for the certificate attribute.58*59* @param serial the serial number for the certificate.60*/61public CertificateSerialNumber(BigInteger num) {62this.serial = new SerialNumber(num);63}6465/**66* Default constructor for the certificate attribute.67*68* @param serial the serial number for the certificate.69*/70public CertificateSerialNumber(int num) {71this.serial = new SerialNumber(num);72}7374/**75* Create the object, decoding the values from the passed DER stream.76*77* @param in the DerInputStream to read the serial number from.78* @exception IOException on decoding errors.79*/80public CertificateSerialNumber(DerInputStream in) throws IOException {81serial = new SerialNumber(in);82}8384/**85* Create the object, decoding the values from the passed stream.86*87* @param in the InputStream to read the serial number from.88* @exception IOException on decoding errors.89*/90public CertificateSerialNumber(InputStream in) throws IOException {91serial = new SerialNumber(in);92}9394/**95* Create the object, decoding the values from the passed DerValue.96*97* @param val the DER encoded value.98* @exception IOException on decoding errors.99*/100public CertificateSerialNumber(DerValue val) throws IOException {101serial = new SerialNumber(val);102}103104/**105* Return the serial number as user readable string.106*/107public String toString() {108if (serial == null) return "";109return (serial.toString());110}111112/**113* Encode the serial number in DER form to the stream.114*115* @param out the DerOutputStream to marshal the contents to.116* @exception IOException on errors.117*/118public void encode(OutputStream out) throws IOException {119DerOutputStream tmp = new DerOutputStream();120serial.encode(tmp);121122out.write(tmp.toByteArray());123}124125/**126* Set the attribute value.127*/128public void set(String name, Object obj) throws IOException {129if (!(obj instanceof SerialNumber)) {130throw new IOException("Attribute must be of type SerialNumber.");131}132if (name.equalsIgnoreCase(NUMBER)) {133serial = (SerialNumber)obj;134} else {135throw new IOException("Attribute name not recognized by " +136"CertAttrSet:CertificateSerialNumber.");137}138}139140/**141* Get the attribute value.142*/143public SerialNumber get(String name) throws IOException {144if (name.equalsIgnoreCase(NUMBER)) {145return (serial);146} else {147throw new IOException("Attribute name not recognized by " +148"CertAttrSet:CertificateSerialNumber.");149}150}151152/**153* Delete the attribute value.154*/155public void delete(String name) throws IOException {156if (name.equalsIgnoreCase(NUMBER)) {157serial = null;158} else {159throw new IOException("Attribute name not recognized by " +160"CertAttrSet:CertificateSerialNumber.");161}162}163164/**165* Return an enumeration of names of attributes existing within this166* attribute.167*/168public Enumeration<String> getElements() {169AttributeNameEnumeration elements = new AttributeNameEnumeration();170elements.addElement(NUMBER);171172return (elements.elements());173}174175/**176* Return the name of this attribute.177*/178public String getName() {179return (NAME);180}181}182183184