1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.conn.ssl;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.security.cert.Certificate;
33 import java.security.cert.X509Certificate;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Locale;
38
39 import javax.net.ssl.SSLException;
40 import javax.net.ssl.SSLSession;
41 import javax.net.ssl.SSLSocket;
42 import javax.security.auth.x500.X500Principal;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.apache.http.conn.util.InetAddressUtils;
47 import org.apache.http.util.Args;
48
49
50
51
52
53
54
55
56
57
58 @Deprecated
59 public abstract class AbstractVerifier implements X509HostnameVerifier {
60
61 private final Log log = LogFactory.getLog(getClass());
62
63 final static String[] BAD_COUNTRY_2LDS =
64 { "ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info",
65 "lg", "ne", "net", "or", "org" };
66
67 static {
68
69 Arrays.sort(BAD_COUNTRY_2LDS);
70 }
71
72 @Override
73 public final void verify(final String host, final SSLSocket ssl)
74 throws IOException {
75 Args.notNull(host, "Host");
76 SSLSession session = ssl.getSession();
77 if(session == null) {
78
79
80
81 final InputStream in = ssl.getInputStream();
82 in.available();
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 session = ssl.getSession();
102 if(session == null) {
103
104
105 ssl.startHandshake();
106
107
108
109 session = ssl.getSession();
110 }
111 }
112
113 final Certificate[] certs = session.getPeerCertificates();
114 final X509Certificate x509 = (X509Certificate) certs[0];
115 verify(host, x509);
116 }
117
118 @Override
119 public final boolean verify(final String host, final SSLSession session) {
120 try {
121 final Certificate[] certs = session.getPeerCertificates();
122 final X509Certificate x509 = (X509Certificate) certs[0];
123 verify(host, x509);
124 return true;
125 } catch(final SSLException ex) {
126 if (log.isDebugEnabled()) {
127 log.debug(ex.getMessage(), ex);
128 }
129 return false;
130 }
131 }
132
133 @Override
134 public final void verify(
135 final String host, final X509Certificate cert) throws SSLException {
136 final List<SubjectName> allSubjectAltNames = DefaultHostnameVerifier.getSubjectAltNames(cert);
137 final List<String> subjectAlts = new ArrayList<String>();
138 if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
139 for (final SubjectName subjectName: allSubjectAltNames) {
140 if (subjectName.getType() == SubjectName.IP) {
141 subjectAlts.add(subjectName.getValue());
142 }
143 }
144 } else {
145 for (final SubjectName subjectName: allSubjectAltNames) {
146 if (subjectName.getType() == SubjectName.DNS) {
147 subjectAlts.add(subjectName.getValue());
148 }
149 }
150 }
151 final X500Principal subjectPrincipal = cert.getSubjectX500Principal();
152 final String cn = DefaultHostnameVerifier.extractCN(subjectPrincipal.getName(X500Principal.RFC2253));
153 verify(host,
154 cn != null ? new String[] {cn} : null,
155 subjectAlts != null && !subjectAlts.isEmpty() ? subjectAlts.toArray(new String[subjectAlts.size()]) : null);
156 }
157
158 public final void verify(final String host, final String[] cns,
159 final String[] subjectAlts,
160 final boolean strictWithSubDomains)
161 throws SSLException {
162
163 final String cn = cns != null && cns.length > 0 ? cns[0] : null;
164 final List<String> subjectAltList = subjectAlts != null && subjectAlts.length > 0 ? Arrays.asList(subjectAlts) : null;
165
166 final String normalizedHost = InetAddressUtils.isIPv6Address(host) ?
167 DefaultHostnameVerifier.normaliseAddress(host.toLowerCase(Locale.ROOT)) : host;
168
169 if (subjectAltList != null) {
170 for (final String subjectAlt: subjectAltList) {
171 final String normalizedAltSubject = InetAddressUtils.isIPv6Address(subjectAlt) ?
172 DefaultHostnameVerifier.normaliseAddress(subjectAlt) : subjectAlt;
173 if (matchIdentity(normalizedHost, normalizedAltSubject, strictWithSubDomains)) {
174 return;
175 }
176 }
177 throw new SSLException("Certificate for <" + host + "> doesn't match any " +
178 "of the subject alternative names: " + subjectAltList);
179 } else if (cn != null) {
180 final String normalizedCN = InetAddressUtils.isIPv6Address(cn) ?
181 DefaultHostnameVerifier.normaliseAddress(cn) : cn;
182 if (matchIdentity(normalizedHost, normalizedCN, strictWithSubDomains)) {
183 return;
184 }
185 throw new SSLException("Certificate for <" + host + "> doesn't match " +
186 "common name of the certificate subject: " + cn);
187 } else {
188 throw new SSLException("Certificate subject for <" + host + "> doesn't contain " +
189 "a common name and does not have alternative names");
190 }
191 }
192
193 private static boolean matchIdentity(final String host, final String identity, final boolean strict) {
194 if (host == null) {
195 return false;
196 }
197 final String normalizedHost = host.toLowerCase(Locale.ROOT);
198 final String normalizedIdentity = identity.toLowerCase(Locale.ROOT);
199
200
201
202 final String parts[] = normalizedIdentity.split("\\.");
203 final boolean doWildcard = parts.length >= 3 && parts[0].endsWith("*") &&
204 (!strict || validCountryWildcard(parts));
205 if (doWildcard) {
206 final boolean match;
207 final String firstpart = parts[0];
208 if (firstpart.length() > 1) {
209 final String prefix = firstpart.substring(0, firstpart.length() - 1);
210 final String suffix = normalizedIdentity.substring(firstpart.length());
211 final String hostSuffix = normalizedHost.substring(prefix.length());
212 match = normalizedHost.startsWith(prefix) && hostSuffix.endsWith(suffix);
213 } else {
214 match = normalizedHost.endsWith(normalizedIdentity.substring(1));
215 }
216 return match && (!strict || countDots(normalizedHost) == countDots(normalizedIdentity));
217 }
218 return normalizedHost.equals(normalizedIdentity);
219 }
220
221 private static boolean validCountryWildcard(final String parts[]) {
222 if (parts.length != 3 || parts[2].length() != 2) {
223 return true;
224 }
225 return Arrays.binarySearch(BAD_COUNTRY_2LDS, parts[1]) < 0;
226 }
227
228 public static boolean acceptableCountryWildcard(final String cn) {
229 return validCountryWildcard(cn.split("\\."));
230 }
231
232 public static String[] getCNs(final X509Certificate cert) {
233 final String subjectPrincipal = cert.getSubjectX500Principal().toString();
234 try {
235 final String cn = DefaultHostnameVerifier.extractCN(subjectPrincipal);
236 return cn != null ? new String[] { cn } : null;
237 } catch (final SSLException ex) {
238 return null;
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 public static String[] getDNSSubjectAlts(final X509Certificate cert) {
259 final List<SubjectName> subjectAltNames = DefaultHostnameVerifier.getSubjectAltNames(cert);
260 if (subjectAltNames == null) {
261 return null;
262 }
263 final List<String> dnsAlts = new ArrayList<String>();
264 for (final SubjectName subjectName: subjectAltNames) {
265 if (subjectName.getType() == SubjectName.DNS) {
266 dnsAlts.add(subjectName.getValue());
267 }
268 }
269 return dnsAlts.isEmpty() ? dnsAlts.toArray(new String[dnsAlts.size()]) : null;
270 }
271
272
273
274
275
276
277 public static int countDots(final String s) {
278 int count = 0;
279 for(int i = 0; i < s.length(); i++) {
280 if(s.charAt(i) == '.') {
281 count++;
282 }
283 }
284 return count;
285 }
286
287 }