/* Skip the CVE-2025-15467 fix in case the version being livepatched already have a fix for it.  */

#ifndef _DISABLE_CVE_2025_15467

#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/** clang-extract: from include/openssl/types.h:42:1 */
typedef struct ossl_provider_st OSSL_PROVIDER;

/** clang-extract: from include/openssl/types.h:83:1 */
typedef struct asn1_type_st ASN1_TYPE;

/** clang-extract: from include/openssl/types.h:110:1 */
typedef struct evp_cipher_st EVP_CIPHER;

/** clang-extract: from include/openssl/types.h:111:1 */
typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX;

/** clang-extract: from include/openssl/types.h:195:1 */
typedef struct engine_st ENGINE;

/** clang-extract: from include/openssl/types.h:232:1 */
typedef struct ossl_param_st OSSL_PARAM;

/** clang-extract: from include/openssl/core.h:85:1 */
struct ossl_param_st;/* Full definition was removed.  */

/** clang-extract: from include/openssl/core_dispatch.h:399:1 */
typedef void *(OSSL_FUNC_cipher_newctx_fn)(void *);

/** clang-extract: from include/openssl/core_dispatch.h:400:1 */
typedef int (OSSL_FUNC_cipher_encrypt_init_fn)(void *, const unsigned char *, size_t, const unsigned char *, size_t, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:406:1 */
typedef int (OSSL_FUNC_cipher_decrypt_init_fn)(void *, const unsigned char *, size_t, const unsigned char *, size_t, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:412:1 */
typedef int (OSSL_FUNC_cipher_update_fn)(void *, unsigned char *, size_t *, size_t, const unsigned char *, size_t);

/** clang-extract: from include/openssl/core_dispatch.h:416:1 */
typedef int (OSSL_FUNC_cipher_final_fn)(void *, unsigned char *, size_t *, size_t);

/** clang-extract: from include/openssl/core_dispatch.h:419:1 */
typedef int (OSSL_FUNC_cipher_cipher_fn)(void *, unsigned char *, size_t *, size_t, const unsigned char *, size_t);

/** clang-extract: from include/openssl/core_dispatch.h:423:1 */
typedef int (OSSL_FUNC_cipher_pipeline_encrypt_init_fn)(void *, const unsigned char *, size_t, size_t, const unsigned char **, size_t, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:428:1 */
typedef int (OSSL_FUNC_cipher_pipeline_decrypt_init_fn)(void *, const unsigned char *, size_t, size_t, const unsigned char **, size_t, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:433:1 */
typedef int (OSSL_FUNC_cipher_pipeline_update_fn)(void *, size_t, unsigned char **, size_t *, const size_t *, const unsigned char **, const size_t *);

/** clang-extract: from include/openssl/core_dispatch.h:437:1 */
typedef int (OSSL_FUNC_cipher_pipeline_final_fn)(void *, size_t, unsigned char **, size_t *, const size_t *);

/** clang-extract: from include/openssl/core_dispatch.h:440:1 */
typedef void (OSSL_FUNC_cipher_freectx_fn)(void *);

/** clang-extract: from include/openssl/core_dispatch.h:441:1 */
typedef void *(OSSL_FUNC_cipher_dupctx_fn)(void *);

/** clang-extract: from include/openssl/core_dispatch.h:442:1 */
typedef int (OSSL_FUNC_cipher_get_params_fn)(OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:443:1 */
typedef int (OSSL_FUNC_cipher_get_ctx_params_fn)(void *, OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:445:1 */
typedef int (OSSL_FUNC_cipher_set_ctx_params_fn)(void *, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:447:1 */
typedef const OSSL_PARAM *(OSSL_FUNC_cipher_gettable_params_fn)(void *);

/** clang-extract: from include/openssl/core_dispatch.h:449:1 */
typedef const OSSL_PARAM *(OSSL_FUNC_cipher_settable_ctx_params_fn)(void *, void *);

/** clang-extract: from include/openssl/core_dispatch.h:451:1 */
typedef const OSSL_PARAM *(OSSL_FUNC_cipher_gettable_ctx_params_fn)(void *, void *);

/** clang-extract: from include/openssl/core_dispatch.h:453:1 */
typedef int (OSSL_FUNC_cipher_encrypt_skey_init_fn)(void *, void *, const unsigned char *, size_t, const OSSL_PARAM *);

/** clang-extract: from include/openssl/core_dispatch.h:458:1 */
typedef int (OSSL_FUNC_cipher_decrypt_skey_init_fn)(void *, void *, const unsigned char *, size_t, const OSSL_PARAM *);

#define EVP_MAX_IV_LENGTH               16
#define EVP_MAX_BLOCK_LENGTH            32
/** clang-extract: from include/internal/refcount.h:35:1 */
typedef struct {
    _Atomic int val;
} CRYPTO_REF_COUNT;

/** clang-extract: from include/crypto/evp.h:300:1 */
struct evp_cipher_st;/* Full definition was removed.  */

/** clang-extract: from include/crypto/evp.h:580:1 */
typedef struct {
    unsigned char iv[EVP_MAX_IV_LENGTH];
    unsigned int iv_len;
    unsigned int tag_len;
} evp_cipher_aead_asn1_params;

/** clang-extract: from crypto/evp/evp_local.h:36:1 */
struct evp_cipher_ctx_st;/* Full definition was removed.  */

/** clang-extract: from include/crypto/asn1.h:136:1 */
int (*klpe_ossl_asn1_type_get_octetstring_int)(const ASN1_TYPE *, long *, unsigned char *, int);

/** clang-extract: from crypto/evp/evp_lib.c:221:1 */
__attribute__((visibility("default")))
int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
                                    evp_cipher_aead_asn1_params *asn1_params)
{
    int i = 0;
    long tl;
    unsigned char iv[EVP_MAX_IV_LENGTH];

    if (type == NULL || asn1_params == NULL)
        return 0;

    i = (*klpe_ossl_asn1_type_get_octetstring_int)(type, &tl, iv, EVP_MAX_IV_LENGTH);
    if (i <= 0 || i > EVP_MAX_IV_LENGTH)
        return -1;

    memcpy(asn1_params->iv, iv, i);
    asn1_params->iv_len = i;

    return i;
}

#endif
