From 82f9d834ad772b2f16b6524f679d14d8a7afe881 Mon Sep 17 00:00:00 2001 From: Gabriele Contini <gcontini@users.noreply.github.com> Date: 周二, 10 9月 2019 23:29:10 +0800 Subject: [PATCH] fix issue #37 tests in windows --- /dev/null | 14 -- test/license-generator/CMakeLists.txt | 2 doc/snippets.txt | 21 ++++ .travis.yml | 42 ++++++-- src/tools/base_lib/win/CryptoHelperWindows.cpp | 69 +++++++------ windows_download_boost.bat | 9 + CMakeLists.txt | 82 ++++++++------- README.md | 2 8 files changed, 143 insertions(+), 98 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0208224..1b67b9b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,32 +51,50 @@ - docker commit centos7_toconfigure centos7_configured script: - docker run --name centos7_make -v `pwd`:/root/open-license-manager centos7_configured /bin/bash -c - "cd /root/open-license-manager/build && make && make install && CTEST_OUTPUT_ON_FAILURE=1 VIRT_ENV=CONTAINER make test" + "cd /root/open-license-manager/build && make && make install && VIRT_ENV=CONTAINER make test" - os: windows - name: "Widnows server 1803 - Visual Studio 15 2017 Win64" + name: "Widnows server 1803 - Visual Studio 15 2017 Win64 - (/MD)" + env: + - CACHE_NAME=boost cache: directories: - "/C/local/boost" before_script: - - ./windows_travis.bat + - sc stop CryptSvc + - travis_wait 30 ./windows_download_boost.bat - cd "/C/Users/travis/build/open-license-manager/open-license-manager" - mkdir build - - cd build && cmake -G "Visual Studio 15 2017 Win64" -T "host=x64" -DBOOST_ROOT="C:/local/boost" -DCMAKE_INSTALL_PREFIX=../../install .. + - cd build && cmake -G "Visual Studio 15 2017 Win64" -DBOOST_ROOT="C:/local/boost" .. script: - - true -# - PATH="/C/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin":$PATH -# - MSBuild.exe "license++.sln" -# - ctest -# - cmake -T "host=x64" -DBOOST_ROOT="C:/local/boost" -DCMAKE_INSTALL_PREFIX=../../install --build .. --target RUN_TESTS - + - cmake --build . --target install --config Release + - ctest -C Release + + - os: windows + name: "Widnows server 1803 - Visual Studio 15 2017 Win64 - (/MT)" + env: + - CACHE_NAME=boost + cache: + directories: + - "/C/local/boost" + before_script: + #- Regsvr32.exe "C:\Widnows\System32\rsaenh.dll" + - travis_wait 30 ./windows_download_boost.bat + - cd "/C/Users/travis/build/open-license-manager/open-license-manager" + - mkdir build + - cd build && cmake -G "Visual Studio 15 2017 Win64" -DBOOST_ROOT="C:/local/boost" -DSTATIC_RUNTIME=1 .. + script: + - cmake --build . --target install --config Release + - ctest -C Release + +env: + - CTEST_OUTPUT_ON_FAILURE=1 before_script: - mkdir build - cd build && cmake -DCMAKE_INSTALL_PREFIX=../../install .. - script: - make - make install - - CTEST_OUTPUT_ON_FAILURE=1 make test + - make test diff --git a/CMakeLists.txt b/CMakeLists.txt index d8f75ac..8a397b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,19 +32,21 @@ include(target_arch) target_architecture( TARGET_ARCHITECTURE ) message(STATUS "architecture detected: ${TARGET_ARCHITECTURE}") - #force to patch an error in cmake - if(TARGET_ARCHITECTURE STREQUAL "x86_64") - SET(CMAKE_EXE_LINKER_FLAGS "/machine:X64" CACHE STRING "Linker flags" FORCE) - SET(CMAKE_MODULE_LINKER_FLAGS "/machine:X64" CACHE STRING "Module Linker flags" FORCE) - SET(CMAKE_SHARED_LINKER_FLAGS "/machine:X64" CACHE STRING "Shared Linker flags" FORCE) - endif() - SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib ) #${CMAKE_FIND_LIBRARY_SUFFIXES} - add_definitions("/D_CRT_SECURE_NO_WARNINGS") + + #Boost > 3.15 handle the /MD flag more nicely than this + if(${STATIC_RUNTIME}) + string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") + string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") + string(REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") + string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + SET(Boost_USE_STATIC_RUNTIME ON) + endif(${STATIC_RUNTIME}) + + #SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib ) #${CMAKE_FIND_LIBRARY_SUFFIXES} + add_definitions("/D _CRT_SECURE_NO_WARNINGS") else(MSVC) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe -fmessage-length=0 -std=c++11 -Wuninitialized -fPIC") #-Wall SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -fmessage-length=0 -Wall -Wuninitialized -fPIC") - #use static libraries in release to make it easier to distribute it - #SET(Boost_USE_STATIC_RUNTIME ${RELEASE_BUILD}) if(RELEASE_BUILD) SET(CMAKE_FIND_LIBRARY_SUFFIXES .a) #.so for dynamic libraries else() @@ -56,25 +58,7 @@ IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) SET(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install path prefix" FORCE) ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) -endif(MSVC) -set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG>) - - -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - SET(HAVE_64BIT_SIZE_T 1) -else(CMAKE_SIZEOF_VOID_P EQUAL 8) - SET(HAVE_64BIT_SIZE_T 0) -endif(CMAKE_SIZEOF_VOID_P EQUAL 8) - -set( Boost_USE_STATIC_LIBS ON ) -find_package(Boost REQUIRED COMPONENTS date_time unit_test_framework program_options system filesystem) -#set below in case of dynamic linking in debug. -#set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:BOOST_TEST_DYN_LINK>) -add_definitions(-DBOOST_ALL_NO_LIB) #Disable Boost Microsoft magic, all dependencies are handled by cmake -add_definitions(-DBOOST_LIB_DIAGNOSTIC) #Check it is really disabled - -if(NOT MSVC) - #find a static version of openssl crypto library + #find a static version of openssl crypto library find_package(OpenSSL REQUIRED) include_directories(${OPENSSL_INCLUDE_DIR}) if(MINGW) @@ -82,7 +66,6 @@ else(MINGW) list(APPEND EXTERNAL_LIBS ${OPENSSL_CRYPTO_LIBRARY}) endif(MINGW) - MESSAGE(STATUS "Found openssl version ${OPENSSL_VERSION} ") #Zlib required when openssl version < 1.0.1f @@ -100,12 +83,40 @@ list(APPEND EXTERNAL_LIBS dl) endif(NOT MINGW) list(APPEND EXTERNAL_LIBS "-Wl,-Bdynamic") -endif(NOT MSVC) +endif(MSVC) + +set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:_DEBUG>) + + +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + SET(HAVE_64BIT_SIZE_T 1) +else(CMAKE_SIZEOF_VOID_P EQUAL 8) + SET(HAVE_64BIT_SIZE_T 0) +endif(CMAKE_SIZEOF_VOID_P EQUAL 8) + +set( Boost_USE_STATIC_LIBS ON ) +find_package(Boost REQUIRED COMPONENTS date_time unit_test_framework program_options system filesystem) +#set below in case of dynamic linking in debug. +#set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:BOOST_TEST_DYN_LINK>) +add_definitions(-DBOOST_ALL_NO_LIB) #Disable Boost Microsoft magic, all dependencies are handled by cmake +add_definitions(-DBOOST_LIB_DIAGNOSTIC) #Check it is really disabled configure_file ( "src/build_properties.h.in" "${CMAKE_BINARY_DIR}/build_properties.h" ) + +message( STATUS "C compiler : " ${CMAKE_C_COMPILER}) +message( STATUS "C compiler flags : " ${CMAKE_C_FLAGS}) +message( STATUS "CXX compiler : " ${CMAKE_CXX_COMPILER}) +message( STATUS "CXX compiler flags: " ${CMAKE_CXX_FLAGS}) +message( STATUS "CXX compiler flags debug : " ${CMAKE_CXX_FLAGS_DEBUG}) +message( STATUS "CXX compiler flags release: " ${CMAKE_CXX_FLAGS_RELEASE}) +if(CMAKE_BUILD_TYPE) + message( STATUS "Build type : " ${CMAKE_BUILD_TYPE}) +endif(CMAKE_BUILD_TYPE) +message( STATUS "Install prefix : " ${CMAKE_INSTALL_PREFIX}) + #include build directory to find build_properties.h include_directories(${CMAKE_BINARY_DIR}) @@ -119,11 +130,4 @@ ENDIF(BUILD_TESTING) add_subdirectory(test) -message( STATUS "C compiler: " ${CMAKE_C_COMPILER}) -message( STATUS "C compiler flags: " ${CMAKE_C_FLAGS}) -message( STATUS "CXX compiler: " ${CMAKE_CXX_COMPILER}) -message( STATUS "CXX compiler flags: " ${CMAKE_CXX_FLAGS}) -if(CMAKE_BUILD_TYPE) - message( STATUS "Build type: " ${CMAKE_BUILD_TYPE}) -endif(CMAKE_BUILD_TYPE) -message( STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX}) + diff --git a/README.md b/README.md index b18f2c7..58dd4ad 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ ## on Windows (with MSVC 2015) ``` -cmake .. -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../install +cmake .. -G "Visual Studio 14 2015 Win64" -DBOOST_ROOT="{Folder where boost is}" -DCMAKE_INSTALL_PREFIX=../install cmake --build . --target install --config Release ``` diff --git a/doc/snippets.txt b/doc/snippets.txt new file mode 100644 index 0000000..b5ce841 --- /dev/null +++ b/doc/snippets.txt @@ -0,0 +1,21 @@ +## windows build boost + +@echo off +REM build and cache boost on Windows N.B. if no output for 10 minutes it's killed by travis. +REM if set every command to verbose it's killed too for "too much output"... +if not exist "C:/local/boost/libs" ( + echo "Boost not cached, compiling it" + rem wget -q https://dl.bintray.com/boostorg/release/1.71.0/source/boost_1_71_0.7z + wget -q -O boost.exe https://dl.bintray.com/boostorg/release/1.71.0/binaries/boost_1_71_0-msvc-14.1-64.exe + echo "Boost downloaded" + rem 7z x boost_1_71_0.7z -bb1 -oC:/local | C:\Windows\System32\findstr.exe /r /c:鈥漜pp鈥� + rem cd "C:/local/boost_1_71_0" + rem C:/local/boost_1_71_0/bootstrap.bat + rem C:/local/boost_1_71_0/b2.exe -j16 toolset=msvc-14.1 address-model=64 architecture=x86 link=static threading=multi runtime-link=static --with-date_time --with-test --with-filesystem --with-program_options --with-regex --with-serialization --with-system --prefix=C:/local/boost stage + rem echo "Boost compiled" + rem C:/local/boost_1_71_0/b2.exe -j16 toolset=msvc-14.1 address-model=64 architecture=x86 link=static threading=multi runtime-link=static --with-date_time --with-test --with-filesystem --with-program_options --with-regex --with-serialization --with-system --prefix=C:/local/boost install > a.txt 2>&1 + boost.exe /SILENT /DIR=C:\local\boost + echo "Boost installed" + cd C:/local/boost + dir +) else echo "Boost already installed" \ No newline at end of file diff --git a/src/tools/base_lib/win/CryptoHelperWindows.cpp b/src/tools/base_lib/win/CryptoHelperWindows.cpp index 7bf0ffb..98d14b3 100644 --- a/src/tools/base_lib/win/CryptoHelperWindows.cpp +++ b/src/tools/base_lib/win/CryptoHelperWindows.cpp @@ -5,9 +5,10 @@ * */ -#include "CryptoHelperWindows.h" #include <sstream> #include <vector> +#include <string> +#include "CryptoHelperWindows.h" // The RSA public-key key exchange algorithm #define ENCRYPT_ALGORITHM CALG_RSA_SIGN // The high order WORD 0x0200 (decimal 512) @@ -20,20 +21,26 @@ CryptoHelperWindows::CryptoHelperWindows() { m_hCryptProv = NULL; m_hCryptKey = NULL; - if (!CryptAcquireContext(&m_hCryptProv, "license++sign", MS_ENHANCED_PROV, - PROV_RSA_FULL, 0)) { + if (!CryptAcquireContext(&m_hCryptProv, "license_sign", NULL, PROV_RSA_FULL, 0)) { // If the key container cannot be opened, try creating a new // container by specifying a container name and setting the // CRYPT_NEWKEYSET flag. - printf("Error in AcquireContext 0x%08x \n", GetLastError()); - if (NTE_BAD_KEYSET == GetLastError()) { - if (!CryptAcquireContext(&m_hCryptProv, "license++sign", - MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { - printf("Error in AcquireContext 0x%08x \n", GetLastError()); - throw logic_error(""); + DWORD lastError = GetLastError(); + printf("Error in CryptAcquireContext (1) 0x%08x \n", lastError); + if (NTE_BAD_KEYSET == lastError) { + if (!CryptAcquireContext(&m_hCryptProv, "license_sign", NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { + printf("Warn in CryptAcquireContext: acquiring new user keyset failed 0x%08x, trying less secure mackine keyset \n", GetLastError()); + //maybe access to protected storage disabled. Try with machine keys (less secure) + if (!CryptAcquireContext(&m_hCryptProv, "license_sign", NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET)) { + printf("Error in CryptAcquireContext (2) 0x%08x \n", GetLastError()); + if (!CryptAcquireContext(&m_hCryptProv, "license_sign", NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET|CRYPT_MACHINE_KEYSET)) { + printf("Error in CryptAcquireContext (3): acquiring new keyset(machine) failed 0x%08x \n", GetLastError()); + throw logic_error(""); + } + } } } else { - printf(" Error in AcquireContext 0x%08x \n", GetLastError()); + printf(" Error in CryptAcquireContext (4) 0x%08x \n", lastError); throw logic_error(""); } } @@ -57,12 +64,12 @@ m_hCryptKey = NULL; // Call the CryptGenKey method to get a handle // to a new exportable key-pair. - if (!CryptGenKey(m_hCryptProv, - ENCRYPT_ALGORITHM, + if (!CryptGenKey(m_hCryptProv, ENCRYPT_ALGORITHM, KEYLENGTH | CRYPT_EXPORTABLE, &m_hCryptKey)) { dwErrCode = GetLastError(); throw logic_error( - string("Error generating keys ") + to_string(static_cast<long long>(dwErrCode))); + string("Error generating keys ") + + to_string(static_cast<long long>(dwErrCode))); } } @@ -81,8 +88,7 @@ // This call here determines the length of the key // blob. if (!CryptExportKey(m_hCryptKey, - NULL, PUBLICKEYBLOB, 0, - nullptr, &dwBlobLen)) { + NULL, PUBLICKEYBLOB, 0, nullptr, &dwBlobLen)) { dwErrCode = GetLastError(); throw logic_error( string("Error calculating size of public key ") @@ -98,7 +104,8 @@ delete pbKeyBlob; dwErrCode = GetLastError(); throw logic_error( - string("Error exporting public key ") + to_string(static_cast<long long>(dwErrCode))); + string("Error exporting public key ") + + to_string(static_cast<long long>(dwErrCode))); } else { ss << "\t"; for (unsigned int i = 0; i < dwBlobLen; i++) { @@ -141,8 +148,7 @@ // This call here determines the length of the key // blob. if (!CryptExportKey(m_hCryptKey, - NULL, PRIVATEKEYBLOB, 0, - nullptr, &dwBlobLen)) { + NULL, PRIVATEKEYBLOB, 0, nullptr, &dwBlobLen)) { dwErrCode = GetLastError(); throw logic_error( string("Error calculating size of private key ") @@ -159,7 +165,8 @@ delete pbKeyBlob; dwErrCode = GetLastError(); throw logic_error( - string("Error exporting private key ") + to_string(static_cast<long long>(dwErrCode))); + string("Error exporting private key ") + + to_string(static_cast<long long>(dwErrCode))); } else { ss << "\t"; for (unsigned int i = 0; i < dwBlobLen; i++) { @@ -176,14 +183,14 @@ return ss.str(); } -void CryptoHelperWindows::printHash(HCRYPTHASH* hHash) const { +void CryptoHelperWindows::printHash(HCRYPTHASH *hHash) const { BYTE *pbHash; DWORD dwHashLen; DWORD dwHashLenSize = sizeof(DWORD); - char* hashStr; + char *hashStr; unsigned int i; - if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *) &dwHashLen, + if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE*) &dwHashLen, &dwHashLenSize, 0)) { pbHash = (BYTE*) malloc(dwHashLen); hashStr = (char*) malloc(dwHashLen * 2 + 1); @@ -198,10 +205,10 @@ } } -const string CryptoHelperWindows::signString(const void* privateKey, - size_t pklen, const string& license) const { - BYTE *pbBuffer = (BYTE *) license.c_str(); - const DWORD dwBufferLen = (DWORD)strlen((char *)pbBuffer); +const string CryptoHelperWindows::signString(const void *privateKey, + size_t pklen, const string &license) const { + BYTE *pbBuffer = (BYTE*) license.c_str(); + const DWORD dwBufferLen = (DWORD) strlen((char*) pbBuffer); HCRYPTHASH hHash; HCRYPTKEY hKey; @@ -212,8 +219,8 @@ //------------------------------------------------------------------- // Acquire a cryptographic provider context handle. - if (!CryptImportKey(m_hCryptProv, (const BYTE *) privateKey, (DWORD) pklen, 0, 0, - &hKey)) { + if (!CryptImportKey(m_hCryptProv, (const BYTE*) privateKey, (DWORD) pklen, + 0, 0, &hKey)) { throw logic_error( string("Error in importing the PrivateKey ") + to_string(static_cast<long long>(GetLastError()))); @@ -251,7 +258,7 @@ //------------------------------------------------------------------- // Allocate memory for the signature buffer. - if (pbSignature = (BYTE *) malloc(dwSigLen)) { + if (pbSignature = (BYTE*) malloc(dwSigLen)) { printf("Memory allocated for the signature.\n"); } else { throw logic_error(string("Out of memory.")); @@ -259,8 +266,8 @@ //------------------------------------------------------------------- // Sign the hash object. - if (CryptSignHash(hHash, AT_SIGNATURE, - nullptr, 0, pbSignature, &dwSigLen)) { + if (CryptSignHash(hHash, AT_SIGNATURE, nullptr, 0, pbSignature, + &dwSigLen)) { printf("pbSignature is the signature length. %d\n", dwSigLen); } else { throw logic_error(string("Error during CryptSignHash.")); diff --git a/test/license-generator/CMakeLists.txt b/test/license-generator/CMakeLists.txt index 484d15e..4708cf0 100644 --- a/test/license-generator/CMakeLists.txt +++ b/test/license-generator/CMakeLists.txt @@ -7,7 +7,7 @@ target_link_libraries( license_generator_test license_generator_lib - ${Boost_LIBRARIES} + ${Boost_LIBRARIES} ) #add_dependencies( license_generator_test boost_test boost_filesystem ) diff --git a/windows_download_boost.bat b/windows_download_boost.bat new file mode 100644 index 0000000..0ce2b16 --- /dev/null +++ b/windows_download_boost.bat @@ -0,0 +1,9 @@ +@echo off +REM build and cache boost on Windows N.B. if no output for 10 minutes it's killed by travis. +if not exist "C:/local/boost/libs" ( + echo "Boost not cached, compiling it" + wget -q -O boost.exe https://dl.bintray.com/boostorg/release/1.71.0/binaries/boost_1_71_0-msvc-14.1-64.exe + echo "Boost downloaded" + boost.exe /SILENT /DIR=C:\local\boost + echo "Boost installed" +) else echo "Boost already installed" \ No newline at end of file diff --git a/windows_travis.bat b/windows_travis.bat deleted file mode 100644 index de31ec9..0000000 --- a/windows_travis.bat +++ /dev/null @@ -1,14 +0,0 @@ -@echo off -if not exist "C:/local/boost/include" ( - echo "Boost not cached, compiling it" - wget -q https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.7z - echo "Boost downloaded" - 7z x boost_1_68_0.7z -bb1 -oC:/local | C:\Windows\System32\findstr.exe /r /c:鈥漜pp鈥� - cd "C:/local/boost_1_68_0" - dir - C:/local/boost_1_68_0/bootstrap.bat - C:/local/boost_1_68_0/b2.exe --with-date_time --with-test --with-filesystem --with-program_options --with-regex --with-serialization --with-system runtime-link=static --prefix=C:/local/boost stage - C:/local/boost_1_68_0/b2.exe --with-date_time --with-test --with-filesystem --with-program_options --with-regex --with-serialization --with-system runtime-link=static --prefix=C:/local/boost install > a.txt 2>&1 - cd C:/local/boost - dir -) \ No newline at end of file -- Gitblit v1.9.1