Class SshPrivateKey
- Namespace
- SshSharp
- Assembly
- SshSharp.dll
A private key for public-key authentication. Load one with Parse(string, string?) or LoadAsync(string, string?, CancellationToken). Supported formats:
the OpenSSH key format (BEGIN OPENSSH PRIVATE KEY, as ssh-keygen writes by default, also passphrase-protected), PuTTY
.ppk (versions 2 and 3, also encrypted), PKCS#8 (BEGIN PRIVATE KEY,
and BEGIN ENCRYPTED PRIVATE KEY for RSA and ECDSA), and PKCS#1/SEC1 PEM (BEGIN RSA PRIVATE KEY, BEGIN EC PRIVATE KEY),
also encrypted the traditional way (Proc-Type: 4,ENCRYPTED with AES-128/192/256-CBC, as ssh-keygen -m PEM writes it).
Passphrase-protected OpenSSH keys are read with every cipher ssh-keygen -Z offers, chacha20-poly1305@openssh.com included.
Key types: Ed25519, ECDSA (nistp256/384/521) and RSA (2048 bits or more). A key held here can be written in the OpenSSH format
(ToOpenSshPrivateKey(string?, int)), PKCS#8 (ToPkcs8PrivateKey(string?)) or PuTTY's (ToPuttyPrivateKey(string?)), or saved
to a file in any of them (SaveAsync(string, SshPrivateKeyFormat, string?, CancellationToken)).
public abstract class SshPrivateKey : IDisposable
- Inheritance
-
SshPrivateKey
- Implements
- Inherited Members
Properties
- Certificate
The user certificate this key authenticates with, if it was paired with one (WithCertificate(SshCertificate)).
- Comment
The comment stored with the key (often
user@host), if the format has one.
- Fingerprint
The OpenSSH-style SHA-256 fingerprint of the public key, as
ssh-keygen -lfprints it.
- KeyType
The key type name, for example
ssh-ed25519.
- PublicKey
The public key in SSH wire format (what
authorized_keysholds, base64-decoded).
Methods
- Dispose()
Clears the private key material.
- FromECDsa(ECDsa, string?)
A key that .NET's ECDsa holds, on P-256, P-384 or P-521, used without exporting it, as FromRsa(RSA, string?) does for RSA.
- FromRsa(RSA, string?)
A key that .NET's RSA holds, used without exporting it: a software key, or a non-exportable one in Windows CNG (a TPM through the Microsoft Platform Crypto Provider, Windows Hello) or in any provider that implements RSA. Only the public key is read. Signs with
rsa-sha2-512orrsa-sha2-256.
- FromSigner(SshKeySigner, string?)
A key that signs through
signer: one in a key vault, a KMS, an HSM or on a smart card. Usable wherever a key is: PrivateKeys, WithCertificate(SshCertificate), SshSignature. Each signature is checked against the public key before it is used.
- Generate(SshKeyType, string?, int)
Generates a new key pair, as
ssh-keygen -tdoes. Write it with SaveAsync(string, string?, CancellationToken) or ToOpenSshPrivateKey(string?, int), and give the server ToAuthorizedKeysLine().
- GetPublicKey()
The public key as an SshPublicKey, with this key's Comment. For a key paired with a certificate (WithCertificate(SshCertificate)), it is the certified key, not the certificate that PublicKey holds then.
- LoadAsync(string, string?, CancellationToken)
Reads and parses a private key file.
- Parse(string, string?)
Parses a private key.
- SaveAsync(string, SshPrivateKeyFormat, string?, CancellationToken)
Writes the key in
format: the private key topath(ToOpenSshPrivateKey(string?, int), ToPkcs8PrivateKey(string?) or ToPuttyPrivateKey(string?)), readable only by the current user (mode 0600 on Unix, an owner-only ACL on Windows), and the public key topath.pubas anauthorized_keysline, for every format. Neither file may exist yet: an existing key is never overwritten.
- SaveAsync(string, string?, int, CancellationToken)
Writes the key as SaveAsync(string, string?, CancellationToken) does, encrypted with
kdfRoundsrounds ofbcrypt_pbkdf(ssh-keygen -a).
- SaveAsync(string, string?, CancellationToken)
Writes the key as
ssh-keygendoes: the private key topath(ToOpenSshPrivateKey(string?, int)), readable only by the current user (mode 0600 on Unix, an owner-only ACL on Windows), and the public key topath.pub. Neither file may exist yet: an existing key is never overwritten.
- ToAuthorizedKeysLine()
The public key as an
authorized_keysline (type, base64, comment).
- ToOpenSshPrivateKey(string?, int)
The key in the OpenSSH private key format (
BEGIN OPENSSH PRIVATE KEY), asssh-keygenwrites it: encrypted withaes256-ctrandbcrypt_pbkdfwhenpassphraseis given, otherwise unencrypted. The text holds the private key: do not log it, and write it only to files other users cannot read (SaveAsync(string, string?, CancellationToken) does).
- ToPkcs8PrivateKey(string?)
The key in PKCS#8 PEM, which OpenSSL,
ssh-keygenand the .NET cryptography APIs (ImportFromPem(ReadOnlySpan<char>), ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<char>)) read:BEGIN PRIVATE KEY, or, whenpassphraseis given,BEGIN ENCRYPTED PRIVATE KEY, encrypted with AES-256-CBC under a key from PBKDF2-HMAC-SHA256 with 600,000 iterations (PBES2, RFC 8018). PKCS#8 has no comment: Comment is not kept. The text holds the private key: do not log it, and write it only to files other users cannot read (SaveAsync(string, SshPrivateKeyFormat, string?, CancellationToken) does).
- ToPuttyPrivateKey(string?)
The key as a PuTTY key file (
.ppk, version 3), as PuTTYgen 0.75 and later writes it: whenpassphraseis given, encrypted with AES-256-CBC under a key from Argon2id (46 MiB, one pass), otherwise unencrypted; authenticated with HMAC-SHA256 either way. PuTTY, Pageant and WinSCP read it; versions of PuTTY before 0.75 do not. The text holds the private key: do not log it, and write it only to files other users cannot read (SaveAsync(string, SshPrivateKeyFormat, string?, CancellationToken) does).
- WithCertificate(SshCertificate)
This key paired with an OpenSSH user certificate for it (
id_ed25519-cert.pub, made withssh-keygen -s): authentication then presents the certificate, which servers that trust its authority (TrustedUserCAKeys) accept without an authorized_keys entry. The returned key shares this key's private material: disposing this key disposes both.