@@ -15,6 +15,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1515using System . IO ;
1616using System . Net ;
1717using System . Security . Authentication ;
18+ using System . Security . Cryptography ;
1819using System . Security . Cryptography . X509Certificates ;
1920using System . Threading ;
2021using System . Threading . Tasks ;
@@ -262,17 +263,29 @@ public void Start()
262263 {
263264 Startup . Listener = this ;
264265 m_hostBuilder = new WebHostBuilder ( ) ;
266+
267+ // prepare the server TLS certificate
268+ var serverCertificate = m_serverCertificate ;
269+ #if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
270+ try
271+ {
272+ // Create a copy of the certificate with the private key on platforms
273+ // which default to the ephemeral KeySet. Also a new certificate must be reloaded.
274+ // If the key fails to copy, its probably a non exportable key from the X509Store.
275+ // Then we can use the original certificate, the private key is already in the key store.
276+ serverCertificate = X509Utils . CreateCopyWithPrivateKey ( m_serverCertificate , false ) ;
277+ }
278+ catch ( CryptographicException ce )
279+ {
280+ Utils . LogTrace ( "Copy of the private key for https was denied: {0}" , ce . Message ) ;
281+ }
282+ #endif
283+
265284 var httpsOptions = new HttpsConnectionAdapterOptions ( ) {
266285 CheckCertificateRevocation = false ,
267286 ClientCertificateMode = ClientCertificateMode . NoCertificate ,
268287 // note: this is the TLS certificate!
269- #if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
270- // Create a copy of the certificate with the private key on platforms
271- // which default to the ephemeral KeySet.
272- ServerCertificate = X509Utils . CreateCopyWithPrivateKey ( m_serverCertificate , false )
273- #else
274- ServerCertificate = m_serverCertificate
275- #endif
288+ ServerCertificate = serverCertificate ,
276289 } ;
277290
278291#if NET462
0 commit comments