1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.security.x509; 19 20 import java.io.IOException; 21 import org.apache.harmony.security.asn1.ASN1BitString; 22 import org.apache.harmony.security.asn1.ASN1Type; 23 24 /** 25 * Key Usage Extension (OID = 2.5.29.15). 26 * 27 * The ASN.1 definition for Key Usage Extension is: 28 * 29 * <pre> 30 * id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } 31 * 32 * KeyUsage ::= BIT STRING { 33 * digitalSignature (0), 34 * nonRepudiation (1), 35 * keyEncipherment (2), 36 * dataEncipherment (3), 37 * keyAgreement (4), 38 * keyCertSign (5), 39 * cRLSign (6), 40 * encipherOnly (7), 41 * decipherOnly (8) 42 * } 43 * </pre> 44 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt) 45 */ 46 public final class KeyUsage extends ExtensionValue { 47 48 /** The names of the usages. */ 49 private static final String[] USAGES = { 50 "digitalSignature", 51 "nonRepudiation", 52 "keyEncipherment", 53 "dataEncipherment", 54 "keyAgreement", 55 "keyCertSign", 56 "cRLSign", 57 "encipherOnly", 58 "decipherOnly", 59 }; 60 61 /** the value of extension */ 62 private final boolean[] keyUsage; 63 64 /** 65 * Creates the extension object on the base of its encoded form. 66 */ 67 public KeyUsage(byte[] encoding) throws IOException { 68 super(encoding); 69 this.keyUsage = (boolean[]) ASN1.decode(encoding); 70 } 71 72 public boolean[] getKeyUsage() { 73 return keyUsage; 74 } 75 76 @Override public byte[] getEncoded() { 77 if (encoding == null) { 78 encoding = ASN1.encode(keyUsage); 79 } 80 return encoding; 81 } 82 83 @Override public void dumpValue(StringBuilder sb, String prefix) { 84 sb.append(prefix).append("KeyUsage [\n"); 85 for (int i = 0; i < keyUsage.length; i++) { 86 if (keyUsage[i]) { 87 sb.append(prefix).append(" ").append(USAGES[i]).append('\n'); 88 } 89 } 90 sb.append(prefix).append("]\n"); 91 } 92 93 /** 94 * X.509 Extension value encoder/decoder. 95 */ 96 private static final ASN1Type ASN1 = new ASN1BitString.ASN1NamedBitList(9); 97 98 } 99