Skip to content

Add SSH key pair generation - #11

Merged
mplpl merged 1 commit into
mplpl:mainfrom
MansiVisuals:ssh-key-generation
Sep 24, 2026
Merged

mplpl merged 1 commit into
mplpl:mainfrom
MansiVisuals:ssh-key-generation

Conversation

@MansiVisuals

Copy link
Copy Markdown
Contributor

Adds MFTKeyGenerator, for generating an SSH key pair locally. No connection is
involved: this is key material made before there is anything to connect to, so
an app can offer "generate a key" without first having a server to talk to.

Ed25519, RSA 4096 and ECDSA P-256 are supported. The pair comes back as an
MFTKeyPair holding the PEM wrapped private key and a single authorized_keys
line for the public half, so the public half can be shown or copied directly.
Passing a passphrase encrypts the private half.

let pair = try MFTKeyGenerator.generate(type: .ed25519, passphrase: "secret")
print(pair.publicKey)   // ssh-ed25519 AAAAC3Nza... 
print(pair.keyType)     // ssh-ed25519

Two details worth flagging:

  • The RSA modulus size is requested through an ssh_pki_ctx. Passing a null
    context to ssh_pki_generate_key leaves libssh at its 3072 bit default, so
    .rsa4096 would otherwise quietly produce a 3072 bit key.
  • Ed25519 private keys come out in OpenSSH format, RSA and ECDSA ones in
    PKCS#8. That is libssh's behaviour; it is documented on the property rather
    than normalised.

Everything is @objc exposed, matching the rest of the framework.

Verified by generating all three types, writing them out, and checking them with
ssh-keygen: the reported sizes are 256 / 4096 / 256, each public line matches
the one ssh-keygen -y derives from the corresponding private key, and a
passphrase protected key fails to load with the wrong passphrase and succeeds
with the right one.

Comment thread mft/MFTSftpConnection.swift Outdated
/// The objects of this class are NOT thread safe and must NOT be used to running multiple operations
/// at the same time. If you need such functionality, create multiple MFTSftpConnection (and by doing that
/// establish multiple SFTP connections).
/// A newly generated SSH key pair.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

By adding new class here, you separated the comment above (starting line 66) from the function (MFTSftpConnection) it refers to. This needs to be fixed.

Also, these new classes could be moved to a new file as then are not directly related to MFTSftpConnection implemented in this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Appreciate the fast review of both PR's

I will adjust this PR, so it's corrected and not writing them into the existing MFTSftpConnection when not needing too.

Thank you will get back to me you asap

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the new classes to their own file, mft/MFTKeyGenerator.swift. MFTSftpConnection.swift is back to how it was and does not show up in the diff at all now, so the comment is back where it should be.

Added the file to both the mft and mft ios targets, both build fine here.

Adds MFTKeyGenerator, which generates an SSH key pair locally, with no
connection needed: the key material is made before there is anything to
connect to. Ed25519, RSA 4096 and ECDSA P-256 are supported, and the
private half is encrypted when a passphrase is given.

The pair comes back as an MFTKeyPair holding the PEM wrapped private key
and a single authorized_keys line for the public half. Ed25519 private
keys come out in OpenSSH format, RSA and ECDSA ones in PKCS#8.

The RSA modulus size goes through an ssh_pki_ctx, since libssh otherwise
defaults to 3072 bits.

These types live in their own file rather than in MFTSftpConnection.swift,
having nothing to do with an established connection.
@mplpl
mplpl merged commit 7dcdc18 into mplpl:main Sep 24, 2026
1 check passed
@mplpl

mplpl commented Sep 24, 2026

Copy link
Copy Markdown
Owner

Thanks for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants