From 82d408374c8ece8f13b50b93ff24ab9633de14f0 Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: 周二, 05 8月 2014 04:58:22 +0800 Subject: [PATCH] windows bootstrap 1 --- src/license-generator/CMakeLists.txt | 1 src/library/os/linux/os-linux.c | 28 +- test/scratch/pc-identifiers.c | 62 ++-- CMakeLists.txt | 4 test/functional/volid_test.cpp | 6 src/bootstrap/bootstrap_linux.cpp | 1 src/library/pc-identifiers.c | 50 +- src/bootstrap/win/CryptoHelper.cpp | 215 ++++++++++++++++ src/library/base/base.h | 2 src/bootstrap/win/CmakeLists.txt | 5 src/bootstrap/win/Main.cpp | 50 +++ test/library/Os_Linux_test.cpp | 8 src/bootstrap/bootstrap_win.cpp | 33 ++ src/bootstrap/win/CryptoHelper.h | 280 +++++++++++++++++++++ src/bootstrap/CMakeLists.txt | 7 .settings/org.eclipse.ltk.core.refactoring.prefs | 2 16 files changed, 672 insertions(+), 82 deletions(-) diff --git a/.settings/org.eclipse.ltk.core.refactoring.prefs b/.settings/org.eclipse.ltk.core.refactoring.prefs new file mode 100644 index 0000000..b196c64 --- /dev/null +++ b/.settings/org.eclipse.ltk.core.refactoring.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false diff --git a/CMakeLists.txt b/CMakeLists.txt index 46a4e1f..b4a22b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,9 @@ SET(CMAKE_DISABLE_SOURCE_CHANGES OFF) SET(CMAKE_DISABLE_IN_SOURCE_BUILD ON) - SET(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "CMake verbose" FORCE) +project (license++ C CXX) SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/build/modules/") @@ -38,7 +38,7 @@ ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) endif(WIN32) -project (license++ C CXX) + if(CMAKE_SIZEOF_VOID_P EQUAL 8) SET(HAVE_64BIT_SIZE_T 1) diff --git a/src/bootstrap/CMakeLists.txt b/src/bootstrap/CMakeLists.txt index 743deeb..1925a07 100644 --- a/src/bootstrap/CMakeLists.txt +++ b/src/bootstrap/CMakeLists.txt @@ -1,9 +1,12 @@ +if(WIN32) + add_subdirectory(win) +else(WIN32) add_executable( bootstrap - bootstrap.cpp + bootstrap_linux.cpp ) - +ENDIF(WIN32) SET_TARGET_PROPERTIES(bootstrap PROPERTIES LINK_SEARCH_START_STATIC ON) SET_TARGET_PROPERTIES(bootstrap PROPERTIES LINK_SEARCH_END_STATIC OFF) diff --git a/src/bootstrap/bootstrap.cpp b/src/bootstrap/bootstrap_linux.cpp similarity index 99% rename from src/bootstrap/bootstrap.cpp rename to src/bootstrap/bootstrap_linux.cpp index 360510a..63069aa 100644 --- a/src/bootstrap/bootstrap.cpp +++ b/src/bootstrap/bootstrap_linux.cpp @@ -75,6 +75,7 @@ BIO_free_all(bio_private); free(pem_key); } + void generatePk(std::string private_fname,string public_fname) { RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0); srand(time(NULL)); /* seed random number generator */ diff --git a/src/bootstrap/bootstrap_win.cpp b/src/bootstrap/bootstrap_win.cpp new file mode 100644 index 0000000..2dc4dc5 --- /dev/null +++ b/src/bootstrap/bootstrap_win.cpp @@ -0,0 +1,33 @@ +/* + * boostrap.c + * + * Created on: Apr 5, 2014 + * Author: devel + */ + +#include <string> + + +using namespace std; + + +void print_usage() { + printf("usage: bootstrap private-fname public-fname\n"); +} + +int main(int argc, char** argv) { + + if (argc != 3) { + print_usage(); + exit(2); + } else { + printf("********************************************\n"); + printf("* Bootstrap!!! *\n"); + printf("********************************************\n"); + + } + string private_fname = string(argv[1]); + string public_fname(argv[2]); + + return 0; +} diff --git a/src/bootstrap/win/CmakeLists.txt b/src/bootstrap/win/CmakeLists.txt new file mode 100644 index 0000000..a710cb4 --- /dev/null +++ b/src/bootstrap/win/CmakeLists.txt @@ -0,0 +1,5 @@ +add_executable( + bootstrap + CryptoHelper.cpp + Main.cpp +) \ No newline at end of file diff --git a/src/bootstrap/win/CryptoHelper.cpp b/src/bootstrap/win/CryptoHelper.cpp new file mode 100644 index 0000000..27c528a --- /dev/null +++ b/src/bootstrap/win/CryptoHelper.cpp @@ -0,0 +1,215 @@ +#include "CryptoHelper.h" +// The RSA public-key key exchange algorithm +#define ENCRYPT_ALGORITHM CALG_RSA_KEYX +// The high order WORD 0x0200 (decimal 512) +// determines the key length in bits. +#define KEYLENGTH 0x02000000 +//-------------------------------------------------------------------- +// The constructor initializes the member variables +// to NULL. +//-------------------------------------------------------------------- +CryptoHelper::CryptoHelper() +{ + m_hCryptProv = NULL; + m_hCryptKey = NULL; +} +//-------------------------------------------------------------------- +// 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; +} + diff --git a/src/bootstrap/win/CryptoHelper.h b/src/bootstrap/win/CryptoHelper.h new file mode 100644 index 0000000..77fac46 --- /dev/null +++ b/src/bootstrap/win/CryptoHelper.h @@ -0,0 +1,280 @@ + +#define _WIN32_WINNT 0x0400 +#include <windows.h> +#include <wincrypt.h> +#include <tchar.h> + +// Helper class definition to generate and export Public/Private keys +// for Asymmetric encryption. The semantics for usage are: +// Call AcquireContext with a container name, call +// GenerateKeyPair next and then call ExportPublicKey or +// ExportPrivateKey. +class CryptoHelper +{ +private: + HCRYPTPROV m_hCryptProv; +public: + HCRYPTKEY m_hCryptKey; + CryptoHelper(); + ~CryptoHelper(); + HRESULT AcquireContext(LPCTSTR wszContainerName); + HRESULT GenerateKeyPair(); + + 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; +} diff --git a/src/bootstrap/win/Main.cpp b/src/bootstrap/win/Main.cpp new file mode 100644 index 0000000..5e24ca7 --- /dev/null +++ b/src/bootstrap/win/Main.cpp @@ -0,0 +1,50 @@ +#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; +} diff --git a/src/library/base/base.h b/src/library/base/base.h index 5656315..d57835e 100644 --- a/src/library/base/base.h +++ b/src/library/base/base.h @@ -32,7 +32,7 @@ _a < _b ? _a : _b; }) */ typedef enum { - OK, NOOK, ERROR, BUFFER_TOO_SMALL + FUNC_RET_OK, FUNC_RET_NOT_AVAIL, FUNC_RET_ERROR, FUNC_RET_BUFFER_TOO_SMALL } FUNCTION_RETURN; #ifdef __cplusplus diff --git a/src/library/os/linux/os-linux.c b/src/library/os/linux/os-linux.c index af828bd..f2f30b4 100644 --- a/src/library/os/linux/os-linux.c +++ b/src/library/os/linux/os-linux.c @@ -50,7 +50,7 @@ FUNCTION_RETURN getAdapterInfos(AdapterInfo * adapterInfos, size_t * adapter_info_size) { - FUNCTION_RETURN f_return = OK; + FUNCTION_RETURN f_return = FUNC_RET_OK; struct ifaddrs *ifaddr, *ifa; int family, i, n, if_name_position; unsigned int if_num, if_max; @@ -59,7 +59,7 @@ if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); - return ERROR; + return FUNC_RET_ERROR; } if (adapterInfos != NULL) { @@ -159,9 +159,9 @@ *adapter_info_size = if_num; if (adapterInfos == NULL) { - f_return = OK; + f_return = FUNC_RET_OK; } else if (*adapter_info_size < if_num) { - f_return = BUFFER_TOO_SMALL; + f_return = FUNC_RET_BUFFER_TOO_SMALL; } freeifaddrs(ifaddr); free(ifnames); @@ -238,7 +238,7 @@ aFile = setmntent("/proc/mounts", "r"); if (aFile == NULL) { /*proc not mounted*/ - return ERROR; + return FUNC_RET_ERROR; } currentDrive = 0; @@ -280,7 +280,7 @@ if (diskInfos == NULL) { *disk_info_size = currentDrive; free(tmpDrives); - result = OK; + result = FUNC_RET_OK; } else if (*disk_info_size >= currentDrive) { disk_by_uuid_dir = opendir("/dev/disk/by-uuid"); if (disk_by_uuid_dir == NULL) { @@ -288,9 +288,9 @@ printf("Open /dev/disk/by-uuid fail"); #endif free(statDrives); - return ERROR; + return FUNC_RET_ERROR; } - result = OK; + result = FUNC_RET_OK; *disk_info_size = currentDrive; while ((dir = readdir(disk_by_uuid_dir)) != NULL) { strcpy(cur_dir, "/dev/disk/by-uuid/"); @@ -334,7 +334,7 @@ closedir(disk_by_label); } } else { - result = BUFFER_TOO_SMALL; + result = FUNC_RET_BUFFER_TOO_SMALL; } /* FILE *mounts = fopen(_PATH_MOUNTED, "r"); @@ -385,7 +385,7 @@ identifier[i * 2] = cpuinfo[i] & 0xFF; identifier[i * 2 + 1] = (cpuinfo[i] & 0xFF00) >> 8; } - return OK; + return FUNC_RET_OK; } VIRTUALIZATION getVirtualization() { @@ -415,18 +415,18 @@ static struct utsname u; if (uname(&u) < 0) { - return ERROR; + return FUNC_RET_ERROR; } memcpy(identifier, u.nodename, 6); - return OK; + return FUNC_RET_OK; } FUNCTION_RETURN getOsSpecificIdentifier(unsigned char identifier[6]) { char* dbus_id = dbus_get_local_machine_id(); if (dbus_id == NULL) { - return ERROR; + return FUNC_RET_ERROR; } memcpy(identifier, dbus_id, 6); dbus_free(dbus_id); - return OK; + return FUNC_RET_OK; } diff --git a/src/library/pc-identifiers.c b/src/library/pc-identifiers.c index 891bd82..37af7d0 100644 --- a/src/library/pc-identifiers.c +++ b/src/library/pc-identifiers.c @@ -22,12 +22,12 @@ AdapterInfo *adapterInfos; result_adapterInfos = getAdapterInfos(NULL, &adapter_num); - if (result_adapterInfos != OK) { + if (result_adapterInfos != FUNC_RET_OK) { //call generate_disk_pc_id; return result_adapterInfos; } result_diskinfos = getDiskInfos(NULL, &disk_num); - if (result_diskinfos == OK) { + if (result_diskinfos == FUNC_RET_OK) { required_id_size = disk_num * adapter_num; } else { required_id_size = disk_num; @@ -35,9 +35,9 @@ int defined_identifiers = *num_identifiers; *num_identifiers = required_id_size; if (identifiers == NULL) { - return OK; + return FUNC_RET_OK; } else if (required_id_size > defined_identifiers) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); result_diskinfos = getDiskInfos(diskInfos, &disk_num); @@ -54,7 +54,7 @@ free(diskInfos); free(adapterInfos); - return OK; + return FUNC_RET_OK; } static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier * identifiers, @@ -65,16 +65,16 @@ AdapterInfo *adapterInfos; result_adapterInfos = getAdapterInfos(NULL, &adapters); - if (result_adapterInfos != OK) { + if (result_adapterInfos != FUNC_RET_OK) { return result_adapterInfos; } int defined_adapters = *num_identifiers; *num_identifiers = adapters; if (identifiers == NULL) { - return OK; + return FUNC_RET_OK; } else if (adapters > defined_adapters) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } adapterInfos = (AdapterInfo*) malloc(adapters * sizeof(AdapterInfo)); @@ -94,7 +94,7 @@ } } free(adapterInfos); - return OK; + return FUNC_RET_OK; } static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers, @@ -106,13 +106,13 @@ DiskInfo * diskInfos; result_diskinfos = getDiskInfos(NULL, &disk_num); - if (result_diskinfos != OK) { + if (result_diskinfos != FUNC_RET_OK) { return result_diskinfos; } diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); //memset(diskInfos,0,disk_num * sizeof(DiskInfo)); result_diskinfos = getDiskInfos(diskInfos, &disk_num); - if (result_diskinfos != OK) { + if (result_diskinfos != FUNC_RET_OK) { free(diskInfos); return result_diskinfos; } @@ -125,10 +125,10 @@ *num_identifiers = available_disk_info; if (identifiers == NULL) { free(diskInfos); - return OK; + return FUNC_RET_OK; } else if (available_disk_info > defined_identifiers) { free(diskInfos); - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } j=0; @@ -147,7 +147,7 @@ } } free(diskInfos); - return OK; + return FUNC_RET_OK; } /** @@ -188,10 +188,10 @@ result = generate_disk_pc_id(identifiers, array_size, true); break; default: - return ERROR; + return FUNC_RET_ERROR; } - if (result == OK && identifiers != NULL) { + if (result == FUNC_RET_OK && identifiers != NULL) { strategy_num = strategy << 5; for (i = 0; i < *array_size; i++) { //encode strategy in the first three bits of the pc_identifier @@ -243,17 +243,17 @@ char* b64_data = base64(concat_identifiers, concatIdentifiersSize, &b64_size); if (b64_size > sizeof(PcSignature)) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], &b64_data[4], &b64_data[8], &b64_data[12]); //free(concat_identifiers); free(b64_data); - return OK; + return FUNC_RET_OK; } FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) { - return OK; + return FUNC_RET_OK; } FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out, @@ -262,17 +262,17 @@ PcIdentifier* identifiers; unsigned int req_buffer_size = 0; result = generate_pc_id(NULL, &req_buffer_size, strategy); - if (result != OK) { + if (result != FUNC_RET_OK) { return result; } if (req_buffer_size == 0) { - return ERROR; + return FUNC_RET_ERROR; } req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size; identifiers = (PcIdentifier *) malloc( sizeof(PcIdentifier) * req_buffer_size); result = generate_pc_id(identifiers, &req_buffer_size, strategy); - if (result != OK) { + if (result != FUNC_RET_OK) { free(identifiers); return result; } @@ -303,13 +303,13 @@ &base64ids[8], &base64ids[12]); concat_identifiers = unbase64(base64ids, 16, &identifiers_size); if (identifiers_size > sizeof(PcIdentifier) * 2) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier)); memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier), sizeof(PcIdentifier)); free(concat_identifiers); - return OK; + return FUNC_RET_OK; } static IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) { @@ -328,7 +328,7 @@ printf("Comparing pc identifiers: \n"); #endif result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code); - if (result != OK) { + if (result != FUNC_RET_OK) { return result; } previous_strategy_id = STRATEGY_UNKNOWN; diff --git a/src/license-generator/CMakeLists.txt b/src/license-generator/CMakeLists.txt index 24b3f28..a79d82d 100644 --- a/src/license-generator/CMakeLists.txt +++ b/src/license-generator/CMakeLists.txt @@ -4,6 +4,7 @@ license-generator.cpp LicenseSigner.cpp ) + link_directories ( ${Boost_LIBRARY_DIRS} ) add_dependencies( license_generator_lib private_key ) diff --git a/test/functional/volid_test.cpp b/test/functional/volid_test.cpp index 3691e13..f31cca9 100644 --- a/test/functional/volid_test.cpp +++ b/test/functional/volid_test.cpp @@ -24,7 +24,7 @@ IDENTIFICATION_STRATEGY strategy = IDENTIFICATION_STRATEGY::ETHERNET; FUNCTION_RETURN generate_ok = generate_user_pc_signature(identifier_out, strategy); - BOOST_ASSERT(generate_ok == FUNCTION_RETURN::OK); + BOOST_ASSERT(generate_ok == FUNCTION_RETURN::FUNC_RET_OK); cout << "Identifier:" << identifier_out << endl; vector<string> extraArgs = { "-s", identifier_out }; generate_license(licLocation, extraArgs); @@ -47,7 +47,7 @@ for (int i = 0; i < num_strategies; i++) { FUNCTION_RETURN generate_ok = generate_user_pc_signature(identifier_out, strategies[i]); - BOOST_ASSERT(generate_ok == FUNCTION_RETURN::OK); + BOOST_ASSERT(generate_ok == FUNCTION_RETURN::FUNC_RET_OK); idfile << identifier_out << endl; } idfile.close(); @@ -74,7 +74,7 @@ for (int i = 0; i < num_strategies; i++) { FUNCTION_RETURN generate_ok = generate_user_pc_signature( generated_identifier, strategies[i]); - BOOST_ASSERT(generate_ok == FUNCTION_RETURN::OK); + BOOST_ASSERT(generate_ok == FUNCTION_RETURN::FUNC_RET_OK); if (reference_signatures[i] != generated_identifier) { string message = string("pc signature compare fail: strategy:") + to_string(strategies[i]) + " generated: [" diff --git a/test/library/Os_Linux_test.cpp b/test/library/Os_Linux_test.cpp index 26dcb76..4bda9c4 100644 --- a/test/library/Os_Linux_test.cpp +++ b/test/library/Os_Linux_test.cpp @@ -13,11 +13,11 @@ DiskInfo * diskInfos = NULL; size_t disk_info_size = 0; FUNCTION_RETURN result = getDiskInfos(NULL, &disk_info_size); - BOOST_CHECK_EQUAL(result, OK); + BOOST_CHECK_EQUAL(result, FUNC_RET_OK); BOOST_CHECK_GT(disk_info_size, 0); diskInfos = (DiskInfo*) malloc(sizeof(DiskInfo) * disk_info_size); result = getDiskInfos(diskInfos, &disk_info_size); - BOOST_CHECK_EQUAL(result, OK); + BOOST_CHECK_EQUAL(result, FUNC_RET_OK); BOOST_CHECK_GT(strlen(diskInfos[0].device), 0); BOOST_CHECK_GT(strlen(diskInfos[0].label), 0); BOOST_CHECK_GT(diskInfos[0].disk_sn[0], 0); @@ -27,12 +27,12 @@ AdapterInfo * adapter_info = NULL; size_t adapter_info_size = 0; FUNCTION_RETURN result = getAdapterInfos(NULL, &adapter_info_size); - BOOST_CHECK_EQUAL(result, OK); + BOOST_CHECK_EQUAL(result, FUNC_RET_OK); BOOST_CHECK_GT(adapter_info_size, 0); adapter_info = (AdapterInfo*) malloc( sizeof(AdapterInfo) * adapter_info_size); result = getAdapterInfos(adapter_info, &adapter_info_size); - BOOST_CHECK_EQUAL(result, OK); + BOOST_CHECK_EQUAL(result, FUNC_RET_OK); for (size_t i = 0; i < adapter_info_size; i++) { cout << "Interface found: " << string(adapter_info[i].description) << endl; diff --git a/test/scratch/pc-identifiers.c b/test/scratch/pc-identifiers.c index 07a362a..a0c38d2 100644 --- a/test/scratch/pc-identifiers.c +++ b/test/scratch/pc-identifiers.c @@ -32,33 +32,33 @@ //just calculate the number of required identifiers result_plat_spec = generate_platform_specific_pc_id(NULL, &plat_spec_id); - if (result_plat_spec == OK) { + if (result_plat_spec == FUNC_RET_OK) { required_id_size += 1; } result_adapterInfos = getAdapterInfos(NULL, &adapter_num); result_diskinfos = getDiskInfos(NULL, &disk_num); - if (result_diskinfos == OK && result_adapterInfos == OK) { + if (result_diskinfos == FUNC_RET_OK && result_adapterInfos == FUNC_RET_OK) { required_id_size += disk_num * adapter_num; - } else if (result_adapterInfos == OK) { + } else if (result_adapterInfos == FUNC_RET_OK) { required_id_size += adapter_num; - } else if (result_diskinfos == OK) { + } else if (result_diskinfos == FUNC_RET_OK) { required_id_size += disk_num; } int defined_identifiers = *num_identifiers; if (identifiers == NULL) { *num_identifiers = required_id_size; - return OK; + return FUNC_RET_OK; } else if (required_id_size > defined_identifiers) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } //calculate the identifiers current_identifier = 0; - if (result_plat_spec == OK) { + if (result_plat_spec == FUNC_RET_OK) { generate_platform_specific_pc_id(identifiers, 1); current_identifier += 1; } - if (result_diskinfos == OK && result_adapterInfos == OK) { + if (result_diskinfos == FUNC_RET_OK && result_adapterInfos == FUNC_RET_OK) { diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); result_diskinfos = getDiskInfos(diskInfos, &disk_num); adapterInfos = (AdapterInfo*) malloc(adapter_num * sizeof(AdapterInfo)); @@ -78,17 +78,17 @@ } free(diskInfos); free(adapterInfos); - } else if (result_adapterInfos == OK) { + } else if (result_adapterInfos == FUNC_RET_OK) { i=defined_identifiers-current_identifier; return generate_ethernet_pc_id(&identifiers[current_identifier], &i, true); - } else if (result_diskinfos == OK) { + } else if (result_diskinfos == FUNC_RET_OK) { i=defined_identifiers-current_identifier; return generate_disk_pc_id(&identifiers[current_identifier], &i, false); } - return OK; + return FUNC_RET_OK; } static void encodeStrategy(PcIdentifier * identifier, @@ -113,16 +113,16 @@ AdapterInfo *adapterInfos; result_adapterInfos = getAdapterInfos(NULL, &adapters); - if (result_adapterInfos != OK) { + if (result_adapterInfos != FUNC_RET_OK) { return result_adapterInfos; } int defined_adapters = *num_identifiers; *num_identifiers = adapters; if (identifiers == NULL) { - return OK; + return FUNC_RET_OK; } else if (adapters > defined_adapters) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } adapterInfos = (AdapterInfo*) malloc(adapters * sizeof(AdapterInfo)); @@ -142,7 +142,7 @@ } } free(adapterInfos); - return OK; + return FUNC_RET_OK; } static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers, @@ -154,13 +154,13 @@ DiskInfo * diskInfos; result_diskinfos = getDiskInfos(NULL, &disk_num); - if (result_diskinfos != OK) { + if (result_diskinfos != FUNC_RET_OK) { return result_diskinfos; } diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); //memset(diskInfos,0,disk_num * sizeof(DiskInfo)); result_diskinfos = getDiskInfos(diskInfos, &disk_num); - if (result_diskinfos != OK) { + if (result_diskinfos != FUNC_RET_OK) { free(diskInfos); return result_diskinfos; } @@ -173,10 +173,10 @@ *num_identifiers = available_disk_info; if (identifiers == NULL) { free(diskInfos); - return OK; + return FUNC_RET_OK; } else if (available_disk_info > defined_identifiers) { free(diskInfos); - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } j = 0; @@ -197,7 +197,7 @@ } } free(diskInfos); - return OK; + return FUNC_RET_OK; } /** @@ -238,10 +238,10 @@ result = generate_platform_specific_pc_id(identifiers, array_size); break; default: - return ERROR; + return FUNC_RET_ERROR; } - if (result == OK && identifiers != NULL) { + if (result == FUNC_RET_OK && identifiers != NULL) { //fill array if larger for (i = *array_size; i < original_array_size; i++) { identifiers[i][0] = STRATEGY_UNKNOWN << 5; @@ -288,17 +288,17 @@ char* b64_data = base64(concat_identifiers, concatIdentifiersSize, &b64_size); if (b64_size > sizeof(PcSignature)) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], &b64_data[4], &b64_data[8], &b64_data[12]); //free(concat_identifiers); free(b64_data); - return OK; + return FUNC_RET_OK; } FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) { - return OK; + return FUNC_RET_OK; } FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out, @@ -307,17 +307,17 @@ PcIdentifier* identifiers; unsigned int req_buffer_size = 0; result = generate_pc_id(NULL, &req_buffer_size, strategy); - if (result != OK) { + if (result != FUNC_RET_OK) { return result; } if (req_buffer_size == 0) { - return ERROR; + return FUNC_RET_ERROR; } req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size; identifiers = (PcIdentifier *) malloc( sizeof(PcIdentifier) * req_buffer_size); result = generate_pc_id(identifiers, &req_buffer_size, strategy); - if (result != OK) { + if (result != FUNC_RET_OK) { free(identifiers); return result; } @@ -348,13 +348,13 @@ &base64ids[8], &base64ids[12]); concat_identifiers = unbase64(base64ids, 16, &identifiers_size); if (identifiers_size > sizeof(PcIdentifier) * 2) { - return BUFFER_TOO_SMALL; + return FUNC_RET_BUFFER_TOO_SMALL; } memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier)); memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier), sizeof(PcIdentifier)); free(concat_identifiers); - return OK; + return FUNC_RET_OK; } static IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) { @@ -373,7 +373,7 @@ printf("Comparing pc identifiers: \n"); #endif result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code); - if (result != OK) { + if (result != FUNC_RET_OK) { return result; } previous_strategy_id = STRATEGY_UNKNOWN; -- Gitblit v1.9.1