Skip to content
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
18 changes: 8 additions & 10 deletions packages/connection-encrypter-tls/src/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export class TLS implements ConnectionEncrypter {
async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
let streamMuxer: StreamMuxerFactory | undefined

let streamMuxers: string[] = []

if (options?.skipStreamMuxerNegotiation !== true) {
streamMuxers = [...this.components.upgrader.getStreamMuxers().keys()]
}

const opts: TLSSocketOptions = {
...await generateCertificate(this.components.privateKey),
isServer,
Expand All @@ -101,7 +107,7 @@ export class TLS implements ConnectionEncrypter {

// early negotiation of muxer via ALPN protocols
ALPNProtocols: [
...this.components.upgrader.getStreamMuxers().keys(),
...streamMuxers,
'libp2p'
],
ALPNCallback: ({ protocols }) => {
Expand Down Expand Up @@ -158,17 +164,9 @@ export class TLS implements ConnectionEncrypter {
.then(remotePeer => {
this.log('remote certificate ok, remote peer %p', remotePeer)

if (!isServer && typeof socket.alpnProtocol === 'string') {
streamMuxer = this.components.upgrader.getStreamMuxers().get(socket.alpnProtocol)

if (streamMuxer == null) {
this.log.error('selected muxer that did not exist')
}
}

// 'libp2p' is a special protocol - if it's sent the remote does not
// support early muxer negotiation
if (!isServer && typeof socket.alpnProtocol === 'string' && socket.alpnProtocol !== 'libp2p') {
if (!isServer && typeof socket.alpnProtocol === 'string' && socket.alpnProtocol !== 'libp2p' && options?.skipStreamMuxerNegotiation !== true) {
this.log.trace('got early muxer', socket.alpnProtocol)
streamMuxer = this.components.upgrader.getStreamMuxers().get(socket.alpnProtocol)

Expand Down
22 changes: 22 additions & 0 deletions packages/connection-encrypter-tls/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,26 @@ describe('tls', () => {
expect(result).to.have.nested.property('[0].streamMuxer.protocol', '/test/muxer')
expect(result).to.have.nested.property('[1].streamMuxer.protocol', '/test/muxer')
})

it('should not select an early muxer when it is skipped', async () => {
const [inbound, outbound] = duplexPair<any>()

const result = await Promise.all([
encrypter.secureInbound(stubInterface<MultiaddrConnection>({
...inbound
}), {
remotePeer: localPeer,
skipStreamMuxerNegotiation: true
}),
encrypter.secureOutbound(stubInterface<MultiaddrConnection>({
...outbound
}), {
remotePeer: localPeer,
skipStreamMuxerNegotiation: true
})
])

expect(result).to.have.nested.property('[0].streamMuxer', undefined)
expect(result).to.have.nested.property('[1].streamMuxer', undefined)
})
})
8 changes: 8 additions & 0 deletions packages/interface/src/connection-encrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import type { Uint8ArrayList } from 'uint8arraylist'
*/
export interface SecureConnectionOptions extends AbortOptions {
remotePeer?: PeerId

/**
* Some encryption protocols allow negotiating application protocols as part
* of the initial handshake. The negotiated stream muxer protocol will be
* included as part of the from the `secureOutbound`/`secureInbound` methods
* unless `false` is passed here.
*/
skipStreamMuxerNegotiation?: boolean
}

/**
Expand Down
Loading