View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import java.util.Enumeration;
22  import java.util.Vector;
23  
24  import org.apache.geronimo.util.asn1.ASN1Encodable;
25  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
26  import org.apache.geronimo.util.asn1.ASN1OctetString;
27  import org.apache.geronimo.util.asn1.ASN1Sequence;
28  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
29  import org.apache.geronimo.util.asn1.DERObject;
30  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
31  import org.apache.geronimo.util.asn1.DEROctetString;
32  import org.apache.geronimo.util.asn1.DERSequence;
33  import org.apache.geronimo.util.asn1.DERTaggedObject;
34  import org.apache.geronimo.util.asn1.DERUTF8String;
35  
36  /**
37   * Implementation of <code>IetfAttrSyntax</code> as specified by RFC3281.
38   */
39  public class IetfAttrSyntax
40      extends ASN1Encodable
41  {
42      public static final int VALUE_OCTETS    = 1;
43      public static final int VALUE_OID       = 2;
44      public static final int VALUE_UTF8      = 3;
45      GeneralNames            policyAuthority = null;
46      Vector                  values          = new Vector();
47      int                     valueChoice     = -1;
48  
49      /**
50       *
51       */
52      public IetfAttrSyntax(ASN1Sequence seq)
53      {
54          int i = 0;
55  
56          if (seq.getObjectAt(0) instanceof ASN1TaggedObject)
57          {
58              policyAuthority = GeneralNames.getInstance(((ASN1TaggedObject)seq.getObjectAt(0)), false);
59              i++;
60          }
61          else if (seq.size() == 2)
62          { // VOMS fix
63              policyAuthority = GeneralNames.getInstance(seq.getObjectAt(0));
64              i++;
65          }
66  
67          if (!(seq.getObjectAt(i) instanceof ASN1Sequence))
68          {
69              throw new IllegalArgumentException("Non-IetfAttrSyntax encoding");
70          }
71  
72          seq = (ASN1Sequence)seq.getObjectAt(i);
73  
74          for (Enumeration e = seq.getObjects(); e.hasMoreElements();)
75          {
76              DERObject obj = (DERObject)e.nextElement();
77              int type;
78  
79              if (obj instanceof DERObjectIdentifier)
80              {
81                  type = VALUE_OID;
82              }
83              else if (obj instanceof DERUTF8String)
84              {
85                  type = VALUE_UTF8;
86              }
87              else if (obj instanceof DEROctetString)
88              {
89                  type = VALUE_OCTETS;
90              }
91              else
92              {
93                  throw new IllegalArgumentException("Bad value type encoding IetfAttrSyntax");
94              }
95  
96              if (valueChoice < 0)
97              {
98                  valueChoice = type;
99              }
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 }