2005-04-07 11:27:13 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2008-02-07 17:25:04 -05:00
|
|
|
* Copyright (C) 1998 - 2008, 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.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all GnuTLS-specific code for the TLS/SSL layer. No code
|
|
|
|
* but sslgen.c should ever call or use these functions.
|
|
|
|
*
|
|
|
|
* Note: don't use the GnuTLS' *_t variable type names in this source code,
|
|
|
|
* since they were not present in 1.0.X.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
#ifdef USE_GNUTLS
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/x509.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "gtls.h"
|
|
|
|
#include "sslgen.h"
|
|
|
|
#include "parsedate.h"
|
|
|
|
#include "connect.h" /* for the connect timeout */
|
|
|
|
#include "select.h"
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
#include "memory.h"
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
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;
|
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()
|
|
|
|
* (although here using the sread/swrite macros as defined by setup_once.h).
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static ssize_t Curl_gtls_push(void *s, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
return swrite(s, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t Curl_gtls_pull(void *s, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
return sread(s, buf, len);
|
|
|
|
}
|
2005-04-09 17:38:14 -04:00
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Global GnuTLS init, called from Curl_ssl_init() */
|
|
|
|
int Curl_gtls_init(void)
|
|
|
|
{
|
2007-04-28 17:01:30 -04:00
|
|
|
/* Unfortunately we can not init here, things like curl --version will
|
|
|
|
* fail to work if there is no egd socket available because libgcrypt
|
|
|
|
* will EXIT the application!!
|
|
|
|
* By doing the actual init later (before actually trying to use GnuTLS),
|
|
|
|
* we can at least provide basic info etc.
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _Curl_gtls_init(void)
|
|
|
|
{
|
|
|
|
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 *tm;
|
|
|
|
#ifdef HAVE_GMTIME_R
|
|
|
|
struct tm buffer;
|
|
|
|
tm = (struct tm *)gmtime_r(&stamp, &buffer);
|
|
|
|
#else
|
|
|
|
tm = gmtime(&stamp);
|
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
infof(data, "%s", data->state.buffer);
|
|
|
|
}
|
|
|
|
|
2005-10-22 17:05:07 -04:00
|
|
|
/* this function does a BLOCKING SSL/TLS (re-)handshake */
|
|
|
|
static CURLcode handshake(struct connectdata *conn,
|
|
|
|
gnutls_session session,
|
|
|
|
int sockindex,
|
|
|
|
bool duringconnect)
|
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
int rc;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!gtls_inited)
|
2007-04-28 17:01:30 -04:00
|
|
|
_Curl_gtls_init();
|
2005-10-22 17:05:07 -04:00
|
|
|
do {
|
|
|
|
rc = gnutls_handshake(session);
|
|
|
|
|
|
|
|
if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
|
2008-02-07 17:25:04 -05:00
|
|
|
long timeout_ms = Curl_connecttimeleft(conn, NULL, duringconnect);
|
2005-10-22 17:05:07 -04:00
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* a precaution, no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
2005-10-22 17:05:07 -04:00
|
|
|
}
|
|
|
|
|
2007-03-26 19:23:46 -04:00
|
|
|
rc = Curl_socket_ready(conn->sock[sockindex],
|
2005-10-22 17:05:07 -04:00
|
|
|
conn->sock[sockindex], (int)timeout_ms);
|
|
|
|
if(rc > 0)
|
|
|
|
/* reabable or writable, go loop*/
|
|
|
|
continue;
|
|
|
|
else if(0 == rc) {
|
|
|
|
/* timeout */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
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);
|
2005-10-22 17:05:07 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(rc < 0) {
|
2006-08-16 13:05:54 -04:00
|
|
|
failf(data, "gnutls_handshake() failed: %s", gnutls_strerror(rc));
|
2005-10-22 17:05:07 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2005-11-13 18:04:28 -05:00
|
|
|
static gnutls_x509_crt_fmt do_file_type(const char *type)
|
2005-11-11 18:20:07 -05:00
|
|
|
{
|
|
|
|
if(!type || !type[0])
|
|
|
|
return GNUTLS_X509_FMT_PEM;
|
|
|
|
if(curl_strequal(type, "PEM"))
|
|
|
|
return GNUTLS_X509_FMT_PEM;
|
|
|
|
if(curl_strequal(type, "DER"))
|
|
|
|
return GNUTLS_X509_FMT_DER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/*
|
|
|
|
* This function is called after the TCP connect has completed. Setup the TLS
|
|
|
|
* layer and do all necessary magic.
|
|
|
|
*/
|
|
|
|
CURLcode
|
|
|
|
Curl_gtls_connect(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
|
|
|
|
{
|
2007-07-19 20:41:12 -04:00
|
|
|
static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
|
2005-04-07 11:27:13 -04:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
gnutls_session session;
|
|
|
|
int rc;
|
|
|
|
unsigned int cert_list_size;
|
|
|
|
const gnutls_datum *chainp;
|
|
|
|
unsigned int verify_status;
|
|
|
|
gnutls_x509_crt x509_cert;
|
|
|
|
char certbuf[256]; /* big enough? */
|
|
|
|
size_t size;
|
|
|
|
unsigned int algo;
|
|
|
|
unsigned int bits;
|
|
|
|
time_t clock;
|
|
|
|
const char *ptr;
|
|
|
|
void *ssl_sessionid;
|
|
|
|
size_t ssl_idsize;
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!gtls_inited) _Curl_gtls_init();
|
2005-04-07 11:27:13 -04:00
|
|
|
/* GnuTLS only supports TLSv1 (and SSLv3?) */
|
|
|
|
if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
|
|
|
|
failf(data, "GnuTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a cred struct */
|
|
|
|
rc = gnutls_certificate_allocate_credentials(&conn->ssl[sockindex].cred);
|
|
|
|
if(rc < 0) {
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Initialize TLS session as a client */
|
|
|
|
rc = gnutls_init(&conn->ssl[sockindex].session, GNUTLS_CLIENT);
|
|
|
|
if(rc) {
|
|
|
|
failf(data, "gnutls_init() failed: %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convenient assign */
|
|
|
|
session = conn->ssl[sockindex].session;
|
|
|
|
|
|
|
|
/* Use default priorities */
|
|
|
|
rc = gnutls_set_default_priority(session);
|
|
|
|
if(rc < 0)
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
|
|
|
|
/* Sets the priority on the certificate types supported by gnutls. Priority
|
|
|
|
is higher for types specified before others. After specifying the types
|
|
|
|
you want, you must append a 0. */
|
|
|
|
rc = gnutls_certificate_type_set_priority(session, cert_type_priority);
|
|
|
|
if(rc < 0)
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
|
2007-08-01 17:20:01 -04:00
|
|
|
if(data->set.str[STRING_CERT]) {
|
2005-11-11 18:20:07 -05:00
|
|
|
if( gnutls_certificate_set_x509_key_file(
|
2007-08-01 17:20:01 -04:00
|
|
|
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]) ) ) {
|
2005-11-11 18:20:07 -05:00
|
|
|
failf(data, "error reading X.509 key or certificate file");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put the credentials to the current session */
|
2005-04-07 11:27:13 -04:00
|
|
|
rc = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
|
|
|
|
conn->ssl[sockindex].cred);
|
|
|
|
|
|
|
|
/* set the connection handle (file descriptor for the socket) */
|
|
|
|
gnutls_transport_set_ptr(session,
|
|
|
|
(gnutls_transport_ptr)conn->sock[sockindex]);
|
|
|
|
|
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);
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2005-10-22 17:05:07 -04:00
|
|
|
rc = handshake(conn, session, sockindex, TRUE);
|
|
|
|
if(rc)
|
|
|
|
/* handshake() sets its own error message with failf() */
|
|
|
|
return rc;
|
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) {
|
|
|
|
if(data->set.ssl.verifyhost) {
|
|
|
|
failf(data, "failed to get server cert");
|
2007-10-03 04:07:50 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2005-04-07 11:27:13 -04:00
|
|
|
}
|
|
|
|
infof(data, "\t common name: WARNING couldn't obtain\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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(). */
|
|
|
|
|
|
|
|
rc = gnutls_certificate_verify_peers2(session, &verify_status);
|
2007-11-07 04:21:35 -05:00
|
|
|
if(rc < 0) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(data, "server cert verify failed: %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify_status is a bitmask of gnutls_certificate_status bits */
|
|
|
|
if(verify_status & GNUTLS_CERT_INVALID) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(data, "server certificate verification failed. CAfile: %s",
|
|
|
|
data->set.ssl.CAfile?data->set.ssl.CAfile:"none");
|
|
|
|
return CURLE_SSL_CACERT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate verification FAILED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate verification OK\n");
|
|
|
|
|
|
|
|
/* initialize an X.509 certificate structure. */
|
|
|
|
gnutls_x509_crt_init(&x509_cert);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(!rc) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifyhost > 1) {
|
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 */
|
|
|
|
clock = gnutls_x509_crt_get_expiration_time(x509_cert);
|
|
|
|
|
|
|
|
if(clock == (time_t)-1) {
|
|
|
|
failf(data, "server cert expiration date verify failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(clock < time(NULL)) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2007-07-10 17:36:30 -04:00
|
|
|
failf(data, "server certificate expiration date has passed.");
|
2007-10-03 04:07:50 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate expiration date FAILED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate expiration date OK\n");
|
|
|
|
|
|
|
|
clock = gnutls_x509_crt_get_activation_time(x509_cert);
|
|
|
|
|
|
|
|
if(clock == (time_t)-1) {
|
|
|
|
failf(data, "server cert activation date verify failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(clock > time(NULL)) {
|
2007-11-07 04:21:35 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2007-07-10 17:36:30 -04:00
|
|
|
failf(data, "server certificate not activated yet.");
|
2007-10-03 04:07:50 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2007-07-10 17:36:30 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate activation date FAILED\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "\t server certificate activation date OK\n");
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
clock = gnutls_x509_crt_get_activation_time(x509_cert);
|
|
|
|
showtime(data, "start date", clock);
|
|
|
|
|
|
|
|
clock = gnutls_x509_crt_get_expiration_time(x509_cert);
|
|
|
|
showtime(data, "expire date", clock);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if(!ssl_sessionid) {
|
|
|
|
/* this session was not previously in the cache, add it now */
|
|
|
|
|
|
|
|
/* get the session ID data size */
|
|
|
|
gnutls_session_get_data(session, NULL, &ssl_idsize);
|
|
|
|
ssl_sessionid = malloc(ssl_idsize); /* get a buffer for it */
|
|
|
|
|
|
|
|
if(ssl_sessionid) {
|
|
|
|
/* extract session ID to the allocated buffer */
|
|
|
|
gnutls_session_get_data(session, ssl_sessionid, &ssl_idsize);
|
|
|
|
|
|
|
|
/* store this session id */
|
|
|
|
return Curl_ssl_addsessionid(conn, ssl_sessionid, ssl_idsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return number of sent (non-SSL) bytes */
|
2006-11-11 16:34:43 -05:00
|
|
|
ssize_t Curl_gtls_send(struct connectdata *conn,
|
2005-04-07 11:27:13 -04:00
|
|
|
int sockindex,
|
|
|
|
void *mem,
|
|
|
|
size_t len)
|
|
|
|
{
|
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 ) {
|
|
|
|
if(rc == GNUTLS_E_AGAIN)
|
|
|
|
return 0; /* EWOULDBLOCK equivalent */
|
|
|
|
rc = -1; /* generic error code for send failure */
|
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_gtls_close_all(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
/* FIX: make the OpenSSL code more generic and use parts of it here */
|
|
|
|
(void)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_one(struct connectdata *conn,
|
|
|
|
int index)
|
|
|
|
{
|
2005-04-07 18:47:43 -04:00
|
|
|
if(conn->ssl[index].session) {
|
|
|
|
gnutls_bye(conn->ssl[index].session, GNUTLS_SHUT_RDWR);
|
|
|
|
gnutls_deinit(conn->ssl[index].session);
|
2007-07-29 08:54:05 -04:00
|
|
|
conn->ssl[index].session = NULL;
|
2005-04-07 18:47:43 -04:00
|
|
|
}
|
2007-07-29 08:54:05 -04:00
|
|
|
if(conn->ssl[index].cred) {
|
2007-04-21 17:32:31 -04:00
|
|
|
gnutls_certificate_free_credentials(conn->ssl[index].cred);
|
2007-07-29 08:54:05 -04:00
|
|
|
conn->ssl[index].cred = NULL;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
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],
|
2007-01-05 18:11:14 -05:00
|
|
|
CURL_SOCKET_BAD, SSL_SHUTDOWN_TIMEOUT);
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
/*
|
|
|
|
* If the read would block we return -1 and set 'wouldblock' to TRUE.
|
|
|
|
* Otherwise we return the amount of data read. Other errors should return -1
|
|
|
|
* and set 'wouldblock' to FALSE.
|
|
|
|
*/
|
|
|
|
ssize_t Curl_gtls_recv(struct connectdata *conn, /* connection data */
|
|
|
|
int num, /* socketindex */
|
|
|
|
char *buf, /* store read data here */
|
|
|
|
size_t buffersize, /* max amount to read */
|
|
|
|
bool *wouldblock)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = gnutls_record_recv(conn->ssl[num].session, buf, buffersize);
|
|
|
|
if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
|
|
|
|
*wouldblock = TRUE;
|
|
|
|
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. */
|
|
|
|
CURLcode rc = handshake(conn, conn->ssl[num].session, num, FALSE);
|
|
|
|
if(rc)
|
|
|
|
/* handshake() writes error message on its own */
|
|
|
|
return rc;
|
|
|
|
*wouldblock = TRUE; /* then return as if this was a wouldblock */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-07 11:27:13 -04:00
|
|
|
*wouldblock = FALSE;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!ret) {
|
2005-04-07 11:27:13 -04:00
|
|
|
failf(conn->data, "Peer closed the TLS connection");
|
|
|
|
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",
|
|
|
|
(int)ret, gnutls_strerror(ret));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_GNUTLS */
|