jdk/src/share/classes/sun/security/x509/CertificateExtensions.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 13040 1f31b09f711d
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    23
 * questions.
2
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 {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
            Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
2
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
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   107
            Constructor<?> cons = extClass.getConstructor(PARAMS);
2
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()};
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   111
                    CertAttrSet<?> certExt = (CertAttrSet<?>)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   112
                            cons.newInstance(passed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    if (map.put(certExt.getName(), (Extension)certExt) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        throw new IOException("Duplicate extensions not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (InvocationTargetException invk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            Throwable e = invk.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (ext.isCritical() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // ignore errors parsing non-critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (unparseableExtensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    unparseableExtensions = new HashMap<String,Extension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                unparseableExtensions.put(ext.getExtensionId().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        new UnparseableExtension(ext, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    debug.println("Error parsing extension: " + ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    HexDumpEncoder h = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    System.err.println(h.encodeBuffer(ext.getExtensionValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   136
                throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   141
            throw new IOException(e);
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Encode the extensions in DER form to the stream, setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the context specific tag as needed in the X.509 v3 certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void encode(OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        encode(out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Encode the extensions in DER form to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param isCertReq if true then no context specific tag is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void encode(OutputStream out, boolean isCertReq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        DerOutputStream extOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Collection<Extension> allExts = map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Object[] objs = allExts.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int i = 0; i < objs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                ((CertAttrSet)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            else if (objs[i] instanceof Extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ((Extension)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new CertificateException("Illegal extension object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        seq.write(DerValue.tag_Sequence, extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        DerOutputStream tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!isCertReq) { // certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            tmp = seq; // pkcs#10 certificateRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param name the extension name used in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param obj the object to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @exception IOException if the object could not be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (obj instanceof Extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            map.put(name, (Extension)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new IOException("Unknown extension type.");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   214
    public Extension get(String name) throws IOException {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   215
        Extension obj = map.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return (obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        Object obj = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        map.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   235
    public String getNameByOid(ObjectIdentifier oid) throws IOException {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   236
        for (String name: map.keySet()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   237
            if (map.get(name).getExtensionId().equals((Object)oid)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   238
                return name;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   239
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   240
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   241
        return null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   242
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   243
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public Enumeration<Extension> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return map.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Return a collection view of the extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return a collection view of the extensions in this Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public Collection<Extension> getAllExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public Map<String,Extension> getUnparseableExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (unparseableExtensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return unparseableExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Return true if a critical extension is found that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * not supported, otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public boolean hasUnsupportedCriticalExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return unsupportedCritExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Compares this CertificateExtensions for equality with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * object. If the <code>other</code> object is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <code>instanceof</code> <code>CertificateExtensions</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * all the entries are compared with the entries from this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param other the object to test for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return true iff all the entries match that of the Other,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (!(other instanceof CertificateExtensions))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Collection<Extension> otherC =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                ((CertificateExtensions)other).getAllExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Object[] objs = otherC.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int len = objs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (len != map.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Extension otherExt, thisExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                key = ((CertAttrSet)objs[i]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            otherExt = (Extension)objs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                key = otherExt.getExtensionId().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            thisExt = map.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (thisExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (! thisExt.equals(otherExt))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return this.getUnparseableExtensions().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                ((CertificateExtensions)other).getUnparseableExtensions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Returns a hashcode value for this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the hashcode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return map.hashCode() + getUnparseableExtensions().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns a string representation of this <tt>CertificateExtensions</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * object in the form of a set of entries, enclosed in braces and separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return  a string representation of this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return map.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
class UnparseableExtension extends Extension {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private Throwable why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public UnparseableExtension(Extension ext, Throwable why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        super(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   357
            Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (extClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                Field field = extClass.getDeclaredField("NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                name = (String)(field.get(null)) + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // If we cannot find the name, just ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        this.why = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    @Override public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                "Unparseable " + name + "extension due to\n" + why + "\n\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                new sun.misc.HexDumpEncoder().encodeBuffer(getExtensionValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
}