From b2cd11f327d431215b2f7d597919e09bb79ae2c2 Mon Sep 17 00:00:00 2001 From: yota Date: Sat, 25 Jan 2025 15:53:58 +0900 Subject: [PATCH 1/5] Added client-tls-smtp-overssl.c --- tls/client-tls-smtp-overssl.c | 549 ++++++++++++++++++++++++++++++++++ 1 file changed, 549 insertions(+) create mode 100644 tls/client-tls-smtp-overssl.c diff --git a/tls/client-tls-smtp-overssl.c b/tls/client-tls-smtp-overssl.c new file mode 100644 index 00000000..0d91e5c1 --- /dev/null +++ b/tls/client-tls-smtp-overssl.c @@ -0,0 +1,549 @@ +/* client-tls-smtp-overssl.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/* the usual suspects */ +#include +#include +#include +#include + +/* socket includes */ +#include +#include +#include +#include +#include + +/* wolfSSL */ +#include +#include +#include + +/* smtp overssl commands */ +const char* oversslCmd[19] = { + "220", + "EHLO mail.example.com\r\n", + "250", + "AUTH LOGIN\r\n", + "334", + "334", + "235", + "MAIL FROM:<", + "250", + "RCPT TO:<", + "250", + "DATA\r\n", + "354", + "Subject: ", + "To: ", + "From: ", + "250", + "QUIT\r\n", + "221" +}; + +int main(int argc, char** argv) +{ + int sockfd; + struct addrinfo hints, *res; + char buff[512], plain[512]; + size_t len; + int ret; + word32 outLen; + + /* declare wolfSSL objects */ + WOLFSSL_CTX* ctx; + WOLFSSL* ssl; + + /* Check for proper calling convention */ + if (argc != 3) { + printf("usage: %s \n", argv[0]); + return 0; + } + + /* Initialize the addrinfo struct with zero */ + memset(&hints, 0, sizeof(hints)); + + /* Fill in the addrinfo struct */ + hints.ai_family = AF_INET; /* using IPv4 */ + hints.ai_socktype = SOCK_STREAM; /* means TCP socket */ + char *service = "465"; /* use port 465 as a default */ + + /* Get a Domain IP address */ + if (getaddrinfo(argv[1], service,&hints, &res) != 0) { + fprintf(stderr, "ERROR: failed to get the server ip\n"); + ret = -1; + goto end; + } + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + fprintf(stderr, "ERROR: failed to create the socket\n"); + ret = -1; + goto end; + } + /* Free a list pointed by res */ + freeaddrinfo(res); + /* Connect to the server */ + if ((ret = connect(sockfd, res->ai_addr, res->ai_addrlen)) == -1) { + fprintf(stderr, "ERROR: failed to connect\n"); + goto end; + } + + /*---------------------------------*/ + /* Start of wolfSSL initialization and configuration */ + /*---------------------------------*/ + /* Initialize wolfSSL */ + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: Failed to initialize the library\n"); + goto socket_cleanup; + } + + /* Create and initialize WOLFSSL_CTX */ + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); + ret = -1; + goto socket_cleanup; + } + + /* Load client certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_load_verify_locations(ctx, argv[2], NULL)) + != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", + argv[2]); + goto ctx_cleanup; + } + + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); + ret = -1; + goto ctx_cleanup; + } + + /* Attach wolfSSL to the socket */ + if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: Failed to set the file descriptor\n"); + goto cleanup; + } + + /* Connect to wolfSSL on the server side */ + if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to connect to wolfSSL\n"); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[0], strlen(oversslCmd[0]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "%s\n", buff); + goto cleanup; + } + + /* Send "EHLO mail.example.com\r\n" to the server */ + len = strlen(oversslCmd[1]); + if ((ret = wolfSSL_write(ssl, oversslCmd[1], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[2], strlen(oversslCmd[2]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Send "AUTH LOGIN\r\n" to the server */ + len = strlen(oversslCmd[3]); + if ((ret = wolfSSL_write(ssl, oversslCmd[3], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[4], strlen(oversslCmd[4]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Get the mail address */ + printf("Mail Address: "); + memset(plain, 0, sizeof(plain)); + if (fgets(plain, sizeof(plain), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get mail address.\n"); + ret = -1; + goto cleanup; + } + /* Get the right mail address length */ + for (len=0; len\r\n"); + printf("%s\n", buff); + + /* Send the sender mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[8], strlen(oversslCmd[8]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Get the right receiver mail address */ + printf("RCPT to: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, oversslCmd[9]); + if (fgets(buff+strlen(oversslCmd[9]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff)-1, ">\r\n"); + printf("%s\n", buff); + + /* Send the receiver mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[10], strlen(oversslCmd[10]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + /* Send "DATA\r\n" to the server */ + memset(buff, 0, sizeof(buff)); + len = strlen(oversslCmd[11]); + if ((ret = wolfSSL_write(ssl, oversslCmd[11], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[12], strlen(oversslCmd[12]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Compose the mail */ + /* Get the Subject */ + printf("Subject: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, oversslCmd[13]); + if (fgets(buff+strlen(oversslCmd[13]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get the mail subject.\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the mail Subject to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the mail subject.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Receiver mail address */ + printf("To: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, oversslCmd[14]); + if (fgets(buff+strlen(oversslCmd[14]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the receiver mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the receiver mail address.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Sender mail address */ + printf("From: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, oversslCmd[15]); + if (fgets(buff+strlen(oversslCmd[15]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the sender mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the sender mail address.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* main message */ + printf("main message: "); + memset(buff, 0, sizeof(buff)); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message.\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the main message to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send message.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Notify the end of the mail input to the server */ + memset(buff, 0, sizeof(buff)); + strcpy(buff, ".\r\n"); + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[16], strlen(oversslCmd[16]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Send "QUIT\r\n" to the server */ + memset(buff, 0, sizeof(buff)); + len = strlen(oversslCmd[17]); + if ((ret = wolfSSL_write(ssl, oversslCmd[17], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, oversslCmd[18], strlen(oversslCmd[18]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + + + /* Cleanup and return */ +cleanup: + wolfSSL_free(ssl); /* Free the wolfSSL object */ +ctx_cleanup: + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ +socket_cleanup: + close(sockfd); /* Close the connection to the server */ +end: + return ret; /* Return reporting a success */ +} From f3b588342d49be54571ddd79fb8ff06932454716 Mon Sep 17 00:00:00 2001 From: yota Date: Sat, 25 Jan 2025 15:56:26 +0900 Subject: [PATCH 2/5] Added client-tls-smtp-starttls.c --- tls/client-tls-smtp-starttls.c | 574 +++++++++++++++++++++++++++++++++ 1 file changed, 574 insertions(+) create mode 100644 tls/client-tls-smtp-starttls.c diff --git a/tls/client-tls-smtp-starttls.c b/tls/client-tls-smtp-starttls.c new file mode 100644 index 00000000..9052cd57 --- /dev/null +++ b/tls/client-tls-smtp-starttls.c @@ -0,0 +1,574 @@ +/* client-tls-smtp-starttls.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/* the usual suspects */ +#include +#include +#include +#include + +/* socket includes */ +#include +#include +#include +#include +#include + +/* wolfSSL */ +#include +#include +#include + +/* smtp starttls commands */ +const char* starttlsCmd[21] = { + "220", + "EHLO mail.example.com\r\n", + "250", + "STARTTLS\r\n", + "220", + "AUTH LOGIN\r\n", + "334", + "334", + "235", + "MAIL FROM:<", + "250", + "RCPT TO:<", + "250", + "DATA\r\n", + "354", + "Subject: ", + "To: ", + "From: ", + "250", + "QUIT\r\n", + "221" +}; + +int main(int argc, char** argv) +{ + int sockfd; + struct addrinfo hints, *res; + char buff[512], plain[512]; + size_t len; + int ret; + word32 outLen; + + /* declare wolfSSL objects */ + WOLFSSL_CTX* ctx; + WOLFSSL* ssl; + + /* Check for proper calling convention */ + if (argc != 3) { + printf("usage: %s \n", argv[0]); + return 0; + } + + /* Initialize the addrinfo struct with zero */ + memset(&hints, 0, sizeof(hints)); + + /* Fill in the addrinfo struct */ + hints.ai_family = AF_INET; /* using IPv4 */ + hints.ai_socktype = SOCK_STREAM; /* means TCP socket */ + char *service = "587"; /* use port 587 as a default */ + + /* Get a Domain IP address */ + if (getaddrinfo(argv[1], service, &hints, &res) != 0) { + fprintf(stderr, "ERROR: failed to get the server ip\n"); + ret = -1; + goto end; + } + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { + fprintf(stderr, "ERROR: failed to create the socket\n"); + ret = -1; + goto end; + } + /* Free a list pointed by res */ + freeaddrinfo(res); + /* Connect to the server */ + if ((ret = connect(sockfd, res->ai_addr, res->ai_addrlen)) == -1) { + fprintf(stderr, "ERROR: failed to connect\n"); + goto end; + } + + /* S: 220 SMTP service ready */ + memset(buff, 0, sizeof(buff)); + if (recv(sockfd, buff, sizeof(buff)-1, 0) < 0) { + fprintf(stderr, "failed to read STARTTLS command\n"); + goto end; + } + + if ((!strncmp(buff, starttlsCmd[0], strlen(starttlsCmd[0]))) && + (buff[strlen(starttlsCmd[0])] == ' ')) { + printf("%s\n", buff); + } else { + fprintf(stderr, "incorrect STARTTLS command received\n"); + goto end; + } + + /* C: EHLO mail.example.com */ + if (send(sockfd, starttlsCmd[1], (int)strlen(starttlsCmd[1]), 0) != + (int)strlen(starttlsCmd[1])) { + fprintf(stderr, "failed to send STARTTLS EHLO command\n"); + goto end; + } + + /* S: 250 offers a warm hug of welcome */ + memset(buff, 0, sizeof(buff)); + if (recv(sockfd, buff, sizeof(buff)-1, 0) < 0) { + fprintf(stderr, "failed to read STARTTLS command\n"); + goto end; + } + + if ((!strncmp(buff, starttlsCmd[2], strlen(starttlsCmd[2]))) && + (buff[strlen(starttlsCmd[2])] == '-')) { + printf("%s\n", buff); + } else { + fprintf(stderr, "incorrect STARTTLS command received\n"); + goto end; + } + + /* C: STARTTLS */ + if (send(sockfd, starttlsCmd[3], (int)strlen(starttlsCmd[3]), 0) != + (int)strlen(starttlsCmd[3])) { + fprintf(stderr, "failed to send STARTTLS command\n"); + goto end; + } + + /* S: 220 Go ahead */ + memset(buff, 0, sizeof(buff)); + if (recv(sockfd, buff, sizeof(buff)-1, 0) < 0) { + fprintf(stderr, "failed to read STARTTLS command\n"); + goto end; + } + buff[sizeof(buff)-1] = '\0'; + + if ((!strncmp(buff, starttlsCmd[4], strlen(starttlsCmd[4]))) && + (buff[strlen(starttlsCmd[4])] == ' ')) { + printf("%s\n", buff); + } else { + fprintf(stderr, "incorrect STARTTLS command received, expected 220\n"); + goto end; + } + + /*---------------------------------*/ + /* Start of wolfSSL initialization and configuration */ + /*---------------------------------*/ + /* Initialize wolfSSL */ + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: Failed to initialize the library\n"); + goto socket_cleanup; + } + + /* Create and initialize WOLFSSL_CTX */ + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); + ret = -1; + goto socket_cleanup; + } + + /* Load client certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_load_verify_locations(ctx, argv[2], NULL)) + != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", + argv[2]); + goto ctx_cleanup; + } + + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); + ret = -1; + goto ctx_cleanup; + } + + /* Attach wolfSSL to the socket */ + if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: Failed to set the file descriptor\n"); + goto cleanup; + } + + /* Connect to wolfSSL on the server side */ + if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to connect to wolfSSL\n"); + goto cleanup; + } + + /* Send "AUTH LOGIN\r\n" to the server */ + len = strlen(starttlsCmd[5]); + if ((ret = wolfSSL_write(ssl, starttlsCmd[5], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[6], strlen(starttlsCmd[6]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Get the mail address */ + printf("Mail Address: "); + memset(plain, 0, sizeof(plain)); + if (fgets(plain, sizeof(plain), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get mail address.\n"); + ret = -1; + goto cleanup; + } + /* Get the right mail address length */ + for (len=0; len\r\n"); + printf("%s\n", buff); + + /* Send the sender mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[10], strlen(starttlsCmd[10]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Get the right receiver mail address */ + printf("RCPT to: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, starttlsCmd[11]); + if (fgets(buff+strlen(starttlsCmd[11]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff)-1, ">\r\n"); + printf("%s\n", buff); + + /* Send the receiver mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[12], strlen(starttlsCmd[12]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + /* Send "DATA\r\n" to the server */ + memset(buff, 0, sizeof(buff)); + len = strlen(starttlsCmd[13]); + if ((ret = wolfSSL_write(ssl, starttlsCmd[13], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[14], strlen(starttlsCmd[14]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Compose the mail */ + /* Get the Subject */ + printf("Subject: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, starttlsCmd[15]); + if (fgets(buff+strlen(starttlsCmd[15]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get the mail subject.\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the mail Subject to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the mail subject.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Receiver mail address */ + printf("To: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, starttlsCmd[16]); + if (fgets(buff+strlen(starttlsCmd[16]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the receiver mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the receiver mail address.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Sender mail address */ + printf("From: "); + memset(buff, 0, sizeof(buff)); + strcpy(buff, starttlsCmd[17]); + if (fgets(buff+strlen(starttlsCmd[17]), sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message for server\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the sender mail address to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send the sender mail address.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* main message */ + printf("main message: "); + memset(buff, 0, sizeof(buff)); + if (fgets(buff, sizeof(buff), stdin) == NULL) { + fprintf(stderr, "ERROR: failed to get message.\n"); + ret = -1; + goto cleanup; + } + + strcpy(buff+strlen(buff), "\r\n"); + + /* Send the main message to the server */ + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send message.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Notify the end of the mail input to the server */ + memset(buff, 0, sizeof(buff)); + strcpy(buff, ".\r\n"); + len = strnlen(buff, sizeof(buff)); + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[18], strlen(starttlsCmd[18]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + /* Send "QUIT\r\n" to the server */ + memset(buff, 0, sizeof(buff)); + len = strlen(starttlsCmd[19]); + if ((ret = wolfSSL_write(ssl, starttlsCmd[19], len)) != len) { + fprintf(stderr, "ERROR: failed to send command.\n"); + fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); + goto cleanup; + } + + /* Read the server data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto cleanup; + } + /* Compare if the response is right code or not */ + if (!strncmp(buff, starttlsCmd[20], strlen(starttlsCmd[20]))) { + printf("%s\n", buff); + } else { + fprintf(stderr, "ERROR: incorrect command received\n"); + printf("%s\n", buff); + goto cleanup; + } + + + + /* Cleanup and return */ +cleanup: + wolfSSL_free(ssl); /* Free the wolfSSL object */ +ctx_cleanup: + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ +socket_cleanup: + close(sockfd); /* Close the connection to the server */ +end: + return ret; /* Return reporting a success */ +} From 1099e5701a2109c717dd7b995508dc1849200ef0 Mon Sep 17 00:00:00 2001 From: yota Date: Fri, 7 Mar 2025 16:26:17 +0900 Subject: [PATCH 3/5] move smtp files to tls-options --- tls-options/README.md | 14 ++++++++++++++ {tls => tls-options}/client-tls-smtp-overssl.c | 0 {tls => tls-options}/client-tls-smtp-starttls.c | 0 3 files changed, 14 insertions(+) rename {tls => tls-options}/client-tls-smtp-overssl.c (100%) rename {tls => tls-options}/client-tls-smtp-starttls.c (100%) diff --git a/tls-options/README.md b/tls-options/README.md index 471c4c7e..1ae9daf1 100644 --- a/tls-options/README.md +++ b/tls-options/README.md @@ -123,6 +123,20 @@ Example: -a -m ``` +#### Use SMTP client + +You can use smtp OVERSSL/STARTTLS client. + +Example: + +```sh +./client-tls-smtp-starttls +``` + +```sh +./client-tls-smtp-overssl +``` + ## Cleaning Up You can remove executable files by doing: diff --git a/tls/client-tls-smtp-overssl.c b/tls-options/client-tls-smtp-overssl.c similarity index 100% rename from tls/client-tls-smtp-overssl.c rename to tls-options/client-tls-smtp-overssl.c diff --git a/tls/client-tls-smtp-starttls.c b/tls-options/client-tls-smtp-starttls.c similarity index 100% rename from tls/client-tls-smtp-starttls.c rename to tls-options/client-tls-smtp-starttls.c From 32caf98d14884c6a1a957293c6de1b3cc65ec917 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 20 Mar 2025 16:35:52 -0700 Subject: [PATCH 4/5] =?UTF-8?q?Fix=20build=20errors=20with=20`error:=20imp?= =?UTF-8?q?licit=20declaration=20of=20function=20=E2=80=98gethostbyname?= =?UTF-8?q?=E2=80=99`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tls-options/client-tls-resume.c | 45 ++++++++++++++++---------------- tls-options/client-tls-session.c | 37 +++++++++++++------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/tls-options/client-tls-resume.c b/tls-options/client-tls-resume.c index 6b7d0110..c1704b76 100644 --- a/tls-options/client-tls-resume.c +++ b/tls-options/client-tls-resume.c @@ -29,6 +29,7 @@ #include #include #include +#include /* wolfSSL */ #include @@ -47,7 +48,7 @@ static void print_SSL_error(const char* msg, SSL* ssl) { int err; - + if (ssl != NULL) { err = wolfSSL_get_error(ssl, 0); fprintf(stderr, "ERROR: %s (err %d, %s)\n", msg, err, @@ -67,28 +68,28 @@ static int read_SESS(const char* file, SSL* ssl) size_t sz; WOLFSSL_SESSION* sess = NULL; int ret = WOLFSSL_FAILURE; - + if (((fp = fopen(file, "rb")) == NULL) || (fseek(fp, 0, SEEK_END) != 0) || ((sz = ftell(fp)) == -1)) { fprintf(stderr, "ERROR : failed file %s operation \n", file); goto cleanup; } - + rewind(fp); if ((buff = (unsigned char*)malloc(sz)) == NULL || (fread(buff, 1, sz, fp) != sz)) { fprintf(stderr, "ERROR : failed reading file\n"); goto cleanup; } - + printf("%s size = %ld\n", SAVED_SESS, sz); - + p = buff; if((sess = wolfSSL_d2i_SSL_SESSION(NULL, (const unsigned char**)&p, sz)) == NULL) { print_SSL_error("wolfSSL_d2i_SSL_SESSION", NULL); } - + if(sess != NULL && (ret = wolfSSL_set_session(ssl, sess) != WOLFSSL_SUCCESS)) { print_SSL_error("failed SSL session", ssl); } else { @@ -118,7 +119,7 @@ int main(int argc, char **argv) char msg[MSG_SIZE]; int ret = WOLFSSL_FAILURE; - + (void)ipadd; /* SSL objects */ @@ -128,15 +129,15 @@ int main(int argc, char **argv) memset(&servAddr, 0, sizeof(servAddr)); /* Check for proper calling convention */ - if (argc == 1) + if (argc == 1) fprintf(stderr, "Send to localhost(%s)\n", LOCALHOST); if (argc >=2) { host = gethostbyname(argv[1]); memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length); } - if (argc >= 3) + if (argc >= 3) ca_cert = argv[2]; - if (argc == 4) + if (argc == 4) port = atoi(argv[3]); if (argc >= 5) { fprintf(stderr, "ERROR: Too many arguments.\n"); @@ -148,7 +149,7 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: failed to initialize the library\n"); goto cleanup; } - + /* Create and initialize an SSL context object*/ if ((ctx = wolfSSL_CTX_new(SSLv23_client_method())) == NULL) { fprintf(stderr, "ERROR: failed to create an SSL context object\n"); @@ -156,7 +157,7 @@ int main(int argc, char **argv) } /* Load client certificate into WOLFwolfSSL_CTX */ - if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, + if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", CERT_FILE); @@ -164,7 +165,7 @@ int main(int argc, char **argv) } /* Load client key into WOLFwolfSSL_CTX */ - if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, + if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", KEY_FILE); @@ -178,17 +179,17 @@ int main(int argc, char **argv) goto cleanup; } - /* - * Set up a TCP Socket and connect to the server + /* + * Set up a TCP Socket and connect to the server */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { fprintf(stderr, "ERROR: failed to create a socket. errno %d\n", errno); goto cleanup; } - + servAddr.sin_family = AF_INET; /* using IPv4 */ servAddr.sin_port = htons(port); /* on DEFAULT_PORT */ - + if ((ret = connect(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr))) == -1) { fprintf(stderr, "ERROR: failed to connect. errno %d\n", errno); @@ -206,7 +207,7 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: failed to read session information\n"); goto cleanup; } - + /* Attach the socket to the SSL */ if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: Failed to set the file descriptor\n"); @@ -226,7 +227,7 @@ int main(int argc, char **argv) printf("Session is not reused. New session was negotiated.\n"); } - /* + /* * Application messaging */ while (1) { @@ -235,7 +236,7 @@ int main(int argc, char **argv) break; if (strcmp(msg, "\n") == 0){ /* if empty send HTTP request */ strncpy(msg, kHttpGetMsg, sizeof(msg)); - } else + } else msg[strnlen(msg, sizeof(msg))-1] = '\0'; /* send a message to the server */ if ((ret = wolfSSL_write(ssl, msg, strnlen(msg, sizeof(msg)))) < 0) { @@ -243,10 +244,10 @@ int main(int argc, char **argv) break; } - /* + /* * closing the session, and write session information into a file * before writing session information, the file is removed if exists - */ + */ if (strcmp(msg, "break") == 0) { printf("Sending break command\n"); ret = WOLFSSL_SUCCESS; diff --git a/tls-options/client-tls-session.c b/tls-options/client-tls-session.c index 8096b87a..34e871a2 100644 --- a/tls-options/client-tls-session.c +++ b/tls-options/client-tls-session.c @@ -29,6 +29,7 @@ #include #include #include +#include /* wolfSSL */ #include @@ -47,7 +48,7 @@ static void print_SSL_error(const char* msg, SSL* ssl) { int err; - + if (ssl != NULL) { err = wolfSSL_get_error(ssl, 0); fprintf(stderr, "ERROR: %s (err %d, %s)\n", msg, err, @@ -75,7 +76,7 @@ static int write_SESS(WOLFSSL_SESSION* sess, const char* file) print_SSL_error("wolfSSL_i2d_SSL_SESSION", NULL); goto cleanup; } - + if ((fwrite(buff, 1, sz, fp)) != sz) { fprintf(stderr, "ERROR : failed fwrite\n"); goto cleanup; @@ -86,7 +87,7 @@ static int write_SESS(WOLFSSL_SESSION* sess, const char* file) fclose(fp); if (buff) free(buff); - + return ret; } @@ -102,7 +103,7 @@ int main(int argc, char **argv) char msg[MSG_SIZE]; int ret = WOLFSSL_FAILURE; - + (void)ipadd; /* SSL objects */ @@ -113,17 +114,17 @@ int main(int argc, char **argv) /* SSL SESSION object */ WOLFSSL_SESSION* session= NULL; - + /* Check for proper calling convention */ - if (argc == 1) + if (argc == 1) fprintf(stderr, "Send to localhost(%s)\n", LOCALHOST); if (argc >=2) { host = gethostbyname(argv[1]); memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length); } - if (argc >= 3) + if (argc >= 3) ca_cert = argv[2]; - if (argc == 4) + if (argc == 4) port = atoi(argv[3]); if (argc >= 5) { fprintf(stderr, "ERROR: Too many arguments.\n"); @@ -135,7 +136,7 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: failed to initialize the library\n"); goto cleanup; } - + /* Create and initialize an SSL context object*/ if ((ctx = wolfSSL_CTX_new(SSLv23_client_method())) == NULL) { fprintf(stderr, "ERROR: failed to create an SSL context object\n"); @@ -143,7 +144,7 @@ int main(int argc, char **argv) } /* Load client certificate into WOLFSSL_CTX */ - if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, + if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", CERT_FILE); @@ -151,7 +152,7 @@ int main(int argc, char **argv) } /* Load client key into WOLFSSL_CTX */ - if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, + if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", KEY_FILE); @@ -165,17 +166,17 @@ int main(int argc, char **argv) goto cleanup; } - /* - * Set up a TCP Socket and connect to the server + /* + * Set up a TCP Socket and connect to the server */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { fprintf(stderr, "ERROR: failed to create a socket. errno %d\n", errno); goto cleanup; } - + servAddr.sin_family = AF_INET; /* using IPv4 */ servAddr.sin_port = htons(port); /* on DEFAULT_PORT */ - + if ((ret = connect(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr))) == -1) { fprintf(stderr, "ERROR: failed to connect. errno %d\n", errno); @@ -199,7 +200,7 @@ int main(int argc, char **argv) goto cleanup; } - /* + /* * Application messaging */ while (1) { @@ -217,10 +218,10 @@ int main(int argc, char **argv) break; } - /* + /* * closing the session, and write session information into a file * before writing session information - */ + */ if (strcmp(msg, "break") == 0) { session = wolfSSL_get_session(ssl); ret = write_SESS(session, SAVED_SESS); From 45b8d1e0b3d1aa5ba4372afb05c904fd13e6e847 Mon Sep 17 00:00:00 2001 From: yota Date: Fri, 28 Mar 2025 16:26:45 +0900 Subject: [PATCH 5/5] Fix file names & unnecessary parts, README --- tls-options/README.md | 13 ++-- ...-smtp-overssl.c => client-smtp-over-tls.c} | 59 +++---------------- ...smtp-starttls.c => client-smtp-starttls.c} | 56 +++--------------- 3 files changed, 24 insertions(+), 104 deletions(-) rename tls-options/{client-tls-smtp-overssl.c => client-smtp-over-tls.c} (90%) rename tls-options/{client-tls-smtp-starttls.c => client-smtp-starttls.c} (90%) diff --git a/tls-options/README.md b/tls-options/README.md index 1ae9daf1..eba4c5e4 100644 --- a/tls-options/README.md +++ b/tls-options/README.md @@ -125,18 +125,23 @@ Example: #### Use SMTP client -You can use smtp OVERSSL/STARTTLS client. +You can use SMTP OVER TLS or STARTTLS client. -Example: +- SMTP OVER TLS - [RFC 8314](https://datatracker.ietf.org/doc/html/rfc8314) + +- STARTTLS - [RFC 3207](https://datatracker.ietf.org/doc/html/rfc3207) +Example: ```sh -./client-tls-smtp-starttls +./client-smtp-starttls ``` ```sh -./client-tls-smtp-overssl +./client-smtp-over-tls ``` +When using Gmail SMTP server (```smtp.gmail.com```), you need to configure an app password in your Google account settings. + ## Cleaning Up You can remove executable files by doing: diff --git a/tls-options/client-tls-smtp-overssl.c b/tls-options/client-smtp-over-tls.c similarity index 90% rename from tls-options/client-tls-smtp-overssl.c rename to tls-options/client-smtp-over-tls.c index 0d91e5c1..0dd0369e 100644 --- a/tls-options/client-tls-smtp-overssl.c +++ b/tls-options/client-smtp-over-tls.c @@ -1,4 +1,4 @@ -/* client-tls-smtp-overssl.c +/* client-smtp-over-tls.c * * Copyright (C) 2006-2025 wolfSSL Inc. * @@ -38,7 +38,7 @@ #include /* smtp overssl commands */ -const char* oversslCmd[19] = { +const char* oversslCmd[17] = { "220", "EHLO mail.example.com\r\n", "250", @@ -53,8 +53,6 @@ const char* oversslCmd[19] = { "DATA\r\n", "354", "Subject: ", - "To: ", - "From: ", "250", "QUIT\r\n", "221" @@ -75,7 +73,7 @@ int main(int argc, char** argv) /* Check for proper calling convention */ if (argc != 3) { - printf("usage: %s \n", argv[0]); + printf("usage: %s \n", argv[0]); return 0; } @@ -235,7 +233,6 @@ int main(int argc, char** argv) goto cleanup; } - /*Change the line end to CRLF */ strcpy(buff+outLen-1, "\r\n"); @@ -426,48 +423,8 @@ int main(int argc, char** argv) goto cleanup; } - /* Receiver mail address */ - printf("To: "); - memset(buff, 0, sizeof(buff)); - strcpy(buff, oversslCmd[14]); - if (fgets(buff+strlen(oversslCmd[14]), sizeof(buff), stdin) == NULL) { - fprintf(stderr, "ERROR: failed to get message for server\n"); - ret = -1; - goto cleanup; - } - - strcpy(buff+strlen(buff), "\r\n"); - - /* Send the receiver mail address to the server */ - len = strnlen(buff, sizeof(buff)); - if ((ret = wolfSSL_write(ssl, buff, len)) != len) { - fprintf(stderr, "ERROR: failed to send the receiver mail address.\n"); - fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); - goto cleanup; - } - - /* Sender mail address */ - printf("From: "); - memset(buff, 0, sizeof(buff)); - strcpy(buff, oversslCmd[15]); - if (fgets(buff+strlen(oversslCmd[15]), sizeof(buff), stdin) == NULL) { - fprintf(stderr, "ERROR: failed to get message for server\n"); - ret = -1; - goto cleanup; - } - - strcpy(buff+strlen(buff), "\r\n"); - - /* Send the sender mail address to the server */ - len = strnlen(buff, sizeof(buff)); - if ((ret = wolfSSL_write(ssl, buff, len)) != len) { - fprintf(stderr, "ERROR: failed to send the sender mail address.\n"); - fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); - goto cleanup; - } - /* main message */ - printf("main message: "); + printf("main message: \n"); memset(buff, 0, sizeof(buff)); if (fgets(buff, sizeof(buff), stdin) == NULL) { fprintf(stderr, "ERROR: failed to get message.\n"); @@ -502,7 +459,7 @@ int main(int argc, char** argv) goto cleanup; } /* Compare if the response is right code or not */ - if (!strncmp(buff, oversslCmd[16], strlen(oversslCmd[16]))) { + if (!strncmp(buff, oversslCmd[14], strlen(oversslCmd[14]))) { printf("%s\n", buff); } else { fprintf(stderr, "ERROR: incorrect command received\n"); @@ -512,8 +469,8 @@ int main(int argc, char** argv) /* Send "QUIT\r\n" to the server */ memset(buff, 0, sizeof(buff)); - len = strlen(oversslCmd[17]); - if ((ret = wolfSSL_write(ssl, oversslCmd[17], len)) != len) { + len = strlen(oversslCmd[15]); + if ((ret = wolfSSL_write(ssl, oversslCmd[15], len)) != len) { fprintf(stderr, "ERROR: failed to send command.\n"); fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); goto cleanup; @@ -526,7 +483,7 @@ int main(int argc, char** argv) goto cleanup; } /* Compare if the response is right code or not */ - if (!strncmp(buff, oversslCmd[18], strlen(oversslCmd[18]))) { + if (!strncmp(buff, oversslCmd[16], strlen(oversslCmd[16]))) { printf("%s\n", buff); } else { fprintf(stderr, "ERROR: incorrect command received\n"); diff --git a/tls-options/client-tls-smtp-starttls.c b/tls-options/client-smtp-starttls.c similarity index 90% rename from tls-options/client-tls-smtp-starttls.c rename to tls-options/client-smtp-starttls.c index 9052cd57..451d340d 100644 --- a/tls-options/client-tls-smtp-starttls.c +++ b/tls-options/client-smtp-starttls.c @@ -1,4 +1,4 @@ -/* client-tls-smtp-starttls.c +/* client-smtp-starttls.c * * Copyright (C) 2006-2025 wolfSSL Inc. * @@ -38,7 +38,7 @@ #include /* smtp starttls commands */ -const char* starttlsCmd[21] = { +const char* starttlsCmd[19] = { "220", "EHLO mail.example.com\r\n", "250", @@ -55,8 +55,6 @@ const char* starttlsCmd[21] = { "DATA\r\n", "354", "Subject: ", - "To: ", - "From: ", "250", "QUIT\r\n", "221" @@ -77,7 +75,7 @@ int main(int argc, char** argv) /* Check for proper calling convention */ if (argc != 3) { - printf("usage: %s \n", argv[0]); + printf("usage: %s \n", argv[0]); return 0; } @@ -451,46 +449,6 @@ int main(int argc, char** argv) goto cleanup; } - /* Receiver mail address */ - printf("To: "); - memset(buff, 0, sizeof(buff)); - strcpy(buff, starttlsCmd[16]); - if (fgets(buff+strlen(starttlsCmd[16]), sizeof(buff), stdin) == NULL) { - fprintf(stderr, "ERROR: failed to get message for server\n"); - ret = -1; - goto cleanup; - } - - strcpy(buff+strlen(buff), "\r\n"); - - /* Send the receiver mail address to the server */ - len = strnlen(buff, sizeof(buff)); - if ((ret = wolfSSL_write(ssl, buff, len)) != len) { - fprintf(stderr, "ERROR: failed to send the receiver mail address.\n"); - fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); - goto cleanup; - } - - /* Sender mail address */ - printf("From: "); - memset(buff, 0, sizeof(buff)); - strcpy(buff, starttlsCmd[17]); - if (fgets(buff+strlen(starttlsCmd[17]), sizeof(buff), stdin) == NULL) { - fprintf(stderr, "ERROR: failed to get message for server\n"); - ret = -1; - goto cleanup; - } - - strcpy(buff+strlen(buff), "\r\n"); - - /* Send the sender mail address to the server */ - len = strnlen(buff, sizeof(buff)); - if ((ret = wolfSSL_write(ssl, buff, len)) != len) { - fprintf(stderr, "ERROR: failed to send the sender mail address.\n"); - fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); - goto cleanup; - } - /* main message */ printf("main message: "); memset(buff, 0, sizeof(buff)); @@ -527,7 +485,7 @@ int main(int argc, char** argv) goto cleanup; } /* Compare if the response is right code or not */ - if (!strncmp(buff, starttlsCmd[18], strlen(starttlsCmd[18]))) { + if (!strncmp(buff, starttlsCmd[16], strlen(starttlsCmd[16]))) { printf("%s\n", buff); } else { fprintf(stderr, "ERROR: incorrect command received\n"); @@ -537,8 +495,8 @@ int main(int argc, char** argv) /* Send "QUIT\r\n" to the server */ memset(buff, 0, sizeof(buff)); - len = strlen(starttlsCmd[19]); - if ((ret = wolfSSL_write(ssl, starttlsCmd[19], len)) != len) { + len = strlen(starttlsCmd[17]); + if ((ret = wolfSSL_write(ssl, starttlsCmd[17], len)) != len) { fprintf(stderr, "ERROR: failed to send command.\n"); fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len); goto cleanup; @@ -551,7 +509,7 @@ int main(int argc, char** argv) goto cleanup; } /* Compare if the response is right code or not */ - if (!strncmp(buff, starttlsCmd[20], strlen(starttlsCmd[20]))) { + if (!strncmp(buff, starttlsCmd[18], strlen(starttlsCmd[18]))) { printf("%s\n", buff); } else { fprintf(stderr, "ERROR: incorrect command received\n");