commit 940fb51a14a915a281011238252f45aef0d97fe8
parent 71cc5f78b24a83c50217cbde85a19a7db441235c
Author: Bohdan Potuzhnyi <potub1@bfh.ch>
Date: Sun, 4 Aug 2024 13:31:52 +0200
base64 less strange now, still needs check for a normal way
Diffstat:
4 files changed, 114 insertions(+), 57 deletions(-)
diff --git a/src/challenger/Makefile.am b/src/challenger/Makefile.am
@@ -47,7 +47,8 @@ challenger_httpd_SOURCES = \
challenger-httpd_mhd.c challenger-httpd_mhd.h \
challenger-httpd_setup.c challenger-httpd_setup.h \
challenger-httpd_solve.c challenger-httpd_solve.h \
- challenger-httpd_token.c challenger-httpd_token.h
+ challenger-httpd_token.c challenger-httpd_token.h \
+ src/base64.c src/base64.h
challenger_httpd_LDADD = \
$(top_builddir)/src/util/libchallengerutil.la \
$(top_builddir)/src/challengerdb/libchallengerdb.la \
diff --git a/src/challenger/challenger-httpd_token.c b/src/challenger/challenger-httpd_token.c
@@ -25,62 +25,7 @@
#include "challenger-httpd_common.h"
#include <taler/taler_json_lib.h>
#include <taler/taler_signatures.h>
-#include <gcrypt.h>
-
-// TODO: Until final merge, find out how it opperates normally
-
-// Base64 character set for standard encoding
-static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-// Function to base64 encode
-char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) {
- *output_length = 4 * ((input_length + 2) / 3);
- char *encoded_data = (char *)malloc(*output_length + 1);
- if (encoded_data == NULL) return NULL;
-
- for (size_t i = 0, j = 0; i < input_length;) {
- uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
- uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
- uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
-
- uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
-
- encoded_data[j++] = base64_table[(triple >> 3 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 2 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 1 * 6) & 0x3F];
- encoded_data[j++] = base64_table[(triple >> 0 * 6) & 0x3F];
- }
-
- static const int mod_table[] = {0, 2, 1};
- for (size_t i = 0; i < mod_table[input_length % 3]; ++i)
- encoded_data[*output_length - 1 - i] = '=';
-
- encoded_data[*output_length] = '\0';
- return encoded_data;
-}
-
-// Function to base64url encode (modifies the base64 encoding output)
-char *base64url_encode(const unsigned char *input, size_t len, size_t *out_len) {
- char *encoded_data = base64_encode(input, len, out_len);
- if (encoded_data == NULL) return NULL;
-
- for (size_t i = 0; i < *out_len; i++) {
- if (encoded_data[i] == '+') {
- encoded_data[i] = '-';
- } else if (encoded_data[i] == '/') {
- encoded_data[i] = '_';
- } else if (encoded_data[i] == '=') {
- encoded_data[i] = '\0'; // Remove padding character
- *out_len = i;
- break;
- }
- }
-
- return encoded_data;
-}
-
-//TODO: END
-
+#include "src/base64.h"
/**
* Context for a /token operation.
*/
diff --git a/src/challenger/src/base64.c b/src/challenger/src/base64.c
@@ -0,0 +1,77 @@
+/*
+ This file is part of Challenger
+ Copyright (C) 2023 Taler Systems SA
+
+ Challenger is free software; you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ Challenger 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 Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with
+ Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file src/base64.c
+ * @brief functions to handle base64 encoding
+ * @author Bohdan Potuzhnyi & Vlada Svirsh
+ */
+
+#include <gcrypt.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include "base64.h"
+
+// Base64 character set for standard encoding
+static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+// Function to base64 encode
+char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) {
+ *output_length = 4 * ((input_length + 2) / 3);
+ char *encoded_data = (char *)malloc(*output_length + 1);
+ if (encoded_data == NULL) return NULL;
+
+ for (size_t i = 0, j = 0; i < input_length;) {
+ uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
+ uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
+ uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
+
+ uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
+
+ encoded_data[j++] = base64_table[(triple >> 3 * 6) & 0x3F];
+ encoded_data[j++] = base64_table[(triple >> 2 * 6) & 0x3F];
+ encoded_data[j++] = base64_table[(triple >> 1 * 6) & 0x3F];
+ encoded_data[j++] = base64_table[(triple >> 0 * 6) & 0x3F];
+ }
+
+ static const int mod_table[] = {0, 2, 1};
+ for (size_t i = 0; i < mod_table[input_length % 3]; ++i)
+ encoded_data[*output_length - 1 - i] = '=';
+
+ encoded_data[*output_length] = '\0';
+ return encoded_data;
+}
+
+// Function to base64url encode (modifies the base64 encoding output)
+char *base64url_encode(const unsigned char *input, size_t len, size_t *out_len) {
+ char *encoded_data = base64_encode(input, len, out_len);
+ if (encoded_data == NULL) return NULL;
+
+ for (size_t i = 0; i < *out_len; i++) {
+ if (encoded_data[i] == '+') {
+ encoded_data[i] = '-';
+ } else if (encoded_data[i] == '/') {
+ encoded_data[i] = '_';
+ } else if (encoded_data[i] == '=') {
+ encoded_data[i] = '\0'; // Remove padding character
+ *out_len = i;
+ break;
+ }
+ }
+
+ return encoded_data;
+}
+
diff --git a/src/challenger/src/base64.h b/src/challenger/src/base64.h
@@ -0,0 +1,34 @@
+/*
+ This file is part of Challenger
+ Copyright (C) 2023 Taler Systems SA
+
+ Challenger is free software; you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ Challenger 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 Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with
+ Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file src/base64.h
+ * @brief functions to handle base64 encoding
+ * @author Bohdan Potuzhnyi & Vlada Svirsh
+ */
+
+#ifndef BASE64_H
+#define BASE64_H
+
+#include <stddef.h>
+
+// Function to base64 encode
+char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length);
+
+// Function to base64url encode (modifies the base64 encoding output)
+char *base64url_encode(const unsigned char *input, size_t len, size_t *out_len);
+
+#endif // BASE64_H
+