2005-04-07 17:27:13 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-06-08 21:33:29 +02:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-04-07 17:27:13 +02: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2009-02-25 13:51:39 +01:00
|
|
|
/* This file is for implementing all "generic" SSL functions that all libcurl
|
|
|
|
internals should use. It is then responsible for calling the proper
|
|
|
|
"backend" function.
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
SSL-functions in libcurl should call functions in this source file, and not
|
|
|
|
to any specific SSL-layer.
|
|
|
|
|
|
|
|
Curl_ssl_ - prefix for generic ones
|
|
|
|
Curl_ossl_ - prefix for OpenSSL ones
|
|
|
|
Curl_gtls_ - prefix for GnuTLS ones
|
2007-02-12 23:32:37 +01:00
|
|
|
Curl_nss_ - prefix for NSS ones
|
2013-07-15 19:00:36 +02:00
|
|
|
Curl_gskit_ - prefix for GSKit ones
|
2010-05-05 22:30:46 +02:00
|
|
|
Curl_polarssl_ - prefix for PolarSSL ones
|
2011-03-08 13:54:58 +01:00
|
|
|
Curl_cyassl_ - prefix for CyaSSL ones
|
2012-04-09 15:40:06 +02:00
|
|
|
Curl_schannel_ - prefix for Schannel SSPI ones
|
2012-06-26 21:39:48 +02:00
|
|
|
Curl_darwinssl_ - prefix for SecureTransport (Darwin) ones
|
2005-04-07 17:27:13 +02:00
|
|
|
|
2008-06-11 19:01:58 +02:00
|
|
|
Note that this source code uses curlssl_* functions, and they are all
|
|
|
|
defines/macros #defined by the lib-specific header files.
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
"SSL/TLS Strong Encryption: An Introduction"
|
|
|
|
http://httpd.apache.org/docs-2.0/ssl/ssl_intro.html
|
|
|
|
*/
|
|
|
|
|
2013-01-06 19:06:49 +01:00
|
|
|
#include "curl_setup.h"
|
2007-02-26 05:24:26 +01:00
|
|
|
|
2013-06-24 22:24:35 +02:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 02:50:28 +01:00
|
|
|
#include "urldata.h"
|
2013-12-17 23:32:47 +01:00
|
|
|
|
|
|
|
#include "vtls.h" /* generic SSL protos etc */
|
2013-07-15 17:26:59 +02:00
|
|
|
#include "slist.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
#include "sendf.h"
|
|
|
|
#include "rawstr.h"
|
|
|
|
#include "url.h"
|
2009-04-21 13:46:16 +02:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
#include "progress.h"
|
|
|
|
#include "share.h"
|
2013-06-24 22:24:35 +02:00
|
|
|
#include "timeval.h"
|
2014-10-13 14:39:50 +02:00
|
|
|
#include "curl_md5.h"
|
2013-07-15 17:26:59 +02:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
/* The last #include file should be: */
|
2013-01-04 02:50:28 +01:00
|
|
|
#include "memdebug.h"
|
2005-04-07 17:27:13 +02:00
|
|
|
|
2011-11-25 14:58:55 +01:00
|
|
|
/* convenience macro to check if this handle is using a shared SSL session */
|
|
|
|
#define SSLSESSION_SHARED(data) (data->share && \
|
|
|
|
(data->share->specifier & \
|
|
|
|
(1<<CURL_LOCK_DATA_SSL_SESSION)))
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
static bool safe_strequal(char* str1, char* str2)
|
|
|
|
{
|
|
|
|
if(str1 && str2)
|
|
|
|
/* both pointers point to something then compare them */
|
2011-09-05 20:46:09 +02:00
|
|
|
return (0 != Curl_raw_equal(str1, str2)) ? TRUE : FALSE;
|
2005-04-07 17:27:13 +02:00
|
|
|
else
|
|
|
|
/* if both pointers are NULL then treat them as equal */
|
2011-09-05 20:46:09 +02:00
|
|
|
return (!str1 && !str2) ? TRUE : FALSE;
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Curl_ssl_config_matches(struct ssl_config_data* data,
|
|
|
|
struct ssl_config_data* needle)
|
|
|
|
{
|
|
|
|
if((data->version == needle->version) &&
|
|
|
|
(data->verifypeer == needle->verifypeer) &&
|
|
|
|
(data->verifyhost == needle->verifyhost) &&
|
|
|
|
safe_strequal(data->CApath, needle->CApath) &&
|
|
|
|
safe_strequal(data->CAfile, needle->CAfile) &&
|
|
|
|
safe_strequal(data->random_file, needle->random_file) &&
|
|
|
|
safe_strequal(data->egdsocket, needle->egdsocket) &&
|
|
|
|
safe_strequal(data->cipher_list, needle->cipher_list))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Curl_clone_ssl_config(struct ssl_config_data *source,
|
|
|
|
struct ssl_config_data *dest)
|
|
|
|
{
|
2007-12-03 12:48:09 +01:00
|
|
|
dest->sessionid = source->sessionid;
|
2005-04-07 17:27:13 +02:00
|
|
|
dest->verifyhost = source->verifyhost;
|
|
|
|
dest->verifypeer = source->verifypeer;
|
|
|
|
dest->version = source->version;
|
|
|
|
|
|
|
|
if(source->CAfile) {
|
|
|
|
dest->CAfile = strdup(source->CAfile);
|
|
|
|
if(!dest->CAfile)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-22 09:25:03 +01:00
|
|
|
else
|
|
|
|
dest->CAfile = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
if(source->CApath) {
|
|
|
|
dest->CApath = strdup(source->CApath);
|
|
|
|
if(!dest->CApath)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-22 09:25:03 +01:00
|
|
|
else
|
|
|
|
dest->CApath = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
if(source->cipher_list) {
|
|
|
|
dest->cipher_list = strdup(source->cipher_list);
|
|
|
|
if(!dest->cipher_list)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-22 09:25:03 +01:00
|
|
|
else
|
|
|
|
dest->cipher_list = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
if(source->egdsocket) {
|
|
|
|
dest->egdsocket = strdup(source->egdsocket);
|
|
|
|
if(!dest->egdsocket)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-22 09:25:03 +01:00
|
|
|
else
|
|
|
|
dest->egdsocket = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
if(source->random_file) {
|
|
|
|
dest->random_file = strdup(source->random_file);
|
|
|
|
if(!dest->random_file)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-22 09:25:03 +01:00
|
|
|
else
|
|
|
|
dest->random_file = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_free_ssl_config(struct ssl_config_data* sslc)
|
|
|
|
{
|
2007-08-01 23:20:01 +02:00
|
|
|
Curl_safefree(sslc->CAfile);
|
|
|
|
Curl_safefree(sslc->CApath);
|
|
|
|
Curl_safefree(sslc->cipher_list);
|
|
|
|
Curl_safefree(sslc->egdsocket);
|
|
|
|
Curl_safefree(sslc->random_file);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:24:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_rand() returns a random unsigned integer, 32bit.
|
|
|
|
*
|
|
|
|
* This non-SSL function is put here only because this file is the only one
|
|
|
|
* with knowledge of what the underlying SSL libraries provide in terms of
|
|
|
|
* randomizers.
|
|
|
|
*
|
|
|
|
* NOTE: 'data' may be passed in as NULL when coming from external API without
|
|
|
|
* easy handle!
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned int Curl_rand(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
unsigned int r;
|
|
|
|
static unsigned int randseed;
|
2013-07-15 15:54:28 +02:00
|
|
|
static bool seeded = FALSE;
|
2013-06-24 22:24:35 +02:00
|
|
|
|
2014-06-08 21:33:29 +02:00
|
|
|
#ifdef CURLDEBUG
|
|
|
|
char *force_entropy = getenv("CURL_ENTROPY");
|
|
|
|
if(force_entropy) {
|
|
|
|
if(!seeded) {
|
|
|
|
size_t elen = strlen(force_entropy);
|
|
|
|
size_t clen = sizeof(randseed);
|
|
|
|
size_t min = elen < clen ? elen : clen;
|
|
|
|
memcpy((char *)&randseed, force_entropy, min);
|
|
|
|
seeded = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
randseed++;
|
|
|
|
return randseed;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-03 20:04:46 +02:00
|
|
|
/* data may be NULL! */
|
|
|
|
if(!Curl_ssl_random(data, (unsigned char *)&r, sizeof(r)))
|
2013-07-15 15:54:28 +02:00
|
|
|
return r;
|
2014-06-03 20:04:46 +02:00
|
|
|
|
|
|
|
/* If Curl_ssl_random() returns non-zero it couldn't offer randomness and we
|
|
|
|
instead perform a "best effort" */
|
2013-06-24 22:24:35 +02:00
|
|
|
|
|
|
|
#ifdef RANDOM_FILE
|
2013-07-15 15:54:28 +02:00
|
|
|
if(!seeded) {
|
|
|
|
/* if there's a random file to read a seed from, use it */
|
|
|
|
int fd = open(RANDOM_FILE, O_RDONLY);
|
|
|
|
if(fd > -1) {
|
|
|
|
/* read random data into the randseed variable */
|
|
|
|
ssize_t nread = read(fd, &randseed, sizeof(randseed));
|
|
|
|
if(nread == sizeof(randseed))
|
2013-07-15 13:20:44 +02:00
|
|
|
seeded = TRUE;
|
2013-07-15 15:54:28 +02:00
|
|
|
close(fd);
|
2013-06-24 22:24:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-15 15:54:28 +02:00
|
|
|
|
|
|
|
if(!seeded) {
|
|
|
|
struct timeval now = curlx_tvnow();
|
2014-06-03 20:04:46 +02:00
|
|
|
infof(data, "WARNING: Using weak random seed\n");
|
2013-07-15 15:54:28 +02:00
|
|
|
randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
|
|
|
|
randseed = randseed * 1103515245 + 12345;
|
|
|
|
randseed = randseed * 1103515245 + 12345;
|
|
|
|
randseed = randseed * 1103515245 + 12345;
|
|
|
|
seeded = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return an unsigned 32-bit pseudo-random number. */
|
|
|
|
r = randseed = randseed * 1103515245 + 12345;
|
|
|
|
return (r << 16) | ((r >> 16) & 0xFFFF);
|
2013-06-24 22:24:35 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 10:40:36 +02:00
|
|
|
int Curl_ssl_backend(void)
|
|
|
|
{
|
|
|
|
return (int)CURL_SSL_BACKEND;
|
|
|
|
}
|
|
|
|
|
2008-06-11 19:01:58 +02:00
|
|
|
#ifdef USE_SSL
|
|
|
|
|
|
|
|
/* "global" init done? */
|
|
|
|
static bool init_ssl=FALSE;
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
/**
|
|
|
|
* Global SSL init
|
|
|
|
*
|
|
|
|
* @retval 0 error initializing SSL
|
|
|
|
* @retval 1 SSL initialized successfully
|
|
|
|
*/
|
|
|
|
int Curl_ssl_init(void)
|
|
|
|
{
|
|
|
|
/* make sure this is only done once */
|
|
|
|
if(init_ssl)
|
|
|
|
return 1;
|
|
|
|
init_ssl = TRUE; /* never again */
|
|
|
|
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_init();
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Global cleanup */
|
|
|
|
void Curl_ssl_cleanup(void)
|
|
|
|
{
|
|
|
|
if(init_ssl) {
|
|
|
|
/* only cleanup if we did a previous init */
|
2008-06-11 19:01:58 +02:00
|
|
|
curlssl_cleanup();
|
2005-04-07 17:27:13 +02:00
|
|
|
init_ssl = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_ssl_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2008-07-03 08:56:03 +02:00
|
|
|
CURLcode res;
|
2008-06-11 19:01:58 +02:00
|
|
|
/* mark this is being ssl-enabled from here on. */
|
2005-04-07 17:27:13 +02:00
|
|
|
conn->ssl[sockindex].use = TRUE;
|
2008-02-20 10:56:26 +01:00
|
|
|
conn->ssl[sockindex].state = ssl_connection_negotiating;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
2008-07-03 08:56:03 +02:00
|
|
|
res = curlssl_connect(conn, sockindex);
|
|
|
|
|
|
|
|
if(!res)
|
|
|
|
Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
|
|
|
|
|
|
|
|
return res;
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2006-03-21 22:54:44 +01:00
|
|
|
CURLcode
|
|
|
|
Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
|
|
|
|
bool *done)
|
|
|
|
{
|
2009-04-26 13:56:22 +02:00
|
|
|
CURLcode res;
|
2008-06-11 19:01:58 +02:00
|
|
|
/* mark this is being ssl requested from here on. */
|
2006-03-21 22:54:44 +01:00
|
|
|
conn->ssl[sockindex].use = TRUE;
|
2012-06-08 23:02:57 +02:00
|
|
|
#ifdef curlssl_connect_nonblocking
|
2009-04-26 13:56:22 +02:00
|
|
|
res = curlssl_connect_nonblocking(conn, sockindex, done);
|
2007-07-23 23:46:26 +02:00
|
|
|
#else
|
|
|
|
*done = TRUE; /* fallback to BLOCKING */
|
2012-06-08 23:02:57 +02:00
|
|
|
res = curlssl_connect(conn, sockindex);
|
2008-06-11 19:01:58 +02:00
|
|
|
#endif /* non-blocking connect support */
|
2012-06-08 23:02:57 +02:00
|
|
|
if(!res && *done)
|
|
|
|
Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
|
|
|
|
return res;
|
2006-03-21 22:54:44 +01:00
|
|
|
}
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
/*
|
|
|
|
* Check if there's a session ID for the given connection in the cache, and if
|
|
|
|
* there's one suitable, it is provided. Returns TRUE when no entry matched.
|
|
|
|
*/
|
|
|
|
int Curl_ssl_getsessionid(struct connectdata *conn,
|
|
|
|
void **ssl_sessionid,
|
|
|
|
size_t *idsize) /* set 0 if unknown */
|
|
|
|
{
|
|
|
|
struct curl_ssl_session *check;
|
|
|
|
struct SessionHandle *data = conn->data;
|
2012-01-18 23:39:30 +01:00
|
|
|
size_t i;
|
2011-11-17 23:34:38 +01:00
|
|
|
long *general_age;
|
2011-11-17 23:55:36 +01:00
|
|
|
bool no_match = TRUE;
|
|
|
|
|
|
|
|
*ssl_sessionid = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
2006-09-11 19:18:18 +02:00
|
|
|
if(!conn->ssl_config.sessionid)
|
|
|
|
/* session ID re-use is disabled */
|
|
|
|
return TRUE;
|
|
|
|
|
2011-11-17 23:55:36 +01:00
|
|
|
/* Lock if shared */
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data)) {
|
2011-11-17 23:55:36 +01:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
|
2011-11-17 23:34:38 +01:00
|
|
|
general_age = &data->share->sessionage;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
general_age = &data->state.sessionage;
|
2011-09-20 17:43:54 +02:00
|
|
|
|
2012-01-18 23:39:30 +01:00
|
|
|
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
|
2005-04-07 17:27:13 +02:00
|
|
|
check = &data->state.session[i];
|
|
|
|
if(!check->sessionid)
|
|
|
|
/* not session ID means blank entry */
|
|
|
|
continue;
|
2008-10-23 03:20:57 +02:00
|
|
|
if(Curl_raw_equal(conn->host.name, check->name) &&
|
2005-04-07 17:27:13 +02:00
|
|
|
(conn->remote_port == check->remote_port) &&
|
|
|
|
Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
|
|
|
|
/* yes, we have a session ID! */
|
2011-11-17 23:46:29 +01:00
|
|
|
(*general_age)++; /* increase general age */
|
2011-11-17 23:34:38 +01:00
|
|
|
check->age = *general_age; /* set this as used in this age */
|
2005-04-07 17:27:13 +02:00
|
|
|
*ssl_sessionid = check->sessionid;
|
|
|
|
if(idsize)
|
|
|
|
*idsize = check->idsize;
|
2011-11-17 23:55:36 +01:00
|
|
|
no_match = FALSE;
|
|
|
|
break;
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-20 17:43:54 +02:00
|
|
|
|
2011-11-17 23:55:36 +01:00
|
|
|
/* Unlock */
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data))
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
|
|
|
|
|
2011-11-17 23:55:36 +01:00
|
|
|
return no_match;
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kill a single session ID entry in the cache.
|
|
|
|
*/
|
2012-01-18 23:39:30 +01:00
|
|
|
void Curl_ssl_kill_session(struct curl_ssl_session *session)
|
2005-04-07 17:27:13 +02:00
|
|
|
{
|
|
|
|
if(session->sessionid) {
|
|
|
|
/* defensive check */
|
|
|
|
|
|
|
|
/* free the ID the SSL-layer specific way */
|
2008-06-11 19:01:58 +02:00
|
|
|
curlssl_session_free(session->sessionid);
|
|
|
|
|
2012-01-18 23:39:30 +01:00
|
|
|
session->sessionid = NULL;
|
2005-04-07 17:27:13 +02:00
|
|
|
session->age = 0; /* fresh */
|
|
|
|
|
|
|
|
Curl_free_ssl_config(&session->ssl_config);
|
|
|
|
|
|
|
|
Curl_safefree(session->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-04 23:57:14 +02:00
|
|
|
/*
|
|
|
|
* Delete the given session ID from the cache.
|
|
|
|
*/
|
|
|
|
void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
|
|
|
|
{
|
2012-01-18 23:39:30 +01:00
|
|
|
size_t i;
|
2011-09-20 17:43:54 +02:00
|
|
|
struct SessionHandle *data=conn->data;
|
|
|
|
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data))
|
2012-01-18 23:39:30 +01:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
|
2011-09-20 17:43:54 +02:00
|
|
|
|
2012-01-18 23:39:30 +01:00
|
|
|
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
|
2011-09-20 17:43:54 +02:00
|
|
|
struct curl_ssl_session *check = &data->state.session[i];
|
2009-05-04 23:57:14 +02:00
|
|
|
|
2011-04-20 15:17:42 +02:00
|
|
|
if(check->sessionid == ssl_sessionid) {
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_ssl_kill_session(check);
|
2009-05-04 23:57:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-20 17:43:54 +02:00
|
|
|
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data))
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
|
2009-05-04 23:57:14 +02:00
|
|
|
}
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
/*
|
|
|
|
* Store session id in the session cache. The ID passed on to this function
|
|
|
|
* must already have been extracted and allocated the proper way for the SSL
|
|
|
|
* layer. Curl_XXXX_session_free() will be called to free/kill the session ID
|
|
|
|
* later on.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
|
|
|
|
void *ssl_sessionid,
|
|
|
|
size_t idsize)
|
|
|
|
{
|
2012-01-18 23:39:30 +01:00
|
|
|
size_t i;
|
2005-04-07 17:27:13 +02:00
|
|
|
struct SessionHandle *data=conn->data; /* the mother of all structs */
|
|
|
|
struct curl_ssl_session *store = &data->state.session[0];
|
|
|
|
long oldest_age=data->state.session[0].age; /* zero if unused */
|
|
|
|
char *clone_host;
|
2011-11-17 23:34:38 +01:00
|
|
|
long *general_age;
|
2005-04-07 17:27:13 +02:00
|
|
|
|
2006-09-11 19:18:18 +02:00
|
|
|
/* Even though session ID re-use might be disabled, that only disables USING
|
|
|
|
IT. We still store it here in case the re-using is again enabled for an
|
|
|
|
upcoming transfer */
|
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
clone_host = strdup(conn->host.name);
|
|
|
|
if(!clone_host)
|
|
|
|
return CURLE_OUT_OF_MEMORY; /* bail out */
|
|
|
|
|
|
|
|
/* Now we should add the session ID and the host name to the cache, (remove
|
|
|
|
the oldest if necessary) */
|
|
|
|
|
2011-09-20 17:43:54 +02:00
|
|
|
/* If using shared SSL session, lock! */
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data)) {
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
|
2011-11-17 23:34:38 +01:00
|
|
|
general_age = &data->share->sessionage;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
general_age = &data->state.sessionage;
|
|
|
|
}
|
2011-09-20 17:43:54 +02:00
|
|
|
|
2005-04-07 17:27:13 +02:00
|
|
|
/* find an empty slot for us, or find the oldest */
|
2012-01-18 23:39:30 +01:00
|
|
|
for(i = 1; (i < data->set.ssl.max_ssl_sessions) &&
|
2005-04-07 17:27:13 +02:00
|
|
|
data->state.session[i].sessionid; i++) {
|
|
|
|
if(data->state.session[i].age < oldest_age) {
|
|
|
|
oldest_age = data->state.session[i].age;
|
|
|
|
store = &data->state.session[i];
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 23:39:30 +01:00
|
|
|
if(i == data->set.ssl.max_ssl_sessions)
|
2005-04-07 17:27:13 +02:00
|
|
|
/* cache is full, we must "kill" the oldest entry! */
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_ssl_kill_session(store);
|
2005-04-07 17:27:13 +02:00
|
|
|
else
|
|
|
|
store = &data->state.session[i]; /* use this slot */
|
|
|
|
|
|
|
|
/* now init the session struct wisely */
|
|
|
|
store->sessionid = ssl_sessionid;
|
|
|
|
store->idsize = idsize;
|
2011-11-17 23:34:38 +01:00
|
|
|
store->age = *general_age; /* set current age */
|
2011-04-20 15:17:42 +02:00
|
|
|
if(store->name)
|
2007-12-25 00:45:48 +01:00
|
|
|
/* free it if there's one already present */
|
2007-12-25 14:26:01 +01:00
|
|
|
free(store->name);
|
2005-04-07 17:27:13 +02:00
|
|
|
store->name = clone_host; /* clone host name */
|
|
|
|
store->remote_port = conn->remote_port; /* port number */
|
|
|
|
|
2011-09-20 17:43:54 +02:00
|
|
|
|
|
|
|
/* Unlock */
|
2011-11-25 14:58:55 +01:00
|
|
|
if(SSLSESSION_SHARED(data))
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
|
|
|
|
|
2011-01-10 13:53:01 +01:00
|
|
|
if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config)) {
|
|
|
|
store->sessionid = NULL; /* let caller free sessionid */
|
|
|
|
free(clone_host);
|
2005-04-07 17:27:13 +02:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-01-10 13:53:01 +01:00
|
|
|
}
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_ssl_close_all(struct SessionHandle *data)
|
|
|
|
{
|
2012-01-18 23:39:30 +01:00
|
|
|
size_t i;
|
2011-11-25 14:58:55 +01:00
|
|
|
/* kill the session ID cache if not shared */
|
|
|
|
if(data->state.session && !SSLSESSION_SHARED(data)) {
|
2012-01-18 23:39:30 +01:00
|
|
|
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++)
|
2005-04-07 17:27:13 +02:00
|
|
|
/* the single-killer function handles empty table slots */
|
2011-09-20 17:43:54 +02:00
|
|
|
Curl_ssl_kill_session(&data->state.session[i]);
|
2005-04-07 17:27:13 +02:00
|
|
|
|
|
|
|
/* free the cache data */
|
2012-01-18 23:39:30 +01:00
|
|
|
Curl_safefree(data->state.session);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
2008-06-11 19:01:58 +02:00
|
|
|
|
|
|
|
curlssl_close_all(data);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 14:54:05 +02:00
|
|
|
void Curl_ssl_close(struct connectdata *conn, int sockindex)
|
2005-04-07 17:27:13 +02:00
|
|
|
{
|
2007-07-29 14:54:05 +02:00
|
|
|
DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
|
2008-06-11 19:01:58 +02:00
|
|
|
curlssl_close(conn, sockindex);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2007-01-06 00:11:14 +01:00
|
|
|
CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
if(curlssl_shutdown(conn, sockindex))
|
2007-07-29 14:54:05 +02:00
|
|
|
return CURLE_SSL_SHUTDOWN_FAILED;
|
|
|
|
|
|
|
|
conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
|
2008-02-20 10:56:26 +01:00
|
|
|
conn->ssl[sockindex].state = ssl_connection_none;
|
2007-07-29 14:54:05 +02:00
|
|
|
|
2011-04-15 16:41:27 +02:00
|
|
|
conn->recv[sockindex] = Curl_recv_plain;
|
|
|
|
conn->send[sockindex] = Curl_send_plain;
|
|
|
|
|
2007-01-06 00:11:14 +01:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2008-06-11 19:01:58 +02:00
|
|
|
/* Selects an SSL crypto engine
|
2005-04-07 17:27:13 +02:00
|
|
|
*/
|
|
|
|
CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_set_engine(data, engine);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2008-06-11 19:01:58 +02:00
|
|
|
/* Selects the default SSL crypto engine
|
2005-04-07 17:27:13 +02:00
|
|
|
*/
|
|
|
|
CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_set_engine_default(data);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return list of OpenSSL crypto engine names. */
|
|
|
|
struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_engines_list(data);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This sets up a session ID cache to the specified size. Make sure this code
|
|
|
|
* is agnostic to what underlying SSL technology we use.
|
|
|
|
*/
|
2012-01-18 23:39:30 +01:00
|
|
|
CURLcode Curl_ssl_initsessions(struct SessionHandle *data, size_t amount)
|
2005-04-07 17:27:13 +02:00
|
|
|
{
|
|
|
|
struct curl_ssl_session *session;
|
|
|
|
|
|
|
|
if(data->state.session)
|
|
|
|
/* this is just a precaution to prevent multiple inits */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2009-11-18 11:33:54 +01:00
|
|
|
session = calloc(amount, sizeof(struct curl_ssl_session));
|
2005-04-07 17:27:13 +02:00
|
|
|
if(!session)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* store the info in the SSL section */
|
2012-01-18 23:39:30 +01:00
|
|
|
data->set.ssl.max_ssl_sessions = amount;
|
2005-04-07 17:27:13 +02:00
|
|
|
data->state.session = session;
|
|
|
|
data->state.sessionage = 1; /* this is brand new */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_ssl_version(char *buffer, size_t size)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_version(buffer, size);
|
2005-04-07 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2006-05-11 00:17:42 +02:00
|
|
|
/*
|
|
|
|
* This function tries to determine connection status.
|
|
|
|
*
|
|
|
|
* Return codes:
|
|
|
|
* 1 means the connection is still in place
|
|
|
|
* 0 means the connection has been closed
|
|
|
|
* -1 means the connection status is unknown
|
|
|
|
*/
|
|
|
|
int Curl_ssl_check_cxn(struct connectdata *conn)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_check_cxn(conn);
|
2006-05-11 00:17:42 +02:00
|
|
|
}
|
2007-01-24 18:19:08 +01:00
|
|
|
|
2007-08-27 08:31:28 +02:00
|
|
|
bool Curl_ssl_data_pending(const struct connectdata *conn,
|
2007-01-24 18:19:08 +01:00
|
|
|
int connindex)
|
|
|
|
{
|
2008-06-11 19:01:58 +02:00
|
|
|
return curlssl_data_pending(conn, connindex);
|
2007-01-24 18:19:08 +01:00
|
|
|
}
|
2008-06-11 19:01:58 +02:00
|
|
|
|
2008-09-05 16:29:21 +02:00
|
|
|
void Curl_ssl_free_certinfo(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct curl_certinfo *ci = &data->info.certs;
|
|
|
|
if(ci->num_of_certs) {
|
|
|
|
/* free all individual lists used */
|
2011-10-07 20:50:57 +02:00
|
|
|
for(i=0; i<ci->num_of_certs; i++) {
|
2008-09-05 16:29:21 +02:00
|
|
|
curl_slist_free_all(ci->certinfo[i]);
|
2011-10-07 20:50:57 +02:00
|
|
|
ci->certinfo[i] = NULL;
|
|
|
|
}
|
2008-09-05 16:29:21 +02:00
|
|
|
free(ci->certinfo); /* free the actual array too */
|
2011-10-07 20:50:57 +02:00
|
|
|
ci->certinfo = NULL;
|
2008-09-05 16:29:21 +02:00
|
|
|
ci->num_of_certs = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 14:52:46 +02:00
|
|
|
|
2013-07-15 17:26:59 +02:00
|
|
|
int Curl_ssl_init_certinfo(struct SessionHandle * data,
|
|
|
|
int num)
|
|
|
|
{
|
|
|
|
struct curl_certinfo * ci = &data->info.certs;
|
|
|
|
struct curl_slist * * table;
|
|
|
|
|
|
|
|
/* Initialize the certificate information structures. Return 0 if OK, else 1.
|
|
|
|
*/
|
|
|
|
Curl_ssl_free_certinfo(data);
|
|
|
|
ci->num_of_certs = num;
|
|
|
|
table = calloc((size_t) num, sizeof(struct curl_slist *));
|
|
|
|
if(!table)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
ci->certinfo = table;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 09:56:18 +01:00
|
|
|
/*
|
|
|
|
* 'value' is NOT a zero terminated string
|
|
|
|
*/
|
2013-07-15 17:26:59 +02:00
|
|
|
CURLcode Curl_ssl_push_certinfo_len(struct SessionHandle *data,
|
|
|
|
int certnum,
|
|
|
|
const char *label,
|
|
|
|
const char *value,
|
|
|
|
size_t valuelen)
|
|
|
|
{
|
|
|
|
struct curl_certinfo * ci = &data->info.certs;
|
|
|
|
char * output;
|
|
|
|
struct curl_slist * nl;
|
|
|
|
CURLcode res = CURLE_OK;
|
2013-11-05 09:56:18 +01:00
|
|
|
size_t labellen = strlen(label);
|
|
|
|
size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
|
2013-07-15 17:26:59 +02:00
|
|
|
|
2013-11-05 09:56:18 +01:00
|
|
|
output = malloc(outlen);
|
2013-07-15 17:26:59 +02:00
|
|
|
if(!output)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2013-11-05 09:56:18 +01:00
|
|
|
/* sprintf the label and colon */
|
|
|
|
snprintf(output, outlen, "%s:", label);
|
|
|
|
|
|
|
|
/* memcpy the value (it might not be zero terminated) */
|
|
|
|
memcpy(&output[labellen+1], value, valuelen);
|
|
|
|
|
|
|
|
/* zero terminate the output */
|
|
|
|
output[labellen + 1 + valuelen] = 0;
|
|
|
|
|
2013-07-15 17:26:59 +02:00
|
|
|
nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
|
|
|
|
if(!nl) {
|
|
|
|
free(output);
|
|
|
|
curl_slist_free_all(ci->certinfo[certnum]);
|
|
|
|
res = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->certinfo[certnum] = nl;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a convenience function for push_certinfo_len that takes a zero
|
|
|
|
* terminated value.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_ssl_push_certinfo(struct SessionHandle *data,
|
|
|
|
int certnum,
|
|
|
|
const char *label,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
size_t valuelen = strlen(value);
|
|
|
|
|
|
|
|
return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
|
|
|
|
}
|
|
|
|
|
2014-06-03 20:04:46 +02:00
|
|
|
int Curl_ssl_random(struct SessionHandle *data,
|
|
|
|
unsigned char *entropy,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
return curlssl_random(data, entropy, length);
|
|
|
|
}
|
|
|
|
|
2014-10-13 18:34:51 +02:00
|
|
|
/*
|
|
|
|
* Generic pinned public key check.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CURLcode Curl_pin_peer_pubkey(const char *pinnedpubkey,
|
|
|
|
const unsigned char *pubkey, size_t pubkeylen)
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
long size = 0;
|
|
|
|
CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
|
|
|
|
/* if a path wasn't specified, don't pin */
|
|
|
|
if(!pinnedpubkey)
|
|
|
|
return CURLE_OK;
|
|
|
|
if(!pubkey || !pubkeylen)
|
|
|
|
return result;
|
|
|
|
fp = fopen(pinnedpubkey, "rb");
|
|
|
|
if(!fp)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Determine the file's size */
|
|
|
|
if(fseek(fp, 0, SEEK_END))
|
|
|
|
break;
|
|
|
|
size = ftell(fp);
|
|
|
|
if(fseek(fp, 0, SEEK_SET))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the size of our certificate doesn't match the size of
|
|
|
|
* the file, they can't be the same, don't bother reading it
|
|
|
|
*/
|
|
|
|
if((long) pubkeylen != size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Allocate buffer for the pinned key. */
|
|
|
|
buf = malloc(pubkeylen);
|
|
|
|
if(!buf)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Returns number of elements read, which should be 1 */
|
|
|
|
if((int) fread(buf, pubkeylen, 1, fp) != 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* The one good exit point */
|
|
|
|
if(!memcmp(pubkey, buf, pubkeylen))
|
|
|
|
result = CURLE_OK;
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
Curl_safefree(buf);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-26 14:52:46 +02:00
|
|
|
void Curl_ssl_md5sum(unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *md5sum, /* output */
|
|
|
|
size_t md5len)
|
|
|
|
{
|
2014-10-13 14:39:50 +02:00
|
|
|
#ifdef curlssl_md5sum
|
2012-06-26 14:52:46 +02:00
|
|
|
curlssl_md5sum(tmp, tmplen, md5sum, md5len);
|
2014-10-13 14:39:50 +02:00
|
|
|
#else
|
|
|
|
MD5_context *MD5pw;
|
|
|
|
|
|
|
|
(void) md5len;
|
|
|
|
MD5pw = Curl_MD5_init(Curl_DIGEST_MD5);
|
|
|
|
Curl_MD5_update(MD5pw, tmp, tmplen);
|
|
|
|
Curl_MD5_final(MD5pw, md5sum);
|
2013-06-24 22:24:35 +02:00
|
|
|
#endif
|
2014-10-13 14:39:50 +02:00
|
|
|
}
|
2012-06-27 23:15:56 +02:00
|
|
|
|
2008-09-05 20:35:29 +02:00
|
|
|
#endif /* USE_SSL */
|