001: /*
002: * @(#)CertificateSerialNumber.java 1.17 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.security.x509;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032: import java.math.BigInteger;
033: import java.util.Enumeration;
034:
035: import sun.security.util.*;
036:
037: /**
038: * This class defines the SerialNumber attribute for the Certificate.
039: *
040: * @author Amit Kapoor
041: * @author Hemma Prafullchandra
042: * @version 1.10
043: * @see CertAttrSet
044: */
045: public class CertificateSerialNumber implements CertAttrSet {
046: /**
047: * Identifier for this attribute, to be used with the
048: * get, set, delete methods of Certificate, x509 type.
049: */
050: public static final String IDENT = "x509.info.serialNumber";
051:
052: /**
053: * Sub attributes name for this CertAttrSet.
054: */
055: public static final String NAME = "serialNumber";
056: public static final String NUMBER = "number";
057:
058: private SerialNumber serial;
059:
060: /**
061: * Default constructor for the certificate attribute.
062: *
063: * @param serial the serial number for the certificate.
064: */
065: public CertificateSerialNumber(BigInteger num) {
066: this .serial = new SerialNumber(num);
067: }
068:
069: /**
070: * Default constructor for the certificate attribute.
071: *
072: * @param serial the serial number for the certificate.
073: */
074: public CertificateSerialNumber(int num) {
075: this .serial = new SerialNumber(num);
076: }
077:
078: /**
079: * Create the object, decoding the values from the passed DER stream.
080: *
081: * @param in the DerInputStream to read the serial number from.
082: * @exception IOException on decoding errors.
083: */
084: public CertificateSerialNumber(DerInputStream in)
085: throws IOException {
086: serial = new SerialNumber(in);
087: }
088:
089: /**
090: * Create the object, decoding the values from the passed stream.
091: *
092: * @param in the InputStream to read the serial number from.
093: * @exception IOException on decoding errors.
094: */
095: public CertificateSerialNumber(InputStream in) throws IOException {
096: serial = new SerialNumber(in);
097: }
098:
099: /**
100: * Create the object, decoding the values from the passed DerValue.
101: *
102: * @param val the DER encoded value.
103: * @exception IOException on decoding errors.
104: */
105: public CertificateSerialNumber(DerValue val) throws IOException {
106: serial = new SerialNumber(val);
107: }
108:
109: /**
110: * Return the serial number as user readable string.
111: */
112: public String toString() {
113: if (serial == null)
114: return "";
115: return (serial.toString());
116: }
117:
118: /**
119: * Encode the serial number in DER form to the stream.
120: *
121: * @param out the DerOutputStream to marshal the contents to.
122: * @exception IOException on errors.
123: */
124: public void encode(OutputStream out) throws IOException {
125: DerOutputStream tmp = new DerOutputStream();
126: serial.encode(tmp);
127:
128: out.write(tmp.toByteArray());
129: }
130:
131: /**
132: * Decode the serial number in DER form from the stream.
133: *
134: * @param in the InputStream to marshal the contents from.
135: * @exception IOException on errors.
136: */
137: public void decode(InputStream in) throws IOException {
138: DerValue derVal = new DerValue(in);
139: serial = new SerialNumber(derVal);
140: }
141:
142: /**
143: * Set the attribute value.
144: */
145: public void set(String name, Object obj) throws IOException {
146: if (!(obj instanceof SerialNumber)) {
147: throw new IOException(
148: "Attribute must be of type SerialNumber.");
149: }
150: if (name.equalsIgnoreCase(NUMBER)) {
151: serial = (SerialNumber) obj;
152: } else {
153: throw new IOException("Attribute name not recognized by "
154: + "CertAttrSet:CertificateSerialNumber.");
155: }
156: }
157:
158: /**
159: * Get the attribute value.
160: */
161: public Object get(String name) throws IOException {
162: if (name.equalsIgnoreCase(NUMBER)) {
163: return (serial);
164: } else {
165: throw new IOException("Attribute name not recognized by "
166: + "CertAttrSet:CertificateSerialNumber.");
167: }
168: }
169:
170: /**
171: * Delete the attribute value.
172: */
173: public void delete(String name) throws IOException {
174: if (name.equalsIgnoreCase(NUMBER)) {
175: serial = null;
176: } else {
177: throw new IOException("Attribute name not recognized by "
178: + "CertAttrSet:CertificateSerialNumber.");
179: }
180: }
181:
182: /**
183: * Return an enumeration of names of attributes existing within this
184: * attribute.
185: */
186: public Enumeration getElements() {
187: AttributeNameEnumeration elements = new AttributeNameEnumeration();
188: elements.addElement(NUMBER);
189:
190: return (elements.elements());
191: }
192:
193: /**
194: * Return the name of this attribute.
195: */
196: public String getName() {
197: return (NAME);
198: }
199: }
|