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.harmony.security.x509;
019:
020: import java.io.IOException;
021: import org.apache.harmony.security.asn1.ASN1Type;
022: import org.apache.harmony.security.asn1.ASN1BitString;
023:
024: /**
025: * Key Usage Extension (OID = 2.5.29.15).
026: *
027: * The ASN.1 definition for Key Usage Extension is:
028: *
029: * <pre>
030: * id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
031: *
032: * KeyUsage ::= BIT STRING {
033: * digitalSignature (0),
034: * nonRepudiation (1),
035: * keyEncipherment (2),
036: * dataEncipherment (3),
037: * keyAgreement (4),
038: * keyCertSign (5),
039: * cRLSign (6),
040: * encipherOnly (7),
041: * decipherOnly (8)
042: * }
043: * </pre>
044: * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt)
045: */
046: public class KeyUsage extends ExtensionValue {
047:
048: /**
049: * The names of the usages.
050: */
051: private static final String[] USAGES = { "digitalSignature", //$NON-NLS-1$
052: "nonRepudiation", //$NON-NLS-1$
053: "keyEncipherment", //$NON-NLS-1$
054: "dataEncipherment", //$NON-NLS-1$
055: "keyAgreement", //$NON-NLS-1$
056: "keyCertSign", //$NON-NLS-1$
057: "cRLSign", //$NON-NLS-1$
058: "encipherOnly", //$NON-NLS-1$
059: "decipherOnly", //$NON-NLS-1$
060: };
061:
062: // the value of extension
063: private final boolean[] keyUsage;
064:
065: /**
066: * Creates the extension object corresponding to the given key usage.
067: */
068: public KeyUsage(boolean[] keyUsage) {
069: this .keyUsage = keyUsage;
070: }
071:
072: /**
073: * Creates the extension object on the base of its encoded form.
074: */
075: public KeyUsage(byte[] encoding) throws IOException {
076: super (encoding);
077: this .keyUsage = (boolean[]) ASN1.decode(encoding);
078: }
079:
080: public boolean[] getKeyUsage() {
081: return keyUsage;
082: }
083:
084: /**
085: * Returns the encoded of the object.
086: * @return a byte array containing ASN.1 encoded form.
087: */
088: public byte[] getEncoded() {
089: if (encoding == null) {
090: encoding = ASN1.encode(keyUsage);
091: }
092: return encoding;
093: }
094:
095: /**
096: * Places the string representation of extension value
097: * into the StringBuffer object.
098: */
099: public void dumpValue(StringBuffer buffer, String prefix) {
100: buffer.append(prefix).append("KeyUsage [\n"); //$NON-NLS-1$
101: for (int i = 0; i < keyUsage.length; i++) {
102: if (keyUsage[i]) {
103: buffer.append(prefix).append(" ") //$NON-NLS-1$
104: .append(USAGES[i]).append('\n');
105: }
106: }
107: buffer.append(prefix).append("]\n"); //$NON-NLS-1$
108: }
109:
110: /**
111: * X.509 Extension value encoder/decoder.
112: */
113: private static final ASN1Type ASN1 = new ASN1BitString.ASN1NamedBitList(
114: 9);
115:
116: }
|