77using NRedisStack . Search ;
88using NRedisStack . Search . Aggregation ;
99using NRedisStack . Search . Literals . Enums ;
10+ using Org . BouncyCastle . Crypto ;
1011using Org . BouncyCastle . Crypto . Parameters ;
1112using Org . BouncyCastle . Math ;
1213using Org . BouncyCastle . OpenSsl ;
@@ -300,7 +301,7 @@ public void TestJsonConvert()
300301
301302#if NET481
302303 [ Fact ]
303- public void TestRedisCloudConnection ( )
304+ public void TestRedisCloudConnection_net481 ( )
304305 {
305306 var root = Path . GetFullPath ( Directory . GetCurrentDirectory ( ) ) ;
306307 var redisCaPath = Path . GetFullPath ( Path . Combine ( root , "redis_ca.pem" ) ) ;
@@ -317,12 +318,8 @@ public void TestRedisCloudConnection()
317318 var rsa = RSA . Create ( ) ;
318319
319320 var redisUserPrivateKeyText = File . ReadAllText ( redisUserPrivateKeyPath ) ;
320- var pemFileData = File . ReadAllLines ( redisUserPrivateKeyPath ) . Where ( x => ! x . StartsWith ( "-" ) ) ;
321- var binaryEncoding = Convert . FromBase64String ( string . Join ( null , pemFileData ) ) ;
321+ rsa . ImportParameters ( ImportPrivateKey ( redisUserPrivateKeyText ) ) ;
322322
323- rsa . ImportParameters ( ImportPrivateKey ( File . ReadAllText ( redisUserPrivateKeyPath ) ) ) ;
324- redisUserCertificate . CopyWithPrivateKey ( rsa ) ;
325- rsa . ImportParameters ( ImportPrivateKey ( File . ReadAllText ( redisUserPrivateKeyText ) ) ) ;
326323 var clientCert = redisUserCertificate . CopyWithPrivateKey ( rsa ) ;
327324
328325 // Connect to Redis Cloud
@@ -333,7 +330,7 @@ public void TestRedisCloudConnection()
333330 Password = password
334331 } ;
335332
336- redisConfiguration . CertificateSelection += ( _ , _ , _ , _ , _ ) => clientCert ;
333+ redisConfiguration . CertificateSelection += ( _ , _ , _ , _ , _ ) => new X509Certificate2 ( clientCert . Export ( X509ContentType . Pfx ) ) ;
337334
338335 redisConfiguration . CertificateValidation += ( _ , cert , _ , errors ) =>
339336 {
@@ -373,18 +370,26 @@ public void TestRedisCloudConnection()
373370
374371 public static RSAParameters ImportPrivateKey ( string pem )
375372 {
376- PemReader pr = new PemReader ( new StringReader ( pem ) ) ;
377- RsaPrivateCrtKeyParameters privKey = ( RsaPrivateCrtKeyParameters ) pr . ReadObject ( ) ;
373+ using var sr = new StringReader ( pem ) ;
374+ PemReader pr = new PemReader ( sr ) ;
378375 RSAParameters rp = new RSAParameters ( ) ;
379- rp . Modulus = privKey . Modulus . ToByteArrayUnsigned ( ) ;
380- rp . Exponent = privKey . PublicExponent . ToByteArrayUnsigned ( ) ;
381- rp . P = privKey . P . ToByteArrayUnsigned ( ) ;
382- rp . Q = privKey . Q . ToByteArrayUnsigned ( ) ;
383- rp . D = ConvertRSAParametersField ( privKey . Exponent , rp . Modulus . Length ) ;
384- rp . DP = ConvertRSAParametersField ( privKey . DP , rp . P . Length ) ;
385- rp . DQ = ConvertRSAParametersField ( privKey . DQ , rp . Q . Length ) ;
386- rp . InverseQ = ConvertRSAParametersField ( privKey . QInv , rp . Q . Length ) ;
387-
376+ while ( sr . Peek ( ) != - 1 )
377+ {
378+ var privKey = pr . ReadObject ( ) as AsymmetricCipherKeyPair ;
379+ var pkParamaters = ( RsaPrivateCrtKeyParameters ) privKey . Private ;
380+ if ( privKey != null )
381+ {
382+ rp . Modulus = pkParamaters . Modulus . ToByteArrayUnsigned ( ) ;
383+ rp . Exponent = pkParamaters . PublicExponent . ToByteArrayUnsigned ( ) ;
384+ rp . P = pkParamaters . P . ToByteArrayUnsigned ( ) ;
385+ rp . Q = pkParamaters . Q . ToByteArrayUnsigned ( ) ;
386+ rp . D = ConvertRSAParametersField ( pkParamaters . Exponent , rp . Modulus . Length ) ;
387+ rp . DP = ConvertRSAParametersField ( pkParamaters . DP , rp . P . Length ) ;
388+ rp . DQ = ConvertRSAParametersField ( pkParamaters . DQ , rp . Q . Length ) ;
389+ rp . InverseQ = ConvertRSAParametersField ( pkParamaters . QInv , rp . Q . Length ) ;
390+ }
391+ }
392+ pr . ReadObject ( ) ;
388393 return rp ;
389394 }
390395
0 commit comments