DnssecRsaPrivateKey.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.IO;
  16. using System.Security.Cryptography;
  17. using TechnitiumLibrary.IO;
  18. using TechnitiumLibrary.Net.Dns.Dnssec;
  19. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  20. namespace DnsServerCore.Dns.Dnssec
  21. {
  22. class DnssecRsaPrivateKey : DnssecPrivateKey
  23. {
  24. #region variables
  25. int _keySize;
  26. RSAParameters _rsaPrivateKey;
  27. readonly HashAlgorithmName _hashAlgorithm;
  28. #endregion
  29. #region constructor
  30. internal DnssecRsaPrivateKey(DnssecAlgorithm algorithm, DnssecPrivateKeyType keyType, int keySize, RSAParameters rsaPrivateKey)
  31. : base(algorithm, keyType)
  32. {
  33. _keySize = keySize;
  34. _rsaPrivateKey = rsaPrivateKey;
  35. _hashAlgorithm = DnsRRSIGRecordData.GetHashAlgorithmName(algorithm);
  36. InitDnsKey();
  37. }
  38. internal DnssecRsaPrivateKey(DnssecAlgorithm algorithm, BinaryReader bR)
  39. : base(algorithm, bR)
  40. {
  41. _hashAlgorithm = DnsRRSIGRecordData.GetHashAlgorithmName(algorithm);
  42. InitDnsKey();
  43. }
  44. #endregion
  45. #region private
  46. private void InitDnsKey()
  47. {
  48. RSAParameters rsaPublicKey = new RSAParameters
  49. {
  50. Exponent = _rsaPrivateKey.Exponent,
  51. Modulus = _rsaPrivateKey.Modulus
  52. };
  53. InitDnsKey(new DnssecRsaPublicKey(rsaPublicKey));
  54. }
  55. #endregion
  56. #region protected
  57. protected override byte[] SignHash(byte[] hash)
  58. {
  59. using (RSA rsa = RSA.Create(_rsaPrivateKey))
  60. {
  61. return rsa.SignHash(hash, _hashAlgorithm, RSASignaturePadding.Pkcs1);
  62. }
  63. }
  64. protected override void ReadPrivateKeyFrom(BinaryReader bR)
  65. {
  66. _keySize = bR.ReadInt32();
  67. _rsaPrivateKey.D = bR.ReadBuffer();
  68. _rsaPrivateKey.DP = bR.ReadBuffer();
  69. _rsaPrivateKey.DQ = bR.ReadBuffer();
  70. _rsaPrivateKey.Exponent = bR.ReadBuffer();
  71. _rsaPrivateKey.InverseQ = bR.ReadBuffer();
  72. _rsaPrivateKey.Modulus = bR.ReadBuffer();
  73. _rsaPrivateKey.P = bR.ReadBuffer();
  74. _rsaPrivateKey.Q = bR.ReadBuffer();
  75. }
  76. protected override void WritePrivateKeyTo(BinaryWriter bW)
  77. {
  78. bW.Write(_keySize);
  79. bW.WriteBuffer(_rsaPrivateKey.D);
  80. bW.WriteBuffer(_rsaPrivateKey.DP);
  81. bW.WriteBuffer(_rsaPrivateKey.DQ);
  82. bW.WriteBuffer(_rsaPrivateKey.Exponent);
  83. bW.WriteBuffer(_rsaPrivateKey.InverseQ);
  84. bW.WriteBuffer(_rsaPrivateKey.Modulus);
  85. bW.WriteBuffer(_rsaPrivateKey.P);
  86. bW.WriteBuffer(_rsaPrivateKey.Q);
  87. }
  88. #endregion
  89. #region protected
  90. public int KeySize
  91. { get { return _keySize; } }
  92. #endregion
  93. }
  94. }