Files
DotNetCore.SKIT.FlurlHttpCl…/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/__Internal/WechatTenpayClientSigningExtensions.cs

104 lines
4.6 KiB
C#
Raw Normal View History

using System;
2022-05-09 19:28:47 +08:00
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
using SKIT.FlurlHttpClient.Primitives;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
2024-01-29 23:12:37 +08:00
internal static class WechatTenpayClientSigningExtensions
2022-05-09 19:28:47 +08:00
{
2024-01-29 23:12:37 +08:00
public static bool VerifySignature(this WechatTenpayClient client, string strTimestamp, string strNonce, string strContent, string strSignature, string strSignScheme, string strSerialNumber, out Exception? error)
2022-05-09 19:28:47 +08:00
{
2024-01-29 23:12:37 +08:00
if (client is null) throw new ArgumentNullException(nameof(client));
2022-05-09 19:28:47 +08:00
2024-01-29 23:12:37 +08:00
switch (strSignScheme)
{
case Constants.SignSchemes.WECHATPAY2_RSA_2048_WITH_SHA256:
{
2024-01-29 23:12:37 +08:00
if (client.PlatformCertificateManager is null)
{
error = new Exception("The platform certificate manager is not initialized.");
return false;
}
2022-05-09 19:28:47 +08:00
CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber);
if (!entry.HasValue)
{
2024-01-29 23:12:37 +08:00
error = new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\".");
return false;
}
2022-05-09 19:28:47 +08:00
if (!CertificateEntry.ALGORITHM_TYPE_RSA.Equals(entry.Value.AlgorithmType))
{
2024-01-29 23:12:37 +08:00
error = new Exception($"The platform certificate with serial number \"{strSerialNumber}\" is not for RSA.");
return false;
}
2022-05-09 19:28:47 +08:00
error = null;
try
{
return Utilities.RSAUtility.VerifyByCertificate(
certificatePem: entry.Value.Certificate,
message: GenerateMessageForSignature(timestamp: strTimestamp, nonce: strNonce, body: strContent),
encodingSignature: new EncodedString(strSignature, EncodingKinds.Base64)
);
}
catch (Exception ex)
{
error = ex;
return false;
}
}
case Constants.SignSchemes.WECHATPAY2_SM2_WITH_SM3:
2022-05-09 19:28:47 +08:00
{
2024-01-29 23:12:37 +08:00
if (client.PlatformCertificateManager is null)
{
error = new Exception("The platform certificate manager is not initialized.");
return false;
}
CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber);
if (!entry.HasValue)
2022-05-09 19:28:47 +08:00
{
2024-01-29 23:12:37 +08:00
error = new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\".");
return false;
}
2022-05-09 19:28:47 +08:00
if (!CertificateEntry.ALGORITHM_TYPE_SM2.Equals(entry.Value.AlgorithmType))
{
2024-01-29 23:12:37 +08:00
error = new Exception($"The platform certificate with serial number \"{strSerialNumber}\" is not for SM2.");
return false;
2022-05-09 19:28:47 +08:00
}
error = null;
try
{
return Utilities.SM2Utility.VerifyWithSM3ByCertificate(
certificatePem: entry.Value.Certificate,
message: GenerateMessageForSignature(timestamp: strTimestamp, nonce: strNonce, body: strContent),
encodingSignature: new EncodedString(strSignature, EncodingKinds.Base64)
);
}
catch (Exception ex)
{
error = ex;
return false;
}
2022-05-09 19:28:47 +08:00
}
default:
{
2024-01-29 23:12:37 +08:00
error = new Exception($"Unsupported signing scheme: \"{strSignScheme}\".");
2022-05-09 19:28:47 +08:00
return false;
}
}
}
private static string GenerateMessageForSignature(string timestamp, string nonce, string body)
2022-05-09 19:28:47 +08:00
{
return $"{timestamp}\n{nonce}\n{body}\n";
}
}
}