RsaKeyParameters.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  8. {
  9. public class RsaKeyParameters
  10. : AsymmetricKeyParameter
  11. {
  12. // Hexadecimal value of the product of the 131 smallest odd primes from 3 to 743
  13. private static readonly BigInteger SmallPrimesProduct = new BigInteger(
  14. "8138e8a0fcf3a4e84a771d40fd305d7f4aa59306d7251de54d98af8fe95729a1f"
  15. + "73d893fa424cd2edc8636a6c3285e022b0e3866a565ae8108eed8591cd4fe8d2"
  16. + "ce86165a978d719ebf647f362d33fca29cd179fb42401cbaf3df0c614056f9c8"
  17. + "f3cfd51e474afb6bc6974f78db8aba8e9e517fded658591ab7502bd41849462f",
  18. 16);
  19. private static BigInteger Validate(BigInteger modulus)
  20. {
  21. if ((modulus.IntValue & 1) == 0)
  22. throw new ArgumentException("RSA modulus is even", "modulus");
  23. if (!modulus.Gcd(SmallPrimesProduct).Equals(BigInteger.One))
  24. throw new ArgumentException("RSA modulus has a small prime factor");
  25. int maxBitLength = AsInteger("Best.HTTP.SecureProtocol.Org.BouncyCastle.Rsa.MaxSize", 15360);
  26. int modBitLength = modulus.BitLength;
  27. if (maxBitLength < modBitLength)
  28. {
  29. throw new ArgumentException("modulus value out of range");
  30. }
  31. // TODO: add additional primePower/Composite test - expensive!!
  32. return modulus;
  33. }
  34. private readonly BigInteger modulus;
  35. private readonly BigInteger exponent;
  36. public RsaKeyParameters(
  37. bool isPrivate,
  38. BigInteger modulus,
  39. BigInteger exponent)
  40. : base(isPrivate)
  41. {
  42. if (modulus == null)
  43. throw new ArgumentNullException("modulus");
  44. if (exponent == null)
  45. throw new ArgumentNullException("exponent");
  46. if (modulus.SignValue <= 0)
  47. throw new ArgumentException("Not a valid RSA modulus", "modulus");
  48. if (exponent.SignValue <= 0)
  49. throw new ArgumentException("Not a valid RSA exponent", "exponent");
  50. if (!isPrivate && (exponent.IntValue & 1) == 0)
  51. throw new ArgumentException("RSA publicExponent is even", "exponent");
  52. this.modulus = Validate(modulus);
  53. this.exponent = exponent;
  54. }
  55. public BigInteger Modulus
  56. {
  57. get { return modulus; }
  58. }
  59. public BigInteger Exponent
  60. {
  61. get { return exponent; }
  62. }
  63. public override bool Equals(
  64. object obj)
  65. {
  66. RsaKeyParameters kp = obj as RsaKeyParameters;
  67. if (kp == null)
  68. {
  69. return false;
  70. }
  71. return kp.IsPrivate == this.IsPrivate
  72. && kp.Modulus.Equals(this.modulus)
  73. && kp.Exponent.Equals(this.exponent);
  74. }
  75. public override int GetHashCode()
  76. {
  77. return modulus.GetHashCode() ^ exponent.GetHashCode() ^ IsPrivate.GetHashCode();
  78. }
  79. internal static int AsInteger(string envVariable, int defaultValue)
  80. {
  81. string v = Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(envVariable);
  82. if (v == null)
  83. {
  84. return defaultValue;
  85. }
  86. return int.Parse(v);
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif