BcTlsECDomain.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  13. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  14. {
  15. /**
  16. * EC domain class for generating key pairs and performing key agreement.
  17. */
  18. public class BcTlsECDomain
  19. : TlsECDomain
  20. {
  21. public static BcTlsSecret CalculateECDHAgreement(BcTlsCrypto crypto, ECPrivateKeyParameters privateKey,
  22. ECPublicKeyParameters publicKey)
  23. {
  24. ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement();
  25. basicAgreement.Init(privateKey);
  26. BigInteger agreementValue = basicAgreement.CalculateAgreement(publicKey);
  27. /*
  28. * RFC 4492 5.10. Note that this octet string (Z in IEEE 1363 terminology) as output by
  29. * FE2OSP, the Field Element to Octet String Conversion Primitive, has constant length for
  30. * any given field; leading zeros found in this octet string MUST NOT be truncated.
  31. */
  32. byte[] secret = BigIntegers.AsUnsignedByteArray(basicAgreement.GetFieldSize(), agreementValue);
  33. return crypto.AdoptLocalSecret(secret);
  34. }
  35. public static ECDomainParameters GetDomainParameters(TlsECConfig ecConfig)
  36. {
  37. return GetDomainParameters(ecConfig.NamedGroup);
  38. }
  39. public static ECDomainParameters GetDomainParameters(int namedGroup)
  40. {
  41. if (!NamedGroup.RefersToASpecificCurve(namedGroup))
  42. return null;
  43. // Parameters are lazily created the first time a particular curve is accessed
  44. string curveName = NamedGroup.GetCurveName(namedGroup);
  45. X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(curveName);
  46. if (ecP == null)
  47. return null;
  48. // It's a bit inefficient to do this conversion every time
  49. return new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());
  50. }
  51. protected readonly BcTlsCrypto m_crypto;
  52. protected readonly TlsECConfig m_config;
  53. protected readonly ECDomainParameters m_domainParameters;
  54. public BcTlsECDomain(BcTlsCrypto crypto, TlsECConfig ecConfig)
  55. {
  56. this.m_crypto = crypto;
  57. this.m_config = ecConfig;
  58. this.m_domainParameters = GetDomainParameters(ecConfig);
  59. }
  60. public virtual BcTlsSecret CalculateECDHAgreement(ECPrivateKeyParameters privateKey,
  61. ECPublicKeyParameters publicKey)
  62. {
  63. return CalculateECDHAgreement(m_crypto, privateKey, publicKey);
  64. }
  65. public virtual TlsAgreement CreateECDH()
  66. {
  67. return new BcTlsECDH(this);
  68. }
  69. public virtual ECPoint DecodePoint(byte[] encoding)
  70. {
  71. return m_domainParameters.Curve.DecodePoint(encoding);
  72. }
  73. /// <exception cref="IOException"/>
  74. public virtual ECPublicKeyParameters DecodePublicKey(byte[] encoding)
  75. {
  76. try
  77. {
  78. ECPoint point = DecodePoint(encoding);
  79. return new ECPublicKeyParameters(point, m_domainParameters);
  80. }
  81. catch (IOException e)
  82. {
  83. throw e;
  84. }
  85. catch (Exception e)
  86. {
  87. throw new TlsFatalAlert(AlertDescription.illegal_parameter, e);
  88. }
  89. }
  90. public virtual byte[] EncodePoint(ECPoint point)
  91. {
  92. return point.GetEncoded(false);
  93. }
  94. public virtual byte[] EncodePublicKey(ECPublicKeyParameters publicKey)
  95. {
  96. return EncodePoint(publicKey.Q);
  97. }
  98. public virtual AsymmetricCipherKeyPair GenerateKeyPair()
  99. {
  100. ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
  101. keyPairGenerator.Init(new ECKeyGenerationParameters(m_domainParameters, m_crypto.SecureRandom));
  102. return keyPairGenerator.GenerateKeyPair();
  103. }
  104. }
  105. }
  106. #pragma warning restore
  107. #endif