Skip to content

Commit

Permalink
minissl.c - Use Random.bytes if available (puma#2642)
Browse files Browse the repository at this point in the history
Co-authored-by: Ekin Dursun <ekindursun@gmail.com>

Co-authored-by: Ekin Dursun <ekindursun@gmail.com>
  • Loading branch information
2 people authored and JuanitoFatas committed Sep 9, 2022
1 parent 4133565 commit 55bd2bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ext/puma_http11/extconf.rb
Expand Up @@ -25,6 +25,14 @@

have_func "X509_STORE_up_ref"
have_func("SSL_CTX_set_ecdh_auto(NULL, 0)", "openssl/ssl.h")

# Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
if Random.respond_to?(:bytes)
$defs.push("-DHAVE_RANDOM_BYTES")
puts "checking for Random.bytes... yes"
else
puts "checking for Random.bytes... no"
end
end
end

Expand Down
13 changes: 10 additions & 3 deletions ext/puma_http11/mini_ssl.c
Expand Up @@ -310,9 +310,16 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
SSL_CTX_set_verify(ctx, NUM2INT(verify_mode), engine_verify_callback);
}

session_id_bytes = rb_funcall(rb_const_get(rb_cRandom, rb_intern_const("DEFAULT")),
rb_intern_const("bytes"),
1, ULL2NUM(SSL_MAX_SSL_SESSION_ID_LENGTH));
// Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
session_id_bytes = rb_funcall(
#ifdef HAVE_RANDOM_BYTES
rb_cRandom,
#else
rb_const_get(rb_cRandom, rb_intern_const("DEFAULT")),
#endif
rb_intern_const("bytes"),
1, ULL2NUM(SSL_MAX_SSL_SESSION_ID_LENGTH));

SSL_CTX_set_session_id_context(ctx,
(unsigned char *) RSTRING_PTR(session_id_bytes),
SSL_MAX_SSL_SESSION_ID_LENGTH);
Expand Down

0 comments on commit 55bd2bf

Please sign in to comment.