Skip to content

Commit e70e71a

Browse files
Peter Stugealamaison
Peter Stuge
authored andcommitted
configure.ac: Add --with-crypto= instead of many different --with-$backend
The new --with-crypto option replaces the previous backend-specific --with-{openssl,libgcrypt,mbedtls,wincng} options and fixes some issues. * libgcrypt or mbedtls would previously be used whenever found, even if configure was passed --without-libgcrypt or --without-mbedtls. * If --with-$backend was specified then configure would not fail even if that library could not be found, and would instead use whichever crypto library was found first. The new option defaults to `auto`, which makes configure check for all supported crypto libraries in turn, choosing the first one found, or exiting with an error if none can be found.
1 parent e83bbc4 commit e70e71a

8 files changed

+175
-126
lines changed

Makefile.OpenSSL.inc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CRYPTO_CSOURCES = openssl.c
22
CRYPTO_HHEADERS = openssl.h
3+
CRYPTO_LTLIBS = $(LTLIBSSL)

Makefile.WinCNG.inc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CRYPTO_CSOURCES = wincng.c
22
CRYPTO_HHEADERS = wincng.h
3+
CRYPTO_LTLIBS = $(LTLIBBCRYPT) $(LTLIBCRYPT32)

Makefile.libgcrypt.inc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CRYPTO_CSOURCES = libgcrypt.c
22
CRYPTO_HHEADERS = libgcrypt.h
3+
CRYPTO_LTLIBS = $(LTLIBGCRYPT)

Makefile.mbedTLS.inc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CRYPTO_CSOURCES = mbedtls.c
22
CRYPTO_HHEADERS = mbedtls.h
3+
CRYPTO_LTLIBS = $(LTLIBMBEDTLS)

acinclude.m4

+63-64
Original file line numberDiff line numberDiff line change
@@ -382,86 +382,85 @@ AC_DEFUN([CURL_CONFIGURE_REENTRANT], [
382382
#
383383
])
384384

385-
AC_DEFUN([LIBSSH2_CHECKFOR_MBEDTLS], [
385+
dnl LIBSSH2_LIB_HAVE_LINKFLAGS
386+
dnl --------------------------
387+
dnl Wrapper around AC_LIB_HAVE_LINKFLAGS to also check $prefix/lib, if set.
388+
dnl
389+
dnl autoconf only checks $prefix/lib64 if gcc -print-search-dirs output
390+
dnl includes a directory named lib64. So, to find libraries in $prefix/lib
391+
dnl we append -L$prefix/lib to LDFLAGS before checking.
392+
dnl
393+
dnl For conveniece, $4 is expanded if [lib]$1 is found.
386394

387-
old_LDFLAGS=$LDFLAGS
388-
old_CFLAGS=$CFLAGS
389-
if test -n "$use_mbedtls" && test "$use_mbedtls" != "no"; then
390-
LDFLAGS="$LDFLAGS -L$use_mbedtls/lib"
391-
CFLAGS="$CFLAGS -I$use_mbedtls/include"
392-
fi
395+
AC_DEFUN([LIBSSH2_LIB_HAVE_LINKFLAGS], [
396+
libssh2_lib_have_linkflags_LDFLAGS="$LDFLAGS"
393397
394-
AC_LIB_HAVE_LINKFLAGS([mbedtls], [], [
395-
#include <mbedtls/version.h>
396-
])
398+
test "${with_lib$1_prefix+set}" = set &&
399+
LDFLAGS="$LDFLAGS${LDFLAGS:+ }-L${with_lib$1_prefix}/lib"
397400
398-
if test "$ac_cv_libmbedtls" = "yes"; then
399-
AC_DEFINE(LIBSSH2_MBEDTLS, 1, [Use mbedtls])
400-
LIBSREQUIRED= # mbedtls doesn't provide a .pc file
401-
LIBS="$LIBS -lmbedtls -lmbedcrypto"
402-
found_crypto=libmbedtls
403-
support_clear_memory=yes
404-
else
405-
# restore
406-
LDFLAGS=$old_LDFLAGS
407-
CFLAGS=$old_CFLAGS
401+
AC_LIB_HAVE_LINKFLAGS([$1], [$2], [$3])
402+
403+
LDFLAGS="$libssh2_lib_have_linkflags_LDFLAGS"
404+
405+
if test "$ac_cv_lib$1" = "yes"; then :
406+
$4
408407
fi
409408
])
410409

411-
AC_DEFUN([LIBSSH2_CHECKFOR_GCRYPT], [
410+
AC_DEFUN([LIBSSH2_CHECK_CRYPTO], [
411+
if test "$use_crypto" = "auto" && test "$found_crypto" = "none" || test "$use_crypto" = "$1"; then
412+
m4_case([$1],
413+
[openssl], [
414+
LIBSSH2_LIB_HAVE_LINKFLAGS([ssl], [crypto], [#include <openssl/ssl.h>], [
415+
AC_DEFINE(LIBSSH2_OPENSSL, 1, [Use $1])
416+
LIBSREQUIRED="$LIBSREQUIRED${LIBSREQUIRED:+ }libssl libcrypto"
412417
413-
old_LDFLAGS=$LDFLAGS
414-
old_CFLAGS=$CFLAGS
415-
if test -n "$with_libgcrypt_prefix" && test "$use_libgcrypt" != "no"; then
416-
LDFLAGS="$LDFLAGS -L$with_libgcrypt_prefix/lib"
417-
CFLAGS="$CFLAGS -I$with_libgcrypt_prefix/include"
418-
fi
419-
AC_LIB_HAVE_LINKFLAGS([gcrypt], [], [
420-
#include <gcrypt.h>
421-
])
418+
# Not all OpenSSL have AES-CTR functions.
419+
AC_CHECK_FUNCS(EVP_aes_128_ctr)
422420
423-
if test "$ac_cv_libgcrypt" = "yes"; then
424-
AC_DEFINE(LIBSSH2_LIBGCRYPT, 1, [Use libgcrypt])
425-
LIBSREQUIRED= # libgcrypt doesn't provide a .pc file. sad face.
426-
LIBS="$LIBS -lgcrypt"
427-
found_crypto=libgcrypt
428-
else
429-
# restore
430-
LDFLAGS=$old_LDFLAGS
431-
CFLAGS=$old_CFLAGS
432-
fi
433-
])
421+
found_crypto="$1"
422+
found_crypto_str="OpenSSL (AES-CTR: ${ac_cv_func_EVP_aes_128_ctr:-N/A})"
423+
])
424+
],
434425
426+
[libgcrypt], [
427+
LIBSSH2_LIB_HAVE_LINKFLAGS([gcrypt], [], [#include <gcrypt.h>], [
428+
AC_DEFINE(LIBSSH2_LIBGCRYPT, 1, [Use $1])
429+
found_crypto="$1"
430+
])
431+
],
435432
436-
AC_DEFUN([LIBSSH2_CHECKFOR_WINCNG], [
433+
[mbedtls], [
434+
LIBSSH2_LIB_HAVE_LINKFLAGS([mbedtls], [], [#include <mbedtls/version.h>], [
435+
AC_DEFINE(LIBSSH2_MBEDTLS, 1, [Use $1])
436+
found_crypto="$1"
437+
support_clear_memory=yes
438+
])
439+
],
437440
441+
[wincng], [
438442
# Look for Windows Cryptography API: Next Generation
439443
440-
AC_LIB_HAVE_LINKFLAGS([bcrypt], [], [
441-
#include <windows.h>
442-
#include <bcrypt.h>
443-
])
444-
AC_LIB_HAVE_LINKFLAGS([crypt32], [], [
444+
AC_CHECK_HEADERS([ntdef.h ntstatus.h], [], [], [#include <windows.h>])
445+
AC_CHECK_DECLS([SecureZeroMemory], [], [], [#include <windows.h>])
446+
447+
LIBSSH2_LIB_HAVE_LINKFLAGS([crypt32], [], [
445448
#include <windows.h>
446449
#include <wincrypt.h>
447450
])
448-
AC_CHECK_HEADERS([ntdef.h ntstatus.h], [], [], [
449-
#include <windows.h>
450-
])
451-
AC_CHECK_DECLS([SecureZeroMemory], [], [], [
451+
LIBSSH2_LIB_HAVE_LINKFLAGS([bcrypt], [], [
452452
#include <windows.h>
453+
#include <bcrypt.h>
454+
], [
455+
AC_DEFINE(LIBSSH2_WINCNG, 1, [Use $1])
456+
found_crypto="$1"
457+
found_crypto_str="Windows Cryptography API: Next Generation"
458+
support_clear_memory="$ac_cv_have_decl_SecureZeroMemory"
453459
])
454-
455-
if test "$ac_cv_libbcrypt" = "yes"; then
456-
AC_DEFINE(LIBSSH2_WINCNG, 1, [Use Windows CNG])
457-
LIBSREQUIRED= # wincng doesn't provide a .pc file. sad face.
458-
LIBS="$LIBS -lbcrypt"
459-
if test "$ac_cv_libcrypt32" = "yes"; then
460-
LIBS="$LIBS -lcrypt32"
461-
fi
462-
found_crypto="Windows Cryptography API: Next Generation"
463-
if test "$ac_cv_have_decl_SecureZeroMemory" = "yes"; then
464-
support_clear_memory=yes
465-
fi
466-
fi
460+
],
461+
)
462+
test "$found_crypto" = "none" &&
463+
crypto_errors="${crypto_errors}No $1 crypto library found!
464+
"
465+
fi
467466
])

configure.ac

+79-60
Original file line numberDiff line numberDiff line change
@@ -83,78 +83,81 @@ AC_C_BIGENDIAN
8383
dnl check for how to do large files
8484
AC_SYS_LARGEFILE
8585

86-
found_crypto=none
87-
8886
# Configure parameters
89-
AC_ARG_WITH(openssl,
90-
AC_HELP_STRING([--with-openssl],[Use OpenSSL for crypto]),
91-
use_openssl=$withval,use_openssl=auto)
92-
AC_ARG_WITH(libgcrypt,
93-
AC_HELP_STRING([--with-libgcrypt],[Use libgcrypt for crypto]),
94-
[ use_libgcrypt=$withval
95-
LIBSSH2_CHECKFOR_GCRYPT
96-
], use_libgcrypt=auto)
97-
AC_ARG_WITH(wincng,
98-
AC_HELP_STRING([--with-wincng],[Use Windows CNG for crypto]),
99-
[ use_wincng=$withval
100-
LIBSSH2_CHECKFOR_WINCNG
101-
] ,use_wincng=auto)
102-
AC_ARG_WITH([mbedtls],
103-
AC_HELP_STRING([--with-mbedtls],[Use mbedTLS for crypto]),
104-
[ use_mbedtls=$withval
105-
LIBSSH2_CHECKFOR_MBEDTLS
106-
], use_mbedtls=auto
107-
)
108-
AC_ARG_WITH(libz,
109-
AC_HELP_STRING([--with-libz],[Use zlib for compression]),
110-
use_libz=$withval,use_libz=auto)
11187

88+
89+
# Crypto backends
90+
91+
found_crypto=none
92+
found_crypto_str=""
11293
support_clear_memory=no
94+
crypto_errors=""
95+
96+
m4_set_add([crypto_backends], [openssl])
97+
m4_set_add([crypto_backends], [libgcrypt])
98+
m4_set_add([crypto_backends], [mbedtls])
99+
m4_set_add([crypto_backends], [wincng])
100+
101+
AC_ARG_WITH([crypto],
102+
AC_HELP_STRING([--with-crypto=auto|]m4_set_contents([crypto_backends], [|]),
103+
[Select crypto backend (default: auto)]),
104+
use_crypto=$withval,
105+
use_crypto=auto
106+
)
113107

114-
# Look for OpenSSL
115-
if test "$found_crypto" = "none" && test "$use_openssl" != "no"; then
116-
AC_LIB_HAVE_LINKFLAGS([ssl], [crypto], [#include <openssl/ssl.h>])
117-
fi
118-
if test "$ac_cv_libssl" = "yes"; then
119-
AC_DEFINE(LIBSSH2_OPENSSL, 1, [Use OpenSSL])
120-
LIBSREQUIRED=libssl,libcrypto
108+
case "${use_crypto}" in
109+
auto|m4_set_contents([crypto_backends], [|]))
110+
m4_set_map([crypto_backends], [LIBSSH2_CHECK_CRYPTO])
111+
;;
112+
yes|"")
113+
crypto_errors="No crypto backend specified!"
114+
;;
115+
*)
116+
crypto_errors="Unknown crypto backend '${use_crypto}' specified!"
117+
;;
118+
esac
121119

122-
# Not all OpenSSL have AES-CTR functions.
123-
save_LIBS="$LIBS"
124-
LIBS="$LIBS $LIBSSL"
125-
AC_CHECK_FUNCS(EVP_aes_128_ctr)
126-
LIBS="$save_LIBS"
120+
if test "$found_crypto" = "none"; then
121+
crypto_errors="${crypto_errors}
122+
Specify --with-crypto=\$backend and/or the neccessary library search prefix.
127123

128-
found_crypto="OpenSSL (AES-CTR: ${ac_cv_func_EVP_aes_128_ctr:-N/A})"
124+
Known crypto backends: auto, m4_set_contents([crypto_backends], [, ])"
125+
AS_MESSAGE([ERROR: ${crypto_errors}])
126+
else
127+
test "$found_crypto_str" = "" && found_crypto_str="$found_crypto"
129128
fi
130129

131-
AM_CONDITIONAL(OPENSSL, test "$ac_cv_libssl" = "yes")
132-
AM_CONDITIONAL(WINCNG, test "$ac_cv_libbcrypt" = "yes")
133-
AM_CONDITIONAL(LIBGCRYPT, test "$ac_cv_libgcrypt" = "yes")
134-
AM_CONDITIONAL(MBEDTLS, test "$ac_cv_libmbedtls" = "yes")
130+
m4_set_foreach([crypto_backends], [backend],
131+
[AM_CONDITIONAL(m4_toupper(backend), test "$found_crypto" = "backend")]
132+
)
133+
m4_undefine([backend])
135134

136-
# Check if crypto library was found
137-
if test "$found_crypto" = "none"; then
138-
AC_MSG_ERROR([No crypto library found!
139-
Try --with-libssl-prefix=PATH
140-
or --with-libgcrypt-prefix=PATH
141-
or --with-libmbedtls-prefix=PATH
142-
or --with-wincng on Windows\
143-
])
144-
fi
145135

146-
# Look for Libz
147-
if test "$use_libz" != "no"; then
136+
# libz
137+
138+
AC_ARG_WITH([libz],
139+
AC_HELP_STRING([--with-libz],[Use libz for compression]),
140+
use_libz=$withval,
141+
use_libz=auto)
142+
143+
found_libz=no
144+
libz_errors=""
145+
146+
if test "$use_libz" != no; then
148147
AC_LIB_HAVE_LINKFLAGS([z], [], [#include <zlib.h>])
149148
if test "$ac_cv_libz" != yes; then
150-
AC_MSG_NOTICE([Cannot find zlib, disabling compression])
151-
AC_MSG_NOTICE([Try --with-libz-prefix=PATH if you know you have it])
149+
if test "$use_libz" = auto; then
150+
AC_MSG_NOTICE([Cannot find libz, disabling compression])
151+
found_libz="disabled; no libz found"
152+
else
153+
libz_errors="No libz found!
154+
Try --with-libz-prefix=PATH if you know that you have it."
155+
AS_MESSAGE([ERROR: $libz_errors])
156+
fi
152157
else
153158
AC_DEFINE(LIBSSH2_HAVE_ZLIB, 1, [Compile in zlib support])
154-
if test "${LIBSREQUIRED}" != ""; then
155-
LIBSREQUIRED="${LIBSREQUIRED},"
156-
fi
157-
LIBSREQUIRED="${LIBSREQUIRED}zlib"
159+
LIBSREQUIRED="$LIBSREQUIRED${LIBSREQUIRED:+ }zlib"
160+
found_libz="yes"
158161
fi
159162
fi
160163

@@ -351,6 +354,22 @@ AC_C_INLINE
351354

352355
CURL_CHECK_NONBLOCKING_SOCKET
353356

357+
missing_required_deps=0
358+
359+
if test "${libz_errors}" != ""; then
360+
AS_MESSAGE([ERROR: ${libz_errors}])
361+
missing_required_deps=1
362+
fi
363+
364+
if test "$found_crypto" = "none"; then
365+
AS_MESSAGE([ERROR: ${crypto_errors}])
366+
missing_required_deps=1
367+
fi
368+
369+
if test $missing_required_deps = 1; then
370+
AC_MSG_ERROR([Required dependencies are missing!])
371+
fi
372+
354373
AC_CONFIG_FILES([Makefile
355374
src/Makefile
356375
tests/Makefile
@@ -367,10 +386,10 @@ AC_MSG_NOTICE([summary of build options:
367386
Compiler: ${CC}
368387
Compiler flags: ${CFLAGS}
369388
Library types: Shared=${enable_shared}, Static=${enable_static}
370-
Crypto library: ${found_crypto}
389+
Crypto library: ${found_crypto_str}
371390
Clear memory: $enable_clear_memory
372391
Debug build: $enable_debug
373392
Build examples: $build_examples
374393
Path to sshd: $ac_cv_path_SSHD (only for self-tests)
375-
zlib compression: $ac_cv_libz
394+
zlib compression: ${found_libz}
376395
])

docs/HACKING.CRYPTO

+27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ Procedures listed as "void" may indeed have a result type: the void indication
1313
indicates the libssh2 core modules never use the function result.
1414

1515

16+
0) Build system.
17+
18+
Add a new crypto backend to the autotools build system (./configure) as such:
19+
20+
* Add one new line to configure.ac:
21+
22+
m4_set_add([crypto_backends], [newname])
23+
24+
This automatically creates a new --with-crypto=newname option which users can
25+
specify when invoking configure at compile-time to select the new backend.
26+
27+
* Add a new m4_case stanza to acinclude.m4 within LIBSSH2_CRYPTO_CHECK,
28+
with checks for library availability. A successful check should set
29+
library linking variables. The LIBSSH2_LIB_HAVE_LINKFLAGS macro creates
30+
such a variable automatically if the checked library can be found.
31+
32+
* Add a Makefile.newname.inc in the top-level directory which sets
33+
CRYPTO_CSOURCES and CRYPTO_HHEADERS to the new backend source files,
34+
and CRYPTO_LTLIBS to the libtool linking parameters for the library, set
35+
e.g. by a LIBSSH2_LIB_HAVE_LINKFLAGS call in LIBSSH2_CRYPTO_CHECK.
36+
37+
* Add a new block to src/Makefile.am:
38+
if NEWNAME
39+
include ../Makefile.newname.inc
40+
endif
41+
42+
1643
1) Crypto library initialization/termination.
1744

1845
void libssh2_crypto_init(void);

src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# $Id: Makefile.am,v 1.21 2009/05/07 17:21:56 bagder Exp $
22
AUTOMAKE_OPTIONS = foreign nostdinc
33

4-
# Get the CRYPTO_CSOURCES and CRYPTO_HHEADERS defines
4+
# Get the CRYPTO_CSOURCES, CRYPTO_HHEADERS and CRYPTO_LTLIBS defines
55
if OPENSSL
66
include ../Makefile.OpenSSL.inc
77
endif
@@ -62,4 +62,4 @@ VERSION=-version-info 1:1:0
6262

6363
libssh2_la_LDFLAGS = $(VERSION) -no-undefined \
6464
-export-symbols-regex '^libssh2_.*' \
65-
$(LTLIBGCRYPT) $(LTLIBSSL) $(LTLIBZ)
65+
$(CRYPTO_LTLIBS) $(LTLIBZ)

0 commit comments

Comments
 (0)