2005-04-07 11:27:13 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-01-16 16:33:49 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-04-07 11:27:13 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all GnuTLS-specific code for the TLS/SSL layer. No code
|
2013-12-25 05:30:51 -05:00
|
|
|
* but vtls.c should ever call or use these functions.
|
2005-04-07 11:27:13 -04:00
|
|
|
*
|
|
|
|
* Note: don't use the GnuTLS' *_t variable type names in this source code,
|
|
|
|
* since they were not present in 1.0.X.
|
|
|
|
*/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
#ifdef USE_GNUTLS
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2014-10-01 02:14:49 -04:00
|
|
|
#include <gnutls/abstract.h>
|
2005-04-07 11:27:13 -04:00
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/x509.h>
|
2012-12-14 11:38:18 -05:00
|
|
|
|
2012-08-06 09:04:25 -04:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
|
|
|
#include <gnutls/crypto.h>
|
|
|
|
#include <nettle/md5.h>
|
|
|
|
#else
|
2009-02-12 15:48:40 -05:00
|
|
|
#include <gcrypt.h>
|
2011-11-15 04:52:32 -05:00
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "gtls.h"
|
2013-12-25 05:20:39 -05:00
|
|
|
#include "vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "parsedate.h"
|
|
|
|
#include "connect.h" /* for the connect timeout */
|
|
|
|
#include "select.h"
|
|
|
|
#include "rawstr.h"
|
2014-07-15 15:28:10 -04:00
|
|
|
#include "warnless.h"
|
2008-11-18 03:53:51 -05:00
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2005-04-07 11:27:13 -04:00
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2009-10-19 14:10:47 -04:00
|
|
|
/*
|
|
|
|
Some hackish cast macros based on:
|
|
|
|
http://library.gnome.org/devel/glib/unstable/glib-Type-Conversion-Macros.html
|
|
|
|
*/
|
|
|
|
#ifndef GNUTLS_POINTER_TO_INT_CAST
|
|
|
|
#define GNUTLS_POINTER_TO_INT_CAST(p) ((int) (long) (p))
|
|
|
|
#endif
|
|
|
|
#ifndef GNUTLS_INT_TO_POINTER_CAST
|
|
|
|
#define GNUTLS_INT_TO_POINTER_CAST(i) ((void*) (long) (i))
|
|
|
|
#endif
|
|
|
|
|
2005-04-09 17:38:14 -04:00
|
|
|
/* Enable GnuTLS debugging by defining GTLSDEBUG */
|
|
|
|
/*#define GTLSDEBUG */
|
|
|
|
|
|
|
|
#ifdef GTLSDEBUG
|
|
|
|
static void tls_log_func(int level, const char *str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "|<%d>| %s", level, str);
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-28 17:01:30 -04:00
|
|
|
static bool gtls_inited = FALSE;
|
2010-11-19 23:00:12 -05:00
|
|
|
|
2011-10-29 08:58:50 -04:00
|
|
|
#if defined(GNUTLS_VERSION_NUMBER)
|
|
|
|
# if (GNUTLS_VERSION_NUMBER >= 0x020c00)
|
|
|
|
# undef gnutls_transport_set_lowat
|
|
|
|
# define gnutls_transport_set_lowat(A,B) Curl_nop_stmt
|
2011-11-02 17:44:22 -04:00
|
|
|
# define USE_GNUTLS_PRIORITY_SET_DIRECT 1
|
2011-10-29 08:58:50 -04:00
|
|
|
# endif
|
2011-11-24 06:11:52 -05:00
|
|
|
# if (GNUTLS_VERSION_NUMBER >= 0x020c03)
|
|
|
|
# define GNUTLS_MAPS_WINSOCK_ERRORS 1
|
2011-10-29 08:58:50 -04:00
|
|
|
# endif
|
2014-02-04 03:10:37 -05:00
|
|
|
|
|
|
|
# ifdef USE_NGHTTP2
|
|
|
|
# undef HAS_ALPN
|
|
|
|
# if (GNUTLS_VERSION_NUMBER >= 0x030200)
|
|
|
|
# define HAS_ALPN
|
|
|
|
# endif
|
|
|
|
# endif
|
2014-06-16 07:21:02 -04:00
|
|
|
|
|
|
|
# if (GNUTLS_VERSION_NUMBER >= 0x03020d)
|
|
|
|
# define HAS_OCSP
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_OCSP
|
|
|
|
# include <gnutls/ocsp.h>
|
2011-10-29 08:58:50 -04:00
|
|
|
#endif
|
|
|
|
|
2006-12-16 16:33:51 -05:00
|
|
|
/*
|
|
|
|
* Custom push and pull callback functions used by GNU TLS to read and write
|
|
|
|
* to the socket. These functions are simple wrappers to send() and recv()
|
2013-01-06 13:06:49 -05:00
|
|
|
* (although here using the sread/swrite macros as defined by
|
|
|
|
* curl_setup_once.h).
|
2006-12-16 16:33:51 -05:00
|
|
|
* We use custom functions rather than the GNU TLS defaults because it allows
|
|
|
|
* us to get specific about the fourth "flags" argument, and to use arbitrary
|
|
|
|
* private data with gnutls_transport_set_ptr if we wish.
|
2010-11-19 16:31:34 -05:00
|
|
|
*
|
2010-11-19 23:00:12 -05:00
|
|
|
* When these custom push and pull callbacks fail, GNU TLS checks its own
|
|
|
|
* session-specific error variable, and when not set also its own global
|
|
|
|
* errno variable, in order to take appropriate action. GNU TLS does not
|
|
|
|
* require that the transport is actually a socket. This implies that for
|
|
|
|
* Windows builds these callbacks should ideally set the session-specific
|
|
|
|
* error variable using function gnutls_transport_set_errno or as a last
|
|
|
|
* resort global errno variable using gnutls_transport_set_global_errno,
|
|
|
|
* with a transport agnostic error value. This implies that some winsock
|
|
|
|
* error translation must take place in these callbacks.
|
2011-11-24 06:11:52 -05:00
|
|
|
*
|
|
|
|
* Paragraph above applies to GNU TLS versions older than 2.12.3, since
|
|
|
|
* this version GNU TLS does its own internal winsock error translation
|
|
|
|
* using system_errno() function.
|
2006-12-16 16:33:51 -05:00
|
|
|
*/
|
2010-11-19 23:00:12 -05:00
|
|
|
|
2011-11-24 06:11:52 -05:00
|
|
|
#if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
|
2010-11-19 23:00:12 -05:00
|
|
|
# define gtls_EINTR 4
|
|
|
|
# define gtls_EIO 5
|
|
|
|
# define gtls_EAGAIN 11
|
|
|
|
static int gtls_mapped_sockerrno(void)
|
2010-11-19 16:31:34 -05:00
|
|
|
{
|
2010-11-19 23:00:12 -05:00
|
|
|
switch(SOCKERRNO) {
|
2010-11-19 16:31:34 -05:00
|
|
|
case WSAEWOULDBLOCK:
|
2010-11-19 23:00:12 -05:00
|
|
|
return gtls_EAGAIN;
|
2010-11-19 16:31:34 -05:00
|
|
|
case WSAEINTR:
|
2010-11-19 23:00:12 -05:00
|
|
|
return gtls_EINTR;
|
2010-11-19 16:31:34 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-19 23:00:12 -05:00
|
|
|
return gtls_EIO;
|
2010-11-19 16:31:34 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-16 16:33:51 -05:00
|
|
|
static ssize_t Curl_gtls_push(void *s, const void *buf, size_t len)
|
|
|
|
{
|
2010-11-19 16:31:34 -05:00
|
|
|
ssize_t ret = swrite(GNUTLS_POINTER_TO_INT_CAST(s), buf, len);
|
2011-11-24 06:11:52 -05:00
|
|
|
#if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
|
2010-11-19 23:00:12 -05:00
|
|
|
if(ret < 0)
|
|
|
|
gnutls_transport_set_global_errno(gtls_mapped_sockerrno());
|
2010-11-19 16:31:34 -05:00
|
|
|
#endif
|
|
|
|
return ret;
|
2006-12-16 16:33:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t Curl_gtls_pull(void *s, void *buf, size_t len)
|
|
|
|
{
|
2010-11-19 16:31:34 -05:00
|
|
|
ssize_t ret = sread(GNUTLS_POINTER_TO_INT_CAST(s), buf, len);
|
2011-11-24 06:11:52 -05:00
|
|
|
#if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
|
2010-11-19 23:00:12 -05:00
|
|
|
if(ret < 0)
|
|
|
|
gnutls_transport_set_global_errno(gtls_mapped_sockerrno());
|
2010-11-19 16:31:34 -05:00
|
|
|
#endif
|
|
|
|
return ret;
|
2006-12-16 16:33:51 -05:00
|
|
|
}
|
2005-04-09 17:38:14 -04:00
|
|
|
|
2009-02-25 07:51:17 -05:00
|
|
|
/* Curl_gtls_init()
|
|
|
|
*
|
|
|
|
* Global GnuTLS init, called from Curl_ssl_init(). This calls functions that
|
|
|
|
* are not thread-safe and thus this function itself is not thread-safe and
|
|
|
|
* must only be called from within curl_global_init() to keep the thread
|
|
|
|
* situation under control!
|
2007-04-28 17:01:30 -04:00
|
|
|
*/
|
2009-02-25 07:51:17 -05:00
|
|
|
int Curl_gtls_init(void)
|
2007-04-28 17:01:30 -04:00
|
|
|
{
|
|
|
|
int ret = 1;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!gtls_inited) {
|
2007-04-28 17:01:30 -04:00
|
|
|
ret = gnutls_global_init()?0:1;
|
2005-04-09 17:38:14 -04:00
|
|
|
#ifdef GTLSDEBUG
|
2007-04-28 17:01:30 -04:00
|
|
|
gnutls_global_set_log_function(tls_log_func);
|
|
|
|
gnutls_global_set_log_level(2);
|
2005-04-09 17:38:14 -04:00
|
|
|
#endif
|
2007-04-28 17:01:30 -04:00
|
|
|
gtls_inited = TRUE;
|
|
|
|
}
|
|
|
|
return ret;
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Curl_gtls_cleanup(void)
|
|
|
|
{
|
2008-02-08 17:02:00 -05:00
|
|
|
if(gtls_inited) {
|
2007-04-28 17:01:30 -04:00
|
|
|
gnutls_global_deinit();
|
2008-02-08 17:02:00 -05:00
|
|
|
gtls_inited = FALSE;
|
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showtime(struct SessionHandle *data,
|
|
|
|
const char *text,
|
|
|
|
time_t stamp)
|
|
|
|
{
|
|
|
|
struct tm buffer;
|
2011-02-07 09:00:48 -05:00
|
|
|
const struct tm *tm = &buffer;
|
|
|
|
CURLcode result = Curl_gmtime(stamp, &buffer);
|
|
|
|
if(result)
|
|
|
|
return;
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
snprintf(data->state.buffer,
|
|
|
|
BUFSIZE,
|
|
|
|
"\t %s: %s, %02d %s %4d %02d:%02d:%02d GMT\n",
|
|
|
|
text,
|
|
|
|
Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
|
|
|
|
tm->tm_mday,
|
|
|
|
Curl_month[tm->tm_mon],
|
|
|
|
tm->tm_year + 1900,
|
|
|
|
tm->tm_hour,
|
|
|
|
tm->tm_min,
|
|
|
|
tm->tm_sec);
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(data, "%s\n", data->state.buffer);
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 08:13:52 -04:00
|
|
|
static gnutls_datum_t load_file (const char *file)
|
2008-06-06 16:52:32 -04:00
|
|
|
{
|
|
|
|
FILE *f;
|
2014-04-29 08:13:52 -04:00
|
|
|
gnutls_datum_t loaded_file = { NULL, 0 };
|
2008-06-06 16:52:32 -04:00
|
|
|
long filelen;
|
|
|
|
void *ptr;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!(f = fopen(file, "r")))
|
2009-07-22 05:48:32 -04:00
|
|
|
return loaded_file;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(fseek(f, 0, SEEK_END) != 0
|
|
|
|
|| (filelen = ftell(f)) < 0
|
|
|
|
|| fseek(f, 0, SEEK_SET) != 0
|
|
|
|
|| !(ptr = malloc((size_t)filelen)))
|
2009-07-22 05:48:32 -04:00
|
|
|
goto out;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen) {
|
2009-07-22 05:48:32 -04:00
|
|
|
free(ptr);
|
|
|
|
goto out;
|
2008-06-06 16:52:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
loaded_file.data = ptr;
|
|
|
|
loaded_file.size = (unsigned int)filelen;
|
2009-07-22 05:48:32 -04:00
|
|
|
out:
|
|
|
|
fclose(f);
|
2008-06-06 16:52:32 -04:00
|
|
|
return loaded_file;
|
|
|
|
}
|
|
|
|
|
2014-04-29 08:13:52 -04:00
|
|
|
static void unload_file(gnutls_datum_t data) {
|
2008-06-06 16:52:32 -04:00
|
|
|
free(data.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
/* this function does a SSL/TLS (re-)handshake */
|
2005-10-22 17:05:07 -04:00
|
|
|
static CURLcode handshake(struct connectdata *conn,
|
|
|
|
int sockindex,
|
2010-04-16 16:43:01 -04:00
|
|
|
bool duringconnect,
|
|
|
|
bool nonblocking)
|
2005-10-22 17:05:07 -04:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2010-04-16 16:43:01 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2014-04-29 08:13:52 -04:00
|
|
|
gnutls_session_t session = conn->ssl[sockindex].session;
|
2010-04-16 16:43:01 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
long timeout_ms;
|
2005-10-22 17:05:07 -04:00
|
|
|
int rc;
|
2010-04-16 16:43:01 -04:00
|
|
|
int what;
|
2005-10-22 17:05:07 -04:00
|
|
|
|
2010-11-07 22:03:11 -05:00
|
|
|
for(;;) {
|
2010-04-16 16:43:01 -04:00
|
|
|
/* check allowed time left */
|
2011-01-04 17:07:58 -05:00
|
|
|
timeout_ms = Curl_timeleft(data, NULL, duringconnect);
|
2005-10-22 17:05:07 -04:00
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2005-10-22 17:05:07 -04:00
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
/* if ssl is expecting something, check if it's available. */
|
|
|
|
if(connssl->connecting_state == ssl_connect_2_reading
|
|
|
|
|| connssl->connecting_state == ssl_connect_2_writing) {
|
|
|
|
|
|
|
|
curl_socket_t writefd = ssl_connect_2_writing==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t readfd = ssl_connect_2_reading==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
|
|
|
|
what = Curl_socket_ready(readfd, writefd,
|
2011-05-18 14:48:42 -04:00
|
|
|
nonblocking?0:
|
2011-06-04 15:19:14 -04:00
|
|
|
timeout_ms?timeout_ms:1000);
|
2010-04-16 16:43:01 -04:00
|
|
|
if(what < 0) {
|
|
|
|
/* fatal error */
|
2007-03-27 14:16:35 -04:00
|
|
|
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
2005-10-22 17:05:07 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2010-04-16 16:43:01 -04:00
|
|
|
else if(0 == what) {
|
2010-11-14 06:42:29 -05:00
|
|
|
if(nonblocking)
|
2010-04-16 16:43:01 -04:00
|
|
|
return CURLE_OK;
|
2010-11-14 06:42:29 -05:00
|
|
|
else if(timeout_ms) {
|
2010-04-16 16:43:01 -04:00
|
|
|
/* timeout */
|
2010-11-14 06:42:29 -05:00
|
|
|
failf(data, "SSL connection timeout at %ld", timeout_ms);
|
2010-04-16 16:43:01 -04:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* socket is readable or writable */
|
2005-10-22 17:05:07 -04:00
|
|
|
}
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
rc = gnutls_handshake(session);
|
2005-10-22 17:05:07 -04:00
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
|
|
|
|
connssl->connecting_state =
|
|
|
|
gnutls_record_get_direction(session)?
|
|
|
|
ssl_connect_2_writing:ssl_connect_2_reading;
|
2012-10-24 05:47:32 -04:00
|
|
|
continue;
|
2010-11-14 06:42:29 -05:00
|
|
|
}
|
2012-10-24 05:47:32 -04:00
|
|
|
else if((rc < 0) && !gnutls_error_is_fatal(rc)) {
|
2012-11-06 13:45:51 -05:00
|
|
|
const char *strerr = NULL;
|
2012-10-24 08:34:00 -04:00
|
|
|
|
|
|
|
if(rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
|
|
|
|
int alert = gnutls_alert_get(session);
|
|
|
|
strerr = gnutls_alert_get_name(alert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strerr == NULL)
|
|
|
|
strerr = gnutls_strerror(rc);
|
|
|
|
|
|
|
|
failf(data, "gnutls_handshake() warning: %s", strerr);
|
2012-08-20 10:47:48 -04:00
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
else if(rc < 0) {
|
2012-11-06 13:45:51 -05:00
|
|
|
const char *strerr = NULL;
|
2012-10-24 08:34:00 -04:00
|
|
|
|
|
|
|
if(rc == GNUTLS_E_FATAL_ALERT_RECEIVED) {
|
|
|
|
int alert = gnutls_alert_get(session);
|
|
|
|
strerr = gnutls_alert_get_name(alert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strerr == NULL)
|
|
|
|
strerr = gnutls_strerror(rc);
|
|
|
|
|
|
|
|
failf(data, "gnutls_handshake() failed: %s", strerr);
|
2010-11-14 06:42:29 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2012-10-24 05:47:32 -04:00
|
|
|
|
|
|
|
/* Reset our connect state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
return CURLE_OK;
|
2010-04-16 16:43:01 -04:00
|
|
|
}
|
2005-10-22 17:05:07 -04:00
|
|
|
}
|
|
|
|
|
2014-04-29 08:13:52 -04:00
|
|
|
static gnutls_x509_crt_fmt_t do_file_type(const char *type)
|
2005-11-11 18:20:07 -05:00
|
|
|
{
|
|
|
|
if(!type || !type[0])
|
|
|
|
return GNUTLS_X509_FMT_PEM;
|
2008-10-16 04:23:48 -04:00
|
|
|
if(Curl_raw_equal(type, "PEM"))
|
2005-11-11 18:20:07 -05:00
|
|
|
return GNUTLS_X509_FMT_PEM;
|
2008-10-16 04:23:48 -04:00
|
|
|
if(Curl_raw_equal(type, "DER"))
|
2005-11-11 18:20:07 -05:00
|
|
|
return GNUTLS_X509_FMT_DER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
static CURLcode
|
|
|
|
gtls_connect_step1(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2005-04-07 11:27:13 -04:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2014-04-29 08:13:52 -04:00
|
|
|
gnutls_session_t session;
|
2005-04-07 11:27:13 -04:00
|
|
|
int rc;
|
|
|
|
void *ssl_sessionid;
|
|
|
|
size_t ssl_idsize;
|
2009-08-01 18:11:58 -04:00
|
|
|
bool sni = TRUE; /* default is SNI enabled */
|
2008-02-26 05:30:13 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr;
|
|
|
|
#else
|
|
|
|
struct in_addr addr;
|
|
|
|
#endif
|
2014-01-19 03:47:31 -05:00
|
|
|
#ifndef USE_GNUTLS_PRIORITY_SET_DIRECT
|
2014-07-11 18:33:16 -04:00
|
|
|
static const int cipher_priority[] = {
|
|
|
|
/* These two ciphers were added to GnuTLS as late as ver. 3.0.1,
|
|
|
|
but this code path is only ever used for ver. < 2.12.0.
|
|
|
|
GNUTLS_CIPHER_AES_128_GCM,
|
|
|
|
GNUTLS_CIPHER_AES_256_GCM,
|
|
|
|
*/
|
|
|
|
GNUTLS_CIPHER_AES_128_CBC,
|
|
|
|
GNUTLS_CIPHER_AES_256_CBC,
|
|
|
|
GNUTLS_CIPHER_CAMELLIA_128_CBC,
|
|
|
|
GNUTLS_CIPHER_CAMELLIA_256_CBC,
|
|
|
|
GNUTLS_CIPHER_3DES_CBC,
|
2014-01-19 03:47:31 -05:00
|
|
|
};
|
|
|
|
static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
|
|
|
|
static int protocol_priority[] = { 0, 0, 0, 0 };
|
|
|
|
#else
|
|
|
|
#define GNUTLS_CIPHERS "NORMAL:-ARCFOUR-128:-CTYPE-ALL:+CTYPE-X509"
|
2014-07-14 16:27:03 -04:00
|
|
|
/* If GnuTLS was compiled without support for SRP it will error out if SRP is
|
|
|
|
requested in the priority string, so treat it specially
|
|
|
|
*/
|
|
|
|
#define GNUTLS_SRP "+SRP"
|
2014-01-19 03:47:31 -05:00
|
|
|
const char* prioritylist;
|
2014-07-12 19:30:52 -04:00
|
|
|
const char *err = NULL;
|
2014-01-19 03:47:31 -05:00
|
|
|
#endif
|
2014-02-04 03:10:37 -05:00
|
|
|
#ifdef HAS_ALPN
|
|
|
|
int protocols_size = 2;
|
|
|
|
gnutls_datum_t protocols[2];
|
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2008-11-11 17:19:27 -05:00
|
|
|
if(conn->ssl[sockindex].state == ssl_connection_complete)
|
|
|
|
/* to make us tolerant against being called more than once for the
|
|
|
|
same connection */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2008-02-20 03:28:02 -05:00
|
|
|
if(!gtls_inited)
|
2009-02-25 07:51:17 -05:00
|
|
|
Curl_gtls_init();
|
2008-02-20 03:28:02 -05:00
|
|
|
|
2008-02-25 02:51:39 -05:00
|
|
|
/* GnuTLS only supports SSLv3 and TLSv1 */
|
2005-04-07 11:27:13 -04:00
|
|
|
if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
|
|
|
|
failf(data, "GnuTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2009-08-01 18:11:58 -04:00
|
|
|
else if(data->set.ssl.version == CURL_SSLVERSION_SSLv3)
|
|
|
|
sni = FALSE; /* SSLv3 has no SNI */
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* allocate a cred struct */
|
|
|
|
rc = gnutls_certificate_allocate_credentials(&conn->ssl[sockindex].cred);
|
2009-03-08 18:52:05 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
2005-04-13 08:38:01 -04:00
|
|
|
failf(data, "gnutls_cert_all_cred() failed: %s", gnutls_strerror(rc));
|
2005-04-07 11:27:13 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-01-19 14:35:02 -05:00
|
|
|
#ifdef USE_TLS_SRP
|
|
|
|
if(data->set.ssl.authtype == CURL_TLSAUTH_SRP) {
|
|
|
|
infof(data, "Using TLS-SRP username: %s\n", data->set.ssl.username);
|
|
|
|
|
|
|
|
rc = gnutls_srp_allocate_client_credentials(
|
|
|
|
&conn->ssl[sockindex].srp_client_cred);
|
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
|
|
|
failf(data, "gnutls_srp_allocate_client_cred() failed: %s",
|
|
|
|
gnutls_strerror(rc));
|
2011-02-09 17:34:30 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-01-19 14:35:02 -05:00
|
|
|
}
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
rc = gnutls_srp_set_client_credentials(conn->ssl[sockindex].
|
|
|
|
srp_client_cred,
|
2011-01-19 14:35:02 -05:00
|
|
|
data->set.ssl.username,
|
|
|
|
data->set.ssl.password);
|
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
|
|
|
failf(data, "gnutls_srp_set_client_cred() failed: %s",
|
|
|
|
gnutls_strerror(rc));
|
2011-02-09 17:34:30 -05:00
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
2011-01-19 14:35:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-09 18:33:14 -04:00
|
|
|
if(data->set.ssl.CAfile) {
|
|
|
|
/* set the trusted CA cert bundle file */
|
2005-08-24 03:40:13 -04:00
|
|
|
gnutls_certificate_set_verify_flags(conn->ssl[sockindex].cred,
|
|
|
|
GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
|
|
|
|
|
2005-04-09 18:33:14 -04:00
|
|
|
rc = gnutls_certificate_set_x509_trust_file(conn->ssl[sockindex].cred,
|
|
|
|
data->set.ssl.CAfile,
|
|
|
|
GNUTLS_X509_FMT_PEM);
|
2006-10-21 07:32:05 -04:00
|
|
|
if(rc < 0) {
|
2005-04-13 17:17:05 -04:00
|
|
|
infof(data, "error reading ca cert file %s (%s)\n",
|
2005-04-13 08:38:01 -04:00
|
|
|
data->set.ssl.CAfile, gnutls_strerror(rc));
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer)
|
2006-10-21 07:32:05 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
2005-08-24 03:40:13 -04:00
|
|
|
else
|
|
|
|
infof(data, "found %d certificates in %s\n",
|
|
|
|
rc, data->set.ssl.CAfile);
|
2005-04-07 18:47:43 -04:00
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2008-06-06 14:40:21 -04:00
|
|
|
if(data->set.ssl.CRLfile) {
|
|
|
|
/* set the CRL list file */
|
|
|
|
rc = gnutls_certificate_set_x509_crl_file(conn->ssl[sockindex].cred,
|
2009-02-25 07:51:17 -05:00
|
|
|
data->set.ssl.CRLfile,
|
|
|
|
GNUTLS_X509_FMT_PEM);
|
2008-06-06 14:40:21 -04:00
|
|
|
if(rc < 0) {
|
2012-06-14 07:32:05 -04:00
|
|
|
failf(data, "error reading crl file %s (%s)",
|
2008-06-06 14:40:21 -04:00
|
|
|
data->set.ssl.CRLfile, gnutls_strerror(rc));
|
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "found %d CRL in %s\n",
|
|
|
|
rc, data->set.ssl.CRLfile);
|
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Initialize TLS session as a client */
|
|
|
|
rc = gnutls_init(&conn->ssl[sockindex].session, GNUTLS_CLIENT);
|
2009-03-08 18:52:05 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(data, "gnutls_init() failed: %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convenient assign */
|
|
|
|
session = conn->ssl[sockindex].session;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if((0 == Curl_inet_pton(AF_INET, conn->host.name, &addr)) &&
|
2008-02-26 05:30:13 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
2011-04-20 09:17:42 -04:00
|
|
|
(0 == Curl_inet_pton(AF_INET6, conn->host.name, &addr)) &&
|
2008-02-26 05:30:13 -05:00
|
|
|
#endif
|
2011-04-20 09:17:42 -04:00
|
|
|
sni &&
|
|
|
|
(gnutls_server_name_set(session, GNUTLS_NAME_DNS, conn->host.name,
|
|
|
|
strlen(conn->host.name)) < 0))
|
2008-02-26 05:30:13 -05:00
|
|
|
infof(data, "WARNING: failed to configure server name indication (SNI) "
|
|
|
|
"TLS extension\n");
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Use default priorities */
|
|
|
|
rc = gnutls_set_default_priority(session);
|
2009-03-08 18:52:05 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS)
|
2005-04-07 11:27:13 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
|
2011-11-02 17:44:22 -04:00
|
|
|
#ifndef USE_GNUTLS_PRIORITY_SET_DIRECT
|
2014-01-19 03:47:31 -05:00
|
|
|
rc = gnutls_cipher_set_priority(session, cipher_priority);
|
|
|
|
if(rc != GNUTLS_E_SUCCESS)
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2008-02-25 02:51:39 -05:00
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Sets the priority on the certificate types supported by gnutls. Priority
|
2014-01-19 03:47:31 -05:00
|
|
|
is higher for types specified before others. After specifying the types
|
|
|
|
you want, you must append a 0. */
|
2005-04-07 11:27:13 -04:00
|
|
|
rc = gnutls_certificate_type_set_priority(session, cert_type_priority);
|
2009-03-08 18:52:05 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS)
|
2005-04-07 11:27:13 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2014-01-19 03:47:31 -05:00
|
|
|
|
|
|
|
if(data->set.ssl.cipher_list != NULL) {
|
|
|
|
failf(data, "can't pass a custom cipher list to older GnuTLS"
|
|
|
|
" versions");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (data->set.ssl.version) {
|
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
protocol_priority[0] = GNUTLS_SSL3;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
protocol_priority[0] = GNUTLS_TLS1_0;
|
|
|
|
protocol_priority[1] = GNUTLS_TLS1_1;
|
|
|
|
protocol_priority[2] = GNUTLS_TLS1_2;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
protocol_priority[0] = GNUTLS_TLS1_0;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
protocol_priority[0] = GNUTLS_TLS1_1;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
protocol_priority[0] = GNUTLS_TLS1_2;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_SSLv2:
|
|
|
|
default:
|
|
|
|
failf(data, "GnuTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = gnutls_protocol_set_priority(session, protocol_priority);
|
2014-07-12 19:30:52 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
|
|
|
failf(data, "Did you pass a valid GnuTLS cipher list?");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-01-19 03:47:31 -05:00
|
|
|
#else
|
2014-07-14 16:27:03 -04:00
|
|
|
/* Ensure +SRP comes at the *end* of all relevant strings so that it can be
|
|
|
|
* removed if a run-time error indicates that SRP is not supported by this
|
|
|
|
* GnuTLS version */
|
2014-01-19 03:47:31 -05:00
|
|
|
switch (data->set.ssl.version) {
|
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
prioritylist = GNUTLS_CIPHERS ":-VERS-TLS-ALL:+VERS-SSL3.0";
|
|
|
|
sni = false;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2014-07-14 16:27:03 -04:00
|
|
|
prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:" GNUTLS_SRP;
|
2014-01-19 03:47:31 -05:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
|
2014-07-14 16:27:03 -04:00
|
|
|
"+VERS-TLS1.0:" GNUTLS_SRP;
|
2014-01-19 03:47:31 -05:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
|
2014-07-14 16:27:03 -04:00
|
|
|
"+VERS-TLS1.1:" GNUTLS_SRP;
|
2014-01-19 03:47:31 -05:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
|
2014-07-14 16:27:03 -04:00
|
|
|
"+VERS-TLS1.2:" GNUTLS_SRP;
|
2014-01-19 03:47:31 -05:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_SSLv2:
|
|
|
|
default:
|
|
|
|
failf(data, "GnuTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = gnutls_priority_set_direct(session, prioritylist, &err);
|
2014-07-14 16:27:03 -04:00
|
|
|
if((rc == GNUTLS_E_INVALID_REQUEST) && err) {
|
|
|
|
if(!strcmp(err, GNUTLS_SRP)) {
|
|
|
|
/* This GnuTLS was probably compiled without support for SRP.
|
|
|
|
* Note that fact and try again without it. */
|
2014-07-15 15:28:10 -04:00
|
|
|
int validprioritylen = curlx_uztosi(err - prioritylist);
|
2014-07-14 16:27:03 -04:00
|
|
|
char *prioritycopy = strdup(prioritylist);
|
|
|
|
if(!prioritycopy)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
infof(data, "This GnuTLS does not support SRP\n");
|
|
|
|
if(validprioritylen)
|
|
|
|
/* Remove the :+SRP */
|
|
|
|
prioritycopy[validprioritylen - 1] = 0;
|
|
|
|
rc = gnutls_priority_set_direct(session, prioritycopy, &err);
|
|
|
|
free(prioritycopy);
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 19:30:52 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
|
|
|
failf(data, "Error %d setting GnuTLS cipher list starting with %s",
|
|
|
|
rc, err);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2011-11-02 17:44:22 -04:00
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2014-02-04 03:10:37 -05:00
|
|
|
#ifdef HAS_ALPN
|
2014-02-11 01:18:11 -05:00
|
|
|
if(data->set.httpversion == CURL_HTTP_VERSION_2_0) {
|
|
|
|
if(data->set.ssl_enable_alpn) {
|
|
|
|
protocols[0].data = NGHTTP2_PROTO_VERSION_ID;
|
|
|
|
protocols[0].size = NGHTTP2_PROTO_VERSION_ID_LEN;
|
|
|
|
protocols[1].data = ALPN_HTTP_1_1;
|
|
|
|
protocols[1].size = ALPN_HTTP_1_1_LENGTH;
|
|
|
|
gnutls_alpn_set_protocols(session, protocols, protocols_size, 0);
|
|
|
|
infof(data, "ALPN, offering %s, %s\n", NGHTTP2_PROTO_VERSION_ID,
|
|
|
|
ALPN_HTTP_1_1);
|
2014-12-08 18:09:24 -05:00
|
|
|
connssl->asked_for_h2 = TRUE;
|
2014-02-11 01:18:11 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "SSL, can't negotiate HTTP/2.0 without ALPN\n");
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 03:10:37 -05:00
|
|
|
#endif
|
2014-01-19 03:47:31 -05:00
|
|
|
|
2007-08-01 17:20:01 -04:00
|
|
|
if(data->set.str[STRING_CERT]) {
|
2011-04-22 17:01:30 -04:00
|
|
|
if(gnutls_certificate_set_x509_key_file(
|
|
|
|
conn->ssl[sockindex].cred,
|
|
|
|
data->set.str[STRING_CERT],
|
|
|
|
data->set.str[STRING_KEY] ?
|
|
|
|
data->set.str[STRING_KEY] : data->set.str[STRING_CERT],
|
|
|
|
do_file_type(data->set.str[STRING_CERT_TYPE]) ) !=
|
|
|
|
GNUTLS_E_SUCCESS) {
|
2005-11-11 18:20:07 -05:00
|
|
|
failf(data, "error reading X.509 key or certificate file");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-19 14:35:02 -05:00
|
|
|
#ifdef USE_TLS_SRP
|
2005-11-11 18:20:07 -05:00
|
|
|
/* put the credentials to the current session */
|
2011-01-19 14:35:02 -05:00
|
|
|
if(data->set.ssl.authtype == CURL_TLSAUTH_SRP) {
|
|
|
|
rc = gnutls_credentials_set(session, GNUTLS_CRD_SRP,
|
|
|
|
conn->ssl[sockindex].srp_client_cred);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc != GNUTLS_E_SUCCESS)
|
2011-01-19 14:35:02 -05:00
|
|
|
failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else
|
2011-01-19 14:35:02 -05:00
|
|
|
#endif
|
|
|
|
rc = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
|
|
conn->ssl[sockindex].cred);
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* set the connection handle (file descriptor for the socket) */
|
|
|
|
gnutls_transport_set_ptr(session,
|
2009-10-19 14:10:47 -04:00
|
|
|
GNUTLS_INT_TO_POINTER_CAST(conn->sock[sockindex]));
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2006-12-16 16:33:51 -05:00
|
|
|
/* register callback functions to send and receive data. */
|
|
|
|
gnutls_transport_set_push_function(session, Curl_gtls_push);
|
|
|
|
gnutls_transport_set_pull_function(session, Curl_gtls_pull);
|
|
|
|
|
|
|
|
/* lowat must be set to zero when using custom push and pull functions. */
|
|
|
|
gnutls_transport_set_lowat(session, 0);
|
|
|
|
|
2014-06-16 07:21:02 -04:00
|
|
|
#ifdef HAS_OCSP
|
|
|
|
if(data->set.ssl.verifystatus) {
|
|
|
|
rc = gnutls_ocsp_status_request_enable_client(session, NULL, 0, NULL);
|
|
|
|
if(rc != GNUTLS_E_SUCCESS) {
|
|
|
|
failf(data, "gnutls_ocsp_status_request_enable_client() failed: %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* This might be a reconnect, so we check for a session ID in the cache
|
|
|
|
to speed up things */
|
|
|
|
|
|
|
|
if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, &ssl_idsize)) {
|
|
|
|
/* we got a session id, use it! */
|
|
|
|
gnutls_session_set_data(session, ssl_sessionid, ssl_idsize);
|
|
|
|
|
|
|
|
/* Informational message */
|
|
|
|
infof (data, "SSL re-using session ID\n");
|
|
|
|
}
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-13 12:34:51 -04:00
|
|
|
static CURLcode pkp_pin_peer_pubkey(gnutls_x509_crt_t cert,
|
|
|
|
const char *pinnedpubkey)
|
2014-10-01 02:14:49 -04:00
|
|
|
{
|
|
|
|
/* Scratch */
|
|
|
|
size_t len1 = 0, len2 = 0;
|
2014-10-13 12:34:51 -04:00
|
|
|
unsigned char *buff1 = NULL;
|
2014-10-01 02:14:49 -04:00
|
|
|
|
|
|
|
gnutls_pubkey_t key = NULL;
|
|
|
|
|
|
|
|
/* Result is returned to caller */
|
2014-10-13 12:34:51 -04:00
|
|
|
int ret = 0;
|
|
|
|
CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
2014-10-01 02:14:49 -04:00
|
|
|
|
|
|
|
/* if a path wasn't specified, don't pin */
|
2014-10-13 16:22:49 -04:00
|
|
|
if(NULL == pinnedpubkey)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
if(NULL == cert)
|
|
|
|
return result;
|
2014-10-01 02:14:49 -04:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* Begin Gyrations to get the public key */
|
|
|
|
gnutls_pubkey_init(&key);
|
|
|
|
|
|
|
|
ret = gnutls_pubkey_import_x509(key, cert, 0);
|
|
|
|
if(ret < 0)
|
|
|
|
break; /* failed */
|
|
|
|
|
|
|
|
ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, NULL, &len1);
|
|
|
|
if(ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0)
|
2014-10-13 16:22:49 -04:00
|
|
|
break; /* failed */
|
2014-10-01 02:14:49 -04:00
|
|
|
|
|
|
|
buff1 = malloc(len1);
|
|
|
|
if(NULL == buff1)
|
|
|
|
break; /* failed */
|
|
|
|
|
|
|
|
len2 = len1;
|
|
|
|
|
|
|
|
ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, buff1, &len2);
|
|
|
|
if(ret < 0 || len1 != len2)
|
|
|
|
break; /* failed */
|
|
|
|
|
|
|
|
/* End Gyrations */
|
|
|
|
|
|
|
|
/* The one good exit point */
|
2014-10-13 12:34:51 -04:00
|
|
|
result = Curl_pin_peer_pubkey(pinnedpubkey, buff1, len1);
|
2014-10-01 02:14:49 -04:00
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(NULL != key)
|
|
|
|
gnutls_pubkey_deinit(key);
|
|
|
|
|
2014-10-13 12:34:51 -04:00
|
|
|
Curl_safefree(buff1);
|
2014-10-01 02:14:49 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static Curl_recv gtls_recv;
|
|
|
|
static Curl_send gtls_send;
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
static CURLcode
|
|
|
|
gtls_connect_step3(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
|
|
|
unsigned int cert_list_size;
|
2014-04-29 08:13:52 -04:00
|
|
|
const gnutls_datum_t *chainp;
|
2010-04-16 16:43:01 -04:00
|
|
|
unsigned int verify_status;
|
2014-04-29 08:13:52 -04:00
|
|
|
gnutls_x509_crt_t x509_cert,x509_issuer;
|
|
|
|
gnutls_datum_t issuerp;
|
2014-07-11 19:31:12 -04:00
|
|
|
char certbuf[256] = ""; /* big enough? */
|
2010-04-16 16:43:01 -04:00
|
|
|
size_t size;
|
|
|
|
unsigned int algo;
|
|
|
|
unsigned int bits;
|
|
|
|
time_t certclock;
|
|
|
|
const char *ptr;
|
|
|
|
struct SessionHandle *data = conn->data;
|
2014-04-29 08:13:52 -04:00
|
|
|
gnutls_session_t session = conn->ssl[sockindex].session;
|
2010-04-16 16:43:01 -04:00
|
|
|
int rc;
|
2014-12-25 12:15:15 -05:00
|
|
|
bool incache;
|
2010-04-16 16:43:01 -04:00
|
|
|
void *ssl_sessionid;
|
2014-02-04 03:10:37 -05:00
|
|
|
#ifdef HAS_ALPN
|
|
|
|
gnutls_datum_t proto;
|
|
|
|
#endif
|
2011-01-05 18:47:37 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* This function will return the peer's raw certificate (chain) as sent by
|
|
|
|
the peer. These certificates are in raw format (DER encoded for
|
|
|
|
X.509). In case of a X.509 then a certificate list may be present. The
|
|
|
|
first certificate in the list is the peer's certificate, following the
|
|
|
|
issuer's certificate, then the issuer's issuer etc. */
|
|
|
|
|
|
|
|
chainp = gnutls_certificate_get_peers(session, &cert_list_size);
|
|
|
|
if(!chainp) {
|
2008-06-06 16:52:32 -04:00
|
|
|
if(data->set.ssl.verifypeer ||
|
|
|
|
data->set.ssl.verifyhost ||
|
|
|
|
data->set.ssl.issuercert) {
|
2011-01-19 14:35:02 -05:00
|
|
|
#ifdef USE_TLS_SRP
|
|
|
|
if(data->set.ssl.authtype == CURL_TLSAUTH_SRP
|
|
|
|
&& data->set.ssl.username != NULL
|
|
|
|
&& !data->set.ssl.verifypeer
|
|
|
|
&& gnutls_cipher_get(session)) {
|
|
|
|
/* no peer cert, but auth is ok if we have SRP user and cipher and no
|
|
|
|
peer verify */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif
|
|
|
|
failf(data, "failed to get server cert");
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
#ifdef USE_TLS_SRP
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
infof(data, "\t common name: WARNING couldn't obtain\n");
|
|
|
|
}
|
|
|
|
|
2008-02-15 17:37:00 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
|
|
|
/* This function will try to verify the peer's certificate and return its
|
|
|
|
status (trusted, invalid etc.). The value of status should be one or
|
|
|
|
more of the gnutls_certificate_status_t enumerated elements bitwise
|
|
|
|
or'd. To avoid denial of service attacks some default upper limits
|
|
|
|
regarding the certificate key size and chain size are set. To override
|
|
|
|
them use gnutls_certificate_set_verify_limits(). */
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2008-02-15 17:37:00 -05:00
|
|
|
rc = gnutls_certificate_verify_peers2(session, &verify_status);
|
|
|
|
if(rc < 0) {
|
|
|
|
failf(data, "server cert verify failed: %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2008-02-15 17:37:00 -05:00
|
|
|
/* verify_status is a bitmask of gnutls_certificate_status bits */
|
|
|
|
if(verify_status & GNUTLS_CERT_INVALID) {
|
|
|
|
if(data->set.ssl.verifypeer) {
|
2008-06-06 16:52:32 -04:00
|
|
|
failf(data, "server certificate verification failed. CAfile: %s "
|
2009-02-25 07:51:17 -05:00
|
|
|
"CRLfile: %s", data->set.ssl.CAfile?data->set.ssl.CAfile:"none",
|
|
|
|
data->set.ssl.CRLfile?data->set.ssl.CRLfile:"none");
|
2008-02-15 17:37:00 -05:00
|
|
|
return CURLE_SSL_CACERT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate verification FAILED\n");
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
else
|
2008-02-15 17:37:00 -05:00
|
|
|
infof(data, "\t server certificate verification OK\n");
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
2013-11-29 16:46:05 -05:00
|
|
|
else
|
2008-02-15 17:37:00 -05:00
|
|
|
infof(data, "\t server certificate verification SKIPPED\n");
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2014-06-16 07:21:02 -04:00
|
|
|
#ifdef HAS_OCSP
|
|
|
|
if(data->set.ssl.verifystatus) {
|
|
|
|
if(gnutls_ocsp_status_request_is_checked(session, 0) == 0) {
|
|
|
|
if(verify_status & GNUTLS_CERT_REVOKED)
|
|
|
|
failf(data, "SSL server certificate was REVOKED\n");
|
|
|
|
else
|
|
|
|
failf(data, "SSL server certificate status verification FAILED");
|
|
|
|
|
|
|
|
return CURLE_SSL_INVALIDCERTSTATUS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "SSL server certificate status verification OK\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "SSL server certificate status verification SKIPPED\n");
|
|
|
|
#endif
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* initialize an X.509 certificate structure. */
|
|
|
|
gnutls_x509_crt_init(&x509_cert);
|
|
|
|
|
2014-04-22 17:24:31 -04:00
|
|
|
if(chainp)
|
|
|
|
/* convert the given DER or PEM encoded Certificate to the native
|
|
|
|
gnutls_x509_crt_t format */
|
|
|
|
gnutls_x509_crt_import(x509_cert, chainp, GNUTLS_X509_FMT_DER);
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(data->set.ssl.issuercert) {
|
2008-06-06 16:52:32 -04:00
|
|
|
gnutls_x509_crt_init(&x509_issuer);
|
|
|
|
issuerp = load_file(data->set.ssl.issuercert);
|
|
|
|
gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
|
|
|
|
rc = gnutls_x509_crt_check_issuer(x509_cert,x509_issuer);
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_issuer);
|
2008-06-06 16:52:32 -04:00
|
|
|
unload_file(issuerp);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc <= 0) {
|
2008-06-06 16:52:32 -04:00
|
|
|
failf(data, "server certificate issuer check failed (IssuerCert: %s)",
|
2009-02-25 07:51:17 -05:00
|
|
|
data->set.ssl.issuercert?data->set.ssl.issuercert:"none");
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2008-06-06 16:52:32 -04:00
|
|
|
return CURLE_SSL_ISSUER_ERROR;
|
|
|
|
}
|
|
|
|
infof(data,"\t server certificate issuer check OK (Issuer Cert: %s)\n",
|
2009-02-25 07:51:17 -05:00
|
|
|
data->set.ssl.issuercert?data->set.ssl.issuercert:"none");
|
2008-06-06 16:52:32 -04:00
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
size=sizeof(certbuf);
|
|
|
|
rc = gnutls_x509_crt_get_dn_by_oid(x509_cert, GNUTLS_OID_X520_COMMON_NAME,
|
|
|
|
0, /* the first and only one */
|
2005-04-22 16:56:26 -04:00
|
|
|
FALSE,
|
2005-04-07 11:27:13 -04:00
|
|
|
certbuf,
|
|
|
|
&size);
|
2005-04-22 16:56:26 -04:00
|
|
|
if(rc) {
|
|
|
|
infof(data, "error fetching CN from cert:%s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* This function will check if the given certificate's subject matches the
|
|
|
|
given hostname. This is a basic implementation of the matching described
|
|
|
|
in RFC2818 (HTTPS), which takes into account wildcards, and the subject
|
|
|
|
alternative name PKIX extension. Returns non zero on success, and zero on
|
|
|
|
failure. */
|
|
|
|
rc = gnutls_x509_crt_check_hostname(x509_cert, conn->host.name);
|
2014-07-14 14:04:55 -04:00
|
|
|
#if GNUTLS_VERSION_NUMBER < 0x030306
|
|
|
|
/* Before 3.3.6, gnutls_x509_crt_check_hostname() didn't check IP
|
|
|
|
addresses. */
|
|
|
|
if(!rc) {
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
#define use_addr in6_addr
|
|
|
|
#else
|
|
|
|
#define use_addr in_addr
|
|
|
|
#endif
|
|
|
|
unsigned char addrbuf[sizeof(struct use_addr)];
|
|
|
|
unsigned char certaddr[sizeof(struct use_addr)];
|
|
|
|
size_t addrlen = 0, certaddrlen;
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if(Curl_inet_pton(AF_INET, conn->host.name, addrbuf) > 0)
|
|
|
|
addrlen = 4;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else if(Curl_inet_pton(AF_INET6, conn->host.name, addrbuf) > 0)
|
|
|
|
addrlen = 16;
|
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2014-07-14 14:04:55 -04:00
|
|
|
if(addrlen) {
|
|
|
|
for(i=0; ; i++) {
|
|
|
|
certaddrlen = sizeof(certaddr);
|
|
|
|
ret = gnutls_x509_crt_get_subject_alt_name(x509_cert, i, certaddr,
|
|
|
|
&certaddrlen, NULL);
|
|
|
|
/* If this happens, it wasn't an IP address. */
|
|
|
|
if(ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
|
|
|
|
continue;
|
|
|
|
if(ret < 0)
|
|
|
|
break;
|
|
|
|
if(ret != GNUTLS_SAN_IPADDRESS)
|
|
|
|
continue;
|
|
|
|
if(certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen)) {
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
if(!rc) {
|
2012-10-27 06:31:39 -04:00
|
|
|
if(data->set.ssl.verifyhost) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(data, "SSL: certificate subject name (%s) does not match "
|
|
|
|
"target host name '%s'", certbuf, conn->host.dispname);
|
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2007-10-03 04:07:50 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t common name: %s (does not match '%s')\n",
|
|
|
|
certbuf, conn->host.dispname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t common name: %s (matched)\n", certbuf);
|
|
|
|
|
2007-07-10 17:36:30 -04:00
|
|
|
/* Check for time-based validity */
|
2008-02-16 08:41:55 -05:00
|
|
|
certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
|
2007-07-10 17:36:30 -04:00
|
|
|
|
2008-02-16 08:41:55 -05:00
|
|
|
if(certclock == (time_t)-1) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2014-07-11 17:21:31 -04:00
|
|
|
failf(data, "server cert expiration date verify failed");
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2014-07-11 17:21:31 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
else
|
2014-07-11 17:21:31 -04:00
|
|
|
infof(data, "\t server certificate expiration date verify FAILED\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(certclock < time(NULL)) {
|
|
|
|
if(data->set.ssl.verifypeer) {
|
|
|
|
failf(data, "server certificate expiration date has passed.");
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2014-07-11 17:21:31 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate expiration date FAILED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate expiration date OK\n");
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
|
2008-02-16 08:41:55 -05:00
|
|
|
certclock = gnutls_x509_crt_get_activation_time(x509_cert);
|
2007-07-10 17:36:30 -04:00
|
|
|
|
2008-02-16 08:41:55 -05:00
|
|
|
if(certclock == (time_t)-1) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2014-07-11 17:21:31 -04:00
|
|
|
failf(data, "server cert activation date verify failed");
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2014-07-11 17:21:31 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
else
|
2014-07-11 17:21:31 -04:00
|
|
|
infof(data, "\t server certificate activation date verify FAILED\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(certclock > time(NULL)) {
|
|
|
|
if(data->set.ssl.verifypeer) {
|
|
|
|
failf(data, "server certificate not activated yet.");
|
2014-10-13 12:34:51 -04:00
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
2014-07-11 17:21:31 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate activation date FAILED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate activation date OK\n");
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
|
2014-10-13 12:34:51 -04:00
|
|
|
ptr = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
|
|
|
|
if(ptr) {
|
|
|
|
result = pkp_pin_peer_pubkey(x509_cert, ptr);
|
|
|
|
if(result != CURLE_OK) {
|
|
|
|
failf(data, "SSL: public key does not match pinned public key!");
|
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
|
|
|
return result;
|
|
|
|
}
|
2014-10-01 02:14:49 -04:00
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Show:
|
|
|
|
|
|
|
|
- ciphers used
|
|
|
|
- subject
|
|
|
|
- start date
|
|
|
|
- expire date
|
|
|
|
- common name
|
|
|
|
- issuer
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* public key algorithm's parameters */
|
|
|
|
algo = gnutls_x509_crt_get_pk_algorithm(x509_cert, &bits);
|
|
|
|
infof(data, "\t certificate public key: %s\n",
|
|
|
|
gnutls_pk_algorithm_get_name(algo));
|
|
|
|
|
|
|
|
/* version of the X.509 certificate. */
|
|
|
|
infof(data, "\t certificate version: #%d\n",
|
|
|
|
gnutls_x509_crt_get_version(x509_cert));
|
|
|
|
|
|
|
|
|
|
|
|
size = sizeof(certbuf);
|
|
|
|
gnutls_x509_crt_get_dn(x509_cert, certbuf, &size);
|
|
|
|
infof(data, "\t subject: %s\n", certbuf);
|
|
|
|
|
2008-02-16 08:41:55 -05:00
|
|
|
certclock = gnutls_x509_crt_get_activation_time(x509_cert);
|
|
|
|
showtime(data, "start date", certclock);
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2008-02-16 08:41:55 -05:00
|
|
|
certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
|
|
|
|
showtime(data, "expire date", certclock);
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
size = sizeof(certbuf);
|
|
|
|
gnutls_x509_crt_get_issuer_dn(x509_cert, certbuf, &size);
|
|
|
|
infof(data, "\t issuer: %s\n", certbuf);
|
|
|
|
|
|
|
|
gnutls_x509_crt_deinit(x509_cert);
|
|
|
|
|
|
|
|
/* compression algorithm (if any) */
|
|
|
|
ptr = gnutls_compression_get_name(gnutls_compression_get(session));
|
|
|
|
/* the *_get_name() says "NULL" if GNUTLS_COMP_NULL is returned */
|
|
|
|
infof(data, "\t compression: %s\n", ptr);
|
|
|
|
|
|
|
|
/* the name of the cipher used. ie 3DES. */
|
|
|
|
ptr = gnutls_cipher_get_name(gnutls_cipher_get(session));
|
|
|
|
infof(data, "\t cipher: %s\n", ptr);
|
|
|
|
|
|
|
|
/* the MAC algorithms name. ie SHA1 */
|
|
|
|
ptr = gnutls_mac_get_name(gnutls_mac_get(session));
|
|
|
|
infof(data, "\t MAC: %s\n", ptr);
|
|
|
|
|
2014-02-04 03:10:37 -05:00
|
|
|
#ifdef HAS_ALPN
|
2014-02-11 01:18:11 -05:00
|
|
|
if(data->set.ssl_enable_alpn) {
|
|
|
|
rc = gnutls_alpn_get_selected_protocol(session, &proto);
|
|
|
|
if(rc == 0) {
|
|
|
|
infof(data, "ALPN, server accepted to use %.*s\n", proto.size,
|
|
|
|
proto.data);
|
|
|
|
|
|
|
|
if(proto.size == NGHTTP2_PROTO_VERSION_ID_LEN &&
|
|
|
|
memcmp(NGHTTP2_PROTO_VERSION_ID, proto.data,
|
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN) == 0) {
|
2014-03-31 02:40:24 -04:00
|
|
|
conn->negnpn = NPN_HTTP2;
|
2014-02-11 01:18:11 -05:00
|
|
|
}
|
|
|
|
else if(proto.size == ALPN_HTTP_1_1_LENGTH && memcmp(ALPN_HTTP_1_1,
|
|
|
|
proto.data, ALPN_HTTP_1_1_LENGTH) == 0) {
|
|
|
|
conn->negnpn = NPN_HTTP1_1;
|
|
|
|
}
|
2014-02-04 03:10:37 -05:00
|
|
|
}
|
2014-12-08 18:09:24 -05:00
|
|
|
else if(connssl->asked_for_h2) {
|
2014-02-11 01:18:11 -05:00
|
|
|
infof(data, "ALPN, server did not agree to a protocol\n");
|
2014-02-04 03:10:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-20 05:01:28 -05:00
|
|
|
conn->ssl[sockindex].state = ssl_connection_complete;
|
2010-05-11 16:48:38 -04:00
|
|
|
conn->recv[sockindex] = gtls_recv;
|
|
|
|
conn->send[sockindex] = gtls_send;
|
2008-02-20 04:56:26 -05:00
|
|
|
|
2009-05-04 18:20:09 -04:00
|
|
|
{
|
|
|
|
/* we always unconditionally get the session id here, as even if we
|
|
|
|
already got it from the cache and asked to use it in the connection, it
|
|
|
|
might've been rejected and then a new one is in use now and we need to
|
|
|
|
detect that. */
|
|
|
|
void *connect_sessionid;
|
2014-07-11 19:31:12 -04:00
|
|
|
size_t connect_idsize = 0;
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* get the session ID data size */
|
2009-05-04 18:20:09 -04:00
|
|
|
gnutls_session_get_data(session, NULL, &connect_idsize);
|
|
|
|
connect_sessionid = malloc(connect_idsize); /* get a buffer for it */
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2009-05-04 18:20:09 -04:00
|
|
|
if(connect_sessionid) {
|
2005-04-07 11:27:13 -04:00
|
|
|
/* extract session ID to the allocated buffer */
|
2009-05-04 18:20:09 -04:00
|
|
|
gnutls_session_get_data(session, connect_sessionid, &connect_idsize);
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
incache = !(Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL));
|
2011-04-20 09:17:42 -04:00
|
|
|
if(incache) {
|
2009-05-05 04:33:29 -04:00
|
|
|
/* there was one before in the cache, so instead of risking that the
|
|
|
|
previous one was rejected, we just kill that and store the new */
|
2009-05-04 18:20:09 -04:00
|
|
|
Curl_ssl_delsessionid(conn, ssl_sessionid);
|
2010-04-16 16:43:01 -04:00
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
|
|
|
|
/* store this session id */
|
2011-01-05 18:47:37 -05:00
|
|
|
result = Curl_ssl_addsessionid(conn, connect_sessionid, connect_idsize);
|
|
|
|
if(result) {
|
|
|
|
free(connect_sessionid);
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
2011-01-05 18:47:37 -05:00
|
|
|
else
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2010-04-16 16:43:01 -04:00
|
|
|
}
|
|
|
|
|
2011-01-05 18:47:37 -05:00
|
|
|
return result;
|
2010-04-16 16:43:01 -04:00
|
|
|
}
|
|
|
|
|
2009-05-04 18:20:09 -04:00
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
/*
|
|
|
|
* This function is called after the TCP connect has completed. Setup the TLS
|
|
|
|
* layer and do all necessary magic.
|
|
|
|
*/
|
|
|
|
/* We use connssl->connecting_state to keep track of the connection status;
|
|
|
|
there are three states: 'ssl_connect_1' (not started yet or complete),
|
|
|
|
'ssl_connect_2_reading' (waiting for data from server), and
|
|
|
|
'ssl_connect_2_writing' (waiting to be able to write).
|
|
|
|
*/
|
|
|
|
static CURLcode
|
|
|
|
gtls_connect_common(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
|
|
|
|
/* Initiate the connection, if not already done */
|
|
|
|
if(ssl_connect_1==connssl->connecting_state) {
|
|
|
|
rc = gtls_connect_step1 (conn, sockindex);
|
|
|
|
if(rc)
|
|
|
|
return rc;
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
rc = handshake(conn, sockindex, TRUE, nonblocking);
|
|
|
|
if(rc)
|
|
|
|
/* handshake() sets its own error message with failf() */
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Finish connecting once the handshake is done */
|
|
|
|
if(ssl_connect_1==connssl->connecting_state) {
|
|
|
|
rc = gtls_connect_step3(conn, sockindex);
|
|
|
|
if(rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
*done = ssl_connect_1==connssl->connecting_state;
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-16 16:43:01 -04:00
|
|
|
CURLcode
|
|
|
|
Curl_gtls_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
return gtls_connect_common(conn, sockindex, TRUE, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_gtls_connect(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
|
|
|
|
{
|
2014-12-24 12:26:21 -05:00
|
|
|
CURLcode result;
|
2010-04-16 16:43:01 -04:00
|
|
|
bool done = FALSE;
|
|
|
|
|
2014-12-24 12:26:21 -05:00
|
|
|
result = gtls_connect_common(conn, sockindex, FALSE, &done);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2010-04-16 16:43:01 -04:00
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t gtls_send(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const void *mem,
|
|
|
|
size_t len,
|
|
|
|
CURLcode *curlcode)
|
2005-04-07 11:27:13 -04:00
|
|
|
{
|
2006-11-11 16:34:43 -05:00
|
|
|
ssize_t rc = gnutls_record_send(conn->ssl[sockindex].session, mem, len);
|
2005-04-07 11:27:13 -04:00
|
|
|
|
2006-05-04 02:00:40 -04:00
|
|
|
if(rc < 0 ) {
|
2010-04-04 17:37:18 -04:00
|
|
|
*curlcode = (rc == GNUTLS_E_AGAIN)
|
2010-05-07 09:05:34 -04:00
|
|
|
? CURLE_AGAIN
|
2010-04-04 17:37:18 -04:00
|
|
|
: CURLE_SEND_ERROR;
|
|
|
|
|
|
|
|
rc = -1;
|
2006-05-04 02:00:40 -04:00
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_one(struct connectdata *conn,
|
2008-02-16 08:41:55 -05:00
|
|
|
int idx)
|
2005-04-07 11:27:13 -04:00
|
|
|
{
|
2008-02-16 08:41:55 -05:00
|
|
|
if(conn->ssl[idx].session) {
|
|
|
|
gnutls_bye(conn->ssl[idx].session, GNUTLS_SHUT_RDWR);
|
|
|
|
gnutls_deinit(conn->ssl[idx].session);
|
|
|
|
conn->ssl[idx].session = NULL;
|
2005-04-07 18:47:43 -04:00
|
|
|
}
|
2008-02-16 08:41:55 -05:00
|
|
|
if(conn->ssl[idx].cred) {
|
|
|
|
gnutls_certificate_free_credentials(conn->ssl[idx].cred);
|
|
|
|
conn->ssl[idx].cred = NULL;
|
2007-07-29 08:54:05 -04:00
|
|
|
}
|
2011-01-19 14:35:02 -05:00
|
|
|
#ifdef USE_TLS_SRP
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->ssl[idx].srp_client_cred) {
|
2011-01-19 14:35:02 -05:00
|
|
|
gnutls_srp_free_client_credentials(conn->ssl[idx].srp_client_cred);
|
|
|
|
conn->ssl[idx].srp_client_cred = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
2007-07-29 08:54:05 -04:00
|
|
|
void Curl_gtls_close(struct connectdata *conn, int sockindex)
|
2005-04-07 11:27:13 -04:00
|
|
|
{
|
2007-07-29 08:54:05 -04:00
|
|
|
close_one(conn, sockindex);
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
2007-01-05 18:11:14 -05:00
|
|
|
/*
|
|
|
|
* This function is called to shut down the SSL layer but keep the
|
|
|
|
* socket open (CCC - Clear Command Channel)
|
|
|
|
*/
|
|
|
|
int Curl_gtls_shutdown(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2009-08-29 21:36:01 -04:00
|
|
|
ssize_t result;
|
2007-01-05 18:11:14 -05:00
|
|
|
int retval = 0;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
int done = 0;
|
|
|
|
char buf[120];
|
|
|
|
|
|
|
|
/* This has only been tested on the proftpd server, and the mod_tls code
|
|
|
|
sends a close notify alert without waiting for a close notify alert in
|
|
|
|
response. Thus we wait for a close notify alert from the server, but
|
|
|
|
we do not send one. Let's hope other servers do the same... */
|
|
|
|
|
2007-02-20 17:02:11 -05:00
|
|
|
if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE)
|
|
|
|
gnutls_bye(conn->ssl[sockindex].session, GNUTLS_SHUT_WR);
|
|
|
|
|
2007-01-05 18:11:14 -05:00
|
|
|
if(conn->ssl[sockindex].session) {
|
|
|
|
while(!done) {
|
2007-03-26 19:23:46 -04:00
|
|
|
int what = Curl_socket_ready(conn->sock[sockindex],
|
2011-06-04 15:19:14 -04:00
|
|
|
CURL_SOCKET_BAD, SSL_SHUTDOWN_TIMEOUT);
|
2007-01-05 18:11:14 -05:00
|
|
|
if(what > 0) {
|
|
|
|
/* Something to read, let's do it and hope that it is the close
|
|
|
|
notify alert from the server */
|
|
|
|
result = gnutls_record_recv(conn->ssl[sockindex].session,
|
|
|
|
buf, sizeof(buf));
|
|
|
|
switch(result) {
|
|
|
|
case 0:
|
|
|
|
/* This is the expected response. There was no data but only
|
|
|
|
the close notify alert */
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case GNUTLS_E_AGAIN:
|
|
|
|
case GNUTLS_E_INTERRUPTED:
|
|
|
|
infof(data, "GNUTLS_E_AGAIN || GNUTLS_E_INTERRUPTED\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = -1;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(0 == what) {
|
|
|
|
/* timeout */
|
|
|
|
failf(data, "SSL shutdown timeout");
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* anything that gets here is fatally bad */
|
2007-03-27 14:16:35 -04:00
|
|
|
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
2007-01-05 18:11:14 -05:00
|
|
|
retval = -1;
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gnutls_deinit(conn->ssl[sockindex].session);
|
|
|
|
}
|
|
|
|
gnutls_certificate_free_credentials(conn->ssl[sockindex].cred);
|
|
|
|
|
2011-01-19 14:35:02 -05:00
|
|
|
#ifdef USE_TLS_SRP
|
|
|
|
if(data->set.ssl.authtype == CURL_TLSAUTH_SRP
|
|
|
|
&& data->set.ssl.username != NULL)
|
|
|
|
gnutls_srp_free_client_credentials(conn->ssl[sockindex].srp_client_cred);
|
|
|
|
#endif
|
|
|
|
|
2007-07-29 08:54:05 -04:00
|
|
|
conn->ssl[sockindex].cred = NULL;
|
2007-01-05 18:11:14 -05:00
|
|
|
conn->ssl[sockindex].session = NULL;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t gtls_recv(struct connectdata *conn, /* connection data */
|
|
|
|
int num, /* socketindex */
|
|
|
|
char *buf, /* store read data here */
|
|
|
|
size_t buffersize, /* max amount to read */
|
|
|
|
CURLcode *curlcode)
|
2005-04-07 11:27:13 -04:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = gnutls_record_recv(conn->ssl[num].session, buf, buffersize);
|
|
|
|
if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
|
2010-05-07 09:05:34 -04:00
|
|
|
*curlcode = CURLE_AGAIN;
|
2005-04-07 11:27:13 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-22 17:05:07 -04:00
|
|
|
if(ret == GNUTLS_E_REHANDSHAKE) {
|
|
|
|
/* BLOCKING call, this is bad but a work-around for now. Fixing this "the
|
|
|
|
proper way" takes a whole lot of work. */
|
2014-12-24 12:26:21 -05:00
|
|
|
CURLcode result = handshake(conn, num, FALSE, FALSE);
|
|
|
|
if(result)
|
2005-10-22 17:05:07 -04:00
|
|
|
/* handshake() writes error message on its own */
|
2014-12-24 12:26:21 -05:00
|
|
|
*curlcode = result;
|
2010-04-04 17:37:18 -04:00
|
|
|
else
|
2010-05-07 09:05:34 -04:00
|
|
|
*curlcode = CURLE_AGAIN; /* then return as if this was a wouldblock */
|
2005-10-22 17:05:07 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(ret < 0) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(conn->data, "GnuTLS recv error (%d): %s",
|
2009-08-29 21:36:01 -04:00
|
|
|
(int)ret, gnutls_strerror((int)ret));
|
2010-04-04 17:37:18 -04:00
|
|
|
*curlcode = CURLE_RECV_ERROR;
|
2005-04-07 11:27:13 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_gtls_session_free(void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_gtls_version(char *buffer, size_t size)
|
|
|
|
{
|
2007-08-24 05:06:17 -04:00
|
|
|
return snprintf(buffer, size, "GnuTLS/%s", gnutls_check_version(NULL));
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
|
2014-08-03 05:17:33 -04:00
|
|
|
#ifndef USE_GNUTLS_NETTLE
|
2014-06-03 14:04:46 -04:00
|
|
|
static int Curl_gtls_seed(struct SessionHandle *data)
|
2009-02-12 15:48:40 -05:00
|
|
|
{
|
|
|
|
/* we have the "SSL is seeded" boolean static to prevent multiple
|
|
|
|
time-consuming seedings in vain */
|
|
|
|
static bool ssl_seeded = FALSE;
|
|
|
|
|
|
|
|
/* Quickly add a bit of entropy */
|
|
|
|
gcry_fast_random_poll();
|
|
|
|
|
|
|
|
if(!ssl_seeded || data->set.str[STRING_SSL_RANDOM_FILE] ||
|
|
|
|
data->set.str[STRING_SSL_EGDSOCKET]) {
|
2009-02-25 07:51:17 -05:00
|
|
|
|
|
|
|
/* TODO: to a good job seeding the RNG
|
|
|
|
This may involve the gcry_control function and these options:
|
|
|
|
GCRYCTL_SET_RANDOM_SEED_FILE
|
|
|
|
GCRYCTL_SET_RNDEGD_SOCKET
|
|
|
|
*/
|
2009-02-12 15:48:40 -05:00
|
|
|
ssl_seeded = TRUE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-03 05:17:33 -04:00
|
|
|
#endif
|
2009-02-12 15:48:40 -05:00
|
|
|
|
2014-06-03 14:04:46 -04:00
|
|
|
/* data might be NULL! */
|
|
|
|
int Curl_gtls_random(struct SessionHandle *data,
|
|
|
|
unsigned char *entropy,
|
|
|
|
size_t length)
|
2012-06-26 08:52:46 -04:00
|
|
|
{
|
|
|
|
#if defined(USE_GNUTLS_NETTLE)
|
|
|
|
(void)data;
|
|
|
|
gnutls_rnd(GNUTLS_RND_RANDOM, entropy, length);
|
|
|
|
#elif defined(USE_GNUTLS)
|
2014-06-03 14:04:46 -04:00
|
|
|
if(data)
|
|
|
|
Curl_gtls_seed(data); /* Initiate the seed if not already done */
|
2012-06-26 08:52:46 -04:00
|
|
|
gcry_randomize(entropy, length, GCRY_STRONG_RANDOM);
|
|
|
|
#endif
|
2014-06-03 14:04:46 -04:00
|
|
|
return 0;
|
2012-06-26 08:52:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_gtls_md5sum(unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *md5sum, /* output */
|
|
|
|
size_t md5len)
|
|
|
|
{
|
|
|
|
#if defined(USE_GNUTLS_NETTLE)
|
|
|
|
struct md5_ctx MD5pw;
|
|
|
|
md5_init(&MD5pw);
|
2014-01-19 17:26:01 -05:00
|
|
|
md5_update(&MD5pw, (unsigned int)tmplen, tmp);
|
|
|
|
md5_digest(&MD5pw, (unsigned int)md5len, md5sum);
|
2012-06-26 08:52:46 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
gcry_md_hd_t MD5pw;
|
|
|
|
gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
|
|
|
|
gcry_md_write(MD5pw, tmp, tmplen);
|
|
|
|
memcpy(md5sum, gcry_md_read (MD5pw, 0), md5len);
|
|
|
|
gcry_md_close(MD5pw);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:21:02 -04:00
|
|
|
bool Curl_gtls_cert_status_request(void)
|
|
|
|
{
|
|
|
|
#ifdef HAS_OCSP
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
#endif /* USE_GNUTLS */
|