jdk/src/java.base/share/classes/sun/security/x509/InvalidityDateExtension.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3045
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3045
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3045
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3045
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3045
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    36
 * From RFC 5280:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The invalidity date is a non-critical CRL entry extension that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * provides the date on which it is known or suspected that the private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * key was compromised or that the certificate otherwise became invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This date may be earlier than the revocation date in the CRL entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * which is the date at which the CA processed the revocation.  When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * revocation is first posted by a CRL issuer in a CRL, the invalidity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * date may precede the date of issue of earlier CRLs, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * revocation date SHOULD NOT precede the date of issue of earlier CRLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Whenever this information is available, CRL issuers are strongly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * encouraged to share it with CRL users.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The GeneralizedTime values included in this field MUST be expressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * in Greenwich Mean Time (Zulu), and MUST be specified and interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * as defined in section 4.1.2.5.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * id-ce-invalidityDate OBJECT IDENTIFIER ::= { id-ce 24 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * invalidityDate ::=  GeneralizedTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class InvalidityDateExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Attribute name and Reason codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final String NAME = "InvalidityDate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String DATE = "date";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Date date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        DerOutputStream dos = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        dos.putGeneralizedTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.extensionValue = dos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Create a InvalidityDateExtension with the passed in date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Criticality automatically set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param date the invalidity date
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public InvalidityDateExtension(Date date) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this(false, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Create a InvalidityDateExtension with the passed in date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param date the invalidity date
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public InvalidityDateExtension(boolean critical, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.extensionId = PKIXExtensions.InvalidityDate_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.critical = critical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.date = date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public InvalidityDateExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.extensionId = PKIXExtensions.InvalidityDate_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.date = val.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!(obj instanceof Date)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IOException("Attribute must be of type Date.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (name.equalsIgnoreCase(DATE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            date = (Date) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                ("Name not supported by InvalidityDateExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   141
    public Date get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (name.equalsIgnoreCase(DATE)) {
3045
cdfc2f549bbc 6850783: InvalidityDateExtension returns reference to internal mutable state
xuelei
parents: 2
diff changeset
   143
            if (date == null) {
cdfc2f549bbc 6850783: InvalidityDateExtension returns reference to internal mutable state
xuelei
parents: 2
diff changeset
   144
                return null;
cdfc2f549bbc 6850783: InvalidityDateExtension returns reference to internal mutable state
xuelei
parents: 2
diff changeset
   145
            } else {
cdfc2f549bbc 6850783: InvalidityDateExtension returns reference to internal mutable state
xuelei
parents: 2
diff changeset
   146
                return (new Date(date.getTime()));    // clone
cdfc2f549bbc 6850783: InvalidityDateExtension returns reference to internal mutable state
xuelei
parents: 2
diff changeset
   147
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ("Name not supported by InvalidityDateExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (name.equalsIgnoreCase(DATE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            date = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ("Name not supported by InvalidityDateExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns a printable representation of the Invalidity Date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return super.toString() + "    Invalidity Date: " + String.valueOf(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param out the DerOutputStream to write the extension to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception IOException on encoding errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (this.extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.extensionId = PKIXExtensions.InvalidityDate_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.critical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        elements.addElement(DATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return elements.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static InvalidityDateExtension toImpl(java.security.cert.Extension ext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (ext instanceof InvalidityDateExtension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return (InvalidityDateExtension) ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return new InvalidityDateExtension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                (Boolean.valueOf(ext.isCritical()), ext.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}