]> sjero.net Git - wget/blobdiff - src/gnutls.c
Fix a linker error on systems with an older gnutls version.
[wget] / src / gnutls.c
index 17c7c9287662b10292cc5c7b22babd33b3ae750e..78f83385a2608d2f3a2a7312d07b3517337781f9 100644 (file)
@@ -1,5 +1,5 @@
 /* SSL support via GnuTLS library.
-   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
    Foundation, Inc.
 
 This file is part of GNU Wget.
@@ -32,9 +32,7 @@ as that of the covered work.  */
 
 #include <assert.h>
 #include <errno.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 #include <dirent.h>
@@ -42,6 +40,7 @@ as that of the covered work.  */
 
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
+#include <sys/ioctl.h>
 
 #include "utils.h"
 #include "connect.h"
@@ -67,6 +66,8 @@ ssl_init ()
 
   gnutls_global_init ();
   gnutls_certificate_allocate_credentials (&credentials);
+  gnutls_certificate_set_verify_flags(credentials,
+                                      GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
 
   ca_directory = opt.ca_directory ? opt.ca_directory : "/etc/ssl/certs";
 
@@ -104,7 +105,8 @@ ssl_init ()
   return true;
 }
 
-struct wgnutls_transport_context {
+struct wgnutls_transport_context
+{
   gnutls_session session;       /* GnuTLS session handle */
   int last_error;               /* last error returned by read/write/... */
 
@@ -140,10 +142,11 @@ wgnutls_read (int fd, char *buf, int bufsize, void *arg)
 
   do
     ret = gnutls_record_recv (ctx->session, buf, bufsize);
-  while (ret == GNUTLS_E_INTERRUPTED);
+  while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN);
 
   if (ret < 0)
     ctx->last_error = ret;
+
   return ret;
 }
 
@@ -154,7 +157,7 @@ wgnutls_write (int fd, char *buf, int bufsize, void *arg)
   struct wgnutls_transport_context *ctx = arg;
   do
     ret = gnutls_record_send (ctx->session, buf, bufsize);
-  while (ret == GNUTLS_E_INTERRUPTED);
+  while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN);
   if (ret < 0)
     ctx->last_error = ret;
   return ret;
@@ -171,7 +174,7 @@ wgnutls_poll (int fd, double timeout, int wait_for, void *arg)
 static int
 wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
 {
-  int ret = 0;
+  int read = 0;
   struct wgnutls_transport_context *ctx = arg;
   int offset = MIN (bufsize, ctx->peeklen);
   if (bufsize > sizeof ctx->peekbuf)
@@ -182,24 +185,30 @@ wgnutls_peek (int fd, char *buf, int bufsize, void *arg)
 
   if (bufsize > offset)
     {
-      do
+      if (gnutls_record_check_pending (ctx->session) <= 0
+          && select_fd (fd, 0.0, WAIT_FOR_READ) <= 0)
+        read = 0;
+      else
+        read = gnutls_record_recv (ctx->session, buf + offset,
+                                   bufsize - offset);
+
+      if (read < 0)
         {
-          if (gnutls_record_check_pending (ctx->session)
-              || select_fd (fd, 0, WAIT_FOR_READ))
-            ret = gnutls_record_recv (ctx->session, buf + offset,
-                                      bufsize - offset);
+          if (offset)
+            read = 0;
+          else
+            return read;
         }
-      while (ret == GNUTLS_E_INTERRUPTED);
 
-      if (ret > 0)
+      if (read > 0)
         {
           memcpy (ctx->peekbuf + offset, buf + offset,
-                  ret);
-          ctx->peeklen += ret;
+                  read);
+          ctx->peeklen += read;
         }
     }
 
-  return offset + ret;
+  return offset + read;
 }
 
 static const char *
@@ -222,7 +231,8 @@ wgnutls_close (int fd, void *arg)
 /* gnutls_transport is the singleton that describes the SSL transport
    methods provided by this file.  */
 
-static struct transport_implementation wgnutls_transport = {
+static struct transport_implementation wgnutls_transport =
+{
   wgnutls_read, wgnutls_write, wgnutls_poll,
   wgnutls_peek, wgnutls_errstr, wgnutls_close
 };
@@ -236,7 +246,6 @@ ssl_connect_wget (int fd)
   struct wgnutls_transport_context *ctx;
   gnutls_session session;
   int err;
-  int allowed_protocols[4] = {0, 0, 0, 0};
   gnutls_init (&session, GNUTLS_CLIENT);
   gnutls_set_default_priority (session);
   gnutls_certificate_type_set_priority (session, cert_type_priority);
@@ -247,6 +256,23 @@ ssl_connect_wget (int fd)
   gnutls_transport_set_ptr (session, (gnutls_transport_ptr) FD_TO_SOCKET (fd));
 
   err = 0;
+#if HAVE_GNUTLS_PRIORITY_SET_DIRECT
+  switch (opt.secure_protocol)
+    {
+    case secure_protocol_auto:
+      break;
+    case secure_protocol_sslv2:
+    case secure_protocol_sslv3:
+      err = gnutls_priority_set_direct (session, "NORMAL:-VERS-TLS-ALL", NULL);
+      break;
+    case secure_protocol_tlsv1:
+      err = gnutls_priority_set_direct (session, "NORMAL:-VERS-SSL3.0", NULL);
+      break;
+    default:
+      abort ();
+    }
+#else
+  int allowed_protocols[4] = {0, 0, 0, 0};
   switch (opt.secure_protocol)
     {
     case secure_protocol_auto:
@@ -256,15 +282,19 @@ ssl_connect_wget (int fd)
       allowed_protocols[0] = GNUTLS_SSL3;
       err = gnutls_protocol_set_priority (session, allowed_protocols);
       break;
+
     case secure_protocol_tlsv1:
       allowed_protocols[0] = GNUTLS_TLS1_0;
       allowed_protocols[1] = GNUTLS_TLS1_1;
       allowed_protocols[2] = GNUTLS_TLS1_2;
       err = gnutls_protocol_set_priority (session, allowed_protocols);
       break;
+
     default:
       abort ();
     }
+#endif
+
   if (err < 0)
     {
       logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err));