jdk/src/share/classes/java/security/Security.java
author smarks
Mon, 10 Jan 2011 17:06:10 -0800
changeset 7973 dffe8439eb20
parent 7970 af1579474d16
child 8153 9c9fb8189bd0
permissions -rw-r--r--
7005608: diamond conversion of JCA and crypto providers Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5776
diff changeset
     2
 * Copyright (c) 1996, 2010, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * should be a standard name. See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * One possible use is by specialized algorithm parsers, which may map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * classes to algorithms which they understand (much like Key parsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * do).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param algName the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param propName the name of the property to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the value of the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @deprecated This method used to return the value of a proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * property in the master file of the "SUN" Cryptographic Service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Provider in order to determine how to parse algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * parameters. Use the new provider-based and algorithm-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>AlgorithmParameters</code> and <code>KeyFactory</code> engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * classes (introduced in the J2SE version 1.2 platform) instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static String getAlgorithmProperty(String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                              String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        ProviderProperty entry = getProviderProperty("Alg." + propName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                                     + "." + algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return entry.className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
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
     * Adds a new provider, at a specified position. The position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * the preference order in which providers are searched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * requested algorithms.  The position is 1-based, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * 1 is most preferred, followed by 2, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>If the given provider is installed at the requested position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the provider that used to be at that position, and all providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * with a position greater than <code>position</code>, are shifted up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * one position (towards the end of the list of installed providers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p>A provider cannot be added if it is already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <code>"insertProvider."+provider.getName()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * to see if it's ok to add a new provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * is used (i.e., that method is not overriden), then this will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * a call to the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>SecurityPermission("insertProvider."+provider.getName())</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param provider the provider to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param position the preference position that the caller would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * like for this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the actual preference position in which the provider was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * added, or -1 if the provider was not added because it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws  NullPointerException if provider is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          denies access to add a new provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public static synchronized int insertProviderAt(Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        String providerName = provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        check("insertProvider." + providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        ProviderList list = Providers.getFullProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        ProviderList newList = ProviderList.insertAt(list, provider, position - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (list == newList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Providers.setProviderList(newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return newList.getIndex(providerName) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Adds a provider to the next position available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * method is called with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>"insertProvider."+provider.getName()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * to see if it's ok to add a new provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * is used (i.e., that method is not overriden), then this will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * a call to the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <code>SecurityPermission("insertProvider."+provider.getName())</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param provider the provider to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return the preference position in which the provider was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * added, or -1 if the provider was not added because it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * already installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @throws  NullPointerException if provider is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *          denies access to add a new provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public static int addProvider(Provider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * We can't assign a position here because the statically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * registered providers may not have been installed yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * insertProviderAt() will fix that value after it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * loaded the static providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return insertProviderAt(provider, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Removes the provider with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <p>When the specified provider is removed, all providers located
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * at a position greater than where the specified provider was are shifted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * down one position (towards the head of the list of installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * providers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>This method returns silently if the provider is not installed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * method is called with the string <code>"removeProvider."+name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * to see if it's ok to remove the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * If the default implementation of <code>checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * is used (i.e., that method is not overriden), then this will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * a call to the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * with a <code>SecurityPermission("removeProvider."+name)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param name the name of the provider to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *          java.lang.SecurityManager#checkSecurityAccess}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *          access to remove the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @see #getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @see #addProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public static synchronized void removeProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        check("removeProvider." + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        ProviderList list = Providers.getFullProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        ProviderList newList = ProviderList.remove(list, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        Providers.setProviderList(newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Returns an array containing all the installed providers. The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * the providers in the array is their preference order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return an array of all the installed providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public static Provider[] getProviders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return Providers.getFullProviderList().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Returns the provider installed with the specified name, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * any. Returns null if no provider with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * installed or if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param name the name of the provider to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return the provider of the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see #removeProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @see #addProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public static Provider getProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return Providers.getProviderList().getProvider(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Returns an array containing all installed providers that satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * specified selection criterion, or null if no such providers have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * installed. The returned providers are ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * according to their <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * "#insertProviderAt(java.security.Provider, int)">preference order</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <p> A cryptographic service is always associated with a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * algorithm or type. For example, a digital signature service is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * always associated with a particular algorithm (e.g., DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * and a CertificateFactory service is always associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * a particular certificate type (e.g., X.509).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>The selection criterion must be specified in one of the following two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <li> <i>&lt;crypto_service>.&lt;algorithm_or_type></i> <p> The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * cryptographic service name must not contain any dots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <p> A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * provider satisfies the specified selection criterion iff the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * specified algorithm or type for the specified cryptographic service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <p> For example, "CertificateFactory.X.509"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * would be satisfied by any provider that supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * a CertificateFactory implementation for X.509 certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <li> <i>&lt;crypto_service>.&lt;algorithm_or_type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * &lt;attribute_name>:&lt attribute_value></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p> The cryptographic service name must not contain any dots. There
2589
af4853bc7e87 6827153: Miscellaneous typos in javadoc
martin
parents: 2
diff changeset
   504
     * must be one or more space charaters between the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <i>&lt;algorithm_or_type></i> and the <i>&lt;attribute_name></i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *  <p> A provider satisfies this selection criterion iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * provider implements the specified algorithm or type for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * cryptographic service and its implementation meets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * constraint expressed by the specified attribute name/value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p> For example, "Signature.SHA1withDSA KeySize:1024" would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * satisfied by any provider that implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * the SHA1withDSA signature algorithm with a keysize of 1024 (or larger).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <p> See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * for information about standard cryptographic service names, standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * algorithm names and standard attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param filter the criterion for selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * providers. The filter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return all the installed providers that satisfy the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * criterion, or null if no such providers have been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *         if the filter is not in the required format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws NullPointerException if filter is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @see #getProviders(java.util.Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public static Provider[] getProviders(String filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int index = filter.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            key = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            key = filter.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            value = filter.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   548
        Hashtable<String, String> hashtableFilter = new Hashtable<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        hashtableFilter.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return (getProviders(hashtableFilter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns an array containing all installed providers that satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * specified* selection criteria, or null if no such providers have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * installed. The returned providers are ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * according to their <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * "#insertProviderAt(java.security.Provider, int)">preference order</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * <p>The selection criteria are represented by a map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Each map entry represents a selection criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * A provider is selected iff it satisfies all selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * criteria. The key for any entry in such a map must be in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * following two formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <li> <i>&lt;crypto_service>.&lt;algorithm_or_type></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <p> The cryptographic service name must not contain any dots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <p> The value associated with the key must be an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p> A provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * satisfies this selection criterion iff the provider implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * specified algorithm or type for the specified cryptographic service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <li>  <i>&lt;crypto_service>.&lt;algorithm_or_type> &lt;attribute_name></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p> The cryptographic service name must not contain any dots. There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * must be one or more space charaters between the <i>&lt;algorithm_or_type></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * and the <i>&lt;attribute_name></i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * <p> The value associated with the key must be a non-empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * A provider satisfies this selection criterion iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * provider implements the specified algorithm or type for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * cryptographic service and its implementation meets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * constraint expressed by the specified attribute name/value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p> See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * for information about standard cryptographic service names, standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * algorithm names and standard attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param filter the criteria for selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * providers. The filter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return all the installed providers that satisfy the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * criteria, or null if no such providers have been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *         if the filter is not in the required format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @throws NullPointerException if filter is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see #getProviders(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public static Provider[] getProviders(Map<String,String> filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // Get all installed providers first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // Then only return those providers who satisfy the selection criteria.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Provider[] allProviders = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        Set<String> keySet = filter.keySet();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   608
        LinkedHashSet<Provider> candidates = new LinkedHashSet<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        // Returns all installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        // if the selection criteria is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if ((keySet == null) || (allProviders == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return allProviders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        boolean firstSearch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // For each selection criterion, remove providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // which don't satisfy the criterion from the candidate set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        for (Iterator<String> ite = keySet.iterator(); ite.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            String key = ite.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            String value = filter.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            LinkedHashSet<Provider> newCandidates = getAllQualifyingCandidates(key, value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                                               allProviders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (firstSearch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                candidates = newCandidates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                firstSearch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if ((newCandidates != null) && !newCandidates.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                // For each provider in the candidates set, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                // isn't in the newCandidate set, we should remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                // it from the candidate set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                for (Iterator<Provider> cansIte = candidates.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                     cansIte.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    Provider prov = cansIte.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    if (!newCandidates.contains(prov)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        cansIte.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                candidates = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if ((candidates == null) || (candidates.isEmpty()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        Object[] candidatesArray = candidates.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        Provider[] result = new Provider[candidatesArray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        for (int i = 0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            result[i] = (Provider)candidatesArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    // 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
   662
    private static final Map<String, Class> spiMap = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Return the Class object for the given engine type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * (e.g. "MessageDigest"). Works for Spis in the java.security package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    private static Class getSpiClass(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        Class clazz = spiMap.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (clazz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            clazz = Class.forName("java.security." + type + "Spi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            spiMap.put(type, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        } catch (ClassNotFoundException e) {
5776
5f48622225ab 6935997: Please add a nested throwable constructor to AssertionError
darcy
parents: 5506
diff changeset
   679
            throw new AssertionError("Spi class not found", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * of that implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * The <code>provider</code> argument can be null, in which case all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * configured providers will be searched in order of preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    static Object[] getImpl(String algorithm, String type, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                (type, getSpiClass(type), algorithm).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                (type, getSpiClass(type), algorithm, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    static Object[] getImpl(String algorithm, String type, String provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            Object params) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            NoSuchProviderException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                (type, getSpiClass(type), algorithm, params).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                (type, getSpiClass(type), algorithm, params, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * of that implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * The <code>provider</code> argument cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    static Object[] getImpl(String algorithm, String type, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            (type, getSpiClass(type), algorithm, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    static Object[] getImpl(String algorithm, String type, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            Object params) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            (type, getSpiClass(type), algorithm, params, provider).toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Gets a security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <code>checkPermission</code>  method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <code>java.security.SecurityPermission("getProperty."+key)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * permission to see if it's ok to retrieve the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * security property value..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param key the key of the property being retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @return the value of the security property corresponding to key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *          java.lang.SecurityManager#checkPermission}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *          denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *          access to retrieve the specified security property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @throws  NullPointerException is key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @see #setProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public static String getProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            sm.checkPermission(new SecurityPermission("getProperty."+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                                      key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        String name = props.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            name = name.trim(); // could be a class name with trailing ws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Sets a security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * <code>checkPermission</code> method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <code>java.security.SecurityPermission("setProperty."+key)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * permission to see if it's ok to set the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * security property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param key the name of the property to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param datum the value of the property to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *          if a security manager exists and its <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *          java.lang.SecurityManager#checkPermission}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *          denies access to set the specified security property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @throws  NullPointerException if key or datum is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @see #getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see java.security.SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    public static void setProperty(String key, String datum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        check("setProperty."+key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        props.put(key, datum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        invalidateSMCache(key);  /* See below. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * Implementation detail:  If the property we just set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * setProperty() was either "package.access" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * "package.definition", we need to signal to the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * class that the value has just changed, and that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * invalidate it's local cache values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Rather than create a new API entry for this function,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * we use reflection to set a private variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private static void invalidateSMCache(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        final boolean pa = key.equals("package.access");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        final boolean pd = key.equals("package.definition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        if (pa || pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        /* Get the class via the bootstrap class loader. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        Class cl = Class.forName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                            "java.lang.SecurityManager", false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        Field f = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        boolean accessible = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        if (pa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                            f = cl.getDeclaredField("packageAccessValid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                            accessible = f.isAccessible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                            f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                            f = cl.getDeclaredField("packageDefinitionValid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                            accessible = f.isAccessible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                            f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        f.setBoolean(f, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        f.setAccessible(accessible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    catch (Exception e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        /* If we couldn't get the class, it hasn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                         * been loaded yet.  If there is no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                         * field, we shouldn't try to set it.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                         * shouldn't be a security execption, as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                         * are loaded by boot class loader, and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                         * are inside a doPrivileged() here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                         * NOOP: don't do anything...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                }  /* run */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            });  /* PrivilegedAction */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }  /* if */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private static void check(String directive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            security.checkSecurityAccess(directive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    * Returns all providers who satisfy the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    * criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    private static LinkedHashSet<Provider> getAllQualifyingCandidates(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                                                String filterKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                                                String filterValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                                                Provider[] allProviders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        String[] filterComponents = getFilterComponents(filterKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                                        filterValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        // The first component is the service name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        // The second is the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        // If the third isn't null, that is the attrinute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        String serviceName = filterComponents[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        String algName = filterComponents[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        String attrName = filterComponents[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return getProvidersNotUsingCache(serviceName, algName, attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                                         filterValue, allProviders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    private static LinkedHashSet<Provider> getProvidersNotUsingCache(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                                String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                                String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                                String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                                String filterValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                                Provider[] allProviders) {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
   886
        LinkedHashSet<Provider> candidates = new LinkedHashSet<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        for (int i = 0; i < allProviders.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if (isCriterionSatisfied(allProviders[i], serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                                     algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                     attrName, filterValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                candidates.add(allProviders[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        return candidates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * Returns true if the given provider satisfies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * the selection criterion key:value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    private static boolean isCriterionSatisfied(Provider prov,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                                String serviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                                String algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                                String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                                String filterValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        String key = serviceName + '.' + algName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (attrName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            key += ' ' + attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // Check whether the provider has a property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // whose key is the same as the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        String propValue = getProviderProperty(key, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (propValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            // Check whether we have an alias instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            // of a standard name in the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            String standardName = getProviderProperty("Alg.Alias." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                                      serviceName + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                                      algName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                                      prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            if (standardName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                key = serviceName + "." + standardName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                if (attrName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    key += ' ' + attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                propValue = getProviderProperty(key, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (propValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                // The provider doesn't have the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                // key in its property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        // If the key is in the format of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        // <crypto_service>.<algorithm_or_type>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        // there is no need to check the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (attrName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        // If we get here, the key must be in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        // format of <crypto_service>.<algorithm_or_provider> <attribute_name>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (isStandardAttr(attrName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return isConstraintSatisfied(attrName, filterValue, propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            return filterValue.equalsIgnoreCase(propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Returns true if the attribute is a standard attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    private static boolean isStandardAttr(String attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        // For now, we just have two standard attributes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        // KeySize and ImplementedIn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        if (attribute.equalsIgnoreCase("KeySize"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (attribute.equalsIgnoreCase("ImplementedIn"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Returns true if the requested attribute value is supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private static boolean isConstraintSatisfied(String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                                                 String value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                                                 String prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // For KeySize, prop is the max key size the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // provider supports for a specific <crypto_service>.<algorithm>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        if (attribute.equalsIgnoreCase("KeySize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            int requestedSize = Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            int maxSize = Integer.parseInt(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            if (requestedSize <= maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        // For Type, prop is the type of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        // for a specific <crypto service>.<algorithm>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (attribute.equalsIgnoreCase("ImplementedIn")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            return value.equalsIgnoreCase(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    static String[] getFilterComponents(String filterKey, String filterValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int algIndex = filterKey.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (algIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // There must be a dot in the filter, and the dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            // shouldn't be at the beginning of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        String serviceName = filterKey.substring(0, algIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        String algName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        String attrName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (filterValue.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            // The filterValue is an empty string. So the filterKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            // should be in the format of <crypto_service>.<algorithm_or_type>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            algName = filterKey.substring(algIndex + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (algName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                // There must be a algorithm or type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // The filterValue is a non-empty string. So the filterKey must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // in the format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            // <crypto_service>.<algorithm_or_type> <attribute_name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            int attrIndex = filterKey.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            if (attrIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                // There is no attribute name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                attrName = filterKey.substring(attrIndex + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                if (attrName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    // There is no attribute name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            // There must be an algorithm name in the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            if ((attrIndex < algIndex) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                (algIndex == attrIndex - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                throw new InvalidParameterException("Invalid filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                algName = filterKey.substring(algIndex + 1, attrIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        String[] result = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        result[0] = serviceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        result[1] = algName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        result[2] = attrName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * Returns a Set of Strings containing the names of all available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * algorithms or types for the specified Java cryptographic service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * (e.g., Signature, MessageDigest, Cipher, Mac, KeyStore). Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * an empty Set if there is no provider that supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * specified service or if serviceName is null. For a complete list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * of Java cryptographic services, please see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <a href="../../../technotes/guides/security/crypto/CryptoSpec.html">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Cryptography Architecture API Specification &amp; Reference</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * Note: the returned set is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @param serviceName the name of the Java cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * service (e.g., Signature, MessageDigest, Cipher, Mac, KeyStore).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * Note: this parameter is case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @return a Set of Strings containing the names of all available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * algorithms or types for the specified Java cryptographic service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * or an empty set if no provider supports the specified service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    public static Set<String> getAlgorithms(String serviceName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if ((serviceName == null) || (serviceName.length() == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            (serviceName.endsWith("."))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return Collections.EMPTY_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7668
diff changeset
  1083
        HashSet<String> result = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        Provider[] providers = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            // Check the keys for each provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            for (Enumeration<Object> e = providers[i].keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                                                e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                String currentKey = ((String)e.nextElement()).toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                if (currentKey.startsWith(serviceName.toUpperCase())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    // We should skip the currentKey if it contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    // whitespace. The reason is: such an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    // provider property contains attributes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    // implementation of an algorithm. We are only interested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    // in entries which lead to the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    // classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    if (currentKey.indexOf(" ") < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                        result.add(currentKey.substring(serviceName.length() + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    }
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
        return Collections.unmodifiableSet(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
}