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