From 6d9b00b3b6d309d3374cfdb16bda3b95196285b4 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Thu, 28 Jan 2021 22:44:46 +0100 Subject: [PATCH v23 8/8] NSS Make the cryptohash NSSInitContext static as an experiment We only need a static reference tot the NSS context, and opening one per cryptohash operation is incredibly wasteful given that it wasn't how NSS was designed to operate. This commit lacks the infrastructure for closing the context on backend exit though, hence experimental. --- src/common/cryptohash_nss.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/common/cryptohash_nss.c b/src/common/cryptohash_nss.c index d4fd2e0008..6b7e48a1b4 100644 --- a/src/common/cryptohash_nss.c +++ b/src/common/cryptohash_nss.c @@ -71,6 +71,8 @@ #define FREE(ptr) free(ptr) #endif +static NSSInitContext *nss_context = NULL; + /* * Internal pg_cryptohash_ctx structure. */ @@ -78,7 +80,6 @@ struct pg_cryptohash_ctx { pg_cryptohash_type type; - NSSInitContext *nss_context; PK11Context *pk11_context; HASH_HashType hash_type; @@ -141,16 +142,20 @@ pg_cryptohash_create(pg_cryptohash_type type) */ memset(¶ms, 0, sizeof(params)); params.length = sizeof(params); - ctx->nss_context = NSS_InitContext("", "", "", "", ¶ms, - NSS_INIT_READONLY | NSS_INIT_NOCERTDB | - NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN | - NSS_INIT_NOROOTINIT | NSS_INIT_PK11RELOAD); - if (!ctx->nss_context) + if (!nss_context) { - explicit_bzero(ctx, sizeof(pg_cryptohash_ctx)); - FREE(ctx); - goto error; + nss_context = NSS_InitContext("", "", "", "", ¶ms, + NSS_INIT_READONLY | NSS_INIT_NOCERTDB | + NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN | + NSS_INIT_NOROOTINIT | NSS_INIT_PK11RELOAD); + if (!nss_context) + { + explicit_bzero(ctx, sizeof(pg_cryptohash_ctx)); + FREE(ctx); + goto error; + } } + ctx->type = type; hash = SECOID_FindOIDByTag(ctx->hash_type); ctx->pk11_context = PK11_CreateDigestContext(hash->offset); @@ -252,7 +257,18 @@ pg_cryptohash_free(pg_cryptohash_ctx *ctx) #ifndef FRONTEND ResourceOwnerForgetCryptoHash(ctx->resowner, PointerGetDatum(ctx)); #endif - NSS_ShutdownContext(ctx->nss_context); explicit_bzero(ctx, sizeof(pg_cryptohash_ctx)); FREE(ctx); } + +/* + * TODO: We need a way to close the static NSS context when the backend is + * terminated. + * +void +pg_cryptohash_teardown(void) +{ + if (nss_context) + NSS_ShutdownContext(ctx->nss_context); +} + */ -- 2.21.1 (Apple Git-122.3)