Skip to content

Commit d172036

Browse files
authored
Merge pull request #397 from rhenium/ky/pkey-refactor-generate
pkey: use high level EVP interface to generate parameters and keys
2 parents 44dbdfa + 81027b7 commit d172036

File tree

10 files changed

+239
-488
lines changed

10 files changed

+239
-488
lines changed

ext/openssl/extconf.rb

-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ def find_openssl_library
130130
if !have_struct_member("SSL", "ctx", "openssl/ssl.h") || is_libressl
131131
$defs.push("-DHAVE_OPAQUE_OPENSSL")
132132
end
133-
have_func("BN_GENCB_new")
134-
have_func("BN_GENCB_free")
135-
have_func("BN_GENCB_get_arg")
136133
have_func("EVP_MD_CTX_new")
137134
have_func("EVP_MD_CTX_free")
138135
have_func("EVP_MD_CTX_pkey_ctx")

ext/openssl/openssl_missing.h

-12
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@
1313
#include "ruby/config.h"
1414

1515
/* added in 1.1.0 */
16-
#if !defined(HAVE_BN_GENCB_NEW)
17-
# define BN_GENCB_new() ((BN_GENCB *)OPENSSL_malloc(sizeof(BN_GENCB)))
18-
#endif
19-
20-
#if !defined(HAVE_BN_GENCB_FREE)
21-
# define BN_GENCB_free(cb) OPENSSL_free(cb)
22-
#endif
23-
24-
#if !defined(HAVE_BN_GENCB_GET_ARG)
25-
# define BN_GENCB_get_arg(cb) (cb)->arg
26-
#endif
27-
2816
#if !defined(HAVE_EVP_MD_CTX_NEW)
2917
# define EVP_MD_CTX_new EVP_MD_CTX_create
3018
#endif

ext/openssl/ossl_pkey.c

+28-63
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,6 @@ VALUE cPKey;
1717
VALUE ePKeyError;
1818
static ID id_private_q;
1919

20-
/*
21-
* callback for generating keys
22-
*/
23-
static VALUE
24-
call_check_ints0(VALUE arg)
25-
{
26-
rb_thread_check_ints();
27-
return Qnil;
28-
}
29-
30-
static void *
31-
call_check_ints(void *arg)
32-
{
33-
int state;
34-
rb_protect(call_check_ints0, Qnil, &state);
35-
return (void *)(VALUE)state;
36-
}
37-
38-
int
39-
ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
40-
{
41-
VALUE ary;
42-
struct ossl_generate_cb_arg *arg;
43-
int state;
44-
45-
arg = (struct ossl_generate_cb_arg *)BN_GENCB_get_arg(cb);
46-
if (arg->yield) {
47-
ary = rb_ary_new2(2);
48-
rb_ary_store(ary, 0, INT2NUM(p));
49-
rb_ary_store(ary, 1, INT2NUM(n));
50-
51-
/*
52-
* can be break by raising exception or 'break'
53-
*/
54-
rb_protect(rb_yield, ary, &state);
55-
if (state) {
56-
arg->state = state;
57-
return 0;
58-
}
59-
}
60-
if (arg->interrupted) {
61-
arg->interrupted = 0;
62-
state = (int)(VALUE)rb_thread_call_with_gvl(call_check_ints, NULL);
63-
if (state) {
64-
arg->state = state;
65-
return 0;
66-
}
67-
}
68-
return 1;
69-
}
70-
71-
void
72-
ossl_generate_cb_stop(void *ptr)
73-
{
74-
struct ossl_generate_cb_arg *arg = (struct ossl_generate_cb_arg *)ptr;
75-
arg->interrupted = 1;
76-
}
77-
7820
static void
7921
ossl_evp_pkey_free(void *ptr)
8022
{
@@ -239,7 +181,7 @@ struct pkey_blocking_generate_arg {
239181
int state;
240182
int yield: 1;
241183
int genparam: 1;
242-
int stop: 1;
184+
int interrupted: 1;
243185
};
244186

245187
static VALUE
@@ -257,27 +199,50 @@ pkey_gen_cb_yield(VALUE ctx_v)
257199
return rb_yield_values2(info_num, argv);
258200
}
259201

202+
static VALUE
203+
call_check_ints0(VALUE arg)
204+
{
205+
rb_thread_check_ints();
206+
return Qnil;
207+
}
208+
209+
static void *
210+
call_check_ints(void *arg)
211+
{
212+
int state;
213+
rb_protect(call_check_ints0, Qnil, &state);
214+
return (void *)(VALUE)state;
215+
}
216+
260217
static int
261218
pkey_gen_cb(EVP_PKEY_CTX *ctx)
262219
{
263220
struct pkey_blocking_generate_arg *arg = EVP_PKEY_CTX_get_app_data(ctx);
221+
int state;
264222

265223
if (arg->yield) {
266-
int state;
267224
rb_protect(pkey_gen_cb_yield, (VALUE)ctx, &state);
268225
if (state) {
269-
arg->stop = 1;
270226
arg->state = state;
227+
return 0;
271228
}
272229
}
273-
return !arg->stop;
230+
if (arg->interrupted) {
231+
arg->interrupted = 0;
232+
state = (int)(VALUE)rb_thread_call_with_gvl(call_check_ints, NULL);
233+
if (state) {
234+
arg->state = state;
235+
return 0;
236+
}
237+
}
238+
return 1;
274239
}
275240

276241
static void
277242
pkey_blocking_gen_stop(void *ptr)
278243
{
279244
struct pkey_blocking_generate_arg *arg = ptr;
280-
arg->stop = 1;
245+
arg->interrupted = 1;
281246
}
282247

283248
static void *

ext/openssl/ossl_pkey.h

-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ extern const rb_data_type_t ossl_evp_pkey_type;
3535
} \
3636
} while (0)
3737

38-
struct ossl_generate_cb_arg {
39-
int yield;
40-
int interrupted;
41-
int state;
42-
};
43-
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
44-
void ossl_generate_cb_stop(void *ptr);
45-
4638
VALUE ossl_pkey_new(EVP_PKEY *);
4739
void ossl_pkey_check_public_key(const EVP_PKEY *);
4840
EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);

0 commit comments

Comments
 (0)