Skip to content

Commit 3df4ae2

Browse files
authored
Add support for Metadata property in Key.Activate (#67)
* First commit * Update * Update internal.py * Update internal.py * Update models.py * Update setup.py
1 parent c4db4bf commit 3df4ae2

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

licensing/internal.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,33 @@ def RSAVP1(pair, s):
7070
return pow(s, e, n)
7171

7272
@staticmethod
73-
def EMSA_PKCS1_V15_ENCODE(M, emLen):
73+
def EMSA_PKCS1_V15_ENCODE(M, emLen, hlen = 256):
7474
import hashlib
7575
h = hashlib.sha256()
76+
if hlen == 512:
77+
h = hashlib.sha512()
7678
h.update(M)
7779
H = h.digest()
7880

7981
T = bytes([0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20]) + H
82+
if hlen == 512:
83+
T = bytes([0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40]) + H
84+
8085
tLen = len(T)
8186
if emLen < tLen + 11:
8287
return None
8388
PS = bytes([0xff for _ in range(emLen - tLen - 3)])
8489
return b"".join([b"\x00\x01", PS, b"\x00", T])
85-
90+
8691
@staticmethod
87-
def RSAASSA_PKCS1_V15_VERIFY(pair, M, S):
92+
def RSAASSA_PKCS1_V15_VERIFY(pair, M, S, l=256):
8893
n, e = pair
8994
s = HelperMethods.OS2IP(S)
9095
m = HelperMethods.RSAVP1((n,e), s)
9196
if m is None: return False
9297
EM = HelperMethods.I2OSP(m, 256)
9398
if EM is None: return False
94-
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256)
99+
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256, l)
95100
if EM2 is None: return False
96101

97102
try:
@@ -114,6 +119,23 @@ def verify_signature(response, rsaPublicKey):
114119

115120
return HelperMethods.RSAASSA_PKCS1_V15_VERIFY((n,e), m, r)
116121

122+
@staticmethod
123+
def verify_signature_metadata(signature, rsaPublicKey):
124+
125+
import base64
126+
import json
127+
128+
n = HelperMethods.OS2IP(base64.b64decode(rsaPublicKey.modulus))
129+
e = HelperMethods.OS2IP(base64.b64decode(rsaPublicKey.exponent))
130+
131+
data = json.loads(base64.b64decode(signature))
132+
133+
d = base64.b64decode(data["Data"])
134+
s = base64.b64decode(data["Signature"])
135+
136+
return [HelperMethods.RSAASSA_PKCS1_V15_VERIFY((n,e), d,s, l=512), d]
137+
138+
117139
@staticmethod
118140
def int2base64(num):
119141
return base64.b64encode(int.to_bytes(num), byteorder='big')

licensing/methods.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,21 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
6161
else:
6262
try:
6363
if HelperMethods.verify_signature(response, pubkey):
64-
return (LicenseKey.from_response(response), response.message)
64+
if metadata:
65+
66+
try:
67+
metadata_s = HelperMethods.verify_signature_metadata(response.metadata["signature"], pubkey)
68+
69+
if metadata_s[0]:
70+
return (LicenseKey.from_response(response), response.message, json.loads(metadata_s[1]))
71+
else:
72+
return (LicenseKey.from_response(response), response.message, "Signature check for metadata object failed.")
73+
except:
74+
return (LicenseKey.from_response(response), response.message, "Signature check for metadata object failed.")
75+
76+
77+
else:
78+
return (LicenseKey.from_response(response), response.message)
6579
else:
6680
return (None, "The signature check failed.")
6781
except Exception as ex:

licensing/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ def __load_activated_machines(obj):
164164

165165
class Response:
166166

167-
def __init__(self, license_key, signature, result, message):
167+
def __init__(self, license_key, signature, result, message, metadata=None):
168168
self.license_key = license_key
169169
self.signature = signature
170170
self.result = result
171171
self.message = message
172+
self.metadata = metadata
172173

173174
@staticmethod
174175
def from_string(responseString):
@@ -178,6 +179,7 @@ def from_string(responseString):
178179
signature = ""
179180
result = 0
180181
message = ""
182+
metadata = None
181183

182184
if "licensekey" in obj:
183185
licenseKey = obj["licensekey"]
@@ -192,8 +194,13 @@ def from_string(responseString):
192194
result = obj["result"]
193195
else:
194196
result = 1
197+
198+
if "metadata" in obj:
199+
metadata = obj["metadata"]
200+
201+
195202

196-
return Response(licenseKey, signature, result, message)
203+
return Response(licenseKey, signature, result, message, metadata)
197204

198205
class RSAPublicKey:
199206

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
setup(
33
name = 'licensing', # How you named your package folder (MyLib)
44
packages = ['licensing'], # Chose the same as "name"
5-
version = '0.41', # Start with a small number and increase it with every change you make
5+
version = '0.42', # Start with a small number and increase it with every change you make
66
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
77
description = 'Client library for Cryptolens licensing Web API.', # Give a short description about your library
88
author = 'Cryptolens AB', # Type in your name
99
author_email = '[email protected]', # Type in your E-Mail
1010
url = 'https://cryptolens.io', # Provide either the link to your github or to your website
11-
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_41.tar.gz', # I explain this later on
11+
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_42.tar.gz', # I explain this later on
1212
keywords = ['software licensing', 'licensing library', 'cryptolens'], # Keywords that define your package best
1313
classifiers=[
1414
#'Development Status :: 5 - Stable', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package

0 commit comments

Comments
 (0)