Skip to content

Commit 5295863

Browse files
authored
Merge pull request #72 from Geod24/set_rsa_keygen
rsa: EVP_PKEY_CTX_set_rsa_keygen_* are no longer macros in v3.0.0
2 parents b8a912c + dee7592 commit 5295863

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ jobs:
106106
echo "Expected version '${{ matrix.openssl.version }}' but got `pkg-config --modversion openssl`"
107107
exit 1
108108
fi
109+
dub test
109110
cd examples/sslecho/
110111
${{ github.workspace }}/openssl/install/bin/openssl req -batch -newkey rsa:4096 -x509 -sha256 -days 3650 -subj "/C=GB/CN=localhost" -nodes -out cert.pem -keyout key.pem
111112
dub build

dub.sdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ configuration "library-applink" {
2424

2525
configuration "unittest" {
2626
targetType "executable"
27+
targetName "openssl-test-library"
2728
dflags "-main"
2829
excludedSourceFiles "source/deimos/openssl/applink.d"
2930
preGenerateCommands `${DUB} scripts/generate_version.d` platform="posix"

source/deimos/openssl/rsa.d

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
module deimos.openssl.rsa;
6060

6161
import deimos.openssl._d_util;
62+
import deimos.openssl.opensslv;
6263

6364
import deimos.openssl.evp; // Needed for EVP_PKEY_ALG_CTRL.
6465

@@ -244,14 +245,44 @@ auto EVP_PKEY_CTX_get_rsa_pss_saltlen()(EVP_PKEY_CTX* ctx, int *plen) {
244245
0, plen);
245246
}
246247

247-
auto EVP_PKEY_CTX_set_rsa_keygen_bits()(EVP_PKEY_CTX* ctx, int bits) {
248-
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
249-
EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, null);
248+
static if (OPENSSL_VERSION_AT_LEAST(3, 0, 0))
249+
{
250+
// v3.0.0 deprecated `EVP_PKEY_CTX_set_rsa_keygen_pubexp` and introduced
251+
// a `[...]set1[...]` alternative:
252+
// https://github.com/openssl/openssl/commit/3786d74868fe440250f902ce1a78974136ca9304
253+
// This is for forward compatibility: Old code still works with new OpenSSL version
254+
alias EVP_PKEY_CTX_set_rsa_keygen_pubexp = EVP_PKEY_CTX_set1_rsa_keygen_pubexp;
255+
256+
// Before v3.0.0, those functions were macros (including above deprecated one):
257+
// https://github.com/openssl/openssl/commit/2972af109e10c5ce30e548190e3eee28327d6043
258+
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX* ctx, int bits);
259+
int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX* ctx, void* pubexp);
260+
int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX* ctx, int primes);
250261
}
262+
else
263+
{
264+
// Forward compatibility alias: Code written for v3.0.0 works with v1.1.1 and below
265+
alias EVP_PKEY_CTX_set1_rsa_keygen_pubexp = EVP_PKEY_CTX_set_rsa_keygen_pubexp;
266+
267+
auto EVP_PKEY_CTX_set_rsa_keygen_bits()(EVP_PKEY_CTX* ctx, int bits) {
268+
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
269+
EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, null);
270+
}
271+
272+
auto EVP_PKEY_CTX_set_rsa_keygen_pubexp()(EVP_PKEY_CTX* ctx, void* pubexp) {
273+
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
274+
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
275+
}
251276

252-
auto EVP_PKEY_CTX_set_rsa_keygen_pubexp()(EVP_PKEY_CTX* ctx, void* pubexp) {
253-
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
254-
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
277+
static if (OPENSSL_VERSION_AT_LEAST(1, 1, 1))
278+
{
279+
// Multi-prime RSA (RFC 8017), introduced in v1.1.1:
280+
// https://github.com/openssl/openssl/commit/665d899fa6d3571da016925067ebcf1789d7d19c
281+
auto EVP_PKEY_CTX_set_rsa_keygen_primes()(EVP_PKEY_CTX* ctx, int primes) {
282+
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
283+
EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes, null);
284+
}
285+
}
255286
}
256287

257288
auto EVP_PKEY_CTX_set_rsa_mgf1_md()(EVP_PKEY_CTX* ctx, EVP_MD* md) {
@@ -275,6 +306,9 @@ enum EVP_PKEY_CTRL_GET_RSA_PADDING = (EVP_PKEY_ALG_CTRL + 6);
275306
enum EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN = (EVP_PKEY_ALG_CTRL + 7);
276307
enum EVP_PKEY_CTRL_GET_RSA_MGF1_MD = (EVP_PKEY_ALG_CTRL + 8);
277308

309+
static if (OPENSSL_VERSION_AT_LEAST(1, 1, 1))
310+
enum EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES = (EVP_PKEY_ALG_CTRL + 13);
311+
278312
enum RSA_PKCS1_PADDING = 1;
279313
enum RSA_SSLV23_PADDING = 2;
280314
enum RSA_NO_PADDING = 3;

0 commit comments

Comments
 (0)