Update copyright notice.
[asdcplib.git] / m4 / ax_lib_openssl.m4
1 # ===========================================================================
2 #             ax_lib_openssl.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_LIB_OPENSSL([MINIMUM-VERSION])
8 #
9 # DESCRIPTION
10 #
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.
14 #
15 #   Macro supports following options and their values:
16 #
17 #   1) Single-option usage:
18 #
19 #     --with-openssl - yes, no or path to OpenSSL installation prefix
20 #
21 #   This macro calls:
22 #
23 #     AC_SUBST(OPENSSL_CFLAGS)
24 #     AC_SUBST(OPENSSL_LDFLAGS)
25 #     AC_SUBST(OPENSSL_VERSION) - only if version requirement is used
26 #
27 #   And sets:
28 #
29 #     HAVE_OPENSSL
30 #
31 # Copyright (c) 2008-2009 CineCert, LLC
32 # All rights reserved.
33 #
34 # Redistribution and use in source and binary forms, with or without
35 # modification, are permitted provided that the following conditions
36 # are met:
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.
44 #
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.
55 #
56
57 AC_DEFUN([AX_LIB_OPENSSL],
58 [
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)]
62         ),
63         [
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
72                 openssl_prefix=/usr
73             else
74                 openssl_prefix=""
75             fi
76             openssl_requested="yes"
77         elif test -d "$withval"; then
78             openssl_prefix="$withval"
79             openssl_requested="yes"
80         else
81             openssl_prefix=""
82             openssl_requested="no"
83         fi
84         ],
85         [
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
94             openssl_prefix=/usr
95         else
96             openssl_prefix=""
97         fi
98         ]
99     )
100
101     OPENSSL_CPPFLAGS=""
102     OPENSSL_LDFLAGS=""
103     OPENSSL_VERSION=""
104
105     dnl
106     dnl Collect include/lib paths and flags
107     dnl
108     run_openssl_test="no"
109
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"
117         fi
118     else
119         run_openssl_test="no"
120     fi
121
122     openssl_libs="-lssl -lcrypto"
123
124     dnl
125     dnl Check OpenSSL files
126     dnl
127     if test "$run_openssl_test" = "yes"; then
128
129         saved_CPPFLAGS="$CPPFLAGS"
130         CPPFLAGS="$CPPFLAGS -I$openssl_include_dir"
131
132         saved_LDFLAGS="$LDFLAGS"
133         LDFLAGS="$LDFLAGS $openssl_ldflags"
134
135         saved_LIBS="$LIBS"
136         LIBS="$openssl_libs $LIBS"
137
138         dnl
139         dnl Check OpenSSL headers
140         dnl
141         AC_MSG_CHECKING([for OpenSSL headers in $openssl_include_dir])
142
143         AC_LANG_PUSH([C])
144         AC_COMPILE_IFELSE([
145             AC_LANG_PROGRAM(
146                 [[
147 @%:@include <openssl/opensslv.h>
148 @%:@include <openssl/ssl.h>
149 @%:@include <openssl/crypto.h>
150                 ]],
151                 [[]]
152             )],
153             [
154             OPENSSL_CPPFLAGS="-I$openssl_include_dir"
155             openssl_header_found="yes"
156             AC_MSG_RESULT([found])
157             ],
158             [
159             openssl_header_found="no"
160             AC_MSG_RESULT([not found])
161             ]
162         )
163         AC_LANG_POP([C])
164
165         dnl
166         dnl Check OpenSSL libraries
167         dnl
168         if test "$openssl_header_found" = "yes"; then
169
170             AC_MSG_CHECKING([for OpenSSL libraries])
171
172             AC_LANG_PUSH([C])
173             AC_LINK_IFELSE([
174                 AC_LANG_PROGRAM(
175                     [[
176 @%:@include <openssl/opensslv.h>
177 @%:@include <openssl/ssl.h>
178 @%:@include <openssl/crypto.h>
179 #if (OPENSSL_VERSION_NUMBER < 0x0090700f)
180 #endif
181                     ]],
182                     [[
183 SSL_library_init();
184 SSLeay();
185                     ]]
186                 )],
187                 [
188                 OPENSSL_LDFLAGS="$openssl_ldflags"
189                 OPENSSL_LIBS="$openssl_libs"
190                 openssl_lib_found="yes"
191                 AC_MSG_RESULT([found])
192                 ],
193                 [
194                 openssl_lib_found="no"
195                 AC_MSG_RESULT([not found])
196                 ]
197             )
198             AC_LANG_POP([C])
199         fi
200
201         CPPFLAGS="$saved_CPPFLAGS"
202         LDFLAGS="$saved_LDFLAGS"
203         LIBS="$saved_LIBS"
204     fi
205
206     AC_MSG_CHECKING([for OpenSSL])
207
208     if test "$run_openssl_test" = "yes"; then
209         if test "$openssl_header_found" = "yes" -a "$openssl_lib_found" = "yes"; then
210
211             AC_SUBST([OPENSSL_CPPFLAGS])
212             AC_SUBST([OPENSSL_LDFLAGS])
213             AC_SUBST([OPENSSL_LIBS])
214
215             HAVE_OPENSSL="yes"
216         else
217             HAVE_OPENSSL="no"
218         fi
219
220         AC_MSG_RESULT([$HAVE_OPENSSL])
221
222         dnl
223         dnl Check OpenSSL version
224         dnl
225         if test "$HAVE_OPENSSL" = "yes"; then
226
227             openssl_version_req=ifelse([$1], [], [], [$1])
228
229             if test  -n "$openssl_version_req"; then
230
231                 AC_MSG_CHECKING([if OpenSSL version is >= $openssl_version_req])
232
233                 if test -f "$openssl_include_dir/openssl/opensslv.h"; then
234
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])
238
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"
246                     fi
247                     if test "x$openssl_version_req_patch" = "x"; then
248                         openssl_version_req_patch="\`"
249                     fi
250
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)) \
255                                                \+ $((0xf))`
256
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)))`
259
260                     openssl_version_check=`expr $openssl_version_number \>\= $openssl_version_req_number`
261                     if test "$openssl_version_check" = "1"; then
262                         AC_MSG_RESULT([yes])
263                     else
264                         AC_MSG_RESULT([no])
265                         AC_MSG_WARN([Found $OPENSSL_VERSION, which is older than required. Possible compilation failure.])
266                     fi
267                 else
268                     AC_MSG_RESULT([no])
269                     AC_MSG_WARN([Missing header openssl/opensslv.h. Unable to determine OpenSSL version.])
270                 fi
271             fi
272         fi
273
274     else
275         HAVE_OPENSSL="no"
276         AC_MSG_RESULT([$HAVE_OPENSSL])
277
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@:>@])
280         fi
281     fi
282     if test "$HAVE_OPENSSL" = "yes"; then
283         CPPFLAGS="$CPPFLAGS $OPENSSL_CPPFLAGS -DHAVE_SSL=1"
284         LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS $OPENSSL_LIBS"
285     fi
286 ])