2007-02-12 17:32:37 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-02-06 17:32:56 -05:00
|
|
|
* Copyright (C) 1998 - 2014, 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
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all 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"
|
|
|
|
#include "strequal.h"
|
|
|
|
#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"
|
2007-02-12 17:32:37 -05:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use the internal *printf() functions */
|
|
|
|
#include <curl/mprintf.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>
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "rawstr.h"
|
|
|
|
#include "warnless.h"
|
2013-11-03 11:36:51 -05:00
|
|
|
#include "x509asn1.h"
|
2007-02-12 17:32:37 -05:00
|
|
|
|
|
|
|
/* The last #include file should be: */
|
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
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
|
|
|
|
|
2008-09-23 06:27:04 -04:00
|
|
|
PRLock * nss_initlock = NULL;
|
2010-05-11 08:10:27 -04:00
|
|
|
PRLock * nss_crllock = NULL;
|
2014-07-04 06:41:53 -04:00
|
|
|
struct curl_llist *nss_crl_list = NULL;
|
2012-04-10 09:42:34 -04:00
|
|
|
NSSInitContext * nss_context = NULL;
|
2008-09-23 06:27:04 -04:00
|
|
|
|
|
|
|
volatile int initialized = 0;
|
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},
|
|
|
|
{"echde_rsa_null", TLS_ECDHE_RSA_WITH_NULL_SHA},
|
|
|
|
{"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
|
2007-02-12 17:32:37 -05:00
|
|
|
};
|
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
static const char* pem_library = "libnsspem.so";
|
|
|
|
SECMODModule* mod = 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;
|
|
|
|
|
2012-04-11 07:44:20 -04:00
|
|
|
static const char* nss_error_to_name(PRErrorCode code)
|
|
|
|
{
|
|
|
|
const char *name = PR_ErrorToName(code);
|
|
|
|
if(name)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:19:12 -04:00
|
|
|
static void nss_print_error_message(struct SessionHandle *data, PRUint32 err)
|
|
|
|
{
|
|
|
|
failf(data, "%s", PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT));
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
static SECStatus set_ciphers(struct SessionHandle *data, PRFileDesc * model,
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
for(i=0; i<SSL_NumImplementedCiphers; i++) {
|
2014-01-29 06:55:36 -05:00
|
|
|
SSL_CipherPrefSet(model, SSL_ImplementedCiphers[i], PR_FALSE);
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set every entry in our list to false */
|
2008-06-19 01:47:27 -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;
|
|
|
|
|
|
|
|
if((cipher_list = strchr(cipher, ','))) {
|
|
|
|
*cipher_list++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
found = PR_FALSE;
|
|
|
|
|
2008-06-19 01:47:27 -04:00
|
|
|
for(i=0; i<NUM_OF_CIPHERS; i++) {
|
2008-10-16 04:23:48 -04:00
|
|
|
if(Curl_raw_equal(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 */
|
2008-06-19 01:47:27 -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
|
|
|
/*
|
|
|
|
* Get the number of ciphers that are enabled. We use this to determine
|
|
|
|
* if we need to call NSS_SetDomesticPolicy() to enable the default ciphers.
|
|
|
|
*/
|
2009-01-07 09:12:01 -05:00
|
|
|
static int num_enabled_ciphers(void)
|
2009-01-07 09:10:35 -05:00
|
|
|
{
|
|
|
|
PRInt32 policy = 0;
|
|
|
|
int count = 0;
|
2009-01-07 09:12:01 -05:00
|
|
|
unsigned int i;
|
2009-01-07 09:10:35 -05:00
|
|
|
|
|
|
|
for(i=0; i<NUM_OF_CIPHERS; i++) {
|
|
|
|
SSL_CipherPolicyGet(cipherlist[i].num, &policy);
|
|
|
|
if(policy)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2011-08-26 08:53:26 -04:00
|
|
|
static char* dup_nickname(struct SessionHandle *data, enum dupstring cert_kind)
|
2009-10-07 16:34:08 -04:00
|
|
|
{
|
2011-01-04 07:52:54 -05:00
|
|
|
const char *str = data->set.str[cert_kind];
|
|
|
|
const char *n;
|
|
|
|
|
|
|
|
if(!is_file(str))
|
|
|
|
/* no such file exists, use the string as nickname */
|
|
|
|
return strdup(str);
|
|
|
|
|
|
|
|
/* search the last slash; we require at least one slash in a file name */
|
|
|
|
n = strrchr(str, '/');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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(). */
|
|
|
|
static CURLcode nss_create_object(struct ssl_connect_data *ssl,
|
|
|
|
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;
|
2011-08-26 09:43:48 -04:00
|
|
|
CURLcode err = (cacert)
|
|
|
|
? 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;
|
|
|
|
|
|
|
|
slot = PK11_FindSlotByName(slot_name);
|
|
|
|
free(slot_name);
|
|
|
|
if(!slot)
|
2011-08-26 09:43:48 -04:00
|
|
|
return err;
|
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,
|
|
|
|
strlen(filename) + 1);
|
|
|
|
|
|
|
|
if(CKO_CERTIFICATE == obj_class) {
|
|
|
|
CK_BBOOL *pval = (cacert) ? (&cktrue) : (&ckfalse);
|
|
|
|
PK11_SETATTRS(attrs, attr_cnt, CKA_TRUST, pval, sizeof(*pval));
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = PK11_CreateGenericObject(slot, attrs, attr_cnt, PR_FALSE);
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
if(!obj)
|
2011-08-26 09:43:48 -04:00
|
|
|
return err;
|
2011-01-27 04:55:02 -05:00
|
|
|
|
|
|
|
if(!Curl_llist_insert_next(ssl->obj_list, ssl->obj_list->tail, obj)) {
|
|
|
|
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 */
|
|
|
|
ssl->obj_clicert = obj;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
PK11GenericObject *obj = (PK11GenericObject *)ptr;
|
|
|
|
(void) user;
|
|
|
|
PK11_DestroyGenericObject(obj);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
SECItem *crl_der = (SECItem *)ptr;
|
|
|
|
(void) user;
|
|
|
|
SECITEM_FreeItem(crl_der, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-08-26 09:43:48 -04:00
|
|
|
CURLcode err = (cacert)
|
|
|
|
? 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))
|
2011-09-06 12:17:38 -04:00
|
|
|
err = nss_create_object(ssl, CKO_CERTIFICATE, filename, cacert);
|
|
|
|
|
|
|
|
if(CURLE_OK == err && !cacert) {
|
|
|
|
/* 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
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
return err;
|
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-07-03 17:53:44 -04:00
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
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() */
|
|
|
|
if(!Curl_llist_insert_next(nss_crl_list, nss_crl_list->tail, crl_der)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-04 06:41:53 -04: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 */
|
|
|
|
body = (char*)filedata.data;
|
|
|
|
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
|
|
|
{
|
2011-01-27 04:55:02 -05:00
|
|
|
PK11SlotInfo *slot;
|
|
|
|
SECStatus status;
|
2012-04-13 12:26:42 -04:00
|
|
|
CURLcode rv;
|
2011-01-27 04:55:02 -05:00
|
|
|
struct ssl_connect_data *ssl = conn->ssl;
|
2012-04-06 10:05:25 -04:00
|
|
|
(void)sockindex; /* unused */
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2012-04-13 12:26:42 -04:00
|
|
|
rv = nss_create_object(ssl, CKO_PRIVATE_KEY, key_file, FALSE);
|
2011-08-26 09:43:48 -04:00
|
|
|
if(CURLE_OK != rv) {
|
2007-09-18 18:21:54 -04:00
|
|
|
PR_SetError(SEC_ERROR_BAD_KEY, 0);
|
2011-08-26 09:43:48 -04:00
|
|
|
return rv;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
slot = PK11_FindSlotByName("PEM Token #1");
|
|
|
|
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 */
|
|
|
|
SECMOD_WaitForAnyTokenEvent(mod, 0, 0);
|
|
|
|
PK11_IsPresent(slot);
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
status = PK11_Authenticate(slot, PR_TRUE,
|
|
|
|
conn->data->set.str[STRING_KEY_PASSWD]);
|
2009-04-24 17:55:18 -04:00
|
|
|
PK11_FreeSlot(slot);
|
2011-08-26 09:43:48 -04: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
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2011-08-26 09:43:48 -04:00
|
|
|
CURLcode rv;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
|
|
|
if(cert_file) {
|
2009-04-13 13:42:10 -04:00
|
|
|
rv = nss_load_cert(&conn->ssl[sockindex], cert_file, PR_FALSE);
|
2011-08-26 09:43:48 -04:00
|
|
|
if(CURLE_OK != rv) {
|
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
|
|
|
|
|
|
|
return rv;
|
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)
|
2009-04-13 13:42:10 -04:00
|
|
|
rv = 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 */
|
2009-04-13 13:42:10 -04:00
|
|
|
rv = nss_load_key(conn, sockindex, cert_file);
|
2011-08-26 09:43:48 -04:00
|
|
|
if(CURLE_OK != rv) {
|
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
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
return rv;
|
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
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
static char * nss_get_password(PK11SlotInfo * slot, PRBool retry, void *arg)
|
|
|
|
{
|
|
|
|
(void)slot; /* unused */
|
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;
|
|
|
|
if(!conn->data->set.ssl.verifypeer) {
|
|
|
|
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-02-10 02:38:55 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
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;
|
|
|
|
|
2014-02-10 02:38:55 -05:00
|
|
|
if(!conn->data->set.ssl_enable_npn && !conn->data->set.ssl_enable_alpn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
if(SSL_GetNextProto(sock, &state, buf, &buflen, buflenmax) == SECSuccess) {
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case SSL_NEXT_PROTO_NO_SUPPORT:
|
|
|
|
case SSL_NEXT_PROTO_NO_OVERLAP:
|
|
|
|
infof(conn->data, "TLS, neither ALPN nor NPN succeeded\n");
|
|
|
|
return;
|
2014-02-09 20:58:54 -05:00
|
|
|
#ifdef SSL_ENABLE_ALPN
|
2014-02-07 04:48:34 -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-02-07 04:48:34 -05:00
|
|
|
case SSL_NEXT_PROTO_NEGOTIATED:
|
|
|
|
infof(conn->data, "NPN, server accepted to use %.*s\n", buflen, buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(buflen == NGHTTP2_PROTO_VERSION_ID_LEN &&
|
2014-02-07 09:38:45 -05:00
|
|
|
memcmp(NGHTTP2_PROTO_VERSION_ID, buf, NGHTTP2_PROTO_VERSION_ID_LEN)
|
|
|
|
== 0) {
|
2014-03-31 02:40:24 -04:00
|
|
|
conn->negnpn = NPN_HTTP2;
|
2014-02-07 04:48:34 -05:00
|
|
|
}
|
|
|
|
else if(buflen == ALPN_HTTP_1_1_LENGTH && memcmp(ALPN_HTTP_1_1, buf,
|
2014-02-07 09:38:45 -05:00
|
|
|
ALPN_HTTP_1_1_LENGTH)) {
|
2014-02-07 04:48:34 -05:00
|
|
|
conn->negnpn = NPN_HTTP1_1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-03 02:39:25 -05:00
|
|
|
#else
|
|
|
|
(void)sock;
|
|
|
|
(void)arg;
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2010-02-17 07:13:55 -05:00
|
|
|
static void display_cert_info(struct SessionHandle *data,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-10-25 16:54:46 -04:00
|
|
|
static void display_conn_info(struct connectdata *conn, PRFileDesc *sock)
|
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
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(SSL_GetChannelInfo(sock, &channel, sizeof channel) ==
|
2008-06-06 16:57:32 -04:00
|
|
|
SECSuccess && channel.length == sizeof channel &&
|
|
|
|
channel.cipherSuite) {
|
2007-11-05 04:45:09 -05:00
|
|
|
if(SSL_GetCipherSuiteInfo(channel.cipherSuite,
|
2008-06-06 16:57:32 -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Curl_ssl_init_certinfo(conn->data, i);
|
|
|
|
for(i = 0; cert; cert = cert2) {
|
2013-11-04 07:59:34 -05:00
|
|
|
Curl_extract_certinfo(conn, i++, (char *)cert->derCert.data,
|
|
|
|
(char *)cert->derCert.data + cert->derCert.len);
|
2013-10-30 06:12:06 -04:00
|
|
|
if(cert->isRoot) {
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cert2 = CERT_FindCertIssuer(cert, now, certUsageSSLCA);
|
|
|
|
CERT_DestroyCertificate(cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:19:12 -04:00
|
|
|
static SECStatus BadCertHandler(void *arg, PRFileDesc *sock)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = (struct connectdata *)arg;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
PRErrorCode err = PR_GetError();
|
|
|
|
CERTCertificate *cert;
|
|
|
|
|
|
|
|
/* remember the cert verification result */
|
|
|
|
data->set.ssl.certverifyresult = err;
|
|
|
|
|
|
|
|
if(err == SSL_ERROR_BAD_CERT_DOMAIN && !data->set.ssl.verifyhost)
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
CERTCertificate *cert,*cert_issuer,*issuer;
|
|
|
|
SECStatus res=SECSuccess;
|
|
|
|
void *proto_win = NULL;
|
|
|
|
|
|
|
|
/*
|
2010-02-17 07:13:55 -05:00
|
|
|
PRArenaPool *tmpArena = NULL;
|
|
|
|
CERTAuthKeyID *authorityKeyID = NULL;
|
|
|
|
SECITEM *caname = NULL;
|
2008-06-06 16:52:32 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
cert = SSL_PeerCertificate(sock);
|
|
|
|
cert_issuer = CERT_FindCertIssuer(cert,PR_Now(),certUsageObjectSigner);
|
|
|
|
|
|
|
|
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,
|
|
|
|
&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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
struct SessionHandle *data = connssl->data;
|
|
|
|
const char *nickname = connssl->client_nickname;
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2011-08-26 08:38:18 -04:00
|
|
|
if(connssl->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
|
|
|
static const char pem_slotname[] = "PEM Token #1";
|
|
|
|
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
|
|
|
|
2011-08-26 08:38:18 -04:00
|
|
|
PK11SlotInfo *slot = PK11_FindSlotByName(pem_slotname);
|
|
|
|
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
|
|
|
|
2011-08-26 08:38:18 -04:00
|
|
|
if(PK11_ReadRawAttribute(PK11_TypeGeneric, connssl->obj_clicert, CKA_VALUE,
|
|
|
|
&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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-11-12 06:16:31 -05:00
|
|
|
/* This function is supposed to decide, which error codes should be used
|
|
|
|
* to conclude server is TLS intolerant.
|
|
|
|
*
|
|
|
|
* taken from xulrunner - nsNSSIOLayer.cpp
|
|
|
|
*/
|
|
|
|
static PRBool
|
|
|
|
isTLSIntoleranceError(PRInt32 err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_BAD_MAC_ALERT:
|
|
|
|
case SSL_ERROR_BAD_MAC_READ:
|
|
|
|
case SSL_ERROR_HANDSHAKE_FAILURE_ALERT:
|
|
|
|
case SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT:
|
|
|
|
case SSL_ERROR_CLIENT_KEY_EXCHANGE_FAILURE:
|
|
|
|
case SSL_ERROR_ILLEGAL_PARAMETER_ALERT:
|
|
|
|
case SSL_ERROR_NO_CYPHER_OVERLAP:
|
|
|
|
case SSL_ERROR_BAD_SERVER:
|
|
|
|
case SSL_ERROR_BAD_BLOCK_PADDING:
|
|
|
|
case SSL_ERROR_UNSUPPORTED_VERSION:
|
|
|
|
case SSL_ERROR_PROTOCOL_VERSION_ALERT:
|
|
|
|
case SSL_ERROR_RX_MALFORMED_FINISHED:
|
|
|
|
case SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE:
|
|
|
|
case SSL_ERROR_DECODE_ERROR_ALERT:
|
|
|
|
case SSL_ERROR_RX_UNKNOWN_ALERT:
|
|
|
|
return PR_TRUE;
|
|
|
|
default:
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-15 07:48:45 -04:00
|
|
|
static CURLcode nss_init_core(struct SessionHandle *data, const char *cert_dir)
|
|
|
|
{
|
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) {
|
|
|
|
const bool use_sql = NSS_VersionCheck("3.12.0");
|
|
|
|
char *certpath = aprintf("%s%s", use_sql ? "sql:" : "", cert_dir);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode nss_init(struct SessionHandle *data)
|
2010-06-26 21:38:01 -04:00
|
|
|
{
|
|
|
|
char *cert_dir;
|
|
|
|
struct_stat st;
|
2011-08-15 07:48:45 -04:00
|
|
|
CURLcode rv;
|
|
|
|
|
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() */
|
|
|
|
nss_crl_list = Curl_llist_alloc(nss_destroy_crl_item);
|
|
|
|
if(!nss_crl_list)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
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 */
|
|
|
|
memcpy(&nspr_io_methods, PR_GetDefaultIOMethods(), sizeof nspr_io_methods);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-08-15 07:48:45 -04:00
|
|
|
rv = nss_init_core(data, cert_dir);
|
|
|
|
if(rv)
|
|
|
|
return rv;
|
2010-06-26 21:38:01 -04:00
|
|
|
|
|
|
|
if(num_enabled_ciphers() == 0)
|
|
|
|
NSS_SetDomesticPolicy();
|
|
|
|
|
2011-08-15 07:48:45 -04:00
|
|
|
initialized = 1;
|
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
|
|
|
|
*/
|
|
|
|
int Curl_nss_init(void)
|
|
|
|
{
|
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();
|
2008-09-23 06:27:04 -04:00
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
|
|
|
|
/* We will actually initialize NSS later */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
CURLcode Curl_nss_force_init(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
CURLcode rv;
|
|
|
|
if(!nss_initlock) {
|
2011-04-20 09:17:42 -04:00
|
|
|
failf(data,
|
|
|
|
"unable to initialize NSS, curl_global_init() should have been "
|
|
|
|
"called with CURL_GLOBAL_SSL or CURL_GLOBAL_ALL");
|
|
|
|
return CURLE_FAILED_INIT;
|
2010-06-26 21:38:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_Lock(nss_initlock);
|
2011-08-15 07:48:45 -04:00
|
|
|
rv = nss_init(data);
|
2010-06-26 21:38:01 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
/* Global cleanup */
|
|
|
|
void Curl_nss_cleanup(void)
|
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
if(mod && SECSuccess == SECMOD_UnloadUserModule(mod)) {
|
2009-04-24 17:55:18 -04:00
|
|
|
SECMOD_DestroyModule(mod);
|
2009-07-20 17:50:21 -04:00
|
|
|
mod = NULL;
|
|
|
|
}
|
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 */
|
|
|
|
Curl_llist_destroy(nss_crl_list, NULL);
|
|
|
|
nss_crl_list = NULL;
|
|
|
|
|
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);
|
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
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
Curl_nss_check_cxn(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char buf;
|
|
|
|
|
|
|
|
rc =
|
|
|
|
PR_Recv(conn->ssl[FIRSTSOCKET].handle, (void *)&buf, 1, PR_MSG_PEEK,
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called when an SSL connection is closed.
|
|
|
|
*/
|
2007-07-29 08:54:05 -04:00
|
|
|
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];
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2007-07-29 08:54:05 -04:00
|
|
|
if(connssl->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;
|
2013-01-11 04:24:21 -05:00
|
|
|
|
|
|
|
if((connssl->client_nickname != NULL) || (connssl->obj_clicert != NULL))
|
|
|
|
/* 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. */
|
|
|
|
SSL_InvalidateSession(connssl->handle);
|
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
if(connssl->client_nickname != NULL) {
|
|
|
|
free(connssl->client_nickname);
|
|
|
|
connssl->client_nickname = NULL;
|
|
|
|
}
|
2011-01-27 04:55:02 -05:00
|
|
|
/* destroy all NSS objects in order to avoid failure of NSS shutdown */
|
|
|
|
Curl_llist_destroy(connssl->obj_list, NULL);
|
2011-02-17 11:37:24 -05:00
|
|
|
connssl->obj_list = NULL;
|
2011-08-26 08:38:18 -04:00
|
|
|
connssl->obj_clicert = NULL;
|
2012-04-06 10:05:25 -04:00
|
|
|
|
2011-04-01 10:31:28 -04:00
|
|
|
PR_Close(connssl->handle);
|
2007-07-29 08:54:05 -04:00
|
|
|
connssl->handle = NULL;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called when the 'data' struct is going away. Close
|
|
|
|
* down everything and free all resources!
|
|
|
|
*/
|
|
|
|
int Curl_nss_close_all(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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_CACERT:
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
const char *cafile = data->set.ssl.CAfile;
|
|
|
|
const char *capath = data->set.ssl.CApath;
|
|
|
|
|
2011-08-26 09:43:48 -04:00
|
|
|
if(cafile) {
|
|
|
|
CURLcode rv = nss_load_cert(&conn->ssl[sockindex], cafile, PR_TRUE);
|
|
|
|
if(CURLE_OK != rv)
|
|
|
|
return rv;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, " CAfile: %s\n CApath: %s\n",
|
|
|
|
cafile ? cafile : "none",
|
|
|
|
capath ? capath : "none");
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-25 10:14:55 -05:00
|
|
|
static CURLcode nss_init_sslver(SSLVersionRange *sslver,
|
|
|
|
struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
switch (data->set.ssl.version) {
|
|
|
|
default:
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
2014-07-02 11:49:37 -04:00
|
|
|
sslver->min = SSL_LIBRARY_VERSION_3_0;
|
2013-11-25 10:14:55 -05:00
|
|
|
if(data->state.ssl_connect_retry) {
|
|
|
|
infof(data, "TLS disabled due to previous handshake failure\n");
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_3_0;
|
2014-02-06 03:41:53 -05:00
|
|
|
return CURLE_OK;
|
2013-11-25 10:14:55 -05:00
|
|
|
}
|
2014-02-06 03:41:53 -05:00
|
|
|
/* intentional fall-through to default to highest TLS version if possible */
|
2013-11-25 10:14:55 -05:00
|
|
|
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2013-11-25 10:25:15 -05:00
|
|
|
#ifdef SSL_LIBRARY_VERSION_TLS_1_2
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_2;
|
|
|
|
#elif defined SSL_LIBRARY_VERSION_TLS_1_1
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_1;
|
|
|
|
#else
|
2013-11-25 10:14:55 -05:00
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_0;
|
2013-11-25 10:25:15 -05:00
|
|
|
#endif
|
2013-11-25 10:14:55 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case CURL_SSLVERSION_SSLv2:
|
|
|
|
sslver->min = SSL_LIBRARY_VERSION_2;
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_2;
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
sslver->min = SSL_LIBRARY_VERSION_3_0;
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_3_0;
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
2013-11-25 10:25:15 -05:00
|
|
|
sslver->min = SSL_LIBRARY_VERSION_TLS_1_0;
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_0;
|
|
|
|
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
|
|
|
|
sslver->min = SSL_LIBRARY_VERSION_TLS_1_1;
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_1;
|
|
|
|
return CURLE_OK;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
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
|
|
|
|
sslver->min = SSL_LIBRARY_VERSION_TLS_1_2;
|
|
|
|
sslver->max = SSL_LIBRARY_VERSION_TLS_1_2;
|
|
|
|
return CURLE_OK;
|
|
|
|
#endif
|
2013-11-25 10:14:55 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
failf(data, "TLS minor version cannot be set");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
static CURLcode nss_fail_connect(struct ssl_connect_data *connssl,
|
|
|
|
struct SessionHandle *data,
|
|
|
|
CURLcode curlerr)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2014-04-17 07:12:59 -04:00
|
|
|
SSLVersionRange sslver;
|
2012-09-11 03:49:23 -04:00
|
|
|
PRErrorCode err = 0;
|
2014-04-17 07:12:59 -04:00
|
|
|
|
|
|
|
/* reset the flag to avoid an infinite loop */
|
|
|
|
data->state.ssl_connect_retry = FALSE;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
Curl_llist_destroy(connssl->obj_list, NULL);
|
|
|
|
connssl->obj_list = NULL;
|
|
|
|
|
2014-07-02 11:37:43 -04:00
|
|
|
if(connssl->handle
|
|
|
|
&& (SSL_VersionRangeGet(connssl->handle, &sslver) == SECSuccess)
|
2014-04-17 07:12:59 -04:00
|
|
|
&& (sslver.min == SSL_LIBRARY_VERSION_3_0)
|
2014-07-02 11:49:37 -04:00
|
|
|
&& (sslver.max != SSL_LIBRARY_VERSION_3_0)
|
2014-04-17 07:12:59 -04:00
|
|
|
&& isTLSIntoleranceError(err)) {
|
|
|
|
/* schedule reconnect through Curl_retry_request() */
|
|
|
|
data->state.ssl_connect_retry = TRUE;
|
|
|
|
infof(data, "Error in TLS handshake, trying SSLv3...\n");
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return curlerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch the SSL socket into non-blocking mode. */
|
|
|
|
static CURLcode nss_set_nonblock(struct ssl_connect_data *connssl,
|
|
|
|
struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
static PRSocketOptionData sock_opt;
|
|
|
|
sock_opt.option = PR_SockOpt_Nonblocking;
|
|
|
|
sock_opt.value.non_blocking = PR_TRUE;
|
|
|
|
|
|
|
|
if(PR_SetSocketOption(connssl->handle, &sock_opt) != PR_SUCCESS)
|
|
|
|
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;
|
2007-02-12 17:32:37 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2011-02-22 07:13:53 -05:00
|
|
|
CURLcode curlerr;
|
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 */
|
2013-12-02 10:09:12 -05:00
|
|
|
SSL_LIBRARY_VERSION_TLS_1_0 /* max */
|
|
|
|
};
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
#if defined(SSL_ENABLE_NPN) || defined(SSL_ENABLE_ALPN)
|
|
|
|
unsigned int alpn_protos_len = NGHTTP2_PROTO_VERSION_ID_LEN +
|
|
|
|
ALPN_HTTP_1_1_LENGTH + 2;
|
|
|
|
unsigned char alpn_protos[NGHTTP2_PROTO_VERSION_ID_LEN + ALPN_HTTP_1_1_LENGTH
|
|
|
|
+ 2];
|
|
|
|
int cur = 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 02:38:55 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(connssl->state == ssl_connection_complete)
|
2008-06-18 18:01:55 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2009-07-20 17:50:21 -04:00
|
|
|
connssl->data = data;
|
|
|
|
|
2011-01-27 04:55:02 -05:00
|
|
|
/* list of all NSS objects we need to destroy in Curl_nss_close() */
|
|
|
|
connssl->obj_list = Curl_llist_alloc(nss_destroy_object);
|
|
|
|
if(!connssl->obj_list)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
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);
|
2011-08-15 07:48:45 -04:00
|
|
|
curlerr = nss_init(conn->data);
|
2010-06-26 21:38:01 -04:00
|
|
|
if(CURLE_OK != curlerr) {
|
|
|
|
PR_Unlock(nss_initlock);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
curlerr = CURLE_SSL_CONNECT_ERROR;
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
if(!mod) {
|
|
|
|
char *configstring = aprintf("library=%s name=PEM", pem_library);
|
|
|
|
if(!configstring) {
|
|
|
|
PR_Unlock(nss_initlock);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
mod = SECMOD_LoadUserModule(configstring, NULL, PR_FALSE);
|
|
|
|
free(configstring);
|
2008-11-15 18:43:10 -05:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
if(!mod || !mod->loaded) {
|
|
|
|
if(mod) {
|
|
|
|
SECMOD_DestroyModule(mod);
|
|
|
|
mod = NULL;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
2010-06-26 21:38:01 -04:00
|
|
|
infof(data, "WARNING: failed to load NSS PEM library %s. Using "
|
|
|
|
"OpenSSL PEM certificates will not work.\n", pem_library);
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
2010-06-26 21:38:01 -04:00
|
|
|
}
|
2009-07-20 17:50:21 -04:00
|
|
|
|
2010-06-26 21:38:01 -04:00
|
|
|
PK11_SetPasswordFunc(nss_get_password);
|
2008-09-23 06:27:04 -04:00
|
|
|
PR_Unlock(nss_initlock);
|
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 */
|
|
|
|
ssl_no_cache = (conn->ssl_config.sessionid && data->set.ssl.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) */
|
|
|
|
if(nss_init_sslver(&sslver, data) != CURLE_OK)
|
2013-09-19 09:17:13 -04:00
|
|
|
goto error;
|
2013-11-25 10:03:52 -05:00
|
|
|
if(SSL_VersionRangeSet(model, &sslver) != SECSuccess)
|
2008-02-19 18:10:07 -05:00
|
|
|
goto error;
|
|
|
|
|
2012-02-08 07:36:36 -05:00
|
|
|
ssl_cbc_random_iv = !data->set.ssl_enable_beast;
|
|
|
|
#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
|
|
|
|
|
2009-11-12 06:16:31 -05:00
|
|
|
/* reset the flag to avoid an infinite loop */
|
|
|
|
data->state.ssl_connect_retry = FALSE;
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
if(data->set.ssl.cipher_list) {
|
2007-09-18 18:21:54 -04:00
|
|
|
if(set_ciphers(data, model, data->set.ssl.cipher_list) != SECSuccess) {
|
|
|
|
curlerr = 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
|
|
|
}
|
|
|
|
|
2011-03-15 09:52:26 -04:00
|
|
|
if(!data->set.ssl.verifypeer && data->set.ssl.verifyhost)
|
|
|
|
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;
|
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
data->set.ssl.certverifyresult=0; /* not checked yet */
|
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;
|
|
|
|
|
2011-02-22 07:13:53 -05:00
|
|
|
if(data->set.ssl.verifypeer) {
|
|
|
|
const CURLcode rv = nss_load_ca_certificates(conn, sockindex);
|
|
|
|
if(CURLE_OK != rv) {
|
|
|
|
curlerr = rv;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(data->set.ssl.CRLfile) {
|
2014-07-04 06:41:53 -04:00
|
|
|
const CURLcode rv = nss_load_crl(data->set.ssl.CRLfile);
|
2014-07-03 17:53:44 -04:00
|
|
|
if(CURLE_OK != rv) {
|
|
|
|
curlerr = rv;
|
2008-06-06 14:40:21 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2014-07-03 17:53:44 -04:00
|
|
|
infof(data, " CRLfile: %s\n", data->set.ssl.CRLfile);
|
2008-06-06 14:40:21 -04:00
|
|
|
}
|
|
|
|
|
2007-08-01 17:20:01 -04:00
|
|
|
if(data->set.str[STRING_CERT]) {
|
2011-08-26 08:53:26 -04:00
|
|
|
char *nickname = dup_nickname(data, STRING_CERT);
|
2011-08-26 09:43:48 -04:00
|
|
|
if(nickname) {
|
|
|
|
/* we are not going to use libnsspem.so to read the client cert */
|
|
|
|
connssl->obj_clicert = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CURLcode rv = cert_stuff(conn, sockindex, data->set.str[STRING_CERT],
|
|
|
|
data->set.str[STRING_KEY]);
|
|
|
|
if(CURLE_OK != rv) {
|
|
|
|
/* failf() is already done in cert_stuff() */
|
|
|
|
curlerr = rv;
|
|
|
|
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 */
|
|
|
|
connssl->client_nickname = nickname;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2007-09-18 18:21:54 -04:00
|
|
|
else
|
|
|
|
connssl->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) {
|
|
|
|
curlerr = CURLE_SSL_CERTPROBLEM;
|
|
|
|
goto error;
|
|
|
|
}
|
2008-06-06 16:52:32 -04:00
|
|
|
|
2014-04-23 09:37:26 -04:00
|
|
|
/* wrap OS file descriptor by NSPR's file descriptor abstraction */
|
|
|
|
nspr_io = PR_ImportTCPSocket(sockfd);
|
|
|
|
if(!nspr_io)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* create our own NSPR I/O layer */
|
|
|
|
nspr_io_stub = PR_CreateIOLayerStub(nspr_io_identity, &nspr_io_methods);
|
|
|
|
if(!nspr_io_stub) {
|
|
|
|
PR_Close(nspr_io);
|
|
|
|
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) {
|
|
|
|
PR_Close(nspr_io);
|
|
|
|
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 */
|
|
|
|
connssl->handle = SSL_ImportFD(model, nspr_io);
|
|
|
|
if(!connssl->handle) {
|
|
|
|
PR_Close(nspr_io);
|
|
|
|
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 */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(data->set.str[STRING_KEY_PASSWD]) {
|
2010-02-17 07:13:55 -05:00
|
|
|
SSL_SetPKCS11PinArg(connssl->handle, data->set.str[STRING_KEY_PASSWD]);
|
2009-07-20 17:50:21 -04:00
|
|
|
}
|
|
|
|
|
2014-02-07 04:48:34 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
2014-02-10 02:38:55 -05:00
|
|
|
if(data->set.httpversion == CURL_HTTP_VERSION_2_0) {
|
2014-02-07 04:48:34 -05:00
|
|
|
#ifdef SSL_ENABLE_NPN
|
2014-02-10 02:38:55 -05:00
|
|
|
if(data->set.ssl_enable_npn) {
|
|
|
|
if(SSL_OptionSet(connssl->handle, SSL_ENABLE_NPN, PR_TRUE) != SECSuccess)
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_ENABLE_ALPN
|
2014-02-10 02:38:55 -05:00
|
|
|
if(data->set.ssl_enable_alpn) {
|
|
|
|
if(SSL_OptionSet(connssl->handle, SSL_ENABLE_ALPN, PR_TRUE)
|
|
|
|
!= SECSuccess)
|
|
|
|
goto error;
|
|
|
|
}
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SSL_ENABLE_NPN) || defined(SSL_ENABLE_ALPN)
|
2014-02-10 02:38:55 -05:00
|
|
|
if(data->set.ssl_enable_npn || data->set.ssl_enable_alpn) {
|
|
|
|
alpn_protos[cur] = NGHTTP2_PROTO_VERSION_ID_LEN;
|
|
|
|
cur++;
|
|
|
|
memcpy(&alpn_protos[cur], NGHTTP2_PROTO_VERSION_ID,
|
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN);
|
|
|
|
cur += NGHTTP2_PROTO_VERSION_ID_LEN;
|
|
|
|
alpn_protos[cur] = ALPN_HTTP_1_1_LENGTH;
|
|
|
|
cur++;
|
|
|
|
memcpy(&alpn_protos[cur], ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH);
|
|
|
|
|
|
|
|
if(SSL_SetNextProtoNego(connssl->handle, alpn_protos, alpn_protos_len)
|
|
|
|
!= SECSuccess)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "SSL, can't negotiate HTTP/2.0 with neither NPN nor ALPN\n");
|
|
|
|
}
|
2014-02-07 04:48:34 -05:00
|
|
|
#endif
|
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 */
|
|
|
|
SSL_ResetHandshake(connssl->handle, /* asServer */ PR_FALSE);
|
|
|
|
|
|
|
|
SSL_SetURL(connssl->handle, conn->host.name);
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if(model)
|
|
|
|
PR_Close(model);
|
|
|
|
|
|
|
|
return nss_fail_connect(connssl, data, curlerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
CURLcode curlerr = CURLE_SSL_CONNECT_ERROR;
|
|
|
|
PRUint32 timeout;
|
|
|
|
|
2010-04-24 17:21:13 -04:00
|
|
|
/* check timeout situation */
|
2014-04-17 07:12:59 -04:00
|
|
|
const long time_left = Curl_timeleft(data, NULL, TRUE);
|
2010-04-24 17:21:13 -04:00
|
|
|
if(time_left < 0L) {
|
|
|
|
failf(data, "timed out before SSL handshake");
|
2012-08-09 03:40:00 -04:00
|
|
|
curlerr = 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);
|
2009-11-05 10:41:31 -05:00
|
|
|
if(SSL_ForceHandshakeWithTimeout(connssl->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;
|
|
|
|
else if(conn->data->set.ssl.certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
|
2009-08-13 12:04:51 -04:00
|
|
|
curlerr = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
else if(conn->data->set.ssl.certverifyresult!=0)
|
2007-09-18 18:21:54 -04:00
|
|
|
curlerr = CURLE_SSL_CACERT;
|
2007-05-25 17:56:27 -04:00
|
|
|
goto error;
|
2007-09-18 18:21:54 -04:00
|
|
|
}
|
|
|
|
|
2008-02-20 04:56:26 -05:00
|
|
|
connssl->state = ssl_connection_complete;
|
2010-05-11 16:48:38 -04:00
|
|
|
conn->recv[sockindex] = nss_recv;
|
|
|
|
conn->send[sockindex] = nss_send;
|
2008-02-20 04:56:26 -05:00
|
|
|
|
2007-09-18 18:21:54 -04:00
|
|
|
display_conn_info(conn, connssl->handle);
|
2007-05-25 17:56:27 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(data->set.str[STRING_SSL_ISSUERCERT]) {
|
2011-01-04 07:52:54 -05:00
|
|
|
SECStatus ret = SECFailure;
|
2011-08-26 08:53:26 -04:00
|
|
|
char *nickname = dup_nickname(data, STRING_SSL_ISSUERCERT);
|
|
|
|
if(nickname) {
|
2011-01-04 07:52:54 -05:00
|
|
|
/* we support only nicknames in case of STRING_SSL_ISSUERCERT for now */
|
|
|
|
ret = check_issuer_cert(connssl->handle, nickname);
|
2011-08-26 08:53:26 -04:00
|
|
|
free(nickname);
|
|
|
|
}
|
2008-11-15 18:43:10 -05:00
|
|
|
|
|
|
|
if(SECFailure == ret) {
|
|
|
|
infof(data,"SSL certificate issuer check failed\n");
|
2008-06-06 16:52:32 -04:00
|
|
|
curlerr = CURLE_SSL_ISSUER_ERROR;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
error:
|
|
|
|
return nss_fail_connect(connssl, data, curlerr);
|
|
|
|
}
|
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];
|
|
|
|
struct SessionHandle *data = conn->data;
|
2014-04-17 07:27:39 -04:00
|
|
|
const bool blocking = (done == NULL);
|
2014-04-17 07:12:59 -04:00
|
|
|
CURLcode rv;
|
2012-05-21 10:19:12 -04:00
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
if(connssl->connecting_state == ssl_connect_1) {
|
|
|
|
rv = nss_setup_connect(conn, sockindex);
|
|
|
|
if(rv)
|
|
|
|
/* we do not expect CURLE_AGAIN from nss_setup_connect() */
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if(!blocking) {
|
|
|
|
/* in non-blocking mode, set NSS non-blocking mode before handshake */
|
|
|
|
rv = nss_set_nonblock(connssl, data);
|
|
|
|
if(rv)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
}
|
2012-05-21 10:19:12 -04:00
|
|
|
|
2014-04-17 07:12:59 -04:00
|
|
|
rv = nss_do_connect(conn, sockindex);
|
|
|
|
switch(rv) {
|
|
|
|
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;
|
|
|
|
/* fall through */
|
2014-04-17 07:12:59 -04:00
|
|
|
default:
|
|
|
|
return rv;
|
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 */
|
|
|
|
rv = nss_set_nonblock(connssl, data);
|
|
|
|
if(rv)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* signal completed SSL handshake */
|
|
|
|
*done = TRUE;
|
2009-11-12 06:16:31 -05:00
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
connssl->connecting_state = ssl_connect_done;
|
2014-04-17 07:12:59 -04:00
|
|
|
return CURLE_OK;
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 07:27:39 -04:00
|
|
|
CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
return nss_connect_common(conn, sockindex, /* blocking */ NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_nss_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex, bool *done)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2013-05-07 17:30:52 -04:00
|
|
|
ssize_t rc = PR_Send(conn->ssl[sockindex].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
|
|
|
}
|
2007-02-12 17:32:37 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return rc; /* number of bytes */
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t nss_recv(struct connectdata * conn, /* connection data */
|
|
|
|
int num, /* socketindex */
|
|
|
|
char *buf, /* store read data here */
|
|
|
|
size_t buffersize, /* max amount to read */
|
|
|
|
CURLcode *curlcode)
|
2007-02-12 17:32:37 -05:00
|
|
|
{
|
2013-05-07 17:30:52 -04:00
|
|
|
ssize_t nread = PR_Recv(conn->ssl[num].handle, buf, (int)buffersize, 0,
|
|
|
|
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
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_nss_version(char *buffer, size_t size)
|
|
|
|
{
|
2007-08-24 05:06:17 -04:00
|
|
|
return snprintf(buffer, size, "NSS/%s", NSS_VERSION);
|
2007-02-12 17:32:37 -05:00
|
|
|
}
|
2010-06-26 21:38:01 -04:00
|
|
|
|
|
|
|
int Curl_nss_seed(struct SessionHandle *data)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-06-26 08:52:46 -04:00
|
|
|
void Curl_nss_random(struct SessionHandle *data,
|
|
|
|
unsigned char *entropy,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
Curl_nss_seed(data); /* Initiate the seed if not already done */
|
2013-08-12 09:14:35 -04:00
|
|
|
if(SECSuccess != PK11_GenerateRandom(entropy, curlx_uztosi(length))) {
|
|
|
|
/* no way to signal a failure from here, we have to abort */
|
|
|
|
failf(data, "PK11_GenerateRandom() failed, calling abort()...");
|
|
|
|
abort();
|
|
|
|
}
|
2012-06-26 08:52:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_nss_md5sum(unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *md5sum, /* output */
|
|
|
|
size_t md5len)
|
|
|
|
{
|
|
|
|
PK11Context *MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
|
|
|
|
unsigned int MD5out;
|
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);
|
|
|
|
}
|
|
|
|
|
2007-02-12 17:32:37 -05:00
|
|
|
#endif /* USE_NSS */
|