]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
sbuff: Convert hex functions
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 28 Jul 2020 04:53:27 +0000 (00:53 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 28 Jul 2020 17:05:44 +0000 (13:05 -0400)
29 files changed:
src/bin/radsniff.c
src/lib/ldap/util.c
src/lib/server/client.c
src/lib/server/map.c
src/lib/server/password.c
src/lib/server/tmpl.c
src/lib/server/util.c
src/lib/tls/ctx.c
src/lib/tls/session.c
src/lib/unlang/xlat_builtin.c
src/lib/util/ascend.c
src/lib/util/dbuff.h
src/lib/util/hex.c [new file with mode: 0644]
src/lib/util/hex.h [new file with mode: 0644]
src/lib/util/libfreeradius-util.mk
src/lib/util/misc.c
src/lib/util/misc.h
src/lib/util/print.c
src/lib/util/sbuff.c
src/lib/util/sbuff.h
src/lib/util/value.c
src/modules/proto_bfd/proto_bfd.c
src/modules/rlm_digest/rlm_digest.c
src/modules/rlm_eap/types/rlm_eap_mschapv2/rlm_eap_mschapv2.c
src/modules/rlm_mschap/rlm_mschap.c
src/modules/rlm_pap/rlm_pap.c
src/modules/rlm_redis_ippool/rlm_redis_ippool.c
src/modules/rlm_unpack/rlm_unpack.c
src/modules/rlm_wimax/rlm_wimax.c

index e2fa6079b54763fcb500f3852fa02893204c4ffe..a011a925804452f15ed131f9a8447c23b5d3f944 100644 (file)
@@ -28,14 +28,15 @@ RCSID("$Id$")
 
 #include <time.h>
 #include <math.h>
-#include <freeradius-devel/util/base.h>
-#include <freeradius-devel/util/event.h>
 
 #include <freeradius-devel/autoconf.h>
+#include <freeradius-devel/radius/list.h>
+#include <freeradius-devel/util/base.h>
 #include <freeradius-devel/util/conf.h>
+#include <freeradius-devel/util/event.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/pcap.h>
 #include <freeradius-devel/util/timeval.h>
-#include <freeradius-devel/radius/list.h>
 
 #ifdef HAVE_COLLECTDC_H
 #  include <collectd/client.h>
@@ -486,7 +487,8 @@ static void rs_packet_print_fancy(uint64_t count, rs_status_t status, fr_pcap_t
                                fr_pair_list_log(&default_log, packet->vps);
                        }
 
-                       fr_bin2hex(vector, packet->vector, RADIUS_AUTH_VECTOR_LENGTH);
+                       fr_bin2hex(&FR_SBUFF_OUT(vector, sizeof(vector)),
+                                                &FR_DBUFF_TMP(packet->vector, RADIUS_AUTH_VECTOR_LENGTH));
                        INFO("\tAuthenticator-Field = 0x%s", vector);
                }
        }
index 6efad3bb169e470a6e50bba127f8d12660949a54..f65ecb74fbf80e6e5643fcdd0d2bf8bbce7b9a34 100644 (file)
@@ -28,6 +28,7 @@ RCSID("$Id$")
 USES_APPLE_DEPRECATED_API
 
 #include <freeradius-devel/ldap/base.h>
+#include <freeradius-devel/util/hex.h>
 
 #include <stdarg.h>
 #include <ctype.h>
@@ -215,7 +216,7 @@ bool fr_ldap_util_is_dn(char const *in, size_t inlen)
                        /*
                         *      Hex encoding, consume three chars
                         */
-                       if (fr_hex2bin((uint8_t *) &c, 1, p + 1, 2) == 1) {
+                       if (fr_hex2bin(&FR_DBUFF_TMP((uint8_t *) &c, 1), &FR_SBUFF_IN(p + 1, 2)) == 1) {
                                inlen -= 2;
                                p += 2;
                                continue;
@@ -416,7 +417,7 @@ size_t fr_ldap_util_normalise_dn(char *out, char const *in)
                         *      special encoding, get rewritten to the
                         *      special encoding.
                         */
-                       if (fr_hex2bin((uint8_t *) &c, 1, p + 1, 2) == 1) {
+                       if (fr_hex2bin(&FR_DBUFF_TMP((uint8_t *) &c, 1), &FR_SBUFF_IN(p + 1, 2)) == 1) {
                                switch (c) {
                                case ' ':
                                case '#':
index 58f80a2b7801da0fa4de4fe0c7885f6d73bd6903..6ab9855e4fb760b3bed794652aa53ced53c1985c 100644 (file)
  */
 RCSID("$Id$")
 
+#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
+#include <freeradius-devel/server/cf_file.h>
 #include <freeradius-devel/server/cf_parse.h>
 #include <freeradius-devel/server/client.h>
 #include <freeradius-devel/server/module.h>
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/server/virtual_servers.h>
-#include <freeradius-devel/server/cf_file.h>
-#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/trie.h>
 
@@ -740,7 +741,7 @@ RADCLIENT *client_afrom_cs(TALLOC_CTX *ctx, CONF_SECTION *cs, CONF_SECTION *serv
                        hex_len = talloc_array_length(value) - 3;
                        bin_len = (hex_len / 2) + 1;
                        MEM(bin = talloc_array(c, uint8_t, bin_len));
-                       converted = fr_hex2bin(bin, bin_len, value + 2, hex_len);
+                       converted = fr_hex2bin(&FR_DBUFF_TMP(bin, bin_len), &FR_SBUFF_IN(value + 2, hex_len));
                        if (converted < (bin_len - 1)) {
                                cf_log_err(cs, "Invalide hex string in shared secret");
                                goto error;
index 707e2eb07b4339bb3d3a0b112be7e31546edaa29..9962633dbd9412f9e9d1174716b8118db115ff84 100644 (file)
@@ -31,8 +31,9 @@ RCSID("$Id$")
 #include <freeradius-devel/server/exec.h>
 #include <freeradius-devel/server/map.h>
 #include <freeradius-devel/server/paircmp.h>
-#include <freeradius-devel/util/debug.h>
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/pair_cursor.h>
 #include <freeradius-devel/util/pair_legacy.h>
@@ -112,7 +113,7 @@ bool map_cast_from_hex(vp_map_t *map, fr_token_t rhs_type, char const *rhs)
        ptr = talloc_array(map, uint8_t, len >> 1);
        if (!ptr) return false;
 
-       fr_hex2bin(ptr, len >> 1, rhs + 2, len);
+       fr_hex2bin(&FR_DBUFF_TMP(ptr, len >> 1), &FR_SBUFF_IN(rhs + 2, len));
 
        /*
         *      Convert to da->type (if possible);
index 1aa59ac2707687b55b39bfa7bcc1390ed9159c5a..22f1284c7e1983d81903197303a9359c05def51c 100644 (file)
@@ -26,6 +26,7 @@ RCSID("$Id$")
 #include <freeradius-devel/server/password.h>
 
 #include <freeradius-devel/util/base64.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md4.h>
 #include <freeradius-devel/util/md5.h>
 #include <freeradius-devel/util/misc.h>
@@ -417,7 +418,7 @@ static ssize_t normify(normalise_t *action, uint8_t *buffer, size_t bufflen,
        if (!(len & 0x01) && len >= (2 * min_len)) {
                size_t  decoded;
 
-               decoded = fr_hex2bin(buffer, bufflen, known_good, len);
+               decoded = fr_hex2bin(&FR_DBUFF_TMP(buffer, bufflen), &FR_SBUFF_IN(known_good, len));
                if (decoded == (len >> 1)) {
                        if (action) *action = NORMALISED_HEX;
                        return decoded;
index 9938a2433b3fa47d88e16d309fe14f2d293224b7..bb8acecb02976b79b690bbee654d20659d05c1a4 100644 (file)
@@ -30,8 +30,9 @@ RCSID("$Id$")
 
 #include <freeradius-devel/server/tmpl.h>
 #include <freeradius-devel/server/base.h>
-#include <freeradius-devel/util/debug.h>
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 
 #include <ctype.h>
@@ -1732,7 +1733,7 @@ ssize_t tmpl_afrom_str(TALLOC_CTX *ctx, vp_tmpl_t **out,
 
                        vpt = tmpl_alloc(ctx, TMPL_TYPE_DATA, in, inlen, type);
                        (void)fr_value_box_mem_alloc(vpt, &bin, &vpt->data.literal, NULL, binlen, false);
-                       len = fr_hex2bin(bin, binlen, in + 2, inlen - 2);
+                       len = fr_hex2bin(&FR_DBUFF_TMP(bin, binlen), &FR_SBUFF_IN(in + 2, inlen - 2));
                        if (len != binlen) {
                                fr_strerror_printf("Hex string contains non-hex char");
                                talloc_free(vpt);
index 12614165d0d079bee0e7fc3c6990935ecf551c48..0e664be4302cb623802056f8d67b3d4f933f96ec 100644 (file)
 
 RCSID("$Id$")
 
+
 #include <freeradius-devel/server/base.h>
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/server/stats.h>
 #include <freeradius-devel/server/util.h>
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 
 #include <ctype.h>
@@ -250,7 +252,8 @@ size_t rad_filename_escape(UNUSED REQUEST *request, char *out, size_t outlen, ch
                 *      Unsafe chars
                 */
                *out++ = '-';
-               fr_bin2hex(out, (uint8_t const *)in++, 1);
+               in++;
+               fr_bin2hex(&FR_SBUFF_OUT(out, freespace), &FR_DBUFF_TMP((uint8_t const *)in, 1));
                out += 2;
                freespace -= 3;
        }
@@ -308,7 +311,7 @@ ssize_t rad_filename_unescape(char *out, size_t outlen, char const *in, size_t i
                        /*
                         *      If hex2bin returns 0 the next two chars weren't hexits.
                         */
-                       if (fr_hex2bin((uint8_t *) out, 1, in, 1) == 0) return in - (p + 1);
+                       if (fr_hex2bin(&FR_DBUFF_TMP((uint8_t *) out, 1), &FR_SBUFF_IN(in, 1)) == 0) return in - (p + 1);
                        in += 2;
                        out++;
                        freespace--;
index 1296dd52b3661baaeaabb6e4f910eedf8aa5a036..9ee80745d9c0706ffe1a394d0616f4a68ee9a069 100644 (file)
@@ -30,9 +30,10 @@ USES_APPLE_DEPRECATED_API    /* OpenSSL API has been deprecated by Apple */
 #ifdef WITH_TLS
 #define LOG_PREFIX "tls - "
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/syserror.h>
-#include <freeradius-devel/util/debug.h>
 
 #include <openssl/rand.h>
 #include <openssl/dh.h>
@@ -394,7 +395,7 @@ SSL_CTX *fr_tls_ctx_alloc(fr_tls_conf_t const *conf, bool client)
                 *      Check the password now, so that we don't have
                 *      errors at run-time.
                 */
-               hex_len = fr_hex2bin(buffer, sizeof(buffer), conf->psk_password, psk_len);
+               hex_len = fr_hex2bin(&FR_DBUFF_TMP(buffer, sizeof(buffer)), &FR_SBUFF_IN(conf->psk_password, psk_len));
                if (psk_len != (2 * hex_len)) {
                        ERROR("psk_hexphrase is not all hex");
                        goto error;
@@ -748,7 +749,7 @@ post_ca:
                        goto error;
                }
                X509_STORE_set_flags(cert_vpstore, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
-#ifdef X509_V_FLAG_USE_DELTAS 
+#ifdef X509_V_FLAG_USE_DELTAS
                /*
                 *      If set, delta CRLs (if present) are used to
                 *      determine certificate status. If not set
index 6d14824e9c161ab32dcc09a9f57ee8dfda510e53..815a9d8f0a4efcc7fac8cea7099b84a27aa5969d 100644 (file)
 #ifdef HAVE_OPENSSL_OCSP_H
 #define LOG_PREFIX "tls - "
 
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/server/pair.h>
 
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/pair_legacy.h>
 
@@ -391,7 +392,8 @@ unsigned int fr_tls_session_psk_client_cb(SSL *ssl, UNUSED char const *hint,
 
        strlcpy(identity, conf->psk_identity, max_identity_len);
 
-       return fr_hex2bin(psk, max_psk_len, conf->psk_password, psk_len);
+       return fr_hex2bin(&FR_DBUFF_TMP((uint8_t *)psk, (size_t)max_psk_len),
+                                       &FR_SBUFF_IN(conf->psk_password, (size_t)psk_len));
 }
 
 /** Determine the PSK to use for an incoming connection
@@ -405,9 +407,9 @@ unsigned int fr_tls_session_psk_client_cb(SSL *ssl, UNUSED char const *hint,
  *     - >0 if a PSK matching identity was found (the length of bytes written to psk).
  */
 unsigned int fr_tls_session_psk_server_cb(SSL *ssl, const char *identity,
-                                      unsigned char *psk, unsigned int max_psk_len)
+                                         unsigned char *psk, unsigned int max_psk_len)
 {
-       unsigned int    psk_len = 0;
+       size_t          psk_len = 0;
        fr_tls_conf_t   *conf;
        REQUEST         *request;
 
@@ -456,7 +458,7 @@ unsigned int fr_tls_session_psk_server_cb(SSL *ssl, const char *identity,
                 *      convert the expansion from printable string
                 *      back to hex.
                 */
-               return fr_hex2bin(psk, max_psk_len, buffer, hex_len);
+               return fr_hex2bin(&FR_DBUFF_TMP((uint8_t *)psk, (size_t)max_psk_len), &FR_SBUFF_IN(buffer, hex_len));
        }
 
        if (!conf->psk_identity) {
@@ -477,7 +479,7 @@ unsigned int fr_tls_session_psk_server_cb(SSL *ssl, const char *identity,
        psk_len = strlen(conf->psk_password);
        if (psk_len > (2 * max_psk_len)) return 0;
 
-       return fr_hex2bin(psk, max_psk_len, conf->psk_password, psk_len);
+       return fr_hex2bin(&FR_DBUFF_TMP((uint8_t *)psk, (size_t)max_psk_len), &FR_SBUFF_IN(conf->psk_password, psk_len));
 }
 #endif /* PSK_MAX_IDENTITY_LEN */
 
index aaab34a2c7f5dcad22548fc9afb3ed885979d270..cce0b3ed12996a95616974f078eb6235b153f144 100644 (file)
@@ -32,11 +32,12 @@ RCSID("$Id$")
 
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/cond.h>
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/server/regex.h>
 #include <freeradius-devel/unlang/xlat_priv.h>
 
 #include <freeradius-devel/util/base64.h>
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md5.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/rand.h>
@@ -1741,7 +1742,7 @@ static xlat_action_t xlat_func_bin(TALLOC_CTX *ctx, fr_cursor_t *out,
 
        MEM(result = fr_value_box_alloc_null(ctx));
        MEM(fr_value_box_mem_alloc(result, &bin, result, NULL, outlen, fr_value_box_list_tainted(*in)) == 0);
-       fr_hex2bin(bin, outlen, p, end - p);
+       fr_hex2bin(&FR_DBUFF_TMP(bin, outlen), &FR_SBUFF_IN(p, end - p));
 
        fr_cursor_append(out, result);
 
@@ -1841,7 +1842,7 @@ static xlat_action_t xlat_func_hex(TALLOC_CTX *ctx, fr_cursor_t *out,
        MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
        vb->vb_length = ((*in)->vb_length * 2);
        vb->vb_strvalue = p = talloc_zero_array(vb, char, vb->vb_length + 1);
-       fr_bin2hex(p, (*in)->vb_octets, (*in)->vb_length);
+       fr_bin2hex(&FR_SBUFF_OUT(p, talloc_array_length(p)), &FR_DBUFF_TMP((*in)->vb_octets, (*in)->vb_length));
 
        fr_cursor_append(out, vb);
 
index 8ba72f4404dacd5f508e8c2e6bb28d042308df36..791f46b2d3f8e3a763dc561df4133a3285f1b9a3 100644 (file)
@@ -25,6 +25,7 @@ RCSID("$Id$")
 #ifdef WITH_ASCEND_BINARY
 #include "ascend.h"
 
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/talloc.h>
 
@@ -395,7 +396,7 @@ static int ascend_parse_ipx_net(int argc, char **argv,
        /*
         *      Node must be 6 octets long.
         */
-       token = fr_hex2bin(net->node, IPX_NODE_ADDR_LEN, p, strlen(p));
+       token = fr_hex2bin(&FR_DBUFF_TMP(net->node, IPX_NODE_ADDR_LEN), &FR_SBUFF_IN(p, strlen(p)));
        if (token != IPX_NODE_ADDR_LEN) return -1;
 
        /*
@@ -893,10 +894,10 @@ static int ascend_parse_generic(int argc, char **argv,
        filter->offset = rcode;
        filter->offset = htons(filter->offset);
 
-       rcode = fr_hex2bin(filter->mask, sizeof(filter->mask), argv[1], strlen(argv[1]));
+       rcode = fr_hex2bin(&FR_DBUFF_TMP(filter->mask, sizeof(filter->mask)), &FR_SBUFF_IN(argv[1], strlen(argv[1])));
        if (rcode != sizeof(filter->mask)) return -1;
 
-       token = fr_hex2bin(filter->value, sizeof(filter->value), argv[2], strlen(argv[2]));
+       token = fr_hex2bin(&FR_DBUFF_TMP(filter->value, sizeof(filter->value)), &FR_SBUFF_IN(argv[2], strlen(argv[2])));
        if (token != sizeof(filter->value)) return -1;
 
        filter->len = rcode;
index 8a2268ded284dc0116584799743e9666de4a8cf2..f2e385e0e4c82ba0cf59f9e5f6c75e22d989326b 100644 (file)
@@ -209,6 +209,9 @@ _fr_dbuff_init(_out, \
               (uint8_t const *)(_start), \
               _Generic((_len_or_end), \
                        size_t          : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                       long            : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                       int             : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                       unsigned int    : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
                        uint8_t *       : (uint8_t const *)(_len_or_end), \
                        uint8_t const * : (uint8_t const *)(_len_or_end), \
                        char *          : (uint8_t const *)(_len_or_end), \
@@ -236,6 +239,9 @@ _fr_dbuff_init(_out, \
        .start_i        = (uint8_t const *)(_start), \
        .end_i          = _Generic((_len_or_end), \
                                size_t          : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                               long            : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                               int             : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
+                               unsigned int    : (uint8_t const *)(_start) + (size_t)(_len_or_end), \
                                uint8_t *       : (uint8_t const *)(_len_or_end), \
                                uint8_t const * : (uint8_t const *)(_len_or_end), \
                                char *          : (uint8_t const *)(_len_or_end), \
diff --git a/src/lib/util/hex.c b/src/lib/util/hex.c
new file mode 100644 (file)
index 0000000..2ad75cd
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ *   This library is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU Lesser General Public
+ *   License as published by the Free Software Foundation; either
+ *   version 2.1 of the License, or (at your option) any later version.
+ *
+ *   This library 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
+ *   Lesser General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Lesser General Public
+ *   License along with this library; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/** A generic string buffer structure for string printing and parsing
+ *
+ * @file src/lib/util/sbuff.c
+ *
+ * @copyright 2020 Arran Cudbard-Bell \<a.cudbardb@freeradius.org\>
+ */
+RCSID("$Id$")
+
+#include <freeradius-devel/util/hex.h>
+
+static char const hextab[] = "0123456789abcdef";
+
+/** Convert hex strings to binary data
+ *
+ * @param[out] out     Output buffer to write to.
+ * @param[in] in       Input string.
+ * @return
+ *     - >=0 the number of bytes written to out.
+ *     - <0 number of bytes we would have needed to copy the next hexit.
+ */
+ssize_t fr_hex2bin(fr_dbuff_t *out, fr_sbuff_t *in)
+{
+       size_t total = 0;
+
+       while (!FR_SBUFF_CANT_EXTEND_LOWAT(in, 2)) {
+               char *c1, *c2;
+
+               if(!(c1 = memchr(hextab, tolower((int) *in->p), sizeof(hextab))) ||
+                  !(c2 = memchr(hextab, tolower((int) *(in->p + 1)), sizeof(hextab)))) break;
+
+               FR_DBUFF_BYTES_IN_RETURN(out, ((c1 - hextab) << 4) + (c2 - hextab));
+
+               fr_sbuff_advance(in, 2);
+               total++;
+       };
+
+       return total;
+}
+
+/** Convert binary data to a hex string
+ *
+ * Ascii encoded hex string will not be prefixed with '0x'
+ *
+ * @warning If the output buffer isn't long enough, we have a buffer overflow.
+ *
+ * @param[out] out     Output buffer to write to.
+ * @param[in] in       input.
+ * @return
+ *     - >=0 the number of bytes written to out.
+ *     - <0 number of bytes we would have needed to print the next hexit.
+ */
+ssize_t fr_bin2hex(fr_sbuff_t *out, fr_dbuff_t *in)
+{
+       size_t  total = 0;
+
+       while (fr_dbuff_remaining(in) > 0) {    /* Fixme to be extension check */
+               FR_SBUFF_IN_CHAR_RETURN(out, hextab[((*in->p) >> 4) & 0x0f], hextab[*in->p & 0x0f]);
+
+               fr_dbuff_advance(in, 1);
+               total += 2;
+       };
+       return total;
+}
+
+/** Convert binary data to a hex string
+ *
+ * Ascii encoded hex string will not be prefixed with '0x'
+ *
+ * @param[in] ctx to alloc buffer in.
+ * @param[in] bin input.
+ * @param[in] inlen of bin input.
+ * @return length of data written to buffer.
+ */
+char *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen)
+{
+       char *buff;
+
+       buff = talloc_array(ctx, char, (inlen << 2));
+       if (!buff) return NULL;
+
+       fr_bin2hex(&FR_SBUFF_OUT(buff, (inlen * 2) + 1), &FR_DBUFF_TMP(bin, inlen));
+
+       return buff;
+}
diff --git a/src/lib/util/hex.h b/src/lib/util/hex.h
new file mode 100644 (file)
index 0000000..80e84e7
--- /dev/null
@@ -0,0 +1,43 @@
+#pragma once
+/*
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program 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 General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/** A generic buffer structure for string printing and parsing strings
+ *
+ * Because doing manual length checks is error prone and a waste of everyones time.
+ *
+ * @file src/lib/util/sbuff.h
+ *
+ * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
+ */
+RCSIDH(hex_h, "$Id$")
+
+#  ifdef __cplusplus
+extern "C" {
+#  endif
+#include <sys/types.h>
+#include <freeradius-devel/util/sbuff.h>
+#include <freeradius-devel/util/dbuff.h>
+
+ssize_t                fr_hex2bin(fr_dbuff_t *out, fr_sbuff_t *in);
+
+ssize_t                fr_bin2hex(fr_sbuff_t *out, fr_dbuff_t *in);
+
+char           *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen);
+
+#ifdef __cplusplus
+}
+#endif
index 94c3933eef919b4cbd978cbb2b53349159075898..3fb7e4746d66f459536938cb711e765a33a66ded 100644 (file)
@@ -26,6 +26,7 @@ SOURCES               := \
                   getaddrinfo.c \
                   hash.c \
                   heap.c \
+                  hex.c \
                   hmac_md5.c \
                   hmac_sha1.c \
                   hw.c \
index e3b554b5354b1d59eb7f75d9c3b5147ef59f0bc6..d2223e3de4c5c2fdc00390278bb3135a24874a3a 100644 (file)
  */
 RCSID("$Id$")
 
+#include <freeradius-devel/util/dbuff.h>
 #include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/util/misc.h>
+#include <freeradius-devel/util/sbuff.h>
 #include <freeradius-devel/util/strerror.h>
 #include <freeradius-devel/util/syserror.h>
 #include <freeradius-devel/util/talloc.h>
@@ -158,86 +160,6 @@ int rad_unlockfd(int fd, int lock_len)
        return rad_lock(fd, lock_len, F_SETLK, F_UNLCK);
 }
 
-static char const hextab[] = "0123456789abcdef";
-
-/** Convert hex strings to binary data
- *
- * @param bin Buffer to write output to.
- * @param outlen length of output buffer (or length of input string / 2).
- * @param hex input string.
- * @param inlen length of the input string
- * @return length of data written to buffer.
- */
-size_t fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen)
-{
-       size_t i;
-       size_t len;
-       char *c1, *c2;
-
-       /*
-        *      Smartly truncate output, caller should check number of bytes
-        *      written.
-        */
-       len = inlen >> 1;
-       if (len > outlen) len = outlen;
-
-       for (i = 0; i < len; i++) {
-               if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), sizeof(hextab))) ||
-                  !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), sizeof(hextab))))
-                       break;
-               bin[i] = ((c1-hextab)<<4) + (c2-hextab);
-       }
-
-       return i;
-}
-
-/** Convert binary data to a hex string
- *
- * Ascii encoded hex string will not be prefixed with '0x'
- *
- * @warning If the output buffer isn't long enough, we have a buffer overflow.
- *
- * @param[out] hex Buffer to write hex output.
- * @param[in] bin input.
- * @param[in] inlen of bin input.
- * @return length of data written to buffer.
- */
-size_t fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen)
-{
-       size_t i;
-
-       for (i = 0; i < inlen; i++) {
-               hex[0] = hextab[((*bin) >> 4) & 0x0f];
-               hex[1] = hextab[*bin & 0x0f];
-               hex += 2;
-               bin++;
-       }
-
-       *hex = '\0';
-       return inlen * 2;
-}
-
-/** Convert binary data to a hex string
- *
- * Ascii encoded hex string will not be prefixed with '0x'
- *
- * @param[in] ctx to alloc buffer in.
- * @param[in] bin input.
- * @param[in] inlen of bin input.
- * @return length of data written to buffer.
- */
-char *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen)
-{
-       char *buff;
-
-       buff = talloc_array(ctx, char, (inlen << 2));
-       if (!buff) return NULL;
-
-       fr_bin2hex(buff, bin, inlen);
-
-       return buff;
-}
-
 /** Consume the integer (or hex) portion of a value string
  *
  * Allows integer or hex representations of integers (but not octal,
index 42eb10aeee6bbd0a1ca5d29310da29a3a39a163d..e88edebde282ca3a4cb1d10466d1997078b5bcf5 100644 (file)
@@ -191,9 +191,6 @@ int         fr_unset_signal(int sig);
 int            rad_lockfd(int fd, int lock_len);
 int            rad_lockfd_nonblock(int fd, int lock_len);
 int            rad_unlockfd(int fd, int lock_len);
-char           *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen);
-size_t         fr_bin2hex(char * restrict hex, uint8_t const * restrict bin, size_t inlen);
-size_t         fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen);
 int            fr_strtoull(uint64_t *out, char **end, char const *value);
 int            fr_strtoll(int64_t *out, char **end, char const *value);
 char           *fr_trim(char const *str, size_t size);
index 285e48a974ea7ae364696e5490dc24b205b6edf4..2bf5c21dc9e822a79d7841e94cd65bc67bb016b1 100644 (file)
@@ -26,6 +26,7 @@ RCSID("$Id$")
 #include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/pair.h>
 #include <freeradius-devel/util/print.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/strerror.h>
 #include <freeradius-devel/util/talloc.h>
 
@@ -741,13 +742,13 @@ char *fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
                                case FR_TYPE_OCTETS:
                                        subst = talloc_array(NULL, char, (in->vb_length * 2) + 1);
                                        if (!subst) goto oom;
-                                       fr_bin2hex(subst, in->vb_octets, in->vb_length);
+                                       fr_bin2hex(&FR_SBUFF_OUT(subst, talloc_array_length(subst)), &FR_DBUFF_TMP(in->vb_octets, in->vb_length));
                                        break;
 
                                case FR_TYPE_STRING:
                                        subst = talloc_array(NULL, char, (in->vb_length * 2) + 1);
                                        if (!subst) goto oom;
-                                       fr_bin2hex(subst, (uint8_t const *)in->vb_strvalue, in->vb_length);
+                                       fr_bin2hex(&FR_SBUFF_OUT(subst, talloc_array_length(subst)), &FR_DBUFF_TMP((uint8_t const *)in->vb_strvalue, in->vb_length));
                                        break;
 
                                default:
@@ -764,7 +765,7 @@ char *fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
 
                                        subst = talloc_array(NULL, char, (dst.vb_length * 2) + 1);
                                        if (!subst) goto oom;
-                                       fr_bin2hex(subst, dst.vb_octets, dst.vb_length);
+                                       fr_bin2hex(&FR_SBUFF_OUT(subst, talloc_array_length(subst)), &FR_DBUFF_TMP(dst.vb_octets, dst.vb_length));
 
                                        fr_value_box_clear(&dst);
                                        break;
index f56659ca4bba3c9172267db5998fa1b579b6c141..9c9542b16a25f1380ee209a9c02a953e6a30edf5 100644 (file)
@@ -847,28 +847,6 @@ size_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t
 SBUFF_PARSE_FLOAT_DEF(float32, float, strtof, 100);
 SBUFF_PARSE_FLOAT_DEF(float64, double, strtod, 100);
 
-/** Copy char into the sbuff
- *
- * @param[in] sbuff    to copy into.
- * @param[in] c        to copy into buffer.
- * @return
- *     - >= 0 the number of bytes copied into the sbuff.
- *     - <0 the number of bytes required to complete the copy operation.
- */
-ssize_t fr_sbuff_in_char(fr_sbuff_t *sbuff, char c)
-{
-       CHECK_SBUFF_INIT(sbuff);
-
-       if (unlikely(sbuff->is_const)) return 0;
-
-       FR_SBUFF_EXTEND_OR_RETURN(sbuff, 1);
-
-       *sbuff->p = c;
-       *(sbuff->p + 1) = '\0';
-
-       return fr_sbuff_advance(sbuff, 1);
-}
-
 /** Copy bytes into the sbuff up to the first \0
  *
  * @param[in] sbuff    to copy into.
index 7da48c8881586cc7bba59d5fb918e9c8a586f424..3df44668d4493e027fe908867ddddd7711cbd54f 100644 (file)
@@ -183,6 +183,7 @@ do { \
                                size_t          : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
                                long            : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
                                int             : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
+                               unsigned int    : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
                                char *          : (char const *)(_len_or_end), \
                                char const *    : (char const *)(_len_or_end) \
                        ), \
@@ -209,6 +210,7 @@ do { \
                                size_t          : (char const *)(_start) + (size_t)(_len_or_end), \
                                long            : (char const *)(_start) + (size_t)(_len_or_end), \
                                int             : (char const *)(_start) + (size_t)(_len_or_end), \
+                               unsigned int    : (char const *)(_start) + (size_t)(_len_or_end), \
                                char *          : (char const *)(_len_or_end), \
                                char const *    : (char const *)(_len_or_end) \
                        ), \
@@ -709,8 +711,8 @@ static inline void fr_sbuff_set_to_marker(fr_sbuff_marker_t *m)
  *
  * @{
  */
-ssize_t        fr_sbuff_in_char(fr_sbuff_t *sbuff, char c);
-#define        FR_SBUFF_IN_CHAR_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_char, ##__VA_ARGS__)
+#define        fr_sbuff_in_char(_sbuff, ...) fr_sbuff_in_bstrncpy(_sbuff, ((char []){ __VA_ARGS__ }), sizeof((char []){ __VA_ARGS__ }))
+#define        FR_SBUFF_IN_CHAR_RETURN(_sbuff, ...) FR_SBUFF_RETURN(fr_sbuff_in_bstrncpy, _sbuff, ((char []){ __VA_ARGS__ }), sizeof((char []){ __VA_ARGS__ }))
 
 ssize_t        fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str);
 #define        FR_SBUFF_IN_STRCPY_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_strcpy, ##__VA_ARGS__)
index 2b21c486e32ea4659fd3065815524711a83444d9..4fef355b4ebbb0594a9b4fd05cc4f6a389e8fda4 100644 (file)
@@ -51,10 +51,10 @@ RCSID("$Id$")
 #include <freeradius-devel/util/ascend.h>
 #include <freeradius-devel/util/cursor.h>
 #include <freeradius-devel/util/dbuff.h>
-#include <freeradius-devel/util/misc.h>
+#include <freeradius-devel/util/hex.h>
+#include <freeradius-devel/util/net.h>
 #include <freeradius-devel/util/strerror.h>
 #include <freeradius-devel/util/talloc.h>
-#include <freeradius-devel/util/net.h>
 
 #include <assert.h>
 #include <ctype.h>
@@ -4432,7 +4432,7 @@ parse:
 
                ret = len >> 1;
                p = talloc_array(ctx, uint8_t, ret);
-               if (fr_hex2bin(p, ret, in + 2, len) != (size_t)ret) {
+               if (fr_hex2bin(&FR_DBUFF_TMP(p, ret), &FR_SBUFF_IN(in + 2, len)) != (ssize_t)ret) {
                        talloc_free(p);
                        fr_strerror_printf("Invalid hex data");
                        return -1;
@@ -4452,7 +4452,7 @@ parse:
                                return -1;
                        }
 
-                       bin = fr_hex2bin((uint8_t *) &dst->datum.filter, ret, in + 2, len - 2);
+                       bin = fr_hex2bin(&FR_DBUFF_TMP((uint8_t *) &dst->datum.filter, (len - 2) / 2), &FR_SBUFF_IN(in + 2, len - 2));
                        if (bin < ret) {
                                memset(((uint8_t *) &dst->datum.filter) + bin, 0, ret - bin);
                        }
@@ -4785,8 +4785,8 @@ char *fr_value_box_asprint(TALLOC_CTX *ctx, fr_value_box_t const *data, char quo
                p[0] = '0';
                p[1] = 'x';
 
-               if (data->vb_octets) {
-                       fr_bin2hex(p + 2, data->vb_octets, data->datum.length);
+               if (data->vb_octets && data->datum.length) {
+                       fr_bin2hex(&FR_SBUFF_OUT(p + 2, (data->datum.length * 2) + 1), &FR_DBUFF_TMP(data->vb_octets, data->datum.length));
                        p[2 + (data->datum.length * 2)] = '\0';
                } else {
                        p[2] = '\0';
@@ -5535,10 +5535,11 @@ size_t fr_value_box_snprint(char *out, size_t outlen, fr_value_box_t const *data
                }
 
                /* Get maximum number of uint8s we can encode given (end - p) */
-               if (data->vb_octets) {
+               if (data->vb_octets && data->datum.length) {
                        max = (((end - p) % 2) ? (end - p) - 1 : (end - p) - 2) / 2;
-                       fr_bin2hex(p, data->vb_octets,
-                                  ((size_t)data->datum.length > max) ? max : (size_t)data->datum.length);
+                       fr_bin2hex(&FR_SBUFF_OUT(p, end),
+                                  &FR_DBUFF_TMP(data->vb_octets,
+                                                (size_t)data->datum.length > max ? max : (size_t)data->datum.length));
                } else {
                        *p = '\0';
                }
index e631e2a4126271c69a158d2bb5ad77a3ace397a5..557c4a84f17e61842e3d85ded0f7b8dffb6c4f49 100644 (file)
@@ -28,8 +28,8 @@
 #include <freeradius-devel/unlang/base.h>
 
 #include <freeradius-devel/util/event.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md5.h>
-#include <freeradius-devel/util/misc.h>
 #include <freeradius-devel/util/rand.h>
 #include <freeradius-devel/util/sha1.h>
 #include <freeradius-devel/util/socket.h>
@@ -456,7 +456,7 @@ static ssize_t bfd_parse_secret(CONF_SECTION *cs, uint8_t secret[BFD_MAX_SECRET_
                        return -1;
                }
 
-               return fr_hex2bin(secret, BFD_MAX_SECRET_LENGTH, value + 2, (len - 2));
+               return fr_hex2bin(&FR_DBUFF_TMP(secret, BFD_MAX_SECRET_LENGTH), &FR_SBUFF_IN(value + 2, (len - 2)));
        }
 
        if (len >= 20) {
index c9d17376b49ee46147a13a1525d320fe26ef988b..a6d4d916ec693f255b389ead934a0835abaedf26 100644 (file)
@@ -26,6 +26,8 @@ RCSID("$Id$")
 
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/module.h>
+
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md5.h>
 
 typedef struct {
@@ -203,7 +205,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED module_ctx_t const *
                 *      Set A1 to Digest-HA1 if no User-Password found
                 */
                if (passwd->da == attr_digest_ha1) {
-                       if (fr_hex2bin(&a1[0], sizeof(a1), passwd->vp_strvalue, passwd->vp_length) != 16) {
+                       if (fr_hex2bin(&FR_DBUFF_TMP(&a1[0], sizeof(a1)), &FR_SBUFF_IN(passwd->vp_strvalue, passwd->vp_length)) != 16) {
                                RDEBUG2("Invalid text in Digest-HA1");
                                return RLM_MODULE_INVALID;
                        }
@@ -218,7 +220,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED module_ctx_t const *
                 */
                if (passwd->da == attr_cleartext_password) {
                        fr_md5_calc(hash, &a1[0], a1_len);
-                       fr_bin2hex((char *) &a1[0], hash, 16);
+                       fr_bin2hex(&FR_SBUFF_OUT((char *) &a1[0], 32 + 1), &FR_DBUFF_TMP(hash, 16));
                } else {        /* MUST be Digest-HA1 */
                        memcpy(&a1[0], passwd->vp_strvalue, 32);
                }
@@ -339,7 +341,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED module_ctx_t const *
        } else {
                memcpy(&hash[0], &a1[0], a1_len);
        }
-       fr_bin2hex((char *) kd, hash, sizeof(hash));
+       fr_bin2hex(&FR_SBUFF_OUT((char *) kd, (sizeof(hash) * 2) + 1), &FR_DBUFF_TMP(hash, sizeof(hash)));
 
        RHEXDUMP_INLINE3(hash, sizeof(hash), "H(A1)");
 
@@ -401,7 +403,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED module_ctx_t const *
 
        fr_md5_calc(&hash[0], &a2[0], a2_len);
 
-       fr_bin2hex((char *) kd + kd_len, hash, sizeof(hash));
+       fr_bin2hex(&FR_SBUFF_OUT((char *) kd + kd_len, (sizeof(hash) * 2) + 1), &FR_DBUFF_TMP(hash, sizeof(hash)));
 
        RHEXDUMP_INLINE3(hash, sizeof(hash), "H(A2)");
 
@@ -426,7 +428,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(UNUSED module_ctx_t const *
                return RLM_MODULE_INVALID;
        }
 
-       if (fr_hex2bin(&hash[0], sizeof(hash), vp->vp_strvalue, vp->vp_length) != (vp->vp_length >> 1)) {
+       if (fr_hex2bin(&FR_DBUFF_TMP(&hash[0], sizeof(hash)), &FR_SBUFF_IN(vp->vp_strvalue, vp->vp_length)) != (vp->vp_length >> 1)) {
                RDEBUG2("Invalid text in Digest-Response");
                return RLM_MODULE_INVALID;
        }
index 8755074353f3e7148ab2103129f617df5a552737..1098688e8c4a0e048b1187b561be54c865db2ade 100644 (file)
@@ -22,8 +22,9 @@
 
 RCSID("$Id$")
 
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/unlang/base.h>
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 
 #include "eap_mschapv2.h"
 
@@ -389,7 +390,7 @@ static rlm_rcode_t mschap_finalize(REQUEST *request, rlm_eap_mschapv2_t const *i
                        if (n == 3) {
                                RDEBUG2("Found new challenge from MS-CHAP-Error: err=%d retry=%d challenge=%s",
                                        err, retry, buf);
-                               fr_hex2bin(data->auth_challenge, 16, buf, strlen(buf));
+                               fr_hex2bin(&FR_DBUFF_TMP(data->auth_challenge, 16), &FR_SBUFF_IN(buf, strlen(buf)));
                        } else {
                                RDEBUG2("Could not parse new challenge from MS-CHAP-Error: %d", n);
                        }
index 4e97cc920086fc231692818589534cfb58dc6c28..071b10f68987fe9acb7d161a28139260f022be81 100644 (file)
@@ -33,6 +33,7 @@ RCSID("$Id$")
 #include <freeradius-devel/server/password.h>
 #include <freeradius-devel/util/debug.h>
 
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md4.h>
 #include <freeradius-devel/util/md5.h>
 #include <freeradius-devel/util/misc.h>
@@ -581,7 +582,7 @@ static ssize_t mschap_xlat(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
                        return -1;
                }
 
-               fr_bin2hex(*out, buffer, NT_DIGEST_LENGTH);
+               fr_bin2hex(&FR_SBUFF_OUT(*out, (NT_DIGEST_LENGTH * 2) + 1), &FR_DBUFF_TMP(buffer, NT_DIGEST_LENGTH));
                (*out)[32] = '\0';
                RDEBUG2("NT-Hash of \"known-good\" password: %s", *out);
                return 32;
@@ -599,7 +600,7 @@ static ssize_t mschap_xlat(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
                fr_skip_whitespace(p);
 
                smbdes_lmpwdhash(p, buffer);
-               fr_bin2hex(*out, buffer, LM_DIGEST_LENGTH);
+               fr_bin2hex(&FR_SBUFF_OUT(*out, (LM_DIGEST_LENGTH * 2) + 1), &FR_DBUFF_TMP(buffer, LM_DIGEST_LENGTH));
                (*out)[32] = '\0';
                RDEBUG2("LM-Hash of %s = %s", p, *out);
                return 32;
@@ -797,7 +798,7 @@ static int CC_HINT(nonnull (1, 2, 4, 5)) do_mschap_cpw(rlm_mschap_t const *inst,
 
                /* now the password blobs */
                len = sprintf(buf, "new-nt-password-blob: ");
-               fr_bin2hex(buf+len, new_nt_password, 516);
+               fr_bin2hex(&FR_SBUFF_OUT(buf + len, sizeof(buf) - len), &FR_DBUFF_TMP(new_nt_password, 516));
                buf[len+1032] = '\n';
                buf[len+1033] = '\0';
                len = strlen(buf);
@@ -807,7 +808,7 @@ static int CC_HINT(nonnull (1, 2, 4, 5)) do_mschap_cpw(rlm_mschap_t const *inst,
                }
 
                len = sprintf(buf, "old-nt-hash-blob: ");
-               fr_bin2hex(buf+len, old_nt_hash, NT_DIGEST_LENGTH);
+               fr_bin2hex(&FR_SBUFF_OUT(buf + len, sizeof(buf) - len), &FR_DBUFF_TMP(old_nt_hash, NT_DIGEST_LENGTH));
                buf[len+32] = '\n';
                buf[len+33] = '\0';
                len = strlen(buf);
@@ -1221,7 +1222,7 @@ static int CC_HINT(nonnull (1, 2, 4, 5, 6)) do_mschap(rlm_mschap_t const *inst,
                /*
                 *      Update the NT hash hash, from the NT key.
                 */
-               if (fr_hex2bin(nthashhash, NT_DIGEST_LENGTH, buffer + 8, len) != NT_DIGEST_LENGTH) {
+               if (fr_hex2bin(&FR_DBUFF_TMP(nthashhash, NT_DIGEST_LENGTH), &FR_SBUFF_IN(buffer + 8, len)) != NT_DIGEST_LENGTH) {
                        REDEBUG("Invalid output from ntlm_auth: NT_KEY has non-hex values");
                        return -1;
                }
index f0fb67294471942654fe8428b2a9ae4aa4fd8494..ba69a30f34c17d62596a3b88af5bf2ad19336b98 100644 (file)
@@ -30,9 +30,11 @@ USES_APPLE_DEPRECATED_API
 #include <freeradius-devel/server/crypt.h>
 #include <freeradius-devel/server/module.h>
 #include <freeradius-devel/server/password.h>
-#include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/tls/base.h>
+
 #include <freeradius-devel/util/base64.h>
+#include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 #include <freeradius-devel/util/md5.h>
 #include <freeradius-devel/util/sha1.h>
 
@@ -715,7 +717,7 @@ static rlm_rcode_t CC_HINT(nonnull) pap_auth_lm(UNUSED rlm_pap_t const *inst, RE
        len = xlat_eval(charbuf, sizeof(charbuf), request, "%{mschap:LM-Hash %{User-Password}}", NULL, NULL);
        if (len < 0) return RLM_MODULE_FAIL;
 
-       if ((fr_hex2bin(digest, sizeof(digest), charbuf, len) != known_good->vp_length) ||
+       if ((fr_hex2bin(&FR_DBUFF_TMP(digest, sizeof(digest)), &FR_SBUFF_IN(charbuf, len)) != (ssize_t)known_good->vp_length) ||
            (fr_digest_cmp(digest, known_good->vp_octets, known_good->vp_length) != 0)) {
                REDEBUG("LM digest does not match \"known good\" digest");
                REDEBUG3("Calculated : %pH", fr_box_octets(digest, sizeof(digest)));
@@ -744,7 +746,7 @@ static rlm_rcode_t CC_HINT(nonnull) pap_auth_ns_mta_md5(UNUSED rlm_pap_t const *
        /*
         *      Sanity check the value of NS-MTA-MD5-Password
         */
-       if (fr_hex2bin(digest, sizeof(digest), known_good->vp_strvalue, known_good->vp_length) != 16) {
+       if (fr_hex2bin(&FR_DBUFF_TMP(digest, sizeof(digest)), &FR_SBUFF_IN(known_good->vp_strvalue, known_good->vp_length)) != 16) {
                REDEBUG("\"known good\" NS-MTA-MD5-Password has invalid value");
                return RLM_MODULE_INVALID;
        }
index 5fe9d757dae18feaae34091a0271e029ab1b1cd5..d29df7edc8ca978514def57ad0aba8e7273b5a96 100644 (file)
@@ -44,7 +44,9 @@ RCSID("$Id$")
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/module.h>
 #include <freeradius-devel/server/modpriv.h>
+
 #include <freeradius-devel/util/debug.h>
+#include <freeradius-devel/util/hex.h>
 
 #include <freeradius-devel/redis/base.h>
 #include <freeradius-devel/redis/cluster.h>
@@ -1364,17 +1366,17 @@ static int mod_instantiate(void *instance, CONF_SECTION *conf)
                fr_sha1_init(&sha1_ctx);
                fr_sha1_update(&sha1_ctx, (uint8_t const *)lua_alloc_cmd, sizeof(lua_alloc_cmd) - 1);
                fr_sha1_final(digest, &sha1_ctx);
-               fr_bin2hex(lua_alloc_digest, digest, sizeof(digest));
+               fr_bin2hex(&FR_SBUFF_OUT(lua_alloc_digest, sizeof(lua_alloc_digest)), &FR_DBUFF_TMP(digest, sizeof(digest)));
 
                fr_sha1_init(&sha1_ctx);
                fr_sha1_update(&sha1_ctx, (uint8_t const *)lua_update_cmd, sizeof(lua_update_cmd) - 1);
                fr_sha1_final(digest, &sha1_ctx);
-               fr_bin2hex(lua_update_digest, digest, sizeof(digest));
+               fr_bin2hex(&FR_SBUFF_OUT(lua_update_digest, sizeof(lua_update_digest)), &FR_DBUFF_TMP(digest, sizeof(digest)));
 
                fr_sha1_init(&sha1_ctx);
                fr_sha1_update(&sha1_ctx, (uint8_t const *)lua_release_cmd, sizeof(lua_release_cmd) - 1);
                fr_sha1_final(digest, &sha1_ctx);
-               fr_bin2hex(lua_release_digest, digest, sizeof(digest));
+               fr_bin2hex(&FR_SBUFF_OUT(lua_release_digest, sizeof(lua_release_digest)), &FR_DBUFF_TMP(digest, sizeof(digest)));
        }
 
        /*
index a444200277799c868f5fdb3a02733d574f98a8c4..166cadca98b7caf012f1f1f29ebe885c23f319d5 100644 (file)
@@ -26,6 +26,9 @@ RCSID("$Id$")
 
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/module.h>
+
+#include <freeradius-devel/util/hex.h>
+
 #include <ctype.h>
 
 static fr_dict_t const *dict_freeradius;
@@ -130,7 +133,7 @@ static ssize_t unpack_xlat(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
                        goto nothing;
                }
                input = blob;
-               input_len = fr_hex2bin(blob, sizeof(blob), data_name + 2, len);
+               input_len = fr_hex2bin(&FR_DBUFF_TMP(blob, sizeof(blob)), &FR_SBUFF_IN(data_name + 2, len));
 
        } else {
                GOTO_ERROR;
index 0c5bb91b8a607614d10e51a3fc68d3e43251a268..35f37d9004863f5e1d87e4d2e2cd5e6294de042e 100644 (file)
@@ -30,6 +30,7 @@ USES_APPLE_DEPRECATED_API     /* OpenSSL API has been deprecated by Apple */
 #include <freeradius-devel/server/module.h>
 #include <freeradius-devel/tls/base.h>
 #include <freeradius-devel/tls/missing.h>
+#include <freeradius-devel/util/hex.h>
 
 /*
  *     FIXME: Add check for this header to configure.ac
@@ -138,7 +139,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED module_ctx_t const *mct
                 *      so we fix it here.
                 */
                for (i = 0; i < 6; i++) {
-                       fr_bin2hex(&p[i * 3], &buffer[i], 1);
+                       fr_bin2hex(&FR_SBUFF_OUT(&p[i * 3], 2 + 1), &FR_DBUFF_TMP(&buffer[i], 1));
                        p[(i * 3) + 2] = '-';
                }