DnssecEcdsaPrivateKey.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.IO;
  17. using System.Security.Cryptography;
  18. using TechnitiumLibrary.IO;
  19. using TechnitiumLibrary.Net.Dns.Dnssec;
  20. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  21. namespace DnsServerCore.Dns.Dnssec
  22. {
  23. class DnssecEcdsaPrivateKey : DnssecPrivateKey
  24. {
  25. #region variables
  26. ECParameters _ecdsaPrivateKey;
  27. #endregion
  28. #region constructor
  29. internal DnssecEcdsaPrivateKey(DnssecAlgorithm algorithm, DnssecPrivateKeyType keyType, ECParameters ecdsaPrivateKey)
  30. : base(algorithm, keyType)
  31. {
  32. _ecdsaPrivateKey = ecdsaPrivateKey;
  33. InitDnsKey();
  34. }
  35. internal DnssecEcdsaPrivateKey(DnssecAlgorithm algorithm, BinaryReader bR)
  36. : base(algorithm, bR)
  37. {
  38. InitDnsKey();
  39. }
  40. #endregion
  41. #region private
  42. private void InitDnsKey()
  43. {
  44. ECParameters ecdsaPublicKey = new ECParameters
  45. {
  46. Curve = _ecdsaPrivateKey.Curve,
  47. Q = _ecdsaPrivateKey.Q
  48. };
  49. InitDnsKey(new DnssecEcdsaPublicKey(ecdsaPublicKey));
  50. }
  51. #endregion
  52. #region protected
  53. protected override byte[] SignHash(byte[] hash)
  54. {
  55. using (ECDsa ecdsa = ECDsa.Create(_ecdsaPrivateKey))
  56. {
  57. return ecdsa.SignHash(hash, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
  58. }
  59. }
  60. protected override void ReadPrivateKeyFrom(BinaryReader bR)
  61. {
  62. switch (Algorithm)
  63. {
  64. case DnssecAlgorithm.ECDSAP256SHA256:
  65. _ecdsaPrivateKey.Curve = ECCurve.NamedCurves.nistP256;
  66. break;
  67. case DnssecAlgorithm.ECDSAP384SHA384:
  68. _ecdsaPrivateKey.Curve = ECCurve.NamedCurves.nistP384;
  69. break;
  70. default:
  71. throw new NotSupportedException();
  72. }
  73. _ecdsaPrivateKey.D = bR.ReadBuffer();
  74. _ecdsaPrivateKey.Q.X = bR.ReadBuffer();
  75. _ecdsaPrivateKey.Q.Y = bR.ReadBuffer();
  76. }
  77. protected override void WritePrivateKeyTo(BinaryWriter bW)
  78. {
  79. bW.WriteBuffer(_ecdsaPrivateKey.D);
  80. bW.WriteBuffer(_ecdsaPrivateKey.Q.X);
  81. bW.WriteBuffer(_ecdsaPrivateKey.Q.Y);
  82. }
  83. #endregion
  84. }
  85. }