jdk/src/share/classes/java/security/CodeSource.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>This class extends the concept of a codebase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * encapsulate not only the location (URL) but also the certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that were used to verify signed code originating from that location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class CodeSource implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long serialVersionUID = 4977541819976013951L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The code location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private URL location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private transient CodeSigner[] signers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The code signers. Certificate chains are concatenated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private transient java.security.cert.Certificate certs[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // cached SocketPermission used for matchLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private transient SocketPermission sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // for generating cert paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private transient CertificateFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Constructs a CodeSource and associates it with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * location and set of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param url the location (URL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param certs the certificate(s). It may be null. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * array are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public CodeSource(URL url, java.security.cert.Certificate certs[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.location = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Copy the supplied certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a CodeSource and associates it with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * location and set of code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param url the location (URL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param signers the code signers. It may be null. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * array are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public CodeSource(URL url, CodeSigner[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.location = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Copy the supplied signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.signers = signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (location != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return location.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * object. Two CodeSource objects are considered equal if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * locations are of identical value and if their signer certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * chains are of identical value. It is not required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * the certificate chains be in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return true if the objects are considered equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // objects types must be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (!(obj instanceof CodeSource))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        CodeSource cs = (CodeSource) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // URLs must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (location == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // if location is null, then cs.location must be null as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (cs.location != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // if location is not null, then it must equal cs.location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (!location.equals(cs.location)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // certs must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return matchCerts(cs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the location associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the location (URL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final URL getLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /* since URL is practically immutable, returning itself is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
           a security problem */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return this.location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns the certificates associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * If this CodeSource object was created using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * {@link #CodeSource(URL url, CodeSigner[] signers)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * constructor then its certificate chains are extracted and used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * create an array of Certificate objects. Each signer certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * followed by its supporting certificate chain (which may be empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Each signer certificate and its supporting certificate chain is ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * bottom-to-top (i.e., with the signer certificate first and the (root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * certificate authority last).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return A copy of the certificates array, or null if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public final java.security.cert.Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Convert the code signers to certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            ArrayList<java.security.cert.Certificate> certChains =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        new ArrayList<java.security.cert.Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                certChains.addAll(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    signers[i].getSignerCertPath().getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            certs = certChains.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        new java.security.cert.Certificate[certChains.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the code signers associated with this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * If this CodeSource object was created using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * {@link #CodeSource(URL url, Certificate[] certs)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * constructor then its certificate chains are extracted and used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * create an array of CodeSigner objects. Note that only X.509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * are examined - all other certificate types are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return A copy of the code signer array, or null if there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public final CodeSigner[] getCodeSigners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // Convert the certs to code signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            signers = convertCertArrayToSignerArray(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
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
     * Returns true if this CodeSource object "implies" the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * More specifically, this method makes the following checks, in order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * If any fail, it returns false. If they all succeed, it returns true.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <li> <i>codesource</i> must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <li> If this object's certificates are not null, then all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * of this object's certificates must be present in <i>codesource</i>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <li> If this object's location (getLocation()) is not null, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * following checks are made against this object's location and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <i>codesource</i>'s:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *   <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *     <li>  <i>codesource</i>'s location must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *     <li>  If this object's location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *           equals <i>codesource</i>'s location, then return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *     <li>  This object's protocol (getLocation().getProtocol()) must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *           equal to <i>codesource</i>'s protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *     <li>  If this object's host (getLocation().getHost()) is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *           then the SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *           constructed with this object's host must imply the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *           SocketPermission constructed with <i>codesource</i>'s host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *     <li>  If this object's port (getLocation().getPort()) is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *           equal to -1 (that is, if a port is specified), it must equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *           <i>codesource</i>'s port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *     <li>  If this object's file (getLocation().getFile()) doesn't equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *           <i>codesource</i>'s file, then the following checks are made:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *           If this object's file ends with "/-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *           then <i>codesource</i>'s file must start with this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *           file (exclusive the trailing "-").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *           If this object's file ends with a "/*",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *           then <i>codesource</i>'s file must start with this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *           file and must not have any further "/" separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *           If this object's file doesn't end with a "/",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *           then <i>codesource</i>'s file must match this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *           file with a '/' appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     <li>  If this object's reference (getLocation().getRef()) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *           not null, it must equal <i>codesource</i>'s reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *   </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * For example, the codesource objects with the following locations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * and null certificates all imply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the codesource with the location "http://java.sun.com/classes/foo.jar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * and null certificates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *     http:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *     http://*.sun.com/classes/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *     http://java.sun.com/classes/-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *     http://java.sun.com/classes/foo.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Note that if this CodeSource has a null location and a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * certificate chain, then it implies every other CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param codesource CodeSource to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return true if the specified codesource is implied by this codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public boolean implies(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (codesource == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return matchCerts(codesource, false) && matchLocation(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns true if all the certs in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * CodeSource are also in <i>that</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param that the CodeSource to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param strict If true then a strict equality match is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *               Otherwise a subset match is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private boolean matchCerts(CodeSource that, boolean strict)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // match any key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (certs == null && signers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (strict) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                return (that.certs == null && that.signers == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // both have signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else if (signers != null && that.signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (strict && signers.length != that.signers.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (int i = 0; i < signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                for (int j = 0; j < that.signers.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    if (signers[i].equals(that.signers[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // both have certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } else if (certs != null && that.certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (strict && certs.length != that.certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                for (int j = 0; j < that.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if (certs[i].equals(that.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Returns true if two CodeSource's have the "same" location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param that CodeSource to compare against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private boolean matchLocation(CodeSource that)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (location == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if ((that == null) || (that.location == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (location.equals(that.location))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (!location.getProtocol().equals(that.location.getProtocol()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            String thisHost = location.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            String thatHost = that.location.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (thisHost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if (("".equals(thisHost) || "localhost".equals(thisHost)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    ("".equals(thatHost) || "localhost".equals(thatHost))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                } else if (!thisHost.equals(thatHost)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    if (thatHost == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    if (this.sp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        this.sp = new SocketPermission(thisHost, "resolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    if (that.sp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        that.sp = new SocketPermission(thatHost, "resolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    if (!this.sp.implies(that.sp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (location.getPort() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (location.getPort() != that.location.getPort())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (location.getFile().endsWith("/-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // Matches the directory and (recursively) all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // and subdirectories contained in that directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // For example, "/a/b/-" implies anything that starts with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                // "/a/b/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                String thisPath = location.getFile().substring(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                                location.getFile().length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (!that.location.getFile().startsWith(thisPath))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } else if (location.getFile().endsWith("/*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // Matches the directory and all the files contained in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // For example, "/a/b/*" implies anything that starts with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // "/a/b/" but has no further slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                int last = that.location.getFile().lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (last == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                String thisPath = location.getFile().substring(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                                location.getFile().length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                String thatPath = that.location.getFile().substring(0, last+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                if (!thatPath.equals(thisPath))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // Exact matches only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                // For example, "/a/b" and "/a/b/" both imply "/a/b/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                if ((!that.location.getFile().equals(location.getFile()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                && (!that.location.getFile().equals(location.getFile()+"/"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (location.getRef() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                return location.getRef().equals(that.location.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Returns a string describing this CodeSource, telling its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * URL and certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return information about this CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        sb.append(this.location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (this.certs != null && this.certs.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            for (int i = 0; i < this.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                sb.append( " " + this.certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        } else if (this.signers != null && this.signers.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            for (int i = 0; i < this.signers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                sb.append( " " + this.signers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            sb.append(" <no signer certificates>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Writes this object out to a stream (i.e., serializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @serialData An initial <code>URL</code> is followed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <code>int</code> indicating the number of certificates to follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * (a value of "zero" denotes that there are no certificates associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * with this object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Each certificate is written out starting with a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * denoting the certificate type, followed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <code>int</code> specifying the length of the certificate encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * followed by the certificate encoding itself which is written out as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * array of bytes. Finally, if any code signers are present then the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * of code signers is serialized and written out too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private void writeObject(java.io.ObjectOutputStream oos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        oos.defaultWriteObject(); // location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // Serialize the array of certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (certs == null || certs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            oos.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            // write out the total number of certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            oos.writeInt(certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // write out each cert, including its type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                java.security.cert.Certificate cert = certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    oos.writeUTF(cert.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    byte[] encoded = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    oos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    oos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                } catch (CertificateEncodingException cee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    throw new IOException(cee.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // Serialize the array of code signers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (signers != null && signers.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            oos.writeObject(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Restores this object from a stream (i.e., deserializes it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private void readObject(java.io.ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        CertificateFactory cf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        Hashtable<String, CertificateFactory> cfs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        ois.defaultReadObject(); // location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // process any new-style certs in the stream (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int size = ois.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // we know of 3 different cert types: X.509, PGP, SDSI, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // could all be present in the stream at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            cfs = new Hashtable<String, CertificateFactory>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            this.certs = new java.security.cert.Certificate[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            // read the certificate type, and instantiate a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // factory of that type (reuse existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            String certType = ois.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                // reuse certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                cf = cfs.get(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                // create new certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    cf = CertificateFactory.getInstance(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    throw new ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        ("Certificate factory for " + certType + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                // store the certificate factory so we can reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            // parse the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                encoded = new byte[ois.readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            } catch (OutOfMemoryError oome) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                throw new IOException("Certificate too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            ois.readFully(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                this.certs[i] = cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                throw new IOException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // Deserialize array of code signers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            this.signers = (CodeSigner[])ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            // no signers present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Convert an array of certificates to an array of code signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * The array of certificates is a concatenation of certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * where the initial certificate in each chain is the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return An array of code signers or null if none are generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    private CodeSigner[] convertCertArrayToSignerArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        java.security.cert.Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // Initialize certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // Iterate through all the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            List<CodeSigner> signers = new ArrayList<CodeSigner>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                List<java.security.cert.Certificate> certChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        new ArrayList<java.security.cert.Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                certChain.add(certs[i++]); // first cert is an end-entity cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                int j = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                // Extract chain of certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                // (loop while certs are not end-entity certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                while (j < certs.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    certs[j] instanceof X509Certificate &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    ((X509Certificate)certs[j]).getBasicConstraints() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    certChain.add(certs[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                i = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                CertPath certPath = factory.generateCertPath(certChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                signers.add(new CodeSigner(certPath, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (signers.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                return signers.toArray(new CodeSigner[signers.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            return null; //TODO - may be better to throw an ex. here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
}