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