1
1
/*
2
- * iperf, Copyright (c) 2014-2020 , The Regents of the University of
2
+ * iperf, Copyright (c) 2014-2023 , The Regents of the University of
3
3
* California, through Lawrence Berkeley National Laboratory (subject
4
4
* to receipt of any required approvals from the U.S. Dept. of
5
5
* Energy). All rights reserved.
46
46
#include <openssl/sha.h>
47
47
#include <openssl/buffer.h>
48
48
#include <openssl/err.h>
49
+ #if OPENSSL_VERSION_MAJOR >= 3
50
+ #include <openssl/evp.h>
51
+ #include <openssl/core_names.h>
52
+ #endif
49
53
50
54
const char * auth_text_format = "user: %s\npwd: %s\nts: %" PRId64 ;
51
55
52
56
void sha256 (const char * string , char outputBuffer [65 ])
53
57
{
54
58
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 );
59
61
int i = 0 ;
60
62
for (i = 0 ; i < SHA256_DIGEST_LENGTH ; i ++ )
61
63
{
@@ -229,25 +231,42 @@ int test_load_private_key_from_file(const char *file){
229
231
}
230
232
231
233
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
232
237
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
236
249
rsa = EVP_PKEY_get1_RSA (public_key );
237
250
keysize = RSA_size (rsa );
238
-
251
+ #endif
239
252
rsa_buffer = OPENSSL_malloc (keysize * 2 );
240
253
* encryptedtext = (unsigned char * )OPENSSL_malloc (keysize );
241
254
242
255
BIO * bioBuff = BIO_new_mem_buf ((void * )plaintext , (int )strlen (plaintext ));
243
256
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 );
246
263
RSA_free (rsa );
264
+ #endif
265
+
247
266
OPENSSL_free (rsa_buffer );
248
267
BIO_free (bioBuff );
249
268
250
- if (encryptedtext_len < 0 ) {
269
+ if (encryptedtext_len <= 0 ) {
251
270
/* We probably shouldn't be printing stuff like this */
252
271
fprintf (stderr , "%s\n" , ERR_error_string (ERR_get_error (), NULL ));
253
272
}
@@ -256,25 +275,42 @@ int encrypt_rsa_message(const char *plaintext, EVP_PKEY *public_key, unsigned ch
256
275
}
257
276
258
277
int decrypt_rsa_message (const unsigned char * encryptedtext , const int encryptedtext_len , EVP_PKEY * private_key , unsigned char * * plaintext ) {
278
+ #if 0 /* #if OPENSSL_VERSION_MAJOR >= 3 */
279
+ EVP_PKEY_CTX * ctx ;
280
+ #else
259
281
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 0 /* #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
263
293
rsa = EVP_PKEY_get1_RSA (private_key );
264
-
265
294
keysize = RSA_size (rsa );
295
+ #endif
266
296
rsa_buffer = OPENSSL_malloc (keysize * 2 );
267
297
* plaintext = (unsigned char * )OPENSSL_malloc (keysize );
268
298
269
299
BIO * bioBuff = BIO_new_mem_buf ((void * )encryptedtext , encryptedtext_len );
270
300
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 0 /* #if OPENSSL_VERSION_MAJOR >= 3 */
302
+ EVP_PKEY_decrypt_init (ctx );
303
+ EVP_PKEY_decrypt (ctx , * plaintext , & plaintext_len , rsa_buffer , rsa_buffer_len );
304
+ EVP_PKEY_CTX_free (ctx );
305
+ #else
306
+ plaintext_len = RSA_private_decrypt (rsa_buffer_len , rsa_buffer , * plaintext , rsa , RSA_PKCS1_PADDING );
273
307
RSA_free (rsa );
308
+ #endif
309
+
274
310
OPENSSL_free (rsa_buffer );
275
311
BIO_free (bioBuff );
276
312
277
- if (plaintext_len < 0 ) {
313
+ if (plaintext_len <= 0 ) {
278
314
/* We probably shouldn't be printing stuff like this */
279
315
fprintf (stderr , "%s\n" , ERR_error_string (ERR_get_error (), NULL ));
280
316
}
0 commit comments