src/java.base/share/classes/sun/security/x509/KeyUsageExtension.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
/*
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, 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.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Represent the Key Usage Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>This extension, if present, defines the purpose (e.g., encipherment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * signature, certificate signing) of the key contained in the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The usage restriction might be employed when a multipurpose key is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * restricted (e.g., when an RSA key should be used only for signing or only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * for key encipherment).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class KeyUsageExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final String IDENT = "x509.info.extensions.KeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final String NAME = "KeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final String DIGITAL_SIGNATURE = "digital_signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final String NON_REPUDIATION = "non_repudiation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final String KEY_ENCIPHERMENT = "key_encipherment";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final String DATA_ENCIPHERMENT = "data_encipherment";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final String KEY_AGREEMENT = "key_agreement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final String KEY_CERTSIGN = "key_certsign";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final String CRL_SIGN = "crl_sign";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String ENCIPHER_ONLY = "encipher_only";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final String DECIPHER_ONLY = "decipher_only";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Private data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private boolean[] bitString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Encode this extension value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        DerOutputStream os = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        os.putTruncatedUnalignedBitString(new BitArray(this.bitString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.extensionValue = os.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Check if bit is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param position the position in the bit string to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean isSet(int position) {
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
    86
        return (position < bitString.length) &&
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
    87
                bitString[position];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Set the bit at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private void set(int position, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // enlarge bitString if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (position >= bitString.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            boolean[] tmp = new boolean[position+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.arraycopy(bitString, 0, tmp, 0, bitString.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            bitString = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        bitString[position] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public KeyUsageExtension(byte[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.bitString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            new BitArray(bitString.length*8,bitString).toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public KeyUsageExtension(boolean[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.bitString = bitString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public KeyUsageExtension(BitArray bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.bitString = bitString.toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * The DER encoded value may be wrapped in an OCTET STRING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param value an array of DER encoded bytes of the actual value (possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * wrapped in an OCTET STRING).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public KeyUsageExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * The following check should be activated again after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * the PKIX profiling work becomes standard and the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * is not a barrier to interoperability !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * if (!this.critical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         *   throw new IOException("KeyUsageExtension not marked critical,"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         *                         + " invalid profile.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        byte[] extValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (extValue[0] == DerValue.tag_OctetString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.extensionValue = new DerValue(extValue).getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            this.extensionValue = extValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.bitString = val.getUnalignedBitString().toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Create a default key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public KeyUsageExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        bitString = new boolean[0];
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
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (!(obj instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new IOException("Attribute must be of type Boolean.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        boolean val = ((Boolean)obj).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            set(0,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            set(1,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            set(2,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            set(3,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            set(4,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            set(5,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            set(6,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            set(7,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            set(8,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                + " CertAttrSet:KeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        encodeThis();
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
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   221
    public Boolean get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return Boolean.valueOf(isSet(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return Boolean.valueOf(isSet(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return Boolean.valueOf(isSet(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return Boolean.valueOf(isSet(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return Boolean.valueOf(isSet(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return Boolean.valueOf(isSet(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return Boolean.valueOf(isSet(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return Boolean.valueOf(isSet(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return Boolean.valueOf(isSet(8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                + " CertAttrSet:KeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            set(0,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            set(1,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            set(2,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            set(3,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            set(4,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            set(5,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            set(6,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            set(7,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            set(8,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                + " CertAttrSet:KeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        encodeThis();
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
     * Returns a printable representation of the KeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public String toString() {
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   279
        StringBuilder sb = new StringBuilder();
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   280
        sb.append(super.toString());
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   281
        sb.append("KeyUsage [\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   283
        if (isSet(0)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   284
            sb.append("  DigitalSignature\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   285
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   286
        if (isSet(1)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   287
            sb.append("  Non_repudiation\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   288
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   289
        if (isSet(2)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   290
            sb.append("  Key_Encipherment\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   291
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   292
        if (isSet(3)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   293
            sb.append("  Data_Encipherment\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   294
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   295
        if (isSet(4)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   296
            sb.append("  Key_Agreement\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   297
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   298
        if (isSet(5)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   299
            sb.append("  Key_CertSign\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   300
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   301
        if (isSet(6)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   302
            sb.append("  Crl_Sign\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   303
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   304
        if (isSet(7)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   305
            sb.append("  Encipher_Only\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   306
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   307
        if (isSet(8)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   308
            sb.append("  Decipher_Only\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   309
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   310
        sb.append("]\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   312
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param out the DerOutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
       DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
       if (this.extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
           this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
           this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
           encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
       super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
       out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        elements.addElement(DIGITAL_SIGNATURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        elements.addElement(NON_REPUDIATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        elements.addElement(KEY_ENCIPHERMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        elements.addElement(DATA_ENCIPHERMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        elements.addElement(KEY_AGREEMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        elements.addElement(KEY_CERTSIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        elements.addElement(CRL_SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        elements.addElement(ENCIPHER_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        elements.addElement(DECIPHER_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return (elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public boolean[] getBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return bitString.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}