summaryrefslogtreecommitdiffstats
path: root/base/util/src/netscape/security/extensions/NSCertTypeExtension.java
blob: 04b3038e515aa1a6838e9db626015bfa27f0cf12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.extensions;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import java.util.Vector;

import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;
import netscape.security.util.ObjectIdentifier;
import netscape.security.x509.CertAttrSet;
import netscape.security.x509.Extension;

/**
 * NSCertTypeExtension
 * Represents Netscape Certificate Type Extension
 * 
 * <p>
 * This deprecated extension, if present, defines both the purpose (e.g., encipherment, signature, certificate signing)
 * and the application (e.g., SSL, S/Mime or Object Signing of the key contained in the certificate.
 * 
 * @author galperin
 * @version $Revision$, $Date$
 */
public class NSCertTypeExtension extends Extension implements CertAttrSet {

    /**
     *
     */
    private static final long serialVersionUID = 1856407688086284397L;

    // The object identifiers
    private static final int CertType_data[] = { 2, 16, 840, 1, 113730, 1, 1 };

    /**
     * Identifies the particular public key used to sign the certificate.
     */
    public static final ObjectIdentifier CertType_Id = new
            ObjectIdentifier(CertType_data);

    /**
     * Attribute names.
     */
    public static final String NAME = "NSCertType";
    public static final String SSL_CLIENT = "ssl_client";
    public static final String SSL_SERVER = "ssl_server";
    public static final String EMAIL = "email";
    public static final String OBJECT_SIGNING = "object_signing";
    public static final String SSL_CA = "ssl_ca";
    public static final String EMAIL_CA = "email_ca";
    public static final String OBJECT_SIGNING_CA = "object_signing_ca";

    /**
     * Attribute names.
     */
    public static final int SSL_CLIENT_BIT = 0;
    public static final int SSL_SERVER_BIT = 1;
    public static final int EMAIL_BIT = 2;
    public static final int OBJECT_SIGNING_BIT = 3;
    // 4 is reserved.
    public static final int SSL_CA_BIT = 5;
    public static final int EMAIL_CA_BIT = 6;
    public static final int OBJECT_SIGNING_CA_BIT = 7;

    public static final int NBITS = 8;

    /**
     * Identifier for this attribute, to be used with the
     * get, set, delete methods of Certificate, x509 type.
     */
    public static final String IDENT = "x509.info.extensions.NSCertType";

    // Private data members
    private byte[] mBitString;

    private static class MapEntry {
        String mName;
        int mPosition;

        MapEntry(String name, int position) {
            mName = name;
            mPosition = position;
        }
    }

    private static MapEntry[] mMapData =
        {
                new MapEntry(SSL_CLIENT, 0),
                new MapEntry(SSL_SERVER, 1),
                new MapEntry(EMAIL, 2),
                new MapEntry(OBJECT_SIGNING, 3),
                // note that bit 4 is reserved
                new MapEntry(SSL_CA, 5),
                new MapEntry(EMAIL_CA, 6),
                new MapEntry(OBJECT_SIGNING_CA, 7),
        };

    private static Vector<String> mAttributeNames = new Vector<String>();

    static {
        for (int i = 0; i < mMapData.length; ++i) {
            mAttributeNames.addElement(mMapData[i].mName);
        }
    }

    private static int getPosition(String name) throws CertificateException {
        for (int i = 0; i < mMapData.length; ++i) {
            if (name.equalsIgnoreCase(mMapData[i].mName))
                return mMapData[i].mPosition;
        }
        throw new CertificateException("Attribute name [" + name
                + "] not recognized by"
                + " CertAttrSet:NSCertType.");
    }

    // Encode this extension value
    private void encodeThis() throws IOException {
        DerOutputStream os = new DerOutputStream();

        os.putUnalignedBitString(mBitString);
        this.extensionValue = os.toByteArray();
    }

    /**
     * Check if bit is set.
     * 
     * @param position the position in the bit string to check.
     */
    public boolean isSet(int position) {
        int index = position / 8;
        byte pos = (byte) (1 << (7 - (position % 8)));

        if (mBitString.length <= index)
            return false;
        return ((mBitString[index] & pos) != 0);
    }

    /**
     * Set the bit at the specified position.
     */
    public void set(int position, boolean val) {
        int index = position / 8;
        byte pos = (byte) (1 << (7 - (position % 8)));

        if (index >= mBitString.length) {
            byte[] tmp = new byte[index + 1];

            System.arraycopy(mBitString, 0, tmp, 0, mBitString.length);
            mBitString = tmp;
        }
        if (val) {
            mBitString[index] |= pos;
        } else {
            mBitString[index] &= ~pos;
        }
    }

    /**
     * Create NSCertTypeExtension from boolean array.
     * The criticality is set to false.
     */
    public NSCertTypeExtension(boolean critical, boolean[] bits) {
        this.extensionId = CertType_Id;
        this.critical = critical;
        this.mBitString = new byte[0];

        for (int i = 0; i < bits.length && i < 8; i++) {
            set(i, bits[i]);
        }
    }

    public NSCertTypeExtension(boolean[] bits) {
        this.extensionId = CertType_Id;
        this.critical = false;
        this.mBitString = new byte[0];

        for (int i = 0; i < bits.length && i < 8; i++) {
            set(i, bits[i]);
        }
    }

    /**
     * Create a NSCertTypeExtension with the passed bit settings.
     * The criticality is set to false.
     * 
     * @param bitString the bits to be set for the extension.
     */
    public NSCertTypeExtension(boolean critical, byte[] bitString) throws IOException {
        this.mBitString = bitString;
        this.extensionId = CertType_Id;
        this.critical = critical;
        encodeThis();
    }

    public NSCertTypeExtension(byte[] bitString) throws IOException {
        this.mBitString = bitString;
        this.extensionId = CertType_Id;
        this.critical = false;
        encodeThis();
    }

    /**
     * Create the extension from the passed DER encoded value of the same.
     * 
     * @param critical true if the extension is to be treated as critical.
     * @param value Array of DER encoded bytes of the actual value.
     * @exception IOException on error.
     */
    public NSCertTypeExtension(Boolean critical, Object value)
            throws IOException {

        /**
         * Debug.trace("NSCertTypeExtension");
         * this.mBitString = new byte[1];
         * this.mBitString[0] = (byte)0x00;
         * return;
         **/

        this.extensionId = CertType_Id;
        this.critical = critical.booleanValue();
        byte[] extValue = (byte[]) ((byte[]) value).clone();

        this.extensionValue = extValue;
        DerValue val = new DerValue(extValue);

        this.mBitString = val.getUnalignedBitString().toByteArray();
    }

    /**
     * Create a default key usage.
     */
    public NSCertTypeExtension() {
        this.extensionId = CertType_Id;
        this.critical = false;
        this.mBitString = new byte[0];
        try {
            encodeThis();
        } catch (Exception e) {
        }
    }

    /**
     * Set the attribute value.
     */
    public void set(String name, Object obj) throws CertificateException {
        if (!(obj instanceof Boolean)) {
            throw new CertificateException("Attribute must be of type Boolean.");
        }
        boolean val = ((Boolean) obj).booleanValue();

        set(getPosition(name), val);
    }

    /**
     * Get the attribute value.
     */
    public Object get(String name) throws CertificateException {
        return new Boolean(isSet(getPosition(name)));
    }

    /**
     * Delete the attribute value.
     */
    public void delete(String name) throws CertificateException {
        set(getPosition(name), false);
    }

    /**
     * Returns a printable representation of the NSCertType.
     */
    public String toString() {
        String s = super.toString() + "NSCertType [\n";

        try {

            if (isSet(getPosition(SSL_CLIENT))) {
                s += "   SSL client";
            }
            if (isSet(getPosition(SSL_SERVER))) {
                s += "   SSL server";
            }

            if (isSet(getPosition(EMAIL))) {
                s += "   Email";
            }

            if (isSet(getPosition(OBJECT_SIGNING))) {
                s += "   Object Signing";
            }

            if (isSet(getPosition(SSL_CA))) {
                s += "   SSL CA";
            }

            if (isSet(getPosition(EMAIL_CA))) {
                s += "   Email CA";
            }

            if (isSet(getPosition(OBJECT_SIGNING_CA))) {
                s += "   Object Signing CA";
            }

        } catch (Exception e) {
            // this is reached only if there is a bug
            throw new IllegalArgumentException(e.getMessage());
        }

        s += "]\n";

        return (s);
    }

    /**
     * Decode the extension from the InputStream.
     * 
     * @param in the InputStream to unmarshal the contents from.
     * @exception IOException on decoding or validity errors.
     */
    public void decode(InputStream in) throws IOException {
        throw new IOException("Method not to be called directly.");
    }

    /**
     * Write the extension to the DerOutputStream.
     * 
     * @param out the DerOutputStream to write the extension to.
     * @exception IOException on encoding errors.
     */
    public void encode(OutputStream out) throws IOException {
        DerOutputStream tmp = new DerOutputStream();

        encodeThis();
        if (this.extensionValue == null) {
            this.extensionId = CertType_Id;
            this.critical = true;
        }
        super.encode(tmp);
        out.write(tmp.toByteArray());
    }

    /**
     * Return an enumeration of names of attributes existing within this
     * attribute.
     */
    public Enumeration<String> getAttributeNames() {
        return mAttributeNames.elements();
    }

    /**
     * Return the name of this attribute.
     */
    public String getName() {
        return (NAME);
    }

    public static void main(String[] argv) {
    }
}