jdk/src/java.base/share/classes/java/security/Security.java
author valeriep
Fri, 26 Jun 2015 21:34:34 +0000
changeset 31270 e6470b24700d
parent 30033 b9c86c17164a
child 37880 60ec48925dc6
permissions -rw-r--r--
7191662: JCE providers should be located via ServiceLoader Summary: Enhanced to use ServiceLoader and switched provider to Provider.Service model. Reviewed-by: mullan, alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30033
diff changeset
     2
 * Copyright (c) 1996, 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: 2589
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: 2589
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: 2589
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.PropertyExpander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>This class centralizes all security properties and common security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * methods. One of its primary uses is to manage providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 10882
diff changeset
    42
 * <p>The default values of security properties are read from an
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 10882
diff changeset
    43
 * implementation-specific location, which is typically the properties file
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30033
diff changeset
    44
 * {@code conf/security/java.security} in the Java installation directory.
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 10882
diff changeset
    45
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class Security {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* Are we debugging? -- for developers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final Debug sdebug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                        Debug.getInstance("properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* The java.security properties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // An element in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static class ProviderProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // doPrivileged here because there are multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // things in initialize that might require privs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // (the FileInputStream call and the File.exists call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // the securityPropFile call, etc)
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
    69
        AccessController.doPrivileged(new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        boolean loadedProps = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        boolean overrideAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // first load the system properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // to determine the value of security.overridePropertiesFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        File propFile = securityPropFile("java.security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (propFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                FileInputStream fis = new FileInputStream(propFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                is = new BufferedInputStream(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                props.load(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                loadedProps = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    sdebug.println("reading security properties file: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                propFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    sdebug.println("unable to load security properties from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                propFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            sdebug.println("unable to close input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if ("true".equalsIgnoreCase(props.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                ("security.overridePropertiesFile"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            String extraPropFile = System.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                        ("java.security.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (extraPropFile != null && extraPropFile.startsWith("=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                overrideAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                extraPropFile = extraPropFile.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (overrideAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    sdebug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        ("overriding other security properties files!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // now load the user-specified file so its values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // will win if they conflict with the earlier values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (extraPropFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                BufferedInputStream bis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    URL propURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    extraPropFile = PropertyExpander.expand(extraPropFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    propFile = new File(extraPropFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    if (propFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        propURL = new URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                ("file:" + propFile.getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        propURL = new URL(extraPropFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    bis = new BufferedInputStream(propURL.openStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    props.load(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    loadedProps = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        sdebug.println("reading security properties file: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                        propURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        if (overrideAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            sdebug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                ("overriding other security properties files!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        sdebug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                ("unable to load security properties from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                extraPropFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (bis != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            bis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                sdebug.println("unable to close input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (!loadedProps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            initializeStatic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (sdebug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                sdebug.println("unable to load security properties " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        "-- using defaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Initialize to default values, if <java.home>/lib/java.security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static void initializeStatic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        props.put("security.provider.1", "sun.security.provider.Sun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        props.put("security.provider.2", "sun.security.rsa.SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        props.put("security.provider.3", "com.sun.net.ssl.internal.ssl.Provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        props.put("security.provider.4", "com.sun.crypto.provider.SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        props.put("security.provider.5", "sun.security.jgss.SunProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        props.put("security.provider.6", "com.sun.security.sasl.Provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Don't let anyone instantiate this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private Security() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private static File securityPropFile(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // maybe check for a system property which will specify where to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // look. Someday.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        String sep = File.separator;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   215
        return new File(System.getProperty("java.home") + sep + "conf" + sep +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        "security" + sep + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Looks up providers, and returns the property (and its associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * provider) mapping the key, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The order in which the providers are looked up is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * provider-preference order, as specificed in the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static ProviderProperty getProviderProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        ProviderProperty entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        List<Provider> providers = Providers.getProviderList().providers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            String matchKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Provider prov = providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            String prop = prov.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // Is there a match if we do a case-insensitive property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // comparison? Let's try ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                for (Enumeration<Object> e = prov.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                e.hasMoreElements() && prop == null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    matchKey = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    if (key.equalsIgnoreCase(matchKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        prop = prov.getProperty(matchKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ProviderProperty newEntry = new ProviderProperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                newEntry.className = prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                newEntry.provider = prov;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return entry;
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
     * Returns the property (if any) mapping the key for the given provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static String getProviderProperty(String key, Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        String prop = provider.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // Is there a match if we do a case-insensitive property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // comparison? Let's try ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (Enumeration<Object> e = provider.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                e.hasMoreElements() && prop == null; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                String matchKey = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (key.equalsIgnoreCase(matchKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    prop = provider.getProperty(matchKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    break;
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 prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Gets a specified property for an algorithm. The algorithm name
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   282
     * should be a standard name. See the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   283
     * "{@docRoot}/../technotes/guides/security/StandardNames.html">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   284
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * for information about standard algorithm names.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   286
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * One possible use is by specialized algorithm parsers, which may map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * classes to algorithms which they understand (much like Key parsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * do).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param algName the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param propName the name of the property to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return the value of the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @deprecated This method used to return the value of a proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * property in the master file of the "SUN" Cryptographic Service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Provider in order to determine how to parse algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * parameters. Use the new provider-based and algorithm-independent
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   301
     * {@code AlgorithmParameters} and {@code KeyFactory} engine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * classes (introduced in the J2SE version 1.2 platform) instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static String getAlgorithmProperty(String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                              String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        ProviderProperty entry = getProviderProperty("Alg." + propName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                                     + "." + algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return entry.className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Adds a new provider, at a specified position. The position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the preference order in which providers are searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * requested algorithms.  The position is 1-based, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * 1 is most preferred, followed by 2, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>If the given provider is installed at the requested position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the provider that used to be at that position, and all providers
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   324
     * with a position greater than {@code position}, are shifted up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * one position (towards the end of the list of installed providers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>A provider cannot be added if it is already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
19193
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   329
     * <p>If there is a security manager, the
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   330
     * {@link java.lang.SecurityManager#checkSecurityAccess} method is called
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   331
     * with the {@code "insertProvider"} permission target name to see if
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   332
     * it's ok to add a new provider. If this permission check is denied,
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   333
     * {@code checkSecurityAccess} is called again with the
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   334
     * {@code "insertProvider."+provider.getName()} permission target name. If
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   335
     * both checks are denied, a {@code SecurityException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param provider the provider to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param position the preference position that the caller would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * like for this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return the actual preference position in which the provider was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * added, or -1 if the provider was not added because it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws  NullPointerException if provider is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws  SecurityException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   348
     *          if a security manager exists and its {@link
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   349
     *          java.lang.SecurityManager#checkSecurityAccess} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *          denies access to add a new provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public static synchronized int insertProviderAt(Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        String providerName = provider.getName();
19193
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   359
        checkInsertProvider(providerName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        ProviderList list = Providers.getFullProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        ProviderList newList = ProviderList.insertAt(list, provider, position - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (list == newList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        Providers.setProviderList(newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return newList.getIndex(providerName) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Adds a provider to the next position available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
19193
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   372
     * <p>If there is a security manager, the
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   373
     * {@link java.lang.SecurityManager#checkSecurityAccess} method is called
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   374
     * with the {@code "insertProvider"} permission target name to see if
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   375
     * it's ok to add a new provider. If this permission check is denied,
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   376
     * {@code checkSecurityAccess} is called again with the
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   377
     * {@code "insertProvider."+provider.getName()} permission target name. If
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   378
     * both checks are denied, a {@code SecurityException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param provider the provider to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @return the preference position in which the provider was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * added, or -1 if the provider was not added because it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws  NullPointerException if provider is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @throws  SecurityException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   388
     *          if a security manager exists and its {@link
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   389
     *          java.lang.SecurityManager#checkSecurityAccess} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *          denies access to add a new provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public static int addProvider(Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * We can't assign a position here because the statically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * registered providers may not have been installed yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * insertProviderAt() will fix that value after it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * loaded the static providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return insertProviderAt(provider, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Removes the provider with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>When the specified provider is removed, all providers located
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * at a position greater than where the specified provider was are shifted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * down one position (towards the head of the list of installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * providers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <p>This method returns silently if the provider is not installed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>First, if there is a security manager, its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   418
     * {@code checkSecurityAccess}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   419
     * method is called with the string {@code "removeProvider."+name}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * to see if it's ok to remove the provider.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   421
     * If the default implementation of {@code checkSecurityAccess}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * is used (i.e., that method is not overriden), then this will result in
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   423
     * a call to the security manager's {@code checkPermission} method
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   424
     * with a {@code SecurityPermission("removeProvider."+name)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param name the name of the provider to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @throws  SecurityException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   430
     *          if a security manager exists and its {@link
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   431
     *          java.lang.SecurityManager#checkSecurityAccess} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *          denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *          access to remove the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see #addProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public static synchronized void removeProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        check("removeProvider." + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        ProviderList list = Providers.getFullProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        ProviderList newList = ProviderList.remove(list, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        Providers.setProviderList(newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns an array containing all the installed providers. The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * the providers in the array is their preference order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return an array of all the installed providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public static Provider[] getProviders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return Providers.getFullProviderList().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns the provider installed with the specified name, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * any. Returns null if no provider with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * installed or if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param name the name of the provider to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return the provider of the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @see #addProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public static Provider getProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return Providers.getProviderList().getProvider(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns an array containing all installed providers that satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * specified selection criterion, or null if no such providers have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * installed. The returned providers are ordered
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   475
     * according to their
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   476
     * {@linkplain #insertProviderAt(java.security.Provider, int) preference order}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <p> A cryptographic service is always associated with a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * algorithm or type. For example, a digital signature service is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * always associated with a particular algorithm (e.g., DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * and a CertificateFactory service is always associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * a particular certificate type (e.g., X.509).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p>The selection criterion must be specified in one of the following two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <ul>
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   487
     * <li> <i>{@literal <crypto_service>.<algorithm_or_type>}</i>
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   488
     * <p> The cryptographic service name must not contain any dots.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p> A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * provider satisfies the specified selection criterion iff the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * specified algorithm or type for the specified cryptographic service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * <p> For example, "CertificateFactory.X.509"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * would be satisfied by any provider that supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * a CertificateFactory implementation for X.509 certificates.
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   496
     * <li> <i>{@literal <crypto_service>.<algorithm_or_type>
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   497
     * <attribute_name>:<attribute_value>}</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <p> The cryptographic service name must not contain any dots. There
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19193
diff changeset
   499
     * must be one or more space characters between the
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   500
     * <i>{@literal <algorithm_or_type>}</i> and the
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   501
     * <i>{@literal <attribute_name>}</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *  <p> A provider satisfies this selection criterion iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * provider implements the specified algorithm or type for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * cryptographic service and its implementation meets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * constraint expressed by the specified attribute name/value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <p> For example, "Signature.SHA1withDSA KeySize:1024" would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * satisfied by any provider that implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * the SHA1withDSA signature algorithm with a keysize of 1024 (or larger).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   512
     * <p> See the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   513
     * "{@docRoot}/../technotes/guides/security/StandardNames.html">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   514
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * for information about standard cryptographic service names, standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * algorithm names and standard attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param filter the criterion for selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * providers. The filter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return all the installed providers that satisfy the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * criterion, or null if no such providers have been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         if the filter is not in the required format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws NullPointerException if filter is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @see #getProviders(java.util.Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public static Provider[] getProviders(String filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int index = filter.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            key = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            key = filter.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            value = filter.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   544
        Hashtable<String, String> hashtableFilter = new Hashtable<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        hashtableFilter.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return (getProviders(hashtableFilter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Returns an array containing all installed providers that satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * specified* selection criteria, or null if no such providers have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * installed. The returned providers are ordered
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   554
     * according to their
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   555
     * {@linkplain #insertProviderAt(java.security.Provider, int)
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   556
     * preference order}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>The selection criteria are represented by a map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Each map entry represents a selection criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * A provider is selected iff it satisfies all selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * criteria. The key for any entry in such a map must be in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * following two formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <ul>
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   564
     * <li> <i>{@literal <crypto_service>.<algorithm_or_type>}</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p> The cryptographic service name must not contain any dots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <p> The value associated with the key must be an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <p> A provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * satisfies this selection criterion iff the provider implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * specified algorithm or type for the specified cryptographic service.
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   570
     * <li>  <i>{@literal <crypto_service>}.
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   571
     * {@literal <algorithm_or_type> <attribute_name>}</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <p> The cryptographic service name must not contain any dots. There
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19193
diff changeset
   573
     * must be one or more space characters between the
18592
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   574
     * <i>{@literal <algorithm_or_type>}</i>
80cdfecea074 8019539: Fix doclint errors in java.security and its subpackages
juh
parents: 18579
diff changeset
   575
     * and the <i>{@literal <attribute_name>}</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p> The value associated with the key must be a non-empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * A provider satisfies this selection criterion iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * provider implements the specified algorithm or type for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * cryptographic service and its implementation meets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * constraint expressed by the specified attribute name/value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   583
     * <p> See the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   584
     * "../../../technotes/guides/security/StandardNames.html">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5776
diff changeset
   585
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * for information about standard cryptographic service names, standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * algorithm names and standard attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param filter the criteria for selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * providers. The filter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return all the installed providers that satisfy the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * criteria, or null if no such providers have been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *         if the filter is not in the required format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @throws NullPointerException if filter is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @see #getProviders(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public static Provider[] getProviders(Map<String,String> filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        // Get all installed providers first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // Then only return those providers who satisfy the selection criteria.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        Provider[] allProviders = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Set<String> keySet = filter.keySet();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   607
        LinkedHashSet<Provider> candidates = new LinkedHashSet<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // Returns all installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        // if the selection criteria is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if ((keySet == null) || (allProviders == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return allProviders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        boolean firstSearch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // For each selection criterion, remove providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // which don't satisfy the criterion from the candidate set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        for (Iterator<String> ite = keySet.iterator(); ite.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            String key = ite.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            String value = filter.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            LinkedHashSet<Provider> newCandidates = getAllQualifyingCandidates(key, value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                                               allProviders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (firstSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                candidates = newCandidates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                firstSearch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if ((newCandidates != null) && !newCandidates.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // For each provider in the candidates set, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                // isn't in the newCandidate set, we should remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                // it from the candidate set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                for (Iterator<Provider> cansIte = candidates.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                     cansIte.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    Provider prov = cansIte.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    if (!newCandidates.contains(prov)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        cansIte.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                candidates = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if ((candidates == null) || (candidates.isEmpty()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        Object[] candidatesArray = candidates.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        Provider[] result = new Provider[candidatesArray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        for (int i = 0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            result[i] = (Provider)candidatesArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    // Map containing cached Spi Class objects of the specified type
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   661
    private static final Map<String, Class<?>> spiMap =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   662
            new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Return the Class object for the given engine type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * (e.g. "MessageDigest"). Works for Spis in the java.security package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   669
    private static Class<?> getSpiClass(String type) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   670
        Class<?> clazz = spiMap.get(type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (clazz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            clazz = Class.forName("java.security." + type + "Spi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            spiMap.put(type, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        } catch (ClassNotFoundException e) {
5776
5f48622225ab 6935997: Please add a nested throwable constructor to AssertionError
darcy
parents: 5506
diff changeset
   679
            throw new AssertionError("Spi class not found", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * of that implementation.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   688
     * The {@code provider} argument can be null, in which case all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * configured providers will be searched in order of preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    static Object[] getImpl(String algorithm, String type, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                (type, getSpiClass(type), algorithm).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                (type, getSpiClass(type), algorithm, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    static Object[] getImpl(String algorithm, String type, String provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            Object params) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            NoSuchProviderException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                (type, getSpiClass(type), algorithm, params).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                (type, getSpiClass(type), algorithm, params, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * of that implementation.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   719
     * The {@code provider} argument cannot be null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    static Object[] getImpl(String algorithm, String type, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            (type, getSpiClass(type), algorithm, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    static Object[] getImpl(String algorithm, String type, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            Object params) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            (type, getSpiClass(type), algorithm, params, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Gets a security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <p>First, if there is a security manager, its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   738
     * {@code checkPermission}  method is called with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   739
     * {@code java.security.SecurityPermission("getProperty."+key)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * permission to see if it's ok to retrieve the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * security property value..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param key the key of the property being retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @return the value of the security property corresponding to key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @throws  SecurityException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   748
     *          if a security manager exists and its {@link
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   749
     *          java.lang.SecurityManager#checkPermission} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *          denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *          access to retrieve the specified security property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @throws  NullPointerException is key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @see #setProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public static String getProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            sm.checkPermission(new SecurityPermission("getProperty."+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                                      key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        String name = props.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            name = name.trim(); // could be a class name with trailing ws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Sets a security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>First, if there is a security manager, its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   773
     * {@code checkPermission} method is called with a
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   774
     * {@code java.security.SecurityPermission("setProperty."+key)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * permission to see if it's ok to set the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param key the name of the property to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param datum the value of the property to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws  SecurityException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   783
     *          if a security manager exists and its {@link
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14775
diff changeset
   784
     *          java.lang.SecurityManager#checkPermission} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *          denies access to set the specified security property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @throws  NullPointerException if key or datum is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @see #getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    public static void setProperty(String key, String datum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        check("setProperty."+key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        props.put(key, datum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        invalidateSMCache(key);  /* See below. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * Implementation detail:  If the property we just set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * setProperty() was either "package.access" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * "package.definition", we need to signal to the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * class that the value has just changed, and that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * invalidate it's local cache values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Rather than create a new API entry for this function,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * we use reflection to set a private variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private static void invalidateSMCache(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        final boolean pa = key.equals("package.access");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        final boolean pd = key.equals("package.definition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        if (pa || pd) {
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
   813
            AccessController.doPrivileged(new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        /* Get the class via the bootstrap class loader. */
10882
7ddadf2b8b4c 7104650: rawtype warnings in several net, nio and security source files
chegar
parents: 10369
diff changeset
   817
                        Class<?> cl = Class.forName(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                            "java.lang.SecurityManager", false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        Field f = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        boolean accessible = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        if (pa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                            f = cl.getDeclaredField("packageAccessValid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                            accessible = f.isAccessible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                            f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                            f = cl.getDeclaredField("packageDefinitionValid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                            accessible = f.isAccessible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                            f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        f.setBoolean(f, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        f.setAccessible(accessible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    catch (Exception e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        /* If we couldn't get the class, it hasn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                         * been loaded yet.  If there is no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                         * field, we shouldn't try to set it.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                         * shouldn't be a security execption, as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                         * are loaded by boot class loader, and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                         * are inside a doPrivileged() here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                         * NOOP: don't do anything...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                }  /* run */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            });  /* PrivilegedAction */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }  /* if */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private static void check(String directive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            security.checkSecurityAccess(directive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
19193
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   858
    private static void checkInsertProvider(String name) {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   859
        SecurityManager security = System.getSecurityManager();
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   860
        if (security != null) {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   861
            try {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   862
                security.checkSecurityAccess("insertProvider");
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   863
            } catch (SecurityException se1) {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   864
                try {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   865
                    security.checkSecurityAccess("insertProvider." + name);
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   866
                } catch (SecurityException se2) {
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   867
                    // throw first exception, but add second to suppressed
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   868
                    se1.addSuppressed(se2);
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   869
                    throw se1;
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   870
                }
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   871
            }
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   872
        }
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   873
    }
103262cae675 8001319: Add SecurityPermission "insertProvider" target name
mullan
parents: 18592
diff changeset
   874
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    * Returns all providers who satisfy the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    * criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    private static LinkedHashSet<Provider> getAllQualifyingCandidates(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                                                String filterKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                                String filterValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                                Provider[] allProviders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        String[] filterComponents = getFilterComponents(filterKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                                        filterValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        // The first component is the service name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // The second is the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // If the third isn't null, that is the attrinute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        String serviceName = filterComponents[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        String algName = filterComponents[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        String attrName = filterComponents[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return getProvidersNotUsingCache(serviceName, algName, attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                         filterValue, allProviders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    private static LinkedHashSet<Provider> getProvidersNotUsingCache(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                                                String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                                                String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                                String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                                                String filterValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                                Provider[] allProviders) {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   903
        LinkedHashSet<Provider> candidates = new LinkedHashSet<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        for (int i = 0; i < allProviders.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if (isCriterionSatisfied(allProviders[i], serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                     algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                     attrName, filterValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                candidates.add(allProviders[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        return candidates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Returns true if the given provider satisfies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * the selection criterion key:value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    private static boolean isCriterionSatisfied(Provider prov,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                                String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                                String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                                String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                                String filterValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        String key = serviceName + '.' + algName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (attrName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            key += ' ' + attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // Check whether the provider has a property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // whose key is the same as the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        String propValue = getProviderProperty(key, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (propValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            // Check whether we have an alias instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // of a standard name in the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            String standardName = getProviderProperty("Alg.Alias." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                                      serviceName + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                                      algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                                      prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            if (standardName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                key = serviceName + "." + standardName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if (attrName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    key += ' ' + attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                propValue = getProviderProperty(key, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (propValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                // The provider doesn't have the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                // key in its property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // If the key is in the format of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        // <crypto_service>.<algorithm_or_type>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // there is no need to check the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        if (attrName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // If we get here, the key must be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // format of <crypto_service>.<algorithm_or_provider> <attribute_name>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (isStandardAttr(attrName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            return isConstraintSatisfied(attrName, filterValue, propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return filterValue.equalsIgnoreCase(propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * Returns true if the attribute is a standard attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private static boolean isStandardAttr(String attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // For now, we just have two standard attributes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // KeySize and ImplementedIn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if (attribute.equalsIgnoreCase("KeySize"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (attribute.equalsIgnoreCase("ImplementedIn"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * Returns true if the requested attribute value is supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private static boolean isConstraintSatisfied(String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                                                 String value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                                                 String prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // For KeySize, prop is the max key size the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        // provider supports for a specific <crypto_service>.<algorithm>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if (attribute.equalsIgnoreCase("KeySize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            int requestedSize = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            int maxSize = Integer.parseInt(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            if (requestedSize <= maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        // For Type, prop is the type of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // for a specific <crypto service>.<algorithm>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (attribute.equalsIgnoreCase("ImplementedIn")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            return value.equalsIgnoreCase(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    static String[] getFilterComponents(String filterKey, String filterValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        int algIndex = filterKey.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (algIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // There must be a dot in the filter, and the dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // shouldn't be at the beginning of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        String serviceName = filterKey.substring(0, algIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        String algName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        String attrName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (filterValue.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            // The filterValue is an empty string. So the filterKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            // should be in the format of <crypto_service>.<algorithm_or_type>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            algName = filterKey.substring(algIndex + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if (algName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                // There must be a algorithm or type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            // The filterValue is a non-empty string. So the filterKey must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            // in the format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            // <crypto_service>.<algorithm_or_type> <attribute_name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            int attrIndex = filterKey.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            if (attrIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                // There is no attribute name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                attrName = filterKey.substring(attrIndex + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                if (attrName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    // There is no attribute name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            // There must be an algorithm name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if ((attrIndex < algIndex) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                (algIndex == attrIndex - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                algName = filterKey.substring(algIndex + 1, attrIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        String[] result = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        result[0] = serviceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        result[1] = algName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        result[2] = attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Returns a Set of Strings containing the names of all available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * algorithms or types for the specified Java cryptographic service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * (e.g., Signature, MessageDigest, Cipher, Mac, KeyStore). Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * an empty Set if there is no provider that supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * specified service or if serviceName is null. For a complete list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * of Java cryptographic services, please see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Cryptography Architecture API Specification &amp; Reference</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * Note: the returned set is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @param serviceName the name of the Java cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * service (e.g., Signature, MessageDigest, Cipher, Mac, KeyStore).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Note: this parameter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @return a Set of Strings containing the names of all available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * algorithms or types for the specified Java cryptographic service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * or an empty set if no provider supports the specified service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    public static Set<String> getAlgorithms(String serviceName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if ((serviceName == null) || (serviceName.length() == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            (serviceName.endsWith("."))) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
  1097
            return Collections.emptySet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
  1100
        HashSet<String> result = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        Provider[] providers = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            // Check the keys for each provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            for (Enumeration<Object> e = providers[i].keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                                                e.hasMoreElements(); ) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1107
                String currentKey =
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1108
                        ((String)e.nextElement()).toUpperCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1109
                if (currentKey.startsWith(
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1110
                        serviceName.toUpperCase(Locale.ENGLISH))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    // We should skip the currentKey if it contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    // whitespace. The reason is: such an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    // provider property contains attributes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    // implementation of an algorithm. We are only interested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    // in entries which lead to the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    // classes.
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 21278
diff changeset
  1117
                    if (currentKey.indexOf(' ') < 0) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1118
                        result.add(currentKey.substring(
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1119
                                                serviceName.length() + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        return Collections.unmodifiableSet(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
}