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 java.util.Iterator;
022: import java.util.List;
023: import org.apache.harmony.security.asn1.ASN1Type;
024: import org.apache.harmony.security.asn1.ASN1SequenceOf;
025: import org.apache.harmony.security.asn1.ASN1Oid;
026: import org.apache.harmony.security.asn1.BerInputStream;
027: import org.apache.harmony.security.asn1.ObjectIdentifier;
028:
029: /**
030: * Extended Key Usage Extension (OID == 2.5.29.37).
031: *
032: * The ASN.1 definition for Extended Key Usage Extension is:
033: *
034: * <pre>
035: * id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 }
036: *
037: * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
038: *
039: * KeyPurposeId ::= OBJECT IDENTIFIER
040: * </pre>
041: * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt
042: */
043: public class ExtendedKeyUsage extends ExtensionValue {
044:
045: // the value of extension
046: private List keys;
047:
048: /**
049: * Creates an object on the base of list of integer arrays representing
050: * key purpose IDs.
051: */
052: public ExtendedKeyUsage(List keys) {
053: this .keys = keys;
054: }
055:
056: /**
057: * Creates the extension object on the base of its encoded form.
058: */
059: public ExtendedKeyUsage(byte[] encoding) {
060: super (encoding);
061: }
062:
063: /**
064: * Returns the list of string representation of OIDs corresponding
065: * to key purpose IDs.
066: */
067: public List getExtendedKeyUsage() throws IOException {
068: if (keys == null) {
069: keys = (List) ASN1.decode(getEncoded());
070: }
071: return keys;
072: }
073:
074: /**
075: * Returns the encoded form of the object.
076: */
077: public byte[] getEncoded() {
078: if (encoding == null) {
079: encoding = ASN1.encode(keys);
080: }
081: return encoding;
082: }
083:
084: /**
085: * Places the string representation of extension value
086: * into the StringBuffer object.
087: */
088: public void dumpValue(StringBuffer buffer, String prefix) {
089: buffer.append(prefix).append("Extended Key Usage: "); //$NON-NLS-1$
090: if (keys == null) {
091: try {
092: keys = getExtendedKeyUsage();
093: } catch (IOException e) {
094: // incorrect extension value encoding
095: super .dumpValue(buffer);
096: return;
097: }
098: }
099: 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 = new ASN1SequenceOf(
113: 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: }
|