From cccb7a3f6824d080935923b2ee8ceb138a68982d Mon Sep 17 00:00:00 2001 From: "ian.mcgreer%sun.com" Date: Fri, 28 Feb 2003 21:13:20 +0000 Subject: [PATCH] bug 191757, InitOIDHash() not threadsafe r=relyea --- security/nss/lib/nss/nssinit.c | 7 ++++++- security/nss/lib/softoken/pkcs11.c | 11 +++++++++++ security/nss/lib/util/secoid.c | 25 +++++++++---------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/security/nss/lib/nss/nssinit.c b/security/nss/lib/nss/nssinit.c index f631154679c..8ec2be79727 100644 --- a/security/nss/lib/nss/nssinit.c +++ b/security/nss/lib/nss/nssinit.c @@ -32,7 +32,7 @@ * may use your version of this file under either the MPL or the * GPL. * - # $Id: nssinit.c,v 1.61 2003-02-20 16:58:53 wtc%netscape.com Exp $ + # $Id: nssinit.c,v 1.62 2003-02-28 21:13:15 ian.mcgreer%sun.com Exp $ */ #include @@ -397,6 +397,8 @@ nss_FindExternalRoot(const char *dbpath, const char* secmodprefix) static PRBool nss_IsInitted = PR_FALSE; +extern SECStatus secoid_Init(void); + static SECStatus nss_Init(const char *configdir, const char *certPrefix, const char *keyPrefix, const char *secmodName, PRBool readOnly, PRBool noCertDB, @@ -466,6 +468,9 @@ loser: } if (rv == SECSuccess) { + if (secoid_Init() != SECSuccess) { + return SECFailure; + } if (STAN_LoadDefaultNSS3TrustDomain() != PR_SUCCESS) { return SECFailure; } diff --git a/security/nss/lib/softoken/pkcs11.c b/security/nss/lib/softoken/pkcs11.c index 6a22d4c9866..a7657f2cecf 100644 --- a/security/nss/lib/softoken/pkcs11.c +++ b/security/nss/lib/softoken/pkcs11.c @@ -2674,6 +2674,7 @@ pk11_closePeer(PRBool isFIPS) } static PRBool nsc_init = PR_FALSE; +extern SECStatus secoid_Init(void); /* NSC_Initialize initializes the Cryptoki library. */ CK_RV nsc_CommonInitialize(CK_VOID_PTR pReserved, PRBool isFIPS) @@ -2694,6 +2695,12 @@ CK_RV nsc_CommonInitialize(CK_VOID_PTR pReserved, PRBool isFIPS) } } + rv = secoid_Init(); + if (rv != SECSuccess) { + crv = CKR_DEVICE_ERROR; + return crv; + } + rv = RNG_RNGInit(); /* initialize random number generator */ if (rv != SECSuccess) { crv = CKR_DEVICE_ERROR; @@ -2758,6 +2765,8 @@ CK_RV NSC_Initialize(CK_VOID_PTR pReserved) return crv; } +extern SECStatus SECOID_Shutdown(void); + /* NSC_Finalize indicates that an application is done with the * Cryptoki library.*/ CK_RV nsc_CommonFinalize (CK_VOID_PTR pReserved, PRBool isFIPS) @@ -2792,6 +2801,8 @@ CK_RV nsc_CommonFinalize (CK_VOID_PTR pReserved, PRBool isFIPS) pk11_CleanupFreeLists(); /* tell freeBL to clean up after itself */ BL_Cleanup(); + /* clean up the default OID table */ + SECOID_Shutdown(); nsc_init = PR_FALSE; return CKR_OK; diff --git a/security/nss/lib/util/secoid.c b/security/nss/lib/util/secoid.c index 3f36294501e..8eb09b6a797 100644 --- a/security/nss/lib/util/secoid.c +++ b/security/nss/lib/util/secoid.c @@ -1336,12 +1336,16 @@ secoid_HashNumber(const void *key) } -static SECStatus -InitOIDHash(void) +SECStatus +secoid_Init(void) { PLHashEntry *entry; const SECOidData *oid; int i; + + if (oidhash) { + return PR_SUCCESS; + } oidhash = PL_NewHashTable(0, SECITEM_Hash, SECITEM_HashCompare, PL_CompareValues, NULL, NULL); @@ -1388,13 +1392,8 @@ SECOID_FindOIDByMechanism(unsigned long mechanism) SECOidData *ret; int rv; - if ( !oidhash ) { - rv = InitOIDHash(); - if ( rv != SECSuccess ) { - PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); - return NULL; - } - } + PR_ASSERT(oidhash != NULL); + ret = PL_HashTableLookupConst ( oidmechhash, (void *)mechanism); if ( ret == NULL ) { PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); @@ -1409,13 +1408,7 @@ SECOID_FindOID(SECItem *oid) SECOidData *ret; int rv; - if ( !oidhash ) { - rv = InitOIDHash(); - if ( rv != SECSuccess ) { - PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); - return NULL; - } - } + PR_ASSERT(oidhash != NULL); ret = PL_HashTableLookupConst ( oidhash, oid ); if ( ret == NULL ) {