Skip to content

Commit 3e6e4d1

Browse files
committed
WIP for OpenSSL 3.0 support.
We attempt to migrate off of APIs that are deprecated in OpenSSL 3.0, as detected by using OPENSSL_VERSION_MAJOR. At this point, encryption with 3.x APIs passes regression tests against the decryption using 1.x APIs, but decryption using 3.x APIs doesn't work. In the case of sha256(), a series of deprecated library calls was replaced by a single macro that is supported on both OpenSSL 1.x and 3.x. Towards #1300.
1 parent 7e05ef2 commit 3e6e4d1

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

src/iperf_auth.c

+56-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* iperf, Copyright (c) 2014-2020, The Regents of the University of
2+
* iperf, Copyright (c) 2014-2023, The Regents of the University of
33
* California, through Lawrence Berkeley National Laboratory (subject
44
* to receipt of any required approvals from the U.S. Dept. of
55
* Energy). All rights reserved.
@@ -46,16 +46,18 @@
4646
#include <openssl/sha.h>
4747
#include <openssl/buffer.h>
4848
#include <openssl/err.h>
49+
#if OPENSSL_VERSION_MAJOR >= 3
50+
#include <openssl/evp.h>
51+
#include <openssl/core_names.h>
52+
#endif
4953

5054
const char *auth_text_format = "user: %s\npwd: %s\nts: %"PRId64;
5155

5256
void sha256(const char *string, char outputBuffer[65])
5357
{
5458
unsigned char hash[SHA256_DIGEST_LENGTH];
55-
SHA256_CTX sha256;
56-
SHA256_Init(&sha256);
57-
SHA256_Update(&sha256, string, strlen(string));
58-
SHA256_Final(hash, &sha256);
59+
60+
SHA256((const unsigned char *) string, strlen(string), hash);
5961
int i = 0;
6062
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
6163
{
@@ -229,25 +231,42 @@ int test_load_private_key_from_file(const char *file){
229231
}
230232

231233
int encrypt_rsa_message(const char *plaintext, EVP_PKEY *public_key, unsigned char **encryptedtext) {
234+
#if OPENSSL_VERSION_MAJOR >= 3
235+
EVP_PKEY_CTX *ctx;
236+
#else
232237
RSA *rsa = NULL;
233-
unsigned char *rsa_buffer = NULL, pad = RSA_PKCS1_PADDING;
234-
int keysize, encryptedtext_len, rsa_buffer_len;
235-
238+
#endif
239+
unsigned char *rsa_buffer = NULL;
240+
size_t encryptedtext_len = 0;
241+
int rsa_buffer_len, keysize;
242+
243+
#if OPENSSL_VERSION_MAJOR >= 3
244+
int rc;
245+
ctx = EVP_PKEY_CTX_new_from_pkey(NULL, public_key, "");
246+
/* See evp_pkey_rsa(7) and provider-keymgmt(7) */
247+
rc = EVP_PKEY_get_int_param(public_key, OSSL_PKEY_PARAM_MAX_SIZE, &keysize); /* XXX not really keysize */
248+
#else
236249
rsa = EVP_PKEY_get1_RSA(public_key);
237250
keysize = RSA_size(rsa);
238-
251+
#endif
239252
rsa_buffer = OPENSSL_malloc(keysize * 2);
240253
*encryptedtext = (unsigned char*)OPENSSL_malloc(keysize);
241254

242255
BIO *bioBuff = BIO_new_mem_buf((void*)plaintext, (int)strlen(plaintext));
243256
rsa_buffer_len = BIO_read(bioBuff, rsa_buffer, keysize * 2);
244-
encryptedtext_len = RSA_public_encrypt(rsa_buffer_len, rsa_buffer, *encryptedtext, rsa, pad);
245-
257+
#if OPENSSL_VERSION_MAJOR >= 3
258+
EVP_PKEY_encrypt_init(ctx);
259+
EVP_PKEY_encrypt(ctx, *encryptedtext, &encryptedtext_len, rsa_buffer, rsa_buffer_len);
260+
EVP_PKEY_CTX_free(ctx);
261+
#else
262+
encryptedtext_len = RSA_public_encrypt(rsa_buffer_len, rsa_buffer, *encryptedtext, rsa, RSA_PKCS1_PADDING);
246263
RSA_free(rsa);
264+
#endif
265+
247266
OPENSSL_free(rsa_buffer);
248267
BIO_free(bioBuff);
249268

250-
if (encryptedtext_len < 0) {
269+
if (encryptedtext_len <= 0) {
251270
/* We probably shouldn't be printing stuff like this */
252271
fprintf(stderr, "%s\n", ERR_error_string(ERR_get_error(), NULL));
253272
}
@@ -256,25 +275,43 @@ int encrypt_rsa_message(const char *plaintext, EVP_PKEY *public_key, unsigned ch
256275
}
257276

258277
int decrypt_rsa_message(const unsigned char *encryptedtext, const int encryptedtext_len, EVP_PKEY *private_key, unsigned char **plaintext) {
278+
#if OPENSSL_VERSION_MAJOR >= 3
279+
EVP_PKEY_CTX *ctx;
280+
#else
259281
RSA *rsa = NULL;
260-
unsigned char *rsa_buffer = NULL, pad = RSA_PKCS1_PADDING;
261-
int plaintext_len, rsa_buffer_len, keysize;
262-
282+
#endif
283+
unsigned char *rsa_buffer = NULL;
284+
size_t plaintext_len = 0;
285+
int rsa_buffer_len, keysize;
286+
287+
#if OPENSSL_VERSION_MAJOR >= 3
288+
int rc;
289+
ctx = EVP_PKEY_CTX_new_from_pkey(NULL, private_key, "");
290+
/* See evp_pkey_rsa(7) and provider-keymgmt(7) */
291+
rc = EVP_PKEY_get_int_param(private_key, OSSL_PKEY_PARAM_MAX_SIZE, &keysize); /* XXX not really keysize */
292+
#else
263293
rsa = EVP_PKEY_get1_RSA(private_key);
264-
265294
keysize = RSA_size(rsa);
295+
#endif
266296
rsa_buffer = OPENSSL_malloc(keysize * 2);
267297
*plaintext = (unsigned char*)OPENSSL_malloc(keysize);
268298

269299
BIO *bioBuff = BIO_new_mem_buf((void*)encryptedtext, encryptedtext_len);
270300
rsa_buffer_len = BIO_read(bioBuff, rsa_buffer, keysize * 2);
271-
plaintext_len = RSA_private_decrypt(rsa_buffer_len, rsa_buffer, *plaintext, rsa, pad);
272-
301+
#if OPENSSL_VERSION_MAJOR >= 3
302+
plaintext_len = keysize;
303+
EVP_PKEY_decrypt_init(ctx);
304+
EVP_PKEY_decrypt(ctx, *plaintext, &plaintext_len, rsa_buffer, rsa_buffer_len);
305+
EVP_PKEY_CTX_free(ctx);
306+
#else
307+
plaintext_len = RSA_private_decrypt(rsa_buffer_len, rsa_buffer, *plaintext, rsa, RSA_PKCS1_PADDING);
273308
RSA_free(rsa);
309+
#endif
310+
274311
OPENSSL_free(rsa_buffer);
275312
BIO_free(bioBuff);
276313

277-
if (plaintext_len < 0) {
314+
if (plaintext_len <= 0) {
278315
/* We probably shouldn't be printing stuff like this */
279316
fprintf(stderr, "%s\n", ERR_error_string(ERR_get_error(), NULL));
280317
}

0 commit comments

Comments
 (0)