Skip to content

Commit a61a3e9

Browse files
committed
selftests: tls: add tests for zero-length records
Test various combinations of zero-length records. Unfortunately, kernel cannot be coerced into producing those, so hardcode the ciphertext messages in the test. Reviewed-by: Sabrina Dubroca <sd@queasysnail.net> Link: https://patch.msgid.link/20250820021952.143068-2-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 62708b9 commit a61a3e9

File tree

1 file changed

+295
-5
lines changed
  • tools/testing/selftests/net

1 file changed

+295
-5
lines changed

tools/testing/selftests/net/tls.c

Lines changed: 295 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,12 @@ static int tls_send_cmsg(int fd, unsigned char record_type,
181181
return sendmsg(fd, &msg, flags);
182182
}
183183

184-
static int tls_recv_cmsg(struct __test_metadata *_metadata,
185-
int fd, unsigned char record_type,
186-
void *data, size_t len, int flags)
184+
static int __tls_recv_cmsg(struct __test_metadata *_metadata,
185+
int fd, unsigned char *ctype,
186+
void *data, size_t len, int flags)
187187
{
188188
char cbuf[CMSG_SPACE(sizeof(char))];
189189
struct cmsghdr *cmsg;
190-
unsigned char ctype;
191190
struct msghdr msg;
192191
struct iovec vec;
193192
int n;
@@ -206,7 +205,20 @@ static int tls_recv_cmsg(struct __test_metadata *_metadata,
206205
EXPECT_NE(cmsg, NULL);
207206
EXPECT_EQ(cmsg->cmsg_level, SOL_TLS);
208207
EXPECT_EQ(cmsg->cmsg_type, TLS_GET_RECORD_TYPE);
209-
ctype = *((unsigned char *)CMSG_DATA(cmsg));
208+
if (ctype)
209+
*ctype = *((unsigned char *)CMSG_DATA(cmsg));
210+
211+
return n;
212+
}
213+
214+
static int tls_recv_cmsg(struct __test_metadata *_metadata,
215+
int fd, unsigned char record_type,
216+
void *data, size_t len, int flags)
217+
{
218+
unsigned char ctype;
219+
int n;
220+
221+
n = __tls_recv_cmsg(_metadata, fd, &ctype, data, len, flags);
210222
EXPECT_EQ(ctype, record_type);
211223

212224
return n;
@@ -2164,6 +2176,284 @@ TEST_F(tls, rekey_poll_delay)
21642176
}
21652177
}
21662178

2179+
struct raw_rec {
2180+
unsigned int plain_len;
2181+
unsigned char plain_data[100];
2182+
unsigned int cipher_len;
2183+
unsigned char cipher_data[128];
2184+
};
2185+
2186+
/* TLS 1.2, AES_CCM, data, seqno:0, plaintext: 'Hello world' */
2187+
static const struct raw_rec id0_data_l11 = {
2188+
.plain_len = 11,
2189+
.plain_data = {
2190+
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
2191+
0x72, 0x6c, 0x64,
2192+
},
2193+
.cipher_len = 40,
2194+
.cipher_data = {
2195+
0x17, 0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00,
2196+
0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0xa2, 0x33,
2197+
0xde, 0x8d, 0x94, 0xf0, 0x29, 0x6c, 0xb1, 0xaf,
2198+
0x6a, 0x75, 0xb2, 0x93, 0xad, 0x45, 0xd5, 0xfd,
2199+
0x03, 0x51, 0x57, 0x8f, 0xf9, 0xcc, 0x3b, 0x42,
2200+
},
2201+
};
2202+
2203+
/* TLS 1.2, AES_CCM, ctrl, seqno:0, plaintext: '' */
2204+
static const struct raw_rec id0_ctrl_l0 = {
2205+
.plain_len = 0,
2206+
.plain_data = {
2207+
},
2208+
.cipher_len = 29,
2209+
.cipher_data = {
2210+
0x16, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2211+
0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x38, 0x7b,
2212+
0xa6, 0x1c, 0xdd, 0xa7, 0x19, 0x33, 0xab, 0xae,
2213+
0x88, 0xe1, 0xd2, 0x08, 0x4f,
2214+
},
2215+
};
2216+
2217+
/* TLS 1.2, AES_CCM, data, seqno:0, plaintext: '' */
2218+
static const struct raw_rec id0_data_l0 = {
2219+
.plain_len = 0,
2220+
.plain_data = {
2221+
},
2222+
.cipher_len = 29,
2223+
.cipher_data = {
2224+
0x17, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2225+
0x00, 0x00, 0x00, 0x00, 0x00, 0xc5, 0x37, 0x90,
2226+
0x70, 0x45, 0x89, 0xfb, 0x5c, 0xc7, 0x89, 0x03,
2227+
0x68, 0x80, 0xd3, 0xd8, 0xcc,
2228+
},
2229+
};
2230+
2231+
/* TLS 1.2, AES_CCM, data, seqno:1, plaintext: 'Hello world' */
2232+
static const struct raw_rec id1_data_l11 = {
2233+
.plain_len = 11,
2234+
.plain_data = {
2235+
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
2236+
0x72, 0x6c, 0x64,
2237+
},
2238+
.cipher_len = 40,
2239+
.cipher_data = {
2240+
0x17, 0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00,
2241+
0x00, 0x00, 0x00, 0x00, 0x01, 0x3a, 0x1a, 0x9c,
2242+
0xd0, 0xa8, 0x9a, 0xd6, 0x69, 0xd6, 0x1a, 0xe3,
2243+
0xb5, 0x1f, 0x0d, 0x2c, 0xe2, 0x97, 0x46, 0xff,
2244+
0x2b, 0xcc, 0x5a, 0xc4, 0xa3, 0xb9, 0xef, 0xba,
2245+
},
2246+
};
2247+
2248+
/* TLS 1.2, AES_CCM, ctrl, seqno:1, plaintext: '' */
2249+
static const struct raw_rec id1_ctrl_l0 = {
2250+
.plain_len = 0,
2251+
.plain_data = {
2252+
},
2253+
.cipher_len = 29,
2254+
.cipher_data = {
2255+
0x16, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2256+
0x00, 0x00, 0x00, 0x00, 0x01, 0x3e, 0xf0, 0xfe,
2257+
0xee, 0xd9, 0xe2, 0x5d, 0xc7, 0x11, 0x4c, 0xe6,
2258+
0xb4, 0x7e, 0xef, 0x40, 0x2b,
2259+
},
2260+
};
2261+
2262+
/* TLS 1.2, AES_CCM, data, seqno:1, plaintext: '' */
2263+
static const struct raw_rec id1_data_l0 = {
2264+
.plain_len = 0,
2265+
.plain_data = {
2266+
},
2267+
.cipher_len = 29,
2268+
.cipher_data = {
2269+
0x17, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2270+
0x00, 0x00, 0x00, 0x00, 0x01, 0xce, 0xfc, 0x86,
2271+
0xc8, 0xf0, 0x55, 0xf9, 0x47, 0x3f, 0x74, 0xdc,
2272+
0xc9, 0xbf, 0xfe, 0x5b, 0xb1,
2273+
},
2274+
};
2275+
2276+
/* TLS 1.2, AES_CCM, ctrl, seqno:2, plaintext: 'Hello world' */
2277+
static const struct raw_rec id2_ctrl_l11 = {
2278+
.plain_len = 11,
2279+
.plain_data = {
2280+
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
2281+
0x72, 0x6c, 0x64,
2282+
},
2283+
.cipher_len = 40,
2284+
.cipher_data = {
2285+
0x16, 0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00,
2286+
0x00, 0x00, 0x00, 0x00, 0x02, 0xe5, 0x3d, 0x19,
2287+
0x3d, 0xca, 0xb8, 0x16, 0xb6, 0xff, 0x79, 0x87,
2288+
0x2a, 0x04, 0x11, 0x3d, 0xf8, 0x64, 0x5f, 0x36,
2289+
0x8b, 0xa8, 0xee, 0x4c, 0x6d, 0x62, 0xa5, 0x00,
2290+
},
2291+
};
2292+
2293+
/* TLS 1.2, AES_CCM, data, seqno:2, plaintext: 'Hello world' */
2294+
static const struct raw_rec id2_data_l11 = {
2295+
.plain_len = 11,
2296+
.plain_data = {
2297+
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
2298+
0x72, 0x6c, 0x64,
2299+
},
2300+
.cipher_len = 40,
2301+
.cipher_data = {
2302+
0x17, 0x03, 0x03, 0x00, 0x23, 0x00, 0x00, 0x00,
2303+
0x00, 0x00, 0x00, 0x00, 0x02, 0xe5, 0x3d, 0x19,
2304+
0x3d, 0xca, 0xb8, 0x16, 0xb6, 0xff, 0x79, 0x87,
2305+
0x8e, 0xa1, 0xd0, 0xcd, 0x33, 0xb5, 0x86, 0x2b,
2306+
0x17, 0xf1, 0x52, 0x2a, 0x55, 0x62, 0x65, 0x11,
2307+
},
2308+
};
2309+
2310+
/* TLS 1.2, AES_CCM, ctrl, seqno:2, plaintext: '' */
2311+
static const struct raw_rec id2_ctrl_l0 = {
2312+
.plain_len = 0,
2313+
.plain_data = {
2314+
},
2315+
.cipher_len = 29,
2316+
.cipher_data = {
2317+
0x16, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2318+
0x00, 0x00, 0x00, 0x00, 0x02, 0xdc, 0x5c, 0x0e,
2319+
0x41, 0xdd, 0xba, 0xd3, 0xcc, 0xcf, 0x6d, 0xd9,
2320+
0x06, 0xdb, 0x79, 0xe5, 0x5d,
2321+
},
2322+
};
2323+
2324+
/* TLS 1.2, AES_CCM, data, seqno:2, plaintext: '' */
2325+
static const struct raw_rec id2_data_l0 = {
2326+
.plain_len = 0,
2327+
.plain_data = {
2328+
},
2329+
.cipher_len = 29,
2330+
.cipher_data = {
2331+
0x17, 0x03, 0x03, 0x00, 0x18, 0x00, 0x00, 0x00,
2332+
0x00, 0x00, 0x00, 0x00, 0x02, 0xc3, 0xca, 0x26,
2333+
0x22, 0xe4, 0x25, 0xfb, 0x5f, 0x6d, 0xbf, 0x83,
2334+
0x30, 0x48, 0x69, 0x1a, 0x47,
2335+
},
2336+
};
2337+
2338+
FIXTURE(zero_len)
2339+
{
2340+
int fd, cfd;
2341+
bool notls;
2342+
};
2343+
2344+
FIXTURE_VARIANT(zero_len)
2345+
{
2346+
const struct raw_rec *recs[4];
2347+
ssize_t recv_ret[4];
2348+
};
2349+
2350+
FIXTURE_VARIANT_ADD(zero_len, data_data_data)
2351+
{
2352+
.recs = { &id0_data_l11, &id1_data_l11, &id2_data_l11, },
2353+
.recv_ret = { 33, -EAGAIN, },
2354+
};
2355+
2356+
FIXTURE_VARIANT_ADD(zero_len, data_0ctrl_data)
2357+
{
2358+
.recs = { &id0_data_l11, &id1_ctrl_l0, &id2_data_l11, },
2359+
.recv_ret = { 11, 0, 11, -EAGAIN, },
2360+
};
2361+
2362+
FIXTURE_VARIANT_ADD(zero_len, 0data_0data_0data)
2363+
{
2364+
.recs = { &id0_data_l0, &id1_data_l0, &id2_data_l0, },
2365+
.recv_ret = { -EAGAIN, },
2366+
};
2367+
2368+
FIXTURE_VARIANT_ADD(zero_len, 0data_0data_ctrl)
2369+
{
2370+
.recs = { &id0_data_l0, &id1_data_l0, &id2_ctrl_l11, },
2371+
.recv_ret = { 0, 11, -EAGAIN, },
2372+
};
2373+
2374+
FIXTURE_VARIANT_ADD(zero_len, 0data_0data_0ctrl)
2375+
{
2376+
.recs = { &id0_data_l0, &id1_data_l0, &id2_ctrl_l0, },
2377+
.recv_ret = { 0, 0, -EAGAIN, },
2378+
};
2379+
2380+
FIXTURE_VARIANT_ADD(zero_len, 0ctrl_0ctrl_0ctrl)
2381+
{
2382+
.recs = { &id0_ctrl_l0, &id1_ctrl_l0, &id2_ctrl_l0, },
2383+
.recv_ret = { 0, 0, 0, -EAGAIN, },
2384+
};
2385+
2386+
FIXTURE_VARIANT_ADD(zero_len, 0data_0data_data)
2387+
{
2388+
.recs = { &id0_data_l0, &id1_data_l0, &id2_data_l11, },
2389+
.recv_ret = { 11, -EAGAIN, },
2390+
};
2391+
2392+
FIXTURE_VARIANT_ADD(zero_len, data_0data_0data)
2393+
{
2394+
.recs = { &id0_data_l11, &id1_data_l0, &id2_data_l0, },
2395+
.recv_ret = { 11, -EAGAIN, },
2396+
};
2397+
2398+
FIXTURE_SETUP(zero_len)
2399+
{
2400+
struct tls_crypto_info_keys tls12;
2401+
int ret;
2402+
2403+
tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_CCM_128,
2404+
&tls12, 0);
2405+
2406+
ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
2407+
if (self->notls)
2408+
return;
2409+
2410+
/* Don't install keys on fd, we'll send raw records */
2411+
ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12, tls12.len);
2412+
ASSERT_EQ(ret, 0);
2413+
}
2414+
2415+
FIXTURE_TEARDOWN(zero_len)
2416+
{
2417+
close(self->fd);
2418+
close(self->cfd);
2419+
}
2420+
2421+
TEST_F(zero_len, test)
2422+
{
2423+
const struct raw_rec *const *rec;
2424+
unsigned char buf[128];
2425+
int rec_off;
2426+
int i;
2427+
2428+
for (i = 0; i < 4 && variant->recs[i]; i++)
2429+
EXPECT_EQ(send(self->fd, variant->recs[i]->cipher_data,
2430+
variant->recs[i]->cipher_len, 0),
2431+
variant->recs[i]->cipher_len);
2432+
2433+
rec = &variant->recs[0];
2434+
rec_off = 0;
2435+
for (i = 0; i < 4; i++) {
2436+
int j, ret;
2437+
2438+
ret = variant->recv_ret[i] >= 0 ? variant->recv_ret[i] : -1;
2439+
EXPECT_EQ(__tls_recv_cmsg(_metadata, self->cfd, NULL,
2440+
buf, sizeof(buf), MSG_DONTWAIT), ret);
2441+
if (ret == -1)
2442+
EXPECT_EQ(errno, -variant->recv_ret[i]);
2443+
if (variant->recv_ret[i] == -EAGAIN)
2444+
break;
2445+
2446+
for (j = 0; j < ret; j++) {
2447+
while (rec_off == (*rec)->plain_len) {
2448+
rec++;
2449+
rec_off = 0;
2450+
}
2451+
EXPECT_EQ(buf[j], (*rec)->plain_data[rec_off]);
2452+
rec_off++;
2453+
}
2454+
}
2455+
};
2456+
21672457
FIXTURE(tls_err)
21682458
{
21692459
int fd, cfd;

0 commit comments

Comments
 (0)