From c3ef82e7b8d17e7dee89c29ce6cbc9dc02888ea5 Mon Sep 17 00:00:00 2001
From: Nalin Dahyabhai <nalin@dahyabhai.net>
Date: Mon, 1 Nov 2010 15:16:33 -0400
Subject: [PATCH 090/150] - clean up cms messages that we create - drop some
 debug-only code

---
 src/plugins/preauth/pkinit/pkinit_crypto_nss.c |   57 +++++++++++++++++++-----
 1 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_nss.c b/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
index 24c918f..12460fe 100644
--- a/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_nss.c
@@ -3726,17 +3726,21 @@ cms_contentinfo_create(krb5_context context,
 
 	info = NSS_CMSMessage_GetContentInfo(msg);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 
 	if (NSS_CMSContentInfo_SetContent(msg, info, encapsulated_tag,
 					  NULL) != SECSuccess) {
+		pkiDebug("%s: error setting encapsulated data\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3746,13 +3750,14 @@ cms_contentinfo_create(krb5_context context,
 	plain.len = in_length;
 	memset(&encoded, 0, sizeof(encoded));
 	if (NSS_CMSDEREncode(msg, &plain, &encoded, pool) != SECSuccess) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
-		pkiDebug("%s: error %d encoding data\n",
-			 __FUNCTION__, encapsulated_tag);
+		pkiDebug("%s: error encoding data\n", __FUNCTION__);
 		return ENOMEM;
 	}
 
 	if (secitem_to_buf_len(&encoded, out_data, out_data_len) != 0) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3764,6 +3769,7 @@ cms_contentinfo_create(krb5_context context,
 	cmsdump(*out_data, *out_data_len);
 #endif
 
+	NSS_CMSMessage_Destroy(msg);
 	PORT_FreeArena(pool, PR_TRUE);
 
 	return 0;
@@ -3891,21 +3897,28 @@ cms_envelopeddata_create(krb5_context context,
 	 * contents. */
 	env = NSS_CMSEnvelopedData_Create(msg, SEC_OID_DES_EDE3_CBC, 0);
 	if (env == NULL) {
+		pkiDebug("%s: error creating enveloped-data\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	info = NSS_CMSMessage_GetContentInfo(msg);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetContent_EnvelopedData(msg, info,
 							env) != SECSuccess) {
+		pkiDebug("%s: error setting enveloped-data content\n",
+			 __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3914,10 +3927,14 @@ cms_envelopeddata_create(krb5_context context,
 	recipient = NSS_CMSRecipientInfo_Create(msg,
 						req_cryptoctx->peer_cert);
 	if (recipient == NULL) {
+		pkiDebug("%s: error creating recipient-info\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSEnvelopedData_AddRecipient(env, recipient) != SECSuccess) {
+		pkiDebug("%s: error adding recipient\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3926,11 +3943,13 @@ cms_envelopeddata_create(krb5_context context,
 	 * contents. */
 	info = NSS_CMSEnvelopedData_GetContentInfo(env);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3944,11 +3963,15 @@ cms_envelopeddata_create(krb5_context context,
 					     add_signed_attributes,
 					     &sdata) != 0) ||
 	    (sdata == NULL)) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetContent_SignedData(msg, info,
 						     sdata) != SECSuccess) {
+		pkiDebug("%s: error setting signed-data content\n",
+			 __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3956,16 +3979,21 @@ cms_envelopeddata_create(krb5_context context,
 	/* Set the raw data as the contents for the signed-data. */
 	info = NSS_CMSSignedData_GetContentInfo(sdata);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetContent(msg, info, encapsulated_tag,
 					  NULL) != SECSuccess) {
+		pkiDebug("%s: error setting encapsulated content\n",
+			 __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3976,12 +4004,13 @@ cms_envelopeddata_create(krb5_context context,
 	plain.len = key_pack_len;
 	memset(&encoded, 0, sizeof(encoded));
 	if (NSS_CMSDEREncode(msg, &plain, &encoded, pool) != SECSuccess) {
+		pkiDebug("%s: error encoding enveloped-data\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
-		pkiDebug("%s: error encoding enveloped-data\n",
-			 __FUNCTION__);
 		return ENOMEM;
 	}
 	if (secitem_to_buf_len(&encoded, envel_data, envel_data_len) != 0) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -3993,6 +4022,7 @@ cms_envelopeddata_create(krb5_context context,
 	cmsdump(*envel_data, *envel_data_len);
 #endif
 
+	NSS_CMSMessage_Destroy(msg);
 	PORT_FreeArena(pool, PR_TRUE);
 
 	return 0;
@@ -4137,13 +4167,6 @@ cms_envelopeddata_verify(krb5_context context,
 #ifdef DEBUG_DER
 	derdump(envel_data, envel_data_len);
 #endif
-	{
-		FILE *fp = fopen("/tmp/enveloped", "w");
-		if (fp) {
-			fwrite(envel_data, 1, envel_data_len, fp);
-			fclose(fp);
-		}
-	}
 	encoded.data = envel_data;
 	encoded.len = envel_data_len;
 	msg = NSS_CMSMessage_CreateFromDER(&encoded,
@@ -4275,11 +4298,13 @@ cms_signeddata_create(krb5_context context,
 	 * contents. */
 	info = NSS_CMSMessage_GetContentInfo(msg);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -4293,11 +4318,15 @@ cms_signeddata_create(krb5_context context,
 					     add_signed_attributes,
 					     &sdata) != 0) ||
 	    (sdata == NULL)) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetContent_SignedData(msg, info,
 						     sdata) != SECSuccess) {
+		pkiDebug("%s: error setting signed-data content\n",
+			 __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -4305,11 +4334,13 @@ cms_signeddata_create(krb5_context context,
 	/* Set the data as the contents of the signed-data. */
 	info = NSS_CMSSignedData_GetContentInfo(sdata);
 	if (info == NULL) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
 	if (NSS_CMSContentInfo_SetDontStream(info, PR_TRUE) != SECSuccess) {
 		pkiDebug("%s: error turning off streaming\n", __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -4317,6 +4348,7 @@ cms_signeddata_create(krb5_context context,
 					  NULL) != SECSuccess) {
 		pkiDebug("%s: error setting encapsulated content type\n",
 			 __FUNCTION__);
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -4327,12 +4359,14 @@ cms_signeddata_create(krb5_context context,
 	plain.len = payload_len;
 	memset(&encoded, 0, sizeof(encoded));
 	if (NSS_CMSDEREncode(msg, &plain, &encoded, pool) != SECSuccess) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		pkiDebug("%s: error encoding signed-data\n",
 			 __FUNCTION__);
 		return ENOMEM;
 	}
 	if (secitem_to_buf_len(&encoded, signed_data, signed_data_len) != 0) {
+		NSS_CMSMessage_Destroy(msg);
 		PORT_FreeArena(pool, PR_TRUE);
 		return ENOMEM;
 	}
@@ -4344,6 +4378,7 @@ cms_signeddata_create(krb5_context context,
 	cmsdump(*signed_data, *signed_data_len);
 #endif
 
+	NSS_CMSMessage_Destroy(msg);
 	PORT_FreeArena(pool, PR_TRUE);
 
 	return 0;
-- 
1.7.6.4

