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 17, 2024
1 parent fa338aa commit 7a7c1ea
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 122 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
62 changes: 62 additions & 0 deletions crypto/comp_methods.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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;
}

STACK_OF(OSSL_COMP) *ossl_comp_get_compression_methods(OSSL_LIB_CTX *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);
}
39 changes: 39 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,10 @@ static int context_init(OSSL_LIB_CTX *ctx)
if (!ossl_property_parse_init(ctx))
goto err;

#ifndef FIPS_MODULE
ctx->comp_methods = ossl_load_builtin_compressions();
#endif

return 1;

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

#ifndef FIPS_MODULE
if (ctx->comp_methods != NULL) {
ossl_free_compression_methods_int(ctx->comp_methods);
ctx->comp_methods = NULL;
}
#endif

}

static int context_deinit(OSSL_LIB_CTX *ctx)
Expand Down Expand Up @@ -634,11 +650,19 @@ 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;
}
}

void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index)
{
return ossl_lib_ctx_get_data(ctx, index);
}

OSSL_EX_DATA_GLOBAL *ossl_lib_ctx_get_ex_data_global(OSSL_LIB_CTX *ctx)
{
ctx = ossl_lib_ctx_get_concrete(ctx);
Expand Down Expand Up @@ -683,3 +707,18 @@ 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 = ossl_lib_ctx_get_concrete(ctx);
if (ctx != NULL) {
comp_methods = ctx->comp_methods;
ctx->comp_methods = meths;
} else {
comp_methods = NULL;
}

return comp_methods;
}
24 changes: 23 additions & 1 deletion doc/man3/OSSL_LIB_CTX.pod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

OSSL_LIB_CTX, OSSL_LIB_CTX_new, OSSL_LIB_CTX_new_from_dispatch,
OSSL_LIB_CTX_new_child, OSSL_LIB_CTX_free, OSSL_LIB_CTX_load_config,
OSSL_LIB_CTX_get0_global_default, OSSL_LIB_CTX_set0_default
OSSL_LIB_CTX_get0_global_default, OSSL_LIB_CTX_set0_default,
OSSL_LIB_CTX_get_data, OSSL_LIB_CTX_set0_compression_methods

- OpenSSL library context

=head1 SYNOPSIS
Expand All @@ -22,6 +24,8 @@ OSSL_LIB_CTX_get0_global_default, OSSL_LIB_CTX_set0_default
void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx);
OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void);
OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *ctx);
void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index);
void *OSSL_LIB_CTX_set0_compression_methods(OSSL_LIB_CTX *ctx, void *methods);

=head1 DESCRIPTION

Expand Down Expand Up @@ -111,6 +115,15 @@ in the mean time. This means that the calling thread must not free the
library context that was the default at the start of the async job before
that job has finished.

OSSL_LIB_CTX_get_data() returns pointer to data/object member bound to context
instance (ctx). The index argument refers to context member which is to
be retrieved. The values for index are private to OpenSSL currently.
If ctx is NULL then function operates on default crypto library context.

OSSL_LIB_CTX_set0_compression_methods() function swaps compression methods
member in library context with new methods. Function returns original
methods to caller.

=head1 RETURN VALUES

OSSL_LIB_CTX_new(), OSSL_LIB_CTX_get0_global_default() and
Expand All @@ -121,10 +134,19 @@ OSSL_LIB_CTX_free() doesn't return any value.

OSSL_LIB_CTX_load_config() returns 1 on success, 0 on error.

OSSL_LIB_CTX_get_data() returns pointer to desired object associated with
library context instance.

OSSL_LIB_CTX_set0_compression_methods() returns pointer to compression
methods which got replaced.

=head1 HISTORY

All of the functions described on this page were added in OpenSSL 3.0.

OSSL_LIB_CTX_get_data() and OSSL_LIB_CTX_set0_compression_methods()
were introduced in OpenSSL 3.4.

=head1 COPYRIGHT

Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
Expand Down
16 changes: 15 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,8 @@ 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);

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

4 changes: 4 additions & 0 deletions include/openssl/crypto.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ void OSSL_LIB_CTX_set_conf_diagnostics(OSSL_LIB_CTX *ctx, int value);

void OSSL_sleep(uint64_t millis);


void *OSSL_LIB_CTX_set0_compression_methods(OSSL_LIB_CTX *ctx, void *meths);
void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index);

# ifdef __cplusplus
}
# endif
Expand Down
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
Loading

0 comments on commit 7a7c1ea

Please sign in to comment.