src/java.base/share/classes/javax/crypto/CryptoPermissions.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2019, 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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.NoSuchElementException;
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    33
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedReader;
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    38
import java.io.ObjectStreamField;
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    39
import java.io.ObjectInputStream;
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    40
import java.io.ObjectOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class contains CryptoPermission objects, organized into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * PermissionCollections according to algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>When the <code>add</code> method is called to add a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * CryptoPermission, the CryptoPermission is stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * appropriate PermissionCollection. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * collection exists yet, the algorithm name associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the CryptoPermission object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * determined and the <code>newPermissionCollection</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * is called on the CryptoPermission or CryptoAllPermission class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * create the PermissionCollection and add it to the Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see javax.crypto.CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Sharon Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
final class CryptoPermissions extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    66
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final long serialVersionUID = 4946547168093391015L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    69
    /**
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    70
     * @serialField perms java.util.Hashtable
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    71
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    72
    @java.io.Serial
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    73
    private static final ObjectStreamField[] serialPersistentFields = {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    74
        new ObjectStreamField("perms", Hashtable.class),
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    75
    };
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    76
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    77
    // Switched from Hashtable to ConcurrentHashMap to improve scalability.
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    78
    // To maintain serialization compatibility, this field is made transient
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    79
    // and custom readObject/writeObject methods are used.
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    80
    private transient ConcurrentHashMap<String,PermissionCollection> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a new CryptoPermissions object containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * no CryptoPermissionCollections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    CryptoPermissions() {
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
    87
        perms = new ConcurrentHashMap<>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Populates the crypto policy from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * InputStream into this CryptoPermissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param in the InputStream to load from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception SecurityException if cannot load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void load(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws IOException, CryptoPolicyParser.ParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        CryptoPolicyParser parser = new CryptoPolicyParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        parser.read(new BufferedReader(new InputStreamReader(in, "UTF-8")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        CryptoPermission[] parsingResult = parser.getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < parsingResult.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.add(parsingResult[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
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
     * Returns true if this CryptoPermissions object doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * contain any CryptoPermission objects; otherwise, returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return perms.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Adds a permission object to the PermissionCollection for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * algorithm returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>(CryptoPermission)permission.getAlgorithm()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * This method creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * a new PermissionCollection object (and adds the permission to it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * if an appropriate collection does not yet exist. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception SecurityException if this CryptoPermissions object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * marked as readonly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see isReadOnly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   138
        if (isReadOnly()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new SecurityException("Attempt to add a Permission " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                        "to a readonly CryptoPermissions " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                        "object");
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   142
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   144
        if (!(permission instanceof CryptoPermission)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return;
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   146
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        CryptoPermission cryptoPerm = (CryptoPermission)permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        PermissionCollection pc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        getPermissionCollection(cryptoPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        pc.add(cryptoPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        String alg = cryptoPerm.getAlgorithm();
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   153
        perms.putIfAbsent(alg, pc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Checks if this object's PermissionCollection for permissons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of the specified permission's algorithm implies the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * permission. Returns true if the checking succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param permission the Permission object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return true if "permission" is implied by the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * in the PermissionCollection it belongs to, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   167
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!(permission instanceof CryptoPermission)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        CryptoPermission cryptoPerm = (CryptoPermission)permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        PermissionCollection pc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            getPermissionCollection(cryptoPerm.getAlgorithm());
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   177
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   178
        if (pc != null) {
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   179
            return pc.implies(cryptoPerm);
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   180
        } else {
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   181
            // none found
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   182
            return false;
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   183
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns an enumeration of all the Permission objects in all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * PermissionCollections in this CryptoPermissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return an enumeration of all the Permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   192
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   193
    public Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // go through each Permissions in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // and call their elements() function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return new PermissionsEnumerator(perms.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns a CryptoPermissions object which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * represents the minimum of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * CryptoPermissions object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * CryptoPermissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param other the CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * object to compare with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    CryptoPermissions getMinimum(CryptoPermissions other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (this.perms.containsKey(CryptoAllPermission.ALG_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (other.perms.containsKey(CryptoAllPermission.ALG_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        CryptoPermissions ret = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        PermissionCollection thatWildcard =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   225
                other.perms.get(CryptoPermission.ALG_NAME_WILDCARD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int maxKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (thatWildcard != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            maxKeySize = ((CryptoPermission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    thatWildcard.elements().nextElement()).getMaxKeySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // For each algorithm in this CryptoPermissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // find out if there is anything we should add into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // ret.
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   234
        Enumeration<String> thisKeys = this.perms.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        while (thisKeys.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   236
            String alg = thisKeys.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   238
            PermissionCollection thisPc = this.perms.get(alg);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   239
            PermissionCollection thatPc = other.perms.get(alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            CryptoPermission[] partialResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (thatPc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (thatWildcard == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    // The other CryptoPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    // doesn't allow this given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    // algorithm at all. Just skip this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    // algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                partialResult = getMinimum(maxKeySize, thisPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                partialResult = getMinimum(thisPc, thatPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            for (int i = 0; i < partialResult.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ret.add(partialResult[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        PermissionCollection thisWildcard =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   262
                this.perms.get(CryptoPermission.ALG_NAME_WILDCARD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // If this CryptoPermissions doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // have a wildcard, we are done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (thisWildcard == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // Deal with the algorithms only appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // in the other CryptoPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        maxKeySize =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            ((CryptoPermission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    thisWildcard.elements().nextElement()).getMaxKeySize();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   275
        Enumeration<String> thatKeys = other.perms.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        while (thatKeys.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   277
            String alg = thatKeys.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (this.perms.containsKey(alg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   283
            PermissionCollection thatPc = other.perms.get(alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            CryptoPermission[] partialResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            partialResult = getMinimum(maxKeySize, thatPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            for (int i = 0; i < partialResult.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                ret.add(partialResult[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Get the minimum of the two given PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>thisPc</code> and <code>thatPc</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param thisPc the first given PermissionColloection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param thatPc the second given PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private CryptoPermission[] getMinimum(PermissionCollection thisPc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                          PermissionCollection thatPc) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   308
        Vector<CryptoPermission> permVector = new Vector<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   310
        Enumeration<Permission> thisPcPermissions = thisPc.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // For each CryptoPermission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // thisPc object, do the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // 1) if this CryptoPermission is implied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //     by thatPc, this CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        //     should be returned, and we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        //     move on to check the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        //     CryptoPermission in thisPc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // 2) otherwise, we should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        //     all CryptoPermissions in thatPc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        //     which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        //     are implied by this CryptoPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        //     Then we can move on to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        //     next CryptoPermission in thisPc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        while (thisPcPermissions.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            CryptoPermission thisCp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                (CryptoPermission)thisPcPermissions.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   329
            Enumeration<Permission> thatPcPermissions = thatPc.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            while (thatPcPermissions.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                CryptoPermission thatCp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    (CryptoPermission)thatPcPermissions.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (thatCp.implies(thisCp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    permVector.addElement(thisCp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (thisCp.implies(thatCp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    permVector.addElement(thatCp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        CryptoPermission[] ret = new CryptoPermission[permVector.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        permVector.copyInto(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Returns all the CryptoPermission objects in the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * whose maximum keysize no greater than <code>maxKeySize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * For all CryptoPermission objects with a maximum keysize greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * than <code>maxKeySize</code>, this method constructs a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * corresponding CryptoPermission object whose maximum keysize is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * set to <code>maxKeySize</code>, and includes that in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param maxKeySize the given maximum key size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param pc the given PermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private CryptoPermission[] getMinimum(int maxKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                          PermissionCollection pc) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   364
        Vector<CryptoPermission> permVector = new Vector<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   366
        Enumeration<Permission> enum_ = pc.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            CryptoPermission cp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                (CryptoPermission)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (cp.getMaxKeySize() <= maxKeySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                permVector.addElement(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (cp.getCheckParam()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    permVector.addElement(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                           new CryptoPermission(cp.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                                maxKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                                cp.getAlgorithmParameterSpec(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                                cp.getExemptionMechanism()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    permVector.addElement(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                           new CryptoPermission(cp.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                                maxKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                                cp.getExemptionMechanism()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
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
        CryptoPermission[] ret = new CryptoPermission[permVector.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        permVector.copyInto(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns the PermissionCollection for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * specified algorithm. Returns null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * isn't such a PermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param alg the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    PermissionCollection getPermissionCollection(String alg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // If this CryptoPermissions includes CryptoAllPermission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // we should return CryptoAllPermission.
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   404
        PermissionCollection pc = perms.get(CryptoAllPermission.ALG_NAME);
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   405
        if (pc == null) {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   406
            pc = perms.get(alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   408
            // If there isn't a PermissionCollection for
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   409
            // the given algorithm,we should return the
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   410
            // PermissionCollection for the wildcard
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   411
            // if there is one.
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   412
            if (pc == null) {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   413
                pc = perms.get(CryptoPermission.ALG_NAME_WILDCARD);
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   414
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return pc;
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
     * Returns the PermissionCollection for the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * associated with the specified CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * object. Creates such a PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * if such a PermissionCollection does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * exist yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param cryptoPerm the CryptoPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private PermissionCollection getPermissionCollection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                          CryptoPermission cryptoPerm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        String alg = cryptoPerm.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   433
        PermissionCollection pc = perms.get(alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (pc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            pc = cryptoPerm.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   440
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   441
    @java.io.Serial
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   442
    private void readObject(ObjectInputStream s)
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   443
        throws IOException, ClassNotFoundException {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   444
        ObjectInputStream.GetField fields = s.readFields();
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   445
        @SuppressWarnings("unchecked")
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   446
        Hashtable<String,PermissionCollection> permTable =
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   447
                (Hashtable<String,PermissionCollection>)
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   448
                (fields.get("perms", null));
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   449
        if (permTable != null) {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   450
            perms = new ConcurrentHashMap<>(permTable);
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   451
        } else {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   452
            perms = new ConcurrentHashMap<>();
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   453
        }
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   454
    }
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   455
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   456
    @java.io.Serial
13557
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   457
    private void writeObject(ObjectOutputStream s) throws IOException {
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   458
        Hashtable<String,PermissionCollection> permTable =
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   459
                new Hashtable<>(perms);
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   460
        ObjectOutputStream.PutField fields = s.putFields();
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   461
        fields.put("perms", permTable);
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   462
        s.writeFields();
f0156a32c08f 7107613: scalability bloker in javax.crypto.CryptoPermissions
valeriep
parents: 10336
diff changeset
   463
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   466
final class PermissionsEnumerator implements Enumeration<Permission> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // all the perms
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   469
    private final Enumeration<PermissionCollection> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // the current set
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   471
    private Enumeration<Permission> permset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   473
    PermissionsEnumerator(Enumeration<PermissionCollection> e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        perms = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        permset = getNextEnumWithMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   478
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public synchronized boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // if we enter with permissionimpl null, we know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // there are no more left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   483
        if (permset == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return  false;
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   485
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // try to see if there are any left in the current one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   489
        if (permset.hasMoreElements()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return true;
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   491
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // get the next one that has something in it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        permset = getNextEnumWithMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // if it is null, we are done!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return (permset != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   500
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   501
    public synchronized Permission nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // hasMoreElements will update permset to the next permset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // with something in it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return permset.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            throw new NoSuchElementException("PermissionsEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   512
    private Enumeration<Permission> getNextEnumWithMore() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        while (perms.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   514
            PermissionCollection pc = perms.nextElement();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   515
            Enumeration<Permission> next = pc.elements();
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   516
            if (next.hasMoreElements()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                return next;
39880
d4cfdddbc897 8161527: NPE is thrown if exempt application is bundled with specific cryptoPerms
wetmore
parents: 25859
diff changeset
   518
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
}