Skip to content

Commit 3e4d61a

Browse files
authored
Fix X.509 version handling. (#1123)
Certificate versions go up to v3 (numeric value 2), CRLs go up to v2 (numeric value 1), and CSRs go up to v1 (numeric value 0). This CL fixes the following issues: - Add a missing check to the return value of X509_set_version - Fix crlDataUnsupportedExtension which had an invalid CRL version. - Switch TestX509.test_version to test valid versions, so it doesn't prevent OpenSSL or an OpenSSL derivative from checking for invalid versions. - Make TestX509Req.test_version tolerate set_version(1) failing on CSRs. Since there's nothing useful to test otherwise, I've made the test work for either possible backend behavior.
1 parent 8e9f0c2 commit 3e4d61a

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/OpenSSL/crypto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ def set_version(self, version):
11481148
if not isinstance(version, int):
11491149
raise TypeError("version must be an integer")
11501150

1151-
_lib.X509_set_version(self._x509, version)
1151+
_openssl_assert(_lib.X509_set_version(self._x509, version) == 1)
11521152

11531153
def get_version(self):
11541154
"""

tests/test_crypto.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,10 @@ def normalize_privatekey_pem(pem):
618618
-----END X509 CRL-----
619619
"""
620620

621+
# The signature on this CRL is invalid.
621622
crlDataUnsupportedExtension = b"""\
622623
-----BEGIN X509 CRL-----
623-
MIIGRzCCBS8CAQIwDQYJKoZIhvcNAQELBQAwJzELMAkGA1UEBhMCVVMxGDAWBgNV
624+
MIIGRzCCBS8CAQEwDQYJKoZIhvcNAQELBQAwJzELMAkGA1UEBhMCVVMxGDAWBgNV
624625
BAMMD2NyeXB0b2dyYXBoeS5pbxgPMjAxNTAxMDEwMDAwMDBaGA8yMDE2MDEwMTAw
625626
MDAwMFowggTOMBQCAQAYDzIwMTUwMTAxMDAwMDAwWjByAgEBGA8yMDE1MDEwMTAw
626627
MDAwMFowXDAYBgNVHRgEERgPMjAxNTAxMDEwMDAwMDBaMDQGA1UdHQQtMCukKTAn
@@ -1598,14 +1599,20 @@ def test_version(self):
15981599
"""
15991600
`X509Req.set_version` sets the X.509 version of the certificate
16001601
request. `X509Req.get_version` returns the X.509 version of the
1601-
certificate request. The initial value of the version is 0.
1602+
certificate request. The only defined version is 0. Others may or
1603+
may not be supported depending on backend.
16021604
"""
16031605
request = X509Req()
16041606
assert request.get_version() == 0
1605-
request.set_version(1)
1606-
assert request.get_version() == 1
1607-
request.set_version(3)
1608-
assert request.get_version() == 3
1607+
request.set_version(0)
1608+
assert request.get_version() == 0
1609+
try:
1610+
request.set_version(1)
1611+
assert request.get_version() == 1
1612+
request.set_version(3)
1613+
assert request.get_version() == 3
1614+
except Error:
1615+
pass
16091616

16101617
def test_version_wrong_args(self):
16111618
"""
@@ -1793,8 +1800,8 @@ def test_version(self):
17931800
`X509.get_version` retrieves it.
17941801
"""
17951802
cert = X509()
1796-
cert.set_version(1234)
1797-
assert cert.get_version() == 1234
1803+
cert.set_version(2)
1804+
assert cert.get_version() == 2
17981805

17991806
def test_serial_number(self):
18001807
"""

0 commit comments

Comments
 (0)