Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import org.bouncycastle.asn1.DERBitString;
      4 
      5 /**
      6  * The KeyUsage object.
      7  * <pre>
      8  *    id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
      9  *
     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
     23     extends DERBitString
     24 {
     25     public static final int        digitalSignature = (1 << 7);
     26     public static final int        nonRepudiation   = (1 << 6);
     27     public static final int        keyEncipherment  = (1 << 5);
     28     public static final int        dataEncipherment = (1 << 4);
     29     public static final int        keyAgreement     = (1 << 3);
     30     public static final int        keyCertSign      = (1 << 2);
     31     public static final int        cRLSign          = (1 << 1);
     32     public static final int        encipherOnly     = (1 << 0);
     33     public static final int        decipherOnly     = (1 << 15);
     34 
     35     public static DERBitString getInstance(Object obj)   // needs to be DERBitString for other VMs
     36     {
     37         if (obj instanceof KeyUsage)
     38         {
     39             return (KeyUsage)obj;
     40         }
     41 
     42         if (obj instanceof X509Extension)
     43         {
     44             return new KeyUsage(DERBitString.getInstance(X509Extension.convertValueToObject((X509Extension)obj)));
     45         }
     46 
     47         return new KeyUsage(DERBitString.getInstance(obj));
     48     }
     49 
     50     /**
     51      * Basic constructor.
     52      *
     53      * @param usage - the bitwise OR of the Key Usage flags giving the
     54      * allowed uses for the key.
     55      * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
     56      */
     57     public KeyUsage(
     58         int usage)
     59     {
     60         super(getBytes(usage), getPadBits(usage));
     61     }
     62 
     63     public KeyUsage(
     64         DERBitString usage)
     65     {
     66         super(usage.getBytes(), usage.getPadBits());
     67     }
     68 
     69     public String toString()
     70     {
     71         if (data.length == 1)
     72         {
     73             return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
     74         }
     75         return "KeyUsage: 0x" + Integer.toHexString((data[1] & 0xff) << 8 | (data[0] & 0xff));
     76     }
     77 }
     78