Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ecdsa cert usage in TLS which bouncycastle broke #293

Merged
merged 4 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions libp2p/src/main/kotlin/io/libp2p/security/tls/TLSSecureChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class TlsSecureChannel(private val localKey: PrivKey, private val muxers: List<S
init {
Security.insertProviderAt(Libp2pCrypto.provider, 1)
Security.insertProviderAt(BouncyCastleJsseProvider(), 2)
Security.setProperty("ssl.KeyManagerFactory.algorithm", "PKIX")
Security.setProperty("ssl.TrustManagerFactory.algorithm", "PKIX")
}

@JvmStatic
fun ECDSA(localKey: PrivKey, muxerIds: List<StreamMuxer>): TlsSecureChannel {
return TlsSecureChannel(localKey, muxerIds, "ECDSA")
}
}

Expand Down Expand Up @@ -132,8 +139,15 @@ fun buildTlsHandler(
cause = cause.cause
handshakeComplete.completeExceptionally(cause)
} else {
val negotiatedProtocols = sslContext.applicationProtocolNegotiator().protocols()
val selectedMuxer = muxers.findBestMatch(negotiatedProtocols)
val nextProtocol = handler.applicationProtocol()
val selectedMuxer = muxers
.filter { mux ->
mux.protocolDescriptor.protocolMatcher.matches(nextProtocol)
}
.map { mux ->
NegotiatedStreamMuxer(mux, nextProtocol)
}
.firstOrNull()
handshakeComplete.complete(
SecureChannel.Session(
PeerId.fromPubKey(localKey.publicKey()),
Expand All @@ -151,15 +165,6 @@ fun buildTlsHandler(
private val <T : ProtocolBinding<*>> List<T>.allProtocols: List<ProtocolId> get() =
this.flatMap { it.protocolDescriptor.announceProtocols }

private fun List<StreamMuxer>.findBestMatch(remoteProtocols: List<ProtocolId>): NegotiatedStreamMuxer? =
this.firstNotNullOfOrNull { muxer ->
remoteProtocols.firstOrNull { remoteProtocol ->
muxer.protocolDescriptor.protocolMatcher.matches(remoteProtocol)
}?.let { negotiatedProtocol ->
NegotiatedStreamMuxer(muxer, negotiatedProtocol)
}
}

private class ChannelSetup(
private val localKey: PrivKey,
private val muxers: List<StreamMuxer>,
Expand Down
2 changes: 1 addition & 1 deletion libp2p/src/test/java/io/libp2p/core/HostTestJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void ping() throws Exception {

Host clientHost = new HostBuilder()
.transport(TcpTransport::new)
.secureChannel(TlsSecureChannel::new)
.secureChannel((k, m) -> new TlsSecureChannel(k, m, "ECDSA"))
.muxer(StreamMuxerProtocol::getYamux)
.build();

Expand Down