Pkcs8Generator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.OpenSsl
  16. {
  17. public class Pkcs8Generator
  18. : PemObjectGenerator
  19. {
  20. // FIXME See PbeUtilities static constructor
  21. // public static readonly string Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc.Id;
  22. // public static readonly string Aes192Cbc = NistObjectIdentifiers.IdAes192Cbc.Id;
  23. // public static readonly string Aes256Cbc = NistObjectIdentifiers.IdAes256Cbc.Id;
  24. //
  25. // public static readonly string Des3Cbc = PkcsObjectIdentifiers.DesEde3Cbc.Id;
  26. public static readonly string PbeSha1_RC4_128 = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4.Id;
  27. public static readonly string PbeSha1_RC4_40 = PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4.Id;
  28. public static readonly string PbeSha1_3DES = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc.Id;
  29. public static readonly string PbeSha1_2DES = PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc.Id;
  30. public static readonly string PbeSha1_RC2_128 = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc.Id;
  31. public static readonly string PbeSha1_RC2_40 = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc.Id;
  32. private char[] password;
  33. private string algorithm;
  34. private int iterationCount;
  35. private AsymmetricKeyParameter privKey;
  36. private SecureRandom random;
  37. /**
  38. * Constructor for an unencrypted private key PEM object.
  39. *
  40. * @param key private key to be encoded.
  41. */
  42. public Pkcs8Generator(AsymmetricKeyParameter privKey)
  43. {
  44. this.privKey = privKey;
  45. }
  46. /**
  47. * Constructor for an encrypted private key PEM object.
  48. *
  49. * @param key private key to be encoded
  50. * @param algorithm encryption algorithm to use
  51. * @param provider provider to use
  52. * @throws NoSuchAlgorithmException if algorithm/mode cannot be found
  53. */
  54. public Pkcs8Generator(AsymmetricKeyParameter privKey, string algorithm)
  55. {
  56. // TODO Check privKey.IsPrivate
  57. this.privKey = privKey;
  58. this.algorithm = algorithm;
  59. this.iterationCount = 2048;
  60. }
  61. public SecureRandom SecureRandom
  62. {
  63. set { this.random = value; }
  64. }
  65. public char[] Password
  66. {
  67. set { this.password = value; }
  68. }
  69. public int IterationCount
  70. {
  71. set { this.iterationCount = value; }
  72. }
  73. public PemObject Generate()
  74. {
  75. if (algorithm == null)
  76. {
  77. PrivateKeyInfo pki = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privKey);
  78. return new PemObject("PRIVATE KEY", pki.GetEncoded());
  79. }
  80. // TODO Theoretically, the amount of salt needed depends on the algorithm
  81. byte[] salt = new byte[20];
  82. if (random == null)
  83. {
  84. random = new SecureRandom();
  85. }
  86. random.NextBytes(salt);
  87. try
  88. {
  89. EncryptedPrivateKeyInfo epki = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
  90. algorithm, password, salt, iterationCount, privKey);
  91. return new PemObject("ENCRYPTED PRIVATE KEY", epki.GetEncoded());
  92. }
  93. catch (Exception e)
  94. {
  95. throw new PemGenerationException("Couldn't encrypt private key", e);
  96. }
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif