1 # - Try to find the OpenSSL encryption library
2 # Once done this will define
4 # OPENSSL_ROOT_DIR - Set this variable to the root installation of OpenSSL
7 # OPENSSL_FOUND - system has the OpenSSL library
8 # OPENSSL_INCLUDE_DIR - the OpenSSL include directory
9 # OPENSSL_LIBRARIES - The libraries needed to use OpenSSL
10 # OPENSSL_VERSION - This is set to $major.$minor.$revision$path (eg. 0.9.8s)
12 #=============================================================================
13 # Copyright 2006-2009 Kitware, Inc.
14 # Copyright 2006 Alexander Neundorf <neundorf@kde.org>
15 # Copyright 2009-2011 Mathieu Malaterre <mathieu.malaterre@gmail.com>
17 # Distributed under the OSI-approved BSD License (the "License");
18 # see accompanying file Copyright.txt for details.
20 # This software is distributed WITHOUT ANY WARRANTY; without even the
21 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 # See the License for more information.
23 #=============================================================================
24 # (To distribute this file outside of CMake, substitute the full
25 # License text for the above reference.)
28 find_package(PkgConfig QUIET)
29 pkg_check_modules(_OPENSSL QUIET openssl)
33 # http://www.slproweb.com/products/Win32OpenSSL.html
34 set(_OPENSSL_ROOT_HINTS
36 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]"
37 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]"
40 file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
41 set(_OPENSSL_ROOT_PATHS
42 "${_programfiles}/OpenSSL"
43 "${_programfiles}/OpenSSL-Win32"
44 "${_programfiles}/OpenSSL-Win64"
50 set(_OPENSSL_ROOT_HINTS_AND_PATHS
51 HINTS ${_OPENSSL_ROOT_HINTS}
52 PATHS ${_OPENSSL_ROOT_PATHS}
55 set(_OPENSSL_ROOT_HINTS
61 find_path(OPENSSL_INCLUDE_DIR
65 ${_OPENSSL_INCLUDEDIR}
66 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
71 if(WIN32 AND NOT CYGWIN)
73 # /MD and /MDd are the standard values - if someone wants to use
74 # others, the libnames have to change here too
75 # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b
76 # TODO: handle /MT and static lib
77 # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix:
78 # * MD for dynamic-release
79 # * MDd for dynamic-debug
80 # * MT for static-release
81 # * MTd for static-debug
83 # Implementation details:
84 # We are using the libraries located in the VC subdir instead of the parent directory eventhough :
85 # libeay32MD.lib is identical to ../libeay32.lib, and
86 # ssleay32MD.lib is identical to ../ssleay32.lib
87 find_library(LIB_EAY_DEBUG
91 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
98 find_library(LIB_EAY_RELEASE
102 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
109 find_library(SSL_EAY_DEBUG
113 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
120 find_library(SSL_EAY_RELEASE
125 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
132 set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}")
133 set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}")
134 set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}")
135 set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}")
137 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
138 select_library_configurations(LIB_EAY)
139 select_library_configurations(SSL_EAY)
141 set( OPENSSL_LIBRARIES ${SSL_EAY_LIBRARY} ${LIB_EAY_LIBRARY} )
143 # same player, for MingW
144 set(LIB_EAY_NAMES libeay32)
145 set(SSL_EAY_NAMES ssleay32)
146 if(CMAKE_CROSS_COMPILING)
147 list(APPEND LIB_EAY_NAMES crypto)
148 list(APPEND SSL_EAY_NAMES ssl)
153 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
162 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
168 mark_as_advanced(SSL_EAY LIB_EAY)
169 set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
173 # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues:
179 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
189 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
194 mark_as_advanced(SSL_EAY LIB_EAY)
195 set( OPENSSL_LIBRARIES ${SSL_EAY} ${LIB_EAY} )
199 find_library(OPENSSL_SSL_LIBRARY
206 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
211 find_library(OPENSSL_CRYPTO_LIBRARY
216 ${_OPENSSL_ROOT_HINTS_AND_PATHS}
221 mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY)
224 set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY})
225 set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY})
227 set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
231 function(from_hex HEX DEC)
232 string(TOUPPER "${HEX}" HEX)
234 string(LENGTH "${HEX}" _strlen)
236 while (_strlen GREATER 0)
237 math(EXPR _res "${_res} * 16")
238 string(SUBSTRING "${HEX}" 0 1 NIBBLE)
239 string(SUBSTRING "${HEX}" 1 -1 HEX)
240 if (NIBBLE STREQUAL "A")
241 math(EXPR _res "${_res} + 10")
242 elseif (NIBBLE STREQUAL "B")
243 math(EXPR _res "${_res} + 11")
244 elseif (NIBBLE STREQUAL "C")
245 math(EXPR _res "${_res} + 12")
246 elseif (NIBBLE STREQUAL "D")
247 math(EXPR _res "${_res} + 13")
248 elseif (NIBBLE STREQUAL "E")
249 math(EXPR _res "${_res} + 14")
250 elseif (NIBBLE STREQUAL "F")
251 math(EXPR _res "${_res} + 15")
253 math(EXPR _res "${_res} + ${NIBBLE}")
256 string(LENGTH "${HEX}" _strlen)
259 set(${DEC} ${_res} PARENT_SCOPE)
262 if (OPENSSL_INCLUDE_DIR)
263 if (_OPENSSL_VERSION)
264 set(OPENSSL_VERSION "${_OPENSSL_VERSION}")
265 elseif(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h")
266 file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str
267 REGEX "^#define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*")
269 # The version number is encoded as 0xMNNFFPPS: major minor fix patch status
270 # The status gives if this is a developer or prerelease and is ignored here.
271 # Major, minor, and fix directly translate into the version numbers shown in
272 # the string. The patch field translates to the single character suffix that
273 # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so
276 string(REGEX REPLACE "^.*OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F]).*$"
277 "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}")
278 list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR)
279 list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR)
280 from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR)
281 list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX)
282 from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX)
283 list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH)
285 if (NOT OPENSSL_VERSION_PATCH STREQUAL "00")
286 from_hex("${OPENSSL_VERSION_PATCH}" _tmp)
287 # 96 is the ASCII code of 'a' minus 1
288 math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96")
290 # Once anyone knows how OpenSSL would call the patch versions beyond 'z'
291 # this should be updated to handle that, too. This has not happened yet
292 # so it is simply ignored here for now.
293 string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING)
296 set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}")
300 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
303 find_package_handle_standard_args(OpenSSL
310 "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
313 find_package_handle_standard_args(OpenSSL "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR"
319 mark_as_advanced(OPENSSL_INCLUDE_DIR OPENSSL_LIBRARIES)