Skip to content

Commit 64b7e11

Browse files
authored
Merge pull request #900 from rhenium/ky/ssl-ctx-set-groups
ssl: rename `SSLContext#ecdh_curves=` to `#groups=`
2 parents b6f56c4 + 59e9860 commit 64b7e11

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

ext/openssl/ossl_ssl.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,47 +1182,48 @@ ossl_sslctx_set_tmp_dh(VALUE self, VALUE arg)
11821182
}
11831183
#endif
11841184

1185-
#if !defined(OPENSSL_NO_EC)
11861185
/*
11871186
* call-seq:
1188-
* ctx.ecdh_curves = curve_list -> curve_list
1187+
* ctx.groups = groups_list
1188+
* ctx.ecdh_curves = groups_list
11891189
*
1190-
* Sets the list of "supported elliptic curves" for this context.
1190+
* Sets the list of supported groups for key agreement for this context.
11911191
*
1192-
* For a TLS client, the list is directly used in the Supported Elliptic Curves
1193-
* Extension. For a server, the list is used by OpenSSL to determine the set of
1194-
* shared curves. OpenSSL will pick the most appropriate one from it.
1192+
* For a TLS client, the list is directly used in the "supported_groups"
1193+
* extension. For a server, the list is used by OpenSSL to determine the set of
1194+
* shared supported groups. OpenSSL will pick the most appropriate one from it.
1195+
*
1196+
* #ecdh_curves= is a deprecated alias for #groups=.
1197+
*
1198+
* See also the man page SSL_CTX_set1_groups_list(3).
11951199
*
11961200
* === Example
11971201
* ctx1 = OpenSSL::SSL::SSLContext.new
1198-
* ctx1.ecdh_curves = "X25519:P-256:P-224"
1202+
* ctx1.groups = "X25519:P-256:P-224"
11991203
* svr = OpenSSL::SSL::SSLServer.new(tcp_svr, ctx1)
12001204
* Thread.new { svr.accept }
12011205
*
12021206
* ctx2 = OpenSSL::SSL::SSLContext.new
1203-
* ctx2.ecdh_curves = "P-256"
1207+
* ctx2.groups = "P-256"
12041208
* cli = OpenSSL::SSL::SSLSocket.new(tcp_sock, ctx2)
12051209
* cli.connect
12061210
*
12071211
* p cli.tmp_key.group.curve_name
12081212
* # => "prime256v1" (is an alias for NIST P-256)
12091213
*/
12101214
static VALUE
1211-
ossl_sslctx_set_ecdh_curves(VALUE self, VALUE arg)
1215+
ossl_sslctx_set_groups(VALUE self, VALUE arg)
12121216
{
12131217
SSL_CTX *ctx;
12141218

12151219
rb_check_frozen(self);
12161220
GetSSLCTX(self, ctx);
12171221
StringValueCStr(arg);
12181222

1219-
if (!SSL_CTX_set1_curves_list(ctx, RSTRING_PTR(arg)))
1220-
ossl_raise(eSSLError, NULL);
1223+
if (!SSL_CTX_set1_groups_list(ctx, RSTRING_PTR(arg)))
1224+
ossl_raise(eSSLError, "SSL_CTX_set1_groups_list");
12211225
return arg;
12221226
}
1223-
#else
1224-
#define ossl_sslctx_set_ecdh_curves rb_f_notimplement
1225-
#endif
12261227

12271228
/*
12281229
* call-seq:
@@ -2958,7 +2959,8 @@ Init_ossl_ssl(void)
29582959
#ifndef OPENSSL_NO_DH
29592960
rb_define_method(cSSLContext, "tmp_dh=", ossl_sslctx_set_tmp_dh, 1);
29602961
#endif
2961-
rb_define_method(cSSLContext, "ecdh_curves=", ossl_sslctx_set_ecdh_curves, 1);
2962+
rb_define_method(cSSLContext, "groups=", ossl_sslctx_set_groups, 1);
2963+
rb_define_alias(cSSLContext, "ecdh_curves=", "groups=");
29622964
rb_define_method(cSSLContext, "security_level", ossl_sslctx_get_security_level, 0);
29632965
rb_define_method(cSSLContext, "security_level=", ossl_sslctx_set_security_level, 1);
29642966
#ifdef SSL_MODE_SEND_FALLBACK_SCSV

test/openssl/test_ssl.rb

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,33 +1764,28 @@ def test_get_ephemeral_key
17641764
end
17651765
end
17661766

1767-
if !aws_lc? # AWS-LC does not support DHE ciphersuites.
1768-
# DHE
1769-
# TODO: SSL_CTX_set1_groups() is required for testing this with TLS 1.3
1770-
ctx_proc2 = proc { |ctx|
1771-
ctx.max_version = OpenSSL::SSL::TLS1_2_VERSION
1772-
ctx.ciphers = "EDH"
1773-
ctx.tmp_dh = Fixtures.pkey("dh-1")
1774-
}
1775-
start_server(ctx_proc: ctx_proc2) do |port|
1767+
# DHE
1768+
# OpenSSL 3.0 added support for named FFDHE groups in TLS 1.3
1769+
# LibreSSL does not support named FFDHE groups currently
1770+
# AWS-LC does not support DHE ciphersuites
1771+
if openssl?(3, 0, 0)
1772+
start_server do |port|
17761773
ctx = OpenSSL::SSL::SSLContext.new
1777-
ctx.max_version = OpenSSL::SSL::TLS1_2_VERSION
1778-
ctx.ciphers = "EDH"
1774+
ctx.groups = "ffdhe3072"
17791775
server_connect(port, ctx) { |ssl|
17801776
assert_instance_of OpenSSL::PKey::DH, ssl.tmp_key
1777+
assert_equal 3072, ssl.tmp_key.p.num_bits
1778+
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
17811779
}
17821780
end
17831781
end
17841782

17851783
# ECDHE
17861784
ctx_proc3 = proc { |ctx|
1787-
ctx.ciphers = "DEFAULT:!kRSA:!kEDH"
1788-
ctx.ecdh_curves = "P-256"
1785+
ctx.groups = "P-256"
17891786
}
17901787
start_server(ctx_proc: ctx_proc3) do |port|
1791-
ctx = OpenSSL::SSL::SSLContext.new
1792-
ctx.ciphers = "DEFAULT:!kRSA:!kEDH"
1793-
server_connect(port, ctx) { |ssl|
1788+
server_connect(port) { |ssl|
17941789
assert_instance_of OpenSSL::PKey::EC, ssl.tmp_key
17951790
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
17961791
}
@@ -2079,17 +2074,17 @@ def test_tmp_dh
20792074
end
20802075
end
20812076

2082-
def test_ecdh_curves_tls12
2077+
def test_set_groups_tls12
20832078
ctx_proc = -> ctx {
20842079
# Enable both ECDHE (~ TLS 1.2) cipher suites and TLS 1.3
20852080
ctx.max_version = OpenSSL::SSL::TLS1_2_VERSION
20862081
ctx.ciphers = "kEECDH"
2087-
ctx.ecdh_curves = "P-384:P-521"
2082+
ctx.groups = "P-384:P-521"
20882083
}
20892084
start_server(ctx_proc: ctx_proc, ignore_listener_error: true) do |port|
20902085
# Test 1: Client=P-256:P-384, Server=P-384:P-521 --> P-384
20912086
ctx = OpenSSL::SSL::SSLContext.new
2092-
ctx.ecdh_curves = "P-256:P-384"
2087+
ctx.groups = "P-256:P-384"
20932088
server_connect(port, ctx) { |ssl|
20942089
cs = ssl.cipher[0]
20952090
assert_match (/\AECDH/), cs
@@ -2099,29 +2094,36 @@ def test_ecdh_curves_tls12
20992094

21002095
# Test 2: Client=P-256, Server=P-521:P-384 --> Fail
21012096
ctx = OpenSSL::SSL::SSLContext.new
2102-
ctx.ecdh_curves = "P-256"
2097+
ctx.groups = "P-256"
21032098
assert_raise(OpenSSL::SSL::SSLError) {
21042099
server_connect(port, ctx) { }
21052100
}
21062101

21072102
# Test 3: Client=P-521:P-384, Server=P-521:P-384 --> P-521
21082103
ctx = OpenSSL::SSL::SSLContext.new
2109-
ctx.ecdh_curves = "P-521:P-384"
2104+
ctx.groups = "P-521:P-384"
21102105
server_connect(port, ctx) { |ssl|
21112106
assert_equal "secp521r1", ssl.tmp_key.group.curve_name
21122107
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
21132108
}
2109+
2110+
# Test 4: #ecdh_curves= alias
2111+
ctx = OpenSSL::SSL::SSLContext.new
2112+
ctx.ecdh_curves = "P-256:P-384"
2113+
server_connect(port, ctx) { |ssl|
2114+
assert_equal "secp384r1", ssl.tmp_key.group.curve_name
2115+
}
21142116
end
21152117
end
21162118

2117-
def test_ecdh_curves_tls13
2119+
def test_set_groups_tls13
21182120
ctx_proc = -> ctx {
21192121
# Assume TLS 1.3 is enabled and chosen by default
2120-
ctx.ecdh_curves = "P-384:P-521"
2122+
ctx.groups = "P-384:P-521"
21212123
}
21222124
start_server(ctx_proc: ctx_proc, ignore_listener_error: true) do |port|
21232125
ctx = OpenSSL::SSL::SSLContext.new
2124-
ctx.ecdh_curves = "P-256:P-384" # disable P-521
2126+
ctx.groups = "P-256:P-384" # disable P-521
21252127

21262128
server_connect(port, ctx) { |ssl|
21272129
assert_equal "TLSv1.3", ssl.ssl_version

0 commit comments

Comments
 (0)