Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tls-options/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ Example:
-a <Peer auth mode> -m <Verify mode>
```

#### Use SMTP client

You can use smtp OVERSSL/STARTTLS client.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a corresponding server?
Can you add a pointer to relevant RFC and/or specifications?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Added relevant RFCs

->SMTP OVER TLS - RFC 8314
->STARTTLS - RFC 3207

  • Added example SMTP server (smtp.gmail.com)


Example:

```sh
./client-tls-smtp-starttls <SERVER_NAME> <CERT_FILE>
```

```sh
./client-tls-smtp-overssl <SERVER_NAME> <CERT_FILE>
```

## Cleaning Up

You can remove executable files by doing:
Expand Down
45 changes: 23 additions & 22 deletions tls-options/client-tls-resume.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>

/* wolfSSL */
#include <wolfssl/options.h>
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -118,7 +119,7 @@ int main(int argc, char **argv)

char msg[MSG_SIZE];
int ret = WOLFSSL_FAILURE;

(void)ipadd;

/* SSL objects */
Expand All @@ -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");
Expand All @@ -148,23 +149,23 @@ 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");
goto cleanup;
}

/* 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);
goto cleanup;
}

/* 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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -226,7 +227,7 @@ int main(int argc, char **argv)
printf("Session is not reused. New session was negotiated.\n");
}

/*
/*
* Application messaging
*/
while (1) {
Expand All @@ -235,18 +236,18 @@ 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) {
print_SSL_error("failed SSL write", ssl);
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;
Expand Down
37 changes: 19 additions & 18 deletions tls-options/client-tls-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>

/* wolfSSL */
#include <wolfssl/options.h>
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -86,7 +87,7 @@ static int write_SESS(WOLFSSL_SESSION* sess, const char* file)
fclose(fp);
if (buff)
free(buff);

return ret;
}

Expand All @@ -102,7 +103,7 @@ int main(int argc, char **argv)

char msg[MSG_SIZE];
int ret = WOLFSSL_FAILURE;

(void)ipadd;

/* SSL objects */
Expand All @@ -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");
Expand All @@ -135,23 +136,23 @@ 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");
goto cleanup;
}

/* 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);
goto cleanup;
}

/* 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);
Expand All @@ -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);
Expand All @@ -199,7 +200,7 @@ int main(int argc, char **argv)
goto cleanup;
}

/*
/*
* Application messaging
*/
while (1) {
Expand All @@ -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);
Expand Down
Loading