src/java.base/share/classes/sun/security/x509/CertificateValidity.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200381: Typos in javadoc - missing verb "be" and alike Reviewed-by: lancea, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.*;
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class defines the interval for which the certificate is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class CertificateValidity implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static final String IDENT = "x509.info.validity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Sub attributes name for this CertAttrSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final String NAME = "validity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final String NOT_BEFORE = "notBefore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final String NOT_AFTER = "notAfter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long YR_2050 = 2524636800000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Private data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Date        notBefore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private Date        notAfter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Returns the first time the certificate is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Date getNotBefore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return (new Date(notBefore.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Returns the last time the certificate is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Date getNotAfter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
       return (new Date(notAfter.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Construct the class from the DerValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void construct(DerValue derVal) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (derVal.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new IOException("Invalid encoded CertificateValidity, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                  "starting sequence tag missing.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // check if UTCTime encoded or GeneralizedTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (derVal.data.available() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new IOException("No data encoded for CertificateValidity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        DerInputStream derIn = new DerInputStream(derVal.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        DerValue[] seq = derIn.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (seq.length != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IOException("Invalid encoding for CertificateValidity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (seq[0].tag == DerValue.tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            notBefore = derVal.data.getUTCTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else if (seq[0].tag == DerValue.tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            notBefore = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IOException("Invalid encoding for CertificateValidity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (seq[1].tag == DerValue.tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            notAfter = derVal.data.getUTCTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else if (seq[1].tag == DerValue.tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            notAfter = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IOException("Invalid encoding for CertificateValidity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Default constructor for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public CertificateValidity() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The default constructor for this class for the specified interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param notBefore the date and time before which the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *                   is not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param notAfter the date and time after which the certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                  not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public CertificateValidity(Date notBefore, Date notAfter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.notBefore = notBefore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.notAfter = notAfter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Create the object, decoding the values from the passed DER stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param in the DerInputStream to read the CertificateValidity from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception IOException on decoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public CertificateValidity(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        DerValue derVal = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        construct(derVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Return the validity period as user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (notBefore == null || notAfter == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return "";
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   137
        return "Validity: [From: " + notBefore +
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   138
               ",\n               To: " + notAfter + ']';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Encode the CertificateValidity period in DER form to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param out the OutputStream to marshal the contents to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception IOException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // in cases where default constructor is used check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // null values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (notBefore == null || notAfter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IOException("CertAttrSet:CertificateValidity:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                  " null values to encode.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        DerOutputStream pair = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (notBefore.getTime() < YR_2050) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            pair.putUTCTime(notBefore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            pair.putGeneralizedTime(notBefore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (notAfter.getTime() < YR_2050) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            pair.putUTCTime(notAfter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            pair.putGeneralizedTime(notAfter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        seq.write(DerValue.tag_Sequence, pair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        out.write(seq.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (!(obj instanceof Date)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new IOException("Attribute must be of type Date.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            notBefore = (Date)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            notAfter = (Date)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            "CertAttrSet: CertificateValidity.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   193
    public Date get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return (getNotBefore());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (getNotAfter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            "CertAttrSet: CertificateValidity.");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            notBefore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            notAfter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            "CertAttrSet: CertificateValidity.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        elements.addElement(NOT_BEFORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        elements.addElement(NOT_AFTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return (elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Verify that the current time is within the validity period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception CertificateExpiredException if the certificate has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * yet valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void valid()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    throws CertificateNotYetValidException, CertificateExpiredException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Date now = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        valid(now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Verify that the passed time is within the validity period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param now the Date against which to compare the validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @exception CertificateExpiredException if the certificate has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * with respect to the <code>Date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * yet valid with respect to the <code>Date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void valid(Date now)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    throws CertificateNotYetValidException, CertificateExpiredException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * we use the internal Dates rather than the passed in Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * because someone could override the Date methods after()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * and before() to do something entirely different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (notBefore.after(now)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new CertificateNotYetValidException("NotBefore: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                                      notBefore.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (notAfter.before(now)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new CertificateExpiredException("NotAfter: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                                  notAfter.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}