001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import org.apache.geronimo.util.asn1.DERBitString;
021    
022    /**
023     * The KeyUsage object.
024     * <pre>
025     *    id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
026     *
027     *    KeyUsage ::= BIT STRING {
028     *         digitalSignature        (0),
029     *         nonRepudiation          (1),
030     *         keyEncipherment         (2),
031     *         dataEncipherment        (3),
032     *         keyAgreement            (4),
033     *         keyCertSign             (5),
034     *         cRLSign                 (6),
035     *         encipherOnly            (7),
036     *         decipherOnly            (8) }
037     * </pre>
038     */
039    public class KeyUsage
040        extends DERBitString
041    {
042        public static final int        digitalSignature = (1 << 7);
043        public static final int        nonRepudiation   = (1 << 6);
044        public static final int        keyEncipherment  = (1 << 5);
045        public static final int        dataEncipherment = (1 << 4);
046        public static final int        keyAgreement     = (1 << 3);
047        public static final int        keyCertSign      = (1 << 2);
048        public static final int        cRLSign          = (1 << 1);
049        public static final int        encipherOnly     = (1 << 0);
050        public static final int        decipherOnly     = (1 << 15);
051    
052        /**
053         * Basic constructor.
054         *
055         * @param usage - the bitwise OR of the Key Usage flags giving the
056         * allowed uses for the key.
057         * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
058         */
059        public KeyUsage(
060            int usage)
061        {
062            super(getBytes(usage), getPadBits(usage));
063        }
064    
065        public KeyUsage(
066            DERBitString usage)
067        {
068            super(usage.getBytes(), usage.getPadBits());
069        }
070    
071        public String toString()
072        {
073            if (data.length == 1)
074            {
075                return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
076            }
077            return "KeyUsage: 0x" + Integer.toHexString((data[1] & 0xff) << 8 | (data[0] & 0xff));
078        }
079    }