123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/wolfcrypt/error-crypt.h>
- #if !defined(WOLFCRYPT_ONLY) && !defined(NO_WOLFSSL_CLIENT)
- #include <wolfssl/ssl.h>
- #include <wolfssl/wolfcrypt/logging.h>
- #include <stdio.h>
- #define MAXSZ 1024
- static int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
- {
- int ret = WOLFSSL_CBIO_ERR_GENERAL;
- (void)ssl;
- (void)ctx;
-
- #warning TODO: Implement your own recv data transport
- #if 0
- ret = usart_read_buffer_wait(&cdc_uart_module, buf, sz);
- if (ret == STATUS_ERR_TIMEOUT)
- return WOLFSSL_CBIO_ERR_WANT_READ;
- return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL;
- #else
- return ret;
- #endif
- }
- static int CbIOSend(WOLFSSL *ssl, char *buf, int sz, void *ctx)
- {
- int ret = WOLFSSL_CBIO_ERR_GENERAL;
- (void)ssl;
- (void)ctx;
-
- #warning TODO: Implement your own send data transport
- #if 0
- ret = usart_write_buffer_wait(&cdc_uart_module, buf, sz);
- if (ret == STATUS_ERR_TIMEOUT)
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL;
- #else
- return ret;
- #endif
- }
- static int tls_client(void)
- {
- char msg[] = "Hello WolfSSL!\r\n";
- char reply[MAXSZ];
- int ret, msgSz, error;
- WOLFSSL* ssl = NULL;
- WOLFSSL_CTX* ctx = NULL;
- if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
- printf("CTXnew failed.\n");
- goto fail;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
-
-
-
- if ((ret = wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-ECDSA-AES128-SHA256")) != WOLFSSL_SUCCESS) {
- wolfSSL_CTX_free(ctx);
- printf("CTXset_cipher_list failed, error: %d\n", ret);
- goto fail;
- }
-
-
-
- wolfSSL_CTX_SetIORecv(ctx, CbIORecv);
- wolfSSL_CTX_SetIOSend(ctx, CbIOSend);
- if ((ssl = wolfSSL_new(ctx)) == NULL) {
- error = wolfSSL_get_error(ssl, 0);
- printf("wolfSSL_new failed %d\n", error);
- wolfSSL_CTX_free(ctx);
- return -1;
- }
-
- ret = WOLFSSL_FAILURE;
- while (ret != WOLFSSL_SUCCESS) {
-
- ret = wolfSSL_connect(ssl);
- error = wolfSSL_get_error(ssl, 0);
- if (ret != WOLFSSL_SUCCESS) {
- if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
-
- printf("wolfSSL connect failed with return code %d\n", error);
- goto fail;
- }
- }
-
- }
-
- while (1) {
-
- msgSz = sizeof(msg);
- ret = wolfSSL_write(ssl, msg, msgSz);
- error = wolfSSL_get_error(ssl, 0);
- if (ret != msgSz) {
- if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
-
- goto fail;
- }
- }
-
- break;
- }
- while (1) {
- ret = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
- error = wolfSSL_get_error(ssl, 0);
- if (ret < 0) {
- if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
-
-
- goto fail;
- }
- continue;
- }
- else {
-
- reply[ret] = '\0';
-
- break;
- }
- }
- return 0;
- fail:
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- return -1;
- }
- #endif
- int main(void)
- {
- int ret;
- #if !defined(WOLFCRYPT_ONLY) && !defined(NO_WOLFSSL_CLIENT)
- wolfSSL_Init();
- ret = tls_client();
- wolfSSL_Cleanup();
- #else
- ret = NOT_COMPILED_IN;
- #endif
- return ret;
- }
|