Skip to content

Commit 73b249e

Browse files
authored
Avoid finalizer race in the SslStreamNetworkStreamTests cert creation (#57381)
1 parent 84ca07f commit 73b249e

File tree

1 file changed

+13
-33
lines changed
  • src/libraries/System.Net.Security/tests/FunctionalTests

1 file changed

+13
-33
lines changed

src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs

+13-33
Original file line numberDiff line numberDiff line change
@@ -140,57 +140,37 @@ internal static (X509Certificate2 certificate, X509Certificate2Collection) Gener
140140
PkiOptions.IssuerRevocationViaCrl,
141141
out RevocationResponder responder,
142142
out CertificateAuthority root,
143-
out CertificateAuthority intermediate,
143+
out CertificateAuthority[] intermediates,
144144
out X509Certificate2 endEntity,
145+
intermediateAuthorityCount: longChain ? 3 : 1,
145146
subjectName: targetName,
146147
testName: testName,
147148
keySize: keySize,
148149
extensions: extensions);
149150

150-
if (longChain)
151+
// Walk the intermediates backwards so we build the chain collection as
152+
// Issuer3
153+
// Issuer2
154+
// Issuer1
155+
// Root
156+
for (int i = intermediates.Length - 1; i >= 0; i--)
151157
{
152-
using (RSA intermedKey2 = RSA.Create(keySize))
153-
using (RSA intermedKey3 = RSA.Create(keySize))
154-
{
155-
X509Certificate2 intermedPub2 = intermediate.CreateSubordinateCA(
156-
$"CN=\"A SSL Test CA 2\", O=\"testName\"",
157-
intermedKey2);
158-
159-
X509Certificate2 intermedCert2 = intermedPub2.CopyWithPrivateKey(intermedKey2);
160-
intermedPub2.Dispose();
161-
CertificateAuthority intermediateAuthority2 = new CertificateAuthority(intermedCert2, null, null, null);
162-
163-
X509Certificate2 intermedPub3 = intermediateAuthority2.CreateSubordinateCA(
164-
$"CN=\"A SSL Test CA 3\", O=\"testName\"",
165-
intermedKey3);
166-
167-
X509Certificate2 intermedCert3 = intermedPub3.CopyWithPrivateKey(intermedKey3);
168-
intermedPub3.Dispose();
169-
CertificateAuthority intermediateAuthority3 = new CertificateAuthority(intermedCert3, null, null, null);
158+
CertificateAuthority authority = intermediates[i];
170159

171-
RSA eeKey = endEntity.GetRSAPrivateKey();
172-
endEntity = intermediateAuthority3.CreateEndEntity(
173-
$"CN=\"A SSL Test\", O=\"testName\"",
174-
eeKey,
175-
extensions);
176-
177-
endEntity = endEntity.CopyWithPrivateKey(eeKey);
178-
179-
chain.Add(intermedCert3);
180-
chain.Add(intermedCert2);
181-
}
160+
chain.Add(authority.CloneIssuerCert());
161+
authority.Dispose();
182162
}
183163

184-
chain.Add(intermediate.CloneIssuerCert());
185164
chain.Add(root.CloneIssuerCert());
186165

187166
responder.Dispose();
188167
root.Dispose();
189-
intermediate.Dispose();
190168

191169
if (PlatformDetection.IsWindows)
192170
{
171+
X509Certificate2 ephemeral = endEntity;
193172
endEntity = new X509Certificate2(endEntity.Export(X509ContentType.Pfx));
173+
ephemeral.Dispose();
194174
}
195175

196176
return (endEntity, chain);

0 commit comments

Comments
 (0)