Class KeyExchange
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]encodeAsMPInt(byte[] raw) protected byte[]encodeAsMPInt(byte[] raw, boolean bzero) protected byte[]encodeAsString(byte[] raw) protected byte[]encodeAsString(byte[] raw, boolean bzero) protected byte[]encodeInt(int raw) abstract intgetState()protected static String[]abstract voidabstract booleanprotected byte[]normalize(byte[] secret) protected booleanVerifies the cryptographic signature of the SSH key exchange hash.
-
Field Details
-
STATE_END
public static final int STATE_END- See Also:
-
session
-
sha
-
K
protected byte[] K -
H
protected byte[] H -
K_S
protected byte[] K_S -
hostKeyCertificate
protected com.jcraft.jsch.OpenSshCertificate hostKeyCertificate -
RSA
protected final int RSA- See Also:
-
DSS
protected final int DSS- See Also:
-
ECDSA
protected final int ECDSA- See Also:
-
EDDSA
protected final int EDDSA- See Also:
-
-
Constructor Details
-
KeyExchange
public KeyExchange()
-
-
Method Details
-
init
-
next
-
getState
public abstract int getState() -
getKeyType
-
getKeyAlgorithName
-
guess
-
getFingerPrint
-
normalize
protected byte[] normalize(byte[] secret) -
verify
Verifies the cryptographic signature of the SSH key exchange hash.This method performs cryptographic verification that the remote server possesses the private key corresponding to the public key presented during the SSH key exchange. It supports both traditional SSH public keys and OpenSSH certificates.
Public Key vs. Certificate Handling
The method handles two distinct input formats:
- Plain Public Keys: When
algis a standard key algorithm (e.g.,"ssh-rsa","ssh-ed25519"), theK_Sparameter contains the server's public key in SSH wire format. The method parses the key components and verifies the signature directly. - OpenSSH Certificates: When
algis a certificate type (e.g.,"ssh-rsa-cert-v01@openssh.com"), theK_Sparameter contains an OpenSSH certificate structure. The method:- Parses the certificate to extract the embedded public key
- Validates that the certificate is a host certificate (not a user certificate)
- Replaces
K_Swith the extracted public key - Extracts the underlying algorithm name from the public key
- Stores the certificate for subsequent CA validation
- Proceeds with signature verification using the extracted public key
Two-Stage Verification for Certificates
For OpenSSH certificates, this method performs only the first stage of verification: proving that the server possesses the private key corresponding to the public key embedded in the certificate. The second stage (validating the certificate's CA signature, validity period, principals, and other certificate-specific properties) is performed separately by
OpenSshCertificateHostKeyVerifier.checkHostCertificate(Session, OpenSshCertificate).Signature Verification Process
After extracting the public key (either from the plain input or from within a certificate), the method:
- Determines the key algorithm (RSA, DSS, ECDSA, or EdDSA)
- Parses the algorithm-specific public key components from the SSH wire format
- Instantiates the appropriate signature verification class
- Verifies that
sig_of_His a valid signature of the exchange hashHusing the public key
- Parameters:
alg- the server host key algorithm name. This can be either a plain key algorithm (e.g.,"ssh-rsa","ssh-dss","ecdsa-sha2-nistp256","ssh-ed25519") or a certificate type (e.g.,"ssh-rsa-cert-v01@openssh.com","ssh-ed25519-cert-v01@openssh.com"). For certificates, this parameter is internally replaced with the underlying key algorithm extracted from the certificate.K_S- the server's public key blob in SSH wire format. For plain keys, this contains the public key directly. For certificates, this contains the complete OpenSSH certificate structure, which includes the public key along with additional metadata (CA signature, principals, validity period, etc.). When a certificate is detected, this reference is replaced internally with the extracted public key for verification purposes.index- the starting byte offset withinK_Sfrom which to begin parsing. For plain keys, this is typically the position after the algorithm string. For certificates, this is typically0(start of the certificate blob), and the offset is recalculated after extracting the embedded public key.sig_of_H- the signature bytes to verify. This is the server's signature of the exchange hashH, which proves the server possesses the private key. The signature format is algorithm-specific and includes both the algorithm identifier and the actual signature data in SSH wire format.- Returns:
trueif the signature is cryptographically valid and proves the server possesses the private key;falseotherwise.- Throws:
JSchException- if the algorithm is unsupported, if a certificate is detected but is not a host certificate (e.g., it's a user certificate), if the signature verification class cannot be instantiated, or if any other error occurs during verification.Exception- if an unexpected error occurs during parsing or cryptographic operations.
- Plain Public Keys: When
-
encodeInt
protected byte[] encodeInt(int raw) -
encodeAsMPInt
protected byte[] encodeAsMPInt(byte[] raw) -
encodeAsMPInt
protected byte[] encodeAsMPInt(byte[] raw, boolean bzero) -
encodeAsString
protected byte[] encodeAsString(byte[] raw) -
encodeAsString
protected byte[] encodeAsString(byte[] raw, boolean bzero)
-