From 5ff36b5c40d40b3804a8a003b9cd7ab1116d5ddd Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Sat, 7 Dec 2024 14:22:33 -0600 Subject: [PATCH] Fix static signing in hosts --- .../Configuration/IdentityServerExtensions.cs | 31 ----------- hosts/main/IdentityServerExtensions.cs | 51 ++++++++++-------- .../TestKeys/identityserver.test.ecdsa.p12 | Bin 2800 -> 0 bytes .../main/TestKeys/identityserver.test.rsa.p12 | Bin 4079 -> 0 bytes 4 files changed, 30 insertions(+), 52 deletions(-) delete mode 100644 hosts/main/TestKeys/identityserver.test.ecdsa.p12 delete mode 100644 hosts/main/TestKeys/identityserver.test.rsa.p12 diff --git a/hosts/Configuration/IdentityServerExtensions.cs b/hosts/Configuration/IdentityServerExtensions.cs index 1911f5720..54e279d40 100644 --- a/hosts/Configuration/IdentityServerExtensions.cs +++ b/hosts/Configuration/IdentityServerExtensions.cs @@ -35,7 +35,6 @@ internal static WebApplicationBuilder ConfigureIdentityServer(this WebApplicatio .AddInMemoryIdentityResources(Resources.IdentityResources) .AddInMemoryApiScopes(Resources.ApiScopes) .AddInMemoryApiResources(Resources.ApiResources) - //.AddStaticSigningCredential() .AddExtensionGrantValidator() .AddExtensionGrantValidator() .AddJwtBearerClientAuthentication() @@ -67,34 +66,4 @@ internal static WebApplicationBuilder ConfigureIdentityServer(this WebApplicatio return builder; } - - private static IIdentityServerBuilder AddStaticSigningCredential(this IIdentityServerBuilder builder) - { - // create random RS256 key - //builder.AddDeveloperSigningCredential(); - - -#pragma warning disable SYSLIB0057 // Type or member is obsolete - // TODO - Use X509CertificateLoader in a future release (when we drop NET8 support) - - // use an RSA-based certificate with RS256 - using var rsaCert = new X509Certificate2("./testkeys/identityserver.test.rsa.p12", "changeit"); - builder.AddSigningCredential(rsaCert, "RS256"); - - // ...and PS256 - builder.AddSigningCredential(rsaCert, "PS256"); - - // or manually extract ECDSA key from certificate (directly using the certificate is not support by Microsoft right now) - using var ecCert = new X509Certificate2("./testkeys/identityserver.test.ecdsa.p12", "changeit"); -#pragma warning restore SYSLIB0057 // Type or member is obsolete - - var key = new ECDsaSecurityKey(ecCert.GetECDsaPrivateKey()) - { - KeyId = CryptoRandom.CreateUniqueId(16, CryptoRandom.OutputFormat.Hex) - }; - - return builder.AddSigningCredential( - key, - IdentityServerConstants.ECDsaSigningAlgorithm.ES256); - } } diff --git a/hosts/main/IdentityServerExtensions.cs b/hosts/main/IdentityServerExtensions.cs index de1097ea1..0228a3dad 100644 --- a/hosts/main/IdentityServerExtensions.cs +++ b/hosts/main/IdentityServerExtensions.cs @@ -77,33 +77,42 @@ internal static WebApplicationBuilder ConfigureIdentityServer(this WebApplicatio return builder; } + // To use static signing credentials, create keys and add it to the certificate store. + // This shows how to create both rsa and ec keys, in case you had clients that were configured to use different algorithms + // You can create keys for dev use with the mkcert util: + // mkcert -pkcs12 identityserver.test.rsa + // mkcert -pkcs12 -ecdsa identityserver.test.ecdsa + // Then import the keys into the certificate manager. This code expect keys in the personal store of the current user. private static IIdentityServerBuilder AddStaticSigningCredential(this IIdentityServerBuilder builder) { - // create random RS256 key - //builder.AddDeveloperSigningCredential(); - - -#pragma warning disable SYSLIB0057 // Type or member is obsolete - // TODO - Use X509CertificateLoader in a future release (when we drop NET8 support) - - // use an RSA-based certificate with RS256 - using var rsaCert = new X509Certificate2("./testkeys/identityserver.test.rsa.p12", "changeit"); - builder.AddSigningCredential(rsaCert, "RS256"); + var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); + try + { + store.Open(OpenFlags.ReadOnly); - // ...and PS256 - builder.AddSigningCredential(rsaCert, "PS256"); + var subs = store.Certificates.Select(c => c.Subject); + var subNames = store.Certificates.Select(c => c.SubjectName); - // or manually extract ECDSA key from certificate (directly using the certificate is not support by Microsoft right now) - using var ecCert = new X509Certificate2("./testkeys/identityserver.test.ecdsa.p12", "changeit"); -#pragma warning restore SYSLIB0057 // Type or member is obsolete + var rsaCert = store.Certificates + .Find(X509FindType.FindBySubjectName, "identityserver.test.rsa", true) + .Single(); + builder.AddSigningCredential(rsaCert, "RS256"); + builder.AddSigningCredential(rsaCert, "PS256"); - var key = new ECDsaSecurityKey(ecCert.GetECDsaPrivateKey()) + var ecCert = store.Certificates + .Find(X509FindType.FindBySubjectName, "identityserver.test.ecdsa", true) + .Single(); + var key = new ECDsaSecurityKey(ecCert.GetECDsaPrivateKey()) + { + KeyId = CryptoRandom.CreateUniqueId(16, CryptoRandom.OutputFormat.Hex) + }; + builder.AddSigningCredential(key, IdentityServerConstants.ECDsaSigningAlgorithm.ES256); + } + finally { - KeyId = CryptoRandom.CreateUniqueId(16, CryptoRandom.OutputFormat.Hex) - }; + store.Close(); + } - return builder.AddSigningCredential( - key, - IdentityServerConstants.ECDsaSigningAlgorithm.ES256); + return builder; } } \ No newline at end of file diff --git a/hosts/main/TestKeys/identityserver.test.ecdsa.p12 b/hosts/main/TestKeys/identityserver.test.ecdsa.p12 deleted file mode 100644 index 5b21ec1bafb4d2936dda7778a692cbdd055d2e2d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2800 zcmV)#f(q;c0Ru3C3b+OdDuzgg_YDCD0ic2ksRV)wr7(gDp)i68j|K@UhDe6@ z4FLxRpn?g2FoFqu0s#Opf(dm72`Yw2hW8Bt2LUh~1_~;MNQUu zg&t*oiblmJLm-f`b8IidmKxTn8Ot}&XhRyhWSTU+! zx{svSK%;T=aX?fK$Va%JtQtgkv2Sb}jAJ8^7GfC@hVY%)hV9)P$v>Kd7z&OK1>nFi zJ8Ee{gtQN&cN9p@Oe1D9f@Yl6jkUfGPJRDJ)lvJNAEBg^_HX;e3$76s_V$A!n$~D_ zua=!!!Oa44-nM(usEiZU;nCJ&xs&C__ds4&u)y^tXf=>`nYDg1R-s&|_vu!Qu>~S` z_)bMs*$si{#$8kUuabZ{P?>43#x{naU%+l&2wVCs0;_(5C{SbGtSSa&EMQsa0&2ul z4@TnlZcYS)%$P{YyRQ)zx)n|+fVYT%DA7fBe9+D7w6%cC86sS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O+RW+Q04rBW@LoJx0G&}JCH9bDT_uAyZ=sx{~^@BQC4 zc#o%$fP#Vy51?_r-S}<;Q`rohGugts?Hg(~NZ>dm$2C1{H=~qr&Q6e!)4)vW`%CF5 zFW)bVs=9-JG0$x0k6Zz6r~1=^8)q{vL+`WqvBB#UYw-OSAJ%-lRpmz|>_*_zxS${5 zz8OdW%Yt|i=`Vu*X&c$4v3`Z!!EN~5DY4A z#bCHb+tNN(ac1)`+VRx*< zm2i=dA3AiDMVH`upgN~0zjjjt(WmS&M-~mMTn%UAR!6?9H>AU&)e^m?sni#HFoFRJ z1_>&LNQUdt0s;sC1c8`?*+p4`19XoXR8YA*h$ztYgk{jUz`}67-o_dh z?(;nY8funYHyLez`In06=(6>_6;s=amR|^d3Tv1Teq1^TUS`SdmN@h_39%gNCb=lq zWI{}GSE>`a7rGbBi~!72und-)J&nSFocq;p!?ilC9~#V(%@(o8aOY*rILVzWO87O? z5d$v)Mq_{i&We}-|5&DHb^AauB`_lf2`Yw2hW8Bt2^BFG1Qh-Aj{vBSz>?1cp=}|P z0GS_v3jr`IFdr}n1_dh)0|FWZ6bRdW3Y$Ig^%LTtE~XY{xf$sG!2}3qtnM_TZQ%7& CuqdPe diff --git a/hosts/main/TestKeys/identityserver.test.rsa.p12 b/hosts/main/TestKeys/identityserver.test.rsa.p12 deleted file mode 100644 index 07a23dee1dc108cc4dbf2c1dd5fa04849baedb00..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4079 zcmVrW6|_X=A5EAGO(b&8UWy2$5{ivA<$2$3VO z=zLzcbN_li7Dl{?!UJX9vB-F#&V5ueFF}Bso;^8?5|?l)Cl{Y1RIIp{;+`DDJ&J@6 z&okckcUyiqIGr8-P7Bbs;)_~pI}foWHm#r7MMjI8ihPKrBBiC;IX3uXcQep9F51OK zCSshjT{ci?N_;IWOKdqzb?5`M$9zsWL&v z&g%wN^XScl7J{@aAI2wiBDPoF7!+|beqqT6So*W9I+3^rb52XcG`090u(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