001: /*
002: * @(#)CRLNumberExtension.java 1.14 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:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.lang.reflect.Array;
034: import java.math.BigInteger;
035: import java.util.Enumeration;
036:
037: import sun.security.util.*;
038:
039: /**
040: * Represent the CRL Number Extension.
041: *
042: * <p>This extension, if present, conveys a monotonically increasing
043: * sequence number for each CRL issued by a given CA through a specific
044: * CA X.500 Directory entry or CRL distribution point. This extension
045: * allows users to easily determine when a particular CRL supersedes
046: * another CRL.
047: *
048: * @author Hemma Prafullchandra
049: * @version 1.7
050: * @see Extension
051: * @see CertAttrSet
052: */
053: public class CRLNumberExtension extends Extension implements
054: CertAttrSet {
055:
056: /**
057: * Attribute name.
058: */
059: public static final String NAME = "CRLNumber";
060: public static final String NUMBER = "value";
061:
062: private BigInteger crlNumber = null;
063:
064: // Encode this extension value
065: private void encodeThis() throws IOException {
066: if (crlNumber == null) {
067: this .extensionValue = null;
068: return;
069: }
070: DerOutputStream os = new DerOutputStream();
071: os.putInteger(this .crlNumber);
072: this .extensionValue = os.toByteArray();
073: }
074:
075: /**
076: * Create a CRLNumberExtension with the integer value .
077: * The criticality is set to false.
078: *
079: * @param crlNum the value to be set for the extension.
080: */
081: public CRLNumberExtension(int crlNum) throws IOException {
082: this .crlNumber = BigInteger.valueOf(crlNum);
083: this .extensionId = PKIXExtensions.CRLNumber_Id;
084: this .critical = false;
085: encodeThis();
086: }
087:
088: /**
089: * Create a CRLNumberExtension with the BigInteger value .
090: * The criticality is set to false.
091: *
092: * @param crlNum the value to be set for the extension.
093: */
094: public CRLNumberExtension(BigInteger crlNum) throws IOException {
095: this .crlNumber = crlNum;
096: this .extensionId = PKIXExtensions.CRLNumber_Id;
097: this .critical = false;
098: encodeThis();
099: }
100:
101: /**
102: * Create the extension from the passed DER encoded value of the same.
103: *
104: * @param critical true if the extension is to be treated as critical.
105: * @param value Array of DER encoded bytes of the actual value.
106: * @exception IOException on error.
107: */
108: public CRLNumberExtension(Boolean critical, Object value)
109: throws IOException {
110: this .extensionId = PKIXExtensions.CRLNumber_Id;
111: this .critical = critical.booleanValue();
112:
113: int len = Array.getLength(value);
114: byte[] extValue = new byte[len];
115: for (int i = 0; i < len; i++) {
116: extValue[i] = Array.getByte(value, i);
117: }
118: this .extensionValue = extValue;
119: DerValue val = new DerValue(extValue);
120: this .crlNumber = val.getBigInteger();
121: }
122:
123: /**
124: * Set the attribute value.
125: */
126: public void set(String name, Object obj) throws IOException {
127: if (name.equalsIgnoreCase(NUMBER)) {
128: if (!(obj instanceof BigInteger)) {
129: throw new IOException(
130: "Attribute must be of type BigInteger.");
131: }
132: crlNumber = (BigInteger) obj;
133: } else {
134: throw new IOException("Attribute name not recognized by"
135: + " CertAttrSet:CRLNumber.");
136: }
137: encodeThis();
138: }
139:
140: /**
141: * Get the attribute value.
142: */
143: public Object get(String name) throws IOException {
144: if (name.equalsIgnoreCase(NUMBER)) {
145: if (crlNumber == null)
146: return null;
147: else
148: return crlNumber;
149: } else {
150: throw new IOException("Attribute name not recognized by"
151: + " CertAttrSet:CRLNumber.");
152: }
153: }
154:
155: /**
156: * Delete the attribute value.
157: */
158: public void delete(String name) throws IOException {
159: if (name.equalsIgnoreCase(NUMBER)) {
160: crlNumber = null;
161: } else {
162: throw new IOException("Attribute name not recognized by"
163: + " CertAttrSet:CRLNumber.");
164: }
165: encodeThis();
166: }
167:
168: /**
169: * Returns a printable representation of the CRLNumberExtension.
170: */
171: public String toString() {
172: String s = super .toString()
173: + "CRL Number: "
174: + ((crlNumber == null) ? "" : Debug
175: .toHexString(crlNumber)) + "\n";
176: return (s);
177: }
178:
179: /**
180: * Decode the extension from the InputStream.
181: *
182: * @param in the InputStream to unmarshal the contents from.
183: * @exception IOException on decoding or validity errors.
184: */
185: public void decode(InputStream in) throws IOException {
186: throw new IOException("Method not to be called directly.");
187: }
188:
189: /**
190: * Write the extension to the DerOutputStream.
191: *
192: * @param out the DerOutputStream to write the extension to.
193: * @exception IOException on encoding errors.
194: */
195: public void encode(OutputStream out) throws IOException {
196: DerOutputStream tmp = new DerOutputStream();
197:
198: if (this .extensionValue == null) {
199: this .extensionId = PKIXExtensions.CRLNumber_Id;
200: this .critical = true;
201: encodeThis();
202: }
203: super .encode(tmp);
204: out.write(tmp.toByteArray());
205: }
206:
207: /**
208: * Return an enumeration of names of attributes existing within this
209: * attribute.
210: */
211: public Enumeration getElements() {
212: AttributeNameEnumeration elements = new AttributeNameEnumeration();
213: elements.addElement(NUMBER);
214: return (elements.elements());
215: }
216:
217: /**
218: * Return the name of this attribute.
219: */
220: public String getName() {
221: return (NAME);
222: }
223: }
|