Skip to content

Commit

Permalink
Support empty string in Name.from_rfc4514_string() (#10964)
Browse files Browse the repository at this point in the history
Empty string is a valid result from RFC4514 serialization, and should parse successfully.

According to https://datatracker.ietf.org/doc/html/rfc4514#section-2.1

> If the RDNSequence is an empty sequence, the result is the empty or zero-length string.
  • Loading branch information
intgr authored May 9, 2024
1 parent 8a9709e commit acc3226
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Changelog
* Added :attr:`~cryptography.x509.InvalidityDate.invalidity_date_utc`, a
timezone-aware alternative to the naïve ``datetime`` attribute
:attr:`~cryptography.x509.InvalidityDate.invalidity_date`.
* Added support for parsing empty DN string in
:meth:`~cryptography.x509.Name.from_rfc4514_string`.

.. _v42-0-7:

Expand Down
4 changes: 4 additions & 0 deletions src/cryptography/x509/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ def parse(self) -> Name:
we parse it, we need to reverse again to get the RDNs on the
correct order.
"""

if not self._has_data():
return Name([])

rdns = [self._parse_rdn()]

while self._has_data():
Expand Down
1 change: 1 addition & 0 deletions tests/x509/test_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_valid(self, subtests):
"2.5.4.10=abc",
Name([NameAttribute(NameOID.ORGANIZATION_NAME, "abc")]),
),
("", Name([])),
]:
with subtests.test():
result = Name.from_rfc4514_string(value)
Expand Down
3 changes: 3 additions & 0 deletions tests/x509/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -5885,6 +5885,9 @@ def test_distinguished_name_custom_attrs(self):
{NameOID.COMMON_NAME: "CommonName", NameOID.EMAIL_ADDRESS: "E"}
) == ("CommonName=Santa Claus,[email protected]")

def test_empty_name(self):
assert x509.Name([]).rfc4514_string() == ""

def test_empty_value(self):
na = x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "")
assert na.rfc4514_string() == r"ST="
Expand Down

0 comments on commit acc3226

Please sign in to comment.