|
| 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