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
29 changes: 29 additions & 0 deletions dtlstransport_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,32 @@ func (r *DTLSTransport) ICETransport() *ICETransport {
underlying: underlying,
}
}

func (t *DTLSTransport) GetRemoteCertificate() []byte {
if t.underlying.IsNull() || t.underlying.IsUndefined() {
return nil
}

// Firefox does not support getRemoteCertificates: https://bugzilla.mozilla.org/show_bug.cgi?id=1805446
jsGet := t.underlying.Get("getRemoteCertificates")
if jsGet.IsUndefined() || jsGet.IsNull() {
return nil
}

jsCerts := t.underlying.Call("getRemoteCertificates")
if jsCerts.Length() == 0 {
return nil
}

buf := jsCerts.Index(0)
u8 := js.Global().Get("Uint8Array").New(buf)

if u8.Length() == 0 {
return nil
}

cert := make([]byte, u8.Length())
js.CopyBytesToGo(cert, u8)

return cert
}
21 changes: 21 additions & 0 deletions peerconnection_js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ func TestPeerConnectionCanTrickleICECandidatesJS(t *testing.T) {
pc.underlying = js.ValueOf(map[string]any{})
assert.Equal(t, ICETrickleCapabilityUnknown, pc.CanTrickleICECandidates())
}

func TestDTLSTransportGetRemoteCertificateMock(t *testing.T) {
expected := []byte{0x01, 0x02, 0x03, 0x04}

u8 := js.Global().Get("Uint8Array").New(len(expected))
if n := js.CopyBytesToJS(u8, expected); n != len(expected) {
t.Fatalf("copied %d bytes to Uint8Array; expected %d", n, len(expected))
}
certBuffer := u8.Get("buffer")

getRemoteCertificates := js.FuncOf(func(this js.Value, args []js.Value) any {
return js.ValueOf([]any{certBuffer})
})
defer getRemoteCertificates.Release()

mockTransport := js.Global().Get("Object").New()
mockTransport.Set("getRemoteCertificates", getRemoteCertificates)

dtlsTransport := &DTLSTransport{underlying: mockTransport}
assert.Equal(t, expected, dtlsTransport.GetRemoteCertificate())
}
Loading