diff --git a/CA-Platform-Linker/Models/Coss/CossModels.cs b/CA-Platform-Linker/Models/Coss/CossModels.cs index 56a1b87..34e4029 100644 --- a/CA-Platform-Linker/Models/Coss/CossModels.cs +++ b/CA-Platform-Linker/Models/Coss/CossModels.cs @@ -75,4 +75,22 @@ namespace CA_Platform_Linker.Models.Coss public string oriData { get; set; } public string attachCert { get; set; } } + + /// + /// 验时间戳返回数据 + /// + public class CossVerifyResultData + { + /// + /// 验签结果:1=通过,-1=时间戳验证不通过,-2=原文验证不通过,-3=不是所信任的根,-4=证书未生效,-5=查询不到此证书,-6=服务器证书过期 + /// + public int verifyRes { get; set; } + public string verifyMsg { get; set; } + } + + public class VerifyTssRequest : CossBaseRequest + { + public string oriData { get; set; } + public string tsResp { get; set; } + } } diff --git a/CA-Platform-Linker/Services/ClientService.cs b/CA-Platform-Linker/Services/ClientService.cs index 641b995..50ca5aa 100644 --- a/CA-Platform-Linker/Services/ClientService.cs +++ b/CA-Platform-Linker/Services/ClientService.cs @@ -90,10 +90,33 @@ namespace CA_Platform_Linker.Services if (result.status == "200") { - _ = Task.Run(() => + _ = Task.Run(async () => { + string tsData = ""; try { + // 1. 协同加盖时间戳 + var createTssRes = await CreateTssAsync(clientType, base64Data); + if (createTssRes.status != "200" || string.IsNullOrEmpty(createTssRes.data?.tsResp)) + { + string errMsg = $"加盖时间戳失败: {createTssRes.message ?? "tsResp为空"}"; + LogHelper.Log.Error(errMsg); + return; + } + tsData = createTssRes.data.tsResp; + LogHelper.Log.Info($"加盖时间戳成功,SignDataId: {result.data.signDataId}"); + + // 2. 时间戳验签 + var verifyTssRes = await VerifyTssAsync(clientType, base64Data, tsData); + if (verifyTssRes.status != "200" || verifyTssRes.data?.verifyRes != 1) + { + string errMsg = $"时间戳验签失败: {verifyTssRes.message ?? verifyTssRes.data?.verifyMsg ?? "未知错误"}"; + LogHelper.Log.Error(errMsg); + return; + } + LogHelper.Log.Info($"时间戳验签通过,SignDataId: {result.data.signDataId}"); + + // 3. 验签通过,写入数据库 var clientConfig = ConfigHelper.GetCossClientConfig(clientType); var (operCode, operName) = SqlHelper.GetOperatorInfo(userId); var signInfo = new SignInfo @@ -106,10 +129,10 @@ namespace CA_Platform_Linker.Services OperCode = operCode ?? "", OperName = operName ?? "", SignDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), - CertID = "", + CertID = result.data.msspId ?? "", OperCert = result.data.signCert, SignData = result.data.signResult, - TSData = "", + TSData = tsData, SignID = signID, OperType = clientType + "自动签名", SignWay = 1 @@ -120,7 +143,7 @@ namespace CA_Platform_Linker.Services } catch (Exception ex) { - LogHelper.Log.Error($"写入签名记录失败:{ex.Message}"); + LogHelper.Log.Error($"签名后处理流程异常:{ex.Message}"); } }); } @@ -156,5 +179,51 @@ namespace CA_Platform_Linker.Services }; } } + + /// + /// 协同加盖时间戳(参考Demo: createAndGetTssInfo) + /// + public async Task> CreateTssAsync(string clientType, string oriData) + { + try + { + Hashtable ht = CossHelper.BuildBaseHashtable(clientType); + ht.Add("oriData", oriData); + ht.Add("attachCert", "true"); + return await CossHelper.PostAsync("createAndGetTssInfo", ht, clientType); + } + catch (Exception ex) + { + LogHelper.Log.Error($"加盖时间戳失败: {ex.Message}"); + return new CossResponse + { + status = "500", + message = ex.Message + }; + } + } + + /// + /// 时间戳验签(参考Demo: verifyTS) + /// + public async Task> VerifyTssAsync(string clientType, string oriData, string tsResp) + { + try + { + Hashtable ht = CossHelper.BuildBaseHashtable(clientType); + ht.Add("oriData", oriData); + ht.Add("tsResp", tsResp); + return await CossHelper.PostAsync("verifyTS", ht, clientType); + } + catch (Exception ex) + { + LogHelper.Log.Error($"时间戳验签失败: {ex.Message}"); + return new CossResponse + { + status = "500", + message = ex.Message + }; + } + } } } \ No newline at end of file diff --git a/CA-Platform-Linker/Services/Interfaces/IClientService.cs b/CA-Platform-Linker/Services/Interfaces/IClientService.cs index 8f606cf..f460a15 100644 --- a/CA-Platform-Linker/Services/Interfaces/IClientService.cs +++ b/CA-Platform-Linker/Services/Interfaces/IClientService.cs @@ -12,5 +12,15 @@ namespace CA_Platform_Linker.Services.Interfaces Task> AddSignJobAsync(string clientType, string title, string data, string userId = "", string algo = "SM3withSM2", string expiryDate = "1400"); Task> AutoSignAsync(string clientType, string userId, string signToken, string data, string algo = "SM3withSM2", string expiryDate = "1400", int mzNum = 0, int zyNum = 0, string signID = "", int clientOper = 0); Task> GetSignResultAsync(string clientType, string signDataId); + + /// + /// 协同加盖时间戳(参考Demo: createAndGetTssInfo) + /// + Task> CreateTssAsync(string clientType, string oriData); + + /// + /// 时间戳验签(参考Demo: verifyTS) + /// + Task> VerifyTssAsync(string clientType, string oriData, string tsResp); } } \ No newline at end of file