Skip to content

Commit 2eecc1d

Browse files
committed
Fix compiler warnings from mingw. Add portability macro WOLFPKCS11_NO_ENV when setenv/getenv is not available. Only require -ldl for non-static builds.
1 parent 8cabca7 commit 2eecc1d

8 files changed

Lines changed: 51 additions & 37 deletions

File tree

configure.ac

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ fi
322322

323323
if test "$enable_shared" = "no"; then
324324
AM_CFLAGS="$AM_CFLAGS -DHAVE_PKCS11_STATIC"
325+
else
326+
LIBS="$LIBS -ldl"
325327
fi
326328

327329

@@ -347,7 +349,7 @@ AX_HARDEN_CC_COMPILER_FLAGS
347349

348350
OPTION_FLAGS="$CFLAGS $CPPFLAGS $AM_CFLAGS"
349351

350-
LIBS="$LIBS -lwolfssl -ldl -lm"
352+
LIBS="$LIBS -lwolfssl -lm"
351353

352354
CREATE_HEX_VERSION
353355
AC_SUBST([AM_CPPFLAGS])

include.am

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/internal.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
#endif
5454
#endif
5555

56+
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
57+
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
58+
#define WOLFPKCS11_NEED_RSA_RNG
59+
#endif
60+
5661
/* Size of hash calculated from PIN. */
5762
#define PIN_HASH_SZ 32
5863
/* Size of seed used when calculating hash from PIN. */
@@ -6503,26 +6508,23 @@ int WP11_RsaPkcs15_PrivateDecrypt(unsigned char* in, word32 inLen,
65036508
WP11_Object* priv, WP11_Slot* slot)
65046509
{
65056510
int ret = 0;
6506-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6507-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6511+
#ifdef WOLFPKCS11_NEED_RSA_RNG
65086512
WC_RNG rng;
65096513
#endif
65106514
/* A random number generator is needed for blinding. */
65116515
if (priv->onToken)
65126516
WP11_Lock_LockRW(priv->lock);
6513-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6514-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6517+
#ifdef WOLFPKCS11_NEED_RSA_RNG
65156518
ret = Rng_New(&slot->token.rng, &slot->token.rngLock, &rng);
6516-
if (ret == 0) {
6517-
priv->data.rsaKey.rng = &rng;
6518-
}
65196519
#endif
65206520
if (ret == 0) {
6521+
#ifdef WOLFPKCS11_NEED_RSA_RNG
6522+
priv->data.rsaKey.rng = &rng;
6523+
#endif
65216524
ret = wc_RsaPrivateDecrypt_ex(in, inLen, out, *outLen,
65226525
&priv->data.rsaKey, WC_RSA_PKCSV15_PAD,
65236526
WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
6524-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6525-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6527+
#ifdef WOLFPKCS11_NEED_RSA_RNG
65266528
priv->data.rsaKey.rng = NULL;
65276529
Rng_Free(&rng);
65286530
#endif
@@ -6607,28 +6609,25 @@ int WP11_RsaOaep_PrivateDecrypt(unsigned char* in, word32 inLen,
66076609
int ret = 0;
66086610
WP11_OaepParams* oaep = &session->params.oaep;
66096611
WP11_Slot* slot = WP11_Session_GetSlot(session);
6610-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6611-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6612+
#ifdef WOLFPKCS11_NEED_RSA_RNG
66126613
WC_RNG rng;
66136614
#endif
66146615

66156616
/* A random number generator is needed for blinding. */
66166617
if (priv->onToken)
66176618
WP11_Lock_LockRW(priv->lock);
6618-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6619-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6619+
#ifdef WOLFPKCS11_NEED_RSA_RNG
66206620
ret = Rng_New(&slot->token.rng, &slot->token.rngLock, &rng);
6621-
if (ret == 0) {
6622-
priv->data.rsaKey.rng = &rng;
6623-
}
66246621
#endif
66256622
if (ret == 0) {
6623+
#ifdef WOLFPKCS11_NEED_RSA_RNG
6624+
priv->data.rsaKey.rng = &rng;
6625+
#endif
66266626
ret = wc_RsaPrivateDecrypt_ex(in, inLen, out, *outLen,
66276627
&priv->data.rsaKey, WC_RSA_OAEP_PAD,
66286628
oaep->hashType, oaep->mgf,
66296629
oaep->label, oaep->labelSz);
6630-
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
6631-
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
6630+
#ifdef WOLFPKCS11_NEED_RSA_RNG
66326631
priv->data.rsaKey.rng = NULL;
66336632
Rng_Free(&rng);
66346633
#endif

tests/include.am

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
check_PROGRAMS += tests/pkcs11test
55
noinst_PROGRAMS += tests/pkcs11test
66
tests_pkcs11test_SOURCES = tests/pkcs11test.c
7-
tests_pkcs11test_LDADD = -lwolfssl -ldl -lm
7+
tests_pkcs11test_LDADD =
88

99
check_PROGRAMS += tests/pkcs11mtt
1010
noinst_PROGRAMS += tests/pkcs11mtt
1111
tests_pkcs11mtt_SOURCES = tests/pkcs11mtt.c
12-
tests_pkcs11mtt_LDADD = -lwolfssl -ldl -lm
12+
tests_pkcs11mtt_LDADD =
1313

1414
check_PROGRAMS += tests/pkcs11str
1515
noinst_PROGRAMS += tests/pkcs11str
1616
tests_pkcs11str_SOURCES = tests/pkcs11str.c
17-
tests_pkcs11str_LDADD = -lwolfssl -ldl -lm
17+
tests_pkcs11str_LDADD =
1818

1919
if BUILD_STATIC
2020
tests_pkcs11test_LDADD +=src/libwolfpkcs11.la

tests/pkcs11mtt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6494,7 +6494,9 @@ int main(int argc, char* argv[])
64946494
int closeDl = 1;
64956495
int i;
64966496

6497+
#ifndef WOLFPKCS11_NO_ENV
64976498
setenv("WOLFPKCS11_NO_STORE", "1", 1);
6499+
#endif
64986500

64996501
argc--;
65006502
argv++;

tests/pkcs11str.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,11 @@ int main(int argc, char* argv[])
918918
int setPin = 1;
919919
int closeDl = 1;
920920

921+
#ifndef WOLFPKCS11_NO_ENV
921922
if (!getenv("WOLFPKCS11_TOKEN_PATH")) {
922923
setenv("WOLFPKCS11_TOKEN_PATH", "./tests", 1);
923924
}
925+
#endif
924926

925927
argc--;
926928
argv++;

tests/pkcs11test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7908,7 +7908,9 @@ int main(int argc, char* argv[])
79087908
int closeDl = 1;
79097909
int i;
79107910

7911+
#ifndef WOLFPKCS11_NO_ENV
79117912
setenv("WOLFPKCS11_NO_STORE", "1", 1);
7913+
#endif
79127914

79137915
argc--;
79147916
argv++;

tests/testdata.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,25 @@ static unsigned char aes_128_gcm_exp_tag[] = {
440440
#endif
441441
#endif
442442

443+
444+
#ifndef WOLFPKCS11_NO_ENV
445+
#include <stdio.h>
446+
#include <stdlib.h> /* setenv/getenv */
447+
#include <string.h>
448+
449+
#if defined(__MINGW32__) || defined(_MSC_VER)
450+
/* Windows/MinGw does not support setenv, but does have putenv and getenv */
451+
extern int putenv(const char *);
452+
static inline int setenv(const char *name, const char *value, int overwrite)
453+
{
454+
char env[255];
455+
size_t len = strlen(name) + 1 + strlen(value) + 1;
456+
if (len < sizeof(env)) {
457+
sprintf(env, "%s=%s", name, value);
458+
return putenv(env);
459+
}
460+
(void)overwrite;
461+
return EXIT_FAILURE;
462+
}
463+
#endif
464+
#endif /* !WOLFPKCS11_NO_ENV */

0 commit comments

Comments
 (0)