From 2a75d150f295407a47bb4bbe5698c8e0c36b4a44 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 19 Feb 2022 12:47:23 +0530 Subject: [PATCH] added DnssecEcdsaPrivateKey --- .../Dns/Dnssec/DnssecEcdsaPrivateKey.cs | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 DnsServerCore/Dns/Dnssec/DnssecEcdsaPrivateKey.cs diff --git a/DnsServerCore/Dns/Dnssec/DnssecEcdsaPrivateKey.cs b/DnsServerCore/Dns/Dnssec/DnssecEcdsaPrivateKey.cs new file mode 100644 index 00000000..7c7be18f --- /dev/null +++ b/DnsServerCore/Dns/Dnssec/DnssecEcdsaPrivateKey.cs @@ -0,0 +1,110 @@ +/* +Technitium DNS Server +Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +*/ + +using System; +using System.IO; +using System.Security.Cryptography; +using TechnitiumLibrary.IO; +using TechnitiumLibrary.Net.Dns.Dnssec; +using TechnitiumLibrary.Net.Dns.ResourceRecords; + +namespace DnsServerCore.Dns.Dnssec +{ + class DnssecEcdsaPrivateKey : DnssecPrivateKey + { + #region variables + + ECParameters _ecdsaPrivateKey; + + #endregion + + #region constructor + + internal DnssecEcdsaPrivateKey(DnssecAlgorithm algorithm, DnssecPrivateKeyType keyType, ECParameters ecdsaPrivateKey) + : base(algorithm, keyType) + { + _ecdsaPrivateKey = ecdsaPrivateKey; + + InitDnsKey(); + } + + internal DnssecEcdsaPrivateKey(DnssecAlgorithm algorithm, BinaryReader bR) + : base(algorithm, bR) + { + InitDnsKey(); + } + + #endregion + + #region private + + private void InitDnsKey() + { + ECParameters ecdsaPublicKey = new ECParameters + { + Curve = _ecdsaPrivateKey.Curve, + Q = _ecdsaPrivateKey.Q + }; + + InitDnsKey(new DnssecEcdsaPublicKey(ecdsaPublicKey)); + } + + #endregion + + #region protected + + protected override byte[] SignHash(byte[] hash) + { + using (ECDsa ecdsa = ECDsa.Create(_ecdsaPrivateKey)) + { + return ecdsa.SignHash(hash, DSASignatureFormat.IeeeP1363FixedFieldConcatenation); + } + } + + protected override void ReadPrivateKeyFrom(BinaryReader bR) + { + switch (Algorithm) + { + case DnssecAlgorithm.ECDSAP256SHA256: + _ecdsaPrivateKey.Curve = ECCurve.NamedCurves.nistP256; + break; + + case DnssecAlgorithm.ECDSAP384SHA384: + _ecdsaPrivateKey.Curve = ECCurve.NamedCurves.nistP384; + break; + + default: + throw new NotSupportedException(); + } + + _ecdsaPrivateKey.D = bR.ReadBuffer(); + _ecdsaPrivateKey.Q.X = bR.ReadBuffer(); + _ecdsaPrivateKey.Q.Y = bR.ReadBuffer(); + } + + protected override void WritePrivateKeyTo(BinaryWriter bW) + { + bW.WriteBuffer(_ecdsaPrivateKey.D); + bW.WriteBuffer(_ecdsaPrivateKey.Q.X); + bW.WriteBuffer(_ecdsaPrivateKey.Q.Y); + } + + #endregion + } +}