J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 26 | package sun.security.x509; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.OutputStream; |
| 30 | import java.util.*; |
| 31 | |
| 32 | import java.security.cert.CertificateException; |
| 33 | |
| 34 | import sun.security.util.*; |
| 35 | |
| 36 | /** |
| 37 | * Represent the Policy Mappings Extension. |
| 38 | * |
| 39 | * This extension, if present, identifies the certificate policies considered |
| 40 | * identical between the issuing and the subject CA. |
| 41 | * <p>Extensions are addiitonal attributes which can be inserted in a X509 |
| 42 | * v3 certificate. For example a "Driving License Certificate" could have |
| 43 | * the driving license number as a extension. |
| 44 | * |
| 45 | * <p>Extensions are represented as a sequence of the extension identifier |
| 46 | * (Object Identifier), a boolean flag stating whether the extension is to |
| 47 | * be treated as being critical and the extension value itself (this is again |
| 48 | * a DER encoding of the extension value). |
| 49 | * |
| 50 | * @author Amit Kapoor |
| 51 | * @author Hemma Prafullchandra |
| 52 | * @see Extension |
| 53 | * @see CertAttrSet |
| 54 | */ |
| 55 | public class PolicyMappingsExtension extends Extension |
| 56 | implements CertAttrSet<String> { |
| 57 | /** |
| 58 | * Identifier for this attribute, to be used with the |
| 59 | * get, set, delete methods of Certificate, x509 type. |
| 60 | */ |
| 61 | public static final String IDENT = "x509.info.extensions.PolicyMappings"; |
| 62 | /** |
| 63 | * Attribute names. |
| 64 | */ |
| 65 | public static final String NAME = "PolicyMappings"; |
| 66 | public static final String MAP = "map"; |
| 67 | |
| 68 | // Private data members |
| 69 | private List<CertificatePolicyMap> maps; |
| 70 | |
| 71 | // Encode this extension value |
| 72 | private void encodeThis() throws IOException { |
| 73 | if (maps == null || maps.isEmpty()) { |
| 74 | this.extensionValue = null; |
| 75 | return; |
| 76 | } |
| 77 | DerOutputStream os = new DerOutputStream(); |
| 78 | DerOutputStream tmp = new DerOutputStream(); |
| 79 | |
| 80 | for (CertificatePolicyMap map : maps) { |
| 81 | map.encode(tmp); |
| 82 | } |
| 83 | |
| 84 | os.write(DerValue.tag_Sequence, tmp); |
| 85 | this.extensionValue = os.toByteArray(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create a PolicyMappings with the List of CertificatePolicyMap. |
| 90 | * |
| 91 | * @param maps the List of CertificatePolicyMap. |
| 92 | */ |
| 93 | public PolicyMappingsExtension(List<CertificatePolicyMap> map) |
| 94 | throws IOException { |
| 95 | this.maps = map; |
| 96 | this.extensionId = PKIXExtensions.PolicyMappings_Id; |
| 97 | this.critical = false; |
| 98 | encodeThis(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Create a default PolicyMappingsExtension. |
| 103 | */ |
| 104 | public PolicyMappingsExtension() { |
| 105 | extensionId = PKIXExtensions.KeyUsage_Id; |
| 106 | critical = false; |
| 107 | maps = new ArrayList<CertificatePolicyMap>(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Create the extension from the passed DER encoded value. |
| 112 | * |
| 113 | * @params critical true if the extension is to be treated as critical. |
| 114 | * @params value an array of DER encoded bytes of the actual value. |
| 115 | * @exception ClassCastException if value is not an array of bytes |
| 116 | * @exception IOException on error. |
| 117 | */ |
| 118 | public PolicyMappingsExtension(Boolean critical, Object value) |
| 119 | throws IOException { |
| 120 | this.extensionId = PKIXExtensions.PolicyMappings_Id; |
| 121 | this.critical = critical.booleanValue(); |
| 122 | |
| 123 | this.extensionValue = (byte[]) value; |
| 124 | DerValue val = new DerValue(this.extensionValue); |
| 125 | if (val.tag != DerValue.tag_Sequence) { |
| 126 | throw new IOException("Invalid encoding for " + |
| 127 | "PolicyMappingsExtension."); |
| 128 | } |
| 129 | maps = new ArrayList<CertificatePolicyMap>(); |
| 130 | while (val.data.available() != 0) { |
| 131 | DerValue seq = val.data.getDerValue(); |
| 132 | CertificatePolicyMap map = new CertificatePolicyMap(seq); |
| 133 | maps.add(map); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns a printable representation of the policy map. |
| 139 | */ |
| 140 | public String toString() { |
| 141 | if (maps == null) return ""; |
| 142 | String s = super.toString() + "PolicyMappings [\n" |
| 143 | + maps.toString() + "]\n"; |
| 144 | |
| 145 | return (s); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Write the extension to the OutputStream. |
| 150 | * |
| 151 | * @param out the OutputStream to write the extension to. |
| 152 | * @exception IOException on encoding errors. |
| 153 | */ |
| 154 | public void encode(OutputStream out) throws IOException { |
| 155 | DerOutputStream tmp = new DerOutputStream(); |
| 156 | if (extensionValue == null) { |
| 157 | extensionId = PKIXExtensions.PolicyMappings_Id; |
| 158 | critical = false; |
| 159 | encodeThis(); |
| 160 | } |
| 161 | super.encode(tmp); |
| 162 | out.write(tmp.toByteArray()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Set the attribute value. |
| 167 | */ |
| 168 | public void set(String name, Object obj) throws IOException { |
| 169 | if (name.equalsIgnoreCase(MAP)) { |
| 170 | if (!(obj instanceof List)) { |
| 171 | throw new IOException("Attribute value should be of" + |
| 172 | " type List."); |
| 173 | } |
| 174 | maps = (List<CertificatePolicyMap>)obj; |
| 175 | } else { |
| 176 | throw new IOException("Attribute name not recognized by " + |
| 177 | "CertAttrSet:PolicyMappingsExtension."); |
| 178 | } |
| 179 | encodeThis(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the attribute value. |
| 184 | */ |
| 185 | public Object get(String name) throws IOException { |
| 186 | if (name.equalsIgnoreCase(MAP)) { |
| 187 | return (maps); |
| 188 | } else { |
| 189 | throw new IOException("Attribute name not recognized by " + |
| 190 | "CertAttrSet:PolicyMappingsExtension."); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Delete the attribute value. |
| 196 | */ |
| 197 | public void delete(String name) throws IOException { |
| 198 | if (name.equalsIgnoreCase(MAP)) { |
| 199 | maps = null; |
| 200 | } else { |
| 201 | throw new IOException("Attribute name not recognized by " + |
| 202 | "CertAttrSet:PolicyMappingsExtension."); |
| 203 | } |
| 204 | encodeThis(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Return an enumeration of names of attributes existing within this |
| 209 | * attribute. |
| 210 | */ |
| 211 | public Enumeration<String> getElements () { |
| 212 | AttributeNameEnumeration elements = new AttributeNameEnumeration(); |
| 213 | elements.addElement(MAP); |
| 214 | |
| 215 | return elements.elements(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Return the name of this attribute. |
| 220 | */ |
| 221 | public String getName () { |
| 222 | return (NAME); |
| 223 | } |
| 224 | } |