Skip to content

Commit c54ed30

Browse files
committed
Encode ECDSA signature based on RFC 3279.
1 parent 5daa0c1 commit c54ed30

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

sdk/src/Core/AWSSDK.Core.NetFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
<ItemGroup>
7272
<PackageReference Include="System.Buffers" Version="4.5.1" />
73+
<PackageReference Include="System.Formats.Asn1" Version="6.0.1" />
7374
<PackageReference Include="System.Memory" Version="4.5.5" />
7475
<!-- Powershell (pwsh) will have a preloaded version of System.Text.Json that is older than
7576
the version we will ship in core nuget packages and dlls, so we compile with an older version of STJ

sdk/src/Core/AWSSDK.Core.NetStandard.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
an older version of System.Text.Json. -->
8989
<PackageReference Include="System.Text.Json" Version="6.0.11" />
9090
</ItemGroup>
91-
<Import Project="overrides.targets"/>
91+
<ItemGroup Condition="'$(TargetFramework)' != 'net8.0'">
92+
<!-- Version 8.x emits a warning about being unsupported on .NET Core 3.1, so we use 6.x.
93+
Also, 6.0.0 has a vulnerability. -->
94+
<PackageReference Include="System.Formats.Asn1" Version="6.0.1" />
95+
</ItemGroup>
96+
<Import Project="overrides.targets" />
9297

9398
</Project>

sdk/src/Core/Amazon.Runtime/Internal/Auth/AWS4aSigner.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
using System.Linq;
1919
using System.Text;
2020
using System.Globalization;
21+
using System.Runtime.CompilerServices;
22+
using System.Security.Cryptography;
2123
using Amazon.Util;
2224
using Amazon.Runtime.Internal.Util;
2325
using Amazon.Runtime.Identity;
24-
using System.Security.Cryptography;
25-
using System.Runtime.CompilerServices;
26+
27+
#if !NET7_0_OR_GREATER
28+
using System.Formats.Asn1;
29+
#endif
2630

2731
using static Amazon.Runtime.Internal.Auth.AWS4Signer;
2832

@@ -382,8 +386,24 @@ public static byte[] SignBlob(ImmutableCredentials credentials, string data)
382386
public static byte[] SignBlob(ImmutableCredentials credentials, byte[] data)
383387
{
384388
var key = credentials.AWS4aSigningKey ??= ComputeSigningKey(credentials.AccessKey, credentials.SecretKey);
385-
return key.SignData(data, HashAlgorithmName.SHA256);
389+
#if NET7_0_OR_GREATER
390+
return key.SignData(data, HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence);
391+
#else
392+
return ConvertToRfc3279DerSequence(key.SignData(data, HashAlgorithmName.SHA256));
393+
#endif
394+
}
395+
396+
#if !NET7_0_OR_GREATER
397+
private static byte[] ConvertToRfc3279DerSequence(byte[] signature)
398+
{
399+
var writer = new AsnWriter(AsnEncodingRules.DER);
400+
writer.PushSequence();
401+
writer.WriteIntegerUnsigned(signature.AsSpan(0, signature.Length / 2)); // R value
402+
writer.WriteIntegerUnsigned(signature.AsSpan(signature.Length / 2)); // S value
403+
writer.PopSequence();
404+
return writer.Encode();
386405
}
406+
#endif
387407
#endregion
388408
}
389409

0 commit comments

Comments
 (0)