- Notifications
You must be signed in to change notification settings - Fork7.7k
UpdateDnsNameList
forX509Certificate2
to useX509SubjectAlternativeNameExtension.EnumerateDnsNames
Method#24714
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from6 commits
b5cc6d2
931e758
25f6635
ad4407d
998c669
e238015
18a73cd
1938119
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3309,30 +3309,37 @@ public EnhancedKeyUsageProperty(X509Certificate2 cert) | ||
public sealed class DnsNameProperty | ||
{ | ||
private readonly List<DnsNameRepresentation> _dnsList = new(); | ||
private readonly IdnMapping idnMapping = new(); | ||
private const string distinguishedNamePrefix = "CN="; | ||
/// <summary> | ||
/// Get property of DnsNameList. | ||
/// </summary> | ||
public List<DnsNameRepresentation> DnsNameList => _dnsList; | ||
private DnsNameRepresentation GetDnsNameRepresentation(string dnsName) | ||
{ | ||
string unicodeName; | ||
try | ||
{ | ||
unicodeName = idnMapping.GetUnicode(dnsName); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
// The name is not valid Punycode, assume it's valid ASCII. | ||
unicodeName = dnsName; | ||
} | ||
return new DnsNameRepresentation(dnsName, unicodeName); | ||
} | ||
/// <summary> | ||
/// Constructor for DnsNameProperty. | ||
/// </summary> | ||
public DnsNameProperty(X509Certificate2 cert) | ||
{ | ||
_dnsList = new List<DnsNameRepresentation>(); | ||
// extract DNS name from subject distinguish name | ||
@@ -3341,50 +3348,24 @@ public DnsNameProperty(X509Certificate2 cert) | ||
if (cert.Subject.StartsWith(distinguishedNamePrefix, System.StringComparison.OrdinalIgnoreCase) && | ||
!cert.Subject.Contains(',')) | ||
{ | ||
string parsedSubjectDistinguishedDnsName = cert.Subject.Substring(distinguishedNamePrefix.Length); | ||
DnsNameRepresentation dnsName = GetDnsNameRepresentation(parsedSubjectDistinguishedDnsName); | ||
Comment on lines +3351 to +3352 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. @iSazonov I wonder if this can also be replaced withhttps://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x500distinguishedname.enumeraterelativedistinguishednames?view=net-9.0. I don't think even distinguished name needs string parsing anymore. ContributorAuthor
| ||
_dnsList.Add(dnsName); | ||
} | ||
// Extract DNS names from SAN extensions | ||
foreach (X509Extension extension in cert.Extensions) | ||
{ | ||
if (extension is X509SubjectAlternativeNameExtension sanExtension) | ||
{ | ||
foreach (string dnsNameEntry in sanExtension.EnumerateDnsNames()) | ||
{ | ||
DnsNameRepresentation dnsName = GetDnsNameRepresentation(dnsNameEntry); | ||
// Only add the name if it is not the same as an existing name. | ||
if (!_dnsList.Contains(dnsName)) | ||
{ | ||
_dnsList.Add(dnsName); | ||
} | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -292,4 +292,80 @@ Describe "Certificate Provider tests" -Tags "Feature" { | ||
$certs.Thumbprint | Should -BeExactly $thumbprint | ||
} | ||
} | ||
Context "SAN DNS Name Tests" { | ||
BeforeAll { | ||
$configFilePath = Join-Path -Path $TestDrive -ChildPath 'openssl.cnf' | ||
$keyFilePath = Join-Path -Path $TestDrive -ChildPath 'privateKey.key' | ||
$certFilePath = Join-Path -Path $TestDrive -ChildPath 'certificate.crt' | ||
$pfxFilePath = Join-Path -Path $TestDrive -ChildPath 'certificate.pfx' | ||
$password = "test" | ||
ArmaanMcleod marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
$config = @" | ||
[ req ] | ||
default_bits = 2048 | ||
distinguished_name = req_distinguished_name | ||
req_extensions = v3_req | ||
prompt = no | ||
[ req_distinguished_name ] | ||
CN = yourdomain.com | ||
[ v3_req ] | ||
subjectAltName = @alt_names | ||
[ alt_names ] | ||
DNS.1 = yourdomain.com | ||
DNS.2 = www.yourdomain.com | ||
DNS.3 = api.yourdomain.com | ||
DNS.4 = xn--mnchen-3ya.com | ||
DNS.5 = xn--80aaxitdbjr.com | ||
DNS.6 = xn--caf-dma.com | ||
"@ | ||
# Write the configuration to the specified path | ||
Set-Content -Path $configFilePath -Value $config | ||
# Generate the self-signed certificate with SANs | ||
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $keyFilePath -out $certFilePath -config $configFilePath -extensions v3_req | ||
# Create the PFX file | ||
openssl pkcs12 -export -out $pfxFilePath -inkey $keyFilePath -in $certFilePath -passout pass:$password | ||
} | ||
It "Should set DNSNameList from SAN extensions" { | ||
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxFilePath, $password) | ||
$expectedDnsNameList = @( | ||
[PSCustomObject]@{ | ||
Punycode = "yourdomain.com" | ||
Unicode = "yourdomain.com" | ||
} | ||
[PSCustomObject]@{ | ||
Punycode = "www.yourdomain.com" | ||
Unicode = "www.yourdomain.com" | ||
} | ||
[PSCustomObject]@{ | ||
Punycode = "api.yourdomain.com" | ||
Unicode = "api.yourdomain.com" | ||
} | ||
[PSCustomObject]@{ | ||
Punycode = "xn--mnchen-3ya.com" | ||
Unicode = "münchen.com" | ||
} | ||
[PSCustomObject]@{ | ||
Punycode = "xn--80aaxitdbjr.com" | ||
Unicode = "папитрока.com" | ||
} | ||
[PSCustomObject]@{ | ||
Punycode = "xn--caf-dma.com" | ||
Unicode = "café.com" | ||
} | ||
) | ||
$cert | Should -Not -BeNullOrEmpty | ||
$cert.DnsNameList | Should -HaveCount 6 | ||
($cert.DnsNameList | ConvertTo-Json -Compress) | Should -BeExactly ($expectedDnsNameList | ConvertTo-Json -Compress) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.