jdk/src/java.base/share/classes/sun/security/x509/ExtendedKeyUsageExtension.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 30374 2abaf49910ea
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2014, 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.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
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    97
    // OID defined in RFC 5280 Sections 4.2.1.12
2
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
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   244
    @SuppressWarnings("unchecked") // Checked with instanceof
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (!(obj instanceof Vector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                throw new IOException("Attribute value should be of type Vector.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            this.keyUsages = (Vector<ObjectIdentifier>)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                "CertAttrSet:ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   262
    public Vector<ObjectIdentifier> get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            //XXXX May want to consider cloning this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return keyUsages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                "CertAttrSet:ExtendedKeyUsageExtension.");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (name.equalsIgnoreCase(USAGES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            keyUsages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
          throw new IOException("Attribute name [" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                "] not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                "CertAttrSet:ExtendedKeyUsageExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        elements.addElement(USAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return (elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public List<String> getExtendedKeyUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        List<String> al = new ArrayList<String>(keyUsages.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        for (ObjectIdentifier oid : keyUsages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            al.add(oid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}