Skip to content

Commit fef27cf

Browse files
committed
Add TLS 1.3 early data examples
Introduces client and server examples that demonstrate the use of TLS 1.3 early data (0-RTT) with session resumption. The client example performs an initial handshake to obtain a session ticket, then reconnects and sends early data. The server example receives early data and sends back a reply.
1 parent 123d562 commit fef27cf

3 files changed

Lines changed: 406 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ android/wolfssljni-ndk-sample/proguard-project.txt
8484
/tls/client-tls
8585
/tls/client-tls13
8686
/tls/client-tls13-resume
87+
/tls/client-tls13-earlydata
88+
/tls/server-tls13-earlydata
8789
/tls/client-tls-bio
8890
/tls/client-tls-cacb
8991
/tls/client-tls-callback

tls/client-tls13-earlydata.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/* client-tls13-earlydata.c
2+
*
3+
* Copyright (C) 2006-2025 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 TLS 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 client!"
44+
#define EARLY_DATA_MSG_LEN (sizeof(EARLY_DATA_MSG))
45+
46+
static int tcp_connect(const char* ip, int port) {
47+
int sockfd;
48+
struct sockaddr_in servAddr;
49+
50+
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
51+
perror("socket()");
52+
return -1;
53+
}
54+
55+
memset(&servAddr, 0, sizeof(servAddr));
56+
servAddr.sin_family = AF_INET;
57+
servAddr.sin_port = htons(port);
58+
59+
if (inet_pton(AF_INET, ip, &servAddr.sin_addr) != 1) {
60+
perror("inet_pton()");
61+
close(sockfd);
62+
return -1;
63+
}
64+
65+
if (connect(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0) {
66+
perror("connect()");
67+
close(sockfd);
68+
return -1;
69+
}
70+
71+
return sockfd;
72+
}
73+
74+
int main(int argc, char** argv)
75+
{
76+
if (argc != 2) {
77+
printf("Usage: %s <server-ip>\n", argv[0]);
78+
return 1;
79+
}
80+
81+
const char* server_ip = argv[1];
82+
int ret = 1;
83+
int sockfd = -1;
84+
WOLFSSL_CTX* ctx = NULL;
85+
WOLFSSL* ssl = NULL;
86+
WOLFSSL_SESSION* session = NULL;
87+
char recvBuf[256];
88+
int len;
89+
int earlyDataSent = 0;
90+
91+
/* Initialize wolfSSL */
92+
if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
93+
fprintf(stderr, "wolfSSL_Init failed\n");
94+
goto cleanup;
95+
}
96+
97+
/* Create and configure context */
98+
ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
99+
if (!ctx) {
100+
fprintf(stderr, "wolfSSL_CTX_new failed\n");
101+
goto cleanup;
102+
}
103+
104+
if (wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS ||
105+
wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS ||
106+
wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL) != WOLFSSL_SUCCESS) {
107+
fprintf(stderr, "Failed to load cert/key/CA\n");
108+
goto cleanup;
109+
}
110+
111+
/* === 1st connection: perform handshake and get session ticket === */
112+
sockfd = tcp_connect(server_ip, DEFAULT_PORT);
113+
if (sockfd < 0) goto cleanup;
114+
115+
ssl = wolfSSL_new(ctx);
116+
if (!ssl) {
117+
fprintf(stderr, "wolfSSL_new failed\n");
118+
goto cleanup;
119+
}
120+
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
121+
fprintf(stderr, "wolfSSL_set_fd failed\n");
122+
goto cleanup;
123+
}
124+
125+
if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
126+
fprintf(stderr, "wolfSSL_connect failed\n");
127+
goto cleanup;
128+
}
129+
130+
/* Check if ticket was received */
131+
if (!wolfSSL_SessionIsSetup(wolfSSL_SSL_get0_session(ssl))) {
132+
/* Attempt to read a session ticket from server */
133+
(void)wolfSSL_read(ssl, recvBuf, sizeof(recvBuf)-1);
134+
if (!wolfSSL_SessionIsSetup(wolfSSL_SSL_get0_session(ssl))) {
135+
fprintf(stderr, "Session ticket not received from server\n");
136+
goto cleanup;
137+
}
138+
}
139+
140+
/* Save session for resumption */
141+
session = wolfSSL_get1_session(ssl);
142+
if (!session) {
143+
fprintf(stderr, "wolfSSL_get1_session failed\n");
144+
goto cleanup;
145+
}
146+
147+
printf("Initial handshake complete, session ticket obtained.\n");
148+
149+
/* Clean up first connection */
150+
wolfSSL_shutdown(ssl);
151+
wolfSSL_free(ssl);
152+
ssl = NULL;
153+
close(sockfd);
154+
sockfd = -1;
155+
156+
/* === 2nd connection: resume session and send early data === */
157+
sockfd = tcp_connect(server_ip, DEFAULT_PORT);
158+
if (sockfd < 0) goto cleanup;
159+
160+
ssl = wolfSSL_new(ctx);
161+
if (!ssl) {
162+
fprintf(stderr, "wolfSSL_new (2nd) failed\n");
163+
goto cleanup;
164+
}
165+
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
166+
fprintf(stderr, "wolfSSL_set_fd (2nd) failed\n");
167+
goto cleanup;
168+
}
169+
170+
if (wolfSSL_set_session(ssl, session) != WOLFSSL_SUCCESS) {
171+
fprintf(stderr, "wolfSSL_set_session failed\n");
172+
goto cleanup;
173+
}
174+
175+
ret = wolfSSL_write_early_data(ssl, EARLY_DATA_MSG, EARLY_DATA_MSG_LEN, &earlyDataSent);
176+
if (ret == EARLY_DATA_MSG_LEN && earlyDataSent == EARLY_DATA_MSG_LEN) {
177+
printf("Sent early data: \"%s\"\n", EARLY_DATA_MSG);
178+
} else {
179+
fprintf(stderr, "wolfSSL_write_early_data failed: ret=%d sent=%d\n", ret, earlyDataSent);
180+
goto cleanup;
181+
}
182+
183+
/* Complete handshake */
184+
if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
185+
fprintf(stderr, "wolfSSL_connect (2nd) failed\n");
186+
goto cleanup;
187+
}
188+
printf("Handshake complete after early data.\n");
189+
190+
/* Read server response */
191+
memset(recvBuf, 0, sizeof(recvBuf));
192+
while ((len = wolfSSL_read(ssl, recvBuf, sizeof(recvBuf) - 1)) > 0)
193+
printf("Server replied: %s\n", recvBuf);
194+
195+
ret = 0; /* Success */
196+
197+
cleanup:
198+
if (ssl) wolfSSL_free(ssl);
199+
if (session) wolfSSL_SESSION_free(session);
200+
if (ctx) wolfSSL_CTX_free(ctx);
201+
if (sockfd >= 0) close(sockfd);
202+
wolfSSL_Cleanup();
203+
return ret;
204+
}

0 commit comments

Comments
 (0)