Skip to content

Commit 6f4d61f

Browse files
authored
Merge pull request #530 from julek-wolfssl/tls13-earlydata
Add TLS 1.3 early data examples
2 parents 850b447 + 66d3f69 commit 6f4d61f

7 files changed

Lines changed: 937 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ android/wolfssljni-ndk-sample/proguard-project.txt
5555
/dtls/client-dtls-shared
5656
/dtls/client-dtls
5757
/dtls/client-dtls13
58+
/dtls/client-dtls13-earlydata
5859
/dtls/client-udp
5960
/dtls/memory-bio-dtls
6061
/dtls/server-dtls-callback
@@ -64,6 +65,7 @@ android/wolfssljni-ndk-sample/proguard-project.txt
6465
/dtls/server-dtls-threaded
6566
/dtls/server-dtls
6667
/dtls/server-dtls13
68+
/dtls/server-dtls13-earlydata
6769
/dtls/server-dtls13-event
6870
/dtls/server-udp
6971

@@ -84,6 +86,8 @@ android/wolfssljni-ndk-sample/proguard-project.txt
8486
/tls/client-tls
8587
/tls/client-tls13
8688
/tls/client-tls13-resume
89+
/tls/client-tls13-earlydata
90+
/tls/server-tls13-earlydata
8791
/tls/client-tls-bio
8892
/tls/client-tls-cacb
8993
/tls/client-tls-callback

dtls/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
- 5.2.4.1. Variables
8080
- 5.2.4.2. Adding a Loop
8181
- 5.2.5. Final Note
82+
- Chapter 6: DTLS 1.3 Early Data (0-RTT) with Session Resumption
8283
- References
8384
## CHAPTER 1: A Simple UDP Server & Client
8485
### Section 1: By Kaleb Himes
@@ -1679,6 +1680,50 @@ The code above was taken directly from the DTLS server nonblocking file.
16791680

16801681
Be sure to keep in mind that the `AwaitDatagram` code is essentially one large loop that will attempt to listen for a client (in a nonblocking fashion) at every iteration, and will close the loop upon a signal passed by the user.
16811682

1683+
## Chapter 6: DTLS 1.3 Early Data (0-RTT) with Session Resumption
1684+
1685+
This pair of examples demonstrates DTLS 1.3 early data (0-RTT) using wolfSSL.
1686+
The client performs an initial connection to obtain a session ticket, then
1687+
reconnects and sends early data during the resumed handshake. The server reads
1688+
early data and can send application data immediately (so-called 0.5-RTT), then
1689+
continues with the normal handshake/application data flow.
1690+
1691+
It is recommended to build wolfSSL with `WOLFSSL_DTLS13_NO_HRR_ON_RESUME` so the
1692+
server does not send a HelloRetryRequest (HRR) when resuming sessions. (The
1693+
server example also enables this behavior per-connection with
1694+
`wolfSSL_dtls13_no_hrr_on_resume()`.)
1695+
1696+
Files:
1697+
- `server-dtls13-earlydata.c`: DTLS 1.3 server that receives early data using
1698+
`wolfSSL_read_early_data()`. It sets a maximum early data size using
1699+
`wolfSSL_CTX_set_max_early_data()` and may send 0.5-RTT application data.
1700+
- `client-dtls13-earlydata.c`: DTLS 1.3 client that first connects to obtain a
1701+
session ticket, then reconnects and sends early data using
1702+
`wolfSSL_write_early_data()` before finishing the handshake. After the
1703+
handshake, it also sends a normal (post-handshake) application data message.
1704+
1705+
Build requirements:
1706+
- wolfSSL must be built with DTLS 1.3 and early data support enabled.
1707+
Enable early data support by building wolfSSL with
1708+
`--enable-earlydata --enable-session-ticket`.
1709+
1710+
Build and run (in `wolfssl-examples/dtls`, in separate terminals):
1711+
1712+
```sh
1713+
make clean && make
1714+
./server-dtls13-earlydata
1715+
./client-dtls13-earlydata 127.0.0.1
1716+
```
1717+
1718+
Expected behavior:
1719+
- On the first client run/connection, a full handshake completes and a session
1720+
ticket is obtained.
1721+
- On the second connection, the client sends early data immediately and then
1722+
completes the DTLS handshake.
1723+
- The server logs any received early data, may send a reply during early-data
1724+
processing, then finishes the handshake and sends a normal reply after
1725+
handshake completion.
1726+
16821727
#### 5.2.5 Final note
16831728
And that's it! The server has been made into a nonblocking server, and the client has been made into a nonblocking client.
16841729

dtls/client-dtls13-earlydata.c

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/* client-dtls13-earlydata.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
/* Example DTLS 1.3 client using wolfSSL early data (0-RTT) with session resumption.
23+
* Performs an initial handshake to obtain a session ticket, then reconnects and
24+
* sends early data using wolfSSL_write_early_data().
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include <unistd.h>
31+
#include <arpa/inet.h>
32+
#include <sys/socket.h>
33+
34+
#include <wolfssl/options.h>
35+
#include <wolfssl/ssl.h>
36+
#include <wolfssl/wolfio.h>
37+
38+
#define DEFAULT_PORT 11111
39+
#define CERT_FILE "../certs/client-cert.pem"
40+
#define KEY_FILE "../certs/client-key.pem"
41+
#define CA_FILE "../certs/ca-cert.pem"
42+
43+
#define EARLY_DATA_MSG "Early data hello from early data DTLS client!"
44+
#define EARLY_DATA_MSG_LEN (sizeof(EARLY_DATA_MSG))
45+
#define DATA_MSG "Normal data hello from early data DTLS client!"
46+
#define DATA_MSG_LEN (sizeof(DATA_MSG))
47+
48+
static int udp_create_socket(const char* ip, int port, struct sockaddr_in* servAddr) {
49+
int sockfd;
50+
51+
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
52+
perror("socket()");
53+
return -1;
54+
}
55+
56+
memset(servAddr, 0, sizeof(*servAddr));
57+
servAddr->sin_family = AF_INET;
58+
servAddr->sin_port = htons(port);
59+
60+
if (inet_pton(AF_INET, ip, &servAddr->sin_addr) != 1) {
61+
perror("inet_pton()");
62+
close(sockfd);
63+
return -1;
64+
}
65+
66+
return sockfd;
67+
}
68+
69+
int main(int argc, char** argv)
70+
{
71+
if (argc != 2) {
72+
printf("Usage: %s <server-ip>\n", argv[0]);
73+
return 1;
74+
}
75+
76+
const char* server_ip = argv[1];
77+
int ret = 1;
78+
int sockfd = -1;
79+
WOLFSSL_CTX* ctx = NULL;
80+
WOLFSSL* ssl = NULL;
81+
WOLFSSL_SESSION* session = NULL;
82+
char recvBuf[256];
83+
int len;
84+
int earlyDataSent = 0;
85+
struct sockaddr_in servAddr;
86+
87+
/* Initialize wolfSSL */
88+
if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
89+
fprintf(stderr, "wolfSSL_Init failed\n");
90+
goto cleanup;
91+
}
92+
93+
/* Create and configure context */
94+
ctx = wolfSSL_CTX_new(wolfDTLSv1_3_client_method());
95+
if (!ctx) {
96+
fprintf(stderr, "wolfSSL_CTX_new failed\n");
97+
goto cleanup;
98+
}
99+
100+
if (wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS ||
101+
wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS ||
102+
wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL) != WOLFSSL_SUCCESS) {
103+
fprintf(stderr, "Failed to load cert/key/CA\n");
104+
goto cleanup;
105+
}
106+
107+
/* === 1st connection: perform handshake and get session ticket === */
108+
sockfd = udp_create_socket(server_ip, DEFAULT_PORT, &servAddr);
109+
if (sockfd < 0) goto cleanup;
110+
111+
ssl = wolfSSL_new(ctx);
112+
if (!ssl) {
113+
fprintf(stderr, "wolfSSL_new failed\n");
114+
goto cleanup;
115+
}
116+
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
117+
fprintf(stderr, "wolfSSL_set_fd failed\n");
118+
goto cleanup;
119+
}
120+
wolfSSL_dtls_set_peer(ssl, (struct sockaddr*)&servAddr, sizeof(servAddr));
121+
122+
if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
123+
fprintf(stderr, "wolfSSL_connect failed\n");
124+
goto cleanup;
125+
}
126+
127+
/* Check if ticket was received */
128+
if (!wolfSSL_SessionIsSetup(wolfSSL_get_session(ssl))) {
129+
(void)wolfSSL_peek(ssl, recvBuf, 0);
130+
if (!wolfSSL_SessionIsSetup(wolfSSL_get_session(ssl))) {
131+
fprintf(stderr, "Session ticket not received from server\n");
132+
goto cleanup;
133+
}
134+
}
135+
136+
/* Save session for resumption */
137+
session = wolfSSL_get1_session(ssl);
138+
if (!session) {
139+
fprintf(stderr, "wolfSSL_get1_session failed\n");
140+
goto cleanup;
141+
}
142+
143+
printf("Initial handshake complete, session ticket obtained.\n");
144+
145+
/* Clean up first connection with full shutdown */
146+
wolfSSL_shutdown(ssl);
147+
wolfSSL_free(ssl);
148+
ssl = NULL;
149+
close(sockfd);
150+
sockfd = -1;
151+
152+
/* === 2nd connection: resume session and send early data === */
153+
sockfd = udp_create_socket(server_ip, DEFAULT_PORT, &servAddr);
154+
if (sockfd < 0) goto cleanup;
155+
156+
ssl = wolfSSL_new(ctx);
157+
if (!ssl) {
158+
fprintf(stderr, "wolfSSL_new (2nd) failed\n");
159+
goto cleanup;
160+
}
161+
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
162+
fprintf(stderr, "wolfSSL_set_fd (2nd) failed\n");
163+
goto cleanup;
164+
}
165+
wolfSSL_dtls_set_peer(ssl, (struct sockaddr*)&servAddr, sizeof(servAddr));
166+
167+
if (wolfSSL_set_session(ssl, session) != WOLFSSL_SUCCESS) {
168+
fprintf(stderr, "wolfSSL_set_session failed\n");
169+
goto cleanup;
170+
}
171+
172+
ret = wolfSSL_write_early_data(ssl, EARLY_DATA_MSG, EARLY_DATA_MSG_LEN, &earlyDataSent);
173+
if (ret == EARLY_DATA_MSG_LEN && earlyDataSent == EARLY_DATA_MSG_LEN) {
174+
printf("Sent early data: \"%s\"\n", EARLY_DATA_MSG);
175+
} else {
176+
fprintf(stderr, "wolfSSL_write_early_data failed: ret=%d sent=%d\n", ret, earlyDataSent);
177+
goto cleanup;
178+
}
179+
180+
/* Complete handshake */
181+
while (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
182+
if (wolfSSL_get_error(ssl, -1) != APP_DATA_READY) {
183+
fprintf(stderr, "wolfSSL_connect (2nd) failed\n");
184+
goto cleanup;
185+
}
186+
else {
187+
memset(recvBuf, 0, sizeof(recvBuf));
188+
len = wolfSSL_read(ssl, recvBuf, sizeof(recvBuf) - 1);
189+
if (len > 0) {
190+
printf("Server sent during handshake: %s\n", recvBuf);
191+
}
192+
}
193+
}
194+
printf("Handshake complete after early data.\n");
195+
196+
if (wolfSSL_write(ssl, DATA_MSG, DATA_MSG_LEN) != DATA_MSG_LEN) {
197+
fprintf(stderr, "wolfSSL_write (normal data) failed\n");
198+
}
199+
else {
200+
printf("Sent normal data: \"%s\"\n", DATA_MSG);
201+
}
202+
203+
/* Read server response */
204+
memset(recvBuf, 0, sizeof(recvBuf));
205+
while ((len = wolfSSL_read(ssl, recvBuf, sizeof(recvBuf) - 1)) > 0)
206+
printf("Server replied: %s\n", recvBuf);
207+
208+
ret = 0; /* Success */
209+
210+
cleanup:
211+
wolfSSL_shutdown(ssl);
212+
if (ssl) wolfSSL_free(ssl);
213+
if (session) wolfSSL_SESSION_free(session);
214+
if (ctx) wolfSSL_CTX_free(ctx);
215+
if (sockfd >= 0) close(sockfd);
216+
wolfSSL_Cleanup();
217+
return ret;
218+
}

0 commit comments

Comments
 (0)