As mentioned before move the base64 and hex encoding / decoding functions into one file. This is just minor cleanup.
-- :wq Claudio PS: I know this will break regress and I will fix that once this goes in. Index: Makefile =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/Makefile,v retrieving revision 1.19 diff -u -p -r1.19 Makefile --- Makefile 4 Mar 2021 13:01:41 -0000 1.19 +++ Makefile 31 Mar 2021 10:56:42 -0000 @@ -1,9 +1,10 @@ # $OpenBSD: Makefile,v 1.19 2021/03/04 13:01:41 claudio Exp $ PROG= rpki-client -SRCS= as.c cert.c cms.c crl.c gbr.c http.c io.c ip.c log.c main.c mft.c \ - mkdir.c output.c output-bgpd.c output-bird.c output-csv.c \ - output-json.c parser.c roa.c rsync.c tal.c validate.c x509.c +SRCS= as.c cert.c cms.c crl.c encoding.c gbr.c http.c io.c ip.c log.c \ + main.c mft.c mkdir.c output.c output-bgpd.c output-bird.c \ + output-csv.c output-json.c parser.c roa.c rsync.c tal.c validate.c \ + x509.c MAN= rpki-client.8 LDADD+= -ltls -lssl -lcrypto -lutil Index: encoding.c =================================================================== RCS file: encoding.c diff -N encoding.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ encoding.c 31 Mar 2021 11:00:49 -0000 @@ -0,0 +1,88 @@ +/* $OpenBSD$ */ +/* + * Copyright (c) 2020 Claudio Jeker <[email protected]> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include <err.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> + +#include <openssl/evp.h> + +#include "extern.h" + +/* + * Decode base64 encoded string into binary buffer returned in out. + * The out buffer size is stored in outlen. + * Returns 0 on success or -1 for any errors. + */ +int +base64_decode(const unsigned char *in, unsigned char **out, size_t *outlen) +{ + static EVP_ENCODE_CTX *ctx; + unsigned char *to; + size_t inlen; + int tolen; + + if (ctx == NULL && (ctx = EVP_ENCODE_CTX_new()) == NULL) + err(1, "EVP_ENCODE_CTX_new"); + + *out = NULL; + *outlen = 0; + + inlen = strlen(in); + if (inlen >= INT_MAX - 3) + return -1; + tolen = ((inlen + 3) / 4) * 3 + 1; + if ((to = malloc(tolen)) == NULL) + return -1; + + EVP_DecodeInit(ctx); + if (EVP_DecodeUpdate(ctx, to, &tolen, in, inlen) == -1) + goto fail; + *outlen = tolen; + if (EVP_DecodeFinal(ctx, to + tolen, &tolen) == -1) + goto fail; + *outlen += tolen; + *out = to; + return 0; + +fail: + free(to); + return -1; +} + +/* + * Convert binary buffer of size dsz into an upper-case hex-string. + * Returns pointer to the newly allocated string. Function can't fail. + */ +char * +hex_encode(const unsigned char *in, size_t insz) +{ + const char hex[] = "0123456789ABCDEF"; + size_t i; + char *out; + + if ((out = calloc(2, insz + 1)) == NULL) + err(1, NULL); + + for (i = 0; i < insz; i++) { + out[i * 2] = hex[in[i] >> 4]; + out[i * 2 + 1] = hex[in[i] & 0xf]; + } + out[i * 2] = '\0'; + + return out; +} Index: extern.h =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v retrieving revision 1.59 diff -u -p -r1.59 extern.h --- extern.h 29 Mar 2021 12:41:34 -0000 1.59 +++ extern.h 31 Mar 2021 10:55:49 -0000 @@ -419,6 +419,13 @@ void cryptoerrx(const char *, ...) __attribute__((format(printf, 1, 2))) __attribute__((noreturn)); +/* Encoding functions for hex and base64. */ + +int base64_decode(const unsigned char *, unsigned char **, + size_t *); +char *hex_encode(const unsigned char *, size_t); + + /* Functions for moving data between processes. */ void io_socket_blocking(int); Index: tal.c =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/tal.c,v retrieving revision 1.29 diff -u -p -r1.29 tal.c --- tal.c 25 Mar 2021 09:27:38 -0000 1.29 +++ tal.c 31 Mar 2021 11:10:27 -0000 @@ -19,49 +19,12 @@ #include <assert.h> #include <ctype.h> #include <err.h> -#include <limits.h> #include <libgen.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "extern.h" - -static int -base64_decode(const unsigned char *in, unsigned char **out, size_t *outlen) -{ - static EVP_ENCODE_CTX *ctx; - unsigned char *to; - size_t inlen; - int tolen; - - if (ctx == NULL && (ctx = EVP_ENCODE_CTX_new()) == NULL) - err(1, "EVP_ENCODE_CTX_new"); - - *out = NULL; - *outlen = 0; - - inlen = strlen(in); - if (inlen >= INT_MAX - 3) - return -1; - tolen = ((inlen + 3) / 4) * 3 + 1; - if ((to = malloc(tolen)) == NULL) - return -1; - - EVP_DecodeInit(ctx); - if (EVP_DecodeUpdate(ctx, to, &tolen, in, inlen) == -1) - goto fail; - *outlen = tolen; - if (EVP_DecodeFinal(ctx, to + tolen, &tolen) == -1) - goto fail; - *outlen += tolen; - *out = to; - return 0; - -fail: - free(to); - return -1; -} static int tal_cmp(const void *a, const void *b) Index: x509.c =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/x509.c,v retrieving revision 1.20 diff -u -p -r1.20 x509.c --- x509.c 29 Mar 2021 12:41:35 -0000 1.20 +++ x509.c 31 Mar 2021 10:58:39 -0000 @@ -20,7 +20,6 @@ #include <assert.h> #include <err.h> #include <stdarg.h> -#include <stdint.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -28,29 +27,6 @@ #include <openssl/x509v3.h> #include "extern.h" - -/* - * Convert binary buffer of size dsz into an upper-case hex-string. - * Returns pointer to the newly allocated string. Function can't fail. - */ -char * -hex_encode(const unsigned char *in, size_t insz) -{ - const char hex[] = "0123456789ABCDEF"; - size_t i; - char *out; - - if ((out = calloc(2, insz + 1)) == NULL) - err(1, NULL); - - for (i = 0; i < insz; i++) { - out[i * 2] = hex[in[i] >> 4]; - out[i * 2 + 1] = hex[in[i] & 0xf]; - } - out[i * 2] = '\0'; - - return out; -} /* * Parse X509v3 authority key identifier (AKI), RFC 6487 sec. 4.8.3.
