01: package org.bouncycastle.asn1.x509;
02:
03: import org.bouncycastle.asn1.DERBitString;
04:
05: /**
06: * The KeyUsage object.
07: * <pre>
08: * id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
09: *
10: * KeyUsage ::= BIT STRING {
11: * digitalSignature (0),
12: * nonRepudiation (1),
13: * keyEncipherment (2),
14: * dataEncipherment (3),
15: * keyAgreement (4),
16: * keyCertSign (5),
17: * cRLSign (6),
18: * encipherOnly (7),
19: * decipherOnly (8) }
20: * </pre>
21: */
22: public class KeyUsage extends DERBitString {
23: public static final int digitalSignature = (1 << 7);
24: public static final int nonRepudiation = (1 << 6);
25: public static final int keyEncipherment = (1 << 5);
26: public static final int dataEncipherment = (1 << 4);
27: public static final int keyAgreement = (1 << 3);
28: public static final int keyCertSign = (1 << 2);
29: public static final int cRLSign = (1 << 1);
30: public static final int encipherOnly = (1 << 0);
31: public static final int decipherOnly = (1 << 15);
32:
33: public static DERBitString getInstance(Object obj) // needs to be DERBitString for other VMs
34: {
35: if (obj instanceof KeyUsage) {
36: return (KeyUsage) obj;
37: }
38:
39: if (obj instanceof X509Extension) {
40: return new KeyUsage(DERBitString.getInstance(X509Extension
41: .convertValueToObject((X509Extension) obj)));
42: }
43:
44: return new KeyUsage(DERBitString.getInstance(obj));
45: }
46:
47: /**
48: * Basic constructor.
49: *
50: * @param usage - the bitwise OR of the Key Usage flags giving the
51: * allowed uses for the key.
52: * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
53: */
54: public KeyUsage(int usage) {
55: super (getBytes(usage), getPadBits(usage));
56: }
57:
58: public KeyUsage(DERBitString usage) {
59: super (usage.getBytes(), usage.getPadBits());
60: }
61:
62: public String toString() {
63: if (data.length == 1) {
64: return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
65: }
66: return "KeyUsage: 0x"
67: + Integer.toHexString((data[1] & 0xff) << 8
68: | (data[0] & 0xff));
69: }
70: }
|