Path: blob/master/src/java.base/share/classes/java/security/CodeSigner.java
41152 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.io.*;28import java.security.cert.CertPath;2930/**31* This class encapsulates information about a code signer.32* It is immutable.33*34* @since 1.535* @author Vincent Ryan36*/3738public final class CodeSigner implements Serializable {3940@java.io.Serial41private static final long serialVersionUID = 6819288105193937581L;4243/**44* The signer's certificate path.45*46* @serial47*/48private CertPath signerCertPath;4950/**51* The signature timestamp.52*53* @serial54*/55private Timestamp timestamp;5657/*58* Hash code for this code signer.59*/60private transient int myhash = -1;6162/**63* Constructs a CodeSigner object.64*65* @param signerCertPath The signer's certificate path.66* It must not be {@code null}.67* @param timestamp A signature timestamp.68* If {@code null} then no timestamp was generated69* for the signature.70* @throws NullPointerException if {@code signerCertPath} is71* {@code null}.72*/73public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {74if (signerCertPath == null) {75throw new NullPointerException();76}77this.signerCertPath = signerCertPath;78this.timestamp = timestamp;79}8081/**82* Returns the signer's certificate path.83*84* @return A certificate path.85*/86public CertPath getSignerCertPath() {87return signerCertPath;88}8990/**91* Returns the signature timestamp.92*93* @return The timestamp or {@code null} if none is present.94*/95public Timestamp getTimestamp() {96return timestamp;97}9899/**100* Returns the hash code value for this code signer.101* The hash code is generated using the signer's certificate path and the102* timestamp, if present.103*104* @return a hash code value for this code signer.105*/106public int hashCode() {107if (myhash == -1) {108if (timestamp == null) {109myhash = signerCertPath.hashCode();110} else {111myhash = signerCertPath.hashCode() + timestamp.hashCode();112}113}114return myhash;115}116117/**118* Tests for equality between the specified object and this119* code signer. Two code signers are considered equal if their120* signer certificate paths are equal and if their timestamps are equal,121* if present in both.122*123* @param obj the object to test for equality with this object.124*125* @return true if the objects are considered equal, false otherwise.126*/127public boolean equals(Object obj) {128if (obj == null || (!(obj instanceof CodeSigner that))) {129return false;130}131132if (this == that) {133return true;134}135Timestamp thatTimestamp = that.getTimestamp();136if (timestamp == null) {137if (thatTimestamp != null) {138return false;139}140} else {141if (thatTimestamp == null ||142(! timestamp.equals(thatTimestamp))) {143return false;144}145}146return signerCertPath.equals(that.getSignerCertPath());147}148149/**150* Returns a string describing this code signer.151*152* @return A string comprising the signer's certificate and a timestamp,153* if present.154*/155public String toString() {156StringBuilder sb = new StringBuilder();157sb.append("(");158sb.append("Signer: " + signerCertPath.getCertificates().get(0));159if (timestamp != null) {160sb.append("timestamp: " + timestamp);161}162sb.append(")");163return sb.toString();164}165166/**167* Restores the state of this object from the stream, and explicitly168* resets hash code value to -1.169*170* @param ois the {@code ObjectInputStream} from which data is read171* @throws IOException if an I/O error occurs172* @throws ClassNotFoundException if a serialized class cannot be loaded173*/174@java.io.Serial175private void readObject(ObjectInputStream ois)176throws IOException, ClassNotFoundException {177ois.defaultReadObject();178myhash = -1;179}180}181182183