Skip to content

Commit

Permalink
OpenPGP: fixed state tracking after erasing card
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmorgner committed Feb 27, 2024
1 parent 8b4cdc4 commit 5a7139b
Showing 1 changed file with 37 additions and 14 deletions.
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

0 comments on commit 5a7139b

Please sign in to comment.