Skip to content

Commit

Permalink
Make rnp_key_handle_st C++ and remove unused locator field.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 authored and ronaldtse committed Mar 18, 2024
1 parent ed0c871 commit ebf0400
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
14 changes: 9 additions & 5 deletions src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2019 Ribose Inc.
* Copyright (c) 2019-2024 Ribose Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -33,10 +33,14 @@
#include "sec_profile.hpp"

struct rnp_key_handle_st {
rnp_ffi_t ffi;
pgp_key_search_t locator;
pgp_key_t * pub;
pgp_key_t * sec;
rnp_ffi_t ffi;
pgp_key_t *pub;
pgp_key_t *sec;

rnp_key_handle_st(rnp_ffi_t affi, pgp_key_t *apub = nullptr, pgp_key_t *asec = nullptr)
: ffi(affi), pub(apub), sec(asec)
{
}
};

struct rnp_uid_handle_st {
Expand Down
35 changes: 7 additions & 28 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,7 @@ rnp_password_cb_bounce(const pgp_password_ctx_t *ctx,
return false;
}

struct rnp_key_handle_st key = {};
key.ffi = ffi;
key.sec = (pgp_key_t *) ctx->key;
rnp_key_handle_st key(ffi, nullptr, (pgp_key_t *) ctx->key);
return ffi->getpasscb(ffi,
ffi->getpasscb_ctx,
ctx->key ? &key : NULL,
Expand Down Expand Up @@ -3697,15 +3695,7 @@ try {
return RNP_ERROR_KEY_NOT_FOUND;
}

struct rnp_key_handle_st *handle = (rnp_key_handle_st *) calloc(1, sizeof(*handle));
if (!handle) {
return RNP_ERROR_OUT_OF_MEMORY;
}
handle->ffi = ffi;
handle->pub = pub;
handle->sec = sec;
handle->locator = search;
*key = handle;
*key = new rnp_key_handle_st(ffi, pub, sec);
return RNP_SUCCESS;
}
FFI_GUARD
Expand Down Expand Up @@ -3868,21 +3858,17 @@ rnp_locate_key_int(rnp_ffi_t ffi,
pgp_key_t *sec = ffi->secring->search(locator);

if (require_secret && !sec) {
*handle = NULL;
*handle = nullptr;
return RNP_SUCCESS;
}

if (pub || sec) {
*handle = (rnp_key_handle_t) malloc(sizeof(**handle));
*handle = new (std::nothrow) rnp_key_handle_st(ffi, pub, sec);
if (!*handle) {
return RNP_ERROR_OUT_OF_MEMORY;
}
(*handle)->ffi = ffi;
(*handle)->pub = pub;
(*handle)->sec = sec;
(*handle)->locator = locator;
} else {
*handle = NULL;
*handle = nullptr;
}
return RNP_SUCCESS;
}
Expand Down Expand Up @@ -5817,13 +5803,7 @@ try {
return RNP_ERROR_BAD_PARAMETERS;
}

*handle = (rnp_key_handle_t) malloc(sizeof(**handle));
if (!*handle) {
return RNP_ERROR_OUT_OF_MEMORY;
}
(*handle)->ffi = op->ffi;
(*handle)->pub = op->gen_pub;
(*handle)->sec = op->gen_sec;
*handle = new rnp_key_handle_st(op->ffi, op->gen_pub, op->gen_sec);
return RNP_SUCCESS;
}
FFI_GUARD
Expand All @@ -5839,8 +5819,7 @@ FFI_GUARD
rnp_result_t
rnp_key_handle_destroy(rnp_key_handle_t key)
try {
// This does not free key->key which is owned by the keyring
free(key);
delete key;
return RNP_SUCCESS;
}
FFI_GUARD
Expand Down
7 changes: 1 addition & 6 deletions src/tests/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,12 +1275,7 @@ get_key_by_uid(rnp_ffi_t ffi, const char *uid)
rnp_key_handle_t
bogus_key_handle(rnp_ffi_t ffi)
{
rnp_key_handle_t handle = (rnp_key_handle_t) calloc(1, sizeof(*handle));
handle->ffi = ffi;
handle->pub = NULL;
handle->sec = NULL;
handle->locator.type = PGP_KEY_SEARCH_KEYID;
return handle;
return new rnp_key_handle_st(ffi);
}

bool
Expand Down

0 comments on commit ebf0400

Please sign in to comment.