PrivateKeyInfoFactory.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  18. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
  19. {
  20. public sealed class PrivateKeyInfoFactory
  21. {
  22. private PrivateKeyInfoFactory()
  23. {
  24. }
  25. public static PrivateKeyInfo CreatePrivateKeyInfo(
  26. AsymmetricKeyParameter privateKey)
  27. {
  28. return CreatePrivateKeyInfo(privateKey, null);
  29. }
  30. /**
  31. * Create a PrivateKeyInfo representation of a private key with attributes.
  32. *
  33. * @param privateKey the key to be encoded into the info object.
  34. * @param attributes the set of attributes to be included.
  35. * @return the appropriate PrivateKeyInfo
  36. * @throws java.io.IOException on an error encoding the key
  37. */
  38. public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes)
  39. {
  40. if (privateKey == null)
  41. throw new ArgumentNullException("privateKey");
  42. if (!privateKey.IsPrivate)
  43. throw new ArgumentException("Public key passed - private key expected", "privateKey");
  44. if (privateKey is ElGamalPrivateKeyParameters)
  45. {
  46. ElGamalPrivateKeyParameters _key = (ElGamalPrivateKeyParameters)privateKey;
  47. ElGamalParameters egp = _key.Parameters;
  48. return new PrivateKeyInfo(
  49. new AlgorithmIdentifier(OiwObjectIdentifiers.ElGamalAlgorithm, new ElGamalParameter(egp.P, egp.G).ToAsn1Object()),
  50. new DerInteger(_key.X),
  51. attributes);
  52. }
  53. if (privateKey is DsaPrivateKeyParameters)
  54. {
  55. DsaPrivateKeyParameters _key = (DsaPrivateKeyParameters)privateKey;
  56. DsaParameters dp = _key.Parameters;
  57. return new PrivateKeyInfo(
  58. new AlgorithmIdentifier(X9ObjectIdentifiers.IdDsa, new DsaParameter(dp.P, dp.Q, dp.G).ToAsn1Object()),
  59. new DerInteger(_key.X),
  60. attributes);
  61. }
  62. if (privateKey is DHPrivateKeyParameters)
  63. {
  64. DHPrivateKeyParameters _key = (DHPrivateKeyParameters)privateKey;
  65. DHParameter p = new DHParameter(
  66. _key.Parameters.P, _key.Parameters.G, _key.Parameters.L);
  67. return new PrivateKeyInfo(
  68. new AlgorithmIdentifier(_key.AlgorithmOid, p.ToAsn1Object()),
  69. new DerInteger(_key.X),
  70. attributes);
  71. }
  72. if (privateKey is RsaKeyParameters)
  73. {
  74. AlgorithmIdentifier algID = new AlgorithmIdentifier(
  75. PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance);
  76. RsaPrivateKeyStructure keyStruct;
  77. if (privateKey is RsaPrivateCrtKeyParameters)
  78. {
  79. RsaPrivateCrtKeyParameters _key = (RsaPrivateCrtKeyParameters)privateKey;
  80. keyStruct = new RsaPrivateKeyStructure(
  81. _key.Modulus,
  82. _key.PublicExponent,
  83. _key.Exponent,
  84. _key.P,
  85. _key.Q,
  86. _key.DP,
  87. _key.DQ,
  88. _key.QInv);
  89. }
  90. else
  91. {
  92. RsaKeyParameters _key = (RsaKeyParameters) privateKey;
  93. keyStruct = new RsaPrivateKeyStructure(
  94. _key.Modulus,
  95. BigInteger.Zero,
  96. _key.Exponent,
  97. BigInteger.Zero,
  98. BigInteger.Zero,
  99. BigInteger.Zero,
  100. BigInteger.Zero,
  101. BigInteger.Zero);
  102. }
  103. return new PrivateKeyInfo(algID, keyStruct.ToAsn1Object(), attributes);
  104. }
  105. if (privateKey is ECPrivateKeyParameters)
  106. {
  107. ECPrivateKeyParameters priv = (ECPrivateKeyParameters)privateKey;
  108. DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(priv).Q.GetEncoded(false));
  109. ECDomainParameters dp = priv.Parameters;
  110. int orderBitLength = dp.N.BitLength;
  111. AlgorithmIdentifier algID;
  112. ECPrivateKeyStructure ec;
  113. if (priv.AlgorithmName == "ECGOST3410")
  114. {
  115. if (priv.PublicKeyParamSet == null)
  116. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
  117. Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
  118. priv.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet);
  119. algID = new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x2001, gostParams);
  120. // TODO Do we need to pass any parameters here?
  121. ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, null);
  122. }
  123. else
  124. {
  125. X962Parameters x962;
  126. if (priv.PublicKeyParamSet == null)
  127. {
  128. X9ECParameters ecP = new X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed());
  129. x962 = new X962Parameters(ecP);
  130. }
  131. else
  132. {
  133. x962 = new X962Parameters(priv.PublicKeyParamSet);
  134. }
  135. ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, x962);
  136. algID = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, x962);
  137. }
  138. return new PrivateKeyInfo(algID, ec, attributes);
  139. }
  140. if (privateKey is Gost3410PrivateKeyParameters)
  141. {
  142. Gost3410PrivateKeyParameters _key = (Gost3410PrivateKeyParameters)privateKey;
  143. if (_key.PublicKeyParamSet == null)
  144. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
  145. byte[] keyEnc = _key.X.ToByteArrayUnsigned();
  146. byte[] keyBytes = new byte[keyEnc.Length];
  147. for (int i = 0; i != keyBytes.Length; i++)
  148. {
  149. keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // must be little endian
  150. }
  151. Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
  152. _key.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet, null);
  153. AlgorithmIdentifier algID = new AlgorithmIdentifier(
  154. CryptoProObjectIdentifiers.GostR3410x94,
  155. algParams.ToAsn1Object());
  156. return new PrivateKeyInfo(algID, new DerOctetString(keyBytes), attributes);
  157. }
  158. if (privateKey is X448PrivateKeyParameters)
  159. {
  160. X448PrivateKeyParameters key = (X448PrivateKeyParameters)privateKey;
  161. return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448),
  162. new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
  163. }
  164. if (privateKey is X25519PrivateKeyParameters)
  165. {
  166. X25519PrivateKeyParameters key = (X25519PrivateKeyParameters)privateKey;
  167. return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519),
  168. new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
  169. }
  170. if (privateKey is Ed448PrivateKeyParameters)
  171. {
  172. Ed448PrivateKeyParameters key = (Ed448PrivateKeyParameters)privateKey;
  173. return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448),
  174. new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
  175. }
  176. if (privateKey is Ed25519PrivateKeyParameters)
  177. {
  178. Ed25519PrivateKeyParameters key = (Ed25519PrivateKeyParameters)privateKey;
  179. return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519),
  180. new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded());
  181. }
  182. throw new ArgumentException("Class provided is not convertible: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey));
  183. }
  184. public static PrivateKeyInfo CreatePrivateKeyInfo(
  185. char[] passPhrase,
  186. EncryptedPrivateKeyInfo encInfo)
  187. {
  188. return CreatePrivateKeyInfo(passPhrase, false, encInfo);
  189. }
  190. public static PrivateKeyInfo CreatePrivateKeyInfo(
  191. char[] passPhrase,
  192. bool wrongPkcs12Zero,
  193. EncryptedPrivateKeyInfo encInfo)
  194. {
  195. AlgorithmIdentifier algID = encInfo.EncryptionAlgorithm;
  196. IBufferedCipher cipher = PbeUtilities.CreateEngine(algID) as IBufferedCipher;
  197. if (cipher == null)
  198. throw new Exception("Unknown encryption algorithm: " + algID.Algorithm);
  199. ICipherParameters cipherParameters = PbeUtilities.GenerateCipherParameters(
  200. algID, passPhrase, wrongPkcs12Zero);
  201. cipher.Init(false, cipherParameters);
  202. byte[] keyBytes = cipher.DoFinal(encInfo.GetEncryptedData());
  203. return PrivateKeyInfo.GetInstance(keyBytes);
  204. }
  205. }
  206. }
  207. #pragma warning restore
  208. #endif