Home | History | Annotate | Download | only in x509
      1 /*
      2  * Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 package sun.security.x509;
     26 
     27 import java.io.IOException;
     28 import java.io.InputStream;
     29 import java.math.BigInteger;
     30 
     31 import sun.security.util.*;
     32 
     33 /**
     34  * This class defines the SerialNumber class used by certificates.
     35  *
     36  * @author Amit Kapoor
     37  * @author Hemma Prafullchandra
     38  */
     39 public class SerialNumber {
     40     private BigInteger  serialNum;
     41 
     42     // Construct the class from the DerValue
     43     private void construct(DerValue derVal) throws IOException {
     44         serialNum = derVal.getBigInteger();
     45         if (derVal.data.available() != 0) {
     46             throw new IOException("Excess SerialNumber data");
     47         }
     48     }
     49 
     50     /**
     51      * The default constructor for this class using BigInteger.
     52      *
     53      * @param num the BigInteger number used to create the serial number.
     54      */
     55     public SerialNumber(BigInteger num) {
     56         serialNum = num;
     57     }
     58 
     59     /**
     60      * The default constructor for this class using int.
     61      *
     62      * @param num the BigInteger number used to create the serial number.
     63      */
     64     public SerialNumber(int num) {
     65         serialNum = BigInteger.valueOf(num);
     66     }
     67 
     68     /**
     69      * Create the object, decoding the values from the passed DER stream.
     70      *
     71      * @param in the DerInputStream to read the SerialNumber from.
     72      * @exception IOException on decoding errors.
     73      */
     74     public SerialNumber(DerInputStream in) throws IOException {
     75         DerValue derVal = in.getDerValue();
     76         construct(derVal);
     77     }
     78 
     79     /**
     80      * Create the object, decoding the values from the passed DerValue.
     81      *
     82      * @param val the DerValue to read the SerialNumber from.
     83      * @exception IOException on decoding errors.
     84      */
     85     public SerialNumber(DerValue val) throws IOException {
     86         construct(val);
     87     }
     88 
     89     /**
     90      * Create the object, decoding the values from the passed stream.
     91      *
     92      * @param in the InputStream to read the SerialNumber from.
     93      * @exception IOException on decoding errors.
     94      */
     95     public SerialNumber(InputStream in) throws IOException {
     96         DerValue derVal = new DerValue(in);
     97         construct(derVal);
     98     }
     99 
    100     /**
    101      * Return the SerialNumber as user readable string.
    102      */
    103     public String toString() {
    104         return ("SerialNumber: [" + Debug.toHexString(serialNum) + "]");
    105     }
    106 
    107     /**
    108      * Encode the SerialNumber in DER form to the stream.
    109      *
    110      * @param out the DerOutputStream to marshal the contents to.
    111      * @exception IOException on errors.
    112      */
    113     public void encode(DerOutputStream out) throws IOException {
    114         out.putInteger(serialNum);
    115     }
    116 
    117     /**
    118      * Return the serial number.
    119      */
    120     public BigInteger getNumber() {
    121         return serialNum;
    122     }
    123 }
    124