Skip to content

Commit 8821982

Browse files
committed
add test for cryptex with no hdr xtn
Verify that an error is return in this invalid use case. Also fix up based on review.
1 parent 7ed0ea9 commit 8821982

File tree

4 files changed

+115
-25
lines changed

4 files changed

+115
-25
lines changed

include/srtp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ typedef enum {
216216
srtp_err_status_pkt_idx_adv = 27, /**< packet index advanced, reset */
217217
/**< needed */
218218
srtp_err_status_buffer_small = 28, /**< out buffer is too small */
219+
srtp_err_status_cryptex_err = 29, /**< unsupported cryptex operation */
219220
} srtp_err_status_t;
220221

221222
typedef struct srtp_ctx_t_ srtp_ctx_t;

srtp/srtp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ static srtp_err_status_t srtp_cryptex_protect_init(
168168
bool *inplace,
169169
size_t *enc_start)
170170
{
171-
if (stream->use_cryptex && stream->rtp_services & sec_serv_conf) {
171+
if (stream->use_cryptex && (stream->rtp_services & sec_serv_conf)) {
172172
if (hdr->cc && hdr->x == 0) {
173173
/* Cryptex can only encrypt CSRCS if header extension is present */
174-
return srtp_err_status_parse_err;
174+
return srtp_err_status_cryptex_err;
175175
}
176176
*inuse = hdr->x == 1;
177177
} else {
@@ -2122,7 +2122,7 @@ static srtp_err_status_t srtp_protect_aead(srtp_ctx_t *ctx,
21222122
if (cryptex_inuse && !cryptex_inplace && hdr->cc) {
21232123
debug_print0(mod_srtp,
21242124
"unsupported cryptex mode, AEAD, CC and not inplace io");
2125-
return srtp_err_status_bad_param;
2125+
return srtp_err_status_cryptex_err;
21262126
}
21272127

21282128
/* note: the passed size is without the auth tag */
@@ -2302,7 +2302,7 @@ static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx,
23022302
if (cryptex_inuse && !cryptex_inplace && hdr->cc) {
23032303
debug_print0(mod_srtp,
23042304
"unsupported cryptex mode, AEAD, CC and not inplace io");
2305-
return srtp_err_status_bad_param;
2305+
return srtp_err_status_cryptex_err;
23062306
}
23072307

23082308
if (enc_start > srtp_len - tag_len - stream->mki_size) {

test/srtp_driver.c

Lines changed: 109 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ srtp_err_status_t srtp_test_roc_mismatch(void);
112112

113113
srtp_err_status_t srtp_test_set_sender_roc(void);
114114

115+
srtp_err_status_t srtp_test_cryptex_csrc_but_no_extension_header(void);
116+
115117
double srtp_bits_per_second(size_t msg_len_octets, const srtp_policy_t *policy);
116118

117119
double srtp_rejections_per_second(size_t msg_len_octets,
@@ -886,6 +888,15 @@ int main(int argc, char *argv[])
886888
printf("failed\n");
887889
exit(1);
888890
}
891+
892+
printf("testing cryptex_csrc_but_no_extension_header()...");
893+
if (srtp_test_cryptex_csrc_but_no_extension_header() ==
894+
srtp_err_status_ok) {
895+
printf("passed\n");
896+
} else {
897+
printf("failed\n");
898+
exit(1);
899+
}
889900
}
890901

891902
if (do_stream_list) {
@@ -2152,30 +2163,58 @@ char *srtp_packet_to_string(uint8_t *packet, size_t pkt_octet_len)
21522163
srtp_hdr_t *hdr = (srtp_hdr_t *)packet;
21532164
size_t octets_in_rtp_header = 12;
21542165
uint8_t *data = packet + octets_in_rtp_header;
2155-
size_t hex_len = pkt_octet_len - octets_in_rtp_header;
2166+
size_t data_len = pkt_octet_len - octets_in_rtp_header;
21562167

21572168
/* sanity checking */
21582169
if ((hdr == NULL) || (pkt_octet_len > MTU)) {
21592170
return NULL;
21602171
}
21612172

21622173
/* write packet into string */
2163-
snprintf(packet_string, sizeof(packet_string),
2164-
"(s)rtp packet: {\n"
2165-
" version:\t%d\n"
2166-
" p:\t\t%d\n"
2167-
" x:\t\t%d\n"
2168-
" cc:\t\t%d\n"
2169-
" m:\t\t%d\n"
2170-
" pt:\t\t%x\n"
2171-
" seq:\t\t%x\n"
2172-
" ts:\t\t%x\n"
2173-
" ssrc:\t%x\n"
2174-
" data:\t%s\n"
2174+
int offset = snprintf(packet_string, sizeof(packet_string),
2175+
"(s)rtp packet: {\n"
2176+
" version:\t%d\n"
2177+
" p:\t\t%d\n"
2178+
" x:\t\t%d\n"
2179+
" cc:\t\t%d\n"
2180+
" m:\t\t%d\n"
2181+
" pt:\t\t%x\n"
2182+
" seq:\t\t%x\n"
2183+
" ts:\t\t%x\n"
2184+
" ssrc:\t%x",
2185+
hdr->version, hdr->p, hdr->x, hdr->cc, hdr->m,
2186+
hdr->pt, hdr->seq, hdr->ts, hdr->ssrc);
2187+
2188+
if (hdr->cc) {
2189+
offset += snprintf(packet_string + offset,
2190+
sizeof(packet_string) - offset, "\n csrc:\t");
2191+
for (int i = 0; i < hdr->cc; i++) {
2192+
offset +=
2193+
snprintf(packet_string + offset, sizeof(packet_string) - offset,
2194+
"%x ", *((&hdr->ssrc) + 1 + i));
2195+
}
2196+
data += 4 * hdr->cc;
2197+
data_len -= 4 * hdr->cc;
2198+
}
2199+
2200+
if (hdr->x) {
2201+
uint16_t profile = *((uint16_t *)data);
2202+
data += 2;
2203+
data_len -= 2;
2204+
uint16_t length = ntohs(*((uint16_t *)data)) * 4;
2205+
data += 2;
2206+
data_len -= 2;
2207+
offset += snprintf(packet_string + offset,
2208+
sizeof(packet_string) - offset, "\n xtn:\t\t%x %s",
2209+
profile, octet_string_hex_string(data, length));
2210+
data += length;
2211+
data_len -= length;
2212+
}
2213+
2214+
snprintf(packet_string + offset, sizeof(packet_string) - offset,
2215+
"\n data:\t%s\n"
21752216
"} (%zu octets in total)\n",
2176-
hdr->version, hdr->p, hdr->x, hdr->cc, hdr->m, hdr->pt, hdr->seq,
2177-
hdr->ts, hdr->ssrc, octet_string_hex_string(data, hex_len),
2178-
pkt_octet_len);
2217+
octet_string_hex_string(data, data_len), pkt_octet_len);
21792218

21802219
return packet_string;
21812220
}
@@ -2913,7 +2952,7 @@ srtp_err_status_t srtp_validate_cryptex(void)
29132952

29142953
// clang-format on
29152954

2916-
struct test_vectors_t vectors[6] = {
2955+
const struct test_vectors_t vectors[6] = {
29172956
{ "Plaintext packet with 1-byte header extension", srtp_1bytehdrext_ref,
29182957
srtp_1bytehdrext_cryptex },
29192958
{ "Plaintext packet with 2-byte header extension", srtp_2bytehdrext_ref,
@@ -2927,6 +2966,7 @@ srtp_err_status_t srtp_validate_cryptex(void)
29272966
{ "Plaintext packet with empty 2-byte header extension and CSRC fields",
29282967
srtp_2byte_empty_hdrext_cc_ref, srtp_2byte_empty_hdrext_cc_cryptex },
29292968
};
2969+
const size_t num_vectors = sizeof(vectors) / sizeof(vectors[0]);
29302970

29312971
srtp_t srtp_snd, srtp_recv;
29322972
size_t len, ref_len, enc_len;
@@ -2947,7 +2987,7 @@ srtp_err_status_t srtp_validate_cryptex(void)
29472987
policy.use_cryptex = true;
29482988
policy.next = NULL;
29492989

2950-
for (size_t i = 0; i < 6; ++i) {
2990+
for (size_t i = 0; i < num_vectors; ++i) {
29512991
uint8_t packet[1400];
29522992
uint8_t reference[1400];
29532993
uint8_t ciphertext[1400];
@@ -2999,6 +3039,53 @@ srtp_err_status_t srtp_validate_cryptex(void)
29993039
return srtp_err_status_ok;
30003040
}
30013041

3042+
srtp_err_status_t srtp_test_cryptex_csrc_but_no_extension_header(void)
3043+
{
3044+
// clang-format off
3045+
/* Plaintext packet with no header extension but CSRC fields. */
3046+
const char *srtp_cc_ref =
3047+
"820f1238"
3048+
"decafbad"
3049+
"cafebabe"
3050+
"0001e240"
3051+
"0000b26e"
3052+
"abababab"
3053+
"abababab"
3054+
"abababab"
3055+
"abababab";
3056+
// clang-format on
3057+
3058+
/*
3059+
* create a session with a single stream using the default srtp
3060+
* policy and with the SSRC value 0xcafebabe
3061+
*/
3062+
srtp_policy_t policy;
3063+
memset(&policy, 0, sizeof(policy));
3064+
srtp_crypto_policy_set_rtp_default(&policy.rtp);
3065+
srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
3066+
policy.ssrc.type = ssrc_specific;
3067+
policy.ssrc.value = 0xcafebabe;
3068+
policy.key = test_key;
3069+
policy.window_size = 128;
3070+
policy.allow_repeat_tx = 0;
3071+
policy.use_cryptex = true;
3072+
policy.next = NULL;
3073+
3074+
srtp_t srtp_snd;
3075+
CHECK_OK(srtp_create(&srtp_snd, &policy));
3076+
3077+
uint8_t packet[1400];
3078+
size_t packet_len =
3079+
hex_string_to_octet_string(packet, srtp_cc_ref, sizeof(packet)) / 2;
3080+
3081+
CHECK_RETURN(call_srtp_protect(srtp_snd, packet, &packet_len, 0),
3082+
srtp_err_status_cryptex_err);
3083+
3084+
CHECK_OK(srtp_dealloc(srtp_snd));
3085+
3086+
return srtp_err_status_ok;
3087+
}
3088+
30023089
#ifdef GCM
30033090
/*
30043091
* srtp_validate_gcm() verifies the correctness of libsrtp by comparing
@@ -3359,7 +3446,7 @@ srtp_err_status_t srtp_validate_gcm_cryptex(void)
33593446
"8b5207d8";
33603447
// clang-format on
33613448

3362-
struct test_vectors_t vectors[6] = {
3449+
const struct test_vectors_t vectors[6] = {
33633450
{ "Plaintext packet with 1-byte header extension", srtp_1bytehdrext_ref,
33643451
srtp_1bytehdrext_cryptex_gcm },
33653452
{ "Plaintext packet with 2-byte header extension", srtp_2bytehdrext_ref,
@@ -3375,6 +3462,7 @@ srtp_err_status_t srtp_validate_gcm_cryptex(void)
33753462
srtp_2byte_empty_hdrext_cc_ref,
33763463
srtp_2byte_empty_hdrext_cc_cryptex_gcm },
33773464
};
3465+
const size_t num_vectors = sizeof(vectors) / sizeof(vectors[0]);
33783466

33793467
srtp_t srtp_snd, srtp_recv;
33803468
size_t len, ref_len, enc_len;
@@ -3397,7 +3485,7 @@ srtp_err_status_t srtp_validate_gcm_cryptex(void)
33973485

33983486
CHECK_OK(srtp_create(&srtp_snd, &policy));
33993487

3400-
for (int i = 0; i < 6; ++i) {
3488+
for (size_t i = 0; i < num_vectors; ++i) {
34013489
uint8_t packet[1400];
34023490
uint8_t reference[1400];
34033491
uint8_t ciphertext[1400];
@@ -3424,7 +3512,7 @@ srtp_err_status_t srtp_validate_gcm_cryptex(void)
34243512
// the combination of cryptex, cc, GCM & not inplace is not
34253513
// supported
34263514
CHECK_RETURN(call_srtp_protect(srtp_snd, packet, &len, 0),
3427-
srtp_err_status_bad_param);
3515+
srtp_err_status_cryptex_err);
34283516
continue;
34293517
}
34303518
CHECK_OK(call_srtp_protect(srtp_snd, packet, &len, 0));

test/util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const char *err_status_string(srtp_err_status_t status)
8888
ERR_STATUS_STRING(pkt_idx_old);
8989
ERR_STATUS_STRING(pkt_idx_adv);
9090
ERR_STATUS_STRING(buffer_small);
91+
ERR_STATUS_STRING(cryptex_err);
9192
}
9293
return "unkown srtp_err_status";
9394
}

0 commit comments

Comments
 (0)