CertificateGenerator.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections;
  4. using System.Security.Cryptography;
  5. namespace Emby.Server.Core.Cryptography
  6. {
  7. public class CertificateGenerator
  8. {
  9. 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>";
  10. public static void CreateSelfSignCertificatePfx(
  11. string fileName,
  12. string hostname,
  13. string password,
  14. ILogger logger)
  15. {
  16. if (string.IsNullOrWhiteSpace(fileName))
  17. {
  18. throw new ArgumentNullException("fileName");
  19. }
  20. byte[] sn = Guid.NewGuid().ToByteArray();
  21. string subject = string.Format("CN={0}", hostname);
  22. string issuer = subject;
  23. DateTime notBefore = DateTime.Now.AddDays(-2);
  24. DateTime notAfter = DateTime.Now.AddYears(10);
  25. RSA issuerKey = RSA.Create();
  26. issuerKey.FromXmlString(MonoTestRootAgency);
  27. RSA subjectKey = RSA.Create();
  28. // serial number MUST be positive
  29. if ((sn[0] & 0x80) == 0x80)
  30. sn[0] -= 0x80;
  31. issuer = subject;
  32. issuerKey = subjectKey;
  33. X509CertificateBuilder cb = new X509CertificateBuilder(3);
  34. cb.SerialNumber = sn;
  35. cb.IssuerName = issuer;
  36. cb.NotBefore = notBefore;
  37. cb.NotAfter = notAfter;
  38. cb.SubjectName = subject;
  39. cb.SubjectPublicKey = subjectKey;
  40. // signature
  41. cb.Hash = "SHA256";
  42. byte[] rawcert = cb.Sign(issuerKey);
  43. PKCS12 p12 = new PKCS12();
  44. ArrayList list = new ArrayList();
  45. // we use a fixed array to avoid endianess issues
  46. // (in case some tools requires the ID to be 1).
  47. list.Add(new byte[4] { 1, 0, 0, 0 });
  48. Hashtable attributes = new Hashtable(1);
  49. attributes.Add(PKCS9.localKeyId, list);
  50. p12.AddCertificate(new X509Certificate(rawcert), attributes);
  51. p12.Password = password;
  52. p12.AddPkcs8ShroudedKeyBag(subjectKey, attributes);
  53. p12.SaveToFile(fileName);
  54. }
  55. }
  56. }