1 # ===========================================================================
3 # ===========================================================================
7 # AX_LIB_OPENSSL([MINIMUM-VERSION])
11 # This macro provides tests of availability of OpenSSL of a
12 # particular version or newer. This macros checks for OpenSSL
13 # headers and libraries and defines compilation flags.
15 # Macro supports following options and their values:
17 # 1) Single-option usage:
19 # --with-openssl - yes, no or path to OpenSSL installation prefix
23 # AC_SUBST(OPENSSL_CFLAGS)
24 # AC_SUBST(OPENSSL_LDFLAGS)
25 # AC_SUBST(OPENSSL_VERSION) - only if version requirement is used
31 # Copyright (c) 2008-2009 CineCert, LLC
32 # All rights reserved.
34 # Redistribution and use in source and binary forms, with or without
35 # modification, are permitted provided that the following conditions
37 # 1. Redistributions of source code must retain the above copyright
38 # notice, this list of conditions and the following disclaimer.
39 # 2. Redistributions in binary form must reproduce the above copyright
40 # notice, this list of conditions and the following disclaimer in the
41 # documentation and/or other materials provided with the distribution.
42 # 3. The name of the author may not be used to endorse or promote products
43 # derived from this software without specific prior written permission.
45 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 AC_DEFUN([AX_LIB_OPENSSL],
59 AC_ARG_WITH([openssl],
60 AC_HELP_STRING([--with-openssl=@<:@ARG@:>@],
61 [use OpenSSL from given prefix (ARG=path); check standard prefixes (ARG=yes); disable (ARG=no)]
64 if test "$withval" = "yes"; then
65 if test -d /var/local/ssl/include ; then
66 openssl_prefix=/var/local/ssl
67 elif test -d /usr/local/ssl/include ; then
68 openssl_prefix=/usr/local/ssl
69 elif test -d /usr/lib/ssl/include ; then
70 openssl_prefix=/usr/lib/ssl
71 elif test -d /usr/include/openssl ; then
76 openssl_requested="yes"
77 elif test -d "$withval"; then
78 openssl_prefix="$withval"
79 openssl_requested="yes"
82 openssl_requested="no"
86 dnl Default behavior is implicit yes
87 if test -d /var/local/ssl/include ; then
88 openssl_prefix=/var/local/ssl
89 elif test -d /usr/local/ssl/include ; then
90 openssl_prefix=/usr/local/ssl
91 elif test -d /usr/lib/ssl/include ; then
92 openssl_prefix=/usr/lib/ssl
93 elif test -d /usr/include/openssl ; then
106 dnl Collect include/lib paths and flags
108 run_openssl_test="no"
110 if test -n "$openssl_prefix"; then
111 openssl_include_dir="$openssl_prefix/include"
112 openssl_ldflags="-L$openssl_prefix/lib"
113 run_openssl_test="yes"
114 elif test "$openssl_requested" = "yes"; then
115 if test -n "$openssl_include_dir" -a -n "$openssl_lib_flags"; then
116 run_openssl_test="yes"
119 run_openssl_test="no"
122 openssl_libs="-lssl -lcrypto"
125 dnl Check OpenSSL files
127 if test "$run_openssl_test" = "yes"; then
129 saved_CPPFLAGS="$CPPFLAGS"
130 CPPFLAGS="$CPPFLAGS -I$openssl_include_dir"
132 saved_LDFLAGS="$LDFLAGS"
133 LDFLAGS="$LDFLAGS $openssl_ldflags"
136 LIBS="$openssl_libs $LIBS"
139 dnl Check OpenSSL headers
141 AC_MSG_CHECKING([for OpenSSL headers in $openssl_include_dir])
147 @%:@include <openssl/opensslv.h>
148 @%:@include <openssl/ssl.h>
149 @%:@include <openssl/crypto.h>
154 OPENSSL_CPPFLAGS="-I$openssl_include_dir"
155 openssl_header_found="yes"
156 AC_MSG_RESULT([found])
159 openssl_header_found="no"
160 AC_MSG_RESULT([not found])
166 dnl Check OpenSSL libraries
168 if test "$openssl_header_found" = "yes"; then
170 AC_MSG_CHECKING([for OpenSSL libraries])
176 @%:@include <openssl/opensslv.h>
177 @%:@include <openssl/ssl.h>
178 @%:@include <openssl/crypto.h>
179 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
188 OPENSSL_LDFLAGS="$openssl_ldflags"
189 OPENSSL_LIBS="$openssl_libs"
190 openssl_lib_found="yes"
191 AC_MSG_RESULT([found])
194 openssl_lib_found="no"
195 AC_MSG_RESULT([not found])
201 CPPFLAGS="$saved_CPPFLAGS"
202 LDFLAGS="$saved_LDFLAGS"
206 AC_MSG_CHECKING([for OpenSSL])
208 if test "$run_openssl_test" = "yes"; then
209 if test "$openssl_header_found" = "yes" -a "$openssl_lib_found" = "yes"; then
211 AC_SUBST([OPENSSL_CPPFLAGS])
212 AC_SUBST([OPENSSL_LDFLAGS])
213 AC_SUBST([OPENSSL_LIBS])
220 AC_MSG_RESULT([$HAVE_OPENSSL])
223 dnl Check OpenSSL version
225 if test "$HAVE_OPENSSL" = "yes"; then
227 openssl_version_req=ifelse([$1], [], [], [$1])
229 if test -n "$openssl_version_req"; then
231 AC_MSG_CHECKING([if OpenSSL version is >= $openssl_version_req])
233 if test -f "$openssl_include_dir/openssl/opensslv.h"; then
235 OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $openssl_include_dir/openssl/opensslv.h \
236 | grep -v fips | grep -v PTEXT | cut -f 2 | tr -d \"`
237 AC_SUBST([OPENSSL_VERSION])
239 dnl Decompose required version string and calculate numerical representation
240 openssl_version_req_major=`expr $openssl_version_req : '\([[0-9]]*\)'`
241 openssl_version_req_minor=`expr $openssl_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
242 openssl_version_req_revision=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
243 openssl_version_req_patch=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.[[0-9]]*\([[a-z]]*\)'`
244 if test "x$openssl_version_req_revision" = "x"; then
245 openssl_version_req_revision="0"
247 if test "x$openssl_version_req_patch" = "x"; then
248 openssl_version_req_patch="\`"
251 openssl_version_req_number=`expr $openssl_version_req_major \* $((0x10000000)) \
252 \+ $openssl_version_req_minor \* $((0x100000)) \
253 \+ $openssl_version_req_revision \* $((0x1000)) \
254 \+ $((1 + $(printf "%d" \'$openssl_version_req_patch) - $(printf "%d" \'a))) \* $((0x10)) \
257 dnl Calculate numerical representation of detected version
258 openssl_version_number=`expr $(($(grep OPENSSL_VERSION_NUMBER $openssl_include_dir/openssl/opensslv.h | cut -f 2 | tr -d L)))`
260 openssl_version_check=`expr $openssl_version_number \>\= $openssl_version_req_number`
261 if test "$openssl_version_check" = "1"; then
265 AC_MSG_WARN([Found $OPENSSL_VERSION, which is older than required. Possible compilation failure.])
269 AC_MSG_WARN([Missing header openssl/opensslv.h. Unable to determine OpenSSL version.])
276 AC_MSG_RESULT([$HAVE_OPENSSL])
278 if test "$openssl_requested" = "yes"; then
279 AC_MSG_WARN([OpenSSL support requested but headers or library not found. Specify valid prefix of OpenSSL using --with-openssl=@<:@DIR@:>@])
282 if test "$HAVE_OPENSSL" = "yes"; then
283 CPPFLAGS="$CPPFLAGS $OPENSSL_CPPFLAGS -DHAVE_SSL=1"
284 LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS $OPENSSL_LIBS"