PbeUtilities.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.BC;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.TeleTrust;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  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.Utilities;
  17. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  18. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Security
  19. {
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public static class PbeUtilities
  24. {
  25. const string Pkcs5S1 = "Pkcs5S1";
  26. const string Pkcs5S2 = "Pkcs5S2";
  27. const string Pkcs12 = "Pkcs12";
  28. const string OpenSsl = "OpenSsl";
  29. private static readonly IDictionary<string, string> Algorithms =
  30. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  31. private static readonly IDictionary<string, string> AlgorithmType =
  32. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  33. private static readonly IDictionary<string, DerObjectIdentifier> Oids =
  34. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  35. static PbeUtilities()
  36. {
  37. Algorithms["PKCS5SCHEME1"] = "Pkcs5scheme1";
  38. Algorithms["PKCS5SCHEME2"] = "Pkcs5scheme2";
  39. Algorithms["PBKDF2"] = "Pkcs5scheme2";
  40. Algorithms[PkcsObjectIdentifiers.IdPbeS2.Id] = "Pkcs5scheme2";
  41. // algorithms[PkcsObjectIdentifiers.IdPbkdf2.Id] = "Pkcs5scheme2";
  42. // FIXME Add support for these? (see Pkcs8Generator)
  43. // algorithms[PkcsObjectIdentifiers.DesEde3Cbc.Id] = "Pkcs5scheme2";
  44. // algorithms[NistObjectIdentifiers.IdAes128Cbc.Id] = "Pkcs5scheme2";
  45. // algorithms[NistObjectIdentifiers.IdAes192Cbc.Id] = "Pkcs5scheme2";
  46. // algorithms[NistObjectIdentifiers.IdAes256Cbc.Id] = "Pkcs5scheme2";
  47. Algorithms["PBEWITHMD2ANDDES-CBC"] = "PBEwithMD2andDES-CBC";
  48. Algorithms[PkcsObjectIdentifiers.PbeWithMD2AndDesCbc.Id] = "PBEwithMD2andDES-CBC";
  49. Algorithms["PBEWITHMD2ANDRC2-CBC"] = "PBEwithMD2andRC2-CBC";
  50. Algorithms[PkcsObjectIdentifiers.PbeWithMD2AndRC2Cbc.Id] = "PBEwithMD2andRC2-CBC";
  51. Algorithms["PBEWITHMD5ANDDES-CBC"] = "PBEwithMD5andDES-CBC";
  52. Algorithms[PkcsObjectIdentifiers.PbeWithMD5AndDesCbc.Id] = "PBEwithMD5andDES-CBC";
  53. Algorithms["PBEWITHMD5ANDRC2-CBC"] = "PBEwithMD5andRC2-CBC";
  54. Algorithms[PkcsObjectIdentifiers.PbeWithMD5AndRC2Cbc.Id] = "PBEwithMD5andRC2-CBC";
  55. Algorithms["PBEWITHSHA1ANDDES"] = "PBEwithSHA-1andDES-CBC";
  56. Algorithms["PBEWITHSHA-1ANDDES"] = "PBEwithSHA-1andDES-CBC";
  57. Algorithms["PBEWITHSHA1ANDDES-CBC"] = "PBEwithSHA-1andDES-CBC";
  58. Algorithms["PBEWITHSHA-1ANDDES-CBC"] = "PBEwithSHA-1andDES-CBC";
  59. Algorithms[PkcsObjectIdentifiers.PbeWithSha1AndDesCbc.Id] = "PBEwithSHA-1andDES-CBC";
  60. Algorithms["PBEWITHSHA1ANDRC2"] = "PBEwithSHA-1andRC2-CBC";
  61. Algorithms["PBEWITHSHA-1ANDRC2"] = "PBEwithSHA-1andRC2-CBC";
  62. Algorithms["PBEWITHSHA1ANDRC2-CBC"] = "PBEwithSHA-1andRC2-CBC";
  63. Algorithms["PBEWITHSHA-1ANDRC2-CBC"] = "PBEwithSHA-1andRC2-CBC";
  64. Algorithms[PkcsObjectIdentifiers.PbeWithSha1AndRC2Cbc.Id] = "PBEwithSHA-1andRC2-CBC";
  65. Algorithms["PKCS12"] = "Pkcs12";
  66. Algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes128_cbc.Id] = "PBEwithSHA-1and128bitAES-CBC-BC";
  67. Algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes192_cbc.Id] = "PBEwithSHA-1and192bitAES-CBC-BC";
  68. Algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes256_cbc.Id] = "PBEwithSHA-1and256bitAES-CBC-BC";
  69. Algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes128_cbc.Id] = "PBEwithSHA-256and128bitAES-CBC-BC";
  70. Algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes192_cbc.Id] = "PBEwithSHA-256and192bitAES-CBC-BC";
  71. Algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes256_cbc.Id] = "PBEwithSHA-256and256bitAES-CBC-BC";
  72. Algorithms["PBEWITHSHAAND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
  73. Algorithms["PBEWITHSHA1AND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
  74. Algorithms["PBEWITHSHA-1AND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
  75. Algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4.Id] = "PBEwithSHA-1and128bitRC4";
  76. Algorithms["PBEWITHSHAAND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
  77. Algorithms["PBEWITHSHA1AND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
  78. Algorithms["PBEWITHSHA-1AND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
  79. Algorithms[PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4.Id] = "PBEwithSHA-1and40bitRC4";
  80. Algorithms["PBEWITHSHAAND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  81. Algorithms["PBEWITHSHAAND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  82. Algorithms["PBEWITHSHA1AND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  83. Algorithms["PBEWITHSHA1AND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  84. Algorithms["PBEWITHSHA-1AND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  85. Algorithms["PBEWITHSHA-1AND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  86. Algorithms[PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc.Id] = "PBEwithSHA-1and3-keyDESEDE-CBC";
  87. Algorithms["PBEWITHSHAAND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  88. Algorithms["PBEWITHSHAAND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  89. Algorithms["PBEWITHSHA1AND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  90. Algorithms["PBEWITHSHA1AND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  91. Algorithms["PBEWITHSHA-1AND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  92. Algorithms["PBEWITHSHA-1AND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  93. Algorithms[PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc.Id] = "PBEwithSHA-1and2-keyDESEDE-CBC";
  94. Algorithms["PBEWITHSHAAND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
  95. Algorithms["PBEWITHSHA1AND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
  96. Algorithms["PBEWITHSHA-1AND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
  97. Algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc.Id] = "PBEwithSHA-1and128bitRC2-CBC";
  98. Algorithms["PBEWITHSHAAND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
  99. Algorithms["PBEWITHSHA1AND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
  100. Algorithms["PBEWITHSHA-1AND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
  101. Algorithms[PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc.Id] = "PBEwithSHA-1and40bitRC2-CBC";
  102. Algorithms["PBEWITHSHAAND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
  103. Algorithms["PBEWITHSHA1AND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
  104. Algorithms["PBEWITHSHA-1AND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
  105. Algorithms["PBEWITHSHAAND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
  106. Algorithms["PBEWITHSHA1AND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
  107. Algorithms["PBEWITHSHA-1AND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
  108. Algorithms["PBEWITHSHAAND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
  109. Algorithms["PBEWITHSHA1AND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
  110. Algorithms["PBEWITHSHA-1AND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
  111. Algorithms["PBEWITHSHA256AND128BITAES-CBC-BC"] = "PBEwithSHA-256and128bitAES-CBC-BC";
  112. Algorithms["PBEWITHSHA-256AND128BITAES-CBC-BC"] = "PBEwithSHA-256and128bitAES-CBC-BC";
  113. Algorithms["PBEWITHSHA256AND192BITAES-CBC-BC"] = "PBEwithSHA-256and192bitAES-CBC-BC";
  114. Algorithms["PBEWITHSHA-256AND192BITAES-CBC-BC"] = "PBEwithSHA-256and192bitAES-CBC-BC";
  115. Algorithms["PBEWITHSHA256AND256BITAES-CBC-BC"] = "PBEwithSHA-256and256bitAES-CBC-BC";
  116. Algorithms["PBEWITHSHA-256AND256BITAES-CBC-BC"] = "PBEwithSHA-256and256bitAES-CBC-BC";
  117. Algorithms["PBEWITHSHAANDIDEA"] = "PBEwithSHA-1andIDEA-CBC";
  118. Algorithms["PBEWITHSHAANDIDEA-CBC"] = "PBEwithSHA-1andIDEA-CBC";
  119. Algorithms["PBEWITHSHAANDTWOFISH"] = "PBEwithSHA-1andTWOFISH-CBC";
  120. Algorithms["PBEWITHSHAANDTWOFISH-CBC"] = "PBEwithSHA-1andTWOFISH-CBC";
  121. Algorithms["PBEWITHHMACSHA1"] = "PBEwithHmacSHA-1";
  122. Algorithms["PBEWITHHMACSHA-1"] = "PBEwithHmacSHA-1";
  123. Algorithms[OiwObjectIdentifiers.IdSha1.Id] = "PBEwithHmacSHA-1";
  124. Algorithms["PBEWITHHMACSHA224"] = "PBEwithHmacSHA-224";
  125. Algorithms["PBEWITHHMACSHA-224"] = "PBEwithHmacSHA-224";
  126. Algorithms[NistObjectIdentifiers.IdSha224.Id] = "PBEwithHmacSHA-224";
  127. Algorithms["PBEWITHHMACSHA256"] = "PBEwithHmacSHA-256";
  128. Algorithms["PBEWITHHMACSHA-256"] = "PBEwithHmacSHA-256";
  129. Algorithms[NistObjectIdentifiers.IdSha256.Id] = "PBEwithHmacSHA-256";
  130. Algorithms["PBEWITHHMACRIPEMD128"] = "PBEwithHmacRipeMD128";
  131. Algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "PBEwithHmacRipeMD128";
  132. Algorithms["PBEWITHHMACRIPEMD160"] = "PBEwithHmacRipeMD160";
  133. Algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "PBEwithHmacRipeMD160";
  134. Algorithms["PBEWITHHMACRIPEMD256"] = "PBEwithHmacRipeMD256";
  135. Algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "PBEwithHmacRipeMD256";
  136. Algorithms["PBEWITHHMACTIGER"] = "PBEwithHmacTiger";
  137. Algorithms["PBEWITHMD5AND128BITAES-CBC-OPENSSL"] = "PBEwithMD5and128bitAES-CBC-OpenSSL";
  138. Algorithms["PBEWITHMD5AND192BITAES-CBC-OPENSSL"] = "PBEwithMD5and192bitAES-CBC-OpenSSL";
  139. Algorithms["PBEWITHMD5AND256BITAES-CBC-OPENSSL"] = "PBEwithMD5and256bitAES-CBC-OpenSSL";
  140. AlgorithmType["Pkcs5scheme1"] = Pkcs5S1;
  141. AlgorithmType["Pkcs5scheme2"] = Pkcs5S2;
  142. AlgorithmType["PBEwithMD2andDES-CBC"] = Pkcs5S1;
  143. AlgorithmType["PBEwithMD2andRC2-CBC"] = Pkcs5S1;
  144. AlgorithmType["PBEwithMD5andDES-CBC"] = Pkcs5S1;
  145. AlgorithmType["PBEwithMD5andRC2-CBC"] = Pkcs5S1;
  146. AlgorithmType["PBEwithSHA-1andDES-CBC"] = Pkcs5S1;
  147. AlgorithmType["PBEwithSHA-1andRC2-CBC"] = Pkcs5S1;
  148. AlgorithmType["Pkcs12"] = Pkcs12;
  149. AlgorithmType["PBEwithSHA-1and128bitRC4"] = Pkcs12;
  150. AlgorithmType["PBEwithSHA-1and40bitRC4"] = Pkcs12;
  151. AlgorithmType["PBEwithSHA-1and3-keyDESEDE-CBC"] = Pkcs12;
  152. AlgorithmType["PBEwithSHA-1and2-keyDESEDE-CBC"] = Pkcs12;
  153. AlgorithmType["PBEwithSHA-1and128bitRC2-CBC"] = Pkcs12;
  154. AlgorithmType["PBEwithSHA-1and40bitRC2-CBC"] = Pkcs12;
  155. AlgorithmType["PBEwithSHA-1and128bitAES-CBC-BC"] = Pkcs12;
  156. AlgorithmType["PBEwithSHA-1and192bitAES-CBC-BC"] = Pkcs12;
  157. AlgorithmType["PBEwithSHA-1and256bitAES-CBC-BC"] = Pkcs12;
  158. AlgorithmType["PBEwithSHA-256and128bitAES-CBC-BC"] = Pkcs12;
  159. AlgorithmType["PBEwithSHA-256and192bitAES-CBC-BC"] = Pkcs12;
  160. AlgorithmType["PBEwithSHA-256and256bitAES-CBC-BC"] = Pkcs12;
  161. AlgorithmType["PBEwithSHA-1andIDEA-CBC"] = Pkcs12;
  162. AlgorithmType["PBEwithSHA-1andTWOFISH-CBC"] = Pkcs12;
  163. AlgorithmType["PBEwithHmacSHA-1"] = Pkcs12;
  164. AlgorithmType["PBEwithHmacSHA-224"] = Pkcs12;
  165. AlgorithmType["PBEwithHmacSHA-256"] = Pkcs12;
  166. AlgorithmType["PBEwithHmacRipeMD128"] = Pkcs12;
  167. AlgorithmType["PBEwithHmacRipeMD160"] = Pkcs12;
  168. AlgorithmType["PBEwithHmacRipeMD256"] = Pkcs12;
  169. AlgorithmType["PBEwithHmacTiger"] = Pkcs12;
  170. AlgorithmType["PBEwithMD5and128bitAES-CBC-OpenSSL"] = OpenSsl;
  171. AlgorithmType["PBEwithMD5and192bitAES-CBC-OpenSSL"] = OpenSsl;
  172. AlgorithmType["PBEwithMD5and256bitAES-CBC-OpenSSL"] = OpenSsl;
  173. Oids["PBEwithMD2andDES-CBC"] = PkcsObjectIdentifiers.PbeWithMD2AndDesCbc;
  174. Oids["PBEwithMD2andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithMD2AndRC2Cbc;
  175. Oids["PBEwithMD5andDES-CBC"] = PkcsObjectIdentifiers.PbeWithMD5AndDesCbc;
  176. Oids["PBEwithMD5andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithMD5AndRC2Cbc;
  177. Oids["PBEwithSHA-1andDES-CBC"] = PkcsObjectIdentifiers.PbeWithSha1AndDesCbc;
  178. Oids["PBEwithSHA-1andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithSha1AndRC2Cbc;
  179. Oids["PBEwithSHA-1and128bitRC4"] = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4;
  180. Oids["PBEwithSHA-1and40bitRC4"] = PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4;
  181. Oids["PBEwithSHA-1and3-keyDESEDE-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc;
  182. Oids["PBEwithSHA-1and2-keyDESEDE-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc;
  183. Oids["PBEwithSHA-1and128bitRC2-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc;
  184. Oids["PBEwithSHA-1and40bitRC2-CBC"] = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc;
  185. Oids["PBEwithHmacSHA-1"] = OiwObjectIdentifiers.IdSha1;
  186. Oids["PBEwithHmacSHA-224"] = NistObjectIdentifiers.IdSha224;
  187. Oids["PBEwithHmacSHA-256"] = NistObjectIdentifiers.IdSha256;
  188. Oids["PBEwithHmacRipeMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
  189. Oids["PBEwithHmacRipeMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
  190. Oids["PBEwithHmacRipeMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
  191. Oids["Pkcs5scheme2"] = PkcsObjectIdentifiers.IdPbeS2;
  192. }
  193. static PbeParametersGenerator MakePbeGenerator(
  194. string type,
  195. IDigest digest,
  196. byte[] key,
  197. byte[] salt,
  198. int iterationCount)
  199. {
  200. PbeParametersGenerator generator;
  201. if (type.Equals(Pkcs5S1))
  202. {
  203. generator = new Pkcs5S1ParametersGenerator(digest);
  204. }
  205. else if (type.Equals(Pkcs5S2))
  206. {
  207. generator = new Pkcs5S2ParametersGenerator(digest);
  208. }
  209. else if (type.Equals(Pkcs12))
  210. {
  211. generator = new Pkcs12ParametersGenerator(digest);
  212. }
  213. else if (type.Equals(OpenSsl))
  214. {
  215. generator = new OpenSslPbeParametersGenerator();
  216. }
  217. else
  218. {
  219. throw new ArgumentException("Unknown PBE type: " + type, "type");
  220. }
  221. generator.Init(key, salt, iterationCount);
  222. return generator;
  223. }
  224. /// <summary>
  225. /// Returns a ObjectIdentifier for a give encoding.
  226. /// </summary>
  227. /// <param name="mechanism">A string representation of the encoding.</param>
  228. /// <returns>A DerObjectIdentifier, null if the Oid is not available.</returns>
  229. public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
  230. {
  231. if (!Algorithms.TryGetValue(mechanism, out var algorithm))
  232. return null;
  233. return CollectionUtilities.GetValueOrNull(Oids, algorithm);
  234. }
  235. //public static ICollection Algorithms
  236. //{
  237. // get { return oids.Keys; }
  238. //}
  239. public static bool IsPkcs12(string algorithm)
  240. {
  241. if (!Algorithms.TryGetValue(algorithm, out var mechanism))
  242. return false;
  243. if (!AlgorithmType.TryGetValue(mechanism, out var algorithmType))
  244. return false;
  245. return Pkcs12.Equals(algorithmType);
  246. }
  247. public static bool IsPkcs5Scheme1(string algorithm)
  248. {
  249. if (!Algorithms.TryGetValue(algorithm, out var mechanism))
  250. return false;
  251. if (!AlgorithmType.TryGetValue(mechanism, out var algorithmType))
  252. return false;
  253. return Pkcs5S1.Equals(algorithmType);
  254. }
  255. public static bool IsPkcs5Scheme2(string algorithm)
  256. {
  257. if (!Algorithms.TryGetValue(algorithm, out var mechanism))
  258. return false;
  259. if (!AlgorithmType.TryGetValue(mechanism, out var algorithmType))
  260. return false;
  261. return Pkcs5S2.Equals(algorithmType);
  262. }
  263. public static bool IsOpenSsl(string algorithm)
  264. {
  265. if (!Algorithms.TryGetValue(algorithm, out var mechanism))
  266. return false;
  267. if (!AlgorithmType.TryGetValue(mechanism, out var algorithmType))
  268. return false;
  269. return OpenSsl.Equals(algorithmType);
  270. }
  271. public static bool IsPbeAlgorithm(string algorithm)
  272. {
  273. if (!Algorithms.TryGetValue(algorithm, out var mechanism))
  274. return false;
  275. return AlgorithmType.ContainsKey(mechanism);
  276. }
  277. public static Asn1Encodable GenerateAlgorithmParameters(
  278. DerObjectIdentifier algorithmOid,
  279. byte[] salt,
  280. int iterationCount)
  281. {
  282. return GenerateAlgorithmParameters(algorithmOid.Id, salt, iterationCount);
  283. }
  284. public static Asn1Encodable GenerateAlgorithmParameters(
  285. string algorithm,
  286. byte[] salt,
  287. int iterationCount)
  288. {
  289. if (IsPkcs12(algorithm))
  290. {
  291. return new Pkcs12PbeParams(salt, iterationCount);
  292. }
  293. else if (IsPkcs5Scheme2(algorithm))
  294. {
  295. return new Pbkdf2Params(salt, iterationCount);
  296. }
  297. else
  298. {
  299. return new PbeParameter(salt, iterationCount);
  300. }
  301. }
  302. public static Asn1Encodable GenerateAlgorithmParameters(
  303. DerObjectIdentifier cipherAlgorithm,
  304. DerObjectIdentifier hashAlgorithm,
  305. byte[] salt,
  306. int iterationCount,
  307. SecureRandom secureRandom)
  308. {
  309. EncryptionScheme encScheme;
  310. if (NistObjectIdentifiers.IdAes128Cbc.Equals(cipherAlgorithm)
  311. || NistObjectIdentifiers.IdAes192Cbc.Equals(cipherAlgorithm)
  312. || NistObjectIdentifiers.IdAes256Cbc.Equals(cipherAlgorithm)
  313. || NistObjectIdentifiers.IdAes128Cfb.Equals(cipherAlgorithm)
  314. || NistObjectIdentifiers.IdAes192Cfb.Equals(cipherAlgorithm)
  315. || NistObjectIdentifiers.IdAes256Cfb.Equals(cipherAlgorithm))
  316. {
  317. byte[] iv = new byte[16];
  318. secureRandom.NextBytes(iv);
  319. encScheme = new EncryptionScheme(cipherAlgorithm, new DerOctetString(iv));
  320. }
  321. else
  322. {
  323. throw new ArgumentException("unknown cipher: " + cipherAlgorithm);
  324. }
  325. KeyDerivationFunc func = new KeyDerivationFunc(PkcsObjectIdentifiers.IdPbkdf2, new Pbkdf2Params(salt, iterationCount, new AlgorithmIdentifier(hashAlgorithm, DerNull.Instance)));
  326. return new PbeS2Parameters(func, encScheme);
  327. }
  328. public static ICipherParameters GenerateCipherParameters(
  329. DerObjectIdentifier algorithmOid,
  330. char[] password,
  331. Asn1Encodable pbeParameters)
  332. {
  333. return GenerateCipherParameters(algorithmOid.Id, password, false, pbeParameters);
  334. }
  335. public static ICipherParameters GenerateCipherParameters(
  336. DerObjectIdentifier algorithmOid,
  337. char[] password,
  338. bool wrongPkcs12Zero,
  339. Asn1Encodable pbeParameters)
  340. {
  341. return GenerateCipherParameters(algorithmOid.Id, password, wrongPkcs12Zero, pbeParameters);
  342. }
  343. public static ICipherParameters GenerateCipherParameters(
  344. AlgorithmIdentifier algID,
  345. char[] password)
  346. {
  347. return GenerateCipherParameters(algID.Algorithm.Id, password, false, algID.Parameters);
  348. }
  349. public static ICipherParameters GenerateCipherParameters(
  350. AlgorithmIdentifier algID,
  351. char[] password,
  352. bool wrongPkcs12Zero)
  353. {
  354. return GenerateCipherParameters(algID.Algorithm.Id, password, wrongPkcs12Zero, algID.Parameters);
  355. }
  356. public static ICipherParameters GenerateCipherParameters(
  357. string algorithm,
  358. char[] password,
  359. Asn1Encodable pbeParameters)
  360. {
  361. return GenerateCipherParameters(algorithm, password, false, pbeParameters);
  362. }
  363. public static ICipherParameters GenerateCipherParameters(
  364. string algorithm,
  365. char[] password,
  366. bool wrongPkcs12Zero,
  367. Asn1Encodable pbeParameters)
  368. {
  369. string mechanism = CollectionUtilities.GetValueOrNull(Algorithms, algorithm);
  370. byte[] keyBytes = null;
  371. byte[] salt = null;
  372. int iterationCount = 0;
  373. if (IsPkcs12(mechanism))
  374. {
  375. Pkcs12PbeParams pbeParams = Pkcs12PbeParams.GetInstance(pbeParameters);
  376. salt = pbeParams.GetIV();
  377. iterationCount = pbeParams.Iterations.IntValue;
  378. keyBytes = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
  379. }
  380. else if (IsPkcs5Scheme2(mechanism))
  381. {
  382. // See below
  383. }
  384. else
  385. {
  386. PbeParameter pbeParams = PbeParameter.GetInstance(pbeParameters);
  387. salt = pbeParams.GetSalt();
  388. iterationCount = pbeParams.IterationCount.IntValue;
  389. keyBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
  390. }
  391. ICipherParameters parameters = null;
  392. if (IsPkcs5Scheme2(mechanism))
  393. {
  394. PbeS2Parameters s2p = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
  395. AlgorithmIdentifier encScheme = s2p.EncryptionScheme;
  396. DerObjectIdentifier encOid = encScheme.Algorithm;
  397. Asn1Object encParams = encScheme.Parameters.ToAsn1Object();
  398. Pbkdf2Params pbeParams = Pbkdf2Params.GetInstance(s2p.KeyDerivationFunc.Parameters.ToAsn1Object());
  399. IDigest digest = DigestUtilities.GetDigest(pbeParams.Prf.Algorithm);
  400. byte[] iv;
  401. if (encOid.Equals(PkcsObjectIdentifiers.RC2Cbc)) // PKCS5.B.2.3
  402. {
  403. RC2CbcParameter rc2Params = RC2CbcParameter.GetInstance(encParams);
  404. iv = rc2Params.GetIV();
  405. }
  406. else
  407. {
  408. iv = Asn1OctetString.GetInstance(encParams).GetOctets();
  409. }
  410. salt = pbeParams.GetSalt();
  411. iterationCount = pbeParams.IterationCount.IntValue;
  412. keyBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
  413. int keyLength = pbeParams.KeyLength != null
  414. ? pbeParams.KeyLength.IntValue * 8
  415. : GeneratorUtilities.GetDefaultKeySize(encOid);
  416. PbeParametersGenerator gen = MakePbeGenerator(
  417. AlgorithmType[mechanism], digest, keyBytes, salt, iterationCount);
  418. parameters = gen.GenerateDerivedParameters(encOid.Id, keyLength);
  419. if (iv != null)
  420. {
  421. // FIXME? OpenSSL weirdness with IV of zeros (for ECB keys?)
  422. if (Arrays.AreEqual(iv, new byte[iv.Length]))
  423. {
  424. //Console.Error.Write("***** IV all 0 (length " + iv.Length + ") *****");
  425. }
  426. else
  427. {
  428. parameters = new ParametersWithIV(parameters, iv);
  429. }
  430. }
  431. }
  432. else if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithSHA-1"))
  433. {
  434. PbeParametersGenerator generator = MakePbeGenerator(
  435. AlgorithmType[mechanism], new Sha1Digest(), keyBytes, salt, iterationCount);
  436. if (mechanism.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
  437. {
  438. parameters = generator.GenerateDerivedParameters("AES", 128, 128);
  439. }
  440. else if (mechanism.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
  441. {
  442. parameters = generator.GenerateDerivedParameters("AES", 192, 128);
  443. }
  444. else if (mechanism.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
  445. {
  446. parameters = generator.GenerateDerivedParameters("AES", 256, 128);
  447. }
  448. else if (mechanism.Equals("PBEwithSHA-1and128bitRC4"))
  449. {
  450. parameters = generator.GenerateDerivedParameters("RC4", 128);
  451. }
  452. else if (mechanism.Equals("PBEwithSHA-1and40bitRC4"))
  453. {
  454. parameters = generator.GenerateDerivedParameters("RC4", 40);
  455. }
  456. else if (mechanism.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
  457. {
  458. parameters = generator.GenerateDerivedParameters("DESEDE", 192, 64);
  459. }
  460. else if (mechanism.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
  461. {
  462. parameters = generator.GenerateDerivedParameters("DESEDE", 128, 64);
  463. }
  464. else if (mechanism.Equals("PBEwithSHA-1and128bitRC2-CBC"))
  465. {
  466. parameters = generator.GenerateDerivedParameters("RC2", 128, 64);
  467. }
  468. else if (mechanism.Equals("PBEwithSHA-1and40bitRC2-CBC"))
  469. {
  470. parameters = generator.GenerateDerivedParameters("RC2", 40, 64);
  471. }
  472. else if (mechanism.Equals("PBEwithSHA-1andDES-CBC"))
  473. {
  474. parameters = generator.GenerateDerivedParameters("DES", 64, 64);
  475. }
  476. else if (mechanism.Equals("PBEwithSHA-1andRC2-CBC"))
  477. {
  478. parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
  479. }
  480. }
  481. else if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithSHA-256"))
  482. {
  483. PbeParametersGenerator generator = MakePbeGenerator(
  484. AlgorithmType[mechanism], new Sha256Digest(), keyBytes, salt, iterationCount);
  485. if (mechanism.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
  486. {
  487. parameters = generator.GenerateDerivedParameters("AES", 128, 128);
  488. }
  489. else if (mechanism.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
  490. {
  491. parameters = generator.GenerateDerivedParameters("AES", 192, 128);
  492. }
  493. else if (mechanism.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
  494. {
  495. parameters = generator.GenerateDerivedParameters("AES", 256, 128);
  496. }
  497. }
  498. else if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithMD5"))
  499. {
  500. PbeParametersGenerator generator = MakePbeGenerator(
  501. AlgorithmType[mechanism], new MD5Digest(), keyBytes, salt, iterationCount);
  502. if (mechanism.Equals("PBEwithMD5andDES-CBC"))
  503. {
  504. parameters = generator.GenerateDerivedParameters("DES", 64, 64);
  505. }
  506. else if (mechanism.Equals("PBEwithMD5andRC2-CBC"))
  507. {
  508. parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
  509. }
  510. else if (mechanism.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
  511. {
  512. parameters = generator.GenerateDerivedParameters("AES", 128, 128);
  513. }
  514. else if (mechanism.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
  515. {
  516. parameters = generator.GenerateDerivedParameters("AES", 192, 128);
  517. }
  518. else if (mechanism.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
  519. {
  520. parameters = generator.GenerateDerivedParameters("AES", 256, 128);
  521. }
  522. }
  523. else if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithMD2"))
  524. {
  525. PbeParametersGenerator generator = MakePbeGenerator(
  526. AlgorithmType[mechanism], new MD2Digest(), keyBytes, salt, iterationCount);
  527. if (mechanism.Equals("PBEwithMD2andDES-CBC"))
  528. {
  529. parameters = generator.GenerateDerivedParameters("DES", 64, 64);
  530. }
  531. else if (mechanism.Equals("PBEwithMD2andRC2-CBC"))
  532. {
  533. parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
  534. }
  535. }
  536. else if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithHmac"))
  537. {
  538. string digestName = mechanism.Substring("PBEwithHmac".Length);
  539. IDigest digest = DigestUtilities.GetDigest(digestName);
  540. PbeParametersGenerator generator = MakePbeGenerator(
  541. AlgorithmType[mechanism], digest, keyBytes, salt, iterationCount);
  542. int bitLen = digest.GetDigestSize() * 8;
  543. parameters = generator.GenerateDerivedMacParameters(bitLen);
  544. }
  545. Array.Clear(keyBytes, 0, keyBytes.Length);
  546. return FixDesParity(mechanism, parameters);
  547. }
  548. public static object CreateEngine(
  549. DerObjectIdentifier algorithmOid)
  550. {
  551. return CreateEngine(algorithmOid.Id);
  552. }
  553. public static object CreateEngine(
  554. AlgorithmIdentifier algID)
  555. {
  556. string algorithm = algID.Algorithm.Id;
  557. if (IsPkcs5Scheme2(algorithm))
  558. {
  559. PbeS2Parameters s2p = PbeS2Parameters.GetInstance(algID.Parameters.ToAsn1Object());
  560. AlgorithmIdentifier encScheme = s2p.EncryptionScheme;
  561. return CipherUtilities.GetCipher(encScheme.Algorithm);
  562. }
  563. return CreateEngine(algorithm);
  564. }
  565. public static object CreateEngine(string algorithm)
  566. {
  567. string mechanism = CollectionUtilities.GetValueOrNull(Algorithms, algorithm);
  568. if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithHmac"))
  569. {
  570. string digestName = mechanism.Substring("PBEwithHmac".Length);
  571. return MacUtilities.GetMac("HMAC/" + digestName);
  572. }
  573. if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithMD2")
  574. || Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithMD5")
  575. || Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithSHA-1")
  576. || Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEwithSHA-256"))
  577. {
  578. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "AES-CBC-BC") || Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "AES-CBC-OPENSSL"))
  579. {
  580. return CipherUtilities.GetCipher("AES/CBC");
  581. }
  582. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DES-CBC"))
  583. {
  584. return CipherUtilities.GetCipher("DES/CBC");
  585. }
  586. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DESEDE-CBC"))
  587. {
  588. return CipherUtilities.GetCipher("DESEDE/CBC");
  589. }
  590. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "RC2-CBC"))
  591. {
  592. return CipherUtilities.GetCipher("RC2/CBC");
  593. }
  594. if (Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "RC4"))
  595. {
  596. return CipherUtilities.GetCipher("RC4");
  597. }
  598. }
  599. return null;
  600. }
  601. public static string GetEncodingName(DerObjectIdentifier oid)
  602. {
  603. return CollectionUtilities.GetValueOrNull(Algorithms, oid.Id);
  604. }
  605. private static ICipherParameters FixDesParity(string mechanism, ICipherParameters parameters)
  606. {
  607. if (!Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DES-CBC") && !Org.BouncyCastle.Utilities.Platform.EndsWith(mechanism, "DESEDE-CBC"))
  608. {
  609. return parameters;
  610. }
  611. if (parameters is ParametersWithIV)
  612. {
  613. ParametersWithIV ivParams = (ParametersWithIV)parameters;
  614. return new ParametersWithIV(FixDesParity(mechanism, ivParams.Parameters), ivParams.GetIV());
  615. }
  616. KeyParameter kParam = (KeyParameter)parameters;
  617. byte[] keyBytes = kParam.GetKey();
  618. DesParameters.SetOddParity(keyBytes);
  619. return new KeyParameter(keyBytes);
  620. }
  621. }
  622. }
  623. #pragma warning restore
  624. #endif