PrivateKeyInfoFactory.cs 12 KB

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