SslConfigLoader.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.IO;
  2. using System.Security.Authentication;
  3. using UnityEngine;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public class SslConfigLoader
  7. {
  8. internal struct Cert
  9. {
  10. public string path;
  11. public string password;
  12. }
  13. public static SslConfig Load(bool sslEnabled, string sslCertJson, SslProtocols sslProtocols)
  14. {
  15. // don't need to load anything if ssl is not enabled
  16. if (!sslEnabled)
  17. return default;
  18. string certJsonPath = sslCertJson;
  19. Cert cert = LoadCertJson(certJsonPath);
  20. return new SslConfig(
  21. enabled: sslEnabled,
  22. sslProtocols: sslProtocols,
  23. certPath: cert.path,
  24. certPassword: cert.password
  25. );
  26. }
  27. internal static Cert LoadCertJson(string certJsonPath)
  28. {
  29. string json = File.ReadAllText(certJsonPath);
  30. Cert cert = JsonUtility.FromJson<Cert>(json);
  31. if (string.IsNullOrWhiteSpace(cert.path))
  32. {
  33. throw new InvalidDataException("Cert Json didn't not contain \"path\"");
  34. }
  35. // don't use IsNullOrWhiteSpace here because whitespace could be a valid password for a cert
  36. if (string.IsNullOrEmpty(cert.password))
  37. {
  38. // password can be empty
  39. cert.password = string.Empty;
  40. }
  41. return cert;
  42. }
  43. }
  44. }