BcTlsECDomain.cs 4.6 KB

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