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