src/java.base/share/classes/sun/security/x509/CertificateExtensions.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 48080 18a4438eb690
child 53865 c925e36a8e6d
permissions -rw-r--r--
8200381: Typos in javadoc - missing verb "be" and alike Reviewed-by: lancea, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, 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
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 32275
diff changeset
    36
import sun.security.util.HexDumpEncoder;
2
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
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
    60
    private Map<String,Extension> map = Collections.synchronizedMap(
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
    61
            new TreeMap<String,Extension>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean unsupportedCritExt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Map<String,Extension> unparseableExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public CertificateExtensions() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Create the object, decoding the values from the passed DER stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param in the DerInputStream to read the Extension from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @exception IOException on decoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public CertificateExtensions(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        init(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // helper routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private void init(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        DerValue[] exts = in.getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 0; i < exts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            Extension ext = new Extension(exts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            parseExtension(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
22041
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 14342
diff changeset
    92
    private static Class<?>[] PARAMS = {Boolean.class, Object.class};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Parse the encoded extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void parseExtension(Extension ext) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    97
            Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (extClass == null) {   // Unsupported extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (ext.isCritical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    unsupportedCritExt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (map.put(ext.getExtensionId().toString(), ext) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    throw new IOException("Duplicate extensions not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   108
            Constructor<?> cons = extClass.getConstructor(PARAMS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    ext.getExtensionValue()};
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   112
                    CertAttrSet<?> certExt = (CertAttrSet<?>)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   113
                            cons.newInstance(passed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    if (map.put(certExt.getName(), (Extension)certExt) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        throw new IOException("Duplicate extensions not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (InvocationTargetException invk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            Throwable e = invk.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (ext.isCritical() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // ignore errors parsing non-critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (unparseableExtensions == null) {
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   122
                    unparseableExtensions = new TreeMap<String,Extension>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                unparseableExtensions.put(ext.getExtensionId().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        new UnparseableExtension(ext, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (debug != null) {
48080
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47216
diff changeset
   127
                    debug.println("Debug info only." +
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47216
diff changeset
   128
                       " Error parsing extension: " + ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    HexDumpEncoder h = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    System.err.println(h.encodeBuffer(ext.getExtensionValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (e instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   138
                throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   143
            throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Encode the extensions in DER form to the stream, setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the context specific tag as needed in the X.509 v3 certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void encode(OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        encode(out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Encode the extensions in DER form to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param out the DerOutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param isCertReq if true then no context specific tag is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void encode(OutputStream out, boolean isCertReq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        DerOutputStream extOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Collection<Extension> allExts = map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Object[] objs = allExts.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (int i = 0; i < objs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ((CertAttrSet)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            else if (objs[i] instanceof Extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                ((Extension)objs[i]).encode(extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new CertificateException("Illegal extension object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        seq.write(DerValue.tag_Sequence, extOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        DerOutputStream tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (!isCertReq) { // certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            tmp = seq; // pkcs#10 certificateRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param name the extension name used in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param obj the object to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception IOException if the object could not be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (obj instanceof Extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            map.put(name, (Extension)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new IOException("Unknown extension type.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   216
    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
   217
        Extension obj = map.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   224
    // Similar to get(String), but throw no exception, might return null.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   225
    // Used in X509CertImpl::getExtension(OID).
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   226
    Extension getExtension(String name) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   227
        return map.get(name);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   228
    }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param name the extension name used in the lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception IOException if named extension is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Object obj = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IOException("No extension found with name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        map.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   243
    public String getNameByOid(ObjectIdentifier oid) throws IOException {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   244
        for (String name: map.keySet()) {
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 25859
diff changeset
   245
            if (map.get(name).getExtensionId().equals(oid)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   246
                return name;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   247
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   248
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   249
        return null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   250
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   251
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public Enumeration<Extension> getElements() {
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents: 5506
diff changeset
   257
        return Collections.enumeration(map.values());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Return a collection view of the extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return a collection view of the extensions in this Certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public Collection<Extension> getAllExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Map<String,Extension> getUnparseableExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (unparseableExtensions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return unparseableExtensions;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return NAME;
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
     * Return true if a critical extension is found that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * not supported, otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public boolean hasUnsupportedCriticalExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return unsupportedCritExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Compares this CertificateExtensions for equality with the specified
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   293
     * object. If the {@code other} object is an
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   294
     * {@code instanceof} {@code CertificateExtensions}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * all the entries are compared with the entries from this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param other the object to test for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return true iff all the entries match that of the Other,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (!(other instanceof CertificateExtensions))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Collection<Extension> otherC =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                ((CertificateExtensions)other).getAllExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Object[] objs = otherC.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int len = objs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (len != map.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        Extension otherExt, thisExt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (objs[i] instanceof CertAttrSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                key = ((CertAttrSet)objs[i]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            otherExt = (Extension)objs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                key = otherExt.getExtensionId().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            thisExt = map.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (thisExt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (! thisExt.equals(otherExt))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return this.getUnparseableExtensions().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ((CertificateExtensions)other).getUnparseableExtensions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns a hashcode value for this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the hashcode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return map.hashCode() + getUnparseableExtensions().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   343
     * Returns a string representation of this {@code CertificateExtensions}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * object in the form of a set of entries, enclosed in braces and separated
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   345
     * by the ASCII characters "<code>,&nbsp;</code>" (comma and space).
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   346
     * <p>Overrides to {@code toString} method of {@code Object}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return  a string representation of this CertificateExtensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return map.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
class UnparseableExtension extends Extension {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private Throwable why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public UnparseableExtension(Extension ext, Throwable why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        super(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   365
            Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (extClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                Field field = extClass.getDeclaredField("NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                name = (String)(field.get(null)) + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // If we cannot find the name, just ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        this.why = why;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    @Override public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                "Unparseable " + name + "extension due to\n" + why + "\n\n" +
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 32275
diff changeset
   380
                new HexDumpEncoder().encodeBuffer(getExtensionValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
}