ECDHPublicBCPGKey.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  7. {
  8. /// <remarks>Base class for an ECDH Public Key.</remarks>
  9. public class ECDHPublicBcpgKey
  10. : ECPublicBcpgKey
  11. {
  12. private byte reserved;
  13. private HashAlgorithmTag hashFunctionId;
  14. private SymmetricKeyAlgorithmTag symAlgorithmId;
  15. /// <param name="bcpgIn">The stream to read the packet from.</param>
  16. public ECDHPublicBcpgKey(
  17. BcpgInputStream bcpgIn)
  18. : base(bcpgIn)
  19. {
  20. int length = bcpgIn.ReadByte();
  21. byte[] kdfParameters = new byte[length];
  22. if (kdfParameters.Length != 3)
  23. throw new InvalidOperationException("kdf parameters size of 3 expected.");
  24. bcpgIn.ReadFully(kdfParameters);
  25. reserved = kdfParameters[0];
  26. hashFunctionId = (HashAlgorithmTag)kdfParameters[1];
  27. symAlgorithmId = (SymmetricKeyAlgorithmTag)kdfParameters[2];
  28. VerifyHashAlgorithm();
  29. VerifySymmetricKeyAlgorithm();
  30. }
  31. public ECDHPublicBcpgKey(
  32. DerObjectIdentifier oid,
  33. ECPoint point,
  34. HashAlgorithmTag hashAlgorithm,
  35. SymmetricKeyAlgorithmTag symmetricKeyAlgorithm)
  36. : base(oid, point)
  37. {
  38. reserved = 1;
  39. hashFunctionId = hashAlgorithm;
  40. symAlgorithmId = symmetricKeyAlgorithm;
  41. VerifyHashAlgorithm();
  42. VerifySymmetricKeyAlgorithm();
  43. }
  44. public virtual byte Reserved
  45. {
  46. get { return reserved; }
  47. }
  48. public virtual HashAlgorithmTag HashAlgorithm
  49. {
  50. get { return hashFunctionId; }
  51. }
  52. public virtual SymmetricKeyAlgorithmTag SymmetricKeyAlgorithm
  53. {
  54. get { return symAlgorithmId; }
  55. }
  56. public override void Encode(
  57. BcpgOutputStream bcpgOut)
  58. {
  59. base.Encode(bcpgOut);
  60. bcpgOut.WriteByte(0x3);
  61. bcpgOut.WriteByte(reserved);
  62. bcpgOut.WriteByte((byte)hashFunctionId);
  63. bcpgOut.WriteByte((byte)symAlgorithmId);
  64. }
  65. private void VerifyHashAlgorithm()
  66. {
  67. switch ((HashAlgorithmTag)hashFunctionId)
  68. {
  69. case HashAlgorithmTag.Sha256:
  70. case HashAlgorithmTag.Sha384:
  71. case HashAlgorithmTag.Sha512:
  72. break;
  73. default:
  74. throw new InvalidOperationException("Hash algorithm must be SHA-256 or stronger.");
  75. }
  76. }
  77. private void VerifySymmetricKeyAlgorithm()
  78. {
  79. switch ((SymmetricKeyAlgorithmTag)symAlgorithmId)
  80. {
  81. case SymmetricKeyAlgorithmTag.Aes128:
  82. case SymmetricKeyAlgorithmTag.Aes192:
  83. case SymmetricKeyAlgorithmTag.Aes256:
  84. break;
  85. default:
  86. throw new InvalidOperationException("Symmetric key algorithm must be AES-128 or stronger.");
  87. }
  88. }
  89. }
  90. }
  91. #pragma warning restore
  92. #endif