-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ed25519 private keys (#212)
Adds support for using ed25519 private key in user authentication. As .NET does not support ED25519 in the BCL it uses BouncyCastle.Cryptography as a dependency for the key signing tasks.
- Loading branch information
Showing
13 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,63 @@ | ||
// This file is part of Tmds.Ssh which is released under MIT. | ||
// See file LICENSE for full license details. | ||
|
||
using System.Buffers; | ||
using Org.BouncyCastle.Math.EC.Rfc8032; | ||
|
||
namespace Tmds.Ssh; | ||
|
||
sealed class Ed25519PrivateKey : PrivateKey | ||
{ | ||
// Contains the private and public key as one block of bytes from the | ||
// serialized OpenSSH key data. | ||
private readonly byte[] _privateKey; | ||
private readonly byte[] _publicKey; | ||
|
||
public Ed25519PrivateKey(byte[] privateKey, byte[] publicKey) : | ||
base([AlgorithmNames.SshEd25519]) | ||
{ | ||
_privateKey = privateKey; | ||
_publicKey = publicKey; | ||
} | ||
|
||
public override void Dispose() | ||
{ } | ||
|
||
public override void AppendPublicKey(ref SequenceWriter writer) | ||
{ | ||
using var innerData = writer.SequencePool.RentSequence(); | ||
var innerWriter = new SequenceWriter(innerData); | ||
innerWriter.WriteString(Algorithms[0]); | ||
innerWriter.WriteString(_publicKey); | ||
|
||
writer.WriteString(innerData.AsReadOnlySequence()); | ||
} | ||
|
||
public override void AppendSignature(Name algorithm, ref SequenceWriter writer, ReadOnlySequence<byte> data) | ||
{ | ||
if (algorithm != Algorithms[0]) | ||
{ | ||
ThrowHelper.ThrowProtocolUnexpectedValue(); | ||
return; | ||
} | ||
|
||
byte[] signature = new byte[Ed25519.SignatureSize]; | ||
Ed25519.Sign( | ||
_privateKey, | ||
0, | ||
_publicKey, | ||
0, | ||
data.ToArray(), | ||
0, | ||
(int)data.Length, | ||
signature, | ||
0); | ||
|
||
using var innerData = writer.SequencePool.RentSequence(); | ||
var innerWriter = new SequenceWriter(innerData); | ||
innerWriter.WriteString(algorithm); | ||
innerWriter.WriteString(signature); | ||
|
||
writer.WriteString(innerData.AsReadOnlySequence()); | ||
} | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ public void Defaults() | |
Assert.Null(settings.HostAuthentication); | ||
Assert.Equal(new[] { new Name("ecdh-sha2-nistp256"), new Name("ecdh-sha2-nistp384"), new Name("ecdh-sha2-nistp521") }, settings.KeyExchangeAlgorithms); | ||
Assert.Equal(new[] { new Name("ecdsa-sha2-nistp521"), new Name("ecdsa-sha2-nistp384"), new Name("ecdsa-sha2-nistp256"), new Name("rsa-sha2-512"), new Name("rsa-sha2-256") }, settings.ServerHostKeyAlgorithms); | ||
Assert.Equal(new[] { new Name("ecdsa-sha2-nistp521"), new Name("ecdsa-sha2-nistp384"), new Name("ecdsa-sha2-nistp256"), new Name("rsa-sha2-512"), new Name("rsa-sha2-256") }, settings.PublicKeyAcceptedAlgorithms); | ||
Assert.Equal(new[] { new Name("ssh-ed25519"), new Name("ecdsa-sha2-nistp521"), new Name("ecdsa-sha2-nistp384"), new Name("ecdsa-sha2-nistp256"), new Name("rsa-sha2-512"), new Name("rsa-sha2-256") }, settings.PublicKeyAcceptedAlgorithms); | ||
Assert.Equal(new[] { new Name("[email protected]"), new Name("[email protected]") }, settings.EncryptionAlgorithmsClientToServer); | ||
Assert.Equal(new[] { new Name("[email protected]"), new Name("[email protected]") }, settings.EncryptionAlgorithmsServerToClient); | ||
Assert.Equal(Array.Empty<Name>(), settings.MacAlgorithmsClientToServer); | ||
|
This file contains 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 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 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,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACDBGs/6MWZXV20KtacQson8/EyafCK7KDCh0ZH8LBgvYQAAAKA1mv4HNZr+ | ||
BwAAAAtzc2gtZWQyNTUxOQAAACDBGs/6MWZXV20KtacQson8/EyafCK7KDCh0ZH8LBgvYQ | ||
AAAEAzz1majY8Z1JKFhHlfyGiApNGarWquj0JNEG7TQynbCcEaz/oxZldXbQq1pxCyifz8 | ||
TJp8IrsoMKHRkfwsGC9hAAAAGnRtZHNAbG9jYWxob3N0LmxvY2FsZG9tYWluAQID | ||
-----END OPENSSH PRIVATE KEY----- |
This file contains 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 @@ | ||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMEaz/oxZldXbQq1pxCyifz8TJp8IrsoMKHRkfwsGC9h [email protected] |