src/java.base/share/classes/sun/security/x509/NetscapeCertTypeExtension.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200381: Typos in javadoc - missing verb "be" and alike Reviewed-by: lancea, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
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
 * Represents Netscape Certificate Type Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The details are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <a href=http://www.netscape.com/eng/security/comm4-cert-exts.html>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * here </a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>This extension, if present, defines both the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * (e.g., encipherment, signature, certificate signing) and the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (e.g., SSL, S/Mime or Object Signing of the key contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * certificate. This extension has been superseded by IETF PKIX extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * but is provided here for compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class NetscapeCertTypeExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final String IDENT = "x509.info.extensions.NetscapeCertType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final String NAME = "NetscapeCertType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final String SSL_CLIENT = "ssl_client";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final String SSL_SERVER = "ssl_server";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final String S_MIME = "s_mime";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String OBJECT_SIGNING = "object_signing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final String SSL_CA = "ssl_ca";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final String S_MIME_CA = "s_mime_ca";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final String OBJECT_SIGNING_CA = "object_signing_ca";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 29917
diff changeset
    72
    private static final int[] CertType_data = { 2, 16, 840, 1, 113730, 1, 1 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Object identifier for the Netscape-Cert-Type extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static ObjectIdentifier NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            NetscapeCertType_Id = new ObjectIdentifier(CertType_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean[] bitString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static class MapEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String mName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int mPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        MapEntry(String name, int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            mName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            mPosition = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static MapEntry[] mMapData = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        new MapEntry(SSL_CLIENT, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        new MapEntry(SSL_SERVER, 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        new MapEntry(S_MIME, 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        new MapEntry(OBJECT_SIGNING, 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // note that bit 4 is reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        new MapEntry(SSL_CA, 5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        new MapEntry(S_MIME_CA, 6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new MapEntry(OBJECT_SIGNING_CA, 7),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final Vector<String> mAttributeNames = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (MapEntry entry : mMapData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            mAttributeNames.add(entry.mName);
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
    private static int getPosition(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = 0; i < mMapData.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (name.equalsIgnoreCase(mMapData[i].mName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return mMapData[i].mPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throw new IOException("Attribute name [" + name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                             + "] not recognized by CertAttrSet:NetscapeCertType.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // Encode this extension value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        DerOutputStream os = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        os.putTruncatedUnalignedBitString(new BitArray(this.bitString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.extensionValue = os.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Check if bit is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param position the position in the bit string to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean isSet(int position) {
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   139
        return (position < bitString.length) &&
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   140
                bitString[position];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Set the bit at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private void set(int position, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // enlarge bitString if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (position >= bitString.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            boolean[] tmp = new boolean[position+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            System.arraycopy(bitString, 0, tmp, 0, bitString.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            bitString = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        bitString[position] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Create a NetscapeCertTypeExtension with the passed bit settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The criticality is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public NetscapeCertTypeExtension(byte[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        this.bitString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            new BitArray(bitString.length*8, bitString).toBooleanArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.extensionId = NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Create a NetscapeCertTypeExtension with the passed bit settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * The criticality is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param bitString the bits to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public NetscapeCertTypeExtension(boolean[] bitString) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.bitString = bitString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.extensionId = NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public NetscapeCertTypeExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.extensionId = NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.bitString = val.getUnalignedBitString().toBooleanArray();
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
     * Create a default key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public NetscapeCertTypeExtension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        extensionId = NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        bitString = new boolean[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (!(obj instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IOException("Attribute must be of type Boolean.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        boolean val = ((Boolean)obj).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        set(getPosition(name), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   224
    public Boolean get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return Boolean.valueOf(isSet(getPosition(name)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        set(getPosition(name), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns a printable representation of the NetscapeCertType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public String toString() {
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   240
        StringBuilder sb = new StringBuilder();
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   241
        sb.append(super.toString());
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   242
        sb.append("NetscapeCertType [\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   244
        if (isSet(0)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   245
            sb.append("   SSL client\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   246
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   247
        if (isSet(1)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   248
            sb.append("   SSL server\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   249
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   250
        if (isSet(2)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   251
            sb.append("   S/MIME\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   252
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   253
        if (isSet(3)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   254
            sb.append("   Object Signing\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   255
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   256
        if (isSet(5)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   257
            sb.append("   SSL CA\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   258
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   259
        if (isSet(6)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   260
            sb.append("   S/MIME CA\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   261
        }
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   262
        if (isSet(7)) {
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   263
            sb.append("   Object Signing CA");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   264
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
29917
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   266
        sb.append("]\n");
5edfc7a4ac68 8068720: Better certificate options checking
igerasim
parents: 25859
diff changeset
   267
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param out the DerOutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (this.extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.extensionId = NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.critical = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return mAttributeNames.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Get a boolean array representing the bits of this extension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * as it maps to the KeyUsage extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return the bit values of this extension mapped to the bit values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * of the KeyUsage extension as an array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public boolean[] getKeyUsageMappedBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        KeyUsageExtension keyUsage = new KeyUsageExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        Boolean val = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (isSet(getPosition(SSL_CLIENT)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                isSet(getPosition(S_MIME)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                isSet(getPosition(OBJECT_SIGNING)))
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   317
                keyUsage.set(KeyUsageExtension.DIGITAL_SIGNATURE, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (isSet(getPosition(SSL_SERVER)))
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   320
                keyUsage.set(KeyUsageExtension.KEY_ENCIPHERMENT, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (isSet(getPosition(SSL_CA)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                isSet(getPosition(S_MIME_CA)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                isSet(getPosition(OBJECT_SIGNING_CA)))
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   325
                keyUsage.set(KeyUsageExtension.KEY_CERTSIGN, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return keyUsage.getBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}