1/*
2 * Copyright (c) 1997, 2006, 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 */
25package sun.security.x509;
26
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.math.BigInteger;
31import java.util.Enumeration;
32
33import sun.security.util.*;
34
35/**
36 * This class defines the SerialNumber attribute for the Certificate.
37 *
38 * @author Amit Kapoor
39 * @author Hemma Prafullchandra
40 * @see CertAttrSet
41 */
42public class CertificateSerialNumber implements CertAttrSet<String> {
43    /**
44     * Identifier for this attribute, to be used with the
45     * get, set, delete methods of Certificate, x509 type.
46     */
47    public static final String IDENT = "x509.info.serialNumber";
48
49    /**
50     * Sub attributes name for this CertAttrSet.
51     */
52    public static final String NAME = "serialNumber";
53    public static final String NUMBER = "number";
54
55    private SerialNumber        serial;
56
57    /**
58     * Default constructor for the certificate attribute.
59     *
60     * @param serial the serial number for the certificate.
61     */
62    public CertificateSerialNumber(BigInteger num) {
63      this.serial = new SerialNumber(num);
64    }
65
66    /**
67     * Default constructor for the certificate attribute.
68     *
69     * @param serial the serial number for the certificate.
70     */
71    public CertificateSerialNumber(int num) {
72      this.serial = new SerialNumber(num);
73    }
74
75    /**
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     */
81    public CertificateSerialNumber(DerInputStream in) throws IOException {
82        serial = new SerialNumber(in);
83    }
84
85    /**
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     */
91    public CertificateSerialNumber(InputStream in) throws IOException {
92        serial = new SerialNumber(in);
93    }
94
95    /**
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     */
101    public CertificateSerialNumber(DerValue val) throws IOException {
102        serial = new SerialNumber(val);
103    }
104
105    /**
106     * Return the serial number as user readable string.
107     */
108    public String toString() {
109        if (serial == null) return "";
110        return (serial.toString());
111    }
112
113    /**
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     */
119    public void encode(OutputStream out) throws IOException {
120        DerOutputStream tmp = new DerOutputStream();
121        serial.encode(tmp);
122
123        out.write(tmp.toByteArray());
124    }
125
126    /**
127     * Set the attribute value.
128     */
129    public void set(String name, Object obj) throws IOException {
130        if (!(obj instanceof SerialNumber)) {
131            throw new IOException("Attribute must be of type SerialNumber.");
132        }
133        if (name.equalsIgnoreCase(NUMBER)) {
134            serial = (SerialNumber)obj;
135        } else {
136            throw new IOException("Attribute name not recognized by " +
137                                "CertAttrSet:CertificateSerialNumber.");
138        }
139    }
140
141    /**
142     * Get the attribute value.
143     */
144    public Object get(String name) throws IOException {
145        if (name.equalsIgnoreCase(NUMBER)) {
146            return (serial);
147        } else {
148            throw new IOException("Attribute name not recognized by " +
149                                "CertAttrSet:CertificateSerialNumber.");
150        }
151    }
152
153    /**
154     * Delete the attribute value.
155     */
156    public void delete(String name) throws IOException {
157        if (name.equalsIgnoreCase(NUMBER)) {
158            serial = null;
159        } else {
160            throw new IOException("Attribute name not recognized by " +
161                                "CertAttrSet:CertificateSerialNumber.");
162        }
163    }
164
165    /**
166     * Return an enumeration of names of attributes existing within this
167     * attribute.
168     */
169    public Enumeration<String> getElements() {
170        AttributeNameEnumeration elements = new AttributeNameEnumeration();
171        elements.addElement(NUMBER);
172
173        return (elements.elements());
174    }
175
176    /**
177     * Return the name of this attribute.
178     */
179    public String getName() {
180        return (NAME);
181    }
182}
183