| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Net.Sockets; |
| | | 7 | | using System.Text; |
| | | 8 | | using Renci.SshNet.Abstractions; |
| | | 9 | | using Renci.SshNet.Messages; |
| | | 10 | | |
| | | 11 | | namespace Renci.SshNet.Common |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Collection of different extension methods. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static partial class Extensions |
| | | 17 | | { |
| | | 18 | | internal static byte[] ToArray(this ServiceName serviceName) |
| | 7677 | 19 | | { |
| | 7677 | 20 | | switch (serviceName) |
| | | 21 | | { |
| | | 22 | | case ServiceName.UserAuthentication: |
| | 2428 | 23 | | return SshData.Ascii.GetBytes("ssh-userauth"); |
| | | 24 | | case ServiceName.Connection: |
| | 5249 | 25 | | return SshData.Ascii.GetBytes("ssh-connection"); |
| | | 26 | | default: |
| | 0 | 27 | | throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", serviceName)); |
| | | 28 | | } |
| | 7677 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static ServiceName ToServiceName(this byte[] data) |
| | 1813 | 32 | | { |
| | 1813 | 33 | | var sshServiceName = SshData.Ascii.GetString(data, 0, data.Length); |
| | 1813 | 34 | | switch (sshServiceName) |
| | | 35 | | { |
| | | 36 | | case "ssh-userauth": |
| | 1813 | 37 | | return ServiceName.UserAuthentication; |
| | | 38 | | case "ssh-connection": |
| | 0 | 39 | | return ServiceName.Connection; |
| | | 40 | | default: |
| | 0 | 41 | | throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", sshServiceName |
| | | 42 | | } |
| | 1813 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal static BigInteger ToBigInteger(this byte[] data) |
| | 3652 | 46 | | { |
| | 3652 | 47 | | var reversed = new byte[data.Length]; |
| | 3652 | 48 | | Buffer.BlockCopy(data, 0, reversed, 0, data.Length); |
| | 3652 | 49 | | return new BigInteger(reversed.Reverse()); |
| | 3652 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format. |
| | | 54 | | /// </summary> |
| | | 55 | | public static BigInteger ToBigInteger2(this byte[] data) |
| | 3622 | 56 | | { |
| | 3622 | 57 | | if ((data[0] & (1 << 7)) != 0) |
| | 621 | 58 | | { |
| | 621 | 59 | | var buf = new byte[data.Length + 1]; |
| | 621 | 60 | | Buffer.BlockCopy(data, 0, buf, 1, data.Length); |
| | 621 | 61 | | data = buf; |
| | 621 | 62 | | } |
| | | 63 | | |
| | 3622 | 64 | | return data.ToBigInteger(); |
| | 3622 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="array">The one-dimensional <see cref="Array"/> to reverse.</param> |
| | | 71 | | /// <returns> |
| | | 72 | | /// The <see cref="Array"/> with its elements reversed. |
| | | 73 | | /// </returns> |
| | | 74 | | internal static T[] Reverse<T>(this T[] array) |
| | 16769 | 75 | | { |
| | 16769 | 76 | | Array.Reverse(array); |
| | 16766 | 77 | | return array; |
| | 16766 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Prints out the specified bytes. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="bytes">The bytes.</param> |
| | | 84 | | internal static void DebugPrint(this IEnumerable<byte> bytes) |
| | 3 | 85 | | { |
| | 3 | 86 | | var sb = new StringBuilder(); |
| | | 87 | | |
| | 783 | 88 | | foreach (var b in bytes) |
| | 387 | 89 | | { |
| | 387 | 90 | | _ = sb.AppendFormat(CultureInfo.CurrentCulture, "0x{0:x2}, ", b); |
| | 387 | 91 | | } |
| | | 92 | | |
| | 3 | 93 | | Debug.WriteLine(sb.ToString()); |
| | 3 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Creates an instance of the specified type using that type's default constructor. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <typeparam name="T">The type to create.</typeparam> |
| | | 100 | | /// <param name="type">Type of the instance to create.</param> |
| | | 101 | | /// <returns>A reference to the newly created object.</returns> |
| | | 102 | | internal static T CreateInstance<T>(this Type type) |
| | | 103 | | where T : class |
| | 1199 | 104 | | { |
| | 1199 | 105 | | if (type is null) |
| | 0 | 106 | | { |
| | 0 | 107 | | return null; |
| | | 108 | | } |
| | | 109 | | |
| | 1199 | 110 | | return Activator.CreateInstance(type) as T; |
| | 1199 | 111 | | } |
| | | 112 | | |
| | | 113 | | internal static void ValidatePort(this uint value, string argument) |
| | 917 | 114 | | { |
| | 917 | 115 | | if (value > IPEndPoint.MaxPort) |
| | 3 | 116 | | { |
| | 3 | 117 | | throw new ArgumentOutOfRangeException(argument, |
| | 3 | 118 | | string.Format(CultureInfo.InvariantCulture, "Specified value canno |
| | | 119 | | } |
| | 914 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal static void ValidatePort(this int value, string argument) |
| | 3272 | 123 | | { |
| | 3272 | 124 | | if (value < IPEndPoint.MinPort) |
| | 9 | 125 | | { |
| | 9 | 126 | | throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified v |
| | | 127 | | } |
| | | 128 | | |
| | 3263 | 129 | | if (value > IPEndPoint.MaxPort) |
| | 9 | 130 | | { |
| | 9 | 131 | | throw new ArgumentOutOfRangeException(argument, string.Format(CultureInfo.InvariantCulture, "Specified v |
| | | 132 | | } |
| | 3254 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Returns a specified number of contiguous bytes from a given offset. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="value">The array to return a number of bytes from.</param> |
| | | 139 | | /// <param name="offset">The zero-based offset in <paramref name="value"/> at which to begin taking bytes.</para |
| | | 140 | | /// <param name="count">The number of bytes to take from <paramref name="value"/>.</param> |
| | | 141 | | /// <returns> |
| | | 142 | | /// A <see cref="byte"/> array that contains the specified number of bytes at the specified offset |
| | | 143 | | /// of the input array. |
| | | 144 | | /// </returns> |
| | | 145 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> |
| | | 146 | | /// <remarks> |
| | | 147 | | /// When <paramref name="offset"/> is zero and <paramref name="count"/> equals the length of <paramref name="val |
| | | 148 | | /// then <paramref name="value"/> is returned. |
| | | 149 | | /// </remarks> |
| | | 150 | | public static byte[] Take(this byte[] value, int offset, int count) |
| | 52979 | 151 | | { |
| | 52979 | 152 | | if (value is null) |
| | 3 | 153 | | { |
| | 3 | 154 | | throw new ArgumentNullException(nameof(value)); |
| | | 155 | | } |
| | | 156 | | |
| | 52976 | 157 | | if (count == 0) |
| | 22 | 158 | | { |
| | 22 | 159 | | return Array.Empty<byte>(); |
| | | 160 | | } |
| | | 161 | | |
| | 52954 | 162 | | if (offset == 0 && value.Length == count) |
| | 69 | 163 | | { |
| | 69 | 164 | | return value; |
| | | 165 | | } |
| | | 166 | | |
| | 52885 | 167 | | var taken = new byte[count]; |
| | 52885 | 168 | | Buffer.BlockCopy(value, offset, taken, 0, count); |
| | 52879 | 169 | | return taken; |
| | 52970 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Returns a specified number of contiguous bytes from the start of the specified byte array. |
| | | 174 | | /// </summary> |
| | | 175 | | /// <param name="value">The array to return a number of bytes from.</param> |
| | | 176 | | /// <param name="count">The number of bytes to take from <paramref name="value"/>.</param> |
| | | 177 | | /// <returns> |
| | | 178 | | /// A <see cref="byte"/> array that contains the specified number of bytes at the start of the input array. |
| | | 179 | | /// </returns> |
| | | 180 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> |
| | | 181 | | /// <remarks> |
| | | 182 | | /// When <paramref name="count"/> equals the length of <paramref name="value"/>, then <paramref name="value"/> |
| | | 183 | | /// is returned. |
| | | 184 | | /// </remarks> |
| | | 185 | | public static byte[] Take(this byte[] value, int count) |
| | 5344 | 186 | | { |
| | 5344 | 187 | | if (value is null) |
| | 3 | 188 | | { |
| | 3 | 189 | | throw new ArgumentNullException(nameof(value)); |
| | | 190 | | } |
| | | 191 | | |
| | 5341 | 192 | | if (count == 0) |
| | 3 | 193 | | { |
| | 3 | 194 | | return Array.Empty<byte>(); |
| | | 195 | | } |
| | | 196 | | |
| | 5338 | 197 | | if (value.Length == count) |
| | 308 | 198 | | { |
| | 308 | 199 | | return value; |
| | | 200 | | } |
| | | 201 | | |
| | 5030 | 202 | | var taken = new byte[count]; |
| | 5030 | 203 | | Buffer.BlockCopy(value, 0, taken, 0, count); |
| | 5027 | 204 | | return taken; |
| | 5338 | 205 | | } |
| | | 206 | | |
| | | 207 | | public static bool IsEqualTo(this byte[] left, byte[] right) |
| | 54567 | 208 | | { |
| | 54567 | 209 | | if (left is null) |
| | 6 | 210 | | { |
| | 6 | 211 | | throw new ArgumentNullException(nameof(left)); |
| | | 212 | | } |
| | | 213 | | |
| | 54561 | 214 | | if (right is null) |
| | 3 | 215 | | { |
| | 3 | 216 | | throw new ArgumentNullException(nameof(right)); |
| | | 217 | | } |
| | | 218 | | |
| | 54558 | 219 | | if (left == right) |
| | 3 | 220 | | { |
| | 3 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | 54555 | 224 | | if (left.Length != right.Length) |
| | 12 | 225 | | { |
| | 12 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | 2569688 | 229 | | for (var i = 0; i < left.Length; i++) |
| | 1230301 | 230 | | { |
| | 1230301 | 231 | | if (left[i] != right[i]) |
| | 0 | 232 | | { |
| | 0 | 233 | | return false; |
| | | 234 | | } |
| | 1230301 | 235 | | } |
| | | 236 | | |
| | 54543 | 237 | | return true; |
| | 54558 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Trims the leading zero from a byte array. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <param name="value">The value.</param> |
| | | 244 | | /// <returns> |
| | | 245 | | /// <paramref name="value"/> without leading zeros. |
| | | 246 | | /// </returns> |
| | | 247 | | public static byte[] TrimLeadingZeros(this byte[] value) |
| | 812 | 248 | | { |
| | 812 | 249 | | if (value is null) |
| | 3 | 250 | | { |
| | 3 | 251 | | throw new ArgumentNullException(nameof(value)); |
| | | 252 | | } |
| | | 253 | | |
| | 2122 | 254 | | for (var i = 0; i < value.Length; i++) |
| | 1058 | 255 | | { |
| | 1058 | 256 | | if (value[i] == 0) |
| | 252 | 257 | | { |
| | 252 | 258 | | continue; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | // if the first byte is non-zero, then we return the byte array as is |
| | 806 | 262 | | if (i == 0) |
| | 557 | 263 | | { |
| | 557 | 264 | | return value; |
| | | 265 | | } |
| | | 266 | | |
| | 249 | 267 | | var remainingBytes = value.Length - i; |
| | | 268 | | |
| | 249 | 269 | | var cleaned = new byte[remainingBytes]; |
| | 249 | 270 | | Buffer.BlockCopy(value, i, cleaned, 0, remainingBytes); |
| | 249 | 271 | | return cleaned; |
| | | 272 | | } |
| | | 273 | | |
| | 3 | 274 | | return value; |
| | 809 | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Pads with leading zeros if needed. |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="data">The data.</param> |
| | | 281 | | /// <param name="length">The length to pad to.</param> |
| | | 282 | | public static byte[] Pad(this byte[] data, int length) |
| | 59 | 283 | | { |
| | 59 | 284 | | if (length <= data.Length) |
| | 49 | 285 | | { |
| | 49 | 286 | | return data; |
| | | 287 | | } |
| | | 288 | | |
| | 10 | 289 | | var newData = new byte[length]; |
| | 10 | 290 | | Buffer.BlockCopy(data, 0, newData, newData.Length - data.Length, data.Length); |
| | 10 | 291 | | return newData; |
| | 59 | 292 | | } |
| | | 293 | | |
| | | 294 | | public static byte[] Concat(this byte[] first, byte[] second) |
| | 86 | 295 | | { |
| | 86 | 296 | | if (first is null || first.Length == 0) |
| | 9 | 297 | | { |
| | 9 | 298 | | return second; |
| | | 299 | | } |
| | | 300 | | |
| | 77 | 301 | | if (second is null || second.Length == 0) |
| | 6 | 302 | | { |
| | 6 | 303 | | return first; |
| | | 304 | | } |
| | | 305 | | |
| | 71 | 306 | | var concat = new byte[first.Length + second.Length]; |
| | 71 | 307 | | Buffer.BlockCopy(first, 0, concat, 0, first.Length); |
| | 71 | 308 | | Buffer.BlockCopy(second, 0, concat, first.Length, second.Length); |
| | 71 | 309 | | return concat; |
| | 86 | 310 | | } |
| | | 311 | | |
| | | 312 | | internal static bool CanRead(this Socket socket) |
| | 0 | 313 | | { |
| | 0 | 314 | | return SocketAbstraction.CanRead(socket); |
| | 0 | 315 | | } |
| | | 316 | | |
| | | 317 | | internal static bool CanWrite(this Socket socket) |
| | 53126 | 318 | | { |
| | 53126 | 319 | | return SocketAbstraction.CanWrite(socket); |
| | 53126 | 320 | | } |
| | | 321 | | |
| | | 322 | | internal static bool IsConnected(this Socket socket) |
| | 57530 | 323 | | { |
| | 57530 | 324 | | if (socket is null) |
| | 50 | 325 | | { |
| | 50 | 326 | | return false; |
| | | 327 | | } |
| | | 328 | | |
| | 57480 | 329 | | return socket.Connected; |
| | 57530 | 330 | | } |
| | | 331 | | } |
| | | 332 | | } |