Skip to content

Commit c6f5209

Browse files
committed
New example for post-handshake authentication.
1 parent 4b8f66a commit c6f5209

3 files changed

Lines changed: 709 additions & 0 deletions

File tree

tls/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,45 @@ kex=P-256
13781378
0
13791379
```
13801380

1381+
## TLS Example with Post-Handshake Authentication
1382+
1383+
See `client-tls-posthsauth.c` and `server-tls-posthsauth.c`. These server and client applications show how to do a handshake without the server authenticating the client. Then after the handshake is complete, the server requests authentication and the client authenticates itself to the server. This is mutual authentication with a faster handshake because the client authentication is done later. This can lead to a better user experience if there are conditions where the client need not be authenticated.
1384+
1385+
To get a better understanding of what is going on, it is best to view a diff between the normal TLS 1.3 examples and the post-handshake authentication examples:
1386+
1387+
`diff -u server-tls13.c server-tls-posthsauth.c`
1388+
1389+
`diff -u client-tls13.c client-tls-posthsauth.c`
1390+
1391+
Of course, to use this example, you must enable post-handshake authentication. For the purposes of verifying that post-handshake authentication is actually happening, you can enable debugging messages.
1392+
1393+
Build and install wolfSSL like so:
1394+
1395+
```
1396+
$ ./autogen.sh
1397+
$ ./configure --enable-debug --enable-postauth
1398+
$ make all
1399+
$ sudo make install
1400+
```
1401+
1402+
Modify `client-tls-posthsauth.c` and `server-tls-posthsauth.c` so that they call `wolfSSL_Debugging_ON()`.
1403+
1404+
Build them like so:
1405+
1406+
```
1407+
make client-tls-posthsauth server-tls-posthsaut
1408+
```
1409+
1410+
Execute them like so:
1411+
1412+
```
1413+
./server-tls-posthsauth
1414+
```
1415+
1416+
```
1417+
./server-tls-posthsauth 127.0.0.1
1418+
```
1419+
13811420
## Support
13821421

13831422
Please contact wolfSSL at support@wolfssl.com with any questions, bug fixes,

tls/client-tls-posthsauth.c

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/* client-tls13.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+
/* the usual suspects */
23+
#include <stdlib.h>
24+
#include <stdio.h>
25+
#include <string.h>
26+
27+
/* socket includes */
28+
#include <sys/socket.h>
29+
#include <arpa/inet.h>
30+
#include <netinet/in.h>
31+
#include <unistd.h>
32+
33+
/* wolfSSL */
34+
#include <wolfssl/options.h>
35+
#include <wolfssl/ssl.h>
36+
#include <wolfssl/wolfio.h>
37+
#include <wolfssl/wolfcrypt/error-crypt.h>
38+
39+
#define DEFAULT_PORT 11111
40+
41+
#define CERT_FILE "../certs/client-cert.pem"
42+
#define KEY_FILE "../certs/client-key.pem"
43+
#define CA_FILE "../certs/ca-cert.pem"
44+
45+
#if defined(WOLFSSL_TLS13) && defined(HAVE_SECRET_CALLBACK)
46+
47+
#ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
48+
#define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
49+
#endif
50+
51+
/* Callback function for TLS v1.3 secrets for use with Wireshark */
52+
static int Tls13SecretCallback(WOLFSSL* ssl, int id, const unsigned char* secret,
53+
int secretSz, void* ctx)
54+
{
55+
int i;
56+
const char* str = NULL;
57+
unsigned char clientRandom[32];
58+
int clientRandomSz;
59+
XFILE fp = stderr;
60+
if (ctx) {
61+
fp = XFOPEN((const char*)ctx, "ab");
62+
if (fp == XBADFILE) {
63+
return BAD_FUNC_ARG;
64+
}
65+
}
66+
67+
clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom,
68+
sizeof(clientRandom));
69+
70+
if (clientRandomSz <= 0) {
71+
printf("Error getting client random %d\n", clientRandomSz);
72+
}
73+
74+
#if 0
75+
printf("TLS Client Secret CB: Rand %d, Secret %d\n",
76+
clientRandomSz, secretSz);
77+
#endif
78+
79+
switch (id) {
80+
case CLIENT_EARLY_TRAFFIC_SECRET:
81+
str = "CLIENT_EARLY_TRAFFIC_SECRET"; break;
82+
case EARLY_EXPORTER_SECRET:
83+
str = "EARLY_EXPORTER_SECRET"; break;
84+
case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
85+
str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break;
86+
case SERVER_HANDSHAKE_TRAFFIC_SECRET:
87+
str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break;
88+
case CLIENT_TRAFFIC_SECRET:
89+
str = "CLIENT_TRAFFIC_SECRET_0"; break;
90+
case SERVER_TRAFFIC_SECRET:
91+
str = "SERVER_TRAFFIC_SECRET_0"; break;
92+
case EXPORTER_SECRET:
93+
str = "EXPORTER_SECRET"; break;
94+
}
95+
96+
fprintf(fp, "%s ", str);
97+
for (i = 0; i < clientRandomSz; i++) {
98+
fprintf(fp, "%02x", clientRandom[i]);
99+
}
100+
fprintf(fp, " ");
101+
for (i = 0; i < secretSz; i++) {
102+
fprintf(fp, "%02x", secret[i]);
103+
}
104+
fprintf(fp, "\n");
105+
106+
if (fp != stderr) {
107+
XFCLOSE(fp);
108+
}
109+
110+
return 0;
111+
}
112+
#endif /* WOLFSSL_TLS13 && HAVE_SECRET_CALLBACK */
113+
114+
int main(int argc, char** argv)
115+
{
116+
int ret = 0;
117+
#ifdef WOLFSSL_TLS13
118+
int sockfd = SOCKET_INVALID;
119+
struct sockaddr_in servAddr;
120+
char buff[256];
121+
size_t len;
122+
123+
/* declare wolfSSL objects */
124+
WOLFSSL_CTX* ctx = NULL;
125+
WOLFSSL* ssl = NULL;
126+
127+
/* Check for proper calling convention */
128+
if (argc != 2) {
129+
printf("usage: %s <IPv4 address>\n", argv[0]);
130+
return 0;
131+
}
132+
133+
/* Create a socket that uses an internet IPv4 address,
134+
* Sets the socket to be stream based (TCP),
135+
* 0 means choose the default protocol. */
136+
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
137+
fprintf(stderr, "ERROR: failed to create the socket\n");
138+
ret = -1; goto exit;
139+
}
140+
141+
/* Initialize the server address struct with zeros */
142+
memset(&servAddr, 0, sizeof(servAddr));
143+
144+
/* Fill in the server address */
145+
servAddr.sin_family = AF_INET; /* using IPv4 */
146+
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
147+
148+
/* Get the server IPv4 address from the command line call */
149+
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
150+
fprintf(stderr, "ERROR: invalid address\n");
151+
ret = -1; goto exit;
152+
}
153+
154+
/* Connect to the server */
155+
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
156+
== -1) {
157+
fprintf(stderr, "ERROR: failed to connect\n");
158+
goto exit;
159+
}
160+
161+
/*---------------------------------*/
162+
/* Start of wolfSSL initialization and configuration */
163+
/*---------------------------------*/
164+
#if 0
165+
wolfSSL_Debugging_ON();
166+
#endif
167+
168+
/* Initialize wolfSSL */
169+
if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
170+
fprintf(stderr, "ERROR: Failed to initialize the library\n");
171+
goto exit;
172+
}
173+
174+
/* Create and initialize WOLFSSL_CTX */
175+
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())) == NULL) {
176+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
177+
ret = -1; goto exit;
178+
}
179+
180+
/* Load client certificate into WOLFSSL_CTX */
181+
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM))
182+
!= WOLFSSL_SUCCESS) {
183+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
184+
CERT_FILE);
185+
goto exit;
186+
}
187+
188+
/* Load client key into WOLFSSL_CTX */
189+
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM))
190+
!= WOLFSSL_SUCCESS) {
191+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
192+
KEY_FILE);
193+
goto exit;
194+
}
195+
196+
/* Load CA certificate into WOLFSSL_CTX */
197+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
198+
!= WOLFSSL_SUCCESS) {
199+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
200+
CA_FILE);
201+
goto exit;
202+
}
203+
204+
if ((ret = wolfSSL_CTX_allow_post_handshake_auth(ctx)) != 0) {
205+
fprintf(stderr, "ERROR: failed to allow post hand-shake auth.\n");
206+
goto exit;
207+
}
208+
209+
/* Create a WOLFSSL object */
210+
if ((ssl = wolfSSL_new(ctx)) == NULL) {
211+
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
212+
ret = -1; goto exit;
213+
}
214+
215+
/* Attach wolfSSL to the socket */
216+
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
217+
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
218+
goto exit;
219+
}
220+
221+
#ifdef HAVE_SECRET_CALLBACK
222+
/* required for getting random used */
223+
wolfSSL_KeepArrays(ssl);
224+
225+
/* optional logging for wireshark */
226+
wolfSSL_set_tls13_secret_cb(ssl, Tls13SecretCallback,
227+
(void*)WOLFSSL_SSLKEYLOGFILE_OUTPUT);
228+
#endif
229+
230+
/* Connect to wolfSSL on the server side */
231+
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
232+
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
233+
goto exit;
234+
}
235+
236+
#ifdef HAVE_SECRET_CALLBACK
237+
wolfSSL_FreeArrays(ssl);
238+
#endif
239+
240+
/* Get a message for the server from stdin */
241+
printf("Message for server: ");
242+
memset(buff, 0, sizeof(buff));
243+
if (fgets(buff, sizeof(buff), stdin) == NULL) {
244+
fprintf(stderr, "ERROR: failed to get message for server\n");
245+
ret = -1; goto exit;
246+
}
247+
len = strnlen(buff, sizeof(buff));
248+
249+
/* Send the message to the server */
250+
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
251+
fprintf(stderr, "ERROR: failed to write entire message\n");
252+
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
253+
goto exit;
254+
}
255+
256+
/* Read the server data into our buff array */
257+
memset(buff, 0, sizeof(buff));
258+
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) < 0) {
259+
fprintf(stderr, "ERROR: failed to read\n");
260+
goto exit;
261+
}
262+
263+
/* Print to stdout any data the server sends */
264+
printf("Server: %s\n", buff);
265+
266+
/* Send the second message to the server */
267+
memset(buff, 0, sizeof(buff));
268+
memcpy(buff, "Hello again from the client\n", 28);
269+
len = strnlen(buff, sizeof(buff));
270+
271+
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
272+
fprintf(stderr, "ERROR: failed to write entire message\n");
273+
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
274+
goto exit;
275+
}
276+
277+
/* Return reporting a success */
278+
ret = 0;
279+
280+
exit:
281+
/* Cleanup and return */
282+
if (sockfd != SOCKET_INVALID)
283+
close(sockfd); /* Close the connection to the server */
284+
if (ssl)
285+
wolfSSL_free(ssl); /* Free the wolfSSL object */
286+
if (ctx)
287+
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
288+
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
289+
#else
290+
printf("Example requires TLS v1.3\n");
291+
#endif
292+
(void)argc;
293+
(void)argv;
294+
295+
return ret;
296+
}

0 commit comments

Comments
 (0)