ECDHPublicBCPGKey.cs 3.8 KB

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