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