Skip to content

Commit eccb0b9

Browse files
Remove Bouncy Castle dependency when targeting .NET 8 et al. (#3844)
1 parent 7f0d713 commit eccb0b9

9 files changed

+36
-53
lines changed

extensions/src/AWSSDK.Extensions.CloudFront.Signers/AWSSDK.Extensions.CloudFront.Signers.NetStandard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</PropertyGroup>
3434
</Otherwise>
3535
</Choose>
36-
<ItemGroup>
36+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netcoreapp3.1'">
3737
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
3838
</ItemGroup>
3939
<ItemGroup>

extensions/src/AWSSDK.Extensions.CloudFront.Signers/AWSSDK.Extensions.CloudFront.Signers.nuspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>AWSSDK.Extensions.CloudFront.Signers</id>
55
<title>AWSSDK - Extensions for AWS CloudFront</title>
6-
<version>4.0.0.0</version>
6+
<version>4.0.0.1</version>
77
<authors>Amazon Web Services</authors>
88
<description>This package contains extension methods for creating signed URLs for Amazon CloudFront distributions and for creating signed cookies for Amazon CloudFront distributions using canned or custom policies.</description>
99
<language>en-US</language>
@@ -30,7 +30,6 @@
3030
<group targetFramework="net8.0">
3131
<dependency id="AWSSDK.Core" version="4.0.0.0" />
3232
<dependency id="AWSSDK.CloudFront" version="4.0.0.0" />
33-
<dependency id="BouncyCastle.Cryptography" version="2.4.0" />
3433
</group>
3534
</dependencies>
3635
</metadata>

extensions/src/AWSSDK.Extensions.CloudFront.Signers/AmazonCloudFrontCookieSigner.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.IO;
18-
using System.Security.Cryptography;
1918
using System.Text;
2019

21-
using Amazon.CloudFront.Model;
22-
using Amazon.Runtime;
2320
using Amazon.Util;
2421

25-
using System.Globalization;
26-
2722
namespace Amazon.CloudFront
2823
{
2924
/// <summary>
@@ -159,12 +154,11 @@ public static CookiesForCannedPolicy GetCookiesForCannedPolicy(string resourceUr
159154
cookies.Expires = new KeyValuePair<string, string>(
160155
ExpiresKey, epochSeconds);
161156

162-
RSAParameters rsaParameters = AmazonCloudFrontUrlSigner.ConvertPEMToRSAParameters(privateKey);
163157
string cannedPolicy = "{\"Statement\":[{\"Resource\":\"" + resourceUrlOrPath
164158
+ "\",\"Condition\":{\"DateLessThan\":{\"AWS:EpochTime\":" + epochSeconds
165159
+ "}}}]}";
166160
byte[] signatureBytes = AmazonCloudFrontUrlSigner.SignWithSha1RSA(
167-
UTF8Encoding.UTF8.GetBytes(cannedPolicy), rsaParameters);
161+
UTF8Encoding.UTF8.GetBytes(cannedPolicy), privateKey);
168162
string urlSafeSignature = AmazonCloudFrontUrlSigner.MakeBytesUrlSafe(signatureBytes);
169163
cookies.Signature = new KeyValuePair<string, string>(SignatureKey, urlSafeSignature);
170164

@@ -252,9 +246,8 @@ public static CookiesForCustomPolicy GetCookiesForCustomPolicy(string resourceUr
252246
var base64EncodedPolicy = AmazonCloudFrontUrlSigner.MakeStringUrlSafe(policy);
253247
cookies.Policy = new KeyValuePair<string, string>(PolicyKey, base64EncodedPolicy);
254248

255-
RSAParameters rsaParameters = AmazonCloudFrontUrlSigner.ConvertPEMToRSAParameters(privateKey);
256249
byte[] signatureBytes = AmazonCloudFrontUrlSigner.SignWithSha1RSA(
257-
UTF8Encoding.UTF8.GetBytes(policy), rsaParameters);
250+
Encoding.UTF8.GetBytes(policy), privateKey);
258251
string urlSafeSignature = AmazonCloudFrontUrlSigner.MakeBytesUrlSafe(signatureBytes);
259252
cookies.Signature = new KeyValuePair<string, string>(SignatureKey, urlSafeSignature);
260253

extensions/src/AWSSDK.Extensions.CloudFront.Signers/AmazonCloudFrontUrlSigner.cs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
1516
using System;
16-
using System.Collections.Generic;
1717
using System.IO;
1818
using System.Security.Cryptography;
1919
using System.Text;
20-
using Amazon.CloudFront.Model;
2120
using Amazon.Runtime;
2221
using Amazon.Util;
22+
#if !NET
2323
using Org.BouncyCastle.OpenSsl;
24-
using System.Globalization;
2524
using Org.BouncyCastle.Crypto;
2625
using Org.BouncyCastle.Crypto.Parameters;
2726
using Org.BouncyCastle.Security;
27+
#endif
2828

2929
#pragma warning disable 1591
3030

@@ -266,8 +266,7 @@ public static string SignUrl(string resourceUrlOrPath, string keyPairId, FileInf
266266
/// <returns>A signed URL that will permit access to distribution and S3 objects as specified in the policy document.</returns>
267267
public static string SignUrl(string resourceUrlOrPath, string keyPairId, TextReader privateKey, string policy)
268268
{
269-
RSAParameters rsaParameters = ConvertPEMToRSAParameters(privateKey);
270-
byte[] signatureBytes = SignWithSha1RSA(UTF8Encoding.UTF8.GetBytes(policy), rsaParameters);
269+
byte[] signatureBytes = SignWithSha1RSA(Encoding.UTF8.GetBytes(policy), privateKey);
271270

272271
string urlSafePolicy = MakeStringUrlSafe(policy);
273272
string urlSafeSignature = MakeBytesUrlSafe(signatureBytes);
@@ -330,11 +329,10 @@ public static String SignUrlCanned(string resourceUrlOrPath,
330329
DateTime expiresOn)
331330
{
332331
string epochSeconds = AWSSDKUtils.ConvertToUnixEpochSecondsString(expiresOn);
333-
RSAParameters rsaParameters = ConvertPEMToRSAParameters(privateKey);
334332
string cannedPolicy = "{\"Statement\":[{\"Resource\":\"" + resourceUrlOrPath
335333
+ "\",\"Condition\":{\"DateLessThan\":{\"AWS:EpochTime\":" + epochSeconds
336334
+ "}}}]}";
337-
byte[] signatureBytes = SignWithSha1RSA(UTF8Encoding.UTF8.GetBytes(cannedPolicy), rsaParameters);
335+
byte[] signatureBytes = SignWithSha1RSA(Encoding.UTF8.GetBytes(cannedPolicy), privateKey);
338336

339337
string urlSafeSignature = MakeBytesUrlSafe(signatureBytes);
340338

@@ -503,20 +501,23 @@ private static string GenerateResourcePath(Protocol protocol,
503501
/// Signs the data given with the private key given, using the SHA1withRSA
504502
/// algorithm provided by bouncy castle.
505503
/// </summary>
506-
internal static byte[] SignWithSha1RSA(byte[] dataToSign, RSAParameters rsaParameters)
504+
internal static byte[] SignWithSha1RSA(byte[] dataToSign, TextReader privateKey)
507505
{
508-
using (SHA1 cryptoSHA1 = GetSHA1Provider())
506+
using (SHA1 cryptoSHA1 = SHA1.Create())
507+
using (RSA rsa = RSA.Create())
509508
{
510-
var providerRSA = RSA.Create();
511-
providerRSA.ImportParameters(rsaParameters);
509+
ImportRSAFromPem(rsa, privateKey);
512510

513511
byte[] hashedData = cryptoSHA1.ComputeHash(dataToSign);
514-
return GetRSAPKCS1SignatureFromSHA1(hashedData, providerRSA);
512+
return GetRSAPKCS1SignatureFromSHA1(hashedData, rsa);
515513
}
516514
}
517515

518-
internal static RSAParameters ConvertPEMToRSAParameters(TextReader privateKeyReader)
516+
private static void ImportRSAFromPem(RSA rsa, TextReader privateKeyReader)
519517
{
518+
#if NET
519+
rsa.ImportFromPem(privateKeyReader.ReadToEnd());
520+
#else
520521
RSAParameters rsaParams;
521522
try
522523
{
@@ -543,15 +544,7 @@ internal static RSAParameters ConvertPEMToRSAParameters(TextReader privateKeyRea
543544
{
544545
throw new AmazonClientException("Invalid RSA Private Key", e);
545546
}
546-
return rsaParams;
547-
}
548-
549-
private static SHA1 GetSHA1Provider()
550-
{
551-
#if NETSTANDARD
552-
return SHA1.Create();
553-
#else
554-
return new SHA1CryptoServiceProvider();
547+
rsa.ImportParameters(rsaParams);
555548
#endif
556549
}
557550

@@ -567,4 +560,4 @@ private static byte[] GetRSAPKCS1SignatureFromSHA1(byte[] hashedData, RSA provid
567560
#endif
568561
}
569562
}
570-
}
563+
}

extensions/src/AWSSDK.Extensions.EC2.DecryptPassword/AWSSDK.Extensions.EC2.DecryptPassword.NetStandard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</PropertyGroup>
3333
</Otherwise>
3434
</Choose>
35-
<ItemGroup>
35+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netcoreapp3.1'">
3636
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
3737
</ItemGroup>
3838
<ItemGroup>

extensions/src/AWSSDK.Extensions.EC2.DecryptPassword/AWSSDK.Extensions.EC2.DecryptPassword.nuspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>AWSSDK.Extensions.EC2.DecryptPassword</id>
55
<title>AWSSDK - Extensions for AWS EC2</title>
6-
<version>4.0.0.0</version>
6+
<version>4.0.0.1</version>
77
<authors>Amazon Web Services</authors>
88
<description>Extensions for the AWS EC2 to get the decrypted password for an EC2 instance.</description>
99
<language>en-US</language>
@@ -30,7 +30,6 @@
3030
<group targetFramework="net8.0">
3131
<dependency id="AWSSDK.Core" version="4.0.0.0" />
3232
<dependency id="AWSSDK.EC2" version="4.0.0.0" />
33-
<dependency id="BouncyCastle.Cryptography" version="2.4.0" />
3433
</group>
3534
</dependencies>
3635
</metadata>

extensions/src/AWSSDK.Extensions.EC2.DecryptPassword/GetPasswordDataResponseExtensions.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
1516
using System;
16-
using System.Collections.Generic;
17-
using System.Xml.Serialization;
17+
using System.Security.Cryptography;
1818
using System.Text;
19-
20-
using Amazon.EC2;
19+
#if !NET
2120
using Amazon.Runtime;
2221
using System.IO;
23-
using System.Security.Cryptography;
24-
using Org.BouncyCastle.OpenSsl;
2522
using Org.BouncyCastle.Crypto;
2623
using Org.BouncyCastle.Crypto.Parameters;
27-
using System.Runtime.CompilerServices;
28-
using System.Diagnostics;
24+
using Org.BouncyCastle.OpenSsl;
2925
using Org.BouncyCastle.Security;
26+
#endif
27+
3028
namespace Amazon.EC2.Model
3129
{
3230
/// <summary>
@@ -43,6 +41,10 @@ public static class GetPasswordDataResponseExtensions
4341
/// <returns>The decrypted password</returns>
4442
public static string GetDecryptedPassword(this GetPasswordDataResponse getPasswordDataResponse, string rsaPrivateKey)
4543
{
44+
RSA rsa = RSA.Create();
45+
#if NET
46+
rsa.ImportFromPem(rsaPrivateKey.AsSpan().Trim());
47+
#else
4648
RSAParameters rsaParams;
4749
try
4850
{
@@ -69,12 +71,11 @@ public static string GetDecryptedPassword(this GetPasswordDataResponse getPasswo
6971
{
7072
throw new AmazonEC2Exception("Invalid RSA Private Key", e);
7173
}
72-
73-
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
7474
rsa.ImportParameters(rsaParams);
75+
#endif
7576

7677
byte[] encryptedBytes = Convert.FromBase64String(getPasswordDataResponse.PasswordData);
77-
var decryptedBytes = rsa.Decrypt(encryptedBytes, false);
78+
var decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.Pkcs1);
7879

7980
string decrypted = Encoding.UTF8.GetString(decryptedBytes);
8081
return decrypted;

extensions/test/CloudFront.SignersTests/CloudFront.Signers.Tests.NetStandard.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.1;net8.0</TargetFrameworks>
44
<AssemblyName>CloudFront.SignersTests</AssemblyName>
@@ -13,7 +13,6 @@
1313
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
1414
</PropertyGroup>
1515
<ItemGroup>
16-
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
1716
<PackageReference Include="xunit" Version="2.4.2" />
1817
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
1918
</ItemGroup>

extensions/test/EC2.DecryptPasswordTests/EC2.DecryptPassword.NetStandard.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.1;net8.0</TargetFrameworks>
44
<AssemblyName>EC2.DecryptPasswordTests</AssemblyName>
@@ -14,7 +14,6 @@
1414
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1515
</PropertyGroup>
1616
<ItemGroup>
17-
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
1817
<PackageReference Include="xunit" Version="2.4.2" />
1918
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
2019
</ItemGroup>

0 commit comments

Comments
 (0)