Skip to content

Implement key formats and export file format function #96

Closed

Description

@afaranha

I'd like to be able to choose the format of the key Im generating, for example: ssh-keygen -m PEM`, but I can't see how to do it on the library.
I was also trying other types of rsa keys (Maybe the default format would be the one I'm looking for), and it seems that they don't work:

https://github.com/ParallelSSH/ssh-python/blob/master/ssh/keytypes.pyx#L53-L60

key_type = keytypes.RSAKey()
print(key_type)

Outputs:
ssh-rsa

key_type = keytypes.RSA1Key()
print(key_type)

Outputs:
unknown

Activity

  1. enkore commented on Apr 25, 2025

    @enkore
    Contributor

    That's basically just the wrapper for an enum, see examples/keygen.py for how to use the ssh.key.generate API.

    https://github.com/ParallelSSH/ssh-python/blob/master/examples/keygen.py

  2. afaranha commented on Apr 25, 2025

    @afaranha
    Author

    Sorry if I wasn't clear when I opened the issue, but that enum if for the key type, not the key format, for example, using the terminal I can do this to generate a RSA key:

    $ ssh-keygen -f test -t rsa
    $ head test
    -----BEGIN OPENSSH PRIVATE KEY-----
    

    or

    $ ssh-keygen -f test -t rsa -m PEM
    $ head test
    -----BEGIN RSA PRIVATE KEY-----
    

    The key type is the same but the format is different.

    https://man.openbsd.org/ssh-keygen.1

    -m Specify a key format for key generation, the -i (import), -e (export) conversion options, and the -p change passphrase operation. The latter may be used to convert between OpenSSH private key and PEM private key formats. The supported key formats are: “RFC4716” (RFC 4716/SSH2 public or private key), “PKCS8” (PKCS8 public or private key) or “PEM” (PEM public key). By default OpenSSH will write newly-generated private keys in its own format, but when converting public keys for export the default format is “RFC4716”. Setting a format of “PEM” when generating or updating a supported private key type will cause the key to be stored in the legacy PEM private key format.
    

    Do we have a way how to choose a different key format? On the generate function we can only pass the bits and the key type, not the key format.

  3. enkore commented on Apr 25, 2025

    @enkore
    Contributor

    Apologies. No, ssh_pki_export_privkey_file_format isn't wrapped by this library.

  4. reopened this on Apr 25, 2025
  5. added a commit that references this issue on Oct 5, 2025
    e986df3
  6. changed the title [-]Add key format option[/-] [+]Add key formats and export file format function[/+] on Oct 5, 2025
  7. changed the title [-]Add key formats and export file format function[/-] [+]Implement key formats and export file format function[/+] on Oct 5, 2025
  8. pkittenis commented on Oct 5, 2025

    @pkittenis
    Member

    See https://github.com/ParallelSSH/ssh-python/blob/master/tests/test_keys.py#L16-L27

    Exported file formats:

    -----BEGIN PRIVATE KEY-----
    
    -----BEGIN OPENSSH PRIVATE KEY-----
    

    For the two formats.

  9. added a commit that references this issue on Nov 17, 2025
    6e4cb16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions