1 /* 2 * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.security; 27 28 import java.io.*; 29 import java.security.cert.CertPath; 30 31 /** 32 * This class encapsulates information about a code signer. 33 * It is immutable. 34 * 35 * @since 1.5 36 * @author Vincent Ryan 37 */ 38 39 public final class CodeSigner implements Serializable { 40 41 @java.io.Serial 42 private static final long serialVersionUID = 6819288105193937581L; 43 44 /** 45 * The signer's certificate path. 46 * 47 * @serial 48 */ 49 private CertPath signerCertPath; 50 51 /** 52 * The signature timestamp. 53 * 54 * @serial 55 */ 56 private Timestamp timestamp; 57 58 /* 59 * Hash code for this code signer. 60 */ 61 private transient int myhash = -1; 62 63 /** 64 * Constructs a CodeSigner object. 65 * 66 * @param signerCertPath The signer's certificate path. 67 * It must not be {@code null}. 68 * @param timestamp A signature timestamp. 69 * If {@code null} then no timestamp was generated 70 * for the signature. 71 * @throws NullPointerException if {@code signerCertPath} is 72 * {@code null}. 73 */ CodeSigner(CertPath signerCertPath, Timestamp timestamp)74 public CodeSigner(CertPath signerCertPath, Timestamp timestamp) { 75 if (signerCertPath == null) { 76 throw new NullPointerException(); 77 } 78 this.signerCertPath = signerCertPath; 79 this.timestamp = timestamp; 80 } 81 82 /** 83 * Returns the signer's certificate path. 84 * 85 * @return A certificate path. 86 */ getSignerCertPath()87 public CertPath getSignerCertPath() { 88 return signerCertPath; 89 } 90 91 /** 92 * Returns the signature timestamp. 93 * 94 * @return The timestamp or {@code null} if none is present. 95 */ getTimestamp()96 public Timestamp getTimestamp() { 97 return timestamp; 98 } 99 100 /** 101 * Returns the hash code value for this code signer. 102 * The hash code is generated using the signer's certificate path and the 103 * timestamp, if present. 104 * 105 * @return a hash code value for this code signer. 106 */ hashCode()107 public int hashCode() { 108 if (myhash == -1) { 109 if (timestamp == null) { 110 myhash = signerCertPath.hashCode(); 111 } else { 112 myhash = signerCertPath.hashCode() + timestamp.hashCode(); 113 } 114 } 115 return myhash; 116 } 117 118 /** 119 * Tests for equality between the specified object and this 120 * code signer. Two code signers are considered equal if their 121 * signer certificate paths are equal and if their timestamps are equal, 122 * if present in both. 123 * 124 * @param obj the object to test for equality with this object. 125 * 126 * @return true if the objects are considered equal, false otherwise. 127 */ equals(Object obj)128 public boolean equals(Object obj) { 129 if (obj == null || (!(obj instanceof CodeSigner that))) { 130 return false; 131 } 132 133 if (this == that) { 134 return true; 135 } 136 Timestamp thatTimestamp = that.getTimestamp(); 137 if (timestamp == null) { 138 if (thatTimestamp != null) { 139 return false; 140 } 141 } else { 142 if (thatTimestamp == null || 143 (! timestamp.equals(thatTimestamp))) { 144 return false; 145 } 146 } 147 return signerCertPath.equals(that.getSignerCertPath()); 148 } 149 150 /** 151 * Returns a string describing this code signer. 152 * 153 * @return A string comprising the signer's certificate and a timestamp, 154 * if present. 155 */ toString()156 public String toString() { 157 StringBuilder sb = new StringBuilder(); 158 sb.append("("); 159 sb.append("Signer: " + signerCertPath.getCertificates().get(0)); 160 if (timestamp != null) { 161 sb.append("timestamp: " + timestamp); 162 } 163 sb.append(")"); 164 return sb.toString(); 165 } 166 167 /** 168 * Restores the state of this object from the stream, and explicitly 169 * resets hash code value to -1. 170 * 171 * @param ois the {@code ObjectInputStream} from which data is read 172 * @throws IOException if an I/O error occurs 173 * @throws ClassNotFoundException if a serialized class cannot be loaded 174 */ 175 @java.io.Serial readObject(ObjectInputStream ois)176 private void readObject(ObjectInputStream ois) 177 throws IOException, ClassNotFoundException { 178 ois.defaultReadObject(); 179 myhash = -1; 180 } 181 } 182