#ifndef _SC_OSSL_COMPAT_H
#define _SC_OSSL_COMPAT_H
#ifdef ENABLE_OPENSSL
#ifdef __cplusplus
extern "C" {
#endif
#include <openssl/opensslv.h>
#include <openssl/opensslconf.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
# if defined(OPENSSL_API_COMPAT) && OPENSSL_API_COMPAT >= 0x10100000L
#define ERR_load_crypto_strings(x) while (0) continue
#define SSL_load_error_strings(x) while (0) continue
#define ERR_free_strings(x) while (0) continue
#define ENGINE_load_dynamic(x) while (0) continue
#define EVP_CIPHER_CTX_cleanup(x) EVP_CIPHER_CTX_reset(x)
#define EVP_CIPHER_CTX_init(x) EVP_CIPHER_CTX_reset(x)
# endif
#endif
#if OPENSSL_VERSION_NUMBER < 0x10001000L
#define EVP_PKEY_base_id(x) (x->type)
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define RSA_PKCS1_OpenSSL RSA_PKCS1_SSLeay
#define X509_get_extension_flags(x) (x->ex_flags)
#define X509_get_key_usage(x) (x->ex_kusage)
#define X509_get_extended_key_usage(x) (x->ex_xkusage)
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x2050300fL
#define X509_up_ref(cert) CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509)
#endif
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x20700000L
#define OPENSSL_malloc_init CRYPTO_malloc_init
#define EVP_PKEY_get0_RSA(x) (x->pkey.rsa)
#define EVP_PKEY_get0_EC_KEY(x) (x->pkey.ec)
#define EVP_PKEY_get0_DSA(x) (x->pkey.dsa)
#define EVP_PKEY_up_ref(user_key) CRYPTO_add(&user_key->references, 1, CRYPTO_LOCK_EVP_PKEY)
#define ASN1_STRING_get0_data(x) ASN1_STRING_data(x)
#endif
#endif
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x20700000L
#define OPENSSL_malloc_init()
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#define EC_POINT_get_affine_coordinates_GFp EC_POINT_get_affine_coordinates
#define EC_POINT_set_affine_coordinates_GFp EC_POINT_set_affine_coordinates
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
# if !defined(inline) && !defined(__cplusplus)
# if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
# define sc_ossl_inline inline
# elif defined(__GNUC__) && __GNUC__>=2
# define sc_ossl_inline __inline__
# elif defined(_MSC_VER)
# define sc_ossl_inline __inline
# else
# define sc_ossl_inline
# endif
# else
# define sc_ossl_inline inline
# endif
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2050300fL)
#define RSA_bits(R) (BN_num_bits(R->n))
#include <openssl/bn.h>
#ifndef OPENSSL_NO_RSA
#include <openssl/rsa.h>
#endif
#ifndef OPENSSL_NO_DSA
#include <openssl/dsa.h>
#endif
#ifndef OPENSSL_NO_EC
#include <openssl/ecdsa.h>
#endif
#ifndef OPENSSL_NO_RSA
static sc_ossl_inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
{
if (n == NULL || e == NULL)
return 0;
BN_free(r->n);
BN_free(r->e);
BN_free(r->d);
r->n = n;
r->e = e;
r->d = d;
return 1;
}
static sc_ossl_inline int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
{
if (p == NULL || q == NULL)
return 0;
BN_free(r->p);
BN_free(r->q);
r->p = p;
r->q = q;
return 1;
}
static sc_ossl_inline int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
{
if (dmp1 == NULL || dmq1 == NULL || iqmp == NULL)
return 0;
BN_free(r->dmp1);
BN_free(r->dmq1);
BN_free(r->iqmp);
r->dmp1 = dmp1;
r->dmq1 = dmq1;
r->iqmp = iqmp;
return 1;
}
static sc_ossl_inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
{
if (n != NULL)
*n = r->n;
if (e != NULL)
*e = r->e;
if (d != NULL)
*d = r->d;
}
static sc_ossl_inline void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
{
if (p != NULL)
*p = r->p;
if (q != NULL)
*q = r->q;
}
static sc_ossl_inline void RSA_get0_crt_params(const RSA *r,
const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp)
{
if (dmp1 != NULL)
*dmp1 = r->dmp1;
if (dmq1 != NULL)
*dmq1 = r->dmq1;
if (iqmp != NULL)
*iqmp = r->iqmp;
}
#endif
#ifndef OPENSSL_NO_DSA
static sc_ossl_inline void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
{
if (p != NULL)
*p = d->p;
if (q != NULL)
*q = d->q;
if (g != NULL)
*g = d->g;
}
static sc_ossl_inline void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
{
if (pub_key != NULL)
*pub_key = d->pub_key;
if (priv_key != NULL)
*priv_key = d->priv_key;
}
#endif
#ifndef OPENSSL_NO_EC
static sc_ossl_inline int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
{
if (r == NULL || s == NULL)
return 0;
BN_clear_free(sig->r);
BN_clear_free(sig->s);
sig->r = r;
sig->s = s;
return 1;
}
#endif
static sc_ossl_inline int CRYPTO_secure_malloc_init(size_t size, int minsize)
{
return 0;
}
static sc_ossl_inline int CRYPTO_secure_malloc_initialized()
{
return 0;
}
static sc_ossl_inline void CRYPTO_secure_malloc_done()
{
}
#else
#include <openssl/crypto.h>
#endif
#ifdef __cplusplus
}
#endif
#endif
#endif