| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | #if NETFRAMEWORK |
| | | 4 | | using System.Security.Cryptography; |
| | | 5 | | #endif // NETFRAMEWORK |
| | | 6 | | |
| | | 7 | | using Renci.SshNet.Common; |
| | | 8 | | |
| | | 9 | | namespace Renci.SshNet.Security.Cryptography |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Implements ECDSA digital signature algorithm. |
| | | 13 | | /// </summary> |
| | | 14 | | public class EcdsaDigitalSignature : DigitalSignature, IDisposable |
| | | 15 | | { |
| | | 16 | | private readonly EcdsaKey _key; |
| | | 17 | | private bool _isDisposed; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="EcdsaDigitalSignature" /> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="key">The ECDSA key.</param> |
| | | 23 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | 39 | 24 | | public EcdsaDigitalSignature(EcdsaKey key) |
| | 39 | 25 | | { |
| | 39 | 26 | | if (key is null) |
| | 0 | 27 | | { |
| | 0 | 28 | | throw new ArgumentNullException(nameof(key)); |
| | | 29 | | } |
| | | 30 | | |
| | 39 | 31 | | _key = key; |
| | 39 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Verifies the signature. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="input">The input.</param> |
| | | 38 | | /// <param name="signature">The signature.</param> |
| | | 39 | | /// <returns> |
| | | 40 | | /// <see langword="true"/> if signature was successfully verified; otherwise <see langword="false"/>. |
| | | 41 | | /// </returns> |
| | | 42 | | public override bool Verify(byte[] input, byte[] signature) |
| | 0 | 43 | | { |
| | | 44 | | // for 521 sig_size is 132 |
| | 0 | 45 | | var sig_size = _key.KeyLength == 521 ? 132 : _key.KeyLength / 4; |
| | 0 | 46 | | var ssh_data = new SshDataSignature(signature, sig_size); |
| | | 47 | | #if NETFRAMEWORK |
| | 0 | 48 | | var ecdsa = (ECDsaCng)_key.Ecdsa; |
| | 0 | 49 | | ecdsa.HashAlgorithm = _key.HashAlgorithm; |
| | 0 | 50 | | return ecdsa.VerifyData(input, ssh_data.Signature); |
| | | 51 | | #else |
| | 0 | 52 | | return _key.Ecdsa.VerifyData(input, ssh_data.Signature, _key.HashAlgorithm); |
| | | 53 | | #endif |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates the signature. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="input">The input.</param> |
| | | 60 | | /// <returns> |
| | | 61 | | /// Signed input data. |
| | | 62 | | /// </returns> |
| | | 63 | | public override byte[] Sign(byte[] input) |
| | 3 | 64 | | { |
| | | 65 | | #if NETFRAMEWORK |
| | 0 | 66 | | var ecdsa = (ECDsaCng)_key.Ecdsa; |
| | 0 | 67 | | ecdsa.HashAlgorithm = _key.HashAlgorithm; |
| | 0 | 68 | | var signed = ecdsa.SignData(input); |
| | | 69 | | #else |
| | 3 | 70 | | var signed = _key.Ecdsa.SignData(input, _key.HashAlgorithm); |
| | | 71 | | #endif |
| | 3 | 72 | | var ssh_data = new SshDataSignature(signed.Length) { Signature = signed }; |
| | 3 | 73 | | return ssh_data.GetBytes(); |
| | 3 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 78 | | /// </summary> |
| | | 79 | | public void Dispose() |
| | 0 | 80 | | { |
| | 0 | 81 | | Dispose(disposing: true); |
| | 0 | 82 | | GC.SuppressFinalize(this); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 89 | | protected virtual void Dispose(bool disposing) |
| | 39 | 90 | | { |
| | 39 | 91 | | if (_isDisposed) |
| | 0 | 92 | | { |
| | 0 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | 39 | 96 | | if (disposing) |
| | 0 | 97 | | { |
| | 0 | 98 | | _isDisposed = true; |
| | 0 | 99 | | } |
| | 39 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Finalizes an instance of the <see cref="EcdsaDigitalSignature"/> class. |
| | | 104 | | /// </summary> |
| | | 105 | | ~EcdsaDigitalSignature() |
| | 78 | 106 | | { |
| | 39 | 107 | | Dispose(disposing: false); |
| | 78 | 108 | | } |
| | | 109 | | |
| | | 110 | | private sealed class SshDataSignature : SshData |
| | | 111 | | { |
| | | 112 | | private readonly int _signature_size; |
| | | 113 | | |
| | | 114 | | private byte[] _signature_r; |
| | | 115 | | private byte[] _signature_s; |
| | | 116 | | |
| | | 117 | | public byte[] Signature |
| | | 118 | | { |
| | | 119 | | get |
| | 0 | 120 | | { |
| | 0 | 121 | | var signature = new byte[_signature_size]; |
| | 0 | 122 | | Buffer.BlockCopy(_signature_r, 0, signature, 0, _signature_r.Length); |
| | 0 | 123 | | Buffer.BlockCopy(_signature_s, 0, signature, _signature_r.Length, _signature_s.Length); |
| | 0 | 124 | | return signature; |
| | 0 | 125 | | } |
| | | 126 | | set |
| | 3 | 127 | | { |
| | 3 | 128 | | var signed_r = new byte[_signature_size / 2]; |
| | 3 | 129 | | Buffer.BlockCopy(value, 0, signed_r, 0, signed_r.Length); |
| | 3 | 130 | | _signature_r = signed_r.ToBigInteger2().ToByteArray().Reverse(); |
| | | 131 | | |
| | 3 | 132 | | var signed_s = new byte[_signature_size / 2]; |
| | 3 | 133 | | Buffer.BlockCopy(value, signed_r.Length, signed_s, 0, signed_s.Length); |
| | 3 | 134 | | _signature_s = signed_s.ToBigInteger2().ToByteArray().Reverse(); |
| | 3 | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 3 | 138 | | public SshDataSignature(int sig_size) |
| | 3 | 139 | | { |
| | 3 | 140 | | _signature_size = sig_size; |
| | 3 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | public SshDataSignature(byte[] data, int sig_size) |
| | 0 | 144 | | { |
| | 0 | 145 | | _signature_size = sig_size; |
| | 0 | 146 | | Load(data); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | protected override void LoadData() |
| | 0 | 150 | | { |
| | 0 | 151 | | _signature_r = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2); |
| | 0 | 152 | | _signature_s = ReadBinary().TrimLeadingZeros().Pad(_signature_size / 2); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | protected override void SaveData() |
| | 3 | 156 | | { |
| | 3 | 157 | | WriteBinaryString(_signature_r.ToBigInteger2().ToByteArray().Reverse()); |
| | 3 | 158 | | WriteBinaryString(_signature_s.ToBigInteger2().ToByteArray().Reverse()); |
| | 3 | 159 | | } |
| | | 160 | | |
| | | 161 | | public new byte[] ReadBinary() |
| | 0 | 162 | | { |
| | 0 | 163 | | var length = ReadUInt32(); |
| | | 164 | | |
| | 0 | 165 | | if (length > int.MaxValue) |
| | 0 | 166 | | { |
| | 0 | 167 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} i |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return ReadBytes((int) length); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | protected override int BufferCapacity |
| | | 174 | | { |
| | | 175 | | get |
| | 3 | 176 | | { |
| | 3 | 177 | | var capacity = base.BufferCapacity; |
| | 3 | 178 | | capacity += 4; // r length |
| | 3 | 179 | | capacity += _signature_r.Length; // signature r |
| | 3 | 180 | | capacity += 4; // s length |
| | 3 | 181 | | capacity += _signature_s.Length; // signature s |
| | 3 | 182 | | return capacity; |
| | 3 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | } |