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.Hashtable;
23 import java.util.Vector;
24
25 import org.apache.geronimo.util.asn1.ASN1Encodable;
26 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
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.DERSequence;
31
32 /**
33 * The extendedKeyUsage object.
34 * <pre>
35 * extendedKeyUsage ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
36 * </pre>
37 */
38 public class ExtendedKeyUsage
39 extends ASN1Encodable
40 {
41 Hashtable usageTable = new Hashtable();
42 ASN1Sequence seq;
43
44 public static ExtendedKeyUsage getInstance(
45 ASN1TaggedObject obj,
46 boolean explicit)
47 {
48 return getInstance(ASN1Sequence.getInstance(obj, explicit));
49 }
50
51 public static ExtendedKeyUsage getInstance(
52 Object obj)
53 {
54 if(obj == null || obj instanceof ExtendedKeyUsage)
55 {
56 return (ExtendedKeyUsage)obj;
57 }
58
59 if(obj instanceof ASN1Sequence)
60 {
61 return new ExtendedKeyUsage((ASN1Sequence)obj);
62 }
63
64 throw new IllegalArgumentException("Invalid ExtendedKeyUsage: " + obj.getClass().getName());
65 }
66
67 public ExtendedKeyUsage(
68 KeyPurposeId usage)
69 {
70 this.seq = new DERSequence(usage);
71
72 this.usageTable.put(usage, usage);
73 }
74
75 public ExtendedKeyUsage(
76 ASN1Sequence seq)
77 {
78 this.seq = seq;
79
80 Enumeration e = seq.getObjects();
81
82 while (e.hasMoreElements())
83 {
84 Object o = e.nextElement();
85
86 this.usageTable.put(o, o);
87 }
88 }
89
90 public ExtendedKeyUsage(
91 Vector usages)
92 {
93 ASN1EncodableVector v = new ASN1EncodableVector();
94 Enumeration e = usages.elements();
95
96 while (e.hasMoreElements())
97 {
98 DERObject o = (DERObject)e.nextElement();
99
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 }