Prechádzať zdrojové kódy

SshData.cs - Added 2 parameter checks.
SshClient.cs - Added 1 parameter check.

In SymetricCipher, added check for constructor parameter key, added documentation to all classes that inherits from this class: Block(Cipher), Aes, Arc4, Blowfish, Cast, Des, Serpent, TripleDes, Twofish and Stream.
These were found using Code Analysis: Full.

Kenneth_aa_cp 14 rokov pred
rodič
commit
20a22115f2

+ 8 - 0
Renci.SshClient/Renci.SshNet/Common/SshData.cs

@@ -354,8 +354,12 @@ namespace Renci.SshNet.Common
         /// </summary>
         /// <param name="data">string data to write.</param>
         /// <param name="encoding">String text encoding to use.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
         protected void Write(string data, Encoding encoding)
         {
+            if (data == null)
+                throw new ArgumentNullException("data");
+
             this.Write((uint)data.Length);
             this.Write(encoding.GetBytes(data));
         }
@@ -373,8 +377,12 @@ namespace Renci.SshNet.Common
         /// Writes string data into internal buffer.
         /// </summary>
         /// <param name="data">string data to write.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
         protected void WriteBinaryString(byte[] data)
         {
+            if (data == null)
+                throw new ArgumentNullException("data");
+
             this.Write((uint)data.Length);
             this._data.AddRange(data);
         }

+ 1 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/BlockCipher.cs

@@ -29,6 +29,7 @@ namespace Renci.SshNet.Security.Cryptography
         /// <param name="key">The key.</param>
         /// <param name="mode">Cipher mode.</param>
         /// <param name="padding">Cipher padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
         protected BlockCipher(byte[] key, CipherMode mode, CipherPadding padding)
             : base(key)
         {

+ 20 - 1
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Globalization;
 
 namespace Renci.SshNet.Security.Cryptography.Ciphers
 {
@@ -602,13 +603,15 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <param name="key">The key.</param>
         /// <param name="mode">The mode.</param>
         /// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
+        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
         public AesCipher(byte[] key, CipherMode mode, CipherPadding padding)
             : base(key, mode, padding)
         {
             var keySize = key.Length * 8;
 
             if (!(keySize == 256 || keySize == 192 || keySize == 128))
-                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
+                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "KeySize '{0}' is not valid for this algorithm.", keySize));
         }
 
         /// <summary>
@@ -622,8 +625,16 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <returns>
         /// The number of bytes encrypted.
         /// </returns>
+        /// <exception cref="ArgumentNullException"><paramref name="inputBuffer"/> or <paramref name="outputBuffer"/> is null.</exception>
+        /// <exception cref="IndexOutOfRangeException"><paramref name="inputBuffer"/> or <paramref name="outputBuffer"/> is too short.</exception>
         public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
         {
+            if (inputBuffer == null)
+                throw new ArgumentNullException("inputBuffer");
+
+            if (outputBuffer == null)
+                throw new ArgumentNullException("outputBuffer");
+
             if ((inputOffset + (32 / 2)) > inputBuffer.Length)
             {
                 throw new IndexOutOfRangeException("input buffer too short");
@@ -654,8 +665,16 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <returns>
         /// The number of bytes decrypted.
         /// </returns>
+        /// <exception cref="ArgumentNullException"><paramref name="inputBuffer"/> or <paramref name="outputBuffer"/> is null.</exception>
+        /// <exception cref="IndexOutOfRangeException"><paramref name="inputBuffer"/> or <paramref name="outputBuffer"/> is too short.</exception>
         public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
         {
+            if (inputBuffer == null)
+                throw new ArgumentNullException("inputBuffer");
+
+            if (outputBuffer == null)
+                throw new ArgumentNullException("outputBuffer");
+
             if ((inputOffset + (32 / 2)) > inputBuffer.Length)
             {
                 throw new IndexOutOfRangeException("input buffer too short");

+ 1 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/Arc4Cipher.cs

@@ -14,6 +14,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// Initializes a new instance of the <see cref="Arc4Cipher"/> class.
         /// </summary>
         /// <param name="key">The key.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
         public Arc4Cipher(byte[] key)
             : base(key)
         {

+ 2 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/BlowfishCipher.cs

@@ -315,6 +315,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 		/// <param name="key">The key.</param>
 		/// <param name="mode">The mode.</param>
 		/// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
+        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
 		public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
 			: base(key, mode, padding)
 		{

+ 2 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/CastCipher.cs

@@ -35,6 +35,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <param name="key">The key.</param>
         /// <param name="mode">The mode.</param>
         /// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
+        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
         public CastCipher(byte[] key, CipherMode mode, CipherPadding padding)
             : base(key, mode, padding)
         {

+ 1 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.cs

@@ -235,6 +235,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
 		/// <param name="key">The key.</param>
 		/// <param name="mode">The mode.</param>
 		/// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
 		public DesCipher(byte[] key, CipherMode mode, CipherPadding padding)
 			: base(key, mode, padding)
 		{

+ 2 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/SerpentCipher.cs

@@ -34,6 +34,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <param name="key">The key.</param>
         /// <param name="mode">The mode.</param>
         /// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
+        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
 		public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding)
 			: base(key, mode, padding)
 		{

+ 1 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/TripleDesCipher.cs

@@ -24,6 +24,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <param name="key">The key.</param>
         /// <param name="mode">The mode.</param>
         /// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
         public TripleDesCipher(byte[] key, CipherMode mode, CipherPadding padding)
             : base(key, mode, padding)
         {

+ 2 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/TwofishCipher.cs

@@ -27,6 +27,8 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// <param name="key">The key.</param>
         /// <param name="mode">The mode.</param>
         /// <param name="padding">The padding.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
+        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
 		public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
 			: base(key, mode, padding)
 		{

+ 1 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/StreamCipher.cs

@@ -14,6 +14,7 @@ namespace Renci.SshNet.Security.Cryptography
         /// Initializes a new instance of the <see cref="StreamCipher"/> class.
         /// </summary>
         /// <param name="key">The key.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
         protected StreamCipher(byte[] key)
             : base(key)
         {

+ 4 - 0
Renci.SshClient/Renci.SshNet/Security/Cryptography/SymmetricCipher.cs

@@ -19,8 +19,12 @@ namespace Renci.SshNet.Security.Cryptography
         /// Initializes a new instance of the <see cref="SymmetricCipher"/> class.
         /// </summary>
         /// <param name="key">The key.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
         protected SymmetricCipher(byte[] key)
         {
+            if (key == null)
+                throw new ArgumentNullException("key");
+
             this.Key = key;
         }
 

+ 4 - 0
Renci.SshClient/Renci.SshNet/SshClient.cs

@@ -182,8 +182,12 @@ namespace Renci.SshNet
         /// Stops and removes the forwarded port from the list.
         /// </summary>
         /// <param name="port">Forwarded port.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="port"/> is null.</exception>
         public void RemoveForwardedPort(ForwardedPort port)
         {
+            if (port == null)
+                throw new ArgumentNullException("port");
+
             //  Stop port forwarding before removing it
             port.Stop();