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 /var/local/include/openssl ; then
68 openssl_prefix=/var/local
69 elif test -d /usr/local/ssl/include ; then
70 openssl_prefix=/usr/local/ssl
71 elif test -d /usr/lib/ssl/include ; then
72 openssl_prefix=/usr/lib/ssl
73 elif test -d /usr/include/openssl ; then
78 openssl_requested="yes"
79 elif test -d "$withval"; then
80 openssl_prefix="$withval"
81 openssl_requested="yes"
84 openssl_requested="no"
88 dnl Default behavior is implicit yes
89 if test -d /var/local/ssl/include ; then
90 openssl_prefix=/var/local/ssl
91 elif test -d /var/local/include/openssl ; then
92 openssl_prefix=/var/local
93 elif test -d /usr/local/ssl/include ; then
94 openssl_prefix=/usr/local/ssl
95 elif test -d /usr/lib/ssl/include ; then
96 openssl_prefix=/usr/lib/ssl
97 elif test -d /usr/include/openssl ; then
110 dnl Collect include/lib paths and flags
112 run_openssl_test="no"
114 if test -n "$openssl_prefix"; then
115 openssl_include_dir="$openssl_prefix/include"
116 openssl_ldflags="-L$openssl_prefix/lib64 -L$openssl_prefix/lib"
117 run_openssl_test="yes"
118 elif test "$openssl_requested" = "yes"; then
119 if test -n "$openssl_include_dir" -a -n "$openssl_lib_flags"; then
120 run_openssl_test="yes"
123 run_openssl_test="no"
126 openssl_libs="-lssl -lcrypto"
129 dnl Check OpenSSL files
131 if test "$run_openssl_test" = "yes"; then
133 saved_CPPFLAGS="$CPPFLAGS"
134 CPPFLAGS="$CPPFLAGS -I$openssl_include_dir"
136 saved_LDFLAGS="$LDFLAGS"
137 LDFLAGS="$LDFLAGS $openssl_ldflags"
140 LIBS="$openssl_libs $LIBS"
143 dnl Check OpenSSL headers
145 AC_MSG_CHECKING([for OpenSSL headers in $openssl_include_dir])
151 @%:@include <openssl/opensslv.h>
152 @%:@include <openssl/ssl.h>
153 @%:@include <openssl/crypto.h>
158 OPENSSL_CPPFLAGS="-I$openssl_include_dir"
159 openssl_header_found="yes"
160 AC_MSG_RESULT([found])
163 openssl_header_found="no"
164 AC_MSG_RESULT([not found])
170 dnl Check OpenSSL libraries
172 if test "$openssl_header_found" = "yes"; then
174 AC_MSG_CHECKING([for OpenSSL libraries])
180 @%:@include <openssl/opensslv.h>
181 @%:@include <openssl/ssl.h>
182 @%:@include <openssl/crypto.h>
183 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
192 OPENSSL_LDFLAGS="$openssl_ldflags"
193 OPENSSL_LIBS="$openssl_libs"
194 openssl_lib_found="yes"
195 AC_MSG_RESULT([found])
198 openssl_lib_found="no"
199 AC_MSG_RESULT([not found])
205 CPPFLAGS="$saved_CPPFLAGS"
206 LDFLAGS="$saved_LDFLAGS"
210 AC_MSG_CHECKING([for OpenSSL])
212 if test "$run_openssl_test" = "yes"; then
213 if test "$openssl_header_found" = "yes" -a "$openssl_lib_found" = "yes"; then
215 AC_SUBST([OPENSSL_CPPFLAGS])
216 AC_SUBST([OPENSSL_LDFLAGS])
217 AC_SUBST([OPENSSL_LIBS])
224 AC_MSG_RESULT([$HAVE_OPENSSL])
227 dnl Check OpenSSL version
229 if test "$HAVE_OPENSSL" = "yes"; then
231 openssl_version_req=ifelse([$1], [], [], [$1])
233 if test -n "$openssl_version_req"; then
235 AC_MSG_CHECKING([if OpenSSL version is >= $openssl_version_req])
237 if test -f "$openssl_include_dir/openssl/opensslv.h"; then
239 OPENSSL_VERSION=`grep OPENSSL_VERSION_TEXT $openssl_include_dir/openssl/opensslv.h \
240 | grep -v fips | grep -v PTEXT | cut -f 2 | tr -d \"`
241 AC_SUBST([OPENSSL_VERSION])
243 dnl Decompose required version string and calculate numerical representation
244 openssl_version_req_major=`expr $openssl_version_req : '\([[0-9]]*\)'`
245 openssl_version_req_minor=`expr $openssl_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
246 openssl_version_req_revision=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
247 openssl_version_req_patch=`expr $openssl_version_req : '[[0-9]]*\.[[0-9]]*\.[[0-9]]*\([[a-z]]*\)'`
248 if test "x$openssl_version_req_revision" = "x"; then
249 openssl_version_req_revision="0"
251 if test "x$openssl_version_req_patch" = "x"; then
252 openssl_version_req_patch="\`"
255 openssl_version_req_number=`expr $openssl_version_req_major \* $((0x10000000)) \
256 \+ $openssl_version_req_minor \* $((0x100000)) \
257 \+ $openssl_version_req_revision \* $((0x1000)) \
258 \+ $((1 + $(printf "%d" \'$openssl_version_req_patch) - $(printf "%d" \'a))) \* $((0x10)) \
261 dnl Calculate numerical representation of detected version
262 openssl_version_number=`expr $(($(grep OPENSSL_VERSION_NUMBER $openssl_include_dir/openssl/opensslv.h | cut -f 2 | tr -d L)))`
264 openssl_version_check=`expr $openssl_version_number \>\= $openssl_version_req_number`
265 if test "$openssl_version_check" = "1"; then
269 AC_MSG_WARN([Found $OPENSSL_VERSION, which is older than required. Possible compilation failure.])
273 AC_MSG_WARN([Missing header openssl/opensslv.h. Unable to determine OpenSSL version.])
280 AC_MSG_RESULT([$HAVE_OPENSSL])
282 if test "$openssl_requested" = "yes"; then
283 AC_MSG_WARN([OpenSSL support requested but headers or library not found. Specify valid prefix of OpenSSL using --with-openssl=@<:@DIR@:>@])
286 if test "$HAVE_OPENSSL" = "yes"; then
287 CPPFLAGS="$CPPFLAGS $OPENSSL_CPPFLAGS -DHAVE_SSL=1"
288 LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS $OPENSSL_LIBS"