0N/A/*
2362N/A * Copyright (c) 1997, 2006, 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 sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.security.cert.CertificateParsingException;
0N/Aimport java.security.cert.CertificateExpiredException;
0N/Aimport java.security.cert.CertificateNotYetValidException;
0N/Aimport java.util.Date;
0N/Aimport java.util.Enumeration;
0N/A
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * This class defines the Private Key Usage Extension.
0N/A *
0N/A * <p>The Private Key Usage Period extension allows the certificate issuer
0N/A * to specify a different validity period for the private key than the
0N/A * certificate. This extension is intended for use with digital
0N/A * signature keys. This extension consists of two optional components
0N/A * notBefore and notAfter. The private key associated with the
0N/A * certificate should not be used to sign objects before or after the
0N/A * times specified by the two components, respectively.
0N/A *
0N/A * <pre>
0N/A * PrivateKeyUsagePeriod ::= SEQUENCE {
0N/A * notBefore [0] GeneralizedTime OPTIONAL,
0N/A * notAfter [1] GeneralizedTime OPTIONAL }
0N/A * </pre>
0N/A *
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A * @see Extension
0N/A * @see CertAttrSet
0N/A */
0N/Apublic class PrivateKeyUsageExtension extends Extension
0N/Aimplements CertAttrSet<String> {
0N/A /**
0N/A * Identifier for this attribute, to be used with the
0N/A * get, set, delete methods of Certificate, x509 type.
0N/A */
0N/A public static final String IDENT = "x509.info.extensions.PrivateKeyUsage";
0N/A /**
0N/A * Sub attributes name for this CertAttrSet.
0N/A */
0N/A public static final String NAME = "PrivateKeyUsage";
0N/A public static final String NOT_BEFORE = "not_before";
0N/A public static final String NOT_AFTER = "not_after";
0N/A
0N/A // Private data members
0N/A private static final byte TAG_BEFORE = 0;
0N/A private static final byte TAG_AFTER = 1;
0N/A
0N/A private Date notBefore = null;
0N/A private Date notAfter = null;
0N/A
0N/A // Encode this extension value.
0N/A private void encodeThis() throws IOException {
0N/A if (notBefore == null && notAfter == null) {
0N/A this.extensionValue = null;
0N/A return;
0N/A }
0N/A DerOutputStream seq = new DerOutputStream();
0N/A
0N/A DerOutputStream tagged = new DerOutputStream();
0N/A if (notBefore != null) {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A tmp.putGeneralizedTime(notBefore);
0N/A tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A false, TAG_BEFORE), tmp);
0N/A }
0N/A if (notAfter != null) {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A tmp.putGeneralizedTime(notAfter);
0N/A tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A false, TAG_AFTER), tmp);
0N/A }
0N/A seq.write(DerValue.tag_Sequence, tagged);
0N/A this.extensionValue = seq.toByteArray();
0N/A }
0N/A
0N/A /**
0N/A * The default constructor for PrivateKeyUsageExtension.
0N/A *
0N/A * @param notBefore the date/time before which the private key
0N/A * should not be used.
0N/A * @param notAfter the date/time after which the private key
0N/A * should not be used.
0N/A */
0N/A public PrivateKeyUsageExtension(Date notBefore, Date notAfter)
0N/A throws IOException {
0N/A this.notBefore = notBefore;
0N/A this.notAfter = notAfter;
0N/A
0N/A this.extensionId = PKIXExtensions.PrivateKeyUsage_Id;
0N/A this.critical = false;
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Create the extension from the passed DER encoded value.
0N/A *
0N/A * @param critical true if the extension is to be treated as critical.
0N/A * @param value an array of DER encoded bytes of the actual value.
0N/A * @exception ClassCastException if value is not an array of bytes
0N/A * @exception CertificateException on certificate parsing errors.
0N/A * @exception IOException on error.
0N/A */
0N/A public PrivateKeyUsageExtension(Boolean critical, Object value)
0N/A throws CertificateException, IOException {
0N/A this.extensionId = PKIXExtensions.PrivateKeyUsage_Id;
0N/A this.critical = critical.booleanValue();
0N/A
0N/A this.extensionValue = (byte[]) value;
0N/A DerInputStream str = new DerInputStream(this.extensionValue);
0N/A DerValue[] seq = str.getSequence(2);
0N/A
0N/A // NB. this is always encoded with the IMPLICIT tag
0N/A // The checks only make sense if we assume implicit tagging,
0N/A // with explicit tagging the form is always constructed.
0N/A for (int i = 0; i < seq.length; i++) {
0N/A DerValue opt = seq[i];
0N/A
0N/A if (opt.isContextSpecific(TAG_BEFORE) &&
0N/A !opt.isConstructed()) {
0N/A if (notBefore != null) {
0N/A throw new CertificateParsingException(
0N/A "Duplicate notBefore in PrivateKeyUsage.");
0N/A }
0N/A opt.resetTag(DerValue.tag_GeneralizedTime);
0N/A str = new DerInputStream(opt.toByteArray());
0N/A notBefore = str.getGeneralizedTime();
0N/A
0N/A } else if (opt.isContextSpecific(TAG_AFTER) &&
0N/A !opt.isConstructed()) {
0N/A if (notAfter != null) {
0N/A throw new CertificateParsingException(
0N/A "Duplicate notAfter in PrivateKeyUsage.");
0N/A }
0N/A opt.resetTag(DerValue.tag_GeneralizedTime);
0N/A str = new DerInputStream(opt.toByteArray());
0N/A notAfter = str.getGeneralizedTime();
0N/A } else
0N/A throw new IOException("Invalid encoding of " +
0N/A "PrivateKeyUsageExtension");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the printable string.
0N/A */
0N/A public String toString() {
0N/A return(super.toString() +
0N/A "PrivateKeyUsage: [\n" +
0N/A ((notBefore == null) ? "" : "From: " + notBefore.toString() + ", ")
0N/A + ((notAfter == null) ? "" : "To: " + notAfter.toString())
0N/A + "]\n");
0N/A }
0N/A
0N/A /**
0N/A * Verify that that the current time is within the validity period.
0N/A *
0N/A * @exception CertificateExpiredException if the certificate has expired.
0N/A * @exception CertificateNotYetValidException if the certificate is not
0N/A * yet valid.
0N/A */
0N/A public void valid()
0N/A throws CertificateNotYetValidException, CertificateExpiredException {
0N/A Date now = new Date();
0N/A valid(now);
0N/A }
0N/A
0N/A /**
0N/A * Verify that that the passed time is within the validity period.
0N/A *
0N/A * @exception CertificateExpiredException if the certificate has expired
0N/A * with respect to the <code>Date</code> supplied.
0N/A * @exception CertificateNotYetValidException if the certificate is not
0N/A * yet valid with respect to the <code>Date</code> supplied.
0N/A *
0N/A */
0N/A public void valid(Date now)
0N/A throws CertificateNotYetValidException, CertificateExpiredException {
0N/A /*
0N/A * we use the internal Dates rather than the passed in Date
0N/A * because someone could override the Date methods after()
0N/A * and before() to do something entirely different.
0N/A */
0N/A if (notBefore.after(now)) {
0N/A throw new CertificateNotYetValidException("NotBefore: " +
0N/A notBefore.toString());
0N/A }
0N/A if (notAfter.before(now)) {
0N/A throw new CertificateExpiredException("NotAfter: " +
0N/A notAfter.toString());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Write the extension to the OutputStream.
0N/A *
0N/A * @param out the OutputStream to write the extension to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encode(OutputStream out) throws IOException {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A if (extensionValue == null) {
0N/A extensionId = PKIXExtensions.PrivateKeyUsage_Id;
0N/A critical = false;
0N/A encodeThis();
0N/A }
0N/A super.encode(tmp);
0N/A out.write(tmp.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Set the attribute value.
0N/A * @exception CertificateException on attribute handling errors.
0N/A */
0N/A public void set(String name, Object obj)
0N/A throws CertificateException, IOException {
0N/A if (!(obj instanceof Date)) {
0N/A throw new CertificateException("Attribute must be of type Date.");
0N/A }
0N/A if (name.equalsIgnoreCase(NOT_BEFORE)) {
0N/A notBefore = (Date)obj;
0N/A } else if (name.equalsIgnoreCase(NOT_AFTER)) {
0N/A notAfter = (Date)obj;
0N/A } else {
0N/A throw new CertificateException("Attribute name not recognized by"
0N/A + " CertAttrSet:PrivateKeyUsage.");
0N/A }
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Get the attribute value.
0N/A * @exception CertificateException on attribute handling errors.
0N/A */
0N/A public Object get(String name) throws CertificateException {
0N/A if (name.equalsIgnoreCase(NOT_BEFORE)) {
0N/A return (new Date(notBefore.getTime()));
0N/A } else if (name.equalsIgnoreCase(NOT_AFTER)) {
0N/A return (new Date(notAfter.getTime()));
0N/A } else {
0N/A throw new CertificateException("Attribute name not recognized by"
0N/A + " CertAttrSet:PrivateKeyUsage.");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Delete the attribute value.
0N/A * @exception CertificateException on attribute handling errors.
0N/A */
0N/A public void delete(String name) throws CertificateException, IOException {
0N/A if (name.equalsIgnoreCase(NOT_BEFORE)) {
0N/A notBefore = null;
0N/A } else if (name.equalsIgnoreCase(NOT_AFTER)) {
0N/A notAfter = null;
0N/A } else {
0N/A throw new CertificateException("Attribute name not recognized by"
0N/A + " CertAttrSet:PrivateKeyUsage.");
0N/A }
0N/A encodeThis();
0N/A }
0N/A
0N/A /**
0N/A * Return an enumeration of names of attributes existing within this
0N/A * attribute.
0N/A */
0N/A public Enumeration<String> getElements() {
0N/A AttributeNameEnumeration elements = new AttributeNameEnumeration();
0N/A elements.addElement(NOT_BEFORE);
0N/A elements.addElement(NOT_AFTER);
0N/A
0N/A return(elements.elements());
0N/A }
0N/A
0N/A /**
0N/A * Return the name of this attribute.
0N/A */
0N/A public String getName() {
0N/A return(NAME);
0N/A }
0N/A}