Skip to content

Commit 3cc7cb0

Browse files
committed
Always add a certificate's CN as a SAN
1 parent 309dcf5 commit 3cc7cb0

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CertificateFixtureBase.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected static X509Certificate2 CreateCertificate(string subjectName, IEnumera
3838
// * Start date: 24hrs ago
3939
// * End date: 24hrs in the future
4040
// * Subject: {subjectName}
41-
// * Subject alternative names: {dnsNames}, {ipAddresses}
41+
// * Subject alternative names: {subjectName}, {dnsNames}, {ipAddresses}
4242
// * Public key: 2048-bit RSA
4343
// * Hash algorithm: SHA256
4444
// * Key usage: digital signature, key encipherment
@@ -55,18 +55,17 @@ protected static X509Certificate2 CreateCertificate(string subjectName, IEnumera
5555
X500DistinguishedNameBuilder subjectBuilder = new X500DistinguishedNameBuilder();
5656
SubjectAlternativeNameBuilder sanBuilder = new SubjectAlternativeNameBuilder();
5757
RSA rsaKey = RSA.Create(2048);
58-
bool hasSans = false;
5958

6059
subjectBuilder.AddCommonName(subjectName);
60+
61+
sanBuilder.AddDnsName(subjectName);
6162
foreach (string dnsName in dnsNames)
6263
{
6364
sanBuilder.AddDnsName(dnsName);
64-
hasSans = true;
6565
}
6666
foreach (string ipAddress in ipAddresses)
6767
{
6868
sanBuilder.AddIpAddress(System.Net.IPAddress.Parse(ipAddress));
69-
hasSans = true;
7069
}
7170

7271
CertificateRequest request = new CertificateRequest(subjectBuilder.Build(), rsaKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
@@ -75,10 +74,7 @@ protected static X509Certificate2 CreateCertificate(string subjectName, IEnumera
7574
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment, false));
7675
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection() { new Oid("1.3.6.1.5.5.7.3.1"), new Oid("1.3.6.1.5.5.7.3.2") }, true));
7776

78-
if (hasSans)
79-
{
80-
request.CertificateExtensions.Add(sanBuilder.Build());
81-
}
77+
request.CertificateExtensions.Add(sanBuilder.Build());
8278

8379
// Generate an ephemeral certificate, then export it and return it as a new certificate with the correct key storage flags set.
8480
// This is to ensure that it's imported into the certificate stores with its private key.

0 commit comments

Comments
 (0)