Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability create issuing authority instances without specifying… #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions license++/issuing-authority.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class IssuingAuthority
{
public:
IssuingAuthority(const std::string& id, const std::string& name,
const std::string& keypair, unsigned int maxValidity,
const std::string& keyPair, unsigned int maxValidity,
bool active = true);
IssuingAuthority(const std::string& id, const std::string& name,
const std::string& privateKey, const std::string& publicKey, unsigned int maxValidity,
bool active = true);

IssuingAuthority(const IssuingAuthority&);
Expand Down Expand Up @@ -82,7 +85,8 @@ class IssuingAuthority
private:
std::string m_id;
std::string m_name;
std::string m_keypair;
std::string m_privateKey;
std::string m_publicKey;
bool m_active;
unsigned int m_maxValidity;
};
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ using namespace licensepp;

std::string Base64::decode(const std::string& encoded)
{
if (encoded.empty()) return encoded;
return Ripe::base64Decode(encoded);
}

std::string Base64::encode(const std::string& raw)
{
if (raw.empty()) return raw;
return Ripe::base64Encode(raw);
}
44 changes: 25 additions & 19 deletions src/issuing-authority.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ using namespace licensepp;

IssuingAuthority::IssuingAuthority(const std::string& id,
const std::string& name,
const std::string& keypair,
const std::string& keyPair,
unsigned int maxValidity,
bool active) : IssuingAuthority::IssuingAuthority(id, name, "", "", maxValidity) {
const auto separatorPos = keyPair.find(":");
if (separatorPos == std::string::npos) {
throw LicenseException("Issuing authority could not be loaded. Invalid keypair");
}

m_privateKey = Base64::decode(keyPair.substr(0, separatorPos));
m_publicKey = Base64::decode(keyPair.substr(separatorPos + 1));
}

IssuingAuthority::IssuingAuthority(const std::string& id,
const std::string& name,
const std::string& privateKey,
const std::string& publicKey,
unsigned int maxValidity,
bool active) :
m_id(id),
m_name(name),
m_keypair(keypair),
m_privateKey(Base64::decode(privateKey)),
m_publicKey(Base64::decode(publicKey)),
m_active(active),
m_maxValidity(maxValidity)
{
Expand All @@ -42,7 +58,8 @@ IssuingAuthority::IssuingAuthority(const std::string& id,
IssuingAuthority::IssuingAuthority(const IssuingAuthority& other):
m_id(other.m_id),
m_name(other.m_name),
m_keypair(other.m_keypair),
m_privateKey(other.m_privateKey),
m_publicKey(other.m_publicKey),
m_active(other.m_active),
m_maxValidity(other.m_maxValidity)
{
Expand All @@ -52,7 +69,8 @@ IssuingAuthority& IssuingAuthority::operator=(IssuingAuthority other)
{
std::swap(m_id, other.m_id);
std::swap(m_name, other.m_name);
std::swap(m_keypair, other.m_keypair);
std::swap(m_privateKey, other.m_privateKey);
std::swap(m_publicKey, other.m_publicKey);
std::swap(m_active, other.m_active);
std::swap(m_maxValidity, other.m_maxValidity);

Expand Down Expand Up @@ -100,13 +118,8 @@ License IssuingAuthority::issue(const std::string& licensee,
throw LicenseException("Failed to issue the license; " + std::string(e.what()));
}
}
// issuing authority signs this license
auto separatorPos = m_keypair.find(":");
if (separatorPos == std::string::npos) {
throw LicenseException("Issuing authority could not be loaded. Invalid keypair");
}

const RSA::PrivateKey key = RSA::loadPrivateKey(Base64::decode(m_keypair.substr(0, separatorPos)), secret);

const RSA::PrivateKey key = RSA::loadPrivateKey(m_privateKey, secret);

try {
license.setAuthoritySignature(RSA::sign(license.raw(), key, secret));
Expand All @@ -128,14 +141,7 @@ bool IssuingAuthority::validate(const License* license,
{
bool result = false;
try {

// issuing authority signs this license
auto separatorPos = m_keypair.find(":");
if (separatorPos == std::string::npos) {
throw LicenseException("Issuing authority could not be loaded. Invalid keypair");
}

RSA::PublicKey key = RSA::loadPublicKey(Base64::decode(m_keypair.substr(separatorPos + 1)));
RSA::PublicKey key = RSA::loadPublicKey(m_publicKey);

result = RSA::verify(license->raw(), license->authoritySignature(), key);
if (!result) {
Expand Down