SecurityProfileHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using EVCB_OCPP.Packet.Features;
  2. using EVCB_OCPP.Packet.Messages;
  3. using OCPPServer.Protocol;
  4. using System;
  5. using Microsoft.Extensions.Logging;
  6. using EVCB_OCPP.WSServer.Service.WsService;
  7. using EVCB_OCPP.Packet.Messages.Security;
  8. using Microsoft.EntityFrameworkCore;
  9. namespace EVCB_OCPP.WSServer.Message
  10. {
  11. internal partial class ProfileHandler
  12. {
  13. internal async Task<MessageResult> ExecuteSecurityRequest(Actions action, WsClientData session, IRequest request)
  14. {
  15. MessageResult result = new MessageResult() { Success = false };
  16. try
  17. {
  18. switch (action)
  19. {
  20. case Actions.SignCertificate:
  21. {
  22. SignCertificateRequest _request = request as SignCertificateRequest;
  23. SignCertificateConfirmation confirm = new();
  24. if (string.IsNullOrEmpty(_request.csr))
  25. {
  26. result.Success = false;
  27. return result;
  28. }
  29. bool isCsrValid = CheckCsr(session.ChargeBoxId, _request.csr);
  30. if (!isCsrValid)
  31. {
  32. confirm.status = Packet.Messages.SubTypes.GenericStatusEnumType.Rejected;
  33. result.Message = confirm;
  34. result.Success = false;
  35. return result;
  36. }
  37. _ = certService.SignCertificate(session.ChargeBoxId, _request.csr);
  38. confirm.status = Packet.Messages.SubTypes.GenericStatusEnumType.Accepted;
  39. result.Message = confirm;
  40. result.Success = true;
  41. return result;
  42. }
  43. case Actions.SecurityEventNotification:
  44. {
  45. SecurityEventNotificationRequest _request = request as SecurityEventNotificationRequest;
  46. SecurityEventNotificationConfirmation confirm = new();
  47. logger.LogInformation("{chargeBoxId} security notification {sects} {sectype} {secmsg}", session.ChargeBoxId, _request.timestamp, _request.type, _request.techInfo);
  48. result.Message = confirm;
  49. result.Success = true;
  50. return result;
  51. }
  52. case Actions.LogStatusNotification:
  53. {
  54. LogStatusNotificationRequest _request = request as LogStatusNotificationRequest;
  55. LogStatusNotificationConfirmation confirm = new();
  56. if (_request.status != Packet.Messages.SubTypes.UploadLogStatusEnumType.Idle)
  57. {
  58. using (var db = await maindbContextFactory.CreateDbContextAsync())
  59. {
  60. var item = await db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action == "GetLog" && x.RequestType == 1)
  61. .OrderByDescending(x => x.CreatedOn).FirstOrDefaultAsync();
  62. if (item != null)
  63. {
  64. item.EVSE_Status = (int)_request.status;
  65. item.FinishedOn = DateTime.UtcNow;
  66. }
  67. await db.SaveChangesAsync();
  68. }
  69. }
  70. result.Message = confirm;
  71. result.Success = true;
  72. return result;
  73. }
  74. default:
  75. {
  76. logger.LogWarning(string.Format("Not Implement {0} Logic(ExecuteCoreRequest)", request.GetType().ToString().Replace("OCPPPackage.Messages.Core.", "")));
  77. }
  78. break;
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. logger.LogCritical("chargeBoxId:{0} {1}", session.ChargeBoxId, action);
  84. logger.LogCritical("Data {0}", request.ToString());
  85. logger.LogCritical("Error {0}", ex.ToString());
  86. result.Exception = ex;
  87. }
  88. return result;
  89. }
  90. private bool CheckCsr(string chargeBoxId, string csrString)
  91. {
  92. string subject = certService.GetCertificateRequestSubject(csrString);
  93. logger.LogInformation("{chargeBoxId} send scr {subject}", chargeBoxId, subject);
  94. if (subject == null)
  95. {
  96. return false;
  97. }
  98. Dictionary<string,string> csrInfo = certService.SubjectToDictionary(subject);
  99. if (csrInfo == null ||
  100. !csrInfo.ContainsKey("CN") ||
  101. csrInfo["CN"] != chargeBoxId)
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. internal async Task<MessageResult> ExecuteSecurityConfirm(Actions action, WsClientData session, IConfirmation confirm, string requestId)
  108. {
  109. MessageResult result = new MessageResult() { Success = false };
  110. switch (action)
  111. {
  112. case Actions.ExtendedTriggerMessage:
  113. {
  114. ExtendedTriggerMessageConfirmation _confirm = confirm as ExtendedTriggerMessageConfirmation;
  115. //ExtendedTriggerMessageRequest _request = _confirm.GetRequest() as ExtendedTriggerMessageRequest;
  116. using (var db = await maindbContextFactory.CreateDbContextAsync())
  117. {
  118. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  119. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  120. if (operation != null)
  121. {
  122. operation.FinishedOn = DateTime.UtcNow;
  123. operation.Status = 1;//電樁有回覆
  124. operation.EVSE_Status = (int)_confirm.status;//OK
  125. operation.EVSE_Value = _confirm.status.ToString();
  126. await db.SaveChangesAsync();
  127. }
  128. }
  129. }
  130. break;
  131. case Actions.CertificateSigned:
  132. {
  133. CertificateSignedConfirmation _confirm = confirm as CertificateSignedConfirmation;
  134. using (var db = await maindbContextFactory.CreateDbContextAsync())
  135. {
  136. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  137. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  138. if (operation != null)
  139. {
  140. operation.FinishedOn = DateTime.UtcNow;
  141. operation.Status = 1;//電樁有回覆
  142. operation.EVSE_Status = (int)_confirm.status;//OK
  143. operation.EVSE_Value = _confirm.status.ToString();
  144. await db.SaveChangesAsync();
  145. }
  146. }
  147. }
  148. break;
  149. case Actions.GetInstalledCertificateIds:
  150. {
  151. GetInstalledCertificateIdsConfirmation _confirm = confirm as GetInstalledCertificateIdsConfirmation;
  152. using (var db = await maindbContextFactory.CreateDbContextAsync())
  153. {
  154. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  155. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  156. if (operation != null)
  157. {
  158. operation.FinishedOn = DateTime.UtcNow;
  159. operation.Status = 1;//電樁有回覆
  160. operation.EVSE_Status = (int)_confirm.status;//OK
  161. operation.EVSE_Value = _confirm.status.ToString();
  162. await db.SaveChangesAsync();
  163. }
  164. }
  165. }
  166. break;
  167. case Actions.DeleteCertificate:
  168. {
  169. DeleteCertificateConfirmation _confirm = confirm as DeleteCertificateConfirmation;
  170. using (var db = await maindbContextFactory.CreateDbContextAsync())
  171. {
  172. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  173. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  174. if (operation != null)
  175. {
  176. operation.FinishedOn = DateTime.UtcNow;
  177. operation.Status = 1;//電樁有回覆
  178. operation.EVSE_Status = (int)_confirm.status;//OK
  179. operation.EVSE_Value = _confirm.status.ToString();
  180. await db.SaveChangesAsync();
  181. }
  182. }
  183. }
  184. break;
  185. case Actions.InstallCertificate:
  186. {
  187. InstallCertificateConfirmation _confirm = confirm as InstallCertificateConfirmation;
  188. using (var db = await maindbContextFactory.CreateDbContextAsync())
  189. {
  190. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  191. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  192. if (operation != null)
  193. {
  194. operation.FinishedOn = DateTime.UtcNow;
  195. operation.Status = 1;//電樁有回覆
  196. operation.EVSE_Status = (int)_confirm.status;//OK
  197. operation.EVSE_Value = _confirm.status.ToString();
  198. await db.SaveChangesAsync();
  199. }
  200. }
  201. }
  202. break;
  203. case Actions.GetLog:
  204. {
  205. GetLogConfirmation _confirm = confirm as GetLogConfirmation;
  206. using (var db = await maindbContextFactory.CreateDbContextAsync())
  207. {
  208. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  209. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  210. if (operation != null)
  211. {
  212. operation.FinishedOn = DateTime.UtcNow;
  213. operation.Status = 1;//電樁有回覆
  214. operation.EVSE_Status = (int)_confirm.status;//OK
  215. operation.EVSE_Value = _confirm.status.ToString();
  216. await db.SaveChangesAsync();
  217. }
  218. }
  219. }
  220. break;
  221. default:
  222. {
  223. logger.LogWarning(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  224. }
  225. break;
  226. }
  227. return result;
  228. }
  229. internal MessageResult ReceivedSecurityError(Actions action, string errorMsg, ClientData session, string requestId)
  230. {
  231. MessageResult result = new MessageResult() { Success = true };
  232. switch (action)
  233. {
  234. default:
  235. {
  236. logger.LogWarning(string.Format("Not Implement {0} Logic", action));
  237. }
  238. break;
  239. }
  240. return result;
  241. }
  242. }
  243. }