Skip to content

Commit 1193fb1

Browse files
committed
Refactor ECDH curve name extraction to avoid deprecated methods
1 parent 435b86a commit 1193fb1

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

native/include/ssl_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#ifndef LIBRESSL_VERSION_NUMBER
5050
#include <openssl/provider.h>
5151
#endif
52+
#include <openssl/core_names.h>
5253

5354
#ifndef RAND_MAX
5455
#include <limits.h>
@@ -378,7 +379,7 @@ void SSL_BIO_doref(BIO *);
378379
DH *SSL_get_dh_params(unsigned keylen);
379380
EVP_PKEY *SSL_dh_GetParamFromFile(const char *);
380381
#ifdef HAVE_ECC
381-
EC_GROUP *SSL_ec_GetParamFromFile(const char *);
382+
int SSL_ec_GetParamFromFile(const char *);
382383
#endif
383384
DH *SSL_callback_tmp_DH(SSL *, int, int);
384385
void SSL_callback_handshake(const SSL *, int, int);

native/src/sslcontext.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,7 @@ TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificate)(TCN_STDARGS, jlong ctx,
946946
const char *p;
947947
char err[TCN_OPENSSL_ERROR_STRING_LENGTH];
948948
#ifdef HAVE_ECC
949-
EC_GROUP *ecparams = NULL;
950949
int nid;
951-
EC_KEY *eckey = NULL;
952950
#endif
953951
EVP_PKEY *evp;
954952

@@ -1036,14 +1034,10 @@ TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificate)(TCN_STDARGS, jlong ctx,
10361034
*/
10371035
/* XXX Does this also work for pkcs12 or only for PEM files?
10381036
* If only for PEM files move above to the PEM handling */
1039-
if ((ecparams = SSL_ec_GetParamFromFile(cert_file)) &&
1040-
(nid = EC_GROUP_get_curve_name(ecparams)) &&
1041-
(eckey = EC_KEY_new_by_curve_name(nid))) {
1042-
SSL_CTX_set_tmp_ecdh(c->ctx, eckey);
1043-
}
1044-
/* OpenSSL assures us that _free() is NULL-safe */
1045-
EC_KEY_free(eckey);
1046-
EC_GROUP_free(ecparams);
1037+
nid = SSL_ec_GetParamFromFile(cert_file);
1038+
if (nid != NID_undef) {
1039+
SSL_CTX_set1_groups(c->ctx, &nid, 1);
1040+
}
10471041
#endif
10481042
SSL_CTX_set_dh_auto(c->ctx, 1);
10491043

native/src/sslutils.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,41 @@ EVP_PKEY *SSL_dh_GetParamFromFile(const char *file)
198198
}
199199

200200
#ifdef HAVE_ECC
201-
EC_GROUP *SSL_ec_GetParamFromFile(const char *file)
201+
int SSL_ec_GetParamFromFile(const char *file)
202202
{
203-
EC_GROUP *group = NULL;
203+
EVP_PKEY *evp = NULL;
204204
BIO *bio;
205+
char curve_name[80];
205206

206207
if ((bio = BIO_new_file(file, "r")) == NULL)
207-
return NULL;
208-
group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL);
208+
return NID_undef;
209+
evp = PEM_read_bio_Parameters_ex(bio, NULL, NULL, NULL);
209210
BIO_free(bio);
210-
return (group);
211+
if (!EVP_PKEY_is_a(evp, "EC")) {
212+
EVP_PKEY_free(evp);
213+
return NID_undef;
214+
}
215+
216+
OSSL_PARAM param[] = {
217+
OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, curve_name, sizeof(curve_name)),
218+
OSSL_PARAM_construct_end()
219+
};
220+
221+
/* Query the curve name from the EVP_PKEY params object */
222+
if (EVP_PKEY_get_params(evp, param) <= 0) {
223+
EVP_PKEY_free(evp);
224+
return NID_undef; /* Failed to retrieve the curve name */
225+
}
226+
227+
/* Convert the curve name to the NID */
228+
int nid = OBJ_sn2nid(curve_name);
229+
if (nid == NID_undef) {
230+
/* If the short name didn't resolve, try the long name */
231+
nid = OBJ_ln2nid(curve_name);
232+
}
233+
234+
EVP_PKEY_free(evp);
235+
return nid; /* Returns the curve's NID, or NID_undef on failure */
211236
}
212237
#endif
213238

xdocs/miscellaneous/changelog.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
Remove group write permissions from the files in the tar.gz source
3838
archive. (markt)
3939
</fix>
40+
<fix>
41+
Refcator extraction of ECDH curve name from the Certificate to avoid
42+
deprecated OpenSSL methods. (markt)
43+
</fix>
4044
</changelog>
4145
</section>
4246
<section name="Changes in 2.0.12">

0 commit comments

Comments
 (0)