From b72db7ae861f48cd2f1b25d3d0b769e86eb6ffee Mon Sep 17 00:00:00 2001 From: Rene Bartkowiak Date: Wed, 29 Jan 2014 21:45:32 +0100 Subject: [PATCH] Initial commit --- README.md | 0 composer.json | 23 ++++++ src/Bart/Licensee/License.php | 138 ++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 README.md create mode 100755 composer.json create mode 100755 src/Bart/Licensee/License.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..8b2199f --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "bart/licensee", + "description": "Simple PHP class for license generation and verification based on openssl signatures", + "keywords": ["license", "openssl", "signature"], + "license": "MIT", + "authors": [ + { + "name": "Rene Bartkowiak", + "email": "info@rene-bartkowiak.com" + } + ], + "require": { + "php": ">=5.3.0", + "illuminate/support": "4.1.x", + "claudusd/cryptography": "dev-master" + }, + "autoload": { + "psr-0": { + "Bart\\Licensee": "src/" + } + }, + "minimum-stability": "dev" +} \ No newline at end of file diff --git a/src/Bart/Licensee/License.php b/src/Bart/Licensee/License.php new file mode 100755 index 0000000..2df36cc --- /dev/null +++ b/src/Bart/Licensee/License.php @@ -0,0 +1,138 @@ + 'Company', 'version' => '1.0', 'valid_until' => time() + 60*60*24*7, 'key' => sha1(uniqid(true))); + * $lic = new License(); + * $lic::createKeypair('/tmp/private.pem', '/tmp/public.pem'); + * $lic->createLicense($data, '/tmp/private.pem', '/tmp/license.lic'); + * $res = $lic->getDataAndValidateLicense('/tmp/license.lic', '/tmp/public.pem'); + * var_dump($res); + * + * @package Bart\Licensee + */ +class License { + + private $sspk, $data, $signature, $license; + + public function __construct() { + $this->setSspk(new SignatureSignPrivateKey()); + } + + /** + * @param array $data Data to sign + * @param string $private_key_path Path to private key file + * @param bool|string $license_path Path to file where created license_path should be stored or false for no license_path storing + * @return string License data in json format + */ + public function createLicense($data, $private_key_path, $license_path = false) { + $this->setData(json_encode($data)); + $this->setSignature(base64_encode($this->getSspk()->sign($this->getData(), file_get_contents($private_key_path)))); + $license = json_encode(array('data' => $this->getData(), + 'signature' => $this->getSignature())); + + $this->setLicense($license); + + if ($license_path !== false) { + $this->storeLicense($license_path); + } + + return $license; + } + + /** + * @param string $path Path to license_path file + * @return void + */ + public function storeLicense($path) { + file_put_contents($path, $this->getLicense()); + } + + /** + * @param string $license_path Path to license_path file + * @param string $public_key_path Path to public key file + * @return bool + */ + public function validateLicense($license_path, $public_key_path) { + $this->setLicense(file_get_contents($license_path)); + $this->processLicense(); + + return $this->getSspk()->verify($this->getData(), base64_decode($this->getSignature()), file_get_contents($public_key_path)); + } + + /** + * @param string $license_path Path to license_path file + * @param string $public_key_path Path to public key file + * @return bool|string Data when verified, false if not + */ + public function getDataAndValidateLicense($license_path, $public_key_path) { + if ($this->validateLicense($license_path, $public_key_path)) { + return $this->getData(); + } + else { + return false; + } + + } + + /** + * @param string|null $private_key_path Path to private key file or false for no private key + * @param string|null $public_key_path Path to public key file or false for no public key + */ + public static function createKeypair($private_key_path = false, $public_key_path = false) { + $key = new KeyGenerationSHA512RSA4096Bits(); + + if ($private_key_path) { + file_put_contents($private_key_path, $key->getPrivateKey()); + } + + if ($public_key_path) { + file_put_contents($public_key_path, $key->getPublicKey()); + } + } + + private function processLicense() { + $this->setData(json_decode($this->getLicense())->data); + $this->setSignature(json_decode($this->getLicense())->signature); + } + + private function setLicense($license) { + $this->license = $license; + } + + private function getLicense() { + return $this->license; + } + + private function setData($data) { + $this->data = $data; + } + + private function getData() { + return $this->data; + } + + private function setSignature($signature) { + $this->signature = $signature; + } + + private function getSignature() { + return $this->signature; + } + + private function setSspk($sspk) { + $this->sspk = $sspk; + } + + private function getSspk() { + return $this->sspk; + } + +} \ No newline at end of file