forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dtls.go
160 lines (135 loc) · 4.19 KB
/
dtls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dtls
/*
#cgo linux windows pkg-config: libssl libcrypto
#cgo linux CFLAGS: -Wno-deprecated-declarations
#cgo darwin CFLAGS: -I/usr/local/opt/openssl/include -I/usr/local/opt/openssl/include -Wno-deprecated-declarations
#cgo darwin LDFLAGS: -L/usr/local/opt/openssl/lib -L/usr/local/opt/openssl/lib -lssl -lcrypto
#cgo windows CFLAGS: -DWIN32_LEAN_AND_MEAN
#include "dtls.h"
*/
import "C"
import (
"fmt"
"net"
"strconv"
"sync"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/net/ipv4"
)
func init() {
if !C.openssl_global_init() {
panic("Failed to initalize OpenSSL")
}
}
var listenerMap = make(map[string]*ipv4.PacketConn)
var listenerMapLock = &sync.Mutex{}
//export go_handle_sendto
func go_handle_sendto(rawSrc *C.char, rawDst *C.char, rawBuf *C.char, rawBufLen C.int) {
src := C.GoString(rawSrc)
dst := C.GoString(rawDst)
buf := []byte(C.GoStringN(rawBuf, rawBufLen))
C.free(unsafe.Pointer(rawBuf))
listenerMapLock.Lock()
defer listenerMapLock.Unlock()
if conn, ok := listenerMap[src]; ok {
strIP, strPort, err := net.SplitHostPort(dst)
if err != nil {
fmt.Println(err)
return
}
port, err := strconv.Atoi(strPort)
if err != nil {
fmt.Println(err)
return
}
_, err = conn.WriteTo(buf, nil, &net.UDPAddr{IP: net.ParseIP(strIP), Port: port})
if err != nil {
fmt.Println(err)
}
} else {
fmt.Printf("Could not find ipv4.PacketConn for %s \n", src)
}
}
// TLSCfg holds the Certificate/PrivateKey used for a single RTCPeerConnection
type TLSCfg struct {
tlscfg *_Ctype_struct_tlscfg
}
// NewTLSCfg creates a new TLSCfg
func NewTLSCfg() *TLSCfg {
return &TLSCfg{
tlscfg: C.dtls_build_tlscfg(),
}
}
// Fingerprint generates a SHA-256 fingerprint of the certificate
func (t *TLSCfg) Fingerprint() string {
rawFingerprint := C.dtls_tlscfg_fingerprint(t.tlscfg)
defer C.free(unsafe.Pointer(rawFingerprint))
return C.GoString(rawFingerprint)
}
// Close cleans up the associated OpenSSL resources
func (t *TLSCfg) Close() {
C.dtls_tlscfg_cleanup(t.tlscfg)
}
// State represents all the state needed for a DTLS session
type State struct {
*TLSCfg
sslctx *_Ctype_struct_ssl_ctx_st
dtlsSession *_Ctype_struct_dtls_sess
rawSrc, rawDst *_Ctype_char
}
// NewState creates a new DTLS session
func NewState(tlscfg *TLSCfg, isClient bool, src, dst string) (d *State, err error) {
if tlscfg == nil || tlscfg.tlscfg == nil {
return d, errors.Errorf("TLSCfg must not be nil")
}
d = &State{
TLSCfg: tlscfg,
rawSrc: C.CString(src),
rawDst: C.CString(dst),
}
d.sslctx = C.dtls_build_sslctx(d.tlscfg)
d.dtlsSession = C.dtls_build_session(d.sslctx, C.bool(!isClient))
return d, err
}
// Close cleans up the associated OpenSSL resources
func (d *State) Close() {
C.free(unsafe.Pointer(d.rawSrc))
C.free(unsafe.Pointer(d.rawDst))
C.dtls_session_cleanup(d.sslctx, d.dtlsSession)
}
// CertPair is the client+server key and profile extracted for SRTP
type CertPair struct {
ClientWriteKey []byte
ServerWriteKey []byte
Profile string
}
// HandleDTLSPacket checks if the packet is a DTLS packet, and if it is passes to the DTLS session
func (d *State) HandleDTLSPacket(packet []byte) (certPair *CertPair) {
packetRaw := C.CBytes(packet)
defer C.free(unsafe.Pointer(packetRaw))
if ret := C.dtls_handle_incoming(d.dtlsSession, d.rawSrc, d.rawDst, packetRaw, C.int(len(packet))); ret != nil {
certPair = &CertPair{
ClientWriteKey: []byte(C.GoStringN(&ret.client_write_key[0], ret.key_length)),
ServerWriteKey: []byte(C.GoStringN(&ret.server_write_key[0], ret.key_length)),
Profile: C.GoString(&ret.profile[0]),
}
C.free(unsafe.Pointer(ret))
}
return certPair
}
// DoHandshake sends the DTLS handshake it the remote peer
func (d *State) DoHandshake() {
C.dtls_do_handshake(d.dtlsSession, d.rawSrc, d.rawDst)
}
// AddListener adds the socket to a map that can be accessed by OpenSSL for sending
// This only needed until DTLS is rewritten in native Go
func AddListener(src string, conn *ipv4.PacketConn) {
listenerMapLock.Lock()
listenerMap[src] = conn
listenerMapLock.Unlock()
}
// RemoveListener removes the socket from a map that can be accessed by OpenSSL for sending
// This only needed until DTLS is rewritten in native Go
func RemoveListener(src string) {
}