Skip to content

Commit

Permalink
ssl: review
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jan 11, 2023
1 parent 34da18c commit 7be969c
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 72 deletions.
47 changes: 25 additions & 22 deletions example-client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* openssl: gcc example-client.c openssl.c -lssl -lcrypto
* wolfssl: gcc example-client.c openssl.c -lwolfssl -DHAVE_WOLFSSL
* mbedtls: gcc example-client.c mbedtls.c -lmbedtls -lmbedcrypto -lmbedx509
* openssl: gcc example-client.c openssl.c -lssl -lcrypto -o client
* wolfssl: gcc example-client.c openssl.c -lwolfssl -DHAVE_WOLFSSL -o client
* mbedtls: gcc example-client.c mbedtls.c -lmbedtls -lmbedcrypto -lmbedx509 -o client
*/

#include <sys/select.h>
Expand All @@ -12,16 +12,12 @@
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>

#include "ssl.h"

static void on_verify_error(int error, const char *str, void *arg)
{
fprintf(stderr, "WARNING: SSL certificate error(%d): %s\n", error, str);
}
#include "example.h"

static void chat(void *ssl, int sock)
{
Expand All @@ -47,32 +43,29 @@ static void chat(void *ssl, int sock)
if (FD_ISSET(STDIN_FILENO, &rfds)) {
int n = read(STDIN_FILENO, buf, sizeof(buf));

do {
ret = ssl_write(ssl, buf, n);
if (ret < 0) {
fprintf(stderr, "ssl_write: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return;
}
} while (ret == 0);
ret = ssl_write_nonblock(ssl, sock, buf, n);
if (ret < 0)
return;
printf("Send: %.*s\n", ret, buf);

} else if (FD_ISSET(sock, &rfds)) {
ret = ssl_read(ssl, buf, sizeof(buf));
bool closed;
ret = ssl_read_nonblock(ssl, sock, buf, sizeof(buf), &closed);
if (ret < 0) {
fprintf(stderr, "ssl_read: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
ssl_session_free(ssl);
close(sock);
return;
}

if (ret == 0) {
if (closed) {
fprintf(stderr, "Connection closed by peer\n");
ssl_session_free(ssl);
close(sock);
return;
}

printf("Recv: %.*s\n", ret, buf);
if (ret > 0)
printf("Recv: %.*s\n", ret, buf);
}
}
}
Expand Down Expand Up @@ -100,14 +93,19 @@ static void *connect_ssl(int sock, const char *host)

ssl_set_server_name(ssl, host);

do {
while (true) {
ret = ssl_connect(ssl, on_verify_error, NULL);
if (ret == SSL_OK)
break;

if (ret == SSL_ERROR) {
fprintf(stderr, "ssl_connect: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return NULL;
}
} while (ret == SSL_PENDING);

if (ssl_select(sock, ret))
return NULL;
}

printf("SSL negotiation OK\n");

Expand Down Expand Up @@ -140,6 +138,11 @@ static bool wait_connect(int sock)
return false;
}

if (err) {
fprintf(stderr, "connect: %s\n", strerror(err));
return false;
}

return true;
}

Expand Down
52 changes: 28 additions & 24 deletions example-server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* openssl: gcc example-server.c openssl.c -lssl -lcrypto
* wolfssl: gcc example-server.c openssl.c -lwolfssl -DHAVE_WOLFSSL
* mbedtls: gcc example-server.c mbedtls.c -lmbedtls -lmbedcrypto -lmbedx509
* openssl: gcc example-server.c openssl.c -lssl -lcrypto -o server
* wolfssl: gcc example-server.c openssl.c -lwolfssl -DHAVE_WOLFSSL -o server
* mbedtls: gcc example-server.c mbedtls.c -lmbedtls -lmbedcrypto -lmbedx509 -o server
*/

#define _GNU_SOURCE
Expand All @@ -17,15 +17,10 @@
#include <stdio.h>
#include <errno.h>

#include "ssl.h"
#include "example.h"

static struct ssl_context *ctx;

static void on_verify_error(int error, const char *str, void *arg)
{
fprintf(stderr, "WARNING: SSL certificate error(%d): %s\n", error, str);
}

static void chat(void *ssl, int sock)
{
char err_buf[128];
Expand All @@ -50,32 +45,29 @@ static void chat(void *ssl, int sock)
if (FD_ISSET(STDIN_FILENO, &rfds)) {
int n = read(STDIN_FILENO, buf, sizeof(buf));

do {
ret = ssl_write(ssl, buf, n);
if (ret < 0) {
fprintf(stderr, "ssl_write: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return;
}
} while (ret == 0);
ret = ssl_write_nonblock(ssl, sock, buf, n);
if (ret < 0)
return;
printf("Send: %.*s\n", ret, buf);

} else if (FD_ISSET(sock, &rfds)) {
ret = ssl_read(ssl, buf, sizeof(buf));
bool closed;
ret = ssl_read_nonblock(ssl, sock, buf, sizeof(buf), &closed);
if (ret < 0) {
fprintf(stderr, "ssl_read error: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
ssl_session_free(ssl);
close(sock);
return;
}

if (ret == 0) {
if (closed) {
fprintf(stderr, "Connection closed by peer\n");
ssl_session_free(ssl);
close(sock);
return;
}

printf("Recv: %.*s\n", ret, buf);
if (ret > 0)
printf("Recv: %.*s\n", ret, buf);
}
}
}
Expand All @@ -94,14 +86,19 @@ static void *ssl_negotiation(int sock)

printf("Wait SSL negotiation...\n");

do {
while (true) {
ret = ssl_accept(ssl, on_verify_error, NULL);
if (ret == SSL_OK)
break;

if (ret == SSL_ERROR) {
fprintf(stderr, "ssl_connect: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return NULL;
}
} while (ret == SSL_PENDING);

if (ssl_select(sock, ret))
return NULL;
}

printf("SSL negotiation OK\n");

Expand Down Expand Up @@ -137,8 +134,15 @@ int main(int argc, char **argv)

ctx = ssl_context_new(true);

ssl_load_crt_file(ctx, "example.crt");
ssl_load_key_file(ctx, "example.key");
if (ssl_load_crt_file(ctx, "example.crt")) {
fprintf(stderr, "ssl_load_crt_file fail\n");
return -1;
}

if (ssl_load_key_file(ctx, "example.key")) {
fprintf(stderr, "ssl_load_key_file fail\n");
return -1;
}

printf("Wait connect...\n");

Expand Down
21 changes: 21 additions & 0 deletions example.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICAT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eT5XhUVkEa2zSOw0NmXTCKpNJvPr0ulLLCpSOErD6Jq+sMw5
En+VZhh4QiK7eqqb/tuQclkcGxcSAIRpVg98+PLnwi0VYR2YPnwxV1SAqMDWKHYH
U3X0GiiOs125w7x4zFV9d+73/Q==
-----END CERTIFICATE-----
86 changes: 86 additions & 0 deletions example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef __EXAMPLE_H
#define __EXAMPLE_H

#include <sys/select.h>
#include <stdbool.h>
#include <stdio.h>

#include "ssl.h"

static int ssl_select(int sock, int ret)
{
fd_set fds = {};

FD_SET(sock, &fds);

if (ret == SSL_WANT_READ)
ret = select(sock + 1, &fds, NULL, NULL, NULL);
else if (ret == SSL_WANT_WRITE)
ret = select(sock + 1, NULL, &fds, NULL, NULL);

if (ret < 0) {
perror("select");
return -1;
}

return 0;
}

static void on_verify_error(int error, const char *str, void *arg)
{
fprintf(stderr, "WARNING: SSL certificate error(%d): %s\n", error, str);
}

static int ssl_write_nonblock(void *ssl, int sock, void *data, int len)
{
char err_buf[128];
fd_set fds;
int ret;

while (true) {
ret = ssl_write(ssl, data, len);
if (ret == SSL_ERROR) {
fprintf(stderr, "ssl_write: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return -1;
}

if (ret > 0)
return ret;

if (ssl_select(sock, ret))
return -1;
}
}

static int ssl_read_nonblock(void *ssl, int sock, void *data, int len, bool *closed)
{
char err_buf[128];
fd_set fds;
int ret;

*closed = false;

while (true) {
ret = ssl_read(ssl, data, len);
if (ret == SSL_ERROR) {
fprintf(stderr, "ssl_read: %s\n", ssl_last_error_string(err_buf, sizeof(err_buf)));
return -1;
}

if (ret > 0)
return ret;

if (ret == 0) {
*closed = true;
return 0;
}

if (ret == SSL_WANT_READ)
return 0;

if (ssl_select(sock, ret))
return -1;
}
}

#endif
28 changes: 28 additions & 0 deletions example.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBAD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wLPZrK4CU+tv
L3CgVq0UYwKBgDOWxpsMrkIV1xsG3rlNK9UB0pvKZi5dpr0m7Z03JvBpiX45S55Y
Do0q0M9uXNU7BoWhJhz9iJKa4uV8la7BKUFb/XDeLYyd9xcVPqCPi3OW/+lr4DvR
jKbWIoT2Z4YVNoJ6uQjmghbk7zwGK4XECGxocwfUKVz3/2Nhrydwl6J3AoGAPN4f
rsS7U/9La+SqZgYZzyH/4YnDtGRpW0gwlibwusACqfxVX4+d/JGpMR6L/dYVZrzv
1svo9Bq+sF5H228/2CcKSzNzSeTTxp/TgyWXGjj/4lM9Xp225bLOObHgLD++Yt7p
LJ2owHK2d8+RLtR8OInszrYn/UvrHgKX0SeHI0UCgYEA7YM/WpeB24Wlt2viujEi
EcBx5BLu+uoere7Q9veCXXvbUYm46nkAU0tf2BolGtmDTMdPPf8sVLfwS4xUQiXq
nbe5SHBUHzB/js8FQtnud4SkDB1I/93XoxHsX20quwISQEi146ezTxBdTzC3wDvM
OkkCScqCD+xXUwi1dsbXr3g=
-----END PRIVATE KEY-----
Loading

0 comments on commit 7be969c

Please sign in to comment.