sequoia: Improve pgp_delete_keypair sync
authorNeal H. Walfield <neal@pep.foundation>
Sat, 01 Jun 2019 10:54:32 +0200
branchsync
changeset 3797657dc02b1cc3
parent 3796 442937f4fabb
child 3798 8c5a4548b7d1
child 3799 5f0990b24c15
child 3840 bfef390b48e2
sequoia: Improve pgp_delete_keypair

- Execute the SQL in the same way as everywhere else.

- Note: the comment that the keydata was not being deleted was
inaccurate: the "ON DELETE CASCADE"s ensures that the key and all
associated data is removed from the database.
src/pEp_internal.h
src/pgp_sequoia.c
     1.1 --- a/src/pEp_internal.h	Fri May 31 23:35:59 2019 +0200
     1.2 +++ b/src/pEp_internal.h	Sat Jun 01 10:54:32 2019 +0200
     1.3 @@ -144,6 +144,7 @@
     1.4          sqlite3_stmt *tpk_save_insert_primary;
     1.5          sqlite3_stmt *tpk_save_insert_subkeys;
     1.6          sqlite3_stmt *tpk_save_insert_userids;
     1.7 +        sqlite3_stmt *delete_keypair;
     1.8      } sq_sql;
     1.9  #endif
    1.10  
     2.1 --- a/src/pgp_sequoia.c	Fri May 31 23:35:59 2019 +0200
     2.2 +++ b/src/pgp_sequoia.c	Sat Jun 01 10:54:32 2019 +0200
     2.3 @@ -400,6 +400,12 @@
     2.4                               -1, &session->sq_sql.tpk_save_insert_userids, NULL);
     2.5      assert(sqlite_result == SQLITE_OK);
     2.6  
     2.7 +    sqlite_result
     2.8 +        = sqlite3_prepare_v2(session->key_db,
     2.9 +                             "DELETE FROM keys WHERE primary_key = ?",
    2.10 +                             -1, &session->sq_sql.delete_keypair, NULL);
    2.11 +    assert(sqlite_result == SQLITE_OK);
    2.12 +
    2.13   out:
    2.14      if (status != PEP_STATUS_OK)
    2.15          pgp_release(session, in_first);
    2.16 @@ -425,8 +431,6 @@
    2.17      }
    2.18  }
    2.19  
    2.20 -/* commented out to omit compiler warning about unused function
    2.21 -
    2.22  // Ensures that a fingerprint is in canonical form.  A canonical
    2.23  // fingerprint doesn't contain any white space.
    2.24  //
    2.25 @@ -441,8 +445,6 @@
    2.26      return fpr_canonicalized;
    2.27  }
    2.28  
    2.29 -*/
    2.30 -
    2.31  // step statement and load the tpk and secret.
    2.32  static PEP_STATUS key_load(PEP_SESSION, sqlite3_stmt *, pgp_tpk_t *, int *)
    2.33      __attribute__((nonnull(1, 2)));
    2.34 @@ -1844,40 +1846,37 @@
    2.35      return status;
    2.36  }
    2.37  
    2.38 -#define SQL_DELETE "DELETE FROM keys WHERE primary_key = '%s' ;"
    2.39 -static const char *sql_delete = SQL_DELETE;
    2.40 -static const size_t sql_delete_size = sizeof(SQL_DELETE);
    2.41 -
    2.42 -// FIXME: this is deleting the key from the index but not the key data
    2.43 -
    2.44 -PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
    2.45 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr_raw)
    2.46  {
    2.47 -    assert(session && fpr && fpr[0]);
    2.48 -    if (!(session && fpr && fpr[0]))
    2.49 -        return PEP_ILLEGAL_VALUE;
    2.50 -
    2.51 -    size_t sql_size = sql_delete_size + strlen(fpr);
    2.52 -    char *sql = calloc(1, sql_size);
    2.53 -    assert(sql);
    2.54 -    if (!sql)
    2.55 -        return PEP_OUT_OF_MEMORY;
    2.56 -
    2.57 -    int r = snprintf(sql, sql_size, sql_delete, fpr);
    2.58 -    assert(r > 0 && r < sql_size);
    2.59 -    if (r < 0)
    2.60 -        return PEP_UNKNOWN_ERROR;
    2.61 -
    2.62 -    int sqlite_result = sqlite3_exec(session->key_db, sql, NULL, NULL, NULL);
    2.63 -    assert(sqlite_result == SQLITE_OK);
    2.64 -    if (sqlite_result != SQLITE_OK)
    2.65 -        return PEP_CANNOT_DELETE_KEY;
    2.66 +    PEP_STATUS status = PEP_STATUS_OK;
    2.67 +
    2.68 +    assert(session && fpr_raw && fpr_raw[0]);
    2.69 +    if (!(session && fpr_raw && fpr_raw[0]))
    2.70 +        ERROR_OUT(NULL, PEP_ILLEGAL_VALUE, "invalid arguments");
    2.71 +
    2.72 +    char *fpr = pgp_fingerprint_canonicalize(fpr_raw);
    2.73 +    if (! fpr)
    2.74 +        ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    2.75 +
    2.76 +    T("Deleting %s", fpr);
    2.77 +
    2.78 +    sqlite3_stmt *stmt = session->sq_sql.delete_keypair;
    2.79 +    sqlite3_bind_text(stmt, 1, fpr, -1, free);
    2.80 +
    2.81 +    int sqlite_result = Sqlite3_step(stmt);
    2.82 +    sqlite3_reset(stmt);
    2.83 +    if (sqlite_result != SQLITE_DONE)
    2.84 +        ERROR_OUT(NULL, PEP_CANNOT_DELETE_KEY,
    2.85 +                  "deletion failed: %s", sqlite3_errmsg(session->key_db));
    2.86  
    2.87      sqlite_result = sqlite3_changes(session->key_db);
    2.88      assert(sqlite_result >= 0 && sqlite_result < 2);
    2.89      if (sqlite_result < 1)
    2.90 -        return PEP_KEY_NOT_FOUND;
    2.91 -
    2.92 -    return PEP_STATUS_OK;
    2.93 +        ERROR_OUT(NULL, PEP_KEY_NOT_FOUND,
    2.94 +                  "attempt to delete non-existent key: %s", fpr_raw);
    2.95 +
    2.96 + out:
    2.97 +    return status;
    2.98  }
    2.99  
   2.100  PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,