@@ -116,7 +116,7 @@ private void ListCertificates()
116116 var certificates = store . Certificates ;
117117 foreach ( var certificate in certificates )
118118 {
119- Output . WriteLine ( $ "Certificate: '{ Convert . ToBase64String ( certificate . Export ( X509ContentType . Cert ) ) } '.") ;
119+ Output . WriteLine ( $ "Certificate: { certificate . Subject } '{ Convert . ToBase64String ( certificate . Export ( X509ContentType . Cert ) ) } '.") ;
120120 certificate . Dispose ( ) ;
121121 }
122122
@@ -225,7 +225,7 @@ public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat_WithoutKey(
225225 public void EnsureCreateHttpsCertificate_CanImport_ExportedPfx ( )
226226 {
227227 // Arrange
228- const string CertificateName = nameof ( EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates ) + ".pfx" ;
228+ const string CertificateName = nameof ( EnsureCreateHttpsCertificate_CanImport_ExportedPfx ) + ".pfx" ;
229229 var certificatePassword = Guid . NewGuid ( ) . ToString ( ) ;
230230
231231 _fixture . CleanupCertificates ( ) ;
@@ -258,7 +258,7 @@ public void EnsureCreateHttpsCertificate_CanImport_ExportedPfx()
258258 public void EnsureCreateHttpsCertificate_CanImport_ExportedPfx_FailsIfThereAreCertificatesPresent ( )
259259 {
260260 // Arrange
261- const string CertificateName = nameof ( EnsureCreateHttpsCertificate_DoesNotCreateACertificate_WhenThereIsAnExistingHttpsCertificates ) + ".pfx" ;
261+ const string CertificateName = nameof ( EnsureCreateHttpsCertificate_CanImport_ExportedPfx_FailsIfThereAreCertificatesPresent ) + ".pfx" ;
262262 var certificatePassword = Guid . NewGuid ( ) . ToString ( ) ;
263263
264264 _fixture . CleanupCertificates ( ) ;
@@ -280,6 +280,47 @@ public void EnsureCreateHttpsCertificate_CanImport_ExportedPfx_FailsIfThereAreCe
280280 Assert . Equal ( ImportCertificateResult . ExistingCertificatesPresent , result ) ;
281281 }
282282
283+ [ ConditionalFact ]
284+ [ SkipOnHelix ( "https://github.com/dotnet/aspnetcore/issues/6720" , Queues = "All.OSX" ) ]
285+ public void EnsureCreateHttpsCertificate_CannotImportIfTheSubjectNameIsWrong ( )
286+ {
287+ // Arrange
288+ const string CertificateName = nameof ( EnsureCreateHttpsCertificate_CannotImportIfTheSubjectNameIsWrong ) + ".pfx" ;
289+ var certificatePassword = Guid . NewGuid ( ) . ToString ( ) ;
290+
291+ _fixture . CleanupCertificates ( ) ;
292+
293+ var now = DateTimeOffset . UtcNow ;
294+ now = new DateTimeOffset ( now . Year , now . Month , now . Day , now . Hour , now . Minute , now . Second , 0 , now . Offset ) ;
295+ var creation = _manager . EnsureAspNetCoreHttpsDevelopmentCertificate ( now , now . AddYears ( 1 ) , path : null , trust : false , isInteractive : false ) ;
296+ Output . WriteLine ( creation . ToString ( ) ) ;
297+ ListCertificates ( ) ;
298+
299+ var httpsCertificate = _manager . ListCertificates ( StoreName . My , StoreLocation . CurrentUser , isValid : false ) . Single ( c => c . Subject == TestCertificateSubject ) ;
300+
301+ _manager . CleanupHttpsCertificates ( ) ;
302+
303+ using var privateKey = httpsCertificate . GetRSAPrivateKey ( ) ;
304+ var csr = new CertificateRequest ( httpsCertificate . Subject + "Not" , privateKey , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
305+ foreach ( var extension in httpsCertificate . Extensions )
306+ {
307+ csr . CertificateExtensions . Add ( extension ) ;
308+ }
309+ var wrongSubjectCertificate = csr . CreateSelfSigned ( httpsCertificate . NotBefore , httpsCertificate . NotAfter ) ;
310+
311+ Assert . True ( CertificateManager . IsHttpsDevelopmentCertificate ( wrongSubjectCertificate ) ) ;
312+ Assert . NotEqual ( _manager . Subject , wrongSubjectCertificate . Subject ) ;
313+
314+ File . WriteAllBytes ( CertificateName , wrongSubjectCertificate . Export ( X509ContentType . Pfx , certificatePassword ) ) ;
315+
316+ // Act
317+ var result = _manager . ImportCertificate ( CertificateName , certificatePassword ) ;
318+
319+ // Assert
320+ Assert . Equal ( ImportCertificateResult . NoDevelopmentHttpsCertificate , result ) ;
321+ Assert . Empty ( _manager . ListCertificates ( StoreName . My , StoreLocation . CurrentUser , isValid : false ) ) ;
322+ }
323+
283324 [ ConditionalFact ]
284325 [ SkipOnHelix ( "https://github.com/dotnet/aspnetcore/issues/6720" , Queues = "All.OSX" ) ]
285326 public void EnsureCreateHttpsCertificate_CanExportTheCertInPemFormat_WithoutPassword ( )
0 commit comments