<commit_before>	/*
 * PKI related functions
 *
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 */

#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "config.h"
#ifdef	WITH_SECURITY
#include <openssl/pem.h>
#include <openssl/err.h>
#endif
#include <fcntl.h>
#include <libisns/isns.h>
#include "security.h"
#include <libisns/util.h>

#ifdef WITH_SECURITY

/* versions prior to 9.6.8 didn't seem to have these */
#if OPENSSL_VERSION_NUMBER < 0x00906080L
# define EVP_MD_CTX_init(c)	do { } while (0)
# define EVP_MD_CTX_cleanup(c)	do { } while (0)
#endif
#if OPENSSL_VERSION_NUMBER < 0x00906070L
# define i2d_DSA_PUBKEY		i2d_DSA_PUBKEY_backwards

static int	i2d_DSA_PUBKEY_backwards(DSA *, unsigned char **);
#endif
/* OpenSSL 1.1 made a lot of structures opaque, so we need to
 * define the 1.1 wrappers in previous versions. */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define EVP_PKEY_base_id(o)  ((o)->type)
#define EVP_PKEY_get0_DSA(o) ((o)->pkey.dsa)
static EVP_MD_CTX *EVP_MD_CTX_new(void)
{
    EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
    EVP_MD_CTX_init(ctx);
    return ctx;
}

static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
{
    EVP_MD_CTX_cleanup(ctx);
    OPENSSL_free(ctx);
}
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;
}
BN_GENCB *BN_GENCB_new(void)
{
    return OPENSSL_malloc(sizeof(BN_GENCB));
}
void BN_GENCB_free(BN_GENCB *cb)
{
    OPENSSL_free(cb);
}
#else
/* EVP_dss1 is now gone completely, so just use EVP_sha1 instead. */
#define EVP_dss1 EVP_sha1
#endif


#if OPENSSL_VERSION_NUMBER < 0x10100000L
static int	isns_openssl_init = 0;
#endif

static int	isns_dsasig_verify(isns_security_t *ctx,
				isns_principal_t *peer,
				buf_t *pdu,
				const struct isns_authblk *);
static int	isns_dsasig_sign(isns_security_t *ctx,
				isns_principal_t *peer,
				buf_t *pdu,
				struct isns_authblk *);
static EVP_PKEY *isns_dsasig_load_private_pem(isns_security_t *ctx,
				const char *filename);
static EVP_PKEY *isns_dsasig_load_public_pem(isns_security_t *ctx,
				const char *filename);
static DSA *	isns_dsa_load_params(const char *);


/*
 * Create a DSA security context
 */
isns_security_t *
isns_create_dsa_context(void)
{
	isns_security_t	*ctx;

#if OPENSSL_VERSION_NUMBER < 0x10100000L
	if (!isns_openssl_init) {
		ERR_load_crypto_strings();
		OpenSSL_add_all_algorithms();
		OpenSSL_add_all_ciphers();
		OpenSSL_add_all_digests();
		isns_openssl_init = 1;
	}
#endif

	ctx = isns_calloc(1, sizeof(*ctx));

	ctx->is_name = "DSA";
	ctx->is_type = ISNS_AUTH_TYPE_SHA1_DSA;
	ctx->is_replay_window = isns_config.ic_auth.replay_window;
	ctx->is_timestamp_jitter = isns_config.ic_auth.timestamp_jitter;

	ctx->is_verify = isns_dsasig_verify;
	ctx->is_sign = isns_dsasig_sign;
	ctx->is_load_private = isns_dsasig_load_private_pem;
	ctx->is_load_public = isns_dsasig_load_public_pem;

	isns_debug_auth("Created DSA authentication context\n");<commit_msg>openssl: handle newer version with ifdefs<commit_after>	/*
 * PKI related functions
 *
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 */

#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "config.h"
#ifdef	WITH_SECURITY
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#endif
#include <fcntl.h>
#include <libisns/isns.h>
#include "security.h"
#include <libisns/util.h>

#ifdef WITH_SECURITY

/* versions prior to 9.6.8 didn't seem to have these */
#if OPENSSL_VERSION_NUMBER < 0x00906080L
# define EVP_MD_CTX_init(c)	do { } while (0)
# define EVP_MD_CTX_cleanup(c)	do { } while (0)
#endif
#if OPENSSL_VERSION_NUMBER < 0x00906070L
# define i2d_DSA_PUBKEY		i2d_DSA_PUBKEY_backwards

static int	i2d_DSA_PUBKEY_backwards(DSA *, unsigned char **);
#endif
/* OpenSSL 1.1 made a lot of structures opaque, so we need to
 * define the 1.1 wrappers in previous versions. */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define EVP_PKEY_base_id(o)  ((o)->type)
#define EVP_PKEY_get0_DSA(o) ((o)->pkey.dsa)
static EVP_MD_CTX *EVP_MD_CTX_new(void)
{
    EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
    EVP_MD_CTX_init(ctx);
    return ctx;
}

static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
{
    EVP_MD_CTX_cleanup(ctx);
    OPENSSL_free(ctx);
}
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;
}
BN_GENCB *BN_GENCB_new(void)
{
    return OPENSSL_malloc(sizeof(BN_GENCB));
}
void BN_GENCB_free(BN_GENCB *cb)
{
    OPENSSL_free(cb);
}
#else
/* EVP_dss1 is now gone completely, so just use EVP_sha1 instead. */
#define EVP_dss1 EVP_sha1
#endif


static int	isns_openssl_init = 0;

static int	isns_dsasig_verify(isns_security_t *ctx,
				isns_principal_t *peer,
				buf_t *pdu,
				const struct isns_authblk *);
static int	isns_dsasig_sign(isns_security_t *ctx,
				isns_principal_t *peer,
				buf_t *pdu,
				struct isns_authblk *);
static EVP_PKEY *isns_dsasig_load_private_pem(isns_security_t *ctx,
				const char *filename);
static EVP_PKEY *isns_dsasig_load_public_pem(isns_security_t *ctx,
				const char *filename);
static DSA *	isns_dsa_load_params(const char *);


/*
 * Create a DSA security context
 */
isns_security_t *
isns_create_dsa_context(void)
{
	isns_security_t	*ctx;

	if (!isns_openssl_init) {
		ERR_load_crypto_strings();
#if OPENSSL_API_COMPAT < 0x10100000L
		OpenSSL_add_all_algorithms();
		OpenSSL_add_all_ciphers();
		OpenSSL_add_all_digests();
#else
		OPENSSL_init_crypto();
#endif
		isns_openssl_init = 1;
	}

	ctx = isns_calloc(1, sizeof(*ctx));

	ctx->is_name = "DSA";
	ctx->is_type = ISNS_AUTH_TYPE_SHA1_DSA;
	ctx->is_replay_window = isns_config.ic_auth.replay_window;
	ctx->is_timestamp_jitter = isns_config.ic_auth.timestamp_jitter;

	ctx->is_verify = isns_dsasig_verify;
	ctx->is_sign = isns_dsasig_sign;
	ctx->is_load_private = isns_dsasig_load_private_pem;
	ctx->is_load_public = isns_dsasig_load_public_pem;

	isns_debug_auth("Created DSA authentication context\n");
{'fraction_non_alphanumeric': 0.11299161831076725, 'fraction_numerical': 0.021437782076079947, 'mean_word_length': 5.041869522882181, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504485', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2593, 'n_words': 500}