From 51d475e170be0a11d7a4be1030b09318971e8bdd Mon Sep 17 00:00:00 2001
From: Nalin Dahyabhai <nalin@dahyabhai.net>
Date: Tue, 26 Oct 2010 17:32:31 -0400
Subject: [PATCH 071/150] - fix module initialization (oops)

---
 src/plugins/preauth/pkinit/pkinit_crypto_nss.c |   83 +++++++++++++-----------
 1 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_nss.c b/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
index e75eba5..cef28ee 100644
--- a/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
@@ -613,11 +613,13 @@ pkinit_init_identity_crypto(pkinit_identity_crypto_context *id_cryptoctx)
 	PLArenaPool *pool;
 	pkinit_identity_crypto_context id;
 	pool = PORT_NewArena(sizeof(double));
-	if (pool != NULL) {
-		id = PORT_ArenaZAlloc(pool, sizeof(*id));
-		if (id != NULL) {
-			return ENOMEM;
-		}
+	if (pool == NULL) {
+		return ENOMEM;
+	}
+	id = PORT_ArenaZAlloc(pool, sizeof(*id));
+	if (id == NULL) {
+		PORT_FreeArena(pool, PR_TRUE);
+		return ENOMEM;
 	}
 	id->pool = pool;
 	id->id_certs = CERT_NewCertList();
@@ -1733,6 +1735,7 @@ crypto_load_pkcs11(krb5_context context,
 			 __FUNCTION__, spec);
 		return SECFailure;
 	}
+	pkiDebug("%s: loaded PKCS11 module \"%s\"\n", __FUNCTION__, spec);
 	/* Add us to the list and set the new list. */
 	id_modules[j++] = module;
 	id_modules[j] = NULL;
@@ -1753,6 +1756,9 @@ crypto_load_pkcs11(krb5_context context,
 				cert = CERT_DupCertificate(node->cert);
 				CERT_AddCertToListTail(id_cryptoctx->id_certs,
 						       cert);
+				pkiDebug("%s: loaded \"%s\" from "
+					 "PKCS11 module \"%s\"\n",
+					 __FUNCTION__, cert->subjectName, spec);
 			}
 			/* Mark the cert trusted as a CA? */
 			if (mark_trusted) {
@@ -1769,19 +1775,20 @@ crypto_load_pkcs11(krb5_context context,
 }
 
 static SECStatus
-crypto_load_cert_and_key(krb5_context context,
-			 pkinit_plg_crypto_context plg_cryptoctx,
-			 pkinit_req_crypto_context req_cryptoctx,
-			 const char *certfile,
-			 const char *keyfile,
-			 const char *crlfile,
-			 PRBool cert_self, PRBool cert_mark_trusted,
-			 pkinit_identity_crypto_context id_cryptoctx)
+crypto_load_files(krb5_context context,
+		  pkinit_plg_crypto_context plg_cryptoctx,
+		  pkinit_req_crypto_context req_cryptoctx,
+		  const char *certfile,
+		  const char *keyfile,
+		  const char *crlfile,
+		  PRBool cert_self, PRBool cert_mark_trusted,
+		  pkinit_identity_crypto_context id_cryptoctx)
 {
 	if ((certfile == NULL) && (crlfile == NULL)) {
 		return SECFailure;
 	}
-	return SECFailure;
+	/* FIXME */
+	return SECSuccess;
 }
 
 static SECStatus
@@ -1840,14 +1847,14 @@ crypto_load_dir(krb5_context context,
 			strcpy(key + i - 4, ".key");
 		}
 		/* Try loading the key and file as a pair. */
-		if (crypto_load_cert_and_key(context,
-					     plg_cryptoctx,
-					     req_cryptoctx,
-					     load_crl ? NULL : certcrl,
-					     key,
-					     load_crl ? certcrl : NULL,
-					     cert_self, cert_mark_trusted,
-					     id_cryptoctx) == SECSuccess) {
+		if (crypto_load_files(context,
+				      plg_cryptoctx,
+				      req_cryptoctx,
+				      load_crl ? NULL : certcrl,
+				      key,
+				      load_crl ? certcrl : NULL,
+				      cert_self, cert_mark_trusted,
+				      id_cryptoctx) == SECSuccess) {
 			status = SECSuccess;
 		}
 		PORT_Free(certcrl);
@@ -1874,14 +1881,14 @@ crypto_load_certs(krb5_context context,
 	certdb = CERT_GetDefaultCertDB();
 	switch (idopts->idtype) {
 	case IDTYPE_FILE:
-		status = crypto_load_cert_and_key(context,
-						  plg_cryptoctx,
-						  req_cryptoctx,
-						  idopts->cert_filename,
-						  idopts->key_filename,
-						  NULL,
-						  PR_TRUE, PR_FALSE,
-						  id_cryptoctx);
+		status = crypto_load_files(context,
+					   plg_cryptoctx,
+					   req_cryptoctx,
+					   idopts->cert_filename,
+					   idopts->key_filename,
+					   NULL,
+					   PR_TRUE, PR_FALSE,
+					   id_cryptoctx);
 		if (status != SECSuccess) {
 			pkiDebug("%s: error loading files \"%s\" and \"%s\"\n",
 				 __FUNCTION__, idopts->cert_filename,
@@ -2434,14 +2441,14 @@ crypto_load_cas_and_crls(krb5_context context,
 
 	switch (idtype) {
 	case IDTYPE_FILE:
-		status = crypto_load_cert_and_key(context,
-						  plg_cryptoctx,
-						  req_cryptoctx,
-						  load_crl ? NULL : id,
-						  NULL,
-						  load_crl ? id : NULL,
-						  cert_self, cert_mark_trusted,
-						  id_cryptoctx);
+		status = crypto_load_files(context,
+					   plg_cryptoctx,
+					   req_cryptoctx,
+					   load_crl ? NULL : id,
+					   NULL,
+					   load_crl ? id : NULL,
+					   cert_self, cert_mark_trusted,
+					   id_cryptoctx);
 		if (status != SECSuccess) {
 			pkiDebug("%s: error loading CA file \"%s\"\n",
 				 __FUNCTION__, idopts->cert_filename);
-- 
1.7.6.4

