open-license-manager
2014-04-16 af928739ea648f8f3a5d64771d236fe7498eaf26
signature OK
6个文件已修改
4个文件已添加
269 ■■■■ 已修改文件
src/library/LicenseReader.cpp 68 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.h 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/license++.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/linux/os-linux.cpp 50 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/os.hpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/license-generator/license-generator.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/date_test.cpp 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/generate-license.cpp 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/generate-license.h 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/functional/hijiaking_test.cpp 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/LicenseReader.cpp
@@ -28,10 +28,10 @@
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), //
@@ -47,6 +47,7 @@
EventRegistry FullLicenseInfo::validate(int sw_version) {
    EventRegistry er;
    OsFunctions::initialize();
    bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(),
            license_signature.c_str());
    if (sigVerified) {
@@ -56,10 +57,10 @@
    }
    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);
        }
    }
@@ -77,9 +78,9 @@
            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;
        }
    }
@@ -87,16 +88,6 @@
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,
@@ -141,8 +132,12 @@
        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);
@@ -290,19 +285,22 @@
    oss << toupper_copy(trim_copy(this->product));
    oss << SHARED_RANDOM
    ;
    if (has_client_sig) {
    /*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;
    }
        oss << "|" << this->from_date << "|" << this->to_date;
    }/*
    if (this->extra_data.length() > 0) {
        oss << "|" << extra_data;
    }
    return oss.str();
     }*/
    string result = oss.str();
    cout << "[" << oss.str() << "]" << endl;
    return result;
}
void FullLicenseInfo::printAsIni(ostream & a_ostream) const {
@@ -323,15 +321,11 @@
        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());
@@ -339,5 +333,13 @@
    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());
}
}
src/library/LicenseReader.h
@@ -25,8 +25,8 @@
    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;
@@ -35,22 +35,23 @@
    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
@@ -72,8 +73,6 @@
 */
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);
src/library/license++.cpp
@@ -31,8 +31,8 @@
            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);
            }
        }
src/library/os/linux/os-linux.cpp
@@ -13,7 +13,8 @@
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <iostream>
namespace license {
using namespace std;
@@ -63,7 +64,7 @@
}
bool OsFunctions::verifySignature(const char* stringToVerify,
        const char* signature) {
        const char* signatureB64) {
    EVP_MD_CTX *mdctx = NULL;
    char *pubKey = PUBLIC_KEY
@@ -73,11 +74,12 @@
    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));
@@ -87,6 +89,18 @@
    //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");
@@ -94,19 +108,35 @@
    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();
    }
}
src/library/os/os.hpp
@@ -34,6 +34,7 @@
class OsFunctions {
public:
    static void initialize();
    static vector<AdapterInfo> getAdapterInfos();
    static vector<DiskInfo> getDiskInfos();
    static string getModuleName();
src/license-generator/license-generator.cpp
@@ -210,7 +210,7 @@
    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();
}
test/functional/date_test.cpp
New file
@@ -0,0 +1,50 @@
#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);
}
test/functional/generate-license.cpp
New file
@@ -0,0 +1,39 @@
/*
 * 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);
}
test/functional/generate-license.h
New file
@@ -0,0 +1,7 @@
#include<string>
#include<vector>
using namespace std;
void generate_license(const string& fname, const vector<string>& other_args) ;
test/functional/hijiaking_test.cpp
New file
@@ -0,0 +1,33 @@
#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);
}