Skip to content

Commit

Permalink
this change moves stack of compression methods, now global variable
Browse files Browse the repository at this point in the history
in libssl into crypto context. This change is rquired by atexit PR.
  • Loading branch information
Sashan committed May 16, 2024
1 parent fa338aa commit d4a5fb3
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 104 deletions.
5 changes: 4 additions & 1 deletion build.info
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ DEPEND[]=include/openssl/asn1.h \
include/openssl/x509_acert.h \
include/openssl/x509_vfy.h \
include/crypto/bn_conf.h include/crypto/dso_conf.h \
include/internal/param_names.h crypto/params_idx.c
include/internal/param_names.h \
include/internal/stack_of_comp_methods.h crypto/params_idx.c

GENERATE[include/openssl/asn1.h]=include/openssl/asn1.h.in
GENERATE[include/openssl/asn1t.h]=include/openssl/asn1t.h.in
Expand Down Expand Up @@ -83,9 +84,11 @@ GENERATE[include/crypto/dso_conf.h]=include/crypto/dso_conf.h.in

DEPEND[crypto/params_idx.c \
include/internal/param_names.h \
include/internal/stack_of_comp_methods.h \
include/openssl/core_names.h]=util/perl|OpenSSL/paramnames.pm
GENERATE[crypto/params_idx.c]=crypto/params_idx.c.in
GENERATE[include/internal/param_names.h]=include/internal/param_names.h.in
GENERATE[include/internal/stack_of_comp_methods.h]=include/internal/stack_of_comp_methods.h.in
GENERATE[include/openssl/core_names.h]=include/openssl/core_names.h.in

IF[{- defined $target{shared_defflag} -}]
Expand Down
8 changes: 4 additions & 4 deletions crypto/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ $UTIL_COMMON=\

SOURCE[../libcrypto]=$UTIL_COMMON \
mem.c mem_sec.c \
cversion.c info.c cpt_err.c ebcdic.c uid.c o_time.c o_dir.c \
o_fopen.c getenv.c o_init.c init.c trace.c provider.c provider_child.c \
punycode.c passphrase.c sleep.c deterministic_nonce.c quic_vlint.c \
time.c
comp_methods.c cversion.c info.c cpt_err.c ebcdic.c uid.c o_time.c \
o_dir.c o_fopen.c getenv.c o_init.c init.c trace.c provider.c \
provider_child.c punycode.c passphrase.c sleep.c deterministic_nonce.c \
quic_vlint.c time.c
SOURCE[../providers/libfips.a]=$UTIL_COMMON

SOURCE[../libcrypto]=$UPLINKSRC
Expand Down
85 changes: 85 additions & 0 deletions crypto/comp_methods.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <openssl/crypto.h>
#include <openssl/comp.h>
#include <openssl/obj_mac.h>

#include "internal/cryptlib.h"

#define OSSL_COMP_NULL_IDX 0
#define OSSL_COMP_ZLIB_IDX 1
#define OSSL_COMP_NUM_IDX 2

static int sk_comp_cmp(const OSSL_COMP *const *a, const OSSL_COMP *const *b)
{
return ((*a)->id - (*b)->id);
}

STACK_OF(OSSL_COMP) *ossl_load_builtin_compressions(void)
{
STACK_OF(OSSL_COMP) *comp_methods = NULL;
#ifndef OPENSSL_NO_COMP
OSSL_COMP *comp = NULL;
COMP_METHOD *method = COMP_zlib();

comp_methods = sk_OSSL_COMP_new(sk_comp_cmp);

if (COMP_get_type(method) != NID_undef && comp_methods != NULL) {
comp = OPENSSL_malloc(sizeof(*comp));
if (comp != NULL) {
comp->method = method;
comp->id = OSSL_COMP_ZLIB_IDX;
comp->name = COMP_get_name(method);
if (!sk_OSSL_COMP_push(comp_methods, comp))
OPENSSL_free(comp);
sk_OSSL_COMP_sort(comp_methods);
}
}
#endif
return comp_methods;
}


OSSL_COMP *ossl_find_compression(OSSL_LIB_CTX *ctx, int cmeth_id)
{
OSSL_LIB_CTX *ossl_ctx;
STACK_OF(OSSL_COMP) *comp_methods;
OSSL_COMP *rv = NULL;

ossl_ctx = (ctx == NULL) ? OSSL_LIB_CTX_get0_global_default() : ctx;
comp_methods = ossl_lib_ctx_get_data(ossl_ctx, OSSL_LIB_CTX_COMP_METHODS);
if (comp_methods != NULL) {
OSSL_COMP srch_key;
int i;

srch_key.id = cmeth_id;
i = sk_OSSL_COMP_find(comp_methods, &srch_key);
if (i >= 0)
rv = sk_OSSL_COMP_value(comp_methods, i);
}

return (rv);
}

STACK_OF(OSSL_COMP) *ossl_comp_get_compression_methods(OSSL_LIB_CTX *ctx)
{
ctx = (ctx == NULL) ? OSSL_LIB_CTX_get0_global_default() : ctx;
return ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_COMP_METHODS);
}

static void cmeth_free(OSSL_COMP *cm)
{
OPENSSL_free(cm);
}

void ossl_free_compression_methods_int(STACK_OF(OSSL_COMP) *methods)
{
sk_OSSL_COMP_pop_free(methods, cmeth_free);
}
25 changes: 25 additions & 0 deletions crypto/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
#include <openssl/conf.h>
#include "internal/thread_once.h"
#include "internal/property.h"
#include "internal/cryptlib.h"
#include "internal/core.h"
#include "internal/bio.h"
#include "internal/provider.h"
#include "crypto/decoder.h"
#include "crypto/context.h"

#include "internal/cryptlib.h"

struct ossl_lib_ctx_st {
CRYPTO_RWLOCK *lock, *rand_crngt_lock;
OSSL_EX_DATA_GLOBAL global;
Expand Down Expand Up @@ -48,6 +51,7 @@ struct ossl_lib_ctx_st {
void *thread_event_handler;
void *fips_prov;
#endif
void *comp_methods;

int ischild;
int conf_diagnostics;
Expand Down Expand Up @@ -204,6 +208,8 @@ static int context_init(OSSL_LIB_CTX *ctx)
if (!ossl_property_parse_init(ctx))
goto err;

ctx->comp_methods = ossl_load_builtin_compressions();

return 1;

err:
Expand Down Expand Up @@ -344,6 +350,11 @@ static void context_deinit_objs(OSSL_LIB_CTX *ctx)
ctx->child_provider = NULL;
}
#endif

if (ctx->comp_methods != NULL) {
ossl_free_compression_methods_int(ctx->comp_methods);
ctx->comp_methods = NULL;
}
}

static int context_deinit(OSSL_LIB_CTX *ctx)
Expand Down Expand Up @@ -634,6 +645,9 @@ void *ossl_lib_ctx_get_data(OSSL_LIB_CTX *ctx, int index)
return ctx->fips_prov;
#endif

case OSSL_LIB_CTX_COMP_METHODS:
return ctx->comp_methods;

default:
return NULL;
}
Expand Down Expand Up @@ -683,3 +697,14 @@ void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *libctx, int value)
return;
libctx->conf_diagnostics = value;
}

void *ossl_lib_ctx_set0_compression_methods(OSSL_LIB_CTX *ctx,
void *meths)
{
STACK_OF(OSSL_COMP) *comp_methods;

ctx = (ctx == NULL) ? OSSL_LIB_CTX_get0_global_default() : ctx;
comp_methods = ctx->comp_methods;

return comp_methods;;
}
18 changes: 17 additions & 1 deletion include/internal/cryptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ typedef struct ossl_ex_data_global_st {
EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT];
} OSSL_EX_DATA_GLOBAL;

typedef struct ossl_comp_st {
int id;
const char *name;
COMP_METHOD *method;
} OSSL_COMP;

STACK_OF(OSSL_COMP);

# include "internal/stack_of_comp_methods.h"

/* OSSL_LIB_CTX */

Expand Down Expand Up @@ -117,7 +126,8 @@ typedef struct ossl_ex_data_global_st {
# define OSSL_LIB_CTX_CHILD_PROVIDER_INDEX 18
# define OSSL_LIB_CTX_THREAD_INDEX 19
# define OSSL_LIB_CTX_DECODER_CACHE_INDEX 20
# define OSSL_LIB_CTX_MAX_INDEXES 20
# define OSSL_LIB_CTX_COMP_METHODS 21
# define OSSL_LIB_CTX_MAX_INDEXES 21

OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx);
int ossl_lib_ctx_is_default(OSSL_LIB_CTX *ctx);
Expand Down Expand Up @@ -161,4 +171,10 @@ char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep);
unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
const char sep);

void *ossl_lib_ctx_set0_compression_methods(OSSL_LIB_CTX *ctx, void *meths);
OSSL_COMP *ossl_find_compression(OSSL_LIB_CTX *ctx, int cmeth_id);
STACK_OF(OSSL_COMP) *ossl_load_builtin_compressions(void);
STACK_OF(OSSL_COMP) *ossl_comp_get_compression_methods(OSSL_LIB_CTX *ctx);
void ossl_free_compression_methods_int(STACK_OF(OSSL_COMP) *methods);

#endif
21 changes: 21 additions & 0 deletions include/internal/stack_of_comp_methods.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

{-
use OpenSSL::stackhash qw(generate_stack_macros generate_const_stack_macros);
-}

#ifndef OPENSSL_STACK_OF_COMP_METHODS_H

{-
generate_stack_macros("OSSL_COMP");
-}

#endif

2 changes: 1 addition & 1 deletion include/openssl/ssl.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ typedef struct ssl_cipher_st SSL_CIPHER;
typedef struct ssl_session_st SSL_SESSION;
typedef struct tls_sigalgs_st TLS_SIGALGS;
typedef struct ssl_conf_ctx_st SSL_CONF_CTX;
typedef struct ssl_comp_st SSL_COMP;
typedef struct ossl_comp_st SSL_COMP;

STACK_OF(SSL_CIPHER);
STACK_OF(SSL_COMP);
Expand Down
2 changes: 2 additions & 0 deletions ssl/record/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <openssl/core_dispatch.h>
#include "internal/recordmethod.h"

#include <openssl/types.h>

/*****************************************************************************
* *
* These structures should be considered PRIVATE to the record layer. No *
Expand Down
Loading

0 comments on commit d4a5fb3

Please sign in to comment.