]> sjero.net Git - wget/blobdiff - src/openssl.c
[svn] Support wildcards for matching of hosts against common names presented by
[wget] / src / openssl.c
index 91552dee674da286b78a48bfc7678b784cd1c9ea..0ae55eb42318293285a715a1ae62006f58c67244 100644 (file)
@@ -41,12 +41,9 @@ so, delete this exception statement from your version.  */
 # include <strings.h>
 #endif
 
-#include <openssl/bio.h>
-#include <openssl/crypto.h>
-#include <openssl/x509.h>
 #include <openssl/ssl.h>
+#include <openssl/x509.h>
 #include <openssl/err.h>
-#include <openssl/pem.h>
 #include <openssl/rand.h>
 
 #include "wget.h"
@@ -132,33 +129,6 @@ init_prng (void)
 #endif
 }
 
-/* #### Someone should audit and document this. */
-
-static int
-verify_callback (int ok, X509_STORE_CTX *ctx)
-{
-  char buf[256];
-  /* #### Why are we not using the result of this call? */
-  X509_NAME_oneline (X509_get_subject_name (ctx->current_cert),
-                    buf, sizeof (buf));
-  if (ok == 0)
-    {
-      switch (ctx->error)
-       {
-       case X509_V_ERR_CERT_NOT_YET_VALID:
-       case X509_V_ERR_CERT_HAS_EXPIRED:
-         /* This mean the CERT is not valid !!! */
-         ok = 0;
-         break;
-       case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
-         /* Unsure if we should handle that this way */
-         ok = 1;
-         break;
-       }
-    }
-  return ok;
-}
-
 /* Print errors in the OpenSSL error stack. */
 
 static void
@@ -192,7 +162,7 @@ key_type_to_ssl_type (enum keyfile_type type)
 /* Create an SSL Context and set default paths etc.  Called the first
    time an HTTP download is attempted.
 
-   Returns 0 on success, non-zero otherwise.  */
+   Returns 1 on success, 0 otherwise.  */
 
 int
 ssl_init ()
@@ -241,9 +211,11 @@ ssl_init ()
 
   SSL_CTX_set_default_verify_paths (ssl_ctx);
   SSL_CTX_load_verify_locations (ssl_ctx, opt.ca_cert, opt.ca_directory);
+
+  /* Specify whether the connect should fail if the verification of
+     the peer fails or if it should go ahead.  */
   SSL_CTX_set_verify (ssl_ctx,
-                     opt.check_cert ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
-                     verify_callback);
+                     opt.check_cert ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
 
   if (opt.cert_file)
     if (SSL_CTX_use_certificate_file (ssl_ctx, opt.cert_file,
@@ -256,6 +228,10 @@ ssl_init ()
        != 1)
       goto error;
 
+  /* Since fd_write unconditionally assumes partial writes (and
+     handles them correctly), allow them in OpenSSL.  */
+  SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
+
   return 1;
 
  error:
@@ -346,12 +322,12 @@ ssl_connect (int fd)
   assert (ssl_ctx != NULL);
   ssl = SSL_new (ssl_ctx);
   if (!ssl)
-    goto err;
+    goto error;
   if (!SSL_set_fd (ssl, fd))
-    goto err;
+    goto error;
   SSL_set_connect_state (ssl);
   if (SSL_connect (ssl) <= 0 || ssl->state != SSL_ST_OK)
-    goto err;
+    goto error;
 
   /* Register FD with Wget's transport layer, i.e. arrange that
      SSL-enabled functions are used for reading, writing, and polling.
@@ -363,9 +339,136 @@ ssl_connect (int fd)
           (unsigned long) ssl));
   return 1;
 
- err:
+ error:
   print_errors ();
   if (ssl)
     SSL_free (ssl);
   return 0;
 }
+
+/* Return 1 is STRING (case-insensitively) matches PATTERN, 0
+   otherwise.  The recognized wildcard character is "*", which matches
+   any character in STRING except ".".  Any number of the "*" wildcard
+   may be present in the pattern.
+
+   This is used to match of hosts as indicated in rfc2818: "Names may
+   contain the wildcard character * which is considered to match any
+   single domain name component or component fragment. E.g., *.a.com
+   matches foo.a.com but not bar.foo.a.com. f*.com matches foo.com but
+   not bar.com."  */
+
+static int
+pattern_match (const char *pattern, const char *string)
+{
+  const char *p = pattern, *n = string;
+  char c;
+  for (; (c = TOLOWER (*p++)) != '\0'; n++)
+    if (c == '*')
+      {
+       for (c = TOLOWER (*p); c == '*'; c = TOLOWER (*++p))
+         ;
+       for (; *n != '\0'; n++)
+         if (TOLOWER (*n) == c && pattern_match (p, n))
+           return 1;
+         else if (*n == '.')
+           return 0;
+       return c == '\0';
+      }
+    else
+      {
+       if (c != TOLOWER (*n))
+         return 0;
+      }
+  return *n == '\0';
+}
+
+/* Check that the identity of the remote host, as presented by its
+   server certificate, corresponds to HOST, which is the host name the
+   user thinks he's connecting to.  This assumes that FD has been
+   connected to an SSL context using ssl_connect.  Return 1 if the
+   identity checks out, 0 otherwise.
+
+   If opt.check_cert is 0, this always returns 1, but still warns the
+   user about the mismatches, if any.  */
+
+int
+ssl_check_server_identity (int fd, const char *host)
+{
+  X509 *peer_cert = NULL;
+  char peer_CN[256];
+  long vresult;
+  int retval;
+
+  /* If the user has specified --no-check-cert, we still want to warn
+     him about problems with the server's certificate.  */
+  const char *severity = opt.check_cert ? _("ERROR") : _("WARNING");
+
+  SSL *ssl = (SSL *) fd_transport_context (fd);
+  assert (ssl != NULL);
+
+  peer_cert = SSL_get_peer_certificate (ssl);
+  if (!peer_cert)
+    {
+      logprintf (LOG_NOTQUIET, _("%s: No certificate presented by %s.\n"),
+                severity, escnonprint (host));
+      retval = 0;
+      goto out;
+    }
+
+#ifdef ENABLE_DEBUG
+  if (opt.debug)
+    {
+      char *subject = X509_NAME_oneline (X509_get_subject_name (peer_cert), 0, 0);
+      char *issuer = X509_NAME_oneline (X509_get_issuer_name (peer_cert), 0, 0);
+      DEBUGP (("certificate:\n  subject: %s\n  issuer:  %s\n",
+              escnonprint (subject), escnonprint (issuer)));
+      OPENSSL_free (subject);
+      OPENSSL_free (issuer);
+    }
+#endif
+
+  vresult = SSL_get_verify_result (ssl);
+  if (vresult != X509_V_OK)
+    {
+      logprintf (LOG_NOTQUIET,
+                _("%s: Certificate verification error for %s: %s\n"),
+                severity, escnonprint (host),
+                X509_verify_cert_error_string (vresult));
+      retval = 0;
+      goto out;
+    }
+
+  /* Check that the common name in the presented certificate matches
+     HOST.  This should be improved in the following ways:
+
+     - It should use dNSName/ipAddress subjectAltName extensions if
+       available; according to rfc2818: "If a subjectAltName extension
+       of type dNSName is present, that MUST be used as the identity."
+
+     - When matching against common names, it should loop over all
+       common names and choose the most specific (apparently the last
+       one).  */
+
+  peer_CN[0] = '\0';
+  X509_NAME_get_text_by_NID (X509_get_subject_name (peer_cert),
+                            NID_commonName, peer_CN, sizeof (peer_CN));
+  if (!pattern_match (peer_CN, host))
+    {
+      logprintf (LOG_NOTQUIET, _("\
+%s: certificate common name `%s' doesn't match requested host name `%s'.\n"),
+                severity, escnonprint (peer_CN), escnonprint (host));
+      retval = 0;
+      goto out;
+    }
+
+  /* The certificate was found, verified, and matched HOST. */
+  retval = 1;
+
+ out:
+  if (peer_cert)
+    X509_free (peer_cert);
+
+  /* Allow --no-check-cert to disable certificate checking. */
+  return opt.check_cert ? retval : 1;
+}
+