From 345c7b764f0721e9bbc993a3cd7f803fd42cf943 Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: 周一, 15 9月 2014 06:58:38 +0800 Subject: [PATCH] moving files for refactoring --- src/library/pc-identifiers.h | 2 src/tools/base_lib/CMakeLists.txt | 19 + src/tools/license-generator/license-generator.cpp | 0 test/functional/generate-license.cpp | 2 src/library/os/os.h | 2 src/tools/bootstrap/Main.cpp | 92 +++++++ test/scratch/pc-identifiers.c | 2 CMakeLists.txt | 1 src/library/LicenseReader.h | 2 src/library/pc-identifiers.c | 2 src/library/os/os-cpp.h | 2 src/library/base/EventRegistry.h | 2 src/tools/CMakeLists.txt | 7 src/tools/license-generator/LicenseSigner.h | 2 src/tools/base_lib/CryptoHelper.cpp | 20 + src/tools/pc-identifier/CMakeLists.txt | 0 src/tools/base_lib/win/CryptoHelper.cpp | 2 src/tools/license-generator/license-generator.h | 2 src/library/base/StringUtils.cpp | 2 src/tools/bootstrap/bootstrap_linux.cpp | 4 src/tools/base_lib/README.TXT | 3 src/library/base/EventRegistry.cpp | 2 src/tools/license-generator/license-generator-main.cpp | 0 src/library/os/linux/os-linux.c | 4 src/tools/base_lib/linux/CryptoHelperLinux.h | 37 +++ src/tools/license-generator/CMakeLists.txt | 12 /dev/null | 125 ---------- src/tools/bootstrap/CMakeLists.txt | 32 ++ src/tools/license-generator/linux/LicenseSigner.cpp | 2 src/library/base/StringUtils.h | 2 src/tools/base_lib/linux/CryptoHelperLinux.cpp | 193 ++++++++++++++++ src/tools/bootstrap/win/CmakeLists.txt | 0 src/library/LicenseReader.cpp | 2 src/tools/base_lib/win/CryptoHelperWindows.h | 27 ++ src/CMakeLists.txt | 4 src/tools/license-generator/win/LicenseSigner.cpp | 2 src/tools/base_lib/CryptoHelper.h | 36 +++ src/tools/base_lib/win/CryptoHelperWindows.cpp | 26 ++ src/tools/license-generator/.gitignore | 0 39 files changed, 516 insertions(+), 160 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01cdfd4..ae9dad4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ SET(LICENSECC_VERSION "${LICENSECC_MAJOR_VERSION}.${LICENSECC_MINOR_VERSION}.${LICENSECC_PATCH_VERSION}") SET(LICENSECC_SHORT_LICENSE "BSD Software License") -# add this options before PROJECT keyword SET(CMAKE_DISABLE_SOURCE_CHANGES OFF) SET(CMAKE_DISABLE_IN_SOURCE_BUILD ON) SET(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "CMake verbose" FORCE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7fb5c49..1fb9b67 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,4 @@ -add_subdirectory("bootstrap") add_subdirectory("library") -add_subdirectory("pc-identifier") -add_subdirectory("license-generator") +add_subdirectory("tools") \ No newline at end of file diff --git a/src/bootstrap/CMakeLists.txt b/src/bootstrap/CMakeLists.txt deleted file mode 100644 index 1925a07..0000000 --- a/src/bootstrap/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ - -if(WIN32) - add_subdirectory(win) -else(WIN32) -add_executable( - bootstrap - 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) -target_link_libraries( - bootstrap - ${EXTERNAL_LIBS} -) - -# add the command to generate the source code -add_custom_command ( - OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/../license-generator/private-key.h" "${CMAKE_CURRENT_SOURCE_DIR}/../library/base/public-key.h" - COMMAND bootstrap "${CMAKE_CURRENT_SOURCE_DIR}/../license-generator/private-key.h" "${CMAKE_CURRENT_SOURCE_DIR}/../library/base/public-key.h" - DEPENDS bootstrap -) - -add_custom_target(private_key DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../license-generator/private-key.h") -add_custom_target(public_key DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../library/base/public-key.h") - - -# add the command to generate the source code -#add_custom_command ( -# OUTPUT "${CMAKE_BINARY_DIR}/public-key.h" -# COMMAND bootstrap derive_public "${CMAKE_CURRENT_SOURCE_DIR}/../license-generator/private-key.h" "${CMAKE_BINARY_DIR}/public-key.h" -# DEPENDS bootstrap private_key -#) -#add_custom_target(public_key DEPENDS "${CMAKE_BINARY_DIR}/public-key.h") diff --git a/src/bootstrap/win/CryptoHelper.h b/src/bootstrap/win/CryptoHelper.h deleted file mode 100644 index 9d69fb2..0000000 --- a/src/bootstrap/win/CryptoHelper.h +++ /dev/null @@ -1,25 +0,0 @@ - -#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); -}; diff --git a/src/bootstrap/win/Main.cpp b/src/bootstrap/win/Main.cpp deleted file mode 100644 index c910a2f..0000000 --- a/src/bootstrap/win/Main.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include <stdio.h> -#include "CryptoHelper.h" -#include <string> -#include <stdlib.h> -#include <iostream> - -using namespace std; - -void write_pubkey_file(string public_fname, BYTE *pbPublicKey, DWORD dwPublicKeySize){ - FILE* fp = fopen(public_fname.c_str(), "w"); - fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n"); - fprintf(fp, "#ifndef PUBLIC_KEY_H_\n#define PUBLIC_KEY_H_\n"); - fprintf(fp, "static BYTE PUBLIC_KEY[] = {"); - for (int i = 0; i < dwPublicKeySize; i++){ - if (i != 0){ - fprintf(fp, ","); - } - if (i % 15 == 0){ - fprintf(fp, "\n "); - } - fprintf(fp, "%d", pbPublicKey[i]); - } - fprintf(fp, "\n};\n\n"); - 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"); - fprintf(fp, "#endif\n"); - fclose(fp); -} - -void generatePk(string private_include, string public_include) { - 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(_T("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. - cerr << "error exporting pubkey" << endl; - return; - } - else { - write_pubkey_file(public_include, pbPublicKey, dwPublicKeySize); - } - // 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. - 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 = \""); - for (DWORD 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; -} - - -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]); - - generatePk(private_fname, public_fname); - return 0; -} diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp index 4beb067..3f49c19 100644 --- a/src/library/LicenseReader.cpp +++ b/src/library/LicenseReader.cpp @@ -2,7 +2,7 @@ * LicenseReader.cpp * * Created on: Mar 30, 2014 - * Author: devel + * */ #ifdef _WIN32 diff --git a/src/library/LicenseReader.h b/src/library/LicenseReader.h index 2d45e05..0d70f2f 100644 --- a/src/library/LicenseReader.h +++ b/src/library/LicenseReader.h @@ -2,7 +2,7 @@ * LicenseReader.h * * Created on: Mar 30, 2014 - * Author: devel + * */ #ifndef LICENSEREADER_H_ diff --git a/src/library/base/EventRegistry.cpp b/src/library/base/EventRegistry.cpp index beefa29..f24275f 100644 --- a/src/library/base/EventRegistry.cpp +++ b/src/library/base/EventRegistry.cpp @@ -2,7 +2,7 @@ * EventRegistry.cpp * * Created on: Mar 30, 2014 - * Author: devel + * */ #include "EventRegistry.h" diff --git a/src/library/base/EventRegistry.h b/src/library/base/EventRegistry.h index 310a430..d86ea77 100644 --- a/src/library/base/EventRegistry.h +++ b/src/library/base/EventRegistry.h @@ -2,7 +2,7 @@ * EventRegistry.h * * Created on: Mar 30, 2014 - * Author: devel + * */ #ifndef EVENTREGISTRY_H_ diff --git a/src/library/base/StringUtils.cpp b/src/library/base/StringUtils.cpp index 33e055a..3d09b99 100644 --- a/src/library/base/StringUtils.cpp +++ b/src/library/base/StringUtils.cpp @@ -2,7 +2,7 @@ * StringUtils.cpp * * Created on: Apr 8, 2014 - * Author: devel + * */ #include <cctype> //toupper diff --git a/src/library/base/StringUtils.h b/src/library/base/StringUtils.h index a7d3cbe..bbb9ce3 100644 --- a/src/library/base/StringUtils.h +++ b/src/library/base/StringUtils.h @@ -2,7 +2,7 @@ * StringUtils.h * * Created on: Apr 8, 2014 - * Author: devel + * */ #ifndef STRINGUTILS_H_ diff --git a/src/library/os/linux/os-linux.c b/src/library/os/linux/os-linux.c index 8996009..c394891 100644 --- a/src/library/os/linux/os-linux.c +++ b/src/library/os/linux/os-linux.c @@ -48,7 +48,7 @@ } -FUNCTION_RETURN getAdapterInfos(AdapterInfo * adapterInfos, +FUNCTION_RETURN getAdapterInfos(OsAdapterInfo * adapterInfos, size_t * adapter_info_size) { FUNCTION_RETURN f_return = FUNC_RET_OK; @@ -64,7 +64,7 @@ } if (adapterInfos != NULL) { - memset(adapterInfos, 0, (*adapter_info_size) * sizeof(AdapterInfo)); + memset(adapterInfos, 0, (*adapter_info_size) * sizeof(OsAdapterInfo)); } /* count the maximum number of interfaces */ diff --git a/src/library/os/os-cpp.h b/src/library/os/os-cpp.h index bf43d3a..75e1132 100644 --- a/src/library/os/os-cpp.h +++ b/src/library/os/os-cpp.h @@ -2,7 +2,7 @@ * os-dependent.hpp * * Created on: Mar 29, 2014 - * Author: devel + * */ #ifndef OS_DEPENDENT_CPP_ diff --git a/src/library/os/os.h b/src/library/os/os.h index 9947378..741efe5 100644 --- a/src/library/os/os.h +++ b/src/library/os/os.h @@ -2,7 +2,7 @@ * os-dependent.hpp * * Created on: Mar 29, 2014 - * Author: devel + * */ #ifndef OS_DEPENDENT_HPP_ diff --git a/src/library/pc-identifiers.c b/src/library/pc-identifiers.c index 9cdb35e..75d70fe 100644 --- a/src/library/pc-identifiers.c +++ b/src/library/pc-identifiers.c @@ -2,7 +2,7 @@ * pc-identifiers.c * * Created on: Apr 16, 2014 - * Author: devel + * */ #include "os/os.h" diff --git a/src/library/pc-identifiers.h b/src/library/pc-identifiers.h index 1492395..2d36da8 100644 --- a/src/library/pc-identifiers.h +++ b/src/library/pc-identifiers.h @@ -2,7 +2,7 @@ * pc-identifiers.h * * Created on: Apr 16, 2014 - * Author: devel + * */ #ifndef PC_IDENTIFIERS_H_ diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt new file mode 100644 index 0000000..34da47f --- /dev/null +++ b/src/tools/CMakeLists.txt @@ -0,0 +1,7 @@ +enable_language(CXX) +add_subdirectory("base_lib") +add_subdirectory("bootstrap") +add_subdirectory("pc-identifier") +add_subdirectory("license-generator") + + \ No newline at end of file diff --git a/src/tools/base_lib/CMakeLists.txt b/src/tools/base_lib/CMakeLists.txt new file mode 100644 index 0000000..157964d --- /dev/null +++ b/src/tools/base_lib/CMakeLists.txt @@ -0,0 +1,19 @@ + +if(WIN32) +ADD_LIBRARY( + tools_base STATIC + CryptoHelper.cpp + win/CryptoHelperWindows.cpp +) +else(WIN32) +ADD_LIBRARY( + tools_base STATIC + CryptoHelper.cpp + linux/CryptoHelperLinux.cpp +) +ENDIF(WIN32) + +target_link_libraries( + tools_base + ${EXTERNAL_LIBS} +) diff --git a/src/tools/base_lib/CryptoHelper.cpp b/src/tools/base_lib/CryptoHelper.cpp new file mode 100644 index 0000000..baa7e8f --- /dev/null +++ b/src/tools/base_lib/CryptoHelper.cpp @@ -0,0 +1,20 @@ +#include <memory> +#include "CryptoHelper.h" +#ifdef __unix__ +#include"linux/CryptoHelperLinux.h" +#else +#include"win/CryptoHelperWindows.h" +#endif + +using namespace std; +namespace license { + +unique_ptr<CryptoHelper> CryptoHelper::getInstance() { +#ifdef __unix__ + unique_ptr<CryptoHelper> ptr((CryptoHelper*) new CryptoHelperLinux()); +#else + unique_ptr<CryptoHelper> ptr((CryptoHelper*) new CryptpHelperWindows()); +#endif + return ptr; +} +} diff --git a/src/tools/base_lib/CryptoHelper.h b/src/tools/base_lib/CryptoHelper.h new file mode 100644 index 0000000..cce02cc --- /dev/null +++ b/src/tools/base_lib/CryptoHelper.h @@ -0,0 +1,36 @@ +#ifndef CRYPTPHELPER_H_ +#define CRYPTPHELPER_H_ + +#include <memory> +#include <cstddef> + +namespace license { +using namespace std; +/** + * Helper class definition to generate and export Public/Private keys + * for Asymmetric encryption. + * + * <p>Since this part relies heavily on operating system libraries this class + * provides a common facade to the cryptographic functions. The two implementing + * subclasses are chosen in the factory method #getInstance(). This is to avoid + * to clutter the code with many "ifdef". (extreme performance is not an issue here)</p> + *<p> *it is shared by bootstrap and license-generator projects.</p> + */ + +class CryptoHelper { + +protected: + CryptoHelper(); + +public: + virtual void generateKeyPair() = 0; + virtual const string exportPrivateKey() const = 0; + virtual const string exportPublicKey() const = 0; + + virtual const string signString(const unsigned char* privateKey, + size_t pklen, const string& license) const = 0; + static unique_ptr<CryptoHelper> getInstance(); + virtual ~CryptoHelper(); +}; +} +#endif diff --git a/src/tools/base_lib/README.TXT b/src/tools/base_lib/README.TXT new file mode 100644 index 0000000..838c5e9 --- /dev/null +++ b/src/tools/base_lib/README.TXT @@ -0,0 +1,3 @@ +This is the base library for the tools projects. +It contains cryptographic functions (that are very different from Linux and +Windows operating systems) \ No newline at end of file diff --git a/src/tools/base_lib/linux/CryptoHelperLinux.cpp b/src/tools/base_lib/linux/CryptoHelperLinux.cpp new file mode 100644 index 0000000..ba094f5 --- /dev/null +++ b/src/tools/base_lib/linux/CryptoHelperLinux.cpp @@ -0,0 +1,193 @@ +/* + * CryptpHelperLinux.cpp + * + * Created on: Sep 14, 2014 + * + */ + +#include "CryptoHelperLinux.h" +#include <openssl/evp.h> +#include <openssl/bio.h> +#include <openssl/pem.h> +#include <openssl/err.h> +#include <stdexcept> +#include <string> +#include <cstddef> +#include <stdexcept> + +namespace license { +using namespace std; + +static std::string replaceAll(std::string subject, const std::string& search, + const std::string& replace) { + size_t pos = 0; + while ((pos = subject.find(search, pos)) != std::string::npos) { + subject.replace(pos, search.length(), replace); + pos += replace.length(); + } + return subject; +} + + +CryptoHelperLinux::CryptoHelperLinux() { + static int initialized = 0; + rsa = NULL; + if (initialized == 0) { + initialized = 1; + ERR_load_ERR_strings(); + ERR_load_crypto_strings(); + OpenSSL_add_all_algorithms(); + } + +} +void CryptoHelperLinux::generateKeyPair() { + srand(time(NULL)); /* seed random number generator */ + int random = rand(); + rsa = RSA_generate_key(kBits, kExp, 0, 0); +} + +const string CryptoHelperLinux::exportPrivateKey() const { + if (rsa == NULL) { + throw logic_error(string("Export not initialized.Call generateKeyPair first.")); + } + BIO* bio_private = BIO_new(BIO_s_mem()); + PEM_write_bio_RSAPrivateKey(bio_private, rsa, NULL, NULL, 0, NULL, NULL); + int keylen = BIO_pending(bio_private); + char* pem_key = (char*) (calloc(keylen + 1, 1)); /* Null-terminate */ + BIO_read(bio_private, pem_key, keylen); + string dest = replaceAll(string(pem_key), string("\n"), + string("\\n\" \\\n\"")); + BIO_free_all(bio_private); + free(pem_key); + return dest; +} + +const string CryptoHelperLinux::exportPublicKey() const { + if (rsa == NULL) { + throw logic_error(string("Export not initialized.Call generateKeyPair first.")); + } + BIO* bio_private = BIO_new(BIO_s_mem()); + PEM_write_bio_RSAPrivateKey(bio_private, rsa, NULL, NULL, 0, NULL, NULL); + int keylen = BIO_pending(bio_private); + char* pem_key = (char*) (calloc(keylen + 1, 1)); /* Null-terminate */ + BIO_read(bio_private, pem_key, keylen); + std::string dest = replaceAll(string(pem_key), string("\n"), + string("\\n\" \\\n\"")); + BIO_free_all(bio_private); + free(pem_key); + return dest; +} + +string CryptoHelperLinux::signString(const unsigned char* privateKey, size_t pklen, + const string& license) const { + size_t slen; + unsigned char* signature; + signature = NULL; + /* Create the Message Digest Context */ + EVP_MD_CTX* mdctx = EVP_MD_CTX_create(); + if (!mdctx) { + throw logic_error("Message digest creation context"); + } + + + BIO* bio = BIO_new_mem_buf((void*) (privateKey), pklen); + EVP_PKEY *pktmp = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); + BIO_free(bio); + /*Initialise the DigestSign operation - SHA-256 has been selected + * as the message digest function in this example */ + if (1 != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pktmp)) { + EVP_MD_CTX_destroy(mdctx); + } + /* Call update with the message */ + if (EVP_DigestSignUpdate(mdctx, (const void*) license.c_str(), (size_t) license.length()) != 1) { + EVP_MD_CTX_destroy(mdctx); + throw logic_error("Message signing exception"); + } + /* Finalise the DigestSign operation */ + /* First call EVP_DigestSignFinal with a NULL sig parameter to obtain the length of the + * signature. Length is returned in slen */ + if (EVP_DigestSignFinal(mdctx, NULL, &slen) != 1) { + EVP_MD_CTX_destroy(mdctx); + throw logic_error("Message signature finalization exception"); + } + /* Allocate memory for the signature based on size in slen */ + if (!(signature = (unsigned char *) OPENSSL_malloc( + sizeof(unsigned char) * slen))) { + EVP_MD_CTX_destroy(mdctx); + throw logic_error("Message signature memory allocation exception"); + } + /* Obtain the signature */ + if (1 != EVP_DigestSignFinal(mdctx, signature, &slen)) { + OPENSSL_free(signature); + EVP_MD_CTX_destroy(mdctx); + throw logic_error("Message signature exception"); + } + /* + FILE* stream = fmemopen(*buffer, encodedSize+1, "w"); + */ + //bio = BIO_new_fp(stdout, BIO_NOCLOSE); + /*int encodedSize = 4 * ceil(slen / 3); + char* buffer = (char*) (malloc(encodedSize + 1)); + memset(buffer,0,encodedSize+1);*/ + string signatureStr = Opensslb64Encode(slen, signature); + /* + * BIO *bio, *b64; + char message[] = "Hello World \n"; + b64 = BIO_new(BIO_f_base64()); + bio = BIO_new_fp(stdout, BIO_NOCLOSE); + bio = BIO_push(b64, bio); + BIO_write(bio, message, strlen(message)); + BIO_flush(bio); + BIO_free_all(bio); + Read Base64 encoded data from standard input and write the decoded data to standard output: + + BIO *bio, *b64, *bio_out; + char inbuf[512]; + int inlen; + b64 = BIO_new(BIO_f_base64()); + bio = BIO_new_fp(stdin, BIO_NOCLOSE); + bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); + bio = BIO_push(b64, bio); + while((inlen = BIO_read(bio, inbuf, 512)) > 0) + BIO_write(bio_out, inbuf, inlen); + BIO_free_all(bio); + */ + /* Clean up */ + //free(buffer); + if (pktmp) + EVP_PKEY_free(pktmp); + if (signature) + OPENSSL_free(signature); + + if (mdctx) + EVP_MD_CTX_destroy(mdctx); + return signatureStr; +} + +const string CryptoHelperLinux::Opensslb64Encode(size_t slen, unsigned char* signature) const{ + /* + FILE* stream = fmemopen(*buffer, encodedSize+1, "w"); + */ + //bio = BIO_new_fp(stdout, BIO_NOCLOSE); + /*int encodedSize = 4 * ceil(slen / 3); + char* buffer = (char*) (malloc(encodedSize + 1)); + memset(buffer,0,encodedSize+1);*/ + BIO* mem_bio = BIO_new(BIO_s_mem()); + BIO* b64 = BIO_new(BIO_f_base64()); + BIO* bio1 = BIO_push(b64, mem_bio); + BIO_set_flags(bio1, BIO_FLAGS_BASE64_NO_NL); + BIO_write(bio1, signature, slen); + BIO_flush(bio1); + char* charBuf; + int sz = BIO_get_mem_data(mem_bio, &charBuf); + string signatureStr; + signatureStr.assign(charBuf, sz); + BIO_free_all(bio1); + return signatureStr; +} + +CryptoHelperLinux::~CryptoHelperLinux() { + RSA_free(rsa); +} + +} /* namespace license */ diff --git a/src/tools/base_lib/linux/CryptoHelperLinux.h b/src/tools/base_lib/linux/CryptoHelperLinux.h new file mode 100644 index 0000000..2573ca3 --- /dev/null +++ b/src/tools/base_lib/linux/CryptoHelperLinux.h @@ -0,0 +1,37 @@ +/* + * CryptpHelperLinux.h + * + * Created on: Sep 14, 2014 + * + */ + +#ifndef CRYPTPHELPERLINUX_H_ +#define CRYPTPHELPERLINUX_H_ +#include <openssl/rsa.h> +#include <cstddef> +#include <string> + +namespace license { +using namespace std; + +class CryptoHelperLinux { +private: + static const int kBits = 1024; + static const int kExp = 65537; + RSA * rsa; + const string Opensslb64Encode(size_t slen, unsigned char* signature) const; +public: + CryptoHelperLinux(); + + virtual void generateKeyPair(); + virtual const string exportPrivateKey() const; + virtual const string exportPublicKey() const; + + virtual string signString(const unsigned char* privateKey, size_t pklen, + const string& license) const; + virtual ~CryptoHelperLinux(); +}; + +} /* namespace license */ + +#endif /* CRYPTPHELPERLINUX_H_ */ diff --git a/src/bootstrap/win/CryptoHelper.cpp b/src/tools/base_lib/win/CryptoHelper.cpp similarity index 99% rename from src/bootstrap/win/CryptoHelper.cpp rename to src/tools/base_lib/win/CryptoHelper.cpp index 50f337b..44a06fe 100644 --- a/src/bootstrap/win/CryptoHelper.cpp +++ b/src/tools/base_lib/win/CryptoHelper.cpp @@ -1,4 +1,4 @@ -#include "CryptoHelper.h" +#include "../CryptoHelper.h" // The RSA public-key key exchange algorithm #define ENCRYPT_ALGORITHM CALG_RSA_SIGN // The high order WORD 0x0200 (decimal 512) diff --git a/src/tools/base_lib/win/CryptoHelperWindows.cpp b/src/tools/base_lib/win/CryptoHelperWindows.cpp new file mode 100644 index 0000000..1b7b40e --- /dev/null +++ b/src/tools/base_lib/win/CryptoHelperWindows.cpp @@ -0,0 +1,26 @@ +/* + * CryptoHelperWindows.cpp + * + * Created on: Sep 14, 2014 + * + */ + +#include "CryptoHelperWindows.h" +// The RSA public-key key exchange algorithm +#define ENCRYPT_ALGORITHM CALG_RSA_SIGN +// The high order WORD 0x0200 (decimal 512) +// determines the key length in bits. +#define KEYLENGTH 0x02000000 + +namespace license { + +CryptoHelperWindows::CryptoHelperWindows() { + // TODO Auto-generated constructor stub + +} + +CryptoHelperWindows::~CryptoHelperWindows() { + // TODO Auto-generated destructor stub +} + +} /* namespace license */ diff --git a/src/tools/base_lib/win/CryptoHelperWindows.h b/src/tools/base_lib/win/CryptoHelperWindows.h new file mode 100644 index 0000000..83ac21c --- /dev/null +++ b/src/tools/base_lib/win/CryptoHelperWindows.h @@ -0,0 +1,27 @@ +/* + * CryptoHelperWindows.h + * + * Created on: Sep 14, 2014 + * + */ + +#ifndef CRYPTOHELPERWINDOWS_H_ +#define CRYPTOHELPERWINDOWS_H_ + +#define _WIN32_WINNT 0x0400 +#include <windows.h> +#include <wincrypt.h> +#include <tchar.h> +#include "../CryptoHelper.h" + +namespace license { + +class CryptoHelperWindows: public CryptoHelper { +public: + CryptoHelperWindows(); + virtual ~CryptoHelperWindows(); +}; + +} /* namespace license */ + +#endif /* CRYPTOHELPERWINDOWS_H_ */ diff --git a/src/tools/bootstrap/CMakeLists.txt b/src/tools/bootstrap/CMakeLists.txt new file mode 100644 index 0000000..8ea8f08 --- /dev/null +++ b/src/tools/bootstrap/CMakeLists.txt @@ -0,0 +1,32 @@ +add_executable( + bootstrap + Main.cpp +) + + +SET_TARGET_PROPERTIES(bootstrap PROPERTIES LINK_SEARCH_START_STATIC ON) +SET_TARGET_PROPERTIES(bootstrap PROPERTIES LINK_SEARCH_END_STATIC OFF) +target_link_libraries( + bootstrap + tools_base + ${EXTERNAL_LIBS} +) + +# add the command to generate the source code +add_custom_command ( + OUTPUT "${CMAKE_BINARY_DIR}/src/tools/license-generator/private-key.h" "${CMAKE_BINARY_DIR}/src/library/base/public-key.h" + COMMAND bootstrap "${CMAKE_BINARY_DIR}/src/tools/license-generator/private-key.h" "${CMAKE_BINARY_DIR}/src/library/base/public-key.h" + DEPENDS bootstrap +) + +add_custom_target(private_key DEPENDS "${CMAKE_BINARY_DIR}/src/tools/license-generator/private-key.h") +add_custom_target(public_key DEPENDS "${CMAKE_BINARY_DIR}/src/library/base/public-key.h") + + +# add the command to generate the source code +#add_custom_command ( +# OUTPUT "${CMAKE_BINARY_DIR}/public-key.h" +# COMMAND bootstrap derive_public "${CMAKE_CURRENT_SOURCE_DIR}/../license-generator/private-key.h" "${CMAKE_BINARY_DIR}/public-key.h" +# DEPENDS bootstrap private_key +#) +#add_custom_target(public_key DEPENDS "${CMAKE_BINARY_DIR}/public-key.h") diff --git a/src/tools/bootstrap/Main.cpp b/src/tools/bootstrap/Main.cpp new file mode 100644 index 0000000..d4faa46 --- /dev/null +++ b/src/tools/bootstrap/Main.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include "../base_lib/CryptoHelper.h" +#include <string> +#include <stdlib.h> +#include <iostream> + +using namespace std; +namespace license { + +void write_pubkey_file(const string& public_fname, const string& pbPublicKey) { + FILE* fp = fopen(public_fname.c_str(), "w"); + if (fp == NULL) { + throw ios_base::failure(string("can't create :") + public_fname); + } + fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n"); + fprintf(fp, "#ifndef PUBLIC_KEY_H_\n#define PUBLIC_KEY_H_\n"); + fprintf(fp, "define PUBLIC_KEY { \n"); + fprintf(fp, "%s", pbPublicKey.c_str()); + fprintf(fp, "\n};\n\n"); + int random = rand() % 1000; + fprintf(fp, "#define SHARED_RANDOM %d;\n", random); + fprintf(fp, "#endif\n"); + fclose(fp); +} + +void write_privkey_file(const string& private_fname, const string& privateKey) { + FILE* fp = fopen(private_fname.c_str(), "w"); + if (fp == NULL) { + throw ios_base::failure(string("can't create :") + private_fname); + } + 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, "define PRIVATE_KEY = {\n"); + fprintf(fp, "%s", privateKey.c_str()); + fprintf(fp, "\n};\n\n"); + fprintf(fp, "#endif\n"); + fclose(fp); +} + +void generatePk(string private_include, string public_include) { + unique_ptr<CryptoHelper> cryptoHlpr = CryptoHelper::getInstance(); + + try { + cryptoHlpr->generateKeyPair(); + } catch (exception &e) { + cerr << endl << "Error generating key pair: " << e.what() << endl + << "aborting" << endl; + exit(2); + } + + try { + const string pubKey = cryptoHlpr->exportPublicKey(); + write_pubkey_file(public_include, pubKey); + // Print out the public key to console as a + // hexadecimal string. + cout << endl << "PublicKey" << pubKey.c_str() << endl; + } catch (exception &e) { + cerr << endl << "Error exporting public key: " << e.what() << endl + << "aborting." << endl; + exit(4); + } + + try { + const string privKey = cryptoHlpr->exportPrivateKey(); + write_privkey_file(private_include, privKey); + } catch (exception &e) { + cerr << endl << "Error exporting private key: " << e.what() << endl + << "aborting" << endl; + exit(5); + } + + return; +} +} + +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]); + + license::generatePk(private_fname, public_fname); + return 0; +} diff --git a/src/bootstrap/bootstrap_linux.cpp b/src/tools/bootstrap/bootstrap_linux.cpp similarity index 99% rename from src/bootstrap/bootstrap_linux.cpp rename to src/tools/bootstrap/bootstrap_linux.cpp index 63069aa..8e42993 100644 --- a/src/bootstrap/bootstrap_linux.cpp +++ b/src/tools/bootstrap/bootstrap_linux.cpp @@ -2,7 +2,7 @@ * boostrap.c * * Created on: Apr 5, 2014 - * Author: devel + * */ #include <string> @@ -77,9 +77,9 @@ } 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 */ int random=rand(); + RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0); /* To get the C-string PEM form: */ //BIO bio_private* = BIO_new_file(const char *filename, "w") diff --git a/src/bootstrap/win/CmakeLists.txt b/src/tools/bootstrap/win/CmakeLists.txt similarity index 100% rename from src/bootstrap/win/CmakeLists.txt rename to src/tools/bootstrap/win/CmakeLists.txt diff --git a/src/license-generator/.gitignore b/src/tools/license-generator/.gitignore similarity index 100% rename from src/license-generator/.gitignore rename to src/tools/license-generator/.gitignore diff --git a/src/license-generator/CMakeLists.txt b/src/tools/license-generator/CMakeLists.txt similarity index 84% rename from src/license-generator/CMakeLists.txt rename to src/tools/license-generator/CMakeLists.txt index 3ad19ef..35199f7 100644 --- a/src/license-generator/CMakeLists.txt +++ b/src/tools/license-generator/CMakeLists.txt @@ -3,23 +3,15 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) link_directories ( ${Boost_LIBRARY_DIR} ) -if(WIN32) + ADD_LIBRARY( license_generator_lib STATIC license-generator.cpp - win/LicenseSigner.cpp ) -else(WIN32) -ADD_LIBRARY( - license_generator_lib STATIC - license-generator.cpp - linux/LicenseSigner.cpp -) -endif(WIN32) target_link_libraries( license_generator_lib - license++_static + tools_base $<$<CONFIG:Debug>:${Boost_PROGRAM_OPTIONS_LIBRARY_DEBUG}> $<$<NOT:$<CONFIG:Debug>>:${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE}> $<$<CONFIG:Debug>:${Boost_SYSTEM_LIBRARY_DEBUG}> diff --git a/src/license-generator/LicenseSigner.h b/src/tools/license-generator/LicenseSigner.h similarity index 96% rename from src/license-generator/LicenseSigner.h rename to src/tools/license-generator/LicenseSigner.h index f00b3ad..4c91000 100644 --- a/src/license-generator/LicenseSigner.h +++ b/src/tools/license-generator/LicenseSigner.h @@ -2,7 +2,7 @@ * LicenseSigner.h * * Created on: Apr 6, 2014 - * Author: devel + * */ #ifndef LICENSESIGNER_H_ diff --git a/src/license-generator/license-generator-main.cpp b/src/tools/license-generator/license-generator-main.cpp similarity index 100% rename from src/license-generator/license-generator-main.cpp rename to src/tools/license-generator/license-generator-main.cpp diff --git a/src/license-generator/license-generator.cpp b/src/tools/license-generator/license-generator.cpp similarity index 100% rename from src/license-generator/license-generator.cpp rename to src/tools/license-generator/license-generator.cpp diff --git a/src/license-generator/license-generator.h b/src/tools/license-generator/license-generator.h similarity index 97% rename from src/license-generator/license-generator.h rename to src/tools/license-generator/license-generator.h index e062e38..26fe56d 100644 --- a/src/license-generator/license-generator.h +++ b/src/tools/license-generator/license-generator.h @@ -2,7 +2,7 @@ * LicenseSigner.h * * Created on: Apr 6, 2014 - * Author: devel + * */ #ifndef LICENSE_GENERATOR_H_ diff --git a/src/license-generator/linux/LicenseSigner.cpp b/src/tools/license-generator/linux/LicenseSigner.cpp similarity index 99% rename from src/license-generator/linux/LicenseSigner.cpp rename to src/tools/license-generator/linux/LicenseSigner.cpp index 69a4357..aa56203 100644 --- a/src/license-generator/linux/LicenseSigner.cpp +++ b/src/tools/license-generator/linux/LicenseSigner.cpp @@ -2,7 +2,7 @@ * LicenseSigner.cpp * * Created on: Apr 6, 2014 - * Author: devel + * */ #include "../LicenseSigner.h" diff --git a/src/license-generator/win/LicenseSigner.cpp b/src/tools/license-generator/win/LicenseSigner.cpp similarity index 99% rename from src/license-generator/win/LicenseSigner.cpp rename to src/tools/license-generator/win/LicenseSigner.cpp index c6aca67..28d7c4d 100644 --- a/src/license-generator/win/LicenseSigner.cpp +++ b/src/tools/license-generator/win/LicenseSigner.cpp @@ -2,7 +2,7 @@ * LicenseSigner.cpp (Windows) * * Created on: Apr 6, 2014 - * Author: devel + * */ #include <stdexcept> diff --git a/src/pc-identifier/CMakeLists.txt b/src/tools/pc-identifier/CMakeLists.txt similarity index 100% rename from src/pc-identifier/CMakeLists.txt rename to src/tools/pc-identifier/CMakeLists.txt diff --git a/test/functional/generate-license.cpp b/test/functional/generate-license.cpp index 1a6594e..27b2f60 100644 --- a/test/functional/generate-license.cpp +++ b/test/functional/generate-license.cpp @@ -2,7 +2,7 @@ * generate-license.c * * Created on: Apr 13, 2014 - * Author: devel + * */ #include <boost/test/unit_test.hpp> diff --git a/test/scratch/pc-identifiers.c b/test/scratch/pc-identifiers.c index a0c38d2..4fe6630 100644 --- a/test/scratch/pc-identifiers.c +++ b/test/scratch/pc-identifiers.c @@ -2,7 +2,7 @@ * pc-identifiers.c * * Created on: Apr 16, 2014 - * Author: devel + * */ #include "os/os.h" -- Gitblit v1.9.1