SecurityProfileHandler.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. case Actions.SignedFirmwareStatusNotification:
  75. {
  76. SignedFirmwareStatusNotificationRequest _request = request as SignedFirmwareStatusNotificationRequest;
  77. SignedFirmwareStatusNotificationConfirmation confirm = new();
  78. if (_request.status != Packet.Messages.SubTypes.FirmwareStatusEnumType.Idle)
  79. {
  80. using (var db = await maindbContextFactory.CreateDbContextAsync())
  81. {
  82. var item = await db.MachineOperateRecord.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.Action == "SignedUpdateFirmware" && x.RequestType == 1)
  83. .OrderByDescending(x => x.CreatedOn).FirstOrDefaultAsync();
  84. if (item != null)
  85. {
  86. item.EVSE_Status = (int)_request.status;
  87. item.FinishedOn = DateTime.UtcNow;
  88. }
  89. await db.SaveChangesAsync();
  90. }
  91. }
  92. result.Message = confirm;
  93. result.Success = true;
  94. return result;
  95. }
  96. default:
  97. {
  98. logger.LogWarning(string.Format("Not Implement {0} Logic(ExecuteCoreRequest)", request.GetType().ToString().Replace("OCPPPackage.Messages.Core.", "")));
  99. }
  100. break;
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. logger.LogCritical("chargeBoxId:{0} {1}", session.ChargeBoxId, action);
  106. logger.LogCritical("Data {0}", request.ToString());
  107. logger.LogCritical("Error {0}", ex.ToString());
  108. result.Exception = ex;
  109. }
  110. return result;
  111. }
  112. private bool CheckCsr(string chargeBoxId, string csrString)
  113. {
  114. string subject = certService.GetCertificateRequestSubject(csrString);
  115. logger.LogInformation("{chargeBoxId} send scr {subject}", chargeBoxId, subject);
  116. if (subject == null)
  117. {
  118. return false;
  119. }
  120. Dictionary<string,string> csrInfo = certService.SubjectToDictionary(subject);
  121. if (csrInfo == null ||
  122. !csrInfo.ContainsKey("CN") ||
  123. csrInfo["CN"] != chargeBoxId)
  124. {
  125. return false;
  126. }
  127. return true;
  128. }
  129. internal async Task<MessageResult> ExecuteSecurityConfirm(Actions action, WsClientData session, IConfirmation confirm, string requestId)
  130. {
  131. MessageResult result = new MessageResult() { Success = false };
  132. switch (action)
  133. {
  134. case Actions.ExtendedTriggerMessage:
  135. {
  136. ExtendedTriggerMessageConfirmation _confirm = confirm as ExtendedTriggerMessageConfirmation;
  137. //ExtendedTriggerMessageRequest _request = _confirm.GetRequest() as ExtendedTriggerMessageRequest;
  138. using (var db = await maindbContextFactory.CreateDbContextAsync())
  139. {
  140. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  141. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  142. if (operation != null)
  143. {
  144. operation.FinishedOn = DateTime.UtcNow;
  145. operation.Status = 1;//電樁有回覆
  146. operation.EVSE_Status = (int)_confirm.status;//OK
  147. operation.EVSE_Value = _confirm.status.ToString();
  148. await db.SaveChangesAsync();
  149. }
  150. }
  151. }
  152. break;
  153. case Actions.CertificateSigned:
  154. {
  155. CertificateSignedConfirmation _confirm = confirm as CertificateSignedConfirmation;
  156. using (var db = await maindbContextFactory.CreateDbContextAsync())
  157. {
  158. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  159. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  160. if (operation != null)
  161. {
  162. operation.FinishedOn = DateTime.UtcNow;
  163. operation.Status = 1;//電樁有回覆
  164. operation.EVSE_Status = (int)_confirm.status;//OK
  165. operation.EVSE_Value = _confirm.status.ToString();
  166. await db.SaveChangesAsync();
  167. }
  168. }
  169. }
  170. break;
  171. case Actions.GetInstalledCertificateIds:
  172. {
  173. GetInstalledCertificateIdsConfirmation _confirm = confirm as GetInstalledCertificateIdsConfirmation;
  174. using (var db = await maindbContextFactory.CreateDbContextAsync())
  175. {
  176. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  177. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  178. if (operation != null)
  179. {
  180. operation.FinishedOn = DateTime.UtcNow;
  181. operation.Status = 1;//電樁有回覆
  182. operation.EVSE_Status = (int)_confirm.status;//OK
  183. operation.EVSE_Value = _confirm.status.ToString();
  184. await db.SaveChangesAsync();
  185. }
  186. }
  187. }
  188. break;
  189. case Actions.DeleteCertificate:
  190. {
  191. DeleteCertificateConfirmation _confirm = confirm as DeleteCertificateConfirmation;
  192. using (var db = await maindbContextFactory.CreateDbContextAsync())
  193. {
  194. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  195. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  196. if (operation != null)
  197. {
  198. operation.FinishedOn = DateTime.UtcNow;
  199. operation.Status = 1;//電樁有回覆
  200. operation.EVSE_Status = (int)_confirm.status;//OK
  201. operation.EVSE_Value = _confirm.status.ToString();
  202. await db.SaveChangesAsync();
  203. }
  204. }
  205. }
  206. break;
  207. case Actions.InstallCertificate:
  208. {
  209. InstallCertificateConfirmation _confirm = confirm as InstallCertificateConfirmation;
  210. using (var db = await maindbContextFactory.CreateDbContextAsync())
  211. {
  212. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  213. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  214. if (operation != null)
  215. {
  216. operation.FinishedOn = DateTime.UtcNow;
  217. operation.Status = 1;//電樁有回覆
  218. operation.EVSE_Status = (int)_confirm.status;//OK
  219. operation.EVSE_Value = _confirm.status.ToString();
  220. await db.SaveChangesAsync();
  221. }
  222. }
  223. }
  224. break;
  225. case Actions.GetLog:
  226. {
  227. GetLogConfirmation _confirm = confirm as GetLogConfirmation;
  228. using (var db = await maindbContextFactory.CreateDbContextAsync())
  229. {
  230. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  231. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  232. if (operation != null)
  233. {
  234. operation.FinishedOn = DateTime.UtcNow;
  235. operation.Status = 1;//電樁有回覆
  236. operation.EVSE_Status = (int)_confirm.status;//OK
  237. operation.EVSE_Value = _confirm.status.ToString();
  238. await db.SaveChangesAsync();
  239. }
  240. }
  241. }
  242. break;
  243. case Actions.SignedUpdateFirmware:
  244. {
  245. SignedUpdateFirmwareConfirmation _confirm = confirm as SignedUpdateFirmwareConfirmation;
  246. using (var db = await maindbContextFactory.CreateDbContextAsync())
  247. {
  248. var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
  249. x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
  250. if (operation != null)
  251. {
  252. operation.FinishedOn = DateTime.UtcNow;
  253. operation.Status = 1;//電樁有回覆
  254. operation.EVSE_Status = (int)_confirm.status;//OK
  255. operation.EVSE_Value = _confirm.status.ToString();
  256. await db.SaveChangesAsync();
  257. }
  258. }
  259. }
  260. break;
  261. default:
  262. {
  263. logger.LogWarning(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
  264. }
  265. break;
  266. }
  267. return result;
  268. }
  269. internal MessageResult ReceivedSecurityError(Actions action, string errorMsg, ClientData session, string requestId)
  270. {
  271. MessageResult result = new MessageResult() { Success = true };
  272. switch (action)
  273. {
  274. default:
  275. {
  276. logger.LogWarning(string.Format("Not Implement {0} Logic", action));
  277. }
  278. break;
  279. }
  280. return result;
  281. }
  282. }
  283. }