PbeUtilities.cs 33 KB

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