Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateSerialNumber.java
41159 views
/*1* Copyright (c) 1997, 2019, 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;31import java.util.Random;3233import sun.security.util.*;3435/**36* This class defines the SerialNumber attribute for the Certificate.37*38* @author Amit Kapoor39* @author Hemma Prafullchandra40* @see CertAttrSet41*/42public class CertificateSerialNumber implements CertAttrSet<String> {43/**44* Identifier for this attribute, to be used with the45* get, set, delete methods of Certificate, x509 type.46*/47public static final String IDENT = "x509.info.serialNumber";4849/**50* Sub attributes name for this CertAttrSet.51*/52public static final String NAME = "serialNumber";53public static final String NUMBER = "number";5455private SerialNumber serial;5657/**58* Default constructor for the certificate attribute.59*60* @param num the serial number for the certificate.61*/62public CertificateSerialNumber(BigInteger num) {63this.serial = new SerialNumber(num);64}6566/**67* Default constructor for the certificate attribute.68*69* @param num the serial number for the certificate.70*/71public CertificateSerialNumber(int num) {72this.serial = new SerialNumber(num);73}7475/**76* Create the object, decoding the values from the passed DER stream.77*78* @param in the DerInputStream to read the serial number from.79* @exception IOException on decoding errors.80*/81public CertificateSerialNumber(DerInputStream in) throws IOException {82serial = new SerialNumber(in);83}8485/**86* Create the object, decoding the values from the passed stream.87*88* @param in the InputStream to read the serial number from.89* @exception IOException on decoding errors.90*/91public CertificateSerialNumber(InputStream in) throws IOException {92serial = new SerialNumber(in);93}9495/**96* Create the object, decoding the values from the passed DerValue.97*98* @param val the DER encoded value.99* @exception IOException on decoding errors.100*/101public CertificateSerialNumber(DerValue val) throws IOException {102serial = new SerialNumber(val);103}104105/**106* Return the serial number as user readable string.107*/108public String toString() {109if (serial == null) return "";110return (serial.toString());111}112113/**114* Encode the serial number in DER form to the stream.115*116* @param out the DerOutputStream to marshal the contents to.117* @exception IOException on errors.118*/119public void encode(OutputStream out) throws IOException {120DerOutputStream tmp = new DerOutputStream();121serial.encode(tmp);122123out.write(tmp.toByteArray());124}125126/**127* Set the attribute value.128*/129public void set(String name, Object obj) throws IOException {130if (!(obj instanceof SerialNumber)) {131throw new IOException("Attribute must be of type SerialNumber.");132}133if (name.equalsIgnoreCase(NUMBER)) {134serial = (SerialNumber)obj;135} else {136throw new IOException("Attribute name not recognized by " +137"CertAttrSet:CertificateSerialNumber.");138}139}140141/**142* Get the attribute value.143*/144public SerialNumber get(String name) throws IOException {145if (name.equalsIgnoreCase(NUMBER)) {146return (serial);147} else {148throw new IOException("Attribute name not recognized by " +149"CertAttrSet:CertificateSerialNumber.");150}151}152153/**154* Delete the attribute value.155*/156public void delete(String name) throws IOException {157if (name.equalsIgnoreCase(NUMBER)) {158serial = null;159} else {160throw new IOException("Attribute name not recognized by " +161"CertAttrSet:CertificateSerialNumber.");162}163}164165/**166* Return an enumeration of names of attributes existing within this167* attribute.168*/169public Enumeration<String> getElements() {170AttributeNameEnumeration elements = new AttributeNameEnumeration();171elements.addElement(NUMBER);172173return (elements.elements());174}175176/**177* Return the name of this attribute.178*/179public String getName() {180return (NAME);181}182183/**184* Generates a new random serial number.185*/186public static CertificateSerialNumber newRandom64bit(Random rand) {187while (true) {188BigInteger b = new BigInteger(64, rand);189if (b.signum() != 0) {190return new CertificateSerialNumber(b);191}192}193}194}195196197