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