Improve LibreSSL compatibility

This commit is contained in:
David Uhden Collado 2025-10-13 22:56:56 +02:00
parent 80840b761e
commit 29660b6745
No known key found for this signature in database
GPG key ID: 1A47E8A7D51FB3DA
4 changed files with 98 additions and 29 deletions

View file

@ -15,9 +15,11 @@
#include <openssl/dh.h>
#include <openssl/md5.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include "TunnelBase.h"
#include <openssl/ssl.h>
#include <openssl/kdf.h>
#include <openssl/objects.h>
#if I2PD_OPENSSL_GE_3 // since 3.0.0
#include <openssl/param_build.h>
#include <openssl/core_names.h>
@ -137,6 +139,62 @@ namespace crypto
return true;
}
EVP_PKEY * GenerateECKey (int curve)
{
#if I2PD_OPENSSL_GE_3
return EVP_EC_gen (OBJ_nid2ln (curve));
#else
EC_KEY * ec = EC_KEY_new_by_curve_name (curve);
if (!ec)
{
LogPrint (eLogError, "Crypto: Failed to allocate EC key for curve ", curve);
return nullptr;
}
if (!EC_KEY_generate_key (ec))
{
LogPrint (eLogError, "Crypto: Failed to generate EC key for curve ", curve);
EC_KEY_free (ec);
return nullptr;
}
EVP_PKEY * pkey = EVP_PKEY_new ();
if (!pkey)
{
LogPrint (eLogError, "Crypto: Failed to allocate EVP_PKEY for EC curve ", curve);
EC_KEY_free (ec);
return nullptr;
}
if (!EVP_PKEY_assign_EC_KEY (pkey, ec))
{
LogPrint (eLogError, "Crypto: EVP_PKEY_assign_EC_KEY failed for curve ", curve);
EVP_PKEY_free (pkey);
EC_KEY_free (ec);
return nullptr;
}
return pkey;
#endif
}
int GetEVPKeyCurveNID (const EVP_PKEY * pkey)
{
if (!pkey) return -1;
#if I2PD_OPENSSL_GE_3
char groupName[64];
if (EVP_PKEY_get_group_name (const_cast<EVP_PKEY *>(pkey), groupName, sizeof (groupName), NULL) == 1)
return OBJ_txt2nid (groupName);
return -1;
#else
if (EVP_PKEY_base_id (pkey) != EVP_PKEY_EC)
return 0;
EC_KEY * ec = EVP_PKEY_get1_EC_KEY (const_cast<EVP_PKEY *>(pkey));
if (!ec)
return -1;
const EC_GROUP * group = EC_KEY_get0_group (ec);
int nid = group ? EC_GROUP_get_curve_name (group) : -1;
EC_KEY_free (ec);
return nid;
#endif
}
// RSA
#define rsae GetCryptoConstants ().rsae
const BIGNUM * GetRSAE ()