Skip to content

Commit 124998f

Browse files
committed
Add example for TLS server with crypto callbacks.
1 parent 90f5882 commit 124998f

3 files changed

Lines changed: 631 additions & 36 deletions

File tree

tls/client-tls-cryptocb.c

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ typedef struct {
4747
int exampleVar; /* example, not used */
4848
} myCryptoCbCtx;
4949

50-
static void error_out(char* msg, int err)
51-
{
52-
printf("Failed at %s with code %d\n", msg, err);
53-
exit(1);
54-
}
55-
5650

5751
/* Example crypto dev callback function that calls software version */
5852
/* This is where you would plug-in calls to your own hardware crypto */
@@ -386,19 +380,21 @@ int main(int argc, char** argv)
386380
{
387381
int ret = 0;
388382
#ifdef WOLF_CRYPTO_CB
389-
int sockfd;
383+
int sockfd = SOCKET_INVALID;
390384
struct sockaddr_in servAddr;
391385
char buff[256];
392386
size_t len;
393387

394388
/* declare wolfSSL objects */
395-
WOLFSSL_CTX* ctx;
396-
WOLFSSL* ssl;
389+
WOLFSSL_CTX* ctx = NULL;
390+
WOLFSSL* ssl = NULL;
391+
WOLFSSL_CIPHER* cipher;
397392

398393
int devId = 1; /* anything besides -2 (INVALID_DEVID) */
399394
myCryptoCbCtx myCtx;
400395

401396
/* example data for callback */
397+
memset(&myCtx, 0, sizeof(myCtx));
402398
myCtx.exampleVar = 1;
403399

404400
/* Check for proper calling convention */
@@ -413,7 +409,7 @@ int main(int argc, char** argv)
413409
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
414410
fprintf(stderr, "ERROR: failed to create the socket\n");
415411
ret = -1;
416-
goto end;
412+
goto exit;
417413
}
418414

419415
/* Initialize the server address struct with zeros */
@@ -427,36 +423,46 @@ int main(int argc, char** argv)
427423
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
428424
fprintf(stderr, "ERROR: invalid address\n");
429425
ret = -1;
430-
goto end;
426+
goto exit;
431427
}
432428

433429
/* Connect to the server */
434430
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
435431
== -1) {
436432
fprintf(stderr, "ERROR: failed to connect\n");
437-
goto end;
433+
goto exit;
438434
}
439435

436+
#if 0
437+
wolfSSL_Debugging_ON();
438+
#endif
439+
440440
/*---------------------------------*/
441441
/* Start of wolfSSL initialization and configuration */
442442
/*---------------------------------*/
443443
/* Initialize wolfSSL */
444444
if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
445445
fprintf(stderr, "ERROR: Failed to initialize the library\n");
446-
goto socket_cleanup;
446+
goto exit;
447447
}
448448

449449
/* Create and initialize WOLFSSL_CTX */
450-
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
450+
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())) == NULL) {
451451
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
452452
ret = -1;
453-
goto socket_cleanup;
453+
goto exit;
454454
}
455455

456+
#if 0
457+
wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384");
458+
#endif
459+
456460
/* register a devID for crypto callbacks */
457461
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
458-
if (ret != 0)
459-
error_out("wc_CryptoCb_RegisterDevice", ret);
462+
if (ret != 0) {
463+
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed %d\n", ret);
464+
goto exit;
465+
}
460466

461467
/* register a devID for crypto callbacks */
462468
wolfSSL_CTX_SetDevId(ctx, devId);
@@ -466,69 +472,73 @@ int main(int argc, char** argv)
466472
!= SSL_SUCCESS) {
467473
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
468474
CA_FILE);
469-
goto ctx_cleanup;
475+
goto exit;
470476
}
471477

472478
/* Create a WOLFSSL object */
473479
if ((ssl = wolfSSL_new(ctx)) == NULL) {
474480
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
475481
ret = -1;
476-
goto ctx_cleanup;
482+
goto exit;
477483
}
478484

479485
/* Attach wolfSSL to the socket */
480486
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
481487
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
482-
goto cleanup;
488+
goto exit;
483489
}
484490

485491
/* Connect to wolfSSL on the server side */
486492
if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) {
487493
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
488-
goto cleanup;
494+
goto exit;
489495
}
490496

497+
cipher = wolfSSL_get_current_cipher(ssl);
498+
printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
499+
491500
/* Get a message for the server from stdin */
492501
printf("Message for server: ");
493502
memset(buff, 0, sizeof(buff));
494503
if (fgets(buff, sizeof(buff), stdin) == NULL) {
495504
fprintf(stderr, "ERROR: failed to get message for server\n");
496505
ret = -1;
497-
goto cleanup;
506+
goto exit;
498507
}
499508
len = strnlen(buff, sizeof(buff));
500509

501510
/* Send the message to the server */
502511
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
503512
fprintf(stderr, "ERROR: failed to write entire message\n");
504513
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
505-
goto cleanup;
514+
goto exit;
506515
}
507516

508517
/* Read the server data into our buff array */
509518
memset(buff, 0, sizeof(buff));
510519
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
511520
fprintf(stderr, "ERROR: failed to read\n");
512-
goto cleanup;
521+
goto exit;
513522
}
514523

515524
/* Print to stdout any data the server sends */
516525
printf("Server: %s\n", buff);
517526

518-
ret = 0;
527+
ret = 0; /* return success */
519528

529+
exit:
520530
/* Cleanup and return */
521-
cleanup:
522-
wolfSSL_free(ssl); /* Free the wolfSSL object */
523-
ctx_cleanup:
524-
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
531+
if (sockfd != SOCKET_INVALID)
532+
close(sockfd); /* Close the connection to the server */
533+
if (ssl != NULL)
534+
wolfSSL_free(ssl); /* Free the wolfSSL object */
535+
if (ctx != NULL)
536+
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
525537
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
526-
socket_cleanup:
527-
close(sockfd); /* Close the connection to the server */
528-
end:
529538

530539
#else
531540
printf("Please configure wolfSSL with --enable-cryptocb and try again\n");
532541
#endif /* WOLF_CRYPTO_CB */
533-
return ret; /* Return reporting a success */
542+
543+
return ret;
534544
}

0 commit comments

Comments
 (0)