001 /*
002 * $HeadURL$
003 * $Revision$
004 * $Date$
005 *
006 * ====================================================================
007 * Licensed to the Apache Software Foundation (ASF) under one
008 * or more contributor license agreements. See the NOTICE file
009 * distributed with this work for additional information
010 * regarding copyright ownership. The ASF licenses this file
011 * to you under the Apache License, Version 2.0 (the
012 * "License"); you may not use this file except in compliance
013 * with the License. You may obtain a copy of the License at
014 *
015 * http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing,
018 * software distributed under the License is distributed on an
019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020 * KIND, either express or implied. See the License for the
021 * specific language governing permissions and limitations
022 * under the License.
023 * ====================================================================
024 *
025 * This software consists of voluntary contributions made by many
026 * individuals on behalf of the Apache Software Foundation. For more
027 * information on the Apache Software Foundation, please see
028 * <http://www.apache.org/>.
029 *
030 */
031
032 package org.apache.hadoop.security.ssl;
033
034 import java.io.IOException;
035 import java.io.InputStream;
036 import java.security.cert.Certificate;
037 import java.security.cert.CertificateParsingException;
038 import java.security.cert.X509Certificate;
039 import java.util.Arrays;
040 import java.util.Collection;
041 import java.util.Iterator;
042 import java.util.LinkedList;
043 import java.util.List;
044 import java.util.Set;
045 import java.util.StringTokenizer;
046 import java.util.TreeSet;
047
048 import javax.net.ssl.SSLException;
049 import javax.net.ssl.SSLPeerUnverifiedException;
050 import javax.net.ssl.SSLSession;
051 import javax.net.ssl.SSLSocket;
052
053 import org.apache.hadoop.classification.InterfaceAudience;
054 import org.apache.hadoop.classification.InterfaceStability;
055
056 /**
057 ************************************************************************
058 * Copied from the not-yet-commons-ssl project at
059 * http://juliusdavies.ca/commons-ssl/
060 * This project is not yet in Apache, but it is Apache 2.0 licensed.
061 ************************************************************************
062 * Interface for checking if a hostname matches the names stored inside the
063 * server's X.509 certificate. Correctly implements
064 * javax.net.ssl.HostnameVerifier, but that interface is not recommended.
065 * Instead we added several check() methods that take SSLSocket,
066 * or X509Certificate, or ultimately (they all end up calling this one),
067 * String. (It's easier to supply JUnit with Strings instead of mock
068 * SSLSession objects!)
069 * </p><p>Our check() methods throw exceptions if the name is
070 * invalid, whereas javax.net.ssl.HostnameVerifier just returns true/false.
071 * <p/>
072 * We provide the HostnameVerifier.DEFAULT, HostnameVerifier.STRICT, and
073 * HostnameVerifier.ALLOW_ALL implementations. We also provide the more
074 * specialized HostnameVerifier.DEFAULT_AND_LOCALHOST, as well as
075 * HostnameVerifier.STRICT_IE6. But feel free to define your own
076 * implementations!
077 * <p/>
078 * Inspired by Sebastian Hauer's original StrictSSLProtocolSocketFactory in the
079 * HttpClient "contrib" repository.
080 */
081 @InterfaceAudience.Private
082 @InterfaceStability.Evolving
083 public interface SSLHostnameVerifier extends javax.net.ssl.HostnameVerifier {
084
085 @Override
086 boolean verify(String host, SSLSession session);
087
088 void check(String host, SSLSocket ssl) throws IOException;
089
090 void check(String host, X509Certificate cert) throws SSLException;
091
092 void check(String host, String[] cns, String[] subjectAlts)
093 throws SSLException;
094
095 void check(String[] hosts, SSLSocket ssl) throws IOException;
096
097 void check(String[] hosts, X509Certificate cert) throws SSLException;
098
099
100 /**
101 * Checks to see if the supplied hostname matches any of the supplied CNs
102 * or "DNS" Subject-Alts. Most implementations only look at the first CN,
103 * and ignore any additional CNs. Most implementations do look at all of
104 * the "DNS" Subject-Alts. The CNs or Subject-Alts may contain wildcards
105 * according to RFC 2818.
106 *
107 * @param cns CN fields, in order, as extracted from the X.509
108 * certificate.
109 * @param subjectAlts Subject-Alt fields of type 2 ("DNS"), as extracted
110 * from the X.509 certificate.
111 * @param hosts The array of hostnames to verify.
112 * @throws SSLException If verification failed.
113 */
114 void check(String[] hosts, String[] cns, String[] subjectAlts)
115 throws SSLException;
116
117
118 /**
119 * The DEFAULT HostnameVerifier works the same way as Curl and Firefox.
120 * <p/>
121 * The hostname must match either the first CN, or any of the subject-alts.
122 * A wildcard can occur in the CN, and in any of the subject-alts.
123 * <p/>
124 * The only difference between DEFAULT and STRICT is that a wildcard (such
125 * as "*.foo.com") with DEFAULT matches all subdomains, including
126 * "a.b.foo.com".
127 */
128 public final static SSLHostnameVerifier DEFAULT =
129 new AbstractVerifier() {
130 @Override
131 public final void check(final String[] hosts, final String[] cns,
132 final String[] subjectAlts)
133 throws SSLException {
134 check(hosts, cns, subjectAlts, false, false);
135 }
136
137 @Override
138 public final String toString() { return "DEFAULT"; }
139 };
140
141
142 /**
143 * The DEFAULT_AND_LOCALHOST HostnameVerifier works like the DEFAULT
144 * one with one additional relaxation: a host of "localhost",
145 * "localhost.localdomain", "127.0.0.1", "::1" will always pass, no matter
146 * what is in the server's certificate.
147 */
148 public final static SSLHostnameVerifier DEFAULT_AND_LOCALHOST =
149 new AbstractVerifier() {
150 @Override
151 public final void check(final String[] hosts, final String[] cns,
152 final String[] subjectAlts)
153 throws SSLException {
154 if (isLocalhost(hosts[0])) {
155 return;
156 }
157 check(hosts, cns, subjectAlts, false, false);
158 }
159
160 @Override
161 public final String toString() { return "DEFAULT_AND_LOCALHOST"; }
162 };
163
164 /**
165 * The STRICT HostnameVerifier works the same way as java.net.URL in Sun
166 * Java 1.4, Sun Java 5, Sun Java 6. It's also pretty close to IE6.
167 * This implementation appears to be compliant with RFC 2818 for dealing
168 * with wildcards.
169 * <p/>
170 * The hostname must match either the first CN, or any of the subject-alts.
171 * A wildcard can occur in the CN, and in any of the subject-alts. The
172 * one divergence from IE6 is how we only check the first CN. IE6 allows
173 * a match against any of the CNs present. We decided to follow in
174 * Sun Java 1.4's footsteps and only check the first CN.
175 * <p/>
176 * A wildcard such as "*.foo.com" matches only subdomains in the same
177 * level, for example "a.foo.com". It does not match deeper subdomains
178 * such as "a.b.foo.com".
179 */
180 public final static SSLHostnameVerifier STRICT =
181 new AbstractVerifier() {
182 @Override
183 public final void check(final String[] host, final String[] cns,
184 final String[] subjectAlts)
185 throws SSLException {
186 check(host, cns, subjectAlts, false, true);
187 }
188
189 @Override
190 public final String toString() { return "STRICT"; }
191 };
192
193 /**
194 * The STRICT_IE6 HostnameVerifier works just like the STRICT one with one
195 * minor variation: the hostname can match against any of the CN's in the
196 * server's certificate, not just the first one. This behaviour is
197 * identical to IE6's behaviour.
198 */
199 public final static SSLHostnameVerifier STRICT_IE6 =
200 new AbstractVerifier() {
201 @Override
202 public final void check(final String[] host, final String[] cns,
203 final String[] subjectAlts)
204 throws SSLException {
205 check(host, cns, subjectAlts, true, true);
206 }
207
208 @Override
209 public final String toString() { return "STRICT_IE6"; }
210 };
211
212 /**
213 * The ALLOW_ALL HostnameVerifier essentially turns hostname verification
214 * off. This implementation is a no-op, and never throws the SSLException.
215 */
216 public final static SSLHostnameVerifier ALLOW_ALL =
217 new AbstractVerifier() {
218 @Override
219 public final void check(final String[] host, final String[] cns,
220 final String[] subjectAlts) {
221 // Allow everything - so never blowup.
222 }
223
224 @Override
225 public final String toString() { return "ALLOW_ALL"; }
226 };
227
228 abstract class AbstractVerifier implements SSLHostnameVerifier {
229
230 /**
231 * This contains a list of 2nd-level domains that aren't allowed to
232 * have wildcards when combined with country-codes.
233 * For example: [*.co.uk].
234 * <p/>
235 * The [*.co.uk] problem is an interesting one. Should we just hope
236 * that CA's would never foolishly allow such a certificate to happen?
237 * Looks like we're the only implementation guarding against this.
238 * Firefox, Curl, Sun Java 1.4, 5, 6 don't bother with this check.
239 */
240 private final static String[] BAD_COUNTRY_2LDS =
241 {"ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info",
242 "lg", "ne", "net", "or", "org"};
243
244 private final static String[] LOCALHOSTS = {"::1", "127.0.0.1",
245 "localhost",
246 "localhost.localdomain"};
247
248
249 static {
250 // Just in case developer forgot to manually sort the array. :-)
251 Arrays.sort(BAD_COUNTRY_2LDS);
252 Arrays.sort(LOCALHOSTS);
253 }
254
255 protected AbstractVerifier() {}
256
257 /**
258 * The javax.net.ssl.HostnameVerifier contract.
259 *
260 * @param host 'hostname' we used to create our socket
261 * @param session SSLSession with the remote server
262 * @return true if the host matched the one in the certificate.
263 */
264 @Override
265 public boolean verify(String host, SSLSession session) {
266 try {
267 Certificate[] certs = session.getPeerCertificates();
268 X509Certificate x509 = (X509Certificate) certs[0];
269 check(new String[]{host}, x509);
270 return true;
271 }
272 catch (SSLException e) {
273 return false;
274 }
275 }
276
277 @Override
278 public void check(String host, SSLSocket ssl) throws IOException {
279 check(new String[]{host}, ssl);
280 }
281
282 @Override
283 public void check(String host, X509Certificate cert)
284 throws SSLException {
285 check(new String[]{host}, cert);
286 }
287
288 @Override
289 public void check(String host, String[] cns, String[] subjectAlts)
290 throws SSLException {
291 check(new String[]{host}, cns, subjectAlts);
292 }
293
294 @Override
295 public void check(String host[], SSLSocket ssl)
296 throws IOException {
297 if (host == null) {
298 throw new NullPointerException("host to verify is null");
299 }
300
301 SSLSession session = ssl.getSession();
302 if (session == null) {
303 // In our experience this only happens under IBM 1.4.x when
304 // spurious (unrelated) certificates show up in the server'
305 // chain. Hopefully this will unearth the real problem:
306 InputStream in = ssl.getInputStream();
307 in.available();
308 /*
309 If you're looking at the 2 lines of code above because
310 you're running into a problem, you probably have two
311 options:
312
313 #1. Clean up the certificate chain that your server
314 is presenting (e.g. edit "/etc/apache2/server.crt"
315 or wherever it is your server's certificate chain
316 is defined).
317
318 OR
319
320 #2. Upgrade to an IBM 1.5.x or greater JVM, or switch
321 to a non-IBM JVM.
322 */
323
324 // If ssl.getInputStream().available() didn't cause an
325 // exception, maybe at least now the session is available?
326 session = ssl.getSession();
327 if (session == null) {
328 // If it's still null, probably a startHandshake() will
329 // unearth the real problem.
330 ssl.startHandshake();
331
332 // Okay, if we still haven't managed to cause an exception,
333 // might as well go for the NPE. Or maybe we're okay now?
334 session = ssl.getSession();
335 }
336 }
337 Certificate[] certs;
338 try {
339 certs = session.getPeerCertificates();
340 } catch (SSLPeerUnverifiedException spue) {
341 InputStream in = ssl.getInputStream();
342 in.available();
343 // Didn't trigger anything interesting? Okay, just throw
344 // original.
345 throw spue;
346 }
347 X509Certificate x509 = (X509Certificate) certs[0];
348 check(host, x509);
349 }
350
351 @Override
352 public void check(String[] host, X509Certificate cert)
353 throws SSLException {
354 String[] cns = Certificates.getCNs(cert);
355 String[] subjectAlts = Certificates.getDNSSubjectAlts(cert);
356 check(host, cns, subjectAlts);
357 }
358
359 public void check(final String[] hosts, final String[] cns,
360 final String[] subjectAlts, final boolean ie6,
361 final boolean strictWithSubDomains)
362 throws SSLException {
363 // Build up lists of allowed hosts For logging/debugging purposes.
364 StringBuffer buf = new StringBuffer(32);
365 buf.append('<');
366 for (int i = 0; i < hosts.length; i++) {
367 String h = hosts[i];
368 h = h != null ? h.trim().toLowerCase() : "";
369 hosts[i] = h;
370 if (i > 0) {
371 buf.append('/');
372 }
373 buf.append(h);
374 }
375 buf.append('>');
376 String hostnames = buf.toString();
377 // Build the list of names we're going to check. Our DEFAULT and
378 // STRICT implementations of the HostnameVerifier only use the
379 // first CN provided. All other CNs are ignored.
380 // (Firefox, wget, curl, Sun Java 1.4, 5, 6 all work this way).
381 final Set<String> names = new TreeSet<String>();
382 if (cns != null && cns.length > 0 && cns[0] != null) {
383 names.add(cns[0]);
384 if (ie6) {
385 for (int i = 1; i < cns.length; i++) {
386 names.add(cns[i]);
387 }
388 }
389 }
390 if (subjectAlts != null) {
391 for (int i = 0; i < subjectAlts.length; i++) {
392 if (subjectAlts[i] != null) {
393 names.add(subjectAlts[i]);
394 }
395 }
396 }
397 if (names.isEmpty()) {
398 String msg = "Certificate for " + hosts[0] + " doesn't contain CN or DNS subjectAlt";
399 throw new SSLException(msg);
400 }
401
402 // StringBuffer for building the error message.
403 buf = new StringBuffer();
404
405 boolean match = false;
406 out:
407 for (Iterator<String> it = names.iterator(); it.hasNext();) {
408 // Don't trim the CN, though!
409 final String cn = it.next().toLowerCase();
410 // Store CN in StringBuffer in case we need to report an error.
411 buf.append(" <");
412 buf.append(cn);
413 buf.append('>');
414 if (it.hasNext()) {
415 buf.append(" OR");
416 }
417
418 // The CN better have at least two dots if it wants wildcard
419 // action. It also can't be [*.co.uk] or [*.co.jp] or
420 // [*.org.uk], etc...
421 boolean doWildcard = cn.startsWith("*.") &&
422 cn.lastIndexOf('.') >= 0 &&
423 !isIP4Address(cn) &&
424 acceptableCountryWildcard(cn);
425
426 for (int i = 0; i < hosts.length; i++) {
427 final String hostName = hosts[i].trim().toLowerCase();
428 if (doWildcard) {
429 match = hostName.endsWith(cn.substring(1));
430 if (match && strictWithSubDomains) {
431 // If we're in strict mode, then [*.foo.com] is not
432 // allowed to match [a.b.foo.com]
433 match = countDots(hostName) == countDots(cn);
434 }
435 } else {
436 match = hostName.equals(cn);
437 }
438 if (match) {
439 break out;
440 }
441 }
442 }
443 if (!match) {
444 throw new SSLException("hostname in certificate didn't match: " + hostnames + " !=" + buf);
445 }
446 }
447
448 public static boolean isIP4Address(final String cn) {
449 boolean isIP4 = true;
450 String tld = cn;
451 int x = cn.lastIndexOf('.');
452 // We only bother analyzing the characters after the final dot
453 // in the name.
454 if (x >= 0 && x + 1 < cn.length()) {
455 tld = cn.substring(x + 1);
456 }
457 for (int i = 0; i < tld.length(); i++) {
458 if (!Character.isDigit(tld.charAt(0))) {
459 isIP4 = false;
460 break;
461 }
462 }
463 return isIP4;
464 }
465
466 public static boolean acceptableCountryWildcard(final String cn) {
467 int cnLen = cn.length();
468 if (cnLen >= 7 && cnLen <= 9) {
469 // Look for the '.' in the 3rd-last position:
470 if (cn.charAt(cnLen - 3) == '.') {
471 // Trim off the [*.] and the [.XX].
472 String s = cn.substring(2, cnLen - 3);
473 // And test against the sorted array of bad 2lds:
474 int x = Arrays.binarySearch(BAD_COUNTRY_2LDS, s);
475 return x < 0;
476 }
477 }
478 return true;
479 }
480
481 public static boolean isLocalhost(String host) {
482 host = host != null ? host.trim().toLowerCase() : "";
483 if (host.startsWith("::1")) {
484 int x = host.lastIndexOf('%');
485 if (x >= 0) {
486 host = host.substring(0, x);
487 }
488 }
489 int x = Arrays.binarySearch(LOCALHOSTS, host);
490 return x >= 0;
491 }
492
493 /**
494 * Counts the number of dots "." in a string.
495 *
496 * @param s string to count dots from
497 * @return number of dots
498 */
499 public static int countDots(final String s) {
500 int count = 0;
501 for (int i = 0; i < s.length(); i++) {
502 if (s.charAt(i) == '.') {
503 count++;
504 }
505 }
506 return count;
507 }
508 }
509
510 static class Certificates {
511 public static String[] getCNs(X509Certificate cert) {
512 final List<String> cnList = new LinkedList<String>();
513 /*
514 Sebastian Hauer's original StrictSSLProtocolSocketFactory used
515 getName() and had the following comment:
516
517 Parses a X.500 distinguished name for the value of the
518 "Common Name" field. This is done a bit sloppy right
519 now and should probably be done a bit more according to
520 <code>RFC 2253</code>.
521
522 I've noticed that toString() seems to do a better job than
523 getName() on these X500Principal objects, so I'm hoping that
524 addresses Sebastian's concern.
525
526 For example, getName() gives me this:
527 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
528
529 whereas toString() gives me this:
530 [email protected]
531
532 Looks like toString() even works with non-ascii domain names!
533 I tested it with "花子.co.jp" and it worked fine.
534 */
535 String subjectPrincipal = cert.getSubjectX500Principal().toString();
536 StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
537 while (st.hasMoreTokens()) {
538 String tok = st.nextToken();
539 int x = tok.indexOf("CN=");
540 if (x >= 0) {
541 cnList.add(tok.substring(x + 3));
542 }
543 }
544 if (!cnList.isEmpty()) {
545 String[] cns = new String[cnList.size()];
546 cnList.toArray(cns);
547 return cns;
548 } else {
549 return null;
550 }
551 }
552
553
554 /**
555 * Extracts the array of SubjectAlt DNS names from an X509Certificate.
556 * Returns null if there aren't any.
557 * <p/>
558 * Note: Java doesn't appear able to extract international characters
559 * from the SubjectAlts. It can only extract international characters
560 * from the CN field.
561 * <p/>
562 * (Or maybe the version of OpenSSL I'm using to test isn't storing the
563 * international characters correctly in the SubjectAlts?).
564 *
565 * @param cert X509Certificate
566 * @return Array of SubjectALT DNS names stored in the certificate.
567 */
568 public static String[] getDNSSubjectAlts(X509Certificate cert) {
569 final List<String> subjectAltList = new LinkedList<String>();
570 Collection<List<?>> c = null;
571 try {
572 c = cert.getSubjectAlternativeNames();
573 }
574 catch (CertificateParsingException cpe) {
575 // Should probably log.debug() this?
576 cpe.printStackTrace();
577 }
578 if (c != null) {
579 Iterator<List<?>> it = c.iterator();
580 while (it.hasNext()) {
581 List<?> list = it.next();
582 int type = ((Integer) list.get(0)).intValue();
583 // If type is 2, then we've got a dNSName
584 if (type == 2) {
585 String s = (String) list.get(1);
586 subjectAltList.add(s);
587 }
588 }
589 }
590 if (!subjectAltList.isEmpty()) {
591 String[] subjectAlts = new String[subjectAltList.size()];
592 subjectAltList.toArray(subjectAlts);
593 return subjectAlts;
594 } else {
595 return null;
596 }
597 }
598 }
599
600 }