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
032package org.apache.hadoop.security.ssl;
033
034import org.apache.hadoop.classification.InterfaceAudience;
035import org.apache.hadoop.classification.InterfaceStability;
036
037import java.io.IOException;
038import java.io.InputStream;
039import java.security.cert.Certificate;
040import java.security.cert.CertificateParsingException;
041import java.security.cert.X509Certificate;
042import java.util.Arrays;
043import java.util.Collection;
044import java.util.Iterator;
045import java.util.LinkedList;
046import java.util.List;
047import java.util.StringTokenizer;
048import java.util.TreeSet;
049
050import javax.net.ssl.SSLException;
051import javax.net.ssl.SSLPeerUnverifiedException;
052import javax.net.ssl.SSLSession;
053import javax.net.ssl.SSLSocket;
054
055/**
056 ************************************************************************
057 * Copied from the not-yet-commons-ssl project at
058 * http://juliusdavies.ca/commons-ssl/
059 * This project is not yet in Apache, but it is Apache 2.0 licensed.
060 ************************************************************************
061 * Interface for checking if a hostname matches the names stored inside the
062 * server's X.509 certificate.  Correctly implements
063 * javax.net.ssl.HostnameVerifier, but that interface is not recommended.
064 * Instead we added several check() methods that take SSLSocket,
065 * or X509Certificate, or ultimately (they all end up calling this one),
066 * String.  (It's easier to supply JUnit with Strings instead of mock
067 * SSLSession objects!)
068 * </p><p>Our check() methods throw exceptions if the name is
069 * invalid, whereas javax.net.ssl.HostnameVerifier just returns true/false.
070 * <p/>
071 * We provide the HostnameVerifier.DEFAULT, HostnameVerifier.STRICT, and
072 * HostnameVerifier.ALLOW_ALL implementations.  We also provide the more
073 * specialized HostnameVerifier.DEFAULT_AND_LOCALHOST, as well as
074 * HostnameVerifier.STRICT_IE6.  But feel free to define your own
075 * implementations!
076 * <p/>
077 * Inspired by Sebastian Hauer's original StrictSSLProtocolSocketFactory in the
078 * HttpClient "contrib" repository.
079 */
080@InterfaceAudience.Private
081@InterfaceStability.Evolving
082public interface SSLHostnameVerifier extends javax.net.ssl.HostnameVerifier {
083
084    @Override
085    boolean verify(String host, SSLSession session);
086
087    void check(String host, SSLSocket ssl) throws IOException;
088
089    void check(String host, X509Certificate cert) throws SSLException;
090
091    void check(String host, String[] cns, String[] subjectAlts)
092        throws SSLException;
093
094    void check(String[] hosts, SSLSocket ssl) throws IOException;
095
096    void check(String[] hosts, X509Certificate cert) throws SSLException;
097
098
099    /**
100     * Checks to see if the supplied hostname matches any of the supplied CNs
101     * or "DNS" Subject-Alts.  Most implementations only look at the first CN,
102     * and ignore any additional CNs.  Most implementations do look at all of
103     * the "DNS" Subject-Alts. The CNs or Subject-Alts may contain wildcards
104     * according to RFC 2818.
105     *
106     * @param cns         CN fields, in order, as extracted from the X.509
107     *                    certificate.
108     * @param subjectAlts Subject-Alt fields of type 2 ("DNS"), as extracted
109     *                    from the X.509 certificate.
110     * @param hosts       The array of hostnames to verify.
111     * @throws SSLException If verification failed.
112     */
113    void check(String[] hosts, String[] cns, String[] subjectAlts)
114        throws SSLException;
115
116
117    /**
118     * The DEFAULT HostnameVerifier works the same way as Curl and Firefox.
119     * <p/>
120     * The hostname must match either the first CN, or any of the subject-alts.
121     * A wildcard can occur in the CN, and in any of the subject-alts.
122     * <p/>
123     * The only difference between DEFAULT and STRICT is that a wildcard (such
124     * as "*.foo.com") with DEFAULT matches all subdomains, including
125     * "a.b.foo.com".
126     */
127    public final static SSLHostnameVerifier DEFAULT =
128        new AbstractVerifier() {
129            @Override
130            public final void check(final String[] hosts, final String[] cns,
131                                    final String[] subjectAlts)
132                throws SSLException {
133                check(hosts, cns, subjectAlts, false, false);
134            }
135
136            @Override
137            public final String toString() { return "DEFAULT"; }
138        };
139
140
141    /**
142     * The DEFAULT_AND_LOCALHOST HostnameVerifier works like the DEFAULT
143     * one with one additional relaxation:  a host of "localhost",
144     * "localhost.localdomain", "127.0.0.1", "::1" will always pass, no matter
145     * what is in the server's certificate.
146     */
147    public final static SSLHostnameVerifier DEFAULT_AND_LOCALHOST =
148        new AbstractVerifier() {
149            @Override
150            public final void check(final String[] hosts, final String[] cns,
151                                    final String[] subjectAlts)
152                throws SSLException {
153                if (isLocalhost(hosts[0])) {
154                    return;
155                }
156                check(hosts, cns, subjectAlts, false, false);
157            }
158
159            @Override
160            public final String toString() { return "DEFAULT_AND_LOCALHOST"; }
161        };
162
163    /**
164     * The STRICT HostnameVerifier works the same way as java.net.URL in Sun
165     * Java 1.4, Sun Java 5, Sun Java 6.  It's also pretty close to IE6.
166     * This implementation appears to be compliant with RFC 2818 for dealing
167     * with wildcards.
168     * <p/>
169     * The hostname must match either the first CN, or any of the subject-alts.
170     * A wildcard can occur in the CN, and in any of the subject-alts.  The
171     * one divergence from IE6 is how we only check the first CN.  IE6 allows
172     * a match against any of the CNs present.  We decided to follow in
173     * Sun Java 1.4's footsteps and only check the first CN.
174     * <p/>
175     * A wildcard such as "*.foo.com" matches only subdomains in the same
176     * level, for example "a.foo.com".  It does not match deeper subdomains
177     * such as "a.b.foo.com".
178     */
179    public final static SSLHostnameVerifier STRICT =
180        new AbstractVerifier() {
181            @Override
182            public final void check(final String[] host, final String[] cns,
183                                    final String[] subjectAlts)
184                throws SSLException {
185                check(host, cns, subjectAlts, false, true);
186            }
187
188            @Override
189            public final String toString() { return "STRICT"; }
190        };
191
192    /**
193     * The STRICT_IE6 HostnameVerifier works just like the STRICT one with one
194     * minor variation:  the hostname can match against any of the CN's in the
195     * server's certificate, not just the first one.  This behaviour is
196     * identical to IE6's behaviour.
197     */
198    public final static SSLHostnameVerifier STRICT_IE6 =
199        new AbstractVerifier() {
200            @Override
201            public final void check(final String[] host, final String[] cns,
202                                    final String[] subjectAlts)
203                throws SSLException {
204                check(host, cns, subjectAlts, true, true);
205            }
206
207            @Override
208            public final String toString() { return "STRICT_IE6"; }
209        };
210
211    /**
212     * The ALLOW_ALL HostnameVerifier essentially turns hostname verification
213     * off.  This implementation is a no-op, and never throws the SSLException.
214     */
215    public final static SSLHostnameVerifier ALLOW_ALL =
216        new AbstractVerifier() {
217            @Override
218            public final void check(final String[] host, final String[] cns,
219                                    final String[] subjectAlts) {
220                // Allow everything - so never blowup.
221            }
222
223            @Override
224            public final String toString() { return "ALLOW_ALL"; }
225        };
226
227    @SuppressWarnings("unchecked")
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            TreeSet names = new TreeSet();
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 it = names.iterator(); it.hasNext();) {
408                // Don't trim the CN, though!
409                String cn = (String) it.next();
410                cn = cn.toLowerCase();
411                // Store CN in StringBuffer in case we need to report an error.
412                buf.append(" <");
413                buf.append(cn);
414                buf.append('>');
415                if (it.hasNext()) {
416                    buf.append(" OR");
417                }
418
419                // The CN better have at least two dots if it wants wildcard
420                // action.  It also can't be [*.co.uk] or [*.co.jp] or
421                // [*.org.uk], etc...
422                boolean doWildcard = cn.startsWith("*.") &&
423                                     cn.lastIndexOf('.') >= 0 &&
424                                     !isIP4Address(cn) &&
425                                     acceptableCountryWildcard(cn);
426
427                for (int i = 0; i < hosts.length; i++) {
428                    final String hostName = hosts[i].trim().toLowerCase();
429                    if (doWildcard) {
430                        match = hostName.endsWith(cn.substring(1));
431                        if (match && strictWithSubDomains) {
432                            // If we're in strict mode, then [*.foo.com] is not
433                            // allowed to match [a.b.foo.com]
434                            match = countDots(hostName) == countDots(cn);
435                        }
436                    } else {
437                        match = hostName.equals(cn);
438                    }
439                    if (match) {
440                        break out;
441                    }
442                }
443            }
444            if (!match) {
445                throw new SSLException("hostname in certificate didn't match: " + hostnames + " !=" + buf);
446            }
447        }
448
449        public static boolean isIP4Address(final String cn) {
450            boolean isIP4 = true;
451            String tld = cn;
452            int x = cn.lastIndexOf('.');
453            // We only bother analyzing the characters after the final dot
454            // in the name.
455            if (x >= 0 && x + 1 < cn.length()) {
456                tld = cn.substring(x + 1);
457            }
458            for (int i = 0; i < tld.length(); i++) {
459                if (!Character.isDigit(tld.charAt(0))) {
460                    isIP4 = false;
461                    break;
462                }
463            }
464            return isIP4;
465        }
466
467        public static boolean acceptableCountryWildcard(final String cn) {
468            int cnLen = cn.length();
469            if (cnLen >= 7 && cnLen <= 9) {
470                // Look for the '.' in the 3rd-last position:
471                if (cn.charAt(cnLen - 3) == '.') {
472                    // Trim off the [*.] and the [.XX].
473                    String s = cn.substring(2, cnLen - 3);
474                    // And test against the sorted array of bad 2lds:
475                    int x = Arrays.binarySearch(BAD_COUNTRY_2LDS, s);
476                    return x < 0;
477                }
478            }
479            return true;
480        }
481
482        public static boolean isLocalhost(String host) {
483            host = host != null ? host.trim().toLowerCase() : "";
484            if (host.startsWith("::1")) {
485                int x = host.lastIndexOf('%');
486                if (x >= 0) {
487                    host = host.substring(0, x);
488                }
489            }
490            int x = Arrays.binarySearch(LOCALHOSTS, host);
491            return x >= 0;
492        }
493
494        /**
495         * Counts the number of dots "." in a string.
496         *
497         * @param s string to count dots from
498         * @return number of dots
499         */
500        public static int countDots(final String s) {
501            int count = 0;
502            for (int i = 0; i < s.length(); i++) {
503                if (s.charAt(i) == '.') {
504                    count++;
505                }
506            }
507            return count;
508        }
509    }
510
511    @SuppressWarnings("unchecked")
512    static class Certificates {
513      public static String[] getCNs(X509Certificate cert) {
514        LinkedList cnList = new LinkedList();
515        /*
516          Sebastian Hauer's original StrictSSLProtocolSocketFactory used
517          getName() and had the following comment:
518
519             Parses a X.500 distinguished name for the value of the
520             "Common Name" field.  This is done a bit sloppy right
521             now and should probably be done a bit more according to
522             <code>RFC 2253</code>.
523
524           I've noticed that toString() seems to do a better job than
525           getName() on these X500Principal objects, so I'm hoping that
526           addresses Sebastian's concern.
527
528           For example, getName() gives me this:
529           1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
530
531           whereas toString() gives me this:
532           [email protected]
533
534           Looks like toString() even works with non-ascii domain names!
535           I tested it with "&#x82b1;&#x5b50;.co.jp" and it worked fine.
536          */
537        String subjectPrincipal = cert.getSubjectX500Principal().toString();
538        StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
539        while (st.hasMoreTokens()) {
540            String tok = st.nextToken();
541            int x = tok.indexOf("CN=");
542            if (x >= 0) {
543                cnList.add(tok.substring(x + 3));
544            }
545        }
546        if (!cnList.isEmpty()) {
547            String[] cns = new String[cnList.size()];
548            cnList.toArray(cns);
549            return cns;
550        } else {
551            return null;
552        }
553      }
554
555
556      /**
557       * Extracts the array of SubjectAlt DNS names from an X509Certificate.
558       * Returns null if there aren't any.
559       * <p/>
560       * Note:  Java doesn't appear able to extract international characters
561       * from the SubjectAlts.  It can only extract international characters
562       * from the CN field.
563       * <p/>
564       * (Or maybe the version of OpenSSL I'm using to test isn't storing the
565       * international characters correctly in the SubjectAlts?).
566       *
567       * @param cert X509Certificate
568       * @return Array of SubjectALT DNS names stored in the certificate.
569       */
570      public static String[] getDNSSubjectAlts(X509Certificate cert) {
571          LinkedList subjectAltList = new LinkedList();
572          Collection c = null;
573          try {
574              c = cert.getSubjectAlternativeNames();
575          }
576          catch (CertificateParsingException cpe) {
577              // Should probably log.debug() this?
578              cpe.printStackTrace();
579          }
580          if (c != null) {
581              Iterator it = c.iterator();
582              while (it.hasNext()) {
583                  List list = (List) it.next();
584                  int type = ((Integer) list.get(0)).intValue();
585                  // If type is 2, then we've got a dNSName
586                  if (type == 2) {
587                      String s = (String) list.get(1);
588                      subjectAltList.add(s);
589                  }
590              }
591          }
592          if (!subjectAltList.isEmpty()) {
593              String[] subjectAlts = new String[subjectAltList.size()];
594              subjectAltList.toArray(subjectAlts);
595              return subjectAlts;
596          } else {
597              return null;
598          }
599      }
600    }
601
602}