jdk/src/share/classes/sun/security/x509/CertificateExtensions.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2067 6f9db5f305cd
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class defines the Extensions attribute for the Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class CertificateExtensions implements CertAttrSet<Extension> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final String IDENT = "x509.info.extensions";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final String NAME = "extensions";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final Debug debug = Debug.getInstance("x509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Hashtable<String,Extension> map = new Hashtable<String,Extension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean unsupportedCritExt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Map<String,Extension> unparseableExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public CertificateExtensions() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Create the object, decoding the values from the passed DER stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param in the DerInputStream to read the Extension from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @exception IOException on decoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public CertificateExtensions(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        init(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // helper routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private void init(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        DerValue[] exts = in.getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < exts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Extension ext = new Extension(exts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            parseExtension(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static Class[] PARAMS = {Boolean.class, Object.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // Parse the encoded extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void parseExtension(Extension ext) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Class extClass = OIDMap.getClass(ext.getExtensionId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (extClass == null) {   // Unsupported extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (ext.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    unsupportedCritExt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (map.put(ext.getExtensionId().toString(), ext) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    throw new IOException("Duplicate extensions not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            Constructor cons = ((Class<?>)extClass).getConstructor(PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    ext.getExtensionValue()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    CertAttrSet certExt = (CertAttrSet)cons.newInstance(passed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    if (map.put(certExt.getName(), (Extension)certExt) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        throw new IOException("Duplicate extensions not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } catch (InvocationTargetException invk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Throwable e = invk.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (ext.isCritical() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // ignore errors parsing non-critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (unparseableExtensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    unparseableExtensions = new HashMap<String,Extension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                unparseableExtensions.put(ext.getExtensionId().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        new UnparseableExtension(ext, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    debug.println("Error parsing extension: " + ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    HexDumpEncoder h = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    System.err.println(h.encodeBuffer(ext.getExtensionValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw (IOException)new IOException(e.toString()).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw (IOException)new IOException(e.toString()).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Encode the extensions in DER form to the stream, setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * the context specific tag as needed in the X.509 v3 certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void encode(OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        encode(out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Encode the extensions in DER form to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param isCertReq if true then no context specific tag is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void encode(OutputStream out, boolean isCertReq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        DerOutputStream extOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Collection<Extension> allExts = map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Object[] objs = allExts.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        for (int i = 0; i < objs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                ((CertAttrSet)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            else if (objs[i] instanceof Extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                ((Extension)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new CertificateException("Illegal extension object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        seq.write(DerValue.tag_Sequence, extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DerOutputStream tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (!isCertReq) { // certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            tmp = seq; // pkcs#10 certificateRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param name the extension name used in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param obj the object to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception IOException if the object could not be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (obj instanceof Extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            map.put(name, (Extension)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IOException("Unknown extension type.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public Object get(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Object obj = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return (obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Object obj = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        map.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Enumeration<Extension> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return map.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Return a collection view of the extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return a collection view of the extensions in this Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public Collection<Extension> getAllExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public Map<String,Extension> getUnparseableExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (unparseableExtensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return unparseableExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Return true if a critical extension is found that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * not supported, otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public boolean hasUnsupportedCriticalExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return unsupportedCritExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Compares this CertificateExtensions for equality with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * object. If the <code>other</code> object is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <code>instanceof</code> <code>CertificateExtensions</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * all the entries are compared with the entries from this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param other the object to test for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return true iff all the entries match that of the Other,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (!(other instanceof CertificateExtensions))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Collection<Extension> otherC =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                ((CertificateExtensions)other).getAllExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        Object[] objs = otherC.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int len = objs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (len != map.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Extension otherExt, thisExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                key = ((CertAttrSet)objs[i]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            otherExt = (Extension)objs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                key = otherExt.getExtensionId().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            thisExt = map.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (thisExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (! thisExt.equals(otherExt))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return this.getUnparseableExtensions().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                ((CertificateExtensions)other).getUnparseableExtensions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns a hashcode value for this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return the hashcode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return map.hashCode() + getUnparseableExtensions().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns a string representation of this <tt>CertificateExtensions</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * object in the form of a set of entries, enclosed in braces and separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return  a string representation of this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return map.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
class UnparseableExtension extends Extension {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private Throwable why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public UnparseableExtension(Extension ext, Throwable why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        super(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Class extClass = OIDMap.getClass(ext.getExtensionId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (extClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                Field field = extClass.getDeclaredField("NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                name = (String)(field.get(null)) + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // If we cannot find the name, just ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        this.why = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    @Override public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                "Unparseable " + name + "extension due to\n" + why + "\n\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                new sun.misc.HexDumpEncoder().encodeBuffer(getExtensionValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}