-
-
Notifications
You must be signed in to change notification settings - Fork 959
WIP: Implement support for rsa-sha2-256 key exchange. #971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Renci.SshNet/Security/Cryptography/RsaSha256DigitalSignature.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using Renci.SshNet.Abstractions; | ||
using Renci.SshNet.Common; | ||
using Renci.SshNet.Security.Cryptography.Ciphers; | ||
|
||
namespace Renci.SshNet.Security.Cryptography | ||
{ | ||
/// <summary> | ||
/// Implements RSA digital signature algorithm. | ||
/// </summary> | ||
public class RsaSha256DigitalSignature : CipherDigitalSignature, IDisposable | ||
{ | ||
private HashAlgorithm _hash; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RsaSha256DigitalSignature"/> class. | ||
/// </summary> | ||
/// <param name="rsaKey">The RSA key.</param> | ||
public RsaSha256DigitalSignature(RsaWithSha256SignatureKey rsaKey) | ||
: base(new ObjectIdentifier(2, 16, 840, 1, 101, 3, 4, 2, 1), new RsaCipher(rsaKey)) | ||
{ | ||
_hash = SHA256.Create(); | ||
} | ||
|
||
/// <summary> | ||
/// Hashes the specified input. | ||
/// </summary> | ||
/// <param name="input">The input.</param> | ||
/// <returns> | ||
/// Hashed data. | ||
/// </returns> | ||
protected override byte[] Hash(byte[] input) | ||
{ | ||
return _hash.ComputeHash(input); | ||
} | ||
|
||
#region IDisposable Members | ||
|
||
private bool _isDisposed; | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Releases unmanaged and - optionally - managed resources | ||
/// </summary> | ||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_isDisposed) | ||
return; | ||
|
||
if (disposing) | ||
{ | ||
var hash = _hash; | ||
if (hash != null) | ||
{ | ||
hash.Dispose(); | ||
_hash = null; | ||
} | ||
|
||
_isDisposed = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Releases unmanaged resources and performs other cleanup operations before the | ||
/// <see cref="RsaSha256DigitalSignature"/> is reclaimed by garbage collection. | ||
/// </summary> | ||
~RsaSha256DigitalSignature() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Renci.SshNet/Security/Cryptography/RsaWithSha256SignatureKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using Renci.SshNet.Common; | ||
using Renci.SshNet.Security.Cryptography; | ||
|
||
namespace Renci.SshNet.Security | ||
{ | ||
/// <summary> | ||
/// Contains RSA private and public key | ||
/// </summary> | ||
public class RsaWithSha256SignatureKey : RsaKey | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RsaWithSha256SignatureKey"/> class. | ||
/// </summary> | ||
public RsaWithSha256SignatureKey() | ||
{ } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RsaWithSha256SignatureKey"/> class. | ||
/// </summary> | ||
/// <param name="data">DER encoded private key data.</param> | ||
public RsaWithSha256SignatureKey(byte[] data) | ||
: base(data) | ||
{ | ||
if (_privateKey.Length != 8) | ||
throw new InvalidOperationException("Invalid private key."); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RsaWithSha256SignatureKey"/> class. | ||
/// </summary> | ||
/// <param name="modulus">The modulus.</param> | ||
/// <param name="exponent">The exponent.</param> | ||
/// <param name="d">The d.</param> | ||
/// <param name="p">The p.</param> | ||
/// <param name="q">The q.</param> | ||
/// <param name="inverseQ">The inverse Q.</param> | ||
public RsaWithSha256SignatureKey(BigInteger modulus, BigInteger exponent, BigInteger d, BigInteger p, BigInteger q, | ||
BigInteger inverseQ) : base(modulus, exponent, d, p, q, inverseQ) | ||
{ | ||
} | ||
|
||
private RsaSha256DigitalSignature _digitalSignature; | ||
|
||
/// <summary> | ||
/// Gets the digital signature. | ||
/// </summary> | ||
protected override DigitalSignature DigitalSignature | ||
{ | ||
get | ||
{ | ||
if (_digitalSignature == null) | ||
{ | ||
_digitalSignature = new RsaSha256DigitalSignature(this); | ||
} | ||
|
||
return _digitalSignature; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Key String. | ||
/// </summary> | ||
public override string ToString() | ||
{ | ||
return "rsa-sha2-256"; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be a pretty big change for the current codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any guidance @darinkes ?