2007-02-12 17:32:37 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-05-04 06:10:39 -04:00
|
|
|
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-02-12 17:32:37 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2007-02-12 17:32:37 -05:00
|
|
|
*
|
|
|
|
* 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 NSS-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.
|
2007-02-12 17:32:37 -05:00
|
|
|
*/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2012-06-28 10:58:07 -04:00
|
|
|
#ifdef USE_NSS
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "formdata.h" /* for the boundary function */
|
|
|
|
#include "url.h" /* for the ssl config check function */
|
|
|
|
#include "connect.h"
|
2016-10-31 12:33:46 -04:00
|
|
|
#include "strcase.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "select.h"
|
2013-12-25 05:20:39 -05:00
|
|
|
#include "vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "llist.h"
|
2015-03-03 17:17:43 -05:00
|
|
|
#include "curl_printf.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "nssg.h"
|
2007-02-12 17:32:37 -05:00
|
|
|
#include <nspr.h>
|
|
|
|
#include <nss.h>
|
|
|
|
#include <ssl.h>
|
|
|
|
#include <sslerr.h>
|
|
|
|
#include <secerr.h>
|
2007-09-18 18:21:54 -04:00
|
|
|
#include <secmod.h>
|
2007-02-12 17:32:37 -05:00
|
|
|
#include <sslproto.h>
|
|
|
|
#include <prtypes.h>
|
|
|
|
#include <pk11pub.h>
|
2008-06-06 14:40:21 -04:00
|
|
|
#include <prio.h>
|
|
|
|
#include <secitem.h>
|
|
|
|
#include <secport.h>
|
2008-06-18 17:48:51 -04:00
|
|
|
#include <certdb.h>
|
2009-09-06 13:59:46 -04:00
|
|
|
#include <base64.h>
|
2010-05-11 08:10:27 -04:00
|
|
|
#include <cert.h>
|
2012-04-11 07:44:20 -04:00
|
|
|
#include <prerror.h>
|
2017-04-06 13:52:39 -04:00
|
|
|
#include <keyhi.h> /* for SECKEY_DestroyPublicKey() */
|
|
|
|
#include <private/pprio.h> /* for PR_ImportTCPSocket */
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2014-06-24 17:25:59 -04:00
|
|
|
#define NSSVERNUM ((NSS_VMAJOR<<16)|(NSS_VMINOR<<8)|NSS_VPATCH)
|
|
|
|
|
|
|
|
#if NSSVERNUM >= 0x030f00 /* 3.15.0 */
|
|
|
|
#include <ocsp.h>
|
|
|
|
#endif
|
|
|
|
|
2016-10-31 07:51:45 -04:00
|
|
|
#include "strcase.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "warnless.h"
|
2013-11-03 11:36:51 -05:00
|
|
|
#include "x509asn1.h"
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2015-03-24 18:12:03 -04:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
#define SSL_DIR "/etc/pki/nssdb"
|
|
|
|
|
|
|
|
/* enough to fit the string "PEM Token #[0|1]" */
|
|
|
|
#define SLOTSIZE 13
|
|
|
|
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
struct ssl_backend_data {
|
|
|
|
PRFileDesc *handle;
|
|
|
|
char *client_nickname;
|
|
|
|
struct Curl_easy *data;
|
|
|
|
struct curl_llist obj_list;
|
|
|
|
PK11GenericObject *obj_clicert;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BACKEND connssl->backend
|
2017-06-21 06:41:18 -04:00
|
|
|
|
2016-08-17 04:50:06 -04:00
|
|
|
static PRLock *nss_initlock = NULL;
|
|
|
|
static PRLock *nss_crllock = NULL;
|
2016-08-26 02:28:39 -04:00
|
|
|
static PRLock *nss_findslot_lock = NULL;
|
2017-04-10 11:40:30 -04:00
|
|
|
static PRLock *nss_trustload_lock = NULL;
|
2017-04-05 17:25:28 -04:00
|
|
|
static struct curl_llist nss_crl_list;
|
2016-08-17 04:50:06 -04:00
|
|
|
static NSSInitContext *nss_context = NULL;
|
|
|
|
static volatile int initialized = 0;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2017-04-24 11:25:00 -04:00
|
|
|
/* type used to wrap pointers as list nodes */
|
|
|
|
struct ptr_list_wrap {
|
|
|
|
void *ptr;
|
|
|
|
struct curl_llist_element node;
|
|
|
|
};
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
int num;
|
|
|
|
} cipher_s;
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
#define PK11_SETATTRS(_attr, _idx, _type, _val, _len) do { \
|
|
|
|
CK_ATTRIBUTE *ptr = (_attr) + ((_idx)++); \
|
|
|
|
ptr->type = (_type); \
|
|
|
|
ptr->pValue = (_val); \
|
|
|
|
ptr->ulValueLen = (_len); \
|
2011-09-03 10:06:10 -04:00
|
|
|
} WHILE_FALSE
|
2007-09-18 18:21:54 -04:00
|
|
|
|
|
|
|
#define CERT_NewTempCertificate __CERT_NewTempCertificate
|
|
|
|
|
2008-06-19 01:47:27 -04:00
|
|
|
#define NUM_OF_CIPHERS sizeof(cipherlist)/sizeof(cipherlist[0])
|
|
|
|
static const cipher_s cipherlist[] = {
|
2007-02-12 17:32:37 -05:00
|
|
|
/* SSL2 cipher suites */
|
2012-08-09 08:08:11 -04:00
|
|
|
{"rc4", SSL_EN_RC4_128_WITH_MD5},
|
|
|
|
{"rc4-md5", SSL_EN_RC4_128_WITH_MD5},
|
|
|
|
{"rc4export", SSL_EN_RC4_128_EXPORT40_WITH_MD5},
|
|
|
|
{"rc2", SSL_EN_RC2_128_CBC_WITH_MD5},
|
|
|
|
{"rc2export", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5},
|
|
|
|
{"des", SSL_EN_DES_64_CBC_WITH_MD5},
|
|
|
|
{"desede3", SSL_EN_DES_192_EDE3_CBC_WITH_MD5},
|
2007-02-12 17:32:37 -05:00
|
|
|
/* SSL3/TLS cipher suites */
|
2012-08-09 08:08:11 -04:00
|
|
|
{"rsa_rc4_128_md5", SSL_RSA_WITH_RC4_128_MD5},
|
|
|
|
{"rsa_rc4_128_sha", SSL_RSA_WITH_RC4_128_SHA},
|
|
|
|
{"rsa_3des_sha", SSL_RSA_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"rsa_des_sha", SSL_RSA_WITH_DES_CBC_SHA},
|
|
|
|
{"rsa_rc4_40_md5", SSL_RSA_EXPORT_WITH_RC4_40_MD5},
|
|
|
|
{"rsa_rc2_40_md5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5},
|
|
|
|
{"rsa_null_md5", SSL_RSA_WITH_NULL_MD5},
|
|
|
|
{"rsa_null_sha", SSL_RSA_WITH_NULL_SHA},
|
|
|
|
{"fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA},
|
|
|
|
{"fortezza", SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA},
|
|
|
|
{"fortezza_rc4_128_sha", SSL_FORTEZZA_DMS_WITH_RC4_128_SHA},
|
|
|
|
{"fortezza_null", SSL_FORTEZZA_DMS_WITH_NULL_SHA},
|
2007-02-12 17:32:37 -05:00
|
|
|
/* TLS 1.0: Exportable 56-bit Cipher Suites. */
|
2012-08-09 08:08:11 -04:00
|
|
|
{"rsa_des_56_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA},
|
|
|
|
{"rsa_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
|
2007-02-12 17:32:37 -05:00
|
|
|
/* AES ciphers. */
|
2014-03-07 07:02:03 -05:00
|
|
|
{"dhe_dss_aes_128_cbc_sha", TLS_DHE_DSS_WITH_AES_128_CBC_SHA},
|
|
|
|
{"dhe_dss_aes_256_cbc_sha", TLS_DHE_DSS_WITH_AES_256_CBC_SHA},
|
|
|
|
{"dhe_rsa_aes_128_cbc_sha", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"dhe_rsa_aes_256_cbc_sha", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
|
2012-08-09 08:08:11 -04:00
|
|
|
{"rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA},
|
2007-02-12 17:32:37 -05:00
|
|
|
/* ECC ciphers. */
|
2012-08-09 08:08:11 -04:00
|
|
|
{"ecdh_ecdsa_null_sha", TLS_ECDH_ECDSA_WITH_NULL_SHA},
|
|
|
|
{"ecdh_ecdsa_rc4_128_sha", TLS_ECDH_ECDSA_WITH_RC4_128_SHA},
|
|
|
|
{"ecdh_ecdsa_3des_sha", TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"ecdh_ecdsa_aes_128_sha", TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"ecdh_ecdsa_aes_256_sha", TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA},
|
|
|
|
{"ecdhe_ecdsa_null_sha", TLS_ECDHE_ECDSA_WITH_NULL_SHA},
|
|
|
|
{"ecdhe_ecdsa_rc4_128_sha", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
|
|
|
|
{"ecdhe_ecdsa_3des_sha", TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"ecdhe_ecdsa_aes_128_sha", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"ecdhe_ecdsa_aes_256_sha", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
|
|
|
|
{"ecdh_rsa_null_sha", TLS_ECDH_RSA_WITH_NULL_SHA},
|
|
|
|
{"ecdh_rsa_128_sha", TLS_ECDH_RSA_WITH_RC4_128_SHA},
|
|
|
|
{"ecdh_rsa_3des_sha", TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"ecdh_rsa_aes_128_sha", TLS_ECDH_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"ecdh_rsa_aes_256_sha", TLS_ECDH_RSA_WITH_AES_256_CBC_SHA},
|
2016-09-19 10:37:05 -04:00
|
|
|
{"ecdhe_rsa_null", TLS_ECDHE_RSA_WITH_NULL_SHA},
|
2012-08-09 08:08:11 -04:00
|
|
|
{"ecdhe_rsa_rc4_128_sha", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
|
|
|
|
{"ecdhe_rsa_3des_sha", TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"ecdhe_rsa_aes_128_sha", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
|
|
|
|
{"ecdhe_rsa_aes_256_sha", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
|
|
|
|
{"ecdh_anon_null_sha", TLS_ECDH_anon_WITH_NULL_SHA},
|
|
|
|
{"ecdh_anon_rc4_128sha", TLS_ECDH_anon_WITH_RC4_128_SHA},
|
|
|
|
{"ecdh_anon_3des_sha", TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA},
|
|
|
|
{"ecdh_anon_aes_128_sha", TLS_ECDH_anon_WITH_AES_128_CBC_SHA},
|
|
|
|
{"ecdh_anon_aes_256_sha", TLS_ECDH_anon_WITH_AES_256_CBC_SHA},
|
2014-03-07 07:10:54 -05:00
|
|
|
#ifdef TLS_RSA_WITH_NULL_SHA256
|
|
|
|
/* new HMAC-SHA256 cipher suites specified in RFC */
|
|
|
|
{"rsa_null_sha_256", TLS_RSA_WITH_NULL_SHA256},
|
|
|
|
{"rsa_aes_128_cbc_sha_256", TLS_RSA_WITH_AES_128_CBC_SHA256},
|
|
|
|
{"rsa_aes_256_cbc_sha_256", TLS_RSA_WITH_AES_256_CBC_SHA256},
|
|
|
|
{"dhe_rsa_aes_128_cbc_sha_256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
|
|
|
|
{"dhe_rsa_aes_256_cbc_sha_256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
|
|
|
|
{"ecdhe_ecdsa_aes_128_cbc_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
|
|
|
|
{"ecdhe_rsa_aes_128_cbc_sha_256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
|
|
|
|
#endif
|
2014-03-07 07:14:08 -05:00
|
|
|
#ifdef TLS_RSA_WITH_AES_128_GCM_SHA256
|
|
|
|
/* AES GCM cipher suites in RFC 5288 and RFC 5289 */
|
|
|
|
{"rsa_aes_128_gcm_sha_256", TLS_RSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"dhe_rsa_aes_128_gcm_sha_256", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"dhe_dss_aes_128_gcm_sha_256", TLS_DHE_DSS_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"ecdhe_ecdsa_aes_128_gcm_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"ecdh_ecdsa_aes_128_gcm_sha_256", TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"ecdhe_rsa_aes_128_gcm_sha_256", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
{"ecdh_rsa_aes_128_gcm_sha_256", TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256},
|
|
|
|
#endif
|
2016-09-19 11:38:23 -04:00
|
|
|
#ifdef TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
|
|
|
|
/* cipher suites using SHA384 */
|
|
|
|
{"rsa_aes_256_gcm_sha_384", TLS_RSA_WITH_AES_256_GCM_SHA384},
|
|
|
|
{"dhe_rsa_aes_256_gcm_sha_384", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
|
|
|
|
{"dhe_dss_aes_256_gcm_sha_384", TLS_DHE_DSS_WITH_AES_256_GCM_SHA384},
|
|
|
|
{"ecdhe_ecdsa_aes_256_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
|
|
|
|
{"ecdhe_rsa_aes_256_sha_384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
|
|
|
|
{"ecdhe_ecdsa_aes_256_gcm_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
|
|
|
|
{"ecdhe_rsa_aes_256_gcm_sha_384", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
|
|
|
|
#endif
|
2016-09-19 11:45:53 -04:00
|
|
|
#ifdef TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
|
|
|
|
/* chacha20-poly1305 cipher suites */
|
|
|
|
{"ecdhe_rsa_chacha20_poly1305_sha_256",
|
|
|
|
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
|
|
|
|
{"ecdhe_ecdsa_chacha20_poly1305_sha_256",
|
|
|
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
|
|
|
|
{"dhe_rsa_chacha20_poly1305_sha_256",
|
|
|
|
TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
|
|
|
|
#endif
|
2007-02-12 17:32:37 -05:00
|
|
|
};
|
|
|
|
|
2018-10-02 22:19:16 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
static const char *pem_library = "nsspem.dll";
|
|
|
|
static const char *trust_library = "nssckbi.dll";
|
|
|
|
#else
|
2016-11-23 01:53:24 -05:00
|
|
|
static const char *pem_library = "libnsspem.so";
|
2017-04-10 11:40:30 -04:00
|
|
|
static const char *trust_library = "libnssckbi.so";
|
2018-10-02 22:19:16 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static SECMODModule *pem_module = NULL;
|
2017-04-10 11:40:30 -04:00
|
|
|
static SECMODModule *trust_module = NULL;
|
|
|
|
|
2014-04-23 09:37:26 -04:00
|
|
|
/* NSPR I/O layer we use to detect blocking direction during SSL handshake */
|
|
|
|
static PRDescIdentity nspr_io_identity = PR_INVALID_IO_LAYER;
|
|
|
|
static PRIOMethods nspr_io_methods;
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static const char *nss_error_to_name(PRErrorCode code)
|
2012-04-11 07:44:20 -04:00
|
|
|
{
|
|
|
|
const char *name = PR_ErrorToName(code);
|
|
|
|
if(name)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
static void nss_print_error_message(struct Curl_easy *data, PRUint32 err)
|
2012-05-21 10:19:12 -04:00
|
|
|
{
|
|
|
|
failf(data, "%s", PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT));
|
|
|
|
}
|
|
|
|
|
2018-12-04 05:48:32 -05:00
|
|
|
static char *nss_sslver_to_name(PRUint16 nssver)
|
|
|
|
{
|
|
|
|
switch(nssver) {
|
|
|
|
case SSL_LIBRARY_VERSION_2:
|
|
|
|
return strdup("SSLv2");
|
|
|
|
case SSL_LIBRARY_VERSION_3_0:
|
|
|
|
return strdup("SSLv3");
|
|
|
|
case SSL_LIBRARY_VERSION_TLS_1_0:
|
|
|
|
return strdup("TLSv1.0");
|
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_1
|
|
|
|
case SSL_LIBRARY_VERSION_TLS_1_1:
|
|
|
|
return strdup("TLSv1.1");
|
|
|
|
#endif
|
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_2
|
|
|
|
case SSL_LIBRARY_VERSION_TLS_1_2:
|
|
|
|
return strdup("TLSv1.2");
|
|
|
|
#endif
|
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_3
|
|
|
|
case SSL_LIBRARY_VERSION_TLS_1_3:
|
|
|
|
return strdup("TLSv1.3");
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return curl_maprintf("0x%04x", nssver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
static SECStatus set_ciphers(struct Curl_easy *data, PRFileDesc * model,
|
2007-02-12 17:32:37 -05:00
|
|
|
char *cipher_list)
|
|
|
|
{
|
2008-06-19 01:47:27 -04:00
|
|
|
unsigned int i;
|
|
|
|
PRBool cipher_state[NUM_OF_CIPHERS];
|
2007-02-12 17:32:37 -05:00
|
|
|
PRBool found;
|
|
|
|
char *cipher;
|
|
|
|
|
2015-09-04 08:35:36 -04:00
|
|
|
/* use accessors to avoid dynamic linking issues after an update of NSS */
|
|
|
|
const PRUint16 num_implemented_ciphers = SSL_GetNumImplementedCiphers();
|
|
|
|
const PRUint16 *implemented_ciphers = SSL_GetImplementedCiphers();
|
|
|
|
if(!implemented_ciphers)
|
|
|
|
return SECFailure;
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/* First disable all ciphers. This uses a different max value in case
|
|
|
|
* NSS adds more ciphers later we don't want them available by
|
|
|
|
* accident
|
|
|
|
*/
|
2015-09-04 08:35:36 -04:00
|
|
|
for(i = 0; i < num_implemented_ciphers; i++) {
|
|
|
|
SSL_CipherPrefSet(model, implemented_ciphers[i], PR_FALSE);
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set every entry in our list to false */
|
2015-09-04 08:35:36 -04:00
|
|
|
for(i = 0; i < NUM_OF_CIPHERS; i++) {
|
2007-02-12 17:32:37 -05:00
|
|
|
cipher_state[i] = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cipher = cipher_list;
|
|
|
|
|
|
|
|
while(cipher_list && (cipher_list[0])) {
|
2007-02-13 12:28:40 -05:00
|
|
|
while((*cipher) && (ISSPACE(*cipher)))
|
2007-02-12 17:32:37 -05:00
|
|
|
++cipher;
|
|
|
|
|
2016-12-13 19:29:44 -05:00
|
|
|
cipher_list = strchr(cipher, ',');
|
|
|
|
if(cipher_list) {
|
2007-02-12 17:32:37 -05:00
|
|
|
*cipher_list++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
found = PR_FALSE;
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i<NUM_OF_CIPHERS; i++) {
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strcasecompare(cipher, cipherlist[i].name)) {
|
2007-02-12 17:32:37 -05:00
|
|
|
cipher_state[i] = PR_TRUE;
|
|
|
|
found = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found == PR_FALSE) {
|
2007-09-18 18:21:54 -04:00
|
|
|
failf(data, "Unknown cipher in list: %s", cipher);
|
2007-02-12 17:32:37 -05:00
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cipher_list) {
|
|
|
|
cipher = cipher_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally actually enable the selected ciphers */
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i<NUM_OF_CIPHERS; i++) {
|
2014-01-29 06:55:36 -05:00
|
|
|
if(!cipher_state[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(SSL_CipherPrefSet(model, cipherlist[i].num, PR_TRUE) != SECSuccess) {
|
2012-08-09 08:08:11 -04:00
|
|
|
failf(data, "cipher-suite not supported by NSS: %s", cipherlist[i].name);
|
2007-02-12 17:32:37 -05:00
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
2009-01-07 09:10:35 -05:00
|
|
|
/*
|
2016-02-04 04:41:15 -05:00
|
|
|
* Return true if at least one cipher-suite is enabled. Used to determine
|
2009-01-07 09:10:35 -05:00
|
|
|
* if we need to call NSS_SetDomesticPolicy() to enable the default ciphers.
|
|
|
|
*/
|
2016-02-04 04:41:15 -05:00
|
|
|
static bool any_cipher_enabled(void)
|
2009-01-07 09:10:35 -05:00
|
|
|
{
|
2009-01-07 09:12:01 -05:00
|
|
|
unsigned int i;
|
2009-01-07 09:10:35 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i<NUM_OF_CIPHERS; i++) {
|
2016-02-04 04:41:15 -05:00
|
|
|
PRInt32 policy = 0;
|
2009-01-07 09:10:35 -05:00
|
|
|
SSL_CipherPolicyGet(cipherlist[i].num, &policy);
|
|
|
|
if(policy)
|
2016-02-04 04:41:15 -05:00
|
|
|
return TRUE;
|
2009-01-07 09:10:35 -05:00
|
|
|
}
|
2016-02-04 04:41:15 -05:00
|
|
|
|
|
|
|
return FALSE;
|
2009-01-07 09:10:35 -05:00
|
|
|
}
|
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
/*
|
|
|
|
* Determine whether the nickname passed in is a filename that needs to
|
|
|
|
* be loaded as a PEM or a regular NSS nickname.
|
|
|
|
*
|
|
|
|
* returns 1 for a file
|
|
|
|
* returns 0 for not a file (NSS nickname)
|
|
|
|
*/
|
|
|
|
static int is_file(const char *filename)
|
|
|
|
{
|
2009-09-06 15:45:08 -04:00
|
|
|
struct_stat st;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
|
|
|
if(filename == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(stat(filename, &st) == 0)
|
|
|
|
if(S_ISREG(st.st_mode))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-26 08:53:26 -04:00
|
|
|
/* Check if the given string is filename or nickname of a certificate. If the
|
|
|
|
* given string is recognized as filename, return NULL. If the given string is
|
|
|
|
* recognized as nickname, return a duplicated string. The returned string
|
|
|
|
* should be later deallocated using free(). If the OOM failure occurs, we
|
|
|
|
* return NULL, too.
|
2011-01-04 07:52:54 -05:00
|
|
|
*/
|
2016-11-23 01:53:24 -05:00
|
|
|
static char *dup_nickname(struct Curl_easy *data, const char *str)
|
2009-10-07 16:34:08 -04:00
|
|
|
{
|
2011-01-04 07:52:54 -05:00
|
|
|
const char *n;
|
|
|
|
|
|
|
|
if(!is_file(str))
|
|
|
|
/* no such file exists, use the string as nickname */
|
|
|
|
return strdup(str);
|
|
|
|
|
2016-02-04 04:44:52 -05:00
|
|
|
/* search the first slash; we require at least one slash in a file name */
|
|
|
|
n = strchr(str, '/');
|
2011-01-04 07:52:54 -05:00
|
|
|
if(!n) {
|
|
|
|
infof(data, "warning: certificate file name \"%s\" handled as nickname; "
|
|
|
|
"please use \"./%s\" to force file name\n", str, str);
|
|
|
|
return strdup(str);
|
2010-02-17 07:13:55 -05:00
|
|
|
}
|
2009-10-07 16:34:08 -04:00
|
|
|
|
2011-01-04 07:52:54 -05:00
|
|
|
/* we'll use the PEM reader to read the certificate from file */
|
2011-08-26 08:53:26 -04:00
|
|
|
return NULL;
|
2009-10-07 16:34:08 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 02:28:39 -04:00
|
|
|
/* Lock/unlock wrapper for PK11_FindSlotByName() to work around race condition
|
|
|
|
* in nssSlot_IsTokenPresent() causing spurious SEC_ERROR_NO_TOKEN. For more
|
|
|
|
* details, go to <https://bugzilla.mozilla.org/1297397>.
|
|
|
|
*/
|
|
|
|
static PK11SlotInfo* nss_find_slot_by_name(const char *slot_name)
|
|
|
|
{
|
|
|
|
PK11SlotInfo *slot;
|
2017-01-15 07:10:43 -05:00
|
|
|
PR_Lock(nss_findslot_lock);
|
2016-08-26 02:28:39 -04:00
|
|
|
slot = PK11_FindSlotByName(slot_name);
|
2017-01-15 07:10:43 -05:00
|
|
|
PR_Unlock(nss_findslot_lock);
|
2016-08-26 02:28:39 -04:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2017-04-24 11:25:00 -04:00
|
|
|
/* wrap 'ptr' as list node and tail-insert into 'list' */
|
|
|
|
static CURLcode insert_wrapped_ptr(struct curl_llist *list, void *ptr)
|
|
|
|
{
|
2018-05-11 17:40:58 -04:00
|
|
|
struct ptr_list_wrap *wrap = malloc(sizeof(*wrap));
|
2017-04-24 11:25:00 -04:00
|
|
|
if(!wrap)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
wrap->ptr = ptr;
|
|
|
|
Curl_llist_insert_next(list, list->tail, wrap, &wrap->node);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
/* Call PK11_CreateGenericObject() with the given obj_class and filename. If
|
|
|
|
* the call succeeds, append the object handle to the list of objects so that
|
|
|
|
* the object can be destroyed in Curl_nss_close(). */
|
2017-06-21 06:41:18 -04:00
|
|
|
static CURLcode nss_create_object(struct ssl_connect_data *connssl,
|
2011-01-27 04:55:02 -05:00
|
|
|
CK_OBJECT_CLASS obj_class,
|
|
|
|
const char *filename, bool cacert)
|
|
|
|
{
|
|
|
|
PK11SlotInfo *slot;
|
|
|
|
PK11GenericObject *obj;
|
|
|
|
CK_BBOOL cktrue = CK_TRUE;
|
|
|
|
CK_BBOOL ckfalse = CK_FALSE;
|
|
|
|
CK_ATTRIBUTE attrs[/* max count of attributes */ 4];
|
|
|
|
int attr_cnt = 0;
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result = (cacert)
|
2011-08-26 09:43:48 -04:00
|
|
|
? CURLE_SSL_CACERT_BADFILE
|
|
|
|
: CURLE_SSL_CERTPROBLEM;
|
2011-01-27 04:55:02 -05:00
|
|
|
|
|
|
|
const int slot_id = (cacert) ? 0 : 1;
|
|
|
|
char *slot_name = aprintf("PEM Token #%d", slot_id);
|
|
|
|
if(!slot_name)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2016-08-26 02:28:39 -04:00
|
|
|
slot = nss_find_slot_by_name(slot_name);
|
2011-01-27 04:55:02 -05:00
|
|
|
free(slot_name);
|
|
|
|
if(!slot)
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2011-01-27 04:55:02 -05:00
|
|
|
|
|
|
|
PK11_SETATTRS(attrs, attr_cnt, CKA_CLASS, &obj_class, sizeof(obj_class));
|
|
|
|
PK11_SETATTRS(attrs, attr_cnt, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL));
|
|
|
|
PK11_SETATTRS(attrs, attr_cnt, CKA_LABEL, (unsigned char *)filename,
|
2017-04-06 13:52:39 -04:00
|
|
|
(CK_ULONG)strlen(filename) + 1);
|
2011-01-27 04:55:02 -05:00
|
|
|
|
|
|
|
if(CKO_CERTIFICATE == obj_class) {
|
|
|
|
CK_BBOOL *pval = (cacert) ? (&cktrue) : (&ckfalse);
|
|
|
|
PK11_SETATTRS(attrs, attr_cnt, CKA_TRUST, pval, sizeof(*pval));
|
|
|
|
}
|
|
|
|
|
2018-02-08 05:23:49 -05:00
|
|
|
/* PK11_CreateManagedGenericObject() was introduced in NSS 3.34 because
|
|
|
|
* PK11_DestroyGenericObject() does not release resources allocated by
|
|
|
|
* PK11_CreateGenericObject() early enough. */
|
|
|
|
obj =
|
|
|
|
#ifdef HAVE_PK11_CREATEMANAGEDGENERICOBJECT
|
|
|
|
PK11_CreateManagedGenericObject
|
|
|
|
#else
|
|
|
|
PK11_CreateGenericObject
|
|
|
|
#endif
|
|
|
|
(slot, attrs, attr_cnt, PR_FALSE);
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
if(!obj)
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2011-01-27 04:55:02 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(insert_wrapped_ptr(&BACKEND->obj_list, obj) != CURLE_OK) {
|
2011-01-27 04:55:02 -05:00
|
|
|
PK11_DestroyGenericObject(obj);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-08-26 08:38:18 -04:00
|
|
|
if(!cacert && CKO_CERTIFICATE == obj_class)
|
|
|
|
/* store reference to a client certificate */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->obj_clicert = obj;
|
2011-08-26 08:38:18 -04:00
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the NSS object whose handle is given by ptr. This function is
|
|
|
|
* a callback of Curl_llist_alloc() used by Curl_llist_destroy() to destroy
|
|
|
|
* NSS objects in Curl_nss_close() */
|
|
|
|
static void nss_destroy_object(void *user, void *ptr)
|
|
|
|
{
|
2017-04-24 11:25:00 -04:00
|
|
|
struct ptr_list_wrap *wrap = (struct ptr_list_wrap *) ptr;
|
|
|
|
PK11GenericObject *obj = (PK11GenericObject *) wrap->ptr;
|
2011-01-27 04:55:02 -05:00
|
|
|
(void) user;
|
|
|
|
PK11_DestroyGenericObject(obj);
|
2017-04-24 11:25:00 -04:00
|
|
|
free(wrap);
|
2011-01-27 04:55:02 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 18:39:23 -04:00
|
|
|
/* same as nss_destroy_object() but for CRL items */
|
|
|
|
static void nss_destroy_crl_item(void *user, void *ptr)
|
|
|
|
{
|
2017-04-24 11:25:00 -04:00
|
|
|
struct ptr_list_wrap *wrap = (struct ptr_list_wrap *) ptr;
|
|
|
|
SECItem *crl_der = (SECItem *) wrap->ptr;
|
2014-07-03 18:39:23 -04:00
|
|
|
(void) user;
|
|
|
|
SECITEM_FreeItem(crl_der, PR_TRUE);
|
2017-04-24 11:25:00 -04:00
|
|
|
free(wrap);
|
2014-07-03 18:39:23 -04:00
|
|
|
}
|
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
static CURLcode nss_load_cert(struct ssl_connect_data *ssl,
|
|
|
|
const char *filename, PRBool cacert)
|
2007-09-18 18:21:54 -04:00
|
|
|
{
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result = (cacert)
|
2011-08-26 09:43:48 -04:00
|
|
|
? CURLE_SSL_CACERT_BADFILE
|
|
|
|
: CURLE_SSL_CERTPROBLEM;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
/* libnsspem.so leaks memory if the requested file does not exist. For more
|
|
|
|
* details, go to <https://bugzilla.redhat.com/734760>. */
|
|
|
|
if(is_file(filename))
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_create_object(ssl, CKO_CERTIFICATE, filename, cacert);
|
2011-09-06 12:17:38 -04:00
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
if(!result && !cacert) {
|
2011-09-06 12:17:38 -04:00
|
|
|
/* we have successfully loaded a client certificate */
|
|
|
|
CERTCertificate *cert;
|
|
|
|
char *nickname = NULL;
|
|
|
|
char *n = strrchr(filename, '/');
|
|
|
|
if(n)
|
|
|
|
n++;
|
|
|
|
|
|
|
|
/* The following undocumented magic helps to avoid a SIGSEGV on call
|
|
|
|
* of PK11_ReadRawAttribute() from SelectClientCert() when using an
|
|
|
|
* immature version of libnsspem.so. For more details, go to
|
|
|
|
* <https://bugzilla.redhat.com/733685>. */
|
|
|
|
nickname = aprintf("PEM Token #1:%s", n);
|
|
|
|
if(nickname) {
|
|
|
|
cert = PK11_FindCertFromNickname(nickname, NULL);
|
|
|
|
if(cert)
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
|
|
|
|
free(nickname);
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2010-05-11 08:10:27 -04:00
|
|
|
/* add given CRL to cache if it is not already there */
|
2014-07-04 06:41:53 -04:00
|
|
|
static CURLcode nss_cache_crl(SECItem *crl_der)
|
2010-05-11 08:10:27 -04:00
|
|
|
{
|
|
|
|
CERTCertDBHandle *db = CERT_GetDefaultCertDB();
|
2014-07-03 18:36:21 -04:00
|
|
|
CERTSignedCrl *crl = SEC_FindCrlByDERCert(db, crl_der, 0);
|
2010-05-11 08:10:27 -04:00
|
|
|
if(crl) {
|
|
|
|
/* CRL already cached */
|
|
|
|
SEC_DestroyCrl(crl);
|
2014-07-03 18:36:21 -04:00
|
|
|
SECITEM_FreeItem(crl_der, PR_TRUE);
|
2014-10-08 11:13:59 -04:00
|
|
|
return CURLE_OK;
|
2010-05-11 08:10:27 -04:00
|
|
|
}
|
|
|
|
|
2014-07-04 06:41:53 -04:00
|
|
|
/* acquire lock before call of CERT_CacheCRL() and accessing nss_crl_list */
|
|
|
|
PR_Lock(nss_crllock);
|
|
|
|
|
|
|
|
/* store the CRL item so that we can free it in Curl_nss_cleanup() */
|
2017-04-24 11:25:00 -04:00
|
|
|
if(insert_wrapped_ptr(&nss_crl_list, crl_der) != CURLE_OK) {
|
2014-07-04 06:41:53 -04:00
|
|
|
SECITEM_FreeItem(crl_der, PR_TRUE);
|
|
|
|
PR_Unlock(nss_crllock);
|
2014-07-03 18:39:23 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:36:21 -04:00
|
|
|
if(SECSuccess != CERT_CacheCRL(db, crl_der)) {
|
2010-05-11 08:10:27 -04:00
|
|
|
/* unable to cache CRL */
|
|
|
|
PR_Unlock(nss_crllock);
|
2014-07-03 17:53:44 -04:00
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
2010-05-11 08:10:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we need to clear session cache, so that the CRL could take effect */
|
|
|
|
SSL_ClearSessionCache();
|
|
|
|
PR_Unlock(nss_crllock);
|
2014-07-03 17:53:44 -04:00
|
|
|
return CURLE_OK;
|
2010-05-11 08:10:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static CURLcode nss_load_crl(const char *crlfilename)
|
2008-06-06 14:40:21 -04:00
|
|
|
{
|
|
|
|
PRFileDesc *infile;
|
|
|
|
PRFileInfo info;
|
2010-05-11 08:39:08 -04:00
|
|
|
SECItem filedata = { 0, NULL, 0 };
|
2014-07-03 18:36:21 -04:00
|
|
|
SECItem *crl_der = NULL;
|
2010-05-11 08:39:08 -04:00
|
|
|
char *body;
|
2008-06-06 14:40:21 -04:00
|
|
|
|
2010-05-11 08:39:08 -04:00
|
|
|
infile = PR_Open(crlfilename, PR_RDONLY, 0);
|
|
|
|
if(!infile)
|
2014-07-03 17:53:44 -04:00
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
2008-06-06 16:57:32 -04:00
|
|
|
|
2010-05-11 08:39:08 -04:00
|
|
|
if(PR_SUCCESS != PR_GetOpenFileInfo(infile, &info))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if(!SECITEM_AllocItem(NULL, &filedata, info.size + /* zero ended */ 1))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if(info.size != PR_Read(infile, filedata.data, info.size))
|
|
|
|
goto fail;
|
|
|
|
|
2014-07-03 18:36:21 -04:00
|
|
|
crl_der = SECITEM_AllocItem(NULL, NULL, 0U);
|
|
|
|
if(!crl_der)
|
|
|
|
goto fail;
|
|
|
|
|
2010-05-11 08:39:08 -04:00
|
|
|
/* place a trailing zero right after the visible data */
|
2016-11-23 02:30:18 -05:00
|
|
|
body = (char *)filedata.data;
|
2010-05-11 08:39:08 -04:00
|
|
|
body[--filedata.len] = '\0';
|
|
|
|
|
|
|
|
body = strstr(body, "-----BEGIN");
|
|
|
|
if(body) {
|
|
|
|
/* assume ASCII */
|
|
|
|
char *trailer;
|
|
|
|
char *begin = PORT_Strchr(body, '\n');
|
|
|
|
if(!begin)
|
|
|
|
begin = PORT_Strchr(body, '\r');
|
|
|
|
if(!begin)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
trailer = strstr(++begin, "-----END");
|
|
|
|
if(!trailer)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* retrieve DER from ASCII */
|
|
|
|
*trailer = '\0';
|
2014-07-03 18:36:21 -04:00
|
|
|
if(ATOB_ConvertAsciiToItem(crl_der, begin))
|
2010-05-11 08:39:08 -04:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
SECITEM_FreeItem(&filedata, PR_FALSE);
|
2008-06-06 14:40:21 -04:00
|
|
|
}
|
2010-05-11 08:39:08 -04:00
|
|
|
else
|
|
|
|
/* assume DER */
|
2014-07-03 18:36:21 -04:00
|
|
|
*crl_der = filedata;
|
2010-05-11 08:39:08 -04:00
|
|
|
|
|
|
|
PR_Close(infile);
|
2014-07-04 06:41:53 -04:00
|
|
|
return nss_cache_crl(crl_der);
|
2008-06-06 14:40:21 -04:00
|
|
|
|
2010-05-11 08:39:08 -04:00
|
|
|
fail:
|
|
|
|
PR_Close(infile);
|
2014-07-03 18:36:21 -04:00
|
|
|
SECITEM_FreeItem(crl_der, PR_TRUE);
|
2010-05-11 08:39:08 -04:00
|
|
|
SECITEM_FreeItem(&filedata, PR_FALSE);
|
2014-07-03 17:53:44 -04:00
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
2008-06-06 14:40:21 -04:00
|
|
|
}
|
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
static CURLcode nss_load_key(struct connectdata *conn, int sockindex,
|
|
|
|
char *key_file)
|
2007-09-18 18:21:54 -04:00
|
|
|
{
|
2017-04-24 09:01:04 -04:00
|
|
|
PK11SlotInfo *slot, *tmp;
|
2011-01-27 04:55:02 -05:00
|
|
|
SECStatus status;
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2011-01-27 04:55:02 -05:00
|
|
|
struct ssl_connect_data *ssl = conn->ssl;
|
2016-11-16 12:49:15 -05:00
|
|
|
struct Curl_easy *data = conn->data;
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2012-04-06 10:05:25 -04:00
|
|
|
(void)sockindex; /* unused */
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_create_object(ssl, CKO_PRIVATE_KEY, key_file, FALSE);
|
|
|
|
if(result) {
|
2007-09-18 18:21:54 -04:00
|
|
|
PR_SetError(SEC_ERROR_BAD_KEY, 0);
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 02:28:39 -04:00
|
|
|
slot = nss_find_slot_by_name("PEM Token #1");
|
2011-01-27 04:55:02 -05:00
|
|
|
if(!slot)
|
2011-08-26 09:43:48 -04:00
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
2011-01-27 04:55:02 -05:00
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
/* This will force the token to be seen as re-inserted */
|
2017-04-10 11:05:05 -04:00
|
|
|
tmp = SECMOD_WaitForAnyTokenEvent(pem_module, 0, 0);
|
2017-04-24 09:01:04 -04:00
|
|
|
if(tmp)
|
|
|
|
PK11_FreeSlot(tmp);
|
2007-09-18 18:21:54 -04:00
|
|
|
PK11_IsPresent(slot);
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
status = PK11_Authenticate(slot, PR_TRUE, SSL_SET_OPTION(key_passwd));
|
2009-04-24 17:55:18 -04:00
|
|
|
PK11_FreeSlot(slot);
|
2014-12-27 16:35:36 -05:00
|
|
|
|
|
|
|
return (SECSuccess == status) ? CURLE_OK : CURLE_SSL_CERTPROBLEM;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int display_error(struct connectdata *conn, PRInt32 err,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
switch(err) {
|
|
|
|
case SEC_ERROR_BAD_PASSWORD:
|
2008-01-15 18:19:02 -05:00
|
|
|
failf(conn->data, "Unable to load client key: Incorrect password");
|
2007-09-18 18:21:54 -04:00
|
|
|
return 1;
|
|
|
|
case SEC_ERROR_UNKNOWN_CERT:
|
2008-01-15 18:19:02 -05:00
|
|
|
failf(conn->data, "Unable to load certificate %s", filename);
|
2007-09-18 18:21:54 -04:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0; /* The caller will print a generic error */
|
|
|
|
}
|
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
static CURLcode cert_stuff(struct connectdata *conn, int sockindex,
|
|
|
|
char *cert_file, char *key_file)
|
2007-09-18 18:21:54 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
|
|
|
if(cert_file) {
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_load_cert(&conn->ssl[sockindex], cert_file, PR_FALSE);
|
|
|
|
if(result) {
|
2012-04-11 07:44:20 -04:00
|
|
|
const PRErrorCode err = PR_GetError();
|
|
|
|
if(!display_error(conn, err, cert_file)) {
|
|
|
|
const char *err_name = nss_error_to_name(err);
|
|
|
|
failf(data, "unable to load client cert: %d (%s)", err, err_name);
|
|
|
|
}
|
2011-08-26 09:43:48 -04:00
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 09:43:48 -04:00
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
if(key_file || (is_file(cert_file))) {
|
|
|
|
if(key_file)
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_load_key(conn, sockindex, key_file);
|
2007-09-18 18:21:54 -04:00
|
|
|
else
|
|
|
|
/* In case the cert file also has the key */
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_load_key(conn, sockindex, cert_file);
|
|
|
|
if(result) {
|
2012-04-11 07:44:20 -04:00
|
|
|
const PRErrorCode err = PR_GetError();
|
|
|
|
if(!display_error(conn, err, key_file)) {
|
|
|
|
const char *err_name = nss_error_to_name(err);
|
|
|
|
failf(data, "unable to load client key: %d (%s)", err, err_name);
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 09:43:48 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:53:24 -05:00
|
|
|
static char *nss_get_password(PK11SlotInfo *slot, PRBool retry, void *arg)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
|
|
|
(void)slot; /* unused */
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2009-05-11 05:13:49 -04:00
|
|
|
if(retry || NULL == arg)
|
2007-09-18 18:21:54 -04:00
|
|
|
return NULL;
|
2007-02-12 17:32:37 -05:00
|
|
|
else
|
2009-05-11 05:13:49 -04:00
|
|
|
return (char *)PORT_Strdup((char *)arg);
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2011-03-15 09:52:26 -04:00
|
|
|
/* bypass the default SSL_AuthCertificate() hook in case we do not want to
|
|
|
|
* verify peer */
|
|
|
|
static SECStatus nss_auth_cert_hook(void *arg, PRFileDesc *fd, PRBool checksig,
|
|
|
|
PRBool isServer)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = (struct connectdata *)arg;
|
2014-06-24 17:25:59 -04:00
|
|
|
|
|
|
|
#ifdef SSL_ENABLE_OCSP_STAPLING
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifystatus)) {
|
2014-06-24 17:25:59 -04:00
|
|
|
SECStatus cacheResult;
|
|
|
|
|
|
|
|
const SECItemArray *csa = SSL_PeerStapledOCSPResponses(fd);
|
|
|
|
if(!csa) {
|
|
|
|
failf(conn->data, "Invalid OCSP response");
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(csa->len == 0) {
|
|
|
|
failf(conn->data, "No OCSP response received");
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheResult = CERT_CacheOCSPResponseFromSideChannel(
|
|
|
|
CERT_GetDefaultCertDB(), SSL_PeerCertificate(fd),
|
|
|
|
PR_Now(), &csa->items[0], arg
|
|
|
|
);
|
|
|
|
|
|
|
|
if(cacheResult != SECSuccess) {
|
|
|
|
failf(conn->data, "Invalid OCSP response");
|
|
|
|
return cacheResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!SSL_CONN_CONFIG(verifypeer)) {
|
2011-03-15 09:52:26 -04:00
|
|
|
infof(conn->data, "skipping SSL peer certificate verification\n");
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
2011-04-04 12:24:32 -04:00
|
|
|
return SSL_AuthCertificate(CERT_GetDefaultCertDB(), fd, checksig, isServer);
|
2011-03-15 09:52:26 -04:00
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/**
|
|
|
|
* Inform the application that the handshake is complete.
|
|
|
|
*/
|
2012-05-21 10:31:21 -04:00
|
|
|
static void HandshakeCallback(PRFileDesc *sock, void *arg)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2014-03-03 02:39:25 -05:00
|
|
|
struct connectdata *conn = (struct connectdata*) arg;
|
2014-02-07 04:48:34 -05:00
|
|
|
unsigned int buflenmax = 50;
|
|
|
|
unsigned char buf[50];
|
|
|
|
unsigned int buflen;
|
|
|
|
SSLNextProtoState state;
|
|
|
|
|
2016-05-09 10:50:11 -04:00
|
|
|
if(!conn->bits.tls_enable_npn && !conn->bits.tls_enable_alpn) {
|
2014-02-10 02:38:55 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
if(SSL_GetNextProto(sock, &state, buf, &buflen, buflenmax) == SECSuccess) {
|
|
|
|
|
|
|
|
switch(state) {
|
2016-11-07 09:27:36 -05:00
|
|
|
#if NSSVERNUM >= 0x031a00 /* 3.26.0 */
|
|
|
|
/* used by NSS internally to implement 0-RTT */
|
|
|
|
case SSL_NEXT_PROTO_EARLY_VALUE:
|
|
|
|
/* fall through! */
|
|
|
|
#endif
|
2014-12-08 18:09:24 -05:00
|
|
|
case SSL_NEXT_PROTO_NO_SUPPORT:
|
|
|
|
case SSL_NEXT_PROTO_NO_OVERLAP:
|
2015-02-16 10:41:57 -05:00
|
|
|
infof(conn->data, "ALPN/NPN, server did not agree to a protocol\n");
|
2014-12-08 18:09:24 -05:00
|
|
|
return;
|
2014-02-09 20:58:54 -05:00
|
|
|
#ifdef SSL_ENABLE_ALPN
|
2014-12-08 18:09:24 -05:00
|
|
|
case SSL_NEXT_PROTO_SELECTED:
|
|
|
|
infof(conn->data, "ALPN, server accepted to use %.*s\n", buflen, buf);
|
|
|
|
break;
|
2014-02-07 09:38:45 -05:00
|
|
|
#endif
|
2014-12-08 18:09:24 -05:00
|
|
|
case SSL_NEXT_PROTO_NEGOTIATED:
|
|
|
|
infof(conn->data, "NPN, server accepted to use %.*s\n", buflen, buf);
|
|
|
|
break;
|
2014-02-07 04:48:34 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 10:41:57 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
2014-02-07 04:48:34 -05:00
|
|
|
if(buflen == NGHTTP2_PROTO_VERSION_ID_LEN &&
|
2015-02-19 13:58:28 -05:00
|
|
|
!memcmp(NGHTTP2_PROTO_VERSION_ID, buf, NGHTTP2_PROTO_VERSION_ID_LEN)) {
|
2015-12-13 03:23:36 -05:00
|
|
|
conn->negnpn = CURL_HTTP_VERSION_2;
|
2014-02-07 04:48:34 -05:00
|
|
|
}
|
2015-02-16 10:41:57 -05:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if(buflen == ALPN_HTTP_1_1_LENGTH &&
|
|
|
|
!memcmp(ALPN_HTTP_1_1, buf, ALPN_HTTP_1_1_LENGTH)) {
|
2015-03-07 05:10:30 -05:00
|
|
|
conn->negnpn = CURL_HTTP_VERSION_1_1;
|
2014-02-07 04:48:34 -05:00
|
|
|
}
|
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2015-04-23 09:56:03 -04:00
|
|
|
#if NSSVERNUM >= 0x030f04 /* 3.15.4 */
|
2015-02-14 10:59:01 -05:00
|
|
|
static SECStatus CanFalseStartCallback(PRFileDesc *sock, void *client_data,
|
|
|
|
PRBool *canFalseStart)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = client_data;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-02-14 10:59:01 -05:00
|
|
|
|
|
|
|
SSLChannelInfo channelInfo;
|
|
|
|
SSLCipherSuiteInfo cipherInfo;
|
|
|
|
|
|
|
|
SECStatus rv;
|
|
|
|
PRBool negotiatedExtension;
|
|
|
|
|
|
|
|
*canFalseStart = PR_FALSE;
|
|
|
|
|
|
|
|
if(SSL_GetChannelInfo(sock, &channelInfo, sizeof(channelInfo)) != SECSuccess)
|
|
|
|
return SECFailure;
|
|
|
|
|
|
|
|
if(SSL_GetCipherSuiteInfo(channelInfo.cipherSuite, &cipherInfo,
|
|
|
|
sizeof(cipherInfo)) != SECSuccess)
|
|
|
|
return SECFailure;
|
|
|
|
|
|
|
|
/* Prevent version downgrade attacks from TLS 1.2, and avoid False Start for
|
|
|
|
* TLS 1.3 and later. See https://bugzilla.mozilla.org/show_bug.cgi?id=861310
|
|
|
|
*/
|
|
|
|
if(channelInfo.protocolVersion != SSL_LIBRARY_VERSION_TLS_1_2)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Only allow ECDHE key exchange algorithm.
|
|
|
|
* See https://bugzilla.mozilla.org/show_bug.cgi?id=952863 */
|
|
|
|
if(cipherInfo.keaType != ssl_kea_ecdh)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Prevent downgrade attacks on the symmetric cipher. We do not allow CBC
|
|
|
|
* mode due to BEAST, POODLE, and other attacks on the MAC-then-Encrypt
|
|
|
|
* design. See https://bugzilla.mozilla.org/show_bug.cgi?id=1109766 */
|
|
|
|
if(cipherInfo.symCipher != ssl_calg_aes_gcm)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Enforce ALPN or NPN to do False Start, as an indicator of server
|
|
|
|
* compatibility. */
|
|
|
|
rv = SSL_HandshakeNegotiatedExtension(sock, ssl_app_layer_protocol_xtn,
|
|
|
|
&negotiatedExtension);
|
|
|
|
if(rv != SECSuccess || !negotiatedExtension) {
|
|
|
|
rv = SSL_HandshakeNegotiatedExtension(sock, ssl_next_proto_nego_xtn,
|
|
|
|
&negotiatedExtension);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rv != SECSuccess || !negotiatedExtension)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
*canFalseStart = PR_TRUE;
|
|
|
|
|
|
|
|
infof(data, "Trying TLS False Start\n");
|
|
|
|
|
|
|
|
end:
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
2015-04-23 09:56:03 -04:00
|
|
|
#endif
|
2015-02-14 10:59:01 -05:00
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
static void display_cert_info(struct Curl_easy *data,
|
2010-02-17 07:13:55 -05:00
|
|
|
CERTCertificate *cert)
|
|
|
|
{
|
|
|
|
char *subject, *issuer, *common_name;
|
|
|
|
PRExplodedTime printableTime;
|
|
|
|
char timeString[256];
|
|
|
|
PRTime notBefore, notAfter;
|
|
|
|
|
|
|
|
subject = CERT_NameToAscii(&cert->subject);
|
|
|
|
issuer = CERT_NameToAscii(&cert->issuer);
|
|
|
|
common_name = CERT_GetCommonName(&cert->subject);
|
|
|
|
infof(data, "\tsubject: %s\n", subject);
|
|
|
|
|
|
|
|
CERT_GetCertTimes(cert, ¬Before, ¬After);
|
|
|
|
PR_ExplodeTime(notBefore, PR_GMTParameters, &printableTime);
|
|
|
|
PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
|
|
|
|
infof(data, "\tstart date: %s\n", timeString);
|
|
|
|
PR_ExplodeTime(notAfter, PR_GMTParameters, &printableTime);
|
|
|
|
PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
|
|
|
|
infof(data, "\texpire date: %s\n", timeString);
|
|
|
|
infof(data, "\tcommon name: %s\n", common_name);
|
|
|
|
infof(data, "\tissuer: %s\n", issuer);
|
|
|
|
|
|
|
|
PR_Free(subject);
|
|
|
|
PR_Free(issuer);
|
|
|
|
PR_Free(common_name);
|
2009-07-20 17:50:21 -04:00
|
|
|
}
|
|
|
|
|
2014-12-27 15:50:14 -05:00
|
|
|
static CURLcode display_conn_info(struct connectdata *conn, PRFileDesc *sock)
|
2007-09-18 18:21:54 -04:00
|
|
|
{
|
2014-12-27 15:50:14 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2007-09-18 18:21:54 -04:00
|
|
|
SSLChannelInfo channel;
|
|
|
|
SSLCipherSuiteInfo suite;
|
|
|
|
CERTCertificate *cert;
|
2013-10-30 06:12:06 -04:00
|
|
|
CERTCertificate *cert2;
|
|
|
|
CERTCertificate *cert3;
|
|
|
|
PRTime now;
|
|
|
|
int i;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2018-05-11 17:40:58 -04:00
|
|
|
if(SSL_GetChannelInfo(sock, &channel, sizeof(channel)) ==
|
|
|
|
SECSuccess && channel.length == sizeof(channel) &&
|
2008-06-06 16:57:32 -04:00
|
|
|
channel.cipherSuite) {
|
2007-11-05 04:45:09 -05:00
|
|
|
if(SSL_GetCipherSuiteInfo(channel.cipherSuite,
|
2018-05-11 17:40:58 -04:00
|
|
|
&suite, sizeof(suite)) == SECSuccess) {
|
2007-09-18 18:21:54 -04:00
|
|
|
infof(conn->data, "SSL connection using %s\n", suite.cipherSuiteName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-25 16:54:46 -04:00
|
|
|
cert = SSL_PeerCertificate(sock);
|
2013-10-30 06:12:06 -04:00
|
|
|
if(cert) {
|
|
|
|
infof(conn->data, "Server certificate:\n");
|
|
|
|
|
|
|
|
if(!conn->data->set.ssl.certinfo) {
|
|
|
|
display_cert_info(conn->data, cert);
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Count certificates in chain. */
|
|
|
|
now = PR_Now();
|
|
|
|
i = 1;
|
|
|
|
if(!cert->isRoot) {
|
|
|
|
cert2 = CERT_FindCertIssuer(cert, now, certUsageSSLCA);
|
|
|
|
while(cert2) {
|
|
|
|
i++;
|
|
|
|
if(cert2->isRoot) {
|
|
|
|
CERT_DestroyCertificate(cert2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cert3 = CERT_FindCertIssuer(cert2, now, certUsageSSLCA);
|
|
|
|
CERT_DestroyCertificate(cert2);
|
|
|
|
cert2 = cert3;
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 15:50:14 -05:00
|
|
|
|
|
|
|
result = Curl_ssl_init_certinfo(conn->data, i);
|
|
|
|
if(!result) {
|
|
|
|
for(i = 0; cert; cert = cert2) {
|
2014-12-27 15:57:33 -05:00
|
|
|
result = Curl_extract_certinfo(conn, i++, (char *)cert->derCert.data,
|
|
|
|
(char *)cert->derCert.data +
|
|
|
|
cert->derCert.len);
|
|
|
|
if(result)
|
|
|
|
break;
|
|
|
|
|
2014-12-27 15:50:14 -05:00
|
|
|
if(cert->isRoot) {
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
break;
|
|
|
|
}
|
2014-12-27 15:57:33 -05:00
|
|
|
|
2014-12-27 15:50:14 -05:00
|
|
|
cert2 = CERT_FindCertIssuer(cert, now, certUsageSSLCA);
|
2013-10-30 06:12:06 -04:00
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2014-12-27 15:50:14 -05:00
|
|
|
return result;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2012-05-21 10:19:12 -04:00
|
|
|
static SECStatus BadCertHandler(void *arg, PRFileDesc *sock)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = (struct connectdata *)arg;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-05-21 10:19:12 -04:00
|
|
|
PRErrorCode err = PR_GetError();
|
|
|
|
CERTCertificate *cert;
|
|
|
|
|
|
|
|
/* remember the cert verification result */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_IS_PROXY())
|
|
|
|
data->set.proxy_ssl.certverifyresult = err;
|
|
|
|
else
|
|
|
|
data->set.ssl.certverifyresult = err;
|
2012-05-21 10:19:12 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(err == SSL_ERROR_BAD_CERT_DOMAIN && !SSL_CONN_CONFIG(verifyhost))
|
2012-05-21 10:19:12 -04:00
|
|
|
/* we are asked not to verify the host name */
|
|
|
|
return SECSuccess;
|
|
|
|
|
|
|
|
/* print only info about the cert, the error is printed off the callback */
|
|
|
|
cert = SSL_PeerCertificate(sock);
|
|
|
|
if(cert) {
|
|
|
|
infof(data, "Server certificate:\n");
|
|
|
|
display_cert_info(data, cert);
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2008-06-06 16:52:32 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Check that the Peer certificate's issuer certificate matches the one found
|
|
|
|
* by issuer_nickname. This is not exactly the way OpenSSL and GNU TLS do the
|
|
|
|
* issuer check, so we provide comments that mimic the OpenSSL
|
|
|
|
* X509_check_issued function (in x509v3/v3_purp.c)
|
|
|
|
*/
|
2008-06-18 17:50:40 -04:00
|
|
|
static SECStatus check_issuer_cert(PRFileDesc *sock,
|
2008-11-15 18:43:10 -05:00
|
|
|
char *issuer_nickname)
|
2008-06-06 16:52:32 -04:00
|
|
|
{
|
2015-03-17 08:41:49 -04:00
|
|
|
CERTCertificate *cert, *cert_issuer, *issuer;
|
2017-09-09 17:09:06 -04:00
|
|
|
SECStatus res = SECSuccess;
|
2008-06-06 16:52:32 -04:00
|
|
|
void *proto_win = NULL;
|
|
|
|
|
|
|
|
cert = SSL_PeerCertificate(sock);
|
2015-03-17 08:41:49 -04:00
|
|
|
cert_issuer = CERT_FindCertIssuer(cert, PR_Now(), certUsageObjectSigner);
|
2008-06-06 16:52:32 -04:00
|
|
|
|
|
|
|
proto_win = SSL_RevealPinArg(sock);
|
|
|
|
issuer = PK11_FindCertFromNickname(issuer_nickname, proto_win);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if((!cert_issuer) || (!issuer))
|
2008-06-06 16:52:32 -04:00
|
|
|
res = SECFailure;
|
2011-04-20 09:17:42 -04:00
|
|
|
else if(SECITEM_CompareItem(&cert_issuer->derCert,
|
2017-09-09 17:09:06 -04:00
|
|
|
&issuer->derCert) != SECEqual)
|
2008-06-06 16:52:32 -04:00
|
|
|
res = SECFailure;
|
|
|
|
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
CERT_DestroyCertificate(issuer);
|
|
|
|
CERT_DestroyCertificate(cert_issuer);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-03-25 08:48:41 -04:00
|
|
|
static CURLcode cmp_peer_pubkey(struct ssl_connect_data *connssl,
|
|
|
|
const char *pinnedpubkey)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
2017-06-21 06:41:18 -04:00
|
|
|
struct Curl_easy *data = BACKEND->data;
|
2015-03-25 08:48:41 -04:00
|
|
|
CERTCertificate *cert;
|
|
|
|
|
|
|
|
if(!pinnedpubkey)
|
|
|
|
/* no pinned public key specified */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
/* get peer certificate */
|
2017-06-21 06:41:18 -04:00
|
|
|
cert = SSL_PeerCertificate(BACKEND->handle);
|
2015-03-25 08:48:41 -04:00
|
|
|
if(cert) {
|
|
|
|
/* extract public key from peer certificate */
|
|
|
|
SECKEYPublicKey *pubkey = CERT_ExtractPublicKey(cert);
|
|
|
|
if(pubkey) {
|
|
|
|
/* encode the public key as DER */
|
|
|
|
SECItem *cert_der = PK11_DEREncodePublicKey(pubkey);
|
|
|
|
if(cert_der) {
|
|
|
|
/* compare the public key with the pinned public key */
|
2015-09-12 17:35:12 -04:00
|
|
|
result = Curl_pin_peer_pubkey(data, pinnedpubkey, cert_der->data,
|
2015-03-25 08:48:41 -04:00
|
|
|
cert_der->len);
|
|
|
|
SECITEM_FreeItem(cert_der, PR_TRUE);
|
|
|
|
}
|
|
|
|
SECKEY_DestroyPublicKey(pubkey);
|
|
|
|
}
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report the resulting status */
|
|
|
|
switch(result) {
|
|
|
|
case CURLE_OK:
|
|
|
|
infof(data, "pinned public key verified successfully!\n");
|
|
|
|
break;
|
|
|
|
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
|
|
|
|
failf(data, "failed to verify pinned public key");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* OOM, etc. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Callback to pick the SSL client certificate.
|
|
|
|
*/
|
2007-10-25 16:54:46 -04:00
|
|
|
static SECStatus SelectClientCert(void *arg, PRFileDesc *sock,
|
|
|
|
struct CERTDistNamesStr *caNames,
|
|
|
|
struct CERTCertificateStr **pRetCert,
|
|
|
|
struct SECKEYPrivateKeyStr **pRetKey)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2009-07-20 17:50:21 -04:00
|
|
|
struct ssl_connect_data *connssl = (struct ssl_connect_data *)arg;
|
2017-06-21 06:41:18 -04:00
|
|
|
struct Curl_easy *data = BACKEND->data;
|
|
|
|
const char *nickname = BACKEND->client_nickname;
|
2016-08-22 04:24:35 -04:00
|
|
|
static const char pem_slotname[] = "PEM Token #1";
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(BACKEND->obj_clicert) {
|
2009-07-20 17:50:21 -04:00
|
|
|
/* use the cert/key provided by PEM reader */
|
2011-08-26 08:38:18 -04:00
|
|
|
SECItem cert_der = { 0, NULL, 0 };
|
2009-07-20 17:50:21 -04:00
|
|
|
void *proto_win = SSL_RevealPinArg(sock);
|
2012-12-03 07:17:50 -05:00
|
|
|
struct CERTCertificateStr *cert;
|
|
|
|
struct SECKEYPrivateKeyStr *key;
|
2009-07-20 17:50:21 -04:00
|
|
|
|
2016-08-26 02:28:39 -04:00
|
|
|
PK11SlotInfo *slot = nss_find_slot_by_name(pem_slotname);
|
2011-08-26 08:38:18 -04:00
|
|
|
if(NULL == slot) {
|
|
|
|
failf(data, "NSS: PK11 slot not found: %s", pem_slotname);
|
2009-07-20 17:50:21 -04:00
|
|
|
return SECFailure;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2009-07-20 17:50:21 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(PK11_ReadRawAttribute(PK11_TypeGeneric, BACKEND->obj_clicert, CKA_VALUE,
|
2011-08-26 08:38:18 -04:00
|
|
|
&cert_der) != SECSuccess) {
|
|
|
|
failf(data, "NSS: CKA_VALUE not found in PK11 generic object");
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2012-12-03 07:17:50 -05:00
|
|
|
cert = PK11_FindCertFromDERCertItem(slot, &cert_der, proto_win);
|
2011-08-26 08:38:18 -04:00
|
|
|
SECITEM_FreeItem(&cert_der, PR_FALSE);
|
2012-12-03 07:17:50 -05:00
|
|
|
if(NULL == cert) {
|
2011-08-26 08:38:18 -04:00
|
|
|
failf(data, "NSS: client certificate from file not found");
|
|
|
|
PK11_FreeSlot(slot);
|
2009-07-20 17:50:21 -04:00
|
|
|
return SECFailure;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2009-07-20 17:50:21 -04:00
|
|
|
|
2012-12-03 07:17:50 -05:00
|
|
|
key = PK11_FindPrivateKeyFromCert(slot, cert, NULL);
|
2009-07-20 17:50:21 -04:00
|
|
|
PK11_FreeSlot(slot);
|
2012-12-03 07:17:50 -05:00
|
|
|
if(NULL == key) {
|
2011-08-26 08:38:18 -04:00
|
|
|
failf(data, "NSS: private key from file not found");
|
2012-12-03 07:17:50 -05:00
|
|
|
CERT_DestroyCertificate(cert);
|
2009-07-20 17:50:21 -04:00
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2011-08-26 08:38:18 -04:00
|
|
|
infof(data, "NSS: client certificate from file\n");
|
2012-12-03 07:17:50 -05:00
|
|
|
display_cert_info(data, cert);
|
|
|
|
|
|
|
|
*pRetCert = cert;
|
|
|
|
*pRetKey = key;
|
2009-07-20 17:50:21 -04:00
|
|
|
return SECSuccess;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2009-07-20 17:50:21 -04:00
|
|
|
/* use the default NSS hook */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(SECSuccess != NSS_GetClientAuthData((void *)nickname, sock, caNames,
|
2009-07-20 17:50:21 -04:00
|
|
|
pRetCert, pRetKey)
|
|
|
|
|| NULL == *pRetCert) {
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(NULL == nickname)
|
2010-02-17 07:13:55 -05:00
|
|
|
failf(data, "NSS: client certificate not found (nickname not "
|
|
|
|
"specified)");
|
2009-07-20 17:50:21 -04:00
|
|
|
else
|
|
|
|
failf(data, "NSS: client certificate not found: %s", nickname);
|
|
|
|
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get certificate nickname if any */
|
|
|
|
nickname = (*pRetCert)->nickname;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(NULL == nickname)
|
2009-07-20 17:50:21 -04:00
|
|
|
nickname = "[unknown]";
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2016-08-22 04:24:35 -04:00
|
|
|
if(!strncmp(nickname, pem_slotname, sizeof(pem_slotname) - 1U)) {
|
|
|
|
failf(data, "NSS: refusing previously loaded certificate from file: %s",
|
|
|
|
nickname);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(NULL == *pRetKey) {
|
2009-07-20 17:50:21 -04:00
|
|
|
failf(data, "NSS: private key not found for certificate: %s", nickname);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "NSS: using client certificate: %s\n", nickname);
|
|
|
|
display_cert_info(data, *pRetCert);
|
|
|
|
return SECSuccess;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2014-04-23 09:37:26 -04:00
|
|
|
/* update blocking direction in case of PR_WOULD_BLOCK_ERROR */
|
|
|
|
static void nss_update_connecting_state(ssl_connect_state state, void *secret)
|
|
|
|
{
|
|
|
|
struct ssl_connect_data *connssl = (struct ssl_connect_data *)secret;
|
|
|
|
if(PR_GetError() != PR_WOULD_BLOCK_ERROR)
|
|
|
|
/* an unrelated error is passing by */
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(connssl->connecting_state) {
|
|
|
|
case ssl_connect_2:
|
|
|
|
case ssl_connect_2_reading:
|
|
|
|
case ssl_connect_2_writing:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* we are not called from an SSL handshake */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update the state accordingly */
|
|
|
|
connssl->connecting_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recv() wrapper we use to detect blocking direction during SSL handshake */
|
|
|
|
static PRInt32 nspr_io_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
|
|
|
|
PRIntn flags, PRIntervalTime timeout)
|
|
|
|
{
|
|
|
|
const PRRecvFN recv_fn = fd->lower->methods->recv;
|
|
|
|
const PRInt32 rv = recv_fn(fd->lower, buf, amount, flags, timeout);
|
|
|
|
if(rv < 0)
|
|
|
|
/* check for PR_WOULD_BLOCK_ERROR and update blocking direction */
|
|
|
|
nss_update_connecting_state(ssl_connect_2_reading, fd->secret);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send() wrapper we use to detect blocking direction during SSL handshake */
|
|
|
|
static PRInt32 nspr_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
|
|
|
|
PRIntn flags, PRIntervalTime timeout)
|
|
|
|
{
|
|
|
|
const PRSendFN send_fn = fd->lower->methods->send;
|
|
|
|
const PRInt32 rv = send_fn(fd->lower, buf, amount, flags, timeout);
|
|
|
|
if(rv < 0)
|
|
|
|
/* check for PR_WOULD_BLOCK_ERROR and update blocking direction */
|
|
|
|
nss_update_connecting_state(ssl_connect_2_writing, fd->secret);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close() wrapper to avoid assertion failure due to fd->secret != NULL */
|
|
|
|
static PRStatus nspr_io_close(PRFileDesc *fd)
|
|
|
|
{
|
|
|
|
const PRCloseFN close_fn = PR_GetDefaultIOMethods()->close;
|
|
|
|
fd->secret = NULL;
|
|
|
|
return close_fn(fd);
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:05:05 -04:00
|
|
|
/* load a PKCS #11 module */
|
|
|
|
static CURLcode nss_load_module(SECMODModule **pmod, const char *library,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
char *config_string;
|
|
|
|
SECMODModule *module = *pmod;
|
|
|
|
if(module)
|
|
|
|
/* already loaded */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
config_string = aprintf("library=%s name=%s", library, name);
|
|
|
|
if(!config_string)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
module = SECMOD_LoadUserModule(config_string, NULL, PR_FALSE);
|
|
|
|
free(config_string);
|
|
|
|
|
|
|
|
if(module && module->loaded) {
|
|
|
|
/* loaded successfully */
|
|
|
|
*pmod = module;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(module)
|
|
|
|
SECMOD_DestroyModule(module);
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unload a PKCS #11 module */
|
|
|
|
static void nss_unload_module(SECMODModule **pmod)
|
|
|
|
{
|
|
|
|
SECMODModule *module = *pmod;
|
|
|
|
if(!module)
|
|
|
|
/* not loaded */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(SECMOD_UnloadUserModule(module) != SECSuccess)
|
|
|
|
/* unload failed */
|
|
|
|
return;
|
|
|
|
|
|
|
|
SECMOD_DestroyModule(module);
|
|
|
|
*pmod = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-24 09:18:45 -05:00
|
|
|
/* data might be NULL */
|
2016-06-21 09:47:12 -04:00
|
|
|
static CURLcode nss_init_core(struct Curl_easy *data, const char *cert_dir)
|
2011-08-15 07:48:45 -04:00
|
|
|
{
|
2012-04-16 16:37:12 -04:00
|
|
|
NSSInitParameters initparams;
|
|
|
|
|
2012-04-10 09:42:34 -04:00
|
|
|
if(nss_context != NULL)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
memset((void *) &initparams, '\0', sizeof(initparams));
|
|
|
|
initparams.length = sizeof(initparams);
|
2011-08-15 07:48:45 -04:00
|
|
|
|
|
|
|
if(cert_dir) {
|
2014-07-28 10:27:04 -04:00
|
|
|
char *certpath = aprintf("sql:%s", cert_dir);
|
2011-08-15 07:48:45 -04:00
|
|
|
if(!certpath)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
infof(data, "Initializing NSS with certpath: %s\n", certpath);
|
2012-04-10 09:42:34 -04:00
|
|
|
nss_context = NSS_InitContext(certpath, "", "", "", &initparams,
|
|
|
|
NSS_INIT_READONLY | NSS_INIT_PK11RELOAD);
|
|
|
|
free(certpath);
|
|
|
|
|
|
|
|
if(nss_context != NULL)
|
|
|
|
return CURLE_OK;
|
2011-08-15 07:48:45 -04:00
|
|
|
|
|
|
|
infof(data, "Unable to initialize NSS database\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "Initializing NSS with certpath: none\n");
|
2012-04-10 09:42:34 -04:00
|
|
|
nss_context = NSS_InitContext("", "", "", "", &initparams, NSS_INIT_READONLY
|
2012-04-13 12:26:42 -04:00
|
|
|
| NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN
|
|
|
|
| NSS_INIT_NOROOTINIT | NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD);
|
2012-04-10 09:42:34 -04:00
|
|
|
if(nss_context != NULL)
|
|
|
|
return CURLE_OK;
|
2011-08-15 07:48:45 -04:00
|
|
|
|
|
|
|
infof(data, "Unable to initialize NSS\n");
|
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
|
2015-02-24 09:18:45 -05:00
|
|
|
/* data might be NULL */
|
2016-06-21 09:47:12 -04:00
|
|
|
static CURLcode nss_init(struct Curl_easy *data)
|
2010-06-26 21:38:01 -04:00
|
|
|
{
|
|
|
|
char *cert_dir;
|
|
|
|
struct_stat st;
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2011-08-15 07:48:45 -04:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
if(initialized)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2014-07-04 06:41:53 -04:00
|
|
|
/* list of all CRL items we need to destroy in Curl_nss_cleanup() */
|
2017-04-05 17:25:28 -04:00
|
|
|
Curl_llist_init(&nss_crl_list, nss_destroy_crl_item);
|
2014-07-04 06:41:53 -04:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
/* First we check if $SSL_DIR points to a valid dir */
|
|
|
|
cert_dir = getenv("SSL_DIR");
|
|
|
|
if(cert_dir) {
|
|
|
|
if((stat(cert_dir, &st) != 0) ||
|
|
|
|
(!S_ISDIR(st.st_mode))) {
|
|
|
|
cert_dir = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we check if the default location is a valid dir */
|
|
|
|
if(!cert_dir) {
|
|
|
|
if((stat(SSL_DIR, &st) == 0) &&
|
|
|
|
(S_ISDIR(st.st_mode))) {
|
|
|
|
cert_dir = (char *)SSL_DIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 09:37:26 -04:00
|
|
|
if(nspr_io_identity == PR_INVALID_IO_LAYER) {
|
|
|
|
/* allocate an identity for our own NSPR I/O layer */
|
|
|
|
nspr_io_identity = PR_GetUniqueIdentity("libcurl");
|
|
|
|
if(nspr_io_identity == PR_INVALID_IO_LAYER)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* the default methods just call down to the lower I/O layer */
|
2018-05-11 17:40:58 -04:00
|
|
|
memcpy(&nspr_io_methods, PR_GetDefaultIOMethods(),
|
|
|
|
sizeof(nspr_io_methods));
|
2014-04-23 09:37:26 -04:00
|
|
|
|
|
|
|
/* override certain methods in the table by our wrappers */
|
|
|
|
nspr_io_methods.recv = nspr_io_recv;
|
|
|
|
nspr_io_methods.send = nspr_io_send;
|
|
|
|
nspr_io_methods.close = nspr_io_close;
|
|
|
|
}
|
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_init_core(data, cert_dir);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2010-06-26 21:38:01 -04:00
|
|
|
|
2016-02-04 04:41:15 -05:00
|
|
|
if(!any_cipher_enabled())
|
2010-06-26 21:38:01 -04:00
|
|
|
NSS_SetDomesticPolicy();
|
|
|
|
|
2011-08-15 07:48:45 -04:00
|
|
|
initialized = 1;
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/**
|
|
|
|
* Global SSL init
|
|
|
|
*
|
|
|
|
* @retval 0 error initializing SSL
|
|
|
|
* @retval 1 SSL initialized successfully
|
|
|
|
*/
|
2017-06-23 07:19:00 -04:00
|
|
|
static int Curl_nss_init(void)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2008-09-23 06:27:04 -04:00
|
|
|
/* curl_global_init() is not thread-safe so this test is ok */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(nss_initlock == NULL) {
|
2007-02-12 17:32:37 -05:00
|
|
|
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
|
2008-09-23 06:27:04 -04:00
|
|
|
nss_initlock = PR_NewLock();
|
2010-05-11 08:10:27 -04:00
|
|
|
nss_crllock = PR_NewLock();
|
2016-08-26 02:28:39 -04:00
|
|
|
nss_findslot_lock = PR_NewLock();
|
2017-04-10 11:40:30 -04:00
|
|
|
nss_trustload_lock = PR_NewLock();
|
2008-09-23 06:27:04 -04:00
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
|
|
|
|
/* We will actually initialize NSS later */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-24 09:18:45 -05:00
|
|
|
/* data might be NULL */
|
2016-06-21 09:47:12 -04:00
|
|
|
CURLcode Curl_nss_force_init(struct Curl_easy *data)
|
2010-06-26 21:38:01 -04:00
|
|
|
{
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2010-06-26 21:38:01 -04:00
|
|
|
if(!nss_initlock) {
|
2015-02-24 09:18:45 -05:00
|
|
|
if(data)
|
|
|
|
failf(data, "unable to initialize NSS, curl_global_init() should have "
|
|
|
|
"been called with CURL_GLOBAL_SSL or CURL_GLOBAL_ALL");
|
2011-04-20 09:17:42 -04:00
|
|
|
return CURLE_FAILED_INIT;
|
2010-06-26 21:38:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_Lock(nss_initlock);
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_init(data);
|
2010-06-26 21:38:01 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
2014-12-27 16:35:36 -05:00
|
|
|
|
|
|
|
return result;
|
2010-06-26 21:38:01 -04:00
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/* Global cleanup */
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_nss_cleanup(void)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2008-09-23 06:27:04 -04:00
|
|
|
/* This function isn't required to be threadsafe and this is only done
|
|
|
|
* as a safety feature.
|
|
|
|
*/
|
|
|
|
PR_Lock(nss_initlock);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(initialized) {
|
2009-07-20 17:50:21 -04:00
|
|
|
/* Free references to client certificates held in the SSL session cache.
|
|
|
|
* Omitting this hampers destruction of the security module owning
|
|
|
|
* the certificates. */
|
|
|
|
SSL_ClearSessionCache();
|
|
|
|
|
2017-04-10 11:05:05 -04:00
|
|
|
nss_unload_module(&pem_module);
|
2017-04-10 11:40:30 -04:00
|
|
|
nss_unload_module(&trust_module);
|
2012-04-10 09:42:34 -04:00
|
|
|
NSS_ShutdownContext(nss_context);
|
|
|
|
nss_context = NULL;
|
2009-04-24 17:55:18 -04:00
|
|
|
}
|
2014-07-04 06:41:53 -04:00
|
|
|
|
|
|
|
/* destroy all CRL items */
|
2017-04-05 17:25:28 -04:00
|
|
|
Curl_llist_destroy(&nss_crl_list, NULL);
|
2014-07-04 06:41:53 -04:00
|
|
|
|
2008-09-23 06:27:04 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
|
|
|
|
|
|
|
PR_DestroyLock(nss_initlock);
|
2010-05-11 08:10:27 -04:00
|
|
|
PR_DestroyLock(nss_crllock);
|
2016-08-26 02:28:39 -04:00
|
|
|
PR_DestroyLock(nss_findslot_lock);
|
2017-04-10 11:40:30 -04:00
|
|
|
PR_DestroyLock(nss_trustload_lock);
|
2008-09-23 06:27:04 -04:00
|
|
|
nss_initlock = NULL;
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
initialized = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function uses SSL_peek 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
|
|
|
|
*/
|
2017-06-23 07:19:00 -04:00
|
|
|
static int Curl_nss_check_cxn(struct connectdata *conn)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
|
2007-02-12 17:32:37 -05:00
|
|
|
int rc;
|
|
|
|
char buf;
|
|
|
|
|
|
|
|
rc =
|
2017-06-21 06:41:18 -04:00
|
|
|
PR_Recv(BACKEND->handle, (void *)&buf, 1, PR_MSG_PEEK,
|
2007-02-12 17:32:37 -05:00
|
|
|
PR_SecondsToInterval(1));
|
|
|
|
if(rc > 0)
|
|
|
|
return 1; /* connection still in place */
|
|
|
|
|
|
|
|
if(rc == 0)
|
|
|
|
return 0; /* connection has been closed */
|
|
|
|
|
|
|
|
return -1; /* connection status unknown */
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
static void nss_close(struct ssl_connect_data *connssl)
|
|
|
|
{
|
|
|
|
/* before the cleanup, check whether we are using a client certificate */
|
2017-06-21 06:41:18 -04:00
|
|
|
const bool client_cert = (BACKEND->client_nickname != NULL)
|
|
|
|
|| (BACKEND->obj_clicert != NULL);
|
2016-11-16 12:49:15 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
free(BACKEND->client_nickname);
|
|
|
|
BACKEND->client_nickname = NULL;
|
2016-11-16 12:49:15 -05:00
|
|
|
|
|
|
|
/* destroy all NSS objects in order to avoid failure of NSS shutdown */
|
2017-06-21 06:41:18 -04:00
|
|
|
Curl_llist_destroy(&BACKEND->obj_list, NULL);
|
|
|
|
BACKEND->obj_clicert = NULL;
|
2016-11-16 12:49:15 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(BACKEND->handle) {
|
2016-11-16 12:49:15 -05:00
|
|
|
if(client_cert)
|
|
|
|
/* A server might require different authentication based on the
|
|
|
|
* particular path being requested by the client. To support this
|
|
|
|
* scenario, we must ensure that a connection will never reuse the
|
|
|
|
* authentication data from a previous connection. */
|
2017-06-21 06:41:18 -04:00
|
|
|
SSL_InvalidateSession(BACKEND->handle);
|
2016-11-16 12:49:15 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
PR_Close(BACKEND->handle);
|
|
|
|
BACKEND->handle = NULL;
|
2016-11-16 12:49:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/*
|
|
|
|
* This function is called when an SSL connection is closed.
|
|
|
|
*/
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_nss_close(struct connectdata *conn, int sockindex)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2007-07-29 08:54:05 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2016-11-16 12:49:15 -05:00
|
|
|
struct ssl_connect_data *connssl_proxy = &conn->proxy_ssl[sockindex];
|
2007-02-12 17:32:37 -05:00
|
|
|
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
if(BACKEND->handle || connssl_proxy->backend->handle) {
|
2009-10-17 20:10:13 -04:00
|
|
|
/* NSS closes the socket we previously handed to it, so we must mark it
|
|
|
|
as closed to avoid double close */
|
2009-10-28 16:30:23 -04:00
|
|
|
fake_sclose(conn->sock[sockindex]);
|
2009-10-17 20:10:13 -04:00
|
|
|
conn->sock[sockindex] = CURL_SOCKET_BAD;
|
2016-11-16 12:49:15 -05:00
|
|
|
}
|
2013-01-11 04:24:21 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(BACKEND->handle)
|
|
|
|
/* nss_close(connssl) will transitively close also
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
connssl_proxy->backend->handle if both are used. Clear it to avoid
|
2017-06-21 06:41:18 -04:00
|
|
|
a double close leading to crash. */
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
connssl_proxy->backend->handle = NULL;
|
2012-04-06 10:05:25 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
nss_close(connssl);
|
|
|
|
nss_close(connssl_proxy);
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2012-08-10 02:54:29 -04:00
|
|
|
/* return true if NSS can provide error code (and possibly msg) for the
|
|
|
|
error */
|
2012-08-09 03:40:00 -04:00
|
|
|
static bool is_nss_error(CURLcode err)
|
2010-04-06 07:42:11 -04:00
|
|
|
{
|
|
|
|
switch(err) {
|
2012-08-09 03:40:00 -04:00
|
|
|
case CURLE_PEER_FAILED_VERIFICATION:
|
|
|
|
case CURLE_SSL_CERTPROBLEM:
|
|
|
|
case CURLE_SSL_CONNECT_ERROR:
|
|
|
|
case CURLE_SSL_ISSUER_ERROR:
|
2010-04-06 07:42:11 -04:00
|
|
|
return true;
|
|
|
|
|
2012-08-09 03:40:00 -04:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-04-06 07:42:11 -04:00
|
|
|
|
2012-08-09 03:40:00 -04:00
|
|
|
/* return true if the given error code is related to a client certificate */
|
|
|
|
static bool is_cc_error(PRInt32 err)
|
|
|
|
{
|
|
|
|
switch(err) {
|
|
|
|
case SSL_ERROR_BAD_CERT_ALERT:
|
2010-04-06 07:42:11 -04:00
|
|
|
case SSL_ERROR_EXPIRED_CERT_ALERT:
|
2012-08-09 03:40:00 -04:00
|
|
|
case SSL_ERROR_REVOKED_CERT_ALERT:
|
2010-04-06 07:42:11 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static Curl_recv nss_recv;
|
|
|
|
static Curl_send nss_send;
|
|
|
|
|
2011-01-18 07:53:43 -05:00
|
|
|
static CURLcode nss_load_ca_certificates(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2016-11-16 12:49:15 -05:00
|
|
|
const char *cafile = SSL_CONN_CONFIG(CAfile);
|
|
|
|
const char *capath = SSL_CONN_CONFIG(CApath);
|
2017-04-10 11:40:30 -04:00
|
|
|
bool use_trust_module;
|
|
|
|
CURLcode result = CURLE_OK;
|
2011-01-18 07:53:43 -05:00
|
|
|
|
2017-04-10 11:40:30 -04:00
|
|
|
/* treat empty string as unset */
|
|
|
|
if(cafile && !cafile[0])
|
|
|
|
cafile = NULL;
|
|
|
|
if(capath && !capath[0])
|
|
|
|
capath = NULL;
|
|
|
|
|
|
|
|
infof(data, " CAfile: %s\n CApath: %s\n",
|
|
|
|
cafile ? cafile : "none",
|
|
|
|
capath ? capath : "none");
|
|
|
|
|
|
|
|
/* load libnssckbi.so if no other trust roots were specified */
|
|
|
|
use_trust_module = !cafile && !capath;
|
|
|
|
|
|
|
|
PR_Lock(nss_trustload_lock);
|
|
|
|
if(use_trust_module && !trust_module) {
|
|
|
|
/* libnssckbi.so needed but not yet loaded --> load it! */
|
|
|
|
result = nss_load_module(&trust_module, trust_library, "trust");
|
|
|
|
infof(data, "%s %s\n", (result) ? "failed to load" : "loaded",
|
|
|
|
trust_library);
|
|
|
|
if(result == CURLE_FAILED_INIT)
|
2018-09-19 04:05:56 -04:00
|
|
|
/* If libnssckbi.so is not available (or fails to load), one can still
|
|
|
|
use CA certificates stored in NSS database. Ignore the failure. */
|
|
|
|
result = CURLE_OK;
|
2017-04-10 11:40:30 -04:00
|
|
|
}
|
|
|
|
else if(!use_trust_module && trust_module) {
|
|
|
|
/* libnssckbi.so not needed but already loaded --> unload it! */
|
|
|
|
infof(data, "unloading %s\n", trust_library);
|
|
|
|
nss_unload_module(&trust_module);
|
2011-08-26 09:43:48 -04:00
|
|
|
}
|
2017-04-10 11:40:30 -04:00
|
|
|
PR_Unlock(nss_trustload_lock);
|
|
|
|
|
|
|
|
if(cafile)
|
|
|
|
result = nss_load_cert(&conn->ssl[sockindex], cafile, PR_TRUE);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
2011-01-18 07:53:43 -05:00
|
|
|
|
|
|
|
if(capath) {
|
|
|
|
struct_stat st;
|
|
|
|
if(stat(capath, &st) == -1)
|
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
|
|
|
|
if(S_ISDIR(st.st_mode)) {
|
|
|
|
PRDirEntry *entry;
|
|
|
|
PRDir *dir = PR_OpenDir(capath);
|
|
|
|
if(!dir)
|
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
|
|
|
|
while((entry = PR_ReadDir(dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN))) {
|
|
|
|
char *fullpath = aprintf("%s/%s", capath, entry->name);
|
|
|
|
if(!fullpath) {
|
|
|
|
PR_CloseDir(dir);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
if(CURLE_OK != nss_load_cert(&conn->ssl[sockindex], fullpath, PR_TRUE))
|
2011-01-18 07:53:43 -05:00
|
|
|
/* This is purposefully tolerant of errors so non-PEM files can
|
|
|
|
* be in the same directory */
|
|
|
|
infof(data, "failed to load '%s' from CURLOPT_CAPATH\n", fullpath);
|
|
|
|
|
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_CloseDir(dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
infof(data, "warning: CURLOPT_CAPATH not a directory (%s)\n", capath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-13 15:10:00 -05:00
|
|
|
static CURLcode nss_sslver_from_curl(PRUint16 *nssver, long version)
|
2013-11-25 10:14:55 -05:00
|
|
|
{
|
2016-12-13 15:10:00 -05:00
|
|
|
switch(version) {
|
2013-11-25 10:14:55 -05:00
|
|
|
case CURL_SSLVERSION_SSLv2:
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_2;
|
2013-11-25 10:14:55 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case CURL_SSLVERSION_SSLv3:
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_3_0;
|
2013-11-25 10:14:55 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_TLS_1_0;
|
2013-11-25 10:25:15 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2013-11-25 10:14:55 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
2013-11-25 10:25:15 -05:00
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_1
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_TLS_1_1;
|
2013-11-25 10:25:15 -05:00
|
|
|
return CURLE_OK;
|
2016-12-13 15:10:00 -05:00
|
|
|
#else
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2013-11-25 10:25:15 -05:00
|
|
|
#endif
|
|
|
|
|
2013-11-25 10:14:55 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
2013-11-25 10:25:15 -05:00
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_2
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_TLS_1_2;
|
2013-11-25 10:25:15 -05:00
|
|
|
return CURLE_OK;
|
2016-12-13 15:10:00 -05:00
|
|
|
#else
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2013-11-25 10:25:15 -05:00
|
|
|
#endif
|
2016-10-27 08:27:25 -04:00
|
|
|
|
2016-10-27 08:57:11 -04:00
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_3
|
2016-12-13 15:10:00 -05:00
|
|
|
*nssver = SSL_LIBRARY_VERSION_TLS_1_3;
|
2016-10-27 08:57:11 -04:00
|
|
|
return CURLE_OK;
|
2016-12-13 15:10:00 -05:00
|
|
|
#else
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2016-10-27 08:57:11 -04:00
|
|
|
#endif
|
|
|
|
|
2016-10-27 08:27:25 -04:00
|
|
|
default:
|
2016-11-07 21:51:27 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2013-11-25 10:14:55 -05:00
|
|
|
}
|
2016-12-13 15:10:00 -05:00
|
|
|
}
|
2013-11-25 10:14:55 -05:00
|
|
|
|
2016-12-13 15:10:00 -05:00
|
|
|
static CURLcode nss_init_sslver(SSLVersionRange *sslver,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
const long min = SSL_CONN_CONFIG(version);
|
|
|
|
const long max = SSL_CONN_CONFIG(version_max);
|
|
|
|
|
|
|
|
/* map CURL_SSLVERSION_DEFAULT to NSS default */
|
|
|
|
if(min == CURL_SSLVERSION_DEFAULT || max == CURL_SSLVERSION_MAX_DEFAULT) {
|
|
|
|
/* map CURL_SSLVERSION_DEFAULT to NSS default */
|
|
|
|
if(SSL_VersionRangeGetDefault(ssl_variant_stream, sslver) != SECSuccess)
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
/* ... but make sure we use at least TLSv1.0 according to libcurl API */
|
|
|
|
if(sslver->min < SSL_LIBRARY_VERSION_TLS_1_0)
|
|
|
|
sslver->min = SSL_LIBRARY_VERSION_TLS_1_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(min) {
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2018-11-13 17:54:56 -05:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
2016-12-13 15:10:00 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = nss_sslver_from_curl(&sslver->min, min);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(max) {
|
|
|
|
case CURL_SSLVERSION_MAX_NONE:
|
|
|
|
case CURL_SSLVERSION_MAX_DEFAULT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = nss_sslver_from_curl(&sslver->max, max >> 16);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
2013-11-25 10:14:55 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
static CURLcode nss_fail_connect(struct ssl_connect_data *connssl,
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data,
|
2014-04-17 07:12:59 -04:00
|
|
|
CURLcode curlerr)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2012-09-11 03:49:23 -04:00
|
|
|
PRErrorCode err = 0;
|
2014-04-17 07:12:59 -04:00
|
|
|
|
|
|
|
if(is_nss_error(curlerr)) {
|
|
|
|
/* read NSPR error code */
|
|
|
|
err = PR_GetError();
|
|
|
|
if(is_cc_error(err))
|
|
|
|
curlerr = CURLE_SSL_CERTPROBLEM;
|
|
|
|
|
|
|
|
/* print the error number and error string */
|
|
|
|
infof(data, "NSS error %d (%s)\n", err, nss_error_to_name(err));
|
|
|
|
|
|
|
|
/* print a human-readable message describing the error if available */
|
|
|
|
nss_print_error_message(data, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup on connection failure */
|
2017-06-21 06:41:18 -04:00
|
|
|
Curl_llist_destroy(&BACKEND->obj_list, NULL);
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
return curlerr;
|
|
|
|
}
|
|
|
|
|
2017-02-09 10:21:52 -05:00
|
|
|
/* Switch the SSL socket into blocking or non-blocking mode. */
|
|
|
|
static CURLcode nss_set_blocking(struct ssl_connect_data *connssl,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
bool blocking)
|
2014-04-17 07:12:59 -04:00
|
|
|
{
|
|
|
|
static PRSocketOptionData sock_opt;
|
|
|
|
sock_opt.option = PR_SockOpt_Nonblocking;
|
2017-02-09 10:21:52 -05:00
|
|
|
sock_opt.value.non_blocking = !blocking;
|
2014-04-17 07:12:59 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(PR_SetSocketOption(BACKEND->handle, &sock_opt) != PR_SUCCESS)
|
2014-04-17 07:12:59 -04:00
|
|
|
return nss_fail_connect(connssl, data, CURLE_SSL_CONNECT_ERROR);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2007-02-12 17:32:37 -05:00
|
|
|
PRFileDesc *model = NULL;
|
2014-04-23 09:37:26 -04:00
|
|
|
PRFileDesc *nspr_io = NULL;
|
|
|
|
PRFileDesc *nspr_io_stub = NULL;
|
2011-03-15 09:52:26 -04:00
|
|
|
PRBool ssl_no_cache;
|
2012-02-08 07:36:36 -05:00
|
|
|
PRBool ssl_cbc_random_iv;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-02-12 17:32:37 -05:00
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2016-11-16 12:49:15 -05:00
|
|
|
bool second_layer = FALSE;
|
2018-12-03 06:42:48 -05:00
|
|
|
SSLVersionRange sslver_supported;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2013-12-02 10:09:12 -05:00
|
|
|
SSLVersionRange sslver = {
|
2014-07-02 11:49:37 -04:00
|
|
|
SSL_LIBRARY_VERSION_TLS_1_0, /* min */
|
2018-11-12 10:22:23 -05:00
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_3
|
|
|
|
SSL_LIBRARY_VERSION_TLS_1_3 /* max */
|
2018-12-04 13:43:51 -05:00
|
|
|
#elif defined SSL_LIBRARY_VERSION_TLS_1_2
|
2018-11-12 10:22:23 -05:00
|
|
|
SSL_LIBRARY_VERSION_TLS_1_2
|
2018-12-04 13:43:51 -05:00
|
|
|
#elif defined SSL_LIBRARY_VERSION_TLS_1_1
|
|
|
|
SSL_LIBRARY_VERSION_TLS_1_1
|
|
|
|
#else
|
|
|
|
SSL_LIBRARY_VERSION_TLS_1_0
|
2018-11-12 10:22:23 -05:00
|
|
|
#endif
|
2013-12-02 10:09:12 -05:00
|
|
|
};
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->data = data;
|
2009-07-20 17:50:21 -04:00
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
/* list of all NSS objects we need to destroy in Curl_nss_close() */
|
2017-06-21 06:41:18 -04:00
|
|
|
Curl_llist_init(&BACKEND->obj_list, nss_destroy_object);
|
2009-04-13 13:42:10 -04:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/* FIXME. NSS doesn't support multiple databases open at the same time. */
|
2008-09-23 06:27:04 -04:00
|
|
|
PR_Lock(nss_initlock);
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_init(conn->data);
|
|
|
|
if(result) {
|
2010-06-26 21:38:01 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
PK11_SetPasswordFunc(nss_get_password);
|
2017-04-10 11:05:05 -04:00
|
|
|
|
|
|
|
result = nss_load_module(&pem_module, pem_library, "PEM");
|
2008-09-23 06:27:04 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
2017-04-10 11:05:05 -04:00
|
|
|
if(result == CURLE_FAILED_INIT)
|
|
|
|
infof(data, "WARNING: failed to load NSS PEM library %s. Using "
|
|
|
|
"OpenSSL PEM certificates will not work.\n", pem_library);
|
|
|
|
else if(result)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
result = CURLE_SSL_CONNECT_ERROR;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
|
|
|
model = PR_NewTCPSocket();
|
|
|
|
if(!model)
|
|
|
|
goto error;
|
|
|
|
model = SSL_ImportFD(NULL, model);
|
|
|
|
|
|
|
|
if(SSL_OptionSet(model, SSL_SECURITY, PR_TRUE) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
|
2013-10-15 15:36:32 -04:00
|
|
|
/* do not use SSL cache if disabled or we are not going to verify peer */
|
2017-03-22 01:59:49 -04:00
|
|
|
ssl_no_cache = (SSL_SET_OPTION(primary.sessionid)
|
2016-11-16 12:49:15 -05:00
|
|
|
&& SSL_CONN_CONFIG(verifypeer)) ? PR_FALSE : PR_TRUE;
|
2011-03-15 09:52:26 -04:00
|
|
|
if(SSL_OptionSet(model, SSL_NO_CACHE, ssl_no_cache) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
|
2013-11-25 10:14:55 -05:00
|
|
|
/* enable/disable the requested SSL version(s) */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(nss_init_sslver(&sslver, data, conn) != CURLE_OK)
|
2013-09-19 09:17:13 -04:00
|
|
|
goto error;
|
2018-12-03 06:42:48 -05:00
|
|
|
if(SSL_VersionRangeGetSupported(ssl_variant_stream,
|
|
|
|
&sslver_supported) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
if(sslver_supported.max < sslver.max && sslver_supported.max >= sslver.min) {
|
2018-12-04 05:48:32 -05:00
|
|
|
char *sslver_req_str, *sslver_supp_str;
|
|
|
|
sslver_req_str = nss_sslver_to_name(sslver.max);
|
|
|
|
sslver_supp_str = nss_sslver_to_name(sslver_supported.max);
|
|
|
|
if(sslver_req_str && sslver_supp_str)
|
|
|
|
infof(data, "Falling back from %s to max supported SSL version (%s)\n",
|
|
|
|
sslver_req_str, sslver_supp_str);
|
|
|
|
free(sslver_req_str);
|
|
|
|
free(sslver_supp_str);
|
2018-12-03 06:42:48 -05:00
|
|
|
sslver.max = sslver_supported.max;
|
|
|
|
}
|
2013-11-25 10:03:52 -05:00
|
|
|
if(SSL_VersionRangeSet(model, &sslver) != SECSuccess)
|
2008-02-19 18:10:07 -05:00
|
|
|
goto error;
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
ssl_cbc_random_iv = !SSL_SET_OPTION(enable_beast);
|
2012-02-08 07:36:36 -05:00
|
|
|
#ifdef SSL_CBC_RANDOM_IV
|
|
|
|
/* unless the user explicitly asks to allow the protocol vulnerability, we
|
|
|
|
use the work-around */
|
|
|
|
if(SSL_OptionSet(model, SSL_CBC_RANDOM_IV, ssl_cbc_random_iv) != SECSuccess)
|
|
|
|
infof(data, "warning: failed to set SSL_CBC_RANDOM_IV = %d\n",
|
|
|
|
ssl_cbc_random_iv);
|
|
|
|
#else
|
|
|
|
if(ssl_cbc_random_iv)
|
|
|
|
infof(data, "warning: support for SSL_CBC_RANDOM_IV not compiled in\n");
|
|
|
|
#endif
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(cipher_list)) {
|
|
|
|
if(set_ciphers(data, model, SSL_CONN_CONFIG(cipher_list)) != SECSuccess) {
|
2014-12-27 16:35:36 -05:00
|
|
|
result = CURLE_SSL_CIPHER;
|
2007-02-12 17:32:37 -05:00
|
|
|
goto error;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!SSL_CONN_CONFIG(verifypeer) && SSL_CONN_CONFIG(verifyhost))
|
2011-03-15 09:52:26 -04:00
|
|
|
infof(data, "warning: ignoring value of ssl.verifyhost\n");
|
2009-08-13 12:04:51 -04:00
|
|
|
|
2011-03-15 09:52:26 -04:00
|
|
|
/* bypass the default SSL_AuthCertificate() hook in case we do not want to
|
|
|
|
* verify peer */
|
|
|
|
if(SSL_AuthCertificateHook(model, nss_auth_cert_hook, conn) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
/* not checked yet */
|
|
|
|
if(SSL_IS_PROXY())
|
|
|
|
data->set.proxy_ssl.certverifyresult = 0;
|
|
|
|
else
|
|
|
|
data->set.ssl.certverifyresult = 0;
|
|
|
|
|
2012-05-21 10:31:21 -04:00
|
|
|
if(SSL_BadCertHook(model, BadCertHandler, conn) != SECSuccess)
|
2007-02-12 17:32:37 -05:00
|
|
|
goto error;
|
2012-05-21 10:31:21 -04:00
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
if(SSL_HandshakeCallback(model, HandshakeCallback, conn) != SECSuccess)
|
2007-02-12 17:32:37 -05:00
|
|
|
goto error;
|
|
|
|
|
2017-03-06 10:20:33 -05:00
|
|
|
{
|
2011-02-22 07:13:53 -05:00
|
|
|
const CURLcode rv = nss_load_ca_certificates(conn, sockindex);
|
2017-03-06 10:20:33 -05:00
|
|
|
if((rv == CURLE_SSL_CACERT_BADFILE) && !SSL_CONN_CONFIG(verifypeer))
|
|
|
|
/* not a fatal error because we are not going to verify the peer */
|
|
|
|
infof(data, "warning: CA certificates failed to load\n");
|
|
|
|
else if(rv) {
|
2014-12-27 16:35:36 -05:00
|
|
|
result = rv;
|
2011-02-22 07:13:53 -05:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(CRLfile)) {
|
|
|
|
const CURLcode rv = nss_load_crl(SSL_SET_OPTION(CRLfile));
|
2014-12-27 16:35:36 -05:00
|
|
|
if(rv) {
|
|
|
|
result = rv;
|
2008-06-06 14:40:21 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2016-11-16 12:49:15 -05:00
|
|
|
infof(data, " CRLfile: %s\n", SSL_SET_OPTION(CRLfile));
|
2008-06-06 14:40:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(cert)) {
|
|
|
|
char *nickname = dup_nickname(data, SSL_SET_OPTION(cert));
|
2011-08-26 09:43:48 -04:00
|
|
|
if(nickname) {
|
|
|
|
/* we are not going to use libnsspem.so to read the client cert */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->obj_clicert = NULL;
|
2011-08-26 09:43:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-11-16 12:49:15 -05:00
|
|
|
CURLcode rv = cert_stuff(conn, sockindex, SSL_SET_OPTION(cert),
|
|
|
|
SSL_SET_OPTION(key));
|
2014-12-27 16:35:36 -05:00
|
|
|
if(rv) {
|
2011-08-26 09:43:48 -04:00
|
|
|
/* failf() is already done in cert_stuff() */
|
2014-12-27 16:35:36 -05:00
|
|
|
result = rv;
|
2011-08-26 09:43:48 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2011-01-04 07:52:54 -05:00
|
|
|
/* store the nickname for SelectClientCert() called during handshake */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->client_nickname = nickname;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
else
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->client_nickname = NULL;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2009-07-20 17:50:21 -04:00
|
|
|
if(SSL_GetClientAuthDataHook(model, SelectClientCert,
|
|
|
|
(void *)connssl) != SECSuccess) {
|
2014-12-27 16:35:36 -05:00
|
|
|
result = CURLE_SSL_CERTPROBLEM;
|
2009-07-20 17:50:21 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2008-06-06 16:52:32 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(conn->proxy_ssl[sockindex].use) {
|
|
|
|
DEBUGASSERT(ssl_connection_complete == conn->proxy_ssl[sockindex].state);
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
DEBUGASSERT(conn->proxy_ssl[sockindex].backend->handle != NULL);
|
|
|
|
nspr_io = conn->proxy_ssl[sockindex].backend->handle;
|
2016-11-16 12:49:15 -05:00
|
|
|
second_layer = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* wrap OS file descriptor by NSPR's file descriptor abstraction */
|
|
|
|
nspr_io = PR_ImportTCPSocket(sockfd);
|
|
|
|
if(!nspr_io)
|
|
|
|
goto error;
|
|
|
|
}
|
2014-04-23 09:37:26 -04:00
|
|
|
|
|
|
|
/* create our own NSPR I/O layer */
|
|
|
|
nspr_io_stub = PR_CreateIOLayerStub(nspr_io_identity, &nspr_io_methods);
|
|
|
|
if(!nspr_io_stub) {
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!second_layer)
|
|
|
|
PR_Close(nspr_io);
|
2014-04-23 09:37:26 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make the per-connection data accessible from NSPR I/O callbacks */
|
|
|
|
nspr_io_stub->secret = (void *)connssl;
|
|
|
|
|
|
|
|
/* push our new layer to the NSPR I/O stack */
|
|
|
|
if(PR_PushIOLayer(nspr_io, PR_TOP_IO_LAYER, nspr_io_stub) != PR_SUCCESS) {
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!second_layer)
|
|
|
|
PR_Close(nspr_io);
|
2014-04-23 09:37:26 -04:00
|
|
|
PR_Close(nspr_io_stub);
|
2007-02-12 17:32:37 -05:00
|
|
|
goto error;
|
2014-04-23 09:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* import our model socket onto the current I/O stack */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->handle = SSL_ImportFD(model, nspr_io);
|
|
|
|
if(!BACKEND->handle) {
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!second_layer)
|
|
|
|
PR_Close(nspr_io);
|
2014-04-23 09:37:26 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2009-11-12 05:54:10 -05:00
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
PR_Close(model); /* We don't need this any more */
|
2009-11-12 05:54:10 -05:00
|
|
|
model = NULL;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2009-07-20 17:50:21 -04:00
|
|
|
/* This is the password associated with the cert that we're using */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(key_passwd)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
SSL_SetPKCS11PinArg(BACKEND->handle, SSL_SET_OPTION(key_passwd));
|
2009-07-20 17:50:21 -04:00
|
|
|
}
|
|
|
|
|
2014-06-24 17:25:59 -04:00
|
|
|
#ifdef SSL_ENABLE_OCSP_STAPLING
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifystatus)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_OptionSet(BACKEND->handle, SSL_ENABLE_OCSP_STAPLING, PR_TRUE)
|
2014-06-24 17:25:59 -04:00
|
|
|
!= SECSuccess)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
#ifdef SSL_ENABLE_NPN
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_OptionSet(BACKEND->handle, SSL_ENABLE_NPN, conn->bits.tls_enable_npn
|
2016-05-09 10:50:11 -04:00
|
|
|
? PR_TRUE : PR_FALSE) != SECSuccess)
|
2015-03-18 14:38:32 -04:00
|
|
|
goto error;
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_ENABLE_ALPN
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_OptionSet(BACKEND->handle, SSL_ENABLE_ALPN, conn->bits.tls_enable_alpn
|
2016-05-09 10:50:11 -04:00
|
|
|
? PR_TRUE : PR_FALSE) != SECSuccess)
|
2015-03-18 14:38:32 -04:00
|
|
|
goto error;
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
|
|
|
|
2015-04-23 09:56:03 -04:00
|
|
|
#if NSSVERNUM >= 0x030f04 /* 3.15.4 */
|
2015-02-14 10:59:01 -05:00
|
|
|
if(data->set.ssl.falsestart) {
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_OptionSet(BACKEND->handle, SSL_ENABLE_FALSE_START, PR_TRUE)
|
2015-02-14 10:59:01 -05:00
|
|
|
!= SECSuccess)
|
|
|
|
goto error;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_SetCanFalseStartCallback(BACKEND->handle, CanFalseStartCallback,
|
2015-02-14 10:59:01 -05:00
|
|
|
conn) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
#if defined(SSL_ENABLE_NPN) || defined(SSL_ENABLE_ALPN)
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_npn || conn->bits.tls_enable_alpn) {
|
2015-02-16 10:41:57 -05:00
|
|
|
int cur = 0;
|
|
|
|
unsigned char protocols[128];
|
|
|
|
|
|
|
|
#ifdef USE_NGHTTP2
|
2017-03-14 03:03:46 -04:00
|
|
|
if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
|
|
|
|
(!SSL_IS_PROXY() || !conn->bits.tunnel_proxy)) {
|
2015-02-16 10:41:57 -05:00
|
|
|
protocols[cur++] = NGHTTP2_PROTO_VERSION_ID_LEN;
|
|
|
|
memcpy(&protocols[cur], NGHTTP2_PROTO_VERSION_ID,
|
2014-02-10 02:38:55 -05:00
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN);
|
|
|
|
cur += NGHTTP2_PROTO_VERSION_ID_LEN;
|
|
|
|
}
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
2015-02-16 10:41:57 -05:00
|
|
|
protocols[cur++] = ALPN_HTTP_1_1_LENGTH;
|
|
|
|
memcpy(&protocols[cur], ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH);
|
|
|
|
cur += ALPN_HTTP_1_1_LENGTH;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_SetNextProtoNego(BACKEND->handle, protocols, cur) != SECSuccess)
|
2015-02-16 10:41:57 -05:00
|
|
|
goto error;
|
2014-02-10 02:38:55 -05:00
|
|
|
}
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
|
|
|
|
2014-02-10 02:38:55 -05:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/* Force handshake on next I/O */
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_ResetHandshake(BACKEND->handle, /* asServer */ PR_FALSE)
|
2015-09-18 11:07:22 -04:00
|
|
|
!= SECSuccess)
|
|
|
|
goto error;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2015-09-18 11:07:22 -04:00
|
|
|
/* propagate hostname to the TLS layer */
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_SetURL(BACKEND->handle, SSL_IS_PROXY() ? conn->http_proxy.host.name :
|
2016-11-16 12:49:15 -05:00
|
|
|
conn->host.name) != SECSuccess)
|
2015-09-18 11:07:22 -04:00
|
|
|
goto error;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2015-09-18 11:10:05 -04:00
|
|
|
/* prevent NSS from re-using the session for a different hostname */
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_SetSockPeerID(BACKEND->handle, SSL_IS_PROXY() ?
|
2016-11-16 12:49:15 -05:00
|
|
|
conn->http_proxy.host.name : conn->host.name)
|
|
|
|
!= SECSuccess)
|
2015-09-18 11:10:05 -04:00
|
|
|
goto error;
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if(model)
|
|
|
|
PR_Close(model);
|
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
return nss_fail_connect(connssl, data, result);
|
2014-04-17 07:12:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result = CURLE_SSL_CONNECT_ERROR;
|
2014-04-17 07:12:59 -04:00
|
|
|
PRUint32 timeout;
|
2016-11-16 12:49:15 -05:00
|
|
|
long * const certverifyresult = SSL_IS_PROXY() ?
|
|
|
|
&data->set.proxy_ssl.certverifyresult : &data->set.ssl.certverifyresult;
|
2016-11-25 04:47:25 -05:00
|
|
|
const char * const pinnedpubkey = SSL_IS_PROXY() ?
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
|
2010-04-24 17:21:13 -04:00
|
|
|
/* check timeout situation */
|
2017-04-06 13:52:39 -04:00
|
|
|
const time_t time_left = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
if(time_left < 0) {
|
2010-04-24 17:21:13 -04:00
|
|
|
failf(data, "timed out before SSL handshake");
|
2014-12-27 16:35:36 -05:00
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
2010-04-24 17:21:13 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2007-05-25 17:56:27 -04:00
|
|
|
/* Force the handshake now */
|
2014-04-17 07:12:59 -04:00
|
|
|
timeout = PR_MillisecondsToInterval((PRUint32) time_left);
|
2017-06-21 06:41:18 -04:00
|
|
|
if(SSL_ForceHandshakeWithTimeout(BACKEND->handle, timeout) != SECSuccess) {
|
2014-04-17 07:27:39 -04:00
|
|
|
if(PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
2014-04-23 09:37:26 -04:00
|
|
|
/* blocking direction is updated by nss_update_connecting_state() */
|
2014-04-17 07:27:39 -04:00
|
|
|
return CURLE_AGAIN;
|
2016-11-16 12:49:15 -05:00
|
|
|
else if(*certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
|
2014-12-27 16:35:36 -05:00
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
2016-11-16 12:49:15 -05:00
|
|
|
else if(*certverifyresult != 0)
|
2018-11-19 20:48:59 -05:00
|
|
|
result = CURLE_PEER_FAILED_VERIFICATION;
|
2007-05-25 17:56:27 -04:00
|
|
|
goto error;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
result = display_conn_info(conn, BACKEND->handle);
|
2014-12-27 15:50:14 -05:00
|
|
|
if(result)
|
|
|
|
goto error;
|
2007-05-25 17:56:27 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(issuercert)) {
|
2011-01-04 07:52:54 -05:00
|
|
|
SECStatus ret = SECFailure;
|
2016-11-16 12:49:15 -05:00
|
|
|
char *nickname = dup_nickname(data, SSL_SET_OPTION(issuercert));
|
2011-08-26 08:53:26 -04:00
|
|
|
if(nickname) {
|
2016-11-16 12:49:15 -05:00
|
|
|
/* we support only nicknames in case of issuercert for now */
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = check_issuer_cert(BACKEND->handle, nickname);
|
2011-08-26 08:53:26 -04:00
|
|
|
free(nickname);
|
|
|
|
}
|
2008-11-15 18:43:10 -05:00
|
|
|
|
|
|
|
if(SECFailure == ret) {
|
2015-03-17 08:41:49 -04:00
|
|
|
infof(data, "SSL certificate issuer check failed\n");
|
2014-12-27 16:35:36 -05:00
|
|
|
result = CURLE_SSL_ISSUER_ERROR;
|
2008-06-06 16:52:32 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else {
|
2008-06-12 18:00:35 -04:00
|
|
|
infof(data, "SSL certificate issuer check ok\n");
|
2008-06-06 16:52:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 04:47:25 -05:00
|
|
|
result = cmp_peer_pubkey(connssl, pinnedpubkey);
|
2015-03-25 08:48:41 -04:00
|
|
|
if(result)
|
|
|
|
/* status already printed */
|
|
|
|
goto error;
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
error:
|
2014-12-27 16:35:36 -05:00
|
|
|
return nss_fail_connect(connssl, data, result);
|
2014-04-17 07:12:59 -04:00
|
|
|
}
|
2009-11-12 06:16:31 -05:00
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
static CURLcode nss_connect_common(struct connectdata *conn, int sockindex,
|
|
|
|
bool *done)
|
2014-04-17 07:12:59 -04:00
|
|
|
{
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2014-04-17 07:27:39 -04:00
|
|
|
const bool blocking = (done == NULL);
|
2014-12-27 16:35:36 -05:00
|
|
|
CURLcode result;
|
2012-05-21 10:19:12 -04:00
|
|
|
|
2016-10-24 11:44:45 -04:00
|
|
|
if(connssl->state == ssl_connection_complete) {
|
|
|
|
if(!blocking)
|
|
|
|
*done = TRUE;
|
2014-10-20 12:18:57 -04:00
|
|
|
return CURLE_OK;
|
2016-10-24 11:44:45 -04:00
|
|
|
}
|
2014-10-20 12:18:57 -04:00
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
if(connssl->connecting_state == ssl_connect_1) {
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_setup_connect(conn, sockindex);
|
|
|
|
if(result)
|
2014-04-17 07:27:39 -04:00
|
|
|
/* we do not expect CURLE_AGAIN from nss_setup_connect() */
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2014-04-17 07:27:39 -04:00
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
}
|
2012-05-21 10:19:12 -04:00
|
|
|
|
2017-02-09 10:21:52 -05:00
|
|
|
/* enable/disable blocking mode before handshake */
|
|
|
|
result = nss_set_blocking(connssl, data, blocking);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2014-12-27 16:35:36 -05:00
|
|
|
result = nss_do_connect(conn, sockindex);
|
|
|
|
switch(result) {
|
2014-04-17 07:12:59 -04:00
|
|
|
case CURLE_OK:
|
|
|
|
break;
|
2014-04-17 07:27:39 -04:00
|
|
|
case CURLE_AGAIN:
|
|
|
|
if(!blocking)
|
|
|
|
/* CURLE_AGAIN in non-blocking mode is not an error */
|
|
|
|
return CURLE_OK;
|
2018-11-12 09:23:17 -05:00
|
|
|
/* FALLTHROUGH */
|
2014-04-17 07:12:59 -04:00
|
|
|
default:
|
2014-12-27 16:35:36 -05:00
|
|
|
return result;
|
2012-08-09 03:40:00 -04:00
|
|
|
}
|
2010-04-06 07:42:11 -04:00
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
if(blocking) {
|
|
|
|
/* in blocking mode, set NSS non-blocking mode _after_ SSL handshake */
|
2017-02-09 10:21:52 -05:00
|
|
|
result = nss_set_blocking(connssl, data, /* blocking */ FALSE);
|
2014-12-27 16:35:36 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2014-04-17 07:27:39 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* signal completed SSL handshake */
|
|
|
|
*done = TRUE;
|
2009-11-12 06:16:31 -05:00
|
|
|
|
2014-10-20 12:18:57 -04:00
|
|
|
connssl->state = ssl_connection_complete;
|
|
|
|
conn->recv[sockindex] = nss_recv;
|
|
|
|
conn->send[sockindex] = nss_send;
|
|
|
|
|
|
|
|
/* ssl_connect_done is never used outside, go back to the initial state */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
return CURLE_OK;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
|
2014-04-17 07:27:39 -04:00
|
|
|
{
|
|
|
|
return nss_connect_common(conn, sockindex, /* blocking */ NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_nss_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex, bool *done)
|
2014-04-17 07:27:39 -04:00
|
|
|
{
|
|
|
|
return nss_connect_common(conn, sockindex, done);
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t nss_send(struct connectdata *conn, /* connection data */
|
|
|
|
int sockindex, /* socketindex */
|
|
|
|
const void *mem, /* send this data */
|
|
|
|
size_t len, /* amount to write */
|
|
|
|
CURLcode *curlcode)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2017-07-20 02:05:59 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
ssize_t rc;
|
|
|
|
|
2017-07-19 12:02:26 -04:00
|
|
|
/* The SelectClientCert() hook uses this for infof() and failf() but the
|
|
|
|
handle stored in nss_setup_connect() could have already been freed. */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->data = conn->data;
|
2017-07-19 12:02:26 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
rc = PR_Send(BACKEND->handle, mem, (int)len, 0, PR_INTERVAL_NO_WAIT);
|
2007-02-12 17:32:37 -05:00
|
|
|
if(rc < 0) {
|
2010-04-04 17:37:18 -04:00
|
|
|
PRInt32 err = PR_GetError();
|
|
|
|
if(err == PR_WOULD_BLOCK_ERROR)
|
2010-05-07 09:05:34 -04:00
|
|
|
*curlcode = CURLE_AGAIN;
|
2010-04-04 17:37:18 -04:00
|
|
|
else {
|
2012-05-21 10:19:12 -04:00
|
|
|
/* print the error number and error string */
|
2012-04-11 07:44:20 -04:00
|
|
|
const char *err_name = nss_error_to_name(err);
|
2012-05-21 10:19:12 -04:00
|
|
|
infof(conn->data, "SSL write: error %d (%s)\n", err, err_name);
|
|
|
|
|
|
|
|
/* print a human-readable message describing the error if available */
|
|
|
|
nss_print_error_message(conn->data, err);
|
|
|
|
|
|
|
|
*curlcode = (is_cc_error(err))
|
|
|
|
? CURLE_SSL_CERTPROBLEM
|
|
|
|
: CURLE_SEND_ERROR;
|
2010-04-04 17:37:18 -04:00
|
|
|
}
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return rc; /* number of bytes */
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:05:59 -04:00
|
|
|
static ssize_t nss_recv(struct connectdata *conn, /* connection data */
|
|
|
|
int sockindex, /* socketindex */
|
2010-05-07 09:05:34 -04:00
|
|
|
char *buf, /* store read data here */
|
|
|
|
size_t buffersize, /* max amount to read */
|
|
|
|
CURLcode *curlcode)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2017-07-20 02:05:59 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
ssize_t nread;
|
|
|
|
|
2017-07-19 12:02:26 -04:00
|
|
|
/* The SelectClientCert() hook uses this for infof() and failf() but the
|
|
|
|
handle stored in nss_setup_connect() could have already been freed. */
|
2017-06-21 06:41:18 -04:00
|
|
|
BACKEND->data = conn->data;
|
2017-07-19 12:02:26 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
nread = PR_Recv(BACKEND->handle, buf, (int)buffersize, 0,
|
2017-07-20 02:05:59 -04:00
|
|
|
PR_INTERVAL_NO_WAIT);
|
2007-02-12 17:32:37 -05:00
|
|
|
if(nread < 0) {
|
|
|
|
/* failed SSL read */
|
|
|
|
PRInt32 err = PR_GetError();
|
|
|
|
|
2010-04-04 17:37:18 -04:00
|
|
|
if(err == PR_WOULD_BLOCK_ERROR)
|
2010-05-07 09:05:34 -04:00
|
|
|
*curlcode = CURLE_AGAIN;
|
2010-04-04 17:37:18 -04:00
|
|
|
else {
|
2012-05-21 10:19:12 -04:00
|
|
|
/* print the error number and error string */
|
2012-04-11 07:44:20 -04:00
|
|
|
const char *err_name = nss_error_to_name(err);
|
2012-05-21 10:19:12 -04:00
|
|
|
infof(conn->data, "SSL read: errno %d (%s)\n", err, err_name);
|
|
|
|
|
|
|
|
/* print a human-readable message describing the error if available */
|
|
|
|
nss_print_error_message(conn->data, err);
|
|
|
|
|
|
|
|
*curlcode = (is_cc_error(err))
|
|
|
|
? CURLE_SSL_CERTPROBLEM
|
|
|
|
: CURLE_RECV_ERROR;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static size_t Curl_nss_version(char *buffer, size_t size)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2018-11-22 03:01:24 -05:00
|
|
|
return msnprintf(buffer, size, "NSS/%s", NSS_VERSION);
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2010-06-26 21:38:01 -04:00
|
|
|
|
2015-02-24 09:18:45 -05:00
|
|
|
/* data might be NULL */
|
2017-06-23 07:19:00 -04:00
|
|
|
static int Curl_nss_seed(struct Curl_easy *data)
|
2010-06-26 21:38:01 -04:00
|
|
|
{
|
2013-08-12 09:14:35 -04:00
|
|
|
/* make sure that NSS is initialized */
|
|
|
|
return !!Curl_nss_force_init(data);
|
2010-06-26 21:38:01 -04:00
|
|
|
}
|
|
|
|
|
2014-06-03 14:04:46 -04:00
|
|
|
/* data might be NULL */
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_nss_random(struct Curl_easy *data,
|
|
|
|
unsigned char *entropy,
|
|
|
|
size_t length)
|
2012-06-26 08:52:46 -04:00
|
|
|
{
|
2015-02-24 09:18:45 -05:00
|
|
|
Curl_nss_seed(data); /* Initiate the seed if not already done */
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2015-02-24 09:10:15 -05:00
|
|
|
if(SECSuccess != PK11_GenerateRandom(entropy, curlx_uztosi(length)))
|
|
|
|
/* signal a failure */
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_FAILED_INIT;
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_OK;
|
2012-06-26 08:52:46 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:04:56 -04:00
|
|
|
static CURLcode Curl_nss_md5sum(unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *md5sum, /* output */
|
|
|
|
size_t md5len)
|
2012-06-26 08:52:46 -04:00
|
|
|
{
|
|
|
|
PK11Context *MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
|
|
|
|
unsigned int MD5out;
|
2014-12-27 16:35:36 -05:00
|
|
|
|
2012-06-28 09:48:32 -04:00
|
|
|
PK11_DigestOp(MD5pw, tmp, curlx_uztoui(tmplen));
|
|
|
|
PK11_DigestFinal(MD5pw, md5sum, &MD5out, curlx_uztoui(md5len));
|
2012-06-26 08:52:46 -04:00
|
|
|
PK11_DestroyContext(MD5pw, PR_TRUE);
|
2017-06-22 19:04:56 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2012-06-26 08:52:46 -04:00
|
|
|
}
|
|
|
|
|
2018-04-02 13:33:00 -04:00
|
|
|
static CURLcode Curl_nss_sha256sum(const unsigned char *tmp, /* input */
|
2017-06-22 19:04:56 -04:00
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *sha256sum, /* output */
|
|
|
|
size_t sha256len)
|
2015-06-30 20:23:54 -04:00
|
|
|
{
|
|
|
|
PK11Context *SHA256pw = PK11_CreateDigestContext(SEC_OID_SHA256);
|
|
|
|
unsigned int SHA256out;
|
|
|
|
|
|
|
|
PK11_DigestOp(SHA256pw, tmp, curlx_uztoui(tmplen));
|
|
|
|
PK11_DigestFinal(SHA256pw, sha256sum, &SHA256out, curlx_uztoui(sha256len));
|
|
|
|
PK11_DestroyContext(SHA256pw, PR_TRUE);
|
2018-04-02 13:33:00 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2015-06-30 20:23:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static bool Curl_nss_cert_status_request(void)
|
2014-06-24 17:25:59 -04:00
|
|
|
{
|
|
|
|
#ifdef SSL_ENABLE_OCSP_STAPLING
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static bool Curl_nss_false_start(void)
|
2016-11-23 02:49:04 -05:00
|
|
|
{
|
2015-04-23 09:56:03 -04:00
|
|
|
#if NSSVERNUM >= 0x030f04 /* 3.15.4 */
|
2015-02-14 10:59:01 -05:00
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-23 10:05:26 -04:00
|
|
|
static void *Curl_nss_get_internals(struct ssl_connect_data *connssl,
|
|
|
|
CURLINFO info UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
(void)info;
|
2017-06-21 06:41:18 -04:00
|
|
|
return BACKEND->handle;
|
2017-06-23 10:05:26 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 10:45:34 -04:00
|
|
|
const struct Curl_ssl Curl_ssl_nss = {
|
2017-07-15 07:49:30 -04:00
|
|
|
{ CURLSSLBACKEND_NSS, "nss" }, /* info */
|
2017-06-22 10:45:34 -04:00
|
|
|
|
2018-05-04 06:10:39 -04:00
|
|
|
SSLSUPP_CA_PATH |
|
|
|
|
SSLSUPP_CERTINFO |
|
|
|
|
SSLSUPP_PINNEDPUBKEY |
|
|
|
|
SSLSUPP_HTTPS_PROXY,
|
2017-06-20 05:32:53 -04:00
|
|
|
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
sizeof(struct ssl_backend_data),
|
|
|
|
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_nss_init, /* init */
|
|
|
|
Curl_nss_cleanup, /* cleanup */
|
|
|
|
Curl_nss_version, /* version */
|
|
|
|
Curl_nss_check_cxn, /* check_cxn */
|
|
|
|
/* NSS has no shutdown function provided and thus always fail */
|
|
|
|
Curl_none_shutdown, /* shutdown */
|
|
|
|
Curl_none_data_pending, /* data_pending */
|
|
|
|
Curl_nss_random, /* random */
|
|
|
|
Curl_nss_cert_status_request, /* cert_status_request */
|
|
|
|
Curl_nss_connect, /* connect */
|
|
|
|
Curl_nss_connect_nonblocking, /* connect_nonblocking */
|
2017-06-23 10:05:26 -04:00
|
|
|
Curl_nss_get_internals, /* get_internals */
|
2017-10-19 14:55:17 -04:00
|
|
|
Curl_nss_close, /* close_one */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_none_close_all, /* close_all */
|
|
|
|
/* NSS has its own session ID cache */
|
|
|
|
Curl_none_session_free, /* session_free */
|
|
|
|
Curl_none_set_engine, /* set_engine */
|
|
|
|
Curl_none_set_engine_default, /* set_engine_default */
|
|
|
|
Curl_none_engines_list, /* engines_list */
|
2017-06-22 19:04:56 -04:00
|
|
|
Curl_nss_false_start, /* false_start */
|
2017-06-22 19:04:56 -04:00
|
|
|
Curl_nss_md5sum, /* md5sum */
|
|
|
|
Curl_nss_sha256sum /* sha256sum */
|
2017-06-22 10:45:34 -04:00
|
|
|
};
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
#endif /* USE_NSS */
|