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 18package org.apache.harmony.security.x509; 19 20import java.io.IOException; 21import java.util.Iterator; 22import java.util.List; 23import org.apache.harmony.security.asn1.ASN1Type; 24import org.apache.harmony.security.asn1.ASN1SequenceOf; 25import org.apache.harmony.security.asn1.ASN1Oid; 26import org.apache.harmony.security.asn1.BerInputStream; 27import org.apache.harmony.security.asn1.ObjectIdentifier; 28 29/** 30 * Extended Key Usage Extension (OID == 2.5.29.37). 31 * 32 * The ASN.1 definition for Extended Key Usage Extension is: 33 * 34 * <pre> 35 * id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } 36 * 37 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId 38 * 39 * KeyPurposeId ::= OBJECT IDENTIFIER 40 * </pre> 41 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt 42 */ 43public class ExtendedKeyUsage extends ExtensionValue { 44 45 // the value of extension 46 private List keys; 47 48 /** 49 * Creates an object on the base of list of integer arrays representing 50 * key purpose IDs. 51 */ 52 public ExtendedKeyUsage(List keys) { 53 this.keys = keys; 54 } 55 56 /** 57 * Creates the extension object on the base of its encoded form. 58 */ 59 public ExtendedKeyUsage(byte[] encoding) { 60 super(encoding); 61 } 62 63 /** 64 * Returns the list of string representation of OIDs corresponding 65 * to key purpose IDs. 66 */ 67 public List getExtendedKeyUsage() throws IOException { 68 if (keys == null) { 69 keys = (List) ASN1.decode(getEncoded()); 70 } 71 return keys; 72 } 73 74 /** 75 * Returns the encoded form of the object. 76 */ 77 public byte[] getEncoded() { 78 if (encoding == null) { 79 encoding = ASN1.encode(keys); 80 } 81 return encoding; 82 } 83 84 /** 85 * Places the string representation of extension value 86 * into the StringBuffer object. 87 */ 88 public void dumpValue(StringBuffer buffer, String prefix) { 89 buffer.append(prefix).append("Extended Key Usage: "); //$NON-NLS-1$ 90 if (keys == null) { 91 try { 92 keys = getExtendedKeyUsage(); 93 } catch (IOException e) { 94 // incorrect extension value encoding 95 super.dumpValue(buffer); 96 return; 97 } 98 } 99 buffer.append('['); 100 for (Iterator it=keys.iterator(); it.hasNext();) { 101 buffer.append(" \"").append(it.next()).append('"'); //$NON-NLS-1$ 102 if (it.hasNext()) { 103 buffer.append(','); 104 } 105 } 106 buffer.append(" ]\n"); //$NON-NLS-1$ 107 } 108 109 /** 110 * ASN.1 Encoder/Decoder. 111 */ 112 public static final ASN1Type ASN1 = 113 new ASN1SequenceOf(new ASN1Oid() { 114 115 public Object getDecodedObject(BerInputStream in) 116 throws IOException { 117 int[] oid = (int[]) super.getDecodedObject(in); 118 return ObjectIdentifier.toString(oid); 119 } 120 121 }); 122} 123