-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pycrypto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
|
||
module License | ||
(readLicense) where | ||
|
||
import Paths_toodles | ||
|
||
import Control.Exception | ||
import Data.Aeson | ||
import Data.Aeson.Encode.Pretty | ||
import qualified Data.ByteString.Base64.Lazy as B64 | ||
import qualified Data.ByteString.Lazy.Char8 as LB | ||
import Data.Maybe | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import Data.Time.Clock.POSIX | ||
import GHC.Generics | ||
import System.Directory | ||
import System.Process | ||
|
||
data UserTier | ||
= BadLicense | ||
| NoLiscense | ||
| Individual | ||
| Commercial | ||
deriving (Show, Eq, Ord, Generic, ToJSON, FromJSON) | ||
|
||
data License = License { | ||
payload :: ToodlesLicense, | ||
encoded :: Text, | ||
payloadSignature :: Text | ||
} deriving (Generic, FromJSON, ToJSON, Show) | ||
|
||
data ToodlesLicense = ToodlesLicense | ||
{ validStart :: Integer | ||
, validEnd :: Integer | ||
, email :: Text | ||
, reference :: Text | ||
, product :: Text | ||
} deriving (Generic, FromJSON, ToJSON, Show) | ||
|
||
readLicense :: FilePath -> FilePath -> IO (Either String UserTier) | ||
readLicense publicKeyPath licensePath = do | ||
licenseExists <- doesFileExist licensePath | ||
putStrLn licensePath | ||
if not licenseExists then | ||
return $ Right NoLiscense | ||
else do | ||
parsedContents <- eitherDecodeFileStrict licensePath | ||
either (return . Left) (isLicenseValid publicKeyPath) parsedContents | ||
|
||
isLicenseValid :: FilePath -> License -> IO (Either String UserTier) | ||
isLicenseValid publicKeyPath (License _ encodedPayload sig) = do | ||
dataDir <- getDataDir | ||
now <- ((* 1000) . round) `fmap` getPOSIXTime | ||
let args = | ||
[ dataDir ++ "/verify.py" | ||
, publicKeyPath | ||
, T.unpack sig | ||
, T.unpack encodedPayload | ||
] | ||
decodedPayload = | ||
decode (B64.decodeLenient . LB.pack $ T.unpack encodedPayload) | ||
result <- | ||
catch | ||
(readProcess "python" args "") | ||
(\(e :: IOException) -> | ||
return $ displayException e) | ||
return $ | ||
let validated = ("True" == (T.strip $ T.pack result)) | ||
in if validated && (maybe 0 validEnd decodedPayload >= now) | ||
then Right Commercial | ||
else Left "Invalid license file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvrVba8B99vGOUvHUSNSv | ||
T6idquIYp3CdhRww2VckyrVwqCeFNePm6EDhHAsG4SVd94HQDpZdJd/Oohrqlt1I | ||
UccaPse4W3J9nMJx82H9BK91VuFWLKvPAEbeQn+uStRsSeAPeIBVo+y4i5cYPqdV | ||
eN6me2r79/cNwD21auTGTsTLJzY+RaiRA+pnmJuXvxhMjyER/GUn6hikJWrSF+6e | ||
9txokiJR9r2hCv2JQqYHWp/moF/nd/25aTrGEgiWz+RGY15P4UUk1Ju1DmNUQA4/ | ||
RmOe1XnXw0X8aoKhuhTjZw5COsq5uHTq7tqC/CemQpldsETqj19XYO3TNi7ojmfa | ||
EQIDAQAB | ||
-----END PUBLIC KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
from Crypto.PublicKey import RSA | ||
from Crypto.Signature import PKCS1_v1_5 | ||
from Crypto.Hash import SHA512 | ||
from base64 import b64decode, b64encode | ||
|
||
def verify_sign(public_key_loc, signature, data): | ||
pub_key = open(public_key_loc, "r").read() | ||
rsakey = RSA.importKey(pub_key) | ||
signer = PKCS1_v1_5.new(rsakey) | ||
digest = SHA512.new() | ||
digest.update(data) | ||
if signer.verify(digest, b64decode(signature)): | ||
return True | ||
return False | ||
|
||
if __name__ == "__main__": | ||
print(verify_sign(sys.argv[1], sys.argv[2], sys.argv[3])) | ||
|