blob: 4e9749d81e16b3d1e99666d09f81364f6371a7fa [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.x509;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.OutputStream;
31import java.util.Enumeration;
32
33import sun.security.util.*;
34
35/**
36 * This class defines the subject/issuer unique identity attribute
37 * for the Certificate.
38 *
39 * @author Amit Kapoor
40 * @author Hemma Prafullchandra
41 * @see CertAttrSet
42 */
43public class CertificateIssuerUniqueIdentity implements CertAttrSet<String> {
44 private UniqueIdentity id;
45
46 /**
47 * Identifier for this attribute, to be used with the
48 * get, set, delete methods of Certificate, x509 type.
49 */
50 public static final String IDENT = "x509.info.issuerID";
51 /**
52 * Sub attributes name for this CertAttrSet.
53 */
54 public static final String NAME = "issuerID";
55 public static final String ID = "id";
56
57 /**
58 * Default constructor for the certificate attribute.
59 *
60 * @param key the UniqueIdentity
61 */
62 public CertificateIssuerUniqueIdentity(UniqueIdentity id) {
63 this.id = id;
64 }
65
66 /**
67 * Create the object, decoding the values from the passed DER stream.
68 *
69 * @param in the DerInputStream to read the UniqueIdentity from.
70 * @exception IOException on decoding errors.
71 */
72 public CertificateIssuerUniqueIdentity(DerInputStream in)
73 throws IOException {
74 id = new UniqueIdentity(in);
75 }
76
77 /**
78 * Create the object, decoding the values from the passed stream.
79 *
80 * @param in the InputStream to read the UniqueIdentity from.
81 * @exception IOException on decoding errors.
82 */
83 public CertificateIssuerUniqueIdentity(InputStream in)
84 throws IOException {
85 DerValue val = new DerValue(in);
86 id = new UniqueIdentity(val);
87 }
88
89 /**
90 * Create the object, decoding the values from the passed DER value.
91 *
92 * @param in the DerValue to read the UniqueIdentity from.
93 * @exception IOException on decoding errors.
94 */
95 public CertificateIssuerUniqueIdentity(DerValue val)
96 throws IOException {
97 id = new UniqueIdentity(val);
98 }
99
100 /**
101 * Return the identity as user readable string.
102 */
103 public String toString() {
104 if (id == null) return "";
105 return (id.toString());
106 }
107
108 /**
109 * Encode the identity in DER form to the stream.
110 *
111 * @param out the DerOutputStream to marshal the contents to.
112 * @exception IOException on errors.
113 */
114 public void encode(OutputStream out) throws IOException {
115 DerOutputStream tmp = new DerOutputStream();
116 id.encode(tmp,DerValue.createTag(DerValue.TAG_CONTEXT,false,(byte)1));
117
118 out.write(tmp.toByteArray());
119 }
120
121 /**
122 * Set the attribute value.
123 */
124 public void set(String name, Object obj) throws IOException {
125 if (!(obj instanceof UniqueIdentity)) {
126 throw new IOException("Attribute must be of type UniqueIdentity.");
127 }
128 if (name.equalsIgnoreCase(ID)) {
129 id = (UniqueIdentity)obj;
130 } else {
131 throw new IOException("Attribute name not recognized by " +
132 "CertAttrSet: CertificateIssuerUniqueIdentity.");
133 }
134 }
135
136 /**
137 * Get the attribute value.
138 */
139 public Object get(String name) throws IOException {
140 if (name.equalsIgnoreCase(ID)) {
141 return (id);
142 } else {
143 throw new IOException("Attribute name not recognized by " +
144 "CertAttrSet: CertificateIssuerUniqueIdentity.");
145 }
146 }
147
148 /**
149 * Delete the attribute value.
150 */
151 public void delete(String name) throws IOException {
152 if (name.equalsIgnoreCase(ID)) {
153 id = null;
154 } else {
155 throw new IOException("Attribute name not recognized by " +
156 "CertAttrSet: CertificateIssuerUniqueIdentity.");
157 }
158 }
159
160 /**
161 * Return an enumeration of names of attributes existing within this
162 * attribute.
163 */
164 public Enumeration<String> getElements() {
165 AttributeNameEnumeration elements = new AttributeNameEnumeration();
166 elements.addElement(ID);
167
168 return (elements.elements());
169 }
170
171 /**
172 * Return the name of this attribute.
173 */
174 public String getName() {
175 return (NAME);
176 }
177}