pkcs15-oberthur: fix memory leakage
This commit is contained in:
parent
ac84d282b1
commit
d636338eaf
|
@ -279,7 +279,10 @@ awp_create_container_record (struct sc_pkcs15_card *p15card, struct sc_profile *
|
||||||
memset(buff, 0, list_file->record_length);
|
memset(buff, 0, list_file->record_length);
|
||||||
|
|
||||||
rv = awp_new_container_entry(p15card, buff, list_file->record_length);
|
rv = awp_new_container_entry(p15card, buff, list_file->record_length);
|
||||||
|
if (rv < 0) {
|
||||||
|
free(buff);
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot create container");
|
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot create container");
|
||||||
|
}
|
||||||
|
|
||||||
*(buff + 0) = (acc->pubkey_id >> 8) & 0xFF;
|
*(buff + 0) = (acc->pubkey_id >> 8) & 0xFF;
|
||||||
*(buff + 1) = acc->pubkey_id & 0xFF;
|
*(buff + 1) = acc->pubkey_id & 0xFF;
|
||||||
|
@ -289,7 +292,6 @@ awp_create_container_record (struct sc_pkcs15_card *p15card, struct sc_profile *
|
||||||
*(buff + 5) = acc->cert_id & 0xFF;
|
*(buff + 5) = acc->cert_id & 0xFF;
|
||||||
|
|
||||||
rv = sc_select_file(p15card->card, &list_file->path, NULL);
|
rv = sc_select_file(p15card->card, &list_file->path, NULL);
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "rv:%i", rv);
|
|
||||||
if (rv == SC_ERROR_FILE_NOT_FOUND)
|
if (rv == SC_ERROR_FILE_NOT_FOUND)
|
||||||
rv = sc_pkcs15init_create_file(profile, p15card, list_file);
|
rv = sc_pkcs15init_create_file(profile, p15card, list_file);
|
||||||
|
|
||||||
|
@ -297,10 +299,6 @@ awp_create_container_record (struct sc_pkcs15_card *p15card, struct sc_profile *
|
||||||
rv = sc_append_record(p15card->card, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
rv = sc_append_record(p15card->card, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
||||||
|
|
||||||
free(buff);
|
free(buff);
|
||||||
|
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "return after failure");
|
|
||||||
|
|
||||||
rv = 0;
|
|
||||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,14 +356,15 @@ awp_update_container_entry (struct sc_pkcs15_card *p15card, struct sc_profile *p
|
||||||
|
|
||||||
if (rec > list_file->record_count) {
|
if (rec > list_file->record_count) {
|
||||||
rv = awp_new_container_entry(p15card, buff, list_file->record_length);
|
rv = awp_new_container_entry(p15card, buff, list_file->record_length);
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot create container");
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rv = sc_select_file(p15card->card, &list_file->path, NULL);
|
rv = sc_select_file(p15card->card, &list_file->path, NULL);
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot select list_file");
|
if (!rv)
|
||||||
|
|
||||||
rv = sc_read_record(p15card->card, rec, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
rv = sc_read_record(p15card->card, rec, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "Cannot read record");
|
}
|
||||||
|
if (rv < 0) {
|
||||||
|
free(buff);
|
||||||
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -390,7 +389,8 @@ awp_update_container_entry (struct sc_pkcs15_card *p15card, struct sc_profile *p
|
||||||
*(buff + offs + 5) = file_id & 0xFF;
|
*(buff + offs + 5) = file_id & 0xFF;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_INCORRECT_PARAMETERS, "invalid object type");
|
free(buff);
|
||||||
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_INCORRECT_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rec > list_file->record_count) {
|
if (rec > list_file->record_count) {
|
||||||
|
@ -403,14 +403,9 @@ awp_update_container_entry (struct sc_pkcs15_card *p15card, struct sc_profile *p
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rv = sc_update_record(p15card->card, rec, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
rv = sc_update_record(p15card->card, rec, buff, list_file->record_length, SC_RECORD_BY_REC_NR);
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "rv:%i", rv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buff);
|
free(buff);
|
||||||
|
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, rv, "return after failure");
|
|
||||||
|
|
||||||
rv = 0;
|
|
||||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,17 +438,14 @@ awp_update_container(struct sc_pkcs15_card *p15card, struct sc_profile *profile,
|
||||||
rv = awp_new_file(p15card, profile, COSM_CONTAINER_LIST, 0, &clist, NULL);
|
rv = awp_new_file(p15card, profile, COSM_CONTAINER_LIST, 0, &clist, NULL);
|
||||||
if (rv)
|
if (rv)
|
||||||
goto done;
|
goto done;
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "contaner cfile(rcount:%i,rlength:%i)", clist->record_count, clist->record_length);
|
|
||||||
|
|
||||||
rv = sc_select_file(p15card->card, &clist->path, &file);
|
rv = sc_select_file(p15card->card, &clist->path, &file);
|
||||||
if (rv)
|
if (rv)
|
||||||
goto done;
|
goto done;
|
||||||
file->record_length = clist->record_length;
|
file->record_length = clist->record_length;
|
||||||
|
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "contaner file(rcount:%i,rlength:%i)", file->record_count, file->record_length);
|
|
||||||
if (type == SC_PKCS15_TYPE_PRKEY_RSA || type == COSM_TYPE_PRKEY_RSA) {
|
if (type == SC_PKCS15_TYPE_PRKEY_RSA || type == COSM_TYPE_PRKEY_RSA) {
|
||||||
rec_offs = 0;
|
rec_offs = 0;
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Append new record %i for private key", file->record_count + 1);
|
|
||||||
rv = awp_update_container_entry(p15card, profile, file, type, obj_id, file->record_count + 1, rec_offs);
|
rv = awp_update_container_entry(p15card, profile, file, type, obj_id, file->record_count + 1, rec_offs);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
@ -490,46 +482,45 @@ awp_update_container(struct sc_pkcs15_card *p15card, struct sc_profile *profile,
|
||||||
struct sc_path path = private_path;
|
struct sc_path path = private_path;
|
||||||
struct sc_file *ff = NULL;
|
struct sc_file *ff = NULL;
|
||||||
|
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "container contains PrKey %02X%02X", *(list + offs + 2), *(list + offs + 3));
|
|
||||||
path.value[path.len - 2] = *(list + offs + 2) | 0x01;
|
path.value[path.len - 2] = *(list + offs + 2) | 0x01;
|
||||||
path.value[path.len - 1] = *(list + offs + 3);
|
path.value[path.len - 1] = *(list + offs + 3);
|
||||||
rv = sc_select_file(p15card->card, &path, &ff);
|
rv = sc_select_file(p15card->card, &path, &ff);
|
||||||
if (rv)
|
if (rv)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "file id %X; size %i", ff->id, ff->size);
|
|
||||||
buff = malloc(ff->size);
|
|
||||||
if (!buff) {
|
|
||||||
rv = SC_ERROR_OUT_OF_MEMORY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
rv = sc_pkcs15init_authenticate(profile, p15card, ff, SC_AC_OP_READ);
|
rv = sc_pkcs15init_authenticate(profile, p15card, ff, SC_AC_OP_READ);
|
||||||
if (rv) {
|
if (rv) {
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "sc_pkcs15init_authenticate(READ) failed");
|
sc_file_free(ff);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buff = malloc(ff->size);
|
||||||
|
if (!buff)
|
||||||
|
rv = SC_ERROR_OUT_OF_MEMORY;
|
||||||
|
|
||||||
|
if (!rv) {
|
||||||
rv = sc_read_binary(p15card->card, 0, buff, ff->size, 0);
|
rv = sc_read_binary(p15card->card, 0, buff, ff->size, 0);
|
||||||
if ((unsigned)rv == ff->size) {
|
if ((unsigned)rv == ff->size) {
|
||||||
rv = 0;
|
rv = 0;
|
||||||
id_offs = 5 + *(buff+3);
|
id_offs = 5 + *(buff+3);
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "rec %i; id offset %i",rec, id_offs);
|
|
||||||
if (key_id->len == *(buff + id_offs) &&
|
if (key_id->len == *(buff + id_offs) &&
|
||||||
!memcmp(key_id->value, buff + id_offs + 1, key_id->len)) {
|
!memcmp(key_id->value, buff + id_offs + 1, key_id->len)) {
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "found key file friend");
|
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "found key file friend");
|
||||||
if (!rv)
|
if (!rv)
|
||||||
rv = awp_update_container_entry(p15card, profile, file, type, obj_id, rec + 1, rec_offs);
|
rv = awp_update_container_entry(p15card, profile, file, type, obj_id, rec + 1, rec_offs);
|
||||||
|
|
||||||
if (rv >= 0 && prkey_id) {
|
if (rv >= 0 && prkey_id)
|
||||||
*prkey_id = *(list + offs + 2) * 0x100 + *(list + offs + 3);
|
*prkey_id = *(list + offs + 2) * 0x100 + *(list + offs + 3);
|
||||||
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "*prkey_id 0x%X", *prkey_id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buff);
|
free(buff);
|
||||||
sc_file_free(ff);
|
sc_file_free(ff);
|
||||||
|
|
||||||
|
if (rv)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -575,8 +566,7 @@ awp_set_certificate_info (struct sc_pkcs15_card *p15card,
|
||||||
*blob = (COSM_TAG_CERT >> 8) & 0xFF;
|
*blob = (COSM_TAG_CERT >> 8) & 0xFF;
|
||||||
*(blob + 1) = COSM_TAG_CERT & 0xFF;
|
*(blob + 1) = COSM_TAG_CERT & 0xFF;
|
||||||
|
|
||||||
if (ci->label.len
|
if (ci->label.len && ci->label.len != strlen(default_cert_label)
|
||||||
&& ci->label.len != strlen(default_cert_label)
|
|
||||||
&& memcmp(ci->label.value, default_cert_label, strlen(default_cert_label)))
|
&& memcmp(ci->label.value, default_cert_label, strlen(default_cert_label)))
|
||||||
r = awp_update_blob(ctx, &blob, &blob_size, &ci->label, TLV_TYPE_LLV);
|
r = awp_update_blob(ctx, &blob, &blob_size, &ci->label, TLV_TYPE_LLV);
|
||||||
else
|
else
|
||||||
|
@ -1088,8 +1078,7 @@ awp_encode_data_info(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *ob
|
||||||
if (di->app.len) {
|
if (di->app.len) {
|
||||||
di->app.value = (unsigned char *)strdup(data_info->app_label);
|
di->app.value = (unsigned char *)strdup(data_info->app_label);
|
||||||
if (!di->app.value)
|
if (!di->app.value)
|
||||||
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY,
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
||||||
"AWP encode data failed: cannot allocate App.Label");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r = sc_asn1_encode_object_id(&buf, &buflen, &data_info->app_oid);
|
r = sc_asn1_encode_object_id(&buf, &buflen, &data_info->app_oid);
|
||||||
|
|
Loading…
Reference in New Issue