Skip to content

Implement GetRemoteCertificate for DTLSTransport in wasm #3119

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

talentlessguy
Copy link
Contributor

Description

Implements basic support for GetRemoteCertificate using getRemoteCertificates

I'm not sure how to write tests for it, do I mock JS globals?

Reference issue

Partially addresses libp2p/go-libp2p#3277

Copy link

codecov bot commented May 5, 2025

Codecov Report

Attention: Patch coverage is 0% with 21 lines in your changes missing coverage. Please review.

Project coverage is 78.45%. Comparing base (d08789b) to head (c78016b).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
dtlstransport_js.go 0.00% 21 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3119      +/-   ##
==========================================
- Coverage   78.67%   78.45%   -0.22%     
==========================================
  Files          91       91              
  Lines       11407    11428      +21     
==========================================
- Hits         8974     8966       -8     
- Misses       1945     1972      +27     
- Partials      488      490       +2     
Flag Coverage Δ
go 80.48% <ø> (-0.08%) ⬇️
wasm 63.17% <0.00%> (-0.68%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Sean-Der
Copy link
Member

Sean-Der commented May 6, 2025

@talentlessguy that sounds good to me! However you think is best to test it.

The WASM stuff can be frustrating. So w/e works I am in support of :)

@talentlessguy
Copy link
Contributor Author

This demo in JavaScript works:

function bufferToHex(buffer) {
  const byteArray = new Uint8Array(buffer)
  return Array.from(byteArray)
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('')
    .toUpperCase()
}

async function run() {
  const pcConfig = {
    iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
  }

  const localPC = new RTCPeerConnection(pcConfig)
  const remotePC = new RTCPeerConnection(pcConfig)

  const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  const [track] = stream.getAudioTracks()
  localPC.addTrack(track, stream)

  localPC.onicecandidate = (e) => {
    if (e.candidate) remotePC.addIceCandidate(e.candidate)
  }
  remotePC.onicecandidate = (e) => {
    if (e.candidate) localPC.addIceCandidate(e.candidate)
  }

  localPC.onconnectionstatechange = () => {
    console.log('Connection state changed:', localPC.connectionState)
    if (localPC.connectionState === 'connected') {
      const [sender] = localPC.getSenders()
      if (sender?.transport instanceof RTCDtlsTransport) {
        const dtlsTransport = sender.transport
        const certs = dtlsTransport.getRemoteCertificates()
        if (certs.length > 0) {
          console.log(`Got ${certs.length} remote certificate(s):`) // Got 1 remote certificate
          certs.forEach((cert, i) => {
            console.log(`Certificate ${i + 1}:\n${bufferToHex(cert)}`) // DER raw cert
          })
        } else {
          console.warn('No remote certificates returned.')
        }
      } else {
        console.warn('DTLS transport not available.')
      }
    }
  }

  const offer = await localPC.createOffer()
  await localPC.setLocalDescription(offer)
  await remotePC.setRemoteDescription(offer)
  const answer = await remotePC.createAnswer()
  await remotePC.setLocalDescription(answer)
  await localPC.setRemoteDescription(answer)
}

run().catch(console.error)

Outputs a DER certificate

I'll try to write tests based on this demo. I'm also not sure if pion/webrtc implements onconnectionstatechange event listening for JS, at least I haven't seen it anywhere

@Sean-Der
Copy link
Member

Sean-Der commented May 6, 2025

OnConnectionStateChange is available for the WASM PeerConnection. Is it not working for you?

@talentlessguy
Copy link
Contributor Author

OnConnectionStateChange is available for the WASM PeerConnection. Is it not working for you?

I haven't tested it personally yet. I will try to recreate the demo using Go+WASM soon-ish to make sure it works exactly the same, and then write tests that will mock Web APIs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants