fixed memory leak

This commit is contained in:
Frank Morgner 2016-02-18 00:16:10 +01:00 committed by Viktor Tarasov
parent 1862970212
commit 50f03bca3f
3 changed files with 28 additions and 17 deletions

View File

@ -1478,7 +1478,7 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
struct sc_pkcs15init_pubkeyargs *keyargs, struct sc_pkcs15_object **res_obj) struct sc_pkcs15init_pubkeyargs *keyargs, struct sc_pkcs15_object **res_obj)
{ {
struct sc_context *ctx = p15card->card->ctx; struct sc_context *ctx = p15card->card->ctx;
struct sc_pkcs15_object *object; struct sc_pkcs15_object *object = NULL;
struct sc_pkcs15_pubkey_info *key_info; struct sc_pkcs15_pubkey_info *key_info;
struct sc_pkcs15_keyinfo_gostparams *keyinfo_gostparams; struct sc_pkcs15_keyinfo_gostparams *keyinfo_gostparams;
struct sc_pkcs15_pubkey key; struct sc_pkcs15_pubkey key;
@ -1546,8 +1546,8 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
* in libopensc (sc_pkcs15_free_prkey_info) */ * in libopensc (sc_pkcs15_free_prkey_info) */
key_info->params.data = malloc(key_info->params.len); key_info->params.data = malloc(key_info->params.len);
if (!key_info->params.data) { if (!key_info->params.data) {
sc_pkcs15init_free_object(object); r = SC_ERROR_OUT_OF_MEMORY;
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate GOST params"); LOG_TEST_GOTO_ERR(ctx, r, "Cannot allocate GOST params");
} }
keyinfo_gostparams = key_info->params.data; keyinfo_gostparams = key_info->params.data;
keyinfo_gostparams->gostr3410 = keyargs->params.gost.gostr3410; keyinfo_gostparams->gostr3410 = keyargs->params.gost.gostr3410;
@ -1559,8 +1559,8 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
if (key.u.ec.params.der.value) { if (key.u.ec.params.der.value) {
key_info->params.data = malloc(key.u.ec.params.der.len); key_info->params.data = malloc(key.u.ec.params.der.len);
if (!key_info->params.data) { if (!key_info->params.data) {
sc_pkcs15init_free_object(object); r = SC_ERROR_OUT_OF_MEMORY;
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate EC params"); LOG_TEST_GOTO_ERR(ctx, r, "Cannot allocate EC params");
} }
key_info->params.len = key.u.ec.params.der.len; key_info->params.len = key.u.ec.params.der.len;
memcpy(key_info->params.data, key.u.ec.params.der.value, key.u.ec.params.der.len); memcpy(key_info->params.data, key.u.ec.params.der.value, key.u.ec.params.der.len);
@ -1569,32 +1569,34 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
/* Select a intrinsic Key ID if the user didn't specify one */ /* Select a intrinsic Key ID if the user didn't specify one */
r = sc_pkcs15init_select_intrinsic_id(p15card, profile, SC_PKCS15_TYPE_PUBKEY, &keyargs->id, &key); r = sc_pkcs15init_select_intrinsic_id(p15card, profile, SC_PKCS15_TYPE_PUBKEY, &keyargs->id, &key);
LOG_TEST_RET(ctx, r, "Get intrinsic ID error"); LOG_TEST_GOTO_ERR(ctx, r, "Get intrinsic ID error");
/* Select a Key ID if the user didn't specify one and there is no intrinsic ID, /* Select a Key ID if the user didn't specify one and there is no intrinsic ID,
* otherwise make sure it's unique */ * otherwise make sure it's unique */
r = select_id(p15card, SC_PKCS15_TYPE_PUBKEY, &keyargs->id); r = select_id(p15card, SC_PKCS15_TYPE_PUBKEY, &keyargs->id);
LOG_TEST_RET(ctx, r, "Failed to select public key object ID"); LOG_TEST_GOTO_ERR(ctx, r, "Failed to select public key object ID");
/* Make sure that private key's ID is the unique inside the PKCS#15 application */ /* Make sure that private key's ID is the unique inside the PKCS#15 application */
r = sc_pkcs15_find_pubkey_by_id(p15card, &keyargs->id, NULL); r = sc_pkcs15_find_pubkey_by_id(p15card, &keyargs->id, NULL);
if (!r) if (!r) {
LOG_TEST_RET(ctx, SC_ERROR_NON_UNIQUE_ID, "Non unique ID of the public key object"); r = SC_ERROR_NON_UNIQUE_ID;
else if (r != SC_ERROR_OBJECT_NOT_FOUND) LOG_TEST_GOTO_ERR(ctx, r, "Non unique ID of the public key object");
LOG_TEST_RET(ctx, r, "Find public key error"); } else if (r != SC_ERROR_OBJECT_NOT_FOUND) {
LOG_TEST_GOTO_ERR(ctx, r, "Find public key error");
}
key_info->id = keyargs->id; key_info->id = keyargs->id;
/* DER encode public key components */ /* DER encode public key components */
r = sc_pkcs15_encode_pubkey(p15card->card->ctx, &key, &object->content.value, &object->content.len); r = sc_pkcs15_encode_pubkey(p15card->card->ctx, &key, &object->content.value, &object->content.len);
LOG_TEST_RET(ctx, r, "Encode public key error"); LOG_TEST_GOTO_ERR(ctx, r, "Encode public key error");
r = sc_pkcs15_encode_pubkey(p15card->card->ctx, &key, &key_info->direct.raw.value, &key_info->direct.raw.len); r = sc_pkcs15_encode_pubkey(p15card->card->ctx, &key, &key_info->direct.raw.value, &key_info->direct.raw.len);
LOG_TEST_RET(ctx, r, "RAW encode public key error"); LOG_TEST_GOTO_ERR(ctx, r, "RAW encode public key error");
/* EC key are encoded as SPKI to preserve domain parameter */ /* EC key are encoded as SPKI to preserve domain parameter */
r = sc_pkcs15_encode_pubkey_as_spki(p15card->card->ctx, &key, &key_info->direct.spki.value, &key_info->direct.spki.len); r = sc_pkcs15_encode_pubkey_as_spki(p15card->card->ctx, &key, &key_info->direct.spki.value, &key_info->direct.spki.len);
LOG_TEST_RET(ctx, r, "SPKI encode public key error"); LOG_TEST_GOTO_ERR(ctx, r, "SPKI encode public key error");
/* Now create key file and store key */ /* Now create key file and store key */
if (type == SC_PKCS15_TYPE_PUBKEY_EC) if (type == SC_PKCS15_TYPE_PUBKEY_EC)
@ -1612,10 +1614,13 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
if (r >= 0) if (r >= 0)
r = sc_pkcs15init_add_object(p15card, profile, SC_PKCS15_PUKDF, object); r = sc_pkcs15init_add_object(p15card, profile, SC_PKCS15_PUKDF, object);
profile->dirty = 1;
err:
if (r >= 0 && res_obj) if (r >= 0 && res_obj)
*res_obj = object; *res_obj = object;
else if (object)
profile->dirty = 1; sc_pkcs15init_free_object(object);
LOG_FUNC_RETURN(ctx, r); LOG_FUNC_RETURN(ctx, r);
} }
@ -2953,6 +2958,8 @@ sc_pkcs15init_change_attrib(struct sc_pkcs15_card *p15card, struct sc_profile *p
struct sc_file *file = NULL; struct sc_file *file = NULL;
r = sc_profile_get_file_by_path(profile, &df->path, &file); r = sc_profile_get_file_by_path(profile, &df->path, &file);
if (r < 0)
free(buf);
LOG_TEST_RET(ctx, r, "Cannot instantiate file by path"); LOG_TEST_RET(ctx, r, "Cannot instantiate file by path");
r = sc_pkcs15init_update_file(profile, p15card, file, buf, bufsize); r = sc_pkcs15init_update_file(profile, p15card, file, buf, bufsize);

View File

@ -611,6 +611,8 @@ sc_profile_get_file_instance(struct sc_profile *profile, const char *name,
if (ret) if (ret)
*ret = file; *ret = file;
else
sc_file_free(file);
LOG_FUNC_RETURN(ctx, SC_SUCCESS); LOG_FUNC_RETURN(ctx, SC_SUCCESS);
} }

View File

@ -812,8 +812,10 @@ static void print_ssh_key(FILE *outf, const char * alg, struct sc_pkcs15_object
if (opt_rfc4716) { if (opt_rfc4716) {
r = sc_base64_encode(buf, len, uu, 2*len, 64); r = sc_base64_encode(buf, len, uu, 2*len, 64);
if (r < 0) if (r < 0) {
free(uu);
return; return;
}
fprintf(outf,"---- BEGIN SSH2 PUBLIC KEY ----\n"); fprintf(outf,"---- BEGIN SSH2 PUBLIC KEY ----\n");