open-license-manager
2014-10-13 62258ba3b4737432a95c3af8d0e03ed3fb7953e7
windows ok
8个文件已修改
256 ■■■■ 已修改文件
src/library/LicenseReader.cpp 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/base/logger.h 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/os.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/win/os-win.c 128 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/win/os-win.cpp 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/tools/base_lib/win/CryptoHelperWindows.cpp 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/tools/base_lib/win/CryptoHelperWindows.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/date_test.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.cpp
@@ -52,8 +52,9 @@
EventRegistry FullLicenseInfo::validate(int sw_version) {
    EventRegistry er;
    os_initialize();
    bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(),
    FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(),
            license_signature.c_str());
    bool sigVerified = sigVer == FUNC_RET_OK;
    if (sigVerified) {
        er.addEvent(LICENSE_VERIFIED, SVRT_INFO);
    } else {
src/library/base/logger.h
@@ -9,8 +9,8 @@
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#ifdef NDEBUG
#define LOG_DEBUG(M, ...) _log("[INFO] %s (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#ifdef _DEBUG
#define LOG_DEBUG(M, ...) _log("[DEBUG] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define LOG_DEBUG(M,...)
#endif
src/library/os/os.h
@@ -73,6 +73,8 @@
VIRTUALIZATION getVirtualization();
void os_initialize();
FUNCTION_RETURN verifySignature(const char* stringToVerify, const char* signatureB64);
#ifdef __cplusplus
}
#endif
src/library/os/win/os-win.c
@@ -2,9 +2,13 @@
#include <iphlpapi.h>
//definition of size_t
#include <stdlib.h>
//#include "../../base/base64.h"
#include "../../base/logger.h"
#include"../os.h"
#include "public-key.h"
#pragma comment(lib, "IPHLPAPI.lib")
unsigned char* unbase64(const char* ascii, int len, int *flen);
FUNCTION_RETURN getOsSpecificIdentifier(unsigned char identifier[6]){
    return FUNC_RET_NOT_AVAIL;
@@ -170,3 +174,127 @@
    return result;
}
static void printHash(HCRYPTHASH* hHash){
    BYTE         *pbHash;
    DWORD        dwHashLen;
    DWORD        dwHashLenSize = sizeof(DWORD);
    char* hashStr;
    int i;
    if (CryptGetHashParam(*hHash,HP_HASHSIZE,(BYTE *)&dwHashLen, &dwHashLenSize, 0))
    {
        pbHash = (BYTE*)malloc(dwHashLen);
        hashStr = (char*)malloc(dwHashLen*2 +1);
        if (CryptGetHashParam(*hHash,HP_HASHVAL,pbHash,    &dwHashLen,    0))    {
            for (i = 0; i < dwHashLen; i++)    {
                sprintf(&hashStr[i * 2], "%02x", pbHash[i]);
            }
            LOG_DEBUG("Hash to verify: %s", hashStr);
        }
        free(pbHash);
        free(hashStr);
    }
}
FUNCTION_RETURN verifySignature(const char* stringToVerify,    const char* signatureB64) {
    //--------------------------------------------------------------------
    // Declare variables.
    //
    // hProv:           Cryptographic service provider (CSP). This example
    //                  uses the Microsoft Enhanced Cryptographic
    //                  Provider.
    // hKey:            Key to be used. In this example, you import the
    //                  key as a PLAINTEXTKEYBLOB.
    // dwBlobLen:       Length of the plaintext key.
    // pbKeyBlob:       Pointer to the exported key.
    BYTE pubKey[] = PUBLIC_KEY;
    HCRYPTPROV hProv = 0;
    HCRYPTKEY hKey = 0;
    HCRYPTHASH hHash = 0;
    DWORD dwSigLen;
    BYTE* sigBlob;
    //--------------------------------------------------------------------
    // Acquire a handle to the CSP.
    if (!CryptAcquireContext(
        &hProv,
        NULL,
        MS_ENHANCED_PROV,
        PROV_RSA_FULL,
        CRYPT_VERIFYCONTEXT))
    {
        // If the key container cannot be opened, try creating a new
        // container by specifying a container name and setting the
        // CRYPT_NEWKEYSET flag.
        LOG_INFO("Error in AcquireContext 0x%08x \n", GetLastError());
        if (NTE_BAD_KEYSET == GetLastError())
        {
            if (!CryptAcquireContext(
                &hProv,
                "license++verify",
                MS_ENHANCED_PROV,
                PROV_RSA_FULL,
                CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT))
            {
                LOG_ERROR("Error in AcquireContext 0x%08x \n",
                    GetLastError());
                return FUNC_RET_ERROR;
            }
        }
        else
        {
            LOG_ERROR(" Error in AcquireContext 0x%08x \n", GetLastError());
            return FUNC_RET_ERROR;
        }
    }
    // Use the CryptImportKey function to import the PLAINTEXTKEYBLOB
    // BYTE array into the key container. The function returns a
    // pointer to an HCRYPTKEY variable that contains the handle of
    // the imported key.
    if (!CryptImportKey(hProv, &pubKey[0], sizeof(pubKey), 0, 0, &hKey))
    {
        LOG_ERROR("Error 0x%08x in importing the PublicKey \n",
            GetLastError());
        return FUNC_RET_ERROR;
    }
    if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
    {
        LOG_DEBUG("Hash object created.");
    }
    else
    {
        LOG_ERROR("Error in hash creation 0x%08x ", GetLastError());
        CryptReleaseContext(hProv,0);
        return FUNC_RET_ERROR;
    }
    if (!CryptHashData(hHash, stringToVerify, strlen(stringToVerify), 0)){
        LOG_ERROR("Error in hashing data 0x%08x ", GetLastError());
        CryptDestroyHash(hHash);
        CryptReleaseContext(hProv, 0);
        return FUNC_RET_ERROR;
    }
#ifdef _DEBUG
    LOG_DEBUG("Lenght %d, hashed Data: [%s]", strlen(stringToVerify), stringToVerify);
    printHash(&hHash);
#endif
    sigBlob = unbase64(signatureB64, strlen(signatureB64), &dwSigLen);
    LOG_DEBUG("raw signature lenght %d", dwSigLen);
    if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0))
    {
        LOG_ERROR("Signature not validated!  0x%08x ", GetLastError());
        free(sigBlob);
        CryptDestroyHash(hHash);
        CryptReleaseContext(hProv, 0);
        return FUNC_RET_ERROR;
    }
    CryptDestroyHash(hHash);
    free(sigBlob);
    CryptReleaseContext(hProv, 0);
    return FUNC_RET_OK;
}
src/library/os/win/os-win.cpp
@@ -1,17 +1,10 @@
#include <string>
#include "../os-cpp.h"
#include "../../base/public-key.h"
namespace license {
using namespace std;
bool OsFunctions::verifySignature(const char* stringToVerify,
        const char* signatureB64) {
    return false;
}
VIRTUALIZATION getVirtualization() {
src/tools/base_lib/win/CryptoHelperWindows.cpp
@@ -20,16 +20,36 @@
    CryptoHelperWindows::CryptoHelperWindows() {
        m_hCryptProv = NULL;
        m_hCryptKey = NULL;
        if (CryptAcquireContext(
        if (!CryptAcquireContext(
            &m_hCryptProv,
            "license-manager2++",
            "license++sign",
            MS_ENHANCED_PROV,
            PROV_RSA_FULL, //CRYPT_NEWKEYSET
            0))    {
        }
        else
            PROV_RSA_FULL,
            0))
        {
            throw exception("Error during CryptAcquireContext");
            // 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("");
                }
            }
            else
            {
                printf(" Error in AcquireContext 0x%08x \n", GetLastError());
                throw logic_error("");
            }
        }
    }
@@ -198,10 +218,32 @@
        return ss.str();
    }
    void CryptoHelperWindows::printHash(HCRYPTHASH* hHash) const {
        BYTE         *pbHash;
        DWORD        dwHashLen;
        DWORD        dwHashLenSize = sizeof(DWORD);
        char* hashStr;
        int i;
        if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *)&dwHashLen, &dwHashLenSize, 0))
        {
            pbHash = (BYTE*)malloc(dwHashLen);
            hashStr = (char*)malloc(dwHashLen * 2 + 1);
            if (CryptGetHashParam(*hHash, HP_HASHVAL, pbHash, &dwHashLen, 0))    {
                for (i = 0; i < dwHashLen; i++)    {
                    sprintf(&hashStr[i * 2], "%02x", pbHash[i]);
                }
                printf("hash To be signed: %s \n", hashStr);
            }
            free(pbHash);
            free(hashStr);
        }
    }
    const string CryptoHelperWindows::signString(const void* privateKey, size_t pklen,
        const string& license) const{
        BYTE *pbBuffer = (BYTE *)license.c_str();
        DWORD dwBufferLen = strlen((char *)pbBuffer) + 1;
        DWORD dwBufferLen = strlen((char *)pbBuffer);
        HCRYPTHASH hHash;
        HCRYPTKEY hKey;
@@ -215,24 +257,9 @@
        //-------------------------------------------------------------------
        // Acquire a cryptographic provider context handle.
        //-------------------------------------------------------------------
        // Get the public at signature key. This is the public key
        // that will be used by the receiver of the hash to verify
        // the signature. In situations where the receiver could obtain the
        // sender's public key from a certificate, this step would not be
        // needed.
        if (CryptGetUserKey(
            m_hCryptProv,
            AT_SIGNATURE,
            &hKey))
        if (!CryptImportKey(m_hCryptProv, (const BYTE *) privateKey, pklen, 0, 0, &hKey))
        {
            printf("The signature key has been acquired. \n");
        }
        else
        {
            printf("Error during CryptGetUserKey for signkey. %d", GetLastError());
            throw logic_error(string("Error in importing the PrivateKey ") + to_string(GetLastError()));
        }
        //-------------------------------------------------------------------
@@ -249,18 +276,18 @@
        }
        else
        {
            CryptDestroyKey(hKey);
            throw logic_error(string("Error during CryptCreateHash."));
        }
        //-------------------------------------------------------------------
        // Compute the cryptographic hash of the buffer.
        if (CryptHashData(
            hHash,
            pbBuffer,
            dwBufferLen,
            0))
        if (CryptHashData(hHash,pbBuffer,dwBufferLen,    0))
        {
            printf("The data buffer has been hashed.\n");
#ifdef _DEBUG
            printf("Length of data to be hashed: %d \n", dwBufferLen);
            printHash(&hHash);
#endif
        }
        else
        {
@@ -306,7 +333,7 @@
            pbSignature,
            &dwSigLen))
        {
            printf("pbSignature is the hash signature.\n");
            printf("pbSignature is the signature length. %d\n", dwSigLen);
        }
        else
        {
@@ -315,11 +342,9 @@
        //-------------------------------------------------------------------
        // Destroy the hash object.
        if (hHash)
            CryptDestroyHash(hHash);
        printf("The hash object has been destroyed.\n");
        printf("The signing phase of this program is completed.\n\n");
        CryptDestroyHash(hHash);
        CryptDestroyKey(hKey);
        CryptBinaryToString(pbSignature,dwSigLen,
            CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &strLen);
@@ -410,20 +435,20 @@
        //-------------------------------------------------------------------
        // Free memory to be used to store signature.
        if (pbSignature)
        free(pbSignature);
        //-------------------------------------------------------------------
        // Destroy the hash object.
        if (hHash)
        CryptDestroyHash(hHash);*/
        //-------------------------------------------------------------------
        // Release the provider handle.
        /*if (hProv)
            CryptReleaseContext(hProv, 0);*/
        if (pbSignature)
            free(pbSignature);
        return string(&buffer[0]);
    }
} /* namespace license */
src/tools/base_lib/win/CryptoHelperWindows.h
@@ -27,6 +27,7 @@
    HCRYPTPROV m_hCryptProv;
    //    Handle to the cryptography key.
    HCRYPTKEY m_hCryptKey;
    void printHash(HCRYPTHASH* hHash) const;
public:
    CryptoHelperWindows();
test/functional/date_test.cpp
@@ -1,4 +1,4 @@
#define BOOST_TEST_MODULE standard_license_test
#define BOOST_TEST_MODULE date_test
//#define BOOST_TEST_MAIN
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>