Skip to content
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

[Private Key] Add support for PuTTY private key file format (V3 and V2) #1543

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

scott-xu
Copy link
Collaborator

@scott-xu scott-xu commented Nov 21, 2024

This PR adds support for PuTTY private key file format (V3 and V2) described in https://tartarus.org/~simon/putty-snapshots/htmldoc/AppendixC.html

Close #341
Close #431
Close #434
Close #510

Notes:

  1. PuTTY private key file format V1 is not included because

    PPK version 1 was a badly designed format, only used during initial development, and not recommended for production use.

  2. The Argon2 algorithm is from latest BouncyCastle beta version. It may subject to change, then we need to change accordingly. (Hope not). Also hope BouncyCastle makes a formal release before SSH.NET next release.

@scott-xu scott-xu marked this pull request as ready for review November 22, 2024 04:41
@scott-xu scott-xu changed the title [Private Key] Add support for PuTTY private key [Private Key] Add support for PuTTY private key file format (V3 and V2) Nov 22, 2024
Comment on lines +86 to +90
#if NET
Convert.FromHexString(_argon2Salt),
#else
Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_argon2Salt),
#endif
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it to be the same as below code. Or change the below code to be the same as here.

var binarySalt = new byte[_salt.Length / 2];
for (var i = 0; i < binarySalt.Length; i++)
{
binarySalt[i] = Convert.ToByte(_salt.Substring(i * 2, 2), 16);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or change the below code to be the same as here.

sounds good

switch (keyType)
{
case "ssh-ed25519":
parsedKey = new ED25519Key(privateKeyReader.ReadBignum2());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ED25519Key class does not have a constructor with both publicKeyData and privateKeyData. The current implementation is to generate public key from private key. Should we add a new constructor?

public ED25519Key(byte[] privateKeyData)
{
PrivateKey = new byte[Ed25519.SecretKeySize];
PublicKey = new byte[Ed25519.PublicKeySize];
Buffer.BlockCopy(privateKeyData, 0, PrivateKey, 0, Ed25519.SecretKeySize);
Ed25519.GeneratePublicKey(privateKeyData, 0, PublicKey, 0);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems ok as it is, but I don't mind

Copy link
Collaborator

@Rob-Hague Rob-Hague left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, will wait for a stable BC release

Comment on lines +86 to +90
#if NET
Convert.FromHexString(_argon2Salt),
#else
Org.BouncyCastle.Utilities.Encoders.Hex.Decode(_argon2Salt),
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or change the below code to be the same as here.

sounds good

switch (keyType)
{
case "ssh-ed25519":
parsedKey = new ED25519Key(privateKeyReader.ReadBignum2());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems ok as it is, but I don't mind

src/Renci.SshNet/PrivateKeyFile.PuTTY.cs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants