GeneratorUtilities.cs 17 KB

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