Skip to content

PoC for using capabilities from Rust #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
matrix:
PYTHON:
# Base builds
- {VERSION: "3.7", TOXENV: "py37"}
- {VERSION: "3.8", TOXENV: "py38"}
- {VERSION: "3.9", TOXENV: "py39"}
- {VERSION: "3.10", TOXENV: "py310"}
- {VERSION: "3.11", TOXENV: "py311"}
- {VERSION: "3.12", TOXENV: "py312"}
- {VERSION: "3.13-dev", TOXENV: "py313"}
- {VERSION: "pypy-3.9", TOXENV: "pypy3"}
- {VERSION: "pypy-3.10", TOXENV: "pypy3"}
- {VERSION: "3.11", TOXENV: "py311-useWheel", OS: "windows-2022" }
# - {VERSION: "3.7", TOXENV: "py37"}
# - {VERSION: "3.8", TOXENV: "py38"}
# - {VERSION: "3.9", TOXENV: "py39"}
# - {VERSION: "3.10", TOXENV: "py310"}
# - {VERSION: "3.11", TOXENV: "py311"}
# - {VERSION: "3.12", TOXENV: "py312"}
# - {VERSION: "3.13-dev", TOXENV: "py313"}
# - {VERSION: "pypy-3.9", TOXENV: "pypy3"}
# - {VERSION: "pypy-3.10", TOXENV: "pypy3"}
# - {VERSION: "3.11", TOXENV: "py311-useWheel", OS: "windows-2022" }
# -cryptographyMain
- {VERSION: "3.7", TOXENV: "py37-cryptographyMain"}
- {VERSION: "3.8", TOXENV: "py38-cryptographyMain"}
Expand All @@ -32,24 +32,24 @@ jobs:
- {VERSION: "pypy-3.9", TOXENV: "pypy3-cryptographyMain"}
- {VERSION: "pypy-3.10", TOXENV: "pypy3-cryptographyMain"}
# -cryptographyMinimum
- {VERSION: "3.7", TOXENV: "py37-cryptographyMinimum"}
- {VERSION: "3.8", TOXENV: "py38-cryptographyMinimum"}
- {VERSION: "3.9", TOXENV: "py39-cryptographyMinimum"}
- {VERSION: "3.10", TOXENV: "py310-cryptographyMinimum"}
- {VERSION: "3.11", TOXENV: "py311-cryptographyMinimum"}
- {VERSION: "3.12", TOXENV: "py312-cryptographyMinimum"}
- {VERSION: "3.13-dev", TOXENV: "py313-cryptographyMinimum"}
- {VERSION: "pypy-3.10", TOXENV: "pypy3-cryptographyMinimum"}
# - {VERSION: "3.7", TOXENV: "py37-cryptographyMinimum"}
# - {VERSION: "3.8", TOXENV: "py38-cryptographyMinimum"}
# - {VERSION: "3.9", TOXENV: "py39-cryptographyMinimum"}
# - {VERSION: "3.10", TOXENV: "py310-cryptographyMinimum"}
# - {VERSION: "3.11", TOXENV: "py311-cryptographyMinimum"}
# - {VERSION: "3.12", TOXENV: "py312-cryptographyMinimum"}
# - {VERSION: "3.13-dev", TOXENV: "py313-cryptographyMinimum"}
# - {VERSION: "pypy-3.10", TOXENV: "pypy3-cryptographyMinimum"}
# Cryptography wheels
- {VERSION: "3.9", TOXENV: "py39-cryptographyMinimum-useWheel"}
- {VERSION: "3.9", TOXENV: "py39-useWheel"}
# - {VERSION: "3.9", TOXENV: "py39-cryptographyMinimum-useWheel"}
# - {VERSION: "3.9", TOXENV: "py39-useWheel"}
# Random order
- {VERSION: "3.9", TOXENV: "py39-randomorder"}
# - {VERSION: "3.9", TOXENV: "py39-randomorder"}
# Meta
- {VERSION: "3.9", TOXENV: "check-manifest"}
- {VERSION: "3.11", TOXENV: "lint"}
- {VERSION: "3.11", TOXENV: "py311-mypy"}
- {VERSION: "3.9", TOXENV: "docs"}
# - {VERSION: "3.11", TOXENV: "py311-mypy"}
# - {VERSION: "3.9", TOXENV: "docs"}
name: "${{ matrix.PYTHON.TOXENV }}${{ matrix.PYTHON.OS && format(' on {0}', matrix.PYTHON.OS) || '' }}"
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -88,8 +88,8 @@ jobs:
fail-fast: false
matrix:
DOWNSTREAM:
- twisted
- certbot
# - twisted
# - certbot
- certbot-josepy
PYTHON:
- 3.12
Expand Down
62 changes: 13 additions & 49 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from weakref import WeakValueDictionary

from cryptography import x509
from cryptography.hazmat.bindings._rust import pyopenssl
from cryptography.hazmat.primitives.asymmetric import ec

from OpenSSL._util import (
Expand Down Expand Up @@ -169,16 +170,16 @@
SENT_SHUTDOWN = _lib.SSL_SENT_SHUTDOWN
RECEIVED_SHUTDOWN = _lib.SSL_RECEIVED_SHUTDOWN

SSLv23_METHOD = 3
TLSv1_METHOD = 4
TLSv1_1_METHOD = 5
TLSv1_2_METHOD = 6
TLS_METHOD = 7
TLS_SERVER_METHOD = 8
TLS_CLIENT_METHOD = 9
DTLS_METHOD = 10
DTLS_SERVER_METHOD = 11
DTLS_CLIENT_METHOD = 12
SSLv23_METHOD = pyopenssl.SSLv23_METHOD
TLSv1_METHOD = pyopenssl.TLSv1_METHOD
TLSv1_1_METHOD = pyopenssl.TLSv1_1_METHOD
TLSv1_2_METHOD = pyopenssl.TLSv1_2_METHOD
TLS_METHOD = pyopenssl.TLS_METHOD
TLS_SERVER_METHOD = pyopenssl.TLS_SERVER_METHOD
TLS_CLIENT_METHOD = pyopenssl.TLS_CLIENT_METHOD
DTLS_METHOD = pyopenssl.DTLS_METHOD
DTLS_SERVER_METHOD = pyopenssl.DTLS_SERVER_METHOD
DTLS_CLIENT_METHOD = pyopenssl.DTLS_CLIENT_METHOD

SSL3_VERSION: int = _lib.SSL3_VERSION
TLS1_VERSION: int = _lib.TLS1_VERSION
Expand Down Expand Up @@ -426,10 +427,7 @@ class X509VerificationCodes:
_CRYPTOGRAPHY_MANYLINUX_CA_FILE = b"/opt/pyca/cryptography/openssl/cert.pem"


class Error(Exception):
"""
An error occurred in an `OpenSSL.SSL` API.
"""
Error = pyopenssl.Error


_raise_current_error = partial(_exception_from_error_queue, Error)
Expand Down Expand Up @@ -827,7 +825,7 @@ class Session:
_session: Any


class Context:
class Context(pyopenssl.Context):
"""
:class:`OpenSSL.SSL.Context` instances define the parameters for setting
up new SSL connections.
Expand All @@ -838,38 +836,7 @@ class Context:
not be used.
"""

_methods: typing.ClassVar[
dict[int, tuple[Callable[[], Any], int | None]]
] = {
SSLv23_METHOD: (_lib.TLS_method, None),
TLSv1_METHOD: (_lib.TLS_method, TLS1_VERSION),
TLSv1_1_METHOD: (_lib.TLS_method, TLS1_1_VERSION),
TLSv1_2_METHOD: (_lib.TLS_method, TLS1_2_VERSION),
TLS_METHOD: (_lib.TLS_method, None),
TLS_SERVER_METHOD: (_lib.TLS_server_method, None),
TLS_CLIENT_METHOD: (_lib.TLS_client_method, None),
DTLS_METHOD: (_lib.DTLS_method, None),
DTLS_SERVER_METHOD: (_lib.DTLS_server_method, None),
DTLS_CLIENT_METHOD: (_lib.DTLS_client_method, None),
}

def __init__(self, method: int) -> None:
if not isinstance(method, int):
raise TypeError("method must be an integer")

try:
method_func, version = self._methods[method]
except KeyError:
raise ValueError("No such protocol")

method_obj = method_func()
_openssl_assert(method_obj != _ffi.NULL)

context = _lib.SSL_CTX_new(method_obj)
_openssl_assert(context != _ffi.NULL)
context = _ffi.gc(context, _lib.SSL_CTX_free)

self._context = context
self._passphrase_helper: _PassphraseHelper | None = None
self._passphrase_callback: _PassphraseCallback[Any] | None = None
self._passphrase_userdata: Any | None = None
Expand All @@ -894,9 +861,6 @@ def __init__(self, method: int) -> None:
self._cookie_verify_helper: _CookieVerifyCallbackHelper | None = None

self.set_mode(_lib.SSL_MODE_ENABLE_PARTIAL_WRITE)
if version is not None:
self.set_min_proto_version(version)
self.set_max_proto_version(version)

def set_min_proto_version(self, version: int) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ setenv =
PIP_NO_BINARY=cryptography
useWheel: PIP_NO_BINARY=
commands =
cryptographyMain: pip install -U git+https://github.com/pyca/cryptography.git
cryptographyMain: pip install -U git+https://github.com/alex/cryptography.git@openssl-horror-show
openssl version
coverage run --parallel -m OpenSSL.debug
coverage run --parallel -m pytest -v {posargs}
Expand Down