| | |
| | | namespace license { |
| | | |
| | | FullLicenseInfo::FullLicenseInfo(const string& source, const string& product, |
| | | const string& license_signature, int licenseVersion, // |
| | | time_t from_date, time_t to_date, // |
| | | const string& client_signature, unsigned int from_sw_version, |
| | | unsigned int to_sw_version, const string& extra_data) : |
| | | const string& license_signature, int licenseVersion, string from_date, |
| | | string to_date, const string& client_signature, |
| | | unsigned int from_sw_version, unsigned int to_sw_version, |
| | | const string& extra_data) : |
| | | source(source), product(product), // |
| | | license_signature(license_signature), license_version(licenseVersion), // |
| | | from_date(from_date), to_date(to_date), // |
| | |
| | | |
| | | EventRegistry FullLicenseInfo::validate(int sw_version) { |
| | | EventRegistry er; |
| | | OsFunctions::initialize(); |
| | | bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(), |
| | | license_signature.c_str()); |
| | | if (sigVerified) { |
| | |
| | | } |
| | | if (has_expiry) { |
| | | time_t now = time(NULL); |
| | | if (this->to_date < now) { |
| | | if (expires_on() < now) { |
| | | er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR, ""); |
| | | } |
| | | if (this->from_date > now) { |
| | | if (valid_from() > now) { |
| | | er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR); |
| | | } |
| | | } |
| | |
| | | license->expiry_date[0] = '\0'; |
| | | license->days_left = 999999; |
| | | } else { |
| | | tm * timeinfo = localtime(&to_date); |
| | | strftime(license->expiry_date, 11, "%Y-%m-%d", timeinfo); |
| | | double secs = difftime(time(NULL), to_date); |
| | | strncpy(license->expiry_date, to_date.c_str(), 11); |
| | | double secs = difftime(time(NULL), |
| | | seconds_from_epoch(to_date.c_str())); |
| | | license->days_left = (int) secs / 60 * 60 * 24; |
| | | } |
| | | } |
| | |
| | | LicenseReader::LicenseReader(const LicenseLocation& licenseLocation) : |
| | | licenseLocation(licenseLocation) { |
| | | |
| | | } |
| | | |
| | | time_t LicenseReader::read_date(const char * productName, const char * ini_key, |
| | | const CSimpleIniA& ini) const { |
| | | string from_date_str = ini.GetValue(productName, ini_key); |
| | | time_t from_date = FullLicenseInfo::UNUSED_TIME; |
| | | if (from_date_str.length() > 0) { |
| | | from_date = seconds_from_epoch(from_date_str.c_str()); |
| | | } |
| | | return from_date; |
| | | } |
| | | |
| | | EventRegistry LicenseReader::readLicenses(const string &product, |
| | |
| | | long license_version = ini.GetLongValue(productNamePtr, |
| | | "license_version", -1); |
| | | if (license_signature != NULL && license_version > 0) { |
| | | time_t from_date = read_date(productNamePtr, "from_date", ini); |
| | | time_t to_date = read_date(productNamePtr, "to_date", ini); |
| | | string from_date = trim_copy( |
| | | ini.GetValue(productNamePtr, "from_date", |
| | | FullLicenseInfo::UNUSED_TIME)); |
| | | string to_date = trim_copy( |
| | | ini.GetValue(productNamePtr, "to_date", |
| | | FullLicenseInfo::UNUSED_TIME)); |
| | | FullLicenseInfo licInfo(*it, product, license_signature, |
| | | (int) license_version, from_date, to_date); |
| | | licenseInfoOut.push_back(licInfo); |
| | |
| | | oss << toupper_copy(trim_copy(this->product)); |
| | | oss << SHARED_RANDOM |
| | | ; |
| | | if (has_client_sig) { |
| | | oss << trim_copy(this->client_signature); |
| | | } |
| | | if (has_versions) { |
| | | oss << "|" << this->from_sw_version << "-" << this->to_sw_version; |
| | | } |
| | | /*if (has_client_sig) { |
| | | oss << trim_copy(this->client_signature); |
| | | } |
| | | if (has_versions) { |
| | | oss << "|" << this->from_sw_version << "-" << this->to_sw_version; |
| | | }*/ |
| | | if (has_expiry) { |
| | | oss << "|" << this->from_date << "-" << this->to_date; |
| | | } |
| | | if (this->extra_data.length() > 0) { |
| | | oss << "|" << extra_data; |
| | | } |
| | | return oss.str(); |
| | | oss << "|" << this->from_date << "|" << this->to_date; |
| | | }/* |
| | | if (this->extra_data.length() > 0) { |
| | | oss << "|" << extra_data; |
| | | }*/ |
| | | string result = oss.str(); |
| | | cout << "[" << oss.str() << "]" << endl; |
| | | return result; |
| | | |
| | | } |
| | | |
| | | void FullLicenseInfo::printAsIni(ostream & a_ostream) const { |
| | |
| | | ini.SetLongValue(product.c_str(), "to_sw_version", to_sw_version); |
| | | } |
| | | |
| | | char buff[20]; |
| | | if (this->from_date != UNUSED_TIME) { |
| | | strftime(buff, 20, "%Y-%m-%d", localtime(&this->from_date)); |
| | | ini.SetValue(product.c_str(), "from_date", buff); |
| | | ini.SetValue(product.c_str(), "from_date", from_date.c_str()); |
| | | } |
| | | char buff2[20]; |
| | | if (this->to_date != UNUSED_TIME) { |
| | | strftime(buff2, 20, "%Y-%m-%d", localtime(&this->to_date)); |
| | | ini.SetValue(product.c_str(), "to_date", buff2); |
| | | ini.SetValue(product.c_str(), "to_date", to_date.c_str()); |
| | | } |
| | | if (this->extra_data.length() > 0) { |
| | | ini.SetValue(product.c_str(), "extra_data", this->extra_data.c_str()); |
| | |
| | | ini.Save(sw); |
| | | } |
| | | |
| | | time_t FullLicenseInfo::expires_on() const { |
| | | return seconds_from_epoch(this->to_date.c_str()); |
| | | } |
| | | |
| | | time_t FullLicenseInfo::valid_from() const { |
| | | return seconds_from_epoch(this->from_date.c_str()); |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | string product; |
| | | string license_signature; |
| | | int license_version; |
| | | time_t from_date; |
| | | time_t to_date; |
| | | string from_date; |
| | | string to_date; |
| | | bool has_expiry; |
| | | unsigned int from_sw_version; |
| | | unsigned int to_sw_version; |
| | |
| | | bool has_client_sig; |
| | | string extra_data; |
| | | |
| | | static const time_t UNUSED_TIME = (time_t) 0; |
| | | static constexpr const char* UNUSED_TIME = "0000-00-00"; |
| | | static const unsigned int UNUSED_SOFTWARE_VERSION = 0; |
| | | |
| | | FullLicenseInfo(const string& source, const string& product, |
| | | const string& license_signature, int licenseVersion, |
| | | time_t from_date = UNUSED_TIME, |
| | | time_t to_date = UNUSED_TIME, // |
| | | string from_date = UNUSED_TIME, |
| | | string to_date = UNUSED_TIME, // |
| | | const string& client_signature = "", // |
| | | unsigned int from_sw_version = UNUSED_SOFTWARE_VERSION, |
| | | unsigned int to_sw_version = UNUSED_SOFTWARE_VERSION, |
| | | const string& extra_data = ""); |
| | | string printForSign() const; |
| | | void printAsIni(ostream & a_ostream) const; |
| | | |
| | | void toLicenseInfo(LicenseInfo* license) const; |
| | | EventRegistry validate(int sw_version); |
| | | time_t expires_on() const; |
| | | time_t valid_from() const; |
| | | }; |
| | | /** |
| | | * This class it is responsible to read the licenses from the disk |
| | |
| | | */ |
| | | class LicenseReader { |
| | | private: |
| | | time_t read_date(const char * productName, const char * ini_key, |
| | | const CSimpleIniA& ini) const; |
| | | const LicenseLocation licenseLocation; |
| | | EventRegistry getLicenseDiskFiles(vector<string>& diskFiles); |
| | | vector<string> filterExistingFiles(vector<string> licensePositions); |
| | |
| | | if (!it->has_expiry) { |
| | | it->toLicenseInfo(license); |
| | | break; |
| | | } else if (curLicense_exp < it->to_date) { |
| | | curLicense_exp = it->to_date; |
| | | } else if (curLicense_exp < it->expires_on()) { |
| | | curLicense_exp = it->expires_on(); |
| | | it->toLicenseInfo(license); |
| | | } |
| | | } |
| | |
| | | #include <openssl/evp.h> |
| | | #include <openssl/bio.h> |
| | | #include <openssl/pem.h> |
| | | |
| | | #include <openssl/err.h> |
| | | #include <iostream> |
| | | namespace license { |
| | | |
| | | using namespace std; |
| | |
| | | } |
| | | |
| | | bool OsFunctions::verifySignature(const char* stringToVerify, |
| | | const char* signature) { |
| | | const char* signatureB64) { |
| | | EVP_MD_CTX *mdctx = NULL; |
| | | |
| | | char *pubKey = PUBLIC_KEY |
| | | ; |
| | | |
| | | BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey)); |
| | | RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL,NULL,NULL); |
| | | RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL); |
| | | BIO_free(bio); |
| | | if (rsa == NULL) { |
| | | cout<<"cippa!"<<endl; |
| | | throw new logic_error("Error reading public key"); |
| | | } |
| | | EVP_PKEY *pkey = EVP_PKEY_new(); |
| | | |
| | | EVP_PKEY_assign_RSA(pkey, rsa); |
| | | cout << "test:" <<EVP_PKEY_assign_RSA(pkey, rsa)<<endl; |
| | | |
| | | /*BIO* bo = BIO_new(BIO_s_mem()); |
| | | BIO_write(bo, pubKey, strlen(pubKey)); |
| | |
| | | |
| | | //RSA* rsa = EVP_PKEY_get1_RSA( key ); |
| | | //RSA * pubKey = d2i_RSA_PUBKEY(NULL, <der encoded byte stream pointer>, <num bytes>); |
| | | unsigned char buffer[129]; |
| | | BIO* b64 = BIO_new(BIO_f_base64()); |
| | | BIO* encoded_signature = BIO_new_mem_buf((void *) signatureB64, |
| | | strlen(signatureB64)); |
| | | BIO* biosig = BIO_push(b64, encoded_signature); |
| | | BIO_set_flags(biosig, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer |
| | | unsigned int len = BIO_read(biosig, (void *) buffer, strlen(signatureB64)); |
| | | //Can test here if len == decodeLen - if not, then return an error |
| | | buffer[len] = 0; |
| | | |
| | | BIO_free_all(biosig); |
| | | |
| | | /* Create the Message Digest Context */ |
| | | if (!(mdctx = EVP_MD_CTX_create())) { |
| | | throw new logic_error("Error creating context"); |
| | |
| | | if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey)) { |
| | | throw new logic_error("Error initializing digest"); |
| | | } |
| | | |
| | | int en=strlen(stringToVerify); |
| | | if (1 |
| | | != EVP_DigestVerifyUpdate(mdctx, stringToVerify, |
| | | strlen(stringToVerify))) { |
| | | en)) { |
| | | throw new logic_error("Error initializing digest"); |
| | | } |
| | | if (1 |
| | | == EVP_DigestVerifyFinal(mdctx, (unsigned char *) signature, |
| | | (unsigned long int) strlen(signature))) { |
| | | return true; |
| | | bool result; |
| | | int res= EVP_DigestVerifyFinal(mdctx, buffer, len); |
| | | if (1 == res) { |
| | | result = true; |
| | | } else { |
| | | result = false; |
| | | } |
| | | if (pkey) { |
| | | EVP_PKEY_free(pkey); |
| | | } |
| | | if (mdctx) { |
| | | EVP_MD_CTX_destroy(mdctx); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | return false; |
| | | void OsFunctions::initialize() { |
| | | static bool initialized = false; |
| | | if (!initialized) { |
| | | initialized = true; |
| | | ERR_load_ERR_strings(); |
| | | ERR_load_crypto_strings(); |
| | | OpenSSL_add_all_algorithms(); |
| | | } |
| | | } |
| | | |
| | |
| | | // |
| | | 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" |
| | | );*/ |
| | | "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; |
| | | } |
| | |
| | | |
| | | class OsFunctions { |
| | | public: |
| | | static void initialize(); |
| | | static vector<AdapterInfo> getAdapterInfos(); |
| | | static vector<DiskInfo> getDiskInfos(); |
| | | static string getModuleName(); |
| | |
| | | ostringstream oss; |
| | | bt::time_facet *facet = new bt::time_facet("%Y-%m-%d"); |
| | | oss.imbue(locale(cout.getloc(), facet)); |
| | | oss << pt << endl; |
| | | oss << pt; |
| | | //delete (facet); |
| | | return oss.str(); |
| | | } |
New file |
| | |
| | | #define BOOST_TEST_MODULE standard_license_test |
| | | //#define BOOST_TEST_MAIN |
| | | #define BOOST_TEST_DYN_LINK |
| | | #include <boost/test/unit_test.hpp> |
| | | #include "../../../src/license-generator/license-generator.h" |
| | | #include "../../../src/library/api/license++.h" |
| | | #include <build_properties.h> |
| | | #include <boost/filesystem.hpp> |
| | | #include "../../src/library/ini/SimpleIni.h" |
| | | #include "generate-license.h" |
| | | |
| | | namespace fs = boost::filesystem; |
| | | using namespace license; |
| | | using namespace std; |
| | | |
| | | BOOST_AUTO_TEST_CASE( license_not_expired ) { |
| | | const string licLocation(PROJECT_TEST_TEMP_DIR "/not_expired.lic"); |
| | | vector<string> extraArgs; |
| | | extraArgs.push_back("-e"); |
| | | extraArgs.push_back("2050-10-10"); |
| | | generate_license(licLocation, extraArgs); |
| | | /* */ |
| | | LicenseInfo license; |
| | | LicenseLocation licenseLocation; |
| | | licenseLocation.openFileNearModule = false; |
| | | licenseLocation.licenseFileLocation = licLocation.c_str(); |
| | | licenseLocation.environmentVariableName = ""; |
| | | EVENT_TYPE result = acquire_license("TEST", licenseLocation, &license); |
| | | BOOST_CHECK_EQUAL(result, LICENSE_OK); |
| | | BOOST_CHECK_EQUAL(license.has_expiry, true); |
| | | BOOST_CHECK_EQUAL(license.linked_to_pc, false); |
| | | } |
| | | |
| | | BOOST_AUTO_TEST_CASE( license_expired ) { |
| | | const string licLocation(PROJECT_TEST_TEMP_DIR "/expired.lic"); |
| | | vector<string> extraArgs; |
| | | extraArgs.push_back("-e"); |
| | | extraArgs.push_back("2013-10-10"); |
| | | generate_license(licLocation, extraArgs); |
| | | /* */ |
| | | LicenseInfo license; |
| | | LicenseLocation licenseLocation; |
| | | licenseLocation.openFileNearModule = false; |
| | | licenseLocation.licenseFileLocation = licLocation.c_str(); |
| | | licenseLocation.environmentVariableName = ""; |
| | | EVENT_TYPE result = acquire_license("TEST", licenseLocation, &license); |
| | | BOOST_CHECK_EQUAL(result, PRODUCT_EXPIRED); |
| | | BOOST_CHECK_EQUAL(license.has_expiry, true); |
| | | BOOST_CHECK_EQUAL(license.linked_to_pc, false); |
| | | } |
New file |
| | |
| | | /* |
| | | * generate-license.c |
| | | * |
| | | * Created on: Apr 13, 2014 |
| | | * Author: devel |
| | | */ |
| | | |
| | | #include <boost/test/unit_test.hpp> |
| | | #include "../../../src/license-generator/license-generator.h" |
| | | #include <build_properties.h> |
| | | #include <boost/filesystem.hpp> |
| | | #include "../../src/library/ini/SimpleIni.h" |
| | | #include "generate-license.h" |
| | | |
| | | namespace fs = boost::filesystem; |
| | | using namespace license; |
| | | using namespace std; |
| | | |
| | | void generate_license(const string& fname, const vector<string>& other_args) { |
| | | int argc = 4+other_args.size(); |
| | | const char** argv = new const char*[argc + 1]; |
| | | unsigned int i=0; |
| | | argv[i++] = "lic-generator"; |
| | | for(;i<=other_args.size();i++){ |
| | | argv[i] = other_args[i-1].c_str(); |
| | | } |
| | | argv[i++] = "-o"; |
| | | argv[i++] = fname.c_str(); |
| | | argv[i++] = "TEST"; |
| | | int retCode = LicenseGenerator::generateLicense(argc, argv); |
| | | delete (argv); |
| | | BOOST_CHECK_EQUAL(retCode, 0); |
| | | BOOST_ASSERT(fs::exists(fname)); |
| | | CSimpleIniA ini; |
| | | SI_Error rc = ini.LoadFile(fname.c_str()); |
| | | BOOST_CHECK_GE(rc,0); |
| | | int sectionSize = ini.GetSectionSize("TEST"); |
| | | BOOST_CHECK_GT(sectionSize,0); |
| | | } |
New file |
| | |
| | | |
| | | |
| | | #include<string> |
| | | #include<vector> |
| | | |
| | | using namespace std; |
| | | void generate_license(const string& fname, const vector<string>& other_args) ; |
New file |
| | |
| | | #define BOOST_TEST_MODULE standard_license_test |
| | | //#define BOOST_TEST_MAIN |
| | | #define BOOST_TEST_DYN_LINK |
| | | #include <boost/test/unit_test.hpp> |
| | | #include "../../../src/license-generator/license-generator.h" |
| | | #include "../../../src/library/api/license++.h" |
| | | #include <build_properties.h> |
| | | #include <boost/filesystem.hpp> |
| | | #include "../../src/library/ini/SimpleIni.h" |
| | | #include "generate-license.h" |
| | | |
| | | namespace fs = boost::filesystem; |
| | | using namespace license; |
| | | using namespace std; |
| | | |
| | | |
| | | BOOST_AUTO_TEST_CASE( standard_lic_file ) { |
| | | const string licLocation(PROJECT_TEST_TEMP_DIR "/standard_license.lic"); |
| | | vector<string> extraArgs; |
| | | generate_license(licLocation, extraArgs); |
| | | /* */ |
| | | LicenseInfo license; |
| | | LicenseLocation licenseLocation; |
| | | licenseLocation.openFileNearModule=false; |
| | | licenseLocation.licenseFileLocation = licLocation.c_str(); |
| | | licenseLocation.environmentVariableName = ""; |
| | | EVENT_TYPE result = acquire_license("TEST", |
| | | licenseLocation, & license); |
| | | BOOST_CHECK_EQUAL(result, LICENSE_OK); |
| | | BOOST_CHECK_EQUAL(license.has_expiry, false); |
| | | BOOST_CHECK_EQUAL(license.linked_to_pc, false); |
| | | } |
| | | |