CertificateService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http.Json;
  7. using System.Runtime;
  8. using System.Runtime.ConstrainedExecution;
  9. using System.Security.Cryptography;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. namespace EVCB_OCPP.WSServer.Service
  15. {
  16. public class CertificateService
  17. {
  18. public CertificateService(
  19. ServerMessageService messageService,
  20. IConfiguration configuration,
  21. ILogger<CertificateService> logger)
  22. {
  23. _serverUrl = configuration["CertificateServerUrl"];
  24. _cpon = configuration["ChargingPointOperator"];
  25. this.messageService = messageService;
  26. this.logger = logger;
  27. }
  28. private readonly string _serverUrl;
  29. private readonly string _cpon;
  30. private readonly ServerMessageService messageService;
  31. private readonly ILogger<CertificateService> logger;
  32. public async Task SignCertificate(string chargeBoxId, string csr)
  33. {
  34. HttpClient client = new HttpClient();
  35. client.BaseAddress = new Uri(_serverUrl);
  36. var signResult = await client.PostAsJsonAsync("cert/csr", new { csr = csr });
  37. if (!signResult.IsSuccessStatusCode)
  38. {
  39. logger.LogWarning("{chargeBoxId} csr failed {csr}", chargeBoxId, csr);
  40. return;
  41. }
  42. var crt = await signResult.Content.ReadAsStringAsync();
  43. await messageService.SendCertificateSignedRequest(chargeBoxId, crt);
  44. client.Dispose();
  45. }
  46. public Task<int> CheckClientCertificate(string chargeboxId, X509Certificate2 cert)
  47. {
  48. return CheckClientCertificate(chargeboxId, cert.ExportCertificatePem());
  49. }
  50. public async Task<int> CheckClientCertificate(string chargeboxId, string certString)
  51. {
  52. var subject = GetCertificateSubject(certString);
  53. logger.LogInformation("{chargeboxId} with cert subject {subject}", chargeboxId, subject);
  54. if (string.IsNullOrEmpty(subject))
  55. {
  56. return -1;
  57. }
  58. var crtInfo = SubjectToDictionary(subject);
  59. if (crtInfo == null ||
  60. //!crtInfo.ContainsKey("O") ||
  61. //crtInfo["O"] != _cpon ||
  62. !crtInfo.ContainsKey("CN") ||
  63. crtInfo["CN"] != chargeboxId)
  64. {
  65. return -1;
  66. }
  67. int certServerResult = await CheckClientCertificateRca(certString);
  68. logger.LogInformation("{chargeboxId} with cert authenticate {subject}", chargeboxId, certServerResult);
  69. return certServerResult;
  70. }
  71. public async Task<int> CheckClientCertificateRca(string certString)
  72. {
  73. using HttpClient client = new HttpClient();
  74. client.BaseAddress = new Uri(_serverUrl);
  75. var result = await client.PutAsJsonAsync("cert/crt", new { crt = certString });
  76. if (!result.IsSuccessStatusCode)
  77. {
  78. return -1;
  79. }
  80. var resultContentString = await result.Content.ReadAsStringAsync();
  81. return Convert.ToInt32(resultContentString);
  82. }
  83. public Dictionary<string, string> SubjectToDictionary(string subject)
  84. {
  85. try
  86. {
  87. MatchCollection regexResult = Regex.Matches(subject, "([a-zA-Z]*)=([a-zA-Z0-9]*)");
  88. if (regexResult.Count == 0)
  89. {
  90. return null;
  91. }
  92. return regexResult.Where(x => x.Success == true && x.Groups.Count == 3).ToDictionary(x => x.Groups[1].Value, x => x.Groups[2].Value);
  93. }
  94. catch (Exception e)
  95. {
  96. logger.LogCritical(e.Message);
  97. logger.LogCritical(e.StackTrace);
  98. return null;
  99. }
  100. }
  101. public string GetCertificateSubject(string crt)
  102. {
  103. try
  104. {
  105. byte[] bytes = Encoding.ASCII.GetBytes(crt);
  106. var clientCertificate = new X509Certificate2(bytes);
  107. return clientCertificate.Subject;
  108. }
  109. catch
  110. {
  111. return null;
  112. }
  113. }
  114. public string GetCertificateRequestSubject(string csrString)
  115. {
  116. try
  117. {
  118. var csr = CertificateRequest.LoadSigningRequestPem(csrString, HashAlgorithmName.SHA512);
  119. var test = csr.SubjectName.Name;
  120. return test;
  121. }
  122. catch
  123. {
  124. return null;
  125. }
  126. }
  127. }
  128. }