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

pkey: use high level EVP interface to generate parameters and keys #397

Merged
merged 5 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions ext/openssl/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ def find_openssl_library
if !have_struct_member("SSL", "ctx", "openssl/ssl.h") || is_libressl
$defs.push("-DHAVE_OPAQUE_OPENSSL")
end
have_func("BN_GENCB_new")
have_func("BN_GENCB_free")
have_func("BN_GENCB_get_arg")
have_func("EVP_MD_CTX_new")
have_func("EVP_MD_CTX_free")
have_func("EVP_MD_CTX_pkey_ctx")
Expand Down
12 changes: 0 additions & 12 deletions ext/openssl/openssl_missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@
#include "ruby/config.h"

/* added in 1.1.0 */
#if !defined(HAVE_BN_GENCB_NEW)
# define BN_GENCB_new() ((BN_GENCB *)OPENSSL_malloc(sizeof(BN_GENCB)))
#endif

#if !defined(HAVE_BN_GENCB_FREE)
# define BN_GENCB_free(cb) OPENSSL_free(cb)
#endif

#if !defined(HAVE_BN_GENCB_GET_ARG)
# define BN_GENCB_get_arg(cb) (cb)->arg
#endif

#if !defined(HAVE_EVP_MD_CTX_NEW)
# define EVP_MD_CTX_new EVP_MD_CTX_create
#endif
Expand Down
91 changes: 28 additions & 63 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,6 @@ VALUE cPKey;
VALUE ePKeyError;
static ID id_private_q;

/*
* callback for generating keys
*/
static VALUE
call_check_ints0(VALUE arg)
{
rb_thread_check_ints();
return Qnil;
}

static void *
call_check_ints(void *arg)
{
int state;
rb_protect(call_check_ints0, Qnil, &state);
return (void *)(VALUE)state;
}

int
ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
{
VALUE ary;
struct ossl_generate_cb_arg *arg;
int state;

arg = (struct ossl_generate_cb_arg *)BN_GENCB_get_arg(cb);
if (arg->yield) {
ary = rb_ary_new2(2);
rb_ary_store(ary, 0, INT2NUM(p));
rb_ary_store(ary, 1, INT2NUM(n));

/*
* can be break by raising exception or 'break'
*/
rb_protect(rb_yield, ary, &state);
if (state) {
arg->state = state;
return 0;
}
}
if (arg->interrupted) {
arg->interrupted = 0;
state = (int)(VALUE)rb_thread_call_with_gvl(call_check_ints, NULL);
if (state) {
arg->state = state;
return 0;
}
}
return 1;
}

void
ossl_generate_cb_stop(void *ptr)
{
struct ossl_generate_cb_arg *arg = (struct ossl_generate_cb_arg *)ptr;
arg->interrupted = 1;
}

static void
ossl_evp_pkey_free(void *ptr)
{
Expand Down Expand Up @@ -229,7 +171,7 @@ struct pkey_blocking_generate_arg {
int state;
int yield: 1;
int genparam: 1;
int stop: 1;
int interrupted: 1;
};

static VALUE
Expand All @@ -247,27 +189,50 @@ pkey_gen_cb_yield(VALUE ctx_v)
return rb_yield_values2(info_num, argv);
}

static VALUE
call_check_ints0(VALUE arg)
{
rb_thread_check_ints();
return Qnil;
}

static void *
call_check_ints(void *arg)
{
int state;
rb_protect(call_check_ints0, Qnil, &state);
return (void *)(VALUE)state;
}

static int
pkey_gen_cb(EVP_PKEY_CTX *ctx)
{
struct pkey_blocking_generate_arg *arg = EVP_PKEY_CTX_get_app_data(ctx);
int state;

if (arg->yield) {
int state;
rb_protect(pkey_gen_cb_yield, (VALUE)ctx, &state);
if (state) {
arg->stop = 1;
arg->state = state;
return 0;
}
}
return !arg->stop;
if (arg->interrupted) {
arg->interrupted = 0;
state = (int)(VALUE)rb_thread_call_with_gvl(call_check_ints, NULL);
if (state) {
arg->state = state;
return 0;
}
}
return 1;
}

static void
pkey_blocking_gen_stop(void *ptr)
{
struct pkey_blocking_generate_arg *arg = ptr;
arg->stop = 1;
arg->interrupted = 1;
}

static void *
Expand Down
8 changes: 0 additions & 8 deletions ext/openssl/ossl_pkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ extern const rb_data_type_t ossl_evp_pkey_type;
} \
} while (0)

struct ossl_generate_cb_arg {
int yield;
int interrupted;
int state;
};
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
void ossl_generate_cb_stop(void *ptr);

VALUE ossl_pkey_new(EVP_PKEY *);
void ossl_pkey_check_public_key(const EVP_PKEY *);
EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
Expand Down
Loading