src/java.base/share/classes/sun/security/jca/ProviderList.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 48080 18a4438eb690
child 57950 4612a3cfb927
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35643
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
     2
 * Copyright (c) 2003, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.security.jca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    30
import java.security.AccessController;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    31
import java.security.PrivilegedAction;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    32
import java.security.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Provider.Service;
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    34
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * List of Providers. Used to represent the provider preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35643
diff changeset
    39
 * The system starts out with a ProviderList that only has the names
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35643
diff changeset
    40
 * of the Providers.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35643
diff changeset
    41
 * When using ServiceLoader to load the providers, Providers are created
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35643
diff changeset
    42
 * semi-eagerly as we iterate through them looking for a match.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * For compatibility reasons, Providers that could not be loaded are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and internally presented as the instance EMPTY_PROVIDER. However, those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * objects cannot be presented to applications. Call the convert() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to force all Providers to be loaded and to obtain a ProviderList with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * invalid entries removed. All this is handled by the Security class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Note that all indices used by this class are 0-based per general Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * convention. These must be converted to the 1-based indices used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Security class externally when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Instances of this class are immutable. This eliminates the need for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * cloning and synchronization in consumers. The add() and remove() style
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * methods are static in order to avoid confusion about the immutability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final class ProviderList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31270
diff changeset
    63
    static final sun.security.util.Debug debug =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        sun.security.util.Debug.getInstance("jca", "ProviderList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31270
diff changeset
    66
    private static final ProviderConfig[] PC0 = new ProviderConfig[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31270
diff changeset
    68
    private static final Provider[] P0 = new Provider[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // constant for an ProviderList with no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final ProviderList EMPTY = new ProviderList(PC0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    73
    // list of all jdk.security.provider.preferred entries
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    74
    static private PreferredList preferredPropList = null;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // dummy provider object to use during initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // used to avoid explicit null checks in various places
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final Provider EMPTY_PROVIDER =
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 38435
diff changeset
    79
        new Provider("##Empty##", "1.0", "initialization in progress") {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    80
            private static final long serialVersionUID = 1151354171352296389L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // override getService() to return null slightly faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            public Service getService(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // construct a ProviderList from the security properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // (static provider configuration in the java.security file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static ProviderList fromSecurityProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // doPrivileged() because of Security.getProperty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        new PrivilegedAction<ProviderList>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            public ProviderList run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return new ProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static ProviderList add(ProviderList providerList, Provider p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return insertAt(providerList, p, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static ProviderList insertAt(ProviderList providerList, Provider p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (providerList.getProvider(p.getName()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return providerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   108
        List<ProviderConfig> list = new ArrayList<>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                    (Arrays.asList(providerList.configs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int n = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if ((position < 0) || (position > n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            position = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        list.add(position, new ProviderConfig(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return new ProviderList(list.toArray(PC0), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static ProviderList remove(ProviderList providerList, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // make sure provider exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (providerList.getProvider(name) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return providerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // copy all except matching to new list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ProviderConfig[] configs = new ProviderConfig[providerList.size() - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (ProviderConfig config : providerList.configs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (config.getProvider().getName().equals(name) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                configs[j++] = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return new ProviderList(configs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // Create a new ProviderList from the specified Providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // This method is for use by SunJSSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static ProviderList newList(Provider ... providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ProviderConfig[] configs = new ProviderConfig[providers.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            configs[i] = new ProviderConfig(providers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return new ProviderList(configs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // configuration of the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private final ProviderConfig[] configs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // flag indicating whether all configs have been loaded successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private volatile boolean allLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // List returned by providers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private final List<Provider> userList = new AbstractList<Provider>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public Provider get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return getProvider(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Create a new ProviderList from an array of configs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private ProviderList(ProviderConfig[] configs, boolean allLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.configs = configs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.allLoaded = allLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Return a new ProviderList parsed from the java.security Properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private ProviderList() {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   172
        List<ProviderConfig> configList = new ArrayList<>();
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   173
        String entry;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   174
        int i = 1;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   175
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   176
        while ((entry = Security.getProperty("security.provider." + i)) != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            entry = entry.trim();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48080
diff changeset
   178
            if (entry.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                System.err.println("invalid entry for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                   "security.provider." + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            int k = entry.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            ProviderConfig config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (k == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                config = new ProviderConfig(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   188
                String provName = entry.substring(0, k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                String argument = entry.substring(k + 1).trim();
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   190
                config = new ProviderConfig(provName, argument);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // Get rid of duplicate providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (configList.contains(config) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                configList.add(config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   197
            i++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        configs = configList.toArray(PC0);
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   200
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   201
        // Load config entries for use when getInstance is called
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   202
        entry = Security.getProperty("jdk.security.provider.preferred");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48080
diff changeset
   203
        if (entry != null && !(entry = entry.trim()).isEmpty()) {
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   204
            String[] entries = entry.split(",");
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   205
            if (ProviderList.preferredPropList == null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   206
                ProviderList.preferredPropList = new PreferredList();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   207
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   208
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   209
            for (String e : entries) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   210
                i = e.indexOf(':');
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   211
                if (i < 0) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   212
                    if (debug != null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   213
                        debug.println("invalid preferred entry skipped.  " +
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   214
                                "Missing colon delimiter \"" + e + "\"");
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   215
                    }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   216
                    continue;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   217
                }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   218
                ProviderList.preferredPropList.add(new PreferredEntry(
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   219
                        e.substring(0, i).trim(), e.substring(i + 1).trim()));
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   220
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   221
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   222
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            debug.println("provider configuration: " + configList);
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   225
            debug.println("config configuration: " +
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   226
                    ProviderList.preferredPropList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Construct a special ProviderList for JAR verification. It consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * of the providers specified via jarClassNames, which must be on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * bootclasspath and cannot be in signed JAR files. This is to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * possible recursion and deadlock during verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   236
    ProviderList getJarList(String[] jarProvNames) {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   237
        List<ProviderConfig> newConfigs = new ArrayList<>();
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   238
        for (String provName : jarProvNames) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30374
diff changeset
   239
            ProviderConfig newConfig = new ProviderConfig(provName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            for (ProviderConfig config : configs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                // if the equivalent object is present in this provider list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // use the old object rather than the new object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // this ensures that when the provider is loaded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // new thread local list, it will also become available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // in this provider list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (config.equals(newConfig)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    newConfig = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            newConfigs.add(newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ProviderConfig[] configArray = newConfigs.toArray(PC0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return new ProviderList(configArray, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Return the Provider at the specified index. Returns EMPTY_PROVIDER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * if the provider could not be loaded at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    Provider getProvider(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Provider p = configs[index].getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return (p != null) ? p : EMPTY_PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Return an unmodifiable List of all Providers in this List. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * individual Providers are loaded on demand. Elements that could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * be initialized are replaced with EMPTY_PROVIDER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public List<Provider> providers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return userList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private ProviderConfig getProviderConfig(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int index = getIndex(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return (index != -1) ? configs[index] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // return the Provider with the specified name or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public Provider getProvider(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        ProviderConfig config = getProviderConfig(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return (config == null) ? null : config.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Return the index at which the provider with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * installed or -1 if it is not present in this ProviderList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public int getIndex(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (int i = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            Provider p = getProvider(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (p.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    // attempt to load all Providers not already loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private int loadAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (allLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return configs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            debug.println("Loading all providers");
48080
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47216
diff changeset
   311
            new Exception("Debug Info. Call trace:").printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        for (int i = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            Provider p = configs[i].getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (n == configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            allLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Try to load all Providers and return the ProviderList. If one or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * more Providers could not be loaded, a new ProviderList with those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * entries removed is returned. Otherwise, the method returns this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    ProviderList removeInvalid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int n = loadAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (n == configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ProviderConfig[] newConfigs = new ProviderConfig[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for (int i = 0, j = 0; i < configs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            ProviderConfig config = configs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (config.isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                newConfigs[j++] = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return new ProviderList(newConfigs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    // return the providers as an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public Provider[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return providers().toArray(P0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // return a String representation of this ProviderList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return Arrays.asList(configs).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Return a Service describing an implementation of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * algorithm from the Provider with the highest precedence that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * supports that algorithm. Return null if no Provider supports this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public Service getService(String type, String name) {
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   363
        ArrayList<PreferredEntry> pList = null;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   364
        int i;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   365
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   366
        // Preferred provider list
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   367
        if (preferredPropList != null &&
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   368
                (pList = preferredPropList.getAll(type, name)) != null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   369
            for (i = 0; i < pList.size(); i++) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   370
                Provider p = getProvider(pList.get(i).provider);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   371
                Service s = p.getService(type, name);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   372
                if (s != null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   373
                    return s;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   374
                }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   375
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   376
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   377
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   378
        for (i = 0; i < configs.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            Provider p = getProvider(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Service s = p.getService(type, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Return a List containing all the Services describing implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * of the specified algorithms in precedence order. If no implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * exists, this method returns an empty List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * The elements of this list are determined lazily on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The List returned is NOT thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public List<Service> getServices(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return new ServiceList(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * This method exists for compatibility with JCE only. It will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * once JCE has been changed to use the replacement method.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   404
     * @deprecated use {@code getServices(List<ServiceId>)} instead
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public List<Service> getServices(String type, List<String> algorithms) {
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   408
        List<ServiceId> ids = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        for (String alg : algorithms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            ids.add(new ServiceId(type, alg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return getServices(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public List<Service> getServices(List<ServiceId> ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return new ServiceList(ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Inner class for a List of Services. Custom List implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * order to delay Provider initialization and lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Not thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private final class ServiceList extends AbstractList<Service> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // type and algorithm for simple lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // avoid allocating/traversing the ServiceId list for these lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        private final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // list of ids for parallel lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // if ids is non-null, type and algorithm are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        private final List<ServiceId> ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // first service we have found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // it is stored in a separate variable so that we can avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // allocating the services list if we do not need the second service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // this is the case if we don't failover (failovers are typically rare)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // list of the services we have found so far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        private List<Service> services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // index into config[] of the next provider we need to query
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   445
        private int providerIndex = 0;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   446
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   447
        // Matching preferred provider list for this ServiceList
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   448
        ArrayList<PreferredEntry> preferredList = null;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   449
        private int preferredIndex = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        ServiceList(String type, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            this.ids = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        ServiceList(List<ServiceId> ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            this.type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            this.algorithm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            this.ids = ids;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        private void addService(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (firstService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                firstService = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                if (services == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    services = new ArrayList<Service>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    services.add(firstService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                services.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        private Service tryGet(int index) {
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   476
            Provider p;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   477
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   478
            // If preferred providers are configured, check for matches with
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   479
            // the requested service.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   480
            if (preferredPropList != null && preferredList == null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   481
                preferredList = preferredPropList.getAll(this);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   482
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   483
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if ((index == 0) && (firstService != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    return firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                } else if ((services != null) && (services.size() > index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    return services.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (providerIndex >= configs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   493
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   494
                // If there were matches with a preferred provider, iterate
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   495
                // through the list first before going through the
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   496
                // ordered list (java.security.provider.#)
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   497
                if (preferredList != null &&
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   498
                        preferredIndex < preferredList.size()) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   499
                    PreferredEntry entry = preferredList.get(preferredIndex++);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   500
                    // Look for the provider name in the PreferredEntry
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   501
                    p = getProvider(entry.provider);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   502
                    if (p == null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   503
                        if (debug != null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   504
                            debug.println("No provider found with name: " +
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   505
                                    entry.provider);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   506
                        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   507
                        continue;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   508
                    }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   509
                } else {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   510
                    // check all algorithms in this provider before moving on
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   511
                    p = getProvider(providerIndex++);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   512
                }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   513
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    // simple lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    Service s = p.getService(type, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        addService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    // parallel lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    for (ServiceId id : ids) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        Service s = p.getService(id.type, id.algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                            addService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        public Service get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            Service s = tryGet(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (services != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                n = services.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                n = (firstService != null) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            while (tryGet(n) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // override isEmpty() and iterator() to not call size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // this avoids loading + checking all Providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return (tryGet(0) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        public Iterator<Service> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return new Iterator<Service>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    return tryGet(index) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                public Service next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    Service s = tryGet(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   584
    // Provider list defined by jdk.security.provider.preferred entry
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   585
    static final class PreferredList {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   586
        ArrayList<PreferredEntry> list = new ArrayList<PreferredEntry>();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   587
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   588
        /*
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   589
         * Return a list of all preferred entries that match the passed
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   590
         * ServiceList.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   591
         */
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   592
        ArrayList<PreferredEntry> getAll(ServiceList s) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   593
            if (s.ids == null) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   594
                return getAll(s.type, s.algorithm);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   595
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   596
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   597
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   598
            ArrayList<PreferredEntry> l = new ArrayList<PreferredEntry>();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   599
            for (ServiceId id : s.ids) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   600
                implGetAll(l, id.type, id.algorithm);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   601
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   602
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   603
            return l;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   604
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   605
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   606
        /*
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   607
         * Return a list of all preferred entries that match the passed
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   608
         * type and algorithm.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   609
         */
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   610
        ArrayList<PreferredEntry> getAll(String type, String algorithm) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   611
            ArrayList<PreferredEntry> l = new ArrayList<PreferredEntry>();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   612
            implGetAll(l, type, algorithm);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   613
            return l;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   614
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   615
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   616
        /*
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   617
         * Compare each preferred entry against the passed type and
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   618
         * algorithm, putting any matches in the passed ArrayList.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   619
         */
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   620
        private void implGetAll(ArrayList<PreferredEntry> l, String type,
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   621
                String algorithm) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   622
            PreferredEntry e;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   623
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   624
            for (int i = 0; i < size(); i++) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   625
                e = list.get(i);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   626
                if (e.match(type, algorithm)) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   627
                    l.add(e);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   628
                }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   629
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   630
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   631
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   632
        public PreferredEntry get(int i) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   633
            return list.get(i);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   634
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   635
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   636
        public int size() {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   637
            return list.size();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   638
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   639
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   640
        public boolean add(PreferredEntry e) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   641
            return list.add(e);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   642
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   643
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   644
        public String toString() {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   645
            String s = "";
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   646
            for (PreferredEntry e: list) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   647
                s += e.toString();
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   648
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   649
            return s;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   650
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   651
    }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   652
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   653
    /* Defined Groups for jdk.security.provider.preferred */
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   654
    private static final String SHA2Group[] = { "SHA-224", "SHA-256",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   655
            "SHA-384", "SHA-512", "SHA-512/224", "SHA-512/256" };
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   656
    private static final String HmacSHA2Group[] = { "HmacSHA224",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   657
            "HmacSHA256", "HmacSHA384", "HmacSHA512"};
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   658
    private static final String SHA2RSAGroup[] = { "SHA224withRSA",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   659
            "SHA256withRSA", "SHA384withRSA", "SHA512withRSA"};
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   660
    private static final String SHA2DSAGroup[] = { "SHA224withDSA",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   661
            "SHA256withDSA", "SHA384withDSA", "SHA512withDSA"};
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   662
    private static final String SHA2ECDSAGroup[] = { "SHA224withECDSA",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   663
            "SHA256withECDSA", "SHA384withECDSA", "SHA512withECDSA"};
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   664
    private static final String SHA3Group[] = { "SHA3-224", "SHA3-256",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   665
            "SHA3-384", "SHA3-512" };
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   666
    private static final String HmacSHA3Group[] = { "HmacSHA3-224",
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   667
            "HmacSHA3-256", "HmacSHA3-384", "HmacSHA3-512"};
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   668
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   669
    // Individual preferred property entry from jdk.security.provider.preferred
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   670
    private static class PreferredEntry {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   671
        private String type = null;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   672
        private String algorithm;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   673
        private String provider;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   674
        private String alternateNames[] = null;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   675
        private boolean group = false;
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   676
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   677
        PreferredEntry(String t, String p) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   678
            int i = t.indexOf('.');
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   679
            if (i > 0) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   680
                type = t.substring(0, i);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   681
                algorithm = t.substring(i + 1);
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   682
            } else {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   683
                algorithm = t;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   684
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   685
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   686
            provider = p;
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   687
            // Group definitions
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   688
            if (type != null && type.compareToIgnoreCase("Group") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   689
                // Currently intrinsic algorithm groups
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   690
                if (algorithm.compareToIgnoreCase("SHA2") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   691
                    alternateNames = SHA2Group;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   692
                } else if (algorithm.compareToIgnoreCase("HmacSHA2") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   693
                    alternateNames = HmacSHA2Group;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   694
                } else if (algorithm.compareToIgnoreCase("SHA2RSA") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   695
                    alternateNames = SHA2RSAGroup;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   696
                } else if (algorithm.compareToIgnoreCase("SHA2DSA") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   697
                    alternateNames = SHA2DSAGroup;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   698
                } else if (algorithm.compareToIgnoreCase("SHA2ECDSA") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   699
                    alternateNames = SHA2ECDSAGroup;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   700
                } else if (algorithm.compareToIgnoreCase("SHA3") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   701
                    alternateNames = SHA3Group;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   702
                } else if (algorithm.compareToIgnoreCase("HmacSHA3") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   703
                    alternateNames = HmacSHA3Group;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   704
                }
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   705
                if (alternateNames != null) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   706
                    group = true;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   707
                }
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   708
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   709
            // If the algorithm name given is SHA1
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   710
            } else if (algorithm.compareToIgnoreCase("SHA1") == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   711
                alternateNames = new String[] { "SHA-1" };
35643
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
   712
            } else if (algorithm.compareToIgnoreCase("SHA-1") == 0) {
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   713
                alternateNames = new String[] { "SHA1" };
35643
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
   714
            }
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   715
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   716
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   717
        boolean match(String t, String a) {
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   718
            if (debug != null) {
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   719
                debug.println("Config check:  " + toString() + " == " +
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   720
                        print(t, a, null));
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   721
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   722
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   723
            // Compare service type if configured
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   724
            if (type != null && !group && type.compareToIgnoreCase(t) != 0) {
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   725
                return false;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   726
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   727
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   728
            // Compare the algorithm string.
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   729
            if (!group && a.compareToIgnoreCase(algorithm) == 0) {
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   730
                if (debug != null) {
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   731
                    debug.println("Config entry matched:  " + toString());
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   732
                }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   733
                return true;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   734
            }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   735
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   736
            if (alternateNames != null) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   737
                for (String alt : alternateNames) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   738
                    if (debug != null) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   739
                        debug.println("AltName check:  " + print(type, alt,
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   740
                                provider));
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   741
                    }
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   742
                    if (a.compareToIgnoreCase(alt) == 0) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   743
                        if (debug != null) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   744
                            debug.println("AltName entry matched:  " +
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   745
                                    provider);
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   746
                        }
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   747
                        return true;
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   748
                    }
35643
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
   749
                }
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
   750
            }
ada9a798efee 8145344: Add SHA1 and SHA-224 to preferred provider list for solaris-sparc
ascarpino
parents: 33241
diff changeset
   751
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   752
            // No match
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   753
            return false;
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   754
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   755
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   756
        // Print debugging output of PreferredEntry
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   757
        private String print(String t, String a, String p) {
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   758
            return "[" + ((t != null) ? t : "" ) + ", " + a +
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   759
                    ((p != null) ? " : " + p : "" ) + "] ";
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   760
        }
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   761
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   762
        public String toString() {
38435
292ad46c1bf1 8155847: SHA groups needed for jdk.security.provider.preferred
ascarpino
parents: 36511
diff changeset
   763
            return print(type, algorithm, provider);
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   764
        }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   765
    }
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   766
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
}