Skip to content

Commit 5c77ce5

Browse files
committed
Move base64 functions to libsudo_util
1 parent af58de4 commit 5c77ce5

File tree

13 files changed

+109
-128
lines changed

13 files changed

+109
-128
lines changed

MANIFEST

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ lib/util/aix.c
236236
lib/util/arc4random.c
237237
lib/util/arc4random_buf.c
238238
lib/util/arc4random_uniform.c
239+
lib/util/b64_decode.c
240+
lib/util/b64_encode.c
239241
lib/util/basename.c
240242
lib/util/cfmakeraw.c
241243
lib/util/chacha_private.h
@@ -297,6 +299,7 @@ lib/util/rcstr.c
297299
lib/util/reallocarray.c
298300
lib/util/realpath.c
299301
lib/util/regex.c
302+
lib/util/regress/base64/base64_test.c
300303
lib/util/regress/closefrom/closefrom_test.c
301304
lib/util/regress/corpus/seed/sudo_conf/sudo.conf.1
302305
lib/util/regress/corpus/seed/sudo_conf/sudo.conf.2
@@ -616,8 +619,6 @@ plugins/sudoers/auth/securid5.c
616619
plugins/sudoers/auth/sia.c
617620
plugins/sudoers/auth/sudo_auth.c
618621
plugins/sudoers/auth/sudo_auth.h
619-
plugins/sudoers/b64_decode.c
620-
plugins/sudoers/b64_encode.c
621622
plugins/sudoers/boottime.c
622623
plugins/sudoers/bsm_audit.c
623624
plugins/sudoers/bsm_audit.h
@@ -890,7 +891,6 @@ plugins/sudoers/regress/harness.in
890891
plugins/sudoers/regress/iolog_plugin/check_iolog_plugin.c
891892
plugins/sudoers/regress/parser/check_addr.c
892893
plugins/sudoers/regress/parser/check_addr.in
893-
plugins/sudoers/regress/parser/check_base64.c
894894
plugins/sudoers/regress/parser/check_digest.c
895895
plugins/sudoers/regress/parser/check_digest.out.ok
896896
plugins/sudoers/regress/parser/check_fill.c

include/sudo_util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ sudo_dso_public int aix_setauthdb_v1(char *user);
178178
sudo_dso_public int aix_setauthdb_v2(char *user, char *registry);
179179
#define aix_setauthdb(_a, _b) aix_setauthdb_v2((_a), (_b))
180180

181+
/* base64.c */
182+
sudo_dso_public size_t sudo_base64_decode_v1(const char * restrict str, unsigned char * restrict dst, size_t dsize);
183+
#define sudo_base64_decode(_a, _b, _c) sudo_base64_decode_v1((_a), (_b), (_c))
184+
sudo_dso_public size_t sudo_base64_encode_v1(const unsigned char * restrict in, size_t in_len, char * restrict out, size_t out_len);
185+
#define sudo_base64_encode(_a, _b, _c, _d) sudo_base64_encode_v1((_a), (_b), (_c), (_d))
186+
181187
/* basename.c */
182188
sudo_dso_public char *sudo_basename_v1(const char *filename);
183189
#define sudo_basename(_a) sudo_basename_v1(_a)

lib/util/Makefile.in

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ PVS_IGNORE = 'V707,V011,V002,V536,V795'
112112
PVS_LOG_OPTS = -a 'GA:1,2' -e -t errorfile -d $(PVS_IGNORE)
113113

114114
# Regression tests
115-
TEST_PROGS = conf_test digest_test getgids getgrouplist_test hexchar_test \
116-
hltq_test json_test multiarch_test open_parent_dir_test \
117-
parse_gids_test parseln_test progname_test regex_test \
118-
strsplit_test strtobool_test strtoid_test strtomode_test \
119-
strtonum_test uuid_test @COMPAT_TEST_PROGS@
115+
TEST_PROGS = base64_test conf_test digest_test getgids getgrouplist_test \
116+
hexchar_test hltq_test json_test multiarch_test \
117+
open_parent_dir_test parse_gids_test parseln_test progname_test \
118+
regex_test strsplit_test strtobool_test strtoid_test \
119+
strtomode_test strtonum_test uuid_test @COMPAT_TEST_PROGS@
120120

121121
TEST_LIBS = @LIBS@
122122
TEST_LDFLAGS = @LDFLAGS@
@@ -145,19 +145,21 @@ DEVEL = @DEVEL@
145145

146146
SHELL = @SHELL@
147147

148-
LTOBJS = basename.lo @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \
149-
gettime.lo getgrouplist.lo gidlist.lo hexchar.lo json.lo lbuf.lo \
150-
locking.lo logfac.lo login_max.lo logpri.lo mkdir_parents.lo \
151-
mmap_alloc.lo multiarch.lo parseln.lo progname.lo rcstr.lo regex.lo \
152-
roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \
153-
strtoid.lo strtomode.lo strtonum.lo sudo_conf.lo sudo_debug.lo \
154-
sudo_dso.lo term.lo ttyname_dev.lo ttysize.lo uuid.lo \
155-
@COMMON_OBJS@ @LTLIBOBJS@
148+
LTOBJS = b64_decode.lo b64_encode.lo basename.lo @DIGEST@ event.lo fatal.lo \
149+
key_val.lo gethostname.lo gettime.lo getgrouplist.lo gidlist.lo \
150+
hexchar.lo json.lo lbuf.lo locking.lo logfac.lo login_max.lo \
151+
logpri.lo mkdir_parents.lo mmap_alloc.lo multiarch.lo parseln.lo \
152+
progname.lo rcstr.lo regex.lo roundup.lo secure_path.lo setgroups.lo \
153+
strsplit.lo strtobool.lo strtoid.lo strtomode.lo strtonum.lo \
154+
sudo_conf.lo sudo_debug.lo sudo_dso.lo term.lo ttyname_dev.lo \
155+
ttysize.lo uuid.lo @COMMON_OBJS@ @LTLIBOBJS@
156156

157157
IOBJS = $(LTOBJS:.lo=.i)
158158

159159
POBJS = $(IOBJS:.i=.plog)
160160

161+
BASE64_TEST_OBJS = base64_test.lo b64_decode.lo b64_encode.lo
162+
161163
MKTEMP_TEST_OBJS = mktemp_test.lo mktemp.lo
162164

163165
PARSELN_TEST_OBJS = parseln_test.lo parseln.lo
@@ -284,6 +286,9 @@ mksiglist.h: sys_siglist.i
284286
mksigname.h: sys_signame.i
285287
$(SED) -e '1,/^int sudo_end_of_headers;/d' -e '/^#/d' < sys_signame.i > mksigname.h
286288

289+
base64_test: $(BASE64_TEST_OBJS) libsudo_util.la
290+
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(BASE64_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
291+
287292
closefrom_test: $(CLOSEFROM_TEST_OBJS) libsudo_util.la
288293
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(CLOSEFROM_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(HARDENING_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
289294

@@ -458,6 +463,7 @@ check: $(TEST_PROGS) check-fuzzer
458463
MALLOC_OPTIONS=S; export MALLOC_OPTIONS; \
459464
MALLOC_CONF="abort:true,junk:true"; export MALLOC_CONF; \
460465
rval=0; \
466+
./base64_test $(TEST_VERBOSE) || rval=`expr $$rval + $$?`; \
461467
if test -f closefrom_test; then \
462468
./closefrom_test $(TEST_VERBOSE) || rval=`expr $$rval + $$?`; \
463469
fi; \
@@ -574,6 +580,40 @@ arc4random_uniform.i: $(srcdir)/arc4random_uniform.c $(incdir)/sudo_compat.h \
574580
$(CPP) $(CPPFLAGS) $(srcdir)/arc4random_uniform.c > $@
575581
arc4random_uniform.plog: arc4random_uniform.i
576582
rm -f $@; pvs-studio --cfg $(PVS_CFG) --source-file $(srcdir)/arc4random_uniform.c --i-file arc4random_uniform.i --output-file $@
583+
b64_decode.lo: $(srcdir)/b64_decode.c $(incdir)/compat/stdbool.h \
584+
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
585+
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
586+
$(top_builddir)/config.h
587+
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/b64_decode.c
588+
b64_decode.i: $(srcdir)/b64_decode.c $(incdir)/compat/stdbool.h \
589+
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
590+
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
591+
$(top_builddir)/config.h
592+
$(CPP) $(CPPFLAGS) $(srcdir)/b64_decode.c > $@
593+
b64_decode.plog: b64_decode.i
594+
rm -f $@; pvs-studio --cfg $(PVS_CFG) --source-file $(srcdir)/b64_decode.c --i-file b64_decode.i --output-file $@
595+
b64_encode.lo: $(srcdir)/b64_encode.c $(incdir)/compat/stdbool.h \
596+
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
597+
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
598+
$(top_builddir)/config.h
599+
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/b64_encode.c
600+
b64_encode.i: $(srcdir)/b64_encode.c $(incdir)/compat/stdbool.h \
601+
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
602+
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
603+
$(top_builddir)/config.h
604+
$(CPP) $(CPPFLAGS) $(srcdir)/b64_encode.c > $@
605+
b64_encode.plog: b64_encode.i
606+
rm -f $@; pvs-studio --cfg $(PVS_CFG) --source-file $(srcdir)/b64_encode.c --i-file b64_encode.i --output-file $@
607+
base64_test.lo: $(srcdir)/regress/base64/base64_test.c \
608+
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
609+
$(incdir)/sudo_util.h $(top_builddir)/config.h
610+
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/regress/base64/base64_test.c
611+
base64_test.i: $(srcdir)/regress/base64/base64_test.c \
612+
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
613+
$(incdir)/sudo_util.h $(top_builddir)/config.h
614+
$(CPP) $(CPPFLAGS) $(srcdir)/regress/base64/base64_test.c > $@
615+
base64_test.plog: base64_test.i
616+
rm -f $@; pvs-studio --cfg $(PVS_CFG) --source-file $(srcdir)/regress/base64/base64_test.c --i-file base64_test.i --output-file $@
577617
basename.lo: $(srcdir)/basename.c $(incdir)/compat/stdbool.h \
578618
$(incdir)/sudo_compat.h $(incdir)/sudo_util.h \
579619
$(top_builddir)/config.h
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#include <config.h>
2020

21-
#include <sudoers.h>
21+
#include <sudo_compat.h>
22+
#include <sudo_debug.h>
23+
#include <sudo_util.h>
2224

2325
/*
2426
* Derived from code with the following declaration:
@@ -49,12 +51,13 @@ static const unsigned char base64dec_tab[256]= {
4951
* result in dst.
5052
*/
5153
size_t
52-
base64_decode(const char * restrict in, unsigned char * restrict out, size_t out_size)
54+
sudo_base64_decode_v1(const char * restrict in, unsigned char * restrict out,
55+
size_t out_size)
5356
{
5457
unsigned char *out_end = out + out_size;
5558
const unsigned char *out0 = out;
5659
unsigned int rem, v;
57-
debug_decl(base64_decode, SUDOERS_DEBUG_MATCH);
60+
debug_decl(sudo_base64_decode, SUDO_DEBUG_UTIL);
5861

5962
for (v = 0, rem = 0; *in != '\0' && *in != '='; in++) {
6063
unsigned char ch = base64dec_tab[(unsigned char)*in];
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818

1919
#include <config.h>
2020

21-
#include <sudoers.h>
21+
#include <sudo_compat.h>
22+
#include <sudo_debug.h>
23+
#include <sudo_util.h>
2224

2325
static const unsigned char base64enc_tab[64] =
2426
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2527

2628
size_t
27-
base64_encode(const unsigned char * restrict in, size_t in_len, char * restrict out, size_t out_len)
29+
sudo_base64_encode_v1(const unsigned char * restrict in, size_t in_len,
30+
char * restrict out, size_t out_len)
2831
{
2932
size_t ii, io;
3033
unsigned int rem, v;
31-
debug_decl(base64_encode, SUDOERS_DEBUG_MATCH);
34+
debug_decl(base64_encode, SUDO_DEBUG_UTIL);
3235

3336
for (io = 0, ii = 0, v = 0, rem = 0; ii < in_len; ii++) {
3437
unsigned char ch = in[ii];

plugins/sudoers/regress/parser/check_base64.c renamed to lib/util/regress/base64/base64_test.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
#include <sudo_compat.h>
2828
#include <sudo_util.h>
2929

30-
/* From parse.h */
31-
extern size_t base64_decode(const char * restrict str, unsigned char * restrict dst, size_t dsize);
32-
extern size_t base64_encode(const unsigned char * restrict in, size_t in_len, char * restrict out, size_t out_len);
33-
3430
sudo_dso_public int main(int argc, char *argv[]);
3531

3632
static unsigned char bstring1[] = { 0xea, 0xb8, 0xa2, 0x71, 0xef, 0x67, 0xc1, 0xcd, 0x0d, 0xd9, 0xa6, 0xaa, 0xa8, 0x24, 0x77, 0x2a, 0xfc, 0x6f, 0x76, 0x37, 0x1b, 0xed, 0x9e, 0x1a, 0x90, 0x5f, 0xcf, 0xbc, 0x00 };
@@ -73,7 +69,7 @@ main(int argc, char *argv[])
7369
unsigned char buf[64];
7470
size_t len;
7571

76-
initprogname(argc > 0 ? argv[0] : "check_base64");
72+
initprogname(argc > 0 ? argv[0] : "base64_test");
7773

7874
while ((ch = getopt(argc, argv, "v")) != -1) {
7975
switch (ch) {
@@ -90,30 +86,30 @@ main(int argc, char *argv[])
9086

9187
for (i = 0; i < ntests; i++) {
9288
/* Test decode. */
93-
len = base64_decode(test_strings[i].encoded, buf, sizeof(buf));
89+
len = sudo_base64_decode(test_strings[i].encoded, buf, sizeof(buf));
9490
if (len == (size_t)-1) {
95-
fprintf(stderr, "check_base64: failed to decode %s\n",
91+
fprintf(stderr, "base64_test: failed to decode %s\n",
9692
test_strings[i].encoded);
9793
errors++;
9894
} else {
9995
buf[len] = '\0';
10096
if (strcmp(test_strings[i].ascii, (char *)buf) != 0) {
101-
fprintf(stderr, "check_base64: expected %s, got %s\n",
97+
fprintf(stderr, "base64_test: expected %s, got %s\n",
10298
test_strings[i].ascii, buf);
10399
errors++;
104100
}
105101
}
106102

107103
/* Test encode. */
108-
len = base64_encode((unsigned char *)test_strings[i].ascii,
104+
len = sudo_base64_encode((unsigned char *)test_strings[i].ascii,
109105
strlen(test_strings[i].ascii), (char *)buf, sizeof(buf));
110106
if (len == (size_t)-1) {
111-
fprintf(stderr, "check_base64: failed to encode %s\n",
107+
fprintf(stderr, "base64_test: failed to encode %s\n",
112108
test_strings[i].ascii);
113109
errors++;
114110
} else {
115111
if (strcmp(test_strings[i].encoded, (char *)buf) != 0) {
116-
fprintf(stderr, "check_base64: expected %s, got %s\n",
112+
fprintf(stderr, "base64_test: expected %s, got %s\n",
117113
test_strings[i].encoded, buf);
118114
errors++;
119115
}

lib/util/util.exp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@COMPAT_EXP@initprogname
22
initprogname2
3+
sudo_base64_decode_v1
4+
sudo_base64_encode_v1
35
sudo_basename_v1
46
sudo_conf_askpass_path_v1
57
sudo_conf_clear_paths_v1

0 commit comments

Comments
 (0)