]> sjero.net Git - wget/blobdiff - src/gen_sslfunc.c
[svn] Remove the "rbuf" buffering layer. Provide peeking primitives instead.
[wget] / src / gen_sslfunc.c
index 2fe2e38631d43006c7494e9e2ce404956ac0a0dc..98e75a1dba6258d77b840a1b05b093aae79b3010 100644 (file)
@@ -58,7 +58,9 @@ so, delete this exception statement from your version.  */
 extern int errno;
 #endif
 
-void
+SSL_CTX *ssl_ctx;
+
+static void
 ssl_init_prng (void)
 {
   /* It is likely that older versions of OpenSSL will fail on
@@ -96,29 +98,24 @@ ssl_init_prng (void)
     return;
 #endif
 
-  /* Still not enough randomness, presumably because neither random
-     file nor EGD have been available.  Use the stupidest possible
-     method -- seed OpenSSL's PRNG with the system's PRNG.  This is
-     insecure in the cryptographic sense, but people who care about
-     security will use /dev/random or their own source of randomness
-     anyway.  */
+  /* Still not enough randomness, most likely because neither
+     /dev/random nor EGD were available.  Resort to a simple and
+     stupid method -- seed OpenSSL's PRNG with libc PRNG.  This is
+     cryptographically weak, but people who care about strong
+     cryptography should install /dev/random (default on Linux) or
+     specify their own source of randomness anyway.  */
+
+  logprintf (LOG_VERBOSE, _("Warning: using a weak random seed.\n"));
 
   while (RAND_status () == 0 && maxrand-- > 0)
     {
       unsigned char rnd = random_number (256);
       RAND_seed (&rnd, sizeof (rnd));
     }
-
-  if (RAND_status () == 0)
-    {
-      logprintf (LOG_NOTQUIET,
-                _("Could not seed OpenSSL PRNG; disabling SSL.\n"));
-      scheme_disable (SCHEME_HTTPS);
-    }
 #endif /* SSLEAY_VERSION_NUMBER >= 0x00905100 */
 }
 
-int
+static int
 verify_callback (int ok, X509_STORE_CTX *ctx)
 {
   char *s, buf[256];
@@ -139,30 +136,40 @@ verify_callback (int ok, X509_STORE_CTX *ctx)
   return ok;
 }
 
-/* pass all ssl errors to DEBUGP
-   returns the number of printed errors */
-int
-ssl_printerrors (void) 
+/* Print SSL errors. */
+
+void
+ssl_print_errors (void) 
 {
-  int ocerr = 0;
   unsigned long curerr = 0;
   char errbuff[1024];
   xzero (errbuff);
   while ((curerr = ERR_get_error ()) != 0)
-    {
-      DEBUGP (("OpenSSL: %s\n", ERR_error_string (curerr, errbuff)));
-      ++ocerr;
-    }
-  return ocerr;
+    logprintf (LOG_NOTQUIET, "OpenSSL: %s\n",
+              ERR_error_string (curerr, errbuff));
 }
 
 /* Creates a SSL Context and sets some defaults for it */
 uerr_t
-init_ssl (SSL_CTX **ctx)
+ssl_init ()
 {
   SSL_METHOD *meth = NULL;
   int verify;
   int can_validate;
+
+  if (ssl_ctx)
+    return 0;
+
+  /* Init the PRNG.  If that fails, bail out.  */
+  ssl_init_prng ();
+  if (RAND_status () == 0)
+    {
+      logprintf (LOG_NOTQUIET,
+                _("Could not seed OpenSSL PRNG; disabling SSL.\n"));
+      scheme_disable (SCHEME_HTTPS);
+      return SSLERRCTXCREATE;
+    }
+
   SSL_library_init ();
   SSL_load_error_strings ();
   SSLeay_add_all_algorithms ();
@@ -184,20 +191,20 @@ init_ssl (SSL_CTX **ctx)
     }
   if (meth == NULL)
     {
-      ssl_printerrors ();
+      ssl_print_errors ();
       return SSLERRCTXCREATE;
     }
 
-  *ctx = SSL_CTX_new (meth);
+  ssl_ctx = SSL_CTX_new (meth);
   if (meth == NULL)
     {
-      ssl_printerrors ();
+      ssl_print_errors ();
       return SSLERRCTXCREATE;
     }
   /* Can we validate the server Cert ? */
   if (opt.sslcadir != NULL || opt.sslcafile != NULL)
     {
-      SSL_CTX_load_verify_locations (*ctx, opt.sslcafile, opt.sslcadir);
+      SSL_CTX_load_verify_locations (ssl_ctx, opt.sslcafile, opt.sslcadir);
       can_validate = 1;
     }
   else
@@ -224,7 +231,7 @@ init_ssl (SSL_CTX **ctx)
        }
     }
 
-  SSL_CTX_set_verify (*ctx, verify, verify_callback);
+  SSL_CTX_set_verify (ssl_ctx, verify, verify_callback);
 
   if (opt.sslcertfile != NULL || opt.sslcertkey != NULL)
     {
@@ -239,14 +246,16 @@ init_ssl (SSL_CTX **ctx)
       if (opt.sslcertfile == NULL)
        opt.sslcertfile = opt.sslcertkey; 
 
-      if (SSL_CTX_use_certificate_file (*ctx, opt.sslcertfile, ssl_cert_type) <= 0)
+      if (SSL_CTX_use_certificate_file (ssl_ctx, opt.sslcertfile,
+                                       ssl_cert_type) <= 0)
        {
-         ssl_printerrors ();
+         ssl_print_errors ();
          return SSLERRCERTFILE;
        }
-      if (SSL_CTX_use_PrivateKey_file  (*ctx, opt.sslcertkey , ssl_cert_type) <= 0)
+      if (SSL_CTX_use_PrivateKey_file  (ssl_ctx, opt.sslcertkey,
+                                       ssl_cert_type) <= 0)
        {
-         ssl_printerrors ();
+         ssl_print_errors ();
          return SSLERRCERTKEY;
        }
     }
@@ -254,28 +263,30 @@ init_ssl (SSL_CTX **ctx)
   return 0; /* Succeded */
 }
 
-static
-int ssl_read (int fd, char *buf, int bufsize, void *ctx)
+static int
+ssl_read (int fd, char *buf, int bufsize, void *ctx)
 {
-  int res;
+  int ret;
   SSL *ssl = (SSL *) ctx;
-  /* #### Does SSL_read actually set EINTR? */
   do
-    res = SSL_read (ssl, buf, bufsize);
-  while (res == -1 && errno == EINTR);
-  return res;
+    ret = SSL_read (ssl, buf, bufsize);
+  while (ret == -1
+        && SSL_get_error (ssl, ret) == SSL_ERROR_SYSCALL
+        && errno == EINTR);
+  return ret;
 }
 
 static int
 ssl_write (int fd, char *buf, int bufsize, void *ctx)
 {
-  int res = 0;
+  int ret = 0;
   SSL *ssl = (SSL *) ctx;
-  /* #### Does SSL_write actually set EINTR? */
   do
-    res = SSL_write (ssl, buf, bufsize);
-  while (res == -1 && errno == EINTR);
-  return res;
+    ret = SSL_write (ssl, buf, bufsize);
+  while (ret == -1
+        && SSL_get_error (ssl, ret) == SSL_ERROR_SYSCALL
+        && errno == EINTR);
+  return ret;
 }
 
 static int
@@ -286,11 +297,20 @@ ssl_poll (int fd, double timeout, int wait_for, void *ctx)
     return 1;
   if (SSL_pending (ssl))
     return 1;
-#ifdef HAVE_SELECT
   return select_fd (fd, timeout, wait_for);
-#else
-  return 1;
-#endif
+}
+
+static int
+ssl_peek (int fd, char *buf, int bufsize, void *ctx)
+{
+  int ret;
+  SSL *ssl = (SSL *) ctx;
+  do
+    ret = SSL_peek (ssl, buf, bufsize);
+  while (ret == -1
+        && SSL_get_error (ssl, ret) == SSL_ERROR_SYSCALL
+        && errno == EINTR);
+  return ret;
 }
 
 static void
@@ -312,9 +332,12 @@ ssl_close (int fd, void *ctx)
 /* Sets up a SSL structure and performs the handshake on fd. */
 
 SSL *
-connect_ssl (int fd, SSL_CTX *ctx
+ssl_connect (int fd
 {
-  SSL *ssl = SSL_new (ctx);
+  SSL *ssl;
+
+  assert (ssl_ctx != NULL);
+  ssl = SSL_new (ssl_ctx);
   if (!ssl)
     goto err;
   if (!SSL_set_fd (ssl, fd))
@@ -323,22 +346,18 @@ connect_ssl (int fd, SSL_CTX *ctx)
   if (SSL_connect (ssl) <= 0 || ssl->state != SSL_ST_OK)
     goto err;
 
-  /* Register the FD to use our functions for read, write, etc.  That
-     way the rest of Wget can keep using xread, xwrite, and
-     friends.  */
-  register_extended (fd, ssl_read, ssl_write, ssl_poll, ssl_close, ssl);
+  /* Register FD with Wget's transport layer, i.e. arrange that
+     SSL-enabled functions are used for reading, writing, and polling.
+     That way the rest of Wget can keep using xread, xwrite, and
+     friends and not care what happens underneath.  */
+  fd_register_transport (fd, ssl_read, ssl_write, ssl_poll, ssl_peek,
+                        ssl_close, ssl);
   DEBUGP (("Connected %d to SSL 0x%0lx\n", fd, (unsigned long) ssl));
   return ssl;
 
  err:
-  ssl_printerrors ();
+  ssl_print_errors ();
   if (ssl)
     SSL_free (ssl);
   return NULL;
 }
-
-void
-free_ssl_ctx (SSL_CTX * ctx)
-{
-  SSL_CTX_free (ctx);
-}