55@author: Artem Los
66"""
77import base64
8- from Crypto .Signature import PKCS1_v1_5
9- from Crypto .Hash import SHA256
10- from Crypto .PublicKey import RSA
118import urllib .request
129import hashlib
1310from subprocess import Popen , PIPE
@@ -23,16 +20,67 @@ def get_SHA256(string):
2320 """
2421 return hashlib .sha256 (string .encode ("utf-8" )).hexdigest ()
2522
23+ @staticmethod
24+ def I2OSP (x , xLen ):
25+ if x > (1 << (8 * xLen )):
26+ return None
27+ Xrev = []
28+ for _ in range (0 , xLen ):
29+ x , m = divmod (x , 256 )
30+ Xrev .append (m )
31+ return bytes (reversed (Xrev ))
32+
33+ @staticmethod
34+ def OS2IP (X ):
35+ import binascii
36+ h = binascii .hexlify (X )
37+ return int (h , 16 )
38+
39+ @staticmethod
40+ def RSAVP1 (pair , s ):
41+ n , e = pair
42+ if s < 0 or n - 1 < s :
43+ return None
44+ return pow (s , e , n )
45+
46+ @staticmethod
47+ def EMSA_PKCS1_V15_ENCODE (M , emLen ):
48+ import hashlib
49+ h = hashlib .sha256 ()
50+ h .update (M )
51+ H = h .digest ()
52+
53+ T = bytes ([0x30 , 0x31 , 0x30 , 0x0d , 0x06 , 0x09 , 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x01 , 0x05 , 0x00 , 0x04 , 0x20 ]) + H
54+ tLen = len (T )
55+ if emLen < tLen + 11 :
56+ return None
57+ PS = bytes ([0xff for _ in range (emLen - tLen - 3 )])
58+ return b"" .join ([b"\x00 \x01 " , PS , b"\x00 " , T ])
59+
60+ @staticmethod
61+ def RSAASSA_PKCS1_V15_VERIFY (pair , M , S ):
62+ n , e = pair
63+ s = HelperMethods .OS2IP (S )
64+ m = HelperMethods .RSAVP1 ((n ,e ), s )
65+ if m is None : return False
66+ EM = HelperMethods .I2OSP (m , 256 )
67+ if EM is None : return False
68+ EM2 = HelperMethods .EMSA_PKCS1_V15_ENCODE (M , 256 ) # Can return None, but it's OK since EM is not None
69+ return EM == EM2
70+
2671 @staticmethod
2772 def verify_signature (response , rsaPublicKey ):
2873 """
2974 Verifies a signature from .NET RSACryptoServiceProvider.
3075 """
31- cryptoPubKey = RSA .construct ((HelperMethods .base642int (rsaPublicKey .modulus ),\
32- HelperMethods .base642int (rsaPublicKey .exponent )))
33- h = SHA256 .new (base64 .b64decode (response .license_key .encode ("utf-8" )))
34- verifier = PKCS1_v1_5 .new (cryptoPubKey )
35- return verifier .verify (h , base64 .b64decode (response .signature .encode ("utf-8" )))
76+
77+ n = HelperMethods .OS2IP (base64 .b64decode (rsaPublicKey .modulus ))
78+ e = HelperMethods .OS2IP (base64 .b64decode (rsaPublicKey .exponent ))
79+
80+ m = base64 .b64decode (response .license_key )
81+ r = base64 .b64decode (response .signature )
82+
83+ return HelperMethods .RSAASSA_PKCS1_V15_VERIFY ((n ,e ), m , r )
3684
3785 @staticmethod
3886 def int2base64 (num ):
0 commit comments