Blame SOURCES/opensc-0.19.0-coverity.patch

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