|
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Security.Cryptography.X509Certificates; |
| 4 | + |
| 5 | +namespace AEMCM.Orchestrator.Tests.TestHelpers |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Builds self-signed / CA-signed PFX blobs for tests using only the BCL |
| 9 | + /// (System.Security.Cryptography). No BouncyCastle dependency. |
| 10 | + /// </summary> |
| 11 | + public static class CertTestFactory |
| 12 | + { |
| 13 | + public const string DefaultPassword = "test-password"; |
| 14 | + |
| 15 | + /// <summary>Self-signed RSA leaf (no chain).</summary> |
| 16 | + public static byte[] CreateSelfSignedRsaPfx( |
| 17 | + string commonName, string[] sans, int keySize = 2048, string password = DefaultPassword) |
| 18 | + { |
| 19 | + using var rsa = RSA.Create(keySize); |
| 20 | + var req = new CertificateRequest( |
| 21 | + $"CN={commonName}", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
| 22 | + AddLeafExtensions(req, sans); |
| 23 | + using var cert = req.CreateSelfSigned( |
| 24 | + DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddYears(1)); |
| 25 | + return cert.Export(X509ContentType.Pkcs12, password); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary>Self-signed ECDSA leaf (no chain). Curve: P-256 (256) or P-384 (384).</summary> |
| 29 | + public static byte[] CreateSelfSignedEcdsaPfx( |
| 30 | + string commonName, string[] sans, int keyBits = 256, string password = DefaultPassword) |
| 31 | + { |
| 32 | + var curve = keyBits == 384 ? ECCurve.NamedCurves.nistP384 : ECCurve.NamedCurves.nistP256; |
| 33 | + using var ecdsa = ECDsa.Create(curve); |
| 34 | + var req = new CertificateRequest($"CN={commonName}", ecdsa, HashAlgorithmName.SHA256); |
| 35 | + AddLeafExtensions(req, sans); |
| 36 | + using var cert = req.CreateSelfSigned( |
| 37 | + DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddYears(1)); |
| 38 | + return cert.Export(X509ContentType.Pkcs12, password); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary>RSA leaf signed by a generated CA, producing a two-cert chain in the PFX.</summary> |
| 42 | + public static byte[] CreateChainedRsaPfx( |
| 43 | + string commonName, string[] sans, int keySize = 2048, string password = DefaultPassword) |
| 44 | + { |
| 45 | + using var caRsa = RSA.Create(2048); |
| 46 | + var caReq = new CertificateRequest( |
| 47 | + "CN=AEMCM Test Root CA", caRsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
| 48 | + caReq.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true)); |
| 49 | + using var caCert = caReq.CreateSelfSigned( |
| 50 | + DateTimeOffset.UtcNow.AddDays(-2), DateTimeOffset.UtcNow.AddYears(10)); |
| 51 | + |
| 52 | + using var leafRsa = RSA.Create(keySize); |
| 53 | + var leafReq = new CertificateRequest( |
| 54 | + $"CN={commonName}", leafRsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
| 55 | + AddLeafExtensions(leafReq, sans); |
| 56 | + |
| 57 | + var serial = new byte[8]; |
| 58 | + RandomNumberGenerator.Fill(serial); |
| 59 | + using var leafPublic = leafReq.Create( |
| 60 | + caCert, DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddYears(1), serial); |
| 61 | + using var leafWithKey = leafPublic.CopyWithPrivateKey(leafRsa); |
| 62 | + |
| 63 | +#if NET9_0_OR_GREATER |
| 64 | + using var caPublic = X509CertificateLoader.LoadCertificate(caCert.RawData); |
| 65 | +#else |
| 66 | + using var caPublic = new X509Certificate2(caCert.RawData); // public-only CA |
| 67 | +#endif |
| 68 | + var collection = new X509Certificate2Collection { leafWithKey, caPublic }; |
| 69 | + return collection.Export(X509ContentType.Pkcs12, password)!; |
| 70 | + } |
| 71 | + |
| 72 | + private static void AddLeafExtensions(CertificateRequest req, string[] sans) |
| 73 | + { |
| 74 | + req.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false)); |
| 75 | + var sanBuilder = new SubjectAlternativeNameBuilder(); |
| 76 | + foreach (var s in sans) sanBuilder.AddDnsName(s); |
| 77 | + req.CertificateExtensions.Add(sanBuilder.Build()); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments