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 java.util.Enumeration;
022    import java.util.Hashtable;
023    import java.util.Vector;
024    
025    import org.apache.geronimo.util.asn1.ASN1Encodable;
026    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
027    import org.apache.geronimo.util.asn1.ASN1Sequence;
028    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
029    import org.apache.geronimo.util.asn1.DERObject;
030    import org.apache.geronimo.util.asn1.DERSequence;
031    
032    /**
033     * The extendedKeyUsage object.
034     * <pre>
035     *      extendedKeyUsage ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
036     * </pre>
037     */
038    public class ExtendedKeyUsage
039        extends ASN1Encodable
040    {
041        Hashtable     usageTable = new Hashtable();
042        ASN1Sequence  seq;
043    
044        public static ExtendedKeyUsage getInstance(
045            ASN1TaggedObject obj,
046            boolean          explicit)
047        {
048            return getInstance(ASN1Sequence.getInstance(obj, explicit));
049        }
050    
051        public static ExtendedKeyUsage getInstance(
052            Object obj)
053        {
054            if(obj == null || obj instanceof ExtendedKeyUsage)
055            {
056                return (ExtendedKeyUsage)obj;
057            }
058    
059            if(obj instanceof ASN1Sequence)
060            {
061                return new ExtendedKeyUsage((ASN1Sequence)obj);
062            }
063    
064            throw new IllegalArgumentException("Invalid ExtendedKeyUsage: " + obj.getClass().getName());
065        }
066    
067        public ExtendedKeyUsage(
068            KeyPurposeId  usage)
069        {
070            this.seq = new DERSequence(usage);
071    
072            this.usageTable.put(usage, usage);
073        }
074    
075        public ExtendedKeyUsage(
076            ASN1Sequence  seq)
077        {
078            this.seq = seq;
079    
080            Enumeration e = seq.getObjects();
081    
082            while (e.hasMoreElements())
083            {
084                Object  o = e.nextElement();
085    
086                this.usageTable.put(o, o);
087            }
088        }
089    
090        public ExtendedKeyUsage(
091            Vector  usages)
092        {
093            ASN1EncodableVector v = new ASN1EncodableVector();
094            Enumeration         e = usages.elements();
095    
096            while (e.hasMoreElements())
097            {
098                DERObject  o = (DERObject)e.nextElement();
099    
100                v.add(o);
101                this.usageTable.put(o, o);
102            }
103    
104            this.seq = new DERSequence(v);
105        }
106    
107        public boolean hasKeyPurposeId(
108            KeyPurposeId keyPurposeId)
109        {
110            return (usageTable.get(keyPurposeId) != null);
111        }
112    
113        public int size()
114        {
115            return usageTable.size();
116        }
117    
118        public DERObject toASN1Object()
119        {
120            return seq;
121        }
122    }