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