diff --git a/modules/dtls_gw/README.md b/modules/dtls_gw/README.md index 4ff49e697..ee6591709 100644 --- a/modules/dtls_gw/README.md +++ b/modules/dtls_gw/README.md @@ -7,8 +7,8 @@ key negotiation and SRTP encryption/decryption. ## Command Protocol -The module supports 5 basic commands, those can be combined with the -respective U or L command with sub-command extension (&&) and module +The module supports 6 basic commands, those can be combined with the +respective U, L or Q command with sub-command extension (&&) and module index:instance (e.g. " && M4:1" for the case of just a single instance loaded). @@ -42,6 +42,26 @@ loaded). Detach and destroy DTLS context created by the `S` command. + - `Q` + + Query the incoming stream's DTLS state without creating or reconfiguring + an endpoint. Returns `none` when no endpoint is attached, otherwise `init`, + `connecting`, `up`, or `dead`. `up` means the peer fingerprint has been + verified and both SRTP contexts are installed. State is read under the + same lock used by the handshake and media paths. + + Use as a subcommand of the session query, for example: + + ``` + Q CALL-ID FROM-TAG TO-TAG npkts_ina && M4:1 Q + ``` + + The response ends with ` && STATE`. The query selects the incoming + stream for `FROM-TAG`; swap the tags to inspect the other leg. Poll both + DTLS peers with a bounded timeout before starting loss-sensitive traffic: + completing the signaling commands does not mean the handshake is done. + The returned state is a snapshot, not a guarantee against later failure. + ## Supported Scenarios with examples: 1. Incoming INVITE with the RTP/AVP. 183/200 OK with UDP/TLS/RTP/SAVP. diff --git a/modules/dtls_gw/rtpp_dtls_conn.c b/modules/dtls_gw/rtpp_dtls_conn.c index 75d4eb65c..3631c2024 100644 --- a/modules/dtls_gw/rtpp_dtls_conn.c +++ b/modules/dtls_gw/rtpp_dtls_conn.c @@ -169,6 +169,7 @@ static struct res_loc rtpp_dtls_conn_srtp_recv(struct rtpp_dtls_conn *, static enum rtpp_dtls_mode rtpp_dtls_conn_setmode(struct rtpp_dtls_conn *, const struct rdc_peer_spec *rdfsp); static void rtpp_dtls_conn_godead(struct rtpp_dtls_conn *); +static const char *rtpp_dtls_conn_getstate(struct rtpp_dtls_conn *); static int tls_srtp_keyinfo(SSL *, const struct srtp_crypto_suite **, uint8_t *, size_t, uint8_t *, size_t, struct rtpp_log *); @@ -180,6 +181,7 @@ DEFINE_SMETHODS(rtpp_dtls_conn, .srtp_recv = &rtpp_dtls_conn_srtp_recv, .setmode = &rtpp_dtls_conn_setmode, .godead = &rtpp_dtls_conn_godead, + .getstate = &rtpp_dtls_conn_getstate, ); static void @@ -275,6 +277,26 @@ rtpp_dtls_conn_pickmode(enum rtpp_dtls_mode peer_mode) /* Not Reached */ } +/* Report readiness only after fingerprint verification and both SRTP contexts. */ +static const char * +rtpp_dtls_conn_getstate(struct rtpp_dtls_conn *self) +{ + struct rtpp_dtls_conn_priv *pvt; + enum rdc_state state; + static const char * const names[] = { + [RDC_INIT] = "init", + [RDC_CONNECTING] = "connecting", + [RDC_UP] = "up", + [RDC_DEAD] = "dead" + }; + + PUB2PVT(self, pvt); + pthread_mutex_lock(&pvt->state_lock); + state = pvt->state; + pthread_mutex_unlock(&pvt->state_lock); + return (names[state]); +} + static enum rtpp_dtls_mode rtpp_dtls_conn_setmode(struct rtpp_dtls_conn *self, const struct rdc_peer_spec *rdfsp) @@ -381,6 +403,12 @@ rtpp_dtls_conn_dtls_recv(struct rtpp_dtls_conn *self, switch (pvt->state) { case RDC_INIT: + /* + * No peer fingerprint until the A or P command, so the handshake + * could not be verified. Drop it, the peer retransmits. + */ + if (pvt->fingerprint[0] == '\0') + goto out; pvt->state = RDC_CONNECTING; break; case RDC_CONNECTING: @@ -429,8 +457,7 @@ rtpp_dtls_conn_dtls_recv(struct rtpp_dtls_conn *self, goto godead; } - if (pvt->fingerprint[0] != '\0' && - strcmp(pvt->fingerprint, srv_fingerprint) != 0) { + if (strcmp(pvt->fingerprint, srv_fingerprint) != 0) { RTPP_LOG(pvt->mself->log, RTPP_LOG_ERR, "fingerprint verification failed"); goto godead; } diff --git a/modules/dtls_gw/rtpp_dtls_conn.h b/modules/dtls_gw/rtpp_dtls_conn.h index 5acf5bf80..191bf04af 100644 --- a/modules/dtls_gw/rtpp_dtls_conn.h +++ b/modules/dtls_gw/rtpp_dtls_conn.h @@ -66,6 +66,7 @@ DEFINE_METHOD(rtpp_dtls_conn, rtpp_dtls_conn_srtp_recv, struct res_loc, DEFINE_METHOD(rtpp_dtls_conn, rtpp_dtls_conn_setmode, enum rtpp_dtls_mode, const struct rdc_peer_spec *); DEFINE_METHOD(rtpp_dtls_conn, rtpp_dtls_conn_godead, void); +DEFINE_METHOD(rtpp_dtls_conn, rtpp_dtls_conn_getstate, const char *); DECLARE_SMETHODS(rtpp_dtls_conn) { @@ -74,6 +75,7 @@ DECLARE_SMETHODS(rtpp_dtls_conn) METHOD_ENTRY(rtpp_dtls_conn_srtp_recv, srtp_recv); METHOD_ENTRY(rtpp_dtls_conn_setmode, setmode); METHOD_ENTRY(rtpp_dtls_conn_godead, godead); + METHOD_ENTRY(rtpp_dtls_conn_getstate, getstate); }; DECLARE_CLASS_PUBTYPE(rtpp_dtls_conn, {}); diff --git a/modules/dtls_gw/rtpp_dtls_gw.c b/modules/dtls_gw/rtpp_dtls_gw.c index 4cefabf2b..574ce1c24 100644 --- a/modules/dtls_gw/rtpp_dtls_gw.c +++ b/modules/dtls_gw/rtpp_dtls_gw.c @@ -269,7 +269,7 @@ rtpp_dtls_gw_setup_sender(struct rtpp_module_priv *pvt, return (0); } -enum rdg_cmd {RDG_CMD_A, RDG_CMD_P, RDG_CMD_S, RDG_CMD_D, RDG_CMD_U}; +enum rdg_cmd {RDG_CMD_A, RDG_CMD_P, RDG_CMD_S, RDG_CMD_D, RDG_CMD_U, RDG_CMD_Q}; static int rtpp_dtls_gw_handle_command(struct rtpp_module_priv *pvt, @@ -308,6 +308,13 @@ rtpp_dtls_gw_handle_command(struct rtpp_module_priv *pvt, rdg_cmd = RDG_CMD_P; break; + case 'q': + case 'Q': + if (argc != 1) + goto invalmode; + rdg_cmd = RDG_CMD_Q; + break; + case 's': case 'S': if (argc != 1) @@ -349,6 +356,7 @@ rtpp_dtls_gw_handle_command(struct rtpp_module_priv *pvt, rdfs.ssrc = (argc == 4) ? &argv[3] : NULL; rdfsp = &rdfs; /* Fallthrough */ + case RDG_CMD_Q: case RDG_CMD_D: dtls_strmp = ctxp->env->strmp_in; break; @@ -368,6 +376,15 @@ rtpp_dtls_gw_handle_command(struct rtpp_module_priv *pvt, rtps_c = dtls_in_poi.arg; } + /* A query must not allocate a connection or alter its mode/fingerprint. */ + if (rdg_cmd == RDG_CMD_Q) { + strlcpy(ctxp->resp->buf_t, lookup_res == 0 ? "none" : + CALL_SMETHOD(rtps_c->dtls_conn, getstate), sizeof(ctxp->resp->buf_t)); + if (lookup_res == 0) + return (0); + goto out; + } + if (rdg_cmd == RDG_CMD_D || rdg_cmd == RDG_CMD_U) { if (lookup_res == 0) { return (-1); diff --git a/python/RPTL/examples/dtls.ready.rptl b/python/RPTL/examples/dtls.ready.rptl new file mode 100644 index 000000000..15923a757 --- /dev/null +++ b/python/RPTL/examples/dtls.ready.rptl @@ -0,0 +1,11 @@ +# Q selects the incoming stream for the named tag: reverse tags for the peer. +# Check every endpoint: one peer being ready does not imply the other is. +fwd: Q %%CALLID_F%% %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, F4A_STATE) +fwd: Q %%CALLID_F%% %%TT%% %%FT%% npkts_ina && M4:1 Q -> str_split(&&, _, F4O_STATE) +fwd: Q %%CALLID_F%%_ipv6 %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, F6A_STATE) +fwd: Q %%CALLID_F%%_ipv6 %%TT%% %%FT%% npkts_ina && M4:1 Q -> str_split(&&, _, F6O_STATE) +geno: Q %%CALLID_O%% %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, O4_STATE) +geno: Q %%CALLID_O%%_ipv6 %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, O6_STATE) +gena: Q %%CALLID_A%% %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, A4_STATE) +gena: Q %%CALLID_A%%_ipv6 %%FT%% %%TT%% npkts_ina && M4:1 Q -> str_split(&&, _, A6_STATE) +.eval: %%F4A_STATE%% %%F4O_STATE%% %%F6A_STATE%% %%F6O_STATE%% %%O4_STATE%% %%O6_STATE%% %%A4_STATE%% %%A6_STATE%% -> str_equal(DTLS_EXPECTED, DTLS_READY) diff --git a/python/RPTL/examples/dtls.scenario.rptl b/python/RPTL/examples/dtls.scenario.rptl index 7a3383319..9d8fd03a4 100644 --- a/python/RPTL/examples/dtls.scenario.rptl +++ b/python/RPTL/examples/dtls.scenario.rptl @@ -48,7 +48,9 @@ gena: R %%CALLID_A%%_ipv6 %%FT%% %%TT%% -> str_compare(RES_OK) geno: R %%CALLID_O%% %%FT%% %%TT%% -> str_compare(RES_OK) geno: R %%CALLID_O%%_ipv6 %%FT%% %%TT%% -> str_compare(RES_OK) -.sleep: 0.1 +# Wait for verified SRTP keys at both ends, not a fixed handshake delay. +.eval: none up none up up up none none -> DTLS_EXPECTED +.until: DTLS_READY do dtls.ready.rptl every 0.05 10 .echo: Starting streaming... geno: P%%NTIMES%% %%CALLID_O%% %%PROMPT_NAME%% %%CODEC%% %%FT%% %%TT%% -> str_compare(RES_OK) @@ -78,16 +80,18 @@ fwd: L %%CALLID_F%% 127.0.0.1 %%PORTO_IPv4%% %%FT%% %%TT%% && M4:1 S -> str_spli .eval: %%DTLS_PARAMS%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) gena: Uc%%CODEC%% %%CALLID_A%% 127.0.0.1 %%PORT2_IPv4%% %%FT%% && M4:1 P %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> validate_port(_) -fwd: U6 %%CALLID_F%%_ipv6 ::1 %%PORTA_IPv6%% %%TT%% %%FT%% && M4:1 S -> str_split(&&, _, DTLS_PARAMS) +fwd: U6 %%CALLID_F%%_ipv6 ::1 %%PORTA_IPv6%% %%TT%% %%FT%% && M4:1 S -> str_split(&&, _, DTLS_PARAMS_AB) .eval: %%_%% -> str_compare(PORT2_IPv6) -.eval: %%DTLS_PARAMS%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) -gena: U6c%%CODEC%% %%CALLID_A%%_ipv6 ::1 %%PORT2_IPv6%% %%FT%% && M4:1 P %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> validate_port(_) -gena: L6z60 %%CALLID_A%%_ipv6 %%TRASH_IPv6%% %%TRASH_PORT%% %%FT%% %%TT%% && M4:1 S -> str_split(&&, _, DTLS_PARAMS) +gena: L6z60 %%CALLID_A%%_ipv6 %%TRASH_IPv6%% %%TRASH_PORT%% %%FT%% %%TT%% && M4:1 S -> str_split(&&, _, DTLS_PARAMS_BA) .eval: %%_%% -> str_compare(PORTA_IPv6) -.eval: %%DTLS_PARAMS%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) +.eval: %%DTLS_PARAMS_BA%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) +fwd: U6 %%CALLID_F%%_ipv6 ::1 %%PORTA_IPv6%% %%FT%% && M4:1 A %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> str_compare(PORT1_IPv6) fwd: L6 %%CALLID_F%%_ipv6 ::1 %%PORTO_IPv6%% %%FT%% %%TT%% && M4:1 A %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> str_compare(PORT2_IPv6) +.eval: %%DTLS_PARAMS_AB%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) +gena: U6c%%CODEC%% %%CALLID_A%%_ipv6 ::1 %%PORT2_IPv6%% %%FT%% && M4:1 P %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> validate_port(_) -.sleep: 0.1 +.eval: up up up up up up up up -> DTLS_EXPECTED +.until: DTLS_READY do dtls.ready.rptl every 0.05 10 .echo: Starting streaming... geno: P%%NTIMES%% %%CALLID_O%% %%PROMPT_NAME%% %%CODEC%% %%FT%% %%TT%% -> str_compare(RES_OK) diff --git a/tests/Makefile.in b/tests/Makefile.in index be6ca389a..8f60a8e77 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -171,7 +171,7 @@ host_triplet = @host@ @BUILD_CRYPTO_TRUE@@ENABLE_MEMGLITCHING_TRUE@am__append_7 = memdeb/glitch_dtls_gw @ENABLE_MEMGLITCHING_TRUE@am__append_8 = ${memdeb_CLEANFILES} @ENABLE_MEMGLITCHING_TRUE@am__append_9 = ${memdeb_EXTRA_DIST} -@BUILD_CRYPTO_TRUE@@ENABLE_BASIC_TESTS_TRUE@am__append_10 = dtls_gw/basic +@BUILD_CRYPTO_TRUE@@ENABLE_BASIC_TESTS_TRUE@am__append_10 = dtls_gw/basic dtls_gw/fingerprint @BUILD_CRYPTO_TRUE@am__append_11 = ${dtls_gw_CLEANFILES} @BUILD_CRYPTO_TRUE@am__append_12 = ${dtls_gw_EXTRA_DIST} subdir = tests @@ -471,7 +471,7 @@ EXTRA_DIST = $(am__append_3) $(am__append_5) $(am__append_9) \ @ENABLE_MEMGLITCHING_TRUE@memdeb_EXTRA_DIST = memdeb @ENABLE_MEMGLITCHING_TRUE@memdeb_CLEANFILES = glitch1.* @BUILD_CRYPTO_TRUE@dtls_gw_EXTRA_DIST = dtls_gw -@BUILD_CRYPTO_TRUE@dtls_gw_CLEANFILES = dtls_gw.* +@BUILD_CRYPTO_TRUE@dtls_gw_CLEANFILES = dtls_gw.* dtls_fingerprint.* # NB: AM_TESTS_ENVIRONMENT not available until automake 1.12 TESTS_ENVIRONMENT = \ BASEDIR=${abs_srcdir} ; export BASEDIR ; \ diff --git a/tests/dtls_gw/Makefile.ami b/tests/dtls_gw/Makefile.ami index e24e00263..1abe2b139 100644 --- a/tests/dtls_gw/Makefile.ami +++ b/tests/dtls_gw/Makefile.ami @@ -1,7 +1,7 @@ dtls_gw_EXTRA_DIST = dtls_gw -dtls_gw_CLEANFILES = dtls_gw.* +dtls_gw_CLEANFILES = dtls_gw.* dtls_fingerprint.* if ENABLE_BASIC_TESTS -TESTS += dtls_gw/basic +TESTS += dtls_gw/basic dtls_gw/fingerprint endif CLEANFILES += ${dtls_gw_CLEANFILES} EXTRA_DIST += ${dtls_gw_EXTRA_DIST} diff --git a/tests/dtls_gw/basic.rptl b/tests/dtls_gw/basic.rptl index d72ea72fa..8564d7f86 100644 --- a/tests/dtls_gw/basic.rptl +++ b/tests/dtls_gw/basic.rptl @@ -12,6 +12,8 @@ socket fwd: stdio: %%RTPPROXY_ARGS%% -i -> dtls_gw.f.creplies .eval: dtls_gw.o.rlog -> RTPPROXY_STDERR socket geno: stdio: %%RTPPROXY_ARGS%% -P -r %%RECORD_DIR%% -S %%RECORD_SPL_DIR%% -> dtls_gw.o.creplies .echo: OK: rtpproxy GenO started +.eval: 0 -> RES_OK +.include: state.rptl .include: %%SCENARIO%% .include: oversize.rptl .close: geno diff --git a/tests/dtls_gw/fingerprint b/tests/dtls_gw/fingerprint new file mode 100755 index 000000000..754cc61c0 --- /dev/null +++ b/tests/dtls_gw/fingerprint @@ -0,0 +1,23 @@ +#!/bin/sh + +BASEDIR="${BASEDIR:-$(dirname -- $0)/..}" +BASEDIR="$(readlink -f -- $BASEDIR)" +. "${BASEDIR}/functions" + +{ ${RPTL_INT} -s "${BASEDIR}/dtls_gw/fingerprint.rptl" \ + -D RTPPROXY_BIN="`strip_wrapper "${RTPPROXY}"`" \ + -D DSO="${TOP_BUILDDIR}/modules/dtls_gw/.libs/rtpp_dtls_gw_debug.so" \ + -D PROBE_SCRIPT="${BASEDIR}/dtls_gw/preauth_probe.py" \ + 2>&1 > dtls_fingerprint.vars + echo ${?} > dtls_fingerprint.rptl.rc +} | tee dtls_fingerprint.rlog +RPTL_RC=`cat dtls_fingerprint.rptl.rc` + +for logfile in dtls_fingerprint.?.rlog +do + if [ -e "${logfile}" ] + then + cat "${logfile}" >> dtls_fingerprint.rlog + fi +done +report_rc ${RPTL_RC} "DTLS peer-fingerprint security regression" diff --git a/tests/dtls_gw/fingerprint.rptl b/tests/dtls_gw/fingerprint.rptl new file mode 100644 index 000000000..d5c6be16b --- /dev/null +++ b/tests/dtls_gw/fingerprint.rptl @@ -0,0 +1,18 @@ +.eval: -T30 -p /dev/null --bridge_symmetric -d dbug -l 0.0.0.0 -6 /:: --dso %%DSO%% -> RTPPROXY_ARGS +.eval: dtls_fingerprint.f.rlog -> RTPPROXY_STDERR +socket fwd: stdio: %%RTPPROXY_ARGS%% -i +.eval: dtls_fingerprint.o.rlog -> RTPPROXY_STDERR +socket geno: stdio: %%RTPPROXY_ARGS%% +.eval: 0 -> RES_OK +.eval: U -> PA_U +.eval: L -> PA_L +.eval: 127.0.0.1 -> PA_HOST +.include: preauth.rptl +.eval: U6 -> PA_U +.eval: L6 -> PA_L +.eval: ::1 -> PA_HOST +.include: preauth.rptl +# A configured but mismatching peer must still fail certificate verification. +.include: state.rptl +.close: geno +.close: fwd diff --git a/tests/dtls_gw/oversize.ready.rptl b/tests/dtls_gw/oversize.ready.rptl new file mode 100644 index 000000000..395c220ca --- /dev/null +++ b/tests/dtls_gw/oversize.ready.rptl @@ -0,0 +1,3 @@ +fwd: Q %%OS_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, OS_F_STATE) +geno: Q %%OS_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, OS_O_STATE) +.eval: %%OS_F_STATE%% %%OS_O_STATE%% -> str_equal(OS_DTLS_EXPECTED, OS_DTLS_READY) diff --git a/tests/dtls_gw/oversize.rptl b/tests/dtls_gw/oversize.rptl index b3a018acb..4f01a0d00 100644 --- a/tests/dtls_gw/oversize.rptl +++ b/tests/dtls_gw/oversize.rptl @@ -7,7 +7,8 @@ geno: U %%OS_CALLID%% 127.0.0.1 %%OS_PORT1%% from_tag_1 && M4:1 A %%DTLS_DIGALG% geno: L %%OS_CALLID%% 0.0.0.0 0 from_tag_1 to_tag_1 && M4:1 S -> str_split(&&, OS_PORTO, DTLS_PARAMS) .eval: %%DTLS_PARAMS%% -> str_split( , DTLS_MODE, DTLS_DIGALG, DTLS_DIGSUM) fwd: L %%OS_CALLID%% 127.0.0.1 %%OS_PORTO%% from_tag_1 to_tag_1 && M4:1 P %%DTLS_DIGALG%% %%DTLS_DIGSUM%% -> validate_port(OS_PORT2) -.sleep: 1 +.eval: up up -> OS_DTLS_EXPECTED +.until: OS_DTLS_READY do oversize.ready.rptl every 0.05 10 .exec: %%PYINT%% -c "import socket; s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM); [s.sendto(bytes.fromhex('8000%04x%08x00000001' % (q, q * 160))+bytes(n),('127.0.0.1',%%OS_PORT2%%)) for q, n in ((1, 160), (2, 9000), (3, 160))]" .eval: npkts_ina=3 nrelayed=2 ndropped=1 -> OS_EXPECTED .until: OS_DONE do oversize.check.rptl every 0.1 5 diff --git a/tests/dtls_gw/preauth.check.rptl b/tests/dtls_gw/preauth.check.rptl new file mode 100644 index 000000000..74ce7ce1d --- /dev/null +++ b/tests/dtls_gw/preauth.check.rptl @@ -0,0 +1,3 @@ +geno: Q %%PA_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, PA_O) +fwd: Q %%PA_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, PA_F) +.eval: %%PA_O%% %%PA_F%% -> str_equal(PA_READY, PA_DONE) diff --git a/tests/dtls_gw/preauth.rptl b/tests/dtls_gw/preauth.rptl new file mode 100644 index 000000000..f1d92e809 --- /dev/null +++ b/tests/dtls_gw/preauth.rptl @@ -0,0 +1,31 @@ +# Exercise S before A with a real self-signed DTLS peer, on the wire. +.eval: dtls_preauth_%%PA_U%% -> PA_CALLID +.eval: PREAUTH_CLIENTHELLOS_IGNORED -> PA_IGNORED +.eval: AUTHORIZED -> PA_AUTHORIZED +.eval: connecting init -> PA_BLOCKED +.eval: up up -> PA_READY +fwd: %%PA_U%% %%PA_CALLID%% %%PA_HOST%% 0 from_tag_1 && M4:1 S -> str_split(&&, PA_PORTF, PA_FP) +.eval: %%PA_FP%% -> str_split( , _, PA_FALG, PA_FSUM) +.pyspawn: %%PROBE_SCRIPT%% %%PA_HOST%% %%PA_PORTF%% -> preauth_probe +.readline: preauth_probe 5 -> validate_port(PA_RELAY) +fwd: %%PA_L%% %%PA_CALLID%% %%PA_HOST%% %%PA_RELAY%% from_tag_1 to_tag_1 -> validate_port(_) +geno: %%PA_U%% %%PA_CALLID%% %%PA_HOST%% %%PA_RELAY%% from_tag_1 -> validate_port(_) +geno: %%PA_L%% %%PA_CALLID%% %%PA_HOST%% 0 from_tag_1 to_tag_1 && M4:1 S -> str_split(&&, _, PA_FP) +.eval: %%PA_FP%% -> str_split( , _, PA_OALG, PA_OSUM) +geno: %%PA_U%% %%PA_CALLID%% %%PA_HOST%% %%PA_RELAY%% from_tag_1 && M4:1 P %%PA_FALG%% %%PA_FSUM%% -> validate_port(_) +# Do not check init immediately after signaling: wait for actual retries. +# The relay fails on ANY server response, even a handshake that later fails. +.readline: preauth_probe 10 -> str_compare(PA_IGNORED) +geno: Q %%PA_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, PA_O) +fwd: Q %%PA_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, PA_F) +.eval: %%PA_O%% %%PA_F%% -> str_compare(PA_BLOCKED) +.kill: preauth_probe USR1 +.readline: preauth_probe 5 -> str_compare(PA_AUTHORIZED) +# The SAME peer must recover on retransmission with the matching fingerprint. +fwd: %%PA_L%% %%PA_CALLID%% %%PA_HOST%% %%PA_RELAY%% from_tag_1 to_tag_1 && M4:1 A %%PA_OALG%% %%PA_OSUM%% -> validate_port(_) +.until: PA_DONE do preauth.check.rptl every 0.05 10 +.kill: preauth_probe TERM +.wait: preauth_probe 5 +fwd: D %%PA_CALLID%% from_tag_1 to_tag_1 -> str_compare(RES_OK) +geno: D %%PA_CALLID%% from_tag_1 to_tag_1 -> str_compare(RES_OK) +.echo: OK: DTLS before fingerprint blocked, then matching peer recovered (%%PA_U%%) diff --git a/tests/dtls_gw/preauth_probe.py b/tests/dtls_gw/preauth_probe.py new file mode 100644 index 000000000..4031c780e --- /dev/null +++ b/tests/dtls_gw/preauth_probe.py @@ -0,0 +1,55 @@ +"""Relay a real DTLS peer and reject any server reply before authorization. + +Three ClientHello flights prove the peer reached the relay and retransmitted; +a disconnected peer or a process that never starts cannot make the test pass. +SIGUSR1 opens the return path only after the runner observes this boundary. +""" +import signal +import socket +import sys +import time + +host, target_port = sys.argv[1:] +family = socket.AF_INET6 if ':' in host else socket.AF_INET +authorized = False + + +def authorize(signum, frame): + global authorized + authorized = True + print('AUTHORIZED', flush=True) + + +signal.signal(signal.SIGUSR1, authorize) +signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit(0)) +with socket.socket(family, socket.SOCK_DGRAM) as relay: + relay.bind((host, 0)) + relay.settimeout(0.1) + target = (host, int(target_port)) + peer = None + flights = 0 + deadline = time.monotonic() + 30 + print(relay.getsockname()[1], flush=True) + while time.monotonic() < deadline: + try: + packet, source = relay.recvfrom(65535) + except socket.timeout: + continue + if source[:2] == target: + if not authorized: + print('FAIL: DTLS response before peer fingerprint', flush=True) + sys.exit(1) + assert peer is not None, 'server answered before a ClientHello' + relay.sendto(packet, peer) + continue + if peer is None: + peer = source + assert source == peer, 'unexpected additional DTLS peer' + if not authorized: + assert len(packet) > 13 and packet[0] == 22 and packet[13] == 1, \ + 'expected a real DTLS ClientHello' + flights += 1 + relay.sendto(packet, target) + if not authorized and flights == 3: + print('PREAUTH_CLIENTHELLOS_IGNORED', flush=True) + raise TimeoutError('DTLS preauthorization probe was not cleaned up') diff --git a/tests/dtls_gw/state.check.rptl b/tests/dtls_gw/state.check.rptl new file mode 100644 index 000000000..fe2dbc391 --- /dev/null +++ b/tests/dtls_gw/state.check.rptl @@ -0,0 +1,3 @@ +geno: Q %%STATE_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, STATE_O) +fwd: Q %%STATE_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, STATE_F) +.eval: %%STATE_O%% %%STATE_F%% -> str_equal(STATE_REJECTED, STATE_DONE) diff --git a/tests/dtls_gw/state.rptl b/tests/dtls_gw/state.rptl new file mode 100644 index 000000000..05c4fcd62 --- /dev/null +++ b/tests/dtls_gw/state.rptl @@ -0,0 +1,36 @@ +# Query absent, unconfigured, connecting and rejected endpoints without +# sending media. The missing-fingerprint gate must remain closed. +.eval: dtls_state -> STATE_CALLID +.eval: none -> STATE_NONE +.eval: init -> STATE_INIT +.eval: connecting init -> STATE_PENDING +.eval: up dead -> STATE_REJECTED +.eval: -1 -> STATE_ERROR +.eval: actpass -> STATE_ACTPASS +fwd: U %%STATE_CALLID%% 0.0.0.0 0 from_tag_1 -> validate_port(_) +fwd: L %%STATE_CALLID%% 0.0.0.0 0 from_tag_1 to_tag_1 -> validate_port(_) +fwd: Q %%STATE_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, STATE) +.eval: %%STATE%% -> str_compare(STATE_NONE) +fwd: U %%STATE_CALLID%% 0.0.0.0 0 from_tag_1 && M4:1 S -> str_split(&&, STATE_PORTF, DTLS_PARAMS) +.eval: %%DTLS_PARAMS%% -> str_split( , STATE_MODE, STATE_ALG, STATE_FP) +fwd: Q %%STATE_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 q -> str_split(&&, _, STATE) +.eval: %%STATE%% -> str_compare(STATE_INIT) +fwd: Q %%STATE_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q bad args -> str_split(&&, _, STATE) +.eval: %%STATE%% -> str_compare(STATE_ERROR) +fwd: Q %%STATE_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 S -> str_split(&&, _, DTLS_PARAMS) +.eval: %%DTLS_PARAMS%% -> str_split( , STATE_MODE, STATE_ALG, STATE_FP) +.eval: %%STATE_MODE%% -> str_compare(STATE_ACTPASS) +# GenO is active, Fwd has no fingerprint and must not answer yet. +geno: U %%STATE_CALLID%% 127.0.0.1 %%STATE_PORTF%% from_tag_1 -> validate_port(STATE_PORTO) +geno: L %%STATE_CALLID%% 0.0.0.0 0 from_tag_1 to_tag_1 && M4:1 S -> str_split(&&, _, DTLS_PARAMS) +.eval: %%DTLS_PARAMS%% -> str_split( , _, STATE_OALG, STATE_OFP) +geno: U %%STATE_CALLID%% 127.0.0.1 %%STATE_PORTF%% from_tag_1 && M4:1 P %%STATE_ALG%% %%STATE_FP%% -> validate_port(_) +geno: Q %%STATE_CALLID%% from_tag_1 to_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, STATE_O) +fwd: Q %%STATE_CALLID%% to_tag_1 from_tag_1 npkts_ina && M4:1 Q -> str_split(&&, _, STATE_F) +.eval: %%STATE_O%% %%STATE_F%% -> str_compare(STATE_PENDING) +# Supply an intentionally wrong peer fingerprint: Fwd must report dead, +# never up. Querying it earlier must not have keyed or reconfigured it. +fwd: L %%STATE_CALLID%% 127.0.0.1 %%STATE_PORTO%% from_tag_1 to_tag_1 && M4:1 A %%STATE_OALG%% 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 -> validate_port(_) +.until: STATE_DONE do state.check.rptl every 0.05 10 +fwd: D %%STATE_CALLID%% from_tag_1 to_tag_1 -> str_compare(RES_OK) +geno: D %%STATE_CALLID%% from_tag_1 to_tag_1 -> str_compare(RES_OK)