|
3 | 3 |
|
4 | 4 | using System.Globalization; |
5 | 5 | using System.Net; |
| 6 | +using System.Runtime.CompilerServices; |
6 | 7 | using System.Security.Claims; |
| 8 | +using System.Security.Cryptography; |
7 | 9 | using System.Security.Cryptography.X509Certificates; |
8 | 10 | using System.Xml.Linq; |
9 | 11 | using Microsoft.AspNetCore.Builder; |
@@ -929,39 +931,110 @@ private static async Task<IHost> CreateHost( |
929 | 931 |
|
930 | 932 | private static class Certificates |
931 | 933 | { |
932 | | - public static X509Certificate2 SelfSignedPrimaryRoot { get; private set; } = |
933 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSelfSignedPrimaryRootCertificate.cer")); |
| 934 | + private static string ServerEku = "1.3.6.1.5.5.7.3.1"; |
| 935 | + private static string ClientEku = "1.3.6.1.5.5.7.3.2"; |
934 | 936 |
|
935 | | - public static X509Certificate2 SignedSecondaryRoot { get; private set; } = |
936 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSignedSecondaryRootCertificate.cer")); |
| 937 | + static Certificates() |
| 938 | + { |
| 939 | + DateTimeOffset now = DateTimeOffset.UtcNow; |
| 940 | + |
| 941 | + SelfSignedPrimaryRoot = MakeCert( |
| 942 | + "CN=Valid Self Signed Client EKU,OU=dev,DC=idunno-dev,DC=org", |
| 943 | + ClientEku, |
| 944 | + now); |
| 945 | + |
| 946 | + SignedSecondaryRoot = MakeCert( |
| 947 | + "CN=Valid Signed Secondary Root EKU,OU=dev,DC=idunno-dev,DC=org", |
| 948 | + ClientEku, |
| 949 | + now); |
| 950 | + |
| 951 | + SelfSignedValidWithServerEku = MakeCert( |
| 952 | + "CN=Valid Self Signed Server EKU,OU=dev,DC=idunno-dev,DC=org", |
| 953 | + ServerEku, |
| 954 | + now); |
| 955 | + |
| 956 | + SelfSignedValidWithClientEku = MakeCert( |
| 957 | + "CN=Valid Self Signed Server EKU,OU=dev,DC=idunno-dev,DC=org", |
| 958 | + ClientEku, |
| 959 | + now); |
| 960 | + |
| 961 | + SelfSignedValidWithNoEku = MakeCert( |
| 962 | + "CN=Valid Self Signed No EKU,OU=dev,DC=idunno-dev,DC=org", |
| 963 | + eku: null, |
| 964 | + now); |
| 965 | + |
| 966 | + SelfSignedExpired = MakeCert( |
| 967 | + "CN=Expired Self Signed,OU=dev,DC=idunno-dev,DC=org", |
| 968 | + eku: null, |
| 969 | + now.AddYears(-1), |
| 970 | + now.AddDays(-1)); |
| 971 | + |
| 972 | + SelfSignedNotYetValid = MakeCert( |
| 973 | + "CN=Not Valid Yet Self Signed,OU=dev,DC=idunno-dev,DC=org", |
| 974 | + eku: null, |
| 975 | + now.AddYears(2), |
| 976 | + now.AddYears(3)); |
| 977 | + |
| 978 | + SignedClient = MakeCert( |
| 979 | + "CN=Valid Signed Client,OU=dev,DC=idunno-dev,DC=org", |
| 980 | + ClientEku, |
| 981 | + now); |
937 | 982 |
|
938 | | - public static X509Certificate2 SignedClient { get; private set; } = |
939 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSignedClientCertificate.cer")); |
| 983 | + } |
940 | 984 |
|
941 | | - public static X509Certificate2 SelfSignedValidWithClientEku { get; private set; } = |
942 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSelfSignedClientEkuCertificate.cer")); |
| 985 | + private static readonly X509KeyUsageExtension s_digitalSignatureOnlyUsage = |
| 986 | + new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature, true); |
943 | 987 |
|
944 | | - public static X509Certificate2 SelfSignedValidWithNoEku { get; private set; } = |
945 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSelfSignedNoEkuCertificate.cer")); |
| 988 | + private static X509Certificate2 MakeCert( |
| 989 | + string subjectName, |
| 990 | + string eku, |
| 991 | + DateTimeOffset now) |
| 992 | + { |
| 993 | + return MakeCert(subjectName, eku, now, now.AddYears(5)); |
| 994 | + } |
946 | 995 |
|
947 | | - public static X509Certificate2 SelfSignedValidWithServerEku { get; private set; } = |
948 | | - new X509Certificate2(GetFullyQualifiedFilePath("validSelfSignedServerEkuCertificate.cer")); |
| 996 | + private static X509Certificate2 MakeCert( |
| 997 | + string subjectName, |
| 998 | + string eku, |
| 999 | + DateTimeOffset notBefore, |
| 1000 | + DateTimeOffset notAfter) |
| 1001 | + { |
| 1002 | + using (RSA key = RSA.Create(2048)) |
| 1003 | + { |
| 1004 | + CertificateRequest request = new CertificateRequest( |
| 1005 | + subjectName, |
| 1006 | + key, |
| 1007 | + HashAlgorithmName.SHA256, |
| 1008 | + RSASignaturePadding.Pkcs1); |
949 | 1009 |
|
950 | | - public static X509Certificate2 SelfSignedNotYetValid { get; private set; } = |
951 | | - new X509Certificate2(GetFullyQualifiedFilePath("selfSignedNoEkuCertificateNotValidYet.cer")); |
| 1010 | + request.CertificateExtensions.Add(s_digitalSignatureOnlyUsage); |
952 | 1011 |
|
953 | | - public static X509Certificate2 SelfSignedExpired { get; private set; } = |
954 | | - new X509Certificate2(GetFullyQualifiedFilePath("selfSignedNoEkuCertificateExpired.cer")); |
| 1012 | + if (eku != null) |
| 1013 | + { |
| 1014 | + request.CertificateExtensions.Add( |
| 1015 | + new X509EnhancedKeyUsageExtension( |
| 1016 | + new OidCollection { new Oid(eku, null) }, false)); |
| 1017 | + } |
955 | 1018 |
|
956 | | - private static string GetFullyQualifiedFilePath(string filename) |
957 | | - { |
958 | | - var filePath = Path.Combine(AppContext.BaseDirectory, filename); |
959 | | - if (!File.Exists(filePath)) |
960 | | - { |
961 | | - throw new FileNotFoundException(filePath); |
| 1019 | + return request.CreateSelfSigned(notBefore, notAfter); |
962 | 1020 | } |
963 | | - return filePath; |
964 | 1021 | } |
| 1022 | + |
| 1023 | + public static X509Certificate2 SelfSignedPrimaryRoot { get; private set; } |
| 1024 | + |
| 1025 | + public static X509Certificate2 SignedSecondaryRoot { get; private set; } |
| 1026 | + |
| 1027 | + public static X509Certificate2 SignedClient { get; private set; } |
| 1028 | + |
| 1029 | + public static X509Certificate2 SelfSignedValidWithClientEku { get; private set; } |
| 1030 | + |
| 1031 | + public static X509Certificate2 SelfSignedValidWithNoEku { get; private set; } |
| 1032 | + |
| 1033 | + public static X509Certificate2 SelfSignedValidWithServerEku { get; private set; } |
| 1034 | + |
| 1035 | + public static X509Certificate2 SelfSignedNotYetValid { get; private set; } |
| 1036 | + |
| 1037 | + public static X509Certificate2 SelfSignedExpired { get; private set; } |
965 | 1038 | } |
966 | 1039 | } |
967 | 1040 |
|
0 commit comments