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.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.ASN1OctetString;
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.DERObjectIdentifier;
031    import org.apache.geronimo.util.asn1.DEROctetString;
032    import org.apache.geronimo.util.asn1.DERSequence;
033    import org.apache.geronimo.util.asn1.DERTaggedObject;
034    import org.apache.geronimo.util.asn1.DERUTF8String;
035    
036    /**
037     * Implementation of <code>IetfAttrSyntax</code> as specified by RFC3281.
038     */
039    public class IetfAttrSyntax
040        extends ASN1Encodable
041    {
042        public static final int VALUE_OCTETS    = 1;
043        public static final int VALUE_OID       = 2;
044        public static final int VALUE_UTF8      = 3;
045        GeneralNames            policyAuthority = null;
046        Vector                  values          = new Vector();
047        int                     valueChoice     = -1;
048    
049        /**
050         *
051         */
052        public IetfAttrSyntax(ASN1Sequence seq)
053        {
054            int i = 0;
055    
056            if (seq.getObjectAt(0) instanceof ASN1TaggedObject)
057            {
058                policyAuthority = GeneralNames.getInstance(((ASN1TaggedObject)seq.getObjectAt(0)), false);
059                i++;
060            }
061            else if (seq.size() == 2)
062            { // VOMS fix
063                policyAuthority = GeneralNames.getInstance(seq.getObjectAt(0));
064                i++;
065            }
066    
067            if (!(seq.getObjectAt(i) instanceof ASN1Sequence))
068            {
069                throw new IllegalArgumentException("Non-IetfAttrSyntax encoding");
070            }
071    
072            seq = (ASN1Sequence)seq.getObjectAt(i);
073    
074            for (Enumeration e = seq.getObjects(); e.hasMoreElements();)
075            {
076                DERObject obj = (DERObject)e.nextElement();
077                int type;
078    
079                if (obj instanceof DERObjectIdentifier)
080                {
081                    type = VALUE_OID;
082                }
083                else if (obj instanceof DERUTF8String)
084                {
085                    type = VALUE_UTF8;
086                }
087                else if (obj instanceof DEROctetString)
088                {
089                    type = VALUE_OCTETS;
090                }
091                else
092                {
093                    throw new IllegalArgumentException("Bad value type encoding IetfAttrSyntax");
094                }
095    
096                if (valueChoice < 0)
097                {
098                    valueChoice = type;
099                }
100    
101                if (type != valueChoice)
102                {
103                    throw new IllegalArgumentException("Mix of value types in IetfAttrSyntax");
104                }
105    
106                values.addElement(obj);
107            }
108        }
109    
110        public GeneralNames getPolicyAuthority()
111        {
112            return policyAuthority;
113        }
114    
115        public int getValueType()
116        {
117            return valueChoice;
118        }
119    
120        public Object[] getValues()
121        {
122            if (this.getValueType() == VALUE_OCTETS)
123            {
124                ASN1OctetString[] tmp = new ASN1OctetString[values.size()];
125    
126                for (int i = 0; i != tmp.length; i++)
127                {
128                    tmp[i] = (ASN1OctetString)values.elementAt(i);
129                }
130    
131                return tmp;
132            }
133            else if (this.getValueType() == VALUE_OID)
134            {
135                DERObjectIdentifier[] tmp = new DERObjectIdentifier[values.size()];
136    
137                for (int i = 0; i != tmp.length; i++)
138                {
139                    tmp[i] = (DERObjectIdentifier)values.elementAt(i);
140                }
141    
142                return tmp;
143            }
144            else
145            {
146                DERUTF8String[] tmp = new DERUTF8String[values.size()];
147    
148                for (int i = 0; i != tmp.length; i++)
149                {
150                    tmp[i] = (DERUTF8String)values.elementAt(i);
151                }
152    
153                return tmp;
154            }
155        }
156    
157        /**
158         *
159         * <pre>
160         *
161         *  IetfAttrSyntax ::= SEQUENCE {
162         *    policyAuthority [0] GeneralNames OPTIONAL,
163         *    values SEQUENCE OF CHOICE {
164         *      octets OCTET STRING,
165         *      oid OBJECT IDENTIFIER,
166         *      string UTF8String
167         *    }
168         *  }
169         *
170         * </pre>
171         */
172        public DERObject toASN1Object()
173        {
174            ASN1EncodableVector v = new ASN1EncodableVector();
175    
176            if (policyAuthority != null)
177            {
178                v.add(new DERTaggedObject(0, policyAuthority));
179            }
180    
181            ASN1EncodableVector v2 = new ASN1EncodableVector();
182    
183            for (Enumeration i = values.elements(); i.hasMoreElements();)
184            {
185                v2.add((ASN1Encodable)i.nextElement());
186            }
187    
188            v.add(new DERSequence(v2));
189    
190            return new DERSequence(v);
191        }
192    }