diff --git a/poetry.lock b/poetry.lock index 93e085568..d250f481c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1248,24 +1248,6 @@ snappy = ["python-snappy"] test = ["pytest (>=8.2)", "pytest-asyncio (>=0.24.0)"] zstd = ["zstandard"] -[[package]] -name = "pyopenssl" -version = "24.2.1" -description = "Python wrapper module around the OpenSSL library" -optional = false -python-versions = ">=3.7" -files = [ - {file = "pyOpenSSL-24.2.1-py3-none-any.whl", hash = "sha256:967d5719b12b243588573f39b0c677637145c7a1ffedcd495a487e58177fbb8d"}, - {file = "pyopenssl-24.2.1.tar.gz", hash = "sha256:4247f0dbe3748d560dcbb2ff3ea01af0f9a1a001ef5f7c4c647956ed8cbf0e95"}, -] - -[package.dependencies] -cryptography = ">=41.0.5,<44" - -[package.extras] -docs = ["sphinx (!=5.2.0,!=5.2.0.post0,!=7.2.5)", "sphinx-rtd-theme"] -test = ["pretend", "pytest (>=3.0.1)", "pytest-rerunfailures"] - [[package]] name = "pytest" version = "8.3.4" @@ -1306,31 +1288,6 @@ pytest = ">=4.6" [package.extras] testing = ["fields", "hunter", "process-tests", "pytest-xdist", "virtualenv"] -[[package]] -name = "python-dateutil" -version = "2.9.0.post0" -description = "Extensions to the standard Python datetime module" -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" -files = [ - {file = "python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3"}, - {file = "python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427"}, -] - -[package.dependencies] -six = ">=1.5" - -[[package]] -name = "pytz" -version = "2025.1" -description = "World timezone definitions, modern and historical" -optional = false -python-versions = "*" -files = [ - {file = "pytz-2025.1-py2.py3-none-any.whl", hash = "sha256:89dd22dca55b46eac6eda23b2d72721bf1bdfef212645d81513ef5d03038de57"}, - {file = "pytz-2025.1.tar.gz", hash = "sha256:c2db42be2a2518b28e65f9207c4d05e6ff547d1efa4086469ef855e4ab70178e"}, -] - [[package]] name = "pyyaml" version = "6.0.2" @@ -1724,42 +1681,6 @@ files = [ docs = ["myst-parser", "pydata-sphinx-theme", "sphinx"] test = ["argcomplete (>=3.0.3)", "mypy (>=1.7.0)", "pre-commit", "pytest (>=7.0,<8.2)", "pytest-mock", "pytest-mypy-testing"] -[[package]] -name = "types-pyopenssl" -version = "23.3.0.20240106" -description = "Typing stubs for pyOpenSSL" -optional = false -python-versions = ">=3.8" -files = [ - {file = "types-pyOpenSSL-23.3.0.20240106.tar.gz", hash = "sha256:3d6f3462bec0c260caadf93fbb377225c126661b779c7d9ab99b6dad5ca10db9"}, - {file = "types_pyOpenSSL-23.3.0.20240106-py3-none-any.whl", hash = "sha256:47a7eedbd18b7bcad17efebf1c53416148f5a173918a6d75027e75e32fe039ae"}, -] - -[package.dependencies] -cryptography = ">=35.0.0" - -[[package]] -name = "types-python-dateutil" -version = "2.9.0.20241206" -description = "Typing stubs for python-dateutil" -optional = false -python-versions = ">=3.8" -files = [ - {file = "types_python_dateutil-2.9.0.20241206-py3-none-any.whl", hash = "sha256:e248a4bc70a486d3e3ec84d0dc30eec3a5f979d6e7ee4123ae043eedbb987f53"}, - {file = "types_python_dateutil-2.9.0.20241206.tar.gz", hash = "sha256:18f493414c26ffba692a72369fea7a154c502646301ebfe3d56a04b3767284cb"}, -] - -[[package]] -name = "types-pytz" -version = "2022.7.1.2" -description = "Typing stubs for pytz" -optional = false -python-versions = "*" -files = [ - {file = "types-pytz-2022.7.1.2.tar.gz", hash = "sha256:487d3e8e9f4071eec8081746d53fa982bbc05812e719dcbf2ebf3d55a1a4cd28"}, - {file = "types_pytz-2022.7.1.2-py3-none-any.whl", hash = "sha256:40ca448a928d566f7d44ddfde0066e384f7ffbd4da2778e42a4570eaca572446"}, -] - [[package]] name = "types-requests" version = "2.32.0.20241016" @@ -1971,4 +1892,4 @@ s2repoze = ["paste", "repoze.who", "zope.interface"] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "03ec3d72ebbfd582b62c153e6921c3032ff637943724b8a9222bc9593eae71e0" +content-hash = "9c587241c4a4516cb2760c18d5b2269de92b2b82060c22894ac974ee52224e80" diff --git a/pyproject.toml b/pyproject.toml index 985692043..f9d2a522e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,14 +37,11 @@ parse_xsd2 = "saml2.tools.parse_xsd2:main" [tool.poetry.dependencies] python = "^3.9" -cryptography = ">=3.1" +cryptography = ">=40.0" defusedxml = "*" importlib-metadata = {version = ">=1.7.0", python = "<3.8"} importlib-resources = {python = "<3.9", version = "*"} paste = {optional = true, version = "*"} -pyopenssl = "<24.3.0" -python-dateutil = "*" -pytz = "*" "repoze.who" = {optional = true, version = "*"} requests = "^2" xmlschema = "^2" @@ -66,9 +63,6 @@ flake8-bugbear = "^22.8.23" flake8-logging-format = "^0.7.5" ipdb = "^0.13.9" mypy = "^1.0.0" -types-pyopenssl = "^23.0.0.3" -types-python-dateutil = "^2.8.19.6" -types-pytz = "^2022.7.1.0" types-setuptools = "^67.2.0.1" types-six = "^1.16.21.4" types-requests = "^2.28.11.12" diff --git a/src/saml2/cert.py b/src/saml2/cert.py index c5f626601..2722d5585 100644 --- a/src/saml2/cert.py +++ b/src/saml2/cert.py @@ -5,9 +5,11 @@ from os import remove from os.path import join -from OpenSSL import crypto -import dateutil.parser -import pytz +from cryptography import x509 +from cryptography.exceptions import InvalidSignature +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.x509.oid import NameOID import saml2.cryptography.pki @@ -36,7 +38,6 @@ def create_certificate( valid_to=315360000, sn=1, key_length=1024, - hash_alg="sha256", write_to_file=False, cert_dir="", cipher_passphrase=None, @@ -87,8 +88,6 @@ def create_certificate( is 1. :param key_length: Length of the key to be generated. Defaults to 1024. - :param hash_alg: Hash algorithm to use for the key. Default - is sha256. :param write_to_file: True if you want to write the certificate to a file. The method will then return a tuple with path to certificate file and @@ -131,49 +130,68 @@ def create_certificate( k_f = join(cert_dir, key_file) # create a key pair - k = crypto.PKey() - k.generate_key(crypto.TYPE_RSA, key_length) + k = rsa.generate_private_key( + public_exponent=65537, + key_size=key_length, + ) # create a self-signed cert - cert = crypto.X509() + builder = x509.CertificateBuilder() if request: - cert = crypto.X509Req() + builder = x509.CertificateSigningRequestBuilder() if len(cert_info["country_code"]) != 2: raise WrongInput("Country code must be two letters!") - cert.get_subject().C = cert_info["country_code"] - cert.get_subject().ST = cert_info["state"] - cert.get_subject().L = cert_info["city"] - cert.get_subject().O = cert_info["organization"] # noqa: E741 - cert.get_subject().OU = cert_info["organization_unit"] - cert.get_subject().CN = cn + subject_name = x509.Name([ + x509.NameAttribute(NameOID.COUNTRY_NAME, + cert_info["country_code"]), + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, + cert_info["state"]), + x509.NameAttribute(NameOID.LOCALITY_NAME, + cert_info["city"]), + x509.NameAttribute(NameOID.ORGANIZATION_NAME, + cert_info["organization"]), + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, + cert_info["organization_unit"]), + x509.NameAttribute(NameOID.COMMON_NAME, cn), + ]) + builder = builder.subject_name(subject_name) if not request: - cert.set_serial_number(sn) - cert.gmtime_adj_notBefore(valid_from) # Valid before present time - cert.gmtime_adj_notAfter(valid_to) # 3 650 days - cert.set_issuer(cert.get_subject()) - cert.set_pubkey(k) - cert.sign(k, hash_alg) + now = datetime.datetime.now(datetime.timezone.utc) + builder = builder.serial_number( + sn, + ).not_valid_before( + now + datetime.timedelta(seconds=valid_from), + ).not_valid_after( + now + datetime.timedelta(seconds=valid_to), + ).issuer_name( + subject_name, + ).public_key( + k.public_key(), + ) + cert = builder.sign(k, hashes.SHA256()) try: - if request: - tmp_cert = crypto.dump_certificate_request(crypto.FILETYPE_PEM, cert) - else: - tmp_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert) - tmp_key = None + tmp_cert = cert.public_bytes(serialization.Encoding.PEM) + key_encryption = None if cipher_passphrase is not None: passphrase = cipher_passphrase["passphrase"] if isinstance(cipher_passphrase["passphrase"], str): passphrase = passphrase.encode("utf-8") - tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k, cipher_passphrase["cipher"], passphrase) + key_encryption = serialization.BestAvailableEncryption(passphrase) else: - tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k) + key_encryption = serialization.NoEncryption() + tmp_key = k.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.TraditionalOpenSSL, + encryption_algorithm=key_encryption, + ) if write_to_file: - with open(c_f, "w") as fc: - fc.write(tmp_cert.decode("utf-8")) - with open(k_f, "w") as fk: - fk.write(tmp_key.decode("utf-8")) + with open(c_f, "wb") as fc: + fc.write(tmp_cert) + with open(k_f, "wb") as fk: + fk.write(tmp_key) return c_f, k_f return tmp_cert, tmp_key except Exception as ex: @@ -198,7 +216,6 @@ def create_cert_signed_certificate( sign_cert_str, sign_key_str, request_cert_str, - hash_alg="sha256", valid_from=0, valid_to=315360000, sn=1, @@ -222,8 +239,6 @@ def create_cert_signed_certificate( the requested certificate. If you only have a file use the method read_str_from_file to get a string representation. - :param hash_alg: Hash algorithm to use for the key. Default - is sha256. :param valid_from: When the certificate starts to be valid. Amount of seconds from when the certificate is generated. @@ -237,27 +252,29 @@ def create_cert_signed_certificate( :return: String representation of the signed certificate. """ - ca_cert = crypto.load_certificate(crypto.FILETYPE_PEM, sign_cert_str) - ca_key = None - if passphrase is not None: - ca_key = crypto.load_privatekey(crypto.FILETYPE_PEM, sign_key_str, passphrase) - else: - ca_key = crypto.load_privatekey(crypto.FILETYPE_PEM, sign_key_str) - req_cert = crypto.load_certificate_request(crypto.FILETYPE_PEM, request_cert_str) - - cert = crypto.X509() - cert.set_subject(req_cert.get_subject()) - cert.set_serial_number(sn) - cert.gmtime_adj_notBefore(valid_from) - cert.gmtime_adj_notAfter(valid_to) - cert.set_issuer(ca_cert.get_subject()) - cert.set_pubkey(req_cert.get_pubkey()) - cert.sign(ca_key, hash_alg) - - cert_dump = crypto.dump_certificate(crypto.FILETYPE_PEM, cert) - if isinstance(cert_dump, str): - return cert_dump - return cert_dump.decode("utf-8") + if isinstance(sign_cert_str, str): + sign_cert_str = sign_cert_str.encode("utf-8") + ca_cert = x509.load_pem_x509_certificate(sign_cert_str) + ca_key = serialization.load_pem_private_key( + sign_key_str, password=passphrase) + req_cert = x509.load_pem_x509_csr(request_cert_str) + + now = datetime.datetime.now(datetime.timezone.utc) + cert = x509.CertificateBuilder().subject_name( + req_cert.subject, + ).serial_number( + sn, + ).not_valid_before( + now + datetime.timedelta(seconds=valid_from), + ).not_valid_after( + now + datetime.timedelta(seconds=valid_to), + ).issuer_name( + ca_cert.subject, + ).public_key( + req_cert.public_key(), + ).sign(ca_key, hashes.SHA256()) + + return cert.public_bytes(serialization.Encoding.PEM).decode("utf-8") def verify_chain(self, cert_chain_str_list, cert_str): """ @@ -276,13 +293,6 @@ def verify_chain(self, cert_chain_str_list, cert_str): cert_str = tmp_cert_str return (True, "Signed certificate is valid and correctly signed by CA " "certificate.") - def certificate_not_valid_yet(self, cert): - starts_to_be_valid = dateutil.parser.parse(cert.get_notBefore()) - now = pytz.UTC.localize(datetime.datetime.utcnow()) - if starts_to_be_valid < now: - return False - return True - def verify(self, signing_cert_str, cert_str): """ Verifies if a certificate is valid and signed by a given certificate. @@ -303,34 +313,34 @@ def verify(self, signing_cert_str, cert_str): Message = Why the validation failed. """ try: - ca_cert = crypto.load_certificate(crypto.FILETYPE_PEM, signing_cert_str) - cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str) - - if self.certificate_not_valid_yet(ca_cert): + if isinstance(signing_cert_str, str): + signing_cert_str = signing_cert_str.encode("utf-8") + if isinstance(cert_str, str): + cert_str = cert_str.encode("utf-8") + ca_cert = x509.load_pem_x509_certificate(signing_cert_str) + cert = x509.load_pem_x509_certificate(cert_str) + now = datetime.datetime.now(datetime.timezone.utc) + + if ca_cert.not_valid_before_utc >= now: return False, "CA certificate is not valid yet." - if ca_cert.has_expired() == 1: + if ca_cert.not_valid_after_utc < now: return False, "CA certificate is expired." - if cert.has_expired() == 1: + if cert.not_valid_after_utc < now: return False, "The signed certificate is expired." - if self.certificate_not_valid_yet(cert): + if cert.not_valid_before_utc >= now: return False, "The signed certificate is not valid yet." - if ca_cert.get_subject().CN == cert.get_subject().CN: + if ca_cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME) == \ + cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME): return False, ("CN may not be equal for CA certificate and the " "signed certificate.") - cert_algorithm = cert.get_signature_algorithm() - cert_algorithm = cert_algorithm.decode("ascii") - cert_str = cert_str.encode("ascii") - - cert_crypto = saml2.cryptography.pki.load_pem_x509_certificate(cert_str) - try: - crypto.verify(ca_cert, cert_crypto.signature, cert_crypto.tbs_certificate_bytes, cert_algorithm) + cert.verify_directly_issued_by(ca_cert) return True, "Signed certificate is valid and correctly signed by CA certificate." - except crypto.Error as e: + except (ValueError, TypeError, InvalidSignature) as e: return False, f"Certificate is incorrectly signed: {str(e)}" except Exception as e: return False, f"Certificate is not valid for an unknown reason. {str(e)}" @@ -352,8 +362,14 @@ def read_cert_from_file(cert_file, cert_type="pem"): data = fp.read() try: - cert = saml2.cryptography.pki.load_x509_certificate(data, cert_type) - pem_data = saml2.cryptography.pki.get_public_bytes_from_cert(cert) + cert = None + if cert_type == "pem": + cert = x509.load_pem_x509_certificate(data) + elif cert_type == "der": + cert = x509.load_der_x509_certificate(data) + else: + raise ValueError(f"cert-type {cert_type} not supported") + pem_data = cert.public_bytes(serialization.Encoding.PEM).decode("utf-8") except Exception as e: raise CertificateError(e) diff --git a/src/saml2/sigver.py b/src/saml2/sigver.py index f3af1ec99..0b0c9a0d8 100644 --- a/src/saml2/sigver.py +++ b/src/saml2/sigver.py @@ -16,8 +16,6 @@ from time import mktime from uuid import uuid4 as gen_random_key -import dateutil - # importlib.resources was introduced in python 3.7 # files API from importlib.resources introduced in python 3.9 @@ -28,8 +26,7 @@ from urllib import parse -from OpenSSL import crypto -import pytz +from cryptography import x509 from saml2 import ExtensionElement from saml2 import SamlBase @@ -383,14 +380,14 @@ def active_cert(key): """ try: cert_str = pem_format(key) - cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str) + cert = x509.load_pem_x509_certificate(cert_str) except AttributeError: return False - now = pytz.UTC.localize(datetime.datetime.utcnow()) - valid_from = dateutil.parser.parse(cert.get_notBefore()) - valid_to = dateutil.parser.parse(cert.get_notAfter()) - active = not cert.has_expired() and valid_from <= now < valid_to + now = datetime.datetime.now(datetime.timezone.utc) + valid_from = cert.not_valid_before_utc + valid_to = cert.not_valid_after_utc + active = valid_from <= now < valid_to return active