Skip to content

Commit

Permalink
Put test name in generated certificate chains. (#50048)
Browse files Browse the repository at this point in the history
This helps the chain builder to not confuse which certificates should
be used in the chain by using unique names for each test.
  • Loading branch information
vcsjones authored Mar 23, 2021
1 parent e51470d commit 2f6c5d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Formats.Asn1;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Test.Cryptography;
using Xunit;
Expand Down Expand Up @@ -71,10 +72,12 @@ public static void BuildInvalidSignatureTwice(
X509ChainStatusFlags intermediateErrors,
X509ChainStatusFlags rootErrors)
{
string testName = $"{nameof(BuildInvalidSignatureTwice)} {endEntityErrors} {intermediateErrors} {rootErrors}";
TestDataGenerator.MakeTestChain3(
out X509Certificate2 endEntityCert,
out X509Certificate2 intermediateCert,
out X509Certificate2 rootCert);
out X509Certificate2 rootCert,
testName: testName);

X509Certificate2 TamperIfNeeded(X509Certificate2 input, X509ChainStatusFlags flags)
{
Expand Down Expand Up @@ -442,10 +445,12 @@ public static void CustomRootTrustDoesNotTrustIntermediates(
bool saveAllInCustomTrustStore,
X509ChainStatusFlags chainFlags)
{
string testName = $"{nameof(CustomRootTrustDoesNotTrustIntermediates)} {saveAllInCustomTrustStore} {chainFlags}";
TestDataGenerator.MakeTestChain3(
out X509Certificate2 endEntityCert,
out X509Certificate2 intermediateCert,
out X509Certificate2 rootCert);
out X509Certificate2 rootCert,
testName: testName);

using (endEntityCert)
using (intermediateCert)
Expand Down Expand Up @@ -891,7 +896,8 @@ private static X509ChainStatusFlags PlatformPolicyConstraints(X509ChainStatusFla
private static void TestNameConstrainedChain(
string intermediateNameConstraints,
SubjectAlternativeNameBuilder endEntitySanBuilder,
Action<bool, X509Chain> body)
Action<bool, X509Chain> body,
[CallerMemberName] string testName = null)
{
X509Extension[] endEntityExtensions = new []
{
Expand Down Expand Up @@ -921,7 +927,8 @@ private static void TestNameConstrainedChain(
out X509Certificate2 intermediateCert,
out X509Certificate2 rootCert,
intermediateExtensions: intermediateExtensions,
endEntityExtensions: endEntityExtensions);
endEntityExtensions: endEntityExtensions,
testName: testName);

using (endEntityCert)
using (intermediateCert)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System.Security.Cryptography.X509Certificates.Tests
{
Expand All @@ -13,7 +14,8 @@ internal static void MakeTestChain3(
out X509Certificate2 rootCert,
IEnumerable<X509Extension> endEntityExtensions = null,
IEnumerable<X509Extension> intermediateExtensions = null,
IEnumerable<X509Extension> rootExtensions = null)
IEnumerable<X509Extension> rootExtensions = null,
[CallerMemberName] string testName = null)
{
using (RSA rootKey = RSA.Create())
using (RSA intermediateKey = RSA.Create())
Expand All @@ -32,7 +34,8 @@ internal static void MakeTestChain3(
certs,
endEntityExtensions,
intermediateExtensions,
rootExtensions);
rootExtensions,
testName);

endEntityCert = certs[0];
intermediateCert = certs[1];
Expand All @@ -48,7 +51,8 @@ internal static void MakeTestChain4(
out X509Certificate2 rootCert,
IEnumerable<X509Extension> endEntityExtensions = null,
IEnumerable<X509Extension> intermediateExtensions = null,
IEnumerable<X509Extension> rootExtensions = null)
IEnumerable<X509Extension> rootExtensions = null,
[CallerMemberName] string testName = null)
{
using (RSA rootKey = RSA.Create())
using (RSA intermediateKey = RSA.Create())
Expand All @@ -68,7 +72,8 @@ internal static void MakeTestChain4(
certs,
endEntityExtensions,
intermediateExtensions,
rootExtensions);
rootExtensions,
testName);

endEntityCert = certs[0];
intermediateCert1 = certs[1];
Expand All @@ -82,7 +87,8 @@ internal static void MakeTestChain(
Span<X509Certificate2> certs,
IEnumerable<X509Extension> endEntityExtensions,
IEnumerable<X509Extension> intermediateExtensions,
IEnumerable<X509Extension> rootExtensions)
IEnumerable<X509Extension> rootExtensions,
string testName)
{
if (keys.Length < 2)
throw new ArgumentException(nameof(keys));
Expand Down Expand Up @@ -128,7 +134,7 @@ internal static void MakeTestChain(
RSASignaturePadding signaturePadding = RSASignaturePadding.Pkcs1;

CertificateRequest rootReq = new CertificateRequest(
"CN=Test Root",
$"CN=Test Root, O=\"{testName}\"",
keys[rootIndex],
hashAlgorithm,
signaturePadding);
Expand All @@ -155,7 +161,7 @@ internal static void MakeTestChain(
presentationNumber++;

CertificateRequest intermediateReq = new CertificateRequest(
$"CN=Intermediate Layer {presentationNumber}",
$"CN=Intermediate Layer {presentationNumber}, O=\"{testName}\"",
keys[i],
hashAlgorithm,
signaturePadding);
Expand All @@ -177,7 +183,7 @@ internal static void MakeTestChain(
}

CertificateRequest eeReq = new CertificateRequest(
"CN=End-Entity",
$"CN=End-Entity, O=\"{testName}\"",
keys[0],
hashAlgorithm,
signaturePadding);
Expand Down

0 comments on commit 2f6c5d6

Please sign in to comment.