Blame SOURCES/opensc-0.19.0-coverity.patch

62d44b
diff --git a/src/libopensc/card-epass2003.c b/src/libopensc/card-epass2003.c
62d44b
index 49b593f9..299520d6 100644
62d44b
--- a/src/libopensc/card-epass2003.c
62d44b
+++ b/src/libopensc/card-epass2003.c
62d44b
@@ -1846,11 +1846,6 @@ epass2003_process_fci(struct sc_card *card, sc_file_t * file, const u8 * buf, si
62d44b
 				case 0x04:
62d44b
 					file->ef_structure = SC_FILE_EF_LINEAR_FIXED;
62d44b
 					break;
62d44b
-				case 0x03:
62d44b
-				case 0x05:
62d44b
-				case 0x06:
62d44b
-				case 0x07:
62d44b
-					break;
62d44b
 				default:
62d44b
 					break;
62d44b
 				}
62d44b
diff --git a/src/libopensc/card-iasecc.c b/src/libopensc/card-iasecc.c
62d44b
index 254f8aa5..7eb3f5d0 100644
62d44b
--- a/src/libopensc/card-iasecc.c
62d44b
+++ b/src/libopensc/card-iasecc.c
62d44b
@@ -2406,7 +2406,11 @@ iasecc_pin_reset(struct sc_card *card, struct sc_pin_cmd_data *data, int *tries_
62d44b
 		sc_format_path("3F00", &path);
62d44b
 		path.type = SC_PATH_TYPE_FILE_ID;
62d44b
 		rv = iasecc_select_file(card, &path, NULL);
62d44b
-		LOG_TEST_RET(ctx, rv, "Unable to select MF");
62d44b
+		if (rv != SC_SUCCESS) {
62d44b
+			sc_file_free(save_current);
62d44b
+			sc_log(ctx, "Unable to select MF");
62d44b
+			LOG_FUNC_RETURN(ctx, rv);
62d44b
+		}
62d44b
 	}
62d44b
 
62d44b
 	memset(&sdo, 0, sizeof(sdo));
62d44b
@@ -3478,9 +3482,12 @@ iasecc_get_free_reference(struct sc_card *card, struct iasecc_ctl_get_free_refer
62d44b
 
62d44b
 			sc_log(ctx, "found empty key slot %i", idx);
62d44b
 			break;
62d44b
+		} else if (rv != SC_SUCCESS) {
62d44b
+			iasecc_sdo_free(card, sdo);
62d44b
+
62d44b
+			sc_log(ctx, "get new key reference failed");
62d44b
+			LOG_FUNC_RETURN(ctx, rv);
62d44b
 		}
62d44b
-		else
62d44b
-			LOG_TEST_RET(ctx, rv, "get new key reference failed");
62d44b
 
62d44b
 		sz = *(sdo->docp.size.value + 0) * 0x100 + *(sdo->docp.size.value + 1);
62d44b
 		sc_log(ctx,
62d44b
diff --git a/src/libopensc/card-muscle.c b/src/libopensc/card-muscle.c
62d44b
index c91b8d5e..be5b9f14 100644
62d44b
--- a/src/libopensc/card-muscle.c
62d44b
+++ b/src/libopensc/card-muscle.c
62d44b
@@ -455,6 +455,7 @@ static int _listFile(mscfs_file_t *file, int reset, void *udata)
62d44b
 static int muscle_init(sc_card_t *card)
62d44b
 {
62d44b
 	muscle_private_t *priv;
62d44b
+	int r;
62d44b
 
62d44b
 	card->name = "MuscleApplet";
62d44b
 	card->drv_data = malloc(sizeof(muscle_private_t));
62d44b
@@ -478,7 +479,10 @@ static int muscle_init(sc_card_t *card)
62d44b
 	card->caps |= SC_CARD_CAP_RNG;
62d44b
 
62d44b
 	/* Card type detection */
62d44b
-	_sc_match_atr(card, muscle_atrs, &card->type);
62d44b
+	r = _sc_match_atr(card, muscle_atrs, &card->type);
62d44b
+	if (r < 0) {
62d44b
+		sc_log(card->ctx, "Failed to match the ATRs");
62d44b
+	}
62d44b
 	if(card->type == SC_CARD_TYPE_MUSCLE_ETOKEN_72K) {
62d44b
 		card->caps |= SC_CARD_CAP_APDU_EXT;
62d44b
 	}
62d44b
diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c
62d44b
index 61acedc8..a678b768 100644
62d44b
--- a/src/libopensc/card-piv.c
62d44b
+++ b/src/libopensc/card-piv.c
62d44b
@@ -922,7 +922,11 @@ piv_get_data(sc_card_t * card, int enumtag, u8 **buf, size_t *buf_len)
62d44b
 	SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
62d44b
 	sc_log(card->ctx, "#%d", enumtag);
62d44b
 
62d44b
-	sc_lock(card); /* do check len and get data in same transaction */
62d44b
+	r = sc_lock(card); /* do check len and get data in same transaction */
62d44b
+	if (r != SC_SUCCESS) {
62d44b
+		sc_log(card->ctx, "sc_lock failed");
62d44b
+		return r;
62d44b
+	}
62d44b
 
62d44b
 	/* assert(enumtag >= 0 && enumtag < PIV_OBJ_LAST_ENUM); */
62d44b
 
62d44b
@@ -1481,7 +1485,7 @@ static int piv_get_key(sc_card_t *card, unsigned int alg_id, u8 **key, size_t *l
62d44b
 	FILE *f = NULL;
62d44b
 	char * keyfilename = NULL;
62d44b
 	size_t expected_keylen;
62d44b
-	size_t keylen;
62d44b
+	size_t keylen, readlen;
62d44b
 	u8 * keybuf = NULL;
62d44b
 	u8 * tkey = NULL;
62d44b
 
62d44b
@@ -1530,11 +1534,12 @@ static int piv_get_key(sc_card_t *card, unsigned int alg_id, u8 **key, size_t *l
62d44b
 	}
62d44b
 	keybuf[fsize] = 0x00;    /* in case it is text need null */
62d44b
 
62d44b
-	if (fread(keybuf, 1, fsize, f) != fsize) {
62d44b
+	if ((readlen = fread(keybuf, 1, fsize, f)) != fsize) {
62d44b
 		sc_log(card->ctx, " Unable to read key\n");
62d44b
 		r = SC_ERROR_WRONG_LENGTH;
62d44b
 		goto err;
62d44b
 	}
62d44b
+	keybuf[readlen] = '\0';
62d44b
 
62d44b
 	tkey = malloc(expected_keylen);
62d44b
 	if (!tkey) {
62d44b
@@ -2126,14 +2131,16 @@ piv_get_serial_nr_from_CHUI(sc_card_t* card, sc_serial_number_t* serial)
62d44b
 				/* test if guid and the fascn starts with ;9999 (in ISO 4bit + parity code) */
62d44b
 				if (!(gbits && fascn[0] == 0xD4 && fascn[1] == 0xE7
62d44b
 						    && fascn[2] == 0x39 && (fascn[3] | 0x7F) == 0xFF)) {
62d44b
-					serial->len = fascnlen < SC_MAX_SERIALNR ? fascnlen : SC_MAX_SERIALNR;
62d44b
+					/* fascnlen is 25 */
62d44b
+					serial->len = fascnlen;
62d44b
 					memcpy (serial->value, fascn, serial->len);
62d44b
 					r = SC_SUCCESS;
62d44b
 					gbits = 0; /* set to skip using guid below */
62d44b
 				}
62d44b
 			}
62d44b
 			if (guid && gbits) {
62d44b
-				serial->len = guidlen < SC_MAX_SERIALNR ? guidlen : SC_MAX_SERIALNR;
62d44b
+				/* guidlen is 16 */
62d44b
+				serial->len = guidlen;
62d44b
 				memcpy (serial->value, guid, serial->len);
62d44b
 				r = SC_SUCCESS;
62d44b
 			}
62d44b
@@ -2981,7 +2988,7 @@ static int piv_match_card(sc_card_t *card)
62d44b
 
62d44b
 static int piv_match_card_continued(sc_card_t *card)
62d44b
 {
62d44b
-	int i;
62d44b
+	int i, r;
62d44b
 	int type  = -1;
62d44b
 	piv_private_data_t *priv = NULL;
62d44b
 	int saved_type = card->type;
62d44b
@@ -3080,7 +3087,13 @@ static int piv_match_card_continued(sc_card_t *card)
62d44b
 		if(piv_objects[i].flags & PIV_OBJECT_NOT_PRESENT)
62d44b
 			priv->obj_cache[i].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
62d44b
 
62d44b
-	sc_lock(card);
62d44b
+	r = sc_lock(card);
62d44b
+	if (r != SC_SUCCESS) {
62d44b
+		sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "sc_lock failed\n");
62d44b
+		piv_finish(card);
62d44b
+		card->type = saved_type;
62d44b
+		return 0;
62d44b
+	}
62d44b
 
62d44b
 	/*
62d44b
 	 * detect if active AID is PIV. NIST 800-73 says Only one PIV application per card
62d44b
@@ -3464,7 +3477,11 @@ piv_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data, int *tries_left)
62d44b
 	if (data->cmd == SC_PIN_CMD_VERIFY && data->pin_type == SC_AC_CONTEXT_SPECIFIC) {
62d44b
 		priv->context_specific = 1;
62d44b
 		sc_log(card->ctx,"Starting CONTEXT_SPECIFIC verify");
62d44b
-		sc_lock(card);
62d44b
+		r = sc_lock(card);
62d44b
+		if (r != SC_SUCCESS) {
62d44b
+			sc_log(card->ctx, "sc_lock failed");
62d44b
+			return r;
62d44b
+		}
62d44b
 	}
62d44b
 
62d44b
 	priv->pin_cmd_verify = 1; /* tell piv_check_sw its a verify to save sw1, sw2 */
62d44b
diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c
62d44b
index 626686a7..f24a61ca 100644
62d44b
--- a/src/libopensc/ctx.c
62d44b
+++ b/src/libopensc/ctx.c
62d44b
@@ -452,6 +452,10 @@ static void *load_dynamic_driver(sc_context_t *ctx, void **dll, const char *name
62d44b
 	const char *(*modversion)(void) = NULL;
62d44b
 	const char *(**tmodv)(void) = &modversion;
62d44b
 
62d44b
+	if (dll == NULL) {
62d44b
+		sc_log(ctx, "No dll parameter specified");
62d44b
+		return NULL;
62d44b
+	}
62d44b
 	if (name == NULL) { /* should not occur, but... */
62d44b
 		sc_log(ctx, "No module specified");
62d44b
 		return NULL;
62d44b
@@ -481,8 +485,8 @@ static void *load_dynamic_driver(sc_context_t *ctx, void **dll, const char *name
62d44b
 		sc_dlclose(handle);
62d44b
 		return NULL;
62d44b
 	}
62d44b
-	if (dll)
62d44b
-		*dll = handle;
62d44b
+
62d44b
+	*dll = handle;
62d44b
 	sc_log(ctx, "successfully loaded card driver '%s'", name);
62d44b
 	return modinit(name);
62d44b
 }
62d44b
diff --git a/src/libopensc/iso7816.c b/src/libopensc/iso7816.c
62d44b
index 718d92ff..6abd2d76 100644
62d44b
--- a/src/libopensc/iso7816.c
62d44b
+++ b/src/libopensc/iso7816.c
62d44b
@@ -841,13 +841,18 @@ iso7816_set_security_env(struct sc_card *card,
62d44b
 	if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT) {
62d44b
 		if (env->file_ref.len > 0xFF)
62d44b
 			return SC_ERROR_INVALID_ARGUMENTS;
62d44b
+		if (sizeof(sbuf) - (p - sbuf) < env->file_ref.len + 2)
62d44b
+			return SC_ERROR_OFFSET_TOO_LARGE;
62d44b
+
62d44b
 		*p++ = 0x81;
62d44b
 		*p++ = (u8) env->file_ref.len;
62d44b
-		assert(sizeof(sbuf) - (p - sbuf) >= env->file_ref.len);
62d44b
 		memcpy(p, env->file_ref.value, env->file_ref.len);
62d44b
 		p += env->file_ref.len;
62d44b
 	}
62d44b
 	if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT) {
62d44b
+		if (sizeof(sbuf) - (p - sbuf) < env->key_ref_len + 2)
62d44b
+			return SC_ERROR_OFFSET_TOO_LARGE;
62d44b
+
62d44b
 		if (env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC)
62d44b
 			*p++ = 0x83;
62d44b
 		else
62d44b
@@ -855,7 +860,6 @@ iso7816_set_security_env(struct sc_card *card,
62d44b
 		if (env->key_ref_len > 0xFF)
62d44b
 			return SC_ERROR_INVALID_ARGUMENTS;
62d44b
 		*p++ = env->key_ref_len & 0xFF;
62d44b
-		assert(sizeof(sbuf) - (p - sbuf) >= env->key_ref_len);
62d44b
 		memcpy(p, env->key_ref, env->key_ref_len);
62d44b
 		p += env->key_ref_len;
62d44b
 	}
62d44b
diff --git a/src/libopensc/pkcs15-cac.c b/src/libopensc/pkcs15-cac.c
62d44b
index 93032113..f34425a5 100644
62d44b
--- a/src/libopensc/pkcs15-cac.c
62d44b
+++ b/src/libopensc/pkcs15-cac.c
62d44b
@@ -388,6 +388,7 @@ static int sc_pkcs15emu_cac_init(sc_pkcs15_card_t *p15card)
62d44b
 			if (r == SC_SUCCESS) {
62d44b
 				token_name = malloc (cn_len+1);
62d44b
 				if (!token_name) {
62d44b
+					free(cn_name);
62d44b
 					r = SC_ERROR_OUT_OF_MEMORY;
62d44b
 					goto fail;
62d44b
 				}
62d44b
diff --git a/src/libopensc/pkcs15-oberthur.c b/src/libopensc/pkcs15-oberthur.c
62d44b
index 3415be7c..8c126e46 100644
62d44b
--- a/src/libopensc/pkcs15-oberthur.c
62d44b
+++ b/src/libopensc/pkcs15-oberthur.c
62d44b
@@ -206,8 +206,10 @@ sc_oberthur_get_certificate_authority(struct sc_pkcs15_der *der, int *out_author
62d44b
 	buf_mem.max = buf_mem.length = der->len;
62d44b
 
62d44b
 	bio = BIO_new(BIO_s_mem());
62d44b
-	if(!bio)
62d44b
+	if (!bio) {
62d44b
+		free(buf_mem.data);
62d44b
 		return SC_ERROR_OUT_OF_MEMORY;
62d44b
+	}
62d44b
 
62d44b
 	BIO_set_mem_buf(bio, &buf_mem, BIO_NOCLOSE);
62d44b
 	x = d2i_X509_bio(bio, 0);
62d44b
diff --git a/src/pkcs15init/pkcs15-authentic.c b/src/pkcs15init/pkcs15-authentic.c
62d44b
index ddccd032..0b6f9c17 100644
62d44b
--- a/src/pkcs15init/pkcs15-authentic.c
62d44b
+++ b/src/pkcs15init/pkcs15-authentic.c
62d44b
@@ -355,7 +355,6 @@ authentic_sdo_allocate_prvkey(struct sc_profile *profile, struct sc_card *card,
62d44b
 		sc_file_free(file);
62d44b
 		LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate 'sc_authentic_sdo'");
62d44b
 	}
62d44b
-	*out = sdo;
62d44b
 
62d44b
 	sdo->magic = AUTHENTIC_SDO_MAGIC;
62d44b
 	sdo->docp.id = key_info->key_reference &  ~AUTHENTIC_OBJECT_REF_FLAG_LOCAL;
62d44b
@@ -364,11 +363,16 @@ authentic_sdo_allocate_prvkey(struct sc_profile *profile, struct sc_card *card,
62d44b
 	rv = authentic_docp_set_acls(card, file, authentic_v3_rsa_ac_ops,
62d44b
 			sizeof(authentic_v3_rsa_ac_ops)/sizeof(authentic_v3_rsa_ac_ops[0]), &sdo->docp);
62d44b
 	sc_file_free(file);
62d44b
-	LOG_TEST_RET(ctx, rv, "Cannot set key ACLs from file");
62d44b
+	if (rv != SC_SUCCESS) {
62d44b
+		free(sdo);
62d44b
+		sc_log(ctx, "Cannot set key ACLs from file");
62d44b
+		LOG_FUNC_RETURN(ctx, rv);
62d44b
+	}
62d44b
 
62d44b
 	sc_log(ctx, "sdo(mech:%X,id:%X,acls:%s)", sdo->docp.mech, sdo->docp.id,
62d44b
 			sc_dump_hex(sdo->docp.acl_data, sdo->docp.acl_data_len));
62d44b
 
62d44b
+	*out = sdo;
62d44b
 	LOG_FUNC_RETURN(ctx, SC_SUCCESS);
62d44b
 }
62d44b
 
62d44b
diff --git a/src/pkcs15init/pkcs15-myeid.c b/src/pkcs15init/pkcs15-myeid.c
62d44b
index 29f9aa22..10258667 100644
62d44b
--- a/src/pkcs15init/pkcs15-myeid.c
62d44b
+++ b/src/pkcs15init/pkcs15-myeid.c
62d44b
@@ -232,6 +232,7 @@ myeid_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df
62d44b
 		for (ii = 0; create_dfs[ii]; ii++) {
62d44b
 			sc_log(ctx, "Create '%s'", create_dfs[ii]);
62d44b
 
62d44b
+			file = NULL;
62d44b
 			r = sc_profile_get_file(profile, create_dfs[ii], &file;;
62d44b
 			sc_file_free(file);
62d44b
 			if (r) {
62d44b
@@ -433,7 +434,11 @@ _add_supported_algo(struct sc_profile *profile, struct sc_pkcs15_card *p15card,
62d44b
 		    unsigned operations, unsigned mechanism, const struct sc_object_id *oid)
62d44b
 {
62d44b
 	struct sc_supported_algo_info *algo;
62d44b
+	struct sc_context *ctx = p15card->card->ctx;
62d44b
 	algo = sc_pkcs15_get_supported_algo(p15card, operations, mechanism);
62d44b
+	int rv;
62d44b
+
62d44b
+	LOG_FUNC_CALLED(ctx);
62d44b
 	if (!algo) {
62d44b
 		unsigned ref = 1, ii;
62d44b
 
62d44b
@@ -451,7 +456,10 @@ _add_supported_algo(struct sc_profile *profile, struct sc_pkcs15_card *p15card,
62d44b
 		}
62d44b
 
62d44b
 	}
62d44b
-	sc_pkcs15_add_supported_algo_ref(object, algo);
62d44b
+	rv = sc_pkcs15_add_supported_algo_ref(object, algo);
62d44b
+	if (rv != SC_SUCCESS) {
62d44b
+		sc_log(ctx, "Failed to add algorithms refs");
62d44b
+	}
62d44b
 }
62d44b
 
62d44b
 static void
62d44b
@@ -742,7 +750,6 @@ myeid_generate_key(struct sc_profile *profile, struct sc_pkcs15_card *p15card,
62d44b
 			break;
62d44b
 		default:
62d44b
 			LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Unsupported key type");
62d44b
-			break;
62d44b
 	}
62d44b
 
62d44b
 	sc_log(ctx, "Generate key with ID:%s and path:%s",
62d44b
diff --git a/src/pkcs15init/pkcs15-oberthur-awp.c b/src/pkcs15init/pkcs15-oberthur-awp.c
62d44b
index f9c96373..9b12f06c 100644
62d44b
--- a/src/pkcs15init/pkcs15-oberthur-awp.c
62d44b
+++ b/src/pkcs15init/pkcs15-oberthur-awp.c
62d44b
@@ -284,9 +284,10 @@ awp_create_container_record (struct sc_pkcs15_card *p15card, struct sc_profile *
62d44b
 	memset(buff, 0, list_file->record_length);
62d44b
 
62d44b
 	rv = awp_new_container_entry(p15card, buff, list_file->record_length);
62d44b
-	if (rv < 0)   {
62d44b
+	if (rv < 0) {
62d44b
 		free(buff);
62d44b
-		SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot create container");
62d44b
+		sc_log(ctx, "Cannot create container");
62d44b
+		SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
62d44b
 	}
62d44b
 
62d44b
 	*(buff + 0) = (acc->pubkey_id >> 8) & 0xFF;
62d44b
diff --git a/src/tools/npa-tool-cmdline.c b/src/tools/npa-tool-cmdline.c
62d44b
index 117c6cb1..26eed929 100644
62d44b
--- a/src/tools/npa-tool-cmdline.c
62d44b
+++ b/src/tools/npa-tool-cmdline.c
62d44b
@@ -1685,7 +1685,14 @@ void update_multiple_arg(void *field, char ***orig_field,
62d44b
   struct generic_list *tmp;
62d44b
 
62d44b
   if (prev_given && list) {
62d44b
+    char **old = *orig_field;
62d44b
+    char *old_field = field;
62d44b
     *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
62d44b
+    if (*orig_field == NULL) {
62d44b
+      free(*old);
62d44b
+      fprintf(stderr, "Failed to allocate memory: aborting");
62d44b
+      exit(1);
62d44b
+    }
62d44b
 
62d44b
     switch(arg_type) {
62d44b
     case ARG_INT:
62d44b
@@ -1695,6 +1702,11 @@ void update_multiple_arg(void *field, char ***orig_field,
62d44b
     default:
62d44b
       break;
62d44b
     };
62d44b
+    if (*((void **)field) == NULL) {
62d44b
+      free(old_field);
62d44b
+      fprintf(stderr, "Failed to allocate memory: aborting");
62d44b
+      exit(1);
62d44b
+    }
62d44b
     
62d44b
     for (i = (prev_given - 1); i >= 0; --i)
62d44b
       {
62d44b
diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c
62d44b
index ac5292f9..7bc5a3ff 100644
62d44b
--- a/src/tools/opensc-explorer.c
62d44b
+++ b/src/tools/opensc-explorer.c
62d44b
@@ -1399,7 +1399,7 @@ static int do_get(int argc, char **argv)
62d44b
 	if (r == SC_SUCCESS)
62d44b
 		r = sc_select_file(card, &path, &file;;
62d44b
 	sc_unlock(card);
62d44b
-	if (r) {
62d44b
+	if (r || file == NULL) {
62d44b
 		check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file);
62d44b
 		goto err;
62d44b
 	}
62d44b
diff --git a/src/tools/piv-tool.c b/src/tools/piv-tool.c
62d44b
index 6dc8213d..23a58ce6 100644
62d44b
--- a/src/tools/piv-tool.c
62d44b
+++ b/src/tools/piv-tool.c
62d44b
@@ -477,6 +477,7 @@ int main(int argc, char *argv[])
62d44b
 	const char *key_info = NULL;
62d44b
 	const char *admin_info = NULL;
62d44b
 	sc_context_param_t ctx_param;
62d44b
+	char **old_apdus = NULL;
62d44b
 
62d44b
 	setbuf(stderr, NULL);
62d44b
 	setbuf(stdout, NULL);
62d44b
@@ -493,9 +494,11 @@ int main(int argc, char *argv[])
62d44b
 			action_count++;
62d44b
 			break;
62d44b
 		case 's':
62d44b
+			old_apdus = opt_apdus;
62d44b
 			opt_apdus = (char **) realloc(opt_apdus,
62d44b
 					(opt_apdu_count + 1) * sizeof(char *));
62d44b
 			if (!opt_apdus) {
62d44b
+				free(old_apdus);
62d44b
 				err = 1;
62d44b
 				goto end;
62d44b
 			}
62d44b
diff --git a/src/tools/pkcs11-tool.c b/src/tools/pkcs11-tool.c
62d44b
index 64525f6a..5795a8ba 100644
62d44b
--- a/src/tools/pkcs11-tool.c
62d44b
+++ b/src/tools/pkcs11-tool.c
62d44b
@@ -2695,6 +2695,7 @@ static int write_object(CK_SESSION_HANDLE session)
62d44b
 		if (!(f = fopen(opt_attr_from_file, "rb")))
62d44b
 			util_fatal("Couldn't open file \"%s\"", opt_attr_from_file);
62d44b
 		certdata_len = fread(certdata, 1, sizeof(certdata), f);
62d44b
+		certdata[certdata_len] = '\0';
62d44b
 		if (certdata_len < 0)
62d44b
 			util_fatal("Couldn't read from file \"%s\"", opt_attr_from_file);
62d44b
 		fclose(f);
62d44b
diff --git a/src/tools/sc-hsm-tool.c b/src/tools/sc-hsm-tool.c
62d44b
index 02cdfcc6..2b424cf7 100644
62d44b
--- a/src/tools/sc-hsm-tool.c
62d44b
+++ b/src/tools/sc-hsm-tool.c
62d44b
@@ -1503,13 +1503,13 @@ static int unwrap_key(sc_card_t *card, int keyid, const char *inf, const char *p
62d44b
 		return -1;
62d44b
 	}
62d44b
 
62d44b
-	if ((keybloblen = fread(keyblob, 1, sizeof(keyblob), in)) < 0) {
62d44b
+	keybloblen = fread(keyblob, 1, sizeof(keyblob), in);
62d44b
+	fclose(in);
62d44b
+	if (keybloblen < 0) {
62d44b
 		perror(inf);
62d44b
 		return -1;
62d44b
 	}
62d44b
 
62d44b
-	fclose(in);
62d44b
-
62d44b
 	ptr = keyblob;
62d44b
 	if ((sc_asn1_read_tag(&ptr, keybloblen, &cla, &tag, &len) != SC_SUCCESS)
62d44b
 		   	|| ((cla & SC_ASN1_TAG_CONSTRUCTED) != SC_ASN1_TAG_CONSTRUCTED)