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 * <https://www.apache.org/>.
029 *
030 */
031
032package org.apache.hadoop.security.ssl;
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.security.cert.Certificate;
037import java.security.cert.CertificateParsingException;
038import java.security.cert.X509Certificate;
039import java.util.Arrays;
040import java.util.Collection;
041import java.util.Iterator;
042import java.util.LinkedList;
043import java.util.List;
044import java.util.Set;
045import java.util.StringTokenizer;
046import java.util.TreeSet;
047
048import javax.net.ssl.SSLException;
049import javax.net.ssl.SSLPeerUnverifiedException;
050import javax.net.ssl.SSLSession;
051import javax.net.ssl.SSLSocket;
052
053import org.apache.hadoop.classification.InterfaceAudience;
054import org.apache.hadoop.classification.InterfaceStability;
055import org.apache.hadoop.util.StringUtils;
056
057/**
058 ************************************************************************
059 * Copied from the not-yet-commons-ssl project at
060 * http://juliusdavies.ca/commons-ssl/
061 * This project is not yet in Apache, but it is Apache 2.0 licensed.
062 ************************************************************************
063 * Interface for checking if a hostname matches the names stored inside the
064 * server's X.509 certificate.  Correctly implements
065 * javax.net.ssl.HostnameVerifier, but that interface is not recommended.
066 * Instead we added several check() methods that take SSLSocket,
067 * or X509Certificate, or ultimately (they all end up calling this one),
068 * String.  (It's easier to supply JUnit with Strings instead of mock
069 * SSLSession objects!)
070 * </p><p>Our check() methods throw exceptions if the name is
071 * invalid, whereas javax.net.ssl.HostnameVerifier just returns true/false.
072 * <p/>
073 * We provide the HostnameVerifier.DEFAULT, HostnameVerifier.STRICT, and
074 * HostnameVerifier.ALLOW_ALL implementations.  We also provide the more
075 * specialized HostnameVerifier.DEFAULT_AND_LOCALHOST, as well as
076 * HostnameVerifier.STRICT_IE6.  But feel free to define your own
077 * implementations!
078 * <p/>
079 * Inspired by Sebastian Hauer's original StrictSSLProtocolSocketFactory in the
080 * HttpClient "contrib" repository.
081 */
082@InterfaceAudience.Private
083@InterfaceStability.Evolving
084public interface SSLHostnameVerifier extends javax.net.ssl.HostnameVerifier {
085
086    @Override
087    boolean verify(String host, SSLSession session);
088
089    void check(String host, SSLSocket ssl) throws IOException;
090
091    void check(String host, X509Certificate cert) throws SSLException;
092
093    void check(String host, String[] cns, String[] subjectAlts)
094        throws SSLException;
095
096    void check(String[] hosts, SSLSocket ssl) throws IOException;
097
098    void check(String[] hosts, X509Certificate cert) throws SSLException;
099
100
101    /**
102     * Checks to see if the supplied hostname matches any of the supplied CNs
103     * or "DNS" Subject-Alts.  Most implementations only look at the first CN,
104     * and ignore any additional CNs.  Most implementations do look at all of
105     * the "DNS" Subject-Alts. The CNs or Subject-Alts may contain wildcards
106     * according to RFC 2818.
107     *
108     * @param cns         CN fields, in order, as extracted from the X.509
109     *                    certificate.
110     * @param subjectAlts Subject-Alt fields of type 2 ("DNS"), as extracted
111     *                    from the X.509 certificate.
112     * @param hosts       The array of hostnames to verify.
113     * @throws SSLException If verification failed.
114     */
115    void check(String[] hosts, String[] cns, String[] subjectAlts)
116        throws SSLException;
117
118
119    /**
120     * The DEFAULT HostnameVerifier works the same way as Curl and Firefox.
121     * <p/>
122     * The hostname must match either the first CN, or any of the subject-alts.
123     * A wildcard can occur in the CN, and in any of the subject-alts.
124     * <p/>
125     * The only difference between DEFAULT and STRICT is that a wildcard (such
126     * as "*.foo.com") with DEFAULT matches all subdomains, including
127     * "a.b.foo.com".
128     */
129    public final static SSLHostnameVerifier DEFAULT =
130        new AbstractVerifier() {
131            @Override
132            public final void check(final String[] hosts, final String[] cns,
133                                    final String[] subjectAlts)
134                throws SSLException {
135                check(hosts, cns, subjectAlts, false, false);
136            }
137
138            @Override
139            public final String toString() { return "DEFAULT"; }
140        };
141
142
143    /**
144     * The DEFAULT_AND_LOCALHOST HostnameVerifier works like the DEFAULT
145     * one with one additional relaxation:  a host of "localhost",
146     * "localhost.localdomain", "127.0.0.1", "::1" will always pass, no matter
147     * what is in the server's certificate.
148     */
149    public final static SSLHostnameVerifier DEFAULT_AND_LOCALHOST =
150        new AbstractVerifier() {
151            @Override
152            public final void check(final String[] hosts, final String[] cns,
153                                    final String[] subjectAlts)
154                throws SSLException {
155                if (isLocalhost(hosts[0])) {
156                    return;
157                }
158                check(hosts, cns, subjectAlts, false, false);
159            }
160
161            @Override
162            public final String toString() { return "DEFAULT_AND_LOCALHOST"; }
163        };
164
165    /**
166     * The STRICT HostnameVerifier works the same way as java.net.URL in Sun
167     * Java 1.4, Sun Java 5, Sun Java 6.  It's also pretty close to IE6.
168     * This implementation appears to be compliant with RFC 2818 for dealing
169     * with wildcards.
170     * <p/>
171     * The hostname must match either the first CN, or any of the subject-alts.
172     * A wildcard can occur in the CN, and in any of the subject-alts.  The
173     * one divergence from IE6 is how we only check the first CN.  IE6 allows
174     * a match against any of the CNs present.  We decided to follow in
175     * Sun Java 1.4's footsteps and only check the first CN.
176     * <p/>
177     * A wildcard such as "*.foo.com" matches only subdomains in the same
178     * level, for example "a.foo.com".  It does not match deeper subdomains
179     * such as "a.b.foo.com".
180     */
181    public final static SSLHostnameVerifier STRICT =
182        new AbstractVerifier() {
183            @Override
184            public final void check(final String[] host, final String[] cns,
185                                    final String[] subjectAlts)
186                throws SSLException {
187                check(host, cns, subjectAlts, false, true);
188            }
189
190            @Override
191            public final String toString() { return "STRICT"; }
192        };
193
194    /**
195     * The STRICT_IE6 HostnameVerifier works just like the STRICT one with one
196     * minor variation:  the hostname can match against any of the CN's in the
197     * server's certificate, not just the first one.  This behaviour is
198     * identical to IE6's behaviour.
199     */
200    public final static SSLHostnameVerifier STRICT_IE6 =
201        new AbstractVerifier() {
202            @Override
203            public final void check(final String[] host, final String[] cns,
204                                    final String[] subjectAlts)
205                throws SSLException {
206                check(host, cns, subjectAlts, true, true);
207            }
208
209            @Override
210            public final String toString() { return "STRICT_IE6"; }
211        };
212
213    /**
214     * The ALLOW_ALL HostnameVerifier essentially turns hostname verification
215     * off.  This implementation is a no-op, and never throws the SSLException.
216     */
217    public final static SSLHostnameVerifier ALLOW_ALL =
218        new AbstractVerifier() {
219            @Override
220            public final void check(final String[] host, final String[] cns,
221                                    final String[] subjectAlts) {
222                // Allow everything - so never blowup.
223            }
224
225            @Override
226            public final String toString() { return "ALLOW_ALL"; }
227        };
228
229    abstract class AbstractVerifier implements SSLHostnameVerifier {
230
231        /**
232         * This contains a list of 2nd-level domains that aren't allowed to
233         * have wildcards when combined with country-codes.
234         * For example: [*.co.uk].
235         * <p/>
236         * The [*.co.uk] problem is an interesting one.  Should we just hope
237         * that CA's would never foolishly allow such a certificate to happen?
238         * Looks like we're the only implementation guarding against this.
239         * Firefox, Curl, Sun Java 1.4, 5, 6 don't bother with this check.
240         */
241        private final static String[] BAD_COUNTRY_2LDS =
242            {"ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info",
243                "lg", "ne", "net", "or", "org"};
244
245        private final static String[] LOCALHOSTS = {"::1", "127.0.0.1",
246            "localhost",
247            "localhost.localdomain"};
248
249
250        static {
251            // Just in case developer forgot to manually sort the array.  :-)
252            Arrays.sort(BAD_COUNTRY_2LDS);
253            Arrays.sort(LOCALHOSTS);
254        }
255
256        protected AbstractVerifier() {}
257
258        /**
259         * The javax.net.ssl.HostnameVerifier contract.
260         *
261         * @param host    'hostname' we used to create our socket
262         * @param session SSLSession with the remote server
263         * @return true if the host matched the one in the certificate.
264         */
265        @Override
266        public boolean verify(String host, SSLSession session) {
267            try {
268                Certificate[] certs = session.getPeerCertificates();
269                X509Certificate x509 = (X509Certificate) certs[0];
270                check(new String[]{host}, x509);
271                return true;
272            }
273            catch (SSLException e) {
274                return false;
275            }
276        }
277
278        @Override
279        public void check(String host, SSLSocket ssl) throws IOException {
280            check(new String[]{host}, ssl);
281        }
282
283        @Override
284        public void check(String host, X509Certificate cert)
285            throws SSLException {
286            check(new String[]{host}, cert);
287        }
288
289        @Override
290        public void check(String host, String[] cns, String[] subjectAlts)
291            throws SSLException {
292            check(new String[]{host}, cns, subjectAlts);
293        }
294
295        @Override
296        public void check(String host[], SSLSocket ssl)
297            throws IOException {
298            if (host == null) {
299                throw new NullPointerException("host to verify is null");
300            }
301
302            SSLSession session = ssl.getSession();
303            if (session == null) {
304                // In our experience this only happens under IBM 1.4.x when
305                // spurious (unrelated) certificates show up in the server'
306                // chain.  Hopefully this will unearth the real problem:
307                InputStream in = ssl.getInputStream();
308                in.available();
309                /*
310                  If you're looking at the 2 lines of code above because
311                  you're running into a problem, you probably have two
312                  options:
313
314                    #1.  Clean up the certificate chain that your server
315                         is presenting (e.g. edit "/etc/apache2/server.crt"
316                         or wherever it is your server's certificate chain
317                         is defined).
318
319                                               OR
320
321                    #2.   Upgrade to an IBM 1.5.x or greater JVM, or switch
322                          to a non-IBM JVM.
323                */
324
325                // If ssl.getInputStream().available() didn't cause an
326                // exception, maybe at least now the session is available?
327                session = ssl.getSession();
328                if (session == null) {
329                    // If it's still null, probably a startHandshake() will
330                    // unearth the real problem.
331                    ssl.startHandshake();
332
333                    // Okay, if we still haven't managed to cause an exception,
334                    // might as well go for the NPE.  Or maybe we're okay now?
335                    session = ssl.getSession();
336                }
337            }
338            Certificate[] certs;
339            try {
340                certs = session.getPeerCertificates();
341            } catch (SSLPeerUnverifiedException spue) {
342                InputStream in = ssl.getInputStream();
343                in.available();
344                // Didn't trigger anything interesting?  Okay, just throw
345                // original.
346                throw spue;
347            }
348            X509Certificate x509 = (X509Certificate) certs[0];
349            check(host, x509);
350        }
351
352        @Override
353        public void check(String[] host, X509Certificate cert)
354            throws SSLException {
355            String[] cns = Certificates.getCNs(cert);
356            String[] subjectAlts = Certificates.getDNSSubjectAlts(cert);
357            check(host, cns, subjectAlts);
358        }
359
360        public void check(final String[] hosts, final String[] cns,
361                          final String[] subjectAlts, final boolean ie6,
362                          final boolean strictWithSubDomains)
363            throws SSLException {
364            // Build up lists of allowed hosts For logging/debugging purposes.
365            StringBuffer buf = new StringBuffer(32);
366            buf.append('<');
367            for (int i = 0; i < hosts.length; i++) {
368                String h = hosts[i];
369                h = h != null ? StringUtils.toLowerCase(h.trim()) : "";
370                hosts[i] = h;
371                if (i > 0) {
372                    buf.append('/');
373                }
374                buf.append(h);
375            }
376            buf.append('>');
377            String hostnames = buf.toString();
378            // Build the list of names we're going to check.  Our DEFAULT and
379            // STRICT implementations of the HostnameVerifier only use the
380            // first CN provided.  All other CNs are ignored.
381            // (Firefox, wget, curl, Sun Java 1.4, 5, 6 all work this way).
382            final Set<String> names = new TreeSet<String>();
383            if (cns != null && cns.length > 0 && cns[0] != null) {
384                names.add(cns[0]);
385                if (ie6) {
386                    for (int i = 1; i < cns.length; i++) {
387                        names.add(cns[i]);
388                    }
389                }
390            }
391            if (subjectAlts != null) {
392                for (int i = 0; i < subjectAlts.length; i++) {
393                    if (subjectAlts[i] != null) {
394                        names.add(subjectAlts[i]);
395                    }
396                }
397            }
398            if (names.isEmpty()) {
399                String msg = "Certificate for " + hosts[0] + " doesn't contain CN or DNS subjectAlt";
400                throw new SSLException(msg);
401            }
402
403            // StringBuffer for building the error message.
404            buf = new StringBuffer();
405
406            boolean match = false;
407            out:
408            for (Iterator<String> it = names.iterator(); it.hasNext();) {
409                // Don't trim the CN, though!
410                final String cn = StringUtils.toLowerCase(it.next());
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 =
429                        StringUtils.toLowerCase(hosts[i].trim());
430                    if (doWildcard) {
431                        match = hostName.endsWith(cn.substring(1));
432                        if (match && strictWithSubDomains) {
433                            // If we're in strict mode, then [*.foo.com] is not
434                            // allowed to match [a.b.foo.com]
435                            match = countDots(hostName) == countDots(cn);
436                        }
437                    } else {
438                        match = hostName.equals(cn);
439                    }
440                    if (match) {
441                        break out;
442                    }
443                }
444            }
445            if (!match) {
446                throw new SSLException("hostname in certificate didn't match: " + hostnames + " !=" + buf);
447            }
448        }
449
450        public static boolean isIP4Address(final String cn) {
451            boolean isIP4 = true;
452            String tld = cn;
453            int x = cn.lastIndexOf('.');
454            // We only bother analyzing the characters after the final dot
455            // in the name.
456            if (x >= 0 && x + 1 < cn.length()) {
457                tld = cn.substring(x + 1);
458            }
459            for (int i = 0; i < tld.length(); i++) {
460                if (!Character.isDigit(tld.charAt(0))) {
461                    isIP4 = false;
462                    break;
463                }
464            }
465            return isIP4;
466        }
467
468        public static boolean acceptableCountryWildcard(final String cn) {
469            int cnLen = cn.length();
470            if (cnLen >= 7 && cnLen <= 9) {
471                // Look for the '.' in the 3rd-last position:
472                if (cn.charAt(cnLen - 3) == '.') {
473                    // Trim off the [*.] and the [.XX].
474                    String s = cn.substring(2, cnLen - 3);
475                    // And test against the sorted array of bad 2lds:
476                    int x = Arrays.binarySearch(BAD_COUNTRY_2LDS, s);
477                    return x < 0;
478                }
479            }
480            return true;
481        }
482
483        public static boolean isLocalhost(String host) {
484            host = host != null ? StringUtils.toLowerCase(host.trim()) : "";
485            if (host.startsWith("::1")) {
486                int x = host.lastIndexOf('%');
487                if (x >= 0) {
488                    host = host.substring(0, x);
489                }
490            }
491            int x = Arrays.binarySearch(LOCALHOSTS, host);
492            return x >= 0;
493        }
494
495        /**
496         * Counts the number of dots "." in a string.
497         *
498         * @param s string to count dots from
499         * @return number of dots
500         */
501        public static int countDots(final String s) {
502            int count = 0;
503            for (int i = 0; i < s.length(); i++) {
504                if (s.charAt(i) == '.') {
505                    count++;
506                }
507            }
508            return count;
509        }
510    }
511
512    static class Certificates {
513      public static String[] getCNs(X509Certificate cert) {
514        final List<String> cnList = new LinkedList<String>();
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           EMAILADDRESS=juliusdavies@cucbc.com
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          final List<String> subjectAltList = new LinkedList<String>();
572          Collection<List<?>> 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<List<?>> it = c.iterator();
582              while (it.hasNext()) {
583                  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}