src: Converted spaces to tabs in license sources

This commit is contained in:
Eberhard Graether
2018-02-27 16:59:05 +01:00
parent f2e4ad1661
commit ef70d38ff7
6 changed files with 401 additions and 402 deletions
+22 -23
View File
@@ -146,14 +146,14 @@ bool License::isTestLicense() const
bool License::isNonCommercialLicenseType() const bool License::isNonCommercialLicenseType() const
{ {
for ( const std::string& nonCommercialLicenseType : NON_COMMERCIAL_LICENSE_TYPES) for ( const std::string& nonCommercialLicenseType : NON_COMMERCIAL_LICENSE_TYPES)
{ {
if (m_type == nonCommercialLicenseType) if (m_type == nonCommercialLicenseType)
{ {
return true; return true;
} }
} }
return false; return false;
} }
size_t License::getNumberOfUsers() const size_t License::getNumberOfUsers() const
@@ -209,21 +209,21 @@ std::string License::getUser() const
int License::getTimeLeft() const int License::getTimeLeft() const
{ {
const std::string dateText = "YYYY-MMM-DD"; const std::string dateText = "YYYY-MMM-DD";
std::string dateString = m_expire; std::string dateString = m_expire;
if (dateString.length() != dateText.length()) if (dateString.length() != dateText.length())
{ {
return -2; return -2;
} }
boost::gregorian::date expireDate( boost::gregorian::date expireDate(
boost::gregorian::from_simple_string(dateString)); boost::gregorian::from_simple_string(dateString));
boost::gregorian::date today = boost::gregorian::day_clock::local_day(); boost::gregorian::date today = boost::gregorian::day_clock::local_day();
boost::gregorian::days daysLeft = expireDate - today; boost::gregorian::days daysLeft = expireDate - today;
return (daysLeft.days() < 0 ? -1 : daysLeft.days()); return (daysLeft.days() < 0 ? -1 : daysLeft.days());
} }
void License::writeToFile(const std::string& filename) void License::writeToFile(const std::string& filename)
@@ -286,12 +286,12 @@ bool License::load(std::istream& stream)
m_signature = ""; m_signature = "";
while (getline(stream, line, '\n')) while (getline(stream, line, '\n'))
{ {
std::string l = utility::trim(line); std::string l = utility::trim(line);
if (l == LicenseConstants::END_LICENSE_STRING) if (l == LicenseConstants::END_LICENSE_STRING)
{ {
break; break;
} }
if (l.size()) if (l.size())
{ {
m_signature += l; m_signature += l;
} }
@@ -350,7 +350,7 @@ bool License::isValid() const
std::cout << "No public key loaded" << std::endl; std::cout << "No public key loaded" << std::endl;
return false; return false;
} }
try try
{ {
if (m_signature.empty()) if (m_signature.empty())
{ {
@@ -368,7 +368,6 @@ bool License::isValid() const
if (isExpired()) if (isExpired())
{ {
std::cout << "License is expired" << std::endl;
return false; return false;
} }
@@ -413,7 +412,7 @@ std::string License::getPublicKeyFilename() const
{ {
if (m_publicKeyFilename.empty()) if (m_publicKeyFilename.empty())
{ {
return "public-sourcetrail" + KEY_FILEENDING; return "public-sourcetrail" + KEY_FILEENDING;
} }
return m_publicKeyFilename; return m_publicKeyFilename;
} }
@@ -594,7 +593,7 @@ bool License::loadFromEncodedString(const std::string& encodedLicense, const std
std::string License::getHashedLicense() const std::string License::getHashedLicense() const
{ {
return getEncodeKey("fakeKey"); return getEncodeKey("fakeKey");
} }
std::string License::getEncodeKey(const std::string applicationLocation) const std::string License::getEncodeKey(const std::string applicationLocation) const
+33 -33
View File
@@ -16,53 +16,53 @@ namespace Botan
namespace LicenseConstants { namespace LicenseConstants {
const char BEGIN_LICENSE_STRING[] = "-----BEGIN LICENSE-----"; const char BEGIN_LICENSE_STRING[] = "-----BEGIN LICENSE-----";
const char END_LICENSE_STRING[] = "-----END LICENSE-----"; const char END_LICENSE_STRING[] = "-----END LICENSE-----";
const char TEST_LICENSE_STRING[] = "Test License"; const char TEST_LICENSE_STRING[] = "Test License";
const char PRODUCT_STRING[] = "Product: Sourcetrail"; const char PRODUCT_STRING[] = "Product: Sourcetrail";
const char LICENSED_TO_STRING[] = "Licensed to: "; const char LICENSED_TO_STRING[] = "Licensed to: ";
const char LICENSE_TYPE_STRING[] = "License type: "; const char LICENSE_TYPE_STRING[] = "License type: ";
const char VALID_UNTIL_STRING[] = "Valid until: "; const char VALID_UNTIL_STRING[] = "Valid until: ";
const char VALID_UP_TO_STRING[] = "Valid up to version: "; const char VALID_UP_TO_STRING[] = "Valid up to version: ";
const char SEPARATOR_STRING[] = "-"; const char SEPARATOR_STRING[] = "-";
const int MINOR_VERSIONS_PER_YEAR = 4; const int MINOR_VERSIONS_PER_YEAR = 4;
} }
class License class License
{ {
public: public:
enum LICENSE_LINE enum LICENSE_LINE
{ {
BEGIN_LICENSE_LINE = 0, BEGIN_LICENSE_LINE = 0,
SOURCETRAIL_LINE = 1, SOURCETRAIL_LINE = 1,
USER_LINE = 2, USER_LINE = 2,
TYPE_LINE = 3, TYPE_LINE = 3,
EXPIRE_LINE = 4, EXPIRE_LINE = 4,
SEPARATOR_LINE=5, SEPARATOR_LINE=5,
HASH_LINE = 6, HASH_LINE = 6,
FIRST_SIGNATURE_LINE = 7, FIRST_SIGNATURE_LINE = 7,
LAST_SIGNATURE_LINE = 13, LAST_SIGNATURE_LINE = 13,
END_LICENSE_LINE = 14, END_LICENSE_LINE = 14,
LICENSE_LINES = 15 LICENSE_LINES = 15
}; };
License(); License();
~License(); ~License();
std::string getMessage(bool withNewlines = false) const; std::string getMessage(bool withNewlines = false) const;
std::string getSignature() const; std::string getSignature() const;
std::string getVersionLineWithoutPrefix() const; std::string getVersionLineWithoutPrefix() const;
std::string getLine(std::istream& stream); std::string getLine(std::istream& stream);
std::string getLineWithoutDescription(LICENSE_LINE line) const; std::string getLineWithoutDescription(LICENSE_LINE line) const;
void setLine(const LICENSE_LINE line, const std::string& value); void setLine(const LICENSE_LINE line, const std::string& value);
void setHashLine(const std::string& hash); void setHashLine(const std::string& hash);
void setTypeAndNumberOfUsers(const std::string& line); void setTypeAndNumberOfUsers(const std::string& line);
std::string getLicenseInfo() const; std::string getLicenseInfo() const;
std::string getPublicKeyFilename() const; std::string getPublicKeyFilename() const;
std::string getVersion() const; std::string getVersion() const;
std::string getType() const; std::string getType() const;
std::string getUser() const; std::string getUser() const;
size_t getNumberOfUsers() const; size_t getNumberOfUsers() const;
void createHeader( void createHeader(
const std::string& user, const std::string& user,
@@ -76,14 +76,14 @@ public:
/// for non Test License -2 /// for non Test License -2
int getTimeLeft() const; int getTimeLeft() const;
std::string getLicenseString() const; std::string getLicenseString() const;
std::string getLicenseEncodedString(const std::string& applicationLocation) const; std::string getLicenseEncodedString(const std::string& applicationLocation) const;
std::string getHashedLicense() const; std::string getHashedLicense() const;
void writeToFile(const std::string& filename); void writeToFile(const std::string& filename);
bool load(std::istream& stream); bool load(std::istream& stream);
bool loadFromString(const std::string& licenseText); bool loadFromString(const std::string& licenseText);
bool loadFromFile(const std::string& filename); bool loadFromFile(const std::string& filename);
bool loadFromEncodedString(const std::string& encodedLicense, const std::string& applicationLocation); bool loadFromEncodedString(const std::string& encodedLicense, const std::string& applicationLocation);
bool loadPublicKey(); bool loadPublicKey();
@@ -93,15 +93,15 @@ public:
void setSignature(const std::string&); void setSignature(const std::string&);
bool isValid() const; bool isValid() const;
bool isExpired() const; bool isExpired() const;
bool isTestLicense() const; bool isTestLicense() const;
bool isNonCommercialLicenseType() const; bool isNonCommercialLicenseType() const;
void print(); void print();
std::string hashLocation(const std::string&) const; std::string hashLocation(const std::string&) const;
static bool checkLocation(const std::string&, const std::string&); static bool checkLocation(const std::string&, const std::string&);
private: private:
std::string getEncodeKey(const std::string applicationLocation) const; std::string getEncodeKey(const std::string applicationLocation) const;
@@ -122,9 +122,9 @@ private:
std::string m_signature; std::string m_signature;
const std::string KEY_FILEENDING = ".pem"; const std::string KEY_FILEENDING = ".pem";
const std::vector<std::string> NON_COMMERCIAL_LICENSE_TYPES = { { "Private/Academic Single User License" } }; const std::vector<std::string> NON_COMMERCIAL_LICENSE_TYPES = { { "Private/Academic Single User License" } };
}; };
#endif // SOURCETRAIL_LICENSE_H #endif // SOURCETRAIL_LICENSE_H
+98 -98
View File
@@ -27,7 +27,7 @@ const char PUBLIC_KEY_FILE[] = "public-sourcetrail.pem";
Generator::Generator() Generator::Generator()
{ {
loadPrivateKeyFromString(PRIVATE_KEY); loadPrivateKeyFromString(PRIVATE_KEY);
} }
Generator::~Generator() Generator::~Generator()
@@ -41,20 +41,20 @@ void Generator::generateKeys()
std::string Generator::getPrivateKeyFilename() std::string Generator::getPrivateKeyFilename()
{ {
if(m_privateKeyFile.empty()) if(m_privateKeyFile.empty())
{ {
return PRIVATE_KEY_FILE; return PRIVATE_KEY_FILE;
} }
return m_privateKeyFile; return m_privateKeyFile;
} }
std::string Generator::getPublicKeyFilename() std::string Generator::getPublicKeyFilename()
{ {
if(m_publicKeyFile.empty()) if(m_publicKeyFile.empty())
{ {
return PUBLIC_KEY_FILE; return PUBLIC_KEY_FILE;
} }
return m_publicKeyFile; return m_publicKeyFile;
} }
std::string Generator::encodeLicense(const std::string& user, const int days) std::string Generator::encodeLicense(const std::string& user, const int days)
@@ -63,89 +63,89 @@ std::string Generator::encodeLicense(const std::string& user, const int days)
boost::gregorian::days daysToTry(days); boost::gregorian::days daysToTry(days);
boost::gregorian::date expireDate = today + daysToTry; boost::gregorian::date expireDate = today + daysToTry;
createLicense(user, LicenseConstants::TEST_LICENSE_STRING, boost::gregorian::to_simple_string(expireDate), 0); createLicense(user, LicenseConstants::TEST_LICENSE_STRING, boost::gregorian::to_simple_string(expireDate), 0);
return m_license->getLicenseString(); return m_license->getLicenseString();
} }
std::string Generator::encodeLicense( std::string Generator::encodeLicense(
const std::string& user, const std::string& user,
const std::string& licenseType, const std::string& licenseType,
size_t numberOfUsers, size_t numberOfUsers,
const std::string& version const std::string& version
) )
{ {
m_license = nullptr; m_license = nullptr;
if (user.size() <= 0) if (user.size() <= 0)
{ {
std::cout << "No user given" << std::endl; std::cout << "No user given" << std::endl;
return ""; return "";
} }
if (licenseType.size() <= 0) if (licenseType.size() <= 0)
{ {
std::cout << "No license type given" << std::endl; std::cout << "No license type given" << std::endl;
return ""; return "";
} }
if (!version.empty()) if (!version.empty())
{ {
Version tempVersion = Version::fromString(version); Version tempVersion = Version::fromString(version);
if (tempVersion.isValid()) if (tempVersion.isValid())
{ {
createLicense(user, licenseType, tempVersion.toShortString(), numberOfUsers); createLicense(user, licenseType, tempVersion.toShortString(), numberOfUsers);
} }
} }
if (!m_license) if (!m_license)
{ {
createLicense(user, licenseType, getExpireVersion(), numberOfUsers); createLicense(user, licenseType, getExpireVersion(), numberOfUsers);
} }
return m_license->getLicenseString(); return m_license->getLicenseString();
} }
void Generator::printLicenseAndWriteItToFile() void Generator::printLicenseAndWriteItToFile()
{ {
if (m_license) if (m_license)
{ {
m_license->print(); m_license->print();
m_license->writeToFile("license.txt"); m_license->writeToFile("license.txt");
} }
else else
{ {
std::cout << "nothing to print" << std::endl; std::cout << "nothing to print" << std::endl;
} }
} }
bool Generator::verifyLicense(const std::string& filename) bool Generator::verifyLicense(const std::string& filename)
{ {
License license; License license;
license.loadFromFile(filename); license.loadFromFile(filename);
// license.loadPublicKeyFromString(PUBLIC_KEY); // license.loadPublicKeyFromString(PUBLIC_KEY);
return license.isValid(); return license.isValid();
} }
void Generator::setCustomPrivateKeyFile(const std::string& file) void Generator::setCustomPrivateKeyFile(const std::string& file)
{ {
if(!file.empty()) if(!file.empty())
{ {
m_privateKeyFile = file; m_privateKeyFile = file;
} }
} }
void Generator::setCustomPublicKeyFile(const std::string& file) void Generator::setCustomPublicKeyFile(const std::string& file)
{ {
if(!file.empty()) if(!file.empty())
{ {
m_publicKeyFile = file; m_publicKeyFile = file;
} }
} }
std::string Generator::getPublicKeyPEMFileAsString() std::string Generator::getPublicKeyPEMFileAsString()
{ {
return Botan::X509::PEM_encode(*m_privateKey); return Botan::X509::PEM_encode(*m_privateKey);
} }
std::string Generator::getPrivateKeyPEMFileAsString() std::string Generator::getPrivateKeyPEMFileAsString()
@@ -156,12 +156,12 @@ std::string Generator::getPrivateKeyPEMFileAsString()
return ""; return "";
} }
return Botan::PKCS8::PEM_encode(*m_privateKey, m_rng, PRIVATE_KEY_PASSWORD); return Botan::PKCS8::PEM_encode(*m_privateKey, m_rng, PRIVATE_KEY_PASSWORD);
} }
void Generator::writeKeysToFiles() void Generator::writeKeysToFiles()
{ {
std::string publicKeyFilename = getPublicKeyFilename(); std::string publicKeyFilename = getPublicKeyFilename();
if (publicKeyFilename.size() <= 0) if (publicKeyFilename.size() <= 0)
{ {
std::cout << "Failed to retrieve file name for public key" << std::endl; std::cout << "Failed to retrieve file name for public key" << std::endl;
@@ -176,15 +176,15 @@ void Generator::writeKeysToFiles()
} }
std::cout << "public key filename: " << publicKeyFilename << std::endl; std::cout << "public key filename: " << publicKeyFilename << std::endl;
std::ofstream pub(publicKeyFilename); std::ofstream pub(publicKeyFilename);
pub << getPublicKeyPEMFileAsString(); pub << getPublicKeyPEMFileAsString();
std::cout << "public key created" << std::endl; std::cout << "public key created" << std::endl;
std::cout << "private key filename: " << privateKeyFilename << std::endl; std::cout << "private key filename: " << privateKeyFilename << std::endl;
std::ofstream priv(privateKeyFilename); std::ofstream priv(privateKeyFilename);
priv << getPrivateKeyPEMFileAsString(); priv << getPrivateKeyPEMFileAsString();
std::cout << "private key created" << std::endl; std::cout << "private key created" << std::endl;
} }
bool Generator::loadPrivateKeyFromFile() bool Generator::loadPrivateKeyFromFile()
@@ -194,14 +194,14 @@ bool Generator::loadPrivateKeyFromFile()
return false; return false;
} }
Botan::Private_Key* privateKey = Botan::PKCS8::load_key(getPrivateKeyFilename(), m_rng, PRIVATE_KEY_PASSWORD); Botan::Private_Key* privateKey = Botan::PKCS8::load_key(getPrivateKeyFilename(), m_rng, PRIVATE_KEY_PASSWORD);
Botan::RSA_PrivateKey *rsaKey = dynamic_cast<Botan::RSA_PrivateKey *>(privateKey); Botan::RSA_PrivateKey *rsaKey = dynamic_cast<Botan::RSA_PrivateKey *>(privateKey);
if (!rsaKey) if (!rsaKey)
{ {
std::cout << "The key is not a RSA key" << std::endl; std::cout << "The key is not a RSA key" << std::endl;
return false; return false;
} }
m_privateKey = std::unique_ptr<Botan::RSA_PrivateKey>(rsaKey); m_privateKey = std::unique_ptr<Botan::RSA_PrivateKey>(rsaKey);
@@ -216,67 +216,67 @@ bool Generator::loadPrivateKeyFromString(const std::string& key)
return false; return false;
} }
Botan::DataSource_Memory in(key); Botan::DataSource_Memory in(key);
Botan::Private_Key* privateKey= Botan::PKCS8::load_key(in, m_rng, PRIVATE_KEY_PASSWORD); Botan::Private_Key* privateKey= Botan::PKCS8::load_key(in, m_rng, PRIVATE_KEY_PASSWORD);
Botan::RSA_PrivateKey *rsaKey = dynamic_cast<Botan::RSA_PrivateKey *>(privateKey); Botan::RSA_PrivateKey *rsaKey = dynamic_cast<Botan::RSA_PrivateKey *>(privateKey);
if (!rsaKey) if (!rsaKey)
{ {
std::cout << "The key is not a RSA key" << std::endl; std::cout << "The key is not a RSA key" << std::endl;
return false; return false;
} }
m_privateKey = std::unique_ptr<Botan::RSA_PrivateKey>(rsaKey); m_privateKey = std::unique_ptr<Botan::RSA_PrivateKey>(rsaKey);
return (m_privateKey != NULL); return (m_privateKey != NULL);
} }
Botan::RSA_PrivateKey *Generator::getPrivateKey() const Botan::RSA_PrivateKey *Generator::getPrivateKey() const
{ {
return m_privateKey.get(); return m_privateKey.get();
} }
void Generator::createLicense( void Generator::createLicense(
const std::string& user, const std::string& user,
const std::string& type, const std::string& type,
const std::string& expiration, const std::string& expiration,
size_t numberOfUsers size_t numberOfUsers
) )
{ {
m_license = std::make_unique<License>(); m_license = std::make_unique<License>();
m_license->createHeader(user, type, expiration, numberOfUsers); m_license->createHeader(user, type, expiration, numberOfUsers);
Botan::AutoSeeded_RNG rng; Botan::AutoSeeded_RNG rng;
std::string pass9 = Botan::generate_passhash9(m_license->getExpireLine(), m_rng); std::string pass9 = Botan::generate_passhash9(m_license->getExpireLine(), m_rng);
m_license->setHashLine(pass9); m_license->setHashLine(pass9);
//encode message //encode message
const std::string emsa = "EMSA4(SHA-256)"; const std::string emsa = "EMSA4(SHA-256)";
Botan::PK_Signer signer(*(m_privateKey.get()), rng, emsa); Botan::PK_Signer signer(*(m_privateKey.get()), rng, emsa);
Botan::DataSource_Memory in(m_license->getMessage()); Botan::DataSource_Memory in(m_license->getMessage());
Botan::byte buffer[4096] = {0}; Botan::byte buffer[4096] = {0};
while (size_t got = in.read(buffer, sizeof(buffer))) while (size_t got = in.read(buffer, sizeof(buffer)))
{ {
signer.update(buffer, got); signer.update(buffer, got);
} }
const std::string signature = Botan::base64_encode(signer.signature(rng)); const std::string signature = Botan::base64_encode(signer.signature(rng));
m_license->setSignature(signature); m_license->setSignature(signature);
} }
int Generator::mapMonthToVersion(int month) int Generator::mapMonthToVersion(int month)
{ {
return (month-1)/3+1; return (month-1)/3+1;
} }
std::string Generator::getExpireVersion(int versions) std::string Generator::getExpireVersion(int versions)
{ {
boost::gregorian::date today = boost::gregorian::day_clock::local_day(); boost::gregorian::date today = boost::gregorian::day_clock::local_day();
int monthNumber = today.month().as_number(); int monthNumber = today.month().as_number();
Version version(today.year(), mapMonthToVersion(monthNumber)); Version version(today.year(), mapMonthToVersion(monthNumber));
version += versions; version += versions;
return version.toShortString(); return version.toShortString();
} }
+39 -39
View File
@@ -15,53 +15,53 @@ class License;
class Generator class Generator
{ {
public: public:
Generator(); Generator();
~Generator(); ~Generator();
std::string encodeLicense( std::string encodeLicense(
const std::string& user, const std::string& user,
const std::string& licenseType, const std::string& licenseType,
size_t numberOfUsers = 0, size_t numberOfUsers = 0,
const std::string& version = "" const std::string& version = ""
); );
std::string encodeLicense( std::string encodeLicense(
const std::string& user, const std::string& user,
const int days const int days
); );
void printLicenseAndWriteItToFile(); void printLicenseAndWriteItToFile();
bool verifyLicense(const std::string& filename = "license.txt"); bool verifyLicense(const std::string& filename = "license.txt");
void generateKeys(); void generateKeys();
void writeKeysToFiles(); void writeKeysToFiles();
void setCustomPrivateKeyFile(const std::string& file); void setCustomPrivateKeyFile(const std::string& file);
void setCustomPublicKeyFile(const std::string& file); void setCustomPublicKeyFile(const std::string& file);
std::string getPublicKeyPEMFileAsString(); std::string getPublicKeyPEMFileAsString();
std::string getPrivateKeyPEMFileAsString(); std::string getPrivateKeyPEMFileAsString();
bool loadPrivateKeyFromFile(); bool loadPrivateKeyFromFile();
bool loadPrivateKeyFromString(const std::string& key); bool loadPrivateKeyFromString(const std::string& key);
Botan::RSA_PrivateKey* getPrivateKey() const; Botan::RSA_PrivateKey* getPrivateKey() const;
void createLicense( void createLicense(
const std::string& user, const std::string& user,
const std::string& type, const std::string& type,
const std::string& expiration, const std::string& expiration,
size_t numberOfUsers size_t numberOfUsers
); );
std::string getExpireVersion(int versions = 4); std::string getExpireVersion(int versions = 4);
void setVersionLine(int year, int minorVersion); void setVersionLine(int year, int minorVersion);
private: private:
int mapMonthToVersion(int month); int mapMonthToVersion(int month);
std::string getPrivateKeyFilename(); std::string getPrivateKeyFilename();
std::string getPublicKeyFilename(); std::string getPublicKeyFilename();
//Botan //Botan
Botan::AutoSeeded_RNG m_rng; Botan::AutoSeeded_RNG m_rng;
std::string m_privateKeyFile; std::string m_privateKeyFile;
std::string m_publicKeyFile; std::string m_publicKeyFile;
std::unique_ptr<License> m_license; std::unique_ptr<License> m_license;
std::unique_ptr<Botan::RSA_PrivateKey> m_privateKey; std::unique_ptr<Botan::RSA_PrivateKey> m_privateKey;
}; };
+109 -109
View File
@@ -13,144 +13,144 @@ namespace po = boost::program_options;
bool process_command_line(int argc, char** argv) bool process_command_line(int argc, char** argv)
{ {
try try
{ {
std::string user; std::string user;
std::string version; std::string version;
std::string privateKeyFile; std::string privateKeyFile;
std::string publicKeyFile; std::string publicKeyFile;
std::string licenseFile = ""; std::string licenseFile = "";
std::string type = ""; std::string type = "";
int days = 0; int days = 0;
int numberOfUsers = 0; int numberOfUsers = 0;
po::options_description modes_description("Sourcetrail Generator Modes"); po::options_description modes_description("Sourcetrail Generator Modes");
modes_description.add_options() modes_description.add_options()
("key,k", "Generate the private and public key") ("key,k", "Generate the private and public key")
("generate,g", po::value<std::string>(&user), "Generate a License, USERNAME as value") ("generate,g", po::value<std::string>(&user), "Generate a License, USERNAME as value")
("check,c", "Validate a License"); ("check,c", "Validate a License");
po::options_description keygen_description("Options Keygeneration"); po::options_description keygen_description("Options Keygeneration");
keygen_description.add_options() keygen_description.add_options()
("version,v", po::value<std::string>(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid") ("version,v", po::value<std::string>(&version), "Versionnumber (in format 20xx.x) until Sourcetrail valid")
("users,u", po::value<int>(&numberOfUsers), "Number of users") ("users,u", po::value<int>(&numberOfUsers), "Number of users")
("seats,s", po::value<int>(&numberOfUsers), "Number of users (prev. seats)") ("seats,s", po::value<int>(&numberOfUsers), "Number of users (prev. seats)")
("licenseType,t", po::value<std::string>(&type), "License Type of ") ("licenseType,t", po::value<std::string>(&type), "License Type of ")
("testLicense,e", po::value<int>(&days), "Generates a test license for <value> days"); ("testLicense,e", po::value<int>(&days), "Generates a test license for <value> days");
po::options_description hidden_description("Hidden Options"); po::options_description hidden_description("Hidden Options");
hidden_description.add_options() hidden_description.add_options()
("public-file", po::value<std::string>(&publicKeyFile), "Custom public key file") ("public-file", po::value<std::string>(&publicKeyFile), "Custom public key file")
("private-file", po::value<std::string>(&privateKeyFile), "Custom private key file") ("private-file", po::value<std::string>(&privateKeyFile), "Custom private key file")
("license-file", po::value<std::string>(&licenseFile), "Custom license") ("license-file", po::value<std::string>(&licenseFile), "Custom license")
("hidden", "Print this help message"); ("hidden", "Print this help message");
po::options_description desc("Sourcetrail Generator"); po::options_description desc("Sourcetrail Generator");
desc.add_options() desc.add_options()
("help,h", "Print this help message"); ("help,h", "Print this help message");
desc.add(modes_description).add(keygen_description); desc.add(modes_description).add(keygen_description);
po::options_description allDescriptions("Sourcetrail Generator"); po::options_description allDescriptions("Sourcetrail Generator");
allDescriptions.add_options(); allDescriptions.add_options();
allDescriptions.add(modes_description).add(keygen_description).add(hidden_description); allDescriptions.add(modes_description).add(keygen_description).add(hidden_description);
po::variables_map vm; po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc), vm); po::store(po::parse_command_line(argc,argv,desc), vm);
po::notify(vm); po::notify(vm);
// display help if no argument or the help argument is given // display help if no argument or the help argument is given
if (vm.count("help")) if (vm.count("help"))
{ {
std::cout << desc << std::endl; std::cout << desc << std::endl;
return 1; return 1;
} }
// no mode chosen -> display help // no mode chosen -> display help
if ( vm.size() == 0 || !(vm.count("hidden") || vm.count("key") || vm.count("check") || vm.count("generate"))) if ( vm.size() == 0 || !(vm.count("hidden") || vm.count("key") || vm.count("check") || vm.count("generate")))
{ {
std::cout << "*****************************\nNo mode chosen, display help: " std::cout << "*****************************\nNo mode chosen, display help: "
<< "\n*****************************\n\n" << desc << std::endl; << "\n*****************************\n\n" << desc << std::endl;
return 1; return 1;
} }
if (vm.count("hidden")) if (vm.count("hidden"))
{ {
std::cout << allDescriptions << std::endl; std::cout << allDescriptions << std::endl;
return 1; return 1;
} }
Generator keygen; Generator keygen;
// make sure there are no negative amount of users // make sure there are no negative amount of users
if (vm.count("users") || vm.count("seats")) if (vm.count("users") || vm.count("seats"))
{ {
if (numberOfUsers < 0) if (numberOfUsers < 0)
{ {
std::cout << "Invalid amount of users. (Must be > 0)" << std::endl; std::cout << "Invalid amount of users. (Must be > 0)" << std::endl;
return false; return false;
} }
} }
if (vm.count("key")) if (vm.count("key"))
{ {
keygen.generateKeys(); keygen.generateKeys();
keygen.writeKeysToFiles(); keygen.writeKeysToFiles();
} }
if (vm.count("public-file")) if (vm.count("public-file"))
{ {
keygen.setCustomPublicKeyFile(publicKeyFile); keygen.setCustomPublicKeyFile(publicKeyFile);
} }
if (vm.count("private-file")) if (vm.count("private-file"))
{ {
keygen.setCustomPrivateKeyFile(privateKeyFile); keygen.setCustomPrivateKeyFile(privateKeyFile);
} }
else else
{ {
keygen.loadPrivateKeyFromString(PRIVATE_KEY); keygen.loadPrivateKeyFromString(PRIVATE_KEY);
} }
if (vm.count("generate")) if (vm.count("generate"))
{ {
if (vm.count("testLicense")) if (vm.count("testLicense"))
{ {
keygen.encodeLicense(user, days); keygen.encodeLicense(user, days);
keygen.printLicenseAndWriteItToFile(); keygen.printLicenseAndWriteItToFile();
} }
else else
{ {
keygen.encodeLicense(user, type, numberOfUsers, version); keygen.encodeLicense(user, type, numberOfUsers, version);
keygen.printLicenseAndWriteItToFile(); keygen.printLicenseAndWriteItToFile();
} }
} }
if (vm.count("check")) if (vm.count("check"))
{ {
if (keygen.verifyLicense()) if (keygen.verifyLicense())
{ {
std::cout << "License valid" << std::endl; std::cout << "License valid" << std::endl;
} }
else else
{ {
std::cout << "License not valid" << std::endl; std::cout << "License not valid" << std::endl;
} }
} }
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cout << "Exception caught: " << e.what() << std::endl; std::cout << "Exception caught: " << e.what() << std::endl;
return false; return false;
} }
return true; return true;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
process_command_line(argc, argv); process_command_line(argc, argv);
return 0; return 0;
} }
+100 -100
View File
@@ -7,144 +7,144 @@ class GeneratorTestSuite : public CxxTest::TestSuite
{ {
public: public:
void test_create_Keys_with_Version_and_check() void test_create_Keys_with_Version_and_check()
{ {
Generator generator; Generator generator;
generator.generateKeys(); generator.generateKeys();
std::string privateKey = generator.getPrivateKeyPEMFileAsString(); std::string privateKey = generator.getPrivateKeyPEMFileAsString();
std::string publicKey = generator.getPublicKeyPEMFileAsString(); std::string publicKey = generator.getPublicKeyPEMFileAsString();
// normally the size is 1886 // normally the size is 1886
// sometimes it could happen that the size is 1866 // sometimes it could happen that the size is 1866
// to prevent the test of failing both sizes are valid // to prevent the test of failing both sizes are valid
TS_ASSERT(privateKey.size() == 1886 || privateKey.size() == 1866); TS_ASSERT(privateKey.size() == 1886 || privateKey.size() == 1866);
TS_ASSERT_EQUALS(publicKey.size(), 451); TS_ASSERT_EQUALS(publicKey.size(), 451);
} }
void test_load_private_Key_from_file() void test_load_private_Key_from_file()
{ {
Generator generator; Generator generator;
generator.setCustomPrivateKeyFile("./data/GeneratorTestSuite/private-v2.pem"); generator.setCustomPrivateKeyFile("./data/GeneratorTestSuite/private-v2.pem");
bool ok = generator.loadPrivateKeyFromFile(); bool ok = generator.loadPrivateKeyFromFile();
TS_ASSERT(ok); TS_ASSERT(ok);
} }
void test_load_private_key_from_string() void test_load_private_key_from_string()
{ {
Generator generator; Generator generator;
bool ok = generator.loadPrivateKeyFromString(m_privateKey); bool ok = generator.loadPrivateKeyFromString(m_privateKey);
TS_ASSERT(ok); TS_ASSERT(ok);
} }
void test_create_volume_license_and_validate() void test_create_volume_license_and_validate()
{ {
Generator generator; Generator generator;
generator.generateKeys(); generator.generateKeys();
generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString());
License license; License license;
license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20)); license.loadFromString(generator.encodeLicense("TestUser", "VolumeLicense", 20));
TS_ASSERT_EQUALS(license.getNumberOfUsers(), 20); TS_ASSERT_EQUALS(license.getNumberOfUsers(), 20);
} }
void test_create_test_license_and_validate() void test_create_test_license_and_validate()
{ {
Generator generator; Generator generator;
generator.generateKeys(); generator.generateKeys();
generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString());
License license; License license;
license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString());
license.loadFromString(generator.encodeLicense("User", 10)); license.loadFromString(generator.encodeLicense("User", 10));
TS_ASSERT(license.isValid()); TS_ASSERT(license.isValid());
TS_ASSERT_EQUALS(license.getTimeLeft(), 10); TS_ASSERT_EQUALS(license.getTimeLeft(), 10);
license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString());
license.loadFromString(generator.encodeLicense("User", -10)); license.loadFromString(generator.encodeLicense("User", -10));
TS_ASSERT(!license.isValid()); TS_ASSERT(!license.isValid());
// -1 and not -10 since it means it is expired // -1 and not -10 since it means it is expired
TS_ASSERT_EQUALS(license.getTimeLeft(), -1); TS_ASSERT_EQUALS(license.getTimeLeft(), -1);
} }
void test_create_licenses_and_check_the_license_info() void test_create_licenses_and_check_the_license_info()
{ {
Generator generator; Generator generator;
generator.generateKeys(); generator.generateKeys();
License license; License license;
license.loadFromString(generator.encodeLicense("TestUser", "Private License", 1, "2017.4")); license.loadFromString(generator.encodeLicense("TestUser", "Private License", 1, "2017.4"));
std::string testInfo = "TestUser\nPrivate License\nValid up to version: 2017.4\n1 user"; std::string testInfo = "TestUser\nPrivate License\nValid up to version: 2017.4\n1 user";
TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo);
license.loadFromString(generator.encodeLicense("TestUser", "Volume License", 20, "2017.3")); license.loadFromString(generator.encodeLicense("TestUser", "Volume License", 20, "2017.3"));
testInfo = "TestUser\nVolume License\nValid up to version: 2017.3\n20 users"; testInfo = "TestUser\nVolume License\nValid up to version: 2017.3\n20 users";
TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo);
license.loadFromString(generator.encodeLicense("TestUser", "Private/Academic Single User License", 0, "2018.1")); license.loadFromString(generator.encodeLicense("TestUser", "Private/Academic Single User License", 0, "2018.1"));
testInfo = "TestUser\nPrivate/Academic Single User License\nValid up to version: 2018.1\nnot registered for commercial development"; testInfo = "TestUser\nPrivate/Academic Single User License\nValid up to version: 2018.1\nnot registered for commercial development";
TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo); TS_ASSERT_EQUALS(license.getLicenseInfo(), testInfo);
} }
void test_create_license_and_validate() void test_create_license_and_validate()
{ {
Generator generator; Generator generator;
generator.generateKeys(); generator.generateKeys();
generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString()); generator.loadPrivateKeyFromString(generator.getPrivateKeyPEMFileAsString());
License license; License license;
license.loadFromString(generator.encodeLicense("TestUser", "Private License", 20, "2017.3")); license.loadFromString(generator.encodeLicense("TestUser", "Private License", 20, "2017.3"));
license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString()); license.loadPublicKeyFromString(generator.getPublicKeyPEMFileAsString());
TS_ASSERT_EQUALS(license.getUser(), "TestUser"); TS_ASSERT_EQUALS(license.getUser(), "TestUser");
TS_ASSERT_EQUALS(license.getType(), "Private License"); TS_ASSERT_EQUALS(license.getType(), "Private License");
TS_ASSERT(license.isValid()); TS_ASSERT(license.isValid());
} }
private: private:
std::string m_privateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" std::string m_privateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
"MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQMWEDuADurEQ4T8kJO\n" "MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQMWEDuADurEQ4T8kJO\n"
"AgMDDUACASAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEGNDHYI3xozW5JBg\n" "AgMDDUACASAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEGNDHYI3xozW5JBg\n"
"pOLkhIMEggTQPwClVPQbk+AVqOAXndQqCs0Ad+eihbekCHt5zkRrYl6JbLq9fvfs\n" "pOLkhIMEggTQPwClVPQbk+AVqOAXndQqCs0Ad+eihbekCHt5zkRrYl6JbLq9fvfs\n"
"kGgn1j7xtFZflcZimlLaao8pLWybJfa3xGSJnlYrJsGVMlmKJw/ee3fZG7y4/Tgo\n" "kGgn1j7xtFZflcZimlLaao8pLWybJfa3xGSJnlYrJsGVMlmKJw/ee3fZG7y4/Tgo\n"
"FytAIP51He4YAZ+4fOXyOoAT0+k2DBekZHeMADX7eXVpCLQXuoQpECvf2IPd/de4\n" "FytAIP51He4YAZ+4fOXyOoAT0+k2DBekZHeMADX7eXVpCLQXuoQpECvf2IPd/de4\n"
"QKS12FRTd7Hkq7DwYs5D7aBizftcsB6FDqwKvvrheglMHfMt3/ChV8u3F8Zuqnh5\n" "QKS12FRTd7Hkq7DwYs5D7aBizftcsB6FDqwKvvrheglMHfMt3/ChV8u3F8Zuqnh5\n"
"S1NVl2b8geAnYWLEfN/V8SDu00heVHq+z2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lUMtZ4Bv1f\n"
"rXMH8igPXg62kzh/hjnEtl8y2+ySAs7RnZ0vUywaKcI0Tu5tqJSLtXSfJKAPmmzE\n" "rXMH8igPXg62kzh/hjnEtl8y2+ySAs7RnZ0vUywaKcI0Tu5tqJSLtXSfJKAPmmzE\n"
"QYsg70kpufXccznTYvLSbM99FIbC6s+cSkJ43Ku4dwbEal+9FfqK639i2X9hdsLc\n" "QYsg70kpufXccznTYvLSbM99FIbC6s+cSkJ43Ku4dwbEal+9FfqK639i2X9hdsLc\n"
"bYXIb0Fguo2IbWruBeSIc8lukOKCOVjanVZhfOCev8ye/Wz+SS8zbEixWZMzMCzC\n" "bYXIb0Fguo2IbWruBeSIc8lukOKCOVjanVZhfOCev8ye/Wz+SS8zbEixWZMzMCzC\n"
"if4dF/CUHnJK32IVlqKr9XGEe8ympqwMqN0eOdJn8IB+BQGCcaF6805D3R/OG4FU\n" "if4dF/CUHnJK32IVlqKr9XGEe8ympqwMqN0eOdJn8IB+BQGCcaF6805D3R/OG4FU\n"
"hedhIvg1cODtoi8ri41Pz7o1x9Ia0zZr7oxhQuSgHiTfRfFN0xhCFZ+sIfGNpgUs\n" "hedhIvg1cODtoi8ri41Pz7o1x9Ia0zZr7oxhQuSgHiTfRfFN0xhCFZ+sIfGNpgUs\n"
"54IZGQlI0nQHOK8h2NI0wp3uo+fDEjjVIjQxINOSBa5awSReBpBUW2I=\n" "54IZGQlI0nQHOK8h2NI0wp3uo+fDEjjVIjQxINOSBa5awSReBpBUW2I=\n"
"-----END ENCRYPTED PRIVATE KEY-----"; "-----END ENCRYPTED PRIVATE KEY-----";
std::string m_publicKey = "-----BEGIN PUBLIC KEY-----\n" std::string m_publicKey = "-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAugYah7LOU0ssdSnyDA8h\n" "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAugYah7LOU0ssdSnyDA8h\n"
"nOeW2lwE+NmZGxIKnqZKYNePY7Tg0c1pI0lfgJ2WYtxbubDFNDYk6bJF6mFg3jjN\n" "nOeW2lwE+NmZGxIKnqZKYNePY7Tg0c1pI0lfgJ2WYtxbubDFNDYk6bJF6mFg3jjN\n"
"gCdocj6pyyibIISbRst+gl/1FwI8vbIkfkJBoZtftO5mKVBbO5mmrxm32fDQs1vo\n" "gCdocj6pyyibIISbRst+gl/1FwI8vbIkfkJBoZtftO5mKVBbO5mmrxm32fDQs1vo\n"
"zgVxcWx3LXW87pzdQQYRACdkLSxPd+ADs+KNv6UxlOEvucDaenX3ckl3HdcWruL8\n" "zgVxcWx3LXW87pzdQQYRACdkLSxPd+ADs+KNv6UxlOEvucDaenX3ckl3HdcWruL8\n"
"xdYoM7z/C+PRShSGvY3wB7Y6A5IcdBmzsTR6xayCmTzzW83dmFzjCX5DSOJLHxq7\n" "xdYoM7z/C+PRShSGvY3wB7Y6A5IcdBmzsTR6xayCmTzzW83dmFzjCX5DSOJLHxq7\n"
"+Fs26xZU83P1boX7eg4TjMViTxJwsCCW/2wfsZAoF0cSYCxhrkSdMHroH7Edz1PM\n" "+Fs26xZU83P1boX7eg4TjMViTxJwsCCW/2wfsZAoF0cSYCxhrkSdMHroH7Edz1PM\n"
"fwIDAQAB\n" "fwIDAQAB\n"
"-----END PUBLIC KEY-----\n"; "-----END PUBLIC KEY-----\n";
}; };