0N/A/*
2938N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security.cert;
0N/A
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Date;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport java.util.Map.Entry;
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/Aimport sun.security.util.ObjectIdentifier;
0N/Aimport sun.security.x509.InvalidityDateExtension;
0N/A
0N/A/**
0N/A * An exception that indicates an X.509 certificate is revoked. A
0N/A * <code>CertificateRevokedException</code> contains additional information
0N/A * about the revoked certificate, such as the date on which the
0N/A * certificate was revoked and the reason it was revoked.
0N/A *
0N/A * @author Sean Mullan
0N/A * @since 1.7
0N/A * @see CertPathValidatorException
0N/A */
0N/Apublic class CertificateRevokedException extends CertificateException {
0N/A
0N/A private static final long serialVersionUID = 7839996631571608627L;
0N/A
0N/A /**
0N/A * @serial the date on which the certificate was revoked
0N/A */
0N/A private Date revocationDate;
0N/A /**
0N/A * @serial the revocation reason
0N/A */
0N/A private final CRLReason reason;
0N/A /**
2938N/A * @serial the <code>X500Principal</code> that represents the name of the
2938N/A * authority that signed the certificate's revocation status information
0N/A */
0N/A private final X500Principal authority;
0N/A
0N/A private transient Map<String, Extension> extensions;
0N/A
0N/A /**
0N/A * Constructs a <code>CertificateRevokedException</code> with
0N/A * the specified revocation date, reason code, authority name, and map
0N/A * of extensions.
0N/A *
0N/A * @param revocationDate the date on which the certificate was revoked. The
0N/A * date is copied to protect against subsequent modification.
0N/A * @param reason the revocation reason
0N/A * @param extensions a map of X.509 Extensions. Each key is an OID String
0N/A * that maps to the corresponding Extension. The map is copied to
0N/A * prevent subsequent modification.
2938N/A * @param authority the <code>X500Principal</code> that represents the name
2938N/A * of the authority that signed the certificate's revocation status
2938N/A * information
0N/A * @throws NullPointerException if <code>revocationDate</code>,
0N/A * <code>reason</code>, <code>authority</code>, or
0N/A * <code>extensions</code> is <code>null</code>
0N/A */
0N/A public CertificateRevokedException(Date revocationDate, CRLReason reason,
0N/A X500Principal authority, Map<String, Extension> extensions) {
0N/A if (revocationDate == null || reason == null || authority == null ||
0N/A extensions == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A this.revocationDate = new Date(revocationDate.getTime());
0N/A this.reason = reason;
0N/A this.authority = authority;
0N/A this.extensions = new HashMap(extensions);
0N/A }
0N/A
0N/A /**
0N/A * Returns the date on which the certificate was revoked. A new copy is
0N/A * returned each time the method is invoked to protect against subsequent
0N/A * modification.
0N/A *
0N/A * @return the revocation date
0N/A */
0N/A public Date getRevocationDate() {
0N/A return (Date) revocationDate.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the reason the certificate was revoked.
0N/A *
0N/A * @return the revocation reason
0N/A */
0N/A public CRLReason getRevocationReason() {
0N/A return reason;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the authority that signed the certificate's
0N/A * revocation status information.
0N/A *
2938N/A * @return the <code>X500Principal</code> that represents the name of the
2938N/A * authority that signed the certificate's revocation status information
0N/A */
0N/A public X500Principal getAuthorityName() {
0N/A return authority;
0N/A }
0N/A
0N/A /**
0N/A * Returns the invalidity date, as specifed in the Invalidity Date
0N/A * extension of this <code>CertificateRevokedException</code>. The
0N/A * invalidity date is the date on which it is known or suspected that the
0N/A * private key was compromised or that the certificate otherwise became
0N/A * invalid. This implementation calls <code>getExtensions()</code> and
0N/A * checks the returned map for an entry for the Invalidity Date extension
0N/A * OID ("2.5.29.24"). If found, it returns the invalidity date in the
0N/A * extension; otherwise null. A new Date object is returned each time the
0N/A * method is invoked to protect against subsequent modification.
0N/A *
0N/A * @return the invalidity date, or <code>null</code> if not specified
0N/A */
0N/A public Date getInvalidityDate() {
0N/A Extension ext = getExtensions().get("2.5.29.24");
0N/A if (ext == null) {
0N/A return null;
0N/A } else {
0N/A try {
0N/A Date invalidity =
0N/A (Date) InvalidityDateExtension.toImpl(ext).get("DATE");
0N/A return new Date(invalidity.getTime());
0N/A } catch (IOException ioe) {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a map of X.509 extensions containing additional information
0N/A * about the revoked certificate, such as the Invalidity Date
0N/A * Extension. Each key is an OID String that maps to the corresponding
0N/A * Extension.
0N/A *
0N/A * @return an unmodifiable map of X.509 extensions, or an empty map
0N/A * if there are no extensions
0N/A */
0N/A public Map<String, Extension> getExtensions() {
0N/A return Collections.unmodifiableMap(extensions);
0N/A }
0N/A
0N/A @Override
0N/A public String getMessage() {
0N/A return "Certificate has been revoked, reason: "
0N/A + reason + ", revocation date: " + revocationDate
0N/A + ", authority: " + authority + ", extensions: " + extensions;
0N/A }
0N/A
0N/A /**
0N/A * Serialize this <code>CertificateRevokedException</code> instance.
0N/A *
0N/A * @serialData the size of the extensions map (int), followed by all of
0N/A * the extensions in the map, in no particular order. For each extension,
0N/A * the following data is emitted: the OID String (Object), the criticality
0N/A * flag (boolean), the length of the encoded extension value byte array
0N/A * (int), and the encoded extension value bytes.
0N/A */
0N/A private void writeObject(ObjectOutputStream oos) throws IOException {
0N/A // Write out the non-transient fields
0N/A // (revocationDate, reason, authority)
0N/A oos.defaultWriteObject();
0N/A
0N/A // Write out the size (number of mappings) of the extensions map
0N/A oos.writeInt(extensions.size());
0N/A
0N/A // For each extension in the map, the following are emitted (in order):
0N/A // the OID String (Object), the criticality flag (boolean), the length
0N/A // of the encoded extension value byte array (int), and the encoded
0N/A // extension value byte array. The extensions themselves are emitted
0N/A // in no particular order.
0N/A for (Map.Entry<String, Extension> entry : extensions.entrySet()) {
0N/A Extension ext = entry.getValue();
0N/A oos.writeObject(ext.getId());
0N/A oos.writeBoolean(ext.isCritical());
0N/A byte[] extVal = ext.getValue();
0N/A oos.writeInt(extVal.length);
0N/A oos.write(extVal);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Deserialize the <code>CertificateRevokedException</code> instance.
0N/A */
0N/A private void readObject(ObjectInputStream ois)
0N/A throws IOException, ClassNotFoundException {
0N/A // Read in the non-transient fields
0N/A // (revocationDate, reason, authority)
0N/A ois.defaultReadObject();
0N/A
0N/A // Defensively copy the revocation date
0N/A revocationDate = new Date(revocationDate.getTime());
0N/A
0N/A // Read in the size (number of mappings) of the extensions map
0N/A // and create the extensions map
0N/A int size = ois.readInt();
0N/A if (size == 0) {
0N/A extensions = Collections.emptyMap();
0N/A } else {
0N/A extensions = new HashMap<String, Extension>(size);
0N/A }
0N/A
0N/A // Read in the extensions and put the mappings in the extensions map
0N/A for (int i = 0; i < size; i++) {
0N/A String oid = (String) ois.readObject();
0N/A boolean critical = ois.readBoolean();
0N/A int length = ois.readInt();
0N/A byte[] extVal = new byte[length];
0N/A ois.readFully(extVal);
0N/A Extension ext = sun.security.x509.Extension.newExtension
0N/A (new ObjectIdentifier(oid), critical, extVal);
0N/A extensions.put(oid, ext);
0N/A }
0N/A }
0N/A}