Class PeerIdentityTrustManager
java.lang.Object
javax.net.ssl.X509ExtendedTrustManager
pt.unl.fct.di.novasys.babel2.ssl.PeerIdentityTrustManager
- All Implemented Interfaces:
TrustManager, X509TrustManager
An
X509ExtendedTrustManager that validates peer certificates according to the
Babel 2 / libp2p TLS identity certificate pattern.
Specifically, this trust manager verifies that:
- The certificate dates are currently valid.
- The certificate is self-signed by the TLS ephemeral public key.
- The certificate contains the custom Babel 2 TLS identity extension (OID "2.25.143997576564619623049175376840488616895").
- The extension contains a valid long-term static public key and a signature over the ephemeral key.
- The signature is verified against the static public key.
- If an authorization allowlist is configured, the SHA-256 hash of the static public key matches an authorized peer ID.
Upon successful authentication, the verified long-term peer public key and hex peer ID
are stored in the active SSLSession attributes under "pt.unl.fct.di.novasys.babel2.ssl.PeerPublicKey"
and "pt.unl.fct.di.novasys.babel2.ssl.PeerId".
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a trust manager that accepts all valid peer certificates conforming to the Babel 2 identity certificate flow without restricting to specific peer IDs.PeerIdentityTrustManager(@Nullable Set<String> authorizedPeerIdHashes) Constructs a trust manager that requires the peer's SHA-256 public key hash to be in the specified allowlist. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbytesToHex(byte[] bytes) Converts a byte array to its lowercase hexadecimal representation.voidcheckClientTrusted(X509Certificate[] chain, String authType) voidcheckClientTrusted(X509Certificate[] chain, String authType, Socket socket) voidcheckClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) voidcheckServerTrusted(X509Certificate[] chain, String authType) voidcheckServerTrusted(X509Certificate[] chain, String authType, Socket socket) voidcheckServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) static StringcomputePeerId(PublicKey publicKey) Computes the hex-encoded SHA-256 peer ID for a given public key.static byte[]computeSha256(byte[] data) Computes the SHA-256 digest of the given data.static @Nullable StringgetPeerId(SSLSession session) Extracts the authenticated peer ID string from an activeSSLSession, if present.static @Nullable PublicKeygetPeerIdentity(SSLSession session) Extracts the authenticated peerPublicKeyfrom an activeSSLSession, if present.static PeerIdentityTrustManagerofAuthorizedPublicKeys(Set<PublicKey> authorizedKeys) Creates a trust manager authorized for the specified set of long-term public keys.Converts this trust manager into aTrustManagerFactory.
-
Field Details
-
PEER_IDENTITY_KEY_ATTR
-
PEER_IDENTITY_ID_ATTR
Session attribute key storing the authenticated peer's SHA-256 hex peer ID.- See Also:
-
-
Constructor Details
-
PeerIdentityTrustManager
public PeerIdentityTrustManager()Constructs a trust manager that accepts all valid peer certificates conforming to the Babel 2 identity certificate flow without restricting to specific peer IDs. -
PeerIdentityTrustManager
-
-
Method Details
-
ofAuthorizedPublicKeys
public static PeerIdentityTrustManager ofAuthorizedPublicKeys(Set<PublicKey> authorizedKeys) throws CertificateException Creates a trust manager authorized for the specified set of long-term public keys.- Parameters:
authorizedKeys- set of authorized public keys- Returns:
- a configured
PeerIdentityTrustManager - Throws:
CertificateException- if hashing any public key fails
-
computePeerId
Computes the hex-encoded SHA-256 peer ID for a given public key.- Parameters:
publicKey- the public key- Returns:
- the lowercase hex string of the SHA-256 hash
- Throws:
CertificateException- if hashing fails
-
getPeerIdentity
Extracts the authenticated peerPublicKeyfrom an activeSSLSession, if present.- Parameters:
session- the TLS session- Returns:
- the authenticated peer public key, or
null
-
getPeerId
Extracts the authenticated peer ID string from an activeSSLSession, if present.- Parameters:
session- the TLS session- Returns:
- the peer ID hex string, or
null
-
toTrustManagerFactory
Converts this trust manager into aTrustManagerFactory.- Returns:
- a new
PeerIdentityTrustManagerFactorywrapping this trust manager
-
checkServerTrusted
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException - Specified by:
checkServerTrustedin classX509ExtendedTrustManager- Throws:
CertificateException
-
checkClientTrusted
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException - Specified by:
checkClientTrustedin classX509ExtendedTrustManager- Throws:
CertificateException
-
checkServerTrusted
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException - Specified by:
checkServerTrustedin classX509ExtendedTrustManager- Throws:
CertificateException
-
checkClientTrusted
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException - Specified by:
checkClientTrustedin classX509ExtendedTrustManager- Throws:
CertificateException
-
checkServerTrusted
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException - Throws:
CertificateException
-
checkClientTrusted
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException - Throws:
CertificateException
-
getAcceptedIssuers
-
computeSha256
Computes the SHA-256 digest of the given data.- Parameters:
data- the byte array to hash- Returns:
- the 32-byte SHA-256 hash
- Throws:
CertificateException- if the digest algorithm is unavailable
-
bytesToHex
Converts a byte array to its lowercase hexadecimal representation.- Parameters:
bytes- the byte array to convert- Returns:
- lowercase hex string
-