GeneratorUtilities.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.Collections;
  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.Iana;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Kisa;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ntt;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  19. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Security
  20. {
  21. public sealed class GeneratorUtilities
  22. {
  23. private GeneratorUtilities()
  24. {
  25. }
  26. private static readonly IDictionary kgAlgorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  27. private static readonly IDictionary kpgAlgorithms = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  28. private static readonly IDictionary defaultKeySizes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  29. static GeneratorUtilities()
  30. {
  31. //
  32. // key generators.
  33. //
  34. AddKgAlgorithm("AES",
  35. "AESWRAP");
  36. AddKgAlgorithm("AES128",
  37. "2.16.840.1.101.3.4.2",
  38. NistObjectIdentifiers.IdAes128Cbc,
  39. NistObjectIdentifiers.IdAes128Cfb,
  40. NistObjectIdentifiers.IdAes128Ecb,
  41. NistObjectIdentifiers.IdAes128Ofb,
  42. NistObjectIdentifiers.IdAes128Wrap);
  43. AddKgAlgorithm("AES192",
  44. "2.16.840.1.101.3.4.22",
  45. NistObjectIdentifiers.IdAes192Cbc,
  46. NistObjectIdentifiers.IdAes192Cfb,
  47. NistObjectIdentifiers.IdAes192Ecb,
  48. NistObjectIdentifiers.IdAes192Ofb,
  49. NistObjectIdentifiers.IdAes192Wrap);
  50. AddKgAlgorithm("AES256",
  51. "2.16.840.1.101.3.4.42",
  52. NistObjectIdentifiers.IdAes256Cbc,
  53. NistObjectIdentifiers.IdAes256Cfb,
  54. NistObjectIdentifiers.IdAes256Ecb,
  55. NistObjectIdentifiers.IdAes256Ofb,
  56. NistObjectIdentifiers.IdAes256Wrap);
  57. AddKgAlgorithm("BLOWFISH",
  58. "1.3.6.1.4.1.3029.1.2");
  59. AddKgAlgorithm("CAMELLIA",
  60. "CAMELLIAWRAP");
  61. AddKgAlgorithm("CAMELLIA128",
  62. NttObjectIdentifiers.IdCamellia128Cbc,
  63. NttObjectIdentifiers.IdCamellia128Wrap);
  64. AddKgAlgorithm("CAMELLIA192",
  65. NttObjectIdentifiers.IdCamellia192Cbc,
  66. NttObjectIdentifiers.IdCamellia192Wrap);
  67. AddKgAlgorithm("CAMELLIA256",
  68. NttObjectIdentifiers.IdCamellia256Cbc,
  69. NttObjectIdentifiers.IdCamellia256Wrap);
  70. AddKgAlgorithm("CAST5",
  71. "1.2.840.113533.7.66.10");
  72. AddKgAlgorithm("CAST6");
  73. AddKgAlgorithm("DES",
  74. OiwObjectIdentifiers.DesCbc,
  75. OiwObjectIdentifiers.DesCfb,
  76. OiwObjectIdentifiers.DesEcb,
  77. OiwObjectIdentifiers.DesOfb);
  78. AddKgAlgorithm("DESEDE",
  79. "DESEDEWRAP",
  80. "TDEA",
  81. OiwObjectIdentifiers.DesEde);
  82. AddKgAlgorithm("DESEDE3",
  83. PkcsObjectIdentifiers.DesEde3Cbc,
  84. PkcsObjectIdentifiers.IdAlgCms3DesWrap);
  85. AddKgAlgorithm("GOST28147",
  86. "GOST",
  87. "GOST-28147",
  88. CryptoProObjectIdentifiers.GostR28147Cbc);
  89. AddKgAlgorithm("HC128");
  90. AddKgAlgorithm("HC256");
  91. AddKgAlgorithm("IDEA",
  92. "1.3.6.1.4.1.188.7.1.1.2");
  93. AddKgAlgorithm("NOEKEON");
  94. AddKgAlgorithm("RC2",
  95. PkcsObjectIdentifiers.RC2Cbc,
  96. PkcsObjectIdentifiers.IdAlgCmsRC2Wrap);
  97. AddKgAlgorithm("RC4",
  98. "ARC4",
  99. "1.2.840.113549.3.4");
  100. AddKgAlgorithm("RC5",
  101. "RC5-32");
  102. AddKgAlgorithm("RC5-64");
  103. AddKgAlgorithm("RC6");
  104. AddKgAlgorithm("RIJNDAEL");
  105. AddKgAlgorithm("SALSA20");
  106. AddKgAlgorithm("SEED",
  107. KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap,
  108. KisaObjectIdentifiers.IdSeedCbc);
  109. AddKgAlgorithm("SERPENT");
  110. AddKgAlgorithm("SKIPJACK");
  111. AddKgAlgorithm("SM4");
  112. AddKgAlgorithm("TEA");
  113. AddKgAlgorithm("THREEFISH-256");
  114. AddKgAlgorithm("THREEFISH-512");
  115. AddKgAlgorithm("THREEFISH-1024");
  116. AddKgAlgorithm("TNEPRES");
  117. AddKgAlgorithm("TWOFISH");
  118. AddKgAlgorithm("VMPC");
  119. AddKgAlgorithm("VMPC-KSA3");
  120. AddKgAlgorithm("XTEA");
  121. //
  122. // HMac key generators
  123. //
  124. AddHMacKeyGenerator("MD2");
  125. AddHMacKeyGenerator("MD4");
  126. AddHMacKeyGenerator("MD5",
  127. IanaObjectIdentifiers.HmacMD5);
  128. AddHMacKeyGenerator("SHA1",
  129. PkcsObjectIdentifiers.IdHmacWithSha1,
  130. IanaObjectIdentifiers.HmacSha1);
  131. AddHMacKeyGenerator("SHA224",
  132. PkcsObjectIdentifiers.IdHmacWithSha224);
  133. AddHMacKeyGenerator("SHA256",
  134. PkcsObjectIdentifiers.IdHmacWithSha256);
  135. AddHMacKeyGenerator("SHA384",
  136. PkcsObjectIdentifiers.IdHmacWithSha384);
  137. AddHMacKeyGenerator("SHA512",
  138. PkcsObjectIdentifiers.IdHmacWithSha512);
  139. AddHMacKeyGenerator("SHA512/224");
  140. AddHMacKeyGenerator("SHA512/256");
  141. AddHMacKeyGenerator("KECCAK224");
  142. AddHMacKeyGenerator("KECCAK256");
  143. AddHMacKeyGenerator("KECCAK288");
  144. AddHMacKeyGenerator("KECCAK384");
  145. AddHMacKeyGenerator("KECCAK512");
  146. AddHMacKeyGenerator("SHA3-224",
  147. NistObjectIdentifiers.IdHMacWithSha3_224);
  148. AddHMacKeyGenerator("SHA3-256",
  149. NistObjectIdentifiers.IdHMacWithSha3_256);
  150. AddHMacKeyGenerator("SHA3-384",
  151. NistObjectIdentifiers.IdHMacWithSha3_384);
  152. AddHMacKeyGenerator("SHA3-512",
  153. NistObjectIdentifiers.IdHMacWithSha3_512);
  154. AddHMacKeyGenerator("RIPEMD128");
  155. AddHMacKeyGenerator("RIPEMD160",
  156. IanaObjectIdentifiers.HmacRipeMD160);
  157. AddHMacKeyGenerator("TIGER",
  158. IanaObjectIdentifiers.HmacTiger);
  159. AddHMacKeyGenerator("GOST3411-2012-256",
  160. RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_256);
  161. AddHMacKeyGenerator("GOST3411-2012-512",
  162. RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_512);
  163. //
  164. // key pair generators.
  165. //
  166. AddKpgAlgorithm("DH",
  167. "DIFFIEHELLMAN");
  168. AddKpgAlgorithm("DSA");
  169. AddKpgAlgorithm("EC",
  170. // TODO Should this be an alias for ECDH?
  171. X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme);
  172. AddKpgAlgorithm("ECDH",
  173. "ECIES");
  174. AddKpgAlgorithm("ECDHC");
  175. AddKpgAlgorithm("ECMQV",
  176. X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme);
  177. AddKpgAlgorithm("ECDSA");
  178. AddKpgAlgorithm("ECGOST3410",
  179. "ECGOST-3410",
  180. "GOST-3410-2001");
  181. AddKpgAlgorithm("Ed25519",
  182. "Ed25519ctx",
  183. "Ed25519ph",
  184. EdECObjectIdentifiers.id_Ed25519);
  185. AddKpgAlgorithm("Ed448",
  186. "Ed448ph",
  187. EdECObjectIdentifiers.id_Ed448);
  188. AddKpgAlgorithm("ELGAMAL");
  189. AddKpgAlgorithm("GOST3410",
  190. "GOST-3410",
  191. "GOST-3410-94");
  192. AddKpgAlgorithm("RSA",
  193. "1.2.840.113549.1.1.1");
  194. AddKpgAlgorithm("X25519",
  195. EdECObjectIdentifiers.id_X25519);
  196. AddKpgAlgorithm("X448",
  197. EdECObjectIdentifiers.id_X448);
  198. AddDefaultKeySizeEntries(64, "DES");
  199. AddDefaultKeySizeEntries(80, "SKIPJACK");
  200. AddDefaultKeySizeEntries(128, "AES128", "BLOWFISH", "CAMELLIA128", "CAST5", "DESEDE",
  201. "HC128", "HMACMD2", "HMACMD4", "HMACMD5", "HMACRIPEMD128", "IDEA", "NOEKEON",
  202. "RC2", "RC4", "RC5", "SALSA20", "SEED", "SM4", "TEA", "XTEA", "VMPC", "VMPC-KSA3");
  203. AddDefaultKeySizeEntries(160, "HMACRIPEMD160", "HMACSHA1");
  204. AddDefaultKeySizeEntries(192, "AES", "AES192", "CAMELLIA192", "DESEDE3", "HMACTIGER",
  205. "RIJNDAEL", "SERPENT", "TNEPRES");
  206. AddDefaultKeySizeEntries(224, "HMACSHA3-224", "HMACKECCAK224", "HMACSHA224", "HMACSHA512/224");
  207. AddDefaultKeySizeEntries(256, "AES256", "CAMELLIA", "CAMELLIA256", "CAST6", "GOST28147",
  208. "HC256", "HMACGOST3411-2012-256", "HMACSHA3-256", "HMACKECCAK256", "HMACSHA256", "HMACSHA512/256", "RC5-64", "RC6", "THREEFISH-256", "TWOFISH");
  209. AddDefaultKeySizeEntries(288, "HMACKECCAK288");
  210. AddDefaultKeySizeEntries(384, "HMACSHA3-384", "HMACKECCAK384", "HMACSHA384");
  211. AddDefaultKeySizeEntries(512, "HMACGOST3411-2012-512", "HMACSHA3-512", "HMACKECCAK512", "HMACSHA512", "THREEFISH-512");
  212. AddDefaultKeySizeEntries(1024, "THREEFISH-1024");
  213. }
  214. private static void AddDefaultKeySizeEntries(int size, params string[] algorithms)
  215. {
  216. foreach (string algorithm in algorithms)
  217. {
  218. defaultKeySizes.Add(algorithm, size);
  219. }
  220. }
  221. private static void AddKgAlgorithm(
  222. string canonicalName,
  223. params object[] aliases)
  224. {
  225. kgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(canonicalName)] = canonicalName;
  226. foreach (object alias in aliases)
  227. {
  228. kgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(alias.ToString())] = canonicalName;
  229. }
  230. }
  231. private static void AddKpgAlgorithm(
  232. string canonicalName,
  233. params object[] aliases)
  234. {
  235. kpgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(canonicalName)] = canonicalName;
  236. foreach (object alias in aliases)
  237. {
  238. kpgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(alias.ToString())] = canonicalName;
  239. }
  240. }
  241. private static void AddHMacKeyGenerator(
  242. string algorithm,
  243. params object[] aliases)
  244. {
  245. string mainName = "HMAC" + algorithm;
  246. kgAlgorithms[mainName] = mainName;
  247. kgAlgorithms["HMAC-" + algorithm] = mainName;
  248. kgAlgorithms["HMAC/" + algorithm] = mainName;
  249. foreach (object alias in aliases)
  250. {
  251. kgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(alias.ToString())] = mainName;
  252. }
  253. }
  254. // TODO Consider making this public
  255. internal static string GetCanonicalKeyGeneratorAlgorithm(
  256. string algorithm)
  257. {
  258. return (string) kgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm)];
  259. }
  260. // TODO Consider making this public
  261. internal static string GetCanonicalKeyPairGeneratorAlgorithm(
  262. string algorithm)
  263. {
  264. return (string)kpgAlgorithms[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm)];
  265. }
  266. public static CipherKeyGenerator GetKeyGenerator(
  267. DerObjectIdentifier oid)
  268. {
  269. return GetKeyGenerator(oid.Id);
  270. }
  271. public static CipherKeyGenerator GetKeyGenerator(
  272. string algorithm)
  273. {
  274. string canonicalName = GetCanonicalKeyGeneratorAlgorithm(algorithm);
  275. if (canonicalName == null)
  276. throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised.");
  277. int defaultKeySize = FindDefaultKeySize(canonicalName);
  278. if (defaultKeySize == -1)
  279. throw new SecurityUtilityException("KeyGenerator " + algorithm
  280. + " (" + canonicalName + ") not supported.");
  281. if (canonicalName == "DES")
  282. return new DesKeyGenerator(defaultKeySize);
  283. if (canonicalName == "DESEDE" || canonicalName == "DESEDE3")
  284. return new DesEdeKeyGenerator(defaultKeySize);
  285. return new CipherKeyGenerator(defaultKeySize);
  286. }
  287. public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(
  288. DerObjectIdentifier oid)
  289. {
  290. return GetKeyPairGenerator(oid.Id);
  291. }
  292. public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(
  293. string algorithm)
  294. {
  295. string canonicalName = GetCanonicalKeyPairGeneratorAlgorithm(algorithm);
  296. if (canonicalName == null)
  297. throw new SecurityUtilityException("KeyPairGenerator " + algorithm + " not recognised.");
  298. if (canonicalName == "DH")
  299. return new DHKeyPairGenerator();
  300. if (canonicalName == "DSA")
  301. return new DsaKeyPairGenerator();
  302. // "EC", "ECDH", "ECDHC", "ECDSA", "ECGOST3410", "ECMQV"
  303. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(canonicalName, "EC"))
  304. return new ECKeyPairGenerator(canonicalName);
  305. if (canonicalName == "Ed25519")
  306. return new Ed25519KeyPairGenerator();
  307. if (canonicalName == "Ed448")
  308. return new Ed448KeyPairGenerator();
  309. if (canonicalName == "ELGAMAL")
  310. return new ElGamalKeyPairGenerator();
  311. if (canonicalName == "GOST3410")
  312. return new Gost3410KeyPairGenerator();
  313. if (canonicalName == "RSA")
  314. return new RsaKeyPairGenerator();
  315. if (canonicalName == "X25519")
  316. return new X25519KeyPairGenerator();
  317. if (canonicalName == "X448")
  318. return new X448KeyPairGenerator();
  319. throw new SecurityUtilityException("KeyPairGenerator " + algorithm
  320. + " (" + canonicalName + ") not supported.");
  321. }
  322. internal static int GetDefaultKeySize(
  323. DerObjectIdentifier oid)
  324. {
  325. return GetDefaultKeySize(oid.Id);
  326. }
  327. internal static int GetDefaultKeySize(
  328. string algorithm)
  329. {
  330. string canonicalName = GetCanonicalKeyGeneratorAlgorithm(algorithm);
  331. if (canonicalName == null)
  332. throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised.");
  333. int defaultKeySize = FindDefaultKeySize(canonicalName);
  334. if (defaultKeySize == -1)
  335. throw new SecurityUtilityException("KeyGenerator " + algorithm
  336. + " (" + canonicalName + ") not supported.");
  337. return defaultKeySize;
  338. }
  339. private static int FindDefaultKeySize(
  340. string canonicalName)
  341. {
  342. if (!defaultKeySizes.Contains(canonicalName))
  343. return -1;
  344. return (int)defaultKeySizes[canonicalName];
  345. }
  346. }
  347. }
  348. #pragma warning restore
  349. #endif