0N/A/*
2362N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.util.*;
0N/A
0N/Aimport sun.misc.HexDumpEncoder;
0N/A
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * This class defines the Extensions attribute for the Certificate.
0N/A *
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A * @see CertAttrSet
0N/A */
0N/Apublic class CertificateExtensions implements CertAttrSet<Extension> {
0N/A /**
0N/A * Identifier for this attribute, to be used with the
0N/A * get, set, delete methods of Certificate, x509 type.
0N/A */
0N/A public static final String IDENT = "x509.info.extensions";
0N/A /**
0N/A * name
0N/A */
0N/A public static final String NAME = "extensions";
0N/A
0N/A private static final Debug debug = Debug.getInstance("x509");
0N/A
5090N/A private Map<String,Extension> map = Collections.synchronizedMap(
5090N/A new TreeMap<String,Extension>());
0N/A private boolean unsupportedCritExt = false;
0N/A
0N/A private Map<String,Extension> unparseableExtensions;
0N/A
0N/A /**
0N/A * Default constructor.
0N/A */
0N/A public CertificateExtensions() { }
0N/A
0N/A /**
0N/A * Create the object, decoding the values from the passed DER stream.
0N/A *
0N/A * @param in the DerInputStream to read the Extension from.
0N/A * @exception IOException on decoding errors.
0N/A */
0N/A public CertificateExtensions(DerInputStream in) throws IOException {
0N/A init(in);
0N/A }
0N/A
0N/A // helper routine
0N/A private void init(DerInputStream in) throws IOException {
0N/A
0N/A DerValue[] exts = in.getSequence(5);
0N/A
0N/A for (int i = 0; i < exts.length; i++) {
0N/A Extension ext = new Extension(exts[i]);
0N/A parseExtension(ext);
0N/A }
0N/A }
0N/A
0N/A private static Class[] PARAMS = {Boolean.class, Object.class};
0N/A
0N/A // Parse the encoded extension
0N/A private void parseExtension(Extension ext) throws IOException {
0N/A try {
0N/A Class extClass = OIDMap.getClass(ext.getExtensionId());
0N/A if (extClass == null) { // Unsupported extension
0N/A if (ext.isCritical()) {
0N/A unsupportedCritExt = true;
0N/A }
0N/A if (map.put(ext.getExtensionId().toString(), ext) == null) {
0N/A return;
0N/A } else {
0N/A throw new IOException("Duplicate extensions not allowed");
0N/A }
0N/A }
0N/A Constructor cons = ((Class<?>)extClass).getConstructor(PARAMS);
0N/A
0N/A Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
0N/A ext.getExtensionValue()};
0N/A CertAttrSet certExt = (CertAttrSet)cons.newInstance(passed);
0N/A if (map.put(certExt.getName(), (Extension)certExt) != null) {
0N/A throw new IOException("Duplicate extensions not allowed");
0N/A }
0N/A } catch (InvocationTargetException invk) {
0N/A Throwable e = invk.getTargetException();
0N/A if (ext.isCritical() == false) {
0N/A // ignore errors parsing non-critical extensions
0N/A if (unparseableExtensions == null) {
5090N/A unparseableExtensions = new TreeMap<String,Extension>();
0N/A }
0N/A unparseableExtensions.put(ext.getExtensionId().toString(),
0N/A new UnparseableExtension(ext, e));
0N/A if (debug != null) {
0N/A debug.println("Error parsing extension: " + ext);
0N/A e.printStackTrace();
0N/A HexDumpEncoder h = new HexDumpEncoder();
0N/A System.err.println(h.encodeBuffer(ext.getExtensionValue()));
0N/A }
0N/A return;
0N/A }
0N/A if (e instanceof IOException) {
0N/A throw (IOException)e;
0N/A } else {
0N/A throw (IOException)new IOException(e.toString()).initCause(e);
0N/A }
0N/A } catch (IOException e) {
0N/A throw e;
0N/A } catch (Exception e) {
0N/A throw (IOException)new IOException(e.toString()).initCause(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Encode the extensions in DER form to the stream, setting
0N/A * the context specific tag as needed in the X.509 v3 certificate.
0N/A *
0N/A * @param out the DerOutputStream to marshal the contents to.
0N/A * @exception CertificateException on encoding errors.
0N/A * @exception IOException on errors.
0N/A */
0N/A public void encode(OutputStream out)
0N/A throws CertificateException, IOException {
0N/A encode(out, false);
0N/A }
0N/A
0N/A /**
0N/A * Encode the extensions in DER form to the stream.
0N/A *
0N/A * @param out the DerOutputStream to marshal the contents to.
0N/A * @param isCertReq if true then no context specific tag is added.
0N/A * @exception CertificateException on encoding errors.
0N/A * @exception IOException on errors.
0N/A */
0N/A public void encode(OutputStream out, boolean isCertReq)
0N/A throws CertificateException, IOException {
0N/A DerOutputStream extOut = new DerOutputStream();
0N/A Collection<Extension> allExts = map.values();
0N/A Object[] objs = allExts.toArray();
0N/A
0N/A for (int i = 0; i < objs.length; i++) {
0N/A if (objs[i] instanceof CertAttrSet)
0N/A ((CertAttrSet)objs[i]).encode(extOut);
0N/A else if (objs[i] instanceof Extension)
0N/A ((Extension)objs[i]).encode(extOut);
0N/A else
0N/A throw new CertificateException("Illegal extension object");
0N/A }
0N/A
0N/A DerOutputStream seq = new DerOutputStream();
0N/A seq.write(DerValue.tag_Sequence, extOut);
0N/A
0N/A DerOutputStream tmp;
0N/A if (!isCertReq) { // certificate
0N/A tmp = new DerOutputStream();
0N/A tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
0N/A seq);
0N/A } else
0N/A tmp = seq; // pkcs#10 certificateRequest
0N/A
0N/A out.write(tmp.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Set the attribute value.
0N/A * @param name the extension name used in the cache.
0N/A * @param obj the object to set.
0N/A * @exception IOException if the object could not be cached.
0N/A */
0N/A public void set(String name, Object obj) throws IOException {
0N/A if (obj instanceof Extension) {
0N/A map.put(name, (Extension)obj);
0N/A } else {
0N/A throw new IOException("Unknown extension type.");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the attribute value.
0N/A * @param name the extension name used in the lookup.
0N/A * @exception IOException if named extension is not found.
0N/A */
0N/A public Object get(String name) throws IOException {
0N/A Object obj = map.get(name);
0N/A if (obj == null) {
0N/A throw new IOException("No extension found with name " + name);
0N/A }
0N/A return (obj);
0N/A }
0N/A
5090N/A // Similar to get(String), but throw no exception, might return null.
5090N/A // Used in X509CertImpl::getExtension(OID).
5090N/A Extension getExtension(String name) {
5090N/A return map.get(name);
5090N/A }
5090N/A
0N/A /**
0N/A * Delete the attribute value.
0N/A * @param name the extension name used in the lookup.
0N/A * @exception IOException if named extension is not found.
0N/A */
0N/A public void delete(String name) throws IOException {
0N/A Object obj = map.get(name);
0N/A if (obj == null) {
0N/A throw new IOException("No extension found with name " + name);
0N/A }
0N/A map.remove(name);
0N/A }
0N/A
903N/A public String getNameByOid(ObjectIdentifier oid) throws IOException {
903N/A for (String name: map.keySet()) {
903N/A if (map.get(name).getExtensionId().equals(oid)) {
903N/A return name;
903N/A }
903N/A }
903N/A return null;
903N/A }
903N/A
0N/A /**
0N/A * Return an enumeration of names of attributes existing within this
0N/A * attribute.
0N/A */
0N/A public Enumeration<Extension> getElements() {
5090N/A return Collections.enumeration(map.values());
0N/A }
0N/A
0N/A /**
0N/A * Return a collection view of the extensions.
0N/A * @return a collection view of the extensions in this Certificate.
0N/A */
0N/A public Collection<Extension> getAllExtensions() {
0N/A return map.values();
0N/A }
0N/A
0N/A public Map<String,Extension> getUnparseableExtensions() {
0N/A if (unparseableExtensions == null) {
0N/A return Collections.emptyMap();
0N/A } else {
0N/A return unparseableExtensions;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the name of this attribute.
0N/A */
0N/A public String getName() {
0N/A return NAME;
0N/A }
0N/A
0N/A /**
0N/A * Return true if a critical extension is found that is
0N/A * not supported, otherwise return false.
0N/A */
0N/A public boolean hasUnsupportedCriticalExtension() {
0N/A return unsupportedCritExt;
0N/A }
0N/A
0N/A /**
0N/A * Compares this CertificateExtensions for equality with the specified
0N/A * object. If the <code>other</code> object is an
0N/A * <code>instanceof</code> <code>CertificateExtensions</code>, then
0N/A * all the entries are compared with the entries from this.
0N/A *
0N/A * @param other the object to test for equality with this
0N/A * CertificateExtensions.
0N/A * @return true iff all the entries match that of the Other,
0N/A * false otherwise.
0N/A */
0N/A public boolean equals(Object other) {
0N/A if (this == other)
0N/A return true;
0N/A if (!(other instanceof CertificateExtensions))
0N/A return false;
0N/A Collection<Extension> otherC =
0N/A ((CertificateExtensions)other).getAllExtensions();
0N/A Object[] objs = otherC.toArray();
0N/A
0N/A int len = objs.length;
0N/A if (len != map.size())
0N/A return false;
0N/A
0N/A Extension otherExt, thisExt;
0N/A String key = null;
0N/A for (int i = 0; i < len; i++) {
0N/A if (objs[i] instanceof CertAttrSet)
0N/A key = ((CertAttrSet)objs[i]).getName();
0N/A otherExt = (Extension)objs[i];
0N/A if (key == null)
0N/A key = otherExt.getExtensionId().toString();
0N/A thisExt = map.get(key);
0N/A if (thisExt == null)
0N/A return false;
0N/A if (! thisExt.equals(otherExt))
0N/A return false;
0N/A }
0N/A return this.getUnparseableExtensions().equals(
0N/A ((CertificateExtensions)other).getUnparseableExtensions());
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode value for this CertificateExtensions.
0N/A *
0N/A * @return the hashcode value.
0N/A */
0N/A public int hashCode() {
0N/A return map.hashCode() + getUnparseableExtensions().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this <tt>CertificateExtensions</tt>
0N/A * object in the form of a set of entries, enclosed in braces and separated
0N/A * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space).
0N/A * <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
0N/A *
0N/A * @return a string representation of this CertificateExtensions.
0N/A */
0N/A public String toString() {
0N/A return map.toString();
0N/A }
0N/A
0N/A}
0N/A
0N/Aclass UnparseableExtension extends Extension {
0N/A private String name;
0N/A private Throwable why;
0N/A
0N/A public UnparseableExtension(Extension ext, Throwable why) {
0N/A super(ext);
0N/A
0N/A name = "";
0N/A try {
0N/A Class extClass = OIDMap.getClass(ext.getExtensionId());
0N/A if (extClass != null) {
0N/A Field field = extClass.getDeclaredField("NAME");
0N/A name = (String)(field.get(null)) + " ";
0N/A }
0N/A } catch (Exception e) {
0N/A // If we cannot find the name, just ignore it
0N/A }
0N/A
0N/A this.why = why;
0N/A }
0N/A
0N/A @Override public String toString() {
0N/A return super.toString() +
0N/A "Unparseable " + name + "extension due to\n" + why + "\n\n" +
0N/A new sun.misc.HexDumpEncoder().encodeBuffer(getExtensionValue());
0N/A }
0N/A}