Blame SOURCES/0029-Remove-NOMODDB-flag-flag-from-context-init-look-for-.patch

5e5f7c
From 34c120f0259750ff2228def2955de9ad985340e6 Mon Sep 17 00:00:00 2001
5e5f7c
From: Rob Crittenden <rcritten@redhat.com>
5e5f7c
Date: Mon, 26 Aug 2019 22:01:35 +0000
5e5f7c
Subject: [PATCH] Remove NOMODDB flag flag from context init, look for full
5e5f7c
 tokens
5e5f7c
5e5f7c
The NSS databases were almost universally initialized with the
5e5f7c
NOMODDB flag. I'm not sure if something changed in NSS but the
5e5f7c
PKCS#11 modules were not being initialized. Adding this back after
5e5f7c
permission checks are done results in tokens working again.
5e5f7c
5e5f7c
When looking for certs and keys try the full token:nickname string
5e5f7c
as well as just nickname when comparing values.
5e5f7c
5e5f7c
https://pagure.io/certmonger/issue/125
5e5f7c
---
5e5f7c
 src/casave.c     |  3 +--
5e5f7c
 src/certread-n.c | 33 ++++++++++++++++-----------------
5e5f7c
 src/certsave-n.c |  5 +++++
5e5f7c
 src/dogtag.c     |  3 +--
5e5f7c
 src/keygen-n.c   |  5 +++++
5e5f7c
 src/keyiread-n.c | 11 ++++++++++-
5e5f7c
 src/scepgen-n.c  |  5 +++++
5e5f7c
 src/submit-n.c   |  5 +++++
5e5f7c
 src/toklist.c    |  2 +-
5e5f7c
 9 files changed, 49 insertions(+), 23 deletions(-)
5e5f7c
5e5f7c
diff --git a/src/casave.c b/src/casave.c
5e5f7c
index bde63f99..1cf5a406 100644
5e5f7c
--- a/src/casave.c
5e5f7c
+++ b/src/casave.c
5e5f7c
@@ -111,8 +111,7 @@ cm_casave_main_n(int fd, struct cm_store_ca *ca, struct cm_store_entry *e,
5e5f7c
 					break;
5e5f7c
 				default:
5e5f7c
 					flags = NSS_INIT_READONLY |
5e5f7c
-						NSS_INIT_NOROOTINIT |
5e5f7c
-						NSS_INIT_NOMODDB;
5e5f7c
+						NSS_INIT_NOROOTINIT;
5e5f7c
 					/* Sigh.  Not a lot of detail.  Check
5e5f7c
 					 * if we succeed in read-only mode,
5e5f7c
 					 * which we'll interpret as lack of
5e5f7c
diff --git a/src/certread-n.c b/src/certread-n.c
5e5f7c
index d535030b..bb61b61b 100644
5e5f7c
--- a/src/certread-n.c
5e5f7c
+++ b/src/certread-n.c
5e5f7c
@@ -157,27 +157,22 @@ cm_certread_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 		cm_log(1, "Unable to open NSS database.\n");
5e5f7c
 		_exit(status);
5e5f7c
 	}
5e5f7c
+    /* Re-open the database with modules enabled */
5e5f7c
+	NSS_ShutdownContext(ctx);
5e5f7c
+	ctx = NSS_InitContext(entry->cm_cert_storage_location,
5e5f7c
+			      NULL, NULL, NULL, NULL,
5e5f7c
+			      (readwrite ? 0 : NSS_INIT_READONLY) |
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	es = util_n_fips_hook();
5e5f7c
 	if (es != NULL) {
5e5f7c
 		cm_log(1, "Error putting NSS into FIPS mode: %s\n", es);
5e5f7c
 		_exit(CM_SUB_STATUS_ERROR_INITIALIZING);
5e5f7c
 	}
5e5f7c
-	/* Allocate a memory pool. */
5e5f7c
-	arena = PORT_NewArena(sizeof(double));
5e5f7c
-	if (arena == NULL) {
5e5f7c
-		cm_log(1, "Error opening database '%s'.\n",
5e5f7c
-		       entry->cm_cert_storage_location);
5e5f7c
-		if (NSS_ShutdownContext(ctx) != SECSuccess) {
5e5f7c
-			cm_log(1, "Error shutting down NSS.\n");
5e5f7c
-		}
5e5f7c
-		_exit(ENOMEM);
5e5f7c
-	}
5e5f7c
 	/* Find the tokens that we might use for cert storage. */
5e5f7c
 	mech = CKM_RSA_X_509;
5e5f7c
 	slotlist = PK11_GetAllTokens(mech, PR_FALSE, PR_FALSE, NULL);
5e5f7c
 	if (slotlist == NULL) {
5e5f7c
 		cm_log(1, "Error getting list of tokens.\n");
5e5f7c
-		PORT_FreeArena(arena, PR_TRUE);
5e5f7c
 		if (NSS_ShutdownContext(ctx) != SECSuccess) {
5e5f7c
 			cm_log(1, "Error shutting down NSS.\n");
5e5f7c
 		}
5e5f7c
@@ -249,6 +244,7 @@ cm_certread_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 		}
5e5f7c
 		/* If we need to log in in order to read certificates, do so. */
5e5f7c
 		if (PK11_NeedLogin(sle->slot)) {
5e5f7c
+			cm_log(3, "Need login to token %s\n", PK11_GetTokenName(sle->slot));
5e5f7c
 			if (cm_pin_read_for_cert(entry, &pin) != 0) {
5e5f7c
 				cm_log(1, "Error reading PIN for cert db, "
5e5f7c
 				       "skipping.\n");
5e5f7c
@@ -272,13 +268,19 @@ cm_certread_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 		/* Walk the list of certificates in the slot, looking for one
5e5f7c
 		 * which matches the specified nickname. */
5e5f7c
 		certs = PK11_ListCertsInSlot(sle->slot);
5e5f7c
+		cm_log(3, "Looking for %s\n", entry->cm_cert_nickname);
5e5f7c
 		if (certs != NULL) {
5e5f7c
 			for (node = CERT_LIST_HEAD(certs);
5e5f7c
 			     !CERT_LIST_EMPTY(certs) &&
5e5f7c
 			     !CERT_LIST_END(node, certs);
5e5f7c
 			     node = CERT_LIST_NEXT(node)) {
5e5f7c
-				if (strcmp(node->cert->nickname,
5e5f7c
-					   entry->cm_cert_nickname) == 0) {
5e5f7c
+				cm_log(3, "certread-n: Slot nickname %s\n",
5e5f7c
+							node->cert->nickname);
5e5f7c
+		        es = talloc_asprintf(entry, "%s:%s",
5e5f7c
+					   entry->cm_cert_token, entry->cm_cert_nickname);
5e5f7c
+				if ((strcmp(node->cert->nickname,
5e5f7c
+					   entry->cm_cert_nickname) == 0) ||
5e5f7c
+                    (strcmp(node->cert->nickname, es) == 0)) {
5e5f7c
 					cm_log(3, "Located the certificate "
5e5f7c
 					       "\"%s\".\n",
5e5f7c
 					       entry->cm_cert_nickname);
5e5f7c
@@ -321,7 +323,6 @@ next_slot:
5e5f7c
 	if (cert == NULL) {
5e5f7c
 		cm_log(1, "Error locating certificate.\n");
5e5f7c
 		PK11_FreeSlotList(slotlist);
5e5f7c
-		PORT_FreeArena(arena, PR_TRUE);
5e5f7c
 		if (NSS_ShutdownContext(ctx) != SECSuccess) {
5e5f7c
 			cm_log(1, "Error shutting down NSS.\n");
5e5f7c
 		}
5e5f7c
@@ -332,7 +333,6 @@ next_slot:
5e5f7c
 	fclose(fp);
5e5f7c
 	CERT_DestroyCertificate(cert);
5e5f7c
 	PK11_FreeSlotList(slotlist);
5e5f7c
-	PORT_FreeArena(arena, PR_TRUE);
5e5f7c
 	if (NSS_ShutdownContext(ctx) != SECSuccess) {
5e5f7c
 		cm_log(1, "Error shutting down NSS.\n");
5e5f7c
 	}
5e5f7c
@@ -358,8 +358,7 @@ cm_certread_n_parse(struct cm_store_entry *entry,
5e5f7c
 			      NULL, NULL, NULL, NULL,
5e5f7c
 			      NSS_INIT_NOCERTDB |
5e5f7c
 			      NSS_INIT_READONLY |
5e5f7c
-			      NSS_INIT_NOROOTINIT |
5e5f7c
-			      NSS_INIT_NOMODDB);
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	if (ctx == NULL) {
5e5f7c
 		cm_log(1, "Unable to initialize NSS.\n");
5e5f7c
 		_exit(1);
5e5f7c
diff --git a/src/certsave-n.c b/src/certsave-n.c
5e5f7c
index 972a1dfa..eda03b34 100644
5e5f7c
--- a/src/certsave-n.c
5e5f7c
+++ b/src/certsave-n.c
5e5f7c
@@ -186,6 +186,11 @@ cm_certsave_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 	} else {
5e5f7c
 		/* We don't try to force FIPS mode here, as it seems to get in
5e5f7c
 		 * the way of saving the certificate. */
5e5f7c
+		NSS_ShutdownContext(ctx);
5e5f7c
+		ctx = NSS_InitContext(entry->cm_cert_storage_location,
5e5f7c
+				      NULL, NULL, NULL, NULL,
5e5f7c
+				      (readwrite ? 0 : NSS_INIT_READONLY) |
5e5f7c
+				      NSS_INIT_NOROOTINIT);
5e5f7c
 
5e5f7c
 		/* Allocate a memory pool. */
5e5f7c
 		arena = PORT_NewArena(sizeof(double));
5e5f7c
diff --git a/src/dogtag.c b/src/dogtag.c
5e5f7c
index 55607f3d..c43664ef 100644
5e5f7c
--- a/src/dogtag.c
5e5f7c
+++ b/src/dogtag.c
5e5f7c
@@ -306,8 +306,7 @@ main(int argc, const char **argv)
5e5f7c
 			       NULL, NULL, NULL, NULL,
5e5f7c
 			       NSS_INIT_NOCERTDB |
5e5f7c
 			       NSS_INIT_READONLY |
5e5f7c
-			       NSS_INIT_NOROOTINIT |
5e5f7c
-			       NSS_INIT_NOMODDB);
5e5f7c
+			       NSS_INIT_NOROOTINIT);
5e5f7c
 	if (nctx == NULL) {
5e5f7c
 		cm_log(1, "Unable to initialize NSS.\n");
5e5f7c
 		_exit(1);
5e5f7c
diff --git a/src/keygen-n.c b/src/keygen-n.c
5e5f7c
index 061bd2af..e921d7ec 100644
5e5f7c
--- a/src/keygen-n.c
5e5f7c
+++ b/src/keygen-n.c
5e5f7c
@@ -226,6 +226,11 @@ cm_keygen_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 			break;
5e5f7c
 		}
5e5f7c
 	}
5e5f7c
+	NSS_ShutdownContext(ctx);
5e5f7c
+	ctx = NSS_InitContext(entry->cm_key_storage_location,
5e5f7c
+			      NULL, NULL, NULL, NULL,
5e5f7c
+			      (readwrite ? 0 : NSS_INIT_READONLY) |
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	reason = util_n_fips_hook();
5e5f7c
 	if (reason != NULL) {
5e5f7c
 		cm_log(1, "Error putting NSS into FIPS mode: %s\n", reason);
5e5f7c
diff --git a/src/keyiread-n.c b/src/keyiread-n.c
5e5f7c
index 91b1be41..dc1c6092 100644
5e5f7c
--- a/src/keyiread-n.c
5e5f7c
+++ b/src/keyiread-n.c
5e5f7c
@@ -115,6 +115,11 @@ cm_keyiread_n_get_keys(struct cm_store_entry *entry, int readwrite)
5e5f7c
 			break;
5e5f7c
 		}
5e5f7c
 	}
5e5f7c
+	NSS_ShutdownContext(ctx);
5e5f7c
+	ctx = NSS_InitContext(entry->cm_key_storage_location,
5e5f7c
+			      NULL, NULL, NULL, NULL,
5e5f7c
+			      (readwrite ? 0 : NSS_INIT_READONLY) |
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	reason = util_n_fips_hook();
5e5f7c
 	if (reason != NULL) {
5e5f7c
 		cm_log(1, "Error putting NSS into FIPS mode: %s\n", reason);
5e5f7c
@@ -340,8 +345,12 @@ cm_keyiread_n_get_keys(struct cm_store_entry *entry, int readwrite)
5e5f7c
 			     cnode = CERT_LIST_NEXT(cnode)) {
5e5f7c
 				nickname = entry->cm_key_nickname;
5e5f7c
 				cert = cnode->cert;
5e5f7c
+				es = talloc_asprintf(entry, "%s:%s",
5e5f7c
+									         entry->cm_cert_token,
5e5f7c
+											 entry->cm_cert_nickname);
5e5f7c
 				if ((nickname != NULL) &&
5e5f7c
-				    (strcmp(cert->nickname, nickname) == 0)) {
5e5f7c
+				    ((strcmp(cert->nickname, nickname) == 0) ||
5e5f7c
+					(strcmp(cert->nickname, es) == 0))) {
5e5f7c
 					cm_log(3, "Located a certificate with "
5e5f7c
 					       "the key's nickname (\"%s\").\n",
5e5f7c
 					       nickname);
5e5f7c
diff --git a/src/scepgen-n.c b/src/scepgen-n.c
5e5f7c
index d6735aa7..8c67b122 100644
5e5f7c
--- a/src/scepgen-n.c
5e5f7c
+++ b/src/scepgen-n.c
5e5f7c
@@ -183,6 +183,11 @@ cm_scepgen_n_main(int fd, struct cm_store_ca *ca, struct cm_store_entry *entry,
5e5f7c
 			break;
5e5f7c
 		}
5e5f7c
 	}
5e5f7c
+	NSS_ShutdownContext(ctx);
5e5f7c
+	ctx = NSS_InitContext(entry->cm_key_storage_location,
5e5f7c
+			      NULL, NULL, NULL, NULL,
5e5f7c
+			      NSS_INIT_READONLY |
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	reason = util_n_fips_hook();
5e5f7c
 	if (reason != NULL) {
5e5f7c
 		cm_log(1, "Error putting NSS into FIPS mode: %s\n", reason);
5e5f7c
diff --git a/src/submit-n.c b/src/submit-n.c
5e5f7c
index b07ea23a..f27b9c7f 100644
5e5f7c
--- a/src/submit-n.c
5e5f7c
+++ b/src/submit-n.c
5e5f7c
@@ -317,6 +317,11 @@ cm_submit_n_decrypt_envelope(const unsigned char *envelope,
5e5f7c
 		}
5e5f7c
 		goto done;
5e5f7c
 	}
5e5f7c
+	NSS_ShutdownContext(ctx);
5e5f7c
+	ctx = NSS_InitContext(args->entry->cm_key_storage_location,
5e5f7c
+			      NULL, NULL, NULL, NULL,
5e5f7c
+			      NSS_INIT_READONLY |
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	reason = util_n_fips_hook();
5e5f7c
 	if (reason != NULL) {
5e5f7c
 		cm_log(1, "Error putting NSS into FIPS mode: %s\n", reason);
5e5f7c
diff --git a/src/toklist.c b/src/toklist.c
5e5f7c
index a4328218..ac166722 100644
5e5f7c
--- a/src/toklist.c
5e5f7c
+++ b/src/toklist.c
5e5f7c
@@ -79,7 +79,7 @@ main(int argc, const char **argv)
5e5f7c
 
5e5f7c
 	/* Open the database. */
5e5f7c
 	ctx = NSS_InitContext(dbdir, NULL, NULL, NULL, NULL,
5e5f7c
-			      NSS_INIT_NOROOTINIT | NSS_INIT_NOMODDB);
5e5f7c
+			      NSS_INIT_NOROOTINIT);
5e5f7c
 	if (ctx == NULL) {
5e5f7c
 		printf("Unable to open NSS database '%s'.\n", dbdir);
5e5f7c
 		_exit(CM_SUB_STATUS_ERROR_INITIALIZING);
5e5f7c
-- 
5e5f7c
2.21.0
5e5f7c