src/pgp_sequoia.c
branchsync
changeset 3797 657dc02b1cc3
parent 3796 442937f4fabb
child 3798 8c5a4548b7d1
child 3800 fb9769f33508
child 3801 8854f7fd79a8
child 3840 bfef390b48e2
     1.1 --- a/src/pgp_sequoia.c	Fri May 31 23:35:59 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Sat Jun 01 10:54:32 2019 +0200
     1.3 @@ -400,6 +400,12 @@
     1.4                               -1, &session->sq_sql.tpk_save_insert_userids, NULL);
     1.5      assert(sqlite_result == SQLITE_OK);
     1.6  
     1.7 +    sqlite_result
     1.8 +        = sqlite3_prepare_v2(session->key_db,
     1.9 +                             "DELETE FROM keys WHERE primary_key = ?",
    1.10 +                             -1, &session->sq_sql.delete_keypair, NULL);
    1.11 +    assert(sqlite_result == SQLITE_OK);
    1.12 +
    1.13   out:
    1.14      if (status != PEP_STATUS_OK)
    1.15          pgp_release(session, in_first);
    1.16 @@ -425,8 +431,6 @@
    1.17      }
    1.18  }
    1.19  
    1.20 -/* commented out to omit compiler warning about unused function
    1.21 -
    1.22  // Ensures that a fingerprint is in canonical form.  A canonical
    1.23  // fingerprint doesn't contain any white space.
    1.24  //
    1.25 @@ -441,8 +445,6 @@
    1.26      return fpr_canonicalized;
    1.27  }
    1.28  
    1.29 -*/
    1.30 -
    1.31  // step statement and load the tpk and secret.
    1.32  static PEP_STATUS key_load(PEP_SESSION, sqlite3_stmt *, pgp_tpk_t *, int *)
    1.33      __attribute__((nonnull(1, 2)));
    1.34 @@ -1844,40 +1846,37 @@
    1.35      return status;
    1.36  }
    1.37  
    1.38 -#define SQL_DELETE "DELETE FROM keys WHERE primary_key = '%s' ;"
    1.39 -static const char *sql_delete = SQL_DELETE;
    1.40 -static const size_t sql_delete_size = sizeof(SQL_DELETE);
    1.41 -
    1.42 -// FIXME: this is deleting the key from the index but not the key data
    1.43 -
    1.44 -PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
    1.45 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr_raw)
    1.46  {
    1.47 -    assert(session && fpr && fpr[0]);
    1.48 -    if (!(session && fpr && fpr[0]))
    1.49 -        return PEP_ILLEGAL_VALUE;
    1.50 -
    1.51 -    size_t sql_size = sql_delete_size + strlen(fpr);
    1.52 -    char *sql = calloc(1, sql_size);
    1.53 -    assert(sql);
    1.54 -    if (!sql)
    1.55 -        return PEP_OUT_OF_MEMORY;
    1.56 -
    1.57 -    int r = snprintf(sql, sql_size, sql_delete, fpr);
    1.58 -    assert(r > 0 && r < sql_size);
    1.59 -    if (r < 0)
    1.60 -        return PEP_UNKNOWN_ERROR;
    1.61 -
    1.62 -    int sqlite_result = sqlite3_exec(session->key_db, sql, NULL, NULL, NULL);
    1.63 -    assert(sqlite_result == SQLITE_OK);
    1.64 -    if (sqlite_result != SQLITE_OK)
    1.65 -        return PEP_CANNOT_DELETE_KEY;
    1.66 +    PEP_STATUS status = PEP_STATUS_OK;
    1.67 +
    1.68 +    assert(session && fpr_raw && fpr_raw[0]);
    1.69 +    if (!(session && fpr_raw && fpr_raw[0]))
    1.70 +        ERROR_OUT(NULL, PEP_ILLEGAL_VALUE, "invalid arguments");
    1.71 +
    1.72 +    char *fpr = pgp_fingerprint_canonicalize(fpr_raw);
    1.73 +    if (! fpr)
    1.74 +        ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.75 +
    1.76 +    T("Deleting %s", fpr);
    1.77 +
    1.78 +    sqlite3_stmt *stmt = session->sq_sql.delete_keypair;
    1.79 +    sqlite3_bind_text(stmt, 1, fpr, -1, free);
    1.80 +
    1.81 +    int sqlite_result = Sqlite3_step(stmt);
    1.82 +    sqlite3_reset(stmt);
    1.83 +    if (sqlite_result != SQLITE_DONE)
    1.84 +        ERROR_OUT(NULL, PEP_CANNOT_DELETE_KEY,
    1.85 +                  "deletion failed: %s", sqlite3_errmsg(session->key_db));
    1.86  
    1.87      sqlite_result = sqlite3_changes(session->key_db);
    1.88      assert(sqlite_result >= 0 && sqlite_result < 2);
    1.89      if (sqlite_result < 1)
    1.90 -        return PEP_KEY_NOT_FOUND;
    1.91 -
    1.92 -    return PEP_STATUS_OK;
    1.93 +        ERROR_OUT(NULL, PEP_KEY_NOT_FOUND,
    1.94 +                  "attempt to delete non-existent key: %s", fpr_raw);
    1.95 +
    1.96 + out:
    1.97 +    return status;
    1.98  }
    1.99  
   1.100  PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,