00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "curl_setup.h"
00029
00030 #ifdef USE_NSS
00031
00032 #include "urldata.h"
00033 #include "sendf.h"
00034 #include "formdata.h"
00035 #include "url.h"
00036 #include "connect.h"
00037 #include "strcase.h"
00038 #include "select.h"
00039 #include "vtls.h"
00040 #include "llist.h"
00041 #include "curl_printf.h"
00042 #include "nssg.h"
00043 #include <nspr.h>
00044 #include <nss.h>
00045 #include <ssl.h>
00046 #include <sslerr.h>
00047 #include <secerr.h>
00048 #include <secmod.h>
00049 #include <sslproto.h>
00050 #include <prtypes.h>
00051 #include <pk11pub.h>
00052 #include <prio.h>
00053 #include <secitem.h>
00054 #include <secport.h>
00055 #include <certdb.h>
00056 #include <base64.h>
00057 #include <cert.h>
00058 #include <prerror.h>
00059 #include <keyhi.h>
00060
00061 #define NSSVERNUM ((NSS_VMAJOR<<16)|(NSS_VMINOR<<8)|NSS_VPATCH)
00062
00063 #if NSSVERNUM >= 0x030f00
00064 #include <ocsp.h>
00065 #endif
00066
00067 #include "strcase.h"
00068 #include "warnless.h"
00069 #include "x509asn1.h"
00070
00071
00072 #include "curl_memory.h"
00073 #include "memdebug.h"
00074
00075 #define SSL_DIR "/etc/pki/nssdb"
00076
00077
00078 #define SLOTSIZE 13
00079
00080 PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
00081 static PRLock *nss_initlock = NULL;
00082 static PRLock *nss_crllock = NULL;
00083 static PRLock *nss_findslot_lock = NULL;
00084 static struct curl_llist *nss_crl_list = NULL;
00085 static NSSInitContext *nss_context = NULL;
00086 static volatile int initialized = 0;
00087
00088 typedef struct {
00089 const char *name;
00090 int num;
00091 } cipher_s;
00092
00093 #define PK11_SETATTRS(_attr, _idx, _type, _val, _len) do { \
00094 CK_ATTRIBUTE *ptr = (_attr) + ((_idx)++); \
00095 ptr->type = (_type); \
00096 ptr->pValue = (_val); \
00097 ptr->ulValueLen = (_len); \
00098 } WHILE_FALSE
00099
00100 #define CERT_NewTempCertificate __CERT_NewTempCertificate
00101
00102 #define NUM_OF_CIPHERS sizeof(cipherlist)/sizeof(cipherlist[0])
00103 static const cipher_s cipherlist[] = {
00104
00105 {"rc4", SSL_EN_RC4_128_WITH_MD5},
00106 {"rc4-md5", SSL_EN_RC4_128_WITH_MD5},
00107 {"rc4export", SSL_EN_RC4_128_EXPORT40_WITH_MD5},
00108 {"rc2", SSL_EN_RC2_128_CBC_WITH_MD5},
00109 {"rc2export", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5},
00110 {"des", SSL_EN_DES_64_CBC_WITH_MD5},
00111 {"desede3", SSL_EN_DES_192_EDE3_CBC_WITH_MD5},
00112
00113 {"rsa_rc4_128_md5", SSL_RSA_WITH_RC4_128_MD5},
00114 {"rsa_rc4_128_sha", SSL_RSA_WITH_RC4_128_SHA},
00115 {"rsa_3des_sha", SSL_RSA_WITH_3DES_EDE_CBC_SHA},
00116 {"rsa_des_sha", SSL_RSA_WITH_DES_CBC_SHA},
00117 {"rsa_rc4_40_md5", SSL_RSA_EXPORT_WITH_RC4_40_MD5},
00118 {"rsa_rc2_40_md5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5},
00119 {"rsa_null_md5", SSL_RSA_WITH_NULL_MD5},
00120 {"rsa_null_sha", SSL_RSA_WITH_NULL_SHA},
00121 {"fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA},
00122 {"fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA},
00123 {"fortezza", SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA},
00124 {"fortezza_rc4_128_sha", SSL_FORTEZZA_DMS_WITH_RC4_128_SHA},
00125 {"fortezza_null", SSL_FORTEZZA_DMS_WITH_NULL_SHA},
00126
00127 {"rsa_des_56_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA},
00128 {"rsa_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
00129
00130 {"dhe_dss_aes_128_cbc_sha", TLS_DHE_DSS_WITH_AES_128_CBC_SHA},
00131 {"dhe_dss_aes_256_cbc_sha", TLS_DHE_DSS_WITH_AES_256_CBC_SHA},
00132 {"dhe_rsa_aes_128_cbc_sha", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
00133 {"dhe_rsa_aes_256_cbc_sha", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
00134 {"rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA},
00135 {"rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA},
00136
00137 {"ecdh_ecdsa_null_sha", TLS_ECDH_ECDSA_WITH_NULL_SHA},
00138 {"ecdh_ecdsa_rc4_128_sha", TLS_ECDH_ECDSA_WITH_RC4_128_SHA},
00139 {"ecdh_ecdsa_3des_sha", TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA},
00140 {"ecdh_ecdsa_aes_128_sha", TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA},
00141 {"ecdh_ecdsa_aes_256_sha", TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA},
00142 {"ecdhe_ecdsa_null_sha", TLS_ECDHE_ECDSA_WITH_NULL_SHA},
00143 {"ecdhe_ecdsa_rc4_128_sha", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA},
00144 {"ecdhe_ecdsa_3des_sha", TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA},
00145 {"ecdhe_ecdsa_aes_128_sha", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA},
00146 {"ecdhe_ecdsa_aes_256_sha", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA},
00147 {"ecdh_rsa_null_sha", TLS_ECDH_RSA_WITH_NULL_SHA},
00148 {"ecdh_rsa_128_sha", TLS_ECDH_RSA_WITH_RC4_128_SHA},
00149 {"ecdh_rsa_3des_sha", TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA},
00150 {"ecdh_rsa_aes_128_sha", TLS_ECDH_RSA_WITH_AES_128_CBC_SHA},
00151 {"ecdh_rsa_aes_256_sha", TLS_ECDH_RSA_WITH_AES_256_CBC_SHA},
00152 {"ecdhe_rsa_null", TLS_ECDHE_RSA_WITH_NULL_SHA},
00153 {"ecdhe_rsa_rc4_128_sha", TLS_ECDHE_RSA_WITH_RC4_128_SHA},
00154 {"ecdhe_rsa_3des_sha", TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
00155 {"ecdhe_rsa_aes_128_sha", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA},
00156 {"ecdhe_rsa_aes_256_sha", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA},
00157 {"ecdh_anon_null_sha", TLS_ECDH_anon_WITH_NULL_SHA},
00158 {"ecdh_anon_rc4_128sha", TLS_ECDH_anon_WITH_RC4_128_SHA},
00159 {"ecdh_anon_3des_sha", TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA},
00160 {"ecdh_anon_aes_128_sha", TLS_ECDH_anon_WITH_AES_128_CBC_SHA},
00161 {"ecdh_anon_aes_256_sha", TLS_ECDH_anon_WITH_AES_256_CBC_SHA},
00162 #ifdef TLS_RSA_WITH_NULL_SHA256
00163
00164 {"rsa_null_sha_256", TLS_RSA_WITH_NULL_SHA256},
00165 {"rsa_aes_128_cbc_sha_256", TLS_RSA_WITH_AES_128_CBC_SHA256},
00166 {"rsa_aes_256_cbc_sha_256", TLS_RSA_WITH_AES_256_CBC_SHA256},
00167 {"dhe_rsa_aes_128_cbc_sha_256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256},
00168 {"dhe_rsa_aes_256_cbc_sha_256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256},
00169 {"ecdhe_ecdsa_aes_128_cbc_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256},
00170 {"ecdhe_rsa_aes_128_cbc_sha_256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256},
00171 #endif
00172 #ifdef TLS_RSA_WITH_AES_128_GCM_SHA256
00173
00174 {"rsa_aes_128_gcm_sha_256", TLS_RSA_WITH_AES_128_GCM_SHA256},
00175 {"dhe_rsa_aes_128_gcm_sha_256", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256},
00176 {"dhe_dss_aes_128_gcm_sha_256", TLS_DHE_DSS_WITH_AES_128_GCM_SHA256},
00177 {"ecdhe_ecdsa_aes_128_gcm_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
00178 {"ecdh_ecdsa_aes_128_gcm_sha_256", TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256},
00179 {"ecdhe_rsa_aes_128_gcm_sha_256", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
00180 {"ecdh_rsa_aes_128_gcm_sha_256", TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256},
00181 #endif
00182 #ifdef TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
00183
00184 {"rsa_aes_256_gcm_sha_384", TLS_RSA_WITH_AES_256_GCM_SHA384},
00185 {"dhe_rsa_aes_256_gcm_sha_384", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384},
00186 {"dhe_dss_aes_256_gcm_sha_384", TLS_DHE_DSS_WITH_AES_256_GCM_SHA384},
00187 {"ecdhe_ecdsa_aes_256_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384},
00188 {"ecdhe_rsa_aes_256_sha_384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384},
00189 {"ecdhe_ecdsa_aes_256_gcm_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
00190 {"ecdhe_rsa_aes_256_gcm_sha_384", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
00191 #endif
00192 #ifdef TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
00193
00194 {"ecdhe_rsa_chacha20_poly1305_sha_256",
00195 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
00196 {"ecdhe_ecdsa_chacha20_poly1305_sha_256",
00197 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256},
00198 {"dhe_rsa_chacha20_poly1305_sha_256",
00199 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256},
00200 #endif
00201 };
00202
00203 static const char *pem_library = "libnsspem.so";
00204 static SECMODModule *mod = NULL;
00205
00206
00207 static PRDescIdentity nspr_io_identity = PR_INVALID_IO_LAYER;
00208 static PRIOMethods nspr_io_methods;
00209
00210 static const char *nss_error_to_name(PRErrorCode code)
00211 {
00212 const char *name = PR_ErrorToName(code);
00213 if(name)
00214 return name;
00215
00216 return "unknown error";
00217 }
00218
00219 static void nss_print_error_message(struct Curl_easy *data, PRUint32 err)
00220 {
00221 failf(data, "%s", PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT));
00222 }
00223
00224 static SECStatus set_ciphers(struct Curl_easy *data, PRFileDesc * model,
00225 char *cipher_list)
00226 {
00227 unsigned int i;
00228 PRBool cipher_state[NUM_OF_CIPHERS];
00229 PRBool found;
00230 char *cipher;
00231
00232
00233 const PRUint16 num_implemented_ciphers = SSL_GetNumImplementedCiphers();
00234 const PRUint16 *implemented_ciphers = SSL_GetImplementedCiphers();
00235 if(!implemented_ciphers)
00236 return SECFailure;
00237
00238
00239
00240
00241
00242 for(i = 0; i < num_implemented_ciphers; i++) {
00243 SSL_CipherPrefSet(model, implemented_ciphers[i], PR_FALSE);
00244 }
00245
00246
00247 for(i = 0; i < NUM_OF_CIPHERS; i++) {
00248 cipher_state[i] = PR_FALSE;
00249 }
00250
00251 cipher = cipher_list;
00252
00253 while(cipher_list && (cipher_list[0])) {
00254 while((*cipher) && (ISSPACE(*cipher)))
00255 ++cipher;
00256
00257 cipher_list = strchr(cipher, ',');
00258 if(cipher_list) {
00259 *cipher_list++ = '\0';
00260 }
00261
00262 found = PR_FALSE;
00263
00264 for(i=0; i<NUM_OF_CIPHERS; i++) {
00265 if(strcasecompare(cipher, cipherlist[i].name)) {
00266 cipher_state[i] = PR_TRUE;
00267 found = PR_TRUE;
00268 break;
00269 }
00270 }
00271
00272 if(found == PR_FALSE) {
00273 failf(data, "Unknown cipher in list: %s", cipher);
00274 return SECFailure;
00275 }
00276
00277 if(cipher_list) {
00278 cipher = cipher_list;
00279 }
00280 }
00281
00282
00283 for(i=0; i<NUM_OF_CIPHERS; i++) {
00284 if(!cipher_state[i])
00285 continue;
00286
00287 if(SSL_CipherPrefSet(model, cipherlist[i].num, PR_TRUE) != SECSuccess) {
00288 failf(data, "cipher-suite not supported by NSS: %s", cipherlist[i].name);
00289 return SECFailure;
00290 }
00291 }
00292
00293 return SECSuccess;
00294 }
00295
00296
00297
00298
00299
00300 static bool any_cipher_enabled(void)
00301 {
00302 unsigned int i;
00303
00304 for(i=0; i<NUM_OF_CIPHERS; i++) {
00305 PRInt32 policy = 0;
00306 SSL_CipherPolicyGet(cipherlist[i].num, &policy);
00307 if(policy)
00308 return TRUE;
00309 }
00310
00311 return FALSE;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321 static int is_file(const char *filename)
00322 {
00323 struct_stat st;
00324
00325 if(filename == NULL)
00326 return 0;
00327
00328 if(stat(filename, &st) == 0)
00329 if(S_ISREG(st.st_mode))
00330 return 1;
00331
00332 return 0;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341 static char *dup_nickname(struct Curl_easy *data, const char *str)
00342 {
00343 const char *n;
00344
00345 if(!is_file(str))
00346
00347 return strdup(str);
00348
00349
00350 n = strchr(str, '/');
00351 if(!n) {
00352 infof(data, "warning: certificate file name \"%s\" handled as nickname; "
00353 "please use \"./%s\" to force file name\n", str, str);
00354 return strdup(str);
00355 }
00356
00357
00358 return NULL;
00359 }
00360
00361
00362
00363
00364
00365 static PK11SlotInfo* nss_find_slot_by_name(const char *slot_name)
00366 {
00367 PK11SlotInfo *slot;
00368 PR_Lock(nss_initlock);
00369 slot = PK11_FindSlotByName(slot_name);
00370 PR_Unlock(nss_initlock);
00371 return slot;
00372 }
00373
00374
00375
00376
00377 static CURLcode nss_create_object(struct ssl_connect_data *ssl,
00378 CK_OBJECT_CLASS obj_class,
00379 const char *filename, bool cacert)
00380 {
00381 PK11SlotInfo *slot;
00382 PK11GenericObject *obj;
00383 CK_BBOOL cktrue = CK_TRUE;
00384 CK_BBOOL ckfalse = CK_FALSE;
00385 CK_ATTRIBUTE attrs[ 4];
00386 int attr_cnt = 0;
00387 CURLcode result = (cacert)
00388 ? CURLE_SSL_CACERT_BADFILE
00389 : CURLE_SSL_CERTPROBLEM;
00390
00391 const int slot_id = (cacert) ? 0 : 1;
00392 char *slot_name = aprintf("PEM Token #%d", slot_id);
00393 if(!slot_name)
00394 return CURLE_OUT_OF_MEMORY;
00395
00396 slot = nss_find_slot_by_name(slot_name);
00397 free(slot_name);
00398 if(!slot)
00399 return result;
00400
00401 PK11_SETATTRS(attrs, attr_cnt, CKA_CLASS, &obj_class, sizeof(obj_class));
00402 PK11_SETATTRS(attrs, attr_cnt, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL));
00403 PK11_SETATTRS(attrs, attr_cnt, CKA_LABEL, (unsigned char *)filename,
00404 strlen(filename) + 1);
00405
00406 if(CKO_CERTIFICATE == obj_class) {
00407 CK_BBOOL *pval = (cacert) ? (&cktrue) : (&ckfalse);
00408 PK11_SETATTRS(attrs, attr_cnt, CKA_TRUST, pval, sizeof(*pval));
00409 }
00410
00411 obj = PK11_CreateGenericObject(slot, attrs, attr_cnt, PR_FALSE);
00412 PK11_FreeSlot(slot);
00413 if(!obj)
00414 return result;
00415
00416 if(!Curl_llist_insert_next(ssl->obj_list, ssl->obj_list->tail, obj)) {
00417 PK11_DestroyGenericObject(obj);
00418 return CURLE_OUT_OF_MEMORY;
00419 }
00420
00421 if(!cacert && CKO_CERTIFICATE == obj_class)
00422
00423 ssl->obj_clicert = obj;
00424
00425 return CURLE_OK;
00426 }
00427
00428
00429
00430
00431 static void nss_destroy_object(void *user, void *ptr)
00432 {
00433 PK11GenericObject *obj = (PK11GenericObject *)ptr;
00434 (void) user;
00435 PK11_DestroyGenericObject(obj);
00436 }
00437
00438
00439 static void nss_destroy_crl_item(void *user, void *ptr)
00440 {
00441 SECItem *crl_der = (SECItem *)ptr;
00442 (void) user;
00443 SECITEM_FreeItem(crl_der, PR_TRUE);
00444 }
00445
00446 static CURLcode nss_load_cert(struct ssl_connect_data *ssl,
00447 const char *filename, PRBool cacert)
00448 {
00449 CURLcode result = (cacert)
00450 ? CURLE_SSL_CACERT_BADFILE
00451 : CURLE_SSL_CERTPROBLEM;
00452
00453
00454
00455 if(is_file(filename))
00456 result = nss_create_object(ssl, CKO_CERTIFICATE, filename, cacert);
00457
00458 if(!result && !cacert) {
00459
00460 CERTCertificate *cert;
00461 char *nickname = NULL;
00462 char *n = strrchr(filename, '/');
00463 if(n)
00464 n++;
00465
00466
00467
00468
00469
00470 nickname = aprintf("PEM Token #1:%s", n);
00471 if(nickname) {
00472 cert = PK11_FindCertFromNickname(nickname, NULL);
00473 if(cert)
00474 CERT_DestroyCertificate(cert);
00475
00476 free(nickname);
00477 }
00478 }
00479
00480 return result;
00481 }
00482
00483
00484 static CURLcode nss_cache_crl(SECItem *crl_der)
00485 {
00486 CERTCertDBHandle *db = CERT_GetDefaultCertDB();
00487 CERTSignedCrl *crl = SEC_FindCrlByDERCert(db, crl_der, 0);
00488 if(crl) {
00489
00490 SEC_DestroyCrl(crl);
00491 SECITEM_FreeItem(crl_der, PR_TRUE);
00492 return CURLE_OK;
00493 }
00494
00495
00496 PR_Lock(nss_crllock);
00497
00498
00499 if(!Curl_llist_insert_next(nss_crl_list, nss_crl_list->tail, crl_der)) {
00500 SECITEM_FreeItem(crl_der, PR_TRUE);
00501 PR_Unlock(nss_crllock);
00502 return CURLE_OUT_OF_MEMORY;
00503 }
00504
00505 if(SECSuccess != CERT_CacheCRL(db, crl_der)) {
00506
00507 PR_Unlock(nss_crllock);
00508 return CURLE_SSL_CRL_BADFILE;
00509 }
00510
00511
00512 SSL_ClearSessionCache();
00513 PR_Unlock(nss_crllock);
00514 return CURLE_OK;
00515 }
00516
00517 static CURLcode nss_load_crl(const char *crlfilename)
00518 {
00519 PRFileDesc *infile;
00520 PRFileInfo info;
00521 SECItem filedata = { 0, NULL, 0 };
00522 SECItem *crl_der = NULL;
00523 char *body;
00524
00525 infile = PR_Open(crlfilename, PR_RDONLY, 0);
00526 if(!infile)
00527 return CURLE_SSL_CRL_BADFILE;
00528
00529 if(PR_SUCCESS != PR_GetOpenFileInfo(infile, &info))
00530 goto fail;
00531
00532 if(!SECITEM_AllocItem(NULL, &filedata, info.size + 1))
00533 goto fail;
00534
00535 if(info.size != PR_Read(infile, filedata.data, info.size))
00536 goto fail;
00537
00538 crl_der = SECITEM_AllocItem(NULL, NULL, 0U);
00539 if(!crl_der)
00540 goto fail;
00541
00542
00543 body = (char *)filedata.data;
00544 body[--filedata.len] = '\0';
00545
00546 body = strstr(body, "-----BEGIN");
00547 if(body) {
00548
00549 char *trailer;
00550 char *begin = PORT_Strchr(body, '\n');
00551 if(!begin)
00552 begin = PORT_Strchr(body, '\r');
00553 if(!begin)
00554 goto fail;
00555
00556 trailer = strstr(++begin, "-----END");
00557 if(!trailer)
00558 goto fail;
00559
00560
00561 *trailer = '\0';
00562 if(ATOB_ConvertAsciiToItem(crl_der, begin))
00563 goto fail;
00564
00565 SECITEM_FreeItem(&filedata, PR_FALSE);
00566 }
00567 else
00568
00569 *crl_der = filedata;
00570
00571 PR_Close(infile);
00572 return nss_cache_crl(crl_der);
00573
00574 fail:
00575 PR_Close(infile);
00576 SECITEM_FreeItem(crl_der, PR_TRUE);
00577 SECITEM_FreeItem(&filedata, PR_FALSE);
00578 return CURLE_SSL_CRL_BADFILE;
00579 }
00580
00581 static CURLcode nss_load_key(struct connectdata *conn, int sockindex,
00582 char *key_file)
00583 {
00584 PK11SlotInfo *slot;
00585 SECStatus status;
00586 CURLcode result;
00587 struct ssl_connect_data *ssl = conn->ssl;
00588 struct Curl_easy *data = conn->data;
00589
00590 (void)sockindex;
00591
00592 result = nss_create_object(ssl, CKO_PRIVATE_KEY, key_file, FALSE);
00593 if(result) {
00594 PR_SetError(SEC_ERROR_BAD_KEY, 0);
00595 return result;
00596 }
00597
00598 slot = nss_find_slot_by_name("PEM Token #1");
00599 if(!slot)
00600 return CURLE_SSL_CERTPROBLEM;
00601
00602
00603 SECMOD_WaitForAnyTokenEvent(mod, 0, 0);
00604 PK11_IsPresent(slot);
00605
00606 status = PK11_Authenticate(slot, PR_TRUE, SSL_SET_OPTION(key_passwd));
00607 PK11_FreeSlot(slot);
00608
00609 return (SECSuccess == status) ? CURLE_OK : CURLE_SSL_CERTPROBLEM;
00610 }
00611
00612 static int display_error(struct connectdata *conn, PRInt32 err,
00613 const char *filename)
00614 {
00615 switch(err) {
00616 case SEC_ERROR_BAD_PASSWORD:
00617 failf(conn->data, "Unable to load client key: Incorrect password");
00618 return 1;
00619 case SEC_ERROR_UNKNOWN_CERT:
00620 failf(conn->data, "Unable to load certificate %s", filename);
00621 return 1;
00622 default:
00623 break;
00624 }
00625 return 0;
00626 }
00627
00628 static CURLcode cert_stuff(struct connectdata *conn, int sockindex,
00629 char *cert_file, char *key_file)
00630 {
00631 struct Curl_easy *data = conn->data;
00632 CURLcode result;
00633
00634 if(cert_file) {
00635 result = nss_load_cert(&conn->ssl[sockindex], cert_file, PR_FALSE);
00636 if(result) {
00637 const PRErrorCode err = PR_GetError();
00638 if(!display_error(conn, err, cert_file)) {
00639 const char *err_name = nss_error_to_name(err);
00640 failf(data, "unable to load client cert: %d (%s)", err, err_name);
00641 }
00642
00643 return result;
00644 }
00645 }
00646
00647 if(key_file || (is_file(cert_file))) {
00648 if(key_file)
00649 result = nss_load_key(conn, sockindex, key_file);
00650 else
00651
00652 result = nss_load_key(conn, sockindex, cert_file);
00653 if(result) {
00654 const PRErrorCode err = PR_GetError();
00655 if(!display_error(conn, err, key_file)) {
00656 const char *err_name = nss_error_to_name(err);
00657 failf(data, "unable to load client key: %d (%s)", err, err_name);
00658 }
00659
00660 return result;
00661 }
00662 }
00663
00664 return CURLE_OK;
00665 }
00666
00667 static char *nss_get_password(PK11SlotInfo *slot, PRBool retry, void *arg)
00668 {
00669 (void)slot;
00670
00671 if(retry || NULL == arg)
00672 return NULL;
00673 else
00674 return (char *)PORT_Strdup((char *)arg);
00675 }
00676
00677
00678
00679 static SECStatus nss_auth_cert_hook(void *arg, PRFileDesc *fd, PRBool checksig,
00680 PRBool isServer)
00681 {
00682 struct connectdata *conn = (struct connectdata *)arg;
00683
00684 #ifdef SSL_ENABLE_OCSP_STAPLING
00685 if(SSL_CONN_CONFIG(verifystatus)) {
00686 SECStatus cacheResult;
00687
00688 const SECItemArray *csa = SSL_PeerStapledOCSPResponses(fd);
00689 if(!csa) {
00690 failf(conn->data, "Invalid OCSP response");
00691 return SECFailure;
00692 }
00693
00694 if(csa->len == 0) {
00695 failf(conn->data, "No OCSP response received");
00696 return SECFailure;
00697 }
00698
00699 cacheResult = CERT_CacheOCSPResponseFromSideChannel(
00700 CERT_GetDefaultCertDB(), SSL_PeerCertificate(fd),
00701 PR_Now(), &csa->items[0], arg
00702 );
00703
00704 if(cacheResult != SECSuccess) {
00705 failf(conn->data, "Invalid OCSP response");
00706 return cacheResult;
00707 }
00708 }
00709 #endif
00710
00711 if(!SSL_CONN_CONFIG(verifypeer)) {
00712 infof(conn->data, "skipping SSL peer certificate verification\n");
00713 return SECSuccess;
00714 }
00715
00716 return SSL_AuthCertificate(CERT_GetDefaultCertDB(), fd, checksig, isServer);
00717 }
00718
00722 static void HandshakeCallback(PRFileDesc *sock, void *arg)
00723 {
00724 struct connectdata *conn = (struct connectdata*) arg;
00725 unsigned int buflenmax = 50;
00726 unsigned char buf[50];
00727 unsigned int buflen;
00728 SSLNextProtoState state;
00729
00730 if(!conn->bits.tls_enable_npn && !conn->bits.tls_enable_alpn) {
00731 return;
00732 }
00733
00734 if(SSL_GetNextProto(sock, &state, buf, &buflen, buflenmax) == SECSuccess) {
00735
00736 switch(state) {
00737 #if NSSVERNUM >= 0x031a00
00738
00739 case SSL_NEXT_PROTO_EARLY_VALUE:
00740
00741 #endif
00742 case SSL_NEXT_PROTO_NO_SUPPORT:
00743 case SSL_NEXT_PROTO_NO_OVERLAP:
00744 infof(conn->data, "ALPN/NPN, server did not agree to a protocol\n");
00745 return;
00746 #ifdef SSL_ENABLE_ALPN
00747 case SSL_NEXT_PROTO_SELECTED:
00748 infof(conn->data, "ALPN, server accepted to use %.*s\n", buflen, buf);
00749 break;
00750 #endif
00751 case SSL_NEXT_PROTO_NEGOTIATED:
00752 infof(conn->data, "NPN, server accepted to use %.*s\n", buflen, buf);
00753 break;
00754 }
00755
00756 #ifdef USE_NGHTTP2
00757 if(buflen == NGHTTP2_PROTO_VERSION_ID_LEN &&
00758 !memcmp(NGHTTP2_PROTO_VERSION_ID, buf, NGHTTP2_PROTO_VERSION_ID_LEN)) {
00759 conn->negnpn = CURL_HTTP_VERSION_2;
00760 }
00761 else
00762 #endif
00763 if(buflen == ALPN_HTTP_1_1_LENGTH &&
00764 !memcmp(ALPN_HTTP_1_1, buf, ALPN_HTTP_1_1_LENGTH)) {
00765 conn->negnpn = CURL_HTTP_VERSION_1_1;
00766 }
00767 }
00768 }
00769
00770 #if NSSVERNUM >= 0x030f04
00771 static SECStatus CanFalseStartCallback(PRFileDesc *sock, void *client_data,
00772 PRBool *canFalseStart)
00773 {
00774 struct connectdata *conn = client_data;
00775 struct Curl_easy *data = conn->data;
00776
00777 SSLChannelInfo channelInfo;
00778 SSLCipherSuiteInfo cipherInfo;
00779
00780 SECStatus rv;
00781 PRBool negotiatedExtension;
00782
00783 *canFalseStart = PR_FALSE;
00784
00785 if(SSL_GetChannelInfo(sock, &channelInfo, sizeof(channelInfo)) != SECSuccess)
00786 return SECFailure;
00787
00788 if(SSL_GetCipherSuiteInfo(channelInfo.cipherSuite, &cipherInfo,
00789 sizeof(cipherInfo)) != SECSuccess)
00790 return SECFailure;
00791
00792
00793
00794
00795 if(channelInfo.protocolVersion != SSL_LIBRARY_VERSION_TLS_1_2)
00796 goto end;
00797
00798
00799
00800 if(cipherInfo.keaType != ssl_kea_ecdh)
00801 goto end;
00802
00803
00804
00805
00806 if(cipherInfo.symCipher != ssl_calg_aes_gcm)
00807 goto end;
00808
00809
00810
00811 rv = SSL_HandshakeNegotiatedExtension(sock, ssl_app_layer_protocol_xtn,
00812 &negotiatedExtension);
00813 if(rv != SECSuccess || !negotiatedExtension) {
00814 rv = SSL_HandshakeNegotiatedExtension(sock, ssl_next_proto_nego_xtn,
00815 &negotiatedExtension);
00816 }
00817
00818 if(rv != SECSuccess || !negotiatedExtension)
00819 goto end;
00820
00821 *canFalseStart = PR_TRUE;
00822
00823 infof(data, "Trying TLS False Start\n");
00824
00825 end:
00826 return SECSuccess;
00827 }
00828 #endif
00829
00830 static void display_cert_info(struct Curl_easy *data,
00831 CERTCertificate *cert)
00832 {
00833 char *subject, *issuer, *common_name;
00834 PRExplodedTime printableTime;
00835 char timeString[256];
00836 PRTime notBefore, notAfter;
00837
00838 subject = CERT_NameToAscii(&cert->subject);
00839 issuer = CERT_NameToAscii(&cert->issuer);
00840 common_name = CERT_GetCommonName(&cert->subject);
00841 infof(data, "\tsubject: %s\n", subject);
00842
00843 CERT_GetCertTimes(cert, ¬Before, ¬After);
00844 PR_ExplodeTime(notBefore, PR_GMTParameters, &printableTime);
00845 PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
00846 infof(data, "\tstart date: %s\n", timeString);
00847 PR_ExplodeTime(notAfter, PR_GMTParameters, &printableTime);
00848 PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
00849 infof(data, "\texpire date: %s\n", timeString);
00850 infof(data, "\tcommon name: %s\n", common_name);
00851 infof(data, "\tissuer: %s\n", issuer);
00852
00853 PR_Free(subject);
00854 PR_Free(issuer);
00855 PR_Free(common_name);
00856 }
00857
00858 static CURLcode display_conn_info(struct connectdata *conn, PRFileDesc *sock)
00859 {
00860 CURLcode result = CURLE_OK;
00861 SSLChannelInfo channel;
00862 SSLCipherSuiteInfo suite;
00863 CERTCertificate *cert;
00864 CERTCertificate *cert2;
00865 CERTCertificate *cert3;
00866 PRTime now;
00867 int i;
00868
00869 if(SSL_GetChannelInfo(sock, &channel, sizeof channel) ==
00870 SECSuccess && channel.length == sizeof channel &&
00871 channel.cipherSuite) {
00872 if(SSL_GetCipherSuiteInfo(channel.cipherSuite,
00873 &suite, sizeof suite) == SECSuccess) {
00874 infof(conn->data, "SSL connection using %s\n", suite.cipherSuiteName);
00875 }
00876 }
00877
00878 cert = SSL_PeerCertificate(sock);
00879 if(cert) {
00880 infof(conn->data, "Server certificate:\n");
00881
00882 if(!conn->data->set.ssl.certinfo) {
00883 display_cert_info(conn->data, cert);
00884 CERT_DestroyCertificate(cert);
00885 }
00886 else {
00887
00888 now = PR_Now();
00889 i = 1;
00890 if(!cert->isRoot) {
00891 cert2 = CERT_FindCertIssuer(cert, now, certUsageSSLCA);
00892 while(cert2) {
00893 i++;
00894 if(cert2->isRoot) {
00895 CERT_DestroyCertificate(cert2);
00896 break;
00897 }
00898 cert3 = CERT_FindCertIssuer(cert2, now, certUsageSSLCA);
00899 CERT_DestroyCertificate(cert2);
00900 cert2 = cert3;
00901 }
00902 }
00903
00904 result = Curl_ssl_init_certinfo(conn->data, i);
00905 if(!result) {
00906 for(i = 0; cert; cert = cert2) {
00907 result = Curl_extract_certinfo(conn, i++, (char *)cert->derCert.data,
00908 (char *)cert->derCert.data +
00909 cert->derCert.len);
00910 if(result)
00911 break;
00912
00913 if(cert->isRoot) {
00914 CERT_DestroyCertificate(cert);
00915 break;
00916 }
00917
00918 cert2 = CERT_FindCertIssuer(cert, now, certUsageSSLCA);
00919 CERT_DestroyCertificate(cert);
00920 }
00921 }
00922 }
00923 }
00924
00925 return result;
00926 }
00927
00928 static SECStatus BadCertHandler(void *arg, PRFileDesc *sock)
00929 {
00930 struct connectdata *conn = (struct connectdata *)arg;
00931 struct Curl_easy *data = conn->data;
00932 PRErrorCode err = PR_GetError();
00933 CERTCertificate *cert;
00934
00935
00936 if(SSL_IS_PROXY())
00937 data->set.proxy_ssl.certverifyresult = err;
00938 else
00939 data->set.ssl.certverifyresult = err;
00940
00941 if(err == SSL_ERROR_BAD_CERT_DOMAIN && !SSL_CONN_CONFIG(verifyhost))
00942
00943 return SECSuccess;
00944
00945
00946 cert = SSL_PeerCertificate(sock);
00947 if(cert) {
00948 infof(data, "Server certificate:\n");
00949 display_cert_info(data, cert);
00950 CERT_DestroyCertificate(cert);
00951 }
00952
00953 return SECFailure;
00954 }
00955
00963 static SECStatus check_issuer_cert(PRFileDesc *sock,
00964 char *issuer_nickname)
00965 {
00966 CERTCertificate *cert, *cert_issuer, *issuer;
00967 SECStatus res=SECSuccess;
00968 void *proto_win = NULL;
00969
00970 cert = SSL_PeerCertificate(sock);
00971 cert_issuer = CERT_FindCertIssuer(cert, PR_Now(), certUsageObjectSigner);
00972
00973 proto_win = SSL_RevealPinArg(sock);
00974 issuer = PK11_FindCertFromNickname(issuer_nickname, proto_win);
00975
00976 if((!cert_issuer) || (!issuer))
00977 res = SECFailure;
00978 else if(SECITEM_CompareItem(&cert_issuer->derCert,
00979 &issuer->derCert)!=SECEqual)
00980 res = SECFailure;
00981
00982 CERT_DestroyCertificate(cert);
00983 CERT_DestroyCertificate(issuer);
00984 CERT_DestroyCertificate(cert_issuer);
00985 return res;
00986 }
00987
00988 static CURLcode cmp_peer_pubkey(struct ssl_connect_data *connssl,
00989 const char *pinnedpubkey)
00990 {
00991 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
00992 struct Curl_easy *data = connssl->data;
00993 CERTCertificate *cert;
00994
00995 if(!pinnedpubkey)
00996
00997 return CURLE_OK;
00998
00999
01000 cert = SSL_PeerCertificate(connssl->handle);
01001 if(cert) {
01002
01003 SECKEYPublicKey *pubkey = CERT_ExtractPublicKey(cert);
01004 if(pubkey) {
01005
01006 SECItem *cert_der = PK11_DEREncodePublicKey(pubkey);
01007 if(cert_der) {
01008
01009 result = Curl_pin_peer_pubkey(data, pinnedpubkey, cert_der->data,
01010 cert_der->len);
01011 SECITEM_FreeItem(cert_der, PR_TRUE);
01012 }
01013 SECKEY_DestroyPublicKey(pubkey);
01014 }
01015 CERT_DestroyCertificate(cert);
01016 }
01017
01018
01019 switch(result) {
01020 case CURLE_OK:
01021 infof(data, "pinned public key verified successfully!\n");
01022 break;
01023 case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
01024 failf(data, "failed to verify pinned public key");
01025 break;
01026 default:
01027
01028 break;
01029 }
01030
01031 return result;
01032 }
01033
01038 static SECStatus SelectClientCert(void *arg, PRFileDesc *sock,
01039 struct CERTDistNamesStr *caNames,
01040 struct CERTCertificateStr **pRetCert,
01041 struct SECKEYPrivateKeyStr **pRetKey)
01042 {
01043 struct ssl_connect_data *connssl = (struct ssl_connect_data *)arg;
01044 struct Curl_easy *data = connssl->data;
01045 const char *nickname = connssl->client_nickname;
01046 static const char pem_slotname[] = "PEM Token #1";
01047
01048 if(connssl->obj_clicert) {
01049
01050 SECItem cert_der = { 0, NULL, 0 };
01051 void *proto_win = SSL_RevealPinArg(sock);
01052 struct CERTCertificateStr *cert;
01053 struct SECKEYPrivateKeyStr *key;
01054
01055 PK11SlotInfo *slot = nss_find_slot_by_name(pem_slotname);
01056 if(NULL == slot) {
01057 failf(data, "NSS: PK11 slot not found: %s", pem_slotname);
01058 return SECFailure;
01059 }
01060
01061 if(PK11_ReadRawAttribute(PK11_TypeGeneric, connssl->obj_clicert, CKA_VALUE,
01062 &cert_der) != SECSuccess) {
01063 failf(data, "NSS: CKA_VALUE not found in PK11 generic object");
01064 PK11_FreeSlot(slot);
01065 return SECFailure;
01066 }
01067
01068 cert = PK11_FindCertFromDERCertItem(slot, &cert_der, proto_win);
01069 SECITEM_FreeItem(&cert_der, PR_FALSE);
01070 if(NULL == cert) {
01071 failf(data, "NSS: client certificate from file not found");
01072 PK11_FreeSlot(slot);
01073 return SECFailure;
01074 }
01075
01076 key = PK11_FindPrivateKeyFromCert(slot, cert, NULL);
01077 PK11_FreeSlot(slot);
01078 if(NULL == key) {
01079 failf(data, "NSS: private key from file not found");
01080 CERT_DestroyCertificate(cert);
01081 return SECFailure;
01082 }
01083
01084 infof(data, "NSS: client certificate from file\n");
01085 display_cert_info(data, cert);
01086
01087 *pRetCert = cert;
01088 *pRetKey = key;
01089 return SECSuccess;
01090 }
01091
01092
01093 if(SECSuccess != NSS_GetClientAuthData((void *)nickname, sock, caNames,
01094 pRetCert, pRetKey)
01095 || NULL == *pRetCert) {
01096
01097 if(NULL == nickname)
01098 failf(data, "NSS: client certificate not found (nickname not "
01099 "specified)");
01100 else
01101 failf(data, "NSS: client certificate not found: %s", nickname);
01102
01103 return SECFailure;
01104 }
01105
01106
01107 nickname = (*pRetCert)->nickname;
01108 if(NULL == nickname)
01109 nickname = "[unknown]";
01110
01111 if(!strncmp(nickname, pem_slotname, sizeof(pem_slotname) - 1U)) {
01112 failf(data, "NSS: refusing previously loaded certificate from file: %s",
01113 nickname);
01114 return SECFailure;
01115 }
01116
01117 if(NULL == *pRetKey) {
01118 failf(data, "NSS: private key not found for certificate: %s", nickname);
01119 return SECFailure;
01120 }
01121
01122 infof(data, "NSS: using client certificate: %s\n", nickname);
01123 display_cert_info(data, *pRetCert);
01124 return SECSuccess;
01125 }
01126
01127
01128 static void nss_update_connecting_state(ssl_connect_state state, void *secret)
01129 {
01130 struct ssl_connect_data *connssl = (struct ssl_connect_data *)secret;
01131 if(PR_GetError() != PR_WOULD_BLOCK_ERROR)
01132
01133 return;
01134
01135 switch(connssl->connecting_state) {
01136 case ssl_connect_2:
01137 case ssl_connect_2_reading:
01138 case ssl_connect_2_writing:
01139 break;
01140 default:
01141
01142 return;
01143 }
01144
01145
01146 connssl->connecting_state = state;
01147 }
01148
01149
01150 static PRInt32 nspr_io_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
01151 PRIntn flags, PRIntervalTime timeout)
01152 {
01153 const PRRecvFN recv_fn = fd->lower->methods->recv;
01154 const PRInt32 rv = recv_fn(fd->lower, buf, amount, flags, timeout);
01155 if(rv < 0)
01156
01157 nss_update_connecting_state(ssl_connect_2_reading, fd->secret);
01158 return rv;
01159 }
01160
01161
01162 static PRInt32 nspr_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
01163 PRIntn flags, PRIntervalTime timeout)
01164 {
01165 const PRSendFN send_fn = fd->lower->methods->send;
01166 const PRInt32 rv = send_fn(fd->lower, buf, amount, flags, timeout);
01167 if(rv < 0)
01168
01169 nss_update_connecting_state(ssl_connect_2_writing, fd->secret);
01170 return rv;
01171 }
01172
01173
01174 static PRStatus nspr_io_close(PRFileDesc *fd)
01175 {
01176 const PRCloseFN close_fn = PR_GetDefaultIOMethods()->close;
01177 fd->secret = NULL;
01178 return close_fn(fd);
01179 }
01180
01181
01182 static CURLcode nss_init_core(struct Curl_easy *data, const char *cert_dir)
01183 {
01184 NSSInitParameters initparams;
01185
01186 if(nss_context != NULL)
01187 return CURLE_OK;
01188
01189 memset((void *) &initparams, '\0', sizeof(initparams));
01190 initparams.length = sizeof(initparams);
01191
01192 if(cert_dir) {
01193 char *certpath = aprintf("sql:%s", cert_dir);
01194 if(!certpath)
01195 return CURLE_OUT_OF_MEMORY;
01196
01197 infof(data, "Initializing NSS with certpath: %s\n", certpath);
01198 nss_context = NSS_InitContext(certpath, "", "", "", &initparams,
01199 NSS_INIT_READONLY | NSS_INIT_PK11RELOAD);
01200 free(certpath);
01201
01202 if(nss_context != NULL)
01203 return CURLE_OK;
01204
01205 infof(data, "Unable to initialize NSS database\n");
01206 }
01207
01208 infof(data, "Initializing NSS with certpath: none\n");
01209 nss_context = NSS_InitContext("", "", "", "", &initparams, NSS_INIT_READONLY
01210 | NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN
01211 | NSS_INIT_NOROOTINIT | NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD);
01212 if(nss_context != NULL)
01213 return CURLE_OK;
01214
01215 infof(data, "Unable to initialize NSS\n");
01216 return CURLE_SSL_CACERT_BADFILE;
01217 }
01218
01219
01220 static CURLcode nss_init(struct Curl_easy *data)
01221 {
01222 char *cert_dir;
01223 struct_stat st;
01224 CURLcode result;
01225
01226 if(initialized)
01227 return CURLE_OK;
01228
01229
01230 nss_crl_list = Curl_llist_alloc(nss_destroy_crl_item);
01231 if(!nss_crl_list)
01232 return CURLE_OUT_OF_MEMORY;
01233
01234
01235 cert_dir = getenv("SSL_DIR");
01236 if(cert_dir) {
01237 if((stat(cert_dir, &st) != 0) ||
01238 (!S_ISDIR(st.st_mode))) {
01239 cert_dir = NULL;
01240 }
01241 }
01242
01243
01244 if(!cert_dir) {
01245 if((stat(SSL_DIR, &st) == 0) &&
01246 (S_ISDIR(st.st_mode))) {
01247 cert_dir = (char *)SSL_DIR;
01248 }
01249 }
01250
01251 if(nspr_io_identity == PR_INVALID_IO_LAYER) {
01252
01253 nspr_io_identity = PR_GetUniqueIdentity("libcurl");
01254 if(nspr_io_identity == PR_INVALID_IO_LAYER)
01255 return CURLE_OUT_OF_MEMORY;
01256
01257
01258 memcpy(&nspr_io_methods, PR_GetDefaultIOMethods(), sizeof nspr_io_methods);
01259
01260
01261 nspr_io_methods.recv = nspr_io_recv;
01262 nspr_io_methods.send = nspr_io_send;
01263 nspr_io_methods.close = nspr_io_close;
01264 }
01265
01266 result = nss_init_core(data, cert_dir);
01267 if(result)
01268 return result;
01269
01270 if(!any_cipher_enabled())
01271 NSS_SetDomesticPolicy();
01272
01273 initialized = 1;
01274
01275 return CURLE_OK;
01276 }
01277
01284 int Curl_nss_init(void)
01285 {
01286
01287 if(nss_initlock == NULL) {
01288 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
01289 nss_initlock = PR_NewLock();
01290 nss_crllock = PR_NewLock();
01291 nss_findslot_lock = PR_NewLock();
01292 }
01293
01294
01295
01296 return 1;
01297 }
01298
01299
01300 CURLcode Curl_nss_force_init(struct Curl_easy *data)
01301 {
01302 CURLcode result;
01303 if(!nss_initlock) {
01304 if(data)
01305 failf(data, "unable to initialize NSS, curl_global_init() should have "
01306 "been called with CURL_GLOBAL_SSL or CURL_GLOBAL_ALL");
01307 return CURLE_FAILED_INIT;
01308 }
01309
01310 PR_Lock(nss_initlock);
01311 result = nss_init(data);
01312 PR_Unlock(nss_initlock);
01313
01314 return result;
01315 }
01316
01317
01318 void Curl_nss_cleanup(void)
01319 {
01320
01321
01322
01323 PR_Lock(nss_initlock);
01324 if(initialized) {
01325
01326
01327
01328 SSL_ClearSessionCache();
01329
01330 if(mod && SECSuccess == SECMOD_UnloadUserModule(mod)) {
01331 SECMOD_DestroyModule(mod);
01332 mod = NULL;
01333 }
01334 NSS_ShutdownContext(nss_context);
01335 nss_context = NULL;
01336 }
01337
01338
01339 Curl_llist_destroy(nss_crl_list, NULL);
01340 nss_crl_list = NULL;
01341
01342 PR_Unlock(nss_initlock);
01343
01344 PR_DestroyLock(nss_initlock);
01345 PR_DestroyLock(nss_crllock);
01346 PR_DestroyLock(nss_findslot_lock);
01347 nss_initlock = NULL;
01348
01349 initialized = 0;
01350 }
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360 int
01361 Curl_nss_check_cxn(struct connectdata *conn)
01362 {
01363 int rc;
01364 char buf;
01365
01366 rc =
01367 PR_Recv(conn->ssl[FIRSTSOCKET].handle, (void *)&buf, 1, PR_MSG_PEEK,
01368 PR_SecondsToInterval(1));
01369 if(rc > 0)
01370 return 1;
01371
01372 if(rc == 0)
01373 return 0;
01374
01375 return -1;
01376 }
01377
01378 static void nss_close(struct ssl_connect_data *connssl)
01379 {
01380
01381 const bool client_cert = (connssl->client_nickname != NULL)
01382 || (connssl->obj_clicert != NULL);
01383
01384 free(connssl->client_nickname);
01385 connssl->client_nickname = NULL;
01386
01387
01388 Curl_llist_destroy(connssl->obj_list, NULL);
01389 connssl->obj_list = NULL;
01390 connssl->obj_clicert = NULL;
01391
01392 if(connssl->handle) {
01393 if(client_cert)
01394
01395
01396
01397
01398 SSL_InvalidateSession(connssl->handle);
01399
01400 PR_Close(connssl->handle);
01401 connssl->handle = NULL;
01402 }
01403 }
01404
01405
01406
01407
01408 void Curl_nss_close(struct connectdata *conn, int sockindex)
01409 {
01410 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
01411 struct ssl_connect_data *connssl_proxy = &conn->proxy_ssl[sockindex];
01412
01413 if(connssl->handle || connssl_proxy->handle) {
01414
01415
01416 fake_sclose(conn->sock[sockindex]);
01417 conn->sock[sockindex] = CURL_SOCKET_BAD;
01418 }
01419
01420 if(connssl->handle)
01421
01422
01423 connssl_proxy->handle = NULL;
01424
01425 nss_close(connssl);
01426 nss_close(connssl_proxy);
01427 }
01428
01429
01430
01431 static bool is_nss_error(CURLcode err)
01432 {
01433 switch(err) {
01434 case CURLE_PEER_FAILED_VERIFICATION:
01435 case CURLE_SSL_CACERT:
01436 case CURLE_SSL_CERTPROBLEM:
01437 case CURLE_SSL_CONNECT_ERROR:
01438 case CURLE_SSL_ISSUER_ERROR:
01439 return true;
01440
01441 default:
01442 return false;
01443 }
01444 }
01445
01446
01447 static bool is_cc_error(PRInt32 err)
01448 {
01449 switch(err) {
01450 case SSL_ERROR_BAD_CERT_ALERT:
01451 case SSL_ERROR_EXPIRED_CERT_ALERT:
01452 case SSL_ERROR_REVOKED_CERT_ALERT:
01453 return true;
01454
01455 default:
01456 return false;
01457 }
01458 }
01459
01460 static Curl_recv nss_recv;
01461 static Curl_send nss_send;
01462
01463 static CURLcode nss_load_ca_certificates(struct connectdata *conn,
01464 int sockindex)
01465 {
01466 struct Curl_easy *data = conn->data;
01467 const char *cafile = SSL_CONN_CONFIG(CAfile);
01468 const char *capath = SSL_CONN_CONFIG(CApath);
01469
01470 if(cafile) {
01471 CURLcode result = nss_load_cert(&conn->ssl[sockindex], cafile, PR_TRUE);
01472 if(result)
01473 return result;
01474 }
01475
01476 if(capath) {
01477 struct_stat st;
01478 if(stat(capath, &st) == -1)
01479 return CURLE_SSL_CACERT_BADFILE;
01480
01481 if(S_ISDIR(st.st_mode)) {
01482 PRDirEntry *entry;
01483 PRDir *dir = PR_OpenDir(capath);
01484 if(!dir)
01485 return CURLE_SSL_CACERT_BADFILE;
01486
01487 while((entry = PR_ReadDir(dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN))) {
01488 char *fullpath = aprintf("%s/%s", capath, entry->name);
01489 if(!fullpath) {
01490 PR_CloseDir(dir);
01491 return CURLE_OUT_OF_MEMORY;
01492 }
01493
01494 if(CURLE_OK != nss_load_cert(&conn->ssl[sockindex], fullpath, PR_TRUE))
01495
01496
01497 infof(data, "failed to load '%s' from CURLOPT_CAPATH\n", fullpath);
01498
01499 free(fullpath);
01500 }
01501
01502 PR_CloseDir(dir);
01503 }
01504 else
01505 infof(data, "warning: CURLOPT_CAPATH not a directory (%s)\n", capath);
01506 }
01507
01508 infof(data, " CAfile: %s\n CApath: %s\n",
01509 cafile ? cafile : "none",
01510 capath ? capath : "none");
01511
01512 return CURLE_OK;
01513 }
01514
01515 static CURLcode nss_init_sslver(SSLVersionRange *sslver,
01516 struct Curl_easy *data,
01517 struct connectdata *conn)
01518 {
01519 switch(SSL_CONN_CONFIG(version)) {
01520 case CURL_SSLVERSION_DEFAULT:
01521
01522 if(SSL_VersionRangeGetDefault(ssl_variant_stream, sslver) != SECSuccess)
01523 return CURLE_SSL_CONNECT_ERROR;
01524
01525 if(sslver->min < SSL_LIBRARY_VERSION_TLS_1_0)
01526 sslver->min = SSL_LIBRARY_VERSION_TLS_1_0;
01527 return CURLE_OK;
01528
01529 case CURL_SSLVERSION_TLSv1:
01530 sslver->min = SSL_LIBRARY_VERSION_TLS_1_0;
01531
01532 #ifdef SSL_LIBRARY_VERSION_TLS_1_2
01533 sslver->max = SSL_LIBRARY_VERSION_TLS_1_2;
01534 #elif defined SSL_LIBRARY_VERSION_TLS_1_1
01535 sslver->max = SSL_LIBRARY_VERSION_TLS_1_1;
01536 #else
01537 sslver->max = SSL_LIBRARY_VERSION_TLS_1_0;
01538 #endif
01539 return CURLE_OK;
01540
01541 case CURL_SSLVERSION_SSLv2:
01542 sslver->min = SSL_LIBRARY_VERSION_2;
01543 sslver->max = SSL_LIBRARY_VERSION_2;
01544 return CURLE_OK;
01545
01546 case CURL_SSLVERSION_SSLv3:
01547 sslver->min = SSL_LIBRARY_VERSION_3_0;
01548 sslver->max = SSL_LIBRARY_VERSION_3_0;
01549 return CURLE_OK;
01550
01551 case CURL_SSLVERSION_TLSv1_0:
01552 sslver->min = SSL_LIBRARY_VERSION_TLS_1_0;
01553 sslver->max = SSL_LIBRARY_VERSION_TLS_1_0;
01554 return CURLE_OK;
01555
01556 case CURL_SSLVERSION_TLSv1_1:
01557 #ifdef SSL_LIBRARY_VERSION_TLS_1_1
01558 sslver->min = SSL_LIBRARY_VERSION_TLS_1_1;
01559 sslver->max = SSL_LIBRARY_VERSION_TLS_1_1;
01560 return CURLE_OK;
01561 #endif
01562 break;
01563
01564 case CURL_SSLVERSION_TLSv1_2:
01565 #ifdef SSL_LIBRARY_VERSION_TLS_1_2
01566 sslver->min = SSL_LIBRARY_VERSION_TLS_1_2;
01567 sslver->max = SSL_LIBRARY_VERSION_TLS_1_2;
01568 return CURLE_OK;
01569 #endif
01570 break;
01571
01572 case CURL_SSLVERSION_TLSv1_3:
01573 #ifdef SSL_LIBRARY_VERSION_TLS_1_3
01574 sslver->min = SSL_LIBRARY_VERSION_TLS_1_3;
01575 sslver->max = SSL_LIBRARY_VERSION_TLS_1_3;
01576 return CURLE_OK;
01577 #endif
01578 break;
01579
01580 default:
01581 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
01582 return CURLE_SSL_CONNECT_ERROR;
01583 }
01584
01585 failf(data, "TLS minor version cannot be set");
01586 return CURLE_SSL_CONNECT_ERROR;
01587 }
01588
01589 static CURLcode nss_fail_connect(struct ssl_connect_data *connssl,
01590 struct Curl_easy *data,
01591 CURLcode curlerr)
01592 {
01593 PRErrorCode err = 0;
01594
01595 if(is_nss_error(curlerr)) {
01596
01597 err = PR_GetError();
01598 if(is_cc_error(err))
01599 curlerr = CURLE_SSL_CERTPROBLEM;
01600
01601
01602 infof(data, "NSS error %d (%s)\n", err, nss_error_to_name(err));
01603
01604
01605 nss_print_error_message(data, err);
01606 }
01607
01608
01609 Curl_llist_destroy(connssl->obj_list, NULL);
01610 connssl->obj_list = NULL;
01611
01612 return curlerr;
01613 }
01614
01615
01616 static CURLcode nss_set_nonblock(struct ssl_connect_data *connssl,
01617 struct Curl_easy *data)
01618 {
01619 static PRSocketOptionData sock_opt;
01620 sock_opt.option = PR_SockOpt_Nonblocking;
01621 sock_opt.value.non_blocking = PR_TRUE;
01622
01623 if(PR_SetSocketOption(connssl->handle, &sock_opt) != PR_SUCCESS)
01624 return nss_fail_connect(connssl, data, CURLE_SSL_CONNECT_ERROR);
01625
01626 return CURLE_OK;
01627 }
01628
01629 static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
01630 {
01631 PRFileDesc *model = NULL;
01632 PRFileDesc *nspr_io = NULL;
01633 PRFileDesc *nspr_io_stub = NULL;
01634 PRBool ssl_no_cache;
01635 PRBool ssl_cbc_random_iv;
01636 struct Curl_easy *data = conn->data;
01637 curl_socket_t sockfd = conn->sock[sockindex];
01638 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
01639 CURLcode result;
01640 bool second_layer = FALSE;
01641
01642 SSLVersionRange sslver = {
01643 SSL_LIBRARY_VERSION_TLS_1_0,
01644 SSL_LIBRARY_VERSION_TLS_1_0
01645 };
01646
01647 connssl->data = data;
01648
01649
01650 connssl->obj_list = Curl_llist_alloc(nss_destroy_object);
01651 if(!connssl->obj_list)
01652 return CURLE_OUT_OF_MEMORY;
01653
01654
01655 PR_Lock(nss_initlock);
01656 result = nss_init(conn->data);
01657 if(result) {
01658 PR_Unlock(nss_initlock);
01659 goto error;
01660 }
01661
01662 result = CURLE_SSL_CONNECT_ERROR;
01663
01664 if(!mod) {
01665 char *configstring = aprintf("library=%s name=PEM", pem_library);
01666 if(!configstring) {
01667 PR_Unlock(nss_initlock);
01668 goto error;
01669 }
01670 mod = SECMOD_LoadUserModule(configstring, NULL, PR_FALSE);
01671 free(configstring);
01672
01673 if(!mod || !mod->loaded) {
01674 if(mod) {
01675 SECMOD_DestroyModule(mod);
01676 mod = NULL;
01677 }
01678 infof(data, "WARNING: failed to load NSS PEM library %s. Using "
01679 "OpenSSL PEM certificates will not work.\n", pem_library);
01680 }
01681 }
01682
01683 PK11_SetPasswordFunc(nss_get_password);
01684 PR_Unlock(nss_initlock);
01685
01686 model = PR_NewTCPSocket();
01687 if(!model)
01688 goto error;
01689 model = SSL_ImportFD(NULL, model);
01690
01691 if(SSL_OptionSet(model, SSL_SECURITY, PR_TRUE) != SECSuccess)
01692 goto error;
01693 if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess)
01694 goto error;
01695 if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)
01696 goto error;
01697
01698
01699 ssl_no_cache = (data->set.general_ssl.sessionid
01700 && SSL_CONN_CONFIG(verifypeer)) ? PR_FALSE : PR_TRUE;
01701 if(SSL_OptionSet(model, SSL_NO_CACHE, ssl_no_cache) != SECSuccess)
01702 goto error;
01703
01704
01705 if(nss_init_sslver(&sslver, data, conn) != CURLE_OK)
01706 goto error;
01707 if(SSL_VersionRangeSet(model, &sslver) != SECSuccess)
01708 goto error;
01709
01710 ssl_cbc_random_iv = !SSL_SET_OPTION(enable_beast);
01711 #ifdef SSL_CBC_RANDOM_IV
01712
01713
01714 if(SSL_OptionSet(model, SSL_CBC_RANDOM_IV, ssl_cbc_random_iv) != SECSuccess)
01715 infof(data, "warning: failed to set SSL_CBC_RANDOM_IV = %d\n",
01716 ssl_cbc_random_iv);
01717 #else
01718 if(ssl_cbc_random_iv)
01719 infof(data, "warning: support for SSL_CBC_RANDOM_IV not compiled in\n");
01720 #endif
01721
01722 if(SSL_CONN_CONFIG(cipher_list)) {
01723 if(set_ciphers(data, model, SSL_CONN_CONFIG(cipher_list)) != SECSuccess) {
01724 result = CURLE_SSL_CIPHER;
01725 goto error;
01726 }
01727 }
01728
01729 if(!SSL_CONN_CONFIG(verifypeer) && SSL_CONN_CONFIG(verifyhost))
01730 infof(data, "warning: ignoring value of ssl.verifyhost\n");
01731
01732
01733
01734 if(SSL_AuthCertificateHook(model, nss_auth_cert_hook, conn) != SECSuccess)
01735 goto error;
01736
01737
01738 if(SSL_IS_PROXY())
01739 data->set.proxy_ssl.certverifyresult = 0;
01740 else
01741 data->set.ssl.certverifyresult = 0;
01742
01743 if(SSL_BadCertHook(model, BadCertHandler, conn) != SECSuccess)
01744 goto error;
01745
01746 if(SSL_HandshakeCallback(model, HandshakeCallback, conn) != SECSuccess)
01747 goto error;
01748
01749 if(SSL_CONN_CONFIG(verifypeer)) {
01750 const CURLcode rv = nss_load_ca_certificates(conn, sockindex);
01751 if(rv) {
01752 result = rv;
01753 goto error;
01754 }
01755 }
01756
01757 if(SSL_SET_OPTION(CRLfile)) {
01758 const CURLcode rv = nss_load_crl(SSL_SET_OPTION(CRLfile));
01759 if(rv) {
01760 result = rv;
01761 goto error;
01762 }
01763 infof(data, " CRLfile: %s\n", SSL_SET_OPTION(CRLfile));
01764 }
01765
01766 if(SSL_SET_OPTION(cert)) {
01767 char *nickname = dup_nickname(data, SSL_SET_OPTION(cert));
01768 if(nickname) {
01769
01770 connssl->obj_clicert = NULL;
01771 }
01772 else {
01773 CURLcode rv = cert_stuff(conn, sockindex, SSL_SET_OPTION(cert),
01774 SSL_SET_OPTION(key));
01775 if(rv) {
01776
01777 result = rv;
01778 goto error;
01779 }
01780 }
01781
01782
01783 connssl->client_nickname = nickname;
01784 }
01785 else
01786 connssl->client_nickname = NULL;
01787
01788 if(SSL_GetClientAuthDataHook(model, SelectClientCert,
01789 (void *)connssl) != SECSuccess) {
01790 result = CURLE_SSL_CERTPROBLEM;
01791 goto error;
01792 }
01793
01794 if(conn->proxy_ssl[sockindex].use) {
01795 DEBUGASSERT(ssl_connection_complete == conn->proxy_ssl[sockindex].state);
01796 DEBUGASSERT(conn->proxy_ssl[sockindex].handle != NULL);
01797 nspr_io = conn->proxy_ssl[sockindex].handle;
01798 second_layer = TRUE;
01799 }
01800 else {
01801
01802 nspr_io = PR_ImportTCPSocket(sockfd);
01803 if(!nspr_io)
01804 goto error;
01805 }
01806
01807
01808 nspr_io_stub = PR_CreateIOLayerStub(nspr_io_identity, &nspr_io_methods);
01809 if(!nspr_io_stub) {
01810 if(!second_layer)
01811 PR_Close(nspr_io);
01812 goto error;
01813 }
01814
01815
01816 nspr_io_stub->secret = (void *)connssl;
01817
01818
01819 if(PR_PushIOLayer(nspr_io, PR_TOP_IO_LAYER, nspr_io_stub) != PR_SUCCESS) {
01820 if(!second_layer)
01821 PR_Close(nspr_io);
01822 PR_Close(nspr_io_stub);
01823 goto error;
01824 }
01825
01826
01827 connssl->handle = SSL_ImportFD(model, nspr_io);
01828 if(!connssl->handle) {
01829 if(!second_layer)
01830 PR_Close(nspr_io);
01831 goto error;
01832 }
01833
01834 PR_Close(model);
01835 model = NULL;
01836
01837
01838 if(SSL_SET_OPTION(key_passwd)) {
01839 SSL_SetPKCS11PinArg(connssl->handle, SSL_SET_OPTION(key_passwd));
01840 }
01841
01842 #ifdef SSL_ENABLE_OCSP_STAPLING
01843 if(SSL_CONN_CONFIG(verifystatus)) {
01844 if(SSL_OptionSet(connssl->handle, SSL_ENABLE_OCSP_STAPLING, PR_TRUE)
01845 != SECSuccess)
01846 goto error;
01847 }
01848 #endif
01849
01850 #ifdef SSL_ENABLE_NPN
01851 if(SSL_OptionSet(connssl->handle, SSL_ENABLE_NPN, conn->bits.tls_enable_npn
01852 ? PR_TRUE : PR_FALSE) != SECSuccess)
01853 goto error;
01854 #endif
01855
01856 #ifdef SSL_ENABLE_ALPN
01857 if(SSL_OptionSet(connssl->handle, SSL_ENABLE_ALPN, conn->bits.tls_enable_alpn
01858 ? PR_TRUE : PR_FALSE) != SECSuccess)
01859 goto error;
01860 #endif
01861
01862 #if NSSVERNUM >= 0x030f04
01863 if(data->set.ssl.falsestart) {
01864 if(SSL_OptionSet(connssl->handle, SSL_ENABLE_FALSE_START, PR_TRUE)
01865 != SECSuccess)
01866 goto error;
01867
01868 if(SSL_SetCanFalseStartCallback(connssl->handle, CanFalseStartCallback,
01869 conn) != SECSuccess)
01870 goto error;
01871 }
01872 #endif
01873
01874 #if defined(SSL_ENABLE_NPN) || defined(SSL_ENABLE_ALPN)
01875 if(conn->bits.tls_enable_npn || conn->bits.tls_enable_alpn) {
01876 int cur = 0;
01877 unsigned char protocols[128];
01878
01879 #ifdef USE_NGHTTP2
01880 if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
01881 protocols[cur++] = NGHTTP2_PROTO_VERSION_ID_LEN;
01882 memcpy(&protocols[cur], NGHTTP2_PROTO_VERSION_ID,
01883 NGHTTP2_PROTO_VERSION_ID_LEN);
01884 cur += NGHTTP2_PROTO_VERSION_ID_LEN;
01885 }
01886 #endif
01887 protocols[cur++] = ALPN_HTTP_1_1_LENGTH;
01888 memcpy(&protocols[cur], ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH);
01889 cur += ALPN_HTTP_1_1_LENGTH;
01890
01891 if(SSL_SetNextProtoNego(connssl->handle, protocols, cur) != SECSuccess)
01892 goto error;
01893 }
01894 #endif
01895
01896
01897
01898 if(SSL_ResetHandshake(connssl->handle, PR_FALSE)
01899 != SECSuccess)
01900 goto error;
01901
01902
01903 if(SSL_SetURL(connssl->handle, SSL_IS_PROXY() ? conn->http_proxy.host.name :
01904 conn->host.name) != SECSuccess)
01905 goto error;
01906
01907
01908 if(SSL_SetSockPeerID(connssl->handle, SSL_IS_PROXY() ?
01909 conn->http_proxy.host.name : conn->host.name)
01910 != SECSuccess)
01911 goto error;
01912
01913 return CURLE_OK;
01914
01915 error:
01916 if(model)
01917 PR_Close(model);
01918
01919 return nss_fail_connect(connssl, data, result);
01920 }
01921
01922 static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
01923 {
01924 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
01925 struct Curl_easy *data = conn->data;
01926 CURLcode result = CURLE_SSL_CONNECT_ERROR;
01927 PRUint32 timeout;
01928 long * const certverifyresult = SSL_IS_PROXY() ?
01929 &data->set.proxy_ssl.certverifyresult : &data->set.ssl.certverifyresult;
01930 const char * const pinnedpubkey = SSL_IS_PROXY() ?
01931 data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
01932 data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
01933
01934
01935
01936 const long time_left = Curl_timeleft(data, NULL, TRUE);
01937 if(time_left < 0L) {
01938 failf(data, "timed out before SSL handshake");
01939 result = CURLE_OPERATION_TIMEDOUT;
01940 goto error;
01941 }
01942
01943
01944 timeout = PR_MillisecondsToInterval((PRUint32) time_left);
01945 if(SSL_ForceHandshakeWithTimeout(connssl->handle, timeout) != SECSuccess) {
01946 if(PR_GetError() == PR_WOULD_BLOCK_ERROR)
01947
01948 return CURLE_AGAIN;
01949 else if(*certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
01950 result = CURLE_PEER_FAILED_VERIFICATION;
01951 else if(*certverifyresult != 0)
01952 result = CURLE_SSL_CACERT;
01953 goto error;
01954 }
01955
01956 result = display_conn_info(conn, connssl->handle);
01957 if(result)
01958 goto error;
01959
01960 if(SSL_SET_OPTION(issuercert)) {
01961 SECStatus ret = SECFailure;
01962 char *nickname = dup_nickname(data, SSL_SET_OPTION(issuercert));
01963 if(nickname) {
01964
01965 ret = check_issuer_cert(connssl->handle, nickname);
01966 free(nickname);
01967 }
01968
01969 if(SECFailure == ret) {
01970 infof(data, "SSL certificate issuer check failed\n");
01971 result = CURLE_SSL_ISSUER_ERROR;
01972 goto error;
01973 }
01974 else {
01975 infof(data, "SSL certificate issuer check ok\n");
01976 }
01977 }
01978
01979 result = cmp_peer_pubkey(connssl, pinnedpubkey);
01980 if(result)
01981
01982 goto error;
01983
01984 return CURLE_OK;
01985
01986 error:
01987 return nss_fail_connect(connssl, data, result);
01988 }
01989
01990 static CURLcode nss_connect_common(struct connectdata *conn, int sockindex,
01991 bool *done)
01992 {
01993 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
01994 struct Curl_easy *data = conn->data;
01995 const bool blocking = (done == NULL);
01996 CURLcode result;
01997
01998 if(connssl->state == ssl_connection_complete) {
01999 if(!blocking)
02000 *done = TRUE;
02001 return CURLE_OK;
02002 }
02003
02004 if(connssl->connecting_state == ssl_connect_1) {
02005 result = nss_setup_connect(conn, sockindex);
02006 if(result)
02007
02008 return result;
02009
02010 if(!blocking) {
02011
02012 result = nss_set_nonblock(connssl, data);
02013 if(result)
02014 return result;
02015 }
02016
02017 connssl->connecting_state = ssl_connect_2;
02018 }
02019
02020 result = nss_do_connect(conn, sockindex);
02021 switch(result) {
02022 case CURLE_OK:
02023 break;
02024 case CURLE_AGAIN:
02025 if(!blocking)
02026
02027 return CURLE_OK;
02028
02029 default:
02030 return result;
02031 }
02032
02033 if(blocking) {
02034
02035 result = nss_set_nonblock(connssl, data);
02036 if(result)
02037 return result;
02038 }
02039 else
02040
02041 *done = TRUE;
02042
02043 connssl->state = ssl_connection_complete;
02044 conn->recv[sockindex] = nss_recv;
02045 conn->send[sockindex] = nss_send;
02046
02047
02048 connssl->connecting_state = ssl_connect_1;
02049
02050 return CURLE_OK;
02051 }
02052
02053 CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
02054 {
02055 return nss_connect_common(conn, sockindex, NULL);
02056 }
02057
02058 CURLcode Curl_nss_connect_nonblocking(struct connectdata *conn,
02059 int sockindex, bool *done)
02060 {
02061 return nss_connect_common(conn, sockindex, done);
02062 }
02063
02064 static ssize_t nss_send(struct connectdata *conn,
02065 int sockindex,
02066 const void *mem,
02067 size_t len,
02068 CURLcode *curlcode)
02069 {
02070 ssize_t rc = PR_Send(conn->ssl[sockindex].handle, mem, (int)len, 0,
02071 PR_INTERVAL_NO_WAIT);
02072 if(rc < 0) {
02073 PRInt32 err = PR_GetError();
02074 if(err == PR_WOULD_BLOCK_ERROR)
02075 *curlcode = CURLE_AGAIN;
02076 else {
02077
02078 const char *err_name = nss_error_to_name(err);
02079 infof(conn->data, "SSL write: error %d (%s)\n", err, err_name);
02080
02081
02082 nss_print_error_message(conn->data, err);
02083
02084 *curlcode = (is_cc_error(err))
02085 ? CURLE_SSL_CERTPROBLEM
02086 : CURLE_SEND_ERROR;
02087 }
02088
02089 return -1;
02090 }
02091
02092 return rc;
02093 }
02094
02095 static ssize_t nss_recv(struct connectdata * conn,
02096 int num,
02097 char *buf,
02098 size_t buffersize,
02099 CURLcode *curlcode)
02100 {
02101 ssize_t nread = PR_Recv(conn->ssl[num].handle, buf, (int)buffersize, 0,
02102 PR_INTERVAL_NO_WAIT);
02103 if(nread < 0) {
02104
02105 PRInt32 err = PR_GetError();
02106
02107 if(err == PR_WOULD_BLOCK_ERROR)
02108 *curlcode = CURLE_AGAIN;
02109 else {
02110
02111 const char *err_name = nss_error_to_name(err);
02112 infof(conn->data, "SSL read: errno %d (%s)\n", err, err_name);
02113
02114
02115 nss_print_error_message(conn->data, err);
02116
02117 *curlcode = (is_cc_error(err))
02118 ? CURLE_SSL_CERTPROBLEM
02119 : CURLE_RECV_ERROR;
02120 }
02121
02122 return -1;
02123 }
02124
02125 return nread;
02126 }
02127
02128 size_t Curl_nss_version(char *buffer, size_t size)
02129 {
02130 return snprintf(buffer, size, "NSS/%s", NSS_VERSION);
02131 }
02132
02133
02134 int Curl_nss_seed(struct Curl_easy *data)
02135 {
02136
02137 return !!Curl_nss_force_init(data);
02138 }
02139
02140
02141 int Curl_nss_random(struct Curl_easy *data,
02142 unsigned char *entropy,
02143 size_t length)
02144 {
02145 Curl_nss_seed(data);
02146
02147 if(SECSuccess != PK11_GenerateRandom(entropy, curlx_uztosi(length)))
02148
02149 return -1;
02150
02151 return 0;
02152 }
02153
02154 void Curl_nss_md5sum(unsigned char *tmp,
02155 size_t tmplen,
02156 unsigned char *md5sum,
02157 size_t md5len)
02158 {
02159 PK11Context *MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
02160 unsigned int MD5out;
02161
02162 PK11_DigestOp(MD5pw, tmp, curlx_uztoui(tmplen));
02163 PK11_DigestFinal(MD5pw, md5sum, &MD5out, curlx_uztoui(md5len));
02164 PK11_DestroyContext(MD5pw, PR_TRUE);
02165 }
02166
02167 void Curl_nss_sha256sum(const unsigned char *tmp,
02168 size_t tmplen,
02169 unsigned char *sha256sum,
02170 size_t sha256len)
02171 {
02172 PK11Context *SHA256pw = PK11_CreateDigestContext(SEC_OID_SHA256);
02173 unsigned int SHA256out;
02174
02175 PK11_DigestOp(SHA256pw, tmp, curlx_uztoui(tmplen));
02176 PK11_DigestFinal(SHA256pw, sha256sum, &SHA256out, curlx_uztoui(sha256len));
02177 PK11_DestroyContext(SHA256pw, PR_TRUE);
02178 }
02179
02180 bool Curl_nss_cert_status_request(void)
02181 {
02182 #ifdef SSL_ENABLE_OCSP_STAPLING
02183 return TRUE;
02184 #else
02185 return FALSE;
02186 #endif
02187 }
02188
02189 bool Curl_nss_false_start(void)
02190 {
02191 #if NSSVERNUM >= 0x030f04
02192 return TRUE;
02193 #else
02194 return FALSE;
02195 #endif
02196 }
02197
02198 #endif