src/java.base/share/classes/java/security/CodeSource.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57950 4612a3cfb927
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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: 57792
diff changeset
     2
 * Copyright (c) 1997, 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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.*;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    37
import sun.net.util.URLUtil;
47418
6993f9f1ff83 8181432: Better processing of unresolved permissions
weijun
parents: 47216
diff changeset
    38
import sun.security.util.IOUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>This class extends the concept of a codebase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * encapsulate not only the location (URL) but also the certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that were used to verify signed code originating from that location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Roland Schemers
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 39560
diff changeset
    48
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class CodeSource implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 57792
diff changeset
    53
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = 4977541819976013951L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The code location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
57792
1b6806340400 8229773: Resolve permissions for code source URLs lazily
redestad
parents: 52700
diff changeset
    61
    private final URL location;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private transient CodeSigner[] signers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The code signers. Certificate chains are concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31141
diff changeset
    71
    private transient java.security.cert.Certificate[] certs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // cached SocketPermission used for matchLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private transient SocketPermission sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // for generating cert paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private transient CertificateFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    80
     * A String form of the URL for use as a key in HashMaps/Sets. The String
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    81
     * form should be behave in the same manner as the URL when compared for
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    82
     * equality in a HashMap/Set, except that no nameservice lookup is done
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    83
     * on the hostname (only string comparison), and the fragment is not
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    84
     * considered.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    85
     */
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    86
    private transient String locationNoFragString;
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    87
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    88
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Constructs a CodeSource and associates it with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * location and set of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
    92
     * @param url the location (URL).  It may be {@code null}.
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
    93
     * @param certs the certificate(s). It may be {@code null}. The contents
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
    94
     * of the array are copied to protect against subsequent modification.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31141
diff changeset
    96
    public CodeSource(URL url, java.security.cert.Certificate[] certs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.location = url;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    98
        if (url != null) {
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
    99
            this.locationNoFragString = URLUtil.urlNoFragString(url);
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   100
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Copy the supplied certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Constructs a CodeSource and associates it with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * location and set of code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   112
     * @param url the location (URL).  It may be {@code null}.
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   113
     * @param signers the code signers. It may be {@code null}. The contents
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   114
     * of the array are copied to protect against subsequent modification.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public CodeSource(URL url, CodeSigner[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.location = url;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   120
        if (url != null) {
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   121
            this.locationNoFragString = URLUtil.urlNoFragString(url);
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   122
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // Copy the supplied signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            this.signers = signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (location != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return location.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * object. Two CodeSource objects are considered equal if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * locations are of identical value and if their signer certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * chains are of identical value. It is not required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the certificate chains be in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return true if the objects are considered equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   154
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // objects types must be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!(obj instanceof CodeSource))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        CodeSource cs = (CodeSource) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // URLs must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (location == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // if location is null, then cs.location must be null as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (cs.location != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // if location is not null, then it must equal cs.location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (!location.equals(cs.location)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // certs must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return matchCerts(cs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the location associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   181
     * @return the location (URL), or {@code null} if no URL was supplied
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   182
     * during construction.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public final URL getLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        /* since URL is practically immutable, returning itself is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
           a security problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return this.location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   191
     * Returns a String form of the URL for use as a key in HashMaps/Sets.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   192
     */
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   193
    String getLocationNoFragString() {
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   194
        return locationNoFragString;
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   195
    }
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   196
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   197
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns the certificates associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * If this CodeSource object was created using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * {@link #CodeSource(URL url, CodeSigner[] signers)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * constructor then its certificate chains are extracted and used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * create an array of Certificate objects. Each signer certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * followed by its supporting certificate chain (which may be empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Each signer certificate and its supporting certificate chain is ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * bottom-to-top (i.e., with the signer certificate first and the (root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * certificate authority last).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   209
     * @return a copy of the certificate array, or {@code null} if there
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   210
     * is none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public final java.security.cert.Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // Convert the code signers to certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ArrayList<java.security.cert.Certificate> certChains =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   219
                        new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                certChains.addAll(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    signers[i].getSignerCertPath().getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            certs = certChains.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        new java.security.cert.Certificate[certChains.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the code signers associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * If this CodeSource object was created using the
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12434
diff changeset
   237
     * {@link #CodeSource(URL url, java.security.cert.Certificate[] certs)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * constructor then its certificate chains are extracted and used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * create an array of CodeSigner objects. Note that only X.509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * are examined - all other certificate types are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   242
     * @return a copy of the code signer array, or {@code null} if there
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   243
     * is none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public final CodeSigner[] getCodeSigners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Convert the certs to code signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            signers = convertCertArrayToSignerArray(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns true if this CodeSource object "implies" the specified CodeSource.
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   263
     * <p>
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   264
     * More specifically, this method makes the following checks.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
   265
     * If any fail, it returns false. If they all succeed, it returns true.
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   266
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <li> <i>codesource</i> must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <li> If this object's certificates are not null, then all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * of this object's certificates must be present in <i>codesource</i>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <li> If this object's location (getLocation()) is not null, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * following checks are made against this object's location and
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
   273
     * <i>codesource</i>'s:
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   274
     *   <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     <li>  <i>codesource</i>'s location must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *     <li>  If this object's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *           equals <i>codesource</i>'s location, then return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     <li>  This object's protocol (getLocation().getProtocol()) must be
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   281
     *           equal to <i>codesource</i>'s protocol, ignoring case.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     <li>  If this object's host (getLocation().getHost()) is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *           then the SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *           constructed with this object's host must imply the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *           SocketPermission constructed with <i>codesource</i>'s host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *     <li>  If this object's port (getLocation().getPort()) is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *           equal to -1 (that is, if a port is specified), it must equal
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   290
     *           <i>codesource</i>'s port or default port
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   291
     *           (codesource.getLocation().getDefaultPort()).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *     <li>  If this object's file (getLocation().getFile()) doesn't equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *           <i>codesource</i>'s file, then the following checks are made:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *           If this object's file ends with "/-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *           then <i>codesource</i>'s file must start with this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *           file (exclusive the trailing "-").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *           If this object's file ends with a "/*",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *           then <i>codesource</i>'s file must start with this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *           file and must not have any further "/" separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *           If this object's file doesn't end with a "/",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *           then <i>codesource</i>'s file must match this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *           file with a '/' appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *     <li>  If this object's reference (getLocation().getRef()) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *           not null, it must equal <i>codesource</i>'s reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   308
     *   </ul>
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   309
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * For example, the codesource objects with the following locations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * and null certificates all imply
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 47418
diff changeset
   313
     * the codesource with the location "http://www.example.com/classes/foo.jar"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * and null certificates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *     http:
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 47418
diff changeset
   317
     *     http://*.example.com/classes/*
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 47418
diff changeset
   318
     *     http://www.example.com/classes/-
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 47418
diff changeset
   319
     *     http://www.example.com/classes/foo.jar
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Note that if this CodeSource has a null location and a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * certificate chain, then it implies every other CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param codesource CodeSource to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return true if the specified codesource is implied by this codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public boolean implies(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (codesource == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return matchCerts(codesource, false) && matchLocation(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Returns true if all the certs in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * CodeSource are also in <i>that</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param that the CodeSource to check against.
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   343
     * @param strict if true then a strict equality match is performed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *               Otherwise a subset match is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31538
diff changeset
   346
    boolean matchCerts(CodeSource that, boolean strict)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // match any key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (certs == null && signers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (strict) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                return (that.certs == null && that.signers == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // both have signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } else if (signers != null && that.signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (strict && signers.length != that.signers.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                for (int j = 0; j < that.signers.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    if (signers[i].equals(that.signers[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // both have certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else if (certs != null && that.certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (strict && certs.length != that.certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                for (int j = 0; j < that.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (certs[i].equals(that.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Returns true if two CodeSource's have the "same" location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param that CodeSource to compare against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   401
    private boolean matchLocation(CodeSource that) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   402
        if (location == null)
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   403
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   405
        if ((that == null) || (that.location == null))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   406
            return false;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   407
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   408
        if (location.equals(that.location))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   409
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   411
        if (!location.getProtocol().equalsIgnoreCase(that.location.getProtocol()))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   412
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   414
        int thisPort = location.getPort();
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   415
        if (thisPort != -1) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   416
            int thatPort = that.location.getPort();
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   417
            int port = thatPort != -1 ? thatPort
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   418
                                      : that.location.getDefaultPort();
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   419
            if (thisPort != port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return false;
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   421
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   423
        if (location.getFile().endsWith("/-")) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   424
            // Matches the directory and (recursively) all files
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   425
            // and subdirectories contained in that directory.
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   426
            // For example, "/a/b/-" implies anything that starts with
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   427
            // "/a/b/"
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   428
            String thisPath = location.getFile().substring(0,
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   429
                                            location.getFile().length()-1);
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   430
            if (!that.location.getFile().startsWith(thisPath))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   431
                return false;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   432
        } else if (location.getFile().endsWith("/*")) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   433
            // Matches the directory and all the files contained in that
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   434
            // directory.
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   435
            // For example, "/a/b/*" implies anything that starts with
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   436
            // "/a/b/" but has no further slashes
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   437
            int last = that.location.getFile().lastIndexOf('/');
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   438
            if (last == -1)
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   439
                return false;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   440
            String thisPath = location.getFile().substring(0,
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   441
                                            location.getFile().length()-1);
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   442
            String thatPath = that.location.getFile().substring(0, last+1);
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   443
            if (!thatPath.equals(thisPath))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   444
                return false;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   445
        } else {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   446
            // Exact matches only.
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   447
            // For example, "/a/b" and "/a/b/" both imply "/a/b/"
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   448
            if ((!that.location.getFile().equals(location.getFile()))
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   449
                && (!that.location.getFile().equals(location.getFile()+"/"))) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   450
                return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   452
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   454
        if (location.getRef() != null
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   455
            && !location.getRef().equals(that.location.getRef())) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   456
            return false;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   457
        }
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   458
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   459
        String thisHost = location.getHost();
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   460
        String thatHost = that.location.getHost();
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   461
        if (thisHost != null) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   462
            if (("".equals(thisHost) || "localhost".equals(thisHost)) &&
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   463
                ("".equals(thatHost) || "localhost".equals(thatHost))) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   464
                // ok
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   465
            } else if (!thisHost.equals(thatHost)) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   466
                if (thatHost == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    return false;
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   468
                }
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   469
                if (this.sp == null) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   470
                    this.sp = new SocketPermission(thisHost, "resolve");
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   471
                }
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   472
                if (that.sp == null) {
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   473
                    that.sp = new SocketPermission(thatHost, "resolve");
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   474
                }
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   475
                if (!this.sp.implies(that.sp)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   480
        // everything matches
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   481
        return true;
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   482
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Returns a string describing this CodeSource, telling its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * URL and certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return information about this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
12434
fed433bd7b71 7152564: Improve CodeSource.matchLocation(CodeSource) performance
mullan
parents: 8784
diff changeset
   490
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        sb.append(this.location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (this.certs != null && this.certs.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            for (int i = 0; i < this.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                sb.append( " " + this.certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } else if (this.signers != null && this.signers.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            for (int i = 0; i < this.signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                sb.append( " " + this.signers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            sb.append(" <no signer certificates>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Writes this object out to a stream (i.e., serializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14014
diff changeset
   515
     * @serialData An initial {@code URL} is followed by an
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14014
diff changeset
   516
     * {@code int} indicating the number of certificates to follow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * (a value of "zero" denotes that there are no certificates associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * with this object).
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14014
diff changeset
   519
     * Each certificate is written out starting with a {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * denoting the certificate type, followed by an
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14014
diff changeset
   521
     * {@code int} specifying the length of the certificate encoding,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * followed by the certificate encoding itself which is written out as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * array of bytes. Finally, if any code signers are present then the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * of code signers is serialized and written out too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 57792
diff changeset
   526
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private void writeObject(java.io.ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        oos.defaultWriteObject(); // location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // Serialize the array of certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (certs == null || certs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            oos.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            // write out the total number of certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            oos.writeInt(certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // write out each cert, including its type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                java.security.cert.Certificate cert = certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    oos.writeUTF(cert.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    oos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    oos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                } catch (CertificateEncodingException cee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    throw new IOException(cee.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // Serialize the array of code signers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (signers != null && signers.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            oos.writeObject(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Restores this object from a stream (i.e., deserializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 57792
diff changeset
   561
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private void readObject(java.io.ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        CertificateFactory cf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        Hashtable<String, CertificateFactory> cfs = null;
45980
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   567
        List<java.security.cert.Certificate> certList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        ois.defaultReadObject(); // location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // process any new-style certs in the stream (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int size = ois.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // we know of 3 different cert types: X.509, PGP, SDSI, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // could all be present in the stream at the same time
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   576
            cfs = new Hashtable<>(3);
45980
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   577
            certList = new ArrayList<>(size > 20 ? 20 : size);
47418
6993f9f1ff83 8181432: Better processing of unresolved permissions
weijun
parents: 47216
diff changeset
   578
        } else if (size < 0) {
6993f9f1ff83 8181432: Better processing of unresolved permissions
weijun
parents: 47216
diff changeset
   579
            throw new IOException("size cannot be negative");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            // read the certificate type, and instantiate a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // factory of that type (reuse existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            String certType = ois.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // reuse certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                cf = cfs.get(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                // create new certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    cf = CertificateFactory.getInstance(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    throw new ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        ("Certificate factory for " + certType + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                // store the certificate factory so we can reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // parse the certificate
47418
6993f9f1ff83 8181432: Better processing of unresolved permissions
weijun
parents: 47216
diff changeset
   601
            byte[] encoded = IOUtils.readNBytes(ois, ois.readInt());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            try {
45980
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   604
                certList.add(cf.generateCertificate(bais));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                throw new IOException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
45980
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   611
        if (certList != null) {
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   612
            this.certs = certList.toArray(
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   613
                    new java.security.cert.Certificate[size]);
b2abb25204a1 8174113: Better sourcing of code
weijun
parents: 45434
diff changeset
   614
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Deserialize array of code signers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        try {
8784
29a6801dcf6f 6686215: Some mutables not defensively copied when deserializing java.security.CodeSource & Timestamp objects
vinnie
parents: 7970
diff changeset
   617
            this.signers = ((CodeSigner[])ois.readObject()).clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            // no signers present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   621
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   622
        if (location != null) {
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   623
            locationNoFragString = URLUtil.urlNoFragString(location);
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 30033
diff changeset
   624
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Convert an array of certificates to an array of code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * The array of certificates is a concatenation of certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * where the initial certificate in each chain is the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
39560
e6ec33bddfcc 8129972: Clarify the javadoc of java.security.CodeSource as to the nullability of 'location'
jnimeh
parents: 31804
diff changeset
   632
     * @return an array of code signers or null if none are generated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private CodeSigner[] convertCertArrayToSignerArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        java.security.cert.Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            // Initialize certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // Iterate through all the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            int i = 0;
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   649
            List<CodeSigner> signers = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                List<java.security.cert.Certificate> certChain =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   652
                        new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                certChain.add(certs[i++]); // first cert is an end-entity cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                int j = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                // Extract chain of certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                // (loop while certs are not end-entity certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                while (j < certs.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    certs[j] instanceof X509Certificate &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    ((X509Certificate)certs[j]).getBasicConstraints() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    certChain.add(certs[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                i = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                CertPath certPath = factory.generateCertPath(certChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                signers.add(new CodeSigner(certPath, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (signers.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                return signers.toArray(new CodeSigner[signers.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return null; //TODO - may be better to throw an ex. here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
}