001: /*
002: * @(#)PolicyMappingsExtension.java 1.19 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.lang.reflect.Array;
034: import java.security.cert.CertificateException;
035: import java.util.Vector;
036: import java.util.Enumeration;
037:
038: import sun.security.util.*;
039:
040: /**
041: * Represent the Policy Mappings Extension.
042: *
043: * This extension, if present, identifies the certificate policies considered
044: * identical between the issuing and the subject CA.
045: * <p>Extensions are addiitonal attributes which can be inserted in a X509
046: * v3 certificate. For example a "Driving License Certificate" could have
047: * the driving license number as a extension.
048: *
049: * <p>Extensions are represented as a sequence of the extension identifier
050: * (Object Identifier), a boolean flag stating whether the extension is to
051: * be treated as being critical and the extension value itself (this is again
052: * a DER encoding of the extension value).
053: *
054: * @author Amit Kapoor
055: * @author Hemma Prafullchandra
056: * @version 1.12
057: * @see Extension
058: * @see CertAttrSet
059: */
060: public class PolicyMappingsExtension extends Extension implements
061: CertAttrSet {
062: /**
063: * Identifier for this attribute, to be used with the
064: * get, set, delete methods of Certificate, x509 type.
065: */
066: public static final String IDENT = "x509.info.extensions.PolicyMappings";
067: /**
068: * Attribute names.
069: */
070: public static final String NAME = "PolicyMappings";
071: public static final String MAP = "map";
072:
073: // Private data members
074: private Vector maps = null;
075:
076: // Encode this extension value
077: private void encodeThis() throws IOException {
078: if (maps == null || maps.isEmpty()) {
079: this .extensionValue = null;
080: return;
081: }
082: DerOutputStream os = new DerOutputStream();
083: DerOutputStream tmp = new DerOutputStream();
084:
085: for (int i = 0; i < maps.size(); i++) {
086: ((CertificatePolicyMap) maps.elementAt(i)).encode(tmp);
087: }
088: os.write(DerValue.tag_Sequence, tmp);
089: this .extensionValue = os.toByteArray();
090: }
091:
092: /**
093: * Create a PolicyMappings with the Vector of CertificatePolicyMap.
094: *
095: * @param maps the Vector of CertificatePolicyMap.
096: */
097: public PolicyMappingsExtension(Vector map) throws IOException {
098: this .maps = map;
099: this .extensionId = PKIXExtensions.PolicyMappings_Id;
100: this .critical = false;
101: encodeThis();
102: }
103:
104: /**
105: * Create a default PolicyMappingsExtension.
106: */
107: public PolicyMappingsExtension() {
108: extensionId = PKIXExtensions.KeyUsage_Id;
109: critical = false;
110: maps = new Vector(1, 1);
111: }
112:
113: /**
114: * Create the extension from the passed DER encoded value.
115: *
116: * @params critical true if the extension is to be treated as critical.
117: * @params value Array of DER encoded bytes of the actual value.
118: * @exception IOException on error.
119: */
120: public PolicyMappingsExtension(Boolean critical, Object value)
121: throws IOException {
122: this .extensionId = PKIXExtensions.PolicyMappings_Id;
123: this .critical = critical.booleanValue();
124:
125: int len = Array.getLength(value);
126: byte[] extValue = new byte[len];
127: for (int i = 0; i < len; i++) {
128: extValue[i] = Array.getByte(value, i);
129: }
130: this .extensionValue = extValue;
131: DerValue val = new DerValue(extValue);
132: if (val.tag != DerValue.tag_Sequence) {
133: throw new IOException("Invalid encoding for "
134: + "PolicyMappingsExtension.");
135: }
136: maps = new Vector(1, 1);
137: while (val.data.available() != 0) {
138: DerValue seq = val.data.getDerValue();
139: CertificatePolicyMap map = new CertificatePolicyMap(seq);
140: maps.addElement(map);
141: }
142: }
143:
144: /**
145: * Returns a printable representation of the policy map.
146: */
147: public String toString() {
148: if (maps == null)
149: return "";
150: String s = super .toString() + "PolicyMappings [\n"
151: + maps.toString() + "]\n";
152:
153: return (s);
154: }
155:
156: /**
157: * Write the extension to the OutputStream.
158: *
159: * @param out the OutputStream to write the extension to.
160: * @exception IOException on encoding errors.
161: */
162: public void encode(OutputStream out) throws IOException {
163: DerOutputStream tmp = new DerOutputStream();
164: if (extensionValue == null) {
165: extensionId = PKIXExtensions.PolicyMappings_Id;
166: critical = false;
167: encodeThis();
168: }
169: super .encode(tmp);
170: out.write(tmp.toByteArray());
171: }
172:
173: /**
174: * Decode the extension from the InputStream.
175: *
176: * @param in the InputStream to unmarshal the contents from.
177: * @exception IOException on decoding or validity errors.
178: */
179: public void decode(InputStream in) throws IOException {
180: throw new IOException("Method not to be called directly.");
181: }
182:
183: /**
184: * Set the attribute value.
185: */
186: public void set(String name, Object obj) throws IOException {
187: if (name.equalsIgnoreCase(MAP)) {
188: if (!(obj instanceof Vector)) {
189: throw new IOException("Attribute value should be of"
190: + " type Vector.");
191: }
192: maps = (Vector) obj;
193: } else {
194: throw new IOException("Attribute name not recognized by "
195: + "CertAttrSet:PolicyMappingsExtension.");
196: }
197: encodeThis();
198: }
199:
200: /**
201: * Get the attribute value.
202: */
203: public Object get(String name) throws IOException {
204: if (name.equalsIgnoreCase(MAP)) {
205: return (maps);
206: } else {
207: throw new IOException("Attribute name not recognized by "
208: + "CertAttrSet:PolicyMappingsExtension.");
209: }
210: }
211:
212: /**
213: * Delete the attribute value.
214: */
215: public void delete(String name) throws IOException {
216: if (name.equalsIgnoreCase(MAP)) {
217: maps = null;
218: } else {
219: throw new IOException("Attribute name not recognized by "
220: + "CertAttrSet:PolicyMappingsExtension.");
221: }
222: encodeThis();
223: }
224:
225: /**
226: * Return an enumeration of names of attributes existing within this
227: * attribute.
228: */
229: public Enumeration getElements() {
230: AttributeNameEnumeration elements = new AttributeNameEnumeration();
231: elements.addElement(MAP);
232:
233: return (elements.elements());
234: }
235:
236: /**
237: * Return the name of this attribute.
238: */
239: public String getName() {
240: return (NAME);
241: }
242: }
|