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