Merge 45b8d1e0b3 into e691776d7f
commit
c649621ca9
|
|
@ -123,6 +123,25 @@ Example:
|
|||
-a <Peer auth mode> -m <Verify mode>
|
||||
```
|
||||
|
||||
#### Use SMTP client
|
||||
|
||||
You can use SMTP OVER TLS or STARTTLS client.
|
||||
|
||||
- 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-smtp-starttls <SERVER_NAME> <CERT_FILE_PATH>
|
||||
```
|
||||
|
||||
```sh
|
||||
./client-smtp-over-tls <SERVER_NAME> <CERT_FILE_PATH>
|
||||
```
|
||||
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,506 @@
|
|||
/* client-smtp-over-tls.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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* socket includes */
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* wolfSSL */
|
||||
#include <wolfssl/options.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/wolfcrypt/coding.h>
|
||||
|
||||
/* smtp overssl commands */
|
||||
const char* oversslCmd[17] = {
|
||||
"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: ",
|
||||
"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 <SERVER_NAME> <CERT_FILE PATH>\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<sizeof(plain); len++) {
|
||||
if (plain[len] == '\n') break;
|
||||
}
|
||||
|
||||
/* Encode the mail to Base64 */
|
||||
outLen = sizeof(buff);
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (Base64_Encode((unsigned char*) plain, len, (unsigned char*) buff, &outLen) != 0) {
|
||||
fprintf(stderr, "ERROR: failed to encode the mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/*Change the line end to CRLF */
|
||||
strcpy(buff+outLen-1, "\r\n");
|
||||
|
||||
/* Send encoded email address to the server */
|
||||
len = strnlen(buff, sizeof(buff));
|
||||
if ((ret = wolfSSL_write(ssl, buff, outLen+1)) != 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[5], strlen(oversslCmd[5]))) {
|
||||
printf("%s\n", buff);
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: incorrect command received\n");
|
||||
printf("%s\n", buff);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Get the password for mail account */
|
||||
printf("Password: ");
|
||||
memset(plain, 0, sizeof(plain));
|
||||
if (fgets(plain, sizeof(plain), stdin) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to get password\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
/* Get the right password length */
|
||||
for (len=0; len<sizeof(plain); len++) {
|
||||
if (plain[len] == '\n') break;
|
||||
}
|
||||
|
||||
/* Encode the password to Base64 */
|
||||
outLen = sizeof(buff);
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (Base64_Encode((unsigned char*) plain, len, (unsigned char*) buff, &outLen) != 0) {
|
||||
fprintf(stderr, "ERROR: failed to encode the mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Change the line end to CRLF */
|
||||
strcpy(buff+outLen-1, "\r\n");
|
||||
|
||||
/* Send the encoded password to the server */
|
||||
len = strnlen(buff, sizeof(buff));
|
||||
if ((ret = wolfSSL_write(ssl, buff, outLen+1)) != 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[6], strlen(oversslCmd[6]))) {
|
||||
printf("%s\n", buff);
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: incorrect command received\n");
|
||||
printf("%s\n", buff);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Get the sender mail address */
|
||||
printf("Mail From: ");
|
||||
memset(buff, 0, sizeof(buff));
|
||||
strcpy(buff, oversslCmd[7]);
|
||||
if (fgets(buff + strlen(oversslCmd[7]), sizeof(buff), stdin) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to get sender mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
strcpy(buff+strlen(buff)-1, ">\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;
|
||||
}
|
||||
|
||||
/* 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");
|
||||
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[14], strlen(oversslCmd[14]))) {
|
||||
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[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;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
|
|
@ -0,0 +1,532 @@
|
|||
/* client-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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* socket includes */
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* wolfSSL */
|
||||
#include <wolfssl/options.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/wolfcrypt/coding.h>
|
||||
|
||||
/* smtp starttls commands */
|
||||
const char* starttlsCmd[19] = {
|
||||
"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: ",
|
||||
"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 <SERVER_NAME> <CERT_FILE_PATH>\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 <host> 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 <host> 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<sizeof(plain); len++) {
|
||||
if (plain[len] == '\n') break;
|
||||
}
|
||||
|
||||
/* Encode the mail to Base64 */
|
||||
outLen = sizeof(buff);
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (Base64_Encode((unsigned char*) plain, len, (unsigned char*) buff, &outLen) !=0) {
|
||||
fprintf(stderr, "ERROR: failed to encode the mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
||||
/*Change the line end to CRLF */
|
||||
strcpy(buff+outLen-1, "\r\n");
|
||||
|
||||
/* Send encoded email address to the server */
|
||||
len = strnlen(buff, sizeof(buff));
|
||||
if ((ret = wolfSSL_write(ssl, buff, outLen+1)) != 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[7], strlen(starttlsCmd[7]))) {
|
||||
printf("%s\n", buff);
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: incorrect command received\n");
|
||||
printf("%s\n", buff);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Get the password for mail account */
|
||||
printf("Password: ");
|
||||
memset(plain, 0, sizeof(plain));
|
||||
if (fgets(plain, sizeof(plain), stdin) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to get password\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
/* Get the right password length */
|
||||
for (len=0; len<sizeof(plain); len++) {
|
||||
if (plain[len] == '\n') break;
|
||||
}
|
||||
|
||||
/* Encode the password to Base64 */
|
||||
outLen = sizeof(buff);
|
||||
memset(buff, 0, sizeof(buff));
|
||||
if (Base64_Encode((unsigned char*) plain, len, (unsigned char*) buff, &outLen) !=0) {
|
||||
fprintf(stderr, "ERROR: failed to encode the mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Change the line end to CRLF */
|
||||
strcpy(buff+outLen-1, "\r\n");
|
||||
|
||||
/* Send the encoded password to the server */
|
||||
len = strnlen(buff, sizeof(buff));
|
||||
if ((ret = wolfSSL_write(ssl, buff, outLen+1)) != 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[8], strlen(starttlsCmd[8]))) {
|
||||
printf("%s\n", buff);
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: incorrect command received\n");
|
||||
printf("%s\n", buff);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Get the sender mail address */
|
||||
printf("Mail From: ");
|
||||
memset(buff, 0, sizeof(buff));
|
||||
strcpy(buff, starttlsCmd[9]);
|
||||
if (fgets(buff+strlen(starttlsCmd[9]), sizeof(buff), stdin) == NULL) {
|
||||
fprintf(stderr, "ERROR: failed to get sender mail address.\n");
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
strcpy(buff+strlen(buff)-1, ">\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;
|
||||
}
|
||||
|
||||
/* 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[16], strlen(starttlsCmd[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(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;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
|
|
@ -48,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,
|
||||
|
|
@ -68,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 {
|
||||
|
|
@ -119,7 +119,7 @@ int main(int argc, char **argv)
|
|||
|
||||
char msg[MSG_SIZE];
|
||||
int ret = WOLFSSL_FAILURE;
|
||||
|
||||
|
||||
(void)ipadd;
|
||||
|
||||
/* SSL objects */
|
||||
|
|
@ -129,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");
|
||||
|
|
@ -149,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");
|
||||
|
|
@ -157,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);
|
||||
|
|
@ -165,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);
|
||||
|
|
@ -179,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);
|
||||
|
|
@ -207,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");
|
||||
|
|
@ -227,7 +227,7 @@ int main(int argc, char **argv)
|
|||
printf("Session is not reused. New session was negotiated.\n");
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* Application messaging
|
||||
*/
|
||||
while (1) {
|
||||
|
|
@ -236,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) {
|
||||
|
|
@ -244,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;
|
||||
|
|
|
|||
|
|
@ -48,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,
|
||||
|
|
@ -76,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;
|
||||
|
|
@ -87,7 +87,7 @@ cleanup:
|
|||
fclose(fp);
|
||||
if (buff)
|
||||
free(buff);
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ int main(int argc, char **argv)
|
|||
|
||||
char msg[MSG_SIZE];
|
||||
int ret = WOLFSSL_FAILURE;
|
||||
|
||||
|
||||
(void)ipadd;
|
||||
|
||||
/* SSL objects */
|
||||
|
|
@ -114,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");
|
||||
|
|
@ -136,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");
|
||||
|
|
@ -144,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);
|
||||
|
|
@ -152,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);
|
||||
|
|
@ -166,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);
|
||||
|
|
@ -200,7 +200,7 @@ int main(int argc, char **argv)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* Application messaging
|
||||
*/
|
||||
while (1) {
|
||||
|
|
@ -218,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_get1_session(ssl);
|
||||
if (session == NULL) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue