Skip to content
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

Reject invalid versions in X509Req.set_version #1208

Merged
merged 2 commits into from
Mar 31, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Deprecations:
Changes:
^^^^^^^^

- Invalid versions are now rejected in ``OpenSSL.crypto.X509Req.set_version``.

23.1.1 (2023-03-28)
-------------------

Expand Down
6 changes: 6 additions & 0 deletions src/OpenSSL/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,12 @@ def set_version(self, version: int) -> None:
:param int version: The version number.
:return: ``None``
"""
if not isinstance(version, int):
raise TypeError("version must be an int")
if version != 0:
raise ValueError(
"Invalid version. The only valid version for X509Req is 0."
)
set_result = _lib.X509_REQ_set_version(self._req, version)
_openssl_assert(set_result == 1)

Expand Down
12 changes: 3 additions & 9 deletions tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,20 +1601,12 @@ def test_version(self):
"""
`X509Req.set_version` sets the X.509 version of the certificate
request. `X509Req.get_version` returns the X.509 version of the
certificate request. The only defined version is 0. Others may or
may not be supported depending on backend.
certificate request. The only defined version is 0.
"""
request = X509Req()
assert request.get_version() == 0
request.set_version(0)
assert request.get_version() == 0
try:
request.set_version(1)
assert request.get_version() == 1
request.set_version(3)
assert request.get_version() == 3
except Error:
pass

def test_version_wrong_args(self):
"""
Expand All @@ -1624,6 +1616,8 @@ def test_version_wrong_args(self):
request = X509Req()
with pytest.raises(TypeError):
request.set_version("foo")
with pytest.raises(ValueError):
request.set_version(2)

def test_get_subject(self):
"""
Expand Down