@@ -52,6 +52,7 @@ internal class WinCertificateMaker : ICertificateMaker
5252 internal WinCertificateMaker ( ExceptionHandler exceptionFunc )
5353 {
5454 this . exceptionFunc = exceptionFunc ;
55+
5556 typeX500DN = Type . GetTypeFromProgID ( "X509Enrollment.CX500DistinguishedName" , true ) ;
5657 typeX509PrivateKey = Type . GetTypeFromProgID ( "X509Enrollment.CX509PrivateKey" , true ) ;
5758 typeOID = Type . GetTypeFromProgID ( "X509Enrollment.CObjectId" , true ) ;
@@ -74,13 +75,41 @@ internal WinCertificateMaker(ExceptionHandler exceptionFunc)
7475 /// <summary>
7576 /// Make certificate.
7677 /// </summary>
77- /// <param name="sSubjectCN"></param>
78- /// <param name="isRoot"></param>
79- /// <param name="signingCert"></param>
80- /// <returns></returns>
8178 public X509Certificate2 MakeCertificate ( string sSubjectCN , bool isRoot , X509Certificate2 signingCert = null )
8279 {
83- return makeCertificateInternal ( sSubjectCN , isRoot , true , signingCert ) ;
80+ return makeCertificate ( sSubjectCN , isRoot , true , signingCert ) ;
81+ }
82+
83+ private X509Certificate2 makeCertificate ( string sSubjectCN , bool isRoot ,
84+ bool switchToMTAIfNeeded , X509Certificate2 signingCert = null ,
85+ CancellationToken cancellationToken = default )
86+ {
87+ if ( switchToMTAIfNeeded && Thread . CurrentThread . GetApartmentState ( ) != ApartmentState . MTA )
88+ {
89+ return Task . Run ( ( ) => makeCertificate ( sSubjectCN , isRoot , false , signingCert ) ,
90+ cancellationToken ) . Result ;
91+ }
92+
93+ // Subject
94+ string fullSubject = $ "CN={ sSubjectCN } ";
95+
96+ // Sig Algo
97+ const string hashAlgo = "SHA256" ;
98+
99+ // Grace Days
100+ const int graceDays = - 366 ;
101+
102+ // ValiDays
103+ const int validDays = 1825 ;
104+
105+ // KeyLength
106+ const int keyLength = 2048 ;
107+
108+ var graceTime = DateTime . Now . AddDays ( graceDays ) ;
109+ var now = DateTime . Now ;
110+ var certificate = makeCertificate ( isRoot , sSubjectCN , fullSubject , keyLength , hashAlgo , graceTime ,
111+ now . AddDays ( validDays ) , isRoot ? null : signingCert ) ;
112+ return certificate ;
84113 }
85114
86115 private X509Certificate2 makeCertificate ( bool isRoot , string subject , string fullSubject ,
@@ -271,39 +300,9 @@ private X509Certificate2 makeCertificate(bool isRoot, string subject, string ful
271300
272301 string empty = ( string ) typeX509Enrollment . InvokeMember ( "CreatePFX" , BindingFlags . InvokeMethod , null ,
273302 x509Enrollment , typeValue ) ;
303+
274304 return new X509Certificate2 ( Convert . FromBase64String ( empty ) , string . Empty , X509KeyStorageFlags . Exportable ) ;
275305 }
276306
277- private X509Certificate2 makeCertificateInternal ( string sSubjectCN , bool isRoot ,
278- bool switchToMTAIfNeeded , X509Certificate2 signingCert = null ,
279- CancellationToken cancellationToken = default )
280- {
281- if ( switchToMTAIfNeeded && Thread . CurrentThread . GetApartmentState ( ) != ApartmentState . MTA )
282- {
283- return Task . Run ( ( ) => makeCertificateInternal ( sSubjectCN , isRoot , false , signingCert ) ,
284- cancellationToken ) . Result ;
285- }
286-
287- // Subject
288- string fullSubject = $ "CN={ sSubjectCN } ";
289-
290- // Sig Algo
291- const string hashAlgo = "SHA256" ;
292-
293- // Grace Days
294- const int graceDays = - 366 ;
295-
296- // ValiDays
297- const int validDays = 1825 ;
298-
299- // KeyLength
300- const int keyLength = 2048 ;
301-
302- var graceTime = DateTime . Now . AddDays ( graceDays ) ;
303- var now = DateTime . Now ;
304- var certificate = makeCertificate ( isRoot , sSubjectCN , fullSubject , keyLength , hashAlgo , graceTime ,
305- now . AddDays ( validDays ) , isRoot ? null : signingCert ) ;
306- return certificate ;
307- }
308307 }
309308}
0 commit comments