@@ -17,6 +17,13 @@ public class BaseRFPEMTest : BaseTest
1717 {
1818 private static string pemCertificate = string . Empty ;
1919 private static string pemKey = string . Empty ;
20+ private static string b64PFXCertificate = string . Empty ;
21+
22+ public enum CERT_TYPE_ENUM
23+ {
24+ PEM ,
25+ PFX
26+ }
2027
2128
2229 public static void CreateStore ( string fileName , bool withExtKeyFile , bool withCertificate , STORE_ENVIRONMENT_ENUM storeEnvironment )
@@ -34,9 +41,14 @@ public static void RemoveStore(string fileName, bool withExtKeyFile, STORE_ENVIR
3441 RemoveFile ( $ "{ fileName } .key", storeEnvironment ) ;
3542 }
3643
37- public static string CreateCertificateAndKey ( string certNameString )
44+ public string GetNewCert ( )
3845 {
39- if ( ! string . IsNullOrEmpty ( pemCertificate ) )
46+ return b64PFXCertificate ;
47+ }
48+
49+ public static string CreateCertificateAndKey ( string certNameString , CERT_TYPE_ENUM certType )
50+ {
51+ if ( ! string . IsNullOrEmpty ( certType == CERT_TYPE_ENUM . PEM ? pemCertificate : b64PFXCertificate ) )
4052 return string . Empty ;
4153
4254 var keyGen = new RsaKeyPairGenerator ( ) ;
@@ -63,22 +75,38 @@ public static string CreateCertificateAndKey(string certNameString)
6375 // Generate the certificate
6476 X509Certificate certificate = certGen . Generate ( new Asn1SignatureFactory ( "SHA256WITHRSA" , keyPair . Private ) ) ;
6577
66- // Export certificate as PEM
67- using ( var sw = new StringWriter ( ) )
78+ if ( certType == CERT_TYPE_ENUM . PEM )
6879 {
69- var pw = new PemWriter ( sw ) ;
70- pw . WriteObject ( certificate ) ;
71- pw . Writer . Flush ( ) ;
72- pemCertificate = sw . ToString ( ) ;
73- }
80+ // Export certificate as PEM
81+ using ( var sw = new StringWriter ( ) )
82+ {
83+ var pw = new PemWriter ( sw ) ;
84+ pw . WriteObject ( certificate ) ;
85+ pw . Writer . Flush ( ) ;
86+ pemCertificate = sw . ToString ( ) ;
87+ }
7488
75- // Export private key as PEM (unencrypted)
76- using ( var sw = new StringWriter ( ) )
89+ // Export private key as PEM (unencrypted)
90+ using ( var sw = new StringWriter ( ) )
91+ {
92+ var pw = new PemWriter ( sw ) ;
93+ pw . WriteObject ( ( new Pkcs8Generator ( keyPair . Private ) ) . Generate ( ) ) ;
94+ pw . Writer . Flush ( ) ;
95+ pemKey = sw . ToString ( ) ;
96+ }
97+ }
98+ else
7799 {
78- var pw = new PemWriter ( sw ) ;
79- pw . WriteObject ( ( new Pkcs8Generator ( keyPair . Private ) ) . Generate ( ) ) ;
80- pw . Writer . Flush ( ) ;
81- pemKey = sw . ToString ( ) ;
100+ Pkcs12StoreBuilder builder = new Pkcs12StoreBuilder ( ) ;
101+ Pkcs12Store store = builder . Build ( ) ;
102+ store . SetCertificateEntry ( "abc" , new X509CertificateEntry ( certificate ) ) ;
103+ store . SetKeyEntry ( "abc" , new AsymmetricKeyEntry ( keyPair . Private ) , new [ ] { new X509CertificateEntry ( certificate ) } ) ;
104+
105+ using ( MemoryStream ms = new MemoryStream ( ) )
106+ {
107+ store . Save ( ms , EnvironmentVariables . StorePassword ? . ToCharArray ( ) , new SecureRandom ( ) ) ;
108+ b64PFXCertificate = Convert . ToBase64String ( ms . ToArray ( ) ) ;
109+ }
82110 }
83111
84112 return certificate . Thumbprint ( ) ;
0 commit comments