src/java.base/share/classes/sun/security/x509/PrivateKeyUsageExtension.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 47866 39db80b32b69
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
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.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateParsingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CertificateExpiredException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertificateNotYetValidException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 30649
diff changeset
    36
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class defines the Private Key Usage Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>The Private Key Usage Period extension allows the certificate issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to specify a different validity period for the private key than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * certificate. This extension is intended for use with digital
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * signature keys.  This extension consists of two optional components
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * notBefore and notAfter.  The private key associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * certificate should not be used to sign objects before or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * times specified by the two components, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * PrivateKeyUsagePeriod ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     notBefore  [0]  GeneralizedTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     notAfter   [1]  GeneralizedTime OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class PrivateKeyUsageExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final String IDENT = "x509.info.extensions.PrivateKeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Sub attributes name for this CertAttrSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final String NAME = "PrivateKeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final String NOT_BEFORE = "not_before";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final String NOT_AFTER = "not_after";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Private data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final byte TAG_BEFORE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final byte TAG_AFTER = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Date        notBefore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Date        notAfter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Encode this extension value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (notBefore == null && notAfter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        DerOutputStream tagged = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (notBefore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            tmp.putGeneralizedTime(notBefore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                 false, TAG_BEFORE), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (notAfter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            tmp.putGeneralizedTime(notAfter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                 false, TAG_AFTER), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        seq.write(DerValue.tag_Sequence, tagged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.extensionValue = seq.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The default constructor for PrivateKeyUsageExtension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param notBefore the date/time before which the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *         should not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param notAfter the date/time after which the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         should not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public PrivateKeyUsageExtension(Date notBefore, Date notAfter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.notBefore = notBefore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.notAfter = notAfter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.extensionId = PKIXExtensions.PrivateKeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.critical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Create the extension from the passed DER encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @exception CertificateException on certificate parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public PrivateKeyUsageExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.extensionId = PKIXExtensions.PrivateKeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        DerInputStream str = new DerInputStream(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        DerValue[] seq = str.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // NB. this is always encoded with the IMPLICIT tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // The checks only make sense if we assume implicit tagging,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // with explicit tagging the form is always constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (int i = 0; i < seq.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            DerValue opt = seq[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (opt.isContextSpecific(TAG_BEFORE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                !opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (notBefore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    throw new CertificateParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        "Duplicate notBefore in PrivateKeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                opt.resetTag(DerValue.tag_GeneralizedTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                str = new DerInputStream(opt.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                notBefore = str.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else if (opt.isContextSpecific(TAG_AFTER) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                       !opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (notAfter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    throw new CertificateParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        "Duplicate notAfter in PrivateKeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                opt.resetTag(DerValue.tag_GeneralizedTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                str = new DerInputStream(opt.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                notAfter = str.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                throw new IOException("Invalid encoding of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                      "PrivateKeyUsageExtension");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Return the printable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public String toString() {
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   179
        StringBuilder sb = new StringBuilder();
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   180
        sb.append(super.toString())
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   181
            .append("PrivateKeyUsage: [\n");
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   182
        if (notBefore != null) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   183
            sb.append("From: ")
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   184
                .append(notBefore);
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   185
            if (notAfter != null) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   186
                sb.append(", ");
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   187
            }
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   188
        }
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   189
        if (notAfter != null) {
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   190
            sb.append("To: ")
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   191
                .append(notAfter);
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   192
        }
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   193
        sb.append("]\n");
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   194
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
47866
39db80b32b69 8191632: Typos in comments due to duplicating words
igerasim
parents: 47216
diff changeset
   198
     * Verify that the current time is within the validity period.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception CertificateExpiredException if the certificate has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * yet valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public void valid()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    throws CertificateNotYetValidException, CertificateExpiredException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Date now = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        valid(now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
47866
39db80b32b69 8191632: Typos in comments due to duplicating words
igerasim
parents: 47216
diff changeset
   211
     * Verify that the passed time is within the validity period.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @exception CertificateExpiredException if the certificate has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * with respect to the <code>Date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception CertificateNotYetValidException if the certificate is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * yet valid with respect to the <code>Date</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void valid(Date now)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    throws CertificateNotYetValidException, CertificateExpiredException {
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 30649
diff changeset
   221
        Objects.requireNonNull(now);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * we use the internal Dates rather than the passed in Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * because someone could override the Date methods after()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * and before() to do something entirely different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 30649
diff changeset
   227
        if (notBefore != null && notBefore.after(now)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new CertificateNotYetValidException("NotBefore: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                                      notBefore.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 30649
diff changeset
   231
        if (notAfter != null && notAfter.before(now)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new CertificateExpiredException("NotAfter: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                                  notAfter.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
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
     * Write the extension to the OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param out the OutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            extensionId = PKIXExtensions.PrivateKeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            critical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception CertificateException on attribute handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void set(String name, Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (!(obj instanceof Date)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new CertificateException("Attribute must be of type Date.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            notBefore = (Date)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            notAfter = (Date)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
          throw new CertificateException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                           + " CertAttrSet:PrivateKeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception CertificateException on attribute handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   278
    public Date get(String name) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
      if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
          return (new Date(notBefore.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
      } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
          return (new Date(notAfter.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
          throw new CertificateException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                           + " CertAttrSet:PrivateKeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception CertificateException on attribute handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public void delete(String name) throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (name.equalsIgnoreCase(NOT_BEFORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            notBefore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } else if (name.equalsIgnoreCase(NOT_AFTER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            notAfter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
          throw new CertificateException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                           + " CertAttrSet:PrivateKeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        elements.addElement(NOT_BEFORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        elements.addElement(NOT_AFTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return(elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
      return(NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
}