Refactor base64 functions to common static library.

This commit is contained in:
Roger A. Light
2024-03-17 21:34:27 +00:00
parent 9fe8b95dcf
commit 16d465fa49
27 changed files with 41 additions and 70 deletions
+1 -1
View File
@@ -1,7 +1,6 @@
if(WITH_TLS AND CJSON_FOUND)
add_executable(mosquitto_ctrl
mosquitto_ctrl.c mosquitto_ctrl.h
../../common/base64_mosq.c ../../common/base64_mosq.h
broker.c
client.c
dynsec.c
@@ -58,6 +57,7 @@ if(WITH_TLS AND CJSON_FOUND)
target_link_libraries(mosquitto_ctrl
PRIVATE
common-options
libmosquitto_common
OpenSSL::SSL
cJSON
)
+1 -5
View File
@@ -6,7 +6,7 @@ include ${R}/config.mk
LOCAL_CFLAGS+=
LOCAL_CPPFLAGS+=-I${R}/lib -I${R}/apps/mosquitto_passwd -I${R}/plugins/dynamic-security -I${R}/common
LOCAL_LDFLAGS+=
LOCAL_LDADD+=-lcjson -ldl ${LIBMOSQ} ${LIB_ARGON2}
LOCAL_LDADD+=-lcjson -ldl ${LIBMOSQ} ${LIBMOSQ_COMMON} ${LIB_ARGON2}
# ------------------------------------------
# Compile time options
@@ -33,7 +33,6 @@ OBJS= \
options.o \
OBJS_EXTERNAL= \
base64_mosq.o \
get_password.o \
json_help.o \
password_mosq.o
@@ -57,9 +56,6 @@ mosquitto_ctrl_example.so : ${EXAMPLE_OBJS}
${OBJS} : %.o: %.c mosquitto_ctrl.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
base64_mosq.o : ${R}/common/base64_mosq.c ${R}/common/base64_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
example.o : example.c mosquitto_ctrl.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -fPIC -c $< -o $@
+2 -3
View File
@@ -32,7 +32,6 @@ Contributors:
#include "mosquitto_ctrl.h"
#include "mosquitto.h"
#include "base64_mosq.h"
#include "json_help.h"
#include "password_mosq.h"
#include "get_password.h"
@@ -587,8 +586,8 @@ static cJSON *init_add_client(const char *username, const char *password, const
if(pw.hashtype == pw_sha512_pbkdf2){
char *salt_b64 = NULL, *password_b64 = NULL;
if(base64__encode(pw.params.sha512_pbkdf2.salt, pw.params.sha512_pbkdf2.salt_len, &salt_b64)
|| base64__encode(pw.params.sha512_pbkdf2.password_hash, sizeof(pw.params.sha512_pbkdf2.password_hash), &password_b64)
if(mosquitto_base64_encode(pw.params.sha512_pbkdf2.salt, pw.params.sha512_pbkdf2.salt_len, &salt_b64)
|| mosquitto_base64_encode(pw.params.sha512_pbkdf2.password_hash, sizeof(pw.params.sha512_pbkdf2.password_hash), &password_b64)
|| cJSON_AddStringToObject(j_client, "salt", salt_b64) == NULL
|| cJSON_AddStringToObject(j_client, "password", password_b64) == NULL
|| cJSON_AddNumberToObject(j_client, "iterations", pw.params.sha512_pbkdf2.iterations) == NULL){
+2 -2
View File
@@ -240,11 +240,11 @@ int dynsec_client__file_set_password(int argc, char *argv[], const char *file)
char *password_b64, *salt_b64;
cJSON *j_password = NULL, *j_salt = NULL, *j_iterations = NULL;
if(base64__encode(client.pw.params.sha512_pbkdf2.password_hash, sizeof(client.pw.params.sha512_pbkdf2.password_hash), &password_b64) != MOSQ_ERR_SUCCESS){
if(mosquitto_base64_encode(client.pw.params.sha512_pbkdf2.password_hash, sizeof(client.pw.params.sha512_pbkdf2.password_hash), &password_b64) != MOSQ_ERR_SUCCESS){
fprintf(stderr, "Error: Problem generating password hash.\n");
return MOSQ_ERR_NOMEM;
}
if(base64__encode(client.pw.params.sha512_pbkdf2.salt, client.pw.params.sha512_pbkdf2.salt_len, &salt_b64) != MOSQ_ERR_SUCCESS){
if(mosquitto_base64_encode(client.pw.params.sha512_pbkdf2.salt, client.pw.params.sha512_pbkdf2.salt_len, &salt_b64) != MOSQ_ERR_SUCCESS){
free(password_b64);
fprintf(stderr, "Error: Problem generating password hash.\n");
return MOSQ_ERR_NOMEM;
-1
View File
@@ -2,7 +2,6 @@ if(WITH_TLS)
add_executable(mosquitto_passwd
mosquitto_passwd.c
get_password.c get_password.h
../../common/base64_mosq.c ../../common/base64_mosq.h
../../common/misc_mosq.c ../../common/misc_mosq.h
../../common/password_mosq.c ../../common/password_mosq.h
)
-4
View File
@@ -13,7 +13,6 @@ OBJS= \
get_password.o \
OBJS_EXTERNAL= \
base64_mosq.o \
misc_mosq.o \
password_mosq.o
@@ -37,9 +36,6 @@ mosquitto_passwd.a : ${OBJS} ${OBJS_EXTERNAL}
${OBJS} : %.o: %.c
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
base64_mosq.o : ${R}/common/base64_mosq.c ${R}/common/base64_mosq.h
${CROSS_COMPILE}${CC} ${LOCAL_CPPFLAGS} $(LOCAL_CFLAGS) -c $< -o $@
misc_mosq.o : ${R}/common/misc_mosq.c ${R}/common/misc_mosq.h
${CROSS_COMPILE}${CC} ${LOCAL_CPPFLAGS} $(LOCAL_CFLAGS) -c $< -o $@
-1
View File
@@ -29,7 +29,6 @@ Contributors:
#include "mosquitto.h"
#include "get_password.h"
#include "base64_mosq.h"
#include "password_mosq.h"
#ifdef WIN32
+8 -9
View File
@@ -26,7 +26,6 @@ Contributors:
#include <string.h>
#include "mosquitto.h"
#include "base64_mosq.h"
#include "password_mosq.h"
#ifdef WIN32
@@ -221,12 +220,12 @@ static int pw__encode_sha512_pbkdf2(struct mosquitto_pw *pw)
int rc;
char *salt64 = NULL, *hash64 = NULL;
rc = base64__encode(pw->params.sha512_pbkdf2.salt, pw->params.sha512_pbkdf2.salt_len, &salt64);
rc = mosquitto_base64_encode(pw->params.sha512_pbkdf2.salt, pw->params.sha512_pbkdf2.salt_len, &salt64);
if(rc){
return MOSQ_ERR_UNKNOWN;
}
rc = base64__encode(pw->params.sha512_pbkdf2.password_hash, sizeof(pw->params.sha512_pbkdf2.password_hash), &hash64);
rc = mosquitto_base64_encode(pw->params.sha512_pbkdf2.password_hash, sizeof(pw->params.sha512_pbkdf2.password_hash), &hash64);
if(rc){
free(salt64);
return MOSQ_ERR_UNKNOWN;
@@ -278,7 +277,7 @@ static int pw__decode_sha512_pbkdf2(struct mosquitto_pw *pw, const char *salt_pa
return MOSQ_ERR_INVAL;
}
rc = base64__decode(salt_b64, &salt, &salt_len);
rc = mosquitto_base64_decode(salt_b64, &salt, &salt_len);
if(rc != MOSQ_ERR_SUCCESS || (salt_len != 12 && salt_len != HASH_LEN)){
free(sp_heap);
free(salt);
@@ -294,7 +293,7 @@ static int pw__decode_sha512_pbkdf2(struct mosquitto_pw *pw, const char *salt_pa
return MOSQ_ERR_INVAL;
}
rc = base64__decode(password_b64, &password, &password_len);
rc = mosquitto_base64_decode(password_b64, &password, &password_len);
free(sp_heap);
if(rc != MOSQ_ERR_SUCCESS || password_len != HASH_LEN){
@@ -391,12 +390,12 @@ static int pw__encode_sha512(struct mosquitto_pw *pw)
int rc;
char *salt64 = NULL, *hash64 = NULL;
rc = base64__encode(pw->params.sha512.salt, pw->params.sha512.salt_len, &salt64);
rc = mosquitto_base64_encode(pw->params.sha512.salt, pw->params.sha512.salt_len, &salt64);
if(rc){
return MOSQ_ERR_UNKNOWN;
}
rc = base64__encode(pw->params.sha512.password_hash, sizeof(pw->params.sha512.password_hash), &hash64);
rc = mosquitto_base64_encode(pw->params.sha512.password_hash, sizeof(pw->params.sha512.password_hash), &hash64);
if(rc){
return MOSQ_ERR_UNKNOWN;
}
@@ -435,7 +434,7 @@ static int pw__decode_sha512(struct mosquitto_pw *pw, const char *salt_password)
return MOSQ_ERR_INVAL;
}
rc = base64__decode(salt_b64, &salt, &salt_len);
rc = mosquitto_base64_decode(salt_b64, &salt, &salt_len);
if(rc != MOSQ_ERR_SUCCESS || (salt_len != 12 && salt_len != HASH_LEN)){
free(sp_heap);
free(salt);
@@ -451,7 +450,7 @@ static int pw__decode_sha512(struct mosquitto_pw *pw, const char *salt_password)
return MOSQ_ERR_INVAL;
}
rc = base64__decode(password_b64, &password, &password_len);
rc = mosquitto_base64_decode(password_b64, &password, &password_len);
free(sp_heap);
if(rc != MOSQ_ERR_SUCCESS || password_len != HASH_LEN){
+1
View File
@@ -26,6 +26,7 @@ Contributors:
extern "C" {
#endif
#include <mosquitto/libcommon_base64.h>
#include <mosquitto/libcommon_memory.h>
#include <mosquitto/libcommon_properties.h>
#include <mosquitto/libcommon_string.h>
@@ -1,5 +1,5 @@
/*
Copyright (c) 2012-2020 Roger Light <roger@atchoo.org>
Copyright (c) 2010-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
@@ -15,18 +15,23 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifndef BASE64_MOSQ_H
#define BASE64_MOSQ_H
#ifndef MOSQUITTO_LIBCOMMON_BASE64_H
#define MOSQUITTO_LIBCOMMON_BASE64_H
/*
* File: mosquitto/libcommon_base64.h
*
* This header contains functions for handling base64
*/
#ifdef __cplusplus
extern "C" {
#endif
int base64__encode(const unsigned char *in, size_t in_len, char **encoded);
int base64__decode(const char *in, unsigned char **decoded, unsigned int *decoded_len);
int mosquitto_base64_encode(const unsigned char *in, size_t in_len, char **encoded);
int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned int *decoded_len);
#ifdef __cplusplus
}
#endif
#endif
-1
View File
@@ -86,7 +86,6 @@ if(WITH_WEBSOCKETS AND WITH_WEBSOCKETS_BUILTIN)
add_definitions("-DWITH_WEBSOCKETS=WS_IS_BUILTIN")
set(C_SRC ${C_SRC}
../deps/picohttpparser/picohttpparser.c
../common/base64_mosq.c ../common/base64_mosq.h
http_client.c http_client.h
net_ws.c
)
-4
View File
@@ -83,7 +83,6 @@ OBJS= \
will_mosq.o
OBJS_EXTERNAL= \
base64_mosq.o \
misc_mosq.o \
password_mosq.o
@@ -143,9 +142,6 @@ libmosquitto.a : ${OBJS} ${OBJS_EXTERNAL}
${OBJS} : %.o: %.c ${R}/include/mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
base64_mosq.o : ${R}/common/base64_mosq.c ${R}/common/base64_mosq.h
${CROSS_COMPILE}$(CC) $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
misc_mosq.o : ${R}/common/misc_mosq.c ${R}/common/misc_mosq.h
${CROSS_COMPILE}$(CC) $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
+1 -2
View File
@@ -25,7 +25,6 @@ Contributors:
#include <string.h>
#include "mosquitto_internal.h"
#include "base64_mosq.h"
#include "http_client.h"
#include "mosquitto/mqtt_protocol.h"
#include "net_mosq.h"
@@ -40,7 +39,7 @@ static int create_request_key(char **encoded)
{
uint8_t bytes[16];
util__random_bytes(bytes, sizeof(bytes));
return base64__encode(bytes, sizeof(bytes), encoded);
return mosquitto_base64_encode(bytes, sizeof(bytes), encoded);
}
+1 -2
View File
@@ -27,7 +27,6 @@ Contributors:
#include <stddef.h>
#include <string.h>
#include "base64_mosq.h"
#include "mosquitto_internal.h"
#include "mosquitto/mqtt_protocol.h"
#include "net_mosq.h"
@@ -350,7 +349,7 @@ int ws__create_accept_key(const char *client_key, size_t client_key_len, char **
if(EVP_DigestFinal_ex(evp, accept_key_hash, &accept_key_hash_len) != 0){
EVP_MD_CTX_free(evp);
return base64__encode(accept_key_hash, accept_key_hash_len, encoded);
return mosquitto_base64_encode(accept_key_hash, accept_key_hash_len, encoded);
}
}
}
+1
View File
@@ -1,4 +1,5 @@
set(C_SRC
base64_common.c
memory_common.c
mqtt_common.c
property_common.c
+1
View File
@@ -12,6 +12,7 @@ LOCAL_LIBADD+=
.PHONY : really clean install
OBJS= \
base64_common.o \
memory_common.o \
mqtt_common.o \
property_common.o \
@@ -18,23 +18,18 @@ Contributors:
#include "config.h"
#include <errno.h>
#ifdef WITH_TLS
# include <openssl/opensslv.h>
# include <openssl/evp.h>
# include <openssl/rand.h>
# include <openssl/buffer.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mosquitto.h"
#include "base64_mosq.h"
#ifdef WITH_TLS
int base64__encode(const unsigned char *in, size_t in_len, char **encoded)
int mosquitto_base64_encode(const unsigned char *in, size_t in_len, char **encoded)
{
BIO *bmem, *b64;
BUF_MEM *bptr = NULL;
@@ -65,7 +60,7 @@ int base64__encode(const unsigned char *in, size_t in_len, char **encoded)
}
int base64__decode(const char *in, unsigned char **decoded, unsigned int *decoded_len)
int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned int *decoded_len)
{
BIO *bmem, *b64;
size_t slen;
-1
View File
@@ -4,7 +4,6 @@ if(WITH_TLS)
set(SRCLIST
acl.c
auth.c
../../common/base64_mosq.c ../../common/base64_mosq.h
clients.c
clientlist.c
config.c
-4
View File
@@ -31,7 +31,6 @@ OBJS = \
tick.o
OBJS_EXTERNAL = \
base64_mosq.o \
json_help.o \
misc_mosq.o \
password_mosq.o \
@@ -46,9 +45,6 @@ endif
all : ${ALL_DEPS}
base64_mosq.o : ${R}/common/base64_mosq.c ${R}/common/base64_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
json_help.o : ${R}/common/json_help.c ${R}/common/json_help.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
+4 -4
View File
@@ -164,7 +164,7 @@ int dynsec_clients__config_load(struct dynsec__data *data, cJSON *tree)
client->pw.hashtype = pw_sha512_pbkdf2;
client->pw.params.sha512_pbkdf2.iterations = iterations;
if(base64__decode(salt, &buf, &buf_len) != MOSQ_ERR_SUCCESS
if(mosquitto_base64_decode(salt, &buf, &buf_len) != MOSQ_ERR_SUCCESS
|| buf_len > sizeof(client->pw.params.sha512_pbkdf2.salt)){
mosquitto_free(buf);
@@ -175,7 +175,7 @@ int dynsec_clients__config_load(struct dynsec__data *data, cJSON *tree)
client->pw.params.sha512_pbkdf2.salt_len = (size_t)buf_len;
mosquitto_free(buf);
if(base64__decode(password, &buf, &buf_len) != MOSQ_ERR_SUCCESS
if(mosquitto_base64_decode(password, &buf, &buf_len) != MOSQ_ERR_SUCCESS
|| buf_len != sizeof(client->pw.params.sha512_pbkdf2.password_hash)){
mosquitto_free(buf);
@@ -279,7 +279,7 @@ static int dynsec__config_add_clients(struct dynsec__data *data, cJSON *j_client
if(client->pw.hashtype == pw_sha512_pbkdf2){
char *buf;
if(base64__encode(client->pw.params.sha512_pbkdf2.password_hash, sizeof(client->pw.params.sha512_pbkdf2.password_hash), &buf) != MOSQ_ERR_SUCCESS){
if(mosquitto_base64_encode(client->pw.params.sha512_pbkdf2.password_hash, sizeof(client->pw.params.sha512_pbkdf2.password_hash), &buf) != MOSQ_ERR_SUCCESS){
return 1;
}
cJSON *jtmp = cJSON_CreateString(buf);
@@ -287,7 +287,7 @@ static int dynsec__config_add_clients(struct dynsec__data *data, cJSON *j_client
if(jtmp == NULL) return 1;
cJSON_AddItemToObject(j_client, "password", jtmp);
if(base64__encode(client->pw.params.sha512_pbkdf2.salt, client->pw.params.sha512_pbkdf2.salt_len, &buf) != MOSQ_ERR_SUCCESS){
if(mosquitto_base64_encode(client->pw.params.sha512_pbkdf2.salt, client->pw.params.sha512_pbkdf2.salt_len, &buf) != MOSQ_ERR_SUCCESS){
return 1;
}
+2 -2
View File
@@ -161,8 +161,8 @@ static int generate_password(struct dynsec__data *data, cJSON *j_client, char **
if(pw.hashtype == pw_sha512_pbkdf2){
char *salt_b64 = NULL, *password_b64 = NULL;
if(base64__encode(pw.params.sha512_pbkdf2.salt, pw.params.sha512_pbkdf2.salt_len, &salt_b64)
|| base64__encode(pw.params.sha512_pbkdf2.password_hash, sizeof(pw.params.sha512_pbkdf2.password_hash), &password_b64)
if(mosquitto_base64_encode(pw.params.sha512_pbkdf2.salt, pw.params.sha512_pbkdf2.salt_len, &salt_b64)
|| mosquitto_base64_encode(pw.params.sha512_pbkdf2.password_hash, sizeof(pw.params.sha512_pbkdf2.password_hash), &password_b64)
|| cJSON_AddStringToObject(j_client, "salt", salt_b64) == NULL
|| cJSON_AddStringToObject(j_client, "password", password_b64) == NULL
|| cJSON_AddNumberToObject(j_client, "iterations", pw.params.sha512_pbkdf2.iterations) == NULL){
@@ -22,7 +22,6 @@ Contributors:
#include <uthash.h>
#include "mosquitto.h"
#include "password_mosq.h"
#include "base64_mosq.h"
#define PRIORITY_MAX 100000
-1
View File
@@ -1,6 +1,5 @@
set (MOSQ_SRCS
../lib/alias_mosq.c ../lib/alias_mosq.h
../common/base64_mosq.c ../common/base64_mosq.h
bridge.c bridge_topic.c
broker_control.c
conf.c
-4
View File
@@ -112,7 +112,6 @@ OBJS= mosquitto.o \
OBJS_EXTERNAL= \
alias_mosq.o \
base64_mosq.o \
handle_ping.o \
handle_pubackcomp.o \
handle_pubrec.o \
@@ -154,9 +153,6 @@ ${OBJS} : %.o: %.c mosquitto_broker_internal.h
alias_mosq.o : ${R}/lib/alias_mosq.c ${R}/lib/alias_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
base64_mosq.o : ${R}/common/base64_mosq.c ${R}/common/base64_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
handle_ping.o : ${R}/lib/handle_ping.c ${R}/lib/read_handle.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
-1
View File
@@ -25,7 +25,6 @@ Contributors:
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "base64_mosq.h"
#include "mosquitto/mqtt_protocol.h"
#include "net_mosq.h"
#include "packet_mosq.h"
-1
View File
@@ -25,7 +25,6 @@ Contributors:
#include "mosquitto_broker_internal.h"
#include "mosquitto/mqtt_protocol.h"
#include "send_mosq.h"
#include "base64_mosq.h"
#include "misc_mosq.h"
#include "util_mosq.h"
+3 -3
View File
@@ -12,7 +12,7 @@ add_executable(bridge-topic-test
stubs.c
)
target_compile_definitions(bridge-topic-test PRIVATE WITH_BRIDGE WITH_BROKER)
target_link_libraries(bridge-topic-test PRIVATE bridge-topic-obj common-unit-test-header libmosquitto_common)
target_link_libraries(bridge-topic-test PRIVATE bridge-topic-obj common-unit-test-header libmosquitto_common OpenSSL::SSL)
add_test(NAME unit-bridge-topic-test COMMAND bridge-topic-test)
# keepalive-test
@@ -21,7 +21,7 @@ add_executable(keepalive-test
keepalive_stubs.c
)
target_compile_definitions(keepalive-test PRIVATE WITH_BROKER)
target_link_libraries(keepalive-test PRIVATE common-unit-test-header libmosquitto_common)
target_link_libraries(keepalive-test PRIVATE common-unit-test-header libmosquitto_common OpenSSL::SSL)
add_test(NAME unit-keepalive-test COMMAND keepalive-test)
# persist-read-test
@@ -99,5 +99,5 @@ add_executable(subs-test
)
target_compile_definitions(subs-test PRIVATE WITH_PERSISTENCE WITH_BROKER WITH_SYS_TREE)
target_link_libraries(subs-test PRIVATE common-unit-test-header subs-obj libmosquitto_common)
target_link_libraries(subs-test PRIVATE common-unit-test-header subs-obj libmosquitto_common OpenSSL::SSL)
add_test(NAME unit-subs-test COMMAND subs-test)