dotnet / dotnet/runtime

X500DistinguishedName cannot parse an unquoted name

Open
#125,750 4 comments 2 reactions 0 assignees View on GitHub
area-System.Security
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

When using the constructor of `X500DistinguishedName` and passing in an unquoted RDN, an exception is thrown. This is despite the `X500DistinguishedNameFlags.DoNotUseQuotes` flag being used.

This is despite unquoted (but correctly escaped) DNs being allowed according to the RFC1779.

FIrst reported on [StackOverflow](https://stackoverflow.com/questions/79910134/x500distinguishedname-doesnt-work-with-escaped-commas).

### Reproduction Steps

We can show it doesn't even parse back its own output.
```cs
var dn = new X500DistinguishedName("CN=\"Litware\\, Adatum\",DC=Fabrikam,DC=COM");
var decoded = dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.Reversed);
var dn2 = new X500DistinguishedName(decoded, X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.Reversed);
```

### Expected behavior

The DN should be parsed, without any string pre-manipulation of the input.

### Actual behavior

`CryptographicException`
```
The string contains an invalid X500 name attribute key, oid, value or delimiter.

at System.Security.Cryptography.X509Certificates.X509Pal.X500DistinguishedNameEncode(String distinguishedName, X500DistinguishedNameFlags flag)
at System.Security.Cryptography.X509Certificates.X500DistinguishedName..ctor(String distinguishedName, X500DistinguishedNameFlags flag)
```

### Regression?

_No response_

### Known Workarounds

Obviously you can just make sure to get the DN quoted, but sometimes inputs come from elsewhere, and regexing them probably isn't wise.

### Configuration

.NET 9.0.14
Windows 11 25H2 26200.8037

### Other information

It appears to be calling through to the `CertStrToNameW` function in WINAPI, which in turn is not able to parse this despite the correct flag being thrown.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.