44
55from datetime import datetime , timezone , MAXYEAR
66
7+ import cryptography
78import logging
89
910from cryptography import x509
1415
1516logger = logging .getLogger ("license" )
1617
18+ cryptographyVersion = [int (part ) for part in cryptography .__version__ .split ("." )]
19+ TZ = None if cryptographyVersion [0 ] < 42 else timezone .utc
20+
21+
22+ def cert_not_before (cert ):
23+ return cert .not_valid_before if cryptographyVersion [0 ] < 42 else cert .not_valid_before_utc
24+
25+
26+ def cert_not_after (cert ):
27+ return cert .not_valid_after if cryptographyVersion [0 ] < 42 else cert .not_valid_after_utc
28+
1729
1830class CertificateError (Exception ):
1931 pass
2032
21-
2233class GrommunioLicense (GenericObject ):
2334 @staticmethod
2435 def validate (cert ):
25- if cert is not None and not cert . not_valid_before_utc <= datetime .now (tz = timezone . utc ) <= cert . not_valid_after_utc :
36+ if cert is not None and not cert_not_before ( cert ) <= datetime .now (tz = TZ ) <= cert_not_after ( cert ) :
2637 raise CertificateError ("Certificate expired" )
2738
2839 @property
@@ -38,8 +49,8 @@ def _defaultLicense():
3849 file = None ,
3950 users = 5 ,
4051 product = "Community" ,
41- notBefore = datetime (1000 , 1 , 1 ),
42- notAfter = datetime (MAXYEAR , 12 , 31 , 23 , 59 , 59 ))
52+ notBefore = datetime (1000 , 1 , 1 , tzinfo = TZ ),
53+ notAfter = datetime (MAXYEAR , 12 , 31 , 23 , 59 , 59 , tzinfo = TZ ))
4354
4455
4556def _processCertificate (data ):
@@ -50,8 +61,8 @@ def _processCertificate(data):
5061 lic = GrommunioLicense (cert = cert , file = data )
5162 lic .users = int (exts .get ("1.3.6.1.4.1.56504.1.1" ))
5263 lic .product = exts .get ("1.3.6.1.4.1.56504.1.2" ).decode ("utf-8" ) if "1.3.6.1.4.1.56504.1.2" in exts else None
53- lic .notBefore = cert . not_valid_before_utc
54- lic .notAfter = cert . not_valid_after_utc
64+ lic .notBefore = cert_not_before ( cert )
65+ lic .notAfter = cert_not_after ( cert )
5566 return True , lic
5667 except ValueError :
5768 return False , "Bad certificate"
0 commit comments