4141#include <wolfssl/wolfio.h>
4242#include <wolfssl/wolfcrypt/error-crypt.h>
4343
44+ #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
4445#define DEFAULT_PORT 11111
4546
4647#define CERT_FILE "../certs/server-cert.pem"
4748#define KEY_FILE "../certs/server-key.pem"
4849#define CA_FILE "../certs/client-cert.pem"
4950
5051
51- #if defined(WOLFSSL_TLS13 ) && defined(HAVE_SECRET_CALLBACK )
52-
53- #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
54- #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
55- #endif
56-
57- /* Callback function for TLS v1.3 secrets for use with Wireshark */
58- static int Tls13SecretCallback (WOLFSSL * ssl , int id , const unsigned char * secret ,
59- int secretSz , void * ctx )
60- {
61- int i ;
62- const char * str = NULL ;
63- unsigned char serverRandom [32 ];
64- int serverRandomSz ;
65- XFILE fp = stderr ;
66- if (ctx ) {
67- fp = XFOPEN ((const char * )ctx , "ab" );
68- if (fp == XBADFILE ) {
69- return BAD_FUNC_ARG ;
70- }
71- }
72-
73- serverRandomSz = (int )wolfSSL_get_server_random (ssl , serverRandom ,
74- sizeof (serverRandom ));
75-
76- if (serverRandomSz <= 0 ) {
77- printf ("Error getting server random %d\n" , serverRandomSz );
78- }
79-
80- #if 0
81- printf ("TLS Server Secret CB: Rand %d, Secret %d\n" ,
82- serverRandomSz , secretSz );
83- #endif
84-
85- switch (id ) {
86- case CLIENT_EARLY_TRAFFIC_SECRET :
87- str = "CLIENT_EARLY_TRAFFIC_SECRET" ; break ;
88- case EARLY_EXPORTER_SECRET :
89- str = "EARLY_EXPORTER_SECRET" ; break ;
90- case CLIENT_HANDSHAKE_TRAFFIC_SECRET :
91- str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET" ; break ;
92- case SERVER_HANDSHAKE_TRAFFIC_SECRET :
93- str = "SERVER_HANDSHAKE_TRAFFIC_SECRET" ; break ;
94- case CLIENT_TRAFFIC_SECRET :
95- str = "CLIENT_TRAFFIC_SECRET_0" ; break ;
96- case SERVER_TRAFFIC_SECRET :
97- str = "SERVER_TRAFFIC_SECRET_0" ; break ;
98- case EXPORTER_SECRET :
99- str = "EXPORTER_SECRET" ; break ;
100- }
101-
102- fprintf (fp , "%s " , str );
103- for (i = 0 ; i < (int )serverRandomSz ; i ++ ) {
104- fprintf (fp , "%02x" , serverRandom [i ]);
105- }
106- fprintf (fp , " " );
107- for (i = 0 ; i < secretSz ; i ++ ) {
108- fprintf (fp , "%02x" , secret [i ]);
109- }
110- fprintf (fp , "\n" );
111-
112- if (fp != stderr ) {
113- XFCLOSE (fp );
114- }
115-
116- return 0 ;
117- }
118- #endif /* WOLFSSL_TLS13 && HAVE_SECRET_CALLBACK */
119-
12052static int mSockfd = SOCKET_INVALID ;
12153static int mConnd = SOCKET_INVALID ;
12254static int mShutdown = 0 ;
@@ -216,7 +148,8 @@ int main(int argc, char** argv)
216148 goto exit ;
217149 }
218150
219- /* No verification during handshake. Will be doing it post handshake. */
151+ /* POSTHSAUTH: No verification during handshake. Will be doing it post
152+ * handshake. */
220153 wolfSSL_CTX_set_verify (ctx , SSL_VERIFY_NONE , NULL );
221154
222155 /* Load server certificates into WOLFSSL_CTX */
@@ -263,15 +196,6 @@ int main(int argc, char** argv)
263196 /* Attach wolfSSL to the socket */
264197 wolfSSL_set_fd (ssl , mConnd );
265198
266- #ifdef HAVE_SECRET_CALLBACK
267- /* required for getting random used */
268- wolfSSL_KeepArrays (ssl );
269-
270- /* optional logging for wireshark */
271- wolfSSL_set_tls13_secret_cb (ssl , Tls13SecretCallback ,
272- (void * )WOLFSSL_SSLKEYLOGFILE_OUTPUT );
273- #endif
274-
275199 /* Establish TLS connection */
276200 if ((ret = wolfSSL_accept (ssl )) != WOLFSSL_SUCCESS ) {
277201 fprintf (stderr , "wolfSSL_accept error = %d\n" ,
@@ -281,10 +205,6 @@ int main(int argc, char** argv)
281205
282206 printf ("Client connected successfully\n" );
283207
284- #ifdef HAVE_SECRET_CALLBACK
285- wolfSSL_FreeArrays (ssl );
286- #endif
287-
288208 /* Read the client data into our buff array */
289209 memset (buff , 0 , sizeof (buff ));
290210 if ((ret = wolfSSL_read (ssl , buff , sizeof (buff )- 1 )) < 0 ) {
@@ -301,9 +221,12 @@ int main(int argc, char** argv)
301221 mShutdown = 1 ;
302222 }
303223
224+ /* POSTHSAUTH: Require the client to send over their certificate; fail
225+ * if we cannot verify the client. */
304226 wolfSSL_set_verify (ssl ,
305227 WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT , 0 );
306228
229+ /* POSTHSAUTH: Request the certficate and do the verification. */
307230 if (wolfSSL_request_certificate (ssl ) != WOLFSSL_SUCCESS ) {
308231 fprintf (stderr , "ERROR: Request for post-hs certificate failed\n" );
309232 goto exit ;
@@ -323,7 +246,8 @@ int main(int argc, char** argv)
323246 goto exit ;
324247 }
325248
326- /* Read second message into our buff array */
249+ /* POSTHSAUTH: Read second message into our buff array. This is now
250+ * authenticated. */
327251 memset (buff , 0 , sizeof (buff ));
328252 if ((ret = wolfSSL_read (ssl , buff , sizeof (buff )- 1 )) < 0 ) {
329253 fprintf (stderr , "ERROR: failed to read\n" );
@@ -372,3 +296,10 @@ int main(int argc, char** argv)
372296
373297 return ret ;
374298}
299+ #else
300+ int main () {
301+ fprintf (stderr , "Please configure with --enable-postauth or compile with "
302+ "WOLFSSL_POST_HANDSHAKE_AUTH defined.\n" );
303+ return 0 ;
304+ }
305+ #endif /* WOLFSSL_POST_HANDSHAKE_AUTH */
0 commit comments