Host Key Retrieval and Verification
Host key verification is a critical security mechanism in SSH that prevents man-in-the-middle (MITM) attacks. When you connect to an SSH server, the server presents its host key to prove its identity. You should verify this key matches what you expect before proceeding with authentication.
Why Host Key Verification Matters
Without host key verification, an attacker could intercept your connection and impersonate the legitimate server. This would allow them to:
Capture your authentication credentials
Monitor all data transmitted during the session
Manipulate commands and responses
Traditional SSH clients (like OpenSSH) maintain a known_hosts file that stores fingerprints of previously-seen servers and warn users when keys change. NullOpsDevs.LibSsh provides the APIs to implement similar verification in your applications.
Basic Host Key Retrieval
After connecting to a server (but before authenticating), you can retrieve the server's host key:
The SshHostKey structure contains:
Key- The raw host key data as a byte arrayType- The key algorithm type (see Host Key Types)
Host Key Types
The server's host key can use one of several cryptographic algorithms:
Type | Description | Recommended |
|---|---|---|
| Ed25519 elliptic curve signature algorithm | ✅ Recommended for new deployments |
| ECDSA with NIST P-256 curve | ✅ Good choice |
| ECDSA with NIST P-384 curve | ✅ Good choice |
| ECDSA with NIST P-521 curve | ✅ Good choice |
| RSA public key algorithm | ⚠️ Acceptable with sufficient key size (2048+ bits) |
| DSA algorithm (deprecated) | ❌ Avoid - deprecated and insecure |
Computing Host Key Fingerprints
To make host keys human-readable and easier to verify, you can compute cryptographic fingerprints (hashes) of the key:
Hash Algorithm Comparison
Algorithm | Output Size | Security | Use Case |
|---|---|---|---|
| 32 bytes (256 bits) | ✅ Strong | Recommended for new implementations |
| 20 bytes (160 bits) | ⚠️ Weak | Legacy compatibility only |
| 16 bytes (128 bits) | ❌ Broken | Legacy compatibility only |
Retrieving Negotiated Algorithms
After connection, you can inspect which algorithms were negotiated for various SSH protocol operations:
This is useful for:
Debugging connection issues
Auditing security configurations
Ensuring strong algorithms are being used
Advanced: Configuring Accepted Host Key Types
You can restrict which host key types your client will accept by setting method preferences before connecting:
Or use the secure defaults which already prefer modern algorithms:
The secure defaults prefer:
Host Keys: Ed25519 → ECDSA (521/384/256) → RSA-SHA2 (no DSA or legacy RSA)
Key Exchange: Curve25519 → ECDH with NIST curves → DH group exchange
Ciphers: ChaCha20-Poly1305 → AES-GCM → AES-CTR
MACs: HMAC-SHA2 with encrypt-then-MAC
See SshSession.cs:240-249 for the complete list of secure defaults.
Complete Secure Connection Example
Here's a complete example that combines host key verification with authentication:
Security Recommendations
Always verify host keys before authenticating - never blindly trust server identities
Use SHA-256 fingerprints for new implementations (avoid MD5 and SHA-1)
Implement key pinning for production systems (pre-configure known fingerprints)
Monitor for key changes - a changed host key may indicate an attack or server redeployment
Use secure algorithm preferences to reject weak cryptographic algorithms
Log verification failures for security auditing and incident response
Educate users about the importance of verifying fingerprints when prompted
See Also
Authentication - Learn about authentication methods after host key verification
Algorithm and Method Preferences - Configure accepted algorithms
Session Lifecycle - When to verify host keys
Error Handling - Handle host key verification errors
Quickstart - Complete connection examples
SshSession.GetHostKey()(SshSession.cs:156) - Retrieve the raw host keySshSession.GetHostKeyHash()(SshSession.cs:347) - Compute host key fingerprintsSshSession.SetSecureMethodPreferences()(SshSession.cs:240) - Configure secure algorithm defaults