Skip to content

Commit 2f420ff

Browse files
committed
test/provider: Do not link against libica use dlopen instead
When an application links against libica (via -lica), then the libica library constructor runs before the program's main function. Libica's library constructor does initialize OpenSSL and thus parses the config file. However, the test programs set up some OpenSSL configuration related environment variables within function check_libica() called from the main function. If libica has already initialized OpenSSL prior to that, OpenSSL won't initialize again, and thus these environment variables have no effect. Dynamically load libica (via dlopen) only after setting the environment variables. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
1 parent 7186bff commit 2f420ff

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ AC_ARG_WITH([provider-libica-full],
116116
[])
117117
AM_CONDITIONAL([PROVIDER_FULL_LIBICA], [test "x$useproviderfulllibica" = xyes])
118118

119+
AC_SUBST(libicaversion, "$libicaversion")
120+
119121
# If compiled against OpenSSL 3.0 or later, build the provider unless
120122
# explicitely disabled.
121123
# If build against OpenSSL 1.1.1, we can not build the provider.

test/provider/Makefile.am

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,27 @@ TESTS = \
2424
check_PROGRAMS = rsakey eckey dhkey threadtest
2525

2626
dhkey_SOURCES = dhkey.c
27+
dhkey_LDADD = -lcrypto -ldl
2728
if PROVIDER_FULL_LIBICA
28-
dhkey_LDADD = -lcrypto -lica
29+
dhkey_CFLAGS = -DLIBICA_NAME=\"libica.so.@libicaversion@\"
2930
else
30-
dhkey_LDADD = -lcrypto -lica-cex
31+
dhkey_CFLAGS = -DLIBICA_NAME=\"libica-cex.so.@libicaversion@\"
3132
endif
3233

3334
eckey_SOURCES = eckey.c
35+
eckey_LDADD = -lcrypto -ldl
3436
if PROVIDER_FULL_LIBICA
35-
eckey_LDADD = -lcrypto -lica
37+
eckey_CFLAGS = -DLIBICA_NAME=\"libica.so.@libicaversion@\"
3638
else
37-
eckey_LDADD = -lcrypto -lica-cex
39+
eckey_CFLAGS = -DLIBICA_NAME=\"libica-cex.so.@libicaversion@\"
3840
endif
3941

4042
rsakey_SOURCES = rsakey.c
43+
rsakey_LDADD = -lcrypto -ldl
4144
if PROVIDER_FULL_LIBICA
42-
rsakey_LDADD = -lcrypto -lica
45+
rsakey_CFLAGS = -DLIBICA_NAME=\"libica.so.@libicaversion@\"
4346
else
44-
rsakey_LDADD = -lcrypto -lica-cex
47+
rsakey_CFLAGS = -DLIBICA_NAME=\"libica-cex.so.@libicaversion@\"
4548
endif
4649

4750
threadtest_SOURCES = threadtest.c

test/provider/dhkey.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <dlfcn.h>
2122

2223
#include <openssl/conf.h>
2324
#include <openssl/evp.h>
@@ -355,13 +356,32 @@ static const unsigned int required_ica_mechs[] = { RSA_ME };
355356
static const unsigned int required_ica_mechs_len =
356357
sizeof(required_ica_mechs) / sizeof(unsigned int);
357358

359+
typedef unsigned int (*ica_get_functionlist_t)(libica_func_list_element *,
360+
unsigned int *);
361+
358362
int check_libica()
359363
{
360364
unsigned int mech_len, i, k, found = 0;
361365
libica_func_list_element *mech_list = NULL;
366+
void *ibmca_dso;
367+
ica_get_functionlist_t p_ica_get_functionlist;
362368
int rc;
363369

364-
rc = ica_get_functionlist(NULL, &mech_len);
370+
ibmca_dso = dlopen(LIBICA_NAME, RTLD_NOW);
371+
if (ibmca_dso == NULL) {
372+
fprintf(stderr, "Failed to load libica '%s'!\n", LIBICA_NAME);
373+
return 77;
374+
}
375+
376+
p_ica_get_functionlist =
377+
(ica_get_functionlist_t)dlsym(ibmca_dso, "ica_get_functionlist");
378+
if (p_ica_get_functionlist == NULL) {
379+
fprintf(stderr, "Failed to get ica_get_functionlist from '%s'!\n",
380+
LIBICA_NAME);
381+
return 77;
382+
}
383+
384+
rc = p_ica_get_functionlist(NULL, &mech_len);
365385
if (rc != 0) {
366386
fprintf(stderr, "Failed to get function list from libica!\n");
367387
return 77;
@@ -373,7 +393,7 @@ int check_libica()
373393
return 77;
374394
}
375395

376-
rc = ica_get_functionlist(mech_list, &mech_len);
396+
rc = p_ica_get_functionlist(mech_list, &mech_len);
377397
if (rc != 0) {
378398
fprintf(stderr, "Failed to get function list from libica!\n");
379399
free(mech_list);

test/provider/eckey.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <dlfcn.h>
2122

2223
#include <openssl/conf.h>
2324
#include <openssl/evp.h>
@@ -788,13 +789,32 @@ static const unsigned int required_ica_mechs[] = { EC_DH, EC_DSA_SIGN,
788789
static const unsigned int required_ica_mechs_len =
789790
sizeof(required_ica_mechs) / sizeof(unsigned int);
790791

792+
typedef unsigned int (*ica_get_functionlist_t)(libica_func_list_element *,
793+
unsigned int *);
794+
791795
int check_libica()
792796
{
793797
unsigned int mech_len, i, k, found = 0;
794798
libica_func_list_element *mech_list = NULL;
799+
void *ibmca_dso;
800+
ica_get_functionlist_t p_ica_get_functionlist;
795801
int rc;
796802

797-
rc = ica_get_functionlist(NULL, &mech_len);
803+
ibmca_dso = dlopen(LIBICA_NAME, RTLD_NOW);
804+
if (ibmca_dso == NULL) {
805+
fprintf(stderr, "Failed to load libica '%s'!\n", LIBICA_NAME);
806+
return 77;
807+
}
808+
809+
p_ica_get_functionlist =
810+
(ica_get_functionlist_t)dlsym(ibmca_dso, "ica_get_functionlist");
811+
if (p_ica_get_functionlist == NULL) {
812+
fprintf(stderr, "Failed to get ica_get_functionlist from '%s'!\n",
813+
LIBICA_NAME);
814+
return 77;
815+
}
816+
817+
rc = p_ica_get_functionlist(NULL, &mech_len);
798818
if (rc != 0) {
799819
fprintf(stderr, "Failed to get function list from libica!\n");
800820
return 77;
@@ -806,7 +826,7 @@ int check_libica()
806826
return 77;
807827
}
808828

809-
rc = ica_get_functionlist(mech_list, &mech_len);
829+
rc = p_ica_get_functionlist(mech_list, &mech_len);
810830
if (rc != 0) {
811831
fprintf(stderr, "Failed to get function list from libica!\n");
812832
free(mech_list);

test/provider/rsakey.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <dlfcn.h>
2122

2223
#include <openssl/conf.h>
2324
#include <openssl/evp.h>
@@ -735,13 +736,32 @@ static const unsigned int required_ica_mechs[] = { RSA_ME, RSA_CRT };
735736
static const unsigned int required_ica_mechs_len =
736737
sizeof(required_ica_mechs) / sizeof(unsigned int);
737738

739+
typedef unsigned int (*ica_get_functionlist_t)(libica_func_list_element *,
740+
unsigned int *);
741+
738742
int check_libica()
739743
{
740744
unsigned int mech_len, i, k, found = 0;
741745
libica_func_list_element *mech_list = NULL;
746+
void *ibmca_dso;
747+
ica_get_functionlist_t p_ica_get_functionlist;
742748
int rc;
743749

744-
rc = ica_get_functionlist(NULL, &mech_len);
750+
ibmca_dso = dlopen(LIBICA_NAME, RTLD_NOW);
751+
if (ibmca_dso == NULL) {
752+
fprintf(stderr, "Failed to load libica '%s'!\n", LIBICA_NAME);
753+
return 77;
754+
}
755+
756+
p_ica_get_functionlist =
757+
(ica_get_functionlist_t)dlsym(ibmca_dso, "ica_get_functionlist");
758+
if (p_ica_get_functionlist == NULL) {
759+
fprintf(stderr, "Failed to get ica_get_functionlist from '%s'!\n",
760+
LIBICA_NAME);
761+
return 77;
762+
}
763+
764+
rc = p_ica_get_functionlist(NULL, &mech_len);
745765
if (rc != 0) {
746766
fprintf(stderr, "Failed to get function list from libica!\n");
747767
return 77;
@@ -753,7 +773,7 @@ int check_libica()
753773
return 77;
754774
}
755775

756-
rc = ica_get_functionlist(mech_list, &mech_len);
776+
rc = p_ica_get_functionlist(mech_list, &mech_len);
757777
if (rc != 0) {
758778
fprintf(stderr, "Failed to get function list from libica!\n");
759779
free(mech_list);

0 commit comments

Comments
 (0)