CodeSigner.java revision 2362
4183N/A/*
4183N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4183N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4183N/A *
4183N/A * This code is free software; you can redistribute it and/or modify it
4183N/A * under the terms of the GNU General Public License version 2 only, as
4183N/A * published by the Free Software Foundation. Oracle designates this
4183N/A * particular file as subject to the "Classpath" exception as provided
4183N/A * by Oracle in the LICENSE file that accompanied this code.
4183N/A *
4183N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4183N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4183N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4183N/A * version 2 for more details (a copy is included in the LICENSE file that
4183N/A * accompanied this code).
4183N/A *
4183N/A * You should have received a copy of the GNU General Public License version
4183N/A * 2 along with this work; if not, write to the Free Software Foundation,
4183N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4183N/A *
4183N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4183N/A * or visit www.oracle.com if you need additional information or have any
4183N/A * questions.
4183N/A */
4183N/A
4183N/Apackage java.security;
4183N/A
4279N/Aimport java.io.Serializable;
4279N/Aimport java.security.cert.CRL;
4279N/Aimport java.security.cert.CertPath;
4279N/Aimport sun.misc.JavaSecurityCodeSignerAccess;
4183N/Aimport sun.misc.SharedSecrets;
4183N/A
4183N/A/**
4183N/A * This class encapsulates information about a code signer.
4183N/A * It is immutable.
4183N/A *
4183N/A * @since 1.5
4183N/A * @author Vincent Ryan
4183N/A */
4183N/A
4183N/Apublic final class CodeSigner implements Serializable {
4183N/A
4183N/A private static final long serialVersionUID = 6819288105193937581L;
4183N/A
4183N/A /**
4183N/A * The signer's certificate path.
4183N/A *
4183N/A * @serial
4183N/A */
4183N/A private CertPath signerCertPath;
4183N/A
4183N/A /*
4183N/A * The signature timestamp.
4183N/A *
4183N/A * @serial
4183N/A */
4183N/A private Timestamp timestamp;
4183N/A
4183N/A /*
4183N/A * Hash code for this code signer.
4183N/A */
4183N/A private transient int myhash = -1;
4183N/A
4183N/A /**
4183N/A * Constructs a CodeSigner object.
4183N/A *
4183N/A * @param signerCertPath The signer's certificate path.
4183N/A * It must not be <code>null</code>.
4183N/A * @param timestamp A signature timestamp.
4183N/A * If <code>null</code> then no timestamp was generated
4183N/A * for the signature.
4183N/A * @throws NullPointerException if <code>signerCertPath</code> is
4183N/A * <code>null</code>.
4183N/A */
4183N/A public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {
4183N/A if (signerCertPath == null) {
4183N/A throw new NullPointerException();
4183N/A }
4183N/A this.signerCertPath = signerCertPath;
4183N/A this.timestamp = timestamp;
4183N/A }
4183N/A
4183N/A /**
4183N/A * Returns the signer's certificate path.
4183N/A *
4183N/A * @return A certificate path.
4183N/A */
4183N/A public CertPath getSignerCertPath() {
4183N/A return signerCertPath;
4183N/A }
4183N/A
4183N/A /**
4183N/A * Returns the signature timestamp.
4183N/A *
4183N/A * @return The timestamp or <code>null</code> if none is present.
4183N/A */
4183N/A public Timestamp getTimestamp() {
4183N/A return timestamp;
4183N/A }
4183N/A
4183N/A /**
4183N/A * Returns the hash code value for this code signer.
4183N/A * The hash code is generated using the signer's certificate path and the
4183N/A * timestamp, if present.
4183N/A *
4183N/A * @return a hash code value for this code signer.
4183N/A */
4183N/A public int hashCode() {
4183N/A if (myhash == -1) {
4183N/A if (timestamp == null) {
4183N/A myhash = signerCertPath.hashCode();
4183N/A } else {
4183N/A myhash = signerCertPath.hashCode() + timestamp.hashCode();
4183N/A }
4183N/A }
4183N/A return myhash;
4183N/A }
4183N/A
4183N/A /**
4183N/A * Tests for equality between the specified object and this
4183N/A * code signer. Two code signers are considered equal if their
4183N/A * signer certificate paths are equal and if their timestamps are equal,
4183N/A * if present in both.
4183N/A *
4183N/A * @param obj the object to test for equality with this object.
4183N/A *
4183N/A * @return true if the objects are considered equal, false otherwise.
4183N/A */
4183N/A public boolean equals(Object obj) {
4183N/A if (obj == null || (!(obj instanceof CodeSigner))) {
4183N/A return false;
4183N/A }
4183N/A CodeSigner that = (CodeSigner)obj;
4183N/A
4183N/A if (this == that) {
4183N/A return true;
4183N/A }
4183N/A Timestamp thatTimestamp = that.getTimestamp();
4183N/A if (timestamp == null) {
4183N/A if (thatTimestamp != null) {
4183N/A return false;
4183N/A }
4183N/A } else {
4183N/A if (thatTimestamp == null ||
4183N/A (! timestamp.equals(thatTimestamp))) {
4183N/A return false;
4183N/A }
4183N/A }
4183N/A return signerCertPath.equals(that.getSignerCertPath());
4183N/A }
4183N/A
4183N/A /**
4183N/A * Returns a string describing this code signer.
4183N/A *
4183N/A * @return A string comprising the signer's certificate and a timestamp,
4183N/A * if present.
4183N/A */
4183N/A public String toString() {
4183N/A StringBuffer sb = new StringBuffer();
4183N/A sb.append("(");
4183N/A sb.append("Signer: " + signerCertPath.getCertificates().get(0));
4183N/A if (timestamp != null) {
4183N/A sb.append("timestamp: " + timestamp);
4183N/A }
4183N/A sb.append(")");
4183N/A return sb.toString();
4183N/A }
4183N/A
4183N/A // A private attribute attached to this CodeSigner object. Can be accessed
4183N/A // through SharedSecrets.getJavaSecurityCodeSignerAccess().[g|s]etCRLs
4183N/A //
4183N/A // Currently called in SignatureFileVerifier.getSigners
4183N/A private transient CRL[] crls;
4183N/A
4183N/A /**
4183N/A * Sets the CRLs attached
4183N/A * @param crls, null to clear
4183N/A */
4183N/A void setCRLs(CRL[] crls) {
4183N/A this.crls = crls;
4183N/A }
4183N/A
4183N/A /**
4183N/A * Returns the CRLs attached
4183N/A * @return the crls, initially null
4183N/A */
4183N/A CRL[] getCRLs() {
4183N/A return crls;
4183N/A }
4183N/A
4183N/A // Set up JavaSecurityCodeSignerAccess in SharedSecrets
4183N/A static {
4183N/A SharedSecrets.setJavaSecurityCodeSignerAccess(
4183N/A new JavaSecurityCodeSignerAccess() {
4183N/A @Override
4183N/A public void setCRLs(CodeSigner signer, CRL[] crls) {
4183N/A signer.setCRLs(crls);
4183N/A }
4183N/A
4183N/A @Override
4183N/A public CRL[] getCRLs(CodeSigner signer) {
4183N/A return signer.getCRLs();
4183N/A }
4183N/A });
4183N/A }
4183N/A
4183N/A}
4183N/A