CertificateGenerator.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.Logging;
  10. using Mono.Security.X509;
  11. namespace MediaBrowser.Server.Mono.Networking
  12. {
  13. internal class CertificateGenerator
  14. {
  15. private const string MonoTestRootAgency = "<RSAKeyValue><Modulus>v/4nALBxCE+9JgEC0LnDUvKh6e96PwTpN4Rj+vWnqKT7IAp1iK/JjuqvAg6DQ2vTfv0dTlqffmHH51OyioprcT5nzxcSTsZb/9jcHScG0s3/FRIWnXeLk/fgm7mSYhjUaHNI0m1/NTTktipicjKxo71hGIg9qucCWnDum+Krh/k=</Modulus><Exponent>AQAB</Exponent><P>9jbKxMXEruW2CfZrzhxtull4O8P47+mNsEL+9gf9QsRO1jJ77C+jmzfU6zbzjf8+ViK+q62tCMdC1ZzulwdpXQ==</P><Q>x5+p198l1PkK0Ga2mRh0SIYSykENpY2aLXoyZD/iUpKYAvATm0/wvKNrE4dKJyPCA+y3hfTdgVag+SP9avvDTQ==</Q><DP>ISSjCvXsUfbOGG05eddN1gXxL2pj+jegQRfjpk7RAsnWKvNExzhqd5x+ZuNQyc6QH5wxun54inP4RTUI0P/IaQ==</DP><DQ>R815VQmR3RIbPqzDXzv5j6CSH6fYlcTiQRtkBsUnzhWmkd/y3XmamO+a8zJFjOCCx9CcjpVuGziivBqi65lVPQ==</DQ><InverseQ>iYiu0KwMWI/dyqN3RJYUzuuLj02/oTD1pYpwo2rvNCXU1Q5VscOeu2DpNg1gWqI+1RrRCsEoaTNzXB1xtKNlSw==</InverseQ><D>nIfh1LYF8fjRBgMdAH/zt9UKHWiaCnc+jXzq5tkR8HVSKTVdzitD8bl1JgAfFQD8VjSXiCJqluexy/B5SGrCXQ49c78NIQj0hD+J13Y8/E0fUbW1QYbhj6Ff7oHyhaYe1WOQfkp2t/h+llHOdt1HRf7bt7dUknYp7m8bQKGxoYE=</D></RSAKeyValue>";
  16. internal static void CreateSelfSignCertificatePfx(
  17. string fileName,
  18. string hostname,
  19. ILogger logger)
  20. {
  21. try
  22. {
  23. if (string.IsNullOrWhiteSpace(fileName))
  24. {
  25. logger.Info("No certificate filename specified.");
  26. return;
  27. }
  28. if (File.Exists(fileName))
  29. {
  30. logger.Info("Certificate file already exists. To regenerate, delete {0}", fileName);
  31. return;
  32. }
  33. byte[] sn = Guid.NewGuid().ToByteArray();
  34. string subject = string.Format("CN={0}", hostname);
  35. string issuer = subject;
  36. DateTime notBefore = DateTime.Now.AddDays(-2);
  37. DateTime notAfter = DateTime.Now.AddYears(10);
  38. RSA issuerKey = RSA.Create();
  39. issuerKey.FromXmlString(MonoTestRootAgency);
  40. RSA subjectKey = RSA.Create();
  41. // serial number MUST be positive
  42. if ((sn[0] & 0x80) == 0x80)
  43. sn[0] -= 0x80;
  44. issuer = subject;
  45. issuerKey = subjectKey;
  46. X509CertificateBuilder cb = new X509CertificateBuilder(3);
  47. cb.SerialNumber = sn;
  48. cb.IssuerName = issuer;
  49. cb.NotBefore = notBefore;
  50. cb.NotAfter = notAfter;
  51. cb.SubjectName = subject;
  52. cb.SubjectPublicKey = subjectKey;
  53. // signature
  54. cb.Hash = "SHA256";
  55. byte[] rawcert = cb.Sign(issuerKey);
  56. PKCS12 p12 = new PKCS12();
  57. ArrayList list = new ArrayList();
  58. // we use a fixed array to avoid endianess issues
  59. // (in case some tools requires the ID to be 1).
  60. list.Add(new byte[4] {1, 0, 0, 0});
  61. Hashtable attributes = new Hashtable(1);
  62. attributes.Add(PKCS9.localKeyId, list);
  63. p12.AddCertificate(new X509Certificate(rawcert), attributes);
  64. p12.AddPkcs8ShroudedKeyBag(subjectKey, attributes);
  65. p12.SaveToFile(fileName);
  66. }
  67. catch (Exception e)
  68. {
  69. logger.ErrorException("Error generating self signed ssl certificate: {0}", e, fileName);
  70. }
  71. }
  72. }
  73. }