@@ -74,13 +74,41 @@ internal WinCertificateMaker(ExceptionHandler exceptionFunc)
74
74
/// <summary>
75
75
/// Make certificate.
76
76
/// </summary>
77
- /// <param name="sSubjectCN"></param>
78
- /// <param name="isRoot"></param>
79
- /// <param name="signingCert"></param>
80
- /// <returns></returns>
81
77
public X509Certificate2 MakeCertificate ( string sSubjectCN , bool isRoot , X509Certificate2 signingCert = null )
82
78
{
83
- return makeCertificateInternal ( sSubjectCN , isRoot , true , signingCert ) ;
79
+ return makeCertificate ( sSubjectCN , isRoot , true , signingCert ) ;
80
+ }
81
+
82
+ private X509Certificate2 makeCertificate ( string sSubjectCN , bool isRoot ,
83
+ bool switchToMTAIfNeeded , X509Certificate2 signingCert = null ,
84
+ CancellationToken cancellationToken = default )
85
+ {
86
+ if ( switchToMTAIfNeeded && Thread . CurrentThread . GetApartmentState ( ) != ApartmentState . MTA )
87
+ {
88
+ return Task . Run ( ( ) => makeCertificate ( sSubjectCN , isRoot , false , signingCert ) ,
89
+ cancellationToken ) . Result ;
90
+ }
91
+
92
+ // Subject
93
+ string fullSubject = $ "CN={ sSubjectCN } ";
94
+
95
+ // Sig Algo
96
+ const string hashAlgo = "SHA256" ;
97
+
98
+ // Grace Days
99
+ const int graceDays = - 366 ;
100
+
101
+ // ValiDays
102
+ const int validDays = 1825 ;
103
+
104
+ // KeyLength
105
+ const int keyLength = 2048 ;
106
+
107
+ var graceTime = DateTime . Now . AddDays ( graceDays ) ;
108
+ var now = DateTime . Now ;
109
+ var certificate = makeCertificate ( isRoot , sSubjectCN , fullSubject , keyLength , hashAlgo , graceTime ,
110
+ now . AddDays ( validDays ) , isRoot ? null : signingCert ) ;
111
+ return certificate ;
84
112
}
85
113
86
114
private X509Certificate2 makeCertificate ( bool isRoot , string subject , string fullSubject ,
@@ -271,39 +299,9 @@ private X509Certificate2 makeCertificate(bool isRoot, string subject, string ful
271
299
272
300
string empty = ( string ) typeX509Enrollment . InvokeMember ( "CreatePFX" , BindingFlags . InvokeMethod , null ,
273
301
x509Enrollment , typeValue ) ;
302
+
274
303
return new X509Certificate2 ( Convert . FromBase64String ( empty ) , string . Empty , X509KeyStorageFlags . Exportable ) ;
275
304
}
276
305
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
- }
308
306
}
309
307
}
0 commit comments