aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/cryptography/next/py3
diff options
context:
space:
mode:
authormaxim-yurchuk <maxim-yurchuk@yandex-team.com>2024-10-09 12:29:46 +0300
committermaxim-yurchuk <maxim-yurchuk@yandex-team.com>2024-10-09 13:14:22 +0300
commit9731d8a4bb7ee2cc8554eaf133bb85498a4c7d80 (patch)
treea8fb3181d5947c0d78cf402aa56e686130179049 /contrib/python/cryptography/next/py3
parenta44b779cd359f06c3ebbef4ec98c6b38609d9d85 (diff)
downloadydb-9731d8a4bb7ee2cc8554eaf133bb85498a4c7d80.tar.gz
publishFullContrib: true for ydb
<HIDDEN_URL> commit_hash:c82a80ac4594723cebf2c7387dec9c60217f603e
Diffstat (limited to 'contrib/python/cryptography/next/py3')
-rw-r--r--contrib/python/cryptography/next/py3/.dist-info/METADATA133
-rw-r--r--contrib/python/cryptography/next/py3/.dist-info/top_level.txt1
-rw-r--r--contrib/python/cryptography/next/py3/.yandex_meta/build.ym7
-rw-r--r--contrib/python/cryptography/next/py3/.yandex_meta/devtools.copyrights.report57
-rw-r--r--contrib/python/cryptography/next/py3/.yandex_meta/devtools.licenses.report348
-rw-r--r--contrib/python/cryptography/next/py3/.yandex_meta/licenses.list.txt508
-rw-r--r--contrib/python/cryptography/next/py3/.yandex_meta/yamaker.yaml1
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/__about__.py17
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/__init__.py13
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/exceptions.py54
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/fernet.py221
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/__init__.py13
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/_oid.py299
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/__init__.py13
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/__init__.py9
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/aead.py527
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/backend.py1938
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ciphers.py281
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/cmac.py89
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/decode_asn1.py32
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ec.py328
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/rsa.py599
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/utils.py63
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/__init__.py3
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.py1
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.pyi34
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/_openssl.pyi8
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/asn1.pyi16
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/exceptions.pyi17
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/ocsp.pyi25
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/__init__.pyi47
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dh.pyi22
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dsa.pyi20
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi14
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed448.pyi14
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi17
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hmac.pyi21
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi22
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi13
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x25519.pyi14
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x448.pyi14
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/pkcs7.pyi15
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/x509.pyi44
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/__init__.py3
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/_conditional.py329
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/binding.py179
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/__init__.py3
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_asymmetric.py19
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_cipheralgorithm.py45
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_serialization.py170
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/__init__.py3
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dh.py261
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dsa.py299
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ec.py490
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed25519.py118
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed448.py117
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/padding.py102
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/rsa.py439
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/types.py111
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/utils.py24
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x25519.py113
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x448.py111
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/__init__.py27
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/aead.py378
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/algorithms.py228
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/base.py269
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/modes.py274
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/cmac.py65
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/constant_time.py14
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hashes.py243
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hmac.py13
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/__init__.py23
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/concatkdf.py124
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/hkdf.py101
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/kbkdf.py299
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/pbkdf2.py64
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/scrypt.py80
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/x963kdf.py61
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/keywrap.py177
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/padding.py225
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/poly1305.py11
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/__init__.py63
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/base.py73
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs12.py229
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs7.py235
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/ssh.py1534
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/__init__.py9
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/hotp.py92
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/totp.py50
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/py.typed0
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/utils.py130
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/__init__.py255
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/base.py1173
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/certificate_transparency.py97
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/extensions.py2215
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/general_name.py283
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/name.py462
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/ocsp.py622
-rw-r--r--contrib/python/cryptography/next/py3/cryptography/x509/oid.py33
-rw-r--r--contrib/python/cryptography/next/py3/ya.make124
100 files changed, 19223 insertions, 0 deletions
diff --git a/contrib/python/cryptography/next/py3/.dist-info/METADATA b/contrib/python/cryptography/next/py3/.dist-info/METADATA
new file mode 100644
index 0000000000..1144a99700
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.dist-info/METADATA
@@ -0,0 +1,133 @@
+Metadata-Version: 2.1
+Name: cryptography
+Version: 41.0.6
+Summary: cryptography is a package which provides cryptographic recipes and primitives to Python developers.
+Author-email: The Python Cryptographic Authority and individual contributors <cryptography-dev@python.org>
+License: Apache-2.0 OR BSD-3-Clause
+Project-URL: homepage, https://github.com/pyca/cryptography
+Project-URL: documentation, https://cryptography.io/
+Project-URL: source, https://github.com/pyca/cryptography/
+Project-URL: issues, https://github.com/pyca/cryptography/issues
+Project-URL: changelog, https://cryptography.io/en/latest/changelog/
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: Apache Software License
+Classifier: License :: OSI Approved :: BSD License
+Classifier: Natural Language :: English
+Classifier: Operating System :: MacOS :: MacOS X
+Classifier: Operating System :: POSIX
+Classifier: Operating System :: POSIX :: BSD
+Classifier: Operating System :: POSIX :: Linux
+Classifier: Operating System :: Microsoft :: Windows
+Classifier: Programming Language :: Python
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: 3 :: Only
+Classifier: Programming Language :: Python :: 3.7
+Classifier: Programming Language :: Python :: 3.8
+Classifier: Programming Language :: Python :: 3.9
+Classifier: Programming Language :: Python :: 3.10
+Classifier: Programming Language :: Python :: 3.11
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
+Classifier: Topic :: Security :: Cryptography
+Requires-Python: >=3.7
+Description-Content-Type: text/x-rst
+License-File: LICENSE
+License-File: LICENSE.APACHE
+License-File: LICENSE.BSD
+Requires-Dist: cffi >=1.12
+Provides-Extra: docs
+Requires-Dist: sphinx >=5.3.0 ; extra == 'docs'
+Requires-Dist: sphinx-rtd-theme >=1.1.1 ; extra == 'docs'
+Provides-Extra: docstest
+Requires-Dist: pyenchant >=1.6.11 ; extra == 'docstest'
+Requires-Dist: twine >=1.12.0 ; extra == 'docstest'
+Requires-Dist: sphinxcontrib-spelling >=4.0.1 ; extra == 'docstest'
+Provides-Extra: nox
+Requires-Dist: nox ; extra == 'nox'
+Provides-Extra: pep8test
+Requires-Dist: black ; extra == 'pep8test'
+Requires-Dist: ruff ; extra == 'pep8test'
+Requires-Dist: mypy ; extra == 'pep8test'
+Requires-Dist: check-sdist ; extra == 'pep8test'
+Provides-Extra: sdist
+Requires-Dist: build ; extra == 'sdist'
+Provides-Extra: ssh
+Requires-Dist: bcrypt >=3.1.5 ; extra == 'ssh'
+Provides-Extra: test
+Requires-Dist: pytest >=6.2.0 ; extra == 'test'
+Requires-Dist: pytest-benchmark ; extra == 'test'
+Requires-Dist: pytest-cov ; extra == 'test'
+Requires-Dist: pytest-xdist ; extra == 'test'
+Requires-Dist: pretend ; extra == 'test'
+Provides-Extra: test-randomorder
+Requires-Dist: pytest-randomly ; extra == 'test-randomorder'
+
+pyca/cryptography
+=================
+
+.. image:: https://img.shields.io/pypi/v/cryptography.svg
+ :target: https://pypi.org/project/cryptography/
+ :alt: Latest Version
+
+.. image:: https://readthedocs.org/projects/cryptography/badge/?version=latest
+ :target: https://cryptography.io
+ :alt: Latest Docs
+
+.. image:: https://github.com/pyca/cryptography/workflows/CI/badge.svg?branch=main
+ :target: https://github.com/pyca/cryptography/actions?query=workflow%3ACI+branch%3Amain
+
+
+``cryptography`` is a package which provides cryptographic recipes and
+primitives to Python developers. Our goal is for it to be your "cryptographic
+standard library". It supports Python 3.7+ and PyPy3 7.3.10+.
+
+``cryptography`` includes both high level recipes and low level interfaces to
+common cryptographic algorithms such as symmetric ciphers, message digests, and
+key derivation functions. For example, to encrypt something with
+``cryptography``'s high level symmetric encryption recipe:
+
+.. code-block:: pycon
+
+ >>> from cryptography.fernet import Fernet
+ >>> # Put this somewhere safe!
+ >>> key = Fernet.generate_key()
+ >>> f = Fernet(key)
+ >>> token = f.encrypt(b"A really secret message. Not for prying eyes.")
+ >>> token
+ b'...'
+ >>> f.decrypt(token)
+ b'A really secret message. Not for prying eyes.'
+
+You can find more information in the `documentation`_.
+
+You can install ``cryptography`` with:
+
+.. code-block:: console
+
+ $ pip install cryptography
+
+For full details see `the installation documentation`_.
+
+Discussion
+~~~~~~~~~~
+
+If you run into bugs, you can file them in our `issue tracker`_.
+
+We maintain a `cryptography-dev`_ mailing list for development discussion.
+
+You can also join ``#pyca`` on ``irc.libera.chat`` to ask questions or get
+involved.
+
+Security
+~~~~~~~~
+
+Need to report a security issue? Please consult our `security reporting`_
+documentation.
+
+
+.. _`documentation`: https://cryptography.io/
+.. _`the installation documentation`: https://cryptography.io/en/latest/installation/
+.. _`issue tracker`: https://github.com/pyca/cryptography/issues
+.. _`cryptography-dev`: https://mail.python.org/mailman/listinfo/cryptography-dev
+.. _`security reporting`: https://cryptography.io/en/latest/security/
diff --git a/contrib/python/cryptography/next/py3/.dist-info/top_level.txt b/contrib/python/cryptography/next/py3/.dist-info/top_level.txt
new file mode 100644
index 0000000000..0d38bc5ea2
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.dist-info/top_level.txt
@@ -0,0 +1 @@
+cryptography
diff --git a/contrib/python/cryptography/next/py3/.yandex_meta/build.ym b/contrib/python/cryptography/next/py3/.yandex_meta/build.ym
new file mode 100644
index 0000000000..61fd6fdb9d
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.yandex_meta/build.ym
@@ -0,0 +1,7 @@
+{% extends '//builtin/pypi.ym' %}
+
+{% block pypi_package %}
+cryptography
+{% endblock %}
+
+{% block current_version %}41.0.6{% endblock %}
diff --git a/contrib/python/cryptography/next/py3/.yandex_meta/devtools.copyrights.report b/contrib/python/cryptography/next/py3/.yandex_meta/devtools.copyrights.report
new file mode 100644
index 0000000000..8a04b7c066
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.yandex_meta/devtools.copyrights.report
@@ -0,0 +1,57 @@
+# File format ($ symbol means the beginning of a line):
+#
+# $ # this message
+# $ # =======================
+# $ # comments (all commentaries should starts with some number of spaces and # symbol)
+# $ IGNORE_FILES {file1.ext1} {file2.ext2} - (optional) ignore listed files when generating license macro and credits
+# $ RENAME {original license id} TO {new license id} # user comments - (optional) use {new license id} instead {original license id} in ya.make files
+# $ # user comments
+# $
+# ${action} {license id} {license text hash}
+# $BELONGS ./ya/make/file/relative/path/1/ya.make ./ya/make/2/ya.make
+# ${all_file_action} filename
+# $ # user commentaries (many lines)
+# $ generated description - files with this license, license text... (some number of lines that starts with some number of spaces, do not modify)
+# ${action} {license spdx} {license text hash}
+# $BELONGS ./ya/make/file/relative/path/3/ya.make
+# ${all_file_action} filename
+# $ # user commentaries
+# $ generated description
+# $ ...
+#
+# You can modify action, all_file_action and add commentaries
+# Available actions:
+# keep - keep license in contrib and use in credits
+# skip - skip license
+# remove - remove all files with this license
+# rename - save license text/links into licenses texts file, but not store SPDX into LINCENSE macro. You should store correct license id into devtools.license.spdx.txt file
+#
+# {all file action} records will be generated when license text contains filename that exists on filesystem (in contrib directory)
+# We suppose that that files can contain some license info
+# Available all file actions:
+# FILE_IGNORE - ignore file (do nothing)
+# FILE_INCLUDE - include all file data into licenses text file
+# =======================
+
+KEEP COPYRIGHT_SERVICE_LABEL 88f55ca2c5566c203df3778d5daef7c4
+BELONGS ya.make
+ License text:
+ Copyright (c) Individual contributors.
+ All rights reserved.
+ Scancode info:
+ Original SPDX id: COPYRIGHT_SERVICE_LABEL
+ Score : 100.00
+ Match type : COPYRIGHT
+ Files with this license:
+ LICENSE.BSD [1:2]
+
+KEEP COPYRIGHT_SERVICE_LABEL bcc5c013a38cf3c8c383b2ec9c4f77d0
+BELONGS ya.make
+ License text:
+ Copyright 2013-2023
+ Scancode info:
+ Original SPDX id: COPYRIGHT_SERVICE_LABEL
+ Score : 100.00
+ Match type : COPYRIGHT
+ Files with this license:
+ cryptography/__about__.py [16:17]
diff --git a/contrib/python/cryptography/next/py3/.yandex_meta/devtools.licenses.report b/contrib/python/cryptography/next/py3/.yandex_meta/devtools.licenses.report
new file mode 100644
index 0000000000..b405fa2d93
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.yandex_meta/devtools.licenses.report
@@ -0,0 +1,348 @@
+# File format ($ symbol means the beginning of a line):
+#
+# $ # this message
+# $ # =======================
+# $ # comments (all commentaries should starts with some number of spaces and # symbol)
+# $ IGNORE_FILES {file1.ext1} {file2.ext2} - (optional) ignore listed files when generating license macro and credits
+# $ RENAME {original license id} TO {new license id} # user comments - (optional) use {new license id} instead {original license id} in ya.make files
+# $ # user comments
+# $
+# ${action} {license id} {license text hash}
+# $BELONGS ./ya/make/file/relative/path/1/ya.make ./ya/make/2/ya.make
+# ${all_file_action} filename
+# $ # user commentaries (many lines)
+# $ generated description - files with this license, license text... (some number of lines that starts with some number of spaces, do not modify)
+# ${action} {license spdx} {license text hash}
+# $BELONGS ./ya/make/file/relative/path/3/ya.make
+# ${all_file_action} filename
+# $ # user commentaries
+# $ generated description
+# $ ...
+#
+# You can modify action, all_file_action and add commentaries
+# Available actions:
+# keep - keep license in contrib and use in credits
+# skip - skip license
+# remove - remove all files with this license
+# rename - save license text/links into licenses texts file, but not store SPDX into LINCENSE macro. You should store correct license id into devtools.license.spdx.txt file
+#
+# {all file action} records will be generated when license text contains filename that exists on filesystem (in contrib directory)
+# We suppose that that files can contain some license info
+# Available all file actions:
+# FILE_IGNORE - ignore file (do nothing)
+# FILE_INCLUDE - include all file data into licenses text file
+# =======================
+
+KEEP BSD-3-Clause 0e8a391459310d387abb8daf053304cc
+BELONGS ya.make
+FILE_INCLUDE LICENSE.BSD found in files: .dist-info/METADATA at line 37
+ License text:
+ License-File: LICENSE.BSD
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 95.00
+ Match type : TAG
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ .dist-info/METADATA [37:37]
+
+KEEP BSD-3-Clause 35864a557f9a10e478ca8d73a8c6886e
+BELONGS ya.make
+ License text:
+ Classifier: License :: OSI Approved :: BSD License
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 99.00
+ Match type : TAG
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ .dist-info/METADATA [15:15]
+
+KEEP Apache-2.0 4065afa98cac5f1fde3ccfcb656fada4
+BELONGS ya.make
+FILE_INCLUDE LICENSE.APACHE found in files: .dist-info/METADATA at line 36
+FILE_INCLUDE LICENSE.BSD found in files: .dist-info/METADATA at line 37
+ License text:
+ License-File: LICENSE.APACHE
+ License-File: LICENSE.BSD
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 99.00
+ Match type : TAG
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ .dist-info/METADATA [36:37]
+
+KEEP BSD-3-Clause 798fe0a03129e9a9c30a9a5c344ac047
+BELONGS ya.make
+ Note: matched license text is too long. Read it in the source files.
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 99.07
+ Match type : TEXT
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ LICENSE.BSD [4:27]
+
+KEEP Apache-2.0 86e8237b9a37a6951edea992afba41f3
+BELONGS ya.make
+ License text:
+ Classifier: License :: OSI Approved :: Apache Software License
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 95.00
+ Match type : TAG
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ .dist-info/METADATA [14:14]
+
+KEEP BSD-3-Clause AND Apache-2.0 c31058d9df12eb0c238aa5bdecc9c7ab
+BELONGS ya.make
+FILE_INCLUDE LICENSE found in files: cryptography/__about__.py at line 2, cryptography/__init__.py at line 2, cryptography/exceptions.py at line 2, cryptography/fernet.py at line 2, cryptography/hazmat/__init__.py at line 2, cryptography/hazmat/_oid.py at line 2, cryptography/hazmat/backends/__init__.py at line 2, cryptography/hazmat/backends/openssl/__init__.py at line 2, cryptography/hazmat/backends/openssl/aead.py at line 2, cryptography/hazmat/backends/openssl/backend.py at line 2, cryptography/hazmat/backends/openssl/ciphers.py at line 2, cryptography/hazmat/backends/openssl/cmac.py at line 2, cryptography/hazmat/backends/openssl/decode_asn1.py at line 2, cryptography/hazmat/backends/openssl/ec.py at line 2, cryptography/hazmat/backends/openssl/rsa.py at line 2, cryptography/hazmat/backends/openssl/utils.py at line 2, cryptography/hazmat/bindings/__init__.py at line 2, cryptography/hazmat/bindings/_rust/__init__.pyi at line 2, cryptography/hazmat/bindings/_rust/_openssl.pyi at line 2, cryptography/hazmat/bindings/_rust/asn1.pyi at line 2, cryptography/hazmat/bindings/_rust/exceptions.pyi at line 2, cryptography/hazmat/bindings/_rust/ocsp.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/__init__.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/dh.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/dsa.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/ed448.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/hashes.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/hmac.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/kdf.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/x25519.pyi at line 2, cryptography/hazmat/bindings/_rust/openssl/x448.pyi at line 2, cryptography/hazmat/bindings/_rust/x509.pyi at line 2, cryptography/hazmat/bindings/openssl/__init__.py at line 2, cryptography/hazmat/bindings/openssl/_conditional.py at line 2, cryptography/hazmat/bindings/openssl/binding.py at line 2, cryptography/hazmat/primitives/__init__.py at line 2, cryptography/hazmat/primitives/_asymmetric.py at line 2, cryptography/hazmat/primitives/_cipheralgorithm.py at line 2, cryptography/hazmat/primitives/_serialization.py at line 2, cryptography/hazmat/primitives/asymmetric/__init__.py at line 2, cryptography/hazmat/primitives/asymmetric/dh.py at line 2, cryptography/hazmat/primitives/asymmetric/dsa.py at line 2, cryptography/hazmat/primitives/asymmetric/ec.py at line 2, cryptography/hazmat/primitives/asymmetric/ed25519.py at line 2, cryptography/hazmat/primitives/asymmetric/ed448.py at line 2, cryptography/hazmat/primitives/asymmetric/padding.py at line 2, cryptography/hazmat/primitives/asymmetric/rsa.py at line 2, cryptography/hazmat/primitives/asymmetric/types.py at line 2, cryptography/hazmat/primitives/asymmetric/utils.py at line 2, cryptography/hazmat/primitives/asymmetric/x25519.py at line 2, cryptography/hazmat/primitives/asymmetric/x448.py at line 2, cryptography/hazmat/primitives/ciphers/__init__.py at line 2, cryptography/hazmat/primitives/ciphers/aead.py at line 2, cryptography/hazmat/primitives/ciphers/algorithms.py at line 2, cryptography/hazmat/primitives/ciphers/base.py at line 2, cryptography/hazmat/primitives/ciphers/modes.py at line 2, cryptography/hazmat/primitives/cmac.py at line 2, cryptography/hazmat/primitives/constant_time.py at line 2, cryptography/hazmat/primitives/hashes.py at line 2, cryptography/hazmat/primitives/hmac.py at line 2, cryptography/hazmat/primitives/kdf/__init__.py at line 2, cryptography/hazmat/primitives/kdf/concatkdf.py at line 2, cryptography/hazmat/primitives/kdf/hkdf.py at line 2, cryptography/hazmat/primitives/kdf/kbkdf.py at line 2, cryptography/hazmat/primitives/kdf/pbkdf2.py at line 2, cryptography/hazmat/primitives/kdf/scrypt.py at line 2, cryptography/hazmat/primitives/kdf/x963kdf.py at line 2, cryptography/hazmat/primitives/keywrap.py at line 2, cryptography/hazmat/primitives/padding.py at line 2, cryptography/hazmat/primitives/poly1305.py at line 2, cryptography/hazmat/primitives/serialization/__init__.py at line 2, cryptography/hazmat/primitives/serialization/base.py at line 2, cryptography/hazmat/primitives/serialization/pkcs12.py at line 2, cryptography/hazmat/primitives/serialization/pkcs7.py at line 2, cryptography/hazmat/primitives/serialization/ssh.py at line 2, cryptography/hazmat/primitives/twofactor/__init__.py at line 2, cryptography/hazmat/primitives/twofactor/hotp.py at line 2, cryptography/hazmat/primitives/twofactor/totp.py at line 2, cryptography/utils.py at line 2, cryptography/x509/__init__.py at line 2, cryptography/x509/base.py at line 2, cryptography/x509/certificate_transparency.py at line 2, cryptography/x509/extensions.py at line 2, cryptography/x509/general_name.py at line 2, cryptography/x509/name.py at line 2, cryptography/x509/ocsp.py at line 2, cryptography/x509/oid.py at line 2
+ License text:
+ \# This file is dual licensed under the terms of the Apache License, Version
+ \# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+ \# for complete details.
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 100.00
+ Match type : NOTICE
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ cryptography/__about__.py [1:3]
+ cryptography/__init__.py [1:3]
+ cryptography/exceptions.py [1:3]
+ cryptography/fernet.py [1:3]
+ cryptography/hazmat/__init__.py [1:3]
+ cryptography/hazmat/_oid.py [1:3]
+ cryptography/hazmat/backends/__init__.py [1:3]
+ cryptography/hazmat/backends/openssl/__init__.py [1:3]
+ cryptography/hazmat/backends/openssl/aead.py [1:3]
+ cryptography/hazmat/backends/openssl/backend.py [1:3]
+ cryptography/hazmat/backends/openssl/ciphers.py [1:3]
+ cryptography/hazmat/backends/openssl/cmac.py [1:3]
+ cryptography/hazmat/backends/openssl/decode_asn1.py [1:3]
+ cryptography/hazmat/backends/openssl/ec.py [1:3]
+ cryptography/hazmat/backends/openssl/rsa.py [1:3]
+ cryptography/hazmat/backends/openssl/utils.py [1:3]
+ cryptography/hazmat/bindings/__init__.py [1:3]
+ cryptography/hazmat/bindings/_rust/__init__.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/_openssl.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/asn1.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/exceptions.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/ocsp.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/__init__.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/dh.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/dsa.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/ed448.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/hashes.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/hmac.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/kdf.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/x25519.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/x448.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/x509.pyi [1:3]
+ cryptography/hazmat/bindings/openssl/__init__.py [1:3]
+ cryptography/hazmat/bindings/openssl/_conditional.py [1:3]
+ cryptography/hazmat/bindings/openssl/binding.py [1:3]
+ cryptography/hazmat/primitives/__init__.py [1:3]
+ cryptography/hazmat/primitives/_asymmetric.py [1:3]
+ cryptography/hazmat/primitives/_cipheralgorithm.py [1:3]
+ cryptography/hazmat/primitives/_serialization.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/__init__.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/dh.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/dsa.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ec.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ed25519.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ed448.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/padding.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/rsa.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/types.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/utils.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/x25519.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/x448.py [1:3]
+ cryptography/hazmat/primitives/ciphers/__init__.py [1:3]
+ cryptography/hazmat/primitives/ciphers/aead.py [1:3]
+ cryptography/hazmat/primitives/ciphers/algorithms.py [1:3]
+ cryptography/hazmat/primitives/ciphers/base.py [1:3]
+ cryptography/hazmat/primitives/ciphers/modes.py [1:3]
+ cryptography/hazmat/primitives/cmac.py [1:3]
+ cryptography/hazmat/primitives/constant_time.py [1:3]
+ cryptography/hazmat/primitives/hashes.py [1:3]
+ cryptography/hazmat/primitives/hmac.py [1:3]
+ cryptography/hazmat/primitives/kdf/__init__.py [1:3]
+ cryptography/hazmat/primitives/kdf/concatkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/hkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/kbkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/pbkdf2.py [1:3]
+ cryptography/hazmat/primitives/kdf/scrypt.py [1:3]
+ cryptography/hazmat/primitives/kdf/x963kdf.py [1:3]
+ cryptography/hazmat/primitives/keywrap.py [1:3]
+ cryptography/hazmat/primitives/padding.py [1:3]
+ cryptography/hazmat/primitives/poly1305.py [1:3]
+ cryptography/hazmat/primitives/serialization/__init__.py [1:3]
+ cryptography/hazmat/primitives/serialization/base.py [1:3]
+ cryptography/hazmat/primitives/serialization/pkcs12.py [1:3]
+ cryptography/hazmat/primitives/serialization/pkcs7.py [1:3]
+ cryptography/hazmat/primitives/serialization/ssh.py [1:3]
+ cryptography/hazmat/primitives/twofactor/__init__.py [1:3]
+ cryptography/hazmat/primitives/twofactor/hotp.py [1:3]
+ cryptography/hazmat/primitives/twofactor/totp.py [1:3]
+ cryptography/utils.py [1:3]
+ cryptography/x509/__init__.py [1:3]
+ cryptography/x509/base.py [1:3]
+ cryptography/x509/certificate_transparency.py [1:3]
+ cryptography/x509/extensions.py [1:3]
+ cryptography/x509/general_name.py [1:3]
+ cryptography/x509/name.py [1:3]
+ cryptography/x509/ocsp.py [1:3]
+ cryptography/x509/oid.py [1:3]
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 100.00
+ Match type : NOTICE
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ cryptography/__about__.py [1:3]
+ cryptography/__init__.py [1:3]
+ cryptography/exceptions.py [1:3]
+ cryptography/fernet.py [1:3]
+ cryptography/hazmat/__init__.py [1:3]
+ cryptography/hazmat/_oid.py [1:3]
+ cryptography/hazmat/backends/__init__.py [1:3]
+ cryptography/hazmat/backends/openssl/__init__.py [1:3]
+ cryptography/hazmat/backends/openssl/aead.py [1:3]
+ cryptography/hazmat/backends/openssl/backend.py [1:3]
+ cryptography/hazmat/backends/openssl/ciphers.py [1:3]
+ cryptography/hazmat/backends/openssl/cmac.py [1:3]
+ cryptography/hazmat/backends/openssl/decode_asn1.py [1:3]
+ cryptography/hazmat/backends/openssl/ec.py [1:3]
+ cryptography/hazmat/backends/openssl/rsa.py [1:3]
+ cryptography/hazmat/backends/openssl/utils.py [1:3]
+ cryptography/hazmat/bindings/__init__.py [1:3]
+ cryptography/hazmat/bindings/_rust/__init__.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/_openssl.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/asn1.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/exceptions.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/ocsp.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/__init__.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/dh.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/dsa.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/ed448.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/hashes.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/hmac.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/kdf.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/x25519.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/openssl/x448.pyi [1:3]
+ cryptography/hazmat/bindings/_rust/x509.pyi [1:3]
+ cryptography/hazmat/bindings/openssl/__init__.py [1:3]
+ cryptography/hazmat/bindings/openssl/_conditional.py [1:3]
+ cryptography/hazmat/bindings/openssl/binding.py [1:3]
+ cryptography/hazmat/primitives/__init__.py [1:3]
+ cryptography/hazmat/primitives/_asymmetric.py [1:3]
+ cryptography/hazmat/primitives/_cipheralgorithm.py [1:3]
+ cryptography/hazmat/primitives/_serialization.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/__init__.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/dh.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/dsa.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ec.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ed25519.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/ed448.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/padding.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/rsa.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/types.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/utils.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/x25519.py [1:3]
+ cryptography/hazmat/primitives/asymmetric/x448.py [1:3]
+ cryptography/hazmat/primitives/ciphers/__init__.py [1:3]
+ cryptography/hazmat/primitives/ciphers/aead.py [1:3]
+ cryptography/hazmat/primitives/ciphers/algorithms.py [1:3]
+ cryptography/hazmat/primitives/ciphers/base.py [1:3]
+ cryptography/hazmat/primitives/ciphers/modes.py [1:3]
+ cryptography/hazmat/primitives/cmac.py [1:3]
+ cryptography/hazmat/primitives/constant_time.py [1:3]
+ cryptography/hazmat/primitives/hashes.py [1:3]
+ cryptography/hazmat/primitives/hmac.py [1:3]
+ cryptography/hazmat/primitives/kdf/__init__.py [1:3]
+ cryptography/hazmat/primitives/kdf/concatkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/hkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/kbkdf.py [1:3]
+ cryptography/hazmat/primitives/kdf/pbkdf2.py [1:3]
+ cryptography/hazmat/primitives/kdf/scrypt.py [1:3]
+ cryptography/hazmat/primitives/kdf/x963kdf.py [1:3]
+ cryptography/hazmat/primitives/keywrap.py [1:3]
+ cryptography/hazmat/primitives/padding.py [1:3]
+ cryptography/hazmat/primitives/poly1305.py [1:3]
+ cryptography/hazmat/primitives/serialization/__init__.py [1:3]
+ cryptography/hazmat/primitives/serialization/base.py [1:3]
+ cryptography/hazmat/primitives/serialization/pkcs12.py [1:3]
+ cryptography/hazmat/primitives/serialization/pkcs7.py [1:3]
+ cryptography/hazmat/primitives/serialization/ssh.py [1:3]
+ cryptography/hazmat/primitives/twofactor/__init__.py [1:3]
+ cryptography/hazmat/primitives/twofactor/hotp.py [1:3]
+ cryptography/hazmat/primitives/twofactor/totp.py [1:3]
+ cryptography/utils.py [1:3]
+ cryptography/x509/__init__.py [1:3]
+ cryptography/x509/base.py [1:3]
+ cryptography/x509/certificate_transparency.py [1:3]
+ cryptography/x509/extensions.py [1:3]
+ cryptography/x509/general_name.py [1:3]
+ cryptography/x509/name.py [1:3]
+ cryptography/x509/ocsp.py [1:3]
+ cryptography/x509/oid.py [1:3]
+
+KEEP Apache-2.0 cac6cbe8ed5a3da569f7c01e4e486688
+BELONGS ya.make
+ Note: matched license text is too long. Read it in the source files.
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 100.00
+ Match type : TEXT
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ LICENSE.APACHE [2:202]
+
+KEEP Apache-2.0 AND BSD-3-Clause e22407f08ab709bace8dd0e3c717917d
+BELONGS ya.make
+ License text:
+ License: Apache-2.0 OR BSD-3-Clause
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 100.00
+ Match type : TAG
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ .dist-info/METADATA [6:6]
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 100.00
+ Match type : REFERENCE
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ .dist-info/METADATA [6:6]
+
+KEEP BSD-3-Clause AND Apache-2.0 fb4b1826cb15b4715a1a70cdb1bd23a9
+BELONGS ya.make
+FILE_INCLUDE LICENSE.APACHE found in files: LICENSE at line 2
+FILE_INCLUDE LICENSE.BSD found in files: LICENSE at line 2
+ License text:
+ This software is made available under the terms of *either* of the licenses
+ found in LICENSE.APACHE or LICENSE.BSD. Contributions to cryptography are made
+ under the terms of *both* these licenses.
+ Scancode info:
+ Original SPDX id: BSD-3-Clause
+ Score : 90.91
+ Match type : NOTICE
+ Links : http://www.opensource.org/licenses/BSD-3-Clause, https://spdx.org/licenses/BSD-3-Clause
+ Files with this license:
+ LICENSE [1:3]
+ Scancode info:
+ Original SPDX id: Apache-2.0
+ Score : 90.91
+ Match type : NOTICE
+ Links : http://www.apache.org/licenses/, http://www.apache.org/licenses/LICENSE-2.0, https://spdx.org/licenses/Apache-2.0
+ Files with this license:
+ LICENSE [1:3]
diff --git a/contrib/python/cryptography/next/py3/.yandex_meta/licenses.list.txt b/contrib/python/cryptography/next/py3/.yandex_meta/licenses.list.txt
new file mode 100644
index 0000000000..2945e24a57
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.yandex_meta/licenses.list.txt
@@ -0,0 +1,508 @@
+====================Apache-2.0====================
+ Apache License
+ Version 2.0, January 2004
+ https://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+====================Apache-2.0====================
+Classifier: License :: OSI Approved :: Apache Software License
+
+
+====================Apache-2.0====================
+License-File: LICENSE.APACHE
+License-File: LICENSE.BSD
+
+
+====================Apache-2.0 AND BSD-3-Clause====================
+License: Apache-2.0 OR BSD-3-Clause
+
+
+====================BSD-3-Clause====================
+Classifier: License :: OSI Approved :: BSD License
+
+
+====================BSD-3-Clause====================
+License-File: LICENSE.BSD
+
+
+====================BSD-3-Clause====================
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. Neither the name of PyCA Cryptography nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+====================BSD-3-Clause AND Apache-2.0====================
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+
+====================BSD-3-Clause AND Apache-2.0====================
+This software is made available under the terms of *either* of the licenses
+found in LICENSE.APACHE or LICENSE.BSD. Contributions to cryptography are made
+under the terms of *both* these licenses.
+
+====================COPYRIGHT====================
+Copyright (c) Individual contributors.
+All rights reserved.
+
+
+====================COPYRIGHT====================
+Copyright 2013-2023
+
+====================File: LICENSE====================
+This software is made available under the terms of *either* of the licenses
+found in LICENSE.APACHE or LICENSE.BSD. Contributions to cryptography are made
+under the terms of *both* these licenses.
+
+
+====================File: LICENSE.APACHE====================
+
+ Apache License
+ Version 2.0, January 2004
+ https://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
+====================File: LICENSE.BSD====================
+Copyright (c) Individual contributors.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. Neither the name of PyCA Cryptography nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/contrib/python/cryptography/next/py3/.yandex_meta/yamaker.yaml b/contrib/python/cryptography/next/py3/.yandex_meta/yamaker.yaml
new file mode 100644
index 0000000000..0967ef424b
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/.yandex_meta/yamaker.yaml
@@ -0,0 +1 @@
+{}
diff --git a/contrib/python/cryptography/next/py3/cryptography/__about__.py b/contrib/python/cryptography/next/py3/cryptography/__about__.py
new file mode 100644
index 0000000000..d267dc7856
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/__about__.py
@@ -0,0 +1,17 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+__all__ = [
+ "__version__",
+ "__author__",
+ "__copyright__",
+]
+
+__version__ = "41.0.6"
+
+
+__author__ = "The Python Cryptographic Authority and individual contributors"
+__copyright__ = f"Copyright 2013-2023 {__author__}"
diff --git a/contrib/python/cryptography/next/py3/cryptography/__init__.py b/contrib/python/cryptography/next/py3/cryptography/__init__.py
new file mode 100644
index 0000000000..86b9a25726
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/__init__.py
@@ -0,0 +1,13 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.__about__ import __author__, __copyright__, __version__
+
+__all__ = [
+ "__version__",
+ "__author__",
+ "__copyright__",
+]
diff --git a/contrib/python/cryptography/next/py3/cryptography/exceptions.py b/contrib/python/cryptography/next/py3/cryptography/exceptions.py
new file mode 100644
index 0000000000..47fdd18eee
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/exceptions.py
@@ -0,0 +1,54 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+
+_Reasons = rust_exceptions._Reasons
+
+
+class UnsupportedAlgorithm(Exception):
+ def __init__(
+ self, message: str, reason: typing.Optional[_Reasons] = None
+ ) -> None:
+ super().__init__(message)
+ self._reason = reason
+
+
+class AlreadyFinalized(Exception):
+ pass
+
+
+class AlreadyUpdated(Exception):
+ pass
+
+
+class NotYetFinalized(Exception):
+ pass
+
+
+class InvalidTag(Exception):
+ pass
+
+
+class InvalidSignature(Exception):
+ pass
+
+
+class InternalError(Exception):
+ def __init__(
+ self, msg: str, err_code: typing.List[rust_openssl.OpenSSLError]
+ ) -> None:
+ super().__init__(msg)
+ self.err_code = err_code
+
+
+class InvalidKey(Exception):
+ pass
diff --git a/contrib/python/cryptography/next/py3/cryptography/fernet.py b/contrib/python/cryptography/next/py3/cryptography/fernet.py
new file mode 100644
index 0000000000..ad8fb40b9d
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/fernet.py
@@ -0,0 +1,221 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import base64
+import binascii
+import os
+import time
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import InvalidSignature
+from cryptography.hazmat.primitives import hashes, padding
+from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+from cryptography.hazmat.primitives.hmac import HMAC
+
+
+class InvalidToken(Exception):
+ pass
+
+
+_MAX_CLOCK_SKEW = 60
+
+
+class Fernet:
+ def __init__(
+ self,
+ key: typing.Union[bytes, str],
+ backend: typing.Any = None,
+ ) -> None:
+ try:
+ key = base64.urlsafe_b64decode(key)
+ except binascii.Error as exc:
+ raise ValueError(
+ "Fernet key must be 32 url-safe base64-encoded bytes."
+ ) from exc
+ if len(key) != 32:
+ raise ValueError(
+ "Fernet key must be 32 url-safe base64-encoded bytes."
+ )
+
+ self._signing_key = key[:16]
+ self._encryption_key = key[16:]
+
+ @classmethod
+ def generate_key(cls) -> bytes:
+ return base64.urlsafe_b64encode(os.urandom(32))
+
+ def encrypt(self, data: bytes) -> bytes:
+ return self.encrypt_at_time(data, int(time.time()))
+
+ def encrypt_at_time(self, data: bytes, current_time: int) -> bytes:
+ iv = os.urandom(16)
+ return self._encrypt_from_parts(data, current_time, iv)
+
+ def _encrypt_from_parts(
+ self, data: bytes, current_time: int, iv: bytes
+ ) -> bytes:
+ utils._check_bytes("data", data)
+
+ padder = padding.PKCS7(algorithms.AES.block_size).padder()
+ padded_data = padder.update(data) + padder.finalize()
+ encryptor = Cipher(
+ algorithms.AES(self._encryption_key),
+ modes.CBC(iv),
+ ).encryptor()
+ ciphertext = encryptor.update(padded_data) + encryptor.finalize()
+
+ basic_parts = (
+ b"\x80"
+ + current_time.to_bytes(length=8, byteorder="big")
+ + iv
+ + ciphertext
+ )
+
+ h = HMAC(self._signing_key, hashes.SHA256())
+ h.update(basic_parts)
+ hmac = h.finalize()
+ return base64.urlsafe_b64encode(basic_parts + hmac)
+
+ def decrypt(
+ self, token: typing.Union[bytes, str], ttl: typing.Optional[int] = None
+ ) -> bytes:
+ timestamp, data = Fernet._get_unverified_token_data(token)
+ if ttl is None:
+ time_info = None
+ else:
+ time_info = (ttl, int(time.time()))
+ return self._decrypt_data(data, timestamp, time_info)
+
+ def decrypt_at_time(
+ self, token: typing.Union[bytes, str], ttl: int, current_time: int
+ ) -> bytes:
+ if ttl is None:
+ raise ValueError(
+ "decrypt_at_time() can only be used with a non-None ttl"
+ )
+ timestamp, data = Fernet._get_unverified_token_data(token)
+ return self._decrypt_data(data, timestamp, (ttl, current_time))
+
+ def extract_timestamp(self, token: typing.Union[bytes, str]) -> int:
+ timestamp, data = Fernet._get_unverified_token_data(token)
+ # Verify the token was not tampered with.
+ self._verify_signature(data)
+ return timestamp
+
+ @staticmethod
+ def _get_unverified_token_data(
+ token: typing.Union[bytes, str]
+ ) -> typing.Tuple[int, bytes]:
+ if not isinstance(token, (str, bytes)):
+ raise TypeError("token must be bytes or str")
+
+ try:
+ data = base64.urlsafe_b64decode(token)
+ except (TypeError, binascii.Error):
+ raise InvalidToken
+
+ if not data or data[0] != 0x80:
+ raise InvalidToken
+
+ if len(data) < 9:
+ raise InvalidToken
+
+ timestamp = int.from_bytes(data[1:9], byteorder="big")
+ return timestamp, data
+
+ def _verify_signature(self, data: bytes) -> None:
+ h = HMAC(self._signing_key, hashes.SHA256())
+ h.update(data[:-32])
+ try:
+ h.verify(data[-32:])
+ except InvalidSignature:
+ raise InvalidToken
+
+ def _decrypt_data(
+ self,
+ data: bytes,
+ timestamp: int,
+ time_info: typing.Optional[typing.Tuple[int, int]],
+ ) -> bytes:
+ if time_info is not None:
+ ttl, current_time = time_info
+ if timestamp + ttl < current_time:
+ raise InvalidToken
+
+ if current_time + _MAX_CLOCK_SKEW < timestamp:
+ raise InvalidToken
+
+ self._verify_signature(data)
+
+ iv = data[9:25]
+ ciphertext = data[25:-32]
+ decryptor = Cipher(
+ algorithms.AES(self._encryption_key), modes.CBC(iv)
+ ).decryptor()
+ plaintext_padded = decryptor.update(ciphertext)
+ try:
+ plaintext_padded += decryptor.finalize()
+ except ValueError:
+ raise InvalidToken
+ unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
+
+ unpadded = unpadder.update(plaintext_padded)
+ try:
+ unpadded += unpadder.finalize()
+ except ValueError:
+ raise InvalidToken
+ return unpadded
+
+
+class MultiFernet:
+ def __init__(self, fernets: typing.Iterable[Fernet]):
+ fernets = list(fernets)
+ if not fernets:
+ raise ValueError(
+ "MultiFernet requires at least one Fernet instance"
+ )
+ self._fernets = fernets
+
+ def encrypt(self, msg: bytes) -> bytes:
+ return self.encrypt_at_time(msg, int(time.time()))
+
+ def encrypt_at_time(self, msg: bytes, current_time: int) -> bytes:
+ return self._fernets[0].encrypt_at_time(msg, current_time)
+
+ def rotate(self, msg: typing.Union[bytes, str]) -> bytes:
+ timestamp, data = Fernet._get_unverified_token_data(msg)
+ for f in self._fernets:
+ try:
+ p = f._decrypt_data(data, timestamp, None)
+ break
+ except InvalidToken:
+ pass
+ else:
+ raise InvalidToken
+
+ iv = os.urandom(16)
+ return self._fernets[0]._encrypt_from_parts(p, timestamp, iv)
+
+ def decrypt(
+ self, msg: typing.Union[bytes, str], ttl: typing.Optional[int] = None
+ ) -> bytes:
+ for f in self._fernets:
+ try:
+ return f.decrypt(msg, ttl)
+ except InvalidToken:
+ pass
+ raise InvalidToken
+
+ def decrypt_at_time(
+ self, msg: typing.Union[bytes, str], ttl: int, current_time: int
+ ) -> bytes:
+ for f in self._fernets:
+ try:
+ return f.decrypt_at_time(msg, ttl, current_time)
+ except InvalidToken:
+ pass
+ raise InvalidToken
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/__init__.py
new file mode 100644
index 0000000000..b9f1187011
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/__init__.py
@@ -0,0 +1,13 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+"""
+Hazardous Materials
+
+This is a "Hazardous Materials" module. You should ONLY use it if you're
+100% absolutely sure that you know what you're doing because this module
+is full of land mines, dragons, and dinosaurs with laser guns.
+"""
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/_oid.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/_oid.py
new file mode 100644
index 0000000000..01d4b34060
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/_oid.py
@@ -0,0 +1,299 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.bindings._rust import (
+ ObjectIdentifier as ObjectIdentifier,
+)
+from cryptography.hazmat.primitives import hashes
+
+
+class ExtensionOID:
+ SUBJECT_DIRECTORY_ATTRIBUTES = ObjectIdentifier("2.5.29.9")
+ SUBJECT_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.14")
+ KEY_USAGE = ObjectIdentifier("2.5.29.15")
+ SUBJECT_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.17")
+ ISSUER_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.18")
+ BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19")
+ NAME_CONSTRAINTS = ObjectIdentifier("2.5.29.30")
+ CRL_DISTRIBUTION_POINTS = ObjectIdentifier("2.5.29.31")
+ CERTIFICATE_POLICIES = ObjectIdentifier("2.5.29.32")
+ POLICY_MAPPINGS = ObjectIdentifier("2.5.29.33")
+ AUTHORITY_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.35")
+ POLICY_CONSTRAINTS = ObjectIdentifier("2.5.29.36")
+ EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37")
+ FRESHEST_CRL = ObjectIdentifier("2.5.29.46")
+ INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54")
+ ISSUING_DISTRIBUTION_POINT = ObjectIdentifier("2.5.29.28")
+ AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1")
+ SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11")
+ OCSP_NO_CHECK = ObjectIdentifier("1.3.6.1.5.5.7.48.1.5")
+ TLS_FEATURE = ObjectIdentifier("1.3.6.1.5.5.7.1.24")
+ CRL_NUMBER = ObjectIdentifier("2.5.29.20")
+ DELTA_CRL_INDICATOR = ObjectIdentifier("2.5.29.27")
+ PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS = ObjectIdentifier(
+ "1.3.6.1.4.1.11129.2.4.2"
+ )
+ PRECERT_POISON = ObjectIdentifier("1.3.6.1.4.1.11129.2.4.3")
+ SIGNED_CERTIFICATE_TIMESTAMPS = ObjectIdentifier("1.3.6.1.4.1.11129.2.4.5")
+ MS_CERTIFICATE_TEMPLATE = ObjectIdentifier("1.3.6.1.4.1.311.21.7")
+
+
+class OCSPExtensionOID:
+ NONCE = ObjectIdentifier("1.3.6.1.5.5.7.48.1.2")
+ ACCEPTABLE_RESPONSES = ObjectIdentifier("1.3.6.1.5.5.7.48.1.4")
+
+
+class CRLEntryExtensionOID:
+ CERTIFICATE_ISSUER = ObjectIdentifier("2.5.29.29")
+ CRL_REASON = ObjectIdentifier("2.5.29.21")
+ INVALIDITY_DATE = ObjectIdentifier("2.5.29.24")
+
+
+class NameOID:
+ COMMON_NAME = ObjectIdentifier("2.5.4.3")
+ COUNTRY_NAME = ObjectIdentifier("2.5.4.6")
+ LOCALITY_NAME = ObjectIdentifier("2.5.4.7")
+ STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8")
+ STREET_ADDRESS = ObjectIdentifier("2.5.4.9")
+ ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10")
+ ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11")
+ SERIAL_NUMBER = ObjectIdentifier("2.5.4.5")
+ SURNAME = ObjectIdentifier("2.5.4.4")
+ GIVEN_NAME = ObjectIdentifier("2.5.4.42")
+ TITLE = ObjectIdentifier("2.5.4.12")
+ INITIALS = ObjectIdentifier("2.5.4.43")
+ GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44")
+ X500_UNIQUE_IDENTIFIER = ObjectIdentifier("2.5.4.45")
+ DN_QUALIFIER = ObjectIdentifier("2.5.4.46")
+ PSEUDONYM = ObjectIdentifier("2.5.4.65")
+ USER_ID = ObjectIdentifier("0.9.2342.19200300.100.1.1")
+ DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25")
+ EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1")
+ JURISDICTION_COUNTRY_NAME = ObjectIdentifier("1.3.6.1.4.1.311.60.2.1.3")
+ JURISDICTION_LOCALITY_NAME = ObjectIdentifier("1.3.6.1.4.1.311.60.2.1.1")
+ JURISDICTION_STATE_OR_PROVINCE_NAME = ObjectIdentifier(
+ "1.3.6.1.4.1.311.60.2.1.2"
+ )
+ BUSINESS_CATEGORY = ObjectIdentifier("2.5.4.15")
+ POSTAL_ADDRESS = ObjectIdentifier("2.5.4.16")
+ POSTAL_CODE = ObjectIdentifier("2.5.4.17")
+ INN = ObjectIdentifier("1.2.643.3.131.1.1")
+ OGRN = ObjectIdentifier("1.2.643.100.1")
+ SNILS = ObjectIdentifier("1.2.643.100.3")
+ UNSTRUCTURED_NAME = ObjectIdentifier("1.2.840.113549.1.9.2")
+
+
+class SignatureAlgorithmOID:
+ RSA_WITH_MD5 = ObjectIdentifier("1.2.840.113549.1.1.4")
+ RSA_WITH_SHA1 = ObjectIdentifier("1.2.840.113549.1.1.5")
+ # This is an alternate OID for RSA with SHA1 that is occasionally seen
+ _RSA_WITH_SHA1 = ObjectIdentifier("1.3.14.3.2.29")
+ RSA_WITH_SHA224 = ObjectIdentifier("1.2.840.113549.1.1.14")
+ RSA_WITH_SHA256 = ObjectIdentifier("1.2.840.113549.1.1.11")
+ RSA_WITH_SHA384 = ObjectIdentifier("1.2.840.113549.1.1.12")
+ RSA_WITH_SHA512 = ObjectIdentifier("1.2.840.113549.1.1.13")
+ RSA_WITH_SHA3_224 = ObjectIdentifier("2.16.840.1.101.3.4.3.13")
+ RSA_WITH_SHA3_256 = ObjectIdentifier("2.16.840.1.101.3.4.3.14")
+ RSA_WITH_SHA3_384 = ObjectIdentifier("2.16.840.1.101.3.4.3.15")
+ RSA_WITH_SHA3_512 = ObjectIdentifier("2.16.840.1.101.3.4.3.16")
+ RSASSA_PSS = ObjectIdentifier("1.2.840.113549.1.1.10")
+ ECDSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10045.4.1")
+ ECDSA_WITH_SHA224 = ObjectIdentifier("1.2.840.10045.4.3.1")
+ ECDSA_WITH_SHA256 = ObjectIdentifier("1.2.840.10045.4.3.2")
+ ECDSA_WITH_SHA384 = ObjectIdentifier("1.2.840.10045.4.3.3")
+ ECDSA_WITH_SHA512 = ObjectIdentifier("1.2.840.10045.4.3.4")
+ ECDSA_WITH_SHA3_224 = ObjectIdentifier("2.16.840.1.101.3.4.3.9")
+ ECDSA_WITH_SHA3_256 = ObjectIdentifier("2.16.840.1.101.3.4.3.10")
+ ECDSA_WITH_SHA3_384 = ObjectIdentifier("2.16.840.1.101.3.4.3.11")
+ ECDSA_WITH_SHA3_512 = ObjectIdentifier("2.16.840.1.101.3.4.3.12")
+ DSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10040.4.3")
+ DSA_WITH_SHA224 = ObjectIdentifier("2.16.840.1.101.3.4.3.1")
+ DSA_WITH_SHA256 = ObjectIdentifier("2.16.840.1.101.3.4.3.2")
+ DSA_WITH_SHA384 = ObjectIdentifier("2.16.840.1.101.3.4.3.3")
+ DSA_WITH_SHA512 = ObjectIdentifier("2.16.840.1.101.3.4.3.4")
+ ED25519 = ObjectIdentifier("1.3.101.112")
+ ED448 = ObjectIdentifier("1.3.101.113")
+ GOSTR3411_94_WITH_3410_2001 = ObjectIdentifier("1.2.643.2.2.3")
+ GOSTR3410_2012_WITH_3411_2012_256 = ObjectIdentifier("1.2.643.7.1.1.3.2")
+ GOSTR3410_2012_WITH_3411_2012_512 = ObjectIdentifier("1.2.643.7.1.1.3.3")
+
+
+_SIG_OIDS_TO_HASH: typing.Dict[
+ ObjectIdentifier, typing.Optional[hashes.HashAlgorithm]
+] = {
+ SignatureAlgorithmOID.RSA_WITH_MD5: hashes.MD5(),
+ SignatureAlgorithmOID.RSA_WITH_SHA1: hashes.SHA1(),
+ SignatureAlgorithmOID._RSA_WITH_SHA1: hashes.SHA1(),
+ SignatureAlgorithmOID.RSA_WITH_SHA224: hashes.SHA224(),
+ SignatureAlgorithmOID.RSA_WITH_SHA256: hashes.SHA256(),
+ SignatureAlgorithmOID.RSA_WITH_SHA384: hashes.SHA384(),
+ SignatureAlgorithmOID.RSA_WITH_SHA512: hashes.SHA512(),
+ SignatureAlgorithmOID.RSA_WITH_SHA3_224: hashes.SHA3_224(),
+ SignatureAlgorithmOID.RSA_WITH_SHA3_256: hashes.SHA3_256(),
+ SignatureAlgorithmOID.RSA_WITH_SHA3_384: hashes.SHA3_384(),
+ SignatureAlgorithmOID.RSA_WITH_SHA3_512: hashes.SHA3_512(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA1: hashes.SHA1(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA224: hashes.SHA224(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA256: hashes.SHA256(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA384: hashes.SHA384(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA512: hashes.SHA512(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA3_224: hashes.SHA3_224(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA3_256: hashes.SHA3_256(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA3_384: hashes.SHA3_384(),
+ SignatureAlgorithmOID.ECDSA_WITH_SHA3_512: hashes.SHA3_512(),
+ SignatureAlgorithmOID.DSA_WITH_SHA1: hashes.SHA1(),
+ SignatureAlgorithmOID.DSA_WITH_SHA224: hashes.SHA224(),
+ SignatureAlgorithmOID.DSA_WITH_SHA256: hashes.SHA256(),
+ SignatureAlgorithmOID.ED25519: None,
+ SignatureAlgorithmOID.ED448: None,
+ SignatureAlgorithmOID.GOSTR3411_94_WITH_3410_2001: None,
+ SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_256: None,
+ SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_512: None,
+}
+
+
+class ExtendedKeyUsageOID:
+ SERVER_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.1")
+ CLIENT_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.2")
+ CODE_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.3")
+ EMAIL_PROTECTION = ObjectIdentifier("1.3.6.1.5.5.7.3.4")
+ TIME_STAMPING = ObjectIdentifier("1.3.6.1.5.5.7.3.8")
+ OCSP_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.9")
+ ANY_EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37.0")
+ SMARTCARD_LOGON = ObjectIdentifier("1.3.6.1.4.1.311.20.2.2")
+ KERBEROS_PKINIT_KDC = ObjectIdentifier("1.3.6.1.5.2.3.5")
+ IPSEC_IKE = ObjectIdentifier("1.3.6.1.5.5.7.3.17")
+ CERTIFICATE_TRANSPARENCY = ObjectIdentifier("1.3.6.1.4.1.11129.2.4.4")
+
+
+class AuthorityInformationAccessOID:
+ CA_ISSUERS = ObjectIdentifier("1.3.6.1.5.5.7.48.2")
+ OCSP = ObjectIdentifier("1.3.6.1.5.5.7.48.1")
+
+
+class SubjectInformationAccessOID:
+ CA_REPOSITORY = ObjectIdentifier("1.3.6.1.5.5.7.48.5")
+
+
+class CertificatePoliciesOID:
+ CPS_QUALIFIER = ObjectIdentifier("1.3.6.1.5.5.7.2.1")
+ CPS_USER_NOTICE = ObjectIdentifier("1.3.6.1.5.5.7.2.2")
+ ANY_POLICY = ObjectIdentifier("2.5.29.32.0")
+
+
+class AttributeOID:
+ CHALLENGE_PASSWORD = ObjectIdentifier("1.2.840.113549.1.9.7")
+ UNSTRUCTURED_NAME = ObjectIdentifier("1.2.840.113549.1.9.2")
+
+
+_OID_NAMES = {
+ NameOID.COMMON_NAME: "commonName",
+ NameOID.COUNTRY_NAME: "countryName",
+ NameOID.LOCALITY_NAME: "localityName",
+ NameOID.STATE_OR_PROVINCE_NAME: "stateOrProvinceName",
+ NameOID.STREET_ADDRESS: "streetAddress",
+ NameOID.ORGANIZATION_NAME: "organizationName",
+ NameOID.ORGANIZATIONAL_UNIT_NAME: "organizationalUnitName",
+ NameOID.SERIAL_NUMBER: "serialNumber",
+ NameOID.SURNAME: "surname",
+ NameOID.GIVEN_NAME: "givenName",
+ NameOID.TITLE: "title",
+ NameOID.GENERATION_QUALIFIER: "generationQualifier",
+ NameOID.X500_UNIQUE_IDENTIFIER: "x500UniqueIdentifier",
+ NameOID.DN_QUALIFIER: "dnQualifier",
+ NameOID.PSEUDONYM: "pseudonym",
+ NameOID.USER_ID: "userID",
+ NameOID.DOMAIN_COMPONENT: "domainComponent",
+ NameOID.EMAIL_ADDRESS: "emailAddress",
+ NameOID.JURISDICTION_COUNTRY_NAME: "jurisdictionCountryName",
+ NameOID.JURISDICTION_LOCALITY_NAME: "jurisdictionLocalityName",
+ NameOID.JURISDICTION_STATE_OR_PROVINCE_NAME: (
+ "jurisdictionStateOrProvinceName"
+ ),
+ NameOID.BUSINESS_CATEGORY: "businessCategory",
+ NameOID.POSTAL_ADDRESS: "postalAddress",
+ NameOID.POSTAL_CODE: "postalCode",
+ NameOID.INN: "INN",
+ NameOID.OGRN: "OGRN",
+ NameOID.SNILS: "SNILS",
+ NameOID.UNSTRUCTURED_NAME: "unstructuredName",
+ SignatureAlgorithmOID.RSA_WITH_MD5: "md5WithRSAEncryption",
+ SignatureAlgorithmOID.RSA_WITH_SHA1: "sha1WithRSAEncryption",
+ SignatureAlgorithmOID.RSA_WITH_SHA224: "sha224WithRSAEncryption",
+ SignatureAlgorithmOID.RSA_WITH_SHA256: "sha256WithRSAEncryption",
+ SignatureAlgorithmOID.RSA_WITH_SHA384: "sha384WithRSAEncryption",
+ SignatureAlgorithmOID.RSA_WITH_SHA512: "sha512WithRSAEncryption",
+ SignatureAlgorithmOID.RSASSA_PSS: "RSASSA-PSS",
+ SignatureAlgorithmOID.ECDSA_WITH_SHA1: "ecdsa-with-SHA1",
+ SignatureAlgorithmOID.ECDSA_WITH_SHA224: "ecdsa-with-SHA224",
+ SignatureAlgorithmOID.ECDSA_WITH_SHA256: "ecdsa-with-SHA256",
+ SignatureAlgorithmOID.ECDSA_WITH_SHA384: "ecdsa-with-SHA384",
+ SignatureAlgorithmOID.ECDSA_WITH_SHA512: "ecdsa-with-SHA512",
+ SignatureAlgorithmOID.DSA_WITH_SHA1: "dsa-with-sha1",
+ SignatureAlgorithmOID.DSA_WITH_SHA224: "dsa-with-sha224",
+ SignatureAlgorithmOID.DSA_WITH_SHA256: "dsa-with-sha256",
+ SignatureAlgorithmOID.ED25519: "ed25519",
+ SignatureAlgorithmOID.ED448: "ed448",
+ SignatureAlgorithmOID.GOSTR3411_94_WITH_3410_2001: (
+ "GOST R 34.11-94 with GOST R 34.10-2001"
+ ),
+ SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_256: (
+ "GOST R 34.10-2012 with GOST R 34.11-2012 (256 bit)"
+ ),
+ SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_512: (
+ "GOST R 34.10-2012 with GOST R 34.11-2012 (512 bit)"
+ ),
+ ExtendedKeyUsageOID.SERVER_AUTH: "serverAuth",
+ ExtendedKeyUsageOID.CLIENT_AUTH: "clientAuth",
+ ExtendedKeyUsageOID.CODE_SIGNING: "codeSigning",
+ ExtendedKeyUsageOID.EMAIL_PROTECTION: "emailProtection",
+ ExtendedKeyUsageOID.TIME_STAMPING: "timeStamping",
+ ExtendedKeyUsageOID.OCSP_SIGNING: "OCSPSigning",
+ ExtendedKeyUsageOID.SMARTCARD_LOGON: "msSmartcardLogin",
+ ExtendedKeyUsageOID.KERBEROS_PKINIT_KDC: "pkInitKDC",
+ ExtensionOID.SUBJECT_DIRECTORY_ATTRIBUTES: "subjectDirectoryAttributes",
+ ExtensionOID.SUBJECT_KEY_IDENTIFIER: "subjectKeyIdentifier",
+ ExtensionOID.KEY_USAGE: "keyUsage",
+ ExtensionOID.SUBJECT_ALTERNATIVE_NAME: "subjectAltName",
+ ExtensionOID.ISSUER_ALTERNATIVE_NAME: "issuerAltName",
+ ExtensionOID.BASIC_CONSTRAINTS: "basicConstraints",
+ ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS: (
+ "signedCertificateTimestampList"
+ ),
+ ExtensionOID.SIGNED_CERTIFICATE_TIMESTAMPS: (
+ "signedCertificateTimestampList"
+ ),
+ ExtensionOID.PRECERT_POISON: "ctPoison",
+ ExtensionOID.MS_CERTIFICATE_TEMPLATE: "msCertificateTemplate",
+ CRLEntryExtensionOID.CRL_REASON: "cRLReason",
+ CRLEntryExtensionOID.INVALIDITY_DATE: "invalidityDate",
+ CRLEntryExtensionOID.CERTIFICATE_ISSUER: "certificateIssuer",
+ ExtensionOID.NAME_CONSTRAINTS: "nameConstraints",
+ ExtensionOID.CRL_DISTRIBUTION_POINTS: "cRLDistributionPoints",
+ ExtensionOID.CERTIFICATE_POLICIES: "certificatePolicies",
+ ExtensionOID.POLICY_MAPPINGS: "policyMappings",
+ ExtensionOID.AUTHORITY_KEY_IDENTIFIER: "authorityKeyIdentifier",
+ ExtensionOID.POLICY_CONSTRAINTS: "policyConstraints",
+ ExtensionOID.EXTENDED_KEY_USAGE: "extendedKeyUsage",
+ ExtensionOID.FRESHEST_CRL: "freshestCRL",
+ ExtensionOID.INHIBIT_ANY_POLICY: "inhibitAnyPolicy",
+ ExtensionOID.ISSUING_DISTRIBUTION_POINT: ("issuingDistributionPoint"),
+ ExtensionOID.AUTHORITY_INFORMATION_ACCESS: "authorityInfoAccess",
+ ExtensionOID.SUBJECT_INFORMATION_ACCESS: "subjectInfoAccess",
+ ExtensionOID.OCSP_NO_CHECK: "OCSPNoCheck",
+ ExtensionOID.CRL_NUMBER: "cRLNumber",
+ ExtensionOID.DELTA_CRL_INDICATOR: "deltaCRLIndicator",
+ ExtensionOID.TLS_FEATURE: "TLSFeature",
+ AuthorityInformationAccessOID.OCSP: "OCSP",
+ AuthorityInformationAccessOID.CA_ISSUERS: "caIssuers",
+ SubjectInformationAccessOID.CA_REPOSITORY: "caRepository",
+ CertificatePoliciesOID.CPS_QUALIFIER: "id-qt-cps",
+ CertificatePoliciesOID.CPS_USER_NOTICE: "id-qt-unotice",
+ OCSPExtensionOID.NONCE: "OCSPNonce",
+ AttributeOID.CHALLENGE_PASSWORD: "challengePassword",
+}
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/__init__.py
new file mode 100644
index 0000000000..b4400aa037
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/__init__.py
@@ -0,0 +1,13 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from typing import Any
+
+
+def default_backend() -> Any:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ return backend
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/__init__.py
new file mode 100644
index 0000000000..51b04476cb
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/__init__.py
@@ -0,0 +1,9 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.backends.openssl.backend import backend
+
+__all__ = ["backend"]
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/aead.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/aead.py
new file mode 100644
index 0000000000..b36f535f3f
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/aead.py
@@ -0,0 +1,527 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.exceptions import InvalidTag
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+ from cryptography.hazmat.primitives.ciphers.aead import (
+ AESCCM,
+ AESGCM,
+ AESOCB3,
+ AESSIV,
+ ChaCha20Poly1305,
+ )
+
+ _AEADTypes = typing.Union[
+ AESCCM, AESGCM, AESOCB3, AESSIV, ChaCha20Poly1305
+ ]
+
+
+def _is_evp_aead_supported_cipher(
+ backend: Backend, cipher: _AEADTypes
+) -> bool:
+ """
+ Checks whether the given cipher is supported through
+ EVP_AEAD rather than the normal OpenSSL EVP_CIPHER API.
+ """
+ from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
+
+ return backend._lib.Cryptography_HAS_EVP_AEAD and isinstance(
+ cipher, ChaCha20Poly1305
+ )
+
+
+def _aead_cipher_supported(backend: Backend, cipher: _AEADTypes) -> bool:
+ if _is_evp_aead_supported_cipher(backend, cipher):
+ return True
+ else:
+ cipher_name = _evp_cipher_cipher_name(cipher)
+ if backend._fips_enabled and cipher_name not in backend._fips_aead:
+ return False
+ # SIV isn't loaded through get_cipherbyname but instead a new fetch API
+ # only available in 3.0+. But if we know we're on 3.0+ then we know
+ # it's supported.
+ if cipher_name.endswith(b"-siv"):
+ return backend._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER == 1
+ else:
+ return (
+ backend._lib.EVP_get_cipherbyname(cipher_name)
+ != backend._ffi.NULL
+ )
+
+
+def _aead_create_ctx(
+ backend: Backend,
+ cipher: _AEADTypes,
+ key: bytes,
+):
+ if _is_evp_aead_supported_cipher(backend, cipher):
+ return _evp_aead_create_ctx(backend, cipher, key)
+ else:
+ return _evp_cipher_create_ctx(backend, cipher, key)
+
+
+def _encrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any = None,
+) -> bytes:
+ if _is_evp_aead_supported_cipher(backend, cipher):
+ return _evp_aead_encrypt(
+ backend, cipher, nonce, data, associated_data, tag_length, ctx
+ )
+ else:
+ return _evp_cipher_encrypt(
+ backend, cipher, nonce, data, associated_data, tag_length, ctx
+ )
+
+
+def _decrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any = None,
+) -> bytes:
+ if _is_evp_aead_supported_cipher(backend, cipher):
+ return _evp_aead_decrypt(
+ backend, cipher, nonce, data, associated_data, tag_length, ctx
+ )
+ else:
+ return _evp_cipher_decrypt(
+ backend, cipher, nonce, data, associated_data, tag_length, ctx
+ )
+
+
+def _evp_aead_create_ctx(
+ backend: Backend,
+ cipher: _AEADTypes,
+ key: bytes,
+ tag_len: typing.Optional[int] = None,
+):
+ aead_cipher = _evp_aead_get_cipher(backend, cipher)
+ assert aead_cipher is not None
+ key_ptr = backend._ffi.from_buffer(key)
+ tag_len = (
+ backend._lib.EVP_AEAD_DEFAULT_TAG_LENGTH
+ if tag_len is None
+ else tag_len
+ )
+ ctx = backend._lib.Cryptography_EVP_AEAD_CTX_new(
+ aead_cipher, key_ptr, len(key), tag_len
+ )
+ backend.openssl_assert(ctx != backend._ffi.NULL)
+ ctx = backend._ffi.gc(ctx, backend._lib.EVP_AEAD_CTX_free)
+ return ctx
+
+
+def _evp_aead_get_cipher(backend: Backend, cipher: _AEADTypes):
+ from cryptography.hazmat.primitives.ciphers.aead import (
+ ChaCha20Poly1305,
+ )
+
+ # Currently only ChaCha20-Poly1305 is supported using this API
+ assert isinstance(cipher, ChaCha20Poly1305)
+ return backend._lib.EVP_aead_chacha20_poly1305()
+
+
+def _evp_aead_encrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any,
+) -> bytes:
+ assert ctx is not None
+
+ aead_cipher = _evp_aead_get_cipher(backend, cipher)
+ assert aead_cipher is not None
+
+ out_len = backend._ffi.new("size_t *")
+ # max_out_len should be in_len plus the result of
+ # EVP_AEAD_max_overhead.
+ max_out_len = len(data) + backend._lib.EVP_AEAD_max_overhead(aead_cipher)
+ out_buf = backend._ffi.new("uint8_t[]", max_out_len)
+ data_ptr = backend._ffi.from_buffer(data)
+ nonce_ptr = backend._ffi.from_buffer(nonce)
+ aad = b"".join(associated_data)
+ aad_ptr = backend._ffi.from_buffer(aad)
+
+ res = backend._lib.EVP_AEAD_CTX_seal(
+ ctx,
+ out_buf,
+ out_len,
+ max_out_len,
+ nonce_ptr,
+ len(nonce),
+ data_ptr,
+ len(data),
+ aad_ptr,
+ len(aad),
+ )
+ backend.openssl_assert(res == 1)
+ encrypted_data = backend._ffi.buffer(out_buf, out_len[0])[:]
+ return encrypted_data
+
+
+def _evp_aead_decrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any,
+) -> bytes:
+ if len(data) < tag_length:
+ raise InvalidTag
+
+ assert ctx is not None
+
+ out_len = backend._ffi.new("size_t *")
+ # max_out_len should at least in_len
+ max_out_len = len(data)
+ out_buf = backend._ffi.new("uint8_t[]", max_out_len)
+ data_ptr = backend._ffi.from_buffer(data)
+ nonce_ptr = backend._ffi.from_buffer(nonce)
+ aad = b"".join(associated_data)
+ aad_ptr = backend._ffi.from_buffer(aad)
+
+ res = backend._lib.EVP_AEAD_CTX_open(
+ ctx,
+ out_buf,
+ out_len,
+ max_out_len,
+ nonce_ptr,
+ len(nonce),
+ data_ptr,
+ len(data),
+ aad_ptr,
+ len(aad),
+ )
+
+ if res == 0:
+ backend._consume_errors()
+ raise InvalidTag
+
+ decrypted_data = backend._ffi.buffer(out_buf, out_len[0])[:]
+ return decrypted_data
+
+
+_ENCRYPT = 1
+_DECRYPT = 0
+
+
+def _evp_cipher_cipher_name(cipher: _AEADTypes) -> bytes:
+ from cryptography.hazmat.primitives.ciphers.aead import (
+ AESCCM,
+ AESGCM,
+ AESOCB3,
+ AESSIV,
+ ChaCha20Poly1305,
+ )
+
+ if isinstance(cipher, ChaCha20Poly1305):
+ return b"chacha20-poly1305"
+ elif isinstance(cipher, AESCCM):
+ return f"aes-{len(cipher._key) * 8}-ccm".encode("ascii")
+ elif isinstance(cipher, AESOCB3):
+ return f"aes-{len(cipher._key) * 8}-ocb".encode("ascii")
+ elif isinstance(cipher, AESSIV):
+ return f"aes-{len(cipher._key) * 8 // 2}-siv".encode("ascii")
+ else:
+ assert isinstance(cipher, AESGCM)
+ return f"aes-{len(cipher._key) * 8}-gcm".encode("ascii")
+
+
+def _evp_cipher(cipher_name: bytes, backend: Backend):
+ if cipher_name.endswith(b"-siv"):
+ evp_cipher = backend._lib.EVP_CIPHER_fetch(
+ backend._ffi.NULL,
+ cipher_name,
+ backend._ffi.NULL,
+ )
+ backend.openssl_assert(evp_cipher != backend._ffi.NULL)
+ evp_cipher = backend._ffi.gc(evp_cipher, backend._lib.EVP_CIPHER_free)
+ else:
+ evp_cipher = backend._lib.EVP_get_cipherbyname(cipher_name)
+ backend.openssl_assert(evp_cipher != backend._ffi.NULL)
+
+ return evp_cipher
+
+
+def _evp_cipher_create_ctx(
+ backend: Backend,
+ cipher: _AEADTypes,
+ key: bytes,
+):
+ ctx = backend._lib.EVP_CIPHER_CTX_new()
+ backend.openssl_assert(ctx != backend._ffi.NULL)
+ ctx = backend._ffi.gc(ctx, backend._lib.EVP_CIPHER_CTX_free)
+ cipher_name = _evp_cipher_cipher_name(cipher)
+ evp_cipher = _evp_cipher(cipher_name, backend)
+ key_ptr = backend._ffi.from_buffer(key)
+ res = backend._lib.EVP_CipherInit_ex(
+ ctx,
+ evp_cipher,
+ backend._ffi.NULL,
+ key_ptr,
+ backend._ffi.NULL,
+ 0,
+ )
+ backend.openssl_assert(res != 0)
+ return ctx
+
+
+def _evp_cipher_aead_setup(
+ backend: Backend,
+ cipher_name: bytes,
+ key: bytes,
+ nonce: bytes,
+ tag: typing.Optional[bytes],
+ tag_len: int,
+ operation: int,
+):
+ evp_cipher = _evp_cipher(cipher_name, backend)
+ ctx = backend._lib.EVP_CIPHER_CTX_new()
+ ctx = backend._ffi.gc(ctx, backend._lib.EVP_CIPHER_CTX_free)
+ res = backend._lib.EVP_CipherInit_ex(
+ ctx,
+ evp_cipher,
+ backend._ffi.NULL,
+ backend._ffi.NULL,
+ backend._ffi.NULL,
+ int(operation == _ENCRYPT),
+ )
+ backend.openssl_assert(res != 0)
+ # CCM requires the IVLEN to be set before calling SET_TAG on decrypt
+ res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx,
+ backend._lib.EVP_CTRL_AEAD_SET_IVLEN,
+ len(nonce),
+ backend._ffi.NULL,
+ )
+ backend.openssl_assert(res != 0)
+ if operation == _DECRYPT:
+ assert tag is not None
+ _evp_cipher_set_tag(backend, ctx, tag)
+ elif cipher_name.endswith(b"-ccm"):
+ res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx,
+ backend._lib.EVP_CTRL_AEAD_SET_TAG,
+ tag_len,
+ backend._ffi.NULL,
+ )
+ backend.openssl_assert(res != 0)
+
+ nonce_ptr = backend._ffi.from_buffer(nonce)
+ key_ptr = backend._ffi.from_buffer(key)
+ res = backend._lib.EVP_CipherInit_ex(
+ ctx,
+ backend._ffi.NULL,
+ backend._ffi.NULL,
+ key_ptr,
+ nonce_ptr,
+ int(operation == _ENCRYPT),
+ )
+ backend.openssl_assert(res != 0)
+ return ctx
+
+
+def _evp_cipher_set_tag(backend, ctx, tag: bytes) -> None:
+ tag_ptr = backend._ffi.from_buffer(tag)
+ res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx, backend._lib.EVP_CTRL_AEAD_SET_TAG, len(tag), tag_ptr
+ )
+ backend.openssl_assert(res != 0)
+
+
+def _evp_cipher_set_nonce_operation(
+ backend, ctx, nonce: bytes, operation: int
+) -> None:
+ nonce_ptr = backend._ffi.from_buffer(nonce)
+ res = backend._lib.EVP_CipherInit_ex(
+ ctx,
+ backend._ffi.NULL,
+ backend._ffi.NULL,
+ backend._ffi.NULL,
+ nonce_ptr,
+ int(operation == _ENCRYPT),
+ )
+ backend.openssl_assert(res != 0)
+
+
+def _evp_cipher_set_length(backend: Backend, ctx, data_len: int) -> None:
+ intptr = backend._ffi.new("int *")
+ res = backend._lib.EVP_CipherUpdate(
+ ctx, backend._ffi.NULL, intptr, backend._ffi.NULL, data_len
+ )
+ backend.openssl_assert(res != 0)
+
+
+def _evp_cipher_process_aad(
+ backend: Backend, ctx, associated_data: bytes
+) -> None:
+ outlen = backend._ffi.new("int *")
+ a_data_ptr = backend._ffi.from_buffer(associated_data)
+ res = backend._lib.EVP_CipherUpdate(
+ ctx, backend._ffi.NULL, outlen, a_data_ptr, len(associated_data)
+ )
+ backend.openssl_assert(res != 0)
+
+
+def _evp_cipher_process_data(backend: Backend, ctx, data: bytes) -> bytes:
+ outlen = backend._ffi.new("int *")
+ buf = backend._ffi.new("unsigned char[]", len(data))
+ data_ptr = backend._ffi.from_buffer(data)
+ res = backend._lib.EVP_CipherUpdate(ctx, buf, outlen, data_ptr, len(data))
+ if res == 0:
+ # AES SIV can error here if the data is invalid on decrypt
+ backend._consume_errors()
+ raise InvalidTag
+ return backend._ffi.buffer(buf, outlen[0])[:]
+
+
+def _evp_cipher_encrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any = None,
+) -> bytes:
+ from cryptography.hazmat.primitives.ciphers.aead import AESCCM, AESSIV
+
+ if ctx is None:
+ cipher_name = _evp_cipher_cipher_name(cipher)
+ ctx = _evp_cipher_aead_setup(
+ backend,
+ cipher_name,
+ cipher._key,
+ nonce,
+ None,
+ tag_length,
+ _ENCRYPT,
+ )
+ else:
+ _evp_cipher_set_nonce_operation(backend, ctx, nonce, _ENCRYPT)
+
+ # CCM requires us to pass the length of the data before processing
+ # anything.
+ # However calling this with any other AEAD results in an error
+ if isinstance(cipher, AESCCM):
+ _evp_cipher_set_length(backend, ctx, len(data))
+
+ for ad in associated_data:
+ _evp_cipher_process_aad(backend, ctx, ad)
+ processed_data = _evp_cipher_process_data(backend, ctx, data)
+ outlen = backend._ffi.new("int *")
+ # All AEADs we support besides OCB are streaming so they return nothing
+ # in finalization. OCB can return up to (16 byte block - 1) bytes so
+ # we need a buffer here too.
+ buf = backend._ffi.new("unsigned char[]", 16)
+ res = backend._lib.EVP_CipherFinal_ex(ctx, buf, outlen)
+ backend.openssl_assert(res != 0)
+ processed_data += backend._ffi.buffer(buf, outlen[0])[:]
+ tag_buf = backend._ffi.new("unsigned char[]", tag_length)
+ res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx, backend._lib.EVP_CTRL_AEAD_GET_TAG, tag_length, tag_buf
+ )
+ backend.openssl_assert(res != 0)
+ tag = backend._ffi.buffer(tag_buf)[:]
+
+ if isinstance(cipher, AESSIV):
+ # RFC 5297 defines the output as IV || C, where the tag we generate
+ # is the "IV" and C is the ciphertext. This is the opposite of our
+ # other AEADs, which are Ciphertext || Tag
+ backend.openssl_assert(len(tag) == 16)
+ return tag + processed_data
+ else:
+ return processed_data + tag
+
+
+def _evp_cipher_decrypt(
+ backend: Backend,
+ cipher: _AEADTypes,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ tag_length: int,
+ ctx: typing.Any = None,
+) -> bytes:
+ from cryptography.hazmat.primitives.ciphers.aead import AESCCM, AESSIV
+
+ if len(data) < tag_length:
+ raise InvalidTag
+
+ if isinstance(cipher, AESSIV):
+ # RFC 5297 defines the output as IV || C, where the tag we generate
+ # is the "IV" and C is the ciphertext. This is the opposite of our
+ # other AEADs, which are Ciphertext || Tag
+ tag = data[:tag_length]
+ data = data[tag_length:]
+ else:
+ tag = data[-tag_length:]
+ data = data[:-tag_length]
+ if ctx is None:
+ cipher_name = _evp_cipher_cipher_name(cipher)
+ ctx = _evp_cipher_aead_setup(
+ backend,
+ cipher_name,
+ cipher._key,
+ nonce,
+ tag,
+ tag_length,
+ _DECRYPT,
+ )
+ else:
+ _evp_cipher_set_nonce_operation(backend, ctx, nonce, _DECRYPT)
+ _evp_cipher_set_tag(backend, ctx, tag)
+
+ # CCM requires us to pass the length of the data before processing
+ # anything.
+ # However calling this with any other AEAD results in an error
+ if isinstance(cipher, AESCCM):
+ _evp_cipher_set_length(backend, ctx, len(data))
+
+ for ad in associated_data:
+ _evp_cipher_process_aad(backend, ctx, ad)
+ # CCM has a different error path if the tag doesn't match. Errors are
+ # raised in Update and Final is irrelevant.
+ if isinstance(cipher, AESCCM):
+ outlen = backend._ffi.new("int *")
+ buf = backend._ffi.new("unsigned char[]", len(data))
+ d_ptr = backend._ffi.from_buffer(data)
+ res = backend._lib.EVP_CipherUpdate(ctx, buf, outlen, d_ptr, len(data))
+ if res != 1:
+ backend._consume_errors()
+ raise InvalidTag
+
+ processed_data = backend._ffi.buffer(buf, outlen[0])[:]
+ else:
+ processed_data = _evp_cipher_process_data(backend, ctx, data)
+ outlen = backend._ffi.new("int *")
+ # OCB can return up to 15 bytes (16 byte block - 1) in finalization
+ buf = backend._ffi.new("unsigned char[]", 16)
+ res = backend._lib.EVP_CipherFinal_ex(ctx, buf, outlen)
+ processed_data += backend._ffi.buffer(buf, outlen[0])[:]
+ if res == 0:
+ backend._consume_errors()
+ raise InvalidTag
+
+ return processed_data
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/backend.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/backend.py
new file mode 100644
index 0000000000..f1c79008b6
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/backend.py
@@ -0,0 +1,1938 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import collections
+import contextlib
+import itertools
+import typing
+from contextlib import contextmanager
+
+from cryptography import utils, x509
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.backends.openssl import aead
+from cryptography.hazmat.backends.openssl.ciphers import _CipherContext
+from cryptography.hazmat.backends.openssl.cmac import _CMACContext
+from cryptography.hazmat.backends.openssl.ec import (
+ _EllipticCurvePrivateKey,
+ _EllipticCurvePublicKey,
+)
+from cryptography.hazmat.backends.openssl.rsa import (
+ _RSAPrivateKey,
+ _RSAPublicKey,
+)
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.bindings.openssl import binding
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives._asymmetric import AsymmetricPadding
+from cryptography.hazmat.primitives.asymmetric import (
+ dh,
+ dsa,
+ ec,
+ ed448,
+ ed25519,
+ rsa,
+ x448,
+ x25519,
+)
+from cryptography.hazmat.primitives.asymmetric.padding import (
+ MGF1,
+ OAEP,
+ PSS,
+ PKCS1v15,
+)
+from cryptography.hazmat.primitives.asymmetric.types import (
+ PrivateKeyTypes,
+ PublicKeyTypes,
+)
+from cryptography.hazmat.primitives.ciphers import (
+ BlockCipherAlgorithm,
+ CipherAlgorithm,
+)
+from cryptography.hazmat.primitives.ciphers.algorithms import (
+ AES,
+ AES128,
+ AES256,
+ ARC4,
+ SM4,
+ Camellia,
+ ChaCha20,
+ TripleDES,
+ _BlowfishInternal,
+ _CAST5Internal,
+ _IDEAInternal,
+ _SEEDInternal,
+)
+from cryptography.hazmat.primitives.ciphers.modes import (
+ CBC,
+ CFB,
+ CFB8,
+ CTR,
+ ECB,
+ GCM,
+ OFB,
+ XTS,
+ Mode,
+)
+from cryptography.hazmat.primitives.serialization import ssh
+from cryptography.hazmat.primitives.serialization.pkcs12 import (
+ PBES,
+ PKCS12Certificate,
+ PKCS12KeyAndCertificates,
+ PKCS12PrivateKeyTypes,
+ _PKCS12CATypes,
+)
+
+_MemoryBIO = collections.namedtuple("_MemoryBIO", ["bio", "char_ptr"])
+
+
+# Not actually supported, just used as a marker for some serialization tests.
+class _RC2:
+ pass
+
+
+class Backend:
+ """
+ OpenSSL API binding interfaces.
+ """
+
+ name = "openssl"
+
+ # FIPS has opinions about acceptable algorithms and key sizes, but the
+ # disallowed algorithms are still present in OpenSSL. They just error if
+ # you try to use them. To avoid that we allowlist the algorithms in
+ # FIPS 140-3. This isn't ideal, but FIPS 140-3 is trash so here we are.
+ _fips_aead = {
+ b"aes-128-ccm",
+ b"aes-192-ccm",
+ b"aes-256-ccm",
+ b"aes-128-gcm",
+ b"aes-192-gcm",
+ b"aes-256-gcm",
+ }
+ # TripleDES encryption is disallowed/deprecated throughout 2023 in
+ # FIPS 140-3. To keep it simple we denylist any use of TripleDES (TDEA).
+ _fips_ciphers = (AES,)
+ # Sometimes SHA1 is still permissible. That logic is contained
+ # within the various *_supported methods.
+ _fips_hashes = (
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+ hashes.SHA512_224,
+ hashes.SHA512_256,
+ hashes.SHA3_224,
+ hashes.SHA3_256,
+ hashes.SHA3_384,
+ hashes.SHA3_512,
+ hashes.SHAKE128,
+ hashes.SHAKE256,
+ )
+ _fips_ecdh_curves = (
+ ec.SECP224R1,
+ ec.SECP256R1,
+ ec.SECP384R1,
+ ec.SECP521R1,
+ )
+ _fips_rsa_min_key_size = 2048
+ _fips_rsa_min_public_exponent = 65537
+ _fips_dsa_min_modulus = 1 << 2048
+ _fips_dh_min_key_size = 2048
+ _fips_dh_min_modulus = 1 << _fips_dh_min_key_size
+
+ def __init__(self) -> None:
+ self._binding = binding.Binding()
+ self._ffi = self._binding.ffi
+ self._lib = self._binding.lib
+ self._fips_enabled = rust_openssl.is_fips_enabled()
+
+ self._cipher_registry: typing.Dict[
+ typing.Tuple[typing.Type[CipherAlgorithm], typing.Type[Mode]],
+ typing.Callable,
+ ] = {}
+ self._register_default_ciphers()
+ self._dh_types = [self._lib.EVP_PKEY_DH]
+ if self._lib.Cryptography_HAS_EVP_PKEY_DHX:
+ self._dh_types.append(self._lib.EVP_PKEY_DHX)
+
+ def __repr__(self) -> str:
+ return "<OpenSSLBackend(version: {}, FIPS: {}, Legacy: {})>".format(
+ self.openssl_version_text(),
+ self._fips_enabled,
+ self._binding._legacy_provider_loaded,
+ )
+
+ def openssl_assert(
+ self,
+ ok: bool,
+ errors: typing.Optional[typing.List[rust_openssl.OpenSSLError]] = None,
+ ) -> None:
+ return binding._openssl_assert(self._lib, ok, errors=errors)
+
+ def _enable_fips(self) -> None:
+ # This function enables FIPS mode for OpenSSL 3.0.0 on installs that
+ # have the FIPS provider installed properly.
+ self._binding._enable_fips()
+ assert rust_openssl.is_fips_enabled()
+ self._fips_enabled = rust_openssl.is_fips_enabled()
+
+ def openssl_version_text(self) -> str:
+ """
+ Friendly string name of the loaded OpenSSL library. This is not
+ necessarily the same version as it was compiled against.
+
+ Example: OpenSSL 1.1.1d 10 Sep 2019
+ """
+ return self._ffi.string(
+ self._lib.OpenSSL_version(self._lib.OPENSSL_VERSION)
+ ).decode("ascii")
+
+ def openssl_version_number(self) -> int:
+ return self._lib.OpenSSL_version_num()
+
+ def _evp_md_from_algorithm(self, algorithm: hashes.HashAlgorithm):
+ if algorithm.name == "blake2b" or algorithm.name == "blake2s":
+ alg = "{}{}".format(
+ algorithm.name, algorithm.digest_size * 8
+ ).encode("ascii")
+ else:
+ alg = algorithm.name.encode("ascii")
+
+ evp_md = self._lib.EVP_get_digestbyname(alg)
+ return evp_md
+
+ def _evp_md_non_null_from_algorithm(self, algorithm: hashes.HashAlgorithm):
+ evp_md = self._evp_md_from_algorithm(algorithm)
+ self.openssl_assert(evp_md != self._ffi.NULL)
+ return evp_md
+
+ def hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
+ if self._fips_enabled and not isinstance(algorithm, self._fips_hashes):
+ return False
+
+ evp_md = self._evp_md_from_algorithm(algorithm)
+ return evp_md != self._ffi.NULL
+
+ def signature_hash_supported(
+ self, algorithm: hashes.HashAlgorithm
+ ) -> bool:
+ # Dedicated check for hashing algorithm use in message digest for
+ # signatures, e.g. RSA PKCS#1 v1.5 SHA1 (sha1WithRSAEncryption).
+ if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
+ return False
+ return self.hash_supported(algorithm)
+
+ def scrypt_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ else:
+ return self._lib.Cryptography_HAS_SCRYPT == 1
+
+ def hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
+ # FIPS mode still allows SHA1 for HMAC
+ if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
+ return True
+
+ return self.hash_supported(algorithm)
+
+ def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool:
+ if self._fips_enabled:
+ # FIPS mode requires AES. TripleDES is disallowed/deprecated in
+ # FIPS 140-3.
+ if not isinstance(cipher, self._fips_ciphers):
+ return False
+
+ try:
+ adapter = self._cipher_registry[type(cipher), type(mode)]
+ except KeyError:
+ return False
+ evp_cipher = adapter(self, cipher, mode)
+ return self._ffi.NULL != evp_cipher
+
+ def register_cipher_adapter(self, cipher_cls, mode_cls, adapter) -> None:
+ if (cipher_cls, mode_cls) in self._cipher_registry:
+ raise ValueError(
+ "Duplicate registration for: {} {}.".format(
+ cipher_cls, mode_cls
+ )
+ )
+ self._cipher_registry[cipher_cls, mode_cls] = adapter
+
+ def _register_default_ciphers(self) -> None:
+ for cipher_cls in [AES, AES128, AES256]:
+ for mode_cls in [CBC, CTR, ECB, OFB, CFB, CFB8, GCM]:
+ self.register_cipher_adapter(
+ cipher_cls,
+ mode_cls,
+ GetCipherByName(
+ "{cipher.name}-{cipher.key_size}-{mode.name}"
+ ),
+ )
+ for mode_cls in [CBC, CTR, ECB, OFB, CFB]:
+ self.register_cipher_adapter(
+ Camellia,
+ mode_cls,
+ GetCipherByName("{cipher.name}-{cipher.key_size}-{mode.name}"),
+ )
+ for mode_cls in [CBC, CFB, CFB8, OFB]:
+ self.register_cipher_adapter(
+ TripleDES, mode_cls, GetCipherByName("des-ede3-{mode.name}")
+ )
+ self.register_cipher_adapter(
+ TripleDES, ECB, GetCipherByName("des-ede3")
+ )
+ self.register_cipher_adapter(
+ ChaCha20, type(None), GetCipherByName("chacha20")
+ )
+ self.register_cipher_adapter(AES, XTS, _get_xts_cipher)
+ for mode_cls in [ECB, CBC, OFB, CFB, CTR]:
+ self.register_cipher_adapter(
+ SM4, mode_cls, GetCipherByName("sm4-{mode.name}")
+ )
+ # Don't register legacy ciphers if they're unavailable. Hypothetically
+ # this wouldn't be necessary because we test availability by seeing if
+ # we get an EVP_CIPHER * in the _CipherContext __init__, but OpenSSL 3
+ # will return a valid pointer even though the cipher is unavailable.
+ if (
+ self._binding._legacy_provider_loaded
+ or not self._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER
+ ):
+ for mode_cls in [CBC, CFB, OFB, ECB]:
+ self.register_cipher_adapter(
+ _BlowfishInternal,
+ mode_cls,
+ GetCipherByName("bf-{mode.name}"),
+ )
+ for mode_cls in [CBC, CFB, OFB, ECB]:
+ self.register_cipher_adapter(
+ _SEEDInternal,
+ mode_cls,
+ GetCipherByName("seed-{mode.name}"),
+ )
+ for cipher_cls, mode_cls in itertools.product(
+ [_CAST5Internal, _IDEAInternal],
+ [CBC, OFB, CFB, ECB],
+ ):
+ self.register_cipher_adapter(
+ cipher_cls,
+ mode_cls,
+ GetCipherByName("{cipher.name}-{mode.name}"),
+ )
+ self.register_cipher_adapter(
+ ARC4, type(None), GetCipherByName("rc4")
+ )
+ # We don't actually support RC2, this is just used by some tests.
+ self.register_cipher_adapter(
+ _RC2, type(None), GetCipherByName("rc2")
+ )
+
+ def create_symmetric_encryption_ctx(
+ self, cipher: CipherAlgorithm, mode: Mode
+ ) -> _CipherContext:
+ return _CipherContext(self, cipher, mode, _CipherContext._ENCRYPT)
+
+ def create_symmetric_decryption_ctx(
+ self, cipher: CipherAlgorithm, mode: Mode
+ ) -> _CipherContext:
+ return _CipherContext(self, cipher, mode, _CipherContext._DECRYPT)
+
+ def pbkdf2_hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
+ return self.hmac_supported(algorithm)
+
+ def _consume_errors(self) -> typing.List[rust_openssl.OpenSSLError]:
+ return rust_openssl.capture_error_stack()
+
+ def _bn_to_int(self, bn) -> int:
+ assert bn != self._ffi.NULL
+ self.openssl_assert(not self._lib.BN_is_negative(bn))
+
+ bn_num_bytes = self._lib.BN_num_bytes(bn)
+ bin_ptr = self._ffi.new("unsigned char[]", bn_num_bytes)
+ bin_len = self._lib.BN_bn2bin(bn, bin_ptr)
+ # A zero length means the BN has value 0
+ self.openssl_assert(bin_len >= 0)
+ val = int.from_bytes(self._ffi.buffer(bin_ptr)[:bin_len], "big")
+ return val
+
+ def _int_to_bn(self, num: int):
+ """
+ Converts a python integer to a BIGNUM. The returned BIGNUM will not
+ be garbage collected (to support adding them to structs that take
+ ownership of the object). Be sure to register it for GC if it will
+ be discarded after use.
+ """
+ binary = num.to_bytes(int(num.bit_length() / 8.0 + 1), "big")
+ bn_ptr = self._lib.BN_bin2bn(binary, len(binary), self._ffi.NULL)
+ self.openssl_assert(bn_ptr != self._ffi.NULL)
+ return bn_ptr
+
+ def generate_rsa_private_key(
+ self, public_exponent: int, key_size: int
+ ) -> rsa.RSAPrivateKey:
+ rsa._verify_rsa_parameters(public_exponent, key_size)
+
+ rsa_cdata = self._lib.RSA_new()
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+
+ bn = self._int_to_bn(public_exponent)
+ bn = self._ffi.gc(bn, self._lib.BN_free)
+
+ res = self._lib.RSA_generate_key_ex(
+ rsa_cdata, key_size, bn, self._ffi.NULL
+ )
+ self.openssl_assert(res == 1)
+ evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
+
+ # We can skip RSA key validation here since we just generated the key
+ return _RSAPrivateKey(
+ self, rsa_cdata, evp_pkey, unsafe_skip_rsa_key_validation=True
+ )
+
+ def generate_rsa_parameters_supported(
+ self, public_exponent: int, key_size: int
+ ) -> bool:
+ return (
+ public_exponent >= 3
+ and public_exponent & 1 != 0
+ and key_size >= 512
+ )
+
+ def load_rsa_private_numbers(
+ self,
+ numbers: rsa.RSAPrivateNumbers,
+ unsafe_skip_rsa_key_validation: bool,
+ ) -> rsa.RSAPrivateKey:
+ rsa._check_private_key_components(
+ numbers.p,
+ numbers.q,
+ numbers.d,
+ numbers.dmp1,
+ numbers.dmq1,
+ numbers.iqmp,
+ numbers.public_numbers.e,
+ numbers.public_numbers.n,
+ )
+ rsa_cdata = self._lib.RSA_new()
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ p = self._int_to_bn(numbers.p)
+ q = self._int_to_bn(numbers.q)
+ d = self._int_to_bn(numbers.d)
+ dmp1 = self._int_to_bn(numbers.dmp1)
+ dmq1 = self._int_to_bn(numbers.dmq1)
+ iqmp = self._int_to_bn(numbers.iqmp)
+ e = self._int_to_bn(numbers.public_numbers.e)
+ n = self._int_to_bn(numbers.public_numbers.n)
+ res = self._lib.RSA_set0_factors(rsa_cdata, p, q)
+ self.openssl_assert(res == 1)
+ res = self._lib.RSA_set0_key(rsa_cdata, n, e, d)
+ self.openssl_assert(res == 1)
+ res = self._lib.RSA_set0_crt_params(rsa_cdata, dmp1, dmq1, iqmp)
+ self.openssl_assert(res == 1)
+ evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
+
+ return _RSAPrivateKey(
+ self,
+ rsa_cdata,
+ evp_pkey,
+ unsafe_skip_rsa_key_validation=unsafe_skip_rsa_key_validation,
+ )
+
+ def load_rsa_public_numbers(
+ self, numbers: rsa.RSAPublicNumbers
+ ) -> rsa.RSAPublicKey:
+ rsa._check_public_key_components(numbers.e, numbers.n)
+ rsa_cdata = self._lib.RSA_new()
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ e = self._int_to_bn(numbers.e)
+ n = self._int_to_bn(numbers.n)
+ res = self._lib.RSA_set0_key(rsa_cdata, n, e, self._ffi.NULL)
+ self.openssl_assert(res == 1)
+ evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
+
+ return _RSAPublicKey(self, rsa_cdata, evp_pkey)
+
+ def _create_evp_pkey_gc(self):
+ evp_pkey = self._lib.EVP_PKEY_new()
+ self.openssl_assert(evp_pkey != self._ffi.NULL)
+ evp_pkey = self._ffi.gc(evp_pkey, self._lib.EVP_PKEY_free)
+ return evp_pkey
+
+ def _rsa_cdata_to_evp_pkey(self, rsa_cdata):
+ evp_pkey = self._create_evp_pkey_gc()
+ res = self._lib.EVP_PKEY_set1_RSA(evp_pkey, rsa_cdata)
+ self.openssl_assert(res == 1)
+ return evp_pkey
+
+ def _bytes_to_bio(self, data: bytes) -> _MemoryBIO:
+ """
+ Return a _MemoryBIO namedtuple of (BIO, char*).
+
+ The char* is the storage for the BIO and it must stay alive until the
+ BIO is finished with.
+ """
+ data_ptr = self._ffi.from_buffer(data)
+ bio = self._lib.BIO_new_mem_buf(data_ptr, len(data))
+ self.openssl_assert(bio != self._ffi.NULL)
+
+ return _MemoryBIO(self._ffi.gc(bio, self._lib.BIO_free), data_ptr)
+
+ def _create_mem_bio_gc(self):
+ """
+ Creates an empty memory BIO.
+ """
+ bio_method = self._lib.BIO_s_mem()
+ self.openssl_assert(bio_method != self._ffi.NULL)
+ bio = self._lib.BIO_new(bio_method)
+ self.openssl_assert(bio != self._ffi.NULL)
+ bio = self._ffi.gc(bio, self._lib.BIO_free)
+ return bio
+
+ def _read_mem_bio(self, bio) -> bytes:
+ """
+ Reads a memory BIO. This only works on memory BIOs.
+ """
+ buf = self._ffi.new("char **")
+ buf_len = self._lib.BIO_get_mem_data(bio, buf)
+ self.openssl_assert(buf_len > 0)
+ self.openssl_assert(buf[0] != self._ffi.NULL)
+ bio_data = self._ffi.buffer(buf[0], buf_len)[:]
+ return bio_data
+
+ def _evp_pkey_to_private_key(
+ self, evp_pkey, unsafe_skip_rsa_key_validation: bool
+ ) -> PrivateKeyTypes:
+ """
+ Return the appropriate type of PrivateKey given an evp_pkey cdata
+ pointer.
+ """
+
+ key_type = self._lib.EVP_PKEY_id(evp_pkey)
+
+ if key_type == self._lib.EVP_PKEY_RSA:
+ rsa_cdata = self._lib.EVP_PKEY_get1_RSA(evp_pkey)
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ return _RSAPrivateKey(
+ self,
+ rsa_cdata,
+ evp_pkey,
+ unsafe_skip_rsa_key_validation=unsafe_skip_rsa_key_validation,
+ )
+ elif (
+ key_type == self._lib.EVP_PKEY_RSA_PSS
+ and not self._lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+ and not self._lib.CRYPTOGRAPHY_OPENSSL_LESS_THAN_111E
+ ):
+ # At the moment the way we handle RSA PSS keys is to strip the
+ # PSS constraints from them and treat them as normal RSA keys
+ # Unfortunately the RSA * itself tracks this data so we need to
+ # extract, serialize, and reload it without the constraints.
+ rsa_cdata = self._lib.EVP_PKEY_get1_RSA(evp_pkey)
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ bio = self._create_mem_bio_gc()
+ res = self._lib.i2d_RSAPrivateKey_bio(bio, rsa_cdata)
+ self.openssl_assert(res == 1)
+ return self.load_der_private_key(
+ self._read_mem_bio(bio),
+ password=None,
+ unsafe_skip_rsa_key_validation=unsafe_skip_rsa_key_validation,
+ )
+ elif key_type == self._lib.EVP_PKEY_DSA:
+ return rust_openssl.dsa.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == self._lib.EVP_PKEY_EC:
+ ec_cdata = self._lib.EVP_PKEY_get1_EC_KEY(evp_pkey)
+ self.openssl_assert(ec_cdata != self._ffi.NULL)
+ ec_cdata = self._ffi.gc(ec_cdata, self._lib.EC_KEY_free)
+ return _EllipticCurvePrivateKey(self, ec_cdata, evp_pkey)
+ elif key_type in self._dh_types:
+ return rust_openssl.dh.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_ED25519", None):
+ # EVP_PKEY_ED25519 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.ed25519.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_X448", None):
+ # EVP_PKEY_X448 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.x448.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == self._lib.EVP_PKEY_X25519:
+ return rust_openssl.x25519.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_ED448", None):
+ # EVP_PKEY_ED448 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.ed448.private_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ else:
+ raise UnsupportedAlgorithm("Unsupported key type.")
+
+ def _evp_pkey_to_public_key(self, evp_pkey) -> PublicKeyTypes:
+ """
+ Return the appropriate type of PublicKey given an evp_pkey cdata
+ pointer.
+ """
+
+ key_type = self._lib.EVP_PKEY_id(evp_pkey)
+
+ if key_type == self._lib.EVP_PKEY_RSA:
+ rsa_cdata = self._lib.EVP_PKEY_get1_RSA(evp_pkey)
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ return _RSAPublicKey(self, rsa_cdata, evp_pkey)
+ elif (
+ key_type == self._lib.EVP_PKEY_RSA_PSS
+ and not self._lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+ and not self._lib.CRYPTOGRAPHY_OPENSSL_LESS_THAN_111E
+ ):
+ rsa_cdata = self._lib.EVP_PKEY_get1_RSA(evp_pkey)
+ self.openssl_assert(rsa_cdata != self._ffi.NULL)
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ bio = self._create_mem_bio_gc()
+ res = self._lib.i2d_RSAPublicKey_bio(bio, rsa_cdata)
+ self.openssl_assert(res == 1)
+ return self.load_der_public_key(self._read_mem_bio(bio))
+ elif key_type == self._lib.EVP_PKEY_DSA:
+ return rust_openssl.dsa.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == self._lib.EVP_PKEY_EC:
+ ec_cdata = self._lib.EVP_PKEY_get1_EC_KEY(evp_pkey)
+ if ec_cdata == self._ffi.NULL:
+ errors = self._consume_errors()
+ raise ValueError("Unable to load EC key", errors)
+ ec_cdata = self._ffi.gc(ec_cdata, self._lib.EC_KEY_free)
+ return _EllipticCurvePublicKey(self, ec_cdata, evp_pkey)
+ elif key_type in self._dh_types:
+ return rust_openssl.dh.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_ED25519", None):
+ # EVP_PKEY_ED25519 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.ed25519.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_X448", None):
+ # EVP_PKEY_X448 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.x448.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == self._lib.EVP_PKEY_X25519:
+ return rust_openssl.x25519.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ elif key_type == getattr(self._lib, "EVP_PKEY_ED448", None):
+ # EVP_PKEY_ED448 is not present in CRYPTOGRAPHY_IS_LIBRESSL
+ return rust_openssl.ed448.public_key_from_ptr(
+ int(self._ffi.cast("uintptr_t", evp_pkey))
+ )
+ else:
+ raise UnsupportedAlgorithm("Unsupported key type.")
+
+ def _oaep_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
+ if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
+ return False
+
+ return isinstance(
+ algorithm,
+ (
+ hashes.SHA1,
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+ ),
+ )
+
+ def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool:
+ if isinstance(padding, PKCS1v15):
+ return True
+ elif isinstance(padding, PSS) and isinstance(padding._mgf, MGF1):
+ # SHA1 is permissible in MGF1 in FIPS even when SHA1 is blocked
+ # as signature algorithm.
+ if self._fips_enabled and isinstance(
+ padding._mgf._algorithm, hashes.SHA1
+ ):
+ return True
+ else:
+ return self.hash_supported(padding._mgf._algorithm)
+ elif isinstance(padding, OAEP) and isinstance(padding._mgf, MGF1):
+ return self._oaep_hash_supported(
+ padding._mgf._algorithm
+ ) and self._oaep_hash_supported(padding._algorithm)
+ else:
+ return False
+
+ def rsa_encryption_supported(self, padding: AsymmetricPadding) -> bool:
+ if self._fips_enabled and isinstance(padding, PKCS1v15):
+ return False
+ else:
+ return self.rsa_padding_supported(padding)
+
+ def generate_dsa_parameters(self, key_size: int) -> dsa.DSAParameters:
+ if key_size not in (1024, 2048, 3072, 4096):
+ raise ValueError(
+ "Key size must be 1024, 2048, 3072, or 4096 bits."
+ )
+
+ return rust_openssl.dsa.generate_parameters(key_size)
+
+ def generate_dsa_private_key(
+ self, parameters: dsa.DSAParameters
+ ) -> dsa.DSAPrivateKey:
+ return parameters.generate_private_key()
+
+ def generate_dsa_private_key_and_parameters(
+ self, key_size: int
+ ) -> dsa.DSAPrivateKey:
+ parameters = self.generate_dsa_parameters(key_size)
+ return self.generate_dsa_private_key(parameters)
+
+ def load_dsa_private_numbers(
+ self, numbers: dsa.DSAPrivateNumbers
+ ) -> dsa.DSAPrivateKey:
+ dsa._check_dsa_private_numbers(numbers)
+ return rust_openssl.dsa.from_private_numbers(numbers)
+
+ def load_dsa_public_numbers(
+ self, numbers: dsa.DSAPublicNumbers
+ ) -> dsa.DSAPublicKey:
+ dsa._check_dsa_parameters(numbers.parameter_numbers)
+ return rust_openssl.dsa.from_public_numbers(numbers)
+
+ def load_dsa_parameter_numbers(
+ self, numbers: dsa.DSAParameterNumbers
+ ) -> dsa.DSAParameters:
+ dsa._check_dsa_parameters(numbers)
+ return rust_openssl.dsa.from_parameter_numbers(numbers)
+
+ def dsa_supported(self) -> bool:
+ return (
+ not self._lib.CRYPTOGRAPHY_IS_BORINGSSL and not self._fips_enabled
+ )
+
+ def dsa_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
+ if not self.dsa_supported():
+ return False
+ return self.signature_hash_supported(algorithm)
+
+ def cmac_algorithm_supported(self, algorithm) -> bool:
+ return self.cipher_supported(
+ algorithm, CBC(b"\x00" * algorithm.block_size)
+ )
+
+ def create_cmac_ctx(self, algorithm: BlockCipherAlgorithm) -> _CMACContext:
+ return _CMACContext(self, algorithm)
+
+ def load_pem_private_key(
+ self,
+ data: bytes,
+ password: typing.Optional[bytes],
+ unsafe_skip_rsa_key_validation: bool,
+ ) -> PrivateKeyTypes:
+ return self._load_key(
+ self._lib.PEM_read_bio_PrivateKey,
+ data,
+ password,
+ unsafe_skip_rsa_key_validation,
+ )
+
+ def load_pem_public_key(self, data: bytes) -> PublicKeyTypes:
+ mem_bio = self._bytes_to_bio(data)
+ # In OpenSSL 3.0.x the PEM_read_bio_PUBKEY function will invoke
+ # the default password callback if you pass an encrypted private
+ # key. This is very, very, very bad as the default callback can
+ # trigger an interactive console prompt, which will hang the
+ # Python process. We therefore provide our own callback to
+ # catch this and error out properly.
+ userdata = self._ffi.new("CRYPTOGRAPHY_PASSWORD_DATA *")
+ evp_pkey = self._lib.PEM_read_bio_PUBKEY(
+ mem_bio.bio,
+ self._ffi.NULL,
+ self._ffi.addressof(
+ self._lib._original_lib, "Cryptography_pem_password_cb"
+ ),
+ userdata,
+ )
+ if evp_pkey != self._ffi.NULL:
+ evp_pkey = self._ffi.gc(evp_pkey, self._lib.EVP_PKEY_free)
+ return self._evp_pkey_to_public_key(evp_pkey)
+ else:
+ # It's not a (RSA/DSA/ECDSA) subjectPublicKeyInfo, but we still
+ # need to check to see if it is a pure PKCS1 RSA public key (not
+ # embedded in a subjectPublicKeyInfo)
+ self._consume_errors()
+ res = self._lib.BIO_reset(mem_bio.bio)
+ self.openssl_assert(res == 1)
+ rsa_cdata = self._lib.PEM_read_bio_RSAPublicKey(
+ mem_bio.bio,
+ self._ffi.NULL,
+ self._ffi.addressof(
+ self._lib._original_lib, "Cryptography_pem_password_cb"
+ ),
+ userdata,
+ )
+ if rsa_cdata != self._ffi.NULL:
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
+ return _RSAPublicKey(self, rsa_cdata, evp_pkey)
+ else:
+ self._handle_key_loading_error()
+
+ def load_pem_parameters(self, data: bytes) -> dh.DHParameters:
+ return rust_openssl.dh.from_pem_parameters(data)
+
+ def load_der_private_key(
+ self,
+ data: bytes,
+ password: typing.Optional[bytes],
+ unsafe_skip_rsa_key_validation: bool,
+ ) -> PrivateKeyTypes:
+ # OpenSSL has a function called d2i_AutoPrivateKey that in theory
+ # handles this automatically, however it doesn't handle encrypted
+ # private keys. Instead we try to load the key two different ways.
+ # First we'll try to load it as a traditional key.
+ bio_data = self._bytes_to_bio(data)
+ key = self._evp_pkey_from_der_traditional_key(bio_data, password)
+ if key:
+ return self._evp_pkey_to_private_key(
+ key, unsafe_skip_rsa_key_validation
+ )
+ else:
+ # Finally we try to load it with the method that handles encrypted
+ # PKCS8 properly.
+ return self._load_key(
+ self._lib.d2i_PKCS8PrivateKey_bio,
+ data,
+ password,
+ unsafe_skip_rsa_key_validation,
+ )
+
+ def _evp_pkey_from_der_traditional_key(self, bio_data, password):
+ key = self._lib.d2i_PrivateKey_bio(bio_data.bio, self._ffi.NULL)
+ if key != self._ffi.NULL:
+ key = self._ffi.gc(key, self._lib.EVP_PKEY_free)
+ if password is not None:
+ raise TypeError(
+ "Password was given but private key is not encrypted."
+ )
+
+ return key
+ else:
+ self._consume_errors()
+ return None
+
+ def load_der_public_key(self, data: bytes) -> PublicKeyTypes:
+ mem_bio = self._bytes_to_bio(data)
+ evp_pkey = self._lib.d2i_PUBKEY_bio(mem_bio.bio, self._ffi.NULL)
+ if evp_pkey != self._ffi.NULL:
+ evp_pkey = self._ffi.gc(evp_pkey, self._lib.EVP_PKEY_free)
+ return self._evp_pkey_to_public_key(evp_pkey)
+ else:
+ # It's not a (RSA/DSA/ECDSA) subjectPublicKeyInfo, but we still
+ # need to check to see if it is a pure PKCS1 RSA public key (not
+ # embedded in a subjectPublicKeyInfo)
+ self._consume_errors()
+ res = self._lib.BIO_reset(mem_bio.bio)
+ self.openssl_assert(res == 1)
+ rsa_cdata = self._lib.d2i_RSAPublicKey_bio(
+ mem_bio.bio, self._ffi.NULL
+ )
+ if rsa_cdata != self._ffi.NULL:
+ rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
+ evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
+ return _RSAPublicKey(self, rsa_cdata, evp_pkey)
+ else:
+ self._handle_key_loading_error()
+
+ def load_der_parameters(self, data: bytes) -> dh.DHParameters:
+ return rust_openssl.dh.from_der_parameters(data)
+
+ def _cert2ossl(self, cert: x509.Certificate) -> typing.Any:
+ data = cert.public_bytes(serialization.Encoding.DER)
+ mem_bio = self._bytes_to_bio(data)
+ x509 = self._lib.d2i_X509_bio(mem_bio.bio, self._ffi.NULL)
+ self.openssl_assert(x509 != self._ffi.NULL)
+ x509 = self._ffi.gc(x509, self._lib.X509_free)
+ return x509
+
+ def _ossl2cert(self, x509_ptr: typing.Any) -> x509.Certificate:
+ bio = self._create_mem_bio_gc()
+ res = self._lib.i2d_X509_bio(bio, x509_ptr)
+ self.openssl_assert(res == 1)
+ return x509.load_der_x509_certificate(self._read_mem_bio(bio))
+
+ def _key2ossl(self, key: PKCS12PrivateKeyTypes) -> typing.Any:
+ data = key.private_bytes(
+ serialization.Encoding.DER,
+ serialization.PrivateFormat.PKCS8,
+ serialization.NoEncryption(),
+ )
+ mem_bio = self._bytes_to_bio(data)
+
+ evp_pkey = self._lib.d2i_PrivateKey_bio(
+ mem_bio.bio,
+ self._ffi.NULL,
+ )
+ self.openssl_assert(evp_pkey != self._ffi.NULL)
+ return self._ffi.gc(evp_pkey, self._lib.EVP_PKEY_free)
+
+ def _load_key(
+ self, openssl_read_func, data, password, unsafe_skip_rsa_key_validation
+ ) -> PrivateKeyTypes:
+ mem_bio = self._bytes_to_bio(data)
+
+ userdata = self._ffi.new("CRYPTOGRAPHY_PASSWORD_DATA *")
+ if password is not None:
+ utils._check_byteslike("password", password)
+ password_ptr = self._ffi.from_buffer(password)
+ userdata.password = password_ptr
+ userdata.length = len(password)
+
+ evp_pkey = openssl_read_func(
+ mem_bio.bio,
+ self._ffi.NULL,
+ self._ffi.addressof(
+ self._lib._original_lib, "Cryptography_pem_password_cb"
+ ),
+ userdata,
+ )
+
+ if evp_pkey == self._ffi.NULL:
+ if userdata.error != 0:
+ self._consume_errors()
+ if userdata.error == -1:
+ raise TypeError(
+ "Password was not given but private key is encrypted"
+ )
+ else:
+ assert userdata.error == -2
+ raise ValueError(
+ "Passwords longer than {} bytes are not supported "
+ "by this backend.".format(userdata.maxsize - 1)
+ )
+ else:
+ self._handle_key_loading_error()
+
+ evp_pkey = self._ffi.gc(evp_pkey, self._lib.EVP_PKEY_free)
+
+ if password is not None and userdata.called == 0:
+ raise TypeError(
+ "Password was given but private key is not encrypted."
+ )
+
+ assert (
+ password is not None and userdata.called == 1
+ ) or password is None
+
+ return self._evp_pkey_to_private_key(
+ evp_pkey, unsafe_skip_rsa_key_validation
+ )
+
+ def _handle_key_loading_error(self) -> typing.NoReturn:
+ errors = self._consume_errors()
+
+ if not errors:
+ raise ValueError(
+ "Could not deserialize key data. The data may be in an "
+ "incorrect format or it may be encrypted with an unsupported "
+ "algorithm."
+ )
+
+ elif (
+ errors[0]._lib_reason_match(
+ self._lib.ERR_LIB_EVP, self._lib.EVP_R_BAD_DECRYPT
+ )
+ or errors[0]._lib_reason_match(
+ self._lib.ERR_LIB_PKCS12,
+ self._lib.PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
+ )
+ or (
+ self._lib.Cryptography_HAS_PROVIDERS
+ and errors[0]._lib_reason_match(
+ self._lib.ERR_LIB_PROV,
+ self._lib.PROV_R_BAD_DECRYPT,
+ )
+ )
+ ):
+ raise ValueError("Bad decrypt. Incorrect password?")
+
+ elif any(
+ error._lib_reason_match(
+ self._lib.ERR_LIB_EVP,
+ self._lib.EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
+ )
+ for error in errors
+ ):
+ raise ValueError("Unsupported public key algorithm.")
+
+ else:
+ raise ValueError(
+ "Could not deserialize key data. The data may be in an "
+ "incorrect format, it may be encrypted with an unsupported "
+ "algorithm, or it may be an unsupported key type (e.g. EC "
+ "curves with explicit parameters).",
+ errors,
+ )
+
+ def elliptic_curve_supported(self, curve: ec.EllipticCurve) -> bool:
+ try:
+ curve_nid = self._elliptic_curve_to_nid(curve)
+ except UnsupportedAlgorithm:
+ curve_nid = self._lib.NID_undef
+
+ group = self._lib.EC_GROUP_new_by_curve_name(curve_nid)
+
+ if group == self._ffi.NULL:
+ self._consume_errors()
+ return False
+ else:
+ self.openssl_assert(curve_nid != self._lib.NID_undef)
+ self._lib.EC_GROUP_free(group)
+ return True
+
+ def elliptic_curve_signature_algorithm_supported(
+ self,
+ signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
+ curve: ec.EllipticCurve,
+ ) -> bool:
+ # We only support ECDSA right now.
+ if not isinstance(signature_algorithm, ec.ECDSA):
+ return False
+
+ return self.elliptic_curve_supported(curve)
+
+ def generate_elliptic_curve_private_key(
+ self, curve: ec.EllipticCurve
+ ) -> ec.EllipticCurvePrivateKey:
+ """
+ Generate a new private key on the named curve.
+ """
+
+ if self.elliptic_curve_supported(curve):
+ ec_cdata = self._ec_key_new_by_curve(curve)
+
+ res = self._lib.EC_KEY_generate_key(ec_cdata)
+ self.openssl_assert(res == 1)
+
+ evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata)
+
+ return _EllipticCurvePrivateKey(self, ec_cdata, evp_pkey)
+ else:
+ raise UnsupportedAlgorithm(
+ f"Backend object does not support {curve.name}.",
+ _Reasons.UNSUPPORTED_ELLIPTIC_CURVE,
+ )
+
+ def load_elliptic_curve_private_numbers(
+ self, numbers: ec.EllipticCurvePrivateNumbers
+ ) -> ec.EllipticCurvePrivateKey:
+ public = numbers.public_numbers
+
+ ec_cdata = self._ec_key_new_by_curve(public.curve)
+
+ private_value = self._ffi.gc(
+ self._int_to_bn(numbers.private_value), self._lib.BN_clear_free
+ )
+ res = self._lib.EC_KEY_set_private_key(ec_cdata, private_value)
+ if res != 1:
+ self._consume_errors()
+ raise ValueError("Invalid EC key.")
+
+ with self._tmp_bn_ctx() as bn_ctx:
+ self._ec_key_set_public_key_affine_coordinates(
+ ec_cdata, public.x, public.y, bn_ctx
+ )
+ # derive the expected public point and compare it to the one we
+ # just set based on the values we were given. If they don't match
+ # this isn't a valid key pair.
+ group = self._lib.EC_KEY_get0_group(ec_cdata)
+ self.openssl_assert(group != self._ffi.NULL)
+ set_point = backend._lib.EC_KEY_get0_public_key(ec_cdata)
+ self.openssl_assert(set_point != self._ffi.NULL)
+ computed_point = self._lib.EC_POINT_new(group)
+ self.openssl_assert(computed_point != self._ffi.NULL)
+ computed_point = self._ffi.gc(
+ computed_point, self._lib.EC_POINT_free
+ )
+ res = self._lib.EC_POINT_mul(
+ group,
+ computed_point,
+ private_value,
+ self._ffi.NULL,
+ self._ffi.NULL,
+ bn_ctx,
+ )
+ self.openssl_assert(res == 1)
+ if (
+ self._lib.EC_POINT_cmp(
+ group, set_point, computed_point, bn_ctx
+ )
+ != 0
+ ):
+ raise ValueError("Invalid EC key.")
+
+ evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata)
+
+ return _EllipticCurvePrivateKey(self, ec_cdata, evp_pkey)
+
+ def load_elliptic_curve_public_numbers(
+ self, numbers: ec.EllipticCurvePublicNumbers
+ ) -> ec.EllipticCurvePublicKey:
+ ec_cdata = self._ec_key_new_by_curve(numbers.curve)
+ with self._tmp_bn_ctx() as bn_ctx:
+ self._ec_key_set_public_key_affine_coordinates(
+ ec_cdata, numbers.x, numbers.y, bn_ctx
+ )
+ evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata)
+
+ return _EllipticCurvePublicKey(self, ec_cdata, evp_pkey)
+
+ def load_elliptic_curve_public_bytes(
+ self, curve: ec.EllipticCurve, point_bytes: bytes
+ ) -> ec.EllipticCurvePublicKey:
+ ec_cdata = self._ec_key_new_by_curve(curve)
+ group = self._lib.EC_KEY_get0_group(ec_cdata)
+ self.openssl_assert(group != self._ffi.NULL)
+ point = self._lib.EC_POINT_new(group)
+ self.openssl_assert(point != self._ffi.NULL)
+ point = self._ffi.gc(point, self._lib.EC_POINT_free)
+ with self._tmp_bn_ctx() as bn_ctx:
+ res = self._lib.EC_POINT_oct2point(
+ group, point, point_bytes, len(point_bytes), bn_ctx
+ )
+ if res != 1:
+ self._consume_errors()
+ raise ValueError("Invalid public bytes for the given curve")
+
+ res = self._lib.EC_KEY_set_public_key(ec_cdata, point)
+ self.openssl_assert(res == 1)
+ evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata)
+ return _EllipticCurvePublicKey(self, ec_cdata, evp_pkey)
+
+ def derive_elliptic_curve_private_key(
+ self, private_value: int, curve: ec.EllipticCurve
+ ) -> ec.EllipticCurvePrivateKey:
+ ec_cdata = self._ec_key_new_by_curve(curve)
+
+ group = self._lib.EC_KEY_get0_group(ec_cdata)
+ self.openssl_assert(group != self._ffi.NULL)
+
+ point = self._lib.EC_POINT_new(group)
+ self.openssl_assert(point != self._ffi.NULL)
+ point = self._ffi.gc(point, self._lib.EC_POINT_free)
+
+ value = self._int_to_bn(private_value)
+ value = self._ffi.gc(value, self._lib.BN_clear_free)
+
+ with self._tmp_bn_ctx() as bn_ctx:
+ res = self._lib.EC_POINT_mul(
+ group, point, value, self._ffi.NULL, self._ffi.NULL, bn_ctx
+ )
+ self.openssl_assert(res == 1)
+
+ bn_x = self._lib.BN_CTX_get(bn_ctx)
+ bn_y = self._lib.BN_CTX_get(bn_ctx)
+
+ res = self._lib.EC_POINT_get_affine_coordinates(
+ group, point, bn_x, bn_y, bn_ctx
+ )
+ if res != 1:
+ self._consume_errors()
+ raise ValueError("Unable to derive key from private_value")
+
+ res = self._lib.EC_KEY_set_public_key(ec_cdata, point)
+ self.openssl_assert(res == 1)
+ private = self._int_to_bn(private_value)
+ private = self._ffi.gc(private, self._lib.BN_clear_free)
+ res = self._lib.EC_KEY_set_private_key(ec_cdata, private)
+ self.openssl_assert(res == 1)
+
+ evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata)
+
+ return _EllipticCurvePrivateKey(self, ec_cdata, evp_pkey)
+
+ def _ec_key_new_by_curve(self, curve: ec.EllipticCurve):
+ curve_nid = self._elliptic_curve_to_nid(curve)
+ return self._ec_key_new_by_curve_nid(curve_nid)
+
+ def _ec_key_new_by_curve_nid(self, curve_nid: int):
+ ec_cdata = self._lib.EC_KEY_new_by_curve_name(curve_nid)
+ self.openssl_assert(ec_cdata != self._ffi.NULL)
+ return self._ffi.gc(ec_cdata, self._lib.EC_KEY_free)
+
+ def elliptic_curve_exchange_algorithm_supported(
+ self, algorithm: ec.ECDH, curve: ec.EllipticCurve
+ ) -> bool:
+ if self._fips_enabled and not isinstance(
+ curve, self._fips_ecdh_curves
+ ):
+ return False
+
+ return self.elliptic_curve_supported(curve) and isinstance(
+ algorithm, ec.ECDH
+ )
+
+ def _ec_cdata_to_evp_pkey(self, ec_cdata):
+ evp_pkey = self._create_evp_pkey_gc()
+ res = self._lib.EVP_PKEY_set1_EC_KEY(evp_pkey, ec_cdata)
+ self.openssl_assert(res == 1)
+ return evp_pkey
+
+ def _elliptic_curve_to_nid(self, curve: ec.EllipticCurve) -> int:
+ """
+ Get the NID for a curve name.
+ """
+
+ curve_aliases = {"secp192r1": "prime192v1", "secp256r1": "prime256v1"}
+
+ curve_name = curve_aliases.get(curve.name, curve.name)
+
+ curve_nid = self._lib.OBJ_sn2nid(curve_name.encode())
+ if curve_nid == self._lib.NID_undef:
+ raise UnsupportedAlgorithm(
+ f"{curve.name} is not a supported elliptic curve",
+ _Reasons.UNSUPPORTED_ELLIPTIC_CURVE,
+ )
+ return curve_nid
+
+ @contextmanager
+ def _tmp_bn_ctx(self):
+ bn_ctx = self._lib.BN_CTX_new()
+ self.openssl_assert(bn_ctx != self._ffi.NULL)
+ bn_ctx = self._ffi.gc(bn_ctx, self._lib.BN_CTX_free)
+ self._lib.BN_CTX_start(bn_ctx)
+ try:
+ yield bn_ctx
+ finally:
+ self._lib.BN_CTX_end(bn_ctx)
+
+ def _ec_key_set_public_key_affine_coordinates(
+ self,
+ ec_cdata,
+ x: int,
+ y: int,
+ bn_ctx,
+ ) -> None:
+ """
+ Sets the public key point in the EC_KEY context to the affine x and y
+ values.
+ """
+
+ if x < 0 or y < 0:
+ raise ValueError(
+ "Invalid EC key. Both x and y must be non-negative."
+ )
+
+ x = self._ffi.gc(self._int_to_bn(x), self._lib.BN_free)
+ y = self._ffi.gc(self._int_to_bn(y), self._lib.BN_free)
+ group = self._lib.EC_KEY_get0_group(ec_cdata)
+ self.openssl_assert(group != self._ffi.NULL)
+ point = self._lib.EC_POINT_new(group)
+ self.openssl_assert(point != self._ffi.NULL)
+ point = self._ffi.gc(point, self._lib.EC_POINT_free)
+ res = self._lib.EC_POINT_set_affine_coordinates(
+ group, point, x, y, bn_ctx
+ )
+ if res != 1:
+ self._consume_errors()
+ raise ValueError("Invalid EC key.")
+ res = self._lib.EC_KEY_set_public_key(ec_cdata, point)
+ self.openssl_assert(res == 1)
+
+ def _private_key_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PrivateFormat,
+ encryption_algorithm: serialization.KeySerializationEncryption,
+ key,
+ evp_pkey,
+ cdata,
+ ) -> bytes:
+ # validate argument types
+ if not isinstance(encoding, serialization.Encoding):
+ raise TypeError("encoding must be an item from the Encoding enum")
+ if not isinstance(format, serialization.PrivateFormat):
+ raise TypeError(
+ "format must be an item from the PrivateFormat enum"
+ )
+ if not isinstance(
+ encryption_algorithm, serialization.KeySerializationEncryption
+ ):
+ raise TypeError(
+ "Encryption algorithm must be a KeySerializationEncryption "
+ "instance"
+ )
+
+ # validate password
+ if isinstance(encryption_algorithm, serialization.NoEncryption):
+ password = b""
+ elif isinstance(
+ encryption_algorithm, serialization.BestAvailableEncryption
+ ):
+ password = encryption_algorithm.password
+ if len(password) > 1023:
+ raise ValueError(
+ "Passwords longer than 1023 bytes are not supported by "
+ "this backend"
+ )
+ elif (
+ isinstance(
+ encryption_algorithm, serialization._KeySerializationEncryption
+ )
+ and encryption_algorithm._format
+ is format
+ is serialization.PrivateFormat.OpenSSH
+ ):
+ password = encryption_algorithm.password
+ else:
+ raise ValueError("Unsupported encryption type")
+
+ # PKCS8 + PEM/DER
+ if format is serialization.PrivateFormat.PKCS8:
+ if encoding is serialization.Encoding.PEM:
+ write_bio = self._lib.PEM_write_bio_PKCS8PrivateKey
+ elif encoding is serialization.Encoding.DER:
+ write_bio = self._lib.i2d_PKCS8PrivateKey_bio
+ else:
+ raise ValueError("Unsupported encoding for PKCS8")
+ return self._private_key_bytes_via_bio(
+ write_bio, evp_pkey, password
+ )
+
+ # TraditionalOpenSSL + PEM/DER
+ if format is serialization.PrivateFormat.TraditionalOpenSSL:
+ if self._fips_enabled and not isinstance(
+ encryption_algorithm, serialization.NoEncryption
+ ):
+ raise ValueError(
+ "Encrypted traditional OpenSSL format is not "
+ "supported in FIPS mode."
+ )
+ key_type = self._lib.EVP_PKEY_id(evp_pkey)
+
+ if encoding is serialization.Encoding.PEM:
+ if key_type == self._lib.EVP_PKEY_RSA:
+ write_bio = self._lib.PEM_write_bio_RSAPrivateKey
+ else:
+ assert key_type == self._lib.EVP_PKEY_EC
+ write_bio = self._lib.PEM_write_bio_ECPrivateKey
+ return self._private_key_bytes_via_bio(
+ write_bio, cdata, password
+ )
+
+ if encoding is serialization.Encoding.DER:
+ if password:
+ raise ValueError(
+ "Encryption is not supported for DER encoded "
+ "traditional OpenSSL keys"
+ )
+ if key_type == self._lib.EVP_PKEY_RSA:
+ write_bio = self._lib.i2d_RSAPrivateKey_bio
+ else:
+ assert key_type == self._lib.EVP_PKEY_EC
+ write_bio = self._lib.i2d_ECPrivateKey_bio
+ return self._bio_func_output(write_bio, cdata)
+
+ raise ValueError("Unsupported encoding for TraditionalOpenSSL")
+
+ # OpenSSH + PEM
+ if format is serialization.PrivateFormat.OpenSSH:
+ if encoding is serialization.Encoding.PEM:
+ return ssh._serialize_ssh_private_key(
+ key, password, encryption_algorithm
+ )
+
+ raise ValueError(
+ "OpenSSH private key format can only be used"
+ " with PEM encoding"
+ )
+
+ # Anything that key-specific code was supposed to handle earlier,
+ # like Raw.
+ raise ValueError("format is invalid with this key")
+
+ def _private_key_bytes_via_bio(
+ self, write_bio, evp_pkey, password
+ ) -> bytes:
+ if not password:
+ evp_cipher = self._ffi.NULL
+ else:
+ # This is a curated value that we will update over time.
+ evp_cipher = self._lib.EVP_get_cipherbyname(b"aes-256-cbc")
+
+ return self._bio_func_output(
+ write_bio,
+ evp_pkey,
+ evp_cipher,
+ password,
+ len(password),
+ self._ffi.NULL,
+ self._ffi.NULL,
+ )
+
+ def _bio_func_output(self, write_bio, *args) -> bytes:
+ bio = self._create_mem_bio_gc()
+ res = write_bio(bio, *args)
+ self.openssl_assert(res == 1)
+ return self._read_mem_bio(bio)
+
+ def _public_key_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PublicFormat,
+ key,
+ evp_pkey,
+ cdata,
+ ) -> bytes:
+ if not isinstance(encoding, serialization.Encoding):
+ raise TypeError("encoding must be an item from the Encoding enum")
+ if not isinstance(format, serialization.PublicFormat):
+ raise TypeError(
+ "format must be an item from the PublicFormat enum"
+ )
+
+ # SubjectPublicKeyInfo + PEM/DER
+ if format is serialization.PublicFormat.SubjectPublicKeyInfo:
+ if encoding is serialization.Encoding.PEM:
+ write_bio = self._lib.PEM_write_bio_PUBKEY
+ elif encoding is serialization.Encoding.DER:
+ write_bio = self._lib.i2d_PUBKEY_bio
+ else:
+ raise ValueError(
+ "SubjectPublicKeyInfo works only with PEM or DER encoding"
+ )
+ return self._bio_func_output(write_bio, evp_pkey)
+
+ # PKCS1 + PEM/DER
+ if format is serialization.PublicFormat.PKCS1:
+ # Only RSA is supported here.
+ key_type = self._lib.EVP_PKEY_id(evp_pkey)
+ if key_type != self._lib.EVP_PKEY_RSA:
+ raise ValueError("PKCS1 format is supported only for RSA keys")
+
+ if encoding is serialization.Encoding.PEM:
+ write_bio = self._lib.PEM_write_bio_RSAPublicKey
+ elif encoding is serialization.Encoding.DER:
+ write_bio = self._lib.i2d_RSAPublicKey_bio
+ else:
+ raise ValueError("PKCS1 works only with PEM or DER encoding")
+ return self._bio_func_output(write_bio, cdata)
+
+ # OpenSSH + OpenSSH
+ if format is serialization.PublicFormat.OpenSSH:
+ if encoding is serialization.Encoding.OpenSSH:
+ return ssh.serialize_ssh_public_key(key)
+
+ raise ValueError(
+ "OpenSSH format must be used with OpenSSH encoding"
+ )
+
+ # Anything that key-specific code was supposed to handle earlier,
+ # like Raw, CompressedPoint, UncompressedPoint
+ raise ValueError("format is invalid with this key")
+
+ def dh_supported(self) -> bool:
+ return not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+
+ def generate_dh_parameters(
+ self, generator: int, key_size: int
+ ) -> dh.DHParameters:
+ return rust_openssl.dh.generate_parameters(generator, key_size)
+
+ def generate_dh_private_key(
+ self, parameters: dh.DHParameters
+ ) -> dh.DHPrivateKey:
+ return parameters.generate_private_key()
+
+ def generate_dh_private_key_and_parameters(
+ self, generator: int, key_size: int
+ ) -> dh.DHPrivateKey:
+ return self.generate_dh_private_key(
+ self.generate_dh_parameters(generator, key_size)
+ )
+
+ def load_dh_private_numbers(
+ self, numbers: dh.DHPrivateNumbers
+ ) -> dh.DHPrivateKey:
+ return rust_openssl.dh.from_private_numbers(numbers)
+
+ def load_dh_public_numbers(
+ self, numbers: dh.DHPublicNumbers
+ ) -> dh.DHPublicKey:
+ return rust_openssl.dh.from_public_numbers(numbers)
+
+ def load_dh_parameter_numbers(
+ self, numbers: dh.DHParameterNumbers
+ ) -> dh.DHParameters:
+ return rust_openssl.dh.from_parameter_numbers(numbers)
+
+ def dh_parameters_supported(
+ self, p: int, g: int, q: typing.Optional[int] = None
+ ) -> bool:
+ try:
+ rust_openssl.dh.from_parameter_numbers(
+ dh.DHParameterNumbers(p=p, g=g, q=q)
+ )
+ except ValueError:
+ return False
+ else:
+ return True
+
+ def dh_x942_serialization_supported(self) -> bool:
+ return self._lib.Cryptography_HAS_EVP_PKEY_DHX == 1
+
+ def x25519_load_public_bytes(self, data: bytes) -> x25519.X25519PublicKey:
+ return rust_openssl.x25519.from_public_bytes(data)
+
+ def x25519_load_private_bytes(
+ self, data: bytes
+ ) -> x25519.X25519PrivateKey:
+ return rust_openssl.x25519.from_private_bytes(data)
+
+ def x25519_generate_key(self) -> x25519.X25519PrivateKey:
+ return rust_openssl.x25519.generate_key()
+
+ def x25519_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ return not self._lib.CRYPTOGRAPHY_LIBRESSL_LESS_THAN_370
+
+ def x448_load_public_bytes(self, data: bytes) -> x448.X448PublicKey:
+ return rust_openssl.x448.from_public_bytes(data)
+
+ def x448_load_private_bytes(self, data: bytes) -> x448.X448PrivateKey:
+ return rust_openssl.x448.from_private_bytes(data)
+
+ def x448_generate_key(self) -> x448.X448PrivateKey:
+ return rust_openssl.x448.generate_key()
+
+ def x448_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ return (
+ not self._lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+ )
+
+ def ed25519_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ return self._lib.CRYPTOGRAPHY_HAS_WORKING_ED25519
+
+ def ed25519_load_public_bytes(
+ self, data: bytes
+ ) -> ed25519.Ed25519PublicKey:
+ return rust_openssl.ed25519.from_public_bytes(data)
+
+ def ed25519_load_private_bytes(
+ self, data: bytes
+ ) -> ed25519.Ed25519PrivateKey:
+ return rust_openssl.ed25519.from_private_bytes(data)
+
+ def ed25519_generate_key(self) -> ed25519.Ed25519PrivateKey:
+ return rust_openssl.ed25519.generate_key()
+
+ def ed448_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ return (
+ not self._lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+ )
+
+ def ed448_load_public_bytes(self, data: bytes) -> ed448.Ed448PublicKey:
+ return rust_openssl.ed448.from_public_bytes(data)
+
+ def ed448_load_private_bytes(self, data: bytes) -> ed448.Ed448PrivateKey:
+ return rust_openssl.ed448.from_private_bytes(data)
+
+ def ed448_generate_key(self) -> ed448.Ed448PrivateKey:
+ return rust_openssl.ed448.generate_key()
+
+ def aead_cipher_supported(self, cipher) -> bool:
+ return aead._aead_cipher_supported(self, cipher)
+
+ def _zero_data(self, data, length: int) -> None:
+ # We clear things this way because at the moment we're not
+ # sure of a better way that can guarantee it overwrites the
+ # memory of a bytearray and doesn't just replace the underlying char *.
+ for i in range(length):
+ data[i] = 0
+
+ @contextlib.contextmanager
+ def _zeroed_null_terminated_buf(self, data):
+ """
+ This method takes bytes, which can be a bytestring or a mutable
+ buffer like a bytearray, and yields a null-terminated version of that
+ data. This is required because PKCS12_parse doesn't take a length with
+ its password char * and ffi.from_buffer doesn't provide null
+ termination. So, to support zeroing the data via bytearray we
+ need to build this ridiculous construct that copies the memory, but
+ zeroes it after use.
+ """
+ if data is None:
+ yield self._ffi.NULL
+ else:
+ data_len = len(data)
+ buf = self._ffi.new("char[]", data_len + 1)
+ self._ffi.memmove(buf, data, data_len)
+ try:
+ yield buf
+ finally:
+ # Cast to a uint8_t * so we can assign by integer
+ self._zero_data(self._ffi.cast("uint8_t *", buf), data_len)
+
+ def load_key_and_certificates_from_pkcs12(
+ self, data: bytes, password: typing.Optional[bytes]
+ ) -> typing.Tuple[
+ typing.Optional[PrivateKeyTypes],
+ typing.Optional[x509.Certificate],
+ typing.List[x509.Certificate],
+ ]:
+ pkcs12 = self.load_pkcs12(data, password)
+ return (
+ pkcs12.key,
+ pkcs12.cert.certificate if pkcs12.cert else None,
+ [cert.certificate for cert in pkcs12.additional_certs],
+ )
+
+ def load_pkcs12(
+ self, data: bytes, password: typing.Optional[bytes]
+ ) -> PKCS12KeyAndCertificates:
+ if password is not None:
+ utils._check_byteslike("password", password)
+
+ bio = self._bytes_to_bio(data)
+ p12 = self._lib.d2i_PKCS12_bio(bio.bio, self._ffi.NULL)
+ if p12 == self._ffi.NULL:
+ self._consume_errors()
+ raise ValueError("Could not deserialize PKCS12 data")
+
+ p12 = self._ffi.gc(p12, self._lib.PKCS12_free)
+ evp_pkey_ptr = self._ffi.new("EVP_PKEY **")
+ x509_ptr = self._ffi.new("X509 **")
+ sk_x509_ptr = self._ffi.new("Cryptography_STACK_OF_X509 **")
+ with self._zeroed_null_terminated_buf(password) as password_buf:
+ res = self._lib.PKCS12_parse(
+ p12, password_buf, evp_pkey_ptr, x509_ptr, sk_x509_ptr
+ )
+ if res == 0:
+ self._consume_errors()
+ raise ValueError("Invalid password or PKCS12 data")
+
+ cert = None
+ key = None
+ additional_certificates = []
+
+ if evp_pkey_ptr[0] != self._ffi.NULL:
+ evp_pkey = self._ffi.gc(evp_pkey_ptr[0], self._lib.EVP_PKEY_free)
+ # We don't support turning off RSA key validation when loading
+ # PKCS12 keys
+ key = self._evp_pkey_to_private_key(
+ evp_pkey, unsafe_skip_rsa_key_validation=False
+ )
+
+ if x509_ptr[0] != self._ffi.NULL:
+ x509 = self._ffi.gc(x509_ptr[0], self._lib.X509_free)
+ cert_obj = self._ossl2cert(x509)
+ name = None
+ maybe_name = self._lib.X509_alias_get0(x509, self._ffi.NULL)
+ if maybe_name != self._ffi.NULL:
+ name = self._ffi.string(maybe_name)
+ cert = PKCS12Certificate(cert_obj, name)
+
+ if sk_x509_ptr[0] != self._ffi.NULL:
+ sk_x509 = self._ffi.gc(sk_x509_ptr[0], self._lib.sk_X509_free)
+ num = self._lib.sk_X509_num(sk_x509_ptr[0])
+
+ # In OpenSSL < 3.0.0 PKCS12 parsing reverses the order of the
+ # certificates.
+ indices: typing.Iterable[int]
+ if (
+ self._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER
+ or self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+ ):
+ indices = range(num)
+ else:
+ indices = reversed(range(num))
+
+ for i in indices:
+ x509 = self._lib.sk_X509_value(sk_x509, i)
+ self.openssl_assert(x509 != self._ffi.NULL)
+ x509 = self._ffi.gc(x509, self._lib.X509_free)
+ addl_cert = self._ossl2cert(x509)
+ addl_name = None
+ maybe_name = self._lib.X509_alias_get0(x509, self._ffi.NULL)
+ if maybe_name != self._ffi.NULL:
+ addl_name = self._ffi.string(maybe_name)
+ additional_certificates.append(
+ PKCS12Certificate(addl_cert, addl_name)
+ )
+
+ return PKCS12KeyAndCertificates(key, cert, additional_certificates)
+
+ def serialize_key_and_certificates_to_pkcs12(
+ self,
+ name: typing.Optional[bytes],
+ key: typing.Optional[PKCS12PrivateKeyTypes],
+ cert: typing.Optional[x509.Certificate],
+ cas: typing.Optional[typing.List[_PKCS12CATypes]],
+ encryption_algorithm: serialization.KeySerializationEncryption,
+ ) -> bytes:
+ password = None
+ if name is not None:
+ utils._check_bytes("name", name)
+
+ if isinstance(encryption_algorithm, serialization.NoEncryption):
+ nid_cert = -1
+ nid_key = -1
+ pkcs12_iter = 0
+ mac_iter = 0
+ mac_alg = self._ffi.NULL
+ elif isinstance(
+ encryption_algorithm, serialization.BestAvailableEncryption
+ ):
+ # PKCS12 encryption is hopeless trash and can never be fixed.
+ # OpenSSL 3 supports PBESv2, but Libre and Boring do not, so
+ # we use PBESv1 with 3DES on the older paths.
+ if self._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER:
+ nid_cert = self._lib.NID_aes_256_cbc
+ nid_key = self._lib.NID_aes_256_cbc
+ else:
+ nid_cert = self._lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC
+ nid_key = self._lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC
+ # At least we can set this higher than OpenSSL's default
+ pkcs12_iter = 20000
+ # mac_iter chosen for compatibility reasons, see:
+ # https://www.openssl.org/docs/man1.1.1/man3/PKCS12_create.html
+ # Did we mention how lousy PKCS12 encryption is?
+ mac_iter = 1
+ # MAC algorithm can only be set on OpenSSL 3.0.0+
+ mac_alg = self._ffi.NULL
+ password = encryption_algorithm.password
+ elif (
+ isinstance(
+ encryption_algorithm, serialization._KeySerializationEncryption
+ )
+ and encryption_algorithm._format
+ is serialization.PrivateFormat.PKCS12
+ ):
+ # Default to OpenSSL's defaults. Behavior will vary based on the
+ # version of OpenSSL cryptography is compiled against.
+ nid_cert = 0
+ nid_key = 0
+ # Use the default iters we use in best available
+ pkcs12_iter = 20000
+ # See the Best Available comment for why this is 1
+ mac_iter = 1
+ password = encryption_algorithm.password
+ keycertalg = encryption_algorithm._key_cert_algorithm
+ if keycertalg is PBES.PBESv1SHA1And3KeyTripleDESCBC:
+ nid_cert = self._lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC
+ nid_key = self._lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC
+ elif keycertalg is PBES.PBESv2SHA256AndAES256CBC:
+ if not self._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER:
+ raise UnsupportedAlgorithm(
+ "PBESv2 is not supported by this version of OpenSSL"
+ )
+ nid_cert = self._lib.NID_aes_256_cbc
+ nid_key = self._lib.NID_aes_256_cbc
+ else:
+ assert keycertalg is None
+ # We use OpenSSL's defaults
+
+ if encryption_algorithm._hmac_hash is not None:
+ if not self._lib.Cryptography_HAS_PKCS12_SET_MAC:
+ raise UnsupportedAlgorithm(
+ "Setting MAC algorithm is not supported by this "
+ "version of OpenSSL."
+ )
+ mac_alg = self._evp_md_non_null_from_algorithm(
+ encryption_algorithm._hmac_hash
+ )
+ self.openssl_assert(mac_alg != self._ffi.NULL)
+ else:
+ mac_alg = self._ffi.NULL
+
+ if encryption_algorithm._kdf_rounds is not None:
+ pkcs12_iter = encryption_algorithm._kdf_rounds
+
+ else:
+ raise ValueError("Unsupported key encryption type")
+
+ if cas is None or len(cas) == 0:
+ sk_x509 = self._ffi.NULL
+ else:
+ sk_x509 = self._lib.sk_X509_new_null()
+ sk_x509 = self._ffi.gc(sk_x509, self._lib.sk_X509_free)
+
+ # This list is to keep the x509 values alive until end of function
+ ossl_cas = []
+ for ca in cas:
+ if isinstance(ca, PKCS12Certificate):
+ ca_alias = ca.friendly_name
+ ossl_ca = self._cert2ossl(ca.certificate)
+ if ca_alias is None:
+ res = self._lib.X509_alias_set1(
+ ossl_ca, self._ffi.NULL, -1
+ )
+ else:
+ res = self._lib.X509_alias_set1(
+ ossl_ca, ca_alias, len(ca_alias)
+ )
+ self.openssl_assert(res == 1)
+ else:
+ ossl_ca = self._cert2ossl(ca)
+ ossl_cas.append(ossl_ca)
+ res = self._lib.sk_X509_push(sk_x509, ossl_ca)
+ backend.openssl_assert(res >= 1)
+
+ with self._zeroed_null_terminated_buf(password) as password_buf:
+ with self._zeroed_null_terminated_buf(name) as name_buf:
+ ossl_cert = self._cert2ossl(cert) if cert else self._ffi.NULL
+ ossl_pkey = (
+ self._key2ossl(key) if key is not None else self._ffi.NULL
+ )
+
+ p12 = self._lib.PKCS12_create(
+ password_buf,
+ name_buf,
+ ossl_pkey,
+ ossl_cert,
+ sk_x509,
+ nid_key,
+ nid_cert,
+ pkcs12_iter,
+ mac_iter,
+ 0,
+ )
+
+ if (
+ self._lib.Cryptography_HAS_PKCS12_SET_MAC
+ and mac_alg != self._ffi.NULL
+ ):
+ self._lib.PKCS12_set_mac(
+ p12,
+ password_buf,
+ -1,
+ self._ffi.NULL,
+ 0,
+ mac_iter,
+ mac_alg,
+ )
+
+ self.openssl_assert(p12 != self._ffi.NULL)
+ p12 = self._ffi.gc(p12, self._lib.PKCS12_free)
+
+ bio = self._create_mem_bio_gc()
+ res = self._lib.i2d_PKCS12_bio(bio, p12)
+ self.openssl_assert(res > 0)
+ return self._read_mem_bio(bio)
+
+ def poly1305_supported(self) -> bool:
+ if self._fips_enabled:
+ return False
+ return self._lib.Cryptography_HAS_POLY1305 == 1
+
+ def pkcs7_supported(self) -> bool:
+ return not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
+
+ def load_pem_pkcs7_certificates(
+ self, data: bytes
+ ) -> typing.List[x509.Certificate]:
+ utils._check_bytes("data", data)
+ bio = self._bytes_to_bio(data)
+ p7 = self._lib.PEM_read_bio_PKCS7(
+ bio.bio, self._ffi.NULL, self._ffi.NULL, self._ffi.NULL
+ )
+ if p7 == self._ffi.NULL:
+ self._consume_errors()
+ raise ValueError("Unable to parse PKCS7 data")
+
+ p7 = self._ffi.gc(p7, self._lib.PKCS7_free)
+ return self._load_pkcs7_certificates(p7)
+
+ def load_der_pkcs7_certificates(
+ self, data: bytes
+ ) -> typing.List[x509.Certificate]:
+ utils._check_bytes("data", data)
+ bio = self._bytes_to_bio(data)
+ p7 = self._lib.d2i_PKCS7_bio(bio.bio, self._ffi.NULL)
+ if p7 == self._ffi.NULL:
+ self._consume_errors()
+ raise ValueError("Unable to parse PKCS7 data")
+
+ p7 = self._ffi.gc(p7, self._lib.PKCS7_free)
+ return self._load_pkcs7_certificates(p7)
+
+ def _load_pkcs7_certificates(self, p7) -> typing.List[x509.Certificate]:
+ nid = self._lib.OBJ_obj2nid(p7.type)
+ self.openssl_assert(nid != self._lib.NID_undef)
+ if nid != self._lib.NID_pkcs7_signed:
+ raise UnsupportedAlgorithm(
+ "Only basic signed structures are currently supported. NID"
+ " for this data was {}".format(nid),
+ _Reasons.UNSUPPORTED_SERIALIZATION,
+ )
+
+ certs: list[x509.Certificate] = []
+ if p7.d.sign == self._ffi.NULL:
+ return certs
+
+ sk_x509 = p7.d.sign.cert
+ num = self._lib.sk_X509_num(sk_x509)
+ for i in range(num):
+ x509 = self._lib.sk_X509_value(sk_x509, i)
+ self.openssl_assert(x509 != self._ffi.NULL)
+ cert = self._ossl2cert(x509)
+ certs.append(cert)
+
+ return certs
+
+
+class GetCipherByName:
+ def __init__(self, fmt: str):
+ self._fmt = fmt
+
+ def __call__(self, backend: Backend, cipher: CipherAlgorithm, mode: Mode):
+ cipher_name = self._fmt.format(cipher=cipher, mode=mode).lower()
+ evp_cipher = backend._lib.EVP_get_cipherbyname(
+ cipher_name.encode("ascii")
+ )
+
+ # try EVP_CIPHER_fetch if present
+ if (
+ evp_cipher == backend._ffi.NULL
+ and backend._lib.Cryptography_HAS_300_EVP_CIPHER
+ ):
+ evp_cipher = backend._lib.EVP_CIPHER_fetch(
+ backend._ffi.NULL,
+ cipher_name.encode("ascii"),
+ backend._ffi.NULL,
+ )
+
+ backend._consume_errors()
+ return evp_cipher
+
+
+def _get_xts_cipher(backend: Backend, cipher: AES, mode):
+ cipher_name = f"aes-{cipher.key_size // 2}-xts"
+ return backend._lib.EVP_get_cipherbyname(cipher_name.encode("ascii"))
+
+
+backend = Backend()
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ciphers.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ciphers.py
new file mode 100644
index 0000000000..bc42adbd49
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ciphers.py
@@ -0,0 +1,281 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.exceptions import InvalidTag, UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.primitives import ciphers
+from cryptography.hazmat.primitives.ciphers import algorithms, modes
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+
+
+class _CipherContext:
+ _ENCRYPT = 1
+ _DECRYPT = 0
+ _MAX_CHUNK_SIZE = 2**30 - 1
+
+ def __init__(self, backend: Backend, cipher, mode, operation: int) -> None:
+ self._backend = backend
+ self._cipher = cipher
+ self._mode = mode
+ self._operation = operation
+ self._tag: typing.Optional[bytes] = None
+
+ if isinstance(self._cipher, ciphers.BlockCipherAlgorithm):
+ self._block_size_bytes = self._cipher.block_size // 8
+ else:
+ self._block_size_bytes = 1
+
+ ctx = self._backend._lib.EVP_CIPHER_CTX_new()
+ ctx = self._backend._ffi.gc(
+ ctx, self._backend._lib.EVP_CIPHER_CTX_free
+ )
+
+ registry = self._backend._cipher_registry
+ try:
+ adapter = registry[type(cipher), type(mode)]
+ except KeyError:
+ raise UnsupportedAlgorithm(
+ "cipher {} in {} mode is not supported "
+ "by this backend.".format(
+ cipher.name, mode.name if mode else mode
+ ),
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ evp_cipher = adapter(self._backend, cipher, mode)
+ if evp_cipher == self._backend._ffi.NULL:
+ msg = f"cipher {cipher.name} "
+ if mode is not None:
+ msg += f"in {mode.name} mode "
+ msg += (
+ "is not supported by this backend (Your version of OpenSSL "
+ "may be too old. Current version: {}.)"
+ ).format(self._backend.openssl_version_text())
+ raise UnsupportedAlgorithm(msg, _Reasons.UNSUPPORTED_CIPHER)
+
+ if isinstance(mode, modes.ModeWithInitializationVector):
+ iv_nonce = self._backend._ffi.from_buffer(
+ mode.initialization_vector
+ )
+ elif isinstance(mode, modes.ModeWithTweak):
+ iv_nonce = self._backend._ffi.from_buffer(mode.tweak)
+ elif isinstance(mode, modes.ModeWithNonce):
+ iv_nonce = self._backend._ffi.from_buffer(mode.nonce)
+ elif isinstance(cipher, algorithms.ChaCha20):
+ iv_nonce = self._backend._ffi.from_buffer(cipher.nonce)
+ else:
+ iv_nonce = self._backend._ffi.NULL
+ # begin init with cipher and operation type
+ res = self._backend._lib.EVP_CipherInit_ex(
+ ctx,
+ evp_cipher,
+ self._backend._ffi.NULL,
+ self._backend._ffi.NULL,
+ self._backend._ffi.NULL,
+ operation,
+ )
+ self._backend.openssl_assert(res != 0)
+ # set the key length to handle variable key ciphers
+ res = self._backend._lib.EVP_CIPHER_CTX_set_key_length(
+ ctx, len(cipher.key)
+ )
+ self._backend.openssl_assert(res != 0)
+ if isinstance(mode, modes.GCM):
+ res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx,
+ self._backend._lib.EVP_CTRL_AEAD_SET_IVLEN,
+ len(iv_nonce),
+ self._backend._ffi.NULL,
+ )
+ self._backend.openssl_assert(res != 0)
+ if mode.tag is not None:
+ res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx,
+ self._backend._lib.EVP_CTRL_AEAD_SET_TAG,
+ len(mode.tag),
+ mode.tag,
+ )
+ self._backend.openssl_assert(res != 0)
+ self._tag = mode.tag
+
+ # pass key/iv
+ res = self._backend._lib.EVP_CipherInit_ex(
+ ctx,
+ self._backend._ffi.NULL,
+ self._backend._ffi.NULL,
+ self._backend._ffi.from_buffer(cipher.key),
+ iv_nonce,
+ operation,
+ )
+
+ # Check for XTS mode duplicate keys error
+ errors = self._backend._consume_errors()
+ lib = self._backend._lib
+ if res == 0 and (
+ (
+ not lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and errors[0]._lib_reason_match(
+ lib.ERR_LIB_EVP, lib.EVP_R_XTS_DUPLICATED_KEYS
+ )
+ )
+ or (
+ lib.Cryptography_HAS_PROVIDERS
+ and errors[0]._lib_reason_match(
+ lib.ERR_LIB_PROV, lib.PROV_R_XTS_DUPLICATED_KEYS
+ )
+ )
+ ):
+ raise ValueError("In XTS mode duplicated keys are not allowed")
+
+ self._backend.openssl_assert(res != 0, errors=errors)
+
+ # We purposely disable padding here as it's handled higher up in the
+ # API.
+ self._backend._lib.EVP_CIPHER_CTX_set_padding(ctx, 0)
+ self._ctx = ctx
+
+ def update(self, data: bytes) -> bytes:
+ buf = bytearray(len(data) + self._block_size_bytes - 1)
+ n = self.update_into(data, buf)
+ return bytes(buf[:n])
+
+ def update_into(self, data: bytes, buf: bytes) -> int:
+ total_data_len = len(data)
+ if len(buf) < (total_data_len + self._block_size_bytes - 1):
+ raise ValueError(
+ "buffer must be at least {} bytes for this "
+ "payload".format(len(data) + self._block_size_bytes - 1)
+ )
+
+ data_processed = 0
+ total_out = 0
+ outlen = self._backend._ffi.new("int *")
+ baseoutbuf = self._backend._ffi.from_buffer(buf, require_writable=True)
+ baseinbuf = self._backend._ffi.from_buffer(data)
+
+ while data_processed != total_data_len:
+ outbuf = baseoutbuf + total_out
+ inbuf = baseinbuf + data_processed
+ inlen = min(self._MAX_CHUNK_SIZE, total_data_len - data_processed)
+
+ res = self._backend._lib.EVP_CipherUpdate(
+ self._ctx, outbuf, outlen, inbuf, inlen
+ )
+ if res == 0 and isinstance(self._mode, modes.XTS):
+ self._backend._consume_errors()
+ raise ValueError(
+ "In XTS mode you must supply at least a full block in the "
+ "first update call. For AES this is 16 bytes."
+ )
+ else:
+ self._backend.openssl_assert(res != 0)
+ data_processed += inlen
+ total_out += outlen[0]
+
+ return total_out
+
+ def finalize(self) -> bytes:
+ if (
+ self._operation == self._DECRYPT
+ and isinstance(self._mode, modes.ModeWithAuthenticationTag)
+ and self.tag is None
+ ):
+ raise ValueError(
+ "Authentication tag must be provided when decrypting."
+ )
+
+ buf = self._backend._ffi.new("unsigned char[]", self._block_size_bytes)
+ outlen = self._backend._ffi.new("int *")
+ res = self._backend._lib.EVP_CipherFinal_ex(self._ctx, buf, outlen)
+ if res == 0:
+ errors = self._backend._consume_errors()
+
+ if not errors and isinstance(self._mode, modes.GCM):
+ raise InvalidTag
+
+ lib = self._backend._lib
+ self._backend.openssl_assert(
+ errors[0]._lib_reason_match(
+ lib.ERR_LIB_EVP,
+ lib.EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH,
+ )
+ or (
+ lib.Cryptography_HAS_PROVIDERS
+ and errors[0]._lib_reason_match(
+ lib.ERR_LIB_PROV,
+ lib.PROV_R_WRONG_FINAL_BLOCK_LENGTH,
+ )
+ )
+ or (
+ lib.CRYPTOGRAPHY_IS_BORINGSSL
+ and errors[0].reason
+ == lib.CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
+ ),
+ errors=errors,
+ )
+ raise ValueError(
+ "The length of the provided data is not a multiple of "
+ "the block length."
+ )
+
+ if (
+ isinstance(self._mode, modes.GCM)
+ and self._operation == self._ENCRYPT
+ ):
+ tag_buf = self._backend._ffi.new(
+ "unsigned char[]", self._block_size_bytes
+ )
+ res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
+ self._ctx,
+ self._backend._lib.EVP_CTRL_AEAD_GET_TAG,
+ self._block_size_bytes,
+ tag_buf,
+ )
+ self._backend.openssl_assert(res != 0)
+ self._tag = self._backend._ffi.buffer(tag_buf)[:]
+
+ res = self._backend._lib.EVP_CIPHER_CTX_reset(self._ctx)
+ self._backend.openssl_assert(res == 1)
+ return self._backend._ffi.buffer(buf)[: outlen[0]]
+
+ def finalize_with_tag(self, tag: bytes) -> bytes:
+ tag_len = len(tag)
+ if tag_len < self._mode._min_tag_length:
+ raise ValueError(
+ "Authentication tag must be {} bytes or longer.".format(
+ self._mode._min_tag_length
+ )
+ )
+ elif tag_len > self._block_size_bytes:
+ raise ValueError(
+ "Authentication tag cannot be more than {} bytes.".format(
+ self._block_size_bytes
+ )
+ )
+ res = self._backend._lib.EVP_CIPHER_CTX_ctrl(
+ self._ctx, self._backend._lib.EVP_CTRL_AEAD_SET_TAG, len(tag), tag
+ )
+ self._backend.openssl_assert(res != 0)
+ self._tag = tag
+ return self.finalize()
+
+ def authenticate_additional_data(self, data: bytes) -> None:
+ outlen = self._backend._ffi.new("int *")
+ res = self._backend._lib.EVP_CipherUpdate(
+ self._ctx,
+ self._backend._ffi.NULL,
+ outlen,
+ self._backend._ffi.from_buffer(data),
+ len(data),
+ )
+ self._backend.openssl_assert(res != 0)
+
+ @property
+ def tag(self) -> typing.Optional[bytes]:
+ return self._tag
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/cmac.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/cmac.py
new file mode 100644
index 0000000000..bdd7fec611
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/cmac.py
@@ -0,0 +1,89 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.exceptions import (
+ InvalidSignature,
+ UnsupportedAlgorithm,
+ _Reasons,
+)
+from cryptography.hazmat.primitives import constant_time
+from cryptography.hazmat.primitives.ciphers.modes import CBC
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+ from cryptography.hazmat.primitives import ciphers
+
+
+class _CMACContext:
+ def __init__(
+ self,
+ backend: Backend,
+ algorithm: ciphers.BlockCipherAlgorithm,
+ ctx=None,
+ ) -> None:
+ if not backend.cmac_algorithm_supported(algorithm):
+ raise UnsupportedAlgorithm(
+ "This backend does not support CMAC.",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ self._backend = backend
+ self._key = algorithm.key
+ self._algorithm = algorithm
+ self._output_length = algorithm.block_size // 8
+
+ if ctx is None:
+ registry = self._backend._cipher_registry
+ adapter = registry[type(algorithm), CBC]
+
+ evp_cipher = adapter(self._backend, algorithm, CBC)
+
+ ctx = self._backend._lib.CMAC_CTX_new()
+
+ self._backend.openssl_assert(ctx != self._backend._ffi.NULL)
+ ctx = self._backend._ffi.gc(ctx, self._backend._lib.CMAC_CTX_free)
+
+ key_ptr = self._backend._ffi.from_buffer(self._key)
+ res = self._backend._lib.CMAC_Init(
+ ctx,
+ key_ptr,
+ len(self._key),
+ evp_cipher,
+ self._backend._ffi.NULL,
+ )
+ self._backend.openssl_assert(res == 1)
+
+ self._ctx = ctx
+
+ def update(self, data: bytes) -> None:
+ res = self._backend._lib.CMAC_Update(self._ctx, data, len(data))
+ self._backend.openssl_assert(res == 1)
+
+ def finalize(self) -> bytes:
+ buf = self._backend._ffi.new("unsigned char[]", self._output_length)
+ length = self._backend._ffi.new("size_t *", self._output_length)
+ res = self._backend._lib.CMAC_Final(self._ctx, buf, length)
+ self._backend.openssl_assert(res == 1)
+
+ self._ctx = None
+
+ return self._backend._ffi.buffer(buf)[:]
+
+ def copy(self) -> _CMACContext:
+ copied_ctx = self._backend._lib.CMAC_CTX_new()
+ copied_ctx = self._backend._ffi.gc(
+ copied_ctx, self._backend._lib.CMAC_CTX_free
+ )
+ res = self._backend._lib.CMAC_CTX_copy(copied_ctx, self._ctx)
+ self._backend.openssl_assert(res == 1)
+ return _CMACContext(self._backend, self._algorithm, ctx=copied_ctx)
+
+ def verify(self, signature: bytes) -> None:
+ digest = self.finalize()
+ if not constant_time.bytes_eq(digest, signature):
+ raise InvalidSignature("Signature did not match digest.")
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/decode_asn1.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/decode_asn1.py
new file mode 100644
index 0000000000..bf123b6285
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/decode_asn1.py
@@ -0,0 +1,32 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography import x509
+
+# CRLReason ::= ENUMERATED {
+# unspecified (0),
+# keyCompromise (1),
+# cACompromise (2),
+# affiliationChanged (3),
+# superseded (4),
+# cessationOfOperation (5),
+# certificateHold (6),
+# -- value 7 is not used
+# removeFromCRL (8),
+# privilegeWithdrawn (9),
+# aACompromise (10) }
+_CRL_ENTRY_REASON_ENUM_TO_CODE = {
+ x509.ReasonFlags.unspecified: 0,
+ x509.ReasonFlags.key_compromise: 1,
+ x509.ReasonFlags.ca_compromise: 2,
+ x509.ReasonFlags.affiliation_changed: 3,
+ x509.ReasonFlags.superseded: 4,
+ x509.ReasonFlags.cessation_of_operation: 5,
+ x509.ReasonFlags.certificate_hold: 6,
+ x509.ReasonFlags.remove_from_crl: 8,
+ x509.ReasonFlags.privilege_withdrawn: 9,
+ x509.ReasonFlags.aa_compromise: 10,
+}
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ec.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ec.py
new file mode 100644
index 0000000000..9821bd193e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/ec.py
@@ -0,0 +1,328 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.exceptions import (
+ InvalidSignature,
+ UnsupportedAlgorithm,
+ _Reasons,
+)
+from cryptography.hazmat.backends.openssl.utils import (
+ _calculate_digest_and_algorithm,
+ _evp_pkey_derive,
+)
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives.asymmetric import ec
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+
+
+def _check_signature_algorithm(
+ signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
+) -> None:
+ if not isinstance(signature_algorithm, ec.ECDSA):
+ raise UnsupportedAlgorithm(
+ "Unsupported elliptic curve signature algorithm.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+
+def _ec_key_curve_sn(backend: Backend, ec_key) -> str:
+ group = backend._lib.EC_KEY_get0_group(ec_key)
+ backend.openssl_assert(group != backend._ffi.NULL)
+
+ nid = backend._lib.EC_GROUP_get_curve_name(group)
+ # The following check is to find EC keys with unnamed curves and raise
+ # an error for now.
+ if nid == backend._lib.NID_undef:
+ raise ValueError(
+ "ECDSA keys with explicit parameters are unsupported at this time"
+ )
+
+ # This is like the above check, but it also catches the case where you
+ # explicitly encoded a curve with the same parameters as a named curve.
+ # Don't do that.
+ if (
+ not backend._lib.CRYPTOGRAPHY_IS_LIBRESSL
+ and backend._lib.EC_GROUP_get_asn1_flag(group) == 0
+ ):
+ raise ValueError(
+ "ECDSA keys with explicit parameters are unsupported at this time"
+ )
+
+ curve_name = backend._lib.OBJ_nid2sn(nid)
+ backend.openssl_assert(curve_name != backend._ffi.NULL)
+
+ sn = backend._ffi.string(curve_name).decode("ascii")
+ return sn
+
+
+def _mark_asn1_named_ec_curve(backend: Backend, ec_cdata):
+ """
+ Set the named curve flag on the EC_KEY. This causes OpenSSL to
+ serialize EC keys along with their curve OID which makes
+ deserialization easier.
+ """
+
+ backend._lib.EC_KEY_set_asn1_flag(
+ ec_cdata, backend._lib.OPENSSL_EC_NAMED_CURVE
+ )
+
+
+def _check_key_infinity(backend: Backend, ec_cdata) -> None:
+ point = backend._lib.EC_KEY_get0_public_key(ec_cdata)
+ backend.openssl_assert(point != backend._ffi.NULL)
+ group = backend._lib.EC_KEY_get0_group(ec_cdata)
+ backend.openssl_assert(group != backend._ffi.NULL)
+ if backend._lib.EC_POINT_is_at_infinity(group, point):
+ raise ValueError(
+ "Cannot load an EC public key where the point is at infinity"
+ )
+
+
+def _sn_to_elliptic_curve(backend: Backend, sn: str) -> ec.EllipticCurve:
+ try:
+ return ec._CURVE_TYPES[sn]()
+ except KeyError:
+ raise UnsupportedAlgorithm(
+ f"{sn} is not a supported elliptic curve",
+ _Reasons.UNSUPPORTED_ELLIPTIC_CURVE,
+ )
+
+
+def _ecdsa_sig_sign(
+ backend: Backend, private_key: _EllipticCurvePrivateKey, data: bytes
+) -> bytes:
+ max_size = backend._lib.ECDSA_size(private_key._ec_key)
+ backend.openssl_assert(max_size > 0)
+
+ sigbuf = backend._ffi.new("unsigned char[]", max_size)
+ siglen_ptr = backend._ffi.new("unsigned int[]", 1)
+ res = backend._lib.ECDSA_sign(
+ 0, data, len(data), sigbuf, siglen_ptr, private_key._ec_key
+ )
+ backend.openssl_assert(res == 1)
+ return backend._ffi.buffer(sigbuf)[: siglen_ptr[0]]
+
+
+def _ecdsa_sig_verify(
+ backend: Backend,
+ public_key: _EllipticCurvePublicKey,
+ signature: bytes,
+ data: bytes,
+) -> None:
+ res = backend._lib.ECDSA_verify(
+ 0, data, len(data), signature, len(signature), public_key._ec_key
+ )
+ if res != 1:
+ backend._consume_errors()
+ raise InvalidSignature
+
+
+class _EllipticCurvePrivateKey(ec.EllipticCurvePrivateKey):
+ def __init__(self, backend: Backend, ec_key_cdata, evp_pkey):
+ self._backend = backend
+ self._ec_key = ec_key_cdata
+ self._evp_pkey = evp_pkey
+
+ sn = _ec_key_curve_sn(backend, ec_key_cdata)
+ self._curve = _sn_to_elliptic_curve(backend, sn)
+ _mark_asn1_named_ec_curve(backend, ec_key_cdata)
+ _check_key_infinity(backend, ec_key_cdata)
+
+ @property
+ def curve(self) -> ec.EllipticCurve:
+ return self._curve
+
+ @property
+ def key_size(self) -> int:
+ return self.curve.key_size
+
+ def exchange(
+ self, algorithm: ec.ECDH, peer_public_key: ec.EllipticCurvePublicKey
+ ) -> bytes:
+ if not (
+ self._backend.elliptic_curve_exchange_algorithm_supported(
+ algorithm, self.curve
+ )
+ ):
+ raise UnsupportedAlgorithm(
+ "This backend does not support the ECDH algorithm.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+
+ if peer_public_key.curve.name != self.curve.name:
+ raise ValueError(
+ "peer_public_key and self are not on the same curve"
+ )
+
+ return _evp_pkey_derive(self._backend, self._evp_pkey, peer_public_key)
+
+ def public_key(self) -> ec.EllipticCurvePublicKey:
+ group = self._backend._lib.EC_KEY_get0_group(self._ec_key)
+ self._backend.openssl_assert(group != self._backend._ffi.NULL)
+
+ curve_nid = self._backend._lib.EC_GROUP_get_curve_name(group)
+ public_ec_key = self._backend._ec_key_new_by_curve_nid(curve_nid)
+
+ point = self._backend._lib.EC_KEY_get0_public_key(self._ec_key)
+ self._backend.openssl_assert(point != self._backend._ffi.NULL)
+
+ res = self._backend._lib.EC_KEY_set_public_key(public_ec_key, point)
+ self._backend.openssl_assert(res == 1)
+
+ evp_pkey = self._backend._ec_cdata_to_evp_pkey(public_ec_key)
+
+ return _EllipticCurvePublicKey(self._backend, public_ec_key, evp_pkey)
+
+ def private_numbers(self) -> ec.EllipticCurvePrivateNumbers:
+ bn = self._backend._lib.EC_KEY_get0_private_key(self._ec_key)
+ private_value = self._backend._bn_to_int(bn)
+ return ec.EllipticCurvePrivateNumbers(
+ private_value=private_value,
+ public_numbers=self.public_key().public_numbers(),
+ )
+
+ def private_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PrivateFormat,
+ encryption_algorithm: serialization.KeySerializationEncryption,
+ ) -> bytes:
+ return self._backend._private_key_bytes(
+ encoding,
+ format,
+ encryption_algorithm,
+ self,
+ self._evp_pkey,
+ self._ec_key,
+ )
+
+ def sign(
+ self,
+ data: bytes,
+ signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
+ ) -> bytes:
+ _check_signature_algorithm(signature_algorithm)
+ data, _ = _calculate_digest_and_algorithm(
+ data,
+ signature_algorithm.algorithm,
+ )
+ return _ecdsa_sig_sign(self._backend, self, data)
+
+
+class _EllipticCurvePublicKey(ec.EllipticCurvePublicKey):
+ def __init__(self, backend: Backend, ec_key_cdata, evp_pkey):
+ self._backend = backend
+ self._ec_key = ec_key_cdata
+ self._evp_pkey = evp_pkey
+
+ sn = _ec_key_curve_sn(backend, ec_key_cdata)
+ self._curve = _sn_to_elliptic_curve(backend, sn)
+ _mark_asn1_named_ec_curve(backend, ec_key_cdata)
+ _check_key_infinity(backend, ec_key_cdata)
+
+ @property
+ def curve(self) -> ec.EllipticCurve:
+ return self._curve
+
+ @property
+ def key_size(self) -> int:
+ return self.curve.key_size
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, _EllipticCurvePublicKey):
+ return NotImplemented
+
+ return (
+ self._backend._lib.EVP_PKEY_cmp(self._evp_pkey, other._evp_pkey)
+ == 1
+ )
+
+ def public_numbers(self) -> ec.EllipticCurvePublicNumbers:
+ group = self._backend._lib.EC_KEY_get0_group(self._ec_key)
+ self._backend.openssl_assert(group != self._backend._ffi.NULL)
+
+ point = self._backend._lib.EC_KEY_get0_public_key(self._ec_key)
+ self._backend.openssl_assert(point != self._backend._ffi.NULL)
+
+ with self._backend._tmp_bn_ctx() as bn_ctx:
+ bn_x = self._backend._lib.BN_CTX_get(bn_ctx)
+ bn_y = self._backend._lib.BN_CTX_get(bn_ctx)
+
+ res = self._backend._lib.EC_POINT_get_affine_coordinates(
+ group, point, bn_x, bn_y, bn_ctx
+ )
+ self._backend.openssl_assert(res == 1)
+
+ x = self._backend._bn_to_int(bn_x)
+ y = self._backend._bn_to_int(bn_y)
+
+ return ec.EllipticCurvePublicNumbers(x=x, y=y, curve=self._curve)
+
+ def _encode_point(self, format: serialization.PublicFormat) -> bytes:
+ if format is serialization.PublicFormat.CompressedPoint:
+ conversion = self._backend._lib.POINT_CONVERSION_COMPRESSED
+ else:
+ assert format is serialization.PublicFormat.UncompressedPoint
+ conversion = self._backend._lib.POINT_CONVERSION_UNCOMPRESSED
+
+ group = self._backend._lib.EC_KEY_get0_group(self._ec_key)
+ self._backend.openssl_assert(group != self._backend._ffi.NULL)
+ point = self._backend._lib.EC_KEY_get0_public_key(self._ec_key)
+ self._backend.openssl_assert(point != self._backend._ffi.NULL)
+ with self._backend._tmp_bn_ctx() as bn_ctx:
+ buflen = self._backend._lib.EC_POINT_point2oct(
+ group, point, conversion, self._backend._ffi.NULL, 0, bn_ctx
+ )
+ self._backend.openssl_assert(buflen > 0)
+ buf = self._backend._ffi.new("char[]", buflen)
+ res = self._backend._lib.EC_POINT_point2oct(
+ group, point, conversion, buf, buflen, bn_ctx
+ )
+ self._backend.openssl_assert(buflen == res)
+
+ return self._backend._ffi.buffer(buf)[:]
+
+ def public_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PublicFormat,
+ ) -> bytes:
+ if (
+ encoding is serialization.Encoding.X962
+ or format is serialization.PublicFormat.CompressedPoint
+ or format is serialization.PublicFormat.UncompressedPoint
+ ):
+ if encoding is not serialization.Encoding.X962 or format not in (
+ serialization.PublicFormat.CompressedPoint,
+ serialization.PublicFormat.UncompressedPoint,
+ ):
+ raise ValueError(
+ "X962 encoding must be used with CompressedPoint or "
+ "UncompressedPoint format"
+ )
+
+ return self._encode_point(format)
+ else:
+ return self._backend._public_key_bytes(
+ encoding, format, self, self._evp_pkey, None
+ )
+
+ def verify(
+ self,
+ signature: bytes,
+ data: bytes,
+ signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
+ ) -> None:
+ _check_signature_algorithm(signature_algorithm)
+ data, _ = _calculate_digest_and_algorithm(
+ data,
+ signature_algorithm.algorithm,
+ )
+ _ecdsa_sig_verify(self._backend, self, signature, data)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/rsa.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/rsa.py
new file mode 100644
index 0000000000..ef27d4ead5
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/rsa.py
@@ -0,0 +1,599 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import threading
+import typing
+
+from cryptography.exceptions import (
+ InvalidSignature,
+ UnsupportedAlgorithm,
+ _Reasons,
+)
+from cryptography.hazmat.backends.openssl.utils import (
+ _calculate_digest_and_algorithm,
+)
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
+from cryptography.hazmat.primitives.asymmetric.padding import (
+ MGF1,
+ OAEP,
+ PSS,
+ AsymmetricPadding,
+ PKCS1v15,
+ _Auto,
+ _DigestLength,
+ _MaxLength,
+ calculate_max_pss_salt_length,
+)
+from cryptography.hazmat.primitives.asymmetric.rsa import (
+ RSAPrivateKey,
+ RSAPrivateNumbers,
+ RSAPublicKey,
+ RSAPublicNumbers,
+)
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+
+
+def _get_rsa_pss_salt_length(
+ backend: Backend,
+ pss: PSS,
+ key: typing.Union[RSAPrivateKey, RSAPublicKey],
+ hash_algorithm: hashes.HashAlgorithm,
+) -> int:
+ salt = pss._salt_length
+
+ if isinstance(salt, _MaxLength):
+ return calculate_max_pss_salt_length(key, hash_algorithm)
+ elif isinstance(salt, _DigestLength):
+ return hash_algorithm.digest_size
+ elif isinstance(salt, _Auto):
+ if isinstance(key, RSAPrivateKey):
+ raise ValueError(
+ "PSS salt length can only be set to AUTO when verifying"
+ )
+ return backend._lib.RSA_PSS_SALTLEN_AUTO
+ else:
+ return salt
+
+
+def _enc_dec_rsa(
+ backend: Backend,
+ key: typing.Union[_RSAPrivateKey, _RSAPublicKey],
+ data: bytes,
+ padding: AsymmetricPadding,
+) -> bytes:
+ if not isinstance(padding, AsymmetricPadding):
+ raise TypeError("Padding must be an instance of AsymmetricPadding.")
+
+ if isinstance(padding, PKCS1v15):
+ padding_enum = backend._lib.RSA_PKCS1_PADDING
+ elif isinstance(padding, OAEP):
+ padding_enum = backend._lib.RSA_PKCS1_OAEP_PADDING
+
+ if not isinstance(padding._mgf, MGF1):
+ raise UnsupportedAlgorithm(
+ "Only MGF1 is supported by this backend.",
+ _Reasons.UNSUPPORTED_MGF,
+ )
+
+ if not backend.rsa_padding_supported(padding):
+ raise UnsupportedAlgorithm(
+ "This combination of padding and hash algorithm is not "
+ "supported by this backend.",
+ _Reasons.UNSUPPORTED_PADDING,
+ )
+
+ else:
+ raise UnsupportedAlgorithm(
+ f"{padding.name} is not supported by this backend.",
+ _Reasons.UNSUPPORTED_PADDING,
+ )
+
+ return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding)
+
+
+def _enc_dec_rsa_pkey_ctx(
+ backend: Backend,
+ key: typing.Union[_RSAPrivateKey, _RSAPublicKey],
+ data: bytes,
+ padding_enum: int,
+ padding: AsymmetricPadding,
+) -> bytes:
+ init: typing.Callable[[typing.Any], int]
+ crypt: typing.Callable[[typing.Any, typing.Any, int, bytes, int], int]
+ if isinstance(key, _RSAPublicKey):
+ init = backend._lib.EVP_PKEY_encrypt_init
+ crypt = backend._lib.EVP_PKEY_encrypt
+ else:
+ init = backend._lib.EVP_PKEY_decrypt_init
+ crypt = backend._lib.EVP_PKEY_decrypt
+
+ pkey_ctx = backend._lib.EVP_PKEY_CTX_new(key._evp_pkey, backend._ffi.NULL)
+ backend.openssl_assert(pkey_ctx != backend._ffi.NULL)
+ pkey_ctx = backend._ffi.gc(pkey_ctx, backend._lib.EVP_PKEY_CTX_free)
+ res = init(pkey_ctx)
+ backend.openssl_assert(res == 1)
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, padding_enum)
+ backend.openssl_assert(res > 0)
+ buf_size = backend._lib.EVP_PKEY_size(key._evp_pkey)
+ backend.openssl_assert(buf_size > 0)
+ if isinstance(padding, OAEP):
+ mgf1_md = backend._evp_md_non_null_from_algorithm(
+ padding._mgf._algorithm
+ )
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1_md)
+ backend.openssl_assert(res > 0)
+ oaep_md = backend._evp_md_non_null_from_algorithm(padding._algorithm)
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_oaep_md(pkey_ctx, oaep_md)
+ backend.openssl_assert(res > 0)
+
+ if (
+ isinstance(padding, OAEP)
+ and padding._label is not None
+ and len(padding._label) > 0
+ ):
+ # set0_rsa_oaep_label takes ownership of the char * so we need to
+ # copy it into some new memory
+ labelptr = backend._lib.OPENSSL_malloc(len(padding._label))
+ backend.openssl_assert(labelptr != backend._ffi.NULL)
+ backend._ffi.memmove(labelptr, padding._label, len(padding._label))
+ res = backend._lib.EVP_PKEY_CTX_set0_rsa_oaep_label(
+ pkey_ctx, labelptr, len(padding._label)
+ )
+ backend.openssl_assert(res == 1)
+
+ outlen = backend._ffi.new("size_t *", buf_size)
+ buf = backend._ffi.new("unsigned char[]", buf_size)
+ # Everything from this line onwards is written with the goal of being as
+ # constant-time as is practical given the constraints of Python and our
+ # API. See Bleichenbacher's '98 attack on RSA, and its many many variants.
+ # As such, you should not attempt to change this (particularly to "clean it
+ # up") without understanding why it was written this way (see
+ # Chesterton's Fence), and without measuring to verify you have not
+ # introduced observable time differences.
+ res = crypt(pkey_ctx, buf, outlen, data, len(data))
+ resbuf = backend._ffi.buffer(buf)[: outlen[0]]
+ backend._lib.ERR_clear_error()
+ if res <= 0:
+ raise ValueError("Encryption/decryption failed.")
+ return resbuf
+
+
+def _rsa_sig_determine_padding(
+ backend: Backend,
+ key: typing.Union[_RSAPrivateKey, _RSAPublicKey],
+ padding: AsymmetricPadding,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+) -> int:
+ if not isinstance(padding, AsymmetricPadding):
+ raise TypeError("Expected provider of AsymmetricPadding.")
+
+ pkey_size = backend._lib.EVP_PKEY_size(key._evp_pkey)
+ backend.openssl_assert(pkey_size > 0)
+
+ if isinstance(padding, PKCS1v15):
+ # Hash algorithm is ignored for PKCS1v15-padding, may be None.
+ padding_enum = backend._lib.RSA_PKCS1_PADDING
+ elif isinstance(padding, PSS):
+ if not isinstance(padding._mgf, MGF1):
+ raise UnsupportedAlgorithm(
+ "Only MGF1 is supported by this backend.",
+ _Reasons.UNSUPPORTED_MGF,
+ )
+
+ # PSS padding requires a hash algorithm
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise TypeError("Expected instance of hashes.HashAlgorithm.")
+
+ # Size of key in bytes - 2 is the maximum
+ # PSS signature length (salt length is checked later)
+ if pkey_size - algorithm.digest_size - 2 < 0:
+ raise ValueError(
+ "Digest too large for key size. Use a larger "
+ "key or different digest."
+ )
+
+ padding_enum = backend._lib.RSA_PKCS1_PSS_PADDING
+ else:
+ raise UnsupportedAlgorithm(
+ f"{padding.name} is not supported by this backend.",
+ _Reasons.UNSUPPORTED_PADDING,
+ )
+
+ return padding_enum
+
+
+# Hash algorithm can be absent (None) to initialize the context without setting
+# any message digest algorithm. This is currently only valid for the PKCS1v15
+# padding type, where it means that the signature data is encoded/decoded
+# as provided, without being wrapped in a DigestInfo structure.
+def _rsa_sig_setup(
+ backend: Backend,
+ padding: AsymmetricPadding,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+ key: typing.Union[_RSAPublicKey, _RSAPrivateKey],
+ init_func: typing.Callable[[typing.Any], int],
+):
+ padding_enum = _rsa_sig_determine_padding(backend, key, padding, algorithm)
+ pkey_ctx = backend._lib.EVP_PKEY_CTX_new(key._evp_pkey, backend._ffi.NULL)
+ backend.openssl_assert(pkey_ctx != backend._ffi.NULL)
+ pkey_ctx = backend._ffi.gc(pkey_ctx, backend._lib.EVP_PKEY_CTX_free)
+ res = init_func(pkey_ctx)
+ if res != 1:
+ errors = backend._consume_errors()
+ raise ValueError("Unable to sign/verify with this key", errors)
+
+ if algorithm is not None:
+ evp_md = backend._evp_md_non_null_from_algorithm(algorithm)
+ res = backend._lib.EVP_PKEY_CTX_set_signature_md(pkey_ctx, evp_md)
+ if res <= 0:
+ backend._consume_errors()
+ raise UnsupportedAlgorithm(
+ "{} is not supported by this backend for RSA signing.".format(
+ algorithm.name
+ ),
+ _Reasons.UNSUPPORTED_HASH,
+ )
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, padding_enum)
+ if res <= 0:
+ backend._consume_errors()
+ raise UnsupportedAlgorithm(
+ "{} is not supported for the RSA signature operation.".format(
+ padding.name
+ ),
+ _Reasons.UNSUPPORTED_PADDING,
+ )
+ if isinstance(padding, PSS):
+ assert isinstance(algorithm, hashes.HashAlgorithm)
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_pss_saltlen(
+ pkey_ctx,
+ _get_rsa_pss_salt_length(backend, padding, key, algorithm),
+ )
+ backend.openssl_assert(res > 0)
+
+ mgf1_md = backend._evp_md_non_null_from_algorithm(
+ padding._mgf._algorithm
+ )
+ res = backend._lib.EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf1_md)
+ backend.openssl_assert(res > 0)
+
+ return pkey_ctx
+
+
+def _rsa_sig_sign(
+ backend: Backend,
+ padding: AsymmetricPadding,
+ algorithm: hashes.HashAlgorithm,
+ private_key: _RSAPrivateKey,
+ data: bytes,
+) -> bytes:
+ pkey_ctx = _rsa_sig_setup(
+ backend,
+ padding,
+ algorithm,
+ private_key,
+ backend._lib.EVP_PKEY_sign_init,
+ )
+ buflen = backend._ffi.new("size_t *")
+ res = backend._lib.EVP_PKEY_sign(
+ pkey_ctx, backend._ffi.NULL, buflen, data, len(data)
+ )
+ backend.openssl_assert(res == 1)
+ buf = backend._ffi.new("unsigned char[]", buflen[0])
+ res = backend._lib.EVP_PKEY_sign(pkey_ctx, buf, buflen, data, len(data))
+ if res != 1:
+ errors = backend._consume_errors()
+ raise ValueError(
+ "Digest or salt length too long for key size. Use a larger key "
+ "or shorter salt length if you are specifying a PSS salt",
+ errors,
+ )
+
+ return backend._ffi.buffer(buf)[:]
+
+
+def _rsa_sig_verify(
+ backend: Backend,
+ padding: AsymmetricPadding,
+ algorithm: hashes.HashAlgorithm,
+ public_key: _RSAPublicKey,
+ signature: bytes,
+ data: bytes,
+) -> None:
+ pkey_ctx = _rsa_sig_setup(
+ backend,
+ padding,
+ algorithm,
+ public_key,
+ backend._lib.EVP_PKEY_verify_init,
+ )
+ res = backend._lib.EVP_PKEY_verify(
+ pkey_ctx, signature, len(signature), data, len(data)
+ )
+ # The previous call can return negative numbers in the event of an
+ # error. This is not a signature failure but we need to fail if it
+ # occurs.
+ backend.openssl_assert(res >= 0)
+ if res == 0:
+ backend._consume_errors()
+ raise InvalidSignature
+
+
+def _rsa_sig_recover(
+ backend: Backend,
+ padding: AsymmetricPadding,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+ public_key: _RSAPublicKey,
+ signature: bytes,
+) -> bytes:
+ pkey_ctx = _rsa_sig_setup(
+ backend,
+ padding,
+ algorithm,
+ public_key,
+ backend._lib.EVP_PKEY_verify_recover_init,
+ )
+
+ # Attempt to keep the rest of the code in this function as constant/time
+ # as possible. See the comment in _enc_dec_rsa_pkey_ctx. Note that the
+ # buflen parameter is used even though its value may be undefined in the
+ # error case. Due to the tolerant nature of Python slicing this does not
+ # trigger any exceptions.
+ maxlen = backend._lib.EVP_PKEY_size(public_key._evp_pkey)
+ backend.openssl_assert(maxlen > 0)
+ buf = backend._ffi.new("unsigned char[]", maxlen)
+ buflen = backend._ffi.new("size_t *", maxlen)
+ res = backend._lib.EVP_PKEY_verify_recover(
+ pkey_ctx, buf, buflen, signature, len(signature)
+ )
+ resbuf = backend._ffi.buffer(buf)[: buflen[0]]
+ backend._lib.ERR_clear_error()
+ # Assume that all parameter errors are handled during the setup phase and
+ # any error here is due to invalid signature.
+ if res != 1:
+ raise InvalidSignature
+ return resbuf
+
+
+class _RSAPrivateKey(RSAPrivateKey):
+ _evp_pkey: object
+ _rsa_cdata: object
+ _key_size: int
+
+ def __init__(
+ self,
+ backend: Backend,
+ rsa_cdata,
+ evp_pkey,
+ *,
+ unsafe_skip_rsa_key_validation: bool,
+ ):
+ res: int
+ # RSA_check_key is slower in OpenSSL 3.0.0 due to improved
+ # primality checking. In normal use this is unlikely to be a problem
+ # since users don't load new keys constantly, but for TESTING we've
+ # added an init arg that allows skipping the checks. You should not
+ # use this in production code unless you understand the consequences.
+ if not unsafe_skip_rsa_key_validation:
+ res = backend._lib.RSA_check_key(rsa_cdata)
+ if res != 1:
+ errors = backend._consume_errors()
+ raise ValueError("Invalid private key", errors)
+ # 2 is prime and passes an RSA key check, so we also check
+ # if p and q are odd just to be safe.
+ p = backend._ffi.new("BIGNUM **")
+ q = backend._ffi.new("BIGNUM **")
+ backend._lib.RSA_get0_factors(rsa_cdata, p, q)
+ backend.openssl_assert(p[0] != backend._ffi.NULL)
+ backend.openssl_assert(q[0] != backend._ffi.NULL)
+ p_odd = backend._lib.BN_is_odd(p[0])
+ q_odd = backend._lib.BN_is_odd(q[0])
+ if p_odd != 1 or q_odd != 1:
+ errors = backend._consume_errors()
+ raise ValueError("Invalid private key", errors)
+
+ self._backend = backend
+ self._rsa_cdata = rsa_cdata
+ self._evp_pkey = evp_pkey
+ # Used for lazy blinding
+ self._blinded = False
+ self._blinding_lock = threading.Lock()
+
+ n = self._backend._ffi.new("BIGNUM **")
+ self._backend._lib.RSA_get0_key(
+ self._rsa_cdata,
+ n,
+ self._backend._ffi.NULL,
+ self._backend._ffi.NULL,
+ )
+ self._backend.openssl_assert(n[0] != self._backend._ffi.NULL)
+ self._key_size = self._backend._lib.BN_num_bits(n[0])
+
+ def _enable_blinding(self) -> None:
+ # If you call blind on an already blinded RSA key OpenSSL will turn
+ # it off and back on, which is a performance hit we want to avoid.
+ if not self._blinded:
+ with self._blinding_lock:
+ self._non_threadsafe_enable_blinding()
+
+ def _non_threadsafe_enable_blinding(self) -> None:
+ # This is only a separate function to allow for testing to cover both
+ # branches. It should never be invoked except through _enable_blinding.
+ # Check if it's not True again in case another thread raced past the
+ # first non-locked check.
+ if not self._blinded:
+ res = self._backend._lib.RSA_blinding_on(
+ self._rsa_cdata, self._backend._ffi.NULL
+ )
+ self._backend.openssl_assert(res == 1)
+ self._blinded = True
+
+ @property
+ def key_size(self) -> int:
+ return self._key_size
+
+ def decrypt(self, ciphertext: bytes, padding: AsymmetricPadding) -> bytes:
+ self._enable_blinding()
+ key_size_bytes = (self.key_size + 7) // 8
+ if key_size_bytes != len(ciphertext):
+ raise ValueError("Ciphertext length must be equal to key size.")
+
+ return _enc_dec_rsa(self._backend, self, ciphertext, padding)
+
+ def public_key(self) -> RSAPublicKey:
+ ctx = self._backend._lib.RSAPublicKey_dup(self._rsa_cdata)
+ self._backend.openssl_assert(ctx != self._backend._ffi.NULL)
+ ctx = self._backend._ffi.gc(ctx, self._backend._lib.RSA_free)
+ evp_pkey = self._backend._rsa_cdata_to_evp_pkey(ctx)
+ return _RSAPublicKey(self._backend, ctx, evp_pkey)
+
+ def private_numbers(self) -> RSAPrivateNumbers:
+ n = self._backend._ffi.new("BIGNUM **")
+ e = self._backend._ffi.new("BIGNUM **")
+ d = self._backend._ffi.new("BIGNUM **")
+ p = self._backend._ffi.new("BIGNUM **")
+ q = self._backend._ffi.new("BIGNUM **")
+ dmp1 = self._backend._ffi.new("BIGNUM **")
+ dmq1 = self._backend._ffi.new("BIGNUM **")
+ iqmp = self._backend._ffi.new("BIGNUM **")
+ self._backend._lib.RSA_get0_key(self._rsa_cdata, n, e, d)
+ self._backend.openssl_assert(n[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(e[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(d[0] != self._backend._ffi.NULL)
+ self._backend._lib.RSA_get0_factors(self._rsa_cdata, p, q)
+ self._backend.openssl_assert(p[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(q[0] != self._backend._ffi.NULL)
+ self._backend._lib.RSA_get0_crt_params(
+ self._rsa_cdata, dmp1, dmq1, iqmp
+ )
+ self._backend.openssl_assert(dmp1[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(dmq1[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(iqmp[0] != self._backend._ffi.NULL)
+ return RSAPrivateNumbers(
+ p=self._backend._bn_to_int(p[0]),
+ q=self._backend._bn_to_int(q[0]),
+ d=self._backend._bn_to_int(d[0]),
+ dmp1=self._backend._bn_to_int(dmp1[0]),
+ dmq1=self._backend._bn_to_int(dmq1[0]),
+ iqmp=self._backend._bn_to_int(iqmp[0]),
+ public_numbers=RSAPublicNumbers(
+ e=self._backend._bn_to_int(e[0]),
+ n=self._backend._bn_to_int(n[0]),
+ ),
+ )
+
+ def private_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PrivateFormat,
+ encryption_algorithm: serialization.KeySerializationEncryption,
+ ) -> bytes:
+ return self._backend._private_key_bytes(
+ encoding,
+ format,
+ encryption_algorithm,
+ self,
+ self._evp_pkey,
+ self._rsa_cdata,
+ )
+
+ def sign(
+ self,
+ data: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> bytes:
+ self._enable_blinding()
+ data, algorithm = _calculate_digest_and_algorithm(data, algorithm)
+ return _rsa_sig_sign(self._backend, padding, algorithm, self, data)
+
+
+class _RSAPublicKey(RSAPublicKey):
+ _evp_pkey: object
+ _rsa_cdata: object
+ _key_size: int
+
+ def __init__(self, backend: Backend, rsa_cdata, evp_pkey):
+ self._backend = backend
+ self._rsa_cdata = rsa_cdata
+ self._evp_pkey = evp_pkey
+
+ n = self._backend._ffi.new("BIGNUM **")
+ self._backend._lib.RSA_get0_key(
+ self._rsa_cdata,
+ n,
+ self._backend._ffi.NULL,
+ self._backend._ffi.NULL,
+ )
+ self._backend.openssl_assert(n[0] != self._backend._ffi.NULL)
+ self._key_size = self._backend._lib.BN_num_bits(n[0])
+
+ @property
+ def key_size(self) -> int:
+ return self._key_size
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, _RSAPublicKey):
+ return NotImplemented
+
+ return (
+ self._backend._lib.EVP_PKEY_cmp(self._evp_pkey, other._evp_pkey)
+ == 1
+ )
+
+ def encrypt(self, plaintext: bytes, padding: AsymmetricPadding) -> bytes:
+ return _enc_dec_rsa(self._backend, self, plaintext, padding)
+
+ def public_numbers(self) -> RSAPublicNumbers:
+ n = self._backend._ffi.new("BIGNUM **")
+ e = self._backend._ffi.new("BIGNUM **")
+ self._backend._lib.RSA_get0_key(
+ self._rsa_cdata, n, e, self._backend._ffi.NULL
+ )
+ self._backend.openssl_assert(n[0] != self._backend._ffi.NULL)
+ self._backend.openssl_assert(e[0] != self._backend._ffi.NULL)
+ return RSAPublicNumbers(
+ e=self._backend._bn_to_int(e[0]),
+ n=self._backend._bn_to_int(n[0]),
+ )
+
+ def public_bytes(
+ self,
+ encoding: serialization.Encoding,
+ format: serialization.PublicFormat,
+ ) -> bytes:
+ return self._backend._public_key_bytes(
+ encoding, format, self, self._evp_pkey, self._rsa_cdata
+ )
+
+ def verify(
+ self,
+ signature: bytes,
+ data: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> None:
+ data, algorithm = _calculate_digest_and_algorithm(data, algorithm)
+ _rsa_sig_verify(
+ self._backend, padding, algorithm, self, signature, data
+ )
+
+ def recover_data_from_signature(
+ self,
+ signature: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+ ) -> bytes:
+ if isinstance(algorithm, asym_utils.Prehashed):
+ raise TypeError(
+ "Prehashed is only supported in the sign and verify methods. "
+ "It cannot be used with recover_data_from_signature."
+ )
+ return _rsa_sig_recover(
+ self._backend, padding, algorithm, self, signature
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/utils.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/utils.py
new file mode 100644
index 0000000000..5b404defde
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/backends/openssl/utils.py
@@ -0,0 +1,63 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.backend import Backend
+
+
+def _evp_pkey_derive(backend: Backend, evp_pkey, peer_public_key) -> bytes:
+ ctx = backend._lib.EVP_PKEY_CTX_new(evp_pkey, backend._ffi.NULL)
+ backend.openssl_assert(ctx != backend._ffi.NULL)
+ ctx = backend._ffi.gc(ctx, backend._lib.EVP_PKEY_CTX_free)
+ res = backend._lib.EVP_PKEY_derive_init(ctx)
+ backend.openssl_assert(res == 1)
+
+ if backend._lib.Cryptography_HAS_EVP_PKEY_SET_PEER_EX:
+ res = backend._lib.EVP_PKEY_derive_set_peer_ex(
+ ctx, peer_public_key._evp_pkey, 0
+ )
+ else:
+ res = backend._lib.EVP_PKEY_derive_set_peer(
+ ctx, peer_public_key._evp_pkey
+ )
+ backend.openssl_assert(res == 1)
+
+ keylen = backend._ffi.new("size_t *")
+ res = backend._lib.EVP_PKEY_derive(ctx, backend._ffi.NULL, keylen)
+ backend.openssl_assert(res == 1)
+ backend.openssl_assert(keylen[0] > 0)
+ buf = backend._ffi.new("unsigned char[]", keylen[0])
+ res = backend._lib.EVP_PKEY_derive(ctx, buf, keylen)
+ if res != 1:
+ errors = backend._consume_errors()
+ raise ValueError("Error computing shared key.", errors)
+
+ return backend._ffi.buffer(buf, keylen[0])[:]
+
+
+def _calculate_digest_and_algorithm(
+ data: bytes,
+ algorithm: typing.Union[Prehashed, hashes.HashAlgorithm],
+) -> typing.Tuple[bytes, hashes.HashAlgorithm]:
+ if not isinstance(algorithm, Prehashed):
+ hash_ctx = hashes.Hash(algorithm)
+ hash_ctx.update(data)
+ data = hash_ctx.finalize()
+ else:
+ algorithm = algorithm._algorithm
+
+ if len(data) != algorithm.digest_size:
+ raise ValueError(
+ "The provided data must be the same length as the hash "
+ "algorithm's digest size."
+ )
+
+ return (data, algorithm)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/__init__.py
new file mode 100644
index 0000000000..b509336233
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/__init__.py
@@ -0,0 +1,3 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.py
new file mode 100644
index 0000000000..b6339850e6
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.py
@@ -0,0 +1 @@
+from _rust import *
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.pyi
new file mode 100644
index 0000000000..94a37a20aa
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/__init__.pyi
@@ -0,0 +1,34 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import types
+import typing
+
+def check_pkcs7_padding(data: bytes) -> bool: ...
+def check_ansix923_padding(data: bytes) -> bool: ...
+
+class ObjectIdentifier:
+ def __init__(self, val: str) -> None: ...
+ @property
+ def dotted_string(self) -> str: ...
+ @property
+ def _name(self) -> str: ...
+
+T = typing.TypeVar("T")
+
+class FixedPool(typing.Generic[T]):
+ def __init__(
+ self,
+ create: typing.Callable[[], T],
+ ) -> None: ...
+ def acquire(self) -> PoolAcquisition[T]: ...
+
+class PoolAcquisition(typing.Generic[T]):
+ def __enter__(self) -> T: ...
+ def __exit__(
+ self,
+ exc_type: typing.Optional[typing.Type[BaseException]],
+ exc_value: typing.Optional[BaseException],
+ exc_tb: typing.Optional[types.TracebackType],
+ ) -> None: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/_openssl.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/_openssl.pyi
new file mode 100644
index 0000000000..80100082ac
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/_openssl.pyi
@@ -0,0 +1,8 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+lib = typing.Any
+ffi = typing.Any
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/asn1.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/asn1.pyi
new file mode 100644
index 0000000000..a8369ba838
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/asn1.pyi
@@ -0,0 +1,16 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+class TestCertificate:
+ not_after_tag: int
+ not_before_tag: int
+ issuer_value_tags: typing.List[int]
+ subject_value_tags: typing.List[int]
+
+def decode_dss_signature(signature: bytes) -> typing.Tuple[int, int]: ...
+def encode_dss_signature(r: int, s: int) -> bytes: ...
+def parse_spki_for_data(data: bytes) -> bytes: ...
+def test_parse_certificate(data: bytes) -> TestCertificate: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/exceptions.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/exceptions.pyi
new file mode 100644
index 0000000000..09f46b1e81
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/exceptions.pyi
@@ -0,0 +1,17 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+class _Reasons:
+ BACKEND_MISSING_INTERFACE: _Reasons
+ UNSUPPORTED_HASH: _Reasons
+ UNSUPPORTED_CIPHER: _Reasons
+ UNSUPPORTED_PADDING: _Reasons
+ UNSUPPORTED_MGF: _Reasons
+ UNSUPPORTED_PUBLIC_KEY_ALGORITHM: _Reasons
+ UNSUPPORTED_ELLIPTIC_CURVE: _Reasons
+ UNSUPPORTED_SERIALIZATION: _Reasons
+ UNSUPPORTED_X509: _Reasons
+ UNSUPPORTED_EXCHANGE_ALGORITHM: _Reasons
+ UNSUPPORTED_DIFFIE_HELLMAN: _Reasons
+ UNSUPPORTED_MAC: _Reasons
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/ocsp.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/ocsp.pyi
new file mode 100644
index 0000000000..4671eb9ba3
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/ocsp.pyi
@@ -0,0 +1,25 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes
+from cryptography.x509.ocsp import (
+ OCSPRequest,
+ OCSPRequestBuilder,
+ OCSPResponse,
+ OCSPResponseBuilder,
+ OCSPResponseStatus,
+)
+
+def load_der_ocsp_request(data: bytes) -> OCSPRequest: ...
+def load_der_ocsp_response(data: bytes) -> OCSPResponse: ...
+def create_ocsp_request(builder: OCSPRequestBuilder) -> OCSPRequest: ...
+def create_ocsp_response(
+ status: OCSPResponseStatus,
+ builder: typing.Optional[OCSPResponseBuilder],
+ private_key: typing.Optional[PrivateKeyTypes],
+ hash_algorithm: typing.Optional[hashes.HashAlgorithm],
+) -> OCSPResponse: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/__init__.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/__init__.pyi
new file mode 100644
index 0000000000..82f30d20b0
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/__init__.pyi
@@ -0,0 +1,47 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+from cryptography.hazmat.bindings._rust.openssl import (
+ dh,
+ dsa,
+ ed448,
+ ed25519,
+ hashes,
+ hmac,
+ kdf,
+ poly1305,
+ x448,
+ x25519,
+)
+
+__all__ = [
+ "openssl_version",
+ "raise_openssl_error",
+ "dh",
+ "dsa",
+ "hashes",
+ "hmac",
+ "kdf",
+ "ed448",
+ "ed25519",
+ "poly1305",
+ "x448",
+ "x25519",
+]
+
+def openssl_version() -> int: ...
+def raise_openssl_error() -> typing.NoReturn: ...
+def capture_error_stack() -> typing.List[OpenSSLError]: ...
+def is_fips_enabled() -> bool: ...
+
+class OpenSSLError:
+ @property
+ def lib(self) -> int: ...
+ @property
+ def reason(self) -> int: ...
+ @property
+ def reason_text(self) -> bytes: ...
+ def _lib_reason_match(self, lib: int, reason: int) -> bool: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dh.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dh.pyi
new file mode 100644
index 0000000000..bfd005d99f
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dh.pyi
@@ -0,0 +1,22 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import dh
+
+MIN_MODULUS_SIZE: int
+
+class DHPrivateKey: ...
+class DHPublicKey: ...
+class DHParameters: ...
+
+def generate_parameters(generator: int, key_size: int) -> dh.DHParameters: ...
+def private_key_from_ptr(ptr: int) -> dh.DHPrivateKey: ...
+def public_key_from_ptr(ptr: int) -> dh.DHPublicKey: ...
+def from_pem_parameters(data: bytes) -> dh.DHParameters: ...
+def from_der_parameters(data: bytes) -> dh.DHParameters: ...
+def from_private_numbers(numbers: dh.DHPrivateNumbers) -> dh.DHPrivateKey: ...
+def from_public_numbers(numbers: dh.DHPublicNumbers) -> dh.DHPublicKey: ...
+def from_parameter_numbers(
+ numbers: dh.DHParameterNumbers,
+) -> dh.DHParameters: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dsa.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dsa.pyi
new file mode 100644
index 0000000000..5a56f256d5
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/dsa.pyi
@@ -0,0 +1,20 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import dsa
+
+class DSAPrivateKey: ...
+class DSAPublicKey: ...
+class DSAParameters: ...
+
+def generate_parameters(key_size: int) -> dsa.DSAParameters: ...
+def private_key_from_ptr(ptr: int) -> dsa.DSAPrivateKey: ...
+def public_key_from_ptr(ptr: int) -> dsa.DSAPublicKey: ...
+def from_private_numbers(
+ numbers: dsa.DSAPrivateNumbers,
+) -> dsa.DSAPrivateKey: ...
+def from_public_numbers(numbers: dsa.DSAPublicNumbers) -> dsa.DSAPublicKey: ...
+def from_parameter_numbers(
+ numbers: dsa.DSAParameterNumbers,
+) -> dsa.DSAParameters: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi
new file mode 100644
index 0000000000..c7f127f0b1
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi
@@ -0,0 +1,14 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import ed25519
+
+class Ed25519PrivateKey: ...
+class Ed25519PublicKey: ...
+
+def generate_key() -> ed25519.Ed25519PrivateKey: ...
+def private_key_from_ptr(ptr: int) -> ed25519.Ed25519PrivateKey: ...
+def public_key_from_ptr(ptr: int) -> ed25519.Ed25519PublicKey: ...
+def from_private_bytes(data: bytes) -> ed25519.Ed25519PrivateKey: ...
+def from_public_bytes(data: bytes) -> ed25519.Ed25519PublicKey: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed448.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed448.pyi
new file mode 100644
index 0000000000..1cf5f1773a
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/ed448.pyi
@@ -0,0 +1,14 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import ed448
+
+class Ed448PrivateKey: ...
+class Ed448PublicKey: ...
+
+def generate_key() -> ed448.Ed448PrivateKey: ...
+def private_key_from_ptr(ptr: int) -> ed448.Ed448PrivateKey: ...
+def public_key_from_ptr(ptr: int) -> ed448.Ed448PublicKey: ...
+def from_private_bytes(data: bytes) -> ed448.Ed448PrivateKey: ...
+def from_public_bytes(data: bytes) -> ed448.Ed448PublicKey: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi
new file mode 100644
index 0000000000..ca5f42a006
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi
@@ -0,0 +1,17 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+from cryptography.hazmat.primitives import hashes
+
+class Hash(hashes.HashContext):
+ def __init__(
+ self, algorithm: hashes.HashAlgorithm, backend: typing.Any = None
+ ) -> None: ...
+ @property
+ def algorithm(self) -> hashes.HashAlgorithm: ...
+ def update(self, data: bytes) -> None: ...
+ def finalize(self) -> bytes: ...
+ def copy(self) -> Hash: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hmac.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hmac.pyi
new file mode 100644
index 0000000000..e38d9b54d0
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/hmac.pyi
@@ -0,0 +1,21 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+from cryptography.hazmat.primitives import hashes
+
+class HMAC(hashes.HashContext):
+ def __init__(
+ self,
+ key: bytes,
+ algorithm: hashes.HashAlgorithm,
+ backend: typing.Any = None,
+ ) -> None: ...
+ @property
+ def algorithm(self) -> hashes.HashAlgorithm: ...
+ def update(self, data: bytes) -> None: ...
+ def finalize(self) -> bytes: ...
+ def verify(self, signature: bytes) -> None: ...
+ def copy(self) -> HMAC: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi
new file mode 100644
index 0000000000..034a8fed2e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi
@@ -0,0 +1,22 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.hashes import HashAlgorithm
+
+def derive_pbkdf2_hmac(
+ key_material: bytes,
+ algorithm: HashAlgorithm,
+ salt: bytes,
+ iterations: int,
+ length: int,
+) -> bytes: ...
+def derive_scrypt(
+ key_material: bytes,
+ salt: bytes,
+ n: int,
+ r: int,
+ p: int,
+ max_mem: int,
+ length: int,
+) -> bytes: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi
new file mode 100644
index 0000000000..2e9b0a9e12
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi
@@ -0,0 +1,13 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+class Poly1305:
+ def __init__(self, key: bytes) -> None: ...
+ @staticmethod
+ def generate_tag(key: bytes, data: bytes) -> bytes: ...
+ @staticmethod
+ def verify_tag(key: bytes, data: bytes, tag: bytes) -> None: ...
+ def update(self, data: bytes) -> None: ...
+ def finalize(self) -> bytes: ...
+ def verify(self, tag: bytes) -> None: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x25519.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x25519.pyi
new file mode 100644
index 0000000000..90f7cbdda9
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x25519.pyi
@@ -0,0 +1,14 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import x25519
+
+class X25519PrivateKey: ...
+class X25519PublicKey: ...
+
+def generate_key() -> x25519.X25519PrivateKey: ...
+def private_key_from_ptr(ptr: int) -> x25519.X25519PrivateKey: ...
+def public_key_from_ptr(ptr: int) -> x25519.X25519PublicKey: ...
+def from_private_bytes(data: bytes) -> x25519.X25519PrivateKey: ...
+def from_public_bytes(data: bytes) -> x25519.X25519PublicKey: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x448.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x448.pyi
new file mode 100644
index 0000000000..d326c8d2d7
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/openssl/x448.pyi
@@ -0,0 +1,14 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from cryptography.hazmat.primitives.asymmetric import x448
+
+class X448PrivateKey: ...
+class X448PublicKey: ...
+
+def generate_key() -> x448.X448PrivateKey: ...
+def private_key_from_ptr(ptr: int) -> x448.X448PrivateKey: ...
+def public_key_from_ptr(ptr: int) -> x448.X448PublicKey: ...
+def from_private_bytes(data: bytes) -> x448.X448PrivateKey: ...
+def from_public_bytes(data: bytes) -> x448.X448PublicKey: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/pkcs7.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/pkcs7.pyi
new file mode 100644
index 0000000000..66bd850981
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/pkcs7.pyi
@@ -0,0 +1,15 @@
+import typing
+
+from cryptography import x509
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives.serialization import pkcs7
+
+def serialize_certificates(
+ certs: typing.List[x509.Certificate],
+ encoding: serialization.Encoding,
+) -> bytes: ...
+def sign_and_serialize(
+ builder: pkcs7.PKCS7SignatureBuilder,
+ encoding: serialization.Encoding,
+ options: typing.Iterable[pkcs7.PKCS7Options],
+) -> bytes: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/x509.pyi b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/x509.pyi
new file mode 100644
index 0000000000..24b2f5e3a7
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/_rust/x509.pyi
@@ -0,0 +1,44 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+import typing
+
+from cryptography import x509
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives.asymmetric.padding import PSS, PKCS1v15
+from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes
+
+def load_pem_x509_certificate(data: bytes) -> x509.Certificate: ...
+def load_pem_x509_certificates(
+ data: bytes,
+) -> typing.List[x509.Certificate]: ...
+def load_der_x509_certificate(data: bytes) -> x509.Certificate: ...
+def load_pem_x509_crl(data: bytes) -> x509.CertificateRevocationList: ...
+def load_der_x509_crl(data: bytes) -> x509.CertificateRevocationList: ...
+def load_pem_x509_csr(data: bytes) -> x509.CertificateSigningRequest: ...
+def load_der_x509_csr(data: bytes) -> x509.CertificateSigningRequest: ...
+def encode_name_bytes(name: x509.Name) -> bytes: ...
+def encode_extension_value(extension: x509.ExtensionType) -> bytes: ...
+def create_x509_certificate(
+ builder: x509.CertificateBuilder,
+ private_key: PrivateKeyTypes,
+ hash_algorithm: typing.Optional[hashes.HashAlgorithm],
+ padding: typing.Optional[typing.Union[PKCS1v15, PSS]],
+) -> x509.Certificate: ...
+def create_x509_csr(
+ builder: x509.CertificateSigningRequestBuilder,
+ private_key: PrivateKeyTypes,
+ hash_algorithm: typing.Optional[hashes.HashAlgorithm],
+) -> x509.CertificateSigningRequest: ...
+def create_x509_crl(
+ builder: x509.CertificateRevocationListBuilder,
+ private_key: PrivateKeyTypes,
+ hash_algorithm: typing.Optional[hashes.HashAlgorithm],
+) -> x509.CertificateRevocationList: ...
+
+class Sct: ...
+class Certificate: ...
+class RevokedCertificate: ...
+class CertificateRevocationList: ...
+class CertificateSigningRequest: ...
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/__init__.py
new file mode 100644
index 0000000000..b509336233
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/__init__.py
@@ -0,0 +1,3 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/_conditional.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/_conditional.py
new file mode 100644
index 0000000000..5e8ecd0418
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/_conditional.py
@@ -0,0 +1,329 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+
+def cryptography_has_set_cert_cb() -> typing.List[str]:
+ return [
+ "SSL_CTX_set_cert_cb",
+ "SSL_set_cert_cb",
+ ]
+
+
+def cryptography_has_ssl_st() -> typing.List[str]:
+ return [
+ "SSL_ST_BEFORE",
+ "SSL_ST_OK",
+ "SSL_ST_INIT",
+ "SSL_ST_RENEGOTIATE",
+ ]
+
+
+def cryptography_has_tls_st() -> typing.List[str]:
+ return [
+ "TLS_ST_BEFORE",
+ "TLS_ST_OK",
+ ]
+
+
+def cryptography_has_evp_pkey_dhx() -> typing.List[str]:
+ return [
+ "EVP_PKEY_DHX",
+ ]
+
+
+def cryptography_has_mem_functions() -> typing.List[str]:
+ return [
+ "Cryptography_CRYPTO_set_mem_functions",
+ ]
+
+
+def cryptography_has_x509_store_ctx_get_issuer() -> typing.List[str]:
+ return [
+ "X509_STORE_set_get_issuer",
+ ]
+
+
+def cryptography_has_ed448() -> typing.List[str]:
+ return [
+ "EVP_PKEY_ED448",
+ "NID_ED448",
+ ]
+
+
+def cryptography_has_ed25519() -> typing.List[str]:
+ return [
+ "NID_ED25519",
+ "EVP_PKEY_ED25519",
+ ]
+
+
+def cryptography_has_poly1305() -> typing.List[str]:
+ return [
+ "NID_poly1305",
+ "EVP_PKEY_POLY1305",
+ ]
+
+
+def cryptography_has_evp_digestfinal_xof() -> typing.List[str]:
+ return [
+ "EVP_DigestFinalXOF",
+ ]
+
+
+def cryptography_has_fips() -> typing.List[str]:
+ return [
+ "FIPS_mode_set",
+ "FIPS_mode",
+ ]
+
+
+def cryptography_has_ssl_sigalgs() -> typing.List[str]:
+ return [
+ "SSL_CTX_set1_sigalgs_list",
+ ]
+
+
+def cryptography_has_psk() -> typing.List[str]:
+ return [
+ "SSL_CTX_use_psk_identity_hint",
+ "SSL_CTX_set_psk_server_callback",
+ "SSL_CTX_set_psk_client_callback",
+ ]
+
+
+def cryptography_has_psk_tlsv13() -> typing.List[str]:
+ return [
+ "SSL_CTX_set_psk_find_session_callback",
+ "SSL_CTX_set_psk_use_session_callback",
+ "Cryptography_SSL_SESSION_new",
+ "SSL_CIPHER_find",
+ "SSL_SESSION_set1_master_key",
+ "SSL_SESSION_set_cipher",
+ "SSL_SESSION_set_protocol_version",
+ ]
+
+
+def cryptography_has_custom_ext() -> typing.List[str]:
+ return [
+ "SSL_CTX_add_client_custom_ext",
+ "SSL_CTX_add_server_custom_ext",
+ "SSL_extension_supported",
+ ]
+
+
+def cryptography_has_tlsv13_functions() -> typing.List[str]:
+ return [
+ "SSL_VERIFY_POST_HANDSHAKE",
+ "SSL_CTX_set_ciphersuites",
+ "SSL_verify_client_post_handshake",
+ "SSL_CTX_set_post_handshake_auth",
+ "SSL_set_post_handshake_auth",
+ "SSL_SESSION_get_max_early_data",
+ "SSL_write_early_data",
+ "SSL_read_early_data",
+ "SSL_CTX_set_max_early_data",
+ ]
+
+
+def cryptography_has_raw_key() -> typing.List[str]:
+ return [
+ "EVP_PKEY_new_raw_private_key",
+ "EVP_PKEY_new_raw_public_key",
+ "EVP_PKEY_get_raw_private_key",
+ "EVP_PKEY_get_raw_public_key",
+ ]
+
+
+def cryptography_has_engine() -> typing.List[str]:
+ return [
+ "ENGINE_by_id",
+ "ENGINE_init",
+ "ENGINE_finish",
+ "ENGINE_get_default_RAND",
+ "ENGINE_set_default_RAND",
+ "ENGINE_unregister_RAND",
+ "ENGINE_ctrl_cmd",
+ "ENGINE_free",
+ "ENGINE_get_name",
+ "ENGINE_ctrl_cmd_string",
+ "ENGINE_load_builtin_engines",
+ "ENGINE_load_private_key",
+ "ENGINE_load_public_key",
+ "SSL_CTX_set_client_cert_engine",
+ ]
+
+
+def cryptography_has_verified_chain() -> typing.List[str]:
+ return [
+ "SSL_get0_verified_chain",
+ ]
+
+
+def cryptography_has_srtp() -> typing.List[str]:
+ return [
+ "SSL_CTX_set_tlsext_use_srtp",
+ "SSL_set_tlsext_use_srtp",
+ "SSL_get_selected_srtp_profile",
+ ]
+
+
+def cryptography_has_providers() -> typing.List[str]:
+ return [
+ "OSSL_PROVIDER_load",
+ "OSSL_PROVIDER_unload",
+ "ERR_LIB_PROV",
+ "PROV_R_WRONG_FINAL_BLOCK_LENGTH",
+ "PROV_R_BAD_DECRYPT",
+ ]
+
+
+def cryptography_has_op_no_renegotiation() -> typing.List[str]:
+ return [
+ "SSL_OP_NO_RENEGOTIATION",
+ ]
+
+
+def cryptography_has_dtls_get_data_mtu() -> typing.List[str]:
+ return [
+ "DTLS_get_data_mtu",
+ ]
+
+
+def cryptography_has_300_fips() -> typing.List[str]:
+ return [
+ "EVP_default_properties_is_fips_enabled",
+ "EVP_default_properties_enable_fips",
+ ]
+
+
+def cryptography_has_ssl_cookie() -> typing.List[str]:
+ return [
+ "SSL_OP_COOKIE_EXCHANGE",
+ "DTLSv1_listen",
+ "SSL_CTX_set_cookie_generate_cb",
+ "SSL_CTX_set_cookie_verify_cb",
+ ]
+
+
+def cryptography_has_pkcs7_funcs() -> typing.List[str]:
+ return [
+ "SMIME_write_PKCS7",
+ "PEM_write_bio_PKCS7_stream",
+ "PKCS7_sign_add_signer",
+ "PKCS7_final",
+ "PKCS7_verify",
+ "SMIME_read_PKCS7",
+ "PKCS7_get0_signers",
+ ]
+
+
+def cryptography_has_bn_flags() -> typing.List[str]:
+ return [
+ "BN_FLG_CONSTTIME",
+ "BN_set_flags",
+ "BN_prime_checks_for_size",
+ ]
+
+
+def cryptography_has_evp_pkey_dh() -> typing.List[str]:
+ return [
+ "EVP_PKEY_set1_DH",
+ ]
+
+
+def cryptography_has_300_evp_cipher() -> typing.List[str]:
+ return ["EVP_CIPHER_fetch", "EVP_CIPHER_free"]
+
+
+def cryptography_has_unexpected_eof_while_reading() -> typing.List[str]:
+ return ["SSL_R_UNEXPECTED_EOF_WHILE_READING"]
+
+
+def cryptography_has_pkcs12_set_mac() -> typing.List[str]:
+ return ["PKCS12_set_mac"]
+
+
+def cryptography_has_ssl_op_ignore_unexpected_eof() -> typing.List[str]:
+ return [
+ "SSL_OP_IGNORE_UNEXPECTED_EOF",
+ ]
+
+
+def cryptography_has_get_extms_support() -> typing.List[str]:
+ return ["SSL_get_extms_support"]
+
+
+def cryptography_has_evp_pkey_set_peer_ex() -> typing.List[str]:
+ return ["EVP_PKEY_derive_set_peer_ex"]
+
+
+def cryptography_has_evp_aead() -> typing.List[str]:
+ return [
+ "EVP_aead_chacha20_poly1305",
+ "EVP_AEAD_CTX_free",
+ "EVP_AEAD_CTX_seal",
+ "EVP_AEAD_CTX_open",
+ "EVP_AEAD_max_overhead",
+ "Cryptography_EVP_AEAD_CTX_new",
+ ]
+
+
+# This is a mapping of
+# {condition: function-returning-names-dependent-on-that-condition} so we can
+# loop over them and delete unsupported names at runtime. It will be removed
+# when cffi supports #if in cdef. We use functions instead of just a dict of
+# lists so we can use coverage to measure which are used.
+CONDITIONAL_NAMES = {
+ "Cryptography_HAS_SET_CERT_CB": cryptography_has_set_cert_cb,
+ "Cryptography_HAS_SSL_ST": cryptography_has_ssl_st,
+ "Cryptography_HAS_TLS_ST": cryptography_has_tls_st,
+ "Cryptography_HAS_EVP_PKEY_DHX": cryptography_has_evp_pkey_dhx,
+ "Cryptography_HAS_MEM_FUNCTIONS": cryptography_has_mem_functions,
+ "Cryptography_HAS_X509_STORE_CTX_GET_ISSUER": (
+ cryptography_has_x509_store_ctx_get_issuer
+ ),
+ "Cryptography_HAS_ED448": cryptography_has_ed448,
+ "Cryptography_HAS_ED25519": cryptography_has_ed25519,
+ "Cryptography_HAS_POLY1305": cryptography_has_poly1305,
+ "Cryptography_HAS_FIPS": cryptography_has_fips,
+ "Cryptography_HAS_SIGALGS": cryptography_has_ssl_sigalgs,
+ "Cryptography_HAS_PSK": cryptography_has_psk,
+ "Cryptography_HAS_PSK_TLSv1_3": cryptography_has_psk_tlsv13,
+ "Cryptography_HAS_CUSTOM_EXT": cryptography_has_custom_ext,
+ "Cryptography_HAS_TLSv1_3_FUNCTIONS": cryptography_has_tlsv13_functions,
+ "Cryptography_HAS_RAW_KEY": cryptography_has_raw_key,
+ "Cryptography_HAS_EVP_DIGESTFINAL_XOF": (
+ cryptography_has_evp_digestfinal_xof
+ ),
+ "Cryptography_HAS_ENGINE": cryptography_has_engine,
+ "Cryptography_HAS_VERIFIED_CHAIN": cryptography_has_verified_chain,
+ "Cryptography_HAS_SRTP": cryptography_has_srtp,
+ "Cryptography_HAS_PROVIDERS": cryptography_has_providers,
+ "Cryptography_HAS_OP_NO_RENEGOTIATION": (
+ cryptography_has_op_no_renegotiation
+ ),
+ "Cryptography_HAS_DTLS_GET_DATA_MTU": cryptography_has_dtls_get_data_mtu,
+ "Cryptography_HAS_300_FIPS": cryptography_has_300_fips,
+ "Cryptography_HAS_SSL_COOKIE": cryptography_has_ssl_cookie,
+ "Cryptography_HAS_PKCS7_FUNCS": cryptography_has_pkcs7_funcs,
+ "Cryptography_HAS_BN_FLAGS": cryptography_has_bn_flags,
+ "Cryptography_HAS_EVP_PKEY_DH": cryptography_has_evp_pkey_dh,
+ "Cryptography_HAS_300_EVP_CIPHER": cryptography_has_300_evp_cipher,
+ "Cryptography_HAS_UNEXPECTED_EOF_WHILE_READING": (
+ cryptography_has_unexpected_eof_while_reading
+ ),
+ "Cryptography_HAS_PKCS12_SET_MAC": cryptography_has_pkcs12_set_mac,
+ "Cryptography_HAS_SSL_OP_IGNORE_UNEXPECTED_EOF": (
+ cryptography_has_ssl_op_ignore_unexpected_eof
+ ),
+ "Cryptography_HAS_GET_EXTMS_SUPPORT": cryptography_has_get_extms_support,
+ "Cryptography_HAS_EVP_PKEY_SET_PEER_EX": (
+ cryptography_has_evp_pkey_set_peer_ex
+ ),
+ "Cryptography_HAS_EVP_AEAD": (cryptography_has_evp_aead),
+}
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/binding.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/binding.py
new file mode 100644
index 0000000000..b50d631518
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/bindings/openssl/binding.py
@@ -0,0 +1,179 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import os
+import sys
+import threading
+import types
+import typing
+import warnings
+
+import cryptography
+from cryptography.exceptions import InternalError
+from cryptography.hazmat.bindings._rust import _openssl, openssl
+from cryptography.hazmat.bindings.openssl._conditional import CONDITIONAL_NAMES
+
+
+def _openssl_assert(
+ lib,
+ ok: bool,
+ errors: typing.Optional[typing.List[openssl.OpenSSLError]] = None,
+) -> None:
+ if not ok:
+ if errors is None:
+ errors = openssl.capture_error_stack()
+
+ raise InternalError(
+ "Unknown OpenSSL error. This error is commonly encountered when "
+ "another library is not cleaning up the OpenSSL error stack. If "
+ "you are using cryptography with another library that uses "
+ "OpenSSL try disabling it before reporting a bug. Otherwise "
+ "please file an issue at https://github.com/pyca/cryptography/"
+ "issues with information on how to reproduce "
+ "this. ({!r})".format(errors),
+ errors,
+ )
+
+
+def _legacy_provider_error(loaded: bool) -> None:
+ if not loaded:
+ raise RuntimeError(
+ "OpenSSL 3.0's legacy provider failed to load. This is a fatal "
+ "error by default, but cryptography supports running without "
+ "legacy algorithms by setting the environment variable "
+ "CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error,"
+ " you have likely made a mistake with your OpenSSL configuration."
+ )
+
+
+def build_conditional_library(
+ lib: typing.Any,
+ conditional_names: typing.Dict[str, typing.Callable[[], typing.List[str]]],
+) -> typing.Any:
+ conditional_lib = types.ModuleType("lib")
+ conditional_lib._original_lib = lib # type: ignore[attr-defined]
+ excluded_names = set()
+ for condition, names_cb in conditional_names.items():
+ if not getattr(lib, condition):
+ excluded_names.update(names_cb())
+
+ for attr in dir(lib):
+ if attr not in excluded_names:
+ setattr(conditional_lib, attr, getattr(lib, attr))
+
+ return conditional_lib
+
+
+class Binding:
+ """
+ OpenSSL API wrapper.
+ """
+
+ lib: typing.ClassVar = None
+ ffi = _openssl.ffi
+ _lib_loaded = False
+ _init_lock = threading.Lock()
+ _legacy_provider: typing.Any = ffi.NULL
+ _legacy_provider_loaded = False
+ _default_provider: typing.Any = ffi.NULL
+
+ def __init__(self) -> None:
+ self._ensure_ffi_initialized()
+
+ def _enable_fips(self) -> None:
+ # This function enables FIPS mode for OpenSSL 3.0.0 on installs that
+ # have the FIPS provider installed properly.
+ _openssl_assert(self.lib, self.lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER)
+ self._base_provider = self.lib.OSSL_PROVIDER_load(
+ self.ffi.NULL, b"base"
+ )
+ _openssl_assert(self.lib, self._base_provider != self.ffi.NULL)
+ self.lib._fips_provider = self.lib.OSSL_PROVIDER_load(
+ self.ffi.NULL, b"fips"
+ )
+ _openssl_assert(self.lib, self.lib._fips_provider != self.ffi.NULL)
+
+ res = self.lib.EVP_default_properties_enable_fips(self.ffi.NULL, 1)
+ _openssl_assert(self.lib, res == 1)
+
+ @classmethod
+ def _ensure_ffi_initialized(cls) -> None:
+ with cls._init_lock:
+ if not cls._lib_loaded:
+ cls.lib = build_conditional_library(
+ _openssl.lib, CONDITIONAL_NAMES
+ )
+ cls._lib_loaded = True
+ # As of OpenSSL 3.0.0 we must register a legacy cipher provider
+ # to get RC2 (needed for junk asymmetric private key
+ # serialization), RC4, Blowfish, IDEA, SEED, etc. These things
+ # are ugly legacy, but we aren't going to get rid of them
+ # any time soon.
+ if cls.lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER:
+ if not os.environ.get("CRYPTOGRAPHY_OPENSSL_NO_LEGACY"):
+ cls._legacy_provider = cls.lib.OSSL_PROVIDER_load(
+ cls.ffi.NULL, b"legacy"
+ )
+ cls._legacy_provider_loaded = (
+ cls._legacy_provider != cls.ffi.NULL
+ )
+ _legacy_provider_error(cls._legacy_provider_loaded)
+
+ cls._default_provider = cls.lib.OSSL_PROVIDER_load(
+ cls.ffi.NULL, b"default"
+ )
+ _openssl_assert(
+ cls.lib, cls._default_provider != cls.ffi.NULL
+ )
+
+ @classmethod
+ def init_static_locks(cls) -> None:
+ cls._ensure_ffi_initialized()
+
+
+def _verify_package_version(version: str) -> None:
+ # Occasionally we run into situations where the version of the Python
+ # package does not match the version of the shared object that is loaded.
+ # This may occur in environments where multiple versions of cryptography
+ # are installed and available in the python path. To avoid errors cropping
+ # up later this code checks that the currently imported package and the
+ # shared object that were loaded have the same version and raise an
+ # ImportError if they do not
+ so_package_version = _openssl.ffi.string(
+ _openssl.lib.CRYPTOGRAPHY_PACKAGE_VERSION
+ )
+ if version.encode("ascii") != so_package_version:
+ raise ImportError(
+ "The version of cryptography does not match the loaded "
+ "shared object. This can happen if you have multiple copies of "
+ "cryptography installed in your Python path. Please try creating "
+ "a new virtual environment to resolve this issue. "
+ "Loaded python version: {}, shared object version: {}".format(
+ version, so_package_version
+ )
+ )
+
+ _openssl_assert(
+ _openssl.lib,
+ _openssl.lib.OpenSSL_version_num() == openssl.openssl_version(),
+ )
+
+
+_verify_package_version(cryptography.__version__)
+
+Binding.init_static_locks()
+
+if (
+ sys.platform == "win32"
+ and os.environ.get("PROCESSOR_ARCHITEW6432") is not None
+):
+ warnings.warn(
+ "You are using cryptography on a 32-bit Python on a 64-bit Windows "
+ "Operating System. Cryptography will be significantly faster if you "
+ "switch to using a 64-bit Python.",
+ UserWarning,
+ stacklevel=2,
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/__init__.py
new file mode 100644
index 0000000000..b509336233
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/__init__.py
@@ -0,0 +1,3 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_asymmetric.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_asymmetric.py
new file mode 100644
index 0000000000..ea55ffdf1a
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_asymmetric.py
@@ -0,0 +1,19 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+# This exists to break an import cycle. It is normally accessible from the
+# asymmetric padding module.
+
+
+class AsymmetricPadding(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def name(self) -> str:
+ """
+ A string naming this padding (e.g. "PSS", "PKCS1").
+ """
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_cipheralgorithm.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_cipheralgorithm.py
new file mode 100644
index 0000000000..3b880b6488
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_cipheralgorithm.py
@@ -0,0 +1,45 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+# This exists to break an import cycle. It is normally accessible from the
+# ciphers module.
+
+
+class CipherAlgorithm(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def name(self) -> str:
+ """
+ A string naming this mode (e.g. "AES", "Camellia").
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_sizes(self) -> typing.FrozenSet[int]:
+ """
+ Valid key sizes for this algorithm in bits
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The size of the key being used as an integer in bits (e.g. 128, 256).
+ """
+
+
+class BlockCipherAlgorithm(CipherAlgorithm):
+ key: bytes
+
+ @property
+ @abc.abstractmethod
+ def block_size(self) -> int:
+ """
+ The size of a block as an integer in bits (e.g. 64, 128).
+ """
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_serialization.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_serialization.py
new file mode 100644
index 0000000000..34f3fbc860
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/_serialization.py
@@ -0,0 +1,170 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography import utils
+from cryptography.hazmat.primitives.hashes import HashAlgorithm
+
+# This exists to break an import cycle. These classes are normally accessible
+# from the serialization module.
+
+
+class PBES(utils.Enum):
+ PBESv1SHA1And3KeyTripleDESCBC = "PBESv1 using SHA1 and 3-Key TripleDES"
+ PBESv2SHA256AndAES256CBC = "PBESv2 using SHA256 PBKDF2 and AES256 CBC"
+
+
+class Encoding(utils.Enum):
+ PEM = "PEM"
+ DER = "DER"
+ OpenSSH = "OpenSSH"
+ Raw = "Raw"
+ X962 = "ANSI X9.62"
+ SMIME = "S/MIME"
+
+
+class PrivateFormat(utils.Enum):
+ PKCS8 = "PKCS8"
+ TraditionalOpenSSL = "TraditionalOpenSSL"
+ Raw = "Raw"
+ OpenSSH = "OpenSSH"
+ PKCS12 = "PKCS12"
+
+ def encryption_builder(self) -> KeySerializationEncryptionBuilder:
+ if self not in (PrivateFormat.OpenSSH, PrivateFormat.PKCS12):
+ raise ValueError(
+ "encryption_builder only supported with PrivateFormat.OpenSSH"
+ " and PrivateFormat.PKCS12"
+ )
+ return KeySerializationEncryptionBuilder(self)
+
+
+class PublicFormat(utils.Enum):
+ SubjectPublicKeyInfo = "X.509 subjectPublicKeyInfo with PKCS#1"
+ PKCS1 = "Raw PKCS#1"
+ OpenSSH = "OpenSSH"
+ Raw = "Raw"
+ CompressedPoint = "X9.62 Compressed Point"
+ UncompressedPoint = "X9.62 Uncompressed Point"
+
+
+class ParameterFormat(utils.Enum):
+ PKCS3 = "PKCS3"
+
+
+class KeySerializationEncryption(metaclass=abc.ABCMeta):
+ pass
+
+
+class BestAvailableEncryption(KeySerializationEncryption):
+ def __init__(self, password: bytes):
+ if not isinstance(password, bytes) or len(password) == 0:
+ raise ValueError("Password must be 1 or more bytes.")
+
+ self.password = password
+
+
+class NoEncryption(KeySerializationEncryption):
+ pass
+
+
+class KeySerializationEncryptionBuilder:
+ def __init__(
+ self,
+ format: PrivateFormat,
+ *,
+ _kdf_rounds: typing.Optional[int] = None,
+ _hmac_hash: typing.Optional[HashAlgorithm] = None,
+ _key_cert_algorithm: typing.Optional[PBES] = None,
+ ) -> None:
+ self._format = format
+
+ self._kdf_rounds = _kdf_rounds
+ self._hmac_hash = _hmac_hash
+ self._key_cert_algorithm = _key_cert_algorithm
+
+ def kdf_rounds(self, rounds: int) -> KeySerializationEncryptionBuilder:
+ if self._kdf_rounds is not None:
+ raise ValueError("kdf_rounds already set")
+
+ if not isinstance(rounds, int):
+ raise TypeError("kdf_rounds must be an integer")
+
+ if rounds < 1:
+ raise ValueError("kdf_rounds must be a positive integer")
+
+ return KeySerializationEncryptionBuilder(
+ self._format,
+ _kdf_rounds=rounds,
+ _hmac_hash=self._hmac_hash,
+ _key_cert_algorithm=self._key_cert_algorithm,
+ )
+
+ def hmac_hash(
+ self, algorithm: HashAlgorithm
+ ) -> KeySerializationEncryptionBuilder:
+ if self._format is not PrivateFormat.PKCS12:
+ raise TypeError(
+ "hmac_hash only supported with PrivateFormat.PKCS12"
+ )
+
+ if self._hmac_hash is not None:
+ raise ValueError("hmac_hash already set")
+ return KeySerializationEncryptionBuilder(
+ self._format,
+ _kdf_rounds=self._kdf_rounds,
+ _hmac_hash=algorithm,
+ _key_cert_algorithm=self._key_cert_algorithm,
+ )
+
+ def key_cert_algorithm(
+ self, algorithm: PBES
+ ) -> KeySerializationEncryptionBuilder:
+ if self._format is not PrivateFormat.PKCS12:
+ raise TypeError(
+ "key_cert_algorithm only supported with "
+ "PrivateFormat.PKCS12"
+ )
+ if self._key_cert_algorithm is not None:
+ raise ValueError("key_cert_algorithm already set")
+ return KeySerializationEncryptionBuilder(
+ self._format,
+ _kdf_rounds=self._kdf_rounds,
+ _hmac_hash=self._hmac_hash,
+ _key_cert_algorithm=algorithm,
+ )
+
+ def build(self, password: bytes) -> KeySerializationEncryption:
+ if not isinstance(password, bytes) or len(password) == 0:
+ raise ValueError("Password must be 1 or more bytes.")
+
+ return _KeySerializationEncryption(
+ self._format,
+ password,
+ kdf_rounds=self._kdf_rounds,
+ hmac_hash=self._hmac_hash,
+ key_cert_algorithm=self._key_cert_algorithm,
+ )
+
+
+class _KeySerializationEncryption(KeySerializationEncryption):
+ def __init__(
+ self,
+ format: PrivateFormat,
+ password: bytes,
+ *,
+ kdf_rounds: typing.Optional[int],
+ hmac_hash: typing.Optional[HashAlgorithm],
+ key_cert_algorithm: typing.Optional[PBES],
+ ):
+ self._format = format
+ self.password = password
+
+ self._kdf_rounds = kdf_rounds
+ self._hmac_hash = hmac_hash
+ self._key_cert_algorithm = key_cert_algorithm
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/__init__.py
new file mode 100644
index 0000000000..b509336233
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/__init__.py
@@ -0,0 +1,3 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dh.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dh.py
new file mode 100644
index 0000000000..751bcc402e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dh.py
@@ -0,0 +1,261 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization
+
+
+def generate_parameters(
+ generator: int, key_size: int, backend: typing.Any = None
+) -> DHParameters:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.generate_dh_parameters(generator, key_size)
+
+
+class DHParameterNumbers:
+ def __init__(self, p: int, g: int, q: typing.Optional[int] = None) -> None:
+ if not isinstance(p, int) or not isinstance(g, int):
+ raise TypeError("p and g must be integers")
+ if q is not None and not isinstance(q, int):
+ raise TypeError("q must be integer or None")
+
+ if g < 2:
+ raise ValueError("DH generator must be 2 or greater")
+
+ if p.bit_length() < rust_openssl.dh.MIN_MODULUS_SIZE:
+ raise ValueError(
+ f"p (modulus) must be at least "
+ f"{rust_openssl.dh.MIN_MODULUS_SIZE}-bit"
+ )
+
+ self._p = p
+ self._g = g
+ self._q = q
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DHParameterNumbers):
+ return NotImplemented
+
+ return (
+ self._p == other._p and self._g == other._g and self._q == other._q
+ )
+
+ def parameters(self, backend: typing.Any = None) -> DHParameters:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dh_parameter_numbers(self)
+
+ @property
+ def p(self) -> int:
+ return self._p
+
+ @property
+ def g(self) -> int:
+ return self._g
+
+ @property
+ def q(self) -> typing.Optional[int]:
+ return self._q
+
+
+class DHPublicNumbers:
+ def __init__(self, y: int, parameter_numbers: DHParameterNumbers) -> None:
+ if not isinstance(y, int):
+ raise TypeError("y must be an integer.")
+
+ if not isinstance(parameter_numbers, DHParameterNumbers):
+ raise TypeError(
+ "parameters must be an instance of DHParameterNumbers."
+ )
+
+ self._y = y
+ self._parameter_numbers = parameter_numbers
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DHPublicNumbers):
+ return NotImplemented
+
+ return (
+ self._y == other._y
+ and self._parameter_numbers == other._parameter_numbers
+ )
+
+ def public_key(self, backend: typing.Any = None) -> DHPublicKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dh_public_numbers(self)
+
+ @property
+ def y(self) -> int:
+ return self._y
+
+ @property
+ def parameter_numbers(self) -> DHParameterNumbers:
+ return self._parameter_numbers
+
+
+class DHPrivateNumbers:
+ def __init__(self, x: int, public_numbers: DHPublicNumbers) -> None:
+ if not isinstance(x, int):
+ raise TypeError("x must be an integer.")
+
+ if not isinstance(public_numbers, DHPublicNumbers):
+ raise TypeError(
+ "public_numbers must be an instance of " "DHPublicNumbers."
+ )
+
+ self._x = x
+ self._public_numbers = public_numbers
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DHPrivateNumbers):
+ return NotImplemented
+
+ return (
+ self._x == other._x
+ and self._public_numbers == other._public_numbers
+ )
+
+ def private_key(self, backend: typing.Any = None) -> DHPrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dh_private_numbers(self)
+
+ @property
+ def public_numbers(self) -> DHPublicNumbers:
+ return self._public_numbers
+
+ @property
+ def x(self) -> int:
+ return self._x
+
+
+class DHParameters(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def generate_private_key(self) -> DHPrivateKey:
+ """
+ Generates and returns a DHPrivateKey.
+ """
+
+ @abc.abstractmethod
+ def parameter_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.ParameterFormat,
+ ) -> bytes:
+ """
+ Returns the parameters serialized as bytes.
+ """
+
+ @abc.abstractmethod
+ def parameter_numbers(self) -> DHParameterNumbers:
+ """
+ Returns a DHParameterNumbers.
+ """
+
+
+DHParametersWithSerialization = DHParameters
+DHParameters.register(rust_openssl.dh.DHParameters)
+
+
+class DHPublicKey(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the prime modulus.
+ """
+
+ @abc.abstractmethod
+ def parameters(self) -> DHParameters:
+ """
+ The DHParameters object associated with this public key.
+ """
+
+ @abc.abstractmethod
+ def public_numbers(self) -> DHPublicNumbers:
+ """
+ Returns a DHPublicNumbers.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+DHPublicKeyWithSerialization = DHPublicKey
+DHPublicKey.register(rust_openssl.dh.DHPublicKey)
+
+
+class DHPrivateKey(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the prime modulus.
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> DHPublicKey:
+ """
+ The DHPublicKey associated with this private key.
+ """
+
+ @abc.abstractmethod
+ def parameters(self) -> DHParameters:
+ """
+ The DHParameters object associated with this private key.
+ """
+
+ @abc.abstractmethod
+ def exchange(self, peer_public_key: DHPublicKey) -> bytes:
+ """
+ Given peer's DHPublicKey, carry out the key exchange and
+ return shared key as bytes.
+ """
+
+ @abc.abstractmethod
+ def private_numbers(self) -> DHPrivateNumbers:
+ """
+ Returns a DHPrivateNumbers.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+DHPrivateKeyWithSerialization = DHPrivateKey
+DHPrivateKey.register(rust_openssl.dh.DHPrivateKey)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dsa.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dsa.py
new file mode 100644
index 0000000000..a8c52de4fb
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/dsa.py
@@ -0,0 +1,299 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization, hashes
+from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
+
+
+class DSAParameters(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def generate_private_key(self) -> DSAPrivateKey:
+ """
+ Generates and returns a DSAPrivateKey.
+ """
+
+ @abc.abstractmethod
+ def parameter_numbers(self) -> DSAParameterNumbers:
+ """
+ Returns a DSAParameterNumbers.
+ """
+
+
+DSAParametersWithNumbers = DSAParameters
+DSAParameters.register(rust_openssl.dsa.DSAParameters)
+
+
+class DSAPrivateKey(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the prime modulus.
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> DSAPublicKey:
+ """
+ The DSAPublicKey associated with this private key.
+ """
+
+ @abc.abstractmethod
+ def parameters(self) -> DSAParameters:
+ """
+ The DSAParameters object associated with this private key.
+ """
+
+ @abc.abstractmethod
+ def sign(
+ self,
+ data: bytes,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> bytes:
+ """
+ Signs the data
+ """
+
+ @abc.abstractmethod
+ def private_numbers(self) -> DSAPrivateNumbers:
+ """
+ Returns a DSAPrivateNumbers.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+DSAPrivateKeyWithSerialization = DSAPrivateKey
+DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)
+
+
+class DSAPublicKey(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the prime modulus.
+ """
+
+ @abc.abstractmethod
+ def parameters(self) -> DSAParameters:
+ """
+ The DSAParameters object associated with this public key.
+ """
+
+ @abc.abstractmethod
+ def public_numbers(self) -> DSAPublicNumbers:
+ """
+ Returns a DSAPublicNumbers.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+ @abc.abstractmethod
+ def verify(
+ self,
+ signature: bytes,
+ data: bytes,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> None:
+ """
+ Verifies the signature of the data.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+DSAPublicKeyWithSerialization = DSAPublicKey
+DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)
+
+
+class DSAParameterNumbers:
+ def __init__(self, p: int, q: int, g: int):
+ if (
+ not isinstance(p, int)
+ or not isinstance(q, int)
+ or not isinstance(g, int)
+ ):
+ raise TypeError(
+ "DSAParameterNumbers p, q, and g arguments must be integers."
+ )
+
+ self._p = p
+ self._q = q
+ self._g = g
+
+ @property
+ def p(self) -> int:
+ return self._p
+
+ @property
+ def q(self) -> int:
+ return self._q
+
+ @property
+ def g(self) -> int:
+ return self._g
+
+ def parameters(self, backend: typing.Any = None) -> DSAParameters:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dsa_parameter_numbers(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DSAParameterNumbers):
+ return NotImplemented
+
+ return self.p == other.p and self.q == other.q and self.g == other.g
+
+ def __repr__(self) -> str:
+ return (
+ "<DSAParameterNumbers(p={self.p}, q={self.q}, "
+ "g={self.g})>".format(self=self)
+ )
+
+
+class DSAPublicNumbers:
+ def __init__(self, y: int, parameter_numbers: DSAParameterNumbers):
+ if not isinstance(y, int):
+ raise TypeError("DSAPublicNumbers y argument must be an integer.")
+
+ if not isinstance(parameter_numbers, DSAParameterNumbers):
+ raise TypeError(
+ "parameter_numbers must be a DSAParameterNumbers instance."
+ )
+
+ self._y = y
+ self._parameter_numbers = parameter_numbers
+
+ @property
+ def y(self) -> int:
+ return self._y
+
+ @property
+ def parameter_numbers(self) -> DSAParameterNumbers:
+ return self._parameter_numbers
+
+ def public_key(self, backend: typing.Any = None) -> DSAPublicKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dsa_public_numbers(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DSAPublicNumbers):
+ return NotImplemented
+
+ return (
+ self.y == other.y
+ and self.parameter_numbers == other.parameter_numbers
+ )
+
+ def __repr__(self) -> str:
+ return (
+ "<DSAPublicNumbers(y={self.y}, "
+ "parameter_numbers={self.parameter_numbers})>".format(self=self)
+ )
+
+
+class DSAPrivateNumbers:
+ def __init__(self, x: int, public_numbers: DSAPublicNumbers):
+ if not isinstance(x, int):
+ raise TypeError("DSAPrivateNumbers x argument must be an integer.")
+
+ if not isinstance(public_numbers, DSAPublicNumbers):
+ raise TypeError(
+ "public_numbers must be a DSAPublicNumbers instance."
+ )
+ self._public_numbers = public_numbers
+ self._x = x
+
+ @property
+ def x(self) -> int:
+ return self._x
+
+ @property
+ def public_numbers(self) -> DSAPublicNumbers:
+ return self._public_numbers
+
+ def private_key(self, backend: typing.Any = None) -> DSAPrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_dsa_private_numbers(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DSAPrivateNumbers):
+ return NotImplemented
+
+ return (
+ self.x == other.x and self.public_numbers == other.public_numbers
+ )
+
+
+def generate_parameters(
+ key_size: int, backend: typing.Any = None
+) -> DSAParameters:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.generate_dsa_parameters(key_size)
+
+
+def generate_private_key(
+ key_size: int, backend: typing.Any = None
+) -> DSAPrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.generate_dsa_private_key_and_parameters(key_size)
+
+
+def _check_dsa_parameters(parameters: DSAParameterNumbers) -> None:
+ if parameters.p.bit_length() not in [1024, 2048, 3072, 4096]:
+ raise ValueError(
+ "p must be exactly 1024, 2048, 3072, or 4096 bits long"
+ )
+ if parameters.q.bit_length() not in [160, 224, 256]:
+ raise ValueError("q must be exactly 160, 224, or 256 bits long")
+
+ if not (1 < parameters.g < parameters.p):
+ raise ValueError("g, p don't satisfy 1 < g < p.")
+
+
+def _check_dsa_private_numbers(numbers: DSAPrivateNumbers) -> None:
+ parameters = numbers.public_numbers.parameter_numbers
+ _check_dsa_parameters(parameters)
+ if numbers.x <= 0 or numbers.x >= parameters.q:
+ raise ValueError("x must be > 0 and < q.")
+
+ if numbers.public_numbers.y != pow(parameters.g, numbers.x, parameters.p):
+ raise ValueError("y must be equal to (g ** x % p).")
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ec.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ec.py
new file mode 100644
index 0000000000..ddfaabf4f3
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ec.py
@@ -0,0 +1,490 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography import utils
+from cryptography.hazmat._oid import ObjectIdentifier
+from cryptography.hazmat.primitives import _serialization, hashes
+from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
+
+
+class EllipticCurveOID:
+ SECP192R1 = ObjectIdentifier("1.2.840.10045.3.1.1")
+ SECP224R1 = ObjectIdentifier("1.3.132.0.33")
+ SECP256K1 = ObjectIdentifier("1.3.132.0.10")
+ SECP256R1 = ObjectIdentifier("1.2.840.10045.3.1.7")
+ SECP384R1 = ObjectIdentifier("1.3.132.0.34")
+ SECP521R1 = ObjectIdentifier("1.3.132.0.35")
+ BRAINPOOLP256R1 = ObjectIdentifier("1.3.36.3.3.2.8.1.1.7")
+ BRAINPOOLP384R1 = ObjectIdentifier("1.3.36.3.3.2.8.1.1.11")
+ BRAINPOOLP512R1 = ObjectIdentifier("1.3.36.3.3.2.8.1.1.13")
+ SECT163K1 = ObjectIdentifier("1.3.132.0.1")
+ SECT163R2 = ObjectIdentifier("1.3.132.0.15")
+ SECT233K1 = ObjectIdentifier("1.3.132.0.26")
+ SECT233R1 = ObjectIdentifier("1.3.132.0.27")
+ SECT283K1 = ObjectIdentifier("1.3.132.0.16")
+ SECT283R1 = ObjectIdentifier("1.3.132.0.17")
+ SECT409K1 = ObjectIdentifier("1.3.132.0.36")
+ SECT409R1 = ObjectIdentifier("1.3.132.0.37")
+ SECT571K1 = ObjectIdentifier("1.3.132.0.38")
+ SECT571R1 = ObjectIdentifier("1.3.132.0.39")
+
+
+class EllipticCurve(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def name(self) -> str:
+ """
+ The name of the curve. e.g. secp256r1.
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ Bit size of a secret scalar for the curve.
+ """
+
+
+class EllipticCurveSignatureAlgorithm(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def algorithm(
+ self,
+ ) -> typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm]:
+ """
+ The digest algorithm used with this signature.
+ """
+
+
+class EllipticCurvePrivateKey(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def exchange(
+ self, algorithm: ECDH, peer_public_key: EllipticCurvePublicKey
+ ) -> bytes:
+ """
+ Performs a key exchange operation using the provided algorithm with the
+ provided peer's public key.
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> EllipticCurvePublicKey:
+ """
+ The EllipticCurvePublicKey for this private key.
+ """
+
+ @property
+ @abc.abstractmethod
+ def curve(self) -> EllipticCurve:
+ """
+ The EllipticCurve that this key is on.
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ Bit size of a secret scalar for the curve.
+ """
+
+ @abc.abstractmethod
+ def sign(
+ self,
+ data: bytes,
+ signature_algorithm: EllipticCurveSignatureAlgorithm,
+ ) -> bytes:
+ """
+ Signs the data
+ """
+
+ @abc.abstractmethod
+ def private_numbers(self) -> EllipticCurvePrivateNumbers:
+ """
+ Returns an EllipticCurvePrivateNumbers.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+EllipticCurvePrivateKeyWithSerialization = EllipticCurvePrivateKey
+
+
+class EllipticCurvePublicKey(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def curve(self) -> EllipticCurve:
+ """
+ The EllipticCurve that this key is on.
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ Bit size of a secret scalar for the curve.
+ """
+
+ @abc.abstractmethod
+ def public_numbers(self) -> EllipticCurvePublicNumbers:
+ """
+ Returns an EllipticCurvePublicNumbers.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+ @abc.abstractmethod
+ def verify(
+ self,
+ signature: bytes,
+ data: bytes,
+ signature_algorithm: EllipticCurveSignatureAlgorithm,
+ ) -> None:
+ """
+ Verifies the signature of the data.
+ """
+
+ @classmethod
+ def from_encoded_point(
+ cls, curve: EllipticCurve, data: bytes
+ ) -> EllipticCurvePublicKey:
+ utils._check_bytes("data", data)
+
+ if not isinstance(curve, EllipticCurve):
+ raise TypeError("curve must be an EllipticCurve instance")
+
+ if len(data) == 0:
+ raise ValueError("data must not be an empty byte string")
+
+ if data[0] not in [0x02, 0x03, 0x04]:
+ raise ValueError("Unsupported elliptic curve point type")
+
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ return backend.load_elliptic_curve_public_bytes(curve, data)
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+EllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey
+
+
+class SECT571R1(EllipticCurve):
+ name = "sect571r1"
+ key_size = 570
+
+
+class SECT409R1(EllipticCurve):
+ name = "sect409r1"
+ key_size = 409
+
+
+class SECT283R1(EllipticCurve):
+ name = "sect283r1"
+ key_size = 283
+
+
+class SECT233R1(EllipticCurve):
+ name = "sect233r1"
+ key_size = 233
+
+
+class SECT163R2(EllipticCurve):
+ name = "sect163r2"
+ key_size = 163
+
+
+class SECT571K1(EllipticCurve):
+ name = "sect571k1"
+ key_size = 571
+
+
+class SECT409K1(EllipticCurve):
+ name = "sect409k1"
+ key_size = 409
+
+
+class SECT283K1(EllipticCurve):
+ name = "sect283k1"
+ key_size = 283
+
+
+class SECT233K1(EllipticCurve):
+ name = "sect233k1"
+ key_size = 233
+
+
+class SECT163K1(EllipticCurve):
+ name = "sect163k1"
+ key_size = 163
+
+
+class SECP521R1(EllipticCurve):
+ name = "secp521r1"
+ key_size = 521
+
+
+class SECP384R1(EllipticCurve):
+ name = "secp384r1"
+ key_size = 384
+
+
+class SECP256R1(EllipticCurve):
+ name = "secp256r1"
+ key_size = 256
+
+
+class SECP256K1(EllipticCurve):
+ name = "secp256k1"
+ key_size = 256
+
+
+class SECP224R1(EllipticCurve):
+ name = "secp224r1"
+ key_size = 224
+
+
+class SECP192R1(EllipticCurve):
+ name = "secp192r1"
+ key_size = 192
+
+
+class BrainpoolP256R1(EllipticCurve):
+ name = "brainpoolP256r1"
+ key_size = 256
+
+
+class BrainpoolP384R1(EllipticCurve):
+ name = "brainpoolP384r1"
+ key_size = 384
+
+
+class BrainpoolP512R1(EllipticCurve):
+ name = "brainpoolP512r1"
+ key_size = 512
+
+
+_CURVE_TYPES: typing.Dict[str, typing.Type[EllipticCurve]] = {
+ "prime192v1": SECP192R1,
+ "prime256v1": SECP256R1,
+ "secp192r1": SECP192R1,
+ "secp224r1": SECP224R1,
+ "secp256r1": SECP256R1,
+ "secp384r1": SECP384R1,
+ "secp521r1": SECP521R1,
+ "secp256k1": SECP256K1,
+ "sect163k1": SECT163K1,
+ "sect233k1": SECT233K1,
+ "sect283k1": SECT283K1,
+ "sect409k1": SECT409K1,
+ "sect571k1": SECT571K1,
+ "sect163r2": SECT163R2,
+ "sect233r1": SECT233R1,
+ "sect283r1": SECT283R1,
+ "sect409r1": SECT409R1,
+ "sect571r1": SECT571R1,
+ "brainpoolP256r1": BrainpoolP256R1,
+ "brainpoolP384r1": BrainpoolP384R1,
+ "brainpoolP512r1": BrainpoolP512R1,
+}
+
+
+class ECDSA(EllipticCurveSignatureAlgorithm):
+ def __init__(
+ self,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ):
+ self._algorithm = algorithm
+
+ @property
+ def algorithm(
+ self,
+ ) -> typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm]:
+ return self._algorithm
+
+
+def generate_private_key(
+ curve: EllipticCurve, backend: typing.Any = None
+) -> EllipticCurvePrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.generate_elliptic_curve_private_key(curve)
+
+
+def derive_private_key(
+ private_value: int,
+ curve: EllipticCurve,
+ backend: typing.Any = None,
+) -> EllipticCurvePrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ if not isinstance(private_value, int):
+ raise TypeError("private_value must be an integer type.")
+
+ if private_value <= 0:
+ raise ValueError("private_value must be a positive integer.")
+
+ if not isinstance(curve, EllipticCurve):
+ raise TypeError("curve must provide the EllipticCurve interface.")
+
+ return ossl.derive_elliptic_curve_private_key(private_value, curve)
+
+
+class EllipticCurvePublicNumbers:
+ def __init__(self, x: int, y: int, curve: EllipticCurve):
+ if not isinstance(x, int) or not isinstance(y, int):
+ raise TypeError("x and y must be integers.")
+
+ if not isinstance(curve, EllipticCurve):
+ raise TypeError("curve must provide the EllipticCurve interface.")
+
+ self._y = y
+ self._x = x
+ self._curve = curve
+
+ def public_key(self, backend: typing.Any = None) -> EllipticCurvePublicKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_elliptic_curve_public_numbers(self)
+
+ @property
+ def curve(self) -> EllipticCurve:
+ return self._curve
+
+ @property
+ def x(self) -> int:
+ return self._x
+
+ @property
+ def y(self) -> int:
+ return self._y
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, EllipticCurvePublicNumbers):
+ return NotImplemented
+
+ return (
+ self.x == other.x
+ and self.y == other.y
+ and self.curve.name == other.curve.name
+ and self.curve.key_size == other.curve.key_size
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.x, self.y, self.curve.name, self.curve.key_size))
+
+ def __repr__(self) -> str:
+ return (
+ "<EllipticCurvePublicNumbers(curve={0.curve.name}, x={0.x}, "
+ "y={0.y}>".format(self)
+ )
+
+
+class EllipticCurvePrivateNumbers:
+ def __init__(
+ self, private_value: int, public_numbers: EllipticCurvePublicNumbers
+ ):
+ if not isinstance(private_value, int):
+ raise TypeError("private_value must be an integer.")
+
+ if not isinstance(public_numbers, EllipticCurvePublicNumbers):
+ raise TypeError(
+ "public_numbers must be an EllipticCurvePublicNumbers "
+ "instance."
+ )
+
+ self._private_value = private_value
+ self._public_numbers = public_numbers
+
+ def private_key(
+ self, backend: typing.Any = None
+ ) -> EllipticCurvePrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_elliptic_curve_private_numbers(self)
+
+ @property
+ def private_value(self) -> int:
+ return self._private_value
+
+ @property
+ def public_numbers(self) -> EllipticCurvePublicNumbers:
+ return self._public_numbers
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, EllipticCurvePrivateNumbers):
+ return NotImplemented
+
+ return (
+ self.private_value == other.private_value
+ and self.public_numbers == other.public_numbers
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.private_value, self.public_numbers))
+
+
+class ECDH:
+ pass
+
+
+_OID_TO_CURVE = {
+ EllipticCurveOID.SECP192R1: SECP192R1,
+ EllipticCurveOID.SECP224R1: SECP224R1,
+ EllipticCurveOID.SECP256K1: SECP256K1,
+ EllipticCurveOID.SECP256R1: SECP256R1,
+ EllipticCurveOID.SECP384R1: SECP384R1,
+ EllipticCurveOID.SECP521R1: SECP521R1,
+ EllipticCurveOID.BRAINPOOLP256R1: BrainpoolP256R1,
+ EllipticCurveOID.BRAINPOOLP384R1: BrainpoolP384R1,
+ EllipticCurveOID.BRAINPOOLP512R1: BrainpoolP512R1,
+ EllipticCurveOID.SECT163K1: SECT163K1,
+ EllipticCurveOID.SECT163R2: SECT163R2,
+ EllipticCurveOID.SECT233K1: SECT233K1,
+ EllipticCurveOID.SECT233R1: SECT233R1,
+ EllipticCurveOID.SECT283K1: SECT283K1,
+ EllipticCurveOID.SECT283R1: SECT283R1,
+ EllipticCurveOID.SECT409K1: SECT409K1,
+ EllipticCurveOID.SECT409R1: SECT409R1,
+ EllipticCurveOID.SECT571K1: SECT571K1,
+ EllipticCurveOID.SECT571R1: SECT571R1,
+}
+
+
+def get_curve_for_oid(oid: ObjectIdentifier) -> typing.Type[EllipticCurve]:
+ try:
+ return _OID_TO_CURVE[oid]
+ except KeyError:
+ raise LookupError(
+ "The provided object identifier has no matching elliptic "
+ "curve class"
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed25519.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed25519.py
new file mode 100644
index 0000000000..f26e54d24e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed25519.py
@@ -0,0 +1,118 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization
+
+
+class Ed25519PublicKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def from_public_bytes(cls, data: bytes) -> Ed25519PublicKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed25519_supported():
+ raise UnsupportedAlgorithm(
+ "ed25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+ return backend.ed25519_load_public_bytes(data)
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ The serialized bytes of the public key.
+ """
+
+ @abc.abstractmethod
+ def public_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the public key.
+ Equivalent to public_bytes(Raw, Raw).
+ """
+
+ @abc.abstractmethod
+ def verify(self, signature: bytes, data: bytes) -> None:
+ """
+ Verify the signature.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+if hasattr(rust_openssl, "ed25519"):
+ Ed25519PublicKey.register(rust_openssl.ed25519.Ed25519PublicKey)
+
+
+class Ed25519PrivateKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def generate(cls) -> Ed25519PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed25519_supported():
+ raise UnsupportedAlgorithm(
+ "ed25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+ return backend.ed25519_generate_key()
+
+ @classmethod
+ def from_private_bytes(cls, data: bytes) -> Ed25519PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed25519_supported():
+ raise UnsupportedAlgorithm(
+ "ed25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+ return backend.ed25519_load_private_bytes(data)
+
+ @abc.abstractmethod
+ def public_key(self) -> Ed25519PublicKey:
+ """
+ The Ed25519PublicKey derived from the private key.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ The serialized bytes of the private key.
+ """
+
+ @abc.abstractmethod
+ def private_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the private key.
+ Equivalent to private_bytes(Raw, Raw, NoEncryption()).
+ """
+
+ @abc.abstractmethod
+ def sign(self, data: bytes) -> bytes:
+ """
+ Signs the data.
+ """
+
+
+if hasattr(rust_openssl, "x25519"):
+ Ed25519PrivateKey.register(rust_openssl.ed25519.Ed25519PrivateKey)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed448.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed448.py
new file mode 100644
index 0000000000..a9a34b251b
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/ed448.py
@@ -0,0 +1,117 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization
+
+
+class Ed448PublicKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def from_public_bytes(cls, data: bytes) -> Ed448PublicKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed448_supported():
+ raise UnsupportedAlgorithm(
+ "ed448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+ return backend.ed448_load_public_bytes(data)
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ The serialized bytes of the public key.
+ """
+
+ @abc.abstractmethod
+ def public_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the public key.
+ Equivalent to public_bytes(Raw, Raw).
+ """
+
+ @abc.abstractmethod
+ def verify(self, signature: bytes, data: bytes) -> None:
+ """
+ Verify the signature.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+if hasattr(rust_openssl, "ed448"):
+ Ed448PublicKey.register(rust_openssl.ed448.Ed448PublicKey)
+
+
+class Ed448PrivateKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def generate(cls) -> Ed448PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed448_supported():
+ raise UnsupportedAlgorithm(
+ "ed448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+ return backend.ed448_generate_key()
+
+ @classmethod
+ def from_private_bytes(cls, data: bytes) -> Ed448PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.ed448_supported():
+ raise UnsupportedAlgorithm(
+ "ed448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
+ )
+
+ return backend.ed448_load_private_bytes(data)
+
+ @abc.abstractmethod
+ def public_key(self) -> Ed448PublicKey:
+ """
+ The Ed448PublicKey derived from the private key.
+ """
+
+ @abc.abstractmethod
+ def sign(self, data: bytes) -> bytes:
+ """
+ Signs the data.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ The serialized bytes of the private key.
+ """
+
+ @abc.abstractmethod
+ def private_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the private key.
+ Equivalent to private_bytes(Raw, Raw, NoEncryption()).
+ """
+
+
+if hasattr(rust_openssl, "x448"):
+ Ed448PrivateKey.register(rust_openssl.ed448.Ed448PrivateKey)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/padding.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/padding.py
new file mode 100644
index 0000000000..7198808eff
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/padding.py
@@ -0,0 +1,102 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives._asymmetric import (
+ AsymmetricPadding as AsymmetricPadding,
+)
+from cryptography.hazmat.primitives.asymmetric import rsa
+
+
+class PKCS1v15(AsymmetricPadding):
+ name = "EMSA-PKCS1-v1_5"
+
+
+class _MaxLength:
+ "Sentinel value for `MAX_LENGTH`."
+
+
+class _Auto:
+ "Sentinel value for `AUTO`."
+
+
+class _DigestLength:
+ "Sentinel value for `DIGEST_LENGTH`."
+
+
+class PSS(AsymmetricPadding):
+ MAX_LENGTH = _MaxLength()
+ AUTO = _Auto()
+ DIGEST_LENGTH = _DigestLength()
+ name = "EMSA-PSS"
+ _salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength]
+
+ def __init__(
+ self,
+ mgf: MGF,
+ salt_length: typing.Union[int, _MaxLength, _Auto, _DigestLength],
+ ) -> None:
+ self._mgf = mgf
+
+ if not isinstance(
+ salt_length, (int, _MaxLength, _Auto, _DigestLength)
+ ):
+ raise TypeError(
+ "salt_length must be an integer, MAX_LENGTH, "
+ "DIGEST_LENGTH, or AUTO"
+ )
+
+ if isinstance(salt_length, int) and salt_length < 0:
+ raise ValueError("salt_length must be zero or greater.")
+
+ self._salt_length = salt_length
+
+
+class OAEP(AsymmetricPadding):
+ name = "EME-OAEP"
+
+ def __init__(
+ self,
+ mgf: MGF,
+ algorithm: hashes.HashAlgorithm,
+ label: typing.Optional[bytes],
+ ):
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise TypeError("Expected instance of hashes.HashAlgorithm.")
+
+ self._mgf = mgf
+ self._algorithm = algorithm
+ self._label = label
+
+
+class MGF(metaclass=abc.ABCMeta):
+ _algorithm: hashes.HashAlgorithm
+
+
+class MGF1(MGF):
+ MAX_LENGTH = _MaxLength()
+
+ def __init__(self, algorithm: hashes.HashAlgorithm):
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise TypeError("Expected instance of hashes.HashAlgorithm.")
+
+ self._algorithm = algorithm
+
+
+def calculate_max_pss_salt_length(
+ key: typing.Union[rsa.RSAPrivateKey, rsa.RSAPublicKey],
+ hash_algorithm: hashes.HashAlgorithm,
+) -> int:
+ if not isinstance(key, (rsa.RSAPrivateKey, rsa.RSAPublicKey)):
+ raise TypeError("key must be an RSA public or private key")
+ # bit length - 1 per RFC 3447
+ emlen = (key.key_size + 6) // 8
+ salt_length = emlen - hash_algorithm.digest_size - 2
+ assert salt_length >= 0
+ return salt_length
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/rsa.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/rsa.py
new file mode 100644
index 0000000000..b740f01f7c
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/rsa.py
@@ -0,0 +1,439 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+from math import gcd
+
+from cryptography.hazmat.primitives import _serialization, hashes
+from cryptography.hazmat.primitives._asymmetric import AsymmetricPadding
+from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
+
+
+class RSAPrivateKey(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def decrypt(self, ciphertext: bytes, padding: AsymmetricPadding) -> bytes:
+ """
+ Decrypts the provided ciphertext.
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the public modulus.
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> RSAPublicKey:
+ """
+ The RSAPublicKey associated with this private key.
+ """
+
+ @abc.abstractmethod
+ def sign(
+ self,
+ data: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> bytes:
+ """
+ Signs the data.
+ """
+
+ @abc.abstractmethod
+ def private_numbers(self) -> RSAPrivateNumbers:
+ """
+ Returns an RSAPrivateNumbers.
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+
+RSAPrivateKeyWithSerialization = RSAPrivateKey
+
+
+class RSAPublicKey(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def encrypt(self, plaintext: bytes, padding: AsymmetricPadding) -> bytes:
+ """
+ Encrypts the given plaintext.
+ """
+
+ @property
+ @abc.abstractmethod
+ def key_size(self) -> int:
+ """
+ The bit length of the public modulus.
+ """
+
+ @abc.abstractmethod
+ def public_numbers(self) -> RSAPublicNumbers:
+ """
+ Returns an RSAPublicNumbers
+ """
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ Returns the key serialized as bytes.
+ """
+
+ @abc.abstractmethod
+ def verify(
+ self,
+ signature: bytes,
+ data: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
+ ) -> None:
+ """
+ Verifies the signature of the data.
+ """
+
+ @abc.abstractmethod
+ def recover_data_from_signature(
+ self,
+ signature: bytes,
+ padding: AsymmetricPadding,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+ ) -> bytes:
+ """
+ Recovers the original data from the signature.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+RSAPublicKeyWithSerialization = RSAPublicKey
+
+
+def generate_private_key(
+ public_exponent: int,
+ key_size: int,
+ backend: typing.Any = None,
+) -> RSAPrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ _verify_rsa_parameters(public_exponent, key_size)
+ return ossl.generate_rsa_private_key(public_exponent, key_size)
+
+
+def _verify_rsa_parameters(public_exponent: int, key_size: int) -> None:
+ if public_exponent not in (3, 65537):
+ raise ValueError(
+ "public_exponent must be either 3 (for legacy compatibility) or "
+ "65537. Almost everyone should choose 65537 here!"
+ )
+
+ if key_size < 512:
+ raise ValueError("key_size must be at least 512-bits.")
+
+
+def _check_private_key_components(
+ p: int,
+ q: int,
+ private_exponent: int,
+ dmp1: int,
+ dmq1: int,
+ iqmp: int,
+ public_exponent: int,
+ modulus: int,
+) -> None:
+ if modulus < 3:
+ raise ValueError("modulus must be >= 3.")
+
+ if p >= modulus:
+ raise ValueError("p must be < modulus.")
+
+ if q >= modulus:
+ raise ValueError("q must be < modulus.")
+
+ if dmp1 >= modulus:
+ raise ValueError("dmp1 must be < modulus.")
+
+ if dmq1 >= modulus:
+ raise ValueError("dmq1 must be < modulus.")
+
+ if iqmp >= modulus:
+ raise ValueError("iqmp must be < modulus.")
+
+ if private_exponent >= modulus:
+ raise ValueError("private_exponent must be < modulus.")
+
+ if public_exponent < 3 or public_exponent >= modulus:
+ raise ValueError("public_exponent must be >= 3 and < modulus.")
+
+ if public_exponent & 1 == 0:
+ raise ValueError("public_exponent must be odd.")
+
+ if dmp1 & 1 == 0:
+ raise ValueError("dmp1 must be odd.")
+
+ if dmq1 & 1 == 0:
+ raise ValueError("dmq1 must be odd.")
+
+ if p * q != modulus:
+ raise ValueError("p*q must equal modulus.")
+
+
+def _check_public_key_components(e: int, n: int) -> None:
+ if n < 3:
+ raise ValueError("n must be >= 3.")
+
+ if e < 3 or e >= n:
+ raise ValueError("e must be >= 3 and < n.")
+
+ if e & 1 == 0:
+ raise ValueError("e must be odd.")
+
+
+def _modinv(e: int, m: int) -> int:
+ """
+ Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
+ """
+ x1, x2 = 1, 0
+ a, b = e, m
+ while b > 0:
+ q, r = divmod(a, b)
+ xn = x1 - q * x2
+ a, b, x1, x2 = b, r, x2, xn
+ return x1 % m
+
+
+def rsa_crt_iqmp(p: int, q: int) -> int:
+ """
+ Compute the CRT (q ** -1) % p value from RSA primes p and q.
+ """
+ return _modinv(q, p)
+
+
+def rsa_crt_dmp1(private_exponent: int, p: int) -> int:
+ """
+ Compute the CRT private_exponent % (p - 1) value from the RSA
+ private_exponent (d) and p.
+ """
+ return private_exponent % (p - 1)
+
+
+def rsa_crt_dmq1(private_exponent: int, q: int) -> int:
+ """
+ Compute the CRT private_exponent % (q - 1) value from the RSA
+ private_exponent (d) and q.
+ """
+ return private_exponent % (q - 1)
+
+
+# Controls the number of iterations rsa_recover_prime_factors will perform
+# to obtain the prime factors. Each iteration increments by 2 so the actual
+# maximum attempts is half this number.
+_MAX_RECOVERY_ATTEMPTS = 1000
+
+
+def rsa_recover_prime_factors(
+ n: int, e: int, d: int
+) -> typing.Tuple[int, int]:
+ """
+ Compute factors p and q from the private exponent d. We assume that n has
+ no more than two factors. This function is adapted from code in PyCrypto.
+ """
+ # See 8.2.2(i) in Handbook of Applied Cryptography.
+ ktot = d * e - 1
+ # The quantity d*e-1 is a multiple of phi(n), even,
+ # and can be represented as t*2^s.
+ t = ktot
+ while t % 2 == 0:
+ t = t // 2
+ # Cycle through all multiplicative inverses in Zn.
+ # The algorithm is non-deterministic, but there is a 50% chance
+ # any candidate a leads to successful factoring.
+ # See "Digitalized Signatures and Public Key Functions as Intractable
+ # as Factorization", M. Rabin, 1979
+ spotted = False
+ a = 2
+ while not spotted and a < _MAX_RECOVERY_ATTEMPTS:
+ k = t
+ # Cycle through all values a^{t*2^i}=a^k
+ while k < ktot:
+ cand = pow(a, k, n)
+ # Check if a^k is a non-trivial root of unity (mod n)
+ if cand != 1 and cand != (n - 1) and pow(cand, 2, n) == 1:
+ # We have found a number such that (cand-1)(cand+1)=0 (mod n).
+ # Either of the terms divides n.
+ p = gcd(cand + 1, n)
+ spotted = True
+ break
+ k *= 2
+ # This value was not any good... let's try another!
+ a += 2
+ if not spotted:
+ raise ValueError("Unable to compute factors p and q from exponent d.")
+ # Found !
+ q, r = divmod(n, p)
+ assert r == 0
+ p, q = sorted((p, q), reverse=True)
+ return (p, q)
+
+
+class RSAPrivateNumbers:
+ def __init__(
+ self,
+ p: int,
+ q: int,
+ d: int,
+ dmp1: int,
+ dmq1: int,
+ iqmp: int,
+ public_numbers: RSAPublicNumbers,
+ ):
+ if (
+ not isinstance(p, int)
+ or not isinstance(q, int)
+ or not isinstance(d, int)
+ or not isinstance(dmp1, int)
+ or not isinstance(dmq1, int)
+ or not isinstance(iqmp, int)
+ ):
+ raise TypeError(
+ "RSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must"
+ " all be an integers."
+ )
+
+ if not isinstance(public_numbers, RSAPublicNumbers):
+ raise TypeError(
+ "RSAPrivateNumbers public_numbers must be an RSAPublicNumbers"
+ " instance."
+ )
+
+ self._p = p
+ self._q = q
+ self._d = d
+ self._dmp1 = dmp1
+ self._dmq1 = dmq1
+ self._iqmp = iqmp
+ self._public_numbers = public_numbers
+
+ @property
+ def p(self) -> int:
+ return self._p
+
+ @property
+ def q(self) -> int:
+ return self._q
+
+ @property
+ def d(self) -> int:
+ return self._d
+
+ @property
+ def dmp1(self) -> int:
+ return self._dmp1
+
+ @property
+ def dmq1(self) -> int:
+ return self._dmq1
+
+ @property
+ def iqmp(self) -> int:
+ return self._iqmp
+
+ @property
+ def public_numbers(self) -> RSAPublicNumbers:
+ return self._public_numbers
+
+ def private_key(
+ self,
+ backend: typing.Any = None,
+ *,
+ unsafe_skip_rsa_key_validation: bool = False,
+ ) -> RSAPrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_rsa_private_numbers(
+ self, unsafe_skip_rsa_key_validation
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, RSAPrivateNumbers):
+ return NotImplemented
+
+ return (
+ self.p == other.p
+ and self.q == other.q
+ and self.d == other.d
+ and self.dmp1 == other.dmp1
+ and self.dmq1 == other.dmq1
+ and self.iqmp == other.iqmp
+ and self.public_numbers == other.public_numbers
+ )
+
+ def __hash__(self) -> int:
+ return hash(
+ (
+ self.p,
+ self.q,
+ self.d,
+ self.dmp1,
+ self.dmq1,
+ self.iqmp,
+ self.public_numbers,
+ )
+ )
+
+
+class RSAPublicNumbers:
+ def __init__(self, e: int, n: int):
+ if not isinstance(e, int) or not isinstance(n, int):
+ raise TypeError("RSAPublicNumbers arguments must be integers.")
+
+ self._e = e
+ self._n = n
+
+ @property
+ def e(self) -> int:
+ return self._e
+
+ @property
+ def n(self) -> int:
+ return self._n
+
+ def public_key(self, backend: typing.Any = None) -> RSAPublicKey:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ return ossl.load_rsa_public_numbers(self)
+
+ def __repr__(self) -> str:
+ return "<RSAPublicNumbers(e={0.e}, n={0.n})>".format(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, RSAPublicNumbers):
+ return NotImplemented
+
+ return self.e == other.e and self.n == other.n
+
+ def __hash__(self) -> int:
+ return hash((self.e, self.n))
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/types.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/types.py
new file mode 100644
index 0000000000..1fe4eaf51d
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/types.py
@@ -0,0 +1,111 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.hazmat.primitives.asymmetric import (
+ dh,
+ dsa,
+ ec,
+ ed448,
+ ed25519,
+ rsa,
+ x448,
+ x25519,
+)
+
+# Every asymmetric key type
+PublicKeyTypes = typing.Union[
+ dh.DHPublicKey,
+ dsa.DSAPublicKey,
+ rsa.RSAPublicKey,
+ ec.EllipticCurvePublicKey,
+ ed25519.Ed25519PublicKey,
+ ed448.Ed448PublicKey,
+ x25519.X25519PublicKey,
+ x448.X448PublicKey,
+]
+PUBLIC_KEY_TYPES = PublicKeyTypes
+utils.deprecated(
+ PUBLIC_KEY_TYPES,
+ __name__,
+ "Use PublicKeyTypes instead",
+ utils.DeprecatedIn40,
+ name="PUBLIC_KEY_TYPES",
+)
+# Every asymmetric key type
+PrivateKeyTypes = typing.Union[
+ dh.DHPrivateKey,
+ ed25519.Ed25519PrivateKey,
+ ed448.Ed448PrivateKey,
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ec.EllipticCurvePrivateKey,
+ x25519.X25519PrivateKey,
+ x448.X448PrivateKey,
+]
+PRIVATE_KEY_TYPES = PrivateKeyTypes
+utils.deprecated(
+ PRIVATE_KEY_TYPES,
+ __name__,
+ "Use PrivateKeyTypes instead",
+ utils.DeprecatedIn40,
+ name="PRIVATE_KEY_TYPES",
+)
+# Just the key types we allow to be used for x509 signing. This mirrors
+# the certificate public key types
+CertificateIssuerPrivateKeyTypes = typing.Union[
+ ed25519.Ed25519PrivateKey,
+ ed448.Ed448PrivateKey,
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ec.EllipticCurvePrivateKey,
+]
+CERTIFICATE_PRIVATE_KEY_TYPES = CertificateIssuerPrivateKeyTypes
+utils.deprecated(
+ CERTIFICATE_PRIVATE_KEY_TYPES,
+ __name__,
+ "Use CertificateIssuerPrivateKeyTypes instead",
+ utils.DeprecatedIn40,
+ name="CERTIFICATE_PRIVATE_KEY_TYPES",
+)
+# Just the key types we allow to be used for x509 signing. This mirrors
+# the certificate private key types
+CertificateIssuerPublicKeyTypes = typing.Union[
+ dsa.DSAPublicKey,
+ rsa.RSAPublicKey,
+ ec.EllipticCurvePublicKey,
+ ed25519.Ed25519PublicKey,
+ ed448.Ed448PublicKey,
+]
+CERTIFICATE_ISSUER_PUBLIC_KEY_TYPES = CertificateIssuerPublicKeyTypes
+utils.deprecated(
+ CERTIFICATE_ISSUER_PUBLIC_KEY_TYPES,
+ __name__,
+ "Use CertificateIssuerPublicKeyTypes instead",
+ utils.DeprecatedIn40,
+ name="CERTIFICATE_ISSUER_PUBLIC_KEY_TYPES",
+)
+# This type removes DHPublicKey. x448/x25519 can be a public key
+# but cannot be used in signing so they are allowed here.
+CertificatePublicKeyTypes = typing.Union[
+ dsa.DSAPublicKey,
+ rsa.RSAPublicKey,
+ ec.EllipticCurvePublicKey,
+ ed25519.Ed25519PublicKey,
+ ed448.Ed448PublicKey,
+ x25519.X25519PublicKey,
+ x448.X448PublicKey,
+]
+CERTIFICATE_PUBLIC_KEY_TYPES = CertificatePublicKeyTypes
+utils.deprecated(
+ CERTIFICATE_PUBLIC_KEY_TYPES,
+ __name__,
+ "Use CertificatePublicKeyTypes instead",
+ utils.DeprecatedIn40,
+ name="CERTIFICATE_PUBLIC_KEY_TYPES",
+)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/utils.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/utils.py
new file mode 100644
index 0000000000..826b9567b4
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/utils.py
@@ -0,0 +1,24 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.bindings._rust import asn1
+from cryptography.hazmat.primitives import hashes
+
+decode_dss_signature = asn1.decode_dss_signature
+encode_dss_signature = asn1.encode_dss_signature
+
+
+class Prehashed:
+ def __init__(self, algorithm: hashes.HashAlgorithm):
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise TypeError("Expected instance of HashAlgorithm.")
+
+ self._algorithm = algorithm
+ self._digest_size = algorithm.digest_size
+
+ @property
+ def digest_size(self) -> int:
+ return self._digest_size
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x25519.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x25519.py
new file mode 100644
index 0000000000..699054c968
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x25519.py
@@ -0,0 +1,113 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization
+
+
+class X25519PublicKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def from_public_bytes(cls, data: bytes) -> X25519PublicKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x25519_supported():
+ raise UnsupportedAlgorithm(
+ "X25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+
+ return backend.x25519_load_public_bytes(data)
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ The serialized bytes of the public key.
+ """
+
+ @abc.abstractmethod
+ def public_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the public key.
+ Equivalent to public_bytes(Raw, Raw).
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+# For LibreSSL
+if hasattr(rust_openssl, "x25519"):
+ X25519PublicKey.register(rust_openssl.x25519.X25519PublicKey)
+
+
+class X25519PrivateKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def generate(cls) -> X25519PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x25519_supported():
+ raise UnsupportedAlgorithm(
+ "X25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+ return backend.x25519_generate_key()
+
+ @classmethod
+ def from_private_bytes(cls, data: bytes) -> X25519PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x25519_supported():
+ raise UnsupportedAlgorithm(
+ "X25519 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+
+ return backend.x25519_load_private_bytes(data)
+
+ @abc.abstractmethod
+ def public_key(self) -> X25519PublicKey:
+ """
+ Returns the public key assosciated with this private key
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ The serialized bytes of the private key.
+ """
+
+ @abc.abstractmethod
+ def private_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the private key.
+ Equivalent to private_bytes(Raw, Raw, NoEncryption()).
+ """
+
+ @abc.abstractmethod
+ def exchange(self, peer_public_key: X25519PublicKey) -> bytes:
+ """
+ Performs a key exchange operation using the provided peer's public key.
+ """
+
+
+# For LibreSSL
+if hasattr(rust_openssl, "x25519"):
+ X25519PrivateKey.register(rust_openssl.x25519.X25519PrivateKey)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x448.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x448.py
new file mode 100644
index 0000000000..abf7848550
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/asymmetric/x448.py
@@ -0,0 +1,111 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import _serialization
+
+
+class X448PublicKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def from_public_bytes(cls, data: bytes) -> X448PublicKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x448_supported():
+ raise UnsupportedAlgorithm(
+ "X448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+
+ return backend.x448_load_public_bytes(data)
+
+ @abc.abstractmethod
+ def public_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PublicFormat,
+ ) -> bytes:
+ """
+ The serialized bytes of the public key.
+ """
+
+ @abc.abstractmethod
+ def public_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the public key.
+ Equivalent to public_bytes(Raw, Raw).
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+
+if hasattr(rust_openssl, "x448"):
+ X448PublicKey.register(rust_openssl.x448.X448PublicKey)
+
+
+class X448PrivateKey(metaclass=abc.ABCMeta):
+ @classmethod
+ def generate(cls) -> X448PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x448_supported():
+ raise UnsupportedAlgorithm(
+ "X448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+ return backend.x448_generate_key()
+
+ @classmethod
+ def from_private_bytes(cls, data: bytes) -> X448PrivateKey:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ if not backend.x448_supported():
+ raise UnsupportedAlgorithm(
+ "X448 is not supported by this version of OpenSSL.",
+ _Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM,
+ )
+
+ return backend.x448_load_private_bytes(data)
+
+ @abc.abstractmethod
+ def public_key(self) -> X448PublicKey:
+ """
+ Returns the public key associated with this private key
+ """
+
+ @abc.abstractmethod
+ def private_bytes(
+ self,
+ encoding: _serialization.Encoding,
+ format: _serialization.PrivateFormat,
+ encryption_algorithm: _serialization.KeySerializationEncryption,
+ ) -> bytes:
+ """
+ The serialized bytes of the private key.
+ """
+
+ @abc.abstractmethod
+ def private_bytes_raw(self) -> bytes:
+ """
+ The raw bytes of the private key.
+ Equivalent to private_bytes(Raw, Raw, NoEncryption()).
+ """
+
+ @abc.abstractmethod
+ def exchange(self, peer_public_key: X448PublicKey) -> bytes:
+ """
+ Performs a key exchange operation using the provided peer's public key.
+ """
+
+
+if hasattr(rust_openssl, "x448"):
+ X448PrivateKey.register(rust_openssl.x448.X448PrivateKey)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/__init__.py
new file mode 100644
index 0000000000..cc88fbf2c4
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/__init__.py
@@ -0,0 +1,27 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.primitives._cipheralgorithm import (
+ BlockCipherAlgorithm,
+ CipherAlgorithm,
+)
+from cryptography.hazmat.primitives.ciphers.base import (
+ AEADCipherContext,
+ AEADDecryptionContext,
+ AEADEncryptionContext,
+ Cipher,
+ CipherContext,
+)
+
+__all__ = [
+ "Cipher",
+ "CipherAlgorithm",
+ "BlockCipherAlgorithm",
+ "CipherContext",
+ "AEADCipherContext",
+ "AEADDecryptionContext",
+ "AEADEncryptionContext",
+]
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/aead.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/aead.py
new file mode 100644
index 0000000000..957b2d221b
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/aead.py
@@ -0,0 +1,378 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import os
+import typing
+
+from cryptography import exceptions, utils
+from cryptography.hazmat.backends.openssl import aead
+from cryptography.hazmat.backends.openssl.backend import backend
+from cryptography.hazmat.bindings._rust import FixedPool
+
+
+class ChaCha20Poly1305:
+ _MAX_SIZE = 2**31 - 1
+
+ def __init__(self, key: bytes):
+ if not backend.aead_cipher_supported(self):
+ raise exceptions.UnsupportedAlgorithm(
+ "ChaCha20Poly1305 is not supported by this version of OpenSSL",
+ exceptions._Reasons.UNSUPPORTED_CIPHER,
+ )
+ utils._check_byteslike("key", key)
+
+ if len(key) != 32:
+ raise ValueError("ChaCha20Poly1305 key must be 32 bytes.")
+
+ self._key = key
+ self._pool = FixedPool(self._create_fn)
+
+ @classmethod
+ def generate_key(cls) -> bytes:
+ return os.urandom(32)
+
+ def _create_fn(self):
+ return aead._aead_create_ctx(backend, self, self._key)
+
+ def encrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
+ # This is OverflowError to match what cffi would raise
+ raise OverflowError(
+ "Data or associated data too long. Max 2**31 - 1 bytes"
+ )
+
+ self._check_params(nonce, data, associated_data)
+ with self._pool.acquire() as ctx:
+ return aead._encrypt(
+ backend, self, nonce, data, [associated_data], 16, ctx
+ )
+
+ def decrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ self._check_params(nonce, data, associated_data)
+ with self._pool.acquire() as ctx:
+ return aead._decrypt(
+ backend, self, nonce, data, [associated_data], 16, ctx
+ )
+
+ def _check_params(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: bytes,
+ ) -> None:
+ utils._check_byteslike("nonce", nonce)
+ utils._check_byteslike("data", data)
+ utils._check_byteslike("associated_data", associated_data)
+ if len(nonce) != 12:
+ raise ValueError("Nonce must be 12 bytes")
+
+
+class AESCCM:
+ _MAX_SIZE = 2**31 - 1
+
+ def __init__(self, key: bytes, tag_length: int = 16):
+ utils._check_byteslike("key", key)
+ if len(key) not in (16, 24, 32):
+ raise ValueError("AESCCM key must be 128, 192, or 256 bits.")
+
+ self._key = key
+ if not isinstance(tag_length, int):
+ raise TypeError("tag_length must be an integer")
+
+ if tag_length not in (4, 6, 8, 10, 12, 14, 16):
+ raise ValueError("Invalid tag_length")
+
+ self._tag_length = tag_length
+
+ if not backend.aead_cipher_supported(self):
+ raise exceptions.UnsupportedAlgorithm(
+ "AESCCM is not supported by this version of OpenSSL",
+ exceptions._Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ @classmethod
+ def generate_key(cls, bit_length: int) -> bytes:
+ if not isinstance(bit_length, int):
+ raise TypeError("bit_length must be an integer")
+
+ if bit_length not in (128, 192, 256):
+ raise ValueError("bit_length must be 128, 192, or 256")
+
+ return os.urandom(bit_length // 8)
+
+ def encrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
+ # This is OverflowError to match what cffi would raise
+ raise OverflowError(
+ "Data or associated data too long. Max 2**31 - 1 bytes"
+ )
+
+ self._check_params(nonce, data, associated_data)
+ self._validate_lengths(nonce, len(data))
+ return aead._encrypt(
+ backend, self, nonce, data, [associated_data], self._tag_length
+ )
+
+ def decrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ self._check_params(nonce, data, associated_data)
+ return aead._decrypt(
+ backend, self, nonce, data, [associated_data], self._tag_length
+ )
+
+ def _validate_lengths(self, nonce: bytes, data_len: int) -> None:
+ # For information about computing this, see
+ # https://tools.ietf.org/html/rfc3610#section-2.1
+ l_val = 15 - len(nonce)
+ if 2 ** (8 * l_val) < data_len:
+ raise ValueError("Data too long for nonce")
+
+ def _check_params(
+ self, nonce: bytes, data: bytes, associated_data: bytes
+ ) -> None:
+ utils._check_byteslike("nonce", nonce)
+ utils._check_byteslike("data", data)
+ utils._check_byteslike("associated_data", associated_data)
+ if not 7 <= len(nonce) <= 13:
+ raise ValueError("Nonce must be between 7 and 13 bytes")
+
+
+class AESGCM:
+ _MAX_SIZE = 2**31 - 1
+
+ def __init__(self, key: bytes):
+ utils._check_byteslike("key", key)
+ if len(key) not in (16, 24, 32):
+ raise ValueError("AESGCM key must be 128, 192, or 256 bits.")
+
+ self._key = key
+
+ @classmethod
+ def generate_key(cls, bit_length: int) -> bytes:
+ if not isinstance(bit_length, int):
+ raise TypeError("bit_length must be an integer")
+
+ if bit_length not in (128, 192, 256):
+ raise ValueError("bit_length must be 128, 192, or 256")
+
+ return os.urandom(bit_length // 8)
+
+ def encrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
+ # This is OverflowError to match what cffi would raise
+ raise OverflowError(
+ "Data or associated data too long. Max 2**31 - 1 bytes"
+ )
+
+ self._check_params(nonce, data, associated_data)
+ return aead._encrypt(backend, self, nonce, data, [associated_data], 16)
+
+ def decrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ self._check_params(nonce, data, associated_data)
+ return aead._decrypt(backend, self, nonce, data, [associated_data], 16)
+
+ def _check_params(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: bytes,
+ ) -> None:
+ utils._check_byteslike("nonce", nonce)
+ utils._check_byteslike("data", data)
+ utils._check_byteslike("associated_data", associated_data)
+ if len(nonce) < 8 or len(nonce) > 128:
+ raise ValueError("Nonce must be between 8 and 128 bytes")
+
+
+class AESOCB3:
+ _MAX_SIZE = 2**31 - 1
+
+ def __init__(self, key: bytes):
+ utils._check_byteslike("key", key)
+ if len(key) not in (16, 24, 32):
+ raise ValueError("AESOCB3 key must be 128, 192, or 256 bits.")
+
+ self._key = key
+
+ if not backend.aead_cipher_supported(self):
+ raise exceptions.UnsupportedAlgorithm(
+ "OCB3 is not supported by this version of OpenSSL",
+ exceptions._Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ @classmethod
+ def generate_key(cls, bit_length: int) -> bytes:
+ if not isinstance(bit_length, int):
+ raise TypeError("bit_length must be an integer")
+
+ if bit_length not in (128, 192, 256):
+ raise ValueError("bit_length must be 128, 192, or 256")
+
+ return os.urandom(bit_length // 8)
+
+ def encrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
+ # This is OverflowError to match what cffi would raise
+ raise OverflowError(
+ "Data or associated data too long. Max 2**31 - 1 bytes"
+ )
+
+ self._check_params(nonce, data, associated_data)
+ return aead._encrypt(backend, self, nonce, data, [associated_data], 16)
+
+ def decrypt(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: typing.Optional[bytes],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = b""
+
+ self._check_params(nonce, data, associated_data)
+ return aead._decrypt(backend, self, nonce, data, [associated_data], 16)
+
+ def _check_params(
+ self,
+ nonce: bytes,
+ data: bytes,
+ associated_data: bytes,
+ ) -> None:
+ utils._check_byteslike("nonce", nonce)
+ utils._check_byteslike("data", data)
+ utils._check_byteslike("associated_data", associated_data)
+ if len(nonce) < 12 or len(nonce) > 15:
+ raise ValueError("Nonce must be between 12 and 15 bytes")
+
+
+class AESSIV:
+ _MAX_SIZE = 2**31 - 1
+
+ def __init__(self, key: bytes):
+ utils._check_byteslike("key", key)
+ if len(key) not in (32, 48, 64):
+ raise ValueError("AESSIV key must be 256, 384, or 512 bits.")
+
+ self._key = key
+
+ if not backend.aead_cipher_supported(self):
+ raise exceptions.UnsupportedAlgorithm(
+ "AES-SIV is not supported by this version of OpenSSL",
+ exceptions._Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ @classmethod
+ def generate_key(cls, bit_length: int) -> bytes:
+ if not isinstance(bit_length, int):
+ raise TypeError("bit_length must be an integer")
+
+ if bit_length not in (256, 384, 512):
+ raise ValueError("bit_length must be 256, 384, or 512")
+
+ return os.urandom(bit_length // 8)
+
+ def encrypt(
+ self,
+ data: bytes,
+ associated_data: typing.Optional[typing.List[bytes]],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = []
+
+ self._check_params(data, associated_data)
+
+ if len(data) > self._MAX_SIZE or any(
+ len(ad) > self._MAX_SIZE for ad in associated_data
+ ):
+ # This is OverflowError to match what cffi would raise
+ raise OverflowError(
+ "Data or associated data too long. Max 2**31 - 1 bytes"
+ )
+
+ return aead._encrypt(backend, self, b"", data, associated_data, 16)
+
+ def decrypt(
+ self,
+ data: bytes,
+ associated_data: typing.Optional[typing.List[bytes]],
+ ) -> bytes:
+ if associated_data is None:
+ associated_data = []
+
+ self._check_params(data, associated_data)
+
+ return aead._decrypt(backend, self, b"", data, associated_data, 16)
+
+ def _check_params(
+ self,
+ data: bytes,
+ associated_data: typing.List[bytes],
+ ) -> None:
+ utils._check_byteslike("data", data)
+ if len(data) == 0:
+ raise ValueError("data must not be zero length")
+
+ if not isinstance(associated_data, list):
+ raise TypeError(
+ "associated_data must be a list of bytes-like objects or None"
+ )
+ for x in associated_data:
+ utils._check_byteslike("associated_data elements", x)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/algorithms.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/algorithms.py
new file mode 100644
index 0000000000..4bfc5d840d
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/algorithms.py
@@ -0,0 +1,228 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography import utils
+from cryptography.hazmat.primitives.ciphers import (
+ BlockCipherAlgorithm,
+ CipherAlgorithm,
+)
+
+
+def _verify_key_size(algorithm: CipherAlgorithm, key: bytes) -> bytes:
+ # Verify that the key is instance of bytes
+ utils._check_byteslike("key", key)
+
+ # Verify that the key size matches the expected key size
+ if len(key) * 8 not in algorithm.key_sizes:
+ raise ValueError(
+ "Invalid key size ({}) for {}.".format(
+ len(key) * 8, algorithm.name
+ )
+ )
+ return key
+
+
+class AES(BlockCipherAlgorithm):
+ name = "AES"
+ block_size = 128
+ # 512 added to support AES-256-XTS, which uses 512-bit keys
+ key_sizes = frozenset([128, 192, 256, 512])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+class AES128(BlockCipherAlgorithm):
+ name = "AES"
+ block_size = 128
+ key_sizes = frozenset([128])
+ key_size = 128
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+
+class AES256(BlockCipherAlgorithm):
+ name = "AES"
+ block_size = 128
+ key_sizes = frozenset([256])
+ key_size = 256
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+
+class Camellia(BlockCipherAlgorithm):
+ name = "camellia"
+ block_size = 128
+ key_sizes = frozenset([128, 192, 256])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+class TripleDES(BlockCipherAlgorithm):
+ name = "3DES"
+ block_size = 64
+ key_sizes = frozenset([64, 128, 192])
+
+ def __init__(self, key: bytes):
+ if len(key) == 8:
+ key += key + key
+ elif len(key) == 16:
+ key += key[:8]
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+class Blowfish(BlockCipherAlgorithm):
+ name = "Blowfish"
+ block_size = 64
+ key_sizes = frozenset(range(32, 449, 8))
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+_BlowfishInternal = Blowfish
+utils.deprecated(
+ Blowfish,
+ __name__,
+ "Blowfish has been deprecated",
+ utils.DeprecatedIn37,
+ name="Blowfish",
+)
+
+
+class CAST5(BlockCipherAlgorithm):
+ name = "CAST5"
+ block_size = 64
+ key_sizes = frozenset(range(40, 129, 8))
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+_CAST5Internal = CAST5
+utils.deprecated(
+ CAST5,
+ __name__,
+ "CAST5 has been deprecated",
+ utils.DeprecatedIn37,
+ name="CAST5",
+)
+
+
+class ARC4(CipherAlgorithm):
+ name = "RC4"
+ key_sizes = frozenset([40, 56, 64, 80, 128, 160, 192, 256])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+class IDEA(BlockCipherAlgorithm):
+ name = "IDEA"
+ block_size = 64
+ key_sizes = frozenset([128])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+_IDEAInternal = IDEA
+utils.deprecated(
+ IDEA,
+ __name__,
+ "IDEA has been deprecated",
+ utils.DeprecatedIn37,
+ name="IDEA",
+)
+
+
+class SEED(BlockCipherAlgorithm):
+ name = "SEED"
+ block_size = 128
+ key_sizes = frozenset([128])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+_SEEDInternal = SEED
+utils.deprecated(
+ SEED,
+ __name__,
+ "SEED has been deprecated",
+ utils.DeprecatedIn37,
+ name="SEED",
+)
+
+
+class ChaCha20(CipherAlgorithm):
+ name = "ChaCha20"
+ key_sizes = frozenset([256])
+
+ def __init__(self, key: bytes, nonce: bytes):
+ self.key = _verify_key_size(self, key)
+ utils._check_byteslike("nonce", nonce)
+
+ if len(nonce) != 16:
+ raise ValueError("nonce must be 128-bits (16 bytes)")
+
+ self._nonce = nonce
+
+ @property
+ def nonce(self) -> bytes:
+ return self._nonce
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
+
+
+class SM4(BlockCipherAlgorithm):
+ name = "SM4"
+ block_size = 128
+ key_sizes = frozenset([128])
+
+ def __init__(self, key: bytes):
+ self.key = _verify_key_size(self, key)
+
+ @property
+ def key_size(self) -> int:
+ return len(self.key) * 8
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/base.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/base.py
new file mode 100644
index 0000000000..38a2ebbe08
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/base.py
@@ -0,0 +1,269 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography.exceptions import (
+ AlreadyFinalized,
+ AlreadyUpdated,
+ NotYetFinalized,
+)
+from cryptography.hazmat.primitives._cipheralgorithm import CipherAlgorithm
+from cryptography.hazmat.primitives.ciphers import modes
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.ciphers import (
+ _CipherContext as _BackendCipherContext,
+ )
+
+
+class CipherContext(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def update(self, data: bytes) -> bytes:
+ """
+ Processes the provided bytes through the cipher and returns the results
+ as bytes.
+ """
+
+ @abc.abstractmethod
+ def update_into(self, data: bytes, buf: bytes) -> int:
+ """
+ Processes the provided bytes and writes the resulting data into the
+ provided buffer. Returns the number of bytes written.
+ """
+
+ @abc.abstractmethod
+ def finalize(self) -> bytes:
+ """
+ Returns the results of processing the final block as bytes.
+ """
+
+
+class AEADCipherContext(CipherContext, metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def authenticate_additional_data(self, data: bytes) -> None:
+ """
+ Authenticates the provided bytes.
+ """
+
+
+class AEADDecryptionContext(AEADCipherContext, metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def finalize_with_tag(self, tag: bytes) -> bytes:
+ """
+ Returns the results of processing the final block as bytes and allows
+ delayed passing of the authentication tag.
+ """
+
+
+class AEADEncryptionContext(AEADCipherContext, metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def tag(self) -> bytes:
+ """
+ Returns tag bytes. This is only available after encryption is
+ finalized.
+ """
+
+
+Mode = typing.TypeVar(
+ "Mode", bound=typing.Optional[modes.Mode], covariant=True
+)
+
+
+class Cipher(typing.Generic[Mode]):
+ def __init__(
+ self,
+ algorithm: CipherAlgorithm,
+ mode: Mode,
+ backend: typing.Any = None,
+ ) -> None:
+ if not isinstance(algorithm, CipherAlgorithm):
+ raise TypeError("Expected interface of CipherAlgorithm.")
+
+ if mode is not None:
+ # mypy needs this assert to narrow the type from our generic
+ # type. Maybe it won't some time in the future.
+ assert isinstance(mode, modes.Mode)
+ mode.validate_for_algorithm(algorithm)
+
+ self.algorithm = algorithm
+ self.mode = mode
+
+ @typing.overload
+ def encryptor(
+ self: Cipher[modes.ModeWithAuthenticationTag],
+ ) -> AEADEncryptionContext:
+ ...
+
+ @typing.overload
+ def encryptor(
+ self: _CIPHER_TYPE,
+ ) -> CipherContext:
+ ...
+
+ def encryptor(self):
+ if isinstance(self.mode, modes.ModeWithAuthenticationTag):
+ if self.mode.tag is not None:
+ raise ValueError(
+ "Authentication tag must be None when encrypting."
+ )
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ ctx = backend.create_symmetric_encryption_ctx(
+ self.algorithm, self.mode
+ )
+ return self._wrap_ctx(ctx, encrypt=True)
+
+ @typing.overload
+ def decryptor(
+ self: Cipher[modes.ModeWithAuthenticationTag],
+ ) -> AEADDecryptionContext:
+ ...
+
+ @typing.overload
+ def decryptor(
+ self: _CIPHER_TYPE,
+ ) -> CipherContext:
+ ...
+
+ def decryptor(self):
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ ctx = backend.create_symmetric_decryption_ctx(
+ self.algorithm, self.mode
+ )
+ return self._wrap_ctx(ctx, encrypt=False)
+
+ def _wrap_ctx(
+ self, ctx: _BackendCipherContext, encrypt: bool
+ ) -> typing.Union[
+ AEADEncryptionContext, AEADDecryptionContext, CipherContext
+ ]:
+ if isinstance(self.mode, modes.ModeWithAuthenticationTag):
+ if encrypt:
+ return _AEADEncryptionContext(ctx)
+ else:
+ return _AEADDecryptionContext(ctx)
+ else:
+ return _CipherContext(ctx)
+
+
+_CIPHER_TYPE = Cipher[
+ typing.Union[
+ modes.ModeWithNonce,
+ modes.ModeWithTweak,
+ None,
+ modes.ECB,
+ modes.ModeWithInitializationVector,
+ ]
+]
+
+
+class _CipherContext(CipherContext):
+ _ctx: typing.Optional[_BackendCipherContext]
+
+ def __init__(self, ctx: _BackendCipherContext) -> None:
+ self._ctx = ctx
+
+ def update(self, data: bytes) -> bytes:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ return self._ctx.update(data)
+
+ def update_into(self, data: bytes, buf: bytes) -> int:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ return self._ctx.update_into(data, buf)
+
+ def finalize(self) -> bytes:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ data = self._ctx.finalize()
+ self._ctx = None
+ return data
+
+
+class _AEADCipherContext(AEADCipherContext):
+ _ctx: typing.Optional[_BackendCipherContext]
+ _tag: typing.Optional[bytes]
+
+ def __init__(self, ctx: _BackendCipherContext) -> None:
+ self._ctx = ctx
+ self._bytes_processed = 0
+ self._aad_bytes_processed = 0
+ self._tag = None
+ self._updated = False
+
+ def _check_limit(self, data_size: int) -> None:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ self._updated = True
+ self._bytes_processed += data_size
+ if self._bytes_processed > self._ctx._mode._MAX_ENCRYPTED_BYTES:
+ raise ValueError(
+ "{} has a maximum encrypted byte limit of {}".format(
+ self._ctx._mode.name, self._ctx._mode._MAX_ENCRYPTED_BYTES
+ )
+ )
+
+ def update(self, data: bytes) -> bytes:
+ self._check_limit(len(data))
+ # mypy needs this assert even though _check_limit already checked
+ assert self._ctx is not None
+ return self._ctx.update(data)
+
+ def update_into(self, data: bytes, buf: bytes) -> int:
+ self._check_limit(len(data))
+ # mypy needs this assert even though _check_limit already checked
+ assert self._ctx is not None
+ return self._ctx.update_into(data, buf)
+
+ def finalize(self) -> bytes:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ data = self._ctx.finalize()
+ self._tag = self._ctx.tag
+ self._ctx = None
+ return data
+
+ def authenticate_additional_data(self, data: bytes) -> None:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ if self._updated:
+ raise AlreadyUpdated("Update has been called on this context.")
+
+ self._aad_bytes_processed += len(data)
+ if self._aad_bytes_processed > self._ctx._mode._MAX_AAD_BYTES:
+ raise ValueError(
+ "{} has a maximum AAD byte limit of {}".format(
+ self._ctx._mode.name, self._ctx._mode._MAX_AAD_BYTES
+ )
+ )
+
+ self._ctx.authenticate_additional_data(data)
+
+
+class _AEADDecryptionContext(_AEADCipherContext, AEADDecryptionContext):
+ def finalize_with_tag(self, tag: bytes) -> bytes:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ data = self._ctx.finalize_with_tag(tag)
+ self._tag = self._ctx.tag
+ self._ctx = None
+ return data
+
+
+class _AEADEncryptionContext(_AEADCipherContext, AEADEncryptionContext):
+ @property
+ def tag(self) -> bytes:
+ if self._ctx is not None:
+ raise NotYetFinalized(
+ "You must finalize encryption before " "getting the tag."
+ )
+ assert self._tag is not None
+ return self._tag
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/modes.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/modes.py
new file mode 100644
index 0000000000..d8ea1888d6
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/ciphers/modes.py
@@ -0,0 +1,274 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
+from cryptography.hazmat.primitives._cipheralgorithm import (
+ BlockCipherAlgorithm,
+ CipherAlgorithm,
+)
+from cryptography.hazmat.primitives.ciphers import algorithms
+
+
+class Mode(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def name(self) -> str:
+ """
+ A string naming this mode (e.g. "ECB", "CBC").
+ """
+
+ @abc.abstractmethod
+ def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
+ """
+ Checks that all the necessary invariants of this (mode, algorithm)
+ combination are met.
+ """
+
+
+class ModeWithInitializationVector(Mode, metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def initialization_vector(self) -> bytes:
+ """
+ The value of the initialization vector for this mode as bytes.
+ """
+
+
+class ModeWithTweak(Mode, metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def tweak(self) -> bytes:
+ """
+ The value of the tweak for this mode as bytes.
+ """
+
+
+class ModeWithNonce(Mode, metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def nonce(self) -> bytes:
+ """
+ The value of the nonce for this mode as bytes.
+ """
+
+
+class ModeWithAuthenticationTag(Mode, metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def tag(self) -> typing.Optional[bytes]:
+ """
+ The value of the tag supplied to the constructor of this mode.
+ """
+
+
+def _check_aes_key_length(self: Mode, algorithm: CipherAlgorithm) -> None:
+ if algorithm.key_size > 256 and algorithm.name == "AES":
+ raise ValueError(
+ "Only 128, 192, and 256 bit keys are allowed for this AES mode"
+ )
+
+
+def _check_iv_length(
+ self: ModeWithInitializationVector, algorithm: BlockCipherAlgorithm
+) -> None:
+ if len(self.initialization_vector) * 8 != algorithm.block_size:
+ raise ValueError(
+ "Invalid IV size ({}) for {}.".format(
+ len(self.initialization_vector), self.name
+ )
+ )
+
+
+def _check_nonce_length(
+ nonce: bytes, name: str, algorithm: CipherAlgorithm
+) -> None:
+ if not isinstance(algorithm, BlockCipherAlgorithm):
+ raise UnsupportedAlgorithm(
+ f"{name} requires a block cipher algorithm",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+ if len(nonce) * 8 != algorithm.block_size:
+ raise ValueError(f"Invalid nonce size ({len(nonce)}) for {name}.")
+
+
+def _check_iv_and_key_length(
+ self: ModeWithInitializationVector, algorithm: CipherAlgorithm
+) -> None:
+ if not isinstance(algorithm, BlockCipherAlgorithm):
+ raise UnsupportedAlgorithm(
+ f"{self} requires a block cipher algorithm",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+ _check_aes_key_length(self, algorithm)
+ _check_iv_length(self, algorithm)
+
+
+class CBC(ModeWithInitializationVector):
+ name = "CBC"
+
+ def __init__(self, initialization_vector: bytes):
+ utils._check_byteslike("initialization_vector", initialization_vector)
+ self._initialization_vector = initialization_vector
+
+ @property
+ def initialization_vector(self) -> bytes:
+ return self._initialization_vector
+
+ validate_for_algorithm = _check_iv_and_key_length
+
+
+class XTS(ModeWithTweak):
+ name = "XTS"
+
+ def __init__(self, tweak: bytes):
+ utils._check_byteslike("tweak", tweak)
+
+ if len(tweak) != 16:
+ raise ValueError("tweak must be 128-bits (16 bytes)")
+
+ self._tweak = tweak
+
+ @property
+ def tweak(self) -> bytes:
+ return self._tweak
+
+ def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
+ if isinstance(algorithm, (algorithms.AES128, algorithms.AES256)):
+ raise TypeError(
+ "The AES128 and AES256 classes do not support XTS, please use "
+ "the standard AES class instead."
+ )
+
+ if algorithm.key_size not in (256, 512):
+ raise ValueError(
+ "The XTS specification requires a 256-bit key for AES-128-XTS"
+ " and 512-bit key for AES-256-XTS"
+ )
+
+
+class ECB(Mode):
+ name = "ECB"
+
+ validate_for_algorithm = _check_aes_key_length
+
+
+class OFB(ModeWithInitializationVector):
+ name = "OFB"
+
+ def __init__(self, initialization_vector: bytes):
+ utils._check_byteslike("initialization_vector", initialization_vector)
+ self._initialization_vector = initialization_vector
+
+ @property
+ def initialization_vector(self) -> bytes:
+ return self._initialization_vector
+
+ validate_for_algorithm = _check_iv_and_key_length
+
+
+class CFB(ModeWithInitializationVector):
+ name = "CFB"
+
+ def __init__(self, initialization_vector: bytes):
+ utils._check_byteslike("initialization_vector", initialization_vector)
+ self._initialization_vector = initialization_vector
+
+ @property
+ def initialization_vector(self) -> bytes:
+ return self._initialization_vector
+
+ validate_for_algorithm = _check_iv_and_key_length
+
+
+class CFB8(ModeWithInitializationVector):
+ name = "CFB8"
+
+ def __init__(self, initialization_vector: bytes):
+ utils._check_byteslike("initialization_vector", initialization_vector)
+ self._initialization_vector = initialization_vector
+
+ @property
+ def initialization_vector(self) -> bytes:
+ return self._initialization_vector
+
+ validate_for_algorithm = _check_iv_and_key_length
+
+
+class CTR(ModeWithNonce):
+ name = "CTR"
+
+ def __init__(self, nonce: bytes):
+ utils._check_byteslike("nonce", nonce)
+ self._nonce = nonce
+
+ @property
+ def nonce(self) -> bytes:
+ return self._nonce
+
+ def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
+ _check_aes_key_length(self, algorithm)
+ _check_nonce_length(self.nonce, self.name, algorithm)
+
+
+class GCM(ModeWithInitializationVector, ModeWithAuthenticationTag):
+ name = "GCM"
+ _MAX_ENCRYPTED_BYTES = (2**39 - 256) // 8
+ _MAX_AAD_BYTES = (2**64) // 8
+
+ def __init__(
+ self,
+ initialization_vector: bytes,
+ tag: typing.Optional[bytes] = None,
+ min_tag_length: int = 16,
+ ):
+ # OpenSSL 3.0.0 constrains GCM IVs to [64, 1024] bits inclusive
+ # This is a sane limit anyway so we'll enforce it here.
+ utils._check_byteslike("initialization_vector", initialization_vector)
+ if len(initialization_vector) < 8 or len(initialization_vector) > 128:
+ raise ValueError(
+ "initialization_vector must be between 8 and 128 bytes (64 "
+ "and 1024 bits)."
+ )
+ self._initialization_vector = initialization_vector
+ if tag is not None:
+ utils._check_bytes("tag", tag)
+ if min_tag_length < 4:
+ raise ValueError("min_tag_length must be >= 4")
+ if len(tag) < min_tag_length:
+ raise ValueError(
+ "Authentication tag must be {} bytes or longer.".format(
+ min_tag_length
+ )
+ )
+ self._tag = tag
+ self._min_tag_length = min_tag_length
+
+ @property
+ def tag(self) -> typing.Optional[bytes]:
+ return self._tag
+
+ @property
+ def initialization_vector(self) -> bytes:
+ return self._initialization_vector
+
+ def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
+ _check_aes_key_length(self, algorithm)
+ if not isinstance(algorithm, BlockCipherAlgorithm):
+ raise UnsupportedAlgorithm(
+ "GCM requires a block cipher algorithm",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+ block_size_bytes = algorithm.block_size // 8
+ if self._tag is not None and len(self._tag) > block_size_bytes:
+ raise ValueError(
+ "Authentication tag cannot be more than {} bytes.".format(
+ block_size_bytes
+ )
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/cmac.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/cmac.py
new file mode 100644
index 0000000000..8aa1d791ac
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/cmac.py
@@ -0,0 +1,65 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized
+from cryptography.hazmat.primitives import ciphers
+
+if typing.TYPE_CHECKING:
+ from cryptography.hazmat.backends.openssl.cmac import _CMACContext
+
+
+class CMAC:
+ _ctx: typing.Optional[_CMACContext]
+ _algorithm: ciphers.BlockCipherAlgorithm
+
+ def __init__(
+ self,
+ algorithm: ciphers.BlockCipherAlgorithm,
+ backend: typing.Any = None,
+ ctx: typing.Optional[_CMACContext] = None,
+ ) -> None:
+ if not isinstance(algorithm, ciphers.BlockCipherAlgorithm):
+ raise TypeError("Expected instance of BlockCipherAlgorithm.")
+ self._algorithm = algorithm
+
+ if ctx is None:
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ self._ctx = ossl.create_cmac_ctx(self._algorithm)
+ else:
+ self._ctx = ctx
+
+ def update(self, data: bytes) -> None:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ utils._check_bytes("data", data)
+ self._ctx.update(data)
+
+ def finalize(self) -> bytes:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ digest = self._ctx.finalize()
+ self._ctx = None
+ return digest
+
+ def verify(self, signature: bytes) -> None:
+ utils._check_bytes("signature", signature)
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ ctx, self._ctx = self._ctx, None
+ ctx.verify(signature)
+
+ def copy(self) -> CMAC:
+ if self._ctx is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ return CMAC(self._algorithm, ctx=self._ctx.copy())
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/constant_time.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/constant_time.py
new file mode 100644
index 0000000000..3975c7147e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/constant_time.py
@@ -0,0 +1,14 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import hmac
+
+
+def bytes_eq(a: bytes, b: bytes) -> bool:
+ if not isinstance(a, bytes) or not isinstance(b, bytes):
+ raise TypeError("a and b must be bytes.")
+
+ return hmac.compare_digest(a, b)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hashes.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hashes.py
new file mode 100644
index 0000000000..b6a7ff140e
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hashes.py
@@ -0,0 +1,243 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+
+__all__ = [
+ "HashAlgorithm",
+ "HashContext",
+ "Hash",
+ "ExtendableOutputFunction",
+ "SHA1",
+ "SHA512_224",
+ "SHA512_256",
+ "SHA224",
+ "SHA256",
+ "SHA384",
+ "SHA512",
+ "SHA3_224",
+ "SHA3_256",
+ "SHA3_384",
+ "SHA3_512",
+ "SHAKE128",
+ "SHAKE256",
+ "MD5",
+ "BLAKE2b",
+ "BLAKE2s",
+ "SM3",
+]
+
+
+class HashAlgorithm(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def name(self) -> str:
+ """
+ A string naming this algorithm (e.g. "sha256", "md5").
+ """
+
+ @property
+ @abc.abstractmethod
+ def digest_size(self) -> int:
+ """
+ The size of the resulting digest in bytes.
+ """
+
+ @property
+ @abc.abstractmethod
+ def block_size(self) -> typing.Optional[int]:
+ """
+ The internal block size of the hash function, or None if the hash
+ function does not use blocks internally (e.g. SHA3).
+ """
+
+
+class HashContext(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def algorithm(self) -> HashAlgorithm:
+ """
+ A HashAlgorithm that will be used by this context.
+ """
+
+ @abc.abstractmethod
+ def update(self, data: bytes) -> None:
+ """
+ Processes the provided bytes through the hash.
+ """
+
+ @abc.abstractmethod
+ def finalize(self) -> bytes:
+ """
+ Finalizes the hash context and returns the hash digest as bytes.
+ """
+
+ @abc.abstractmethod
+ def copy(self) -> HashContext:
+ """
+ Return a HashContext that is a copy of the current context.
+ """
+
+
+Hash = rust_openssl.hashes.Hash
+HashContext.register(Hash)
+
+
+class ExtendableOutputFunction(metaclass=abc.ABCMeta):
+ """
+ An interface for extendable output functions.
+ """
+
+
+class SHA1(HashAlgorithm):
+ name = "sha1"
+ digest_size = 20
+ block_size = 64
+
+
+class SHA512_224(HashAlgorithm): # noqa: N801
+ name = "sha512-224"
+ digest_size = 28
+ block_size = 128
+
+
+class SHA512_256(HashAlgorithm): # noqa: N801
+ name = "sha512-256"
+ digest_size = 32
+ block_size = 128
+
+
+class SHA224(HashAlgorithm):
+ name = "sha224"
+ digest_size = 28
+ block_size = 64
+
+
+class SHA256(HashAlgorithm):
+ name = "sha256"
+ digest_size = 32
+ block_size = 64
+
+
+class SHA384(HashAlgorithm):
+ name = "sha384"
+ digest_size = 48
+ block_size = 128
+
+
+class SHA512(HashAlgorithm):
+ name = "sha512"
+ digest_size = 64
+ block_size = 128
+
+
+class SHA3_224(HashAlgorithm): # noqa: N801
+ name = "sha3-224"
+ digest_size = 28
+ block_size = None
+
+
+class SHA3_256(HashAlgorithm): # noqa: N801
+ name = "sha3-256"
+ digest_size = 32
+ block_size = None
+
+
+class SHA3_384(HashAlgorithm): # noqa: N801
+ name = "sha3-384"
+ digest_size = 48
+ block_size = None
+
+
+class SHA3_512(HashAlgorithm): # noqa: N801
+ name = "sha3-512"
+ digest_size = 64
+ block_size = None
+
+
+class SHAKE128(HashAlgorithm, ExtendableOutputFunction):
+ name = "shake128"
+ block_size = None
+
+ def __init__(self, digest_size: int):
+ if not isinstance(digest_size, int):
+ raise TypeError("digest_size must be an integer")
+
+ if digest_size < 1:
+ raise ValueError("digest_size must be a positive integer")
+
+ self._digest_size = digest_size
+
+ @property
+ def digest_size(self) -> int:
+ return self._digest_size
+
+
+class SHAKE256(HashAlgorithm, ExtendableOutputFunction):
+ name = "shake256"
+ block_size = None
+
+ def __init__(self, digest_size: int):
+ if not isinstance(digest_size, int):
+ raise TypeError("digest_size must be an integer")
+
+ if digest_size < 1:
+ raise ValueError("digest_size must be a positive integer")
+
+ self._digest_size = digest_size
+
+ @property
+ def digest_size(self) -> int:
+ return self._digest_size
+
+
+class MD5(HashAlgorithm):
+ name = "md5"
+ digest_size = 16
+ block_size = 64
+
+
+class BLAKE2b(HashAlgorithm):
+ name = "blake2b"
+ _max_digest_size = 64
+ _min_digest_size = 1
+ block_size = 128
+
+ def __init__(self, digest_size: int):
+ if digest_size != 64:
+ raise ValueError("Digest size must be 64")
+
+ self._digest_size = digest_size
+
+ @property
+ def digest_size(self) -> int:
+ return self._digest_size
+
+
+class BLAKE2s(HashAlgorithm):
+ name = "blake2s"
+ block_size = 64
+ _max_digest_size = 32
+ _min_digest_size = 1
+
+ def __init__(self, digest_size: int):
+ if digest_size != 32:
+ raise ValueError("Digest size must be 32")
+
+ self._digest_size = digest_size
+
+ @property
+ def digest_size(self) -> int:
+ return self._digest_size
+
+
+class SM3(HashAlgorithm):
+ name = "sm3"
+ digest_size = 32
+ block_size = 64
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hmac.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hmac.py
new file mode 100644
index 0000000000..a9442d59ab
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/hmac.py
@@ -0,0 +1,13 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import hashes
+
+__all__ = ["HMAC"]
+
+HMAC = rust_openssl.hmac.HMAC
+hashes.HashContext.register(HMAC)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/__init__.py
new file mode 100644
index 0000000000..79bb459f01
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/__init__.py
@@ -0,0 +1,23 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+
+
+class KeyDerivationFunction(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def derive(self, key_material: bytes) -> bytes:
+ """
+ Deterministically generates and returns a new key based on the existing
+ key material.
+ """
+
+ @abc.abstractmethod
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ """
+ Checks whether the key generated by the key material matches the
+ expected derived key. Raises an exception if they do not match.
+ """
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/concatkdf.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/concatkdf.py
new file mode 100644
index 0000000000..d5ea58a945
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/concatkdf.py
@@ -0,0 +1,124 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized, InvalidKey
+from cryptography.hazmat.primitives import constant_time, hashes, hmac
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+
+def _int_to_u32be(n: int) -> bytes:
+ return n.to_bytes(length=4, byteorder="big")
+
+
+def _common_args_checks(
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ otherinfo: typing.Optional[bytes],
+) -> None:
+ max_length = algorithm.digest_size * (2**32 - 1)
+ if length > max_length:
+ raise ValueError(f"Cannot derive keys larger than {max_length} bits.")
+ if otherinfo is not None:
+ utils._check_bytes("otherinfo", otherinfo)
+
+
+def _concatkdf_derive(
+ key_material: bytes,
+ length: int,
+ auxfn: typing.Callable[[], hashes.HashContext],
+ otherinfo: bytes,
+) -> bytes:
+ utils._check_byteslike("key_material", key_material)
+ output = [b""]
+ outlen = 0
+ counter = 1
+
+ while length > outlen:
+ h = auxfn()
+ h.update(_int_to_u32be(counter))
+ h.update(key_material)
+ h.update(otherinfo)
+ output.append(h.finalize())
+ outlen += len(output[-1])
+ counter += 1
+
+ return b"".join(output)[:length]
+
+
+class ConcatKDFHash(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ otherinfo: typing.Optional[bytes],
+ backend: typing.Any = None,
+ ):
+ _common_args_checks(algorithm, length, otherinfo)
+ self._algorithm = algorithm
+ self._length = length
+ self._otherinfo: bytes = otherinfo if otherinfo is not None else b""
+
+ self._used = False
+
+ def _hash(self) -> hashes.Hash:
+ return hashes.Hash(self._algorithm)
+
+ def derive(self, key_material: bytes) -> bytes:
+ if self._used:
+ raise AlreadyFinalized
+ self._used = True
+ return _concatkdf_derive(
+ key_material, self._length, self._hash, self._otherinfo
+ )
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
+
+
+class ConcatKDFHMAC(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ salt: typing.Optional[bytes],
+ otherinfo: typing.Optional[bytes],
+ backend: typing.Any = None,
+ ):
+ _common_args_checks(algorithm, length, otherinfo)
+ self._algorithm = algorithm
+ self._length = length
+ self._otherinfo: bytes = otherinfo if otherinfo is not None else b""
+
+ if algorithm.block_size is None:
+ raise TypeError(f"{algorithm.name} is unsupported for ConcatKDF")
+
+ if salt is None:
+ salt = b"\x00" * algorithm.block_size
+ else:
+ utils._check_bytes("salt", salt)
+
+ self._salt = salt
+
+ self._used = False
+
+ def _hmac(self) -> hmac.HMAC:
+ return hmac.HMAC(self._salt, self._algorithm)
+
+ def derive(self, key_material: bytes) -> bytes:
+ if self._used:
+ raise AlreadyFinalized
+ self._used = True
+ return _concatkdf_derive(
+ key_material, self._length, self._hmac, self._otherinfo
+ )
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/hkdf.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/hkdf.py
new file mode 100644
index 0000000000..d476894436
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/hkdf.py
@@ -0,0 +1,101 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized, InvalidKey
+from cryptography.hazmat.primitives import constant_time, hashes, hmac
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+
+class HKDF(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ salt: typing.Optional[bytes],
+ info: typing.Optional[bytes],
+ backend: typing.Any = None,
+ ):
+ self._algorithm = algorithm
+
+ if salt is None:
+ salt = b"\x00" * self._algorithm.digest_size
+ else:
+ utils._check_bytes("salt", salt)
+
+ self._salt = salt
+
+ self._hkdf_expand = HKDFExpand(self._algorithm, length, info)
+
+ def _extract(self, key_material: bytes) -> bytes:
+ h = hmac.HMAC(self._salt, self._algorithm)
+ h.update(key_material)
+ return h.finalize()
+
+ def derive(self, key_material: bytes) -> bytes:
+ utils._check_byteslike("key_material", key_material)
+ return self._hkdf_expand.derive(self._extract(key_material))
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
+
+
+class HKDFExpand(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ info: typing.Optional[bytes],
+ backend: typing.Any = None,
+ ):
+ self._algorithm = algorithm
+
+ max_length = 255 * algorithm.digest_size
+
+ if length > max_length:
+ raise ValueError(
+ f"Cannot derive keys larger than {max_length} octets."
+ )
+
+ self._length = length
+
+ if info is None:
+ info = b""
+ else:
+ utils._check_bytes("info", info)
+
+ self._info = info
+
+ self._used = False
+
+ def _expand(self, key_material: bytes) -> bytes:
+ output = [b""]
+ counter = 1
+
+ while self._algorithm.digest_size * (len(output) - 1) < self._length:
+ h = hmac.HMAC(key_material, self._algorithm)
+ h.update(output[-1])
+ h.update(self._info)
+ h.update(bytes([counter]))
+ output.append(h.finalize())
+ counter += 1
+
+ return b"".join(output)[: self._length]
+
+ def derive(self, key_material: bytes) -> bytes:
+ utils._check_byteslike("key_material", key_material)
+ if self._used:
+ raise AlreadyFinalized
+
+ self._used = True
+ return self._expand(key_material)
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/kbkdf.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/kbkdf.py
new file mode 100644
index 0000000000..967763828f
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/kbkdf.py
@@ -0,0 +1,299 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import (
+ AlreadyFinalized,
+ InvalidKey,
+ UnsupportedAlgorithm,
+ _Reasons,
+)
+from cryptography.hazmat.primitives import (
+ ciphers,
+ cmac,
+ constant_time,
+ hashes,
+ hmac,
+)
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+
+class Mode(utils.Enum):
+ CounterMode = "ctr"
+
+
+class CounterLocation(utils.Enum):
+ BeforeFixed = "before_fixed"
+ AfterFixed = "after_fixed"
+ MiddleFixed = "middle_fixed"
+
+
+class _KBKDFDeriver:
+ def __init__(
+ self,
+ prf: typing.Callable,
+ mode: Mode,
+ length: int,
+ rlen: int,
+ llen: typing.Optional[int],
+ location: CounterLocation,
+ break_location: typing.Optional[int],
+ label: typing.Optional[bytes],
+ context: typing.Optional[bytes],
+ fixed: typing.Optional[bytes],
+ ):
+ assert callable(prf)
+
+ if not isinstance(mode, Mode):
+ raise TypeError("mode must be of type Mode")
+
+ if not isinstance(location, CounterLocation):
+ raise TypeError("location must be of type CounterLocation")
+
+ if break_location is None and location is CounterLocation.MiddleFixed:
+ raise ValueError("Please specify a break_location")
+
+ if (
+ break_location is not None
+ and location != CounterLocation.MiddleFixed
+ ):
+ raise ValueError(
+ "break_location is ignored when location is not"
+ " CounterLocation.MiddleFixed"
+ )
+
+ if break_location is not None and not isinstance(break_location, int):
+ raise TypeError("break_location must be an integer")
+
+ if break_location is not None and break_location < 0:
+ raise ValueError("break_location must be a positive integer")
+
+ if (label or context) and fixed:
+ raise ValueError(
+ "When supplying fixed data, " "label and context are ignored."
+ )
+
+ if rlen is None or not self._valid_byte_length(rlen):
+ raise ValueError("rlen must be between 1 and 4")
+
+ if llen is None and fixed is None:
+ raise ValueError("Please specify an llen")
+
+ if llen is not None and not isinstance(llen, int):
+ raise TypeError("llen must be an integer")
+
+ if label is None:
+ label = b""
+
+ if context is None:
+ context = b""
+
+ utils._check_bytes("label", label)
+ utils._check_bytes("context", context)
+ self._prf = prf
+ self._mode = mode
+ self._length = length
+ self._rlen = rlen
+ self._llen = llen
+ self._location = location
+ self._break_location = break_location
+ self._label = label
+ self._context = context
+ self._used = False
+ self._fixed_data = fixed
+
+ @staticmethod
+ def _valid_byte_length(value: int) -> bool:
+ if not isinstance(value, int):
+ raise TypeError("value must be of type int")
+
+ value_bin = utils.int_to_bytes(1, value)
+ if not 1 <= len(value_bin) <= 4:
+ return False
+ return True
+
+ def derive(self, key_material: bytes, prf_output_size: int) -> bytes:
+ if self._used:
+ raise AlreadyFinalized
+
+ utils._check_byteslike("key_material", key_material)
+ self._used = True
+
+ # inverse floor division (equivalent to ceiling)
+ rounds = -(-self._length // prf_output_size)
+
+ output = [b""]
+
+ # For counter mode, the number of iterations shall not be
+ # larger than 2^r-1, where r <= 32 is the binary length of the counter
+ # This ensures that the counter values used as an input to the
+ # PRF will not repeat during a particular call to the KDF function.
+ r_bin = utils.int_to_bytes(1, self._rlen)
+ if rounds > pow(2, len(r_bin) * 8) - 1:
+ raise ValueError("There are too many iterations.")
+
+ fixed = self._generate_fixed_input()
+
+ if self._location == CounterLocation.BeforeFixed:
+ data_before_ctr = b""
+ data_after_ctr = fixed
+ elif self._location == CounterLocation.AfterFixed:
+ data_before_ctr = fixed
+ data_after_ctr = b""
+ else:
+ if isinstance(
+ self._break_location, int
+ ) and self._break_location > len(fixed):
+ raise ValueError("break_location offset > len(fixed)")
+ data_before_ctr = fixed[: self._break_location]
+ data_after_ctr = fixed[self._break_location :]
+
+ for i in range(1, rounds + 1):
+ h = self._prf(key_material)
+
+ counter = utils.int_to_bytes(i, self._rlen)
+ input_data = data_before_ctr + counter + data_after_ctr
+
+ h.update(input_data)
+
+ output.append(h.finalize())
+
+ return b"".join(output)[: self._length]
+
+ def _generate_fixed_input(self) -> bytes:
+ if self._fixed_data and isinstance(self._fixed_data, bytes):
+ return self._fixed_data
+
+ l_val = utils.int_to_bytes(self._length * 8, self._llen)
+
+ return b"".join([self._label, b"\x00", self._context, l_val])
+
+
+class KBKDFHMAC(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ mode: Mode,
+ length: int,
+ rlen: int,
+ llen: typing.Optional[int],
+ location: CounterLocation,
+ label: typing.Optional[bytes],
+ context: typing.Optional[bytes],
+ fixed: typing.Optional[bytes],
+ backend: typing.Any = None,
+ *,
+ break_location: typing.Optional[int] = None,
+ ):
+ if not isinstance(algorithm, hashes.HashAlgorithm):
+ raise UnsupportedAlgorithm(
+ "Algorithm supplied is not a supported hash algorithm.",
+ _Reasons.UNSUPPORTED_HASH,
+ )
+
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ if not ossl.hmac_supported(algorithm):
+ raise UnsupportedAlgorithm(
+ "Algorithm supplied is not a supported hmac algorithm.",
+ _Reasons.UNSUPPORTED_HASH,
+ )
+
+ self._algorithm = algorithm
+
+ self._deriver = _KBKDFDeriver(
+ self._prf,
+ mode,
+ length,
+ rlen,
+ llen,
+ location,
+ break_location,
+ label,
+ context,
+ fixed,
+ )
+
+ def _prf(self, key_material: bytes) -> hmac.HMAC:
+ return hmac.HMAC(key_material, self._algorithm)
+
+ def derive(self, key_material: bytes) -> bytes:
+ return self._deriver.derive(key_material, self._algorithm.digest_size)
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
+
+
+class KBKDFCMAC(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm,
+ mode: Mode,
+ length: int,
+ rlen: int,
+ llen: typing.Optional[int],
+ location: CounterLocation,
+ label: typing.Optional[bytes],
+ context: typing.Optional[bytes],
+ fixed: typing.Optional[bytes],
+ backend: typing.Any = None,
+ *,
+ break_location: typing.Optional[int] = None,
+ ):
+ if not issubclass(
+ algorithm, ciphers.BlockCipherAlgorithm
+ ) or not issubclass(algorithm, ciphers.CipherAlgorithm):
+ raise UnsupportedAlgorithm(
+ "Algorithm supplied is not a supported cipher algorithm.",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ self._algorithm = algorithm
+ self._cipher: typing.Optional[ciphers.BlockCipherAlgorithm] = None
+
+ self._deriver = _KBKDFDeriver(
+ self._prf,
+ mode,
+ length,
+ rlen,
+ llen,
+ location,
+ break_location,
+ label,
+ context,
+ fixed,
+ )
+
+ def _prf(self, _: bytes) -> cmac.CMAC:
+ assert self._cipher is not None
+
+ return cmac.CMAC(self._cipher)
+
+ def derive(self, key_material: bytes) -> bytes:
+ self._cipher = self._algorithm(key_material)
+
+ assert self._cipher is not None
+
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ if not ossl.cmac_algorithm_supported(self._cipher):
+ raise UnsupportedAlgorithm(
+ "Algorithm supplied is not a supported cipher algorithm.",
+ _Reasons.UNSUPPORTED_CIPHER,
+ )
+
+ return self._deriver.derive(key_material, self._cipher.block_size // 8)
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/pbkdf2.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/pbkdf2.py
new file mode 100644
index 0000000000..623e1ca7f9
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/pbkdf2.py
@@ -0,0 +1,64 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import (
+ AlreadyFinalized,
+ InvalidKey,
+ UnsupportedAlgorithm,
+ _Reasons,
+)
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import constant_time, hashes
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+
+class PBKDF2HMAC(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ salt: bytes,
+ iterations: int,
+ backend: typing.Any = None,
+ ):
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ if not ossl.pbkdf2_hmac_supported(algorithm):
+ raise UnsupportedAlgorithm(
+ "{} is not supported for PBKDF2 by this backend.".format(
+ algorithm.name
+ ),
+ _Reasons.UNSUPPORTED_HASH,
+ )
+ self._used = False
+ self._algorithm = algorithm
+ self._length = length
+ utils._check_bytes("salt", salt)
+ self._salt = salt
+ self._iterations = iterations
+
+ def derive(self, key_material: bytes) -> bytes:
+ if self._used:
+ raise AlreadyFinalized("PBKDF2 instances can only be used once.")
+ self._used = True
+
+ return rust_openssl.kdf.derive_pbkdf2_hmac(
+ key_material,
+ self._algorithm,
+ self._salt,
+ self._iterations,
+ self._length,
+ )
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ derived_key = self.derive(key_material)
+ if not constant_time.bytes_eq(derived_key, expected_key):
+ raise InvalidKey("Keys do not match.")
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/scrypt.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/scrypt.py
new file mode 100644
index 0000000000..05a4f675b6
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/scrypt.py
@@ -0,0 +1,80 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import sys
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import (
+ AlreadyFinalized,
+ InvalidKey,
+ UnsupportedAlgorithm,
+)
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+from cryptography.hazmat.primitives import constant_time
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+# This is used by the scrypt tests to skip tests that require more memory
+# than the MEM_LIMIT
+_MEM_LIMIT = sys.maxsize // 2
+
+
+class Scrypt(KeyDerivationFunction):
+ def __init__(
+ self,
+ salt: bytes,
+ length: int,
+ n: int,
+ r: int,
+ p: int,
+ backend: typing.Any = None,
+ ):
+ from cryptography.hazmat.backends.openssl.backend import (
+ backend as ossl,
+ )
+
+ if not ossl.scrypt_supported():
+ raise UnsupportedAlgorithm(
+ "This version of OpenSSL does not support scrypt"
+ )
+ self._length = length
+ utils._check_bytes("salt", salt)
+ if n < 2 or (n & (n - 1)) != 0:
+ raise ValueError("n must be greater than 1 and be a power of 2.")
+
+ if r < 1:
+ raise ValueError("r must be greater than or equal to 1.")
+
+ if p < 1:
+ raise ValueError("p must be greater than or equal to 1.")
+
+ self._used = False
+ self._salt = salt
+ self._n = n
+ self._r = r
+ self._p = p
+
+ def derive(self, key_material: bytes) -> bytes:
+ if self._used:
+ raise AlreadyFinalized("Scrypt instances can only be used once.")
+ self._used = True
+
+ utils._check_byteslike("key_material", key_material)
+
+ return rust_openssl.kdf.derive_scrypt(
+ key_material,
+ self._salt,
+ self._n,
+ self._r,
+ self._p,
+ _MEM_LIMIT,
+ self._length,
+ )
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ derived_key = self.derive(key_material)
+ if not constant_time.bytes_eq(derived_key, expected_key):
+ raise InvalidKey("Keys do not match.")
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/x963kdf.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/x963kdf.py
new file mode 100644
index 0000000000..17acc5174b
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/kdf/x963kdf.py
@@ -0,0 +1,61 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized, InvalidKey
+from cryptography.hazmat.primitives import constant_time, hashes
+from cryptography.hazmat.primitives.kdf import KeyDerivationFunction
+
+
+def _int_to_u32be(n: int) -> bytes:
+ return n.to_bytes(length=4, byteorder="big")
+
+
+class X963KDF(KeyDerivationFunction):
+ def __init__(
+ self,
+ algorithm: hashes.HashAlgorithm,
+ length: int,
+ sharedinfo: typing.Optional[bytes],
+ backend: typing.Any = None,
+ ):
+ max_len = algorithm.digest_size * (2**32 - 1)
+ if length > max_len:
+ raise ValueError(f"Cannot derive keys larger than {max_len} bits.")
+ if sharedinfo is not None:
+ utils._check_bytes("sharedinfo", sharedinfo)
+
+ self._algorithm = algorithm
+ self._length = length
+ self._sharedinfo = sharedinfo
+ self._used = False
+
+ def derive(self, key_material: bytes) -> bytes:
+ if self._used:
+ raise AlreadyFinalized
+ self._used = True
+ utils._check_byteslike("key_material", key_material)
+ output = [b""]
+ outlen = 0
+ counter = 1
+
+ while self._length > outlen:
+ h = hashes.Hash(self._algorithm)
+ h.update(key_material)
+ h.update(_int_to_u32be(counter))
+ if self._sharedinfo is not None:
+ h.update(self._sharedinfo)
+ output.append(h.finalize())
+ outlen += len(output[-1])
+ counter += 1
+
+ return b"".join(output)[: self._length]
+
+ def verify(self, key_material: bytes, expected_key: bytes) -> None:
+ if not constant_time.bytes_eq(self.derive(key_material), expected_key):
+ raise InvalidKey
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/keywrap.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/keywrap.py
new file mode 100644
index 0000000000..59b0326c2a
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/keywrap.py
@@ -0,0 +1,177 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.primitives.ciphers import Cipher
+from cryptography.hazmat.primitives.ciphers.algorithms import AES
+from cryptography.hazmat.primitives.ciphers.modes import ECB
+from cryptography.hazmat.primitives.constant_time import bytes_eq
+
+
+def _wrap_core(
+ wrapping_key: bytes,
+ a: bytes,
+ r: typing.List[bytes],
+) -> bytes:
+ # RFC 3394 Key Wrap - 2.2.1 (index method)
+ encryptor = Cipher(AES(wrapping_key), ECB()).encryptor()
+ n = len(r)
+ for j in range(6):
+ for i in range(n):
+ # every encryption operation is a discrete 16 byte chunk (because
+ # AES has a 128-bit block size) and since we're using ECB it is
+ # safe to reuse the encryptor for the entire operation
+ b = encryptor.update(a + r[i])
+ a = (
+ int.from_bytes(b[:8], byteorder="big") ^ ((n * j) + i + 1)
+ ).to_bytes(length=8, byteorder="big")
+ r[i] = b[-8:]
+
+ assert encryptor.finalize() == b""
+
+ return a + b"".join(r)
+
+
+def aes_key_wrap(
+ wrapping_key: bytes,
+ key_to_wrap: bytes,
+ backend: typing.Any = None,
+) -> bytes:
+ if len(wrapping_key) not in [16, 24, 32]:
+ raise ValueError("The wrapping key must be a valid AES key length")
+
+ if len(key_to_wrap) < 16:
+ raise ValueError("The key to wrap must be at least 16 bytes")
+
+ if len(key_to_wrap) % 8 != 0:
+ raise ValueError("The key to wrap must be a multiple of 8 bytes")
+
+ a = b"\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6"
+ r = [key_to_wrap[i : i + 8] for i in range(0, len(key_to_wrap), 8)]
+ return _wrap_core(wrapping_key, a, r)
+
+
+def _unwrap_core(
+ wrapping_key: bytes,
+ a: bytes,
+ r: typing.List[bytes],
+) -> typing.Tuple[bytes, typing.List[bytes]]:
+ # Implement RFC 3394 Key Unwrap - 2.2.2 (index method)
+ decryptor = Cipher(AES(wrapping_key), ECB()).decryptor()
+ n = len(r)
+ for j in reversed(range(6)):
+ for i in reversed(range(n)):
+ atr = (
+ int.from_bytes(a, byteorder="big") ^ ((n * j) + i + 1)
+ ).to_bytes(length=8, byteorder="big") + r[i]
+ # every decryption operation is a discrete 16 byte chunk so
+ # it is safe to reuse the decryptor for the entire operation
+ b = decryptor.update(atr)
+ a = b[:8]
+ r[i] = b[-8:]
+
+ assert decryptor.finalize() == b""
+ return a, r
+
+
+def aes_key_wrap_with_padding(
+ wrapping_key: bytes,
+ key_to_wrap: bytes,
+ backend: typing.Any = None,
+) -> bytes:
+ if len(wrapping_key) not in [16, 24, 32]:
+ raise ValueError("The wrapping key must be a valid AES key length")
+
+ aiv = b"\xA6\x59\x59\xA6" + len(key_to_wrap).to_bytes(
+ length=4, byteorder="big"
+ )
+ # pad the key to wrap if necessary
+ pad = (8 - (len(key_to_wrap) % 8)) % 8
+ key_to_wrap = key_to_wrap + b"\x00" * pad
+ if len(key_to_wrap) == 8:
+ # RFC 5649 - 4.1 - exactly 8 octets after padding
+ encryptor = Cipher(AES(wrapping_key), ECB()).encryptor()
+ b = encryptor.update(aiv + key_to_wrap)
+ assert encryptor.finalize() == b""
+ return b
+ else:
+ r = [key_to_wrap[i : i + 8] for i in range(0, len(key_to_wrap), 8)]
+ return _wrap_core(wrapping_key, aiv, r)
+
+
+def aes_key_unwrap_with_padding(
+ wrapping_key: bytes,
+ wrapped_key: bytes,
+ backend: typing.Any = None,
+) -> bytes:
+ if len(wrapped_key) < 16:
+ raise InvalidUnwrap("Must be at least 16 bytes")
+
+ if len(wrapping_key) not in [16, 24, 32]:
+ raise ValueError("The wrapping key must be a valid AES key length")
+
+ if len(wrapped_key) == 16:
+ # RFC 5649 - 4.2 - exactly two 64-bit blocks
+ decryptor = Cipher(AES(wrapping_key), ECB()).decryptor()
+ out = decryptor.update(wrapped_key)
+ assert decryptor.finalize() == b""
+ a = out[:8]
+ data = out[8:]
+ n = 1
+ else:
+ r = [wrapped_key[i : i + 8] for i in range(0, len(wrapped_key), 8)]
+ encrypted_aiv = r.pop(0)
+ n = len(r)
+ a, r = _unwrap_core(wrapping_key, encrypted_aiv, r)
+ data = b"".join(r)
+
+ # 1) Check that MSB(32,A) = A65959A6.
+ # 2) Check that 8*(n-1) < LSB(32,A) <= 8*n. If so, let
+ # MLI = LSB(32,A).
+ # 3) Let b = (8*n)-MLI, and then check that the rightmost b octets of
+ # the output data are zero.
+ mli = int.from_bytes(a[4:], byteorder="big")
+ b = (8 * n) - mli
+ if (
+ not bytes_eq(a[:4], b"\xa6\x59\x59\xa6")
+ or not 8 * (n - 1) < mli <= 8 * n
+ or (b != 0 and not bytes_eq(data[-b:], b"\x00" * b))
+ ):
+ raise InvalidUnwrap()
+
+ if b == 0:
+ return data
+ else:
+ return data[:-b]
+
+
+def aes_key_unwrap(
+ wrapping_key: bytes,
+ wrapped_key: bytes,
+ backend: typing.Any = None,
+) -> bytes:
+ if len(wrapped_key) < 24:
+ raise InvalidUnwrap("Must be at least 24 bytes")
+
+ if len(wrapped_key) % 8 != 0:
+ raise InvalidUnwrap("The wrapped key must be a multiple of 8 bytes")
+
+ if len(wrapping_key) not in [16, 24, 32]:
+ raise ValueError("The wrapping key must be a valid AES key length")
+
+ aiv = b"\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6"
+ r = [wrapped_key[i : i + 8] for i in range(0, len(wrapped_key), 8)]
+ a = r.pop(0)
+ a, r = _unwrap_core(wrapping_key, a, r)
+ if not bytes_eq(a, aiv):
+ raise InvalidUnwrap()
+
+ return b"".join(r)
+
+
+class InvalidUnwrap(Exception):
+ pass
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/padding.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/padding.py
new file mode 100644
index 0000000000..fde3094b00
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/padding.py
@@ -0,0 +1,225 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import typing
+
+from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized
+from cryptography.hazmat.bindings._rust import (
+ check_ansix923_padding,
+ check_pkcs7_padding,
+)
+
+
+class PaddingContext(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def update(self, data: bytes) -> bytes:
+ """
+ Pads the provided bytes and returns any available data as bytes.
+ """
+
+ @abc.abstractmethod
+ def finalize(self) -> bytes:
+ """
+ Finalize the padding, returns bytes.
+ """
+
+
+def _byte_padding_check(block_size: int) -> None:
+ if not (0 <= block_size <= 2040):
+ raise ValueError("block_size must be in range(0, 2041).")
+
+ if block_size % 8 != 0:
+ raise ValueError("block_size must be a multiple of 8.")
+
+
+def _byte_padding_update(
+ buffer_: typing.Optional[bytes], data: bytes, block_size: int
+) -> typing.Tuple[bytes, bytes]:
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ utils._check_byteslike("data", data)
+
+ buffer_ += bytes(data)
+
+ finished_blocks = len(buffer_) // (block_size // 8)
+
+ result = buffer_[: finished_blocks * (block_size // 8)]
+ buffer_ = buffer_[finished_blocks * (block_size // 8) :]
+
+ return buffer_, result
+
+
+def _byte_padding_pad(
+ buffer_: typing.Optional[bytes],
+ block_size: int,
+ paddingfn: typing.Callable[[int], bytes],
+) -> bytes:
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ pad_size = block_size // 8 - len(buffer_)
+ return buffer_ + paddingfn(pad_size)
+
+
+def _byte_unpadding_update(
+ buffer_: typing.Optional[bytes], data: bytes, block_size: int
+) -> typing.Tuple[bytes, bytes]:
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ utils._check_byteslike("data", data)
+
+ buffer_ += bytes(data)
+
+ finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)
+
+ result = buffer_[: finished_blocks * (block_size // 8)]
+ buffer_ = buffer_[finished_blocks * (block_size // 8) :]
+
+ return buffer_, result
+
+
+def _byte_unpadding_check(
+ buffer_: typing.Optional[bytes],
+ block_size: int,
+ checkfn: typing.Callable[[bytes], int],
+) -> bytes:
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
+
+ if len(buffer_) != block_size // 8:
+ raise ValueError("Invalid padding bytes.")
+
+ valid = checkfn(buffer_)
+
+ if not valid:
+ raise ValueError("Invalid padding bytes.")
+
+ pad_size = buffer_[-1]
+ return buffer_[:-pad_size]
+
+
+class PKCS7:
+ def __init__(self, block_size: int):
+ _byte_padding_check(block_size)
+ self.block_size = block_size
+
+ def padder(self) -> PaddingContext:
+ return _PKCS7PaddingContext(self.block_size)
+
+ def unpadder(self) -> PaddingContext:
+ return _PKCS7UnpaddingContext(self.block_size)
+
+
+class _PKCS7PaddingContext(PaddingContext):
+ _buffer: typing.Optional[bytes]
+
+ def __init__(self, block_size: int):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data: bytes) -> bytes:
+ self._buffer, result = _byte_padding_update(
+ self._buffer, data, self.block_size
+ )
+ return result
+
+ def _padding(self, size: int) -> bytes:
+ return bytes([size]) * size
+
+ def finalize(self) -> bytes:
+ result = _byte_padding_pad(
+ self._buffer, self.block_size, self._padding
+ )
+ self._buffer = None
+ return result
+
+
+class _PKCS7UnpaddingContext(PaddingContext):
+ _buffer: typing.Optional[bytes]
+
+ def __init__(self, block_size: int):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data: bytes) -> bytes:
+ self._buffer, result = _byte_unpadding_update(
+ self._buffer, data, self.block_size
+ )
+ return result
+
+ def finalize(self) -> bytes:
+ result = _byte_unpadding_check(
+ self._buffer, self.block_size, check_pkcs7_padding
+ )
+ self._buffer = None
+ return result
+
+
+class ANSIX923:
+ def __init__(self, block_size: int):
+ _byte_padding_check(block_size)
+ self.block_size = block_size
+
+ def padder(self) -> PaddingContext:
+ return _ANSIX923PaddingContext(self.block_size)
+
+ def unpadder(self) -> PaddingContext:
+ return _ANSIX923UnpaddingContext(self.block_size)
+
+
+class _ANSIX923PaddingContext(PaddingContext):
+ _buffer: typing.Optional[bytes]
+
+ def __init__(self, block_size: int):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data: bytes) -> bytes:
+ self._buffer, result = _byte_padding_update(
+ self._buffer, data, self.block_size
+ )
+ return result
+
+ def _padding(self, size: int) -> bytes:
+ return bytes([0]) * (size - 1) + bytes([size])
+
+ def finalize(self) -> bytes:
+ result = _byte_padding_pad(
+ self._buffer, self.block_size, self._padding
+ )
+ self._buffer = None
+ return result
+
+
+class _ANSIX923UnpaddingContext(PaddingContext):
+ _buffer: typing.Optional[bytes]
+
+ def __init__(self, block_size: int):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data: bytes) -> bytes:
+ self._buffer, result = _byte_unpadding_update(
+ self._buffer, data, self.block_size
+ )
+ return result
+
+ def finalize(self) -> bytes:
+ result = _byte_unpadding_check(
+ self._buffer,
+ self.block_size,
+ check_ansix923_padding,
+ )
+ self._buffer = None
+ return result
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/poly1305.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/poly1305.py
new file mode 100644
index 0000000000..7f5a77a576
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/poly1305.py
@@ -0,0 +1,11 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.bindings._rust import openssl as rust_openssl
+
+__all__ = ["Poly1305"]
+
+Poly1305 = rust_openssl.poly1305.Poly1305
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/__init__.py
new file mode 100644
index 0000000000..b6c9a5cdc5
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/__init__.py
@@ -0,0 +1,63 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat.primitives._serialization import (
+ BestAvailableEncryption,
+ Encoding,
+ KeySerializationEncryption,
+ NoEncryption,
+ ParameterFormat,
+ PrivateFormat,
+ PublicFormat,
+ _KeySerializationEncryption,
+)
+from cryptography.hazmat.primitives.serialization.base import (
+ load_der_parameters,
+ load_der_private_key,
+ load_der_public_key,
+ load_pem_parameters,
+ load_pem_private_key,
+ load_pem_public_key,
+)
+from cryptography.hazmat.primitives.serialization.ssh import (
+ SSHCertificate,
+ SSHCertificateBuilder,
+ SSHCertificateType,
+ SSHCertPrivateKeyTypes,
+ SSHCertPublicKeyTypes,
+ SSHPrivateKeyTypes,
+ SSHPublicKeyTypes,
+ load_ssh_private_key,
+ load_ssh_public_identity,
+ load_ssh_public_key,
+)
+
+__all__ = [
+ "load_der_parameters",
+ "load_der_private_key",
+ "load_der_public_key",
+ "load_pem_parameters",
+ "load_pem_private_key",
+ "load_pem_public_key",
+ "load_ssh_private_key",
+ "load_ssh_public_identity",
+ "load_ssh_public_key",
+ "Encoding",
+ "PrivateFormat",
+ "PublicFormat",
+ "ParameterFormat",
+ "KeySerializationEncryption",
+ "BestAvailableEncryption",
+ "NoEncryption",
+ "_KeySerializationEncryption",
+ "SSHCertificateBuilder",
+ "SSHCertificate",
+ "SSHCertificateType",
+ "SSHCertPublicKeyTypes",
+ "SSHCertPrivateKeyTypes",
+ "SSHPrivateKeyTypes",
+ "SSHPublicKeyTypes",
+]
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/base.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/base.py
new file mode 100644
index 0000000000..18a96ccfd5
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/base.py
@@ -0,0 +1,73 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.primitives.asymmetric import dh
+from cryptography.hazmat.primitives.asymmetric.types import (
+ PrivateKeyTypes,
+ PublicKeyTypes,
+)
+
+
+def load_pem_private_key(
+ data: bytes,
+ password: typing.Optional[bytes],
+ backend: typing.Any = None,
+ *,
+ unsafe_skip_rsa_key_validation: bool = False,
+) -> PrivateKeyTypes:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_pem_private_key(
+ data, password, unsafe_skip_rsa_key_validation
+ )
+
+
+def load_pem_public_key(
+ data: bytes, backend: typing.Any = None
+) -> PublicKeyTypes:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_pem_public_key(data)
+
+
+def load_pem_parameters(
+ data: bytes, backend: typing.Any = None
+) -> dh.DHParameters:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_pem_parameters(data)
+
+
+def load_der_private_key(
+ data: bytes,
+ password: typing.Optional[bytes],
+ backend: typing.Any = None,
+ *,
+ unsafe_skip_rsa_key_validation: bool = False,
+) -> PrivateKeyTypes:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_der_private_key(
+ data, password, unsafe_skip_rsa_key_validation
+ )
+
+
+def load_der_public_key(
+ data: bytes, backend: typing.Any = None
+) -> PublicKeyTypes:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_der_public_key(data)
+
+
+def load_der_parameters(
+ data: bytes, backend: typing.Any = None
+) -> dh.DHParameters:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_der_parameters(data)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs12.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs12.py
new file mode 100644
index 0000000000..27133a3fa8
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs12.py
@@ -0,0 +1,229 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography import x509
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives._serialization import PBES as PBES
+from cryptography.hazmat.primitives.asymmetric import (
+ dsa,
+ ec,
+ ed448,
+ ed25519,
+ rsa,
+)
+from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes
+
+__all__ = [
+ "PBES",
+ "PKCS12PrivateKeyTypes",
+ "PKCS12Certificate",
+ "PKCS12KeyAndCertificates",
+ "load_key_and_certificates",
+ "load_pkcs12",
+ "serialize_key_and_certificates",
+]
+
+PKCS12PrivateKeyTypes = typing.Union[
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ec.EllipticCurvePrivateKey,
+ ed25519.Ed25519PrivateKey,
+ ed448.Ed448PrivateKey,
+]
+
+
+class PKCS12Certificate:
+ def __init__(
+ self,
+ cert: x509.Certificate,
+ friendly_name: typing.Optional[bytes],
+ ):
+ if not isinstance(cert, x509.Certificate):
+ raise TypeError("Expecting x509.Certificate object")
+ if friendly_name is not None and not isinstance(friendly_name, bytes):
+ raise TypeError("friendly_name must be bytes or None")
+ self._cert = cert
+ self._friendly_name = friendly_name
+
+ @property
+ def friendly_name(self) -> typing.Optional[bytes]:
+ return self._friendly_name
+
+ @property
+ def certificate(self) -> x509.Certificate:
+ return self._cert
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PKCS12Certificate):
+ return NotImplemented
+
+ return (
+ self.certificate == other.certificate
+ and self.friendly_name == other.friendly_name
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.certificate, self.friendly_name))
+
+ def __repr__(self) -> str:
+ return "<PKCS12Certificate({}, friendly_name={!r})>".format(
+ self.certificate, self.friendly_name
+ )
+
+
+class PKCS12KeyAndCertificates:
+ def __init__(
+ self,
+ key: typing.Optional[PrivateKeyTypes],
+ cert: typing.Optional[PKCS12Certificate],
+ additional_certs: typing.List[PKCS12Certificate],
+ ):
+ if key is not None and not isinstance(
+ key,
+ (
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ec.EllipticCurvePrivateKey,
+ ed25519.Ed25519PrivateKey,
+ ed448.Ed448PrivateKey,
+ ),
+ ):
+ raise TypeError(
+ "Key must be RSA, DSA, EllipticCurve, ED25519, or ED448"
+ " private key, or None."
+ )
+ if cert is not None and not isinstance(cert, PKCS12Certificate):
+ raise TypeError("cert must be a PKCS12Certificate object or None")
+ if not all(
+ isinstance(add_cert, PKCS12Certificate)
+ for add_cert in additional_certs
+ ):
+ raise TypeError(
+ "all values in additional_certs must be PKCS12Certificate"
+ " objects"
+ )
+ self._key = key
+ self._cert = cert
+ self._additional_certs = additional_certs
+
+ @property
+ def key(self) -> typing.Optional[PrivateKeyTypes]:
+ return self._key
+
+ @property
+ def cert(self) -> typing.Optional[PKCS12Certificate]:
+ return self._cert
+
+ @property
+ def additional_certs(self) -> typing.List[PKCS12Certificate]:
+ return self._additional_certs
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PKCS12KeyAndCertificates):
+ return NotImplemented
+
+ return (
+ self.key == other.key
+ and self.cert == other.cert
+ and self.additional_certs == other.additional_certs
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.key, self.cert, tuple(self.additional_certs)))
+
+ def __repr__(self) -> str:
+ fmt = (
+ "<PKCS12KeyAndCertificates(key={}, cert={}, additional_certs={})>"
+ )
+ return fmt.format(self.key, self.cert, self.additional_certs)
+
+
+def load_key_and_certificates(
+ data: bytes,
+ password: typing.Optional[bytes],
+ backend: typing.Any = None,
+) -> typing.Tuple[
+ typing.Optional[PrivateKeyTypes],
+ typing.Optional[x509.Certificate],
+ typing.List[x509.Certificate],
+]:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_key_and_certificates_from_pkcs12(data, password)
+
+
+def load_pkcs12(
+ data: bytes,
+ password: typing.Optional[bytes],
+ backend: typing.Any = None,
+) -> PKCS12KeyAndCertificates:
+ from cryptography.hazmat.backends.openssl.backend import backend as ossl
+
+ return ossl.load_pkcs12(data, password)
+
+
+_PKCS12CATypes = typing.Union[
+ x509.Certificate,
+ PKCS12Certificate,
+]
+
+
+def serialize_key_and_certificates(
+ name: typing.Optional[bytes],
+ key: typing.Optional[PKCS12PrivateKeyTypes],
+ cert: typing.Optional[x509.Certificate],
+ cas: typing.Optional[typing.Iterable[_PKCS12CATypes]],
+ encryption_algorithm: serialization.KeySerializationEncryption,
+) -> bytes:
+ if key is not None and not isinstance(
+ key,
+ (
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ec.EllipticCurvePrivateKey,
+ ed25519.Ed25519PrivateKey,
+ ed448.Ed448PrivateKey,
+ ),
+ ):
+ raise TypeError(
+ "Key must be RSA, DSA, EllipticCurve, ED25519, or ED448"
+ " private key, or None."
+ )
+ if cert is not None and not isinstance(cert, x509.Certificate):
+ raise TypeError("cert must be a certificate or None")
+
+ if cas is not None:
+ cas = list(cas)
+ if not all(
+ isinstance(
+ val,
+ (
+ x509.Certificate,
+ PKCS12Certificate,
+ ),
+ )
+ for val in cas
+ ):
+ raise TypeError("all values in cas must be certificates")
+
+ if not isinstance(
+ encryption_algorithm, serialization.KeySerializationEncryption
+ ):
+ raise TypeError(
+ "Key encryption algorithm must be a "
+ "KeySerializationEncryption instance"
+ )
+
+ if key is None and cert is None and not cas:
+ raise ValueError("You must supply at least one of key, cert, or cas")
+
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ return backend.serialize_key_and_certificates_to_pkcs12(
+ name, key, cert, cas, encryption_algorithm
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs7.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs7.py
new file mode 100644
index 0000000000..9998bcaa11
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/pkcs7.py
@@ -0,0 +1,235 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import email.base64mime
+import email.generator
+import email.message
+import email.policy
+import io
+import typing
+
+from cryptography import utils, x509
+from cryptography.hazmat.bindings._rust import pkcs7 as rust_pkcs7
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives.asymmetric import ec, rsa
+from cryptography.utils import _check_byteslike
+
+
+def load_pem_pkcs7_certificates(data: bytes) -> typing.List[x509.Certificate]:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ return backend.load_pem_pkcs7_certificates(data)
+
+
+def load_der_pkcs7_certificates(data: bytes) -> typing.List[x509.Certificate]:
+ from cryptography.hazmat.backends.openssl.backend import backend
+
+ return backend.load_der_pkcs7_certificates(data)
+
+
+def serialize_certificates(
+ certs: typing.List[x509.Certificate],
+ encoding: serialization.Encoding,
+) -> bytes:
+ return rust_pkcs7.serialize_certificates(certs, encoding)
+
+
+PKCS7HashTypes = typing.Union[
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+]
+
+PKCS7PrivateKeyTypes = typing.Union[
+ rsa.RSAPrivateKey, ec.EllipticCurvePrivateKey
+]
+
+
+class PKCS7Options(utils.Enum):
+ Text = "Add text/plain MIME type"
+ Binary = "Don't translate input data into canonical MIME format"
+ DetachedSignature = "Don't embed data in the PKCS7 structure"
+ NoCapabilities = "Don't embed SMIME capabilities"
+ NoAttributes = "Don't embed authenticatedAttributes"
+ NoCerts = "Don't embed signer certificate"
+
+
+class PKCS7SignatureBuilder:
+ def __init__(
+ self,
+ data: typing.Optional[bytes] = None,
+ signers: typing.List[
+ typing.Tuple[
+ x509.Certificate,
+ PKCS7PrivateKeyTypes,
+ PKCS7HashTypes,
+ ]
+ ] = [],
+ additional_certs: typing.List[x509.Certificate] = [],
+ ):
+ self._data = data
+ self._signers = signers
+ self._additional_certs = additional_certs
+
+ def set_data(self, data: bytes) -> PKCS7SignatureBuilder:
+ _check_byteslike("data", data)
+ if self._data is not None:
+ raise ValueError("data may only be set once")
+
+ return PKCS7SignatureBuilder(data, self._signers)
+
+ def add_signer(
+ self,
+ certificate: x509.Certificate,
+ private_key: PKCS7PrivateKeyTypes,
+ hash_algorithm: PKCS7HashTypes,
+ ) -> PKCS7SignatureBuilder:
+ if not isinstance(
+ hash_algorithm,
+ (
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+ ),
+ ):
+ raise TypeError(
+ "hash_algorithm must be one of hashes.SHA224, "
+ "SHA256, SHA384, or SHA512"
+ )
+ if not isinstance(certificate, x509.Certificate):
+ raise TypeError("certificate must be a x509.Certificate")
+
+ if not isinstance(
+ private_key, (rsa.RSAPrivateKey, ec.EllipticCurvePrivateKey)
+ ):
+ raise TypeError("Only RSA & EC keys are supported at this time.")
+
+ return PKCS7SignatureBuilder(
+ self._data,
+ self._signers + [(certificate, private_key, hash_algorithm)],
+ )
+
+ def add_certificate(
+ self, certificate: x509.Certificate
+ ) -> PKCS7SignatureBuilder:
+ if not isinstance(certificate, x509.Certificate):
+ raise TypeError("certificate must be a x509.Certificate")
+
+ return PKCS7SignatureBuilder(
+ self._data, self._signers, self._additional_certs + [certificate]
+ )
+
+ def sign(
+ self,
+ encoding: serialization.Encoding,
+ options: typing.Iterable[PKCS7Options],
+ backend: typing.Any = None,
+ ) -> bytes:
+ if len(self._signers) == 0:
+ raise ValueError("Must have at least one signer")
+ if self._data is None:
+ raise ValueError("You must add data to sign")
+ options = list(options)
+ if not all(isinstance(x, PKCS7Options) for x in options):
+ raise ValueError("options must be from the PKCS7Options enum")
+ if encoding not in (
+ serialization.Encoding.PEM,
+ serialization.Encoding.DER,
+ serialization.Encoding.SMIME,
+ ):
+ raise ValueError(
+ "Must be PEM, DER, or SMIME from the Encoding enum"
+ )
+
+ # Text is a meaningless option unless it is accompanied by
+ # DetachedSignature
+ if (
+ PKCS7Options.Text in options
+ and PKCS7Options.DetachedSignature not in options
+ ):
+ raise ValueError(
+ "When passing the Text option you must also pass "
+ "DetachedSignature"
+ )
+
+ if PKCS7Options.Text in options and encoding in (
+ serialization.Encoding.DER,
+ serialization.Encoding.PEM,
+ ):
+ raise ValueError(
+ "The Text option is only available for SMIME serialization"
+ )
+
+ # No attributes implies no capabilities so we'll error if you try to
+ # pass both.
+ if (
+ PKCS7Options.NoAttributes in options
+ and PKCS7Options.NoCapabilities in options
+ ):
+ raise ValueError(
+ "NoAttributes is a superset of NoCapabilities. Do not pass "
+ "both values."
+ )
+
+ return rust_pkcs7.sign_and_serialize(self, encoding, options)
+
+
+def _smime_encode(
+ data: bytes, signature: bytes, micalg: str, text_mode: bool
+) -> bytes:
+ # This function works pretty hard to replicate what OpenSSL does
+ # precisely. For good and for ill.
+
+ m = email.message.Message()
+ m.add_header("MIME-Version", "1.0")
+ m.add_header(
+ "Content-Type",
+ "multipart/signed",
+ protocol="application/x-pkcs7-signature",
+ micalg=micalg,
+ )
+
+ m.preamble = "This is an S/MIME signed message\n"
+
+ msg_part = OpenSSLMimePart()
+ msg_part.set_payload(data)
+ if text_mode:
+ msg_part.add_header("Content-Type", "text/plain")
+ m.attach(msg_part)
+
+ sig_part = email.message.MIMEPart()
+ sig_part.add_header(
+ "Content-Type", "application/x-pkcs7-signature", name="smime.p7s"
+ )
+ sig_part.add_header("Content-Transfer-Encoding", "base64")
+ sig_part.add_header(
+ "Content-Disposition", "attachment", filename="smime.p7s"
+ )
+ sig_part.set_payload(
+ email.base64mime.body_encode(signature, maxlinelen=65)
+ )
+ del sig_part["MIME-Version"]
+ m.attach(sig_part)
+
+ fp = io.BytesIO()
+ g = email.generator.BytesGenerator(
+ fp,
+ maxheaderlen=0,
+ mangle_from_=False,
+ policy=m.policy.clone(linesep="\r\n"),
+ )
+ g.flatten(m)
+ return fp.getvalue()
+
+
+class OpenSSLMimePart(email.message.MIMEPart):
+ # A MIMEPart subclass that replicates OpenSSL's behavior of not including
+ # a newline if there are no headers.
+ def _write_headers(self, generator) -> None:
+ if list(self.raw_items()):
+ generator._write_headers(self)
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/ssh.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/ssh.py
new file mode 100644
index 0000000000..35e53c1028
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/serialization/ssh.py
@@ -0,0 +1,1534 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import binascii
+import enum
+import os
+import re
+import typing
+import warnings
+from base64 import encodebytes as _base64_encode
+from dataclasses import dataclass
+
+from cryptography import utils
+from cryptography.exceptions import UnsupportedAlgorithm
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives.asymmetric import (
+ dsa,
+ ec,
+ ed25519,
+ padding,
+ rsa,
+)
+from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
+from cryptography.hazmat.primitives.ciphers import (
+ AEADDecryptionContext,
+ Cipher,
+ algorithms,
+ modes,
+)
+from cryptography.hazmat.primitives.serialization import (
+ Encoding,
+ KeySerializationEncryption,
+ NoEncryption,
+ PrivateFormat,
+ PublicFormat,
+ _KeySerializationEncryption,
+)
+
+try:
+ from bcrypt import kdf as _bcrypt_kdf
+
+ _bcrypt_supported = True
+except ImportError:
+ _bcrypt_supported = False
+
+ def _bcrypt_kdf(
+ password: bytes,
+ salt: bytes,
+ desired_key_bytes: int,
+ rounds: int,
+ ignore_few_rounds: bool = False,
+ ) -> bytes:
+ raise UnsupportedAlgorithm("Need bcrypt module")
+
+
+_SSH_ED25519 = b"ssh-ed25519"
+_SSH_RSA = b"ssh-rsa"
+_SSH_DSA = b"ssh-dss"
+_ECDSA_NISTP256 = b"ecdsa-sha2-nistp256"
+_ECDSA_NISTP384 = b"ecdsa-sha2-nistp384"
+_ECDSA_NISTP521 = b"ecdsa-sha2-nistp521"
+_CERT_SUFFIX = b"-cert-v01@openssh.com"
+
+# These are not key types, only algorithms, so they cannot appear
+# as a public key type
+_SSH_RSA_SHA256 = b"rsa-sha2-256"
+_SSH_RSA_SHA512 = b"rsa-sha2-512"
+
+_SSH_PUBKEY_RC = re.compile(rb"\A(\S+)[ \t]+(\S+)")
+_SK_MAGIC = b"openssh-key-v1\0"
+_SK_START = b"-----BEGIN OPENSSH PRIVATE KEY-----"
+_SK_END = b"-----END OPENSSH PRIVATE KEY-----"
+_BCRYPT = b"bcrypt"
+_NONE = b"none"
+_DEFAULT_CIPHER = b"aes256-ctr"
+_DEFAULT_ROUNDS = 16
+
+# re is only way to work on bytes-like data
+_PEM_RC = re.compile(_SK_START + b"(.*?)" + _SK_END, re.DOTALL)
+
+# padding for max blocksize
+_PADDING = memoryview(bytearray(range(1, 1 + 16)))
+
+
+@dataclass
+class _SSHCipher:
+ alg: typing.Type[algorithms.AES]
+ key_len: int
+ mode: typing.Union[
+ typing.Type[modes.CTR],
+ typing.Type[modes.CBC],
+ typing.Type[modes.GCM],
+ ]
+ block_len: int
+ iv_len: int
+ tag_len: typing.Optional[int]
+ is_aead: bool
+
+
+# ciphers that are actually used in key wrapping
+_SSH_CIPHERS: typing.Dict[bytes, _SSHCipher] = {
+ b"aes256-ctr": _SSHCipher(
+ alg=algorithms.AES,
+ key_len=32,
+ mode=modes.CTR,
+ block_len=16,
+ iv_len=16,
+ tag_len=None,
+ is_aead=False,
+ ),
+ b"aes256-cbc": _SSHCipher(
+ alg=algorithms.AES,
+ key_len=32,
+ mode=modes.CBC,
+ block_len=16,
+ iv_len=16,
+ tag_len=None,
+ is_aead=False,
+ ),
+ b"aes256-gcm@openssh.com": _SSHCipher(
+ alg=algorithms.AES,
+ key_len=32,
+ mode=modes.GCM,
+ block_len=16,
+ iv_len=12,
+ tag_len=16,
+ is_aead=True,
+ ),
+}
+
+# map local curve name to key type
+_ECDSA_KEY_TYPE = {
+ "secp256r1": _ECDSA_NISTP256,
+ "secp384r1": _ECDSA_NISTP384,
+ "secp521r1": _ECDSA_NISTP521,
+}
+
+
+def _get_ssh_key_type(
+ key: typing.Union[SSHPrivateKeyTypes, SSHPublicKeyTypes]
+) -> bytes:
+ if isinstance(key, ec.EllipticCurvePrivateKey):
+ key_type = _ecdsa_key_type(key.public_key())
+ elif isinstance(key, ec.EllipticCurvePublicKey):
+ key_type = _ecdsa_key_type(key)
+ elif isinstance(key, (rsa.RSAPrivateKey, rsa.RSAPublicKey)):
+ key_type = _SSH_RSA
+ elif isinstance(key, (dsa.DSAPrivateKey, dsa.DSAPublicKey)):
+ key_type = _SSH_DSA
+ elif isinstance(
+ key, (ed25519.Ed25519PrivateKey, ed25519.Ed25519PublicKey)
+ ):
+ key_type = _SSH_ED25519
+ else:
+ raise ValueError("Unsupported key type")
+
+ return key_type
+
+
+def _ecdsa_key_type(public_key: ec.EllipticCurvePublicKey) -> bytes:
+ """Return SSH key_type and curve_name for private key."""
+ curve = public_key.curve
+ if curve.name not in _ECDSA_KEY_TYPE:
+ raise ValueError(
+ f"Unsupported curve for ssh private key: {curve.name!r}"
+ )
+ return _ECDSA_KEY_TYPE[curve.name]
+
+
+def _ssh_pem_encode(
+ data: bytes,
+ prefix: bytes = _SK_START + b"\n",
+ suffix: bytes = _SK_END + b"\n",
+) -> bytes:
+ return b"".join([prefix, _base64_encode(data), suffix])
+
+
+def _check_block_size(data: bytes, block_len: int) -> None:
+ """Require data to be full blocks"""
+ if not data or len(data) % block_len != 0:
+ raise ValueError("Corrupt data: missing padding")
+
+
+def _check_empty(data: bytes) -> None:
+ """All data should have been parsed."""
+ if data:
+ raise ValueError("Corrupt data: unparsed data")
+
+
+def _init_cipher(
+ ciphername: bytes,
+ password: typing.Optional[bytes],
+ salt: bytes,
+ rounds: int,
+) -> Cipher[typing.Union[modes.CBC, modes.CTR, modes.GCM]]:
+ """Generate key + iv and return cipher."""
+ if not password:
+ raise ValueError("Key is password-protected.")
+
+ ciph = _SSH_CIPHERS[ciphername]
+ seed = _bcrypt_kdf(
+ password, salt, ciph.key_len + ciph.iv_len, rounds, True
+ )
+ return Cipher(
+ ciph.alg(seed[: ciph.key_len]),
+ ciph.mode(seed[ciph.key_len :]),
+ )
+
+
+def _get_u32(data: memoryview) -> typing.Tuple[int, memoryview]:
+ """Uint32"""
+ if len(data) < 4:
+ raise ValueError("Invalid data")
+ return int.from_bytes(data[:4], byteorder="big"), data[4:]
+
+
+def _get_u64(data: memoryview) -> typing.Tuple[int, memoryview]:
+ """Uint64"""
+ if len(data) < 8:
+ raise ValueError("Invalid data")
+ return int.from_bytes(data[:8], byteorder="big"), data[8:]
+
+
+def _get_sshstr(data: memoryview) -> typing.Tuple[memoryview, memoryview]:
+ """Bytes with u32 length prefix"""
+ n, data = _get_u32(data)
+ if n > len(data):
+ raise ValueError("Invalid data")
+ return data[:n], data[n:]
+
+
+def _get_mpint(data: memoryview) -> typing.Tuple[int, memoryview]:
+ """Big integer."""
+ val, data = _get_sshstr(data)
+ if val and val[0] > 0x7F:
+ raise ValueError("Invalid data")
+ return int.from_bytes(val, "big"), data
+
+
+def _to_mpint(val: int) -> bytes:
+ """Storage format for signed bigint."""
+ if val < 0:
+ raise ValueError("negative mpint not allowed")
+ if not val:
+ return b""
+ nbytes = (val.bit_length() + 8) // 8
+ return utils.int_to_bytes(val, nbytes)
+
+
+class _FragList:
+ """Build recursive structure without data copy."""
+
+ flist: typing.List[bytes]
+
+ def __init__(
+ self, init: typing.Optional[typing.List[bytes]] = None
+ ) -> None:
+ self.flist = []
+ if init:
+ self.flist.extend(init)
+
+ def put_raw(self, val: bytes) -> None:
+ """Add plain bytes"""
+ self.flist.append(val)
+
+ def put_u32(self, val: int) -> None:
+ """Big-endian uint32"""
+ self.flist.append(val.to_bytes(length=4, byteorder="big"))
+
+ def put_u64(self, val: int) -> None:
+ """Big-endian uint64"""
+ self.flist.append(val.to_bytes(length=8, byteorder="big"))
+
+ def put_sshstr(self, val: typing.Union[bytes, _FragList]) -> None:
+ """Bytes prefixed with u32 length"""
+ if isinstance(val, (bytes, memoryview, bytearray)):
+ self.put_u32(len(val))
+ self.flist.append(val)
+ else:
+ self.put_u32(val.size())
+ self.flist.extend(val.flist)
+
+ def put_mpint(self, val: int) -> None:
+ """Big-endian bigint prefixed with u32 length"""
+ self.put_sshstr(_to_mpint(val))
+
+ def size(self) -> int:
+ """Current number of bytes"""
+ return sum(map(len, self.flist))
+
+ def render(self, dstbuf: memoryview, pos: int = 0) -> int:
+ """Write into bytearray"""
+ for frag in self.flist:
+ flen = len(frag)
+ start, pos = pos, pos + flen
+ dstbuf[start:pos] = frag
+ return pos
+
+ def tobytes(self) -> bytes:
+ """Return as bytes"""
+ buf = memoryview(bytearray(self.size()))
+ self.render(buf)
+ return buf.tobytes()
+
+
+class _SSHFormatRSA:
+ """Format for RSA keys.
+
+ Public:
+ mpint e, n
+ Private:
+ mpint n, e, d, iqmp, p, q
+ """
+
+ def get_public(self, data: memoryview):
+ """RSA public fields"""
+ e, data = _get_mpint(data)
+ n, data = _get_mpint(data)
+ return (e, n), data
+
+ def load_public(
+ self, data: memoryview
+ ) -> typing.Tuple[rsa.RSAPublicKey, memoryview]:
+ """Make RSA public key from data."""
+ (e, n), data = self.get_public(data)
+ public_numbers = rsa.RSAPublicNumbers(e, n)
+ public_key = public_numbers.public_key()
+ return public_key, data
+
+ def load_private(
+ self, data: memoryview, pubfields
+ ) -> typing.Tuple[rsa.RSAPrivateKey, memoryview]:
+ """Make RSA private key from data."""
+ n, data = _get_mpint(data)
+ e, data = _get_mpint(data)
+ d, data = _get_mpint(data)
+ iqmp, data = _get_mpint(data)
+ p, data = _get_mpint(data)
+ q, data = _get_mpint(data)
+
+ if (e, n) != pubfields:
+ raise ValueError("Corrupt data: rsa field mismatch")
+ dmp1 = rsa.rsa_crt_dmp1(d, p)
+ dmq1 = rsa.rsa_crt_dmq1(d, q)
+ public_numbers = rsa.RSAPublicNumbers(e, n)
+ private_numbers = rsa.RSAPrivateNumbers(
+ p, q, d, dmp1, dmq1, iqmp, public_numbers
+ )
+ private_key = private_numbers.private_key()
+ return private_key, data
+
+ def encode_public(
+ self, public_key: rsa.RSAPublicKey, f_pub: _FragList
+ ) -> None:
+ """Write RSA public key"""
+ pubn = public_key.public_numbers()
+ f_pub.put_mpint(pubn.e)
+ f_pub.put_mpint(pubn.n)
+
+ def encode_private(
+ self, private_key: rsa.RSAPrivateKey, f_priv: _FragList
+ ) -> None:
+ """Write RSA private key"""
+ private_numbers = private_key.private_numbers()
+ public_numbers = private_numbers.public_numbers
+
+ f_priv.put_mpint(public_numbers.n)
+ f_priv.put_mpint(public_numbers.e)
+
+ f_priv.put_mpint(private_numbers.d)
+ f_priv.put_mpint(private_numbers.iqmp)
+ f_priv.put_mpint(private_numbers.p)
+ f_priv.put_mpint(private_numbers.q)
+
+
+class _SSHFormatDSA:
+ """Format for DSA keys.
+
+ Public:
+ mpint p, q, g, y
+ Private:
+ mpint p, q, g, y, x
+ """
+
+ def get_public(
+ self, data: memoryview
+ ) -> typing.Tuple[typing.Tuple, memoryview]:
+ """DSA public fields"""
+ p, data = _get_mpint(data)
+ q, data = _get_mpint(data)
+ g, data = _get_mpint(data)
+ y, data = _get_mpint(data)
+ return (p, q, g, y), data
+
+ def load_public(
+ self, data: memoryview
+ ) -> typing.Tuple[dsa.DSAPublicKey, memoryview]:
+ """Make DSA public key from data."""
+ (p, q, g, y), data = self.get_public(data)
+ parameter_numbers = dsa.DSAParameterNumbers(p, q, g)
+ public_numbers = dsa.DSAPublicNumbers(y, parameter_numbers)
+ self._validate(public_numbers)
+ public_key = public_numbers.public_key()
+ return public_key, data
+
+ def load_private(
+ self, data: memoryview, pubfields
+ ) -> typing.Tuple[dsa.DSAPrivateKey, memoryview]:
+ """Make DSA private key from data."""
+ (p, q, g, y), data = self.get_public(data)
+ x, data = _get_mpint(data)
+
+ if (p, q, g, y) != pubfields:
+ raise ValueError("Corrupt data: dsa field mismatch")
+ parameter_numbers = dsa.DSAParameterNumbers(p, q, g)
+ public_numbers = dsa.DSAPublicNumbers(y, parameter_numbers)
+ self._validate(public_numbers)
+ private_numbers = dsa.DSAPrivateNumbers(x, public_numbers)
+ private_key = private_numbers.private_key()
+ return private_key, data
+
+ def encode_public(
+ self, public_key: dsa.DSAPublicKey, f_pub: _FragList
+ ) -> None:
+ """Write DSA public key"""
+ public_numbers = public_key.public_numbers()
+ parameter_numbers = public_numbers.parameter_numbers
+ self._validate(public_numbers)
+
+ f_pub.put_mpint(parameter_numbers.p)
+ f_pub.put_mpint(parameter_numbers.q)
+ f_pub.put_mpint(parameter_numbers.g)
+ f_pub.put_mpint(public_numbers.y)
+
+ def encode_private(
+ self, private_key: dsa.DSAPrivateKey, f_priv: _FragList
+ ) -> None:
+ """Write DSA private key"""
+ self.encode_public(private_key.public_key(), f_priv)
+ f_priv.put_mpint(private_key.private_numbers().x)
+
+ def _validate(self, public_numbers: dsa.DSAPublicNumbers) -> None:
+ parameter_numbers = public_numbers.parameter_numbers
+ if parameter_numbers.p.bit_length() != 1024:
+ raise ValueError("SSH supports only 1024 bit DSA keys")
+
+
+class _SSHFormatECDSA:
+ """Format for ECDSA keys.
+
+ Public:
+ str curve
+ bytes point
+ Private:
+ str curve
+ bytes point
+ mpint secret
+ """
+
+ def __init__(self, ssh_curve_name: bytes, curve: ec.EllipticCurve):
+ self.ssh_curve_name = ssh_curve_name
+ self.curve = curve
+
+ def get_public(
+ self, data: memoryview
+ ) -> typing.Tuple[typing.Tuple, memoryview]:
+ """ECDSA public fields"""
+ curve, data = _get_sshstr(data)
+ point, data = _get_sshstr(data)
+ if curve != self.ssh_curve_name:
+ raise ValueError("Curve name mismatch")
+ if point[0] != 4:
+ raise NotImplementedError("Need uncompressed point")
+ return (curve, point), data
+
+ def load_public(
+ self, data: memoryview
+ ) -> typing.Tuple[ec.EllipticCurvePublicKey, memoryview]:
+ """Make ECDSA public key from data."""
+ (curve_name, point), data = self.get_public(data)
+ public_key = ec.EllipticCurvePublicKey.from_encoded_point(
+ self.curve, point.tobytes()
+ )
+ return public_key, data
+
+ def load_private(
+ self, data: memoryview, pubfields
+ ) -> typing.Tuple[ec.EllipticCurvePrivateKey, memoryview]:
+ """Make ECDSA private key from data."""
+ (curve_name, point), data = self.get_public(data)
+ secret, data = _get_mpint(data)
+
+ if (curve_name, point) != pubfields:
+ raise ValueError("Corrupt data: ecdsa field mismatch")
+ private_key = ec.derive_private_key(secret, self.curve)
+ return private_key, data
+
+ def encode_public(
+ self, public_key: ec.EllipticCurvePublicKey, f_pub: _FragList
+ ) -> None:
+ """Write ECDSA public key"""
+ point = public_key.public_bytes(
+ Encoding.X962, PublicFormat.UncompressedPoint
+ )
+ f_pub.put_sshstr(self.ssh_curve_name)
+ f_pub.put_sshstr(point)
+
+ def encode_private(
+ self, private_key: ec.EllipticCurvePrivateKey, f_priv: _FragList
+ ) -> None:
+ """Write ECDSA private key"""
+ public_key = private_key.public_key()
+ private_numbers = private_key.private_numbers()
+
+ self.encode_public(public_key, f_priv)
+ f_priv.put_mpint(private_numbers.private_value)
+
+
+class _SSHFormatEd25519:
+ """Format for Ed25519 keys.
+
+ Public:
+ bytes point
+ Private:
+ bytes point
+ bytes secret_and_point
+ """
+
+ def get_public(
+ self, data: memoryview
+ ) -> typing.Tuple[typing.Tuple, memoryview]:
+ """Ed25519 public fields"""
+ point, data = _get_sshstr(data)
+ return (point,), data
+
+ def load_public(
+ self, data: memoryview
+ ) -> typing.Tuple[ed25519.Ed25519PublicKey, memoryview]:
+ """Make Ed25519 public key from data."""
+ (point,), data = self.get_public(data)
+ public_key = ed25519.Ed25519PublicKey.from_public_bytes(
+ point.tobytes()
+ )
+ return public_key, data
+
+ def load_private(
+ self, data: memoryview, pubfields
+ ) -> typing.Tuple[ed25519.Ed25519PrivateKey, memoryview]:
+ """Make Ed25519 private key from data."""
+ (point,), data = self.get_public(data)
+ keypair, data = _get_sshstr(data)
+
+ secret = keypair[:32]
+ point2 = keypair[32:]
+ if point != point2 or (point,) != pubfields:
+ raise ValueError("Corrupt data: ed25519 field mismatch")
+ private_key = ed25519.Ed25519PrivateKey.from_private_bytes(secret)
+ return private_key, data
+
+ def encode_public(
+ self, public_key: ed25519.Ed25519PublicKey, f_pub: _FragList
+ ) -> None:
+ """Write Ed25519 public key"""
+ raw_public_key = public_key.public_bytes(
+ Encoding.Raw, PublicFormat.Raw
+ )
+ f_pub.put_sshstr(raw_public_key)
+
+ def encode_private(
+ self, private_key: ed25519.Ed25519PrivateKey, f_priv: _FragList
+ ) -> None:
+ """Write Ed25519 private key"""
+ public_key = private_key.public_key()
+ raw_private_key = private_key.private_bytes(
+ Encoding.Raw, PrivateFormat.Raw, NoEncryption()
+ )
+ raw_public_key = public_key.public_bytes(
+ Encoding.Raw, PublicFormat.Raw
+ )
+ f_keypair = _FragList([raw_private_key, raw_public_key])
+
+ self.encode_public(public_key, f_priv)
+ f_priv.put_sshstr(f_keypair)
+
+
+_KEY_FORMATS = {
+ _SSH_RSA: _SSHFormatRSA(),
+ _SSH_DSA: _SSHFormatDSA(),
+ _SSH_ED25519: _SSHFormatEd25519(),
+ _ECDSA_NISTP256: _SSHFormatECDSA(b"nistp256", ec.SECP256R1()),
+ _ECDSA_NISTP384: _SSHFormatECDSA(b"nistp384", ec.SECP384R1()),
+ _ECDSA_NISTP521: _SSHFormatECDSA(b"nistp521", ec.SECP521R1()),
+}
+
+
+def _lookup_kformat(key_type: bytes):
+ """Return valid format or throw error"""
+ if not isinstance(key_type, bytes):
+ key_type = memoryview(key_type).tobytes()
+ if key_type in _KEY_FORMATS:
+ return _KEY_FORMATS[key_type]
+ raise UnsupportedAlgorithm(f"Unsupported key type: {key_type!r}")
+
+
+SSHPrivateKeyTypes = typing.Union[
+ ec.EllipticCurvePrivateKey,
+ rsa.RSAPrivateKey,
+ dsa.DSAPrivateKey,
+ ed25519.Ed25519PrivateKey,
+]
+
+
+def load_ssh_private_key(
+ data: bytes,
+ password: typing.Optional[bytes],
+ backend: typing.Any = None,
+) -> SSHPrivateKeyTypes:
+ """Load private key from OpenSSH custom encoding."""
+ utils._check_byteslike("data", data)
+ if password is not None:
+ utils._check_bytes("password", password)
+
+ m = _PEM_RC.search(data)
+ if not m:
+ raise ValueError("Not OpenSSH private key format")
+ p1 = m.start(1)
+ p2 = m.end(1)
+ data = binascii.a2b_base64(memoryview(data)[p1:p2])
+ if not data.startswith(_SK_MAGIC):
+ raise ValueError("Not OpenSSH private key format")
+ data = memoryview(data)[len(_SK_MAGIC) :]
+
+ # parse header
+ ciphername, data = _get_sshstr(data)
+ kdfname, data = _get_sshstr(data)
+ kdfoptions, data = _get_sshstr(data)
+ nkeys, data = _get_u32(data)
+ if nkeys != 1:
+ raise ValueError("Only one key supported")
+
+ # load public key data
+ pubdata, data = _get_sshstr(data)
+ pub_key_type, pubdata = _get_sshstr(pubdata)
+ kformat = _lookup_kformat(pub_key_type)
+ pubfields, pubdata = kformat.get_public(pubdata)
+ _check_empty(pubdata)
+
+ if (ciphername, kdfname) != (_NONE, _NONE):
+ ciphername_bytes = ciphername.tobytes()
+ if ciphername_bytes not in _SSH_CIPHERS:
+ raise UnsupportedAlgorithm(
+ f"Unsupported cipher: {ciphername_bytes!r}"
+ )
+ if kdfname != _BCRYPT:
+ raise UnsupportedAlgorithm(f"Unsupported KDF: {kdfname!r}")
+ blklen = _SSH_CIPHERS[ciphername_bytes].block_len
+ tag_len = _SSH_CIPHERS[ciphername_bytes].tag_len
+ # load secret data
+ edata, data = _get_sshstr(data)
+ # see https://bugzilla.mindrot.org/show_bug.cgi?id=3553 for
+ # information about how OpenSSH handles AEAD tags
+ if _SSH_CIPHERS[ciphername_bytes].is_aead:
+ tag = bytes(data)
+ if len(tag) != tag_len:
+ raise ValueError("Corrupt data: invalid tag length for cipher")
+ else:
+ _check_empty(data)
+ _check_block_size(edata, blklen)
+ salt, kbuf = _get_sshstr(kdfoptions)
+ rounds, kbuf = _get_u32(kbuf)
+ _check_empty(kbuf)
+ ciph = _init_cipher(ciphername_bytes, password, salt.tobytes(), rounds)
+ dec = ciph.decryptor()
+ edata = memoryview(dec.update(edata))
+ if _SSH_CIPHERS[ciphername_bytes].is_aead:
+ assert isinstance(dec, AEADDecryptionContext)
+ _check_empty(dec.finalize_with_tag(tag))
+ else:
+ # _check_block_size requires data to be a full block so there
+ # should be no output from finalize
+ _check_empty(dec.finalize())
+ else:
+ # load secret data
+ edata, data = _get_sshstr(data)
+ _check_empty(data)
+ blklen = 8
+ _check_block_size(edata, blklen)
+ ck1, edata = _get_u32(edata)
+ ck2, edata = _get_u32(edata)
+ if ck1 != ck2:
+ raise ValueError("Corrupt data: broken checksum")
+
+ # load per-key struct
+ key_type, edata = _get_sshstr(edata)
+ if key_type != pub_key_type:
+ raise ValueError("Corrupt data: key type mismatch")
+ private_key, edata = kformat.load_private(edata, pubfields)
+ comment, edata = _get_sshstr(edata)
+
+ # yes, SSH does padding check *after* all other parsing is done.
+ # need to follow as it writes zero-byte padding too.
+ if edata != _PADDING[: len(edata)]:
+ raise ValueError("Corrupt data: invalid padding")
+
+ if isinstance(private_key, dsa.DSAPrivateKey):
+ warnings.warn(
+ "SSH DSA keys are deprecated and will be removed in a future "
+ "release.",
+ utils.DeprecatedIn40,
+ stacklevel=2,
+ )
+
+ return private_key
+
+
+def _serialize_ssh_private_key(
+ private_key: SSHPrivateKeyTypes,
+ password: bytes,
+ encryption_algorithm: KeySerializationEncryption,
+) -> bytes:
+ """Serialize private key with OpenSSH custom encoding."""
+ utils._check_bytes("password", password)
+ if isinstance(private_key, dsa.DSAPrivateKey):
+ warnings.warn(
+ "SSH DSA key support is deprecated and will be "
+ "removed in a future release",
+ utils.DeprecatedIn40,
+ stacklevel=4,
+ )
+
+ key_type = _get_ssh_key_type(private_key)
+ kformat = _lookup_kformat(key_type)
+
+ # setup parameters
+ f_kdfoptions = _FragList()
+ if password:
+ ciphername = _DEFAULT_CIPHER
+ blklen = _SSH_CIPHERS[ciphername].block_len
+ kdfname = _BCRYPT
+ rounds = _DEFAULT_ROUNDS
+ if (
+ isinstance(encryption_algorithm, _KeySerializationEncryption)
+ and encryption_algorithm._kdf_rounds is not None
+ ):
+ rounds = encryption_algorithm._kdf_rounds
+ salt = os.urandom(16)
+ f_kdfoptions.put_sshstr(salt)
+ f_kdfoptions.put_u32(rounds)
+ ciph = _init_cipher(ciphername, password, salt, rounds)
+ else:
+ ciphername = kdfname = _NONE
+ blklen = 8
+ ciph = None
+ nkeys = 1
+ checkval = os.urandom(4)
+ comment = b""
+
+ # encode public and private parts together
+ f_public_key = _FragList()
+ f_public_key.put_sshstr(key_type)
+ kformat.encode_public(private_key.public_key(), f_public_key)
+
+ f_secrets = _FragList([checkval, checkval])
+ f_secrets.put_sshstr(key_type)
+ kformat.encode_private(private_key, f_secrets)
+ f_secrets.put_sshstr(comment)
+ f_secrets.put_raw(_PADDING[: blklen - (f_secrets.size() % blklen)])
+
+ # top-level structure
+ f_main = _FragList()
+ f_main.put_raw(_SK_MAGIC)
+ f_main.put_sshstr(ciphername)
+ f_main.put_sshstr(kdfname)
+ f_main.put_sshstr(f_kdfoptions)
+ f_main.put_u32(nkeys)
+ f_main.put_sshstr(f_public_key)
+ f_main.put_sshstr(f_secrets)
+
+ # copy result info bytearray
+ slen = f_secrets.size()
+ mlen = f_main.size()
+ buf = memoryview(bytearray(mlen + blklen))
+ f_main.render(buf)
+ ofs = mlen - slen
+
+ # encrypt in-place
+ if ciph is not None:
+ ciph.encryptor().update_into(buf[ofs:mlen], buf[ofs:])
+
+ return _ssh_pem_encode(buf[:mlen])
+
+
+SSHPublicKeyTypes = typing.Union[
+ ec.EllipticCurvePublicKey,
+ rsa.RSAPublicKey,
+ dsa.DSAPublicKey,
+ ed25519.Ed25519PublicKey,
+]
+
+SSHCertPublicKeyTypes = typing.Union[
+ ec.EllipticCurvePublicKey,
+ rsa.RSAPublicKey,
+ ed25519.Ed25519PublicKey,
+]
+
+
+class SSHCertificateType(enum.Enum):
+ USER = 1
+ HOST = 2
+
+
+class SSHCertificate:
+ def __init__(
+ self,
+ _nonce: memoryview,
+ _public_key: SSHPublicKeyTypes,
+ _serial: int,
+ _cctype: int,
+ _key_id: memoryview,
+ _valid_principals: typing.List[bytes],
+ _valid_after: int,
+ _valid_before: int,
+ _critical_options: typing.Dict[bytes, bytes],
+ _extensions: typing.Dict[bytes, bytes],
+ _sig_type: memoryview,
+ _sig_key: memoryview,
+ _inner_sig_type: memoryview,
+ _signature: memoryview,
+ _tbs_cert_body: memoryview,
+ _cert_key_type: bytes,
+ _cert_body: memoryview,
+ ):
+ self._nonce = _nonce
+ self._public_key = _public_key
+ self._serial = _serial
+ try:
+ self._type = SSHCertificateType(_cctype)
+ except ValueError:
+ raise ValueError("Invalid certificate type")
+ self._key_id = _key_id
+ self._valid_principals = _valid_principals
+ self._valid_after = _valid_after
+ self._valid_before = _valid_before
+ self._critical_options = _critical_options
+ self._extensions = _extensions
+ self._sig_type = _sig_type
+ self._sig_key = _sig_key
+ self._inner_sig_type = _inner_sig_type
+ self._signature = _signature
+ self._cert_key_type = _cert_key_type
+ self._cert_body = _cert_body
+ self._tbs_cert_body = _tbs_cert_body
+
+ @property
+ def nonce(self) -> bytes:
+ return bytes(self._nonce)
+
+ def public_key(self) -> SSHCertPublicKeyTypes:
+ # make mypy happy until we remove DSA support entirely and
+ # the underlying union won't have a disallowed type
+ return typing.cast(SSHCertPublicKeyTypes, self._public_key)
+
+ @property
+ def serial(self) -> int:
+ return self._serial
+
+ @property
+ def type(self) -> SSHCertificateType:
+ return self._type
+
+ @property
+ def key_id(self) -> bytes:
+ return bytes(self._key_id)
+
+ @property
+ def valid_principals(self) -> typing.List[bytes]:
+ return self._valid_principals
+
+ @property
+ def valid_before(self) -> int:
+ return self._valid_before
+
+ @property
+ def valid_after(self) -> int:
+ return self._valid_after
+
+ @property
+ def critical_options(self) -> typing.Dict[bytes, bytes]:
+ return self._critical_options
+
+ @property
+ def extensions(self) -> typing.Dict[bytes, bytes]:
+ return self._extensions
+
+ def signature_key(self) -> SSHCertPublicKeyTypes:
+ sigformat = _lookup_kformat(self._sig_type)
+ signature_key, sigkey_rest = sigformat.load_public(self._sig_key)
+ _check_empty(sigkey_rest)
+ return signature_key
+
+ def public_bytes(self) -> bytes:
+ return (
+ bytes(self._cert_key_type)
+ + b" "
+ + binascii.b2a_base64(bytes(self._cert_body), newline=False)
+ )
+
+ def verify_cert_signature(self) -> None:
+ signature_key = self.signature_key()
+ if isinstance(signature_key, ed25519.Ed25519PublicKey):
+ signature_key.verify(
+ bytes(self._signature), bytes(self._tbs_cert_body)
+ )
+ elif isinstance(signature_key, ec.EllipticCurvePublicKey):
+ # The signature is encoded as a pair of big-endian integers
+ r, data = _get_mpint(self._signature)
+ s, data = _get_mpint(data)
+ _check_empty(data)
+ computed_sig = asym_utils.encode_dss_signature(r, s)
+ hash_alg = _get_ec_hash_alg(signature_key.curve)
+ signature_key.verify(
+ computed_sig, bytes(self._tbs_cert_body), ec.ECDSA(hash_alg)
+ )
+ else:
+ assert isinstance(signature_key, rsa.RSAPublicKey)
+ if self._inner_sig_type == _SSH_RSA:
+ hash_alg = hashes.SHA1()
+ elif self._inner_sig_type == _SSH_RSA_SHA256:
+ hash_alg = hashes.SHA256()
+ else:
+ assert self._inner_sig_type == _SSH_RSA_SHA512
+ hash_alg = hashes.SHA512()
+ signature_key.verify(
+ bytes(self._signature),
+ bytes(self._tbs_cert_body),
+ padding.PKCS1v15(),
+ hash_alg,
+ )
+
+
+def _get_ec_hash_alg(curve: ec.EllipticCurve) -> hashes.HashAlgorithm:
+ if isinstance(curve, ec.SECP256R1):
+ return hashes.SHA256()
+ elif isinstance(curve, ec.SECP384R1):
+ return hashes.SHA384()
+ else:
+ assert isinstance(curve, ec.SECP521R1)
+ return hashes.SHA512()
+
+
+def _load_ssh_public_identity(
+ data: bytes,
+ _legacy_dsa_allowed=False,
+) -> typing.Union[SSHCertificate, SSHPublicKeyTypes]:
+ utils._check_byteslike("data", data)
+
+ m = _SSH_PUBKEY_RC.match(data)
+ if not m:
+ raise ValueError("Invalid line format")
+ key_type = orig_key_type = m.group(1)
+ key_body = m.group(2)
+ with_cert = False
+ if key_type.endswith(_CERT_SUFFIX):
+ with_cert = True
+ key_type = key_type[: -len(_CERT_SUFFIX)]
+ if key_type == _SSH_DSA and not _legacy_dsa_allowed:
+ raise UnsupportedAlgorithm(
+ "DSA keys aren't supported in SSH certificates"
+ )
+ kformat = _lookup_kformat(key_type)
+
+ try:
+ rest = memoryview(binascii.a2b_base64(key_body))
+ except (TypeError, binascii.Error):
+ raise ValueError("Invalid format")
+
+ if with_cert:
+ cert_body = rest
+ inner_key_type, rest = _get_sshstr(rest)
+ if inner_key_type != orig_key_type:
+ raise ValueError("Invalid key format")
+ if with_cert:
+ nonce, rest = _get_sshstr(rest)
+ public_key, rest = kformat.load_public(rest)
+ if with_cert:
+ serial, rest = _get_u64(rest)
+ cctype, rest = _get_u32(rest)
+ key_id, rest = _get_sshstr(rest)
+ principals, rest = _get_sshstr(rest)
+ valid_principals = []
+ while principals:
+ principal, principals = _get_sshstr(principals)
+ valid_principals.append(bytes(principal))
+ valid_after, rest = _get_u64(rest)
+ valid_before, rest = _get_u64(rest)
+ crit_options, rest = _get_sshstr(rest)
+ critical_options = _parse_exts_opts(crit_options)
+ exts, rest = _get_sshstr(rest)
+ extensions = _parse_exts_opts(exts)
+ # Get the reserved field, which is unused.
+ _, rest = _get_sshstr(rest)
+ sig_key_raw, rest = _get_sshstr(rest)
+ sig_type, sig_key = _get_sshstr(sig_key_raw)
+ if sig_type == _SSH_DSA and not _legacy_dsa_allowed:
+ raise UnsupportedAlgorithm(
+ "DSA signatures aren't supported in SSH certificates"
+ )
+ # Get the entire cert body and subtract the signature
+ tbs_cert_body = cert_body[: -len(rest)]
+ signature_raw, rest = _get_sshstr(rest)
+ _check_empty(rest)
+ inner_sig_type, sig_rest = _get_sshstr(signature_raw)
+ # RSA certs can have multiple algorithm types
+ if (
+ sig_type == _SSH_RSA
+ and inner_sig_type
+ not in [_SSH_RSA_SHA256, _SSH_RSA_SHA512, _SSH_RSA]
+ ) or (sig_type != _SSH_RSA and inner_sig_type != sig_type):
+ raise ValueError("Signature key type does not match")
+ signature, sig_rest = _get_sshstr(sig_rest)
+ _check_empty(sig_rest)
+ return SSHCertificate(
+ nonce,
+ public_key,
+ serial,
+ cctype,
+ key_id,
+ valid_principals,
+ valid_after,
+ valid_before,
+ critical_options,
+ extensions,
+ sig_type,
+ sig_key,
+ inner_sig_type,
+ signature,
+ tbs_cert_body,
+ orig_key_type,
+ cert_body,
+ )
+ else:
+ _check_empty(rest)
+ return public_key
+
+
+def load_ssh_public_identity(
+ data: bytes,
+) -> typing.Union[SSHCertificate, SSHPublicKeyTypes]:
+ return _load_ssh_public_identity(data)
+
+
+def _parse_exts_opts(exts_opts: memoryview) -> typing.Dict[bytes, bytes]:
+ result: typing.Dict[bytes, bytes] = {}
+ last_name = None
+ while exts_opts:
+ name, exts_opts = _get_sshstr(exts_opts)
+ bname: bytes = bytes(name)
+ if bname in result:
+ raise ValueError("Duplicate name")
+ if last_name is not None and bname < last_name:
+ raise ValueError("Fields not lexically sorted")
+ value, exts_opts = _get_sshstr(exts_opts)
+ if len(value) > 0:
+ try:
+ value, extra = _get_sshstr(value)
+ except ValueError:
+ warnings.warn(
+ "This certificate has an incorrect encoding for critical "
+ "options or extensions. This will be an exception in "
+ "cryptography 42",
+ utils.DeprecatedIn41,
+ stacklevel=4,
+ )
+ else:
+ if len(extra) > 0:
+ raise ValueError("Unexpected extra data after value")
+ result[bname] = bytes(value)
+ last_name = bname
+ return result
+
+
+def load_ssh_public_key(
+ data: bytes, backend: typing.Any = None
+) -> SSHPublicKeyTypes:
+ cert_or_key = _load_ssh_public_identity(data, _legacy_dsa_allowed=True)
+ public_key: SSHPublicKeyTypes
+ if isinstance(cert_or_key, SSHCertificate):
+ public_key = cert_or_key.public_key()
+ else:
+ public_key = cert_or_key
+
+ if isinstance(public_key, dsa.DSAPublicKey):
+ warnings.warn(
+ "SSH DSA keys are deprecated and will be removed in a future "
+ "release.",
+ utils.DeprecatedIn40,
+ stacklevel=2,
+ )
+ return public_key
+
+
+def serialize_ssh_public_key(public_key: SSHPublicKeyTypes) -> bytes:
+ """One-line public key format for OpenSSH"""
+ if isinstance(public_key, dsa.DSAPublicKey):
+ warnings.warn(
+ "SSH DSA key support is deprecated and will be "
+ "removed in a future release",
+ utils.DeprecatedIn40,
+ stacklevel=4,
+ )
+ key_type = _get_ssh_key_type(public_key)
+ kformat = _lookup_kformat(key_type)
+
+ f_pub = _FragList()
+ f_pub.put_sshstr(key_type)
+ kformat.encode_public(public_key, f_pub)
+
+ pub = binascii.b2a_base64(f_pub.tobytes()).strip()
+ return b"".join([key_type, b" ", pub])
+
+
+SSHCertPrivateKeyTypes = typing.Union[
+ ec.EllipticCurvePrivateKey,
+ rsa.RSAPrivateKey,
+ ed25519.Ed25519PrivateKey,
+]
+
+
+# This is an undocumented limit enforced in the openssh codebase for sshd and
+# ssh-keygen, but it is undefined in the ssh certificates spec.
+_SSHKEY_CERT_MAX_PRINCIPALS = 256
+
+
+class SSHCertificateBuilder:
+ def __init__(
+ self,
+ _public_key: typing.Optional[SSHCertPublicKeyTypes] = None,
+ _serial: typing.Optional[int] = None,
+ _type: typing.Optional[SSHCertificateType] = None,
+ _key_id: typing.Optional[bytes] = None,
+ _valid_principals: typing.List[bytes] = [],
+ _valid_for_all_principals: bool = False,
+ _valid_before: typing.Optional[int] = None,
+ _valid_after: typing.Optional[int] = None,
+ _critical_options: typing.List[typing.Tuple[bytes, bytes]] = [],
+ _extensions: typing.List[typing.Tuple[bytes, bytes]] = [],
+ ):
+ self._public_key = _public_key
+ self._serial = _serial
+ self._type = _type
+ self._key_id = _key_id
+ self._valid_principals = _valid_principals
+ self._valid_for_all_principals = _valid_for_all_principals
+ self._valid_before = _valid_before
+ self._valid_after = _valid_after
+ self._critical_options = _critical_options
+ self._extensions = _extensions
+
+ def public_key(
+ self, public_key: SSHCertPublicKeyTypes
+ ) -> SSHCertificateBuilder:
+ if not isinstance(
+ public_key,
+ (
+ ec.EllipticCurvePublicKey,
+ rsa.RSAPublicKey,
+ ed25519.Ed25519PublicKey,
+ ),
+ ):
+ raise TypeError("Unsupported key type")
+ if self._public_key is not None:
+ raise ValueError("public_key already set")
+
+ return SSHCertificateBuilder(
+ _public_key=public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def serial(self, serial: int) -> SSHCertificateBuilder:
+ if not isinstance(serial, int):
+ raise TypeError("serial must be an integer")
+ if not 0 <= serial < 2**64:
+ raise ValueError("serial must be between 0 and 2**64")
+ if self._serial is not None:
+ raise ValueError("serial already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def type(self, type: SSHCertificateType) -> SSHCertificateBuilder:
+ if not isinstance(type, SSHCertificateType):
+ raise TypeError("type must be an SSHCertificateType")
+ if self._type is not None:
+ raise ValueError("type already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def key_id(self, key_id: bytes) -> SSHCertificateBuilder:
+ if not isinstance(key_id, bytes):
+ raise TypeError("key_id must be bytes")
+ if self._key_id is not None:
+ raise ValueError("key_id already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def valid_principals(
+ self, valid_principals: typing.List[bytes]
+ ) -> SSHCertificateBuilder:
+ if self._valid_for_all_principals:
+ raise ValueError(
+ "Principals can't be set because the cert is valid "
+ "for all principals"
+ )
+ if (
+ not all(isinstance(x, bytes) for x in valid_principals)
+ or not valid_principals
+ ):
+ raise TypeError(
+ "principals must be a list of bytes and can't be empty"
+ )
+ if self._valid_principals:
+ raise ValueError("valid_principals already set")
+
+ if len(valid_principals) > _SSHKEY_CERT_MAX_PRINCIPALS:
+ raise ValueError(
+ "Reached or exceeded the maximum number of valid_principals"
+ )
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def valid_for_all_principals(self):
+ if self._valid_principals:
+ raise ValueError(
+ "valid_principals already set, can't set "
+ "valid_for_all_principals"
+ )
+ if self._valid_for_all_principals:
+ raise ValueError("valid_for_all_principals already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=True,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def valid_before(
+ self, valid_before: typing.Union[int, float]
+ ) -> SSHCertificateBuilder:
+ if not isinstance(valid_before, (int, float)):
+ raise TypeError("valid_before must be an int or float")
+ valid_before = int(valid_before)
+ if valid_before < 0 or valid_before >= 2**64:
+ raise ValueError("valid_before must [0, 2**64)")
+ if self._valid_before is not None:
+ raise ValueError("valid_before already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def valid_after(
+ self, valid_after: typing.Union[int, float]
+ ) -> SSHCertificateBuilder:
+ if not isinstance(valid_after, (int, float)):
+ raise TypeError("valid_after must be an int or float")
+ valid_after = int(valid_after)
+ if valid_after < 0 or valid_after >= 2**64:
+ raise ValueError("valid_after must [0, 2**64)")
+ if self._valid_after is not None:
+ raise ValueError("valid_after already set")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions,
+ )
+
+ def add_critical_option(
+ self, name: bytes, value: bytes
+ ) -> SSHCertificateBuilder:
+ if not isinstance(name, bytes) or not isinstance(value, bytes):
+ raise TypeError("name and value must be bytes")
+ # This is O(n**2)
+ if name in [name for name, _ in self._critical_options]:
+ raise ValueError("Duplicate critical option name")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options + [(name, value)],
+ _extensions=self._extensions,
+ )
+
+ def add_extension(
+ self, name: bytes, value: bytes
+ ) -> SSHCertificateBuilder:
+ if not isinstance(name, bytes) or not isinstance(value, bytes):
+ raise TypeError("name and value must be bytes")
+ # This is O(n**2)
+ if name in [name for name, _ in self._extensions]:
+ raise ValueError("Duplicate extension name")
+
+ return SSHCertificateBuilder(
+ _public_key=self._public_key,
+ _serial=self._serial,
+ _type=self._type,
+ _key_id=self._key_id,
+ _valid_principals=self._valid_principals,
+ _valid_for_all_principals=self._valid_for_all_principals,
+ _valid_before=self._valid_before,
+ _valid_after=self._valid_after,
+ _critical_options=self._critical_options,
+ _extensions=self._extensions + [(name, value)],
+ )
+
+ def sign(self, private_key: SSHCertPrivateKeyTypes) -> SSHCertificate:
+ if not isinstance(
+ private_key,
+ (
+ ec.EllipticCurvePrivateKey,
+ rsa.RSAPrivateKey,
+ ed25519.Ed25519PrivateKey,
+ ),
+ ):
+ raise TypeError("Unsupported private key type")
+
+ if self._public_key is None:
+ raise ValueError("public_key must be set")
+
+ # Not required
+ serial = 0 if self._serial is None else self._serial
+
+ if self._type is None:
+ raise ValueError("type must be set")
+
+ # Not required
+ key_id = b"" if self._key_id is None else self._key_id
+
+ # A zero length list is valid, but means the certificate
+ # is valid for any principal of the specified type. We require
+ # the user to explicitly set valid_for_all_principals to get
+ # that behavior.
+ if not self._valid_principals and not self._valid_for_all_principals:
+ raise ValueError(
+ "valid_principals must be set if valid_for_all_principals "
+ "is False"
+ )
+
+ if self._valid_before is None:
+ raise ValueError("valid_before must be set")
+
+ if self._valid_after is None:
+ raise ValueError("valid_after must be set")
+
+ if self._valid_after > self._valid_before:
+ raise ValueError("valid_after must be earlier than valid_before")
+
+ # lexically sort our byte strings
+ self._critical_options.sort(key=lambda x: x[0])
+ self._extensions.sort(key=lambda x: x[0])
+
+ key_type = _get_ssh_key_type(self._public_key)
+ cert_prefix = key_type + _CERT_SUFFIX
+
+ # Marshal the bytes to be signed
+ nonce = os.urandom(32)
+ kformat = _lookup_kformat(key_type)
+ f = _FragList()
+ f.put_sshstr(cert_prefix)
+ f.put_sshstr(nonce)
+ kformat.encode_public(self._public_key, f)
+ f.put_u64(serial)
+ f.put_u32(self._type.value)
+ f.put_sshstr(key_id)
+ fprincipals = _FragList()
+ for p in self._valid_principals:
+ fprincipals.put_sshstr(p)
+ f.put_sshstr(fprincipals.tobytes())
+ f.put_u64(self._valid_after)
+ f.put_u64(self._valid_before)
+ fcrit = _FragList()
+ for name, value in self._critical_options:
+ fcrit.put_sshstr(name)
+ if len(value) > 0:
+ foptval = _FragList()
+ foptval.put_sshstr(value)
+ fcrit.put_sshstr(foptval.tobytes())
+ else:
+ fcrit.put_sshstr(value)
+ f.put_sshstr(fcrit.tobytes())
+ fext = _FragList()
+ for name, value in self._extensions:
+ fext.put_sshstr(name)
+ if len(value) > 0:
+ fextval = _FragList()
+ fextval.put_sshstr(value)
+ fext.put_sshstr(fextval.tobytes())
+ else:
+ fext.put_sshstr(value)
+ f.put_sshstr(fext.tobytes())
+ f.put_sshstr(b"") # RESERVED FIELD
+ # encode CA public key
+ ca_type = _get_ssh_key_type(private_key)
+ caformat = _lookup_kformat(ca_type)
+ caf = _FragList()
+ caf.put_sshstr(ca_type)
+ caformat.encode_public(private_key.public_key(), caf)
+ f.put_sshstr(caf.tobytes())
+ # Sigs according to the rules defined for the CA's public key
+ # (RFC4253 section 6.6 for ssh-rsa, RFC5656 for ECDSA,
+ # and RFC8032 for Ed25519).
+ if isinstance(private_key, ed25519.Ed25519PrivateKey):
+ signature = private_key.sign(f.tobytes())
+ fsig = _FragList()
+ fsig.put_sshstr(ca_type)
+ fsig.put_sshstr(signature)
+ f.put_sshstr(fsig.tobytes())
+ elif isinstance(private_key, ec.EllipticCurvePrivateKey):
+ hash_alg = _get_ec_hash_alg(private_key.curve)
+ signature = private_key.sign(f.tobytes(), ec.ECDSA(hash_alg))
+ r, s = asym_utils.decode_dss_signature(signature)
+ fsig = _FragList()
+ fsig.put_sshstr(ca_type)
+ fsigblob = _FragList()
+ fsigblob.put_mpint(r)
+ fsigblob.put_mpint(s)
+ fsig.put_sshstr(fsigblob.tobytes())
+ f.put_sshstr(fsig.tobytes())
+
+ else:
+ assert isinstance(private_key, rsa.RSAPrivateKey)
+ # Just like Golang, we're going to use SHA512 for RSA
+ # https://cs.opensource.google/go/x/crypto/+/refs/tags/
+ # v0.4.0:ssh/certs.go;l=445
+ # RFC 8332 defines SHA256 and 512 as options
+ fsig = _FragList()
+ fsig.put_sshstr(_SSH_RSA_SHA512)
+ signature = private_key.sign(
+ f.tobytes(), padding.PKCS1v15(), hashes.SHA512()
+ )
+ fsig.put_sshstr(signature)
+ f.put_sshstr(fsig.tobytes())
+
+ cert_data = binascii.b2a_base64(f.tobytes()).strip()
+ # load_ssh_public_identity returns a union, but this is
+ # guaranteed to be an SSHCertificate, so we cast to make
+ # mypy happy.
+ return typing.cast(
+ SSHCertificate,
+ load_ssh_public_identity(b"".join([cert_prefix, b" ", cert_data])),
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/__init__.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/__init__.py
new file mode 100644
index 0000000000..c1af423004
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/__init__.py
@@ -0,0 +1,9 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+
+class InvalidToken(Exception):
+ pass
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/hotp.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/hotp.py
new file mode 100644
index 0000000000..2067108a63
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/hotp.py
@@ -0,0 +1,92 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import base64
+import typing
+from urllib.parse import quote, urlencode
+
+from cryptography.hazmat.primitives import constant_time, hmac
+from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512
+from cryptography.hazmat.primitives.twofactor import InvalidToken
+
+HOTPHashTypes = typing.Union[SHA1, SHA256, SHA512]
+
+
+def _generate_uri(
+ hotp: HOTP,
+ type_name: str,
+ account_name: str,
+ issuer: typing.Optional[str],
+ extra_parameters: typing.List[typing.Tuple[str, int]],
+) -> str:
+ parameters = [
+ ("digits", hotp._length),
+ ("secret", base64.b32encode(hotp._key)),
+ ("algorithm", hotp._algorithm.name.upper()),
+ ]
+
+ if issuer is not None:
+ parameters.append(("issuer", issuer))
+
+ parameters.extend(extra_parameters)
+
+ label = (
+ f"{quote(issuer)}:{quote(account_name)}"
+ if issuer
+ else quote(account_name)
+ )
+ return f"otpauth://{type_name}/{label}?{urlencode(parameters)}"
+
+
+class HOTP:
+ def __init__(
+ self,
+ key: bytes,
+ length: int,
+ algorithm: HOTPHashTypes,
+ backend: typing.Any = None,
+ enforce_key_length: bool = True,
+ ) -> None:
+ if len(key) < 16 and enforce_key_length is True:
+ raise ValueError("Key length has to be at least 128 bits.")
+
+ if not isinstance(length, int):
+ raise TypeError("Length parameter must be an integer type.")
+
+ if length < 6 or length > 8:
+ raise ValueError("Length of HOTP has to be between 6 and 8.")
+
+ if not isinstance(algorithm, (SHA1, SHA256, SHA512)):
+ raise TypeError("Algorithm must be SHA1, SHA256 or SHA512.")
+
+ self._key = key
+ self._length = length
+ self._algorithm = algorithm
+
+ def generate(self, counter: int) -> bytes:
+ truncated_value = self._dynamic_truncate(counter)
+ hotp = truncated_value % (10**self._length)
+ return "{0:0{1}}".format(hotp, self._length).encode()
+
+ def verify(self, hotp: bytes, counter: int) -> None:
+ if not constant_time.bytes_eq(self.generate(counter), hotp):
+ raise InvalidToken("Supplied HOTP value does not match.")
+
+ def _dynamic_truncate(self, counter: int) -> int:
+ ctx = hmac.HMAC(self._key, self._algorithm)
+ ctx.update(counter.to_bytes(length=8, byteorder="big"))
+ hmac_value = ctx.finalize()
+
+ offset = hmac_value[len(hmac_value) - 1] & 0b1111
+ p = hmac_value[offset : offset + 4]
+ return int.from_bytes(p, byteorder="big") & 0x7FFFFFFF
+
+ def get_provisioning_uri(
+ self, account_name: str, counter: int, issuer: typing.Optional[str]
+ ) -> str:
+ return _generate_uri(
+ self, "hotp", account_name, issuer, [("counter", int(counter))]
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/totp.py b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/totp.py
new file mode 100644
index 0000000000..daddcea2f7
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/hazmat/primitives/twofactor/totp.py
@@ -0,0 +1,50 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import typing
+
+from cryptography.hazmat.primitives import constant_time
+from cryptography.hazmat.primitives.twofactor import InvalidToken
+from cryptography.hazmat.primitives.twofactor.hotp import (
+ HOTP,
+ HOTPHashTypes,
+ _generate_uri,
+)
+
+
+class TOTP:
+ def __init__(
+ self,
+ key: bytes,
+ length: int,
+ algorithm: HOTPHashTypes,
+ time_step: int,
+ backend: typing.Any = None,
+ enforce_key_length: bool = True,
+ ):
+ self._time_step = time_step
+ self._hotp = HOTP(
+ key, length, algorithm, enforce_key_length=enforce_key_length
+ )
+
+ def generate(self, time: typing.Union[int, float]) -> bytes:
+ counter = int(time / self._time_step)
+ return self._hotp.generate(counter)
+
+ def verify(self, totp: bytes, time: int) -> None:
+ if not constant_time.bytes_eq(self.generate(time), totp):
+ raise InvalidToken("Supplied TOTP value does not match.")
+
+ def get_provisioning_uri(
+ self, account_name: str, issuer: typing.Optional[str]
+ ) -> str:
+ return _generate_uri(
+ self._hotp,
+ "totp",
+ account_name,
+ issuer,
+ [("period", int(self._time_step))],
+ )
diff --git a/contrib/python/cryptography/next/py3/cryptography/py.typed b/contrib/python/cryptography/next/py3/cryptography/py.typed
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/py.typed
diff --git a/contrib/python/cryptography/next/py3/cryptography/utils.py b/contrib/python/cryptography/next/py3/cryptography/utils.py
new file mode 100644
index 0000000000..7191681684
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/utils.py
@@ -0,0 +1,130 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import enum
+import sys
+import types
+import typing
+import warnings
+
+
+# We use a UserWarning subclass, instead of DeprecationWarning, because CPython
+# decided deprecation warnings should be invisble by default.
+class CryptographyDeprecationWarning(UserWarning):
+ pass
+
+
+# Several APIs were deprecated with no specific end-of-life date because of the
+# ubiquity of their use. They should not be removed until we agree on when that
+# cycle ends.
+DeprecatedIn36 = CryptographyDeprecationWarning
+DeprecatedIn37 = CryptographyDeprecationWarning
+DeprecatedIn40 = CryptographyDeprecationWarning
+DeprecatedIn41 = CryptographyDeprecationWarning
+
+
+def _check_bytes(name: str, value: bytes) -> None:
+ if not isinstance(value, bytes):
+ raise TypeError(f"{name} must be bytes")
+
+
+def _check_byteslike(name: str, value: bytes) -> None:
+ try:
+ memoryview(value)
+ except TypeError:
+ raise TypeError(f"{name} must be bytes-like")
+
+
+def int_to_bytes(integer: int, length: typing.Optional[int] = None) -> bytes:
+ return integer.to_bytes(
+ length or (integer.bit_length() + 7) // 8 or 1, "big"
+ )
+
+
+def _extract_buffer_length(obj: typing.Any) -> typing.Tuple[typing.Any, int]:
+ from cryptography.hazmat.bindings._rust import _openssl
+
+ buf = _openssl.ffi.from_buffer(obj)
+ return buf, int(_openssl.ffi.cast("uintptr_t", buf))
+
+
+class InterfaceNotImplemented(Exception):
+ pass
+
+
+class _DeprecatedValue:
+ def __init__(self, value: object, message: str, warning_class):
+ self.value = value
+ self.message = message
+ self.warning_class = warning_class
+
+
+class _ModuleWithDeprecations(types.ModuleType):
+ def __init__(self, module: types.ModuleType):
+ super().__init__(module.__name__)
+ self.__dict__["_module"] = module
+
+ def __getattr__(self, attr: str) -> object:
+ obj = getattr(self._module, attr)
+ if isinstance(obj, _DeprecatedValue):
+ warnings.warn(obj.message, obj.warning_class, stacklevel=2)
+ obj = obj.value
+ return obj
+
+ def __setattr__(self, attr: str, value: object) -> None:
+ setattr(self._module, attr, value)
+
+ def __delattr__(self, attr: str) -> None:
+ obj = getattr(self._module, attr)
+ if isinstance(obj, _DeprecatedValue):
+ warnings.warn(obj.message, obj.warning_class, stacklevel=2)
+
+ delattr(self._module, attr)
+
+ def __dir__(self) -> typing.Sequence[str]:
+ return ["_module"] + dir(self._module)
+
+
+def deprecated(
+ value: object,
+ module_name: str,
+ message: str,
+ warning_class: typing.Type[Warning],
+ name: typing.Optional[str] = None,
+) -> _DeprecatedValue:
+ module = sys.modules[module_name]
+ if not isinstance(module, _ModuleWithDeprecations):
+ sys.modules[module_name] = module = _ModuleWithDeprecations(module)
+ dv = _DeprecatedValue(value, message, warning_class)
+ # Maintain backwards compatibility with `name is None` for pyOpenSSL.
+ if name is not None:
+ setattr(module, name, dv)
+ return dv
+
+
+def cached_property(func: typing.Callable) -> property:
+ cached_name = f"_cached_{func}"
+ sentinel = object()
+
+ def inner(instance: object):
+ cache = getattr(instance, cached_name, sentinel)
+ if cache is not sentinel:
+ return cache
+ result = func(instance)
+ setattr(instance, cached_name, result)
+ return result
+
+ return property(inner)
+
+
+# Python 3.10 changed representation of enums. We use well-defined object
+# representation and string representation from Python 3.9.
+class Enum(enum.Enum):
+ def __repr__(self) -> str:
+ return f"<{self.__class__.__name__}.{self._name_}: {self._value_!r}>"
+
+ def __str__(self) -> str:
+ return f"{self.__class__.__name__}.{self._name_}"
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/__init__.py b/contrib/python/cryptography/next/py3/cryptography/x509/__init__.py
new file mode 100644
index 0000000000..d77694a299
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/__init__.py
@@ -0,0 +1,255 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.x509 import certificate_transparency
+from cryptography.x509.base import (
+ Attribute,
+ AttributeNotFound,
+ Attributes,
+ Certificate,
+ CertificateBuilder,
+ CertificateRevocationList,
+ CertificateRevocationListBuilder,
+ CertificateSigningRequest,
+ CertificateSigningRequestBuilder,
+ InvalidVersion,
+ RevokedCertificate,
+ RevokedCertificateBuilder,
+ Version,
+ load_der_x509_certificate,
+ load_der_x509_crl,
+ load_der_x509_csr,
+ load_pem_x509_certificate,
+ load_pem_x509_certificates,
+ load_pem_x509_crl,
+ load_pem_x509_csr,
+ random_serial_number,
+)
+from cryptography.x509.extensions import (
+ AccessDescription,
+ AuthorityInformationAccess,
+ AuthorityKeyIdentifier,
+ BasicConstraints,
+ CertificateIssuer,
+ CertificatePolicies,
+ CRLDistributionPoints,
+ CRLNumber,
+ CRLReason,
+ DeltaCRLIndicator,
+ DistributionPoint,
+ DuplicateExtension,
+ ExtendedKeyUsage,
+ Extension,
+ ExtensionNotFound,
+ Extensions,
+ ExtensionType,
+ FreshestCRL,
+ GeneralNames,
+ InhibitAnyPolicy,
+ InvalidityDate,
+ IssuerAlternativeName,
+ IssuingDistributionPoint,
+ KeyUsage,
+ MSCertificateTemplate,
+ NameConstraints,
+ NoticeReference,
+ OCSPAcceptableResponses,
+ OCSPNoCheck,
+ OCSPNonce,
+ PolicyConstraints,
+ PolicyInformation,
+ PrecertificateSignedCertificateTimestamps,
+ PrecertPoison,
+ ReasonFlags,
+ SignedCertificateTimestamps,
+ SubjectAlternativeName,
+ SubjectInformationAccess,
+ SubjectKeyIdentifier,
+ TLSFeature,
+ TLSFeatureType,
+ UnrecognizedExtension,
+ UserNotice,
+)
+from cryptography.x509.general_name import (
+ DirectoryName,
+ DNSName,
+ GeneralName,
+ IPAddress,
+ OtherName,
+ RegisteredID,
+ RFC822Name,
+ UniformResourceIdentifier,
+ UnsupportedGeneralNameType,
+)
+from cryptography.x509.name import (
+ Name,
+ NameAttribute,
+ RelativeDistinguishedName,
+)
+from cryptography.x509.oid import (
+ AuthorityInformationAccessOID,
+ CertificatePoliciesOID,
+ CRLEntryExtensionOID,
+ ExtendedKeyUsageOID,
+ ExtensionOID,
+ NameOID,
+ ObjectIdentifier,
+ SignatureAlgorithmOID,
+)
+
+OID_AUTHORITY_INFORMATION_ACCESS = ExtensionOID.AUTHORITY_INFORMATION_ACCESS
+OID_AUTHORITY_KEY_IDENTIFIER = ExtensionOID.AUTHORITY_KEY_IDENTIFIER
+OID_BASIC_CONSTRAINTS = ExtensionOID.BASIC_CONSTRAINTS
+OID_CERTIFICATE_POLICIES = ExtensionOID.CERTIFICATE_POLICIES
+OID_CRL_DISTRIBUTION_POINTS = ExtensionOID.CRL_DISTRIBUTION_POINTS
+OID_EXTENDED_KEY_USAGE = ExtensionOID.EXTENDED_KEY_USAGE
+OID_FRESHEST_CRL = ExtensionOID.FRESHEST_CRL
+OID_INHIBIT_ANY_POLICY = ExtensionOID.INHIBIT_ANY_POLICY
+OID_ISSUER_ALTERNATIVE_NAME = ExtensionOID.ISSUER_ALTERNATIVE_NAME
+OID_KEY_USAGE = ExtensionOID.KEY_USAGE
+OID_NAME_CONSTRAINTS = ExtensionOID.NAME_CONSTRAINTS
+OID_OCSP_NO_CHECK = ExtensionOID.OCSP_NO_CHECK
+OID_POLICY_CONSTRAINTS = ExtensionOID.POLICY_CONSTRAINTS
+OID_POLICY_MAPPINGS = ExtensionOID.POLICY_MAPPINGS
+OID_SUBJECT_ALTERNATIVE_NAME = ExtensionOID.SUBJECT_ALTERNATIVE_NAME
+OID_SUBJECT_DIRECTORY_ATTRIBUTES = ExtensionOID.SUBJECT_DIRECTORY_ATTRIBUTES
+OID_SUBJECT_INFORMATION_ACCESS = ExtensionOID.SUBJECT_INFORMATION_ACCESS
+OID_SUBJECT_KEY_IDENTIFIER = ExtensionOID.SUBJECT_KEY_IDENTIFIER
+
+OID_DSA_WITH_SHA1 = SignatureAlgorithmOID.DSA_WITH_SHA1
+OID_DSA_WITH_SHA224 = SignatureAlgorithmOID.DSA_WITH_SHA224
+OID_DSA_WITH_SHA256 = SignatureAlgorithmOID.DSA_WITH_SHA256
+OID_ECDSA_WITH_SHA1 = SignatureAlgorithmOID.ECDSA_WITH_SHA1
+OID_ECDSA_WITH_SHA224 = SignatureAlgorithmOID.ECDSA_WITH_SHA224
+OID_ECDSA_WITH_SHA256 = SignatureAlgorithmOID.ECDSA_WITH_SHA256
+OID_ECDSA_WITH_SHA384 = SignatureAlgorithmOID.ECDSA_WITH_SHA384
+OID_ECDSA_WITH_SHA512 = SignatureAlgorithmOID.ECDSA_WITH_SHA512
+OID_RSA_WITH_MD5 = SignatureAlgorithmOID.RSA_WITH_MD5
+OID_RSA_WITH_SHA1 = SignatureAlgorithmOID.RSA_WITH_SHA1
+OID_RSA_WITH_SHA224 = SignatureAlgorithmOID.RSA_WITH_SHA224
+OID_RSA_WITH_SHA256 = SignatureAlgorithmOID.RSA_WITH_SHA256
+OID_RSA_WITH_SHA384 = SignatureAlgorithmOID.RSA_WITH_SHA384
+OID_RSA_WITH_SHA512 = SignatureAlgorithmOID.RSA_WITH_SHA512
+OID_RSASSA_PSS = SignatureAlgorithmOID.RSASSA_PSS
+
+OID_COMMON_NAME = NameOID.COMMON_NAME
+OID_COUNTRY_NAME = NameOID.COUNTRY_NAME
+OID_DOMAIN_COMPONENT = NameOID.DOMAIN_COMPONENT
+OID_DN_QUALIFIER = NameOID.DN_QUALIFIER
+OID_EMAIL_ADDRESS = NameOID.EMAIL_ADDRESS
+OID_GENERATION_QUALIFIER = NameOID.GENERATION_QUALIFIER
+OID_GIVEN_NAME = NameOID.GIVEN_NAME
+OID_LOCALITY_NAME = NameOID.LOCALITY_NAME
+OID_ORGANIZATIONAL_UNIT_NAME = NameOID.ORGANIZATIONAL_UNIT_NAME
+OID_ORGANIZATION_NAME = NameOID.ORGANIZATION_NAME
+OID_PSEUDONYM = NameOID.PSEUDONYM
+OID_SERIAL_NUMBER = NameOID.SERIAL_NUMBER
+OID_STATE_OR_PROVINCE_NAME = NameOID.STATE_OR_PROVINCE_NAME
+OID_SURNAME = NameOID.SURNAME
+OID_TITLE = NameOID.TITLE
+
+OID_CLIENT_AUTH = ExtendedKeyUsageOID.CLIENT_AUTH
+OID_CODE_SIGNING = ExtendedKeyUsageOID.CODE_SIGNING
+OID_EMAIL_PROTECTION = ExtendedKeyUsageOID.EMAIL_PROTECTION
+OID_OCSP_SIGNING = ExtendedKeyUsageOID.OCSP_SIGNING
+OID_SERVER_AUTH = ExtendedKeyUsageOID.SERVER_AUTH
+OID_TIME_STAMPING = ExtendedKeyUsageOID.TIME_STAMPING
+
+OID_ANY_POLICY = CertificatePoliciesOID.ANY_POLICY
+OID_CPS_QUALIFIER = CertificatePoliciesOID.CPS_QUALIFIER
+OID_CPS_USER_NOTICE = CertificatePoliciesOID.CPS_USER_NOTICE
+
+OID_CERTIFICATE_ISSUER = CRLEntryExtensionOID.CERTIFICATE_ISSUER
+OID_CRL_REASON = CRLEntryExtensionOID.CRL_REASON
+OID_INVALIDITY_DATE = CRLEntryExtensionOID.INVALIDITY_DATE
+
+OID_CA_ISSUERS = AuthorityInformationAccessOID.CA_ISSUERS
+OID_OCSP = AuthorityInformationAccessOID.OCSP
+
+__all__ = [
+ "certificate_transparency",
+ "load_pem_x509_certificate",
+ "load_pem_x509_certificates",
+ "load_der_x509_certificate",
+ "load_pem_x509_csr",
+ "load_der_x509_csr",
+ "load_pem_x509_crl",
+ "load_der_x509_crl",
+ "random_serial_number",
+ "Attribute",
+ "AttributeNotFound",
+ "Attributes",
+ "InvalidVersion",
+ "DeltaCRLIndicator",
+ "DuplicateExtension",
+ "ExtensionNotFound",
+ "UnsupportedGeneralNameType",
+ "NameAttribute",
+ "Name",
+ "RelativeDistinguishedName",
+ "ObjectIdentifier",
+ "ExtensionType",
+ "Extensions",
+ "Extension",
+ "ExtendedKeyUsage",
+ "FreshestCRL",
+ "IssuingDistributionPoint",
+ "TLSFeature",
+ "TLSFeatureType",
+ "OCSPAcceptableResponses",
+ "OCSPNoCheck",
+ "BasicConstraints",
+ "CRLNumber",
+ "KeyUsage",
+ "AuthorityInformationAccess",
+ "SubjectInformationAccess",
+ "AccessDescription",
+ "CertificatePolicies",
+ "PolicyInformation",
+ "UserNotice",
+ "NoticeReference",
+ "SubjectKeyIdentifier",
+ "NameConstraints",
+ "CRLDistributionPoints",
+ "DistributionPoint",
+ "ReasonFlags",
+ "InhibitAnyPolicy",
+ "SubjectAlternativeName",
+ "IssuerAlternativeName",
+ "AuthorityKeyIdentifier",
+ "GeneralNames",
+ "GeneralName",
+ "RFC822Name",
+ "DNSName",
+ "UniformResourceIdentifier",
+ "RegisteredID",
+ "DirectoryName",
+ "IPAddress",
+ "OtherName",
+ "Certificate",
+ "CertificateRevocationList",
+ "CertificateRevocationListBuilder",
+ "CertificateSigningRequest",
+ "RevokedCertificate",
+ "RevokedCertificateBuilder",
+ "CertificateSigningRequestBuilder",
+ "CertificateBuilder",
+ "Version",
+ "OID_CA_ISSUERS",
+ "OID_OCSP",
+ "CertificateIssuer",
+ "CRLReason",
+ "InvalidityDate",
+ "UnrecognizedExtension",
+ "PolicyConstraints",
+ "PrecertificateSignedCertificateTimestamps",
+ "PrecertPoison",
+ "OCSPNonce",
+ "SignedCertificateTimestamps",
+ "SignatureAlgorithmOID",
+ "NameOID",
+ "MSCertificateTemplate",
+]
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/base.py b/contrib/python/cryptography/next/py3/cryptography/x509/base.py
new file mode 100644
index 0000000000..576385e088
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/base.py
@@ -0,0 +1,1173 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import datetime
+import os
+import typing
+
+from cryptography import utils
+from cryptography.hazmat.bindings._rust import x509 as rust_x509
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives.asymmetric import (
+ dsa,
+ ec,
+ ed448,
+ ed25519,
+ padding,
+ rsa,
+ x448,
+ x25519,
+)
+from cryptography.hazmat.primitives.asymmetric.types import (
+ CertificateIssuerPrivateKeyTypes,
+ CertificateIssuerPublicKeyTypes,
+ CertificatePublicKeyTypes,
+)
+from cryptography.x509.extensions import (
+ Extension,
+ Extensions,
+ ExtensionType,
+ _make_sequence_methods,
+)
+from cryptography.x509.name import Name, _ASN1Type
+from cryptography.x509.oid import ObjectIdentifier
+
+_EARLIEST_UTC_TIME = datetime.datetime(1950, 1, 1)
+
+# This must be kept in sync with sign.rs's list of allowable types in
+# identify_hash_type
+_AllowedHashTypes = typing.Union[
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+ hashes.SHA3_224,
+ hashes.SHA3_256,
+ hashes.SHA3_384,
+ hashes.SHA3_512,
+]
+
+
+class AttributeNotFound(Exception):
+ def __init__(self, msg: str, oid: ObjectIdentifier) -> None:
+ super().__init__(msg)
+ self.oid = oid
+
+
+def _reject_duplicate_extension(
+ extension: Extension[ExtensionType],
+ extensions: typing.List[Extension[ExtensionType]],
+) -> None:
+ # This is quadratic in the number of extensions
+ for e in extensions:
+ if e.oid == extension.oid:
+ raise ValueError("This extension has already been set.")
+
+
+def _reject_duplicate_attribute(
+ oid: ObjectIdentifier,
+ attributes: typing.List[
+ typing.Tuple[ObjectIdentifier, bytes, typing.Optional[int]]
+ ],
+) -> None:
+ # This is quadratic in the number of attributes
+ for attr_oid, _, _ in attributes:
+ if attr_oid == oid:
+ raise ValueError("This attribute has already been set.")
+
+
+def _convert_to_naive_utc_time(time: datetime.datetime) -> datetime.datetime:
+ """Normalizes a datetime to a naive datetime in UTC.
+
+ time -- datetime to normalize. Assumed to be in UTC if not timezone
+ aware.
+ """
+ if time.tzinfo is not None:
+ offset = time.utcoffset()
+ offset = offset if offset else datetime.timedelta()
+ return time.replace(tzinfo=None) - offset
+ else:
+ return time
+
+
+class Attribute:
+ def __init__(
+ self,
+ oid: ObjectIdentifier,
+ value: bytes,
+ _type: int = _ASN1Type.UTF8String.value,
+ ) -> None:
+ self._oid = oid
+ self._value = value
+ self._type = _type
+
+ @property
+ def oid(self) -> ObjectIdentifier:
+ return self._oid
+
+ @property
+ def value(self) -> bytes:
+ return self._value
+
+ def __repr__(self) -> str:
+ return f"<Attribute(oid={self.oid}, value={self.value!r})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, Attribute):
+ return NotImplemented
+
+ return (
+ self.oid == other.oid
+ and self.value == other.value
+ and self._type == other._type
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.oid, self.value, self._type))
+
+
+class Attributes:
+ def __init__(
+ self,
+ attributes: typing.Iterable[Attribute],
+ ) -> None:
+ self._attributes = list(attributes)
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_attributes")
+
+ def __repr__(self) -> str:
+ return f"<Attributes({self._attributes})>"
+
+ def get_attribute_for_oid(self, oid: ObjectIdentifier) -> Attribute:
+ for attr in self:
+ if attr.oid == oid:
+ return attr
+
+ raise AttributeNotFound(f"No {oid} attribute was found", oid)
+
+
+class Version(utils.Enum):
+ v1 = 0
+ v3 = 2
+
+
+class InvalidVersion(Exception):
+ def __init__(self, msg: str, parsed_version: int) -> None:
+ super().__init__(msg)
+ self.parsed_version = parsed_version
+
+
+class Certificate(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def fingerprint(self, algorithm: hashes.HashAlgorithm) -> bytes:
+ """
+ Returns bytes using digest passed.
+ """
+
+ @property
+ @abc.abstractmethod
+ def serial_number(self) -> int:
+ """
+ Returns certificate serial number
+ """
+
+ @property
+ @abc.abstractmethod
+ def version(self) -> Version:
+ """
+ Returns the certificate version
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> CertificatePublicKeyTypes:
+ """
+ Returns the public key
+ """
+
+ @property
+ @abc.abstractmethod
+ def not_valid_before(self) -> datetime.datetime:
+ """
+ Not before time (represented as UTC datetime)
+ """
+
+ @property
+ @abc.abstractmethod
+ def not_valid_after(self) -> datetime.datetime:
+ """
+ Not after time (represented as UTC datetime)
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer(self) -> Name:
+ """
+ Returns the issuer name object.
+ """
+
+ @property
+ @abc.abstractmethod
+ def subject(self) -> Name:
+ """
+ Returns the subject name object.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_hash_algorithm(
+ self,
+ ) -> typing.Optional[hashes.HashAlgorithm]:
+ """
+ Returns a HashAlgorithm corresponding to the type of the digest signed
+ in the certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm_oid(self) -> ObjectIdentifier:
+ """
+ Returns the ObjectIdentifier of the signature algorithm.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm_parameters(
+ self,
+ ) -> typing.Union[None, padding.PSS, padding.PKCS1v15, ec.ECDSA]:
+ """
+ Returns the signature algorithm parameters.
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> Extensions:
+ """
+ Returns an Extensions object.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature(self) -> bytes:
+ """
+ Returns the signature bytes.
+ """
+
+ @property
+ @abc.abstractmethod
+ def tbs_certificate_bytes(self) -> bytes:
+ """
+ Returns the tbsCertificate payload bytes as defined in RFC 5280.
+ """
+
+ @property
+ @abc.abstractmethod
+ def tbs_precertificate_bytes(self) -> bytes:
+ """
+ Returns the tbsCertificate payload bytes with the SCT list extension
+ stripped.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+ @abc.abstractmethod
+ def __hash__(self) -> int:
+ """
+ Computes a hash.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(self, encoding: serialization.Encoding) -> bytes:
+ """
+ Serializes the certificate to PEM or DER format.
+ """
+
+ @abc.abstractmethod
+ def verify_directly_issued_by(self, issuer: Certificate) -> None:
+ """
+ This method verifies that certificate issuer name matches the
+ issuer subject name and that the certificate is signed by the
+ issuer's private key. No other validation is performed.
+ """
+
+
+# Runtime isinstance checks need this since the rust class is not a subclass.
+Certificate.register(rust_x509.Certificate)
+
+
+class RevokedCertificate(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def serial_number(self) -> int:
+ """
+ Returns the serial number of the revoked certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def revocation_date(self) -> datetime.datetime:
+ """
+ Returns the date of when this certificate was revoked.
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> Extensions:
+ """
+ Returns an Extensions object containing a list of Revoked extensions.
+ """
+
+
+# Runtime isinstance checks need this since the rust class is not a subclass.
+RevokedCertificate.register(rust_x509.RevokedCertificate)
+
+
+class _RawRevokedCertificate(RevokedCertificate):
+ def __init__(
+ self,
+ serial_number: int,
+ revocation_date: datetime.datetime,
+ extensions: Extensions,
+ ):
+ self._serial_number = serial_number
+ self._revocation_date = revocation_date
+ self._extensions = extensions
+
+ @property
+ def serial_number(self) -> int:
+ return self._serial_number
+
+ @property
+ def revocation_date(self) -> datetime.datetime:
+ return self._revocation_date
+
+ @property
+ def extensions(self) -> Extensions:
+ return self._extensions
+
+
+class CertificateRevocationList(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def public_bytes(self, encoding: serialization.Encoding) -> bytes:
+ """
+ Serializes the CRL to PEM or DER format.
+ """
+
+ @abc.abstractmethod
+ def fingerprint(self, algorithm: hashes.HashAlgorithm) -> bytes:
+ """
+ Returns bytes using digest passed.
+ """
+
+ @abc.abstractmethod
+ def get_revoked_certificate_by_serial_number(
+ self, serial_number: int
+ ) -> typing.Optional[RevokedCertificate]:
+ """
+ Returns an instance of RevokedCertificate or None if the serial_number
+ is not in the CRL.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_hash_algorithm(
+ self,
+ ) -> typing.Optional[hashes.HashAlgorithm]:
+ """
+ Returns a HashAlgorithm corresponding to the type of the digest signed
+ in the certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm_oid(self) -> ObjectIdentifier:
+ """
+ Returns the ObjectIdentifier of the signature algorithm.
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer(self) -> Name:
+ """
+ Returns the X509Name with the issuer of this CRL.
+ """
+
+ @property
+ @abc.abstractmethod
+ def next_update(self) -> typing.Optional[datetime.datetime]:
+ """
+ Returns the date of next update for this CRL.
+ """
+
+ @property
+ @abc.abstractmethod
+ def last_update(self) -> datetime.datetime:
+ """
+ Returns the date of last update for this CRL.
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> Extensions:
+ """
+ Returns an Extensions object containing a list of CRL extensions.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature(self) -> bytes:
+ """
+ Returns the signature bytes.
+ """
+
+ @property
+ @abc.abstractmethod
+ def tbs_certlist_bytes(self) -> bytes:
+ """
+ Returns the tbsCertList payload bytes as defined in RFC 5280.
+ """
+
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+ @abc.abstractmethod
+ def __len__(self) -> int:
+ """
+ Number of revoked certificates in the CRL.
+ """
+
+ @typing.overload
+ def __getitem__(self, idx: int) -> RevokedCertificate:
+ ...
+
+ @typing.overload
+ def __getitem__(self, idx: slice) -> typing.List[RevokedCertificate]:
+ ...
+
+ @abc.abstractmethod
+ def __getitem__(
+ self, idx: typing.Union[int, slice]
+ ) -> typing.Union[RevokedCertificate, typing.List[RevokedCertificate]]:
+ """
+ Returns a revoked certificate (or slice of revoked certificates).
+ """
+
+ @abc.abstractmethod
+ def __iter__(self) -> typing.Iterator[RevokedCertificate]:
+ """
+ Iterator over the revoked certificates
+ """
+
+ @abc.abstractmethod
+ def is_signature_valid(
+ self, public_key: CertificateIssuerPublicKeyTypes
+ ) -> bool:
+ """
+ Verifies signature of revocation list against given public key.
+ """
+
+
+CertificateRevocationList.register(rust_x509.CertificateRevocationList)
+
+
+class CertificateSigningRequest(metaclass=abc.ABCMeta):
+ @abc.abstractmethod
+ def __eq__(self, other: object) -> bool:
+ """
+ Checks equality.
+ """
+
+ @abc.abstractmethod
+ def __hash__(self) -> int:
+ """
+ Computes a hash.
+ """
+
+ @abc.abstractmethod
+ def public_key(self) -> CertificatePublicKeyTypes:
+ """
+ Returns the public key
+ """
+
+ @property
+ @abc.abstractmethod
+ def subject(self) -> Name:
+ """
+ Returns the subject name object.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_hash_algorithm(
+ self,
+ ) -> typing.Optional[hashes.HashAlgorithm]:
+ """
+ Returns a HashAlgorithm corresponding to the type of the digest signed
+ in the certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm_oid(self) -> ObjectIdentifier:
+ """
+ Returns the ObjectIdentifier of the signature algorithm.
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> Extensions:
+ """
+ Returns the extensions in the signing request.
+ """
+
+ @property
+ @abc.abstractmethod
+ def attributes(self) -> Attributes:
+ """
+ Returns an Attributes object.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(self, encoding: serialization.Encoding) -> bytes:
+ """
+ Encodes the request to PEM or DER format.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature(self) -> bytes:
+ """
+ Returns the signature bytes.
+ """
+
+ @property
+ @abc.abstractmethod
+ def tbs_certrequest_bytes(self) -> bytes:
+ """
+ Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
+ 2986.
+ """
+
+ @property
+ @abc.abstractmethod
+ def is_signature_valid(self) -> bool:
+ """
+ Verifies signature of signing request.
+ """
+
+ @abc.abstractmethod
+ def get_attribute_for_oid(self, oid: ObjectIdentifier) -> bytes:
+ """
+ Get the attribute value for a given OID.
+ """
+
+
+# Runtime isinstance checks need this since the rust class is not a subclass.
+CertificateSigningRequest.register(rust_x509.CertificateSigningRequest)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_pem_x509_certificate(
+ data: bytes, backend: typing.Any = None
+) -> Certificate:
+ return rust_x509.load_pem_x509_certificate(data)
+
+
+def load_pem_x509_certificates(data: bytes) -> typing.List[Certificate]:
+ return rust_x509.load_pem_x509_certificates(data)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_der_x509_certificate(
+ data: bytes, backend: typing.Any = None
+) -> Certificate:
+ return rust_x509.load_der_x509_certificate(data)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_pem_x509_csr(
+ data: bytes, backend: typing.Any = None
+) -> CertificateSigningRequest:
+ return rust_x509.load_pem_x509_csr(data)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_der_x509_csr(
+ data: bytes, backend: typing.Any = None
+) -> CertificateSigningRequest:
+ return rust_x509.load_der_x509_csr(data)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_pem_x509_crl(
+ data: bytes, backend: typing.Any = None
+) -> CertificateRevocationList:
+ return rust_x509.load_pem_x509_crl(data)
+
+
+# Backend argument preserved for API compatibility, but ignored.
+def load_der_x509_crl(
+ data: bytes, backend: typing.Any = None
+) -> CertificateRevocationList:
+ return rust_x509.load_der_x509_crl(data)
+
+
+class CertificateSigningRequestBuilder:
+ def __init__(
+ self,
+ subject_name: typing.Optional[Name] = None,
+ extensions: typing.List[Extension[ExtensionType]] = [],
+ attributes: typing.List[
+ typing.Tuple[ObjectIdentifier, bytes, typing.Optional[int]]
+ ] = [],
+ ):
+ """
+ Creates an empty X.509 certificate request (v1).
+ """
+ self._subject_name = subject_name
+ self._extensions = extensions
+ self._attributes = attributes
+
+ def subject_name(self, name: Name) -> CertificateSigningRequestBuilder:
+ """
+ Sets the certificate requestor's distinguished name.
+ """
+ if not isinstance(name, Name):
+ raise TypeError("Expecting x509.Name object.")
+ if self._subject_name is not None:
+ raise ValueError("The subject name may only be set once.")
+ return CertificateSigningRequestBuilder(
+ name, self._extensions, self._attributes
+ )
+
+ def add_extension(
+ self, extval: ExtensionType, critical: bool
+ ) -> CertificateSigningRequestBuilder:
+ """
+ Adds an X.509 extension to the certificate request.
+ """
+ if not isinstance(extval, ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+
+ return CertificateSigningRequestBuilder(
+ self._subject_name,
+ self._extensions + [extension],
+ self._attributes,
+ )
+
+ def add_attribute(
+ self,
+ oid: ObjectIdentifier,
+ value: bytes,
+ *,
+ _tag: typing.Optional[_ASN1Type] = None,
+ ) -> CertificateSigningRequestBuilder:
+ """
+ Adds an X.509 attribute with an OID and associated value.
+ """
+ if not isinstance(oid, ObjectIdentifier):
+ raise TypeError("oid must be an ObjectIdentifier")
+
+ if not isinstance(value, bytes):
+ raise TypeError("value must be bytes")
+
+ if _tag is not None and not isinstance(_tag, _ASN1Type):
+ raise TypeError("tag must be _ASN1Type")
+
+ _reject_duplicate_attribute(oid, self._attributes)
+
+ if _tag is not None:
+ tag = _tag.value
+ else:
+ tag = None
+
+ return CertificateSigningRequestBuilder(
+ self._subject_name,
+ self._extensions,
+ self._attributes + [(oid, value, tag)],
+ )
+
+ def sign(
+ self,
+ private_key: CertificateIssuerPrivateKeyTypes,
+ algorithm: typing.Optional[_AllowedHashTypes],
+ backend: typing.Any = None,
+ ) -> CertificateSigningRequest:
+ """
+ Signs the request using the requestor's private key.
+ """
+ if self._subject_name is None:
+ raise ValueError("A CertificateSigningRequest must have a subject")
+ return rust_x509.create_x509_csr(self, private_key, algorithm)
+
+
+class CertificateBuilder:
+ _extensions: typing.List[Extension[ExtensionType]]
+
+ def __init__(
+ self,
+ issuer_name: typing.Optional[Name] = None,
+ subject_name: typing.Optional[Name] = None,
+ public_key: typing.Optional[CertificatePublicKeyTypes] = None,
+ serial_number: typing.Optional[int] = None,
+ not_valid_before: typing.Optional[datetime.datetime] = None,
+ not_valid_after: typing.Optional[datetime.datetime] = None,
+ extensions: typing.List[Extension[ExtensionType]] = [],
+ ) -> None:
+ self._version = Version.v3
+ self._issuer_name = issuer_name
+ self._subject_name = subject_name
+ self._public_key = public_key
+ self._serial_number = serial_number
+ self._not_valid_before = not_valid_before
+ self._not_valid_after = not_valid_after
+ self._extensions = extensions
+
+ def issuer_name(self, name: Name) -> CertificateBuilder:
+ """
+ Sets the CA's distinguished name.
+ """
+ if not isinstance(name, Name):
+ raise TypeError("Expecting x509.Name object.")
+ if self._issuer_name is not None:
+ raise ValueError("The issuer name may only be set once.")
+ return CertificateBuilder(
+ name,
+ self._subject_name,
+ self._public_key,
+ self._serial_number,
+ self._not_valid_before,
+ self._not_valid_after,
+ self._extensions,
+ )
+
+ def subject_name(self, name: Name) -> CertificateBuilder:
+ """
+ Sets the requestor's distinguished name.
+ """
+ if not isinstance(name, Name):
+ raise TypeError("Expecting x509.Name object.")
+ if self._subject_name is not None:
+ raise ValueError("The subject name may only be set once.")
+ return CertificateBuilder(
+ self._issuer_name,
+ name,
+ self._public_key,
+ self._serial_number,
+ self._not_valid_before,
+ self._not_valid_after,
+ self._extensions,
+ )
+
+ def public_key(
+ self,
+ key: CertificatePublicKeyTypes,
+ ) -> CertificateBuilder:
+ """
+ Sets the requestor's public key (as found in the signing request).
+ """
+ if not isinstance(
+ key,
+ (
+ dsa.DSAPublicKey,
+ rsa.RSAPublicKey,
+ ec.EllipticCurvePublicKey,
+ ed25519.Ed25519PublicKey,
+ ed448.Ed448PublicKey,
+ x25519.X25519PublicKey,
+ x448.X448PublicKey,
+ ),
+ ):
+ raise TypeError(
+ "Expecting one of DSAPublicKey, RSAPublicKey,"
+ " EllipticCurvePublicKey, Ed25519PublicKey,"
+ " Ed448PublicKey, X25519PublicKey, or "
+ "X448PublicKey."
+ )
+ if self._public_key is not None:
+ raise ValueError("The public key may only be set once.")
+ return CertificateBuilder(
+ self._issuer_name,
+ self._subject_name,
+ key,
+ self._serial_number,
+ self._not_valid_before,
+ self._not_valid_after,
+ self._extensions,
+ )
+
+ def serial_number(self, number: int) -> CertificateBuilder:
+ """
+ Sets the certificate serial number.
+ """
+ if not isinstance(number, int):
+ raise TypeError("Serial number must be of integral type.")
+ if self._serial_number is not None:
+ raise ValueError("The serial number may only be set once.")
+ if number <= 0:
+ raise ValueError("The serial number should be positive.")
+
+ # ASN.1 integers are always signed, so most significant bit must be
+ # zero.
+ if number.bit_length() >= 160: # As defined in RFC 5280
+ raise ValueError(
+ "The serial number should not be more than 159 " "bits."
+ )
+ return CertificateBuilder(
+ self._issuer_name,
+ self._subject_name,
+ self._public_key,
+ number,
+ self._not_valid_before,
+ self._not_valid_after,
+ self._extensions,
+ )
+
+ def not_valid_before(self, time: datetime.datetime) -> CertificateBuilder:
+ """
+ Sets the certificate activation time.
+ """
+ if not isinstance(time, datetime.datetime):
+ raise TypeError("Expecting datetime object.")
+ if self._not_valid_before is not None:
+ raise ValueError("The not valid before may only be set once.")
+ time = _convert_to_naive_utc_time(time)
+ if time < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The not valid before date must be on or after"
+ " 1950 January 1)."
+ )
+ if self._not_valid_after is not None and time > self._not_valid_after:
+ raise ValueError(
+ "The not valid before date must be before the not valid after "
+ "date."
+ )
+ return CertificateBuilder(
+ self._issuer_name,
+ self._subject_name,
+ self._public_key,
+ self._serial_number,
+ time,
+ self._not_valid_after,
+ self._extensions,
+ )
+
+ def not_valid_after(self, time: datetime.datetime) -> CertificateBuilder:
+ """
+ Sets the certificate expiration time.
+ """
+ if not isinstance(time, datetime.datetime):
+ raise TypeError("Expecting datetime object.")
+ if self._not_valid_after is not None:
+ raise ValueError("The not valid after may only be set once.")
+ time = _convert_to_naive_utc_time(time)
+ if time < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The not valid after date must be on or after"
+ " 1950 January 1."
+ )
+ if (
+ self._not_valid_before is not None
+ and time < self._not_valid_before
+ ):
+ raise ValueError(
+ "The not valid after date must be after the not valid before "
+ "date."
+ )
+ return CertificateBuilder(
+ self._issuer_name,
+ self._subject_name,
+ self._public_key,
+ self._serial_number,
+ self._not_valid_before,
+ time,
+ self._extensions,
+ )
+
+ def add_extension(
+ self, extval: ExtensionType, critical: bool
+ ) -> CertificateBuilder:
+ """
+ Adds an X.509 extension to the certificate.
+ """
+ if not isinstance(extval, ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+
+ return CertificateBuilder(
+ self._issuer_name,
+ self._subject_name,
+ self._public_key,
+ self._serial_number,
+ self._not_valid_before,
+ self._not_valid_after,
+ self._extensions + [extension],
+ )
+
+ def sign(
+ self,
+ private_key: CertificateIssuerPrivateKeyTypes,
+ algorithm: typing.Optional[_AllowedHashTypes],
+ backend: typing.Any = None,
+ *,
+ rsa_padding: typing.Optional[
+ typing.Union[padding.PSS, padding.PKCS1v15]
+ ] = None,
+ ) -> Certificate:
+ """
+ Signs the certificate using the CA's private key.
+ """
+ if self._subject_name is None:
+ raise ValueError("A certificate must have a subject name")
+
+ if self._issuer_name is None:
+ raise ValueError("A certificate must have an issuer name")
+
+ if self._serial_number is None:
+ raise ValueError("A certificate must have a serial number")
+
+ if self._not_valid_before is None:
+ raise ValueError("A certificate must have a not valid before time")
+
+ if self._not_valid_after is None:
+ raise ValueError("A certificate must have a not valid after time")
+
+ if self._public_key is None:
+ raise ValueError("A certificate must have a public key")
+
+ if rsa_padding is not None:
+ if not isinstance(rsa_padding, (padding.PSS, padding.PKCS1v15)):
+ raise TypeError("Padding must be PSS or PKCS1v15")
+ if not isinstance(private_key, rsa.RSAPrivateKey):
+ raise TypeError("Padding is only supported for RSA keys")
+
+ return rust_x509.create_x509_certificate(
+ self, private_key, algorithm, rsa_padding
+ )
+
+
+class CertificateRevocationListBuilder:
+ _extensions: typing.List[Extension[ExtensionType]]
+ _revoked_certificates: typing.List[RevokedCertificate]
+
+ def __init__(
+ self,
+ issuer_name: typing.Optional[Name] = None,
+ last_update: typing.Optional[datetime.datetime] = None,
+ next_update: typing.Optional[datetime.datetime] = None,
+ extensions: typing.List[Extension[ExtensionType]] = [],
+ revoked_certificates: typing.List[RevokedCertificate] = [],
+ ):
+ self._issuer_name = issuer_name
+ self._last_update = last_update
+ self._next_update = next_update
+ self._extensions = extensions
+ self._revoked_certificates = revoked_certificates
+
+ def issuer_name(
+ self, issuer_name: Name
+ ) -> CertificateRevocationListBuilder:
+ if not isinstance(issuer_name, Name):
+ raise TypeError("Expecting x509.Name object.")
+ if self._issuer_name is not None:
+ raise ValueError("The issuer name may only be set once.")
+ return CertificateRevocationListBuilder(
+ issuer_name,
+ self._last_update,
+ self._next_update,
+ self._extensions,
+ self._revoked_certificates,
+ )
+
+ def last_update(
+ self, last_update: datetime.datetime
+ ) -> CertificateRevocationListBuilder:
+ if not isinstance(last_update, datetime.datetime):
+ raise TypeError("Expecting datetime object.")
+ if self._last_update is not None:
+ raise ValueError("Last update may only be set once.")
+ last_update = _convert_to_naive_utc_time(last_update)
+ if last_update < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The last update date must be on or after" " 1950 January 1."
+ )
+ if self._next_update is not None and last_update > self._next_update:
+ raise ValueError(
+ "The last update date must be before the next update date."
+ )
+ return CertificateRevocationListBuilder(
+ self._issuer_name,
+ last_update,
+ self._next_update,
+ self._extensions,
+ self._revoked_certificates,
+ )
+
+ def next_update(
+ self, next_update: datetime.datetime
+ ) -> CertificateRevocationListBuilder:
+ if not isinstance(next_update, datetime.datetime):
+ raise TypeError("Expecting datetime object.")
+ if self._next_update is not None:
+ raise ValueError("Last update may only be set once.")
+ next_update = _convert_to_naive_utc_time(next_update)
+ if next_update < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The last update date must be on or after" " 1950 January 1."
+ )
+ if self._last_update is not None and next_update < self._last_update:
+ raise ValueError(
+ "The next update date must be after the last update date."
+ )
+ return CertificateRevocationListBuilder(
+ self._issuer_name,
+ self._last_update,
+ next_update,
+ self._extensions,
+ self._revoked_certificates,
+ )
+
+ def add_extension(
+ self, extval: ExtensionType, critical: bool
+ ) -> CertificateRevocationListBuilder:
+ """
+ Adds an X.509 extension to the certificate revocation list.
+ """
+ if not isinstance(extval, ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+ return CertificateRevocationListBuilder(
+ self._issuer_name,
+ self._last_update,
+ self._next_update,
+ self._extensions + [extension],
+ self._revoked_certificates,
+ )
+
+ def add_revoked_certificate(
+ self, revoked_certificate: RevokedCertificate
+ ) -> CertificateRevocationListBuilder:
+ """
+ Adds a revoked certificate to the CRL.
+ """
+ if not isinstance(revoked_certificate, RevokedCertificate):
+ raise TypeError("Must be an instance of RevokedCertificate")
+
+ return CertificateRevocationListBuilder(
+ self._issuer_name,
+ self._last_update,
+ self._next_update,
+ self._extensions,
+ self._revoked_certificates + [revoked_certificate],
+ )
+
+ def sign(
+ self,
+ private_key: CertificateIssuerPrivateKeyTypes,
+ algorithm: typing.Optional[_AllowedHashTypes],
+ backend: typing.Any = None,
+ ) -> CertificateRevocationList:
+ if self._issuer_name is None:
+ raise ValueError("A CRL must have an issuer name")
+
+ if self._last_update is None:
+ raise ValueError("A CRL must have a last update time")
+
+ if self._next_update is None:
+ raise ValueError("A CRL must have a next update time")
+
+ return rust_x509.create_x509_crl(self, private_key, algorithm)
+
+
+class RevokedCertificateBuilder:
+ def __init__(
+ self,
+ serial_number: typing.Optional[int] = None,
+ revocation_date: typing.Optional[datetime.datetime] = None,
+ extensions: typing.List[Extension[ExtensionType]] = [],
+ ):
+ self._serial_number = serial_number
+ self._revocation_date = revocation_date
+ self._extensions = extensions
+
+ def serial_number(self, number: int) -> RevokedCertificateBuilder:
+ if not isinstance(number, int):
+ raise TypeError("Serial number must be of integral type.")
+ if self._serial_number is not None:
+ raise ValueError("The serial number may only be set once.")
+ if number <= 0:
+ raise ValueError("The serial number should be positive")
+
+ # ASN.1 integers are always signed, so most significant bit must be
+ # zero.
+ if number.bit_length() >= 160: # As defined in RFC 5280
+ raise ValueError(
+ "The serial number should not be more than 159 " "bits."
+ )
+ return RevokedCertificateBuilder(
+ number, self._revocation_date, self._extensions
+ )
+
+ def revocation_date(
+ self, time: datetime.datetime
+ ) -> RevokedCertificateBuilder:
+ if not isinstance(time, datetime.datetime):
+ raise TypeError("Expecting datetime object.")
+ if self._revocation_date is not None:
+ raise ValueError("The revocation date may only be set once.")
+ time = _convert_to_naive_utc_time(time)
+ if time < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The revocation date must be on or after" " 1950 January 1."
+ )
+ return RevokedCertificateBuilder(
+ self._serial_number, time, self._extensions
+ )
+
+ def add_extension(
+ self, extval: ExtensionType, critical: bool
+ ) -> RevokedCertificateBuilder:
+ if not isinstance(extval, ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+ return RevokedCertificateBuilder(
+ self._serial_number,
+ self._revocation_date,
+ self._extensions + [extension],
+ )
+
+ def build(self, backend: typing.Any = None) -> RevokedCertificate:
+ if self._serial_number is None:
+ raise ValueError("A revoked certificate must have a serial number")
+ if self._revocation_date is None:
+ raise ValueError(
+ "A revoked certificate must have a revocation date"
+ )
+ return _RawRevokedCertificate(
+ self._serial_number,
+ self._revocation_date,
+ Extensions(self._extensions),
+ )
+
+
+def random_serial_number() -> int:
+ return int.from_bytes(os.urandom(20), "big") >> 1
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/certificate_transparency.py b/contrib/python/cryptography/next/py3/cryptography/x509/certificate_transparency.py
new file mode 100644
index 0000000000..73647ee716
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/certificate_transparency.py
@@ -0,0 +1,97 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import datetime
+
+from cryptography import utils
+from cryptography.hazmat.bindings._rust import x509 as rust_x509
+from cryptography.hazmat.primitives.hashes import HashAlgorithm
+
+
+class LogEntryType(utils.Enum):
+ X509_CERTIFICATE = 0
+ PRE_CERTIFICATE = 1
+
+
+class Version(utils.Enum):
+ v1 = 0
+
+
+class SignatureAlgorithm(utils.Enum):
+ """
+ Signature algorithms that are valid for SCTs.
+
+ These are exactly the same as SignatureAlgorithm in RFC 5246 (TLS 1.2).
+
+ See: <https://datatracker.ietf.org/doc/html/rfc5246#section-7.4.1.4.1>
+ """
+
+ ANONYMOUS = 0
+ RSA = 1
+ DSA = 2
+ ECDSA = 3
+
+
+class SignedCertificateTimestamp(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def version(self) -> Version:
+ """
+ Returns the SCT version.
+ """
+
+ @property
+ @abc.abstractmethod
+ def log_id(self) -> bytes:
+ """
+ Returns an identifier indicating which log this SCT is for.
+ """
+
+ @property
+ @abc.abstractmethod
+ def timestamp(self) -> datetime.datetime:
+ """
+ Returns the timestamp for this SCT.
+ """
+
+ @property
+ @abc.abstractmethod
+ def entry_type(self) -> LogEntryType:
+ """
+ Returns whether this is an SCT for a certificate or pre-certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_hash_algorithm(self) -> HashAlgorithm:
+ """
+ Returns the hash algorithm used for the SCT's signature.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm(self) -> SignatureAlgorithm:
+ """
+ Returns the signing algorithm used for the SCT's signature.
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature(self) -> bytes:
+ """
+ Returns the signature for this SCT.
+ """
+
+ @property
+ @abc.abstractmethod
+ def extension_bytes(self) -> bytes:
+ """
+ Returns the raw bytes of any extensions for this SCT.
+ """
+
+
+SignedCertificateTimestamp.register(rust_x509.Sct)
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/extensions.py b/contrib/python/cryptography/next/py3/cryptography/x509/extensions.py
new file mode 100644
index 0000000000..ac99592f55
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/extensions.py
@@ -0,0 +1,2215 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import datetime
+import hashlib
+import ipaddress
+import typing
+
+from cryptography import utils
+from cryptography.hazmat.bindings._rust import asn1
+from cryptography.hazmat.bindings._rust import x509 as rust_x509
+from cryptography.hazmat.primitives import constant_time, serialization
+from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey
+from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
+from cryptography.hazmat.primitives.asymmetric.types import (
+ CertificateIssuerPublicKeyTypes,
+ CertificatePublicKeyTypes,
+)
+from cryptography.x509.certificate_transparency import (
+ SignedCertificateTimestamp,
+)
+from cryptography.x509.general_name import (
+ DirectoryName,
+ DNSName,
+ GeneralName,
+ IPAddress,
+ OtherName,
+ RegisteredID,
+ RFC822Name,
+ UniformResourceIdentifier,
+ _IPAddressTypes,
+)
+from cryptography.x509.name import Name, RelativeDistinguishedName
+from cryptography.x509.oid import (
+ CRLEntryExtensionOID,
+ ExtensionOID,
+ ObjectIdentifier,
+ OCSPExtensionOID,
+)
+
+ExtensionTypeVar = typing.TypeVar(
+ "ExtensionTypeVar", bound="ExtensionType", covariant=True
+)
+
+
+def _key_identifier_from_public_key(
+ public_key: CertificatePublicKeyTypes,
+) -> bytes:
+ if isinstance(public_key, RSAPublicKey):
+ data = public_key.public_bytes(
+ serialization.Encoding.DER,
+ serialization.PublicFormat.PKCS1,
+ )
+ elif isinstance(public_key, EllipticCurvePublicKey):
+ data = public_key.public_bytes(
+ serialization.Encoding.X962,
+ serialization.PublicFormat.UncompressedPoint,
+ )
+ else:
+ # This is a very slow way to do this.
+ serialized = public_key.public_bytes(
+ serialization.Encoding.DER,
+ serialization.PublicFormat.SubjectPublicKeyInfo,
+ )
+ data = asn1.parse_spki_for_data(serialized)
+
+ return hashlib.sha1(data).digest()
+
+
+def _make_sequence_methods(field_name: str):
+ def len_method(self) -> int:
+ return len(getattr(self, field_name))
+
+ def iter_method(self):
+ return iter(getattr(self, field_name))
+
+ def getitem_method(self, idx):
+ return getattr(self, field_name)[idx]
+
+ return len_method, iter_method, getitem_method
+
+
+class DuplicateExtension(Exception):
+ def __init__(self, msg: str, oid: ObjectIdentifier) -> None:
+ super().__init__(msg)
+ self.oid = oid
+
+
+class ExtensionNotFound(Exception):
+ def __init__(self, msg: str, oid: ObjectIdentifier) -> None:
+ super().__init__(msg)
+ self.oid = oid
+
+
+class ExtensionType(metaclass=abc.ABCMeta):
+ oid: typing.ClassVar[ObjectIdentifier]
+
+ def public_bytes(self) -> bytes:
+ """
+ Serializes the extension type to DER.
+ """
+ raise NotImplementedError(
+ "public_bytes is not implemented for extension type {!r}".format(
+ self
+ )
+ )
+
+
+class Extensions:
+ def __init__(
+ self, extensions: typing.Iterable[Extension[ExtensionType]]
+ ) -> None:
+ self._extensions = list(extensions)
+
+ def get_extension_for_oid(
+ self, oid: ObjectIdentifier
+ ) -> Extension[ExtensionType]:
+ for ext in self:
+ if ext.oid == oid:
+ return ext
+
+ raise ExtensionNotFound(f"No {oid} extension was found", oid)
+
+ def get_extension_for_class(
+ self, extclass: typing.Type[ExtensionTypeVar]
+ ) -> Extension[ExtensionTypeVar]:
+ if extclass is UnrecognizedExtension:
+ raise TypeError(
+ "UnrecognizedExtension can't be used with "
+ "get_extension_for_class because more than one instance of the"
+ " class may be present."
+ )
+
+ for ext in self:
+ if isinstance(ext.value, extclass):
+ return ext
+
+ raise ExtensionNotFound(
+ f"No {extclass} extension was found", extclass.oid
+ )
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_extensions")
+
+ def __repr__(self) -> str:
+ return f"<Extensions({self._extensions})>"
+
+
+class CRLNumber(ExtensionType):
+ oid = ExtensionOID.CRL_NUMBER
+
+ def __init__(self, crl_number: int) -> None:
+ if not isinstance(crl_number, int):
+ raise TypeError("crl_number must be an integer")
+
+ self._crl_number = crl_number
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, CRLNumber):
+ return NotImplemented
+
+ return self.crl_number == other.crl_number
+
+ def __hash__(self) -> int:
+ return hash(self.crl_number)
+
+ def __repr__(self) -> str:
+ return f"<CRLNumber({self.crl_number})>"
+
+ @property
+ def crl_number(self) -> int:
+ return self._crl_number
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class AuthorityKeyIdentifier(ExtensionType):
+ oid = ExtensionOID.AUTHORITY_KEY_IDENTIFIER
+
+ def __init__(
+ self,
+ key_identifier: typing.Optional[bytes],
+ authority_cert_issuer: typing.Optional[typing.Iterable[GeneralName]],
+ authority_cert_serial_number: typing.Optional[int],
+ ) -> None:
+ if (authority_cert_issuer is None) != (
+ authority_cert_serial_number is None
+ ):
+ raise ValueError(
+ "authority_cert_issuer and authority_cert_serial_number "
+ "must both be present or both None"
+ )
+
+ if authority_cert_issuer is not None:
+ authority_cert_issuer = list(authority_cert_issuer)
+ if not all(
+ isinstance(x, GeneralName) for x in authority_cert_issuer
+ ):
+ raise TypeError(
+ "authority_cert_issuer must be a list of GeneralName "
+ "objects"
+ )
+
+ if authority_cert_serial_number is not None and not isinstance(
+ authority_cert_serial_number, int
+ ):
+ raise TypeError("authority_cert_serial_number must be an integer")
+
+ self._key_identifier = key_identifier
+ self._authority_cert_issuer = authority_cert_issuer
+ self._authority_cert_serial_number = authority_cert_serial_number
+
+ # This takes a subset of CertificatePublicKeyTypes because an issuer
+ # cannot have an X25519/X448 key. This introduces some unfortunate
+ # asymmetry that requires typing users to explicitly
+ # narrow their type, but we should make this accurate and not just
+ # convenient.
+ @classmethod
+ def from_issuer_public_key(
+ cls, public_key: CertificateIssuerPublicKeyTypes
+ ) -> AuthorityKeyIdentifier:
+ digest = _key_identifier_from_public_key(public_key)
+ return cls(
+ key_identifier=digest,
+ authority_cert_issuer=None,
+ authority_cert_serial_number=None,
+ )
+
+ @classmethod
+ def from_issuer_subject_key_identifier(
+ cls, ski: SubjectKeyIdentifier
+ ) -> AuthorityKeyIdentifier:
+ return cls(
+ key_identifier=ski.digest,
+ authority_cert_issuer=None,
+ authority_cert_serial_number=None,
+ )
+
+ def __repr__(self) -> str:
+ return (
+ "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, "
+ "authority_cert_issuer={0.authority_cert_issuer}, "
+ "authority_cert_serial_number={0.authority_cert_serial_number}"
+ ")>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, AuthorityKeyIdentifier):
+ return NotImplemented
+
+ return (
+ self.key_identifier == other.key_identifier
+ and self.authority_cert_issuer == other.authority_cert_issuer
+ and self.authority_cert_serial_number
+ == other.authority_cert_serial_number
+ )
+
+ def __hash__(self) -> int:
+ if self.authority_cert_issuer is None:
+ aci = None
+ else:
+ aci = tuple(self.authority_cert_issuer)
+ return hash(
+ (self.key_identifier, aci, self.authority_cert_serial_number)
+ )
+
+ @property
+ def key_identifier(self) -> typing.Optional[bytes]:
+ return self._key_identifier
+
+ @property
+ def authority_cert_issuer(
+ self,
+ ) -> typing.Optional[typing.List[GeneralName]]:
+ return self._authority_cert_issuer
+
+ @property
+ def authority_cert_serial_number(self) -> typing.Optional[int]:
+ return self._authority_cert_serial_number
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class SubjectKeyIdentifier(ExtensionType):
+ oid = ExtensionOID.SUBJECT_KEY_IDENTIFIER
+
+ def __init__(self, digest: bytes) -> None:
+ self._digest = digest
+
+ @classmethod
+ def from_public_key(
+ cls, public_key: CertificatePublicKeyTypes
+ ) -> SubjectKeyIdentifier:
+ return cls(_key_identifier_from_public_key(public_key))
+
+ @property
+ def digest(self) -> bytes:
+ return self._digest
+
+ @property
+ def key_identifier(self) -> bytes:
+ return self._digest
+
+ def __repr__(self) -> str:
+ return f"<SubjectKeyIdentifier(digest={self.digest!r})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, SubjectKeyIdentifier):
+ return NotImplemented
+
+ return constant_time.bytes_eq(self.digest, other.digest)
+
+ def __hash__(self) -> int:
+ return hash(self.digest)
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class AuthorityInformationAccess(ExtensionType):
+ oid = ExtensionOID.AUTHORITY_INFORMATION_ACCESS
+
+ def __init__(
+ self, descriptions: typing.Iterable[AccessDescription]
+ ) -> None:
+ descriptions = list(descriptions)
+ if not all(isinstance(x, AccessDescription) for x in descriptions):
+ raise TypeError(
+ "Every item in the descriptions list must be an "
+ "AccessDescription"
+ )
+
+ self._descriptions = descriptions
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_descriptions")
+
+ def __repr__(self) -> str:
+ return f"<AuthorityInformationAccess({self._descriptions})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, AuthorityInformationAccess):
+ return NotImplemented
+
+ return self._descriptions == other._descriptions
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._descriptions))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class SubjectInformationAccess(ExtensionType):
+ oid = ExtensionOID.SUBJECT_INFORMATION_ACCESS
+
+ def __init__(
+ self, descriptions: typing.Iterable[AccessDescription]
+ ) -> None:
+ descriptions = list(descriptions)
+ if not all(isinstance(x, AccessDescription) for x in descriptions):
+ raise TypeError(
+ "Every item in the descriptions list must be an "
+ "AccessDescription"
+ )
+
+ self._descriptions = descriptions
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_descriptions")
+
+ def __repr__(self) -> str:
+ return f"<SubjectInformationAccess({self._descriptions})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, SubjectInformationAccess):
+ return NotImplemented
+
+ return self._descriptions == other._descriptions
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._descriptions))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class AccessDescription:
+ def __init__(
+ self, access_method: ObjectIdentifier, access_location: GeneralName
+ ) -> None:
+ if not isinstance(access_method, ObjectIdentifier):
+ raise TypeError("access_method must be an ObjectIdentifier")
+
+ if not isinstance(access_location, GeneralName):
+ raise TypeError("access_location must be a GeneralName")
+
+ self._access_method = access_method
+ self._access_location = access_location
+
+ def __repr__(self) -> str:
+ return (
+ "<AccessDescription(access_method={0.access_method}, access_locati"
+ "on={0.access_location})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, AccessDescription):
+ return NotImplemented
+
+ return (
+ self.access_method == other.access_method
+ and self.access_location == other.access_location
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.access_method, self.access_location))
+
+ @property
+ def access_method(self) -> ObjectIdentifier:
+ return self._access_method
+
+ @property
+ def access_location(self) -> GeneralName:
+ return self._access_location
+
+
+class BasicConstraints(ExtensionType):
+ oid = ExtensionOID.BASIC_CONSTRAINTS
+
+ def __init__(self, ca: bool, path_length: typing.Optional[int]) -> None:
+ if not isinstance(ca, bool):
+ raise TypeError("ca must be a boolean value")
+
+ if path_length is not None and not ca:
+ raise ValueError("path_length must be None when ca is False")
+
+ if path_length is not None and (
+ not isinstance(path_length, int) or path_length < 0
+ ):
+ raise TypeError(
+ "path_length must be a non-negative integer or None"
+ )
+
+ self._ca = ca
+ self._path_length = path_length
+
+ @property
+ def ca(self) -> bool:
+ return self._ca
+
+ @property
+ def path_length(self) -> typing.Optional[int]:
+ return self._path_length
+
+ def __repr__(self) -> str:
+ return (
+ "<BasicConstraints(ca={0.ca}, " "path_length={0.path_length})>"
+ ).format(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, BasicConstraints):
+ return NotImplemented
+
+ return self.ca == other.ca and self.path_length == other.path_length
+
+ def __hash__(self) -> int:
+ return hash((self.ca, self.path_length))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class DeltaCRLIndicator(ExtensionType):
+ oid = ExtensionOID.DELTA_CRL_INDICATOR
+
+ def __init__(self, crl_number: int) -> None:
+ if not isinstance(crl_number, int):
+ raise TypeError("crl_number must be an integer")
+
+ self._crl_number = crl_number
+
+ @property
+ def crl_number(self) -> int:
+ return self._crl_number
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DeltaCRLIndicator):
+ return NotImplemented
+
+ return self.crl_number == other.crl_number
+
+ def __hash__(self) -> int:
+ return hash(self.crl_number)
+
+ def __repr__(self) -> str:
+ return f"<DeltaCRLIndicator(crl_number={self.crl_number})>"
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class CRLDistributionPoints(ExtensionType):
+ oid = ExtensionOID.CRL_DISTRIBUTION_POINTS
+
+ def __init__(
+ self, distribution_points: typing.Iterable[DistributionPoint]
+ ) -> None:
+ distribution_points = list(distribution_points)
+ if not all(
+ isinstance(x, DistributionPoint) for x in distribution_points
+ ):
+ raise TypeError(
+ "distribution_points must be a list of DistributionPoint "
+ "objects"
+ )
+
+ self._distribution_points = distribution_points
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods(
+ "_distribution_points"
+ )
+
+ def __repr__(self) -> str:
+ return f"<CRLDistributionPoints({self._distribution_points})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, CRLDistributionPoints):
+ return NotImplemented
+
+ return self._distribution_points == other._distribution_points
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._distribution_points))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class FreshestCRL(ExtensionType):
+ oid = ExtensionOID.FRESHEST_CRL
+
+ def __init__(
+ self, distribution_points: typing.Iterable[DistributionPoint]
+ ) -> None:
+ distribution_points = list(distribution_points)
+ if not all(
+ isinstance(x, DistributionPoint) for x in distribution_points
+ ):
+ raise TypeError(
+ "distribution_points must be a list of DistributionPoint "
+ "objects"
+ )
+
+ self._distribution_points = distribution_points
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods(
+ "_distribution_points"
+ )
+
+ def __repr__(self) -> str:
+ return f"<FreshestCRL({self._distribution_points})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, FreshestCRL):
+ return NotImplemented
+
+ return self._distribution_points == other._distribution_points
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._distribution_points))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class DistributionPoint:
+ def __init__(
+ self,
+ full_name: typing.Optional[typing.Iterable[GeneralName]],
+ relative_name: typing.Optional[RelativeDistinguishedName],
+ reasons: typing.Optional[typing.FrozenSet[ReasonFlags]],
+ crl_issuer: typing.Optional[typing.Iterable[GeneralName]],
+ ) -> None:
+ if full_name and relative_name:
+ raise ValueError(
+ "You cannot provide both full_name and relative_name, at "
+ "least one must be None."
+ )
+ if not full_name and not relative_name and not crl_issuer:
+ raise ValueError(
+ "Either full_name, relative_name or crl_issuer must be "
+ "provided."
+ )
+
+ if full_name is not None:
+ full_name = list(full_name)
+ if not all(isinstance(x, GeneralName) for x in full_name):
+ raise TypeError(
+ "full_name must be a list of GeneralName objects"
+ )
+
+ if relative_name:
+ if not isinstance(relative_name, RelativeDistinguishedName):
+ raise TypeError(
+ "relative_name must be a RelativeDistinguishedName"
+ )
+
+ if crl_issuer is not None:
+ crl_issuer = list(crl_issuer)
+ if not all(isinstance(x, GeneralName) for x in crl_issuer):
+ raise TypeError(
+ "crl_issuer must be None or a list of general names"
+ )
+
+ if reasons and (
+ not isinstance(reasons, frozenset)
+ or not all(isinstance(x, ReasonFlags) for x in reasons)
+ ):
+ raise TypeError("reasons must be None or frozenset of ReasonFlags")
+
+ if reasons and (
+ ReasonFlags.unspecified in reasons
+ or ReasonFlags.remove_from_crl in reasons
+ ):
+ raise ValueError(
+ "unspecified and remove_from_crl are not valid reasons in a "
+ "DistributionPoint"
+ )
+
+ self._full_name = full_name
+ self._relative_name = relative_name
+ self._reasons = reasons
+ self._crl_issuer = crl_issuer
+
+ def __repr__(self) -> str:
+ return (
+ "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela"
+ "tive_name}, reasons={0.reasons}, "
+ "crl_issuer={0.crl_issuer})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DistributionPoint):
+ return NotImplemented
+
+ return (
+ self.full_name == other.full_name
+ and self.relative_name == other.relative_name
+ and self.reasons == other.reasons
+ and self.crl_issuer == other.crl_issuer
+ )
+
+ def __hash__(self) -> int:
+ if self.full_name is not None:
+ fn: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
+ self.full_name
+ )
+ else:
+ fn = None
+
+ if self.crl_issuer is not None:
+ crl_issuer: typing.Optional[
+ typing.Tuple[GeneralName, ...]
+ ] = tuple(self.crl_issuer)
+ else:
+ crl_issuer = None
+
+ return hash((fn, self.relative_name, self.reasons, crl_issuer))
+
+ @property
+ def full_name(self) -> typing.Optional[typing.List[GeneralName]]:
+ return self._full_name
+
+ @property
+ def relative_name(self) -> typing.Optional[RelativeDistinguishedName]:
+ return self._relative_name
+
+ @property
+ def reasons(self) -> typing.Optional[typing.FrozenSet[ReasonFlags]]:
+ return self._reasons
+
+ @property
+ def crl_issuer(self) -> typing.Optional[typing.List[GeneralName]]:
+ return self._crl_issuer
+
+
+class ReasonFlags(utils.Enum):
+ unspecified = "unspecified"
+ key_compromise = "keyCompromise"
+ ca_compromise = "cACompromise"
+ affiliation_changed = "affiliationChanged"
+ superseded = "superseded"
+ cessation_of_operation = "cessationOfOperation"
+ certificate_hold = "certificateHold"
+ privilege_withdrawn = "privilegeWithdrawn"
+ aa_compromise = "aACompromise"
+ remove_from_crl = "removeFromCRL"
+
+
+# These are distribution point bit string mappings. Not to be confused with
+# CRLReason reason flags bit string mappings.
+# ReasonFlags ::= BIT STRING {
+# unused (0),
+# keyCompromise (1),
+# cACompromise (2),
+# affiliationChanged (3),
+# superseded (4),
+# cessationOfOperation (5),
+# certificateHold (6),
+# privilegeWithdrawn (7),
+# aACompromise (8) }
+_REASON_BIT_MAPPING = {
+ 1: ReasonFlags.key_compromise,
+ 2: ReasonFlags.ca_compromise,
+ 3: ReasonFlags.affiliation_changed,
+ 4: ReasonFlags.superseded,
+ 5: ReasonFlags.cessation_of_operation,
+ 6: ReasonFlags.certificate_hold,
+ 7: ReasonFlags.privilege_withdrawn,
+ 8: ReasonFlags.aa_compromise,
+}
+
+_CRLREASONFLAGS = {
+ ReasonFlags.key_compromise: 1,
+ ReasonFlags.ca_compromise: 2,
+ ReasonFlags.affiliation_changed: 3,
+ ReasonFlags.superseded: 4,
+ ReasonFlags.cessation_of_operation: 5,
+ ReasonFlags.certificate_hold: 6,
+ ReasonFlags.privilege_withdrawn: 7,
+ ReasonFlags.aa_compromise: 8,
+}
+
+
+class PolicyConstraints(ExtensionType):
+ oid = ExtensionOID.POLICY_CONSTRAINTS
+
+ def __init__(
+ self,
+ require_explicit_policy: typing.Optional[int],
+ inhibit_policy_mapping: typing.Optional[int],
+ ) -> None:
+ if require_explicit_policy is not None and not isinstance(
+ require_explicit_policy, int
+ ):
+ raise TypeError(
+ "require_explicit_policy must be a non-negative integer or "
+ "None"
+ )
+
+ if inhibit_policy_mapping is not None and not isinstance(
+ inhibit_policy_mapping, int
+ ):
+ raise TypeError(
+ "inhibit_policy_mapping must be a non-negative integer or None"
+ )
+
+ if inhibit_policy_mapping is None and require_explicit_policy is None:
+ raise ValueError(
+ "At least one of require_explicit_policy and "
+ "inhibit_policy_mapping must not be None"
+ )
+
+ self._require_explicit_policy = require_explicit_policy
+ self._inhibit_policy_mapping = inhibit_policy_mapping
+
+ def __repr__(self) -> str:
+ return (
+ "<PolicyConstraints(require_explicit_policy={0.require_explicit"
+ "_policy}, inhibit_policy_mapping={0.inhibit_policy_"
+ "mapping})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PolicyConstraints):
+ return NotImplemented
+
+ return (
+ self.require_explicit_policy == other.require_explicit_policy
+ and self.inhibit_policy_mapping == other.inhibit_policy_mapping
+ )
+
+ def __hash__(self) -> int:
+ return hash(
+ (self.require_explicit_policy, self.inhibit_policy_mapping)
+ )
+
+ @property
+ def require_explicit_policy(self) -> typing.Optional[int]:
+ return self._require_explicit_policy
+
+ @property
+ def inhibit_policy_mapping(self) -> typing.Optional[int]:
+ return self._inhibit_policy_mapping
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class CertificatePolicies(ExtensionType):
+ oid = ExtensionOID.CERTIFICATE_POLICIES
+
+ def __init__(self, policies: typing.Iterable[PolicyInformation]) -> None:
+ policies = list(policies)
+ if not all(isinstance(x, PolicyInformation) for x in policies):
+ raise TypeError(
+ "Every item in the policies list must be a "
+ "PolicyInformation"
+ )
+
+ self._policies = policies
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_policies")
+
+ def __repr__(self) -> str:
+ return f"<CertificatePolicies({self._policies})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, CertificatePolicies):
+ return NotImplemented
+
+ return self._policies == other._policies
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._policies))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class PolicyInformation:
+ def __init__(
+ self,
+ policy_identifier: ObjectIdentifier,
+ policy_qualifiers: typing.Optional[
+ typing.Iterable[typing.Union[str, UserNotice]]
+ ],
+ ) -> None:
+ if not isinstance(policy_identifier, ObjectIdentifier):
+ raise TypeError("policy_identifier must be an ObjectIdentifier")
+
+ self._policy_identifier = policy_identifier
+
+ if policy_qualifiers is not None:
+ policy_qualifiers = list(policy_qualifiers)
+ if not all(
+ isinstance(x, (str, UserNotice)) for x in policy_qualifiers
+ ):
+ raise TypeError(
+ "policy_qualifiers must be a list of strings and/or "
+ "UserNotice objects or None"
+ )
+
+ self._policy_qualifiers = policy_qualifiers
+
+ def __repr__(self) -> str:
+ return (
+ "<PolicyInformation(policy_identifier={0.policy_identifier}, polic"
+ "y_qualifiers={0.policy_qualifiers})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PolicyInformation):
+ return NotImplemented
+
+ return (
+ self.policy_identifier == other.policy_identifier
+ and self.policy_qualifiers == other.policy_qualifiers
+ )
+
+ def __hash__(self) -> int:
+ if self.policy_qualifiers is not None:
+ pq: typing.Optional[
+ typing.Tuple[typing.Union[str, UserNotice], ...]
+ ] = tuple(self.policy_qualifiers)
+ else:
+ pq = None
+
+ return hash((self.policy_identifier, pq))
+
+ @property
+ def policy_identifier(self) -> ObjectIdentifier:
+ return self._policy_identifier
+
+ @property
+ def policy_qualifiers(
+ self,
+ ) -> typing.Optional[typing.List[typing.Union[str, UserNotice]]]:
+ return self._policy_qualifiers
+
+
+class UserNotice:
+ def __init__(
+ self,
+ notice_reference: typing.Optional[NoticeReference],
+ explicit_text: typing.Optional[str],
+ ) -> None:
+ if notice_reference and not isinstance(
+ notice_reference, NoticeReference
+ ):
+ raise TypeError(
+ "notice_reference must be None or a NoticeReference"
+ )
+
+ self._notice_reference = notice_reference
+ self._explicit_text = explicit_text
+
+ def __repr__(self) -> str:
+ return (
+ "<UserNotice(notice_reference={0.notice_reference}, explicit_text="
+ "{0.explicit_text!r})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, UserNotice):
+ return NotImplemented
+
+ return (
+ self.notice_reference == other.notice_reference
+ and self.explicit_text == other.explicit_text
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.notice_reference, self.explicit_text))
+
+ @property
+ def notice_reference(self) -> typing.Optional[NoticeReference]:
+ return self._notice_reference
+
+ @property
+ def explicit_text(self) -> typing.Optional[str]:
+ return self._explicit_text
+
+
+class NoticeReference:
+ def __init__(
+ self,
+ organization: typing.Optional[str],
+ notice_numbers: typing.Iterable[int],
+ ) -> None:
+ self._organization = organization
+ notice_numbers = list(notice_numbers)
+ if not all(isinstance(x, int) for x in notice_numbers):
+ raise TypeError("notice_numbers must be a list of integers")
+
+ self._notice_numbers = notice_numbers
+
+ def __repr__(self) -> str:
+ return (
+ "<NoticeReference(organization={0.organization!r}, notice_numbers="
+ "{0.notice_numbers})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, NoticeReference):
+ return NotImplemented
+
+ return (
+ self.organization == other.organization
+ and self.notice_numbers == other.notice_numbers
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.organization, tuple(self.notice_numbers)))
+
+ @property
+ def organization(self) -> typing.Optional[str]:
+ return self._organization
+
+ @property
+ def notice_numbers(self) -> typing.List[int]:
+ return self._notice_numbers
+
+
+class ExtendedKeyUsage(ExtensionType):
+ oid = ExtensionOID.EXTENDED_KEY_USAGE
+
+ def __init__(self, usages: typing.Iterable[ObjectIdentifier]) -> None:
+ usages = list(usages)
+ if not all(isinstance(x, ObjectIdentifier) for x in usages):
+ raise TypeError(
+ "Every item in the usages list must be an ObjectIdentifier"
+ )
+
+ self._usages = usages
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_usages")
+
+ def __repr__(self) -> str:
+ return f"<ExtendedKeyUsage({self._usages})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, ExtendedKeyUsage):
+ return NotImplemented
+
+ return self._usages == other._usages
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._usages))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class OCSPNoCheck(ExtensionType):
+ oid = ExtensionOID.OCSP_NO_CHECK
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, OCSPNoCheck):
+ return NotImplemented
+
+ return True
+
+ def __hash__(self) -> int:
+ return hash(OCSPNoCheck)
+
+ def __repr__(self) -> str:
+ return "<OCSPNoCheck()>"
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class PrecertPoison(ExtensionType):
+ oid = ExtensionOID.PRECERT_POISON
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PrecertPoison):
+ return NotImplemented
+
+ return True
+
+ def __hash__(self) -> int:
+ return hash(PrecertPoison)
+
+ def __repr__(self) -> str:
+ return "<PrecertPoison()>"
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class TLSFeature(ExtensionType):
+ oid = ExtensionOID.TLS_FEATURE
+
+ def __init__(self, features: typing.Iterable[TLSFeatureType]) -> None:
+ features = list(features)
+ if (
+ not all(isinstance(x, TLSFeatureType) for x in features)
+ or len(features) == 0
+ ):
+ raise TypeError(
+ "features must be a list of elements from the TLSFeatureType "
+ "enum"
+ )
+
+ self._features = features
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_features")
+
+ def __repr__(self) -> str:
+ return f"<TLSFeature(features={self._features})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, TLSFeature):
+ return NotImplemented
+
+ return self._features == other._features
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._features))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class TLSFeatureType(utils.Enum):
+ # status_request is defined in RFC 6066 and is used for what is commonly
+ # called OCSP Must-Staple when present in the TLS Feature extension in an
+ # X.509 certificate.
+ status_request = 5
+ # status_request_v2 is defined in RFC 6961 and allows multiple OCSP
+ # responses to be provided. It is not currently in use by clients or
+ # servers.
+ status_request_v2 = 17
+
+
+_TLS_FEATURE_TYPE_TO_ENUM = {x.value: x for x in TLSFeatureType}
+
+
+class InhibitAnyPolicy(ExtensionType):
+ oid = ExtensionOID.INHIBIT_ANY_POLICY
+
+ def __init__(self, skip_certs: int) -> None:
+ if not isinstance(skip_certs, int):
+ raise TypeError("skip_certs must be an integer")
+
+ if skip_certs < 0:
+ raise ValueError("skip_certs must be a non-negative integer")
+
+ self._skip_certs = skip_certs
+
+ def __repr__(self) -> str:
+ return f"<InhibitAnyPolicy(skip_certs={self.skip_certs})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, InhibitAnyPolicy):
+ return NotImplemented
+
+ return self.skip_certs == other.skip_certs
+
+ def __hash__(self) -> int:
+ return hash(self.skip_certs)
+
+ @property
+ def skip_certs(self) -> int:
+ return self._skip_certs
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class KeyUsage(ExtensionType):
+ oid = ExtensionOID.KEY_USAGE
+
+ def __init__(
+ self,
+ digital_signature: bool,
+ content_commitment: bool,
+ key_encipherment: bool,
+ data_encipherment: bool,
+ key_agreement: bool,
+ key_cert_sign: bool,
+ crl_sign: bool,
+ encipher_only: bool,
+ decipher_only: bool,
+ ) -> None:
+ if not key_agreement and (encipher_only or decipher_only):
+ raise ValueError(
+ "encipher_only and decipher_only can only be true when "
+ "key_agreement is true"
+ )
+
+ self._digital_signature = digital_signature
+ self._content_commitment = content_commitment
+ self._key_encipherment = key_encipherment
+ self._data_encipherment = data_encipherment
+ self._key_agreement = key_agreement
+ self._key_cert_sign = key_cert_sign
+ self._crl_sign = crl_sign
+ self._encipher_only = encipher_only
+ self._decipher_only = decipher_only
+
+ @property
+ def digital_signature(self) -> bool:
+ return self._digital_signature
+
+ @property
+ def content_commitment(self) -> bool:
+ return self._content_commitment
+
+ @property
+ def key_encipherment(self) -> bool:
+ return self._key_encipherment
+
+ @property
+ def data_encipherment(self) -> bool:
+ return self._data_encipherment
+
+ @property
+ def key_agreement(self) -> bool:
+ return self._key_agreement
+
+ @property
+ def key_cert_sign(self) -> bool:
+ return self._key_cert_sign
+
+ @property
+ def crl_sign(self) -> bool:
+ return self._crl_sign
+
+ @property
+ def encipher_only(self) -> bool:
+ if not self.key_agreement:
+ raise ValueError(
+ "encipher_only is undefined unless key_agreement is true"
+ )
+ else:
+ return self._encipher_only
+
+ @property
+ def decipher_only(self) -> bool:
+ if not self.key_agreement:
+ raise ValueError(
+ "decipher_only is undefined unless key_agreement is true"
+ )
+ else:
+ return self._decipher_only
+
+ def __repr__(self) -> str:
+ try:
+ encipher_only = self.encipher_only
+ decipher_only = self.decipher_only
+ except ValueError:
+ # Users found None confusing because even though encipher/decipher
+ # have no meaning unless key_agreement is true, to construct an
+ # instance of the class you still need to pass False.
+ encipher_only = False
+ decipher_only = False
+
+ return (
+ "<KeyUsage(digital_signature={0.digital_signature}, "
+ "content_commitment={0.content_commitment}, "
+ "key_encipherment={0.key_encipherment}, "
+ "data_encipherment={0.data_encipherment}, "
+ "key_agreement={0.key_agreement}, "
+ "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, "
+ "encipher_only={1}, decipher_only={2})>"
+ ).format(self, encipher_only, decipher_only)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, KeyUsage):
+ return NotImplemented
+
+ return (
+ self.digital_signature == other.digital_signature
+ and self.content_commitment == other.content_commitment
+ and self.key_encipherment == other.key_encipherment
+ and self.data_encipherment == other.data_encipherment
+ and self.key_agreement == other.key_agreement
+ and self.key_cert_sign == other.key_cert_sign
+ and self.crl_sign == other.crl_sign
+ and self._encipher_only == other._encipher_only
+ and self._decipher_only == other._decipher_only
+ )
+
+ def __hash__(self) -> int:
+ return hash(
+ (
+ self.digital_signature,
+ self.content_commitment,
+ self.key_encipherment,
+ self.data_encipherment,
+ self.key_agreement,
+ self.key_cert_sign,
+ self.crl_sign,
+ self._encipher_only,
+ self._decipher_only,
+ )
+ )
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class NameConstraints(ExtensionType):
+ oid = ExtensionOID.NAME_CONSTRAINTS
+
+ def __init__(
+ self,
+ permitted_subtrees: typing.Optional[typing.Iterable[GeneralName]],
+ excluded_subtrees: typing.Optional[typing.Iterable[GeneralName]],
+ ) -> None:
+ if permitted_subtrees is not None:
+ permitted_subtrees = list(permitted_subtrees)
+ if not permitted_subtrees:
+ raise ValueError(
+ "permitted_subtrees must be a non-empty list or None"
+ )
+ if not all(isinstance(x, GeneralName) for x in permitted_subtrees):
+ raise TypeError(
+ "permitted_subtrees must be a list of GeneralName objects "
+ "or None"
+ )
+
+ self._validate_tree(permitted_subtrees)
+
+ if excluded_subtrees is not None:
+ excluded_subtrees = list(excluded_subtrees)
+ if not excluded_subtrees:
+ raise ValueError(
+ "excluded_subtrees must be a non-empty list or None"
+ )
+ if not all(isinstance(x, GeneralName) for x in excluded_subtrees):
+ raise TypeError(
+ "excluded_subtrees must be a list of GeneralName objects "
+ "or None"
+ )
+
+ self._validate_tree(excluded_subtrees)
+
+ if permitted_subtrees is None and excluded_subtrees is None:
+ raise ValueError(
+ "At least one of permitted_subtrees and excluded_subtrees "
+ "must not be None"
+ )
+
+ self._permitted_subtrees = permitted_subtrees
+ self._excluded_subtrees = excluded_subtrees
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, NameConstraints):
+ return NotImplemented
+
+ return (
+ self.excluded_subtrees == other.excluded_subtrees
+ and self.permitted_subtrees == other.permitted_subtrees
+ )
+
+ def _validate_tree(self, tree: typing.Iterable[GeneralName]) -> None:
+ self._validate_ip_name(tree)
+ self._validate_dns_name(tree)
+
+ def _validate_ip_name(self, tree: typing.Iterable[GeneralName]) -> None:
+ if any(
+ isinstance(name, IPAddress)
+ and not isinstance(
+ name.value, (ipaddress.IPv4Network, ipaddress.IPv6Network)
+ )
+ for name in tree
+ ):
+ raise TypeError(
+ "IPAddress name constraints must be an IPv4Network or"
+ " IPv6Network object"
+ )
+
+ def _validate_dns_name(self, tree: typing.Iterable[GeneralName]) -> None:
+ if any(
+ isinstance(name, DNSName) and "*" in name.value for name in tree
+ ):
+ raise ValueError(
+ "DNSName name constraints must not contain the '*' wildcard"
+ " character"
+ )
+
+ def __repr__(self) -> str:
+ return (
+ "<NameConstraints(permitted_subtrees={0.permitted_subtrees}, "
+ "excluded_subtrees={0.excluded_subtrees})>".format(self)
+ )
+
+ def __hash__(self) -> int:
+ if self.permitted_subtrees is not None:
+ ps: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
+ self.permitted_subtrees
+ )
+ else:
+ ps = None
+
+ if self.excluded_subtrees is not None:
+ es: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
+ self.excluded_subtrees
+ )
+ else:
+ es = None
+
+ return hash((ps, es))
+
+ @property
+ def permitted_subtrees(
+ self,
+ ) -> typing.Optional[typing.List[GeneralName]]:
+ return self._permitted_subtrees
+
+ @property
+ def excluded_subtrees(
+ self,
+ ) -> typing.Optional[typing.List[GeneralName]]:
+ return self._excluded_subtrees
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class Extension(typing.Generic[ExtensionTypeVar]):
+ def __init__(
+ self, oid: ObjectIdentifier, critical: bool, value: ExtensionTypeVar
+ ) -> None:
+ if not isinstance(oid, ObjectIdentifier):
+ raise TypeError(
+ "oid argument must be an ObjectIdentifier instance."
+ )
+
+ if not isinstance(critical, bool):
+ raise TypeError("critical must be a boolean value")
+
+ self._oid = oid
+ self._critical = critical
+ self._value = value
+
+ @property
+ def oid(self) -> ObjectIdentifier:
+ return self._oid
+
+ @property
+ def critical(self) -> bool:
+ return self._critical
+
+ @property
+ def value(self) -> ExtensionTypeVar:
+ return self._value
+
+ def __repr__(self) -> str:
+ return (
+ "<Extension(oid={0.oid}, critical={0.critical}, "
+ "value={0.value})>"
+ ).format(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, Extension):
+ return NotImplemented
+
+ return (
+ self.oid == other.oid
+ and self.critical == other.critical
+ and self.value == other.value
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.oid, self.critical, self.value))
+
+
+class GeneralNames:
+ def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
+ general_names = list(general_names)
+ if not all(isinstance(x, GeneralName) for x in general_names):
+ raise TypeError(
+ "Every item in the general_names list must be an "
+ "object conforming to the GeneralName interface"
+ )
+
+ self._general_names = general_names
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[UniformResourceIdentifier],
+ typing.Type[RFC822Name],
+ ],
+ ) -> typing.List[str]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[DirectoryName],
+ ) -> typing.List[Name]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[RegisteredID],
+ ) -> typing.List[ObjectIdentifier]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[IPAddress]
+ ) -> typing.List[_IPAddressTypes]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[OtherName]
+ ) -> typing.List[OtherName]:
+ ...
+
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[DirectoryName],
+ typing.Type[IPAddress],
+ typing.Type[OtherName],
+ typing.Type[RFC822Name],
+ typing.Type[RegisteredID],
+ typing.Type[UniformResourceIdentifier],
+ ],
+ ) -> typing.Union[
+ typing.List[_IPAddressTypes],
+ typing.List[str],
+ typing.List[OtherName],
+ typing.List[Name],
+ typing.List[ObjectIdentifier],
+ ]:
+ # Return the value of each GeneralName, except for OtherName instances
+ # which we return directly because it has two important properties not
+ # just one value.
+ objs = (i for i in self if isinstance(i, type))
+ if type != OtherName:
+ return [i.value for i in objs]
+ return list(objs)
+
+ def __repr__(self) -> str:
+ return f"<GeneralNames({self._general_names})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, GeneralNames):
+ return NotImplemented
+
+ return self._general_names == other._general_names
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._general_names))
+
+
+class SubjectAlternativeName(ExtensionType):
+ oid = ExtensionOID.SUBJECT_ALTERNATIVE_NAME
+
+ def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
+ self._general_names = GeneralNames(general_names)
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[UniformResourceIdentifier],
+ typing.Type[RFC822Name],
+ ],
+ ) -> typing.List[str]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[DirectoryName],
+ ) -> typing.List[Name]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[RegisteredID],
+ ) -> typing.List[ObjectIdentifier]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[IPAddress]
+ ) -> typing.List[_IPAddressTypes]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[OtherName]
+ ) -> typing.List[OtherName]:
+ ...
+
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[DirectoryName],
+ typing.Type[IPAddress],
+ typing.Type[OtherName],
+ typing.Type[RFC822Name],
+ typing.Type[RegisteredID],
+ typing.Type[UniformResourceIdentifier],
+ ],
+ ) -> typing.Union[
+ typing.List[_IPAddressTypes],
+ typing.List[str],
+ typing.List[OtherName],
+ typing.List[Name],
+ typing.List[ObjectIdentifier],
+ ]:
+ return self._general_names.get_values_for_type(type)
+
+ def __repr__(self) -> str:
+ return f"<SubjectAlternativeName({self._general_names})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, SubjectAlternativeName):
+ return NotImplemented
+
+ return self._general_names == other._general_names
+
+ def __hash__(self) -> int:
+ return hash(self._general_names)
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class IssuerAlternativeName(ExtensionType):
+ oid = ExtensionOID.ISSUER_ALTERNATIVE_NAME
+
+ def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
+ self._general_names = GeneralNames(general_names)
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[UniformResourceIdentifier],
+ typing.Type[RFC822Name],
+ ],
+ ) -> typing.List[str]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[DirectoryName],
+ ) -> typing.List[Name]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[RegisteredID],
+ ) -> typing.List[ObjectIdentifier]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[IPAddress]
+ ) -> typing.List[_IPAddressTypes]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[OtherName]
+ ) -> typing.List[OtherName]:
+ ...
+
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[DirectoryName],
+ typing.Type[IPAddress],
+ typing.Type[OtherName],
+ typing.Type[RFC822Name],
+ typing.Type[RegisteredID],
+ typing.Type[UniformResourceIdentifier],
+ ],
+ ) -> typing.Union[
+ typing.List[_IPAddressTypes],
+ typing.List[str],
+ typing.List[OtherName],
+ typing.List[Name],
+ typing.List[ObjectIdentifier],
+ ]:
+ return self._general_names.get_values_for_type(type)
+
+ def __repr__(self) -> str:
+ return f"<IssuerAlternativeName({self._general_names})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, IssuerAlternativeName):
+ return NotImplemented
+
+ return self._general_names == other._general_names
+
+ def __hash__(self) -> int:
+ return hash(self._general_names)
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class CertificateIssuer(ExtensionType):
+ oid = CRLEntryExtensionOID.CERTIFICATE_ISSUER
+
+ def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
+ self._general_names = GeneralNames(general_names)
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[UniformResourceIdentifier],
+ typing.Type[RFC822Name],
+ ],
+ ) -> typing.List[str]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[DirectoryName],
+ ) -> typing.List[Name]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self,
+ type: typing.Type[RegisteredID],
+ ) -> typing.List[ObjectIdentifier]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[IPAddress]
+ ) -> typing.List[_IPAddressTypes]:
+ ...
+
+ @typing.overload
+ def get_values_for_type(
+ self, type: typing.Type[OtherName]
+ ) -> typing.List[OtherName]:
+ ...
+
+ def get_values_for_type(
+ self,
+ type: typing.Union[
+ typing.Type[DNSName],
+ typing.Type[DirectoryName],
+ typing.Type[IPAddress],
+ typing.Type[OtherName],
+ typing.Type[RFC822Name],
+ typing.Type[RegisteredID],
+ typing.Type[UniformResourceIdentifier],
+ ],
+ ) -> typing.Union[
+ typing.List[_IPAddressTypes],
+ typing.List[str],
+ typing.List[OtherName],
+ typing.List[Name],
+ typing.List[ObjectIdentifier],
+ ]:
+ return self._general_names.get_values_for_type(type)
+
+ def __repr__(self) -> str:
+ return f"<CertificateIssuer({self._general_names})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, CertificateIssuer):
+ return NotImplemented
+
+ return self._general_names == other._general_names
+
+ def __hash__(self) -> int:
+ return hash(self._general_names)
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class CRLReason(ExtensionType):
+ oid = CRLEntryExtensionOID.CRL_REASON
+
+ def __init__(self, reason: ReasonFlags) -> None:
+ if not isinstance(reason, ReasonFlags):
+ raise TypeError("reason must be an element from ReasonFlags")
+
+ self._reason = reason
+
+ def __repr__(self) -> str:
+ return f"<CRLReason(reason={self._reason})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, CRLReason):
+ return NotImplemented
+
+ return self.reason == other.reason
+
+ def __hash__(self) -> int:
+ return hash(self.reason)
+
+ @property
+ def reason(self) -> ReasonFlags:
+ return self._reason
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class InvalidityDate(ExtensionType):
+ oid = CRLEntryExtensionOID.INVALIDITY_DATE
+
+ def __init__(self, invalidity_date: datetime.datetime) -> None:
+ if not isinstance(invalidity_date, datetime.datetime):
+ raise TypeError("invalidity_date must be a datetime.datetime")
+
+ self._invalidity_date = invalidity_date
+
+ def __repr__(self) -> str:
+ return "<InvalidityDate(invalidity_date={})>".format(
+ self._invalidity_date
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, InvalidityDate):
+ return NotImplemented
+
+ return self.invalidity_date == other.invalidity_date
+
+ def __hash__(self) -> int:
+ return hash(self.invalidity_date)
+
+ @property
+ def invalidity_date(self) -> datetime.datetime:
+ return self._invalidity_date
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class PrecertificateSignedCertificateTimestamps(ExtensionType):
+ oid = ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS
+
+ def __init__(
+ self,
+ signed_certificate_timestamps: typing.Iterable[
+ SignedCertificateTimestamp
+ ],
+ ) -> None:
+ signed_certificate_timestamps = list(signed_certificate_timestamps)
+ if not all(
+ isinstance(sct, SignedCertificateTimestamp)
+ for sct in signed_certificate_timestamps
+ ):
+ raise TypeError(
+ "Every item in the signed_certificate_timestamps list must be "
+ "a SignedCertificateTimestamp"
+ )
+ self._signed_certificate_timestamps = signed_certificate_timestamps
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods(
+ "_signed_certificate_timestamps"
+ )
+
+ def __repr__(self) -> str:
+ return "<PrecertificateSignedCertificateTimestamps({})>".format(
+ list(self)
+ )
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._signed_certificate_timestamps))
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, PrecertificateSignedCertificateTimestamps):
+ return NotImplemented
+
+ return (
+ self._signed_certificate_timestamps
+ == other._signed_certificate_timestamps
+ )
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class SignedCertificateTimestamps(ExtensionType):
+ oid = ExtensionOID.SIGNED_CERTIFICATE_TIMESTAMPS
+
+ def __init__(
+ self,
+ signed_certificate_timestamps: typing.Iterable[
+ SignedCertificateTimestamp
+ ],
+ ) -> None:
+ signed_certificate_timestamps = list(signed_certificate_timestamps)
+ if not all(
+ isinstance(sct, SignedCertificateTimestamp)
+ for sct in signed_certificate_timestamps
+ ):
+ raise TypeError(
+ "Every item in the signed_certificate_timestamps list must be "
+ "a SignedCertificateTimestamp"
+ )
+ self._signed_certificate_timestamps = signed_certificate_timestamps
+
+ __len__, __iter__, __getitem__ = _make_sequence_methods(
+ "_signed_certificate_timestamps"
+ )
+
+ def __repr__(self) -> str:
+ return f"<SignedCertificateTimestamps({list(self)})>"
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._signed_certificate_timestamps))
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, SignedCertificateTimestamps):
+ return NotImplemented
+
+ return (
+ self._signed_certificate_timestamps
+ == other._signed_certificate_timestamps
+ )
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class OCSPNonce(ExtensionType):
+ oid = OCSPExtensionOID.NONCE
+
+ def __init__(self, nonce: bytes) -> None:
+ if not isinstance(nonce, bytes):
+ raise TypeError("nonce must be bytes")
+
+ self._nonce = nonce
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, OCSPNonce):
+ return NotImplemented
+
+ return self.nonce == other.nonce
+
+ def __hash__(self) -> int:
+ return hash(self.nonce)
+
+ def __repr__(self) -> str:
+ return f"<OCSPNonce(nonce={self.nonce!r})>"
+
+ @property
+ def nonce(self) -> bytes:
+ return self._nonce
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class OCSPAcceptableResponses(ExtensionType):
+ oid = OCSPExtensionOID.ACCEPTABLE_RESPONSES
+
+ def __init__(self, responses: typing.Iterable[ObjectIdentifier]) -> None:
+ responses = list(responses)
+ if any(not isinstance(r, ObjectIdentifier) for r in responses):
+ raise TypeError("All responses must be ObjectIdentifiers")
+
+ self._responses = responses
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, OCSPAcceptableResponses):
+ return NotImplemented
+
+ return self._responses == other._responses
+
+ def __hash__(self) -> int:
+ return hash(tuple(self._responses))
+
+ def __repr__(self) -> str:
+ return f"<OCSPAcceptableResponses(responses={self._responses})>"
+
+ def __iter__(self) -> typing.Iterator[ObjectIdentifier]:
+ return iter(self._responses)
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class IssuingDistributionPoint(ExtensionType):
+ oid = ExtensionOID.ISSUING_DISTRIBUTION_POINT
+
+ def __init__(
+ self,
+ full_name: typing.Optional[typing.Iterable[GeneralName]],
+ relative_name: typing.Optional[RelativeDistinguishedName],
+ only_contains_user_certs: bool,
+ only_contains_ca_certs: bool,
+ only_some_reasons: typing.Optional[typing.FrozenSet[ReasonFlags]],
+ indirect_crl: bool,
+ only_contains_attribute_certs: bool,
+ ) -> None:
+ if full_name is not None:
+ full_name = list(full_name)
+
+ if only_some_reasons and (
+ not isinstance(only_some_reasons, frozenset)
+ or not all(isinstance(x, ReasonFlags) for x in only_some_reasons)
+ ):
+ raise TypeError(
+ "only_some_reasons must be None or frozenset of ReasonFlags"
+ )
+
+ if only_some_reasons and (
+ ReasonFlags.unspecified in only_some_reasons
+ or ReasonFlags.remove_from_crl in only_some_reasons
+ ):
+ raise ValueError(
+ "unspecified and remove_from_crl are not valid reasons in an "
+ "IssuingDistributionPoint"
+ )
+
+ if not (
+ isinstance(only_contains_user_certs, bool)
+ and isinstance(only_contains_ca_certs, bool)
+ and isinstance(indirect_crl, bool)
+ and isinstance(only_contains_attribute_certs, bool)
+ ):
+ raise TypeError(
+ "only_contains_user_certs, only_contains_ca_certs, "
+ "indirect_crl and only_contains_attribute_certs "
+ "must all be boolean."
+ )
+
+ crl_constraints = [
+ only_contains_user_certs,
+ only_contains_ca_certs,
+ indirect_crl,
+ only_contains_attribute_certs,
+ ]
+
+ if len([x for x in crl_constraints if x]) > 1:
+ raise ValueError(
+ "Only one of the following can be set to True: "
+ "only_contains_user_certs, only_contains_ca_certs, "
+ "indirect_crl, only_contains_attribute_certs"
+ )
+
+ if not any(
+ [
+ only_contains_user_certs,
+ only_contains_ca_certs,
+ indirect_crl,
+ only_contains_attribute_certs,
+ full_name,
+ relative_name,
+ only_some_reasons,
+ ]
+ ):
+ raise ValueError(
+ "Cannot create empty extension: "
+ "if only_contains_user_certs, only_contains_ca_certs, "
+ "indirect_crl, and only_contains_attribute_certs are all False"
+ ", then either full_name, relative_name, or only_some_reasons "
+ "must have a value."
+ )
+
+ self._only_contains_user_certs = only_contains_user_certs
+ self._only_contains_ca_certs = only_contains_ca_certs
+ self._indirect_crl = indirect_crl
+ self._only_contains_attribute_certs = only_contains_attribute_certs
+ self._only_some_reasons = only_some_reasons
+ self._full_name = full_name
+ self._relative_name = relative_name
+
+ def __repr__(self) -> str:
+ return (
+ "<IssuingDistributionPoint(full_name={0.full_name}, "
+ "relative_name={0.relative_name}, "
+ "only_contains_user_certs={0.only_contains_user_certs}, "
+ "only_contains_ca_certs={0.only_contains_ca_certs}, "
+ "only_some_reasons={0.only_some_reasons}, "
+ "indirect_crl={0.indirect_crl}, "
+ "only_contains_attribute_certs="
+ "{0.only_contains_attribute_certs})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, IssuingDistributionPoint):
+ return NotImplemented
+
+ return (
+ self.full_name == other.full_name
+ and self.relative_name == other.relative_name
+ and self.only_contains_user_certs == other.only_contains_user_certs
+ and self.only_contains_ca_certs == other.only_contains_ca_certs
+ and self.only_some_reasons == other.only_some_reasons
+ and self.indirect_crl == other.indirect_crl
+ and self.only_contains_attribute_certs
+ == other.only_contains_attribute_certs
+ )
+
+ def __hash__(self) -> int:
+ return hash(
+ (
+ self.full_name,
+ self.relative_name,
+ self.only_contains_user_certs,
+ self.only_contains_ca_certs,
+ self.only_some_reasons,
+ self.indirect_crl,
+ self.only_contains_attribute_certs,
+ )
+ )
+
+ @property
+ def full_name(self) -> typing.Optional[typing.List[GeneralName]]:
+ return self._full_name
+
+ @property
+ def relative_name(self) -> typing.Optional[RelativeDistinguishedName]:
+ return self._relative_name
+
+ @property
+ def only_contains_user_certs(self) -> bool:
+ return self._only_contains_user_certs
+
+ @property
+ def only_contains_ca_certs(self) -> bool:
+ return self._only_contains_ca_certs
+
+ @property
+ def only_some_reasons(
+ self,
+ ) -> typing.Optional[typing.FrozenSet[ReasonFlags]]:
+ return self._only_some_reasons
+
+ @property
+ def indirect_crl(self) -> bool:
+ return self._indirect_crl
+
+ @property
+ def only_contains_attribute_certs(self) -> bool:
+ return self._only_contains_attribute_certs
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class MSCertificateTemplate(ExtensionType):
+ oid = ExtensionOID.MS_CERTIFICATE_TEMPLATE
+
+ def __init__(
+ self,
+ template_id: ObjectIdentifier,
+ major_version: typing.Optional[int],
+ minor_version: typing.Optional[int],
+ ) -> None:
+ if not isinstance(template_id, ObjectIdentifier):
+ raise TypeError("oid must be an ObjectIdentifier")
+ self._template_id = template_id
+ if (
+ major_version is not None and not isinstance(major_version, int)
+ ) or (
+ minor_version is not None and not isinstance(minor_version, int)
+ ):
+ raise TypeError(
+ "major_version and minor_version must be integers or None"
+ )
+ self._major_version = major_version
+ self._minor_version = minor_version
+
+ @property
+ def template_id(self) -> ObjectIdentifier:
+ return self._template_id
+
+ @property
+ def major_version(self) -> typing.Optional[int]:
+ return self._major_version
+
+ @property
+ def minor_version(self) -> typing.Optional[int]:
+ return self._minor_version
+
+ def __repr__(self) -> str:
+ return (
+ f"<MSCertificateTemplate(template_id={self.template_id}, "
+ f"major_version={self.major_version}, "
+ f"minor_version={self.minor_version})>"
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, MSCertificateTemplate):
+ return NotImplemented
+
+ return (
+ self.template_id == other.template_id
+ and self.major_version == other.major_version
+ and self.minor_version == other.minor_version
+ )
+
+ def __hash__(self) -> int:
+ return hash((self.template_id, self.major_version, self.minor_version))
+
+ def public_bytes(self) -> bytes:
+ return rust_x509.encode_extension_value(self)
+
+
+class UnrecognizedExtension(ExtensionType):
+ def __init__(self, oid: ObjectIdentifier, value: bytes) -> None:
+ if not isinstance(oid, ObjectIdentifier):
+ raise TypeError("oid must be an ObjectIdentifier")
+ self._oid = oid
+ self._value = value
+
+ @property
+ def oid(self) -> ObjectIdentifier: # type: ignore[override]
+ return self._oid
+
+ @property
+ def value(self) -> bytes:
+ return self._value
+
+ def __repr__(self) -> str:
+ return (
+ "<UnrecognizedExtension(oid={0.oid}, "
+ "value={0.value!r})>".format(self)
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, UnrecognizedExtension):
+ return NotImplemented
+
+ return self.oid == other.oid and self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash((self.oid, self.value))
+
+ def public_bytes(self) -> bytes:
+ return self.value
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/general_name.py b/contrib/python/cryptography/next/py3/cryptography/x509/general_name.py
new file mode 100644
index 0000000000..79271afbf9
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/general_name.py
@@ -0,0 +1,283 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import ipaddress
+import typing
+from email.utils import parseaddr
+
+from cryptography.x509.name import Name
+from cryptography.x509.oid import ObjectIdentifier
+
+_IPAddressTypes = typing.Union[
+ ipaddress.IPv4Address,
+ ipaddress.IPv6Address,
+ ipaddress.IPv4Network,
+ ipaddress.IPv6Network,
+]
+
+
+class UnsupportedGeneralNameType(Exception):
+ pass
+
+
+class GeneralName(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def value(self) -> typing.Any:
+ """
+ Return the value of the object
+ """
+
+
+class RFC822Name(GeneralName):
+ def __init__(self, value: str) -> None:
+ if isinstance(value, str):
+ try:
+ value.encode("ascii")
+ except UnicodeEncodeError:
+ raise ValueError(
+ "RFC822Name values should be passed as an A-label string. "
+ "This means unicode characters should be encoded via "
+ "a library like idna."
+ )
+ else:
+ raise TypeError("value must be string")
+
+ name, address = parseaddr(value)
+ if name or not address:
+ # parseaddr has found a name (e.g. Name <email>) or the entire
+ # value is an empty string.
+ raise ValueError("Invalid rfc822name value")
+
+ self._value = value
+
+ @property
+ def value(self) -> str:
+ return self._value
+
+ @classmethod
+ def _init_without_validation(cls, value: str) -> RFC822Name:
+ instance = cls.__new__(cls)
+ instance._value = value
+ return instance
+
+ def __repr__(self) -> str:
+ return f"<RFC822Name(value={self.value!r})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, RFC822Name):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class DNSName(GeneralName):
+ def __init__(self, value: str) -> None:
+ if isinstance(value, str):
+ try:
+ value.encode("ascii")
+ except UnicodeEncodeError:
+ raise ValueError(
+ "DNSName values should be passed as an A-label string. "
+ "This means unicode characters should be encoded via "
+ "a library like idna."
+ )
+ else:
+ raise TypeError("value must be string")
+
+ self._value = value
+
+ @property
+ def value(self) -> str:
+ return self._value
+
+ @classmethod
+ def _init_without_validation(cls, value: str) -> DNSName:
+ instance = cls.__new__(cls)
+ instance._value = value
+ return instance
+
+ def __repr__(self) -> str:
+ return f"<DNSName(value={self.value!r})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DNSName):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class UniformResourceIdentifier(GeneralName):
+ def __init__(self, value: str) -> None:
+ if isinstance(value, str):
+ try:
+ value.encode("ascii")
+ except UnicodeEncodeError:
+ raise ValueError(
+ "URI values should be passed as an A-label string. "
+ "This means unicode characters should be encoded via "
+ "a library like idna."
+ )
+ else:
+ raise TypeError("value must be string")
+
+ self._value = value
+
+ @property
+ def value(self) -> str:
+ return self._value
+
+ @classmethod
+ def _init_without_validation(cls, value: str) -> UniformResourceIdentifier:
+ instance = cls.__new__(cls)
+ instance._value = value
+ return instance
+
+ def __repr__(self) -> str:
+ return f"<UniformResourceIdentifier(value={self.value!r})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, UniformResourceIdentifier):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class DirectoryName(GeneralName):
+ def __init__(self, value: Name) -> None:
+ if not isinstance(value, Name):
+ raise TypeError("value must be a Name")
+
+ self._value = value
+
+ @property
+ def value(self) -> Name:
+ return self._value
+
+ def __repr__(self) -> str:
+ return f"<DirectoryName(value={self.value})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, DirectoryName):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class RegisteredID(GeneralName):
+ def __init__(self, value: ObjectIdentifier) -> None:
+ if not isinstance(value, ObjectIdentifier):
+ raise TypeError("value must be an ObjectIdentifier")
+
+ self._value = value
+
+ @property
+ def value(self) -> ObjectIdentifier:
+ return self._value
+
+ def __repr__(self) -> str:
+ return f"<RegisteredID(value={self.value})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, RegisteredID):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class IPAddress(GeneralName):
+ def __init__(self, value: _IPAddressTypes) -> None:
+ if not isinstance(
+ value,
+ (
+ ipaddress.IPv4Address,
+ ipaddress.IPv6Address,
+ ipaddress.IPv4Network,
+ ipaddress.IPv6Network,
+ ),
+ ):
+ raise TypeError(
+ "value must be an instance of ipaddress.IPv4Address, "
+ "ipaddress.IPv6Address, ipaddress.IPv4Network, or "
+ "ipaddress.IPv6Network"
+ )
+
+ self._value = value
+
+ @property
+ def value(self) -> _IPAddressTypes:
+ return self._value
+
+ def _packed(self) -> bytes:
+ if isinstance(
+ self.value, (ipaddress.IPv4Address, ipaddress.IPv6Address)
+ ):
+ return self.value.packed
+ else:
+ return (
+ self.value.network_address.packed + self.value.netmask.packed
+ )
+
+ def __repr__(self) -> str:
+ return f"<IPAddress(value={self.value})>"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, IPAddress):
+ return NotImplemented
+
+ return self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash(self.value)
+
+
+class OtherName(GeneralName):
+ def __init__(self, type_id: ObjectIdentifier, value: bytes) -> None:
+ if not isinstance(type_id, ObjectIdentifier):
+ raise TypeError("type_id must be an ObjectIdentifier")
+ if not isinstance(value, bytes):
+ raise TypeError("value must be a binary string")
+
+ self._type_id = type_id
+ self._value = value
+
+ @property
+ def type_id(self) -> ObjectIdentifier:
+ return self._type_id
+
+ @property
+ def value(self) -> bytes:
+ return self._value
+
+ def __repr__(self) -> str:
+ return "<OtherName(type_id={}, value={!r})>".format(
+ self.type_id, self.value
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, OtherName):
+ return NotImplemented
+
+ return self.type_id == other.type_id and self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash((self.type_id, self.value))
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/name.py b/contrib/python/cryptography/next/py3/cryptography/x509/name.py
new file mode 100644
index 0000000000..ff98e8724a
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/name.py
@@ -0,0 +1,462 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import binascii
+import re
+import sys
+import typing
+import warnings
+
+from cryptography import utils
+from cryptography.hazmat.bindings._rust import x509 as rust_x509
+from cryptography.x509.oid import NameOID, ObjectIdentifier
+
+
+class _ASN1Type(utils.Enum):
+ BitString = 3
+ OctetString = 4
+ UTF8String = 12
+ NumericString = 18
+ PrintableString = 19
+ T61String = 20
+ IA5String = 22
+ UTCTime = 23
+ GeneralizedTime = 24
+ VisibleString = 26
+ UniversalString = 28
+ BMPString = 30
+
+
+_ASN1_TYPE_TO_ENUM = {i.value: i for i in _ASN1Type}
+_NAMEOID_DEFAULT_TYPE: typing.Dict[ObjectIdentifier, _ASN1Type] = {
+ NameOID.COUNTRY_NAME: _ASN1Type.PrintableString,
+ NameOID.JURISDICTION_COUNTRY_NAME: _ASN1Type.PrintableString,
+ NameOID.SERIAL_NUMBER: _ASN1Type.PrintableString,
+ NameOID.DN_QUALIFIER: _ASN1Type.PrintableString,
+ NameOID.EMAIL_ADDRESS: _ASN1Type.IA5String,
+ NameOID.DOMAIN_COMPONENT: _ASN1Type.IA5String,
+}
+
+# Type alias
+_OidNameMap = typing.Mapping[ObjectIdentifier, str]
+_NameOidMap = typing.Mapping[str, ObjectIdentifier]
+
+#: Short attribute names from RFC 4514:
+#: https://tools.ietf.org/html/rfc4514#page-7
+_NAMEOID_TO_NAME: _OidNameMap = {
+ NameOID.COMMON_NAME: "CN",
+ NameOID.LOCALITY_NAME: "L",
+ NameOID.STATE_OR_PROVINCE_NAME: "ST",
+ NameOID.ORGANIZATION_NAME: "O",
+ NameOID.ORGANIZATIONAL_UNIT_NAME: "OU",
+ NameOID.COUNTRY_NAME: "C",
+ NameOID.STREET_ADDRESS: "STREET",
+ NameOID.DOMAIN_COMPONENT: "DC",
+ NameOID.USER_ID: "UID",
+}
+_NAME_TO_NAMEOID = {v: k for k, v in _NAMEOID_TO_NAME.items()}
+
+
+def _escape_dn_value(val: typing.Union[str, bytes]) -> str:
+ """Escape special characters in RFC4514 Distinguished Name value."""
+
+ if not val:
+ return ""
+
+ # RFC 4514 Section 2.4 defines the value as being the # (U+0023) character
+ # followed by the hexadecimal encoding of the octets.
+ if isinstance(val, bytes):
+ return "#" + binascii.hexlify(val).decode("utf8")
+
+ # See https://tools.ietf.org/html/rfc4514#section-2.4
+ val = val.replace("\\", "\\\\")
+ val = val.replace('"', '\\"')
+ val = val.replace("+", "\\+")
+ val = val.replace(",", "\\,")
+ val = val.replace(";", "\\;")
+ val = val.replace("<", "\\<")
+ val = val.replace(">", "\\>")
+ val = val.replace("\0", "\\00")
+
+ if val[0] in ("#", " "):
+ val = "\\" + val
+ if val[-1] == " ":
+ val = val[:-1] + "\\ "
+
+ return val
+
+
+def _unescape_dn_value(val: str) -> str:
+ if not val:
+ return ""
+
+ # See https://tools.ietf.org/html/rfc4514#section-3
+
+ # special = escaped / SPACE / SHARP / EQUALS
+ # escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
+ def sub(m):
+ val = m.group(1)
+ # Regular escape
+ if len(val) == 1:
+ return val
+ # Hex-value scape
+ return chr(int(val, 16))
+
+ return _RFC4514NameParser._PAIR_RE.sub(sub, val)
+
+
+class NameAttribute:
+ def __init__(
+ self,
+ oid: ObjectIdentifier,
+ value: typing.Union[str, bytes],
+ _type: typing.Optional[_ASN1Type] = None,
+ *,
+ _validate: bool = True,
+ ) -> None:
+ if not isinstance(oid, ObjectIdentifier):
+ raise TypeError(
+ "oid argument must be an ObjectIdentifier instance."
+ )
+ if _type == _ASN1Type.BitString:
+ if oid != NameOID.X500_UNIQUE_IDENTIFIER:
+ raise TypeError(
+ "oid must be X500_UNIQUE_IDENTIFIER for BitString type."
+ )
+ if not isinstance(value, bytes):
+ raise TypeError("value must be bytes for BitString")
+ else:
+ if not isinstance(value, str):
+ raise TypeError("value argument must be a str")
+
+ if (
+ oid == NameOID.COUNTRY_NAME
+ or oid == NameOID.JURISDICTION_COUNTRY_NAME
+ ):
+ assert isinstance(value, str)
+ c_len = len(value.encode("utf8"))
+ if c_len != 2 and _validate is True:
+ raise ValueError(
+ "Country name must be a 2 character country code"
+ )
+ elif c_len != 2:
+ warnings.warn(
+ "Country names should be two characters, but the "
+ "attribute is {} characters in length.".format(c_len),
+ stacklevel=2,
+ )
+
+ # The appropriate ASN1 string type varies by OID and is defined across
+ # multiple RFCs including 2459, 3280, and 5280. In general UTF8String
+ # is preferred (2459), but 3280 and 5280 specify several OIDs with
+ # alternate types. This means when we see the sentinel value we need
+ # to look up whether the OID has a non-UTF8 type. If it does, set it
+ # to that. Otherwise, UTF8!
+ if _type is None:
+ _type = _NAMEOID_DEFAULT_TYPE.get(oid, _ASN1Type.UTF8String)
+
+ if not isinstance(_type, _ASN1Type):
+ raise TypeError("_type must be from the _ASN1Type enum")
+
+ self._oid = oid
+ self._value = value
+ self._type = _type
+
+ @property
+ def oid(self) -> ObjectIdentifier:
+ return self._oid
+
+ @property
+ def value(self) -> typing.Union[str, bytes]:
+ return self._value
+
+ @property
+ def rfc4514_attribute_name(self) -> str:
+ """
+ The short attribute name (for example "CN") if available,
+ otherwise the OID dotted string.
+ """
+ return _NAMEOID_TO_NAME.get(self.oid, self.oid.dotted_string)
+
+ def rfc4514_string(
+ self, attr_name_overrides: typing.Optional[_OidNameMap] = None
+ ) -> str:
+ """
+ Format as RFC4514 Distinguished Name string.
+
+ Use short attribute name if available, otherwise fall back to OID
+ dotted string.
+ """
+ attr_name = (
+ attr_name_overrides.get(self.oid) if attr_name_overrides else None
+ )
+ if attr_name is None:
+ attr_name = self.rfc4514_attribute_name
+
+ return f"{attr_name}={_escape_dn_value(self.value)}"
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, NameAttribute):
+ return NotImplemented
+
+ return self.oid == other.oid and self.value == other.value
+
+ def __hash__(self) -> int:
+ return hash((self.oid, self.value))
+
+ def __repr__(self) -> str:
+ return "<NameAttribute(oid={0.oid}, value={0.value!r})>".format(self)
+
+
+class RelativeDistinguishedName:
+ def __init__(self, attributes: typing.Iterable[NameAttribute]):
+ attributes = list(attributes)
+ if not attributes:
+ raise ValueError("a relative distinguished name cannot be empty")
+ if not all(isinstance(x, NameAttribute) for x in attributes):
+ raise TypeError("attributes must be an iterable of NameAttribute")
+
+ # Keep list and frozenset to preserve attribute order where it matters
+ self._attributes = attributes
+ self._attribute_set = frozenset(attributes)
+
+ if len(self._attribute_set) != len(attributes):
+ raise ValueError("duplicate attributes are not allowed")
+
+ def get_attributes_for_oid(
+ self, oid: ObjectIdentifier
+ ) -> typing.List[NameAttribute]:
+ return [i for i in self if i.oid == oid]
+
+ def rfc4514_string(
+ self, attr_name_overrides: typing.Optional[_OidNameMap] = None
+ ) -> str:
+ """
+ Format as RFC4514 Distinguished Name string.
+
+ Within each RDN, attributes are joined by '+', although that is rarely
+ used in certificates.
+ """
+ return "+".join(
+ attr.rfc4514_string(attr_name_overrides)
+ for attr in self._attributes
+ )
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, RelativeDistinguishedName):
+ return NotImplemented
+
+ return self._attribute_set == other._attribute_set
+
+ def __hash__(self) -> int:
+ return hash(self._attribute_set)
+
+ def __iter__(self) -> typing.Iterator[NameAttribute]:
+ return iter(self._attributes)
+
+ def __len__(self) -> int:
+ return len(self._attributes)
+
+ def __repr__(self) -> str:
+ return f"<RelativeDistinguishedName({self.rfc4514_string()})>"
+
+
+class Name:
+ @typing.overload
+ def __init__(self, attributes: typing.Iterable[NameAttribute]) -> None:
+ ...
+
+ @typing.overload
+ def __init__(
+ self, attributes: typing.Iterable[RelativeDistinguishedName]
+ ) -> None:
+ ...
+
+ def __init__(
+ self,
+ attributes: typing.Iterable[
+ typing.Union[NameAttribute, RelativeDistinguishedName]
+ ],
+ ) -> None:
+ attributes = list(attributes)
+ if all(isinstance(x, NameAttribute) for x in attributes):
+ self._attributes = [
+ RelativeDistinguishedName([typing.cast(NameAttribute, x)])
+ for x in attributes
+ ]
+ elif all(isinstance(x, RelativeDistinguishedName) for x in attributes):
+ self._attributes = typing.cast(
+ typing.List[RelativeDistinguishedName], attributes
+ )
+ else:
+ raise TypeError(
+ "attributes must be a list of NameAttribute"
+ " or a list RelativeDistinguishedName"
+ )
+
+ @classmethod
+ def from_rfc4514_string(
+ cls,
+ data: str,
+ attr_name_overrides: typing.Optional[_NameOidMap] = None,
+ ) -> Name:
+ return _RFC4514NameParser(data, attr_name_overrides or {}).parse()
+
+ def rfc4514_string(
+ self, attr_name_overrides: typing.Optional[_OidNameMap] = None
+ ) -> str:
+ """
+ Format as RFC4514 Distinguished Name string.
+ For example 'CN=foobar.com,O=Foo Corp,C=US'
+
+ An X.509 name is a two-level structure: a list of sets of attributes.
+ Each list element is separated by ',' and within each list element, set
+ elements are separated by '+'. The latter is almost never used in
+ real world certificates. According to RFC4514 section 2.1 the
+ RDNSequence must be reversed when converting to string representation.
+ """
+ return ",".join(
+ attr.rfc4514_string(attr_name_overrides)
+ for attr in reversed(self._attributes)
+ )
+
+ def get_attributes_for_oid(
+ self, oid: ObjectIdentifier
+ ) -> typing.List[NameAttribute]:
+ return [i for i in self if i.oid == oid]
+
+ @property
+ def rdns(self) -> typing.List[RelativeDistinguishedName]:
+ return self._attributes
+
+ def public_bytes(self, backend: typing.Any = None) -> bytes:
+ return rust_x509.encode_name_bytes(self)
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, Name):
+ return NotImplemented
+
+ return self._attributes == other._attributes
+
+ def __hash__(self) -> int:
+ # TODO: this is relatively expensive, if this looks like a bottleneck
+ # for you, consider optimizing!
+ return hash(tuple(self._attributes))
+
+ def __iter__(self) -> typing.Iterator[NameAttribute]:
+ for rdn in self._attributes:
+ for ava in rdn:
+ yield ava
+
+ def __len__(self) -> int:
+ return sum(len(rdn) for rdn in self._attributes)
+
+ def __repr__(self) -> str:
+ rdns = ",".join(attr.rfc4514_string() for attr in self._attributes)
+ return f"<Name({rdns})>"
+
+
+class _RFC4514NameParser:
+ _OID_RE = re.compile(r"(0|([1-9]\d*))(\.(0|([1-9]\d*)))+")
+ _DESCR_RE = re.compile(r"[a-zA-Z][a-zA-Z\d-]*")
+
+ _PAIR = r"\\([\\ #=\"\+,;<>]|[\da-zA-Z]{2})"
+ _PAIR_RE = re.compile(_PAIR)
+ _LUTF1 = r"[\x01-\x1f\x21\x24-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
+ _SUTF1 = r"[\x01-\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
+ _TUTF1 = r"[\x01-\x1F\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
+ _UTFMB = rf"[\x80-{chr(sys.maxunicode)}]"
+ _LEADCHAR = rf"{_LUTF1}|{_UTFMB}"
+ _STRINGCHAR = rf"{_SUTF1}|{_UTFMB}"
+ _TRAILCHAR = rf"{_TUTF1}|{_UTFMB}"
+ _STRING_RE = re.compile(
+ rf"""
+ (
+ ({_LEADCHAR}|{_PAIR})
+ (
+ ({_STRINGCHAR}|{_PAIR})*
+ ({_TRAILCHAR}|{_PAIR})
+ )?
+ )?
+ """,
+ re.VERBOSE,
+ )
+ _HEXSTRING_RE = re.compile(r"#([\da-zA-Z]{2})+")
+
+ def __init__(self, data: str, attr_name_overrides: _NameOidMap) -> None:
+ self._data = data
+ self._idx = 0
+
+ self._attr_name_overrides = attr_name_overrides
+
+ def _has_data(self) -> bool:
+ return self._idx < len(self._data)
+
+ def _peek(self) -> typing.Optional[str]:
+ if self._has_data():
+ return self._data[self._idx]
+ return None
+
+ def _read_char(self, ch: str) -> None:
+ if self._peek() != ch:
+ raise ValueError
+ self._idx += 1
+
+ def _read_re(self, pat) -> str:
+ match = pat.match(self._data, pos=self._idx)
+ if match is None:
+ raise ValueError
+ val = match.group()
+ self._idx += len(val)
+ return val
+
+ def parse(self) -> Name:
+ """
+ Parses the `data` string and converts it to a Name.
+
+ According to RFC4514 section 2.1 the RDNSequence must be
+ reversed when converting to string representation. So, when
+ we parse it, we need to reverse again to get the RDNs on the
+ correct order.
+ """
+ rdns = [self._parse_rdn()]
+
+ while self._has_data():
+ self._read_char(",")
+ rdns.append(self._parse_rdn())
+
+ return Name(reversed(rdns))
+
+ def _parse_rdn(self) -> RelativeDistinguishedName:
+ nas = [self._parse_na()]
+ while self._peek() == "+":
+ self._read_char("+")
+ nas.append(self._parse_na())
+
+ return RelativeDistinguishedName(nas)
+
+ def _parse_na(self) -> NameAttribute:
+ try:
+ oid_value = self._read_re(self._OID_RE)
+ except ValueError:
+ name = self._read_re(self._DESCR_RE)
+ oid = self._attr_name_overrides.get(
+ name, _NAME_TO_NAMEOID.get(name)
+ )
+ if oid is None:
+ raise ValueError
+ else:
+ oid = ObjectIdentifier(oid_value)
+
+ self._read_char("=")
+ if self._peek() == "#":
+ value = self._read_re(self._HEXSTRING_RE)
+ value = binascii.unhexlify(value[1:]).decode()
+ else:
+ raw_value = self._read_re(self._STRING_RE)
+ value = _unescape_dn_value(raw_value)
+
+ return NameAttribute(oid, value)
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/ocsp.py b/contrib/python/cryptography/next/py3/cryptography/x509/ocsp.py
new file mode 100644
index 0000000000..7054795fcd
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/ocsp.py
@@ -0,0 +1,622 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+import abc
+import datetime
+import typing
+
+from cryptography import utils, x509
+from cryptography.hazmat.bindings._rust import ocsp
+from cryptography.hazmat.primitives import hashes, serialization
+from cryptography.hazmat.primitives.asymmetric.types import (
+ CertificateIssuerPrivateKeyTypes,
+)
+from cryptography.x509.base import (
+ _EARLIEST_UTC_TIME,
+ _convert_to_naive_utc_time,
+ _reject_duplicate_extension,
+)
+
+
+class OCSPResponderEncoding(utils.Enum):
+ HASH = "By Hash"
+ NAME = "By Name"
+
+
+class OCSPResponseStatus(utils.Enum):
+ SUCCESSFUL = 0
+ MALFORMED_REQUEST = 1
+ INTERNAL_ERROR = 2
+ TRY_LATER = 3
+ SIG_REQUIRED = 5
+ UNAUTHORIZED = 6
+
+
+_ALLOWED_HASHES = (
+ hashes.SHA1,
+ hashes.SHA224,
+ hashes.SHA256,
+ hashes.SHA384,
+ hashes.SHA512,
+)
+
+
+def _verify_algorithm(algorithm: hashes.HashAlgorithm) -> None:
+ if not isinstance(algorithm, _ALLOWED_HASHES):
+ raise ValueError(
+ "Algorithm must be SHA1, SHA224, SHA256, SHA384, or SHA512"
+ )
+
+
+class OCSPCertStatus(utils.Enum):
+ GOOD = 0
+ REVOKED = 1
+ UNKNOWN = 2
+
+
+class _SingleResponse:
+ def __init__(
+ self,
+ cert: x509.Certificate,
+ issuer: x509.Certificate,
+ algorithm: hashes.HashAlgorithm,
+ cert_status: OCSPCertStatus,
+ this_update: datetime.datetime,
+ next_update: typing.Optional[datetime.datetime],
+ revocation_time: typing.Optional[datetime.datetime],
+ revocation_reason: typing.Optional[x509.ReasonFlags],
+ ):
+ if not isinstance(cert, x509.Certificate) or not isinstance(
+ issuer, x509.Certificate
+ ):
+ raise TypeError("cert and issuer must be a Certificate")
+
+ _verify_algorithm(algorithm)
+ if not isinstance(this_update, datetime.datetime):
+ raise TypeError("this_update must be a datetime object")
+ if next_update is not None and not isinstance(
+ next_update, datetime.datetime
+ ):
+ raise TypeError("next_update must be a datetime object or None")
+
+ self._cert = cert
+ self._issuer = issuer
+ self._algorithm = algorithm
+ self._this_update = this_update
+ self._next_update = next_update
+
+ if not isinstance(cert_status, OCSPCertStatus):
+ raise TypeError(
+ "cert_status must be an item from the OCSPCertStatus enum"
+ )
+ if cert_status is not OCSPCertStatus.REVOKED:
+ if revocation_time is not None:
+ raise ValueError(
+ "revocation_time can only be provided if the certificate "
+ "is revoked"
+ )
+ if revocation_reason is not None:
+ raise ValueError(
+ "revocation_reason can only be provided if the certificate"
+ " is revoked"
+ )
+ else:
+ if not isinstance(revocation_time, datetime.datetime):
+ raise TypeError("revocation_time must be a datetime object")
+
+ revocation_time = _convert_to_naive_utc_time(revocation_time)
+ if revocation_time < _EARLIEST_UTC_TIME:
+ raise ValueError(
+ "The revocation_time must be on or after"
+ " 1950 January 1."
+ )
+
+ if revocation_reason is not None and not isinstance(
+ revocation_reason, x509.ReasonFlags
+ ):
+ raise TypeError(
+ "revocation_reason must be an item from the ReasonFlags "
+ "enum or None"
+ )
+
+ self._cert_status = cert_status
+ self._revocation_time = revocation_time
+ self._revocation_reason = revocation_reason
+
+
+class OCSPRequest(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def issuer_key_hash(self) -> bytes:
+ """
+ The hash of the issuer public key
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer_name_hash(self) -> bytes:
+ """
+ The hash of the issuer name
+ """
+
+ @property
+ @abc.abstractmethod
+ def hash_algorithm(self) -> hashes.HashAlgorithm:
+ """
+ The hash algorithm used in the issuer name and key hashes
+ """
+
+ @property
+ @abc.abstractmethod
+ def serial_number(self) -> int:
+ """
+ The serial number of the cert whose status is being checked
+ """
+
+ @abc.abstractmethod
+ def public_bytes(self, encoding: serialization.Encoding) -> bytes:
+ """
+ Serializes the request to DER
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> x509.Extensions:
+ """
+ The list of request extensions. Not single request extensions.
+ """
+
+
+class OCSPSingleResponse(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def certificate_status(self) -> OCSPCertStatus:
+ """
+ The status of the certificate (an element from the OCSPCertStatus enum)
+ """
+
+ @property
+ @abc.abstractmethod
+ def revocation_time(self) -> typing.Optional[datetime.datetime]:
+ """
+ The date of when the certificate was revoked or None if not
+ revoked.
+ """
+
+ @property
+ @abc.abstractmethod
+ def revocation_reason(self) -> typing.Optional[x509.ReasonFlags]:
+ """
+ The reason the certificate was revoked or None if not specified or
+ not revoked.
+ """
+
+ @property
+ @abc.abstractmethod
+ def this_update(self) -> datetime.datetime:
+ """
+ The most recent time at which the status being indicated is known by
+ the responder to have been correct
+ """
+
+ @property
+ @abc.abstractmethod
+ def next_update(self) -> typing.Optional[datetime.datetime]:
+ """
+ The time when newer information will be available
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer_key_hash(self) -> bytes:
+ """
+ The hash of the issuer public key
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer_name_hash(self) -> bytes:
+ """
+ The hash of the issuer name
+ """
+
+ @property
+ @abc.abstractmethod
+ def hash_algorithm(self) -> hashes.HashAlgorithm:
+ """
+ The hash algorithm used in the issuer name and key hashes
+ """
+
+ @property
+ @abc.abstractmethod
+ def serial_number(self) -> int:
+ """
+ The serial number of the cert whose status is being checked
+ """
+
+
+class OCSPResponse(metaclass=abc.ABCMeta):
+ @property
+ @abc.abstractmethod
+ def responses(self) -> typing.Iterator[OCSPSingleResponse]:
+ """
+ An iterator over the individual SINGLERESP structures in the
+ response
+ """
+
+ @property
+ @abc.abstractmethod
+ def response_status(self) -> OCSPResponseStatus:
+ """
+ The status of the response. This is a value from the OCSPResponseStatus
+ enumeration
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_algorithm_oid(self) -> x509.ObjectIdentifier:
+ """
+ The ObjectIdentifier of the signature algorithm
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature_hash_algorithm(
+ self,
+ ) -> typing.Optional[hashes.HashAlgorithm]:
+ """
+ Returns a HashAlgorithm corresponding to the type of the digest signed
+ """
+
+ @property
+ @abc.abstractmethod
+ def signature(self) -> bytes:
+ """
+ The signature bytes
+ """
+
+ @property
+ @abc.abstractmethod
+ def tbs_response_bytes(self) -> bytes:
+ """
+ The tbsResponseData bytes
+ """
+
+ @property
+ @abc.abstractmethod
+ def certificates(self) -> typing.List[x509.Certificate]:
+ """
+ A list of certificates used to help build a chain to verify the OCSP
+ response. This situation occurs when the OCSP responder uses a delegate
+ certificate.
+ """
+
+ @property
+ @abc.abstractmethod
+ def responder_key_hash(self) -> typing.Optional[bytes]:
+ """
+ The responder's key hash or None
+ """
+
+ @property
+ @abc.abstractmethod
+ def responder_name(self) -> typing.Optional[x509.Name]:
+ """
+ The responder's Name or None
+ """
+
+ @property
+ @abc.abstractmethod
+ def produced_at(self) -> datetime.datetime:
+ """
+ The time the response was produced
+ """
+
+ @property
+ @abc.abstractmethod
+ def certificate_status(self) -> OCSPCertStatus:
+ """
+ The status of the certificate (an element from the OCSPCertStatus enum)
+ """
+
+ @property
+ @abc.abstractmethod
+ def revocation_time(self) -> typing.Optional[datetime.datetime]:
+ """
+ The date of when the certificate was revoked or None if not
+ revoked.
+ """
+
+ @property
+ @abc.abstractmethod
+ def revocation_reason(self) -> typing.Optional[x509.ReasonFlags]:
+ """
+ The reason the certificate was revoked or None if not specified or
+ not revoked.
+ """
+
+ @property
+ @abc.abstractmethod
+ def this_update(self) -> datetime.datetime:
+ """
+ The most recent time at which the status being indicated is known by
+ the responder to have been correct
+ """
+
+ @property
+ @abc.abstractmethod
+ def next_update(self) -> typing.Optional[datetime.datetime]:
+ """
+ The time when newer information will be available
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer_key_hash(self) -> bytes:
+ """
+ The hash of the issuer public key
+ """
+
+ @property
+ @abc.abstractmethod
+ def issuer_name_hash(self) -> bytes:
+ """
+ The hash of the issuer name
+ """
+
+ @property
+ @abc.abstractmethod
+ def hash_algorithm(self) -> hashes.HashAlgorithm:
+ """
+ The hash algorithm used in the issuer name and key hashes
+ """
+
+ @property
+ @abc.abstractmethod
+ def serial_number(self) -> int:
+ """
+ The serial number of the cert whose status is being checked
+ """
+
+ @property
+ @abc.abstractmethod
+ def extensions(self) -> x509.Extensions:
+ """
+ The list of response extensions. Not single response extensions.
+ """
+
+ @property
+ @abc.abstractmethod
+ def single_extensions(self) -> x509.Extensions:
+ """
+ The list of single response extensions. Not response extensions.
+ """
+
+ @abc.abstractmethod
+ def public_bytes(self, encoding: serialization.Encoding) -> bytes:
+ """
+ Serializes the response to DER
+ """
+
+
+class OCSPRequestBuilder:
+ def __init__(
+ self,
+ request: typing.Optional[
+ typing.Tuple[
+ x509.Certificate, x509.Certificate, hashes.HashAlgorithm
+ ]
+ ] = None,
+ request_hash: typing.Optional[
+ typing.Tuple[bytes, bytes, int, hashes.HashAlgorithm]
+ ] = None,
+ extensions: typing.List[x509.Extension[x509.ExtensionType]] = [],
+ ) -> None:
+ self._request = request
+ self._request_hash = request_hash
+ self._extensions = extensions
+
+ def add_certificate(
+ self,
+ cert: x509.Certificate,
+ issuer: x509.Certificate,
+ algorithm: hashes.HashAlgorithm,
+ ) -> OCSPRequestBuilder:
+ if self._request is not None or self._request_hash is not None:
+ raise ValueError("Only one certificate can be added to a request")
+
+ _verify_algorithm(algorithm)
+ if not isinstance(cert, x509.Certificate) or not isinstance(
+ issuer, x509.Certificate
+ ):
+ raise TypeError("cert and issuer must be a Certificate")
+
+ return OCSPRequestBuilder(
+ (cert, issuer, algorithm), self._request_hash, self._extensions
+ )
+
+ def add_certificate_by_hash(
+ self,
+ issuer_name_hash: bytes,
+ issuer_key_hash: bytes,
+ serial_number: int,
+ algorithm: hashes.HashAlgorithm,
+ ) -> OCSPRequestBuilder:
+ if self._request is not None or self._request_hash is not None:
+ raise ValueError("Only one certificate can be added to a request")
+
+ if not isinstance(serial_number, int):
+ raise TypeError("serial_number must be an integer")
+
+ _verify_algorithm(algorithm)
+ utils._check_bytes("issuer_name_hash", issuer_name_hash)
+ utils._check_bytes("issuer_key_hash", issuer_key_hash)
+ if algorithm.digest_size != len(
+ issuer_name_hash
+ ) or algorithm.digest_size != len(issuer_key_hash):
+ raise ValueError(
+ "issuer_name_hash and issuer_key_hash must be the same length "
+ "as the digest size of the algorithm"
+ )
+
+ return OCSPRequestBuilder(
+ self._request,
+ (issuer_name_hash, issuer_key_hash, serial_number, algorithm),
+ self._extensions,
+ )
+
+ def add_extension(
+ self, extval: x509.ExtensionType, critical: bool
+ ) -> OCSPRequestBuilder:
+ if not isinstance(extval, x509.ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = x509.Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+
+ return OCSPRequestBuilder(
+ self._request, self._request_hash, self._extensions + [extension]
+ )
+
+ def build(self) -> OCSPRequest:
+ if self._request is None and self._request_hash is None:
+ raise ValueError("You must add a certificate before building")
+
+ return ocsp.create_ocsp_request(self)
+
+
+class OCSPResponseBuilder:
+ def __init__(
+ self,
+ response: typing.Optional[_SingleResponse] = None,
+ responder_id: typing.Optional[
+ typing.Tuple[x509.Certificate, OCSPResponderEncoding]
+ ] = None,
+ certs: typing.Optional[typing.List[x509.Certificate]] = None,
+ extensions: typing.List[x509.Extension[x509.ExtensionType]] = [],
+ ):
+ self._response = response
+ self._responder_id = responder_id
+ self._certs = certs
+ self._extensions = extensions
+
+ def add_response(
+ self,
+ cert: x509.Certificate,
+ issuer: x509.Certificate,
+ algorithm: hashes.HashAlgorithm,
+ cert_status: OCSPCertStatus,
+ this_update: datetime.datetime,
+ next_update: typing.Optional[datetime.datetime],
+ revocation_time: typing.Optional[datetime.datetime],
+ revocation_reason: typing.Optional[x509.ReasonFlags],
+ ) -> OCSPResponseBuilder:
+ if self._response is not None:
+ raise ValueError("Only one response per OCSPResponse.")
+
+ singleresp = _SingleResponse(
+ cert,
+ issuer,
+ algorithm,
+ cert_status,
+ this_update,
+ next_update,
+ revocation_time,
+ revocation_reason,
+ )
+ return OCSPResponseBuilder(
+ singleresp,
+ self._responder_id,
+ self._certs,
+ self._extensions,
+ )
+
+ def responder_id(
+ self, encoding: OCSPResponderEncoding, responder_cert: x509.Certificate
+ ) -> OCSPResponseBuilder:
+ if self._responder_id is not None:
+ raise ValueError("responder_id can only be set once")
+ if not isinstance(responder_cert, x509.Certificate):
+ raise TypeError("responder_cert must be a Certificate")
+ if not isinstance(encoding, OCSPResponderEncoding):
+ raise TypeError(
+ "encoding must be an element from OCSPResponderEncoding"
+ )
+
+ return OCSPResponseBuilder(
+ self._response,
+ (responder_cert, encoding),
+ self._certs,
+ self._extensions,
+ )
+
+ def certificates(
+ self, certs: typing.Iterable[x509.Certificate]
+ ) -> OCSPResponseBuilder:
+ if self._certs is not None:
+ raise ValueError("certificates may only be set once")
+ certs = list(certs)
+ if len(certs) == 0:
+ raise ValueError("certs must not be an empty list")
+ if not all(isinstance(x, x509.Certificate) for x in certs):
+ raise TypeError("certs must be a list of Certificates")
+ return OCSPResponseBuilder(
+ self._response,
+ self._responder_id,
+ certs,
+ self._extensions,
+ )
+
+ def add_extension(
+ self, extval: x509.ExtensionType, critical: bool
+ ) -> OCSPResponseBuilder:
+ if not isinstance(extval, x509.ExtensionType):
+ raise TypeError("extension must be an ExtensionType")
+
+ extension = x509.Extension(extval.oid, critical, extval)
+ _reject_duplicate_extension(extension, self._extensions)
+
+ return OCSPResponseBuilder(
+ self._response,
+ self._responder_id,
+ self._certs,
+ self._extensions + [extension],
+ )
+
+ def sign(
+ self,
+ private_key: CertificateIssuerPrivateKeyTypes,
+ algorithm: typing.Optional[hashes.HashAlgorithm],
+ ) -> OCSPResponse:
+ if self._response is None:
+ raise ValueError("You must add a response before signing")
+ if self._responder_id is None:
+ raise ValueError("You must add a responder_id before signing")
+
+ return ocsp.create_ocsp_response(
+ OCSPResponseStatus.SUCCESSFUL, self, private_key, algorithm
+ )
+
+ @classmethod
+ def build_unsuccessful(
+ cls, response_status: OCSPResponseStatus
+ ) -> OCSPResponse:
+ if not isinstance(response_status, OCSPResponseStatus):
+ raise TypeError(
+ "response_status must be an item from OCSPResponseStatus"
+ )
+ if response_status is OCSPResponseStatus.SUCCESSFUL:
+ raise ValueError("response_status cannot be SUCCESSFUL")
+
+ return ocsp.create_ocsp_response(response_status, None, None, None)
+
+
+def load_der_ocsp_request(data: bytes) -> OCSPRequest:
+ return ocsp.load_der_ocsp_request(data)
+
+
+def load_der_ocsp_response(data: bytes) -> OCSPResponse:
+ return ocsp.load_der_ocsp_response(data)
diff --git a/contrib/python/cryptography/next/py3/cryptography/x509/oid.py b/contrib/python/cryptography/next/py3/cryptography/x509/oid.py
new file mode 100644
index 0000000000..cda50cced5
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/cryptography/x509/oid.py
@@ -0,0 +1,33 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import annotations
+
+from cryptography.hazmat._oid import (
+ AttributeOID,
+ AuthorityInformationAccessOID,
+ CertificatePoliciesOID,
+ CRLEntryExtensionOID,
+ ExtendedKeyUsageOID,
+ ExtensionOID,
+ NameOID,
+ ObjectIdentifier,
+ OCSPExtensionOID,
+ SignatureAlgorithmOID,
+ SubjectInformationAccessOID,
+)
+
+__all__ = [
+ "AttributeOID",
+ "AuthorityInformationAccessOID",
+ "CRLEntryExtensionOID",
+ "CertificatePoliciesOID",
+ "ExtendedKeyUsageOID",
+ "ExtensionOID",
+ "NameOID",
+ "OCSPExtensionOID",
+ "ObjectIdentifier",
+ "SignatureAlgorithmOID",
+ "SubjectInformationAccessOID",
+]
diff --git a/contrib/python/cryptography/next/py3/ya.make b/contrib/python/cryptography/next/py3/ya.make
new file mode 100644
index 0000000000..652034cd9b
--- /dev/null
+++ b/contrib/python/cryptography/next/py3/ya.make
@@ -0,0 +1,124 @@
+# Generated by devtools/yamaker/ym2.
+
+PY3_LIBRARY()
+
+SUBSCRIBER(g:python-contrib)
+
+VERSION(41.0.6)
+
+LICENSE(
+ Apache-2.0 AND
+ BSD-3-Clause
+)
+
+LICENSE_TEXTS(.yandex_meta/licenses.list.txt)
+
+PEERDIR(
+ contrib/python/cffi
+)
+
+NO_LINT()
+
+PY_SRCS(
+ TOP_LEVEL
+ cryptography/hazmat/bindings/_rust/__init__.py
+ cryptography/__about__.py
+ cryptography/__init__.py
+ cryptography/exceptions.py
+ cryptography/fernet.py
+ cryptography/hazmat/__init__.py
+ cryptography/hazmat/_oid.py
+ cryptography/hazmat/backends/__init__.py
+ cryptography/hazmat/backends/openssl/__init__.py
+ cryptography/hazmat/backends/openssl/aead.py
+ cryptography/hazmat/backends/openssl/backend.py
+ cryptography/hazmat/backends/openssl/ciphers.py
+ cryptography/hazmat/backends/openssl/cmac.py
+ cryptography/hazmat/backends/openssl/decode_asn1.py
+ cryptography/hazmat/backends/openssl/ec.py
+ cryptography/hazmat/backends/openssl/rsa.py
+ cryptography/hazmat/backends/openssl/utils.py
+ cryptography/hazmat/bindings/__init__.py
+ cryptography/hazmat/bindings/_rust/__init__.pyi
+ cryptography/hazmat/bindings/_rust/_openssl.pyi
+ cryptography/hazmat/bindings/_rust/asn1.pyi
+ cryptography/hazmat/bindings/_rust/exceptions.pyi
+ cryptography/hazmat/bindings/_rust/ocsp.pyi
+ cryptography/hazmat/bindings/_rust/openssl/__init__.pyi
+ cryptography/hazmat/bindings/_rust/openssl/dh.pyi
+ cryptography/hazmat/bindings/_rust/openssl/dsa.pyi
+ cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi
+ cryptography/hazmat/bindings/_rust/openssl/ed448.pyi
+ cryptography/hazmat/bindings/_rust/openssl/hashes.pyi
+ cryptography/hazmat/bindings/_rust/openssl/hmac.pyi
+ cryptography/hazmat/bindings/_rust/openssl/kdf.pyi
+ cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi
+ cryptography/hazmat/bindings/_rust/openssl/x25519.pyi
+ cryptography/hazmat/bindings/_rust/openssl/x448.pyi
+ cryptography/hazmat/bindings/_rust/pkcs7.pyi
+ cryptography/hazmat/bindings/_rust/x509.pyi
+ cryptography/hazmat/bindings/openssl/__init__.py
+ cryptography/hazmat/bindings/openssl/_conditional.py
+ cryptography/hazmat/bindings/openssl/binding.py
+ cryptography/hazmat/primitives/__init__.py
+ cryptography/hazmat/primitives/_asymmetric.py
+ cryptography/hazmat/primitives/_cipheralgorithm.py
+ cryptography/hazmat/primitives/_serialization.py
+ cryptography/hazmat/primitives/asymmetric/__init__.py
+ cryptography/hazmat/primitives/asymmetric/dh.py
+ cryptography/hazmat/primitives/asymmetric/dsa.py
+ cryptography/hazmat/primitives/asymmetric/ec.py
+ cryptography/hazmat/primitives/asymmetric/ed25519.py
+ cryptography/hazmat/primitives/asymmetric/ed448.py
+ cryptography/hazmat/primitives/asymmetric/padding.py
+ cryptography/hazmat/primitives/asymmetric/rsa.py
+ cryptography/hazmat/primitives/asymmetric/types.py
+ cryptography/hazmat/primitives/asymmetric/utils.py
+ cryptography/hazmat/primitives/asymmetric/x25519.py
+ cryptography/hazmat/primitives/asymmetric/x448.py
+ cryptography/hazmat/primitives/ciphers/__init__.py
+ cryptography/hazmat/primitives/ciphers/aead.py
+ cryptography/hazmat/primitives/ciphers/algorithms.py
+ cryptography/hazmat/primitives/ciphers/base.py
+ cryptography/hazmat/primitives/ciphers/modes.py
+ cryptography/hazmat/primitives/cmac.py
+ cryptography/hazmat/primitives/constant_time.py
+ cryptography/hazmat/primitives/hashes.py
+ cryptography/hazmat/primitives/hmac.py
+ cryptography/hazmat/primitives/kdf/__init__.py
+ cryptography/hazmat/primitives/kdf/concatkdf.py
+ cryptography/hazmat/primitives/kdf/hkdf.py
+ cryptography/hazmat/primitives/kdf/kbkdf.py
+ cryptography/hazmat/primitives/kdf/pbkdf2.py
+ cryptography/hazmat/primitives/kdf/scrypt.py
+ cryptography/hazmat/primitives/kdf/x963kdf.py
+ cryptography/hazmat/primitives/keywrap.py
+ cryptography/hazmat/primitives/padding.py
+ cryptography/hazmat/primitives/poly1305.py
+ cryptography/hazmat/primitives/serialization/__init__.py
+ cryptography/hazmat/primitives/serialization/base.py
+ cryptography/hazmat/primitives/serialization/pkcs12.py
+ cryptography/hazmat/primitives/serialization/pkcs7.py
+ cryptography/hazmat/primitives/serialization/ssh.py
+ cryptography/hazmat/primitives/twofactor/__init__.py
+ cryptography/hazmat/primitives/twofactor/hotp.py
+ cryptography/hazmat/primitives/twofactor/totp.py
+ cryptography/utils.py
+ cryptography/x509/__init__.py
+ cryptography/x509/base.py
+ cryptography/x509/certificate_transparency.py
+ cryptography/x509/extensions.py
+ cryptography/x509/general_name.py
+ cryptography/x509/name.py
+ cryptography/x509/ocsp.py
+ cryptography/x509/oid.py
+)
+
+RESOURCE_FILES(
+ PREFIX contrib/python/cryptography/
+ .dist-info/METADATA
+ .dist-info/top_level.txt
+ cryptography/py.typed
+)
+
+END()