Expand description
§RustCrypto: SSH Keys and Certificates
§About
Pure Rust implementation of SSH key file format decoders/encoders as described in RFC4251 and RFC4253 as well as OpenSSH’s PROTOCOL.key format specification.
Additionally provides support for SSH signatures as described in
PROTOCOL.sshsig, OpenSSH certificates as specified in PROTOCOL.certkeys
including certificate validation and certificate authority (CA) support,
FIDO/U2F keys as specified in PROTOCOL.u2f (and certificates thereof), and
also the authorized_keys and known_hosts file formats.
Supports a minimal profile which works on heapless no_std targets. See
“Supported algorithms” table below for which key formats work on heapless
targets and which algorithms require alloc.
When the ed25519, p256, and/or rsa features of this crate are enabled,
provides key generation and certificate signing/verification support for that
respective SSH key algorithm.
Key fingerprints, certificate validation, and sshsig signing/verification of
messages compute SHA-2 hashes using the sha2 crate, and are only available
when the sha2 feature is enabled. It’s on by default and implied by the
ed25519, p256, p384, p521, ppk, and rsa features. With it disabled,
sshsig signatures can still be created and verified from a digest computed by
another SHA-2 implementation, using the *_prehash methods, or the *_digest
methods with a type that implements AssociatedHashAlg.
§Features
-
Constant-time Base64 decoder/encoder using
base64ct/pem-rfc7468crates -
OpenSSH-compatible decoder/encoders for the following formats:
- OpenSSH public keys
-
OpenSSH private keys (i.e.
BEGIN OPENSSH PRIVATE KEY) - OpenSSH certificates
- OpenSSH signatures (a.k.a. “sshsig”)
-
OpenSSH certificate support
- OpenSSH certificate validation
- OpenSSH certificate authority (CA) support i.e. cert builder/signer
-
Private key encryption/decryption (
bcrypt-pbkdf+aes256-ctronly) - Private key generation support: DSA, Ed25519, ECDSA (P-256/P-384/P-521), RSA, MLDSA44-Ed25519, and MLDSA87-P384
-
FIDO/U2F key support (
sk-*) as specified in PROTOCOL.u2f -
Fingerprint support
- “randomart” fingerprint visualizations
-
no_stdsupport including support for “heapless” (no-alloc) targets -
Parsing
authorized_keysfiles -
Parsing
known_hostsfiles -
serdesupport -
zeroizesupport for private keys
§TODO
- FIDO/U2F signature support
-
Legacy (pre-OpenSSH) SSH key format support
- PKCS#1 SSH private keys (i.e. RSA-only)
- PKCS#8 SSH private keys
- RFC4716 SSH public keys
- SEC1 SSH public keys
§Supported Signature Algorithms
| Name | Decode | Encode | Cert | Keygen | Sign | Verify | Feature | Alloc? |
|---|---|---|---|---|---|---|---|---|
ecdsa‑sha2‑nistp256 | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | p256 | no_alloc |
ecdsa‑sha2‑nistp384 | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | p384 | no_alloc |
ecdsa‑sha2‑nistp521 | ✅ | ✅ | ✅ | ✅️️ | ✅️ ️ | ✅️️ | p521 | no_alloc |
ssh‑dsa | ✅ | ✅ | ✅ | ✅ | ✅️ | ✅️ | dsa | alloc ️ |
ssh‑ed25519 | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅ | ed25519 | no_alloc |
ssh‑rsa | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅ | rsa | alloc |
sk‑ecdsa‑sha2‑nistp256@openssh.com | ✅ | ✅ | ✅ | ⛔ | ⛔️ | ✅️ | ⛔ | alloc |
sk‑ssh‑ed25519@openssh.com | ✅ | ✅ | ✅ | ⛔ | ⛔️ | ✅️️ | ed25519 | alloc |
ssh-mldsa44-ed25519 | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | mldsa44-ed25519 | no_alloc |
ssh-mldsa87-p384 | ✅ | ✅ | ✅ | ✅️ | ✅️ | ✅️ | mldsa87-p384 | no_alloc |
By default no SSH signature algorithms are enabled and you will get an
Error::AlgorithmUnsupported error if you try to use them.
Enable the crypto feature or the “Feature” for specific algorithms in the
chart above (e.g. p256, rsa) in order to use cryptographic functionality.
The “Feature” column lists the name of ssh-key crate features which can
be enabled to provide full support for the “Keygen”, “Sign”, and “Verify”
functionality for a particular SSH key algorithm.
mldsa enables the SSH composite for each enabled curve and requires at least
one of ed25519 or p384. Use mldsa44-ed25519 or mldsa87-p384 to select
a pair, or combine mldsa with crypto to enable both composites.
Both composites are specified in draft-ietf-sshm-composite-sigs-01. The Ed25519 variant shipped in OpenSSH 10.6.
With default features disabled, both composites support key generation, OpenSSH
key encoding and decoding, and signing and verification with Signature.
Heapless builds store the public key and signature bytes inline. ML-DSA also
uses stack space for its working buffers. Certificates and SshSig require
the alloc feature.
§License
Licensed under either of:
at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
§Usage
The main types provided by this library are:
Certificate: OpenSSH certificatesFingerprint: public key fingerprints (i.e. hashes)PrivateKey: SSH private keys (i.e. digital signature keys)PublicKey: SSH public keys (i.e. signature verification keys)SshSig: signatures with SSH keys alassh-keygen -Y sign/ssh-keygen -Y verify
§Parsing OpenSSH Public Keys
OpenSSH-formatted public keys have the form:
<algorithm id> <base64 data> <comment>§Example
use ssh_key::PublicKey;
let encoded_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti user@example.com";
let public_key = PublicKey::from_openssh(encoded_key)?;
// Key attributes
assert_eq!(public_key.algorithm(), ssh_key::Algorithm::Ed25519);
// Key data: in this example an Ed25519 key
if let Some(ed25519_public_key) = public_key.key_data().ed25519() {
assert_eq!(
ed25519_public_key.as_ref(),
&[
0xb3, 0x3e, 0xae, 0xf3, 0x7e, 0xa2, 0xdf, 0x7c, 0xaa, 0x1, 0xd, 0xef, 0xde, 0xa3,
0x4e, 0x24, 0x1f, 0x65, 0xf1, 0xb5, 0x29, 0xa4, 0xf4, 0x3e, 0xd1, 0x43, 0x27, 0xf5,
0xc5, 0x4a, 0xab, 0x62
]
);
}§Parsing OpenSSH Private Keys
NOTE: for more private key usage examples, see the private module.
OpenSSH-formatted private keys are PEM-encoded and begin with the following:
-----BEGIN OPENSSH PRIVATE KEY-----§Example
use ssh_key::PrivateKey;
// WARNING: don't actually hardcode private keys in source code!!!
let encoded_key = r#"
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCzPq7zfqLffKoBDe/eo04kH2XxtSmk9D7RQyf1xUqrYgAAAJgAIAxdACAM
XQAAAAtzc2gtZWQyNTUxOQAAACCzPq7zfqLffKoBDe/eo04kH2XxtSmk9D7RQyf1xUqrYg
AAAEC2BsIi0QwW2uFscKTUUXNHLsYX4FxlaSDSblbAj7WR7bM+rvN+ot98qgEN796jTiQf
ZfG1KaT0PtFDJ/XFSqtiAAAAEHVzZXJAZXhhbXBsZS5jb20BAgMEBQ==
-----END OPENSSH PRIVATE KEY-----
"#;
let private_key = PrivateKey::from_openssh(encoded_key)?;
// Key attributes
assert_eq!(private_key.algorithm(), ssh_key::Algorithm::Ed25519);
// Key data: in this example an Ed25519 key
if let Some(ed25519_keypair) = private_key.key_data().ed25519() {
assert_eq!(
ed25519_keypair.public.as_ref(),
&[
0xb3, 0x3e, 0xae, 0xf3, 0x7e, 0xa2, 0xdf, 0x7c, 0xaa, 0x1, 0xd, 0xef, 0xde, 0xa3,
0x4e, 0x24, 0x1f, 0x65, 0xf1, 0xb5, 0x29, 0xa4, 0xf4, 0x3e, 0xd1, 0x43, 0x27, 0xf5,
0xc5, 0x4a, 0xab, 0x62
]
);
assert_eq!(
ed25519_keypair.private.as_ref(),
&[
0xb6, 0x6, 0xc2, 0x22, 0xd1, 0xc, 0x16, 0xda, 0xe1, 0x6c, 0x70, 0xa4, 0xd4, 0x51,
0x73, 0x47, 0x2e, 0xc6, 0x17, 0xe0, 0x5c, 0x65, 0x69, 0x20, 0xd2, 0x6e, 0x56, 0xc0,
0x8f, 0xb5, 0x91, 0xed
]
)
}§serde support
When the serde feature of this crate is enabled, the Certificate, Fingerprint, and
PublicKey types receive impls of serde’s Deserialize and
Serialize traits.
Serializing/deserializing PrivateKey using serde is presently unsupported.
Re-exports§
pub use crate::algorithm::Algorithm;pub use crate::algorithm::AssociatedHashAlg;pub use crate::algorithm::EcdsaCurve;pub use crate::algorithm::HashAlg;pub use crate::algorithm::KdfAlg;pub use crate::authorized_keys::AuthorizedKeys;pub use crate::error::Error;pub use crate::error::Result;pub use crate::fingerprint::Fingerprint;pub use crate::kdf::Kdf;pub use crate::private::PrivateKey;pub use crate::public::PublicKey;pub use crate::algorithm::AlgorithmName;allocpub use crate::certificate::Certificate;allocpub use crate::comment::Comment;allocpub use crate::known_hosts::KnownHosts;allocpub use crate::sshsig::SshSig;allocpub use ssh_cipher;pub use ssh_encoding;pub use cipher::cipher::common::getrandom;getrandompub use rand_core;rand_corepub use sec1;ecdsapub use sha2;sha2
Modules§
- authorized_
keys - Parser for
AuthorizedKeysFile-formatted data. - certificate
alloc - OpenSSH certificate support.
- known_
hosts alloc - Parser for
KnownHostsFile-formatted data. - private
- SSH private key support.
- public
- SSH public key support.
Structs§
- Algorithm
Name alloc - A string representing an additional algorithm name in the
name@domainnameformat (see RFC4251 § 6). - Comment
alloc - SSH key comment (e.g. email address of owner)
- DotSsh
std ~/.sshdirectory support (or similarly structured directories).- Mpint
alloc - Multiple precision integer, a.k.a.
mpint. - Signature
allocormldsa - Low-level digital signature (e.g. DSA, ECDSA, Ed25519).
- SshSig
alloc sshsigprovides a general-purpose signature format based on SSH keys and wire formats.
Enums§
- Algorithm
- SSH key algorithms, i.e. digital signature algorithms used with SSH private/public keys.
- Cipher
- Cipher algorithms.
- Ecdsa
Curve - Elliptic curves supported for use with ECDSA.
- Error
- Error type.
- Fingerprint
- SSH public key fingerprints.
- HashAlg
- Hashing algorithms a.k.a. digest functions.
- Kdf
- Key Derivation Functions (KDF).
- KdfAlg
- Key Derivation Function (KDF) algorithms.
- Line
Ending - Line endings: variants of newline characters that can be used with Base64.
Traits§
- Associated
Hash Alg - Associate an SSH
HashAlgwith the given type. - Signing
Key allocormldsa - Trait for signing keys which produce a
Signature.