PrivateKeyInfoFactory.cs 12 KB

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