1 |
769 |
jeremybenn |
/* CertificatePolicies.java -- certificate policy extension.
|
2 |
|
|
Copyright (C) 2004, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.java.security.x509.ext;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.OID;
|
42 |
|
|
import gnu.java.security.der.DER;
|
43 |
|
|
import gnu.java.security.der.DERReader;
|
44 |
|
|
import gnu.java.security.der.DERValue;
|
45 |
|
|
|
46 |
|
|
import java.io.IOException;
|
47 |
|
|
import java.security.cert.PolicyQualifierInfo;
|
48 |
|
|
import java.util.ArrayList;
|
49 |
|
|
import java.util.Collections;
|
50 |
|
|
import java.util.HashMap;
|
51 |
|
|
import java.util.Iterator;
|
52 |
|
|
import java.util.LinkedList;
|
53 |
|
|
import java.util.List;
|
54 |
|
|
import java.util.Map;
|
55 |
|
|
|
56 |
|
|
public class CertificatePolicies extends Extension.Value
|
57 |
|
|
{
|
58 |
|
|
|
59 |
|
|
// Constants and fields.
|
60 |
|
|
// -------------------------------------------------------------------------
|
61 |
|
|
|
62 |
|
|
public static final OID ID = new OID("2.5.29.32");
|
63 |
|
|
|
64 |
|
|
private final List<OID> policies;
|
65 |
|
|
private final Map<OID, List<PolicyQualifierInfo>> policyQualifierInfos;
|
66 |
|
|
|
67 |
|
|
// Constructor.
|
68 |
|
|
// -------------------------------------------------------------------------
|
69 |
|
|
|
70 |
|
|
public CertificatePolicies(final byte[] encoded) throws IOException
|
71 |
|
|
{
|
72 |
|
|
super(encoded);
|
73 |
|
|
DERReader der = new DERReader(encoded);
|
74 |
|
|
DERValue pol = der.read();
|
75 |
|
|
if (!pol.isConstructed())
|
76 |
|
|
throw new IOException("malformed CertificatePolicies");
|
77 |
|
|
|
78 |
|
|
int len = 0;
|
79 |
|
|
LinkedList<OID> policyList = new LinkedList<OID>();
|
80 |
|
|
HashMap<OID, List<PolicyQualifierInfo>> qualifierMap
|
81 |
|
|
= new HashMap<OID, List<PolicyQualifierInfo>>();
|
82 |
|
|
while (len < pol.getLength())
|
83 |
|
|
{
|
84 |
|
|
DERValue policyInfo = der.read();
|
85 |
|
|
if (!policyInfo.isConstructed())
|
86 |
|
|
throw new IOException("malformed PolicyInformation");
|
87 |
|
|
DERValue val = der.read();
|
88 |
|
|
if (val.getTag() != DER.OBJECT_IDENTIFIER)
|
89 |
|
|
throw new IOException("malformed CertPolicyId");
|
90 |
|
|
OID policyId = (OID) val.getValue();
|
91 |
|
|
policyList.add(policyId);
|
92 |
|
|
if (val.getEncodedLength() < policyInfo.getLength())
|
93 |
|
|
{
|
94 |
|
|
DERValue qual = der.read();
|
95 |
|
|
int len2 = 0;
|
96 |
|
|
LinkedList<PolicyQualifierInfo> quals = new LinkedList<PolicyQualifierInfo>();
|
97 |
|
|
while (len2 < qual.getLength())
|
98 |
|
|
{
|
99 |
|
|
val = der.read();
|
100 |
|
|
quals.add(new PolicyQualifierInfo(val.getEncoded()));
|
101 |
|
|
der.skip(val.getLength());
|
102 |
|
|
len2 += val.getEncodedLength();
|
103 |
|
|
}
|
104 |
|
|
qualifierMap.put(policyId, quals);
|
105 |
|
|
}
|
106 |
|
|
len += policyInfo.getEncodedLength();
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
policies = Collections.unmodifiableList(policyList);
|
110 |
|
|
policyQualifierInfos = Collections.unmodifiableMap(qualifierMap);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
public CertificatePolicies (final List<OID> policies,
|
114 |
|
|
final Map<OID, List<PolicyQualifierInfo>> policyQualifierInfos)
|
115 |
|
|
{
|
116 |
|
|
for (Iterator it = policies.iterator(); it.hasNext(); )
|
117 |
|
|
if (!(it.next() instanceof OID))
|
118 |
|
|
throw new IllegalArgumentException ("policies must be OIDs");
|
119 |
|
|
for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();)
|
120 |
|
|
{
|
121 |
|
|
Map.Entry e = (Map.Entry) it.next();
|
122 |
|
|
if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey()))
|
123 |
|
|
throw new IllegalArgumentException
|
124 |
|
|
("policyQualifierInfos keys must be OIDs");
|
125 |
|
|
if (!(e.getValue() instanceof List))
|
126 |
|
|
throw new IllegalArgumentException
|
127 |
|
|
("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
|
128 |
|
|
for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); )
|
129 |
|
|
if (!(it2.next() instanceof PolicyQualifierInfo))
|
130 |
|
|
throw new IllegalArgumentException
|
131 |
|
|
("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
|
132 |
|
|
}
|
133 |
|
|
this.policies = Collections.unmodifiableList (new ArrayList<OID>(policies));
|
134 |
|
|
this.policyQualifierInfos = Collections.unmodifiableMap
|
135 |
|
|
(new HashMap<OID, List<PolicyQualifierInfo>>(policyQualifierInfos));
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
// Instance methods.
|
139 |
|
|
// -------------------------------------------------------------------------
|
140 |
|
|
|
141 |
|
|
public List<OID> getPolicies()
|
142 |
|
|
{
|
143 |
|
|
return policies;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
|
|
* Returns the list of policy OIDs, formatted as dotted-decimal strings.
|
148 |
|
|
*
|
149 |
|
|
* @return
|
150 |
|
|
*/
|
151 |
|
|
public List<String> getPolicyStrings()
|
152 |
|
|
{
|
153 |
|
|
List<String> l = new ArrayList<String>(policies.size());
|
154 |
|
|
for (OID oid : policies)
|
155 |
|
|
{
|
156 |
|
|
l.add(oid.toString());
|
157 |
|
|
}
|
158 |
|
|
return l;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
public List<PolicyQualifierInfo> getPolicyQualifierInfos(OID oid)
|
162 |
|
|
{
|
163 |
|
|
return policyQualifierInfos.get(oid);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
public byte[] getEncoded()
|
167 |
|
|
{
|
168 |
|
|
if (encoded == null)
|
169 |
|
|
{
|
170 |
|
|
List<DERValue> pol = new ArrayList<DERValue>(policies.size());
|
171 |
|
|
for (Iterator<OID> it = policies.iterator(); it.hasNext(); )
|
172 |
|
|
{
|
173 |
|
|
OID policy = it.next();
|
174 |
|
|
List<PolicyQualifierInfo> qualifiers = getPolicyQualifierInfos(policy);
|
175 |
|
|
List<DERValue> l = new ArrayList<DERValue>(qualifiers == null ? 1 : 2);
|
176 |
|
|
l.add(new DERValue(DER.OBJECT_IDENTIFIER, policy));
|
177 |
|
|
if (qualifiers != null)
|
178 |
|
|
{
|
179 |
|
|
List<DERValue> ll = new ArrayList<DERValue>(qualifiers.size());
|
180 |
|
|
for (Iterator<PolicyQualifierInfo> it2 = qualifiers.iterator(); it.hasNext(); )
|
181 |
|
|
{
|
182 |
|
|
PolicyQualifierInfo info = it2.next();
|
183 |
|
|
try
|
184 |
|
|
{
|
185 |
|
|
ll.add(DERReader.read(info.getEncoded()));
|
186 |
|
|
}
|
187 |
|
|
catch (IOException ioe)
|
188 |
|
|
{
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
l.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, ll));
|
192 |
|
|
}
|
193 |
|
|
pol.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, l));
|
194 |
|
|
}
|
195 |
|
|
encoded = new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded();
|
196 |
|
|
}
|
197 |
|
|
return (byte[]) encoded.clone();
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
public String toString()
|
201 |
|
|
{
|
202 |
|
|
return CertificatePolicies.class.getName() + " [ policies=" + policies +
|
203 |
|
|
" policyQualifierInfos=" + policyQualifierInfos + " ]";
|
204 |
|
|
}
|
205 |
|
|
}
|