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