PrivateKeyFactory.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cryptlib;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.CryptoPro;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.EdEC;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Gnu;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Sec;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  16. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  17. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  18. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  19. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  20. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Pkcs;
  21. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  22. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Security
  23. {
  24. public static class PrivateKeyFactory
  25. {
  26. public static AsymmetricKeyParameter CreateKey(
  27. byte[] privateKeyInfoData)
  28. {
  29. return CreateKey(
  30. PrivateKeyInfo.GetInstance(
  31. Asn1Object.FromByteArray(privateKeyInfoData)));
  32. }
  33. public static AsymmetricKeyParameter CreateKey(
  34. Stream inStr)
  35. {
  36. return CreateKey(
  37. PrivateKeyInfo.GetInstance(
  38. Asn1Object.FromStream(inStr)));
  39. }
  40. public static AsymmetricKeyParameter CreateKey(
  41. PrivateKeyInfo keyInfo)
  42. {
  43. AlgorithmIdentifier algID = keyInfo.PrivateKeyAlgorithm;
  44. DerObjectIdentifier algOid = algID.Algorithm;
  45. // TODO See RSAUtil.isRsaOid in Java build
  46. if (algOid.Equals(PkcsObjectIdentifiers.RsaEncryption)
  47. || algOid.Equals(X509ObjectIdentifiers.IdEARsa)
  48. || algOid.Equals(PkcsObjectIdentifiers.IdRsassaPss)
  49. || algOid.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
  50. {
  51. RsaPrivateKeyStructure keyStructure = RsaPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey());
  52. return new RsaPrivateCrtKeyParameters(
  53. keyStructure.Modulus,
  54. keyStructure.PublicExponent,
  55. keyStructure.PrivateExponent,
  56. keyStructure.Prime1,
  57. keyStructure.Prime2,
  58. keyStructure.Exponent1,
  59. keyStructure.Exponent2,
  60. keyStructure.Coefficient);
  61. }
  62. // TODO?
  63. // else if (algOid.Equals(X9ObjectIdentifiers.DHPublicNumber))
  64. else if (algOid.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
  65. {
  66. DHParameter para = new DHParameter(
  67. Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
  68. DerInteger derX = (DerInteger)keyInfo.ParsePrivateKey();
  69. BigInteger lVal = para.L;
  70. int l = lVal == null ? 0 : lVal.IntValue;
  71. DHParameters dhParams = new DHParameters(para.P, para.G, null, l);
  72. return new DHPrivateKeyParameters(derX.Value, dhParams, algOid);
  73. }
  74. else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
  75. {
  76. ElGamalParameter para = new ElGamalParameter(
  77. Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
  78. DerInteger derX = (DerInteger)keyInfo.ParsePrivateKey();
  79. return new ElGamalPrivateKeyParameters(
  80. derX.Value,
  81. new ElGamalParameters(para.P, para.G));
  82. }
  83. else if (algOid.Equals(X9ObjectIdentifiers.IdDsa))
  84. {
  85. DerInteger derX = (DerInteger)keyInfo.ParsePrivateKey();
  86. Asn1Encodable ae = algID.Parameters;
  87. DsaParameters parameters = null;
  88. if (ae != null)
  89. {
  90. DsaParameter para = DsaParameter.GetInstance(ae.ToAsn1Object());
  91. parameters = new DsaParameters(para.P, para.Q, para.G);
  92. }
  93. return new DsaPrivateKeyParameters(derX.Value, parameters);
  94. }
  95. else if (algOid.Equals(X9ObjectIdentifiers.IdECPublicKey))
  96. {
  97. X962Parameters para = X962Parameters.GetInstance(algID.Parameters.ToAsn1Object());
  98. X9ECParameters x9;
  99. if (para.IsNamedCurve)
  100. {
  101. x9 = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)para.Parameters);
  102. }
  103. else
  104. {
  105. x9 = new X9ECParameters((Asn1Sequence)para.Parameters);
  106. }
  107. ECPrivateKeyStructure ec = ECPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey());
  108. BigInteger d = ec.GetKey();
  109. if (para.IsNamedCurve)
  110. {
  111. return new ECPrivateKeyParameters("EC", d, (DerObjectIdentifier)para.Parameters);
  112. }
  113. ECDomainParameters dParams = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
  114. return new ECPrivateKeyParameters(d, dParams);
  115. }
  116. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001) ||
  117. algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) ||
  118. algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256))
  119. {
  120. Asn1Object p = algID.Parameters.ToAsn1Object();
  121. Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(p);
  122. ECGost3410Parameters ecSpec;
  123. BigInteger d;
  124. if (p is Asn1Sequence seq && (seq.Count == 2 || seq.Count == 3))
  125. {
  126. X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);
  127. if (ecP == null)
  128. throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key");
  129. ecSpec = new ECGost3410Parameters(
  130. new ECNamedDomainParameters(gostParams.PublicKeyParamSet, ecP),
  131. gostParams.PublicKeyParamSet,
  132. gostParams.DigestParamSet,
  133. gostParams.EncryptionParamSet);
  134. Asn1OctetString privEnc = keyInfo.PrivateKeyData;
  135. if (privEnc.GetOctets().Length == 32 || privEnc.GetOctets().Length == 64)
  136. {
  137. d = new BigInteger(1, Arrays.Reverse(privEnc.GetOctets()));
  138. }
  139. else
  140. {
  141. Asn1Object privKey = keyInfo.ParsePrivateKey();
  142. if (privKey is DerInteger derInteger)
  143. {
  144. d = derInteger.PositiveValue;
  145. }
  146. else
  147. {
  148. byte[] dVal = Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets());
  149. d = new BigInteger(1, dVal);
  150. }
  151. }
  152. }
  153. else
  154. {
  155. X962Parameters x962Parameters = X962Parameters.GetInstance(p);
  156. if (x962Parameters.IsNamedCurve)
  157. {
  158. DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(x962Parameters.Parameters);
  159. X9ECParameters ecP = ECNamedCurveTable.GetByOid(oid);
  160. if (ecP == null)
  161. throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key");
  162. ecSpec = new ECGost3410Parameters(
  163. new ECNamedDomainParameters(oid, ecP),
  164. gostParams.PublicKeyParamSet,
  165. gostParams.DigestParamSet,
  166. gostParams.EncryptionParamSet);
  167. }
  168. else if (x962Parameters.IsImplicitlyCA)
  169. {
  170. ecSpec = null;
  171. }
  172. else
  173. {
  174. X9ECParameters ecP = X9ECParameters.GetInstance(x962Parameters.Parameters);
  175. ecSpec = new ECGost3410Parameters(
  176. new ECNamedDomainParameters(algOid, ecP),
  177. gostParams.PublicKeyParamSet,
  178. gostParams.DigestParamSet,
  179. gostParams.EncryptionParamSet);
  180. }
  181. Asn1Object privKey = keyInfo.ParsePrivateKey();
  182. if (privKey is DerInteger derD)
  183. {
  184. d = derD.Value;
  185. }
  186. else
  187. {
  188. ECPrivateKeyStructure ec = ECPrivateKeyStructure.GetInstance(privKey);
  189. d = ec.GetKey();
  190. }
  191. }
  192. return new ECPrivateKeyParameters(
  193. d,
  194. new ECGost3410Parameters(
  195. ecSpec,
  196. gostParams.PublicKeyParamSet,
  197. gostParams.DigestParamSet,
  198. gostParams.EncryptionParamSet));
  199. }
  200. else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
  201. {
  202. Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
  203. Asn1Object privKey = keyInfo.ParsePrivateKey();
  204. BigInteger x;
  205. if (privKey is DerInteger)
  206. {
  207. x = DerInteger.GetInstance(privKey).PositiveValue;
  208. }
  209. else
  210. {
  211. x = new BigInteger(1, Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets()));
  212. }
  213. return new Gost3410PrivateKeyParameters(x, gostParams.PublicKeyParamSet);
  214. }
  215. else if (algOid.Equals(EdECObjectIdentifiers.id_X25519)
  216. || algOid.Equals(CryptlibObjectIdentifiers.curvey25519))
  217. {
  218. return new X25519PrivateKeyParameters(GetRawKey(keyInfo));
  219. }
  220. else if (algOid.Equals(EdECObjectIdentifiers.id_X448))
  221. {
  222. return new X448PrivateKeyParameters(GetRawKey(keyInfo));
  223. }
  224. else if (algOid.Equals(EdECObjectIdentifiers.id_Ed25519)
  225. || algOid.Equals(GnuObjectIdentifiers.Ed25519))
  226. {
  227. return new Ed25519PrivateKeyParameters(GetRawKey(keyInfo));
  228. }
  229. else if (algOid.Equals(EdECObjectIdentifiers.id_Ed448))
  230. {
  231. return new Ed448PrivateKeyParameters(GetRawKey(keyInfo));
  232. }
  233. else if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256)
  234. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)
  235. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256)
  236. || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512))
  237. {
  238. Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(
  239. keyInfo.PrivateKeyAlgorithm.Parameters);
  240. ECGost3410Parameters ecSpec;
  241. BigInteger d;
  242. Asn1Object p = keyInfo.PrivateKeyAlgorithm.Parameters.ToAsn1Object();
  243. if (p is Asn1Sequence && (Asn1Sequence.GetInstance(p).Count == 2 || Asn1Sequence.GetInstance(p).Count == 3))
  244. {
  245. X9ECParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);
  246. ecSpec = new ECGost3410Parameters(
  247. new ECNamedDomainParameters(
  248. gostParams.PublicKeyParamSet, ecP),
  249. gostParams.PublicKeyParamSet,
  250. gostParams.DigestParamSet,
  251. gostParams.EncryptionParamSet);
  252. Asn1OctetString privEnc = keyInfo.PrivateKeyData;
  253. if (privEnc.GetOctets().Length == 32 || privEnc.GetOctets().Length == 64)
  254. {
  255. byte[] dVal = Arrays.Reverse(privEnc.GetOctets());
  256. d = new BigInteger(1, dVal);
  257. }
  258. else
  259. {
  260. Asn1Encodable privKey = keyInfo.ParsePrivateKey();
  261. if (privKey is DerInteger)
  262. {
  263. d = DerInteger.GetInstance(privKey).PositiveValue;
  264. }
  265. else
  266. {
  267. byte[] dVal = Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets());
  268. d = new BigInteger(1, dVal);
  269. }
  270. }
  271. }
  272. else
  273. {
  274. X962Parameters parameters = X962Parameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters);
  275. if (parameters.IsNamedCurve)
  276. {
  277. DerObjectIdentifier oid = DerObjectIdentifier.GetInstance(parameters.Parameters);
  278. X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByOid(oid);
  279. ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(oid, ecP),
  280. gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
  281. gostParams.EncryptionParamSet);
  282. }
  283. else if (parameters.IsImplicitlyCA)
  284. {
  285. ecSpec = null;
  286. }
  287. else
  288. {
  289. X9ECParameters ecP = X9ECParameters.GetInstance(parameters.Parameters);
  290. ecSpec = new ECGost3410Parameters(new ECNamedDomainParameters(algOid, ecP),
  291. gostParams.PublicKeyParamSet, gostParams.DigestParamSet,
  292. gostParams.EncryptionParamSet);
  293. }
  294. Asn1Encodable privKey = keyInfo.ParsePrivateKey();
  295. if (privKey is DerInteger)
  296. {
  297. DerInteger derD = DerInteger.GetInstance(privKey);
  298. d = derD.Value;
  299. }
  300. else
  301. {
  302. ECPrivateKeyStructure ec = ECPrivateKeyStructure.GetInstance(privKey);
  303. d = ec.GetKey();
  304. }
  305. }
  306. return new ECPrivateKeyParameters(
  307. d,
  308. new ECGost3410Parameters(
  309. ecSpec,
  310. gostParams.PublicKeyParamSet,
  311. gostParams.DigestParamSet,
  312. gostParams.EncryptionParamSet));
  313. }
  314. else
  315. {
  316. throw new SecurityUtilityException("algorithm identifier in private key not recognised");
  317. }
  318. }
  319. private static byte[] GetRawKey(PrivateKeyInfo keyInfo)
  320. {
  321. return Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets();
  322. }
  323. public static AsymmetricKeyParameter DecryptKey(
  324. char[] passPhrase,
  325. EncryptedPrivateKeyInfo encInfo)
  326. {
  327. return CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(passPhrase, encInfo));
  328. }
  329. public static AsymmetricKeyParameter DecryptKey(
  330. char[] passPhrase,
  331. byte[] encryptedPrivateKeyInfoData)
  332. {
  333. return DecryptKey(passPhrase, Asn1Object.FromByteArray(encryptedPrivateKeyInfoData));
  334. }
  335. public static AsymmetricKeyParameter DecryptKey(
  336. char[] passPhrase,
  337. Stream encryptedPrivateKeyInfoStream)
  338. {
  339. return DecryptKey(passPhrase, Asn1Object.FromStream(encryptedPrivateKeyInfoStream));
  340. }
  341. private static AsymmetricKeyParameter DecryptKey(
  342. char[] passPhrase,
  343. Asn1Object asn1Object)
  344. {
  345. return DecryptKey(passPhrase, EncryptedPrivateKeyInfo.GetInstance(asn1Object));
  346. }
  347. public static byte[] EncryptKey(
  348. DerObjectIdentifier algorithm,
  349. char[] passPhrase,
  350. byte[] salt,
  351. int iterationCount,
  352. AsymmetricKeyParameter key)
  353. {
  354. return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
  355. algorithm, passPhrase, salt, iterationCount, key).GetEncoded();
  356. }
  357. public static byte[] EncryptKey(
  358. string algorithm,
  359. char[] passPhrase,
  360. byte[] salt,
  361. int iterationCount,
  362. AsymmetricKeyParameter key)
  363. {
  364. return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
  365. algorithm, passPhrase, salt, iterationCount, key).GetEncoded();
  366. }
  367. }
  368. }
  369. #pragma warning restore
  370. #endif