jdk/src/share/classes/sun/security/x509/KeyUsageExtension.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return bitString[position];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Set the bit at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private void set(int position, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // enlarge bitString if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (position >= bitString.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            boolean[] tmp = new boolean[position+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.arraycopy(bitString, 0, tmp, 0, bitString.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            bitString = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        bitString[position] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public KeyUsageExtension(byte[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.bitString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new BitArray(bitString.length*8,bitString).toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public KeyUsageExtension(boolean[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.bitString = bitString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Create a KeyUsageExtension with the passed bit settings. The criticality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public KeyUsageExtension(BitArray bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.bitString = bitString.toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The DER encoded value may be wrapped in an OCTET STRING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param value an array of DER encoded bytes of the actual value (possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * wrapped in an OCTET STRING).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public KeyUsageExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * The following check should be activated again after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * the PKIX profiling work becomes standard and the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * is not a barrier to interoperability !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * if (!this.critical) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         *   throw new IOException("KeyUsageExtension not marked critical,"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         *                         + " invalid profile.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[] extValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (extValue[0] == DerValue.tag_OctetString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.extensionValue = new DerValue(extValue).getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            this.extensionValue = extValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.bitString = val.getUnalignedBitString().toBooleanArray();
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
     * Create a default key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public KeyUsageExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        extensionId = PKIXExtensions.KeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        bitString = new boolean[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (!(obj instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IOException("Attribute must be of type Boolean.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        boolean val = ((Boolean)obj).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            set(0,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            set(1,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            set(2,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            set(3,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            set(4,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            set(5,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            set(6,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            set(7,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            set(8,val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                + " CertAttrSet:KeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public Object get(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return Boolean.valueOf(isSet(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return Boolean.valueOf(isSet(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return Boolean.valueOf(isSet(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return Boolean.valueOf(isSet(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return Boolean.valueOf(isSet(4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return Boolean.valueOf(isSet(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return Boolean.valueOf(isSet(6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return Boolean.valueOf(isSet(7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return Boolean.valueOf(isSet(8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                + " CertAttrSet:KeyUsage.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
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
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (name.equalsIgnoreCase(DIGITAL_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            set(0,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else if (name.equalsIgnoreCase(NON_REPUDIATION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            set(1,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else if (name.equalsIgnoreCase(KEY_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            set(2,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else if (name.equalsIgnoreCase(DATA_ENCIPHERMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            set(3,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else if (name.equalsIgnoreCase(KEY_AGREEMENT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            set(4,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else if (name.equalsIgnoreCase(KEY_CERTSIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            set(5,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else if (name.equalsIgnoreCase(CRL_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            set(6,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else if (name.equalsIgnoreCase(ENCIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            set(7,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else if (name.equalsIgnoreCase(DECIPHER_ONLY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            set(8,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                + " CertAttrSet:KeyUsage.");
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
     * Returns a printable representation of the KeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        String s = super.toString() + "KeyUsage [\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (isSet(0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                s += "  DigitalSignature\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (isSet(1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                s += "  Non_repudiation\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (isSet(2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                s += "  Key_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (isSet(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                s += "  Data_Encipherment\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (isSet(4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                s += "  Key_Agreement\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (isSet(5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                s += "  Key_CertSign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (isSet(6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                s += "  Crl_Sign\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (isSet(7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                s += "  Encipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (isSet(8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                s += "  Decipher_Only\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (ArrayIndexOutOfBoundsException ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        s += "]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return (s);
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
}