001: /*
002: * @(#)CertificateIssuerUniqueIdentity.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:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Enumeration;
034:
035: import sun.security.util.*;
036:
037: /**
038: * This class defines the subject/issuer unique identity attribute
039: * for the Certificate.
040: *
041: * @author Amit Kapoor
042: * @author Hemma Prafullchandra
043: * @version 1.10
044: * @see CertAttrSet
045: */
046: public class CertificateIssuerUniqueIdentity implements CertAttrSet {
047: private UniqueIdentity id;
048:
049: /**
050: * Identifier for this attribute, to be used with the
051: * get, set, delete methods of Certificate, x509 type.
052: */
053: public static final String IDENT = "x509.info.issuerID";
054: /**
055: * Sub attributes name for this CertAttrSet.
056: */
057: public static final String NAME = "issuerID";
058: public static final String ID = "id";
059:
060: /**
061: * Default constructor for the certificate attribute.
062: *
063: * @param key the UniqueIdentity
064: */
065: public CertificateIssuerUniqueIdentity(UniqueIdentity id) {
066: this .id = id;
067: }
068:
069: /**
070: * Create the object, decoding the values from the passed DER stream.
071: *
072: * @param in the DerInputStream to read the UniqueIdentity from.
073: * @exception IOException on decoding errors.
074: */
075: public CertificateIssuerUniqueIdentity(DerInputStream in)
076: throws IOException {
077: id = new UniqueIdentity(in);
078: }
079:
080: /**
081: * Create the object, decoding the values from the passed stream.
082: *
083: * @param in the InputStream to read the UniqueIdentity from.
084: * @exception IOException on decoding errors.
085: */
086: public CertificateIssuerUniqueIdentity(InputStream in)
087: throws IOException {
088: DerValue val = new DerValue(in);
089: id = new UniqueIdentity(val);
090: }
091:
092: /**
093: * Create the object, decoding the values from the passed DER value.
094: *
095: * @param in the DerValue to read the UniqueIdentity from.
096: * @exception IOException on decoding errors.
097: */
098: public CertificateIssuerUniqueIdentity(DerValue val)
099: throws IOException {
100: id = new UniqueIdentity(val);
101: }
102:
103: /**
104: * Return the identity as user readable string.
105: */
106: public String toString() {
107: if (id == null)
108: return "";
109: return (id.toString());
110: }
111:
112: /**
113: * Decode the identity in DER form from the stream.
114: *
115: * @param in the InputStream to unmarshal the contents from.
116: * @exception IOException on errors.
117: */
118: public void decode(InputStream in) throws IOException {
119: DerValue val = new DerValue(in);
120: id = new UniqueIdentity(val);
121: }
122:
123: /**
124: * Encode the identity in DER form to the stream.
125: *
126: * @param out the DerOutputStream to marshal the contents to.
127: * @exception IOException on errors.
128: */
129: public void encode(OutputStream out) throws IOException {
130: DerOutputStream tmp = new DerOutputStream();
131: id.encode(tmp, DerValue.createTag(DerValue.TAG_CONTEXT, false,
132: (byte) 1));
133:
134: out.write(tmp.toByteArray());
135: }
136:
137: /**
138: * Set the attribute value.
139: */
140: public void set(String name, Object obj) throws IOException {
141: if (!(obj instanceof UniqueIdentity)) {
142: throw new IOException(
143: "Attribute must be of type UniqueIdentity.");
144: }
145: if (name.equalsIgnoreCase(ID)) {
146: id = (UniqueIdentity) obj;
147: } else {
148: throw new IOException("Attribute name not recognized by "
149: + "CertAttrSet: CertificateIssuerUniqueIdentity.");
150: }
151: }
152:
153: /**
154: * Get the attribute value.
155: */
156: public Object get(String name) throws IOException {
157: if (name.equalsIgnoreCase(ID)) {
158: return (id);
159: } else {
160: throw new IOException("Attribute name not recognized by "
161: + "CertAttrSet: CertificateIssuerUniqueIdentity.");
162: }
163: }
164:
165: /**
166: * Delete the attribute value.
167: */
168: public void delete(String name) throws IOException {
169: if (name.equalsIgnoreCase(ID)) {
170: id = null;
171: } else {
172: throw new IOException("Attribute name not recognized by "
173: + "CertAttrSet: CertificateIssuerUniqueIdentity.");
174: }
175: }
176:
177: /**
178: * Return an enumeration of names of attributes existing within this
179: * attribute.
180: */
181: public Enumeration getElements() {
182: AttributeNameEnumeration elements = new AttributeNameEnumeration();
183: elements.addElement(ID);
184:
185: return (elements.elements());
186: }
187:
188: /**
189: * Return the name of this attribute.
190: */
191: public String getName() {
192: return (NAME);
193: }
194: }
|