Skip to content

Commit 1c6e1c4

Browse files
committed
Add mutual auth, RSA/ECC and TLS v1.2/v1.3 support to TLS crypto callback examples.
1 parent d505779 commit 1c6e1c4

2 files changed

Lines changed: 85 additions & 7 deletions

File tree

tls/client-tls-cryptocb.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,28 @@
3131
#include <unistd.h>
3232

3333
/* wolfSSL */
34-
#include <wolfssl/options.h>
34+
#ifndef WOLFSSL_USER_SETTINGS
35+
#include <wolfssl/options.h>
36+
#endif
3537
#include <wolfssl/ssl.h>
3638
#include <wolfssl/wolfcrypt/sha256.h>
3739
#include <wolfssl/wolfcrypt/cryptocb.h>
3840
#include <wolfssl/wolfcrypt/error-crypt.h>
3941

4042
#define DEFAULT_PORT 11111
4143

42-
#define CA_FILE "../certs/ca-cert.pem"
44+
#define USE_ECDHE_ECDSA
45+
#define USE_TLSV13
46+
47+
#ifdef USE_ECDHE_ECDSA
48+
#define CERT_FILE "../certs/client-ecc-cert.pem"
49+
#define KEY_FILE "../certs/ecc-client-key.pem"
50+
#define CA_FILE "../certs/ca-ecc-cert.pem"
51+
#else
52+
#define CERT_FILE "../certs/client-cert.pem"
53+
#define KEY_FILE "../certs/client-key.pem"
54+
#define CA_FILE "../certs/ca-cert.pem"
55+
#endif
4356

4457
#ifdef WOLF_CRYPTO_CB
4558
/* Example custom context for crypto callback */
@@ -555,12 +568,45 @@ int main(int argc, char** argv)
555568
#endif
556569

557570
/* Create and initialize WOLFSSL_CTX */
558-
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())) == NULL) {
571+
#ifdef USE_TLSV13
572+
ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
573+
#else
574+
ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
575+
#endif
576+
if (ctx == NULL) {
559577
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
560578
ret = -1;
561579
goto exit;
562580
}
563581

582+
/* Mutual Authentication */
583+
/* Load client certificate into WOLFSSL_CTX */
584+
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE,
585+
WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
586+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
587+
CERT_FILE);
588+
goto exit;
589+
}
590+
591+
/* Load client key into WOLFSSL_CTX */
592+
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE,
593+
WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
594+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
595+
KEY_FILE);
596+
goto exit;
597+
}
598+
599+
/* Load CA certificate into WOLFSSL_CTX for validating peer */
600+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
601+
!= WOLFSSL_SUCCESS) {
602+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
603+
CA_FILE);
604+
goto exit;
605+
}
606+
607+
/* validate peer certificate */
608+
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
609+
564610
/* register a devID for crypto callbacks */
565611
wolfSSL_CTX_SetDevId(ctx, devId);
566612

tls/server-tls-cryptocb.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,28 @@
3131
#include <unistd.h>
3232

3333
/* wolfSSL */
34-
#include <wolfssl/options.h>
34+
#ifndef WOLFSSL_USER_SETTINGS
35+
#include <wolfssl/options.h>
36+
#endif
3537
#include <wolfssl/ssl.h>
38+
#include <wolfssl/wolfcrypt/sha256.h>
39+
#include <wolfssl/wolfcrypt/cryptocb.h>
40+
#include <wolfssl/wolfcrypt/error-crypt.h>
3641

3742
#define DEFAULT_PORT 11111
3843

39-
#define CERT_FILE "../certs/server-cert.pem"
40-
#define KEY_FILE "../certs/server-key.pem"
44+
#define USE_ECDHE_ECDSA
45+
#define USE_TLSV13
46+
47+
#ifdef USE_ECDHE_ECDSA
48+
#define CERT_FILE "../certs/server-ecc.pem"
49+
#define KEY_FILE "../certs/ecc-key.pem"
50+
#define CA_FILE "../certs/client-ecc-cert.pem"
51+
#else
52+
#define CERT_FILE "../certs/server-cert.pem"
53+
#define KEY_FILE "../certs/server-key.pem"
54+
#define CA_FILE "../certs/client-cert.pem"
55+
#endif
4156

4257
#ifdef WOLF_CRYPTO_CB
4358
/* Example custom context for crypto callback */
@@ -518,7 +533,12 @@ int main(int argc, char** argv)
518533
#endif
519534

520535
/* Create and initialize WOLFSSL_CTX */
521-
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method())) == NULL) {
536+
#ifdef USE_TLSV13
537+
ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
538+
#else
539+
ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
540+
#endif
541+
if (ctx == NULL) {
522542
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
523543
ret = -1;
524544
goto exit;
@@ -543,6 +563,18 @@ int main(int argc, char** argv)
543563
goto exit;
544564
}
545565

566+
/* Load CA certificate into WOLFSSL_CTX for validating peer */
567+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
568+
!= WOLFSSL_SUCCESS) {
569+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
570+
CA_FILE);
571+
goto exit;
572+
}
573+
574+
/* enable mutual authentication */
575+
wolfSSL_CTX_set_verify(ctx,
576+
WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
577+
546578
#if 0
547579
/* Example: "TLS13-AES256-GCM-SHA384", "TLS13-AES128-GCM-SHA256" or "TLS13-CHACHA20-POLY1305-SHA256" */
548580
wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384");

0 commit comments

Comments
 (0)