Commit Graph

1594 Commits

Author SHA1 Message Date
Rob Hague 60f3cd803e Drop DSA (#1558)
DSA is removed at compile time from OpenSSH 9.8 and higher.
That means we can no longer test it in our integration tests. It seems like a
good time to remove it. From the OpenSSH release notes:

    DSA, as specified in the SSHv2 protocol, is inherently weak - being
    limited to a 160 bit private key and use of the SHA1 digest. Its
    estimated security level is only 80 bits symmetric equivalent.

    OpenSSH has disabled DSA keys by default since 2015 but has retained
    run-time optional support for them. DSA was the only mandatory-to-
    implement algorithm in the SSHv2 RFCs, mostly because alternative
    algorithms were encumbered by patents when the SSHv2 protocol was
    specified.

    This has not been the case for decades at this point and better
    algorithms are well supported by all actively-maintained SSH
    implementations. We do not consider the costs of maintaining DSA
    in OpenSSH to be justified and hope that removing it from OpenSSH
    can accelerate its wider deprecation in supporting cryptography
    libraries.
2025-01-03 11:12:21 +01:00
Scott Xu 2e68828f61 Add support for sntrup761x25519Sha512 key exchange method (#1562) 2025-01-02 13:20:28 +01:00
Scott Xu 14c652cb45 Use System.Security.Cryptography for TripleDesCipher (#1546)
* Use System.Security.Cryptography in DesCipher and TripleDesCipher; Fall back to use BouncyCastle if BCL doesn't support

* Drop DesCipher; Replace PKCS7Padding with BouncyCastle's implementation.

* Restore `CbcCipherMode`

* Restore AesCipherMode; Use BlockImpl instead of BouncyCastleImpl for 3DES-CFB on lower targets.

* Restore the xml doc comment
2024-12-27 13:37:02 +01:00
Scott Xu 29997aebe1 Add padding when encrypt and remove padding when decrypt (#1545)
* Tighten private key checking to reveal padding issue

* `Encrypt` should take into account padding for length of `inputBuffer` passed to `EncryptBlock` if padding is specified, no matter input is divisible or not.

* `Decrypt` should take into account unpadding for the final output if padding is specified.

* `Decrypt` should take into account *manual* padding for length of `inputBuffer` passed to `DecryptBlock` and unpadding for the final output if padding is not specified and mode is CFB or OFB.

* `Encrypt` should take into account *manual* padding for length of `inputBuffer` passed to `EncryptBlock` and unpadding for the final output if padding is not specified and mode is CFB or OFB.

* Rectify DES cipher tests. There's no padding in the data.

* Borrow `PadCount` method from BouncyCastle

* Manually pad input in CTR mode as well. Update AesCipherTest.

Co-Authored-By: Rob Hague <5132141+Rob-Hague@users.noreply.github.com>

* Manually pad/unpad for Aes CFB/OFB mode

* Update test/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/AesCipherTest.Gen.cs.txt

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Re-generate AES cipher tests

---------

Co-authored-by: Rob Hague <5132141+Rob-Hague@users.noreply.github.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-12-22 16:21:02 +01:00
Rob Hague 42d75bc3d1 Drop net7.0 target (#1468)
* Drop net7.0 target

.NET 7 is EOL since May. The only .NET 7 features we use are
`ObjectDisposedException.ThrowIf` (moved to a throw helper) and
some newer regex features.

This feels a bit weird, but I suppose it is the expected course of action.

* fix build warning-as-error which is suddenly appearing on net6.0

IsAotCompatible not supported on net6.0

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-12-18 17:35:03 +00:00
Rob Hague d96b9ebc32 Fix consumption of NBGV version properties in build (#1544)
The last release was originally missing the tag in the release notes link
2024-12-18 17:34:23 +00:00
Rob Hague 021ee997b6 Pin Alpine Linux image to 3.20 (#1554) 2024-12-18 17:15:51 +00:00
Varorbc ee054f4d31 fix newline characters in PuTTY key file comment (#1550) 2024-12-04 09:49:40 +01:00
Scott Xu 3b4f2cfc1c [Private Key] Add support for PuTTY private key file format (V3 and V2) (#1543)
* [Private Key] Add support for PuTTY private key

* add negative test for mac

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-12-02 23:44:44 +01:00
mus65 70c12467dd Replace DiagnosticAbstration with Microsoft.Extensions.Logging.Abstractions (#1509)
* Replace DiagnosticAbstrations with Microsoft.Extensions.Logging.Abstractions

* add documentation

* reduce allocations by SessionId hex conversion

generate the hex string once instead of every log
call and optimize ToHex().

* Update docfx/logging.md

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* reduce log levels

* hook up testcontainers logging

* drop packet logs further down to trace

* add kex traces

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-12-02 23:15:48 +01:00
mus65 f50fdcc249 Remove appveyor.yml (#1547)
Follow-up #1539. Looks like AppVeyor has been disabled.
2024-11-30 19:12:11 +01:00
mus65 752b1db8f0 Add .NET 9 target (#1480)
* Add .NET 9 target

* Disable SonarSource S3236

This following change in the runtime now causes this analyzer
to complain about some Debug.Assert calls which doesn't make sense.

https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview7/libraries.md#debugassert-now-reports-assert-condition-by-default
https://rules.sonarsource.com/csharp/RSPEC-3236/

* make use of .NET 9 Lock type

see https://github.com/dotnet/runtime/issues/34812

* Define own Lock type to avoid ifdefs

* revert irrelevant style changes

* update global.json

* Keep net8.0 target in IntegrationTests

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* fix Package Downgrade Warning

for some reason this happens starting with .NET 9.0 RC2:

/home/mus/git/SSH.NET/test/Renci.SshNet.IntegrationTests/Renci.SshNet.IntegrationTests.csproj : error NU1605:
      Warning As Error: Detected package downgrade: BouncyCastle.Cryptography from 2.4.0 to 2.3.1. Reference the package directly from the project to select a different version.
       Renci.SshNet.IntegrationTests -> SSH.NET 1.0.0 -> BouncyCastle.Cryptography (>= 2.4.0)
       Renci.SshNet.IntegrationTests -> Testcontainers 3.10.0 -> BouncyCastle.Cryptography (>= 2.3.1)

* update global.json to RC2

* update global.json to .NET 9 GA

* update GitHub Actions for .NET 9

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-11-23 17:51:59 +01:00
mus65 f65ca44997 Migrate from AppVeyor to GitHub Actions (#1539)
* Migrate from AppVeyor to GitHub Actions

* also run on pull_request

* small formatting improvements

* add on: workflow_dispatch

this is needed to re-run jobs manually from the web UI

* Publish NuGet package to GitHub Registry

only on develop branch.

* re-add empty appveyor.yml

so it doesn't fail CI until AppVeyor integration is disabled

* fix appveyor

* typo
2024-11-23 17:04:12 +01:00
Scott Xu 3ec45e1af3 Split PrivateKeyFile into different implementations. (#1542)
* Split PrivateKeyFile into different implementations.

* Remove duplicate keyName check. Get cipherName and salt only if the key is PKCS1 format.

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-11-20 13:42:08 +01:00
Rikard Johansson bdaa16463a Handle lower-case hex in private key's salt field (#179)
* Handle lower-case hex in private key's salt field

I'm using BouncyCastle (http://bouncycastle.org/) to produce public/private key pairs. In later versions of SSH.NET an exception is thrown (SshException: "Invalid private key file.") while establishing connection using the private keys previously generated.

It seems to be an issue with the regex matching the private key file data which this commit handles properly.

* add test

---------

Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
2024-11-19 09:20:46 +01:00
Scott Xu 239fe31123 Update PrivateKeyFile.cs (#1541) 2024-11-18 22:32:13 +01:00
mus65 1d2d186a26 fix Package Downgrade Warning with .NET 9 SDK (#1538)
for some reason this happens starting with .NET 9.0 RC2:

/home/mus/git/SSH.NET/test/Renci.SshNet.IntegrationTests/Renci.SshNet.IntegrationTests.csproj : error NU1605:
      Warning As Error: Detected package downgrade: BouncyCastle.Cryptography from 2.4.0 to 2.3.1. Reference the package directly from the project to select a different version.
       Renci.SshNet.IntegrationTests -> SSH.NET 1.0.0 -> BouncyCastle.Cryptography (>= 2.4.0)
       Renci.SshNet.IntegrationTests -> Testcontainers 3.10.0 -> BouncyCastle.Cryptography (>= 2.3.1)
2024-11-13 19:52:03 +01:00
Rob Hague 6b4524efbd Set version to '2025.0.0-prerelease.{height}' 2024-11-11 21:27:08 +00:00
Rob Hague 74d4364c32 Set version to '2024.2.0' 2024.2.0 2024-11-11 21:27:08 +00:00
Rob Hague c7702f3644 Bump test dependencies (#1532) 2024-11-11 21:10:11 +01:00
Rob Hague c28f02301f Swallow ObjectDisposed on SFTP wait handle when receiving late response (#1531) 2024-11-11 21:09:54 +01:00
Rob Hague 3e12c96317 Add support for OpenSSH certificates (#1498)
Co-authored-by: cedricMicrovision <cedric.legoff@microvision.fr>
2024-10-19 14:36:41 +02:00
Rob Hague fbedaabb9c Fix sftp async methods not observing error conditions (#1510)
* Fix sftp async methods not observing error conditions

* Update ISftpClient
2024-10-08 08:48:57 +02:00
Ryan Esteves 1a8839ec3e Added support for deleting directories asynchronously (#1503)
* Added support for deleting directories asynchronously

* Clarify that the task represents the asynchronous delete operation

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Added DeleteAsync and DeleteDirectoryAsync to ISftpClient

* Inherit docs from interface

* Added additional tests for new async delete functions

* Update list directory test to use async delete methods

* x

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-26 08:55:51 +02:00
Ryan Esteves ce867d6640 Added CreateDirectoryAsync to SftpClient (#1505)
* Added CreateDirectoryAsync to SftpClient

* Use CreateDirectoryAsync for async test

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-25 21:50:27 +02:00
Ryan Esteves 737c3e5f9d Added ChangeDirectoryAsync to SftpClient (#1504)
* Add ChangeDirectoryAsync

* Added async tests for ChangeDirectoryAsync
2024-09-24 21:19:26 +02:00
mus65 28e674228f fix ConnectAsync not respecting the connection timeout (#1502) 2024-09-22 13:05:32 +02:00
Ryan Esteves 4c5d0c075e Added ExistsAsync and GetAsync to SftpClient (#1501)
* Added ExistsAsync to SftpClient

* Added GetAsync to SftpClient

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-21 18:07:37 +02:00
Eugen Cowie 82246e3cc9 Add interface to SshClient (#1499) 2024-09-21 16:24:22 +02:00
Scott Xu 548ef23e12 Add support for OpenSSL PKCS#8 private key format (#1496)
* Add support for OpenSSL PKCS#8 private key format

* Update comments

* Convert public key to ssh format

* Convert existing keys instead of generate new keys; Use DataRow for testing

* Minimize the change

* Minimize the change

* Fix build
2024-09-21 16:07:59 +02:00
mus65 3b55ba3a0a Update SonarAnalyzer.CSharp (#1494)
* Update SonarAnalyzer.CSharp

* fix S3993: Custom attributes should be marked with "System.AttributeUsageAttribute"

https://rules.sonarsource.com/csharp/RSPEC-3993/

* fix S6966: Awaitable method should be used

Introduced abstractions for CancellationTokenSource.CancelAsync()
and Stream.DisposeAsync() to avoid #ifdef.

Supressed pipeStream.WriteAsync because it deadlocks the test.
I assume because PipeStream doesn't override WriteAsync.

https://rules.sonarsource.com/csharp/RSPEC-6966/

temp

* fix S3431: "[ExpectedException]" should not be used

Removed the Connect() from Multifactor_PublicKeyWithEmptyPassPhrase
because the Exception is already thrown in the factory.

https://rules.sonarsource.com/csharp/RSPEC-3431/

* fix S2325: Methods and properties that don't access instance data should be static

This one is pretty redundant with CA1822 (which is also disabled in the
tests).

It caught a few more cases in the library itself, most of which can't
be changed because they are public API.

https://rules.sonarsource.com/csharp/RSPEC-2325/

* fix S127: "for" loop stop conditions should be invariant

not sure if this one is worth having. The only cases it found
are imho legitimate or not worth fixing, so I supressed them.

https://rules.sonarsource.com/csharp/RSPEC-127/

* fix S1964: An abstract class should have both abstract and concrete methods

Suppressed for public APIs, changed ExtendedReplyInfo to interface.

https://rules.sonarsource.com/csharp/RSPEC-1694/

* Remove redundant test

this is already covered by Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull

* Revert "fix S2325: Methods and properties that don't access instance data should be static"

suppress it instead

This reverts commit 2020604958.

* Revert "fix S127: "for" loop stop conditions should be invariant"

suppress it instead

This reverts commit 1d8b4ac335.
2024-09-19 18:56:32 +02:00
Rob Hague aac10fba3d Miscellaneous cleanup/throw helpers (#1491)
* PrivateKeyFile stuff

* Extract from net7 branch

* more ThrowHelper stuff
2024-09-19 07:53:05 +02:00
Gert Driesen 2ce54c0058 Add Directory.Packages.props to solution. 2024-09-15 16:58:22 +02:00
mus65 68e4b9ef77 Update dependencies (#1492) 2024-09-14 12:07:18 +02:00
Scott Xu f4bf62b847 Ref System.Formats.Asn1 nuget package (#1490)
* Ref System.Formats.Asn1 nuget package

* Ref System.Formats.Asn1 only for net462, netstandard2.0 and netstandard2.1
Delete ObjectIdentifier.cs

* Consolidate package reference

* 100x improvement

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-11 22:51:33 +02:00
mus65 dde5f67b2e Migrate to NuGet Central Package Management (#1488)
* Migrate to NuGet Central Package Management

* set CentralPackageVersionOverrideEnabled to false
2024-09-11 21:41:13 +02:00
Scott Xu b067e75f18 Support more ciphers for OpenSSH private key decryption. (#1487) 2024-09-10 08:47:37 +02:00
mus65 666930974d fix "client not connected" after SFTP reconnect (#1484)
* fix "client not connected" after SFTP reconnect

if the server closes the session and the client reconnects,
this currently leads to a broken state because the session
is re-created, but the SFTP subsession is not and still
references the old session.

This causes all operations to fail with "client not connected" or
even throwing the "An established connection was aborted by the server."
exception of the old session.

Always re-create the SFTP subsession to fix this.

fixes #1474

* Dispose old session on reconnect
2024-09-06 22:05:15 +02:00
Rob Hague a157dd3b88 Remove unnecessary finalizers (#1209)
All of the finalizers in the library are no-ops, but their existence means that
when Dispose (and thus GC.SuppressFinalize) is not called, the objects' lifetimes
are extended unnecessarily while they are waiting to be finalized.

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-09-05 15:33:57 +02:00
Rob Hague d06dfa5e4d Use System.Numerics.BigInteger (#1469)
Instead of the point-in-time copy
2024-09-05 13:51:57 +02:00
Scott Xu fe827a529a [ECDsa] Falls back to use BouncyCastle if BCL (Mono) doesn't support (#1461)
* Use BouncyCastle ECDsa when runtime is Mono

* Falls back to use BouncyCastle if CngKey.Import throws NotImplementedException (in Mono)

* Take NETStandard into consideration

* Adjust some comments

* Change #if NETFRAEWORK to #if NET462 for CngKey

* Separate implementations

* Consolidate Ecdsa property and HashAlgorithm property

* Rename Import_Cng and Import_Bcl to Import; Rename Export_Cng and Export_Bcl to Export;

* Add comments

* refactor

* add host key tests

---------

Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-08-31 13:00:51 +02:00
mus65 3dda5c92ce another .NET 9 SDK Build fix (#1475)
see 1b71e8fea4 and
 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1872
2024-08-24 12:50:00 +02:00
Scott Xu ebb31bb16e AesGcmCipher uses BouncyCastle as a fallback if BCL does not support. (#1450)
* [AesGcmCipher] Use BouncyCastle as a fallback if BCL does not support.

* Switch back to collection initializer

* Remove conditional compilation

* Throw SshConnectionException with Reason MacError when authentication tag mismatch

* Separate BCL and BouncyCastle implementation

* Update AesGcmCipher.BclImpl.cs

* Naming enhancement

* Remove empty line

* Disable S1199. See https://github.com/sshnet/SSH.NET/pull/1371#discussion_r1704293356

* Set InnerException when MAC error. Remove Message check.

* Store KeyParameter as private field

* Use GcmCipher.ProcessAadBytes to avoid the copy of associated data

* Move nonce to constructor to avoid creating AeadParameters each packet

* Use const int for tag size

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-08-19 14:13:10 +02:00
Rob Hague 1af01692fe Port shared tests folder (#1467)
These tests were presumably once shared with the old integration tests repo
but have since been sat doing nothing. This brings them into the unit tests
project.

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-08-12 09:33:01 +02:00
Scott Xu a067e2852a Use ExceptionDispatchInfo to retain call stack in AsyncResult.EndInvoke() (#1470) 2024-08-12 09:30:24 +02:00
Rob Hague fe65570ebc Use System.Security.Cryptography for DSA (#1458)
* Use System.Security.Cryptography for DSA

This is the analogue of the RSA change #1373 for DSA. This has a couple of caveats:

- The BCL supports only FIPS 186-compliant keys, that is, public (P, Q) lengths
  of (512 <= P <= 1024, 160) for FIPS 186-1/186-2; and (2048, 256), (3072, 256)
  for FIPS 186-3/186-4. The latter also specifies (2048, 224) but due to a quirk
  in the Windows API, the BCL does not support Q values of length 224[^1].
- OpenSSH, based on the SSH spec, only supports (supported) Q values of length 160,
  but appears to also work in non-FIPS-compliant cases such as in our integration
  tests with a (2048, 160) host key. That test now fails and I changed that host key
  to (1024, 160).

This basically means that (1024, 160) is the largest DSA key size supported by both
SSH.NET and OpenSSH. However, given that OpenSSH deprecated DSA in 2015[^2], and the
alternative that I have been considering is just to delete support for DSA in the
library, this change seems reasonable to me. I don't think we can justify keeping the
current handwritten code around.

I think we may still consider dropping DSA from the library, I just had this branch
laying around and figured I'd finish it off.

[^1]: https://github.com/dotnet/runtime/blob/fadd8313653f71abd0068c8bf914be88edb2c8d3/src/libraries/Common/src/System/Security/Cryptography/DSACng.ImportExport.cs#L259-L265
[^2]: https://www.openssh.com/txt/release-7.0

* Appease mono

* test experiment

* Revert "Appease mono"

This reverts commit 881eefe5e8.
2024-08-11 07:48:58 +02:00
Rob Hague c7a0ca93de Add another allowed SocketError value in tests (#1457)
These have started failing for me locally (Win11). SocketError.NoData means
"The requested name or IP address was not found on the name server." which
lines up with what we are testing here.
2024-08-11 07:47:42 +02:00
Rob Hague 64d67cd770 Revert mono/.NET Framework integration tests (#1462)
* Revert mono/.NET Framework integration tests

* Revert back to a subset

* x
2024-08-06 21:24:31 +02:00
Scott Xu 889f4f66ca Enable all net48 integration tests (#1456)
* Enable all net48 integration tests

* Skip ECDsa in net48 integration tests

* Stabilise tests

* Stabilise tests

* Stabilise tests

* Stabilise tests

* Spelling
2024-08-02 14:32:08 +02:00
Scott Xu 58c85ee79c Use BCL ECDiffieHellman for KeyExchange instead of BouncyCastle (.NET 8.0 onward only) (#1371)
* Use BCL ECDiffieHellman for KeyExchange (.NET 8.0 onward only)

* Add back an empty line

* Remove the BouncyCastle dependency when target .NET 8.0 onward.

* Run KeyExchangeAlgorithmTests for .NET 6.0

* Build Renci.SshNet.IntegrationTests.csproj for net6.0

* Update filter

* Add back BouncyCastle as fallback

* Add back the missing `SendMessage`

* Run ECDH KEX integration tests under .NET48

* Use SshNamedCurves instead of SecNamedCurves for BouncyCastle.
BCL supports both names. See https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/OidLookup.cs#L200-L202

* typo

* Fix build

* Use System.Security.Cryptography namespace if NET8_0_OR_GREATER;
Use one parameter constructor for class ECDomainParameters

* Separate BCL and BouncyCastle implementation

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-07-31 23:06:14 +02:00