Bläddra i källkod

支付宝证书配置,支持Base64文本

Roc 6 år sedan
förälder
incheckning
617663c6c4

+ 2 - 5
src/Essensoft.AspNetCore.Payment.Alipay/AlipayClient.cs

@@ -489,15 +489,12 @@ namespace Essensoft.AspNetCore.Payment.Alipay
                 throw new AlipayException("支付宝公钥证书校验失败,请确认是否为支付宝签发的有效公钥证书");
             }
 
-            var alipayCertBase64 = response.AlipayCertContent;
-            var alipayCertBytes = Convert.FromBase64String(alipayCertBase64);
-            var alipayCertContent = Encoding.UTF8.GetString(alipayCertBytes);
-            if (!AntCertificationUtil.IsTrusted(alipayCertContent, options.RootCert))
+            if (!AntCertificationUtil.IsTrusted(response.AlipayCertContent, options.RootCert))
             {
                 throw new AlipayException("支付宝公钥证书校验失败,请确认是否为支付宝签发的有效公钥证书");
             }
 
-            var alipayCert = AntCertificationUtil.ParseCert(alipayCertContent);
+            var alipayCert = AntCertificationUtil.ParseCert(response.AlipayCertContent);
             var alipayCertSN = AntCertificationUtil.GetCertSN(alipayCert);
             var newAlipayPublicKey = AntCertificationUtil.ExtractPemPublicKeyFromCert(alipayCert);
             _certificateManager.TryAdd(alipayCertSN, newAlipayPublicKey);

+ 6 - 25
src/Essensoft.AspNetCore.Payment.Alipay/AlipayNotifyClient.cs

@@ -42,7 +42,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
 
             var parameters = GetParameters(request);
             var rsp = AlipayDictionaryParser.Parse<T>(parameters);
-            CheckNotifySign(parameters, options);
+            CheckNotifySign(parameters, options, false);
             return Task.FromResult(rsp);
         }
 
@@ -62,14 +62,14 @@ namespace Essensoft.AspNetCore.Payment.Alipay
                 throw new ArgumentNullException(nameof(options.SignType));
             }
 
-            if (string.IsNullOrEmpty(options.AlipayPublicCert))
+            if (string.IsNullOrEmpty(options.AlipayPublicCertKey))
             {
-                throw new ArgumentNullException(nameof(options.AlipayPublicCert));
+                throw new ArgumentNullException(nameof(options.AlipayPublicCertKey));
             }
 
             var parameters = GetParameters(request);
             var rsp = AlipayDictionaryParser.Parse<T>(parameters);
-            CheckNotifySignCertificate(parameters, options);
+            CheckNotifySign(parameters, options, true);
             return Task.FromResult(rsp);
         }
 
@@ -97,7 +97,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
             return parameters;
         }
 
-        private void CheckNotifySign(IDictionary<string, string> dictionary, AlipayOptions options)
+        private void CheckNotifySign(IDictionary<string, string> dictionary, AlipayOptions options, bool useCert)
         {
             if (dictionary == null || dictionary.Count == 0)
             {
@@ -110,26 +110,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
             }
 
             var prestr = GetSignContent(dictionary);
-            if (!AlipaySignature.RSACheckContent(prestr, sign, options.AlipayPublicKey, options.SignType))
-            {
-                throw new AlipayException("sign check fail: check Sign Data Fail!");
-            }
-        }
-
-        private void CheckNotifySignCertificate(IDictionary<string, string> dictionary, AlipayOptions options)
-        {
-            if (dictionary == null || dictionary.Count == 0)
-            {
-                throw new AlipayException("sign check fail: dictionary is Empty!");
-            }
-
-            if (!dictionary.TryGetValue("sign", out var sign))
-            {
-                throw new AlipayException("sign check fail: sign is Empty!");
-            }
-
-            var prestr = GetSignContent(dictionary);
-            if (!AlipaySignature.RSACheckContent(prestr, sign, options.AlipayPublicCertificate, options.SignType))
+            if (!AlipaySignature.RSACheckContent(prestr, sign, useCert ? options.AlipayPublicCertKey : options.AlipayPublicKey, options.SignType))
             {
                 throw new AlipayException("sign check fail: check Sign Data Fail!");
             }

+ 0 - 1
src/Essensoft.AspNetCore.Payment.Alipay/ServiceCollectionExtensions.cs

@@ -21,7 +21,6 @@ namespace Essensoft.AspNetCore.Payment.Alipay
             services.AddSingleton<IAlipayClient, AlipayClient>();
 
 #if NETCOREAPP3_1
-
             services.AddSingleton<IAlipayNotifyClient, AlipayNotifyClient>();
 #endif
 

+ 0 - 13
src/Essensoft.AspNetCore.Payment.Alipay/Utility/AlipaySignature.cs

@@ -54,19 +54,6 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
             }
         }
 
-        public static bool RSACheckContent(string data, string sign, X509Certificate publicCert, string signType)
-        {
-            var publicKey = AntCertificationUtil.ExtractPemPublicKeyFromCert(publicCert);
-            var key = RSAUtilities.GetRSAParametersFormPublicKey(publicKey);
-            switch (signType)
-            {
-                case "RSA2":
-                    return SHA256WithRSA.Verify(data, sign, key);
-                default:
-                    return SHA1WithRSA.Verify(data, sign, key);
-            }
-        }
-
         public static string AESEncrypt(string data, string encyptKey)
         {
             return AES.Encrypt(data, encyptKey, AES_IV, CipherMode.CBC, PaddingMode.PKCS7);

+ 5 - 6
src/Essensoft.AspNetCore.Payment.Alipay/Utility/AntCertificationUtil.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
-using System.Text;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.X509;
 using MD5 = Essensoft.AspNetCore.Payment.Security.MD5;
@@ -56,8 +55,8 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// <returns>X509Certificate证书对象</returns>
         public static X509Certificate ParseCert(string certificate)
         {
-            var certContent = File.Exists(certificate) ? File.ReadAllText(certificate) : certificate;
-            return new X509CertificateParser().ReadCertificate(Encoding.UTF8.GetBytes(certContent));
+            var stream = File.Exists(certificate) ? File.OpenRead(certificate) : (Stream)new MemoryStream(Convert.FromBase64String(certificate));
+            return new X509CertificateParser().ReadCertificate(stream);
         }
 
         /// <summary>
@@ -101,10 +100,10 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// </summary>
         /// <param name="cert">证书链文本</param>
         /// <returns>证书链集合</returns>
-        private static List<X509Certificate> ReadPemCertChain(string cert)
+        private static List<X509Certificate> ReadPemCertChain(string certificate)
         {
-            var certContent = File.Exists(cert) ? File.ReadAllText(cert) : cert;
-            var collection = new X509CertificateParser().ReadCertificates(Encoding.UTF8.GetBytes(certContent));
+            var stream = File.Exists(certificate) ? File.OpenRead(certificate) : (Stream)new MemoryStream(Convert.FromBase64String(certificate));
+            var collection = new X509CertificateParser().ReadCertificates(stream);
             var result = new List<X509Certificate>();
             foreach (var each in collection)
             {