Skip to content

Commit 1bbbb73

Browse files
authored
fix ecdsa signing issue. (#16772)
1 parent f03cbea commit 1bbbb73

9 files changed

Lines changed: 48 additions & 6 deletions

File tree

modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ namespace {{packageName}}.Client
373373
else
374374
ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount);
375375

376-
var signedBytes = ecdsa.SignHash(dataToSign);
377-
var derBytes = ConvertToECDSAANS1Format(signedBytes);
376+
var derBytes = ecdsa.SignHash(dataToSign, DSASignatureFormat.Rfc3279DerSequence);
378377
var signedString = System.Convert.ToBase64String(derBytes);
379378

380379
return signedString;
@@ -384,6 +383,11 @@ namespace {{packageName}}.Client
384383
{{/net60OrLater}}
385384
}
386385

386+
/// <summary>
387+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
388+
/// </summary>
389+
/// <param name="signedBytes"></param>
390+
/// <returns></returns>
387391
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
388392
{
389393
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,17 @@ private string GetECDSASignature(byte[] dataToSign)
380380
else
381381
ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount);
382382

383-
var signedBytes = ecdsa.SignHash(dataToSign);
384-
var derBytes = ConvertToECDSAANS1Format(signedBytes);
383+
var derBytes = ecdsa.SignHash(dataToSign, DSASignatureFormat.Rfc3279DerSequence);
385384
var signedString = System.Convert.ToBase64String(derBytes);
386385

387386
return signedString;
388387
}
389388

389+
/// <summary>
390+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
391+
/// </summary>
392+
/// <param name="signedBytes"></param>
393+
/// <returns></returns>
390394
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
391395
{
392396
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ private string GetECDSASignature(byte[] dataToSign)
352352
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
353353
}
354354

355+
/// <summary>
356+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
357+
/// </summary>
358+
/// <param name="signedBytes"></param>
359+
/// <returns></returns>
355360
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
356361
{
357362
var derBytes = new List<byte>();

samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,17 @@ private string GetECDSASignature(byte[] dataToSign)
380380
else
381381
ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount);
382382

383-
var signedBytes = ecdsa.SignHash(dataToSign);
384-
var derBytes = ConvertToECDSAANS1Format(signedBytes);
383+
var derBytes = ecdsa.SignHash(dataToSign, DSASignatureFormat.Rfc3279DerSequence);
385384
var signedString = System.Convert.ToBase64String(derBytes);
386385

387386
return signedString;
388387
}
389388

389+
/// <summary>
390+
/// Convert ANS1 format to DER format. Not recommended to use because it generate inavlid signature occationally.
391+
/// </summary>
392+
/// <param name="signedBytes"></param>
393+
/// <returns></returns>
390394
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
391395
{
392396
var derBytes = new List<byte>();

0 commit comments

Comments
 (0)