@@ -39,7 +39,6 @@ class PEMCertificateStoreSerializer : ICertificateStoreSerializer
39
39
private bool IsTrustStore { get ; set ; }
40
40
private bool IncludesChain { get ; set ; }
41
41
private string SeparatePrivateKeyFilePath { get ; set ; }
42
- private bool IsRSAPrivateKey { get ; set ; }
43
42
private bool IgnorePrivateKeyOnInventory { get ; set ; }
44
43
45
44
private ILogger logger ;
@@ -53,9 +52,6 @@ public PEMCertificateStoreSerializer(string storeProperties)
53
52
public Pkcs12Store DeserializeRemoteCertificateStore ( byte [ ] storeContentBytes , string storePath , string storePassword , IRemoteHandler remoteHandler , bool isInventory )
54
53
{
55
54
logger . MethodEntry ( LogLevel . Debug ) ;
56
-
57
- if ( IsRSAPrivateKey && ! string . IsNullOrEmpty ( storePassword ) )
58
- throw new RemoteFileException ( $ "Certificate store with an RSA Private Key cannot contain a store password. Invalid store format not supported.") ;
59
55
60
56
Pkcs12StoreBuilder storeBuilder = new Pkcs12StoreBuilder ( ) ;
61
57
Pkcs12Store store = storeBuilder . Build ( ) ;
@@ -72,7 +68,12 @@ public Pkcs12Store DeserializeRemoteCertificateStore(byte[] storeContentBytes, s
72
68
}
73
69
else
74
70
{
75
- AsymmetricKeyEntry keyEntry = GetPrivateKey ( storeContents , storePassword ?? string . Empty , remoteHandler ) ;
71
+ bool isRSAPrivateKey = false ;
72
+ AsymmetricKeyEntry keyEntry = GetPrivateKey ( storeContents , storePassword ?? string . Empty , remoteHandler , out isRSAPrivateKey ) ;
73
+
74
+ if ( isRSAPrivateKey && ! string . IsNullOrEmpty ( storePassword ) )
75
+ throw new RemoteFileException ( $ "Certificate store with an RSA Private Key cannot contain a store password. Invalid store format not supported.") ;
76
+
76
77
store . SetKeyEntry ( CertificateConverterFactory . FromBouncyCastleCertificate ( certificates [ 0 ] . Certificate ) . ToX509Certificate2 ( ) . Thumbprint , keyEntry , certificates ) ;
77
78
}
78
79
@@ -93,9 +94,6 @@ public List<SerializedStoreInfo> SerializeRemoteCertificateStore(Pkcs12Store cer
93
94
{
94
95
logger . MethodEntry ( LogLevel . Debug ) ;
95
96
96
- if ( IsRSAPrivateKey && ! string . IsNullOrEmpty ( storePassword ) )
97
- throw new RemoteFileException ( $ "Certificate store with an RSA Private Key cannot contain a store password. Invalid store format not supported.") ;
98
-
99
97
string pemString = string . Empty ;
100
98
string keyString = string . Empty ;
101
99
List < SerializedStoreInfo > storeInfo = new List < SerializedStoreInfo > ( ) ;
@@ -113,6 +111,17 @@ public List<SerializedStoreInfo> SerializeRemoteCertificateStore(Pkcs12Store cer
113
111
}
114
112
else
115
113
{
114
+ string storeContents = Encoding . ASCII . GetString ( remoteHandler . DownloadCertificateFile ( storePath + storeFileName ) ) ;
115
+ bool isRSAPrivateKey = false ;
116
+ try
117
+ {
118
+ GetPrivateKey ( storeContents , storePassword , remoteHandler , out isRSAPrivateKey ) ;
119
+ }
120
+ catch ( RemoteFileException ) { }
121
+
122
+ if ( isRSAPrivateKey && ! string . IsNullOrEmpty ( storePassword ) )
123
+ throw new RemoteFileException ( $ "Certificate store with an RSA Private Key cannot contain a store password. Invalid store format not supported.") ;
124
+
116
125
bool keyEntryProcessed = false ;
117
126
foreach ( string alias in certificateStore . Aliases )
118
127
{
@@ -131,7 +140,7 @@ public List<SerializedStoreInfo> SerializeRemoteCertificateStore(Pkcs12Store cer
131
140
X509CertificateEntry [ ] certEntries = certificateStore . GetCertificateChain ( alias ) ;
132
141
AsymmetricKeyParameter publicKey = certEntries [ 0 ] . Certificate . GetPublicKey ( ) ;
133
142
134
- if ( IsRSAPrivateKey )
143
+ if ( isRSAPrivateKey )
135
144
{
136
145
TextWriter textWriter = new StringWriter ( ) ;
137
146
PemWriter pemWriter = new PemWriter ( textWriter ) ;
@@ -185,7 +194,6 @@ private void LoadCustomProperties(string storeProperties)
185
194
IsTrustStore = properties . IsTrustStore == null || string . IsNullOrEmpty ( properties . IsTrustStore . Value ) ? false : bool . Parse ( properties . IsTrustStore . Value ) ;
186
195
IncludesChain = properties . IncludesChain == null || string . IsNullOrEmpty ( properties . IncludesChain . Value ) ? false : bool . Parse ( properties . IncludesChain . Value ) ;
187
196
SeparatePrivateKeyFilePath = properties . SeparatePrivateKeyFilePath == null || string . IsNullOrEmpty ( properties . SeparatePrivateKeyFilePath . Value ) ? String . Empty : properties . SeparatePrivateKeyFilePath . Value ;
188
- IsRSAPrivateKey = properties . IsRSAPrivateKey == null || string . IsNullOrEmpty ( properties . IsRSAPrivateKey . Value ) ? false : bool . Parse ( properties . IsRSAPrivateKey . Value ) ;
189
197
IgnorePrivateKeyOnInventory = properties . IgnorePrivateKeyOnInventory == null || string . IsNullOrEmpty ( properties . IgnorePrivateKeyOnInventory . Value ) ? false : bool . Parse ( properties . IgnorePrivateKeyOnInventory . Value ) ;
190
198
191
199
logger . MethodExit ( LogLevel . Debug ) ;
@@ -222,7 +230,7 @@ private X509CertificateEntry[] GetCertificates(string certificates)
222
230
return certificateEntries . ToArray ( ) ;
223
231
}
224
232
225
- private AsymmetricKeyEntry GetPrivateKey ( string storeContents , string storePassword , IRemoteHandler remoteHandler )
233
+ private AsymmetricKeyEntry GetPrivateKey ( string storeContents , string storePassword , IRemoteHandler remoteHandler , out bool isRSA )
226
234
{
227
235
logger . MethodEntry ( LogLevel . Debug ) ;
228
236
@@ -231,8 +239,18 @@ private AsymmetricKeyEntry GetPrivateKey(string storeContents, string storePassw
231
239
storeContents = Encoding . ASCII . GetString ( remoteHandler . DownloadCertificateFile ( SeparatePrivateKeyFilePath ) ) ;
232
240
}
233
241
242
+ isRSA = false ;
243
+ foreach ( string begDelim in PrivateKeyDelimetersPkcs1 )
244
+ {
245
+ if ( storeContents . Contains ( begDelim ) )
246
+ {
247
+ isRSA = true ;
248
+ break ;
249
+ }
250
+ }
251
+
234
252
string privateKey = string . Empty ;
235
- foreach ( string begDelim in IsRSAPrivateKey ? PrivateKeyDelimetersPkcs1 : PrivateKeyDelimetersPkcs8 )
253
+ foreach ( string begDelim in isRSA ? PrivateKeyDelimetersPkcs1 : PrivateKeyDelimetersPkcs8 )
236
254
{
237
255
string endDelim = begDelim . Replace ( "BEGIN" , "END" ) ;
238
256
@@ -252,7 +270,7 @@ private AsymmetricKeyEntry GetPrivateKey(string storeContents, string storePassw
252
270
throw new RemoteFileException ( "Invalid private key: No private key or invalid private key format found." ) ;
253
271
254
272
PrivateKeyConverter c ;
255
- if ( IsRSAPrivateKey )
273
+ if ( isRSA )
256
274
{
257
275
RSA rsa = RSA . Create ( ) ;
258
276
int bytesRead ;
0 commit comments