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) src/CMakeLists.txt
@@ -1,6 +1,4 @@ add_subdirectory("bootstrap") add_subdirectory("library") add_subdirectory("pc-identifier") add_subdirectory("license-generator") add_subdirectory("tools") src/bootstrap/CMakeLists.txt
File was deleted src/bootstrap/win/CryptoHelper.h
File was deleted src/bootstrap/win/Main.cpp
File was deleted src/library/LicenseReader.cpp
@@ -2,7 +2,7 @@ * LicenseReader.cpp * * Created on: Mar 30, 2014 * Author: devel * */ #ifdef _WIN32 src/library/LicenseReader.h
@@ -2,7 +2,7 @@ * LicenseReader.h * * Created on: Mar 30, 2014 * Author: devel * */ #ifndef LICENSEREADER_H_ src/library/base/EventRegistry.cpp
@@ -2,7 +2,7 @@ * EventRegistry.cpp * * Created on: Mar 30, 2014 * Author: devel * */ #include "EventRegistry.h" src/library/base/EventRegistry.h
@@ -2,7 +2,7 @@ * EventRegistry.h * * Created on: Mar 30, 2014 * Author: devel * */ #ifndef EVENTREGISTRY_H_ src/library/base/StringUtils.cpp
@@ -2,7 +2,7 @@ * StringUtils.cpp * * Created on: Apr 8, 2014 * Author: devel * */ #include <cctype> //toupper src/library/base/StringUtils.h
@@ -2,7 +2,7 @@ * StringUtils.h * * Created on: Apr 8, 2014 * Author: devel * */ #ifndef STRINGUTILS_H_ 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 */ src/library/os/os-cpp.h
@@ -2,7 +2,7 @@ * os-dependent.hpp * * Created on: Mar 29, 2014 * Author: devel * */ #ifndef OS_DEPENDENT_CPP_ src/library/os/os.h
@@ -2,7 +2,7 @@ * os-dependent.hpp * * Created on: Mar 29, 2014 * Author: devel * */ #ifndef OS_DEPENDENT_HPP_ src/library/pc-identifiers.c
@@ -2,7 +2,7 @@ * pc-identifiers.c * * Created on: Apr 16, 2014 * Author: devel * */ #include "os/os.h" src/library/pc-identifiers.h
@@ -2,7 +2,7 @@ * pc-identifiers.h * * Created on: Apr 16, 2014 * Author: devel * */ #ifndef PC_IDENTIFIERS_H_ src/tools/CMakeLists.txt
New file @@ -0,0 +1,7 @@ enable_language(CXX) add_subdirectory("base_lib") add_subdirectory("bootstrap") add_subdirectory("pc-identifier") add_subdirectory("license-generator") src/tools/base_lib/CMakeLists.txt
New file @@ -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} ) src/tools/base_lib/CryptoHelper.cpp
New file @@ -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; } } src/tools/base_lib/CryptoHelper.h
New file @@ -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 src/tools/base_lib/README.TXT
New file @@ -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) src/tools/base_lib/linux/CryptoHelperLinux.cpp
New file @@ -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 */ src/tools/base_lib/linux/CryptoHelperLinux.h
New file @@ -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_ */ src/tools/base_lib/win/CryptoHelper.cpp
File was renamed from src/bootstrap/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) src/tools/base_lib/win/CryptoHelperWindows.cpp
New file @@ -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 */ src/tools/base_lib/win/CryptoHelperWindows.h
New file @@ -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_ */ src/tools/bootstrap/CMakeLists.txt
New file @@ -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") src/tools/bootstrap/Main.cpp
New file @@ -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; } src/tools/bootstrap/bootstrap_linux.cpp
File was renamed from src/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") src/tools/bootstrap/win/CmakeLists.txt
src/tools/license-generator/.gitignore
src/tools/license-generator/CMakeLists.txt
File was renamed from src/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}> src/tools/license-generator/LicenseSigner.h
File was renamed from src/license-generator/LicenseSigner.h @@ -2,7 +2,7 @@ * LicenseSigner.h * * Created on: Apr 6, 2014 * Author: devel * */ #ifndef LICENSESIGNER_H_ src/tools/license-generator/license-generator-main.cpp
src/tools/license-generator/license-generator.cpp
src/tools/license-generator/license-generator.h
File was renamed from src/license-generator/license-generator.h @@ -2,7 +2,7 @@ * LicenseSigner.h * * Created on: Apr 6, 2014 * Author: devel * */ #ifndef LICENSE_GENERATOR_H_ src/tools/license-generator/linux/LicenseSigner.cpp
File was renamed from src/license-generator/linux/LicenseSigner.cpp @@ -2,7 +2,7 @@ * LicenseSigner.cpp * * Created on: Apr 6, 2014 * Author: devel * */ #include "../LicenseSigner.h" src/tools/license-generator/win/LicenseSigner.cpp
File was renamed from src/license-generator/win/LicenseSigner.cpp @@ -2,7 +2,7 @@ * LicenseSigner.cpp (Windows) * * Created on: Apr 6, 2014 * Author: devel * */ #include <stdexcept> src/tools/pc-identifier/CMakeLists.txt
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> test/scratch/pc-identifiers.c
@@ -2,7 +2,7 @@ * pc-identifiers.c * * Created on: Apr 16, 2014 * Author: devel * */ #include "os/os.h"