Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenPGP: fixed state tracking after erasing card #3024

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
OpenPGP: fixed state tracking after erasing card
fixes #3018
  • Loading branch information
frankmorgner committed Feb 27, 2024
commit 5a7139b6406343f840c12c16b0830e2ad30567a7
51 changes: 37 additions & 14 deletions src/libopensc/card-openpgp.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ pgp_match_card(sc_card_t *card)
}


/* populate MF - add matching blobs listed in the pgp_objects table */
int populate_blobs_to_mf(sc_card_t *card, struct pgp_priv_data *priv)
{
pgp_do_info_t *info;
for (info = priv->pgp_objects; (info != NULL) && (info->id > 0); info++) {
if (((info->access & READ_MASK) != READ_NEVER) && (info->get_fn != NULL)) {
pgp_blob_t *child = NULL;
sc_file_t *file = sc_file_new();

child = pgp_new_blob(card, priv->mf, info->id, file);

/* catch out of memory condition */
if (child == NULL) {
sc_file_free(file);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
}
}
}
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}

/**
* ABI: initialize driver & allocate private data.
*/
Expand All @@ -366,7 +387,6 @@ pgp_init(sc_card_t *card)
struct pgp_priv_data *priv;
sc_path_t path;
sc_file_t *file = NULL;
pgp_do_info_t *info;
int r, i;

LOG_FUNC_CALLED(card->ctx);
Expand Down Expand Up @@ -483,19 +503,10 @@ pgp_init(sc_card_t *card)
/* select MF */
priv->current = priv->mf;

/* populate MF - add matching blobs listed in the pgp_objects table */
for (info = priv->pgp_objects; (info != NULL) && (info->id > 0); info++) {
if (((info->access & READ_MASK) != READ_NEVER) && (info->get_fn != NULL)) {
pgp_blob_t *child = NULL;

child = pgp_new_blob(card, priv->mf, info->id, sc_file_new());

/* catch out of memory condition */
if (child == NULL) {
pgp_finish(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
}
}
r = populate_blobs_to_mf(card, priv);
if (r < 0) {
pgp_finish(card);
LOG_FUNC_RETURN(card->ctx, r);
}

/* get card_features from ATR & DOs */
Expand Down Expand Up @@ -3551,6 +3562,18 @@ pgp_erase_card(sc_card_t *card)
"Card does not offer life cycle management");
}

if (r == SC_SUCCESS && priv->mf) {
pgp_blob_t *new_mf = pgp_new_blob(card, NULL, priv->mf->id, priv->mf->file);
if (new_mf == NULL) {
LOG_TEST_RET(card->ctx, SC_ERROR_INTERNAL, "Failed to allocate the new MF blob");
}
priv->mf->file = NULL;

pgp_free_blobs(priv->mf);
priv->mf = new_mf;
populate_blobs_to_mf(card, priv);
}

LOG_FUNC_RETURN(card->ctx, r);
}

Expand Down
Loading