diff options
-rw-r--r-- | configure.ac | 5 | ||||
-rw-r--r-- | src/Makefile.am | 12 | ||||
-rw-r--r-- | src/pack.c | 60 | ||||
-rw-r--r-- | src/pack.h | 2 | ||||
-rw-r--r-- | src/proone-unpack.c | 10 | ||||
-rw-r--r-- | src/util_rt.c | 135 |
6 files changed, 67 insertions, 157 deletions
diff --git a/configure.ac b/configure.ac index 8779eec..7928ec4 100644 --- a/configure.ac +++ b/configure.ac @@ -36,7 +36,10 @@ AM_COND_IF([TESTS], [ [AC_MSG_ERROR([gtest/gtest.h not found.])]) ]) -PKG_CHECK_MODULES_STATIC([DEP], [zlib openssl]) +PKG_CHECK_MODULES_STATIC([DEP_PKGCFG], [zlib]) +AC_CHECK_LIB([mbedcrypto], [mbedtls_cipher_setup], [], [AC_MSG_ERROR([mbedtls not found])]) +AC_CHECK_LIB([mbedtls], [mbedtls_ssl_init], [], [AC_MSG_ERROR([mbedtls not found])]) +AC_CHECK_LIB([mbedx509], [mbedtls_x509_crt_parse], [], [AC_MSG_ERROR([mbedtls not found])]) AC_CONFIG_FILES([Makefile src/Makefile]) AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am index e8509ec..c35dcc2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,30 +22,30 @@ libproone_a_SOURCES =\ rnd.c proone_LDFLAGS = -static -proone_LDADD = libproone.a $(DEP_LIBS) -lrt +proone_LDADD = libproone.a $(DEP_PKGCFG_LIBS) $(LIBS) -lrt -lpthread proone_SOURCES =\ worker.c\ heartbeat-worker.c\ proone.c proone_pack_LDADD = libproone.a -proone_pack_LDFLAGS = $(DEP_LIBS) +proone_pack_LDFLAGS = $(DEP_PKGCFG_LIBS) $(LIBS) proone_pack_SOURCES = proone-pack.c proone_unpack_LDADD = libproone.a -proone_unpack_LDFLAGS = $(DEP_LIBS) +proone_unpack_LDFLAGS = $(DEP_PKGCFG_LIBS) $(LIBS) proone_unpack_SOURCES = proone-unpack.c proone_list_arch_LDADD = libproone.a -proone_list_arch_LDFLAGS = $(DEP_LIBS) +proone_list_arch_LDFLAGS = $(DEP_PKGCFG_LIBS) $(LIBS) proone_list_arch_SOURCES = proone-list-arch.c proone_mask_LDADD = libproone.a -proone_mask_LDFLAGS = $(DEP_LIBS) +proone_mask_LDFLAGS = $(DEP_PKGCFG_LIBS) $(LIBS) proone_mask_SOURCES = proone-mask.c proone_print_all_data_LDADD = libproone.a -proone_print_all_data_LDFLAGS = $(DEP_LIBS) +proone_print_all_data_LDFLAGS = $(DEP_PKGCFG_LIBS) $(LIBS) proone_print_all_data_SOURCES = proone-print-all-data.c if TESTS @@ -10,10 +10,8 @@ #include <unistd.h> #include <errno.h> -#include <openssl/bio.h> -#include <openssl/evp.h> -#include <openssl/err.h> #include <zlib.h> +#include <mbedtls/base64.h> void prne_init_bin_archive (prne_bin_archive_t *a) { @@ -33,30 +31,29 @@ void prne_init_unpack_bin_archive_result (prne_unpack_bin_archive_result_t *r) { } prne_unpack_bin_archive_result_t prne_unpack_bin_archive (const int fd) { - static const size_t fd_buf_size = 77, bio_buf_size = 58, z_buf_size = 4096; + static const size_t fd_buf_size = 77, b64_buf_size = 58, z_buf_size = 1024; prne_unpack_bin_archive_result_t ret; - BIO *b64_bio = NULL, *mem_bio = NULL; - uint8_t *mem = NULL, *fd_buf = NULL, *bio_buf = NULL, *z_buf = NULL; - int fd_read_size, fd_data_size, bio_write_size, bio_read_size; + uint8_t *mem = NULL, *fd_buf = NULL, *b64_buf = NULL, *z_buf = NULL; + int fd_read_size, fd_data_size, rem_size = 0; int z_func_ret; z_stream stream; - size_t z_out_size; + size_t dec_b64_size, z_out_size; void *ny_buf; bool stream_end; prne_init_unpack_bin_archive_result(&ret); memset(&stream, 0, sizeof(z_stream)); - mem = (uint8_t*)prne_malloc(1, fd_buf_size + bio_buf_size + z_buf_size); + mem = (uint8_t*)prne_malloc(1, fd_buf_size + b64_buf_size + z_buf_size); if (mem == NULL) { ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; ret.err = errno; goto END; } fd_buf = mem; - bio_buf = mem + fd_buf_size; - z_buf = mem + fd_buf_size + bio_buf_size; + b64_buf = mem + fd_buf_size; + z_buf = mem + fd_buf_size + b64_buf_size; z_func_ret = inflateInit(&stream); if (z_func_ret != Z_OK) { @@ -65,17 +62,9 @@ prne_unpack_bin_archive_result_t prne_unpack_bin_archive (const int fd) { goto END; } - if ((mem_bio = BIO_new(BIO_s_mem())) == NULL || (b64_bio = BIO_new(BIO_f_base64())) == NULL) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_OPENSSL_ERR; - ret.err = ERR_get_error(); - goto END; - } - BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); - BIO_push(b64_bio, mem_bio); - stream_end = false; do { - fd_read_size = read(fd, fd_buf, fd_buf_size); + fd_read_size = read(fd, fd_buf + rem_size, fd_buf_size - rem_size); if (fd_read_size < 0) { ret.result = PRNE_UNPACK_BIN_ARCHIVE_ERRNO; ret.err = errno; @@ -84,28 +73,21 @@ prne_unpack_bin_archive_result_t prne_unpack_bin_archive (const int fd) { if (fd_read_size == 0) { break; } + fd_read_size += rem_size; - // remove white spaces - fd_data_size = prne_str_shift_spaces((char*)fd_buf, (size_t)fd_read_size); + fd_read_size = fd_data_size = prne_str_shift_spaces((char*)fd_buf, (size_t)fd_read_size); + fd_data_size = fd_data_size / 4 * 4; + rem_size = fd_read_size - fd_data_size; if (fd_data_size > 0) { - BIO_reset(mem_bio); - bio_write_size = BIO_write(mem_bio, fd_buf, fd_data_size); - if (bio_write_size != fd_data_size) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR; - goto END; + ret.err = mbedtls_base64_decode(b64_buf, b64_buf_size, &dec_b64_size, fd_buf, fd_data_size); + if (ret.err != 0) { + ret.result = PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR; } - bio_read_size = BIO_read(b64_bio, bio_buf, (int)bio_buf_size); - if (bio_read_size < 0) { - ret.result = PRNE_UNPACK_BIN_ARCHIVE_OPENSSL_ERR; - ret.err = ERR_get_error(); - goto END; - } - - if (bio_read_size > 0) { - stream.avail_in = bio_read_size; - stream.next_in = bio_buf; + if (dec_b64_size > 0) { + stream.avail_in = dec_b64_size; + stream.next_in = b64_buf; do { stream.avail_out = z_buf_size; stream.next_out = z_buf; @@ -139,6 +121,8 @@ prne_unpack_bin_archive_result_t prne_unpack_bin_archive (const int fd) { } while (stream.avail_out == 0); } } + + memmove(fd_buf, fd_buf + fd_data_size, rem_size); } while (!stream_end); if (ret.data_size == 0) { @@ -153,8 +137,6 @@ END: ret.data_size = 0; } inflateEnd(&stream); - BIO_free(b64_bio); - BIO_free(mem_bio); return ret; } @@ -19,7 +19,7 @@ struct prne_bin_archive { typedef enum { PRNE_UNPACK_BIN_ARCHIVE_OK, - PRNE_UNPACK_BIN_ARCHIVE_OPENSSL_ERR, + PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR, PRNE_UNPACK_BIN_ARCHIVE_Z_ERR, PRNE_UNPACK_BIN_ARCHIVE_ERRNO, PRNE_UNPACK_BIN_ARCHIVE_MEM_ERR, diff --git a/src/proone-unpack.c b/src/proone-unpack.c index 32b80da..d865bd4 100644 --- a/src/proone-unpack.c +++ b/src/proone-unpack.c @@ -6,8 +6,8 @@ #include <unistd.h> #include <fcntl.h> -#include <openssl/err.h> #include <zlib.h> +#include <mbedtls/error.h> #include "pack.h" #include "util_rt.h" @@ -15,14 +15,16 @@ static void report_unpack_bin_archive_err (const prne_unpack_bin_archive_result_t *r) { const char *err_str, *err_msg = NULL; + char err_buf[1024]; switch (r->result) { case PRNE_UNPACK_BIN_ARCHIVE_OK: err_str = "ok"; break; - case PRNE_UNPACK_BIN_ARCHIVE_OPENSSL_ERR: - err_str = "openssl error"; - err_msg = ERR_error_string(r->err, NULL); + case PRNE_UNPACK_BIN_ARCHIVE_CRYPTO_ERR: + err_str = "crypto error"; + mbedtls_strerror(r->err, err_buf, 1024); + err_msg = err_buf; break; case PRNE_UNPACK_BIN_ARCHIVE_Z_ERR: err_str = "zlib error"; diff --git a/src/util_rt.c b/src/util_rt.c index 28133e0..6a4d139 100644 --- a/src/util_rt.c +++ b/src/util_rt.c @@ -6,8 +6,7 @@ #include <errno.h> -#include <openssl/bio.h> -#include <openssl/evp.h> +#include <mbedtls/base64.h> void prne_succeed_or_die (const int ret) { @@ -117,124 +116,48 @@ int prne_cmp_timespec (const struct timespec *a, const struct timespec *b) { } char *prne_enc_base64_mem (const uint8_t *data, const size_t size) { - char *ret = NULL, *p = NULL; - BIO *b64_bio = NULL, *mem_bio = NULL; - bool ok = true; - int out_len; + size_t ret_size; + char *ret; - if (size > INT32_MAX || size == 0) { + mbedtls_base64_encode(NULL, 0, &ret_size, data, size); + if (ret_size == 0) { return NULL; } - - b64_bio = BIO_new(BIO_f_base64()); - mem_bio = BIO_new(BIO_s_mem()); - if (b64_bio == NULL || mem_bio == NULL) { - ok = false; - goto END; - } - BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); - BIO_push(b64_bio, mem_bio); - - if (BIO_write(b64_bio, data, size) != (int)size) { - ok = false; - goto END; - } - - out_len = BIO_get_mem_data(mem_bio, &p); - if (out_len < 0) { - ok = false; - goto END; - } - if (out_len > 0) { - ret = (char*)prne_malloc(1, out_len + 1); - if (ret == NULL) { - ok = false; - goto END; - } - memcpy(ret, p, out_len); - ret[out_len] = 0; + ret = (char*)prne_malloc(1, ret_size); + if (ret == NULL) { + return NULL; } -END: - BIO_free(b64_bio); - BIO_free(mem_bio); - if (!ok) { + if (mbedtls_base64_encode((uint8_t*)ret, ret_size, &ret_size, data, size) < 0) { prne_free(ret); - ret = NULL; + return NULL; } return ret; } bool prne_dec_base64_mem (const char *str, const size_t str_len, uint8_t **data, size_t *size) { - char *in_mem = NULL; - size_t in_mem_len, out_len; - uint8_t *out_mem = NULL; - BIO *b64_bio = NULL, *mem_bio = NULL; - bool ret = true; - int read_size = 0; - - if (str_len > INT32_MAX) { - errno = EINVAL; - return false; + size_t ret_size; + uint8_t *ret; + + mbedtls_base64_decode(NULL, 0, &ret_size, (uint8_t*)str, str_len); + if (ret_size == 0) { + *data = NULL; + *size = 0; + return true; } - if (str_len == 0) { - ret = true; - goto END; - } - - in_mem = (char*)prne_malloc(1, str_len); - if (in_mem == NULL) { - ret = false; - goto END; - } - memcpy(in_mem, str, str_len); - in_mem_len = prne_str_shift_spaces(in_mem, str_len); - if (in_mem_len == 0) { - ret = true; - goto END; - } - - b64_bio = BIO_new(BIO_f_base64()); - mem_bio = BIO_new_mem_buf(in_mem, in_mem_len); - if (b64_bio == NULL || mem_bio == NULL) { - ret = false; - goto END; - } - BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); - BIO_push(b64_bio, mem_bio); - - out_len = in_mem_len * 3 / 4; - out_mem = (uint8_t*)prne_malloc(1, (size_t)out_len); - if (out_mem == NULL) { - ret = false; - goto END; - } - - read_size = BIO_read(b64_bio, out_mem, out_len); - if (read_size < 0) { - ret = false; - goto END; - } - -END: - BIO_free(b64_bio); - BIO_free(mem_bio); - prne_free(in_mem); - if (ret) { - if (read_size > 0) { - *data = out_mem; - *size = (size_t)read_size; - } - else { - prne_free(out_mem); - *data = NULL; - *size = 0; - } + ret = prne_malloc(1, ret_size); + if (ret == NULL) { + return false; } - else { - prne_free(out_mem); + + if (mbedtls_base64_decode(ret, ret_size, &ret_size, (uint8_t*)str, str_len) < 0) { + prne_free(ret); + errno = EINVAL; + return false; } - return ret; + *data = ret; + *size = ret_size; + return true; } |