View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import org.apache.geronimo.util.asn1.DERBitString;
22  
23  /**
24   * The KeyUsage object.
25   * <pre>
26   *    id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
27   *
28   *    KeyUsage ::= BIT STRING {
29   *         digitalSignature        (0),
30   *         nonRepudiation          (1),
31   *         keyEncipherment         (2),
32   *         dataEncipherment        (3),
33   *         keyAgreement            (4),
34   *         keyCertSign             (5),
35   *         cRLSign                 (6),
36   *         encipherOnly            (7),
37   *         decipherOnly            (8) }
38   * </pre>
39   */
40  public class KeyUsage
41      extends DERBitString
42  {
43      public static final int        digitalSignature = (1 << 7);
44      public static final int        nonRepudiation   = (1 << 6);
45      public static final int        keyEncipherment  = (1 << 5);
46      public static final int        dataEncipherment = (1 << 4);
47      public static final int        keyAgreement     = (1 << 3);
48      public static final int        keyCertSign      = (1 << 2);
49      public static final int        cRLSign          = (1 << 1);
50      public static final int        encipherOnly     = (1 << 0);
51      public static final int        decipherOnly     = (1 << 15);
52  
53      /**
54       * Basic constructor.
55       *
56       * @param usage - the bitwise OR of the Key Usage flags giving the
57       * allowed uses for the key.
58       * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
59       */
60      public KeyUsage(
61          int usage)
62      {
63          super(getBytes(usage), getPadBits(usage));
64      }
65  
66      public KeyUsage(
67          DERBitString usage)
68      {
69          super(usage.getBytes(), usage.getPadBits());
70      }
71  
72      public String toString()
73      {
74          if (data.length == 1)
75          {
76              return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
77          }
78          return "KeyUsage: 0x" + Integer.toHexString((data[1] & 0xff) << 8 | (data[0] & 0xff));
79      }
80  }