jdk/src/share/classes/sun/security/x509/ExtendedKeyUsageExtension.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 2000-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.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class defines the Extended Key Usage Extension, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * indicates one or more purposes for which the certified public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * may be used, in addition to or in place of the basic purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * indicated in the key usage extension field.  This field is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * as follows:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * KeyPurposeId ::= OBJECT IDENTIFIER<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Key purposes may be defined by any organization with a need. Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * identifiers used to identify key purposes shall be assigned in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * accordance with IANA or ITU-T Rec. X.660 | ISO/IEC/ITU 9834-1.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * This extension may, at the option of the certificate issuer, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * either critical or non-critical.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * If the extension is flagged critical, then the certificate MUST be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * used only for one of the purposes indicated.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * If the extension is flagged non-critical, then it indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * intended purpose or purposes of the key, and may be used in finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the correct key/certificate of an entity that has multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * keys/certificates. It is an advisory field and does not imply that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * usage of the key is restricted by the certification authority to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the purpose indicated. Certificate using applications may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * nevertheless require that a particular purpose be indicated in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * order for the certificate to be acceptable to that application.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * If a certificate contains both a critical key usage field and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * critical extended key usage field, then both fields MUST be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * processed independently and the certificate MUST only be used for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * purpose consistent with both fields.  If there is no purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * consistent with both fields, then the certificate MUST NOT be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * for any purpose.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class ExtendedKeyUsageExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final String IDENT = "x509.info.extensions.ExtendedKeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final String NAME = "ExtendedKeyUsage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final String USAGES = "usages";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // OID defined in RFC 3280 Sections 4.2.1.13
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // more from http://www.alvestrand.no/objectid/1.3.6.1.5.5.7.3.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final Map <ObjectIdentifier, String> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new HashMap <ObjectIdentifier, String> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int[] anyExtendedKeyUsageOidData = {2, 5, 29, 37, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final int[] serverAuthOidData = {1, 3, 6, 1, 5, 5, 7, 3, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int[] clientAuthOidData = {1, 3, 6, 1, 5, 5, 7, 3, 2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int[] codeSigningOidData = {1, 3, 6, 1, 5, 5, 7, 3, 3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int[] emailProtectionOidData = {1, 3, 6, 1, 5, 5, 7, 3, 4};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final int[] ipsecEndSystemOidData = {1, 3, 6, 1, 5, 5, 7, 3, 5};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final int[] ipsecTunnelOidData = {1, 3, 6, 1, 5, 5, 7, 3, 6};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final int[] ipsecUserOidData = {1, 3, 6, 1, 5, 5, 7, 3, 7};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final int[] timeStampingOidData = {1, 3, 6, 1, 5, 5, 7, 3, 8};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int[] OCSPSigningOidData = {1, 3, 6, 1, 5, 5, 7, 3, 9};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        map.put(ObjectIdentifier.newInternal(anyExtendedKeyUsageOidData), "anyExtendedKeyUsage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        map.put(ObjectIdentifier.newInternal(serverAuthOidData), "serverAuth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        map.put(ObjectIdentifier.newInternal(clientAuthOidData), "clientAuth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        map.put(ObjectIdentifier.newInternal(codeSigningOidData), "codeSigning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        map.put(ObjectIdentifier.newInternal(emailProtectionOidData), "emailProtection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        map.put(ObjectIdentifier.newInternal(ipsecEndSystemOidData), "ipsecEndSystem");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        map.put(ObjectIdentifier.newInternal(ipsecTunnelOidData), "ipsecTunnel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        map.put(ObjectIdentifier.newInternal(ipsecUserOidData), "ipsecUser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        map.put(ObjectIdentifier.newInternal(timeStampingOidData), "timeStamping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        map.put(ObjectIdentifier.newInternal(OCSPSigningOidData), "OCSPSigning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Vector of KeyUsages for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private Vector<ObjectIdentifier> keyUsages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // Encode this extension value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (keyUsages == null || keyUsages.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        DerOutputStream os = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0; i < keyUsages.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            tmp.putOID(keyUsages.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        os.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.extensionValue = os.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Create a ExtendedKeyUsageExtension object from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * a Vector of Key Usages; the criticality is set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public ExtendedKeyUsageExtension(Vector<ObjectIdentifier> keyUsages)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this(Boolean.FALSE, keyUsages);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Create a ExtendedKeyUsageExtension object from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * a Vector of KeyUsages with specified criticality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param keyUsages the Vector of KeyUsages (ObjectIdentifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public ExtendedKeyUsageExtension(Boolean critical, Vector<ObjectIdentifier> keyUsages)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.keyUsages = keyUsages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        encodeThis();
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
     * Create the extension from its DER encoded value and criticality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public ExtendedKeyUsageExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.extensionId = PKIXExtensions.ExtendedKeyUsage_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IOException("Invalid encoding for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                   "ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        keyUsages = new Vector<ObjectIdentifier>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        while (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            DerValue seq = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ObjectIdentifier usage = seq.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            keyUsages.addElement(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Return the extension as user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (keyUsages == null) return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String usage = "  ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (ObjectIdentifier oid: keyUsages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if(!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                usage += "\n  ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            String result = map.get(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                usage += result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                usage += oid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return super.toString() + "ExtendedKeyUsages [\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
               + usage + "\n]\n";
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 DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param out the DerOutputStream 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.ExtendedKeyUsage_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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (!(obj instanceof Vector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                throw new IOException("Attribute value should be of type Vector.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            this.keyUsages = (Vector<ObjectIdentifier>)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                "CertAttrSet:ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public Object get(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            //XXXX May want to consider cloning this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return keyUsages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                "CertAttrSet:ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            keyUsages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                "CertAttrSet:ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        elements.addElement(USAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return (elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public List<String> getExtendedKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        List<String> al = new ArrayList<String>(keyUsages.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        for (ObjectIdentifier oid : keyUsages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            al.add(oid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}