@@ -120,7 +120,7 @@ private ICertificateMaker certEngine
120
120
/// <param name="exceptionFunc"></param>
121
121
internal CertificateManager ( string ? rootCertificateName , string ? rootCertificateIssuerName ,
122
122
bool userTrustRootCertificate , bool machineTrustRootCertificate , bool trustRootCertificateAsAdmin ,
123
- ExceptionHandler exceptionFunc )
123
+ ExceptionHandler ? exceptionFunc )
124
124
{
125
125
ExceptionFunc = exceptionFunc ;
126
126
@@ -167,7 +167,7 @@ internal CertificateManager(string? rootCertificateName, string? rootCertificate
167
167
/// <summary>
168
168
/// Exception handler
169
169
/// </summary>
170
- internal ExceptionHandler ExceptionFunc { get ; set ; }
170
+ internal ExceptionHandler ? ExceptionFunc { get ; set ; }
171
171
172
172
/// <summary>
173
173
/// Select Certificate Engine.
@@ -339,7 +339,7 @@ private void installCertificate(StoreName storeName, StoreLocation storeLocation
339
339
}
340
340
catch ( Exception e )
341
341
{
342
- ExceptionFunc (
342
+ onException (
343
343
new Exception ( "Failed to make system trust root certificate "
344
344
+ $ " for { storeName } \\ { storeLocation } store location. You may need admin rights.",
345
345
e ) ) ;
@@ -360,7 +360,7 @@ private void uninstallCertificate(StoreName storeName, StoreLocation storeLocati
360
360
{
361
361
if ( certificate == null )
362
362
{
363
- ExceptionFunc ( new Exception ( "Could not remove certificate as it is null or empty." ) ) ;
363
+ onException ( new Exception ( "Could not remove certificate as it is null or empty." ) ) ;
364
364
return ;
365
365
}
366
366
@@ -374,9 +374,8 @@ private void uninstallCertificate(StoreName storeName, StoreLocation storeLocati
374
374
}
375
375
catch ( Exception e )
376
376
{
377
- ExceptionFunc (
378
- new Exception ( "Failed to remove root certificate trust "
379
- + $ " for { storeLocation } store location. You may need admin rights.", e ) ) ;
377
+ onException ( new Exception ( "Failed to remove root certificate trust "
378
+ + $ " for { storeLocation } store location. You may need admin rights.", e ) ) ;
380
379
}
381
380
finally
382
381
{
@@ -408,6 +407,11 @@ private X509Certificate2 makeCertificate(string certificateName, bool isRootCert
408
407
return certificate ;
409
408
}
410
409
410
+ private void onException ( Exception exception )
411
+ {
412
+ ExceptionFunc ? . Invoke ( exception ) ;
413
+ }
414
+
411
415
private static ConcurrentDictionary < string , object > saveCertificateLocks
412
416
= new ConcurrentDictionary < string , object > ( ) ;
413
417
@@ -434,13 +438,13 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
434
438
435
439
if ( certificate != null && certificate . NotAfter <= DateTime . Now )
436
440
{
437
- ExceptionFunc ( new Exception ( $ "Cached certificate for { subjectName } has expired.") ) ;
441
+ onException ( new Exception ( $ "Cached certificate for { subjectName } has expired.") ) ;
438
442
certificate = null ;
439
443
}
440
444
}
441
445
catch ( Exception e )
442
446
{
443
- ExceptionFunc ( new Exception ( "Failed to load fake certificate." , e ) ) ;
447
+ onException ( new Exception ( "Failed to load fake certificate." , e ) ) ;
444
448
certificate = null ;
445
449
}
446
450
@@ -472,7 +476,7 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
472
476
}
473
477
catch ( Exception e )
474
478
{
475
- ExceptionFunc ( new Exception ( "Failed to save fake certificate." , e ) ) ;
479
+ onException ( new Exception ( "Failed to save fake certificate." , e ) ) ;
476
480
}
477
481
} ) ;
478
482
}
@@ -484,7 +488,7 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
484
488
}
485
489
catch ( Exception e )
486
490
{
487
- ExceptionFunc ( e ) ;
491
+ onException ( e ) ;
488
492
certificate = null ;
489
493
}
490
494
@@ -628,7 +632,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
628
632
629
633
if ( rootCert != null && rootCert . NotAfter <= DateTime . Now )
630
634
{
631
- ExceptionFunc ( new Exception ( "Loaded root certificate has expired." ) ) ;
635
+ onException ( new Exception ( "Loaded root certificate has expired." ) ) ;
632
636
return false ;
633
637
}
634
638
@@ -641,7 +645,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
641
645
catch ( Exception e )
642
646
{
643
647
// root cert cannot be loaded
644
- ExceptionFunc ( new Exception ( "Root cert cannot be loaded." , e ) ) ;
648
+ onException ( new Exception ( "Root cert cannot be loaded." , e ) ) ;
645
649
}
646
650
}
647
651
@@ -651,7 +655,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
651
655
}
652
656
catch ( Exception e )
653
657
{
654
- ExceptionFunc ( e ) ;
658
+ onException ( e ) ;
655
659
}
656
660
657
661
if ( persistToFile && RootCertificate != null )
@@ -664,14 +668,14 @@ public bool CreateRootCertificate(bool persistToFile = true)
664
668
}
665
669
catch ( Exception e )
666
670
{
667
- ExceptionFunc ( new Exception ( "An error happened when clearing certificate cache." , e ) ) ;
671
+ onException ( new Exception ( "An error happened when clearing certificate cache." , e ) ) ;
668
672
}
669
673
670
674
certificateCache . SaveRootCertificate ( PfxFilePath , PfxPassword , RootCertificate ) ;
671
675
}
672
676
catch ( Exception e )
673
677
{
674
- ExceptionFunc ( e ) ;
678
+ onException ( e ) ;
675
679
}
676
680
}
677
681
@@ -691,15 +695,15 @@ public bool CreateRootCertificate(bool persistToFile = true)
691
695
692
696
if ( rootCert != null && rootCert . NotAfter <= DateTime . Now )
693
697
{
694
- ExceptionFunc ( new ArgumentException ( "Loaded root certificate has expired." ) ) ;
698
+ onException ( new ArgumentException ( "Loaded root certificate has expired." ) ) ;
695
699
return null ;
696
700
}
697
701
698
702
return rootCert ;
699
703
}
700
704
catch ( Exception e )
701
705
{
702
- ExceptionFunc ( e ) ;
706
+ onException ( e ) ;
703
707
return null ;
704
708
}
705
709
}
@@ -808,7 +812,7 @@ public bool TrustRootCertificateAsAdmin(bool machineTrusted = false)
808
812
}
809
813
catch ( Exception e )
810
814
{
811
- ExceptionFunc ( e ) ;
815
+ onException ( e ) ;
812
816
return false ;
813
817
}
814
818
0 commit comments