2001-10-11 16:34:49 +00:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Netscape security libraries.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* terms of the GNU General Public License Version 2 or later (the
|
|
|
|
* "GPL"), in which case the provisions of the GPL are applicable
|
|
|
|
* instead of those above. If you wish to allow use of your
|
|
|
|
* version of this file only under the terms of the GPL and not to
|
|
|
|
* allow others to use your version of this file under the MPL,
|
|
|
|
* indicate your decision by deleting the provisions above and
|
|
|
|
* replace them with the notice and other provisions required by
|
|
|
|
* the GPL. If you do not delete the provisions above, a recipient
|
|
|
|
* may use your version of this file under either the MPL or the
|
|
|
|
* GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2002-04-19 23:06:44 +00:00
|
|
|
static const char CVS_ID[] = "@(#) $RCSfile: tdcache.c,v $ $Revision: 1.31 $ $Date: 2002/04/19 23:06:44 $ $Name: $";
|
2001-10-11 16:34:49 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2001-10-15 17:18:06 +00:00
|
|
|
#ifndef PKIM_H
|
|
|
|
#include "pkim.h"
|
|
|
|
#endif /* PKIM_H */
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
#ifndef PKIT_H
|
|
|
|
#include "pkit.h"
|
|
|
|
#endif /* PKIT_H */
|
|
|
|
|
2001-12-10 19:05:51 +00:00
|
|
|
#ifndef NSSPKI_H
|
|
|
|
#include "nsspki.h"
|
|
|
|
#endif /* NSSPKI_H */
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
#ifndef PKI_H
|
|
|
|
#include "pki.h"
|
|
|
|
#endif /* PKI_H */
|
|
|
|
|
2001-12-10 19:05:51 +00:00
|
|
|
#ifndef NSSBASE_H
|
|
|
|
#include "nssbase.h"
|
|
|
|
#endif /* NSSBASE_H */
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
#ifndef BASE_H
|
|
|
|
#include "base.h"
|
|
|
|
#endif /* BASE_H */
|
|
|
|
|
2001-12-07 01:36:25 +00:00
|
|
|
#ifdef NSS_3_4_CODE
|
|
|
|
#include "cert.h"
|
2002-04-19 23:06:44 +00:00
|
|
|
#include "dev.h"
|
|
|
|
#include "pki3hack.h"
|
2001-12-07 01:36:25 +00:00
|
|
|
#endif
|
|
|
|
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
static PRLogModuleInfo *s_log = NULL;
|
|
|
|
#endif
|
|
|
|
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
static void log_item_dump(const char *msg, NSSItem *it)
|
|
|
|
{
|
|
|
|
char buf[33];
|
|
|
|
int i, j;
|
|
|
|
for (i=0; i<10 && i<it->size; i++) {
|
|
|
|
sprintf(&buf[2*i], "%02X", ((PRUint8 *)it->data)[i]);
|
|
|
|
}
|
|
|
|
if (it->size>10) {
|
|
|
|
sprintf(&buf[2*i], "..");
|
|
|
|
i += 1;
|
|
|
|
for (j=it->size-1; i<=16 && j>10; i++, j--) {
|
|
|
|
sprintf(&buf[2*i], "%02X", ((PRUint8 *)it->data)[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("%s: %s", msg, buf));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
static void log_cert_ref(const char *msg, NSSCertificate *c)
|
|
|
|
{
|
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("%s: %s", msg,
|
|
|
|
(c->nickname) ? c->nickname : c->email));
|
|
|
|
log_item_dump("\tserial", &c->serial);
|
|
|
|
log_item_dump("\tsubject", &c->subject);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
/* Certificate cache routines */
|
|
|
|
|
|
|
|
/* XXX
|
|
|
|
* Locking is not handled well at all. A single, global lock with sub-locks
|
|
|
|
* in the collection types. Cleanup needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* should it live in its own arena? */
|
2001-11-28 16:23:51 +00:00
|
|
|
struct nssTDCertificateCacheStr
|
|
|
|
{
|
|
|
|
PZLock *lock;
|
|
|
|
NSSArena *arena;
|
|
|
|
nssHash *issuerAndSN;
|
|
|
|
nssHash *subject;
|
|
|
|
nssHash *nickname;
|
|
|
|
nssHash *email;
|
2001-10-11 16:34:49 +00:00
|
|
|
};
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
struct cache_entry_str
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
NSSCertificate *cert;
|
|
|
|
nssList *list;
|
|
|
|
void *value;
|
|
|
|
} entry;
|
|
|
|
PRUint32 hits;
|
|
|
|
PRTime lastHit;
|
2002-02-05 23:55:43 +00:00
|
|
|
NSSArena *arena;
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *nickname;
|
2001-11-28 16:23:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct cache_entry_str cache_entry;
|
|
|
|
|
|
|
|
static cache_entry *
|
2002-02-05 23:55:43 +00:00
|
|
|
new_cache_entry(NSSArena *arena, void *value, PRBool ownArena)
|
2001-11-28 16:23:51 +00:00
|
|
|
{
|
|
|
|
cache_entry *ce = nss_ZNEW(arena, cache_entry);
|
|
|
|
if (ce) {
|
|
|
|
ce->entry.value = value;
|
|
|
|
ce->hits = 1;
|
|
|
|
ce->lastHit = PR_Now();
|
2002-02-05 23:55:43 +00:00
|
|
|
if (ownArena) {
|
|
|
|
ce->arena = arena;
|
|
|
|
}
|
2002-04-18 17:52:55 +00:00
|
|
|
ce->nickname = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
return ce;
|
|
|
|
}
|
|
|
|
|
2001-10-15 17:18:06 +00:00
|
|
|
/* sort the subject list from newest to oldest */
|
|
|
|
static PRIntn subject_list_sort(void *v1, void *v2)
|
|
|
|
{
|
|
|
|
NSSCertificate *c1 = (NSSCertificate *)v1;
|
|
|
|
NSSCertificate *c2 = (NSSCertificate *)v2;
|
|
|
|
nssDecodedCert *dc1 = nssCertificate_GetDecoding(c1);
|
|
|
|
nssDecodedCert *dc2 = nssCertificate_GetDecoding(c2);
|
|
|
|
if (dc1->isNewerThan(dc1, dc2)) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
/* this should not be exposed in a header, but is here to keep the above
|
|
|
|
* types/functions static
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT PRStatus
|
|
|
|
nssTrustDomain_InitializeCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
PRUint32 cacheSize
|
|
|
|
)
|
|
|
|
{
|
2001-11-28 16:23:51 +00:00
|
|
|
NSSArena *arena;
|
|
|
|
nssTDCertificateCache *cache = td->cache;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
s_log = PR_NewLogModule("nss_cache");
|
|
|
|
PR_ASSERT(s_log);
|
|
|
|
#endif
|
|
|
|
PR_ASSERT(!cache);
|
|
|
|
arena = nssArena_Create();
|
|
|
|
if (!arena) {
|
2001-10-11 16:34:49 +00:00
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
cache = nss_ZNEW(arena, nssTDCertificateCache);
|
|
|
|
if (!cache) {
|
|
|
|
nssArena_Destroy(arena);
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
cache->lock = PZ_NewLock(nssILockCache);
|
|
|
|
if (!cache->lock) {
|
|
|
|
nssArena_Destroy(arena);
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
/* Create the issuer and serial DER --> certificate hash */
|
2001-12-14 17:32:23 +00:00
|
|
|
cache->issuerAndSN = nssHash_CreateCertificate(arena, cacheSize);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (!cache->issuerAndSN) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
/* Create the subject DER --> subject list hash */
|
2001-11-28 16:23:51 +00:00
|
|
|
cache->subject = nssHash_CreateItem(arena, cacheSize);
|
|
|
|
if (!cache->subject) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
/* Create the nickname --> subject list hash */
|
2001-11-28 16:23:51 +00:00
|
|
|
cache->nickname = nssHash_CreateString(arena, cacheSize);
|
|
|
|
if (!cache->nickname) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
/* Create the email --> list of subject lists hash */
|
2001-11-28 16:23:51 +00:00
|
|
|
cache->email = nssHash_CreateString(arena, cacheSize);
|
|
|
|
if (!cache->email) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
cache->arena = arena;
|
|
|
|
td->cache = cache;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("Cache initialized."));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
return PR_SUCCESS;
|
|
|
|
loser:
|
2001-11-28 16:23:51 +00:00
|
|
|
PZ_DestroyLock(cache->lock);
|
|
|
|
nssArena_Destroy(arena);
|
2001-10-11 16:34:49 +00:00
|
|
|
td->cache = NULL;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("Cache initialization failed."));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-02-05 23:55:43 +00:00
|
|
|
/* The entries of the hashtable are currently dependent on the certificate(s)
|
|
|
|
* that produced them. That is, the entries will be freed when the cert is
|
|
|
|
* released from the cache. If there are certs in the cache at any time,
|
|
|
|
* including shutdown, the hash table entries will hold memory. In order for
|
|
|
|
* clean shutdown, it is necessary for there to be no certs in the cache.
|
|
|
|
*/
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
NSS_IMPLEMENT PRStatus
|
|
|
|
nssTrustDomain_DestroyCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td
|
|
|
|
)
|
|
|
|
{
|
2001-11-29 19:34:08 +00:00
|
|
|
if (!td->cache) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_DestroyLock(td->cache->lock);
|
2001-11-29 19:34:08 +00:00
|
|
|
nssHash_Destroy(td->cache->issuerAndSN);
|
|
|
|
nssHash_Destroy(td->cache->subject);
|
|
|
|
nssHash_Destroy(td->cache->nickname);
|
|
|
|
nssHash_Destroy(td->cache->email);
|
2001-11-28 16:23:51 +00:00
|
|
|
nssArena_Destroy(td->cache->arena);
|
2001-10-11 16:34:49 +00:00
|
|
|
td->cache = NULL;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("Cache destroyed."));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRStatus
|
2001-11-28 16:23:51 +00:00
|
|
|
remove_issuer_and_serial_entry
|
|
|
|
(
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/* Remove the cert from the issuer/serial hash */
|
|
|
|
nssHash_Remove(cache->issuerAndSN, cert);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("removed issuer/sn", cert);
|
|
|
|
#endif
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRStatus
|
|
|
|
remove_subject_entry
|
|
|
|
(
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert,
|
2002-02-05 23:55:43 +00:00
|
|
|
nssList **subjectList,
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 **nickname,
|
2002-02-05 23:55:43 +00:00
|
|
|
NSSArena **arena
|
2001-11-28 16:23:51 +00:00
|
|
|
)
|
2001-10-11 16:34:49 +00:00
|
|
|
{
|
|
|
|
PRStatus nssrv;
|
2001-11-28 16:23:51 +00:00
|
|
|
cache_entry *ce;
|
|
|
|
*subjectList = NULL;
|
2002-02-05 23:55:43 +00:00
|
|
|
*arena = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
/* Get the subject list for the cert's subject */
|
|
|
|
ce = (cache_entry *)nssHash_Lookup(cache->subject, &cert->subject);
|
|
|
|
if (ce) {
|
|
|
|
/* Remove the cert from the subject hash */
|
|
|
|
nssList_Remove(ce->entry.list, cert);
|
|
|
|
*subjectList = ce->entry.list;
|
2002-04-18 17:52:55 +00:00
|
|
|
*nickname = ce->nickname;
|
2002-02-05 23:55:43 +00:00
|
|
|
*arena = ce->arena;
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv = PR_SUCCESS;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("removed cert", cert);
|
|
|
|
log_item_dump("from subject list", &cert->subject);
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
} else {
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv = PR_FAILURE;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
return nssrv;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
static PRStatus
|
|
|
|
remove_nickname_entry
|
2001-10-11 16:34:49 +00:00
|
|
|
(
|
2001-11-28 16:23:51 +00:00
|
|
|
nssTDCertificateCache *cache,
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *nickname,
|
2001-11-28 16:23:51 +00:00
|
|
|
nssList *subjectList
|
2001-10-11 16:34:49 +00:00
|
|
|
)
|
|
|
|
{
|
2001-11-28 16:23:51 +00:00
|
|
|
PRStatus nssrv;
|
2002-03-07 22:08:00 +00:00
|
|
|
if (nickname) {
|
|
|
|
nssHash_Remove(cache->nickname, nickname);
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv = PR_SUCCESS;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2002-03-07 22:08:00 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("removed nickname %s", nickname));
|
2001-11-28 16:23:51 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
nssrv = PR_FAILURE;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
return nssrv;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
static PRStatus
|
|
|
|
remove_email_entry
|
|
|
|
(
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert,
|
|
|
|
nssList *subjectList
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PRStatus nssrv = PR_FAILURE;
|
|
|
|
cache_entry *ce;
|
|
|
|
/* Find the subject list in the email hash */
|
|
|
|
if (cert->email) {
|
|
|
|
ce = (cache_entry *)nssHash_Lookup(cache->email, cert->email);
|
|
|
|
if (ce) {
|
|
|
|
nssList *subjects = ce->entry.list;
|
|
|
|
/* Remove the subject list from the email hash */
|
|
|
|
nssList_Remove(subjects, subjectList);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_item_dump("removed subject list", &cert->subject);
|
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("for email %s", cert->email));
|
|
|
|
#endif
|
|
|
|
if (nssList_Count(subjects) == 0) {
|
|
|
|
/* No more subject lists for email, delete list and
|
|
|
|
* remove hash entry
|
|
|
|
*/
|
|
|
|
(void)nssList_Destroy(subjects);
|
|
|
|
nssHash_Remove(cache->email, cert->email);
|
2002-02-05 23:55:43 +00:00
|
|
|
/* there are no entries left for this address, free space
|
|
|
|
* used for email entries
|
|
|
|
*/
|
|
|
|
nssArena_Destroy(ce->arena);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("removed email %s", cert->email));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
nssrv = PR_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nssrv;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
NSS_IMPLEMENT void
|
2001-10-11 16:34:49 +00:00
|
|
|
nssTrustDomain_RemoveCertFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSCertificate *cert
|
|
|
|
)
|
|
|
|
{
|
|
|
|
nssList *subjectList;
|
2001-11-28 16:23:51 +00:00
|
|
|
PRStatus nssrv;
|
2002-01-11 00:41:26 +00:00
|
|
|
cache_entry *ce;
|
2002-02-05 23:55:43 +00:00
|
|
|
NSSArena *arena;
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *nickname;
|
|
|
|
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("attempt to remove cert", cert);
|
2001-10-11 16:34:49 +00:00
|
|
|
#endif
|
2001-11-28 16:23:51 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
2002-01-11 00:41:26 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->issuerAndSN, cert);
|
|
|
|
if (!ce || ce->entry.cert != cert) {
|
|
|
|
/* If it's not in the cache, or a different cert is (this is really
|
|
|
|
* for safety reasons, though it shouldn't happen), do nothing
|
|
|
|
*/
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("but it wasn't in the cache"));
|
|
|
|
#endif
|
|
|
|
return;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv = remove_issuer_and_serial_entry(td->cache, cert);
|
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2002-04-18 17:52:55 +00:00
|
|
|
nssrv = remove_subject_entry(td->cache, cert, &subjectList,
|
|
|
|
&nickname, &arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
if (nssList_Count(subjectList) == 0) {
|
|
|
|
PRStatus nssrv2;
|
2002-04-18 17:52:55 +00:00
|
|
|
nssrv = remove_nickname_entry(td->cache, nickname, subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv2 = remove_email_entry(td->cache, cert, subjectList);
|
|
|
|
#ifndef NSS_3_4_CODE
|
|
|
|
/* XXX Again, 3.4 allows for certs w/o either nickname or email */
|
|
|
|
if (nssrv != PR_SUCCESS && nssrv2 != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
(void)nssList_Destroy(subjectList);
|
|
|
|
nssHash_Remove(td->cache->subject, &cert->subject);
|
2002-02-05 23:55:43 +00:00
|
|
|
/* there are no entries left for this subject, free the space used
|
|
|
|
* for both the nickname and subject entries
|
|
|
|
*/
|
|
|
|
if (arena) {
|
|
|
|
nssArena_Destroy(arena);
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-01-03 20:09:30 +00:00
|
|
|
NSSCertificate_Destroy(cert); /* release the reference */
|
2001-11-28 16:23:51 +00:00
|
|
|
return;
|
|
|
|
loser:
|
|
|
|
/* if here, then the cache is inconsistent. For now, flush it. */
|
|
|
|
PZ_Unlock(td->cache->lock);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("remove cert failed, flushing"));
|
|
|
|
#endif
|
|
|
|
nssTrustDomain_FlushCache(td, -1.0);
|
2002-01-03 20:09:30 +00:00
|
|
|
NSSCertificate_Destroy(cert); /* release the reference */
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is used to remove all certs below a certain threshold, where
|
|
|
|
* the value is determined by how many times the cert has been requested
|
|
|
|
* and when the last request was.
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT void
|
|
|
|
nssTrustDomain_FlushCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
PRFloat64 threshold
|
|
|
|
)
|
|
|
|
{
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2002-04-19 23:06:44 +00:00
|
|
|
struct token_cert_dtor {
|
2001-10-11 16:34:49 +00:00
|
|
|
NSSToken *token;
|
2002-04-19 23:06:44 +00:00
|
|
|
nssTDCertificateCache *cache;
|
|
|
|
NSSCertificate **certs;
|
|
|
|
PRUint32 numCerts, arrSize;
|
2001-10-11 16:34:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_token_certs(const void *k, void *v, void *a)
|
|
|
|
{
|
2002-04-19 23:06:44 +00:00
|
|
|
NSSCertificate *c = (NSSCertificate *)k;
|
|
|
|
nssPKIObject *object = &c->object;
|
|
|
|
struct token_cert_dtor *dtor = a;
|
|
|
|
PRUint32 i;
|
|
|
|
PZ_Lock(object->lock);
|
|
|
|
for (i=0; i<object->numInstances; i++) {
|
|
|
|
if (object->instances[i]->token == dtor->token) {
|
|
|
|
nssCryptokiObject_Destroy(object->instances[i]);
|
|
|
|
object->instances[i] = object->instances[object->numInstances-1];
|
|
|
|
object->instances[object->numInstances-1] = NULL;
|
|
|
|
object->numInstances--;
|
|
|
|
dtor->certs[dtor->numCerts++] = nssCertificate_AddRef(c);
|
|
|
|
if (dtor->numCerts == dtor->arrSize) {
|
|
|
|
dtor->arrSize *= 2;
|
|
|
|
dtor->certs = nss_ZREALLOCARRAY(dtor->certs,
|
|
|
|
NSSCertificate *,
|
|
|
|
dtor->arrSize);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2002-04-19 23:06:44 +00:00
|
|
|
PZ_Unlock(object->lock);
|
|
|
|
return;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove all certs for the given token from the cache. This is
|
2002-01-11 00:41:26 +00:00
|
|
|
* needed if the token is removed.
|
2001-10-11 16:34:49 +00:00
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT PRStatus
|
|
|
|
nssTrustDomain_RemoveTokenCertsFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSToken *token
|
|
|
|
)
|
|
|
|
{
|
2002-04-19 23:06:44 +00:00
|
|
|
NSSCertificate **certs;
|
|
|
|
PRUint32 i, arrSize = 10;
|
|
|
|
struct token_cert_dtor dtor;
|
|
|
|
certs = nss_ZNEWARRAY(NULL, NSSCertificate *, arrSize);
|
|
|
|
if (!certs) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
dtor.cache = td->cache;
|
|
|
|
dtor.token = token;
|
|
|
|
dtor.certs = certs;
|
|
|
|
dtor.numCerts = 0;
|
|
|
|
dtor.arrSize = arrSize;
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
2002-04-19 23:06:44 +00:00
|
|
|
nssHash_Iterate(td->cache->issuerAndSN, remove_token_certs, (void *)&dtor);
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-04-19 23:06:44 +00:00
|
|
|
for (i=0; i<dtor.numCerts; i++) {
|
|
|
|
if (dtor.certs[i]->object.numInstances == 0) {
|
|
|
|
nssTrustDomain_RemoveCertFromCache(td, dtor.certs[i]);
|
|
|
|
} else {
|
|
|
|
STAN_ForceCERTCertificateUpdate(dtor.certs[i]);
|
|
|
|
}
|
|
|
|
nssCertificate_Destroy(dtor.certs[i]);
|
|
|
|
}
|
|
|
|
nss_ZFreeIf(dtor.certs);
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSS_IMPLEMENT PRStatus
|
|
|
|
nssTrustDomain_UpdateCachedTokenCerts
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSToken *token
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate **cp, **cached = NULL;
|
|
|
|
nssList *certList;
|
|
|
|
PRUint32 count;
|
|
|
|
certList = nssList_Create(NULL, PR_FALSE);
|
|
|
|
if (!certList) return PR_FAILURE;
|
|
|
|
(void *)nssTrustDomain_GetCertsFromCache(td, certList);
|
|
|
|
count = nssList_Count(certList);
|
|
|
|
if (count > 0) {
|
|
|
|
cached = nss_ZNEWARRAY(NULL, NSSCertificate *, count + 1);
|
|
|
|
if (!cached) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
nssList_GetArray(certList, (void **)cached, count);
|
|
|
|
nssList_Destroy(certList);
|
|
|
|
for (cp = cached; *cp; cp++) {
|
|
|
|
nssCryptokiObject *instance;
|
|
|
|
NSSCertificate *c = *cp;
|
|
|
|
nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly;
|
|
|
|
instance = nssToken_FindCertificateByIssuerAndSerialNumber(
|
|
|
|
token,
|
|
|
|
NULL,
|
|
|
|
&c->issuer,
|
|
|
|
&c->serial,
|
|
|
|
tokenOnly,
|
|
|
|
NULL);
|
|
|
|
if (instance) {
|
|
|
|
nssPKIObject_AddInstance(&c->object, instance);
|
|
|
|
STAN_ForceCERTCertificateUpdate(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nssCertificateArray_Destroy(cached);
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
static PRStatus
|
|
|
|
add_issuer_and_serial_entry
|
|
|
|
(
|
|
|
|
NSSArena *arena,
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert
|
|
|
|
)
|
|
|
|
{
|
|
|
|
cache_entry *ce;
|
2002-02-05 23:55:43 +00:00
|
|
|
ce = new_cache_entry(arena, (void *)cert, PR_FALSE);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("added to issuer/sn", cert);
|
|
|
|
#endif
|
2002-01-10 14:28:53 +00:00
|
|
|
return nssHash_Add(cache->issuerAndSN, cert, (void *)ce);
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PRStatus
|
|
|
|
add_subject_entry
|
|
|
|
(
|
|
|
|
NSSArena *arena,
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert,
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *nickname,
|
2001-11-28 16:23:51 +00:00
|
|
|
nssList **subjectList
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PRStatus nssrv;
|
|
|
|
nssList *list;
|
|
|
|
cache_entry *ce;
|
|
|
|
*subjectList = NULL; /* this is only set if a new one is created */
|
|
|
|
ce = (cache_entry *)nssHash_Lookup(cache->subject, &cert->subject);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
|
|
|
/* The subject is already in, add this cert to the list */
|
|
|
|
nssrv = nssList_AddUnique(ce->entry.list, cert);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("added to existing subject list", cert);
|
|
|
|
#endif
|
|
|
|
} else {
|
2002-01-09 21:09:21 +00:00
|
|
|
NSSDER *subject;
|
2001-11-28 16:23:51 +00:00
|
|
|
/* Create a new subject list for the subject */
|
|
|
|
list = nssList_Create(arena, PR_FALSE);
|
|
|
|
if (!list) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2002-02-05 23:55:43 +00:00
|
|
|
ce = new_cache_entry(arena, (void *)list, PR_TRUE);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (!ce) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2002-04-18 17:52:55 +00:00
|
|
|
if (nickname) {
|
|
|
|
ce->nickname = nssUTF8_Duplicate(nickname, arena);
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
nssList_SetSortFunction(list, subject_list_sort);
|
|
|
|
/* Add the cert entry to this list of subjects */
|
|
|
|
nssrv = nssList_AddUnique(list, cert);
|
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
/* Add the subject list to the cache */
|
2002-01-09 21:09:21 +00:00
|
|
|
subject = nssItem_Duplicate(&cert->subject, arena, NULL);
|
|
|
|
if (!subject) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
nssrv = nssHash_Add(cache->subject, subject, ce);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
*subjectList = list;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("created subject list", cert);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRStatus
|
|
|
|
add_nickname_entry
|
|
|
|
(
|
|
|
|
NSSArena *arena,
|
|
|
|
nssTDCertificateCache *cache,
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *certNickname,
|
2001-11-28 16:23:51 +00:00
|
|
|
nssList *subjectList
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PRStatus nssrv = PR_SUCCESS;
|
|
|
|
cache_entry *ce;
|
2002-03-07 22:08:00 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(cache->nickname, certNickname);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (ce) {
|
|
|
|
/* This is a collision. A nickname entry already exists for this
|
|
|
|
* subject, but a subject entry didn't. This would imply there are
|
|
|
|
* two subjects using the same nickname, which is not allowed.
|
|
|
|
*/
|
|
|
|
return PR_FAILURE;
|
|
|
|
} else {
|
2002-01-09 21:09:21 +00:00
|
|
|
NSSUTF8 *nickname;
|
2002-02-05 23:55:43 +00:00
|
|
|
ce = new_cache_entry(arena, subjectList, PR_FALSE);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (!ce) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2002-03-07 22:08:00 +00:00
|
|
|
nickname = nssUTF8_Duplicate(certNickname, arena);
|
2002-01-09 21:09:21 +00:00
|
|
|
if (!nickname) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
nssrv = nssHash_Add(cache->nickname, nickname, ce);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("created nickname for", cert);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRStatus
|
|
|
|
add_email_entry
|
|
|
|
(
|
|
|
|
nssTDCertificateCache *cache,
|
|
|
|
NSSCertificate *cert,
|
|
|
|
nssList *subjectList
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PRStatus nssrv = PR_SUCCESS;
|
|
|
|
nssList *subjects;
|
|
|
|
cache_entry *ce;
|
|
|
|
ce = (cache_entry *)nssHash_Lookup(cache->email, cert->email);
|
|
|
|
if (ce) {
|
|
|
|
/* Already have an entry for this email address, but not subject */
|
|
|
|
subjects = ce->entry.list;
|
|
|
|
nssrv = nssList_AddUnique(subjects, subjectList);
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("added subject to email for", cert);
|
|
|
|
#endif
|
|
|
|
} else {
|
2002-01-09 21:09:21 +00:00
|
|
|
NSSASCII7 *email;
|
2002-02-05 23:55:43 +00:00
|
|
|
NSSArena *arena;
|
|
|
|
arena = nssArena_Create();
|
|
|
|
if (!arena) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
/* Create a new list of subject lists, add this subject */
|
|
|
|
subjects = nssList_Create(arena, PR_TRUE);
|
|
|
|
if (!subjects) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssArena_Destroy(arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
/* Add the new subject to the list */
|
|
|
|
nssrv = nssList_AddUnique(subjects, subjectList);
|
|
|
|
if (nssrv != PR_SUCCESS) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssArena_Destroy(arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
/* Add the new entry to the cache */
|
2002-02-05 23:55:43 +00:00
|
|
|
ce = new_cache_entry(arena, (void *)subjects, PR_TRUE);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (!ce) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssArena_Destroy(arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
2002-01-09 21:09:21 +00:00
|
|
|
email = nssUTF8_Duplicate(cert->email, arena);
|
|
|
|
if (!email) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssArena_Destroy(arena);
|
2002-01-09 21:09:21 +00:00
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
nssrv = nssHash_Add(cache->email, email, ce);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssArena_Destroy(arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
return nssrv;
|
|
|
|
}
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("created email for", cert);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return nssrv;
|
|
|
|
}
|
|
|
|
|
2001-12-10 19:05:51 +00:00
|
|
|
extern const NSSError NSS_ERROR_CERTIFICATE_IN_CACHE;
|
|
|
|
|
2002-04-15 15:22:11 +00:00
|
|
|
static NSSCertificate *
|
2001-11-28 16:23:51 +00:00
|
|
|
add_cert_to_cache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSCertificate *cert
|
|
|
|
)
|
|
|
|
{
|
2002-02-05 23:55:43 +00:00
|
|
|
NSSArena *arena = NULL;
|
2002-04-18 17:52:55 +00:00
|
|
|
nssList *subjectList = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
PRStatus nssrv;
|
|
|
|
PRUint32 added = 0;
|
2002-04-15 15:22:11 +00:00
|
|
|
cache_entry *ce;
|
|
|
|
NSSCertificate *rvCert = NULL;
|
2002-04-18 17:52:55 +00:00
|
|
|
NSSUTF8 *certNickname = nssCertificate_GetNickname(cert, NULL);
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
|
|
|
/* If it exists in the issuer/serial hash, it's already in all */
|
2002-04-15 15:22:11 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->issuerAndSN, cert);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
|
|
|
rvCert = nssCertificate_AddRef(ce->entry.cert);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_cert_ref("attempted to add cert already in cache", cert);
|
|
|
|
#endif
|
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-04-15 15:22:11 +00:00
|
|
|
/* collision - somebody else already added the cert
|
2001-12-10 19:05:51 +00:00
|
|
|
* to the cache before this thread got around to it.
|
|
|
|
*/
|
2002-04-15 15:22:11 +00:00
|
|
|
nssCertificate_Destroy(cert);
|
|
|
|
return rvCert;
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
2002-02-05 23:55:43 +00:00
|
|
|
/* create a new cache entry for this cert within the cert's arena*/
|
|
|
|
nssrv = add_issuer_and_serial_entry(cert->object.arena, td->cache, cert);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
added++;
|
2002-02-05 23:55:43 +00:00
|
|
|
/* create an arena for the nickname and subject entries */
|
|
|
|
arena = nssArena_Create();
|
|
|
|
if (!arena) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
/* create a new subject list for this cert, or add to existing */
|
2002-04-18 17:52:55 +00:00
|
|
|
nssrv = add_subject_entry(arena, td->cache, cert,
|
|
|
|
certNickname, &subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
added++;
|
|
|
|
/* If a new subject entry was created, also need nickname and/or email */
|
|
|
|
if (subjectList != NULL) {
|
|
|
|
PRBool handle = PR_FALSE;
|
2002-03-07 22:08:00 +00:00
|
|
|
if (certNickname) {
|
2002-04-18 17:52:55 +00:00
|
|
|
nssrv = add_nickname_entry(arena, td->cache,
|
|
|
|
certNickname, subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
handle = PR_TRUE;
|
|
|
|
added++;
|
|
|
|
}
|
|
|
|
if (cert->email) {
|
2002-02-05 23:55:43 +00:00
|
|
|
nssrv = add_email_entry(td->cache, cert, subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
handle = PR_TRUE;
|
|
|
|
added += 2;
|
|
|
|
}
|
|
|
|
#ifdef nodef
|
|
|
|
/* I think either a nickname or email address must be associated
|
|
|
|
* with the cert. However, certs are passed to NewTemp without
|
|
|
|
* either. This worked in the old code, so it must work now.
|
|
|
|
*/
|
|
|
|
if (!handle) {
|
|
|
|
/* Require either nickname or email handle */
|
|
|
|
nssrv = PR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2002-01-03 20:09:30 +00:00
|
|
|
nssCertificate_AddRef(cert);
|
2002-02-08 02:51:41 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-04-15 15:22:11 +00:00
|
|
|
return rvCert;
|
2001-11-28 16:23:51 +00:00
|
|
|
loser:
|
|
|
|
/* Remove any handles that have been created */
|
2002-03-04 21:06:10 +00:00
|
|
|
subjectList = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
if (added >= 1) {
|
|
|
|
(void)remove_issuer_and_serial_entry(td->cache, cert);
|
|
|
|
}
|
|
|
|
if (added >= 2) {
|
2002-04-18 17:52:55 +00:00
|
|
|
(void)remove_subject_entry(td->cache, cert, &subjectList,
|
|
|
|
&certNickname, &arena);
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
if (added == 3 || added == 5) {
|
2002-04-18 17:52:55 +00:00
|
|
|
(void)remove_nickname_entry(td->cache, certNickname, subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
if (added >= 4) {
|
|
|
|
(void)remove_email_entry(td->cache, cert, subjectList);
|
|
|
|
}
|
2002-03-04 21:06:10 +00:00
|
|
|
if (subjectList) {
|
|
|
|
nssHash_Remove(td->cache->subject, &cert->subject);
|
|
|
|
nssList_Destroy(subjectList);
|
|
|
|
}
|
2002-02-05 23:55:43 +00:00
|
|
|
if (arena) {
|
|
|
|
nssArena_Destroy(arena);
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-04-15 15:22:11 +00:00
|
|
|
return NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSS_IMPLEMENT PRStatus
|
|
|
|
nssTrustDomain_AddCertsToCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSCertificate **certs,
|
|
|
|
PRUint32 numCerts
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PRUint32 i;
|
2002-04-15 15:22:11 +00:00
|
|
|
NSSCertificate *c;
|
2001-11-28 16:23:51 +00:00
|
|
|
for (i=0; i<numCerts && certs[i]; i++) {
|
2002-04-15 15:22:11 +00:00
|
|
|
c = add_cert_to_cache(td, certs[i]);
|
|
|
|
if (c == NULL) {
|
2001-11-28 16:23:51 +00:00
|
|
|
return PR_FAILURE;
|
2002-04-15 15:22:11 +00:00
|
|
|
} else {
|
|
|
|
certs[i] = c;
|
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
}
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2001-10-11 16:34:49 +00:00
|
|
|
static NSSCertificate **
|
|
|
|
collect_subject_certs
|
|
|
|
(
|
|
|
|
nssList *subjectList,
|
|
|
|
nssList *rvCertListOpt
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate *c;
|
2001-11-08 00:15:51 +00:00
|
|
|
NSSCertificate **rvArray = NULL;
|
2002-02-08 02:51:41 +00:00
|
|
|
PRUint32 count;
|
2002-02-08 21:47:05 +00:00
|
|
|
nssCertificateList_AddReferences(subjectList);
|
2001-10-11 16:34:49 +00:00
|
|
|
if (rvCertListOpt) {
|
|
|
|
nssListIterator *iter = nssList_CreateIterator(subjectList);
|
|
|
|
for (c = (NSSCertificate *)nssListIterator_Start(iter);
|
|
|
|
c != (NSSCertificate *)NULL;
|
|
|
|
c = (NSSCertificate *)nssListIterator_Next(iter)) {
|
2002-02-08 02:51:41 +00:00
|
|
|
nssList_Add(rvCertListOpt, c);
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
nssListIterator_Finish(iter);
|
|
|
|
nssListIterator_Destroy(iter);
|
|
|
|
} else {
|
|
|
|
count = nssList_Count(subjectList);
|
2001-11-28 16:23:51 +00:00
|
|
|
rvArray = nss_ZNEWARRAY(NULL, NSSCertificate *, count + 1);
|
|
|
|
if (!rvArray) {
|
|
|
|
return (NSSCertificate **)NULL;
|
|
|
|
}
|
2001-10-11 16:34:49 +00:00
|
|
|
nssList_GetArray(subjectList, (void **)rvArray, count);
|
|
|
|
}
|
2001-11-08 00:15:51 +00:00
|
|
|
return rvArray;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all cached certs with this subject.
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT NSSCertificate **
|
|
|
|
nssTrustDomain_GetCertsForSubjectFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSDER *subject,
|
|
|
|
nssList *certListOpt
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate **rvArray = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
cache_entry *ce;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_item_dump("looking for cert by subject", subject);
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
2001-11-28 16:23:51 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->subject, subject);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("... found, %d hits", ce->hits));
|
|
|
|
#endif
|
|
|
|
rvArray = collect_subject_certs(ce->entry.list, certListOpt);
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2002-02-08 21:47:05 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2001-10-11 16:34:49 +00:00
|
|
|
return rvArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all cached certs with this label.
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT NSSCertificate **
|
|
|
|
nssTrustDomain_GetCertsForNicknameFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSUTF8 *nickname,
|
|
|
|
nssList *certListOpt
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate **rvArray = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
cache_entry *ce;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("looking for cert by nick %s", nickname));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
2001-11-28 16:23:51 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->nickname, nickname);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("... found, %d hits", ce->hits));
|
|
|
|
#endif
|
|
|
|
rvArray = collect_subject_certs(ce->entry.list, certListOpt);
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2002-02-08 21:47:05 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2001-10-11 16:34:49 +00:00
|
|
|
return rvArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all cached certs with this email address.
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT NSSCertificate **
|
|
|
|
nssTrustDomain_GetCertsForEmailAddressFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSASCII7 *email,
|
|
|
|
nssList *certListOpt
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate **rvArray = NULL;
|
2001-11-28 16:23:51 +00:00
|
|
|
cache_entry *ce;
|
2001-12-07 01:36:25 +00:00
|
|
|
nssList *collectList = NULL;
|
2002-02-08 02:51:41 +00:00
|
|
|
nssListIterator *iter = NULL;
|
|
|
|
nssList *subjectList;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("looking for cert by email %s", email));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
PZ_Lock(td->cache->lock);
|
2001-11-28 16:23:51 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->email, email);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("... found, %d hits", ce->hits));
|
|
|
|
#endif
|
2002-02-08 02:51:41 +00:00
|
|
|
/* loop over subject lists and get refs for certs */
|
2001-10-11 16:34:49 +00:00
|
|
|
if (certListOpt) {
|
|
|
|
collectList = certListOpt;
|
|
|
|
} else {
|
|
|
|
collectList = nssList_Create(NULL, PR_FALSE);
|
2002-02-08 21:47:05 +00:00
|
|
|
if (!collectList) {
|
|
|
|
PZ_Unlock(td->cache->lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter = nssList_CreateIterator(ce->entry.list);
|
|
|
|
if (!iter) {
|
|
|
|
PZ_Unlock(td->cache->lock);
|
|
|
|
if (!certListOpt) {
|
|
|
|
nssList_Destroy(collectList);
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
for (subjectList = (nssList *)nssListIterator_Start(iter);
|
|
|
|
subjectList != (nssList *)NULL;
|
|
|
|
subjectList = (nssList *)nssListIterator_Next(iter)) {
|
|
|
|
(void)collect_subject_certs(subjectList, collectList);
|
|
|
|
}
|
|
|
|
nssListIterator_Finish(iter);
|
|
|
|
nssListIterator_Destroy(iter);
|
|
|
|
}
|
2002-02-08 21:47:05 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2001-12-07 01:36:25 +00:00
|
|
|
if (!certListOpt && collectList) {
|
2001-10-11 16:34:49 +00:00
|
|
|
PRUint32 count = nssList_Count(collectList);
|
|
|
|
rvArray = nss_ZNEWARRAY(NULL, NSSCertificate *, count);
|
|
|
|
if (rvArray) {
|
|
|
|
nssList_GetArray(collectList, (void **)rvArray, count);
|
|
|
|
}
|
|
|
|
nssList_Destroy(collectList);
|
|
|
|
}
|
|
|
|
return rvArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a specific cert in the cache
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT NSSCertificate *
|
|
|
|
nssTrustDomain_GetCertForIssuerAndSNFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSDER *issuer,
|
|
|
|
NSSDER *serial
|
|
|
|
)
|
|
|
|
{
|
2001-11-28 16:23:51 +00:00
|
|
|
NSSCertificate certkey;
|
|
|
|
NSSCertificate *rvCert = NULL;
|
|
|
|
cache_entry *ce;
|
|
|
|
certkey.issuer.data = issuer->data;
|
|
|
|
certkey.issuer.size = issuer->size;
|
|
|
|
certkey.serial.data = serial->data;
|
|
|
|
certkey.serial.size = serial->size;
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_item_dump("looking for cert by issuer/sn, issuer", issuer);
|
|
|
|
log_item_dump(" serial", serial);
|
2001-10-11 16:34:49 +00:00
|
|
|
#endif
|
|
|
|
PZ_Lock(td->cache->lock);
|
2001-11-28 16:23:51 +00:00
|
|
|
ce = (cache_entry *)nssHash_Lookup(td->cache->issuerAndSN, &certkey);
|
|
|
|
if (ce) {
|
|
|
|
ce->hits++;
|
|
|
|
ce->lastHit = PR_Now();
|
2002-02-08 02:51:41 +00:00
|
|
|
rvCert = nssCertificate_AddRef(ce->entry.cert);
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
PR_LOG(s_log, PR_LOG_DEBUG, ("... found, %d hits", ce->hits));
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2001-11-28 16:23:51 +00:00
|
|
|
PZ_Unlock(td->cache->lock);
|
2002-02-08 02:51:41 +00:00
|
|
|
return rvCert;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2001-11-28 16:23:51 +00:00
|
|
|
#ifdef NSS_3_4_CODE
|
|
|
|
static PRStatus
|
|
|
|
issuer_and_serial_from_encoding
|
|
|
|
(
|
|
|
|
NSSBER *encoding,
|
|
|
|
NSSDER *issuer,
|
|
|
|
NSSDER *serial
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SECItem derCert, derIssuer, derSerial;
|
|
|
|
SECStatus secrv;
|
|
|
|
derCert.data = (unsigned char *)encoding->data;
|
|
|
|
derCert.len = encoding->size;
|
|
|
|
secrv = CERT_IssuerNameFromDERCert(&derCert, &derIssuer);
|
|
|
|
if (secrv != SECSuccess) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
secrv = CERT_SerialNumberFromDERCert(&derCert, &derSerial);
|
|
|
|
if (secrv != SECSuccess) {
|
|
|
|
return PR_FAILURE;
|
|
|
|
}
|
|
|
|
issuer->data = derIssuer.data;
|
|
|
|
issuer->size = derIssuer.len;
|
|
|
|
serial->data = derSerial.data;
|
|
|
|
serial->size = derSerial.len;
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
2001-10-11 16:34:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a specific cert in the cache
|
|
|
|
*/
|
|
|
|
NSS_IMPLEMENT NSSCertificate *
|
|
|
|
nssTrustDomain_GetCertByDERFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
NSSDER *der
|
|
|
|
)
|
|
|
|
{
|
2001-11-28 16:23:51 +00:00
|
|
|
PRStatus nssrv = PR_FAILURE;
|
|
|
|
NSSDER issuer, serial;
|
|
|
|
NSSCertificate *rvCert;
|
2001-10-11 16:34:49 +00:00
|
|
|
#ifdef NSS_3_4_CODE
|
2001-11-28 16:23:51 +00:00
|
|
|
nssrv = issuer_and_serial_from_encoding(der, &issuer, &serial);
|
2001-10-11 16:34:49 +00:00
|
|
|
#endif
|
2001-11-28 16:23:51 +00:00
|
|
|
if (nssrv != PR_SUCCESS) {
|
|
|
|
return NULL;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
2001-11-28 20:19:38 +00:00
|
|
|
#ifdef DEBUG_CACHE
|
2001-11-28 16:23:51 +00:00
|
|
|
log_item_dump("looking for cert by DER", der);
|
|
|
|
#endif
|
|
|
|
rvCert = nssTrustDomain_GetCertForIssuerAndSNFromCache(td,
|
|
|
|
&issuer, &serial);
|
|
|
|
#ifdef NSS_3_4_CODE
|
|
|
|
PORT_Free(issuer.data);
|
|
|
|
PORT_Free(serial.data);
|
|
|
|
#endif
|
2002-02-08 02:51:41 +00:00
|
|
|
return rvCert;
|
2001-10-11 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 18:16:45 +00:00
|
|
|
static void cert_iter(const void *k, void *v, void *a)
|
|
|
|
{
|
|
|
|
nssList *certList = (nssList *)a;
|
2002-01-03 20:09:30 +00:00
|
|
|
NSSCertificate *c = (NSSCertificate *)k;
|
|
|
|
nssList_Add(certList, nssCertificate_AddRef(c));
|
2001-10-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSS_EXTERN NSSCertificate **
|
|
|
|
nssTrustDomain_GetCertsFromCache
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
nssList *certListOpt
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NSSCertificate **rvArray = NULL;
|
|
|
|
nssList *certList;
|
|
|
|
if (certListOpt) {
|
|
|
|
certList = certListOpt;
|
|
|
|
} else {
|
|
|
|
certList = nssList_Create(NULL, PR_FALSE);
|
|
|
|
}
|
|
|
|
PZ_Lock(td->cache->lock);
|
|
|
|
nssHash_Iterate(td->cache->issuerAndSN, cert_iter, (void *)certList);
|
|
|
|
PZ_Unlock(td->cache->lock);
|
|
|
|
if (!certListOpt) {
|
|
|
|
PRUint32 count = nssList_Count(certList);
|
|
|
|
rvArray = nss_ZNEWARRAY(NULL, NSSCertificate *, count);
|
|
|
|
nssList_GetArray(certList, (void **)rvArray, count);
|
2002-01-03 20:09:30 +00:00
|
|
|
/* array takes the references */
|
2001-10-19 18:16:45 +00:00
|
|
|
nssList_Destroy(certList);
|
|
|
|
}
|
|
|
|
return rvArray;
|
|
|
|
}
|
|
|
|
|
2002-01-31 17:08:32 +00:00
|
|
|
NSS_IMPLEMENT void
|
|
|
|
nssTrustDomain_DumpCacheInfo
|
|
|
|
(
|
|
|
|
NSSTrustDomain *td,
|
|
|
|
void (* cert_dump_iter)(const void *, void *, void *),
|
|
|
|
void *arg
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PZ_Lock(td->cache->lock);
|
|
|
|
nssHash_Iterate(td->cache->issuerAndSN, cert_dump_iter, arg);
|
|
|
|
PZ_Unlock(td->cache->lock);
|
|
|
|
}
|