Path: blob/master/src/java.base/share/classes/sun/security/x509/InvalidityDateExtension.java
41159 views
/*1* Copyright (c) 2007, 2014, 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 sun.security.x509;2627import java.io.IOException;28import java.io.OutputStream;29import java.util.Date;30import java.util.Enumeration;3132import sun.security.util.*;3334/**35* From RFC 5280:36* <p>37* The invalidity date is a non-critical CRL entry extension that38* provides the date on which it is known or suspected that the private39* key was compromised or that the certificate otherwise became invalid.40* This date may be earlier than the revocation date in the CRL entry,41* which is the date at which the CA processed the revocation. When a42* revocation is first posted by a CRL issuer in a CRL, the invalidity43* date may precede the date of issue of earlier CRLs, but the44* revocation date SHOULD NOT precede the date of issue of earlier CRLs.45* Whenever this information is available, CRL issuers are strongly46* encouraged to share it with CRL users.47* <p>48* The GeneralizedTime values included in this field MUST be expressed49* in Greenwich Mean Time (Zulu), and MUST be specified and interpreted50* as defined in section 4.1.2.5.2.51* <pre>52* id-ce-invalidityDate OBJECT IDENTIFIER ::= { id-ce 24 }53*54* invalidityDate ::= GeneralizedTime55* </pre>56*57* @author Sean Mullan58*/59public class InvalidityDateExtension extends Extension60implements CertAttrSet<String> {6162/**63* Attribute name and Reason codes64*/65public static final String NAME = "InvalidityDate";66public static final String DATE = "date";6768private Date date;6970private void encodeThis() throws IOException {71if (date == null) {72this.extensionValue = null;73return;74}75DerOutputStream dos = new DerOutputStream();76dos.putGeneralizedTime(date);77this.extensionValue = dos.toByteArray();78}7980/**81* Create a InvalidityDateExtension with the passed in date.82* Criticality automatically set to false.83*84* @param date the invalidity date85*/86public InvalidityDateExtension(Date date) throws IOException {87this(false, date);88}8990/**91* Create a InvalidityDateExtension with the passed in date.92*93* @param critical true if the extension is to be treated as critical.94* @param date the invalidity date95*/96public InvalidityDateExtension(boolean critical, Date date)97throws IOException {98this.extensionId = PKIXExtensions.InvalidityDate_Id;99this.critical = critical;100this.date = date;101encodeThis();102}103104/**105* Create the extension from the passed DER encoded value of the same.106*107* @param critical true if the extension is to be treated as critical.108* @param value an array of DER encoded bytes of the actual value.109* @exception ClassCastException if value is not an array of bytes110* @exception IOException on error.111*/112public InvalidityDateExtension(Boolean critical, Object value)113throws IOException {114this.extensionId = PKIXExtensions.InvalidityDate_Id;115this.critical = critical.booleanValue();116this.extensionValue = (byte[]) value;117DerValue val = new DerValue(this.extensionValue);118this.date = val.getGeneralizedTime();119}120121/**122* Set the attribute value.123*/124public void set(String name, Object obj) throws IOException {125if (!(obj instanceof Date)) {126throw new IOException("Attribute must be of type Date.");127}128if (name.equalsIgnoreCase(DATE)) {129date = (Date) obj;130} else {131throw new IOException132("Name not supported by InvalidityDateExtension");133}134encodeThis();135}136137/**138* Get the attribute value.139*/140public Date get(String name) throws IOException {141if (name.equalsIgnoreCase(DATE)) {142if (date == null) {143return null;144} else {145return (new Date(date.getTime())); // clone146}147} else {148throw new IOException149("Name not supported by InvalidityDateExtension");150}151}152153/**154* Delete the attribute value.155*/156public void delete(String name) throws IOException {157if (name.equalsIgnoreCase(DATE)) {158date = null;159} else {160throw new IOException161("Name not supported by InvalidityDateExtension");162}163encodeThis();164}165166/**167* Returns a printable representation of the Invalidity Date.168*/169public String toString() {170return super.toString() + " Invalidity Date: " + String.valueOf(date);171}172173/**174* Write the extension to the DerOutputStream.175*176* @param out the DerOutputStream to write the extension to177* @exception IOException on encoding errors178*/179public void encode(OutputStream out) throws IOException {180DerOutputStream tmp = new DerOutputStream();181182if (this.extensionValue == null) {183this.extensionId = PKIXExtensions.InvalidityDate_Id;184this.critical = false;185encodeThis();186}187super.encode(tmp);188out.write(tmp.toByteArray());189}190191/**192* Return an enumeration of names of attributes existing within this193* attribute.194*/195public Enumeration<String> getElements() {196AttributeNameEnumeration elements = new AttributeNameEnumeration();197elements.addElement(DATE);198199return elements.elements();200}201202/**203* Return the name of this attribute.204*/205public String getName() {206return NAME;207}208209public static InvalidityDateExtension toImpl(java.security.cert.Extension ext)210throws IOException {211if (ext instanceof InvalidityDateExtension) {212return (InvalidityDateExtension) ext;213} else {214return new InvalidityDateExtension215(Boolean.valueOf(ext.isCritical()), ext.getValue());216}217}218}219220221