123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
- {
- public class RsaKeyParameters
- : AsymmetricKeyParameter
- {
- // Hexadecimal value of the product of the 131 smallest odd primes from 3 to 743
- private static readonly BigInteger SmallPrimesProduct = new BigInteger(
- "8138e8a0fcf3a4e84a771d40fd305d7f4aa59306d7251de54d98af8fe95729a1f"
- + "73d893fa424cd2edc8636a6c3285e022b0e3866a565ae8108eed8591cd4fe8d2"
- + "ce86165a978d719ebf647f362d33fca29cd179fb42401cbaf3df0c614056f9c8"
- + "f3cfd51e474afb6bc6974f78db8aba8e9e517fded658591ab7502bd41849462f",
- 16);
- private static BigInteger Validate(BigInteger modulus)
- {
- if ((modulus.IntValue & 1) == 0)
- throw new ArgumentException("RSA modulus is even", "modulus");
- if (!modulus.Gcd(SmallPrimesProduct).Equals(BigInteger.One))
- throw new ArgumentException("RSA modulus has a small prime factor");
- int maxBitLength = AsInteger("Best.HTTP.SecureProtocol.Org.BouncyCastle.Rsa.MaxSize", 15360);
- int modBitLength = modulus.BitLength;
- if (maxBitLength < modBitLength)
- {
- throw new ArgumentException("modulus value out of range");
- }
-
- // TODO: add additional primePower/Composite test - expensive!!
- return modulus;
- }
- private readonly BigInteger modulus;
- private readonly BigInteger exponent;
- public RsaKeyParameters(
- bool isPrivate,
- BigInteger modulus,
- BigInteger exponent)
- : base(isPrivate)
- {
- if (modulus == null)
- throw new ArgumentNullException("modulus");
- if (exponent == null)
- throw new ArgumentNullException("exponent");
- if (modulus.SignValue <= 0)
- throw new ArgumentException("Not a valid RSA modulus", "modulus");
- if (exponent.SignValue <= 0)
- throw new ArgumentException("Not a valid RSA exponent", "exponent");
- if (!isPrivate && (exponent.IntValue & 1) == 0)
- throw new ArgumentException("RSA publicExponent is even", "exponent");
- this.modulus = Validate(modulus);
- this.exponent = exponent;
- }
- public BigInteger Modulus
- {
- get { return modulus; }
- }
- public BigInteger Exponent
- {
- get { return exponent; }
- }
- public override bool Equals(
- object obj)
- {
- RsaKeyParameters kp = obj as RsaKeyParameters;
- if (kp == null)
- {
- return false;
- }
- return kp.IsPrivate == this.IsPrivate
- && kp.Modulus.Equals(this.modulus)
- && kp.Exponent.Equals(this.exponent);
- }
- public override int GetHashCode()
- {
- return modulus.GetHashCode() ^ exponent.GetHashCode() ^ IsPrivate.GetHashCode();
- }
- internal static int AsInteger(string envVariable, int defaultValue)
- {
- string v = Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(envVariable);
- if (v == null)
- {
- return defaultValue;
- }
- return int.Parse(v);
- }
- }
- }
- #pragma warning restore
- #endif
|