diff --git a/cmake/Modules/FindLibGcrypt.cmake b/cmake/Modules/FindLibGcrypt.cmake new file mode 100644 index 0000000..0e1727f --- /dev/null +++ b/cmake/Modules/FindLibGcrypt.cmake @@ -0,0 +1,50 @@ + +# - Try to find the Gcrypt library +# Once run this will define +# +# LIBGCRYPT_FOUND - set if the system has the gcrypt library +# LIBGCRYPT_CFLAGS - the required gcrypt compilation flags +# LIBGCRYPT_LIBRARIES - the linker libraries needed to use the gcrypt library +# +# Copyright (c) 2006 Brad Hards +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +# libgcrypt is moving to pkg-config, but earlier version don't have it + +#search in typical paths for libgcrypt-config +FIND_PROGRAM(LIBGCRYPTCONFIG_EXECUTABLE NAMES libgcrypt-config) + +#reset variables +set(LIBGCRYPT_LIBRARIES) +set(LIBGCRYPT_CFLAGS) + +# if libgcrypt-config has been found +IF(LIBGCRYPTCONFIG_EXECUTABLE) + + EXEC_PROGRAM(${LIBGCRYPTCONFIG_EXECUTABLE} ARGS --libs RETURN_VALUE _return_VALUE OUTPUT_VARIABLE LIBGCRYPT_LIBRARIES) + + EXEC_PROGRAM(${LIBGCRYPTCONFIG_EXECUTABLE} ARGS --cflags RETURN_VALUE _return_VALUE OUTPUT_VARIABLE LIBGCRYPT_CFLAGS) + + IF(${LIBGCRYPT_CFLAGS} MATCHES "\n") + SET(LIBGCRYPT_CFLAGS " ") + ENDIF(${LIBGCRYPT_CFLAGS} MATCHES "\n") + + IF(LIBGCRYPT_LIBRARIES AND LIBGCRYPT_CFLAGS) + SET(LIBGCRYPT_FOUND TRUE) + ENDIF(LIBGCRYPT_LIBRARIES AND LIBGCRYPT_CFLAGS) + +ENDIF(LIBGCRYPTCONFIG_EXECUTABLE) + +if (LIBGCRYPT_FOUND) + if (NOT LibGcrypt_FIND_QUIETLY) + message(STATUS "Found libgcrypt: ${LIBGCRYPT_LIBRARIES}") + endif (NOT LibGcrypt_FIND_QUIETLY) +else (LIBGCRYPT_FOUND) + if (LibGcrypt_FIND_REQUIRED) + message(FATAL_ERROR "Could not find libgcrypt libraries") + endif (LibGcrypt_FIND_REQUIRED) +endif (LIBGCRYPT_FOUND) + +MARK_AS_ADVANCED(LIBGCRYPT_CFLAGS LIBGCRYPT_LIBRARIES) diff --git a/grive/src/main.cc b/grive/src/main.cc index f819311..bdb127f 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -32,6 +32,8 @@ #include +#include + #include #include #include @@ -40,10 +42,25 @@ const std::string client_id = "22314510474.apps.googleusercontent.com" ; const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ; +// libgcrypt insist this to be done in application, not library +void InitGCrypt() +{ + if ( !gcry_check_version(GCRYPT_VERSION) ) + throw gr::Exception() << gr::expt::ErrMsg( "libgcrypt version mismatch" ) ; + + // disable secure memory + gcry_control(GCRYCTL_DISABLE_SECMEM, 0); + + // tell Libgcrypt that initialization has completed + gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); +} + int main( int argc, char **argv ) { using namespace gr ; + InitGCrypt() ; + Config config ; std::auto_ptr comp_log(new log::CompositeLog) ; diff --git a/libgrive/CMakeLists.txt b/libgrive/CMakeLists.txt index b57cdcc..ab0e55a 100644 --- a/libgrive/CMakeLists.txt +++ b/libgrive/CMakeLists.txt @@ -2,7 +2,7 @@ project(libgrive) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") -find_package(OpenSSL REQUIRED) +find_package(LibGcrypt REQUIRED) find_package(JSONC REQUIRED) find_package(CURL REQUIRED) find_package(EXPAT REQUIRED) @@ -73,7 +73,7 @@ add_library( grive SHARED ${LIBGRIVE_SRC} ${OPT_SRC} ) target_link_libraries( grive ${CURL_LIBRARIES} ${JSONC_LIBRARY} - ${OPENSSL_LIBRARIES} + ${LIBGCRYPT_LIBRARIES} ${GDBM_LIBRARIES} ${Boost_LIBRARIES} ${IBERTY_LIBRARY} diff --git a/libgrive/src/util/Crypt.cc b/libgrive/src/util/Crypt.cc index 70a1251..43145e0 100644 --- a/libgrive/src/util/Crypt.cc +++ b/libgrive/src/util/Crypt.cc @@ -20,12 +20,14 @@ #include "Crypt.hh" #include "StdioFile.hh" +#include "Exception.hh" #include #include // dependent libraries -#include +#include +#include namespace gr { namespace crypt { @@ -47,23 +49,28 @@ std::string MD5( const fs::path& file ) std::string MD5( StdioFile& file ) { char buf[read_size] ; - EVP_MD_CTX md ; - EVP_MD_CTX_init( &md ); - EVP_DigestInit_ex( &md, EVP_md5(), 0 ) ; + + gcry_md_hd_t hd ; + gcry_error_t err = gcry_md_open( &hd, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC ) ; + if ( err != GPG_ERR_NO_ERROR ) + { + BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( gcry_strerror(err) ) ) ; + } std::size_t count = 0 ; while ( (count = file.Read( buf, sizeof(buf) )) > 0 ) - EVP_DigestUpdate( &md, buf, count ) ; - - unsigned int md5_size = EVP_MAX_MD_SIZE ; - unsigned char md5[EVP_MAX_MD_SIZE] ; - EVP_DigestFinal_ex( &md, md5, &md5_size ) ; + gcry_md_write( hd, buf, count ) ; + gcry_md_final(hd) ; + + unsigned char *md5 = gcry_md_read( hd, GCRY_MD_MD5 ) ; // format the MD5 string std::ostringstream ss ; - for ( unsigned int i = 0 ; i < md5_size ; i++ ) + for ( unsigned int i = 0 ; i < gcry_md_get_algo_dlen(GCRY_MD_MD5) ; i++ ) ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(md5[i]) ; + gcry_md_close( hd ) ; + return ss.str() ; }