Skip to content

Commit 4183beb

Browse files
romualdalex
authored andcommitted
Handle NULL bytes in get_components() values (#804)
* Handle NULL bytes in get_components() values Some old software may generate "bogus" CN with each character preceded by a NULL. This is already handled in commonName, but wasn't in get_components() * review fixes (fix py3 test & avoid unpack/cast)
1 parent 7d5a3bf commit 4183beb

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/OpenSSL/crypto.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,11 @@ def get_components(self):
695695
nid = _lib.OBJ_obj2nid(fname)
696696
name = _lib.OBJ_nid2sn(nid)
697697

698-
result.append((
699-
_ffi.string(name),
700-
_ffi.string(
701-
_lib.ASN1_STRING_data(fval),
702-
_lib.ASN1_STRING_length(fval))))
698+
# ffi.string does not handle strings containing NULL bytes
699+
# (which may have been generated by old, broken software)
700+
value = _ffi.buffer(_lib.ASN1_STRING_data(fval),
701+
_lib.ASN1_STRING_length(fval))[:]
702+
result.append((_ffi.string(name), value))
703703

704704
return result
705705

tests/test_crypto.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,17 @@ def test_load_nul_byte_attribute(self):
12141214
subject = cert.get_subject()
12151215
assert "null.python.org\x00example.org" == subject.commonName
12161216

1217+
def test_load_nul_byte_components(self):
1218+
"""
1219+
An `X509Name` from an `X509` instance loaded from a file can have a
1220+
NUL byte in the value of its components
1221+
"""
1222+
cert = load_certificate(FILETYPE_PEM, nulbyteSubjectAltNamePEM)
1223+
subject = cert.get_subject()
1224+
components = subject.get_components()
1225+
ccn = [value for name, value in components if name == b'CN']
1226+
assert ccn[0] == b'null.python.org\x00example.org'
1227+
12171228
def test_set_attribute_failure(self):
12181229
"""
12191230
If the value of an attribute cannot be set for some reason then

0 commit comments

Comments
 (0)