open-license-manager
2014-08-07 3894473c87783c8c50b6bf14a6a14b9a636d879a
windows dev
8个文件已修改
2个文件已添加
403 ■■■■■ 已修改文件
src/bootstrap/win/CryptoHelper.h 271 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/bootstrap/win/Main.cpp 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.cpp 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/api/license++.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/license++.cpp 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/win/CMakeLists.txt 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/win/os-win.c 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/win/os-win.cpp 47 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/pc-identifiers.c 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/bootstrap/win/CryptoHelper.h
@@ -12,269 +12,14 @@
class CryptoHelper
{
private:
       HCRYPTPROV    m_hCryptProv;
    HCRYPTPROV    m_hCryptProv;
public:
       HCRYPTKEY     m_hCryptKey;
       CryptoHelper();
       ~CryptoHelper();
       HRESULT AcquireContext(LPCTSTR wszContainerName);
       HRESULT GenerateKeyPair();
    HCRYPTKEY     m_hCryptKey;
    CryptoHelper();
    ~CryptoHelper();
    HRESULT AcquireContext(LPCTSTR wszContainerName);
    HRESULT GenerateKeyPair();
       HRESULT ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize);;
       HRESULT ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize);
    HRESULT ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize);;
    HRESULT ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize);
};
3333333//--------------------------------------------------------------------
// The destructor releases the handles acquired
// when an object goes out of scope.
//--------------------------------------------------------------------
CryptoHelper::~CryptoHelper()
{
       if (m_hCryptProv)
       {
              CryptReleaseContext(m_hCryptProv,0);
              m_hCryptProv = NULL;
       }
       if (m_hCryptKey)
              m_hCryptKey = NULL;
}
//--------------------------------------------------------------------
// This method calls the CryptAcquireContext function
// to get a handle to a key container owned by the the
// Microsoft Enhanced Cryptographic Provider.
//--------------------------------------------------------------------
HRESULT CryptoHelper::AcquireContext(LPCTSTR wszContainerName)
{
       HRESULT       hr = S_OK;
       DWORD         dwErrCode;
// Release a previously acquired handle to the key container.
if (m_hCryptProv != NULL)
       {
              CryptReleaseContext(m_hCryptProv,0);
              m_hCryptProv = NULL;
       }
// Release a previously acquired handle to key-pair.
       if (m_hCryptKey != NULL)
              m_hCryptKey = NULL;
       // Attempt to acquire a context and a key container.
// The context will use Microsoft Enhanced Cryptographic
       // Provider for the RSA_FULL provider type.
       if(!CryptAcquireContext(&m_hCryptProv,
                               wszContainerName,
                               MS_ENHANCED_PROV,
                               PROV_RSA_FULL,
    CRYPT_MACHINE_KEYSET))
       {
         // An error occurred in acquiring the context. This could mean
         // that the key container requested does not exist. In this case,
        // the function can be called again to attempt to create a new key
              // container.
              if (GetLastError() == NTE_BAD_KEYSET)
              {
                     if(!CryptAcquireContext(&m_hCryptProv,
                                            wszContainerName,
                                            MS_ENHANCED_PROV,   PROV_RSA_FULL,
                                            CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
                     {
                           dwErrCode = GetLastError();
                            return HRESULT_FROM_WIN32(dwErrCode);
                     }
              }
              else
              {
                     dwErrCode = GetLastError();
                     return HRESULT_FROM_WIN32(dwErrCode);
              }
       }
       return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptGenKey function to get a handle to an
// exportable key-pair. The key-pair is  generated with the RSA public-key
// key exchange algorithm using Microsoft Enhanced Cryptographic Provider.
//--------------------------------------------------------------------
HRESULT CryptoHelper::GenerateKeyPair()
{
       HRESULT       hr = S_OK;
       DWORD         dwErrCode;
       // If the handle to key container is NULL, fail.
if (m_hCryptProv == NULL)
              return E_FAIL;
// Release a previously acquired handle to key-pair.
if (m_hCryptKey)
              m_hCryptKey = NULL;
       // Call the CryptGenKey method to get a handle
       // to a new exportable key-pair.
if(!CryptGenKey(m_hCryptProv,
                ENCRYPT_ALGORITHM,
   KEYLENGTH | CRYPT_EXPORTABLE,
   &m_hCryptKey))
       {
              dwErrCode = GetLastError();
              return HRESULT_FROM_WIN32(dwErrCode);
       }
       return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptExportKey function to get the Public key
// in a byte array. The byte array is allocated on the heap and the size
// of this is returned to the caller. The caller is responsible for releasing // this memory using a delete call.
//--------------------------------------------------------------------
HRESULT CryptoHelper::ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize)
{
       HRESULT hr = S_OK;
       DWORD    dwErrCode;
       DWORD dwBlobLen;
       BYTE *pbKeyBlob = NULL;
       // If the handle to key container is NULL, fail.
       if (m_hCryptKey == NULL)
              return E_FAIL;
       // This call here determines the length of the key
       // blob.
       if(!CryptExportKey(
                 m_hCryptKey,
                 NULL,
                 PUBLICKEYBLOB,
                 0,
                 NULL,
                 &dwBlobLen))
       {
              dwErrCode = GetLastError();
              return HRESULT_FROM_WIN32(dwErrCode);
       }
       // Allocate memory for the pbKeyBlob.
       if((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
       {
              return E_OUTOFMEMORY;
       }
       // Do the actual exporting into the key BLOB.
       if(!CryptExportKey(
                 m_hCryptKey,
                 NULL,
                 PUBLICKEYBLOB,
                 0,
                 pbKeyBlob,
                 &dwBlobLen))
       {
              delete pbKeyBlob;
              dwErrCode = GetLastError();
              return HRESULT_FROM_WIN32(dwErrCode);
       }
       else
       {
               *ppPublicKey = pbKeyBlob;
               cbKeySize = dwBlobLen;
       }
       return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptExportKey function to get the Private key
// in a byte array. The byte array is allocated on the heap and the size
// of this is returned to the caller. The caller is responsible for releasing // this memory using a delete call.
//--------------------------------------------------------------------
HRESULT CryptoHelper::ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize)
{
       HRESULT       hr = S_OK;
       DWORD         dwErrCode;
       DWORD dwBlobLen;
       BYTE *pbKeyBlob;
       // If the handle to key container is NULL, fail.
       if (m_hCryptKey == NULL)
              return E_FAIL;
       // This call here determines the length of the key
       // blob.
       if(!CryptExportKey(
                 m_hCryptKey,
                 NULL,
                 PRIVATEKEYBLOB,
                 0,
                 NULL,
                 &dwBlobLen))
       {
              dwErrCode = GetLastError();
              return HRESULT_FROM_WIN32(dwErrCode);
       }
       // Allocate memory for the pbKeyBlob.
       if((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
       {
              return E_OUTOFMEMORY;
       }
       // Do the actual exporting into the key BLOB.
       if(!CryptExportKey(
                 m_hCryptKey,
                 NULL,
                 PRIVATEKEYBLOB,
                 0,
                 pbKeyBlob,
                 &dwBlobLen))
       {
              delete pbKeyBlob;
              dwErrCode = GetLastError();
              return HRESULT_FROM_WIN32(dwErrCode);
       }
       else
       {
               *ppPrivateKey = pbKeyBlob;
               cbKeySize = dwBlobLen;
       }
       return hr;
}
Main.cpp
 #include <stdio.h>
#include "CryptoHelper.h"
void main()
{
       CryptoHelper  cryptoHlpr;
       BYTE          *pbPublicKey = NULL, *pbPrivateKey = NULL;
       DWORD         dwPublicKeySize = 0,dwPrivateKeySize = 0;
       HRESULT       hr = S_OK;
       // Get the key container context.
       if (FAILED(hr = cryptoHlpr.AcquireContext((L"TestContainer"))))
       {
// Call FormatMessage to display the error returned in hr.
              return;
       }
        // Generate the public/private key pair.
if (FAILED(hr = cryptoHlpr.GenerateKeyPair()))
       {
// Call FormatMessage to display the error returned in hr.
              return;
       }
       // Export out the public key blob.
if (FAILED(hr = cryptoHlpr.ExportPublicKey(&pbPublicKey, dwPublicKeySize)))
       {
// Call FormatMessage to display the error returned in hr.
              return;
       }
       // Print out the public key to console as a
       // hexadecimal string.
wprintf(L"\n\nPublicKey = \"");
       for (DWORD i=0; i < dwPublicKeySize; i++)
       {
              wprintf(L"%02x",pbPublicKey[i]);
       }
       wprintf(L"\"\n");
       // Export out the private key blob.
       cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize);
       // Print out the private key to console as a
       // hexadecimal string.
       wprintf(L"\n\nPrivateKey = \"");
       for (i=0; i < dwPrivateKeySize; i++)
       {
              wprintf(L"%02x",pbPrivateKey[i]);
       }
wprintf(L"\"\n");
       // Delete the public key blob allocated by the
// ExportPublicKey method.
if (pbPublicKey)
          delete [] pbPublicKey;
       // Delete the private key blob allocated by the
// ExportPrivateKey method.
       if (pbPrivateKey)
              delete [] pbPrivateKey;
       return;
}
src/bootstrap/win/Main.cpp
@@ -1,6 +1,8 @@
#include <stdio.h>
#include "CryptoHelper.h"
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;
@@ -19,8 +21,27 @@
        fprintf(fp, "%d", pbPublicKey[i]);
    }
    fprintf(fp, "\n};\n\n");
    fprintf(fp, "#define SHARED_RANDOM %d;\n", 12345);
    int random = rand() % 1000;
    fprintf(fp, "#define SHARED_RANDOM %d;\n", random);
    fprintf(fp, "#endif\n");
    fclose(fp);
}
void write_privkey_file(string private_fname, BYTE *privateKey, DWORD dwPrivateKeySize){
    FILE* fp = fopen(private_fname.c_str(), "w");
    fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n");
    fprintf(fp, "#ifndef PRIVATE_KEY_H_\n#define PRIVATE_KEY_H_\n");
    fprintf(fp, "static BYTE PRIVATE_KEY[] = {");
    for (int i = 0; i < dwPrivateKeySize; i++){
        if (i != 0){
            fprintf(fp, ",");
        }
        if (i % 15 == 0){
            fprintf(fp, "\n        ");
        }
        fprintf(fp, "%d", privateKey[i]);
    }
    fprintf(fp, "\n};\n\n");
    fclose(fp);
}
@@ -43,6 +64,7 @@
    if (FAILED(
            hr = cryptoHlpr.ExportPublicKey(&pbPublicKey, dwPublicKeySize))) {
// Call FormatMessage to display the error returned in hr.
        cerr << "error exporting pubkey" << endl;
        return;
    }
    else {
@@ -56,7 +78,13 @@
    }
    wprintf(L"\"\n");
    // Export out the private key blob.
    cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize);
    if (FAILED(cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize))){
        cerr << "Error exporting private key." << endl;
        return;
    }
    else{
        write_privkey_file(private_include, pbPrivateKey, dwPrivateKeySize);
    }
    // Print out the private key to console as a
    // hexadecimal string.
    wprintf(L"\n\nPrivateKey = \"");
src/library/LicenseReader.cpp
@@ -28,6 +28,8 @@
namespace license {
const char *FullLicenseInfo::UNUSED_TIME = "0000-00-00";
FullLicenseInfo::FullLicenseInfo(const string& source, const string& product,
        const string& license_signature, int licenseVersion, string from_date,
        string to_date, const string& client_signature,
src/library/LicenseReader.h
@@ -35,7 +35,7 @@
    bool has_client_sig;
    string extra_data;
    static constexpr const char* UNUSED_TIME = "0000-00-00";
    static const char* UNUSED_TIME;
    static const unsigned int UNUSED_SOFTWARE_VERSION = 0;
    FullLicenseInfo(const string& source, const string& product,
src/library/api/license++.h
@@ -33,7 +33,7 @@
 * This method calculate the pc identifier. The string has to be shown
 * to the user in order to calculate the license.
 */
void identify_pc(IDENTIFICATION_STRATEGY pc_id_method,
DllExport void identify_pc(IDENTIFICATION_STRATEGY pc_id_method,
        char chbuffer[PC_IDENTIFIER_SIZE + 1]);
/*
 * The optional parameter License contains the information the program that uses the library
src/library/license++.cpp
@@ -17,8 +17,7 @@
}
DllExport void identify_pc(IDENTIFICATION_STRATEGY pc_id_method,
        char chbuffer[PC_IDENTIFIER_SIZE + 1]) {
DllExport void identify_pc(IDENTIFICATION_STRATEGY pc_id_method, char chbuffer[PC_IDENTIFIER_SIZE + 1]) {
}
src/library/os/win/CMakeLists.txt
@@ -0,0 +1,10 @@
ADD_LIBRARY(os STATIC
    os-win.c
    os-win.cpp
)
target_link_libraries(
     os
     base
     ${EXTERNAL_LIBS}
)
src/library/os/win/os-win.c
New file
@@ -0,0 +1,28 @@
#include <Windows.h>
#include"../os.h"
FUNCTION_RETURN getOsSpecificIdentifier(unsigned char identifier[6]){
}
FUNCTION_RETURN getMachineName(unsigned char identifier[6]) {
    char* buffer[MAX_COMPUTERNAME_LENGTH + 1];
    int bufsize = MAX_COMPUTERNAME_LENGTH + 1;
    BOOL cmpName = GetComputerName(
        buffer,&bufsize);
    strncpy(identifier, buffer, 6);
}
FUNCTION_RETURN getCpuId(unsigned char identifier[6]) {
}
void os_initialize() {
}
#define MAX_UNITS 20
FUNCTION_RETURN getDiskInfos(DiskInfo * diskInfos, size_t * disk_info_size) {
}
FUNCTION_RETURN getAdapterInfos(AdapterInfo * adapterInfos,
    size_t * adapter_info_size) {
}
src/library/os/win/os-win.cpp
New file
@@ -0,0 +1,47 @@
#include <string>
#include "../os-cpp.h"
#include "../../base/public-key.h"
namespace license {
using namespace std;
string OsFunctions::getModuleName() {
    char lpFilename[MAX_PATH];
    DWORD result = GetModuleFileName(NULL,lpFilename,MAX_PATH);
    return string(lpFilename);
}
bool OsFunctions::verifySignature(const char* stringToVerify,
        const char* signatureB64) {
    return false;
}
VIRTUALIZATION getVirtualization() {
//http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
//
//bool rc = true;
    /*__asm__ (
     "push   %edx\n"
     "push   %ecx\n"
     "push   %ebx\n"
     "mov    %eax, 'VMXh'\n"
     "mov    %ebx, 0\n" // any value but not the MAGIC VALUE
     "mov    %ecx, 10\n"// get VMWare version
     "mov    %edx, 'VX'\n"// port number
     "in     %eax, dx\n"// read port on return EAX returns the VERSION
     "cmp    %ebx, 'VMXh'\n"// is it a reply from VMWare?
     "setz   [rc] \n"// set return value
     "pop    %ebx \n"
     "pop    %ecx \n"
     "pop    %edx \n"
     );*/
    return NONE;
}
}
src/library/pc-identifiers.c
@@ -11,7 +11,9 @@
#include <string.h>
#include <stdbool.h>
#include "base/base64.h"
#ifdef __linux__
#include <valgrind/memcheck.h>
#endif
static FUNCTION_RETURN generate_default_pc_id(PcIdentifier * identifiers,
        unsigned int * num_identifiers) {
@@ -276,10 +278,14 @@
        free(identifiers);
        return result;
    }
#ifdef __linux__
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[0]);
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[1]);
#endif
    result = encode_pc_id(identifiers[0], identifiers[1], identifier_out);
#ifdef __linux__
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifier_out);
#endif
    free(identifiers);
    return result;
}