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