jdk/src/share/classes/java/security/CodeSigner.java
author juh
Mon, 09 Sep 2013 10:52:56 -0700
changeset 19828 b4f91bc595fe
parent 18579 b678846778ad
child 24969 afa6934dd8e8
permissions -rw-r--r--
8024432: Fix doclint issues in java.security Reviewed-by: darcy, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 5462
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: 5462
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: 5462
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
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
7548
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
    28
import java.io.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class encapsulates information about a code signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public final class CodeSigner implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final long serialVersionUID = 6819288105193937581L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * The signer's certificate path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private CertPath signerCertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * The signature timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Timestamp timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Hash code for this code signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private transient int myhash = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a CodeSigner object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param signerCertPath The signer's certificate path.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
    66
     *                       It must not be {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param timestamp A signature timestamp.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
    68
     *                  If {@code null} then no timestamp was generated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *                  for the signature.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
    70
     * @throws NullPointerException if {@code signerCertPath} is
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
    71
     *                              {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (signerCertPath == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.signerCertPath = signerCertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.timestamp = timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns the signer's certificate path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return A certificate path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public CertPath getSignerCertPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return signerCertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the signature timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 8556
diff changeset
    93
     * @return The timestamp or {@code null} if none is present.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public Timestamp getTimestamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Returns the hash code value for this code signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The hash code is generated using the signer's certificate path and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * timestamp, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return a hash code value for this code signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (myhash == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (timestamp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                myhash = signerCertPath.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                myhash = signerCertPath.hashCode() + timestamp.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return myhash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * code signer. Two code signers are considered equal if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * signer certificate paths are equal and if their timestamps are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * if present in both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return true if the objects are considered equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (obj == null || (!(obj instanceof CodeSigner))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        CodeSigner that = (CodeSigner)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (this == that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Timestamp thatTimestamp = that.getTimestamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (timestamp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (thatTimestamp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (thatTimestamp == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                (! timestamp.equals(thatTimestamp))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return signerCertPath.equals(that.getSignerCertPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns a string describing this code signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return A string comprising the signer's certificate and a timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sb.append("Signer: " + signerCertPath.getCertificates().get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (timestamp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            sb.append("timestamp: " + timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   166
7548
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   167
    // Explicitly reset hash code value to -1
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   168
    private void readObject(ObjectInputStream ois)
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   169
        throws IOException, ClassNotFoundException {
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   170
     ois.defaultReadObject();
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   171
     myhash = -1;
1460351b32bc 6799854: CodeSigner.hashCode() does not work with serialization
vinnie
parents: 5506
diff changeset
   172
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}