Skip to content

Commit 20cb12d

Browse files
committed
nss: use NSS_InitContext() to initialize NSS if available
NSS_InitContext() was introduced in NSS 3.12.5 and helps to prevent collisions on NSS initialization/shutdown with other libraries. Bug: https://bugzilla.redhat.com/738456
1 parent 42aa796 commit 20cb12d

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

RELEASE-NOTES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This release includes the following changes:
1414

1515
This release includes the following bugfixes:
1616

17+
o nss: libcurl now uses NSS_InitContext() to prevent collisions if available [1]
1718
o
1819

1920
This release includes the following known bugs:
@@ -29,4 +30,5 @@ advice from friends like these:
2930

3031
References to bug reports and discussions on issues:
3132

33+
[1] = https://bugzilla.redhat.com/738456
3234

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,14 @@ if test "$OPENSSL_ENABLED" != "1" -a "$GNUTLS_ENABLED" != "1"; then
21182118
if test "x$USE_NSS" = "xyes"; then
21192119
AC_MSG_NOTICE([detected NSS version $version])
21202120

2121+
dnl NSS_InitContext() was introduced in NSS 3.12.5 and helps to prevent
2122+
dnl collisions on NSS initialization/shutdown with other libraries
2123+
AC_CHECK_FUNC(NSS_InitContext,
2124+
[
2125+
AC_DEFINE(HAVE_NSS_INITCONTEXT, 1, [if you have the NSS_InitContext function])
2126+
AC_SUBST(HAVE_NSS_INITCONTEXT, [1])
2127+
])
2128+
21212129
dnl when shared libs were found in a path that the run-time
21222130
dnl linker doesn't search through, we need to add it to
21232131
dnl LD_LIBRARY_PATH to prevent further configure tests to fail

lib/nss.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
7878

7979
PRLock * nss_initlock = NULL;
8080
PRLock * nss_crllock = NULL;
81+
#ifdef HAVE_NSS_INITCONTEXT
82+
NSSInitContext * nss_context = NULL;
83+
#endif
8184

8285
volatile int initialized = 0;
8386

@@ -861,29 +864,56 @@ isTLSIntoleranceError(PRInt32 err)
861864

862865
static CURLcode nss_init_core(struct SessionHandle *data, const char *cert_dir)
863866
{
867+
#ifdef HAVE_NSS_INITCONTEXT
868+
if(nss_context != NULL)
869+
return CURLE_OK;
870+
871+
NSSInitParameters initparams;
872+
memset((void *) &initparams, '\0', sizeof(initparams));
873+
initparams.length = sizeof(initparams);
874+
#else /* HAVE_NSS_INITCONTEXT */
875+
SECStatus rv;
876+
864877
if(NSS_IsInitialized())
865878
return CURLE_OK;
879+
#endif
866880

867881
if(cert_dir) {
868-
SECStatus rv;
869882
const bool use_sql = NSS_VersionCheck("3.12.0");
870883
char *certpath = aprintf("%s%s", use_sql ? "sql:" : "", cert_dir);
871884
if(!certpath)
872885
return CURLE_OUT_OF_MEMORY;
873886

874887
infof(data, "Initializing NSS with certpath: %s\n", certpath);
888+
#ifdef HAVE_NSS_INITCONTEXT
889+
nss_context = NSS_InitContext(certpath, "", "", "", &initparams,
890+
NSS_INIT_READONLY | NSS_INIT_PK11RELOAD);
891+
free(certpath);
892+
893+
if(nss_context != NULL)
894+
return CURLE_OK;
895+
#else /* HAVE_NSS_INITCONTEXT */
875896
rv = NSS_Initialize(certpath, "", "", "", NSS_INIT_READONLY);
876897
free(certpath);
877898

878899
if(rv == SECSuccess)
879900
return CURLE_OK;
901+
#endif
880902

881903
infof(data, "Unable to initialize NSS database\n");
882904
}
883905

884906
infof(data, "Initializing NSS with certpath: none\n");
907+
#ifdef HAVE_NSS_INITCONTEXT
908+
nss_context = NSS_InitContext("", "", "", "", &initparams, NSS_INIT_READONLY
909+
| NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN
910+
| NSS_INIT_NOROOTINIT | NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD);
911+
if(nss_context != NULL)
912+
return CURLE_OK;
913+
#else /* HAVE_NSS_INITCONTEXT */
885914
if(NSS_NoDB_Init(NULL) == SECSuccess)
886915
return CURLE_OK;
916+
#endif
887917

888918
infof(data, "Unable to initialize NSS\n");
889919
return CURLE_SSL_CACERT_BADFILE;
@@ -979,7 +1009,12 @@ void Curl_nss_cleanup(void)
9791009
SECMOD_DestroyModule(mod);
9801010
mod = NULL;
9811011
}
1012+
#ifdef HAVE_NSS_INITCONTEXT
1013+
NSS_ShutdownContext(nss_context);
1014+
nss_context = NULL;
1015+
#else /* HAVE_NSS_INITCONTEXT */
9821016
NSS_Shutdown();
1017+
#endif
9831018
}
9841019
PR_Unlock(nss_initlock);
9851020

0 commit comments

Comments
 (0)