Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt.orig
+++ CMakeLists.txt
@@ -22,7 +22,6 @@ SET(PODOFO_LIBVERSION "${PODOFO_SOVERSIO
 #
 INCLUDE(CheckIncludeFile)
 INCLUDE(CheckLibraryExists)
-INCLUDE(UsePkgConfig)
 INCLUDE(TestBigEndian)
 INCLUDE(CheckTypeSize)
 
@@ -486,9 +485,14 @@ LINK_DIRECTORIES(
 # will include these and the correct podofo target, so clients
 # should specify only PODOFO_LIB .
 #
+# The LIBCRYPTO_LDFLAGS is defined only if the libcrypto was found
+# by pkg-config, otherwise it will be empty. It can contain proper
+# path for the libcrypto too, thus it's added here.
+#
 SET(PODOFO_LIB_DEPENDS
   ${ZLIB_LIBRARIES}
   ${LIBIDN_LIBRARIES}
+  ${LIBCRYPTO_LDFLAGS}
   ${LIBCRYPTO_LIBRARIES}
   ${LIBJPEG_LIBRARIES}
   ${PLATFORM_SYSTEM_LIBRARIES}
Index: cmake/modules/FindPkgConfig.cmake
===================================================================
--- cmake/modules/FindPkgConfig.cmake.orig
+++ cmake/modules/FindPkgConfig.cmake
@@ -1,74 +0,0 @@
-## FindPkgConfig.cmake
-## by Albert Strasheim <http://students . ee . sun . ac . za/~albert/>
-##
-## This module finds packages using pkg-config, which retrieves
-## information about packages from special metadata files.
-##
-## See http://www . freedesktop . org/Software/pkgconfig/
-##
-## -------------------------------------------------------------------
-##
-## Usage:
-##
-## INCLUDE( ${CMAKE_ROOT}/Modules/FindPkgConfig.cmake)
-## PKGCONFIG("libxml-2.0 >= 1.3")
-## IF(PKGCONFIG_FOUND)
-##   # do something with CMAKE_PKGCONFIG_C_FLAGS
-##   # do something with PKGCONFIG_LIBRARIES
-## ELSE(PKGCONFIG_FOUND)
-#    MESSAGE("Cannot find libxml2 version 1.3 or above")
-## ENDIF(PKGCONFIG_FOUND)
-##
-## Notes:
-## 
-## You can set the PKG_CONFIG_PATH environment variable to tell
-## pkg-config where to search for .pc files. See pkg-config(1) for
-## more information.
-
-#FIXME: IF(WIN32) pkg-config --msvc-syntax ENDIF(WIN32) ???
-
-FIND_PROGRAM(CMAKE_PKGCONFIG_EXECUTABLE pkg-config)
-MARK_AS_ADVANCED(CMAKE_PKGCONFIG_EXECUTABLE)
-
-MACRO(PKGCONFIG LIBRARY)
-  SET(PKGCONFIG_FOUND 0)
-
-#  MESSAGE("DEBUG: find library '${LIBRARY}'")
-  
-  IF(CMAKE_PKGCONFIG_EXECUTABLE)
-#    MESSAGE("DEBUG: pkg-config executable found")
-    
-    EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
-      ARGS "'${LIBRARY}'"
-      OUTPUT_VARIABLE PKGCONFIG_OUTPUT
-      RETURN_VALUE PKGCONFIG_RETURN)
-    IF(NOT PKGCONFIG_RETURN)
-#      MESSAGE("DEBUG: packages found")
-      
-      # set C_FLAGS and CXX_FLAGS
-      EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
-        ARGS "--cflags '${LIBRARY}'"
-        OUTPUT_VARIABLE CMAKE_PKGCONFIG_C_FLAGS)
-      SET(CMAKE_PKGCONFIG_CXX_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS}")
-      
-      # set LIBRARIES
-      EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
-        ARGS "--libs '${LIBRARY}'"
-        OUTPUT_VARIABLE PKGCONFIG_LIBRARIES)
-      
-      SET(PKGCONFIG_FOUND 1)
-    ELSE(NOT PKGCONFIG_RETURN)
-#      MESSAGE("DEBUG '${LIBRARY}' NOT FOUND by pkg-config")
-      
-      SET(CMAKE_PKGCONFIG_C_FLAGS "")
-      SET(CMAKE_PKGCONFIG_CXX_FLAGS "")
-      SET(PKGCONFIG_LIBRARIES "")
-    ENDIF(NOT PKGCONFIG_RETURN)
-#  ELSE(CMAKE_PKGCONFIG_EXECUTABLE)
-#    MESSAGE("DEBUG: pkg-config executable NOT FOUND")
-  ENDIF(CMAKE_PKGCONFIG_EXECUTABLE)
-
-#  MESSAGE("DEBUG: CMAKE_PKGCONFIG_C_FLAGS=${CMAKE_PKGCONFIG_C_FLAGS}")
-#  MESSAGE("DEBUG: PKGCONFIG_LIBRARIES=${PKGCONFIG_LIBRARIES}")
-ENDMACRO(PKGCONFIG)
-
Index: cmake/modules/FindLIBCRYPTO.cmake
===================================================================
--- cmake/modules/FindLIBCRYPTO.cmake.orig
+++ cmake/modules/FindLIBCRYPTO.cmake
@@ -11,39 +11,70 @@ IF (LIBCRYPTO_INCLUDE_DIR)
   SET(LIBCRYPTO_FIND_QUIETLY TRUE)
 ENDIF (LIBCRYPTO_INCLUDE_DIR)
 
-# Require a regular OpenSSL even on OSX/iOS
-# IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
-#   # MacOSX has deprecated the use of openssl crypto functions
-#   # and replaced it with API-compatible CommonCrypto
-#   FIND_PATH(LIBCRYPTO_INCLUDE_DIR CommonCrypto/CommonDigest.h)
-#   SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} ssl)
-#   SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} ssld)
-# ELSE(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
-  FIND_PATH(LIBCRYPTO_INCLUDE_DIR openssl/sha.h)
-  SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} crypto)
-  SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} cryptod)
-# ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
-
-FIND_LIBRARY(LIBCRYPTO_LIBRARY_RELEASE NAMES ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} )
-
-# Find a debug library if one exists and use that for debug builds.
-# This really only does anything for win32, but does no harm on other
-# platforms.
-FIND_LIBRARY(LIBCRYPTO_LIBRARY_DEBUG NAMES ${LIBCRYPTO_LIBRARY_NAMES_DEBUG})
-
-INCLUDE(LibraryDebugAndRelease)
-SET_LIBRARY_FROM_DEBUG_AND_RELEASE(LIBCRYPTO)
-
-# handle the QUIETLY and REQUIRED arguments and set LIBCRYPTO_FOUND to TRUE if 
-# all listed variables are TRUE
-INCLUDE(PoDoFoFindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBCRYPTO DEFAULT_MSG LIBCRYPTO_LIBRARY LIBCRYPTO_INCLUDE_DIR)
-
-IF(LIBCRYPTO_FOUND)
-  SET( LIBCRYPTO_LIBRARIES ${LIBCRYPTO_LIBRARY} )
-ELSE(LIBCRYPTO_FOUND)
-  SET( LIBCRYPTO_LIBRARIES )
-ENDIF(LIBCRYPTO_FOUND)
-
-MARK_AS_ADVANCED( LIBCRYPTO_LIBRARY LIBCRYPTO_INCLUDE_DIR )
+IF (NOT LIBCRYPTO_INCLUDE_DIR OR NOT LIBCRYPTO_LIBRARIES)
+  FIND_PACKAGE(PkgConfig)
 
+  IF (PKG_CONFIG_FOUND)
+    PKG_CHECK_MODULES (LIBCRYPTO libcrypto)
+    IF (LIBCRYPTO_FOUND)
+      SET (LIBCRYPTO_INCLUDE_DIR ${LIBCRYPTO_INCLUDE_DIRS})
+    ENDIF (LIBCRYPTO_FOUND)
+  ENDIF (PKG_CONFIG_FOUND)
+ENDIF (NOT LIBCRYPTO_INCLUDE_DIR OR NOT LIBCRYPTO_LIBRARIES)
+
+IF (NOT LIBCRYPTO_INCLUDE_DIR OR NOT LIBCRYPTO_LIBRARIES)
+  # Require a regular OpenSSL even on OSX/iOS
+  # IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+  #   # MacOSX has deprecated the use of openssl crypto functions
+  #   # and replaced it with API-compatible CommonCrypto
+  #   FIND_PATH(LIBCRYPTO_INCLUDE_DIR CommonCrypto/CommonDigest.h)
+  #   SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} ssl)
+  #   SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} ssld)
+  # ELSE(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
+    FIND_PATH(LIBCRYPTO_INCLUDE_DIR openssl/sha.h)
+    SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} crypto)
+    SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} cryptod)
+  # ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
+
+  FIND_LIBRARY(LIBCRYPTO_LIBRARY_RELEASE NAMES ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} )
+
+  # Find a debug library if one exists and use that for debug builds.
+  # This really only does anything for win32, but does no harm on other
+  # platforms.
+  FIND_LIBRARY(LIBCRYPTO_LIBRARY_DEBUG NAMES ${LIBCRYPTO_LIBRARY_NAMES_DEBUG})
+
+  INCLUDE(LibraryDebugAndRelease)
+  SET_LIBRARY_FROM_DEBUG_AND_RELEASE(LIBCRYPTO)
+
+  # handle the QUIETLY and REQUIRED arguments and set LIBCRYPTO_FOUND to TRUE if 
+  # all listed variables are TRUE
+  INCLUDE(FindPackageHandleStandardArgs)
+  FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBCRYPTO DEFAULT_MSG LIBCRYPTO_LIBRARY LIBCRYPTO_INCLUDE_DIR)
+
+  IF(LIBCRYPTO_FOUND)
+    SET( LIBCRYPTO_LIBRARIES ${LIBCRYPTO_LIBRARY} )
+  ELSE(LIBCRYPTO_FOUND)
+    SET( LIBCRYPTO_LIBRARIES )
+  ENDIF(LIBCRYPTO_FOUND)
+
+  MARK_AS_ADVANCED( LIBCRYPTO_LIBRARY LIBCRYPTO_INCLUDE_DIR )
+
+ENDIF (NOT LIBCRYPTO_INCLUDE_DIR OR NOT LIBCRYPTO_LIBRARIES)
+
+# check whether using OpenSSL 1.1 API
+IF (DEFINED LIBCRYPTO_INCLUDE_DIR AND DEFINED LIBCRYPTO_LIBRARIES)
+  INCLUDE(CheckCSourceCompiles)
+
+  SET(CMAKE_REQUIRED_INCLUDES ${LIBCRYPTO_INCLUDE_DIR})
+  SET(CMAKE_REQUIRED_LIBRARIES ${LIBCRYPTO_LIBRARIES})
+  CHECK_C_SOURCE_COMPILES("#include <openssl/opensslv.h>
+			#ifndef OPENSSL_VERSION_NUMBER
+			#error No OPENSSL_VERSION_NUMBER defined
+			#endif
+			#if OPENSSL_VERSION_NUMBER < 0x10100000L
+			#error This is not OpenSSL 1.1 or higher
+			#endif
+			int main(void) { return 0; }" PODOFO_HAVE_OPENSSL_1_1)
+  UNSET(CMAKE_REQUIRED_INCLUDES)
+  UNSET(CMAKE_REQUIRED_LIBRARIES)
+ENDIF (DEFINED LIBCRYPTO_INCLUDE_DIR AND DEFINED LIBCRYPTO_LIBRARIES)
Index: podofo_config.h.in
===================================================================
--- podofo_config.h.in.orig
+++ podofo_config.h.in
@@ -48,6 +48,7 @@
 #cmakedefine PODOFO_HAVE_BOOST
 #cmakedefine PODOFO_HAVE_CPPUNIT
 #cmakedefine PODOFO_HAVE_OPENSSL
+#cmakedefine PODOFO_HAVE_OPENSSL_1_1
 #cmakedefine PODOFO_HAVE_LIBIDN
 
 /* Platform quirks */
Index: src/base/PdfEncrypt.cpp
===================================================================
--- src/base/PdfEncrypt.cpp.orig
+++ src/base/PdfEncrypt.cpp
@@ -79,19 +79,38 @@ class AESCryptoEngine {
     
         AESCryptoEngine()
         {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+            aes = EVP_CIPHER_CTX_new();
+    #else
             EVP_CIPHER_CTX_init(&aes);
+    #endif
         }
     
-        EVP_CIPHER_CTX* getEngine() {return &aes;}
+        EVP_CIPHER_CTX* getEngine()
+        {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+            return aes;
+    #else
+            return &aes;
+    #endif
+        }
     
         ~AESCryptoEngine()
         {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+            EVP_CIPHER_CTX_free(aes);
+    #else
             EVP_CIPHER_CTX_cleanup(&aes);
+    #endif
         }
     
     private:
-    
+
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+        EVP_CIPHER_CTX *aes;
+    #else
         EVP_CIPHER_CTX aes;
+    #endif
 };
 
 // A class that holds the RC4 Crypto object
@@ -101,19 +120,38 @@ public:
     
     RC4CryptoEngine()
     {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+        rc4 = EVP_CIPHER_CTX_new();
+    #else
         EVP_CIPHER_CTX_init(&rc4);
+    #endif
     }
     
-    EVP_CIPHER_CTX* getEngine() {return &rc4;}
+    EVP_CIPHER_CTX* getEngine()
+    {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+        return rc4;
+    #else
+        return &rc4;
+    #endif
+    }
     
     ~RC4CryptoEngine()
     {
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+        EVP_CIPHER_CTX_free(rc4);
+    #else
         EVP_CIPHER_CTX_cleanup(&rc4);
+    #endif
     }
     
 private:
     
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    EVP_CIPHER_CTX *rc4;
+    #else
     EVP_CIPHER_CTX rc4;
+    #endif
 };
     
 /** A class that can encrypt/decrpyt streamed data block wise
@@ -1304,24 +1342,34 @@ void PdfEncryptSHABase::ComputeUserKey(c
     // UE = AES-256 encoded file encryption key with key=hash
     // CBC mode, no padding, init vector=0
     
-    EVP_CIPHER_CTX aes;
-    EVP_CIPHER_CTX_init(&aes);
+    EVP_CIPHER_CTX *aes;
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    aes = EVP_CIPHER_CTX_new();
+    #else
+    EVP_CIPHER_CTX aes_local;
+    EVP_CIPHER_CTX_init(&aes_local);
+    aes = &aes_local;
+    #endif
     
-    int status = EVP_EncryptInit_ex(&aes, EVP_aes_256_cbc(), NULL, hashValue, NULL);
+    int status = EVP_EncryptInit_ex(aes, EVP_aes_256_cbc(), NULL, hashValue, NULL);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    EVP_CIPHER_CTX_set_padding(&aes, 0); // disable padding
+    EVP_CIPHER_CTX_set_padding(aes, 0); // disable padding
     
     int dataOutMoved;
-    status = EVP_EncryptUpdate(&aes, m_ueValue, &dataOutMoved, m_encryptionKey, m_keyLength);
+    status = EVP_EncryptUpdate(aes, m_ueValue, &dataOutMoved, m_encryptionKey, m_keyLength);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    status = EVP_EncryptFinal_ex(&aes, &m_ueValue[dataOutMoved], &dataOutMoved);
+    status = EVP_EncryptFinal_ex(aes, &m_ueValue[dataOutMoved], &dataOutMoved);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    EVP_CIPHER_CTX_cleanup(&aes);
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    EVP_CIPHER_CTX_free(aes);
+    #else
+    EVP_CIPHER_CTX_cleanup(&aes_local);
+    #endif
 }
 
 void PdfEncryptSHABase::ComputeOwnerKey(const unsigned char * ownerpswd, int len)
@@ -1360,24 +1408,34 @@ void PdfEncryptSHABase::ComputeOwnerKey(
     // OE = AES-256 encoded file encryption key with key=hash
     // CBC mode, no padding, init vector=0
     
-    EVP_CIPHER_CTX aes;
-    EVP_CIPHER_CTX_init(&aes);
+    EVP_CIPHER_CTX *aes;
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    aes = EVP_CIPHER_CTX_new();
+    #else
+    EVP_CIPHER_CTX aes_local;
+    EVP_CIPHER_CTX_init(&aes_local);
+    aes = &aes_local;
+    #endif
     
-    int status = EVP_EncryptInit_ex(&aes, EVP_aes_256_cbc(), NULL, hashValue, NULL);
+    int status = EVP_EncryptInit_ex(aes, EVP_aes_256_cbc(), NULL, hashValue, NULL);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    EVP_CIPHER_CTX_set_padding(&aes, 0); // disable padding
+    EVP_CIPHER_CTX_set_padding(aes, 0); // disable padding
     
     int dataOutMoved;
-    status = EVP_EncryptUpdate(&aes, m_oeValue, &dataOutMoved, m_encryptionKey, m_keyLength);
+    status = EVP_EncryptUpdate(aes, m_oeValue, &dataOutMoved, m_encryptionKey, m_keyLength);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    status = EVP_EncryptFinal_ex(&aes, &m_oeValue[dataOutMoved], &dataOutMoved);
+    status = EVP_EncryptFinal_ex(aes, &m_oeValue[dataOutMoved], &dataOutMoved);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    EVP_CIPHER_CTX_cleanup(&aes);
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    EVP_CIPHER_CTX_free(aes);
+    #else
+    EVP_CIPHER_CTX_cleanup(&aes_local);
+    #endif
 }
 
 void PdfEncryptSHABase::PreprocessPassword( const std::string &password, unsigned char* outBuf, int &len)
@@ -1508,24 +1566,34 @@ PdfEncryptAESV3::GenerateEncryptionKey(c
     
     // Encrypt Perms value
     
-    EVP_CIPHER_CTX aes;
-    EVP_CIPHER_CTX_init(&aes);
+    EVP_CIPHER_CTX *aes;
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    aes = EVP_CIPHER_CTX_new();
+    #else
+    EVP_CIPHER_CTX aes_local;
+    EVP_CIPHER_CTX_init(&aes_local);
+    aes = &aes_local;
+    #endif
     
-    int status = EVP_EncryptInit_ex(&aes, EVP_aes_256_ecb(), NULL, m_encryptionKey, NULL);
+    int status = EVP_EncryptInit_ex(aes, EVP_aes_256_ecb(), NULL, m_encryptionKey, NULL);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    EVP_CIPHER_CTX_set_padding(&aes, 0); // disable padding
+    EVP_CIPHER_CTX_set_padding(aes, 0); // disable padding
     
     int dataOutMoved;
-    status = EVP_EncryptUpdate(&aes, m_permsValue, &dataOutMoved, perms, 16);
+    status = EVP_EncryptUpdate(aes, m_permsValue, &dataOutMoved, perms, 16);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    status = EVP_EncryptFinal_ex(&aes, &m_permsValue[dataOutMoved], &dataOutMoved);
+    status = EVP_EncryptFinal_ex(aes, &m_permsValue[dataOutMoved], &dataOutMoved);
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
-    EVP_CIPHER_CTX_cleanup(&aes);
+    #ifdef PODOFO_HAVE_OPENSSL_1_1
+    EVP_CIPHER_CTX_free(aes);
+    #else
+    EVP_CIPHER_CTX_cleanup(&aes_local);
+    #endif
 }
 
 bool PdfEncryptAESV3::Authenticate( const std::string & password, const PdfString & )
