Skip to content

Commit 37996af

Browse files
Ticket #456 : Develop a screen to generate Certificate Authority
1 parent 8f867a8 commit 37996af

36 files changed

+1327
-37
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Org.BouncyCastle.Ocsp;
2+
using System;
3+
using System.IO;
4+
using System.Runtime.ConstrainedExecution;
5+
using System.Security.Cryptography;
6+
using System.Security.Cryptography.X509Certificates;
7+
8+
namespace SimpleIdServer.IdServer.ConformanceSuite.Startup
9+
{
10+
public static class OtherFeatures
11+
{
12+
private const string DIRECTORYPATH = @"c:\Projects\SimpleIdServer\certificates";
13+
14+
public static void CreateCertificateAuthority(string caName)
15+
{
16+
using (RSA parent = RSA.Create(2048))
17+
using (RSA rsa = RSA.Create(2048))
18+
{
19+
CertificateRequest parentReq = new CertificateRequest(
20+
"CN=sidCA",
21+
parent,
22+
HashAlgorithmName.SHA256,
23+
RSASignaturePadding.Pkcs1);
24+
25+
parentReq.CertificateExtensions.Add(
26+
new X509BasicConstraintsExtension(true, false, 0, true));
27+
28+
parentReq.CertificateExtensions.Add(
29+
new X509SubjectKeyIdentifierExtension(parentReq.PublicKey, false));
30+
31+
using (X509Certificate2 parentCert = parentReq.CreateSelfSigned(
32+
DateTimeOffset.UtcNow.AddDays(-45),
33+
DateTimeOffset.UtcNow.AddDays(365)))
34+
{
35+
var exported = new X509Certificate2(parentCert.Export(X509ContentType.Pfx, "password"), "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
36+
var caPrivateKey = parentCert.GetRSAPrivateKey().ExportRSAPrivateKeyPem();
37+
File.WriteAllText(Path.Combine(DIRECTORYPATH, "sidCA.crt"), SanitizePem(parentCert.ExportCertificatePem()));
38+
File.WriteAllText(Path.Combine(DIRECTORYPATH, "sidCA.key"), SanitizePem(caPrivateKey));
39+
File.WriteAllBytes(Path.Combine(DIRECTORYPATH, "sidCA.pfx"), exported.Export(X509ContentType.Pfx, "password"));
40+
CertificateRequest req = new CertificateRequest(
41+
"CN=sidClient",
42+
rsa,
43+
HashAlgorithmName.SHA256,
44+
RSASignaturePadding.Pkcs1);
45+
46+
req.CertificateExtensions.Add(
47+
new X509BasicConstraintsExtension(false, false, 0, false));
48+
49+
req.CertificateExtensions.Add(
50+
new X509KeyUsageExtension(
51+
X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.NonRepudiation | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment,
52+
false));
53+
54+
req.CertificateExtensions.Add(
55+
new X509SubjectKeyIdentifierExtension(req.PublicKey, false));
56+
57+
using (X509Certificate2 cert = req.Create(
58+
parentCert,
59+
DateTimeOffset.UtcNow.AddDays(-1),
60+
DateTimeOffset.UtcNow.AddDays(90),
61+
new byte[] { 1, 2, 3, 4 }))
62+
{
63+
var privatePem = new string(PemEncoding.Write("PRIVATE KEY", rsa.ExportPkcs8PrivateKey()));
64+
File.WriteAllText(Path.Combine(DIRECTORYPATH, "client.crt"), SanitizePem(cert.ExportCertificatePem()));
65+
File.WriteAllText(Path.Combine(DIRECTORYPATH, "client.key"), SanitizePem(privatePem));
66+
// Do something with these certs, like export them to PFX,
67+
// or add them to an X509Store, or whatever.
68+
}
69+
}
70+
}
71+
72+
string SanitizePem(string pem) => pem.Replace("\n", "\r\n");
73+
}
74+
}
75+
}

src/IdServer/SimpleIdServer.IdServer.ConformanceSuite.Startup/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@
1111
using SimpleIdServer.IdServer.Sms;
1212
using System;
1313
using System.Collections.Generic;
14+
using System.IO;
15+
using System.Linq;
1416
using System.Reflection;
1517
using System.Security.Cryptography;
18+
using System.Security.Cryptography.X509Certificates;
19+
20+
OtherFeatures.CreateCertificateAuthority("");
21+
22+
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
23+
store.Open(OpenFlags.ReadOnly);
24+
var cc = store.Certificates.ToList();
25+
var tt = store.Certificates.Find(X509FindType.FindBySubjectName, "sidCA", true).First();
26+
var pp = tt.Export(X509ContentType.Pfx, "password");
1627

1728
var builder = WebApplication.CreateBuilder(args);
1829
builder.Services.Configure<KestrelServerOptions>(options =>

src/IdServer/SimpleIdServer.IdServer.ConformanceSuite.Startup/SimpleIdServer.IdServer.ConformanceSuite.Startup.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PrivateAssets>all</PrivateAssets>
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2121
</PackageReference>
22+
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
2223
</ItemGroup>
2324
<ItemGroup>
2425
<Compile Update="Resources\AccountsResource.Designer.cs">
@@ -132,4 +133,7 @@
132133
<LastGenOutput>LayoutResource.Designer.cs</LastGenOutput>
133134
</EmbeddedResource>
134135
</ItemGroup>
136+
<ItemGroup>
137+
<Folder Include="Cert\" />
138+
</ItemGroup>
135139
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
using System.Security.Cryptography.X509Certificates;
4+
5+
namespace SimpleIdServer.IdServer.Domains
6+
{
7+
public class CertificateAuthority
8+
{
9+
public string Id { get; set; } = null!;
10+
public string SubjectName { get; set; } = null!;
11+
public CertificateAuthoritySources Source { get; set; } = CertificateAuthoritySources.DB;
12+
public StoreLocation? StoreLocation { get; set; } = null;
13+
public StoreName? StoreName { get; set; } = null;
14+
public X509FindType? FindType { get; set; } = null;
15+
public string? FindValue { get; set; } = null;
16+
public string? Password { get; set; } = null;
17+
public string? PublicKey { get; set; } = null;
18+
public string? PrivateKey { get; set; } = null;
19+
public DateTime StartDateTime { get; set; }
20+
public DateTime EndDateTime { get; set; }
21+
public DateTime UpdateDateTime { get; set; }
22+
public ICollection<ClientCertificate> ClientCertificates { get; set; } = new List<ClientCertificate>();
23+
public ICollection<Realm> Realms { get; set; } = new List<Realm>();
24+
}
25+
26+
public enum CertificateAuthoritySources
27+
{
28+
DB = 0,
29+
CERTIFICATESTORE = 1
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
namespace SimpleIdServer.IdServer.Domains
4+
{
5+
public class ClientCertificate
6+
{
7+
public string Id { get; set; } = null!;
8+
public string PublicKey { get; set; } = null!;
9+
public string PrivateKey { get; set; } = null!;
10+
public CertificateAuthority CertificateAuthority { get; set; }
11+
}
12+
}

src/IdServer/SimpleIdServer.IdServer.Domains/Realm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Realm
1515
public ICollection<AuthenticationContextClassReference> AuthenticationContextClassReferences { get; set; } = new List<AuthenticationContextClassReference>();
1616
public ICollection<AuthenticationSchemeProvider> AuthenticationSchemeProviders { get; set; } = new List<AuthenticationSchemeProvider>();
1717
public ICollection<ApiResource> ApiResources { get; set; } = new List<ApiResource>();
18-
public ICollection<SerializedFileKey> SerializedFileKeys { get; set; }
18+
public ICollection<SerializedFileKey> SerializedFileKeys { get; set; } = new List<SerializedFileKey>();
19+
public ICollection<CertificateAuthority> CertificateAuthorities { get; set; } = new List<CertificateAuthority>();
1920
}
2021
}

src/IdServer/SimpleIdServer.IdServer.Startup/IdServerConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,10 @@ public class IdServerConfiguration
6565
{
6666
Constants.StandardRealms.Master
6767
};
68+
69+
public static ICollection<CertificateAuthority> CertificateAuthorities = new List<CertificateAuthority>
70+
{
71+
CertificateAuthorityBuilder.Create("CN=simpleIdServerCA", Constants.StandardRealms.Master).Build()
72+
};
6873
}
6974
}

src/IdServer/SimpleIdServer.IdServer.Startup/Migrations/20230315143557_Init.Designer.cs renamed to src/IdServer/SimpleIdServer.IdServer.Startup/Migrations/20230318213256_Init.Designer.cs

Lines changed: 118 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)