]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Clean up base64_{encode,decode} pointer types
authorJouni Malinen <jouni@codeaurora.org>
Wed, 27 Nov 2019 13:55:33 +0000 (15:55 +0200)
committerJouni Malinen <j@w1.fi>
Thu, 28 Nov 2019 14:39:09 +0000 (16:39 +0200)
Allow any pointer to be used as source for encoding and use char * as
the return value from encoding and input value for decoding to reduce
number of type casts needed in the callers.

Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
19 files changed:
hs20/client/est.c
hs20/client/osu_client.c
hs20/server/spp_server.c
src/common/dpp.c
src/eap_peer/tncc.c
src/eap_server/tncs.c
src/tls/tlsv1_cred.c
src/utils/base64.c
src/utils/base64.h
src/utils/utils_module_tests.c
src/utils/xml_libxml2.c
src/wps/upnp_xml.c
src/wps/wps_er.c
src/wps/wps_registrar.c
src/wps/wps_upnp.c
src/wps/wps_upnp_web.c
wpa_supplicant/config_file.c
wpa_supplicant/eapol_test.c
wpa_supplicant/hs20_supplicant.c

index db65334b20f3e05f76aca5a75e328861f6f7d0b0..97f9132100c46cafaa7e4c908868db3d894a4be5 100644 (file)
@@ -158,7 +158,7 @@ int est_load_cacerts(struct hs20_osu_client *ctx, const char *url)
                return -1;
        }
 
-       pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
+       pkcs7 = base64_decode(resp, resp_len, &pkcs7_len);
        if (pkcs7 && pkcs7_len < resp_len / 2) {
                wpa_printf(MSG_INFO, "Too short base64 decode (%u bytes; downloaded %u bytes) - assume this was binary",
                           (unsigned int) pkcs7_len, (unsigned int) resp_len);
@@ -639,8 +639,7 @@ int est_build_csr(struct hs20_osu_client *ctx, const char *url)
                        return -1;
                }
 
-               attrs = base64_decode((unsigned char *) resp, resp_len,
-                                     &attrs_len);
+               attrs = base64_decode(resp, resp_len, &attrs_len);
                os_free(resp);
 
                if (attrs == NULL) {
@@ -734,7 +733,7 @@ int est_simple_enroll(struct hs20_osu_client *ctx, const char *url,
        }
        wpa_printf(MSG_DEBUG, "EST simpleenroll response: %s", resp);
 
-       pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
+       pkcs7 = base64_decode(resp, resp_len, &pkcs7_len);
        if (pkcs7 == NULL) {
                wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
                pkcs7 = os_malloc(resp_len);
index fd99600da7880a7c36aa407e4e01edf01384d3cc..a94f40c51ee01f8745a7af5a92982440b5b5f06c 100644 (file)
@@ -310,7 +310,7 @@ static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
        size_t len;
        u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
        int res;
-       unsigned char *b64;
+       char *b64;
        FILE *f;
 
        url_node = get_node(ctx->xml, params, "CertURL");
@@ -364,7 +364,7 @@ static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
                return -1;
        }
 
-       b64 = base64_encode((unsigned char *) cert, len, NULL);
+       b64 = base64_encode(cert, len, NULL);
        os_free(cert);
        if (b64 == NULL)
                return -1;
index 4bef0ffb2d4ee8bf89bc92e3366dc4726f931699..a50e9074f7b43c48be3f14f2ae8d099fca6575e5 100644 (file)
@@ -633,7 +633,7 @@ static xml_node_t * build_username_password(struct hs20_svc *ctx,
 
        add_text_node(ctx, node, "Username", user);
 
-       b64 = (char *) base64_encode((unsigned char *) pw, strlen(pw), NULL);
+       b64 = base64_encode(pw, strlen(pw), NULL);
        if (b64 == NULL)
                return NULL;
        len = os_strlen(b64);
@@ -1602,8 +1602,7 @@ static xml_node_t * spp_exec_get_certificate(struct hs20_svc *ctx,
 
        xml_node_create_text(ctx->xml, enroll, ns, "estUserID", user);
 
-       b64 = (char *) base64_encode((unsigned char *) password,
-                                    strlen(password), NULL);
+       b64 = base64_encode(password, strlen(password), NULL);
        if (b64 == NULL) {
                xml_node_free(ctx->xml, spp_node);
                return NULL;
index 1dfc346728755d26af1235d64ba5eb058f378b45..704f55c7ae4167509ff3839b91a00827f5d1969f 100644 (file)
@@ -1007,8 +1007,7 @@ static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
        if (!end)
                return -1;
 
-       data = base64_decode((const unsigned char *) info, end - info,
-                            &data_len);
+       data = base64_decode(info, end - info, &data_len);
        if (!data) {
                wpa_printf(MSG_DEBUG,
                           "DPP: Invalid base64 encoding on URI public-key");
@@ -1482,7 +1481,7 @@ int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi)
 char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
                  const u8 *privkey, size_t privkey_len)
 {
-       unsigned char *base64 = NULL;
+       char *base64 = NULL;
        char *pos, *end;
        size_t len;
        struct wpabuf *der = NULL;
@@ -1528,7 +1527,7 @@ char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
        der = NULL;
        if (!base64)
                goto fail;
-       pos = (char *) base64;
+       pos = base64;
        end = pos + len;
        for (;;) {
                pos = os_strchr(pos, '\n');
@@ -1536,7 +1535,7 @@ char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
                        break;
                os_memmove(pos, pos + 1, end - pos);
        }
-       return (char *) base64;
+       return base64;
 fail:
        os_free(base64);
        wpabuf_free(der);
index a9bafe2886c09b7ddfde639675ab456dd1dc5b3b..c460980999db1214c27be2a9b16ed6ff699d2dea 100644 (file)
@@ -144,7 +144,7 @@ static TNC_Result TNC_TNCC_SendMessage(
        TNC_MessageType messageType)
 {
        struct tnc_if_imc *imc;
-       unsigned char *b64;
+       char *b64;
        size_t b64len;
 
        wpa_printf(MSG_DEBUG, "TNC: TNC_TNCC_SendMessage(imcID=%lu "
@@ -629,8 +629,7 @@ static unsigned char * tncc_get_base64(char *start, size_t *decoded_len)
                return NULL;
        *pos2 = '\0';
 
-       decoded = base64_decode((unsigned char *) pos, os_strlen(pos),
-                               decoded_len);
+       decoded = base64_decode(pos, os_strlen(pos), decoded_len);
        *pos2 = '<';
        if (decoded == NULL) {
                wpa_printf(MSG_DEBUG, "TNC: Failed to decode Base64 data");
index 942a195761ac7e6ebd90f8ca17d15cbac26913ef..4a30486f7792df34501e5889766f3e2c52e9a108 100644 (file)
@@ -179,7 +179,7 @@ static TNC_Result TNC_TNCS_SendMessage(
        TNC_MessageType messageType)
 {
        struct tncs_data *tncs;
-       unsigned char *b64;
+       char *b64;
        size_t b64len;
 
        wpa_printf(MSG_DEBUG, "TNC: TNC_TNCS_SendMessage(imvID=%lu "
@@ -678,8 +678,7 @@ static unsigned char * tncs_get_base64(char *start, size_t *decoded_len)
                return NULL;
        *pos2 = '\0';
 
-       decoded = base64_decode((unsigned char *) pos, os_strlen(pos),
-                               decoded_len);
+       decoded = base64_decode(pos, os_strlen(pos), decoded_len);
        *pos2 = '<';
        if (decoded == NULL) {
                wpa_printf(MSG_DEBUG, "TNC: Failed to decode Base64 data");
index 842e5dd728c78cac7ab00f6d370d46b13345f607..01b2f83425cbc5261a7d9b80988a93c5577a7b17 100644 (file)
@@ -130,7 +130,7 @@ static int tlsv1_add_cert(struct x509_certificate **chain,
                        return -1;
                }
 
-               der = base64_decode(pos, end - pos, &der_len);
+               der = base64_decode((const char *) pos, end - pos, &der_len);
                if (der == NULL) {
                        wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM "
                                   "certificate");
@@ -293,7 +293,7 @@ static struct crypto_private_key * tlsv1_set_key_pem(const u8 *key, size_t len)
                }
        }
 
-       der = base64_decode(pos, end - pos, &der_len);
+       der = base64_decode((const char *) pos, end - pos, &der_len);
        if (!der)
                return NULL;
        pkey = crypto_private_key_import(der, der_len, NULL);
@@ -321,7 +321,7 @@ static struct crypto_private_key * tlsv1_set_key_enc_pem(const u8 *key,
        if (!end)
                return NULL;
 
-       der = base64_decode(pos, end - pos, &der_len);
+       der = base64_decode((const char *) pos, end - pos, &der_len);
        if (!der)
                return NULL;
        pkey = crypto_private_key_import(der, der_len, passwd);
@@ -1225,7 +1225,7 @@ static int tlsv1_set_dhparams_blob(struct tlsv1_credentials *cred,
                return -1;
        }
 
-       der = base64_decode(pos, end - pos, &der_len);
+       der = base64_decode((const char *) pos, end - pos, &der_len);
        if (der == NULL) {
                wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM dhparams");
                return -1;
index 0518cf0836dd7784e50cb998910674cb71f1575d..a17d2d36dc2f0c5f3346ac161f72aab4aa726da0 100644 (file)
 #include "os.h"
 #include "base64.h"
 
-static const unsigned char base64_table[65] =
+static const char base64_table[65] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-static const unsigned char base64_url_table[65] =
+static const char base64_url_table[65] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
 
 
-static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
-                                        size_t *out_len,
-                                        const unsigned char *table,
-                                        int add_pad)
+static char * base64_gen_encode(const unsigned char *src, size_t len,
+                               size_t *out_len, const char *table, int add_pad)
 {
-       unsigned char *out, *pos;
+       char *out, *pos;
        const unsigned char *end, *in;
        size_t olen;
        int line_len;
@@ -83,9 +81,8 @@ static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
 }
 
 
-static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
-                                        size_t *out_len,
-                                        const unsigned char *table)
+static unsigned char * base64_gen_decode(const char *src, size_t len,
+                                        size_t *out_len, const char *table)
 {
        unsigned char dtable[256], *out, *pos, block[4], tmp;
        size_t i, count, olen;
@@ -94,12 +91,12 @@ static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
 
        os_memset(dtable, 0x80, 256);
        for (i = 0; i < sizeof(base64_table) - 1; i++)
-               dtable[table[i]] = (unsigned char) i;
+               dtable[(unsigned char) table[i]] = (unsigned char) i;
        dtable['='] = 0;
 
        count = 0;
        for (i = 0; i < len; i++) {
-               if (dtable[src[i]] != 0x80)
+               if (dtable[(unsigned char) src[i]] != 0x80)
                        count++;
        }
 
@@ -165,8 +162,7 @@ static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
  * nul terminated to make it easier to use as a C string. The nul terminator is
  * not included in out_len.
  */
-unsigned char * base64_encode(const unsigned char *src, size_t len,
-                             size_t *out_len)
+char * base64_encode(const void *src, size_t len, size_t *out_len)
 {
        return base64_gen_encode(src, len, out_len, base64_table, 1);
 }
@@ -174,8 +170,7 @@ unsigned char * base64_encode(const unsigned char *src, size_t len,
 
 char * base64_url_encode(const void *src, size_t len, size_t *out_len)
 {
-       return (char *) base64_gen_encode(src, len, out_len, base64_url_table,
-                                         0);
+       return base64_gen_encode(src, len, out_len, base64_url_table, 0);
 }
 
 
@@ -189,8 +184,7 @@ char * base64_url_encode(const void *src, size_t len, size_t *out_len)
  *
  * Caller is responsible for freeing the returned buffer.
  */
-unsigned char * base64_decode(const unsigned char *src, size_t len,
-                             size_t *out_len)
+unsigned char * base64_decode(const char *src, size_t len, size_t *out_len)
 {
        return base64_gen_decode(src, len, out_len, base64_table);
 }
@@ -198,6 +192,5 @@ unsigned char * base64_decode(const unsigned char *src, size_t len,
 
 unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
 {
-       return base64_gen_decode((const unsigned char *) src, len, out_len,
-                                base64_url_table);
+       return base64_gen_decode(src, len, out_len, base64_url_table);
 }
index 1e9f0532c1e9ed209bbbf66d773cd91574091b42..6216f44e559582e021183de7f93371420f9551bb 100644 (file)
@@ -9,10 +9,8 @@
 #ifndef BASE64_H
 #define BASE64_H
 
-unsigned char * base64_encode(const unsigned char *src, size_t len,
-                             size_t *out_len);
-unsigned char * base64_decode(const unsigned char *src, size_t len,
-                             size_t *out_len);
+char * base64_encode(const void *src, size_t len, size_t *out_len);
+unsigned char * base64_decode(const char *src, size_t len, size_t *out_len);
 char * base64_url_encode(const void *src, size_t len, size_t *out_len);
 unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len);
 
index 3af4fcde1daa86385f60cde71f8e2e1eea4b1239..b09225de0533f6eabc4730c869dfe3e88e3a2547 100644 (file)
@@ -296,52 +296,53 @@ static int base64_tests(void)
 {
        int errors = 0;
        unsigned char *res;
+       char *res2;
        size_t res_len;
 
        wpa_printf(MSG_INFO, "base64 tests");
 
-       res = base64_encode((const unsigned char *) "", ~0, &res_len);
-       if (res) {
+       res2 = base64_encode("", ~0, &res_len);
+       if (res2) {
                errors++;
-               os_free(res);
+               os_free(res2);
        }
 
-       res = base64_encode((const unsigned char *) "=", 1, &res_len);
-       if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
-           res[2] != '=' || res[3] != '=' || res[4] != '\n')
+       res2 = base64_encode("=", 1, &res_len);
+       if (!res2 || res_len != 5 || res2[0] != 'P' || res2[1] != 'Q' ||
+           res2[2] != '=' || res2[3] != '=' || res2[4] != '\n')
                errors++;
-       os_free(res);
+       os_free(res2);
 
-       res = base64_encode((const unsigned char *) "=", 1, NULL);
-       if (!res || res[0] != 'P' || res[1] != 'Q' ||
-           res[2] != '=' || res[3] != '=' || res[4] != '\n')
+       res2 = base64_encode("=", 1, NULL);
+       if (!res2 || res2[0] != 'P' || res2[1] != 'Q' ||
+           res2[2] != '=' || res2[3] != '=' || res2[4] != '\n')
                errors++;
-       os_free(res);
+       os_free(res2);
 
-       res = base64_decode((const unsigned char *) "", 0, &res_len);
+       res = base64_decode("", 0, &res_len);
        if (res) {
                errors++;
                os_free(res);
        }
 
-       res = base64_decode((const unsigned char *) "a", 1, &res_len);
+       res = base64_decode("a", 1, &res_len);
        if (res) {
                errors++;
                os_free(res);
        }
 
-       res = base64_decode((const unsigned char *) "====", 4, &res_len);
+       res = base64_decode("====", 4, &res_len);
        if (res) {
                errors++;
                os_free(res);
        }
 
-       res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
+       res = base64_decode("PQ==", 4, &res_len);
        if (!res || res_len != 1 || res[0] != '=')
                errors++;
        os_free(res);
 
-       res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
+       res = base64_decode("P.Q-=!=*", 8, &res_len);
        if (!res || res_len != 1 || res[0] != '=')
                errors++;
        os_free(res);
index 7b6d2764b0ed2f0ab24ee65dc6fb1e4a32773cc2..d73654eb522def8a39348d186b788c979fedd411 100644 (file)
@@ -409,7 +409,7 @@ char * xml_node_get_base64_text(struct xml_node_ctx *ctx, xml_node_t *node,
        if (txt == NULL)
                return NULL;
 
-       ret = base64_decode((unsigned char *) txt, strlen(txt), &len);
+       ret = base64_decode(txt, strlen(txt), &len);
        if (ret_len)
                *ret_len = len;
        xml_node_get_text_free(ctx, txt);
index a9958eeda80d1881711485b673e13cecee61a801..ca0925cb5833092aa70adf22ebbe1eb07abc5eb2 100644 (file)
@@ -235,7 +235,7 @@ struct wpabuf * xml_get_base64_item(const char *data, const char *name,
                return NULL;
        }
 
-       decoded = base64_decode((unsigned char *) msg, os_strlen(msg), &len);
+       decoded = base64_decode(msg, os_strlen(msg), &len);
        os_free(msg);
        if (decoded == NULL) {
                *ret = UPNP_OUT_OF_MEMORY;
index 06a8fdaf3459ccfd138940c93bba309056ec72f2..6bded14327f8959b72ed83a72dab8fd8c839e0e9 100644 (file)
@@ -897,7 +897,7 @@ static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
                                       const struct sockaddr_in *dst,
                                       char **len_ptr, char **body_ptr)
 {
-       unsigned char *encoded;
+       char *encoded;
        size_t encoded_len;
        struct wpabuf *buf;
 
@@ -939,7 +939,7 @@ static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
        wpabuf_put_str(buf, "\">\n");
        if (encoded) {
                wpabuf_printf(buf, "<%s>%s</%s>\n",
-                             arg_name, (char *) encoded, arg_name);
+                             arg_name, encoded, arg_name);
                os_free(encoded);
        }
 
index 0ac5b2831379312e125f4e5be6f5b19f50388186..671f5fed76b4d8c7deb6f559afa9695dd5f67ba2 100644 (file)
@@ -1745,7 +1745,8 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                        return -1;
                }
                os_free(wps->new_psk);
-               wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
+               wps->new_psk = (u8 *) base64_encode(r, sizeof(r),
+                                                   &wps->new_psk_len);
                if (wps->new_psk == NULL)
                        return -1;
                wps->new_psk_len--; /* remove newline */
index ca893a43c64b4dc437f058e9e5ab47a43455556d..61d21597c1b39dc39d86f382cb514ae91fc1e3cb 100644 (file)
@@ -647,7 +647,7 @@ static int subscription_first_event(struct subscription *s)
                           "initial WLANEvent");
                msg = build_fake_wsc_ack();
                if (msg) {
-                       s->sm->wlanevent = (char *)
+                       s->sm->wlanevent =
                                base64_encode(wpabuf_head(msg),
                                              wpabuf_len(msg), NULL);
                        wpabuf_free(msg);
@@ -822,7 +822,7 @@ int upnp_wps_device_send_wlan_event(struct upnp_wps_device_sm *sm,
        }
        raw_len = pos;
 
-       val = (char *) base64_encode(raw, raw_len, &val_len);
+       val = base64_encode(raw, raw_len, &val_len);
        if (val == NULL)
                goto fail;
 
index 7548e8432a68fd5c85b598527fb27e0be0c5ff8e..3c5a97c78406db4ebaf9523ed358542d8e9ec614 100644 (file)
@@ -765,8 +765,8 @@ static void web_connection_send_reply(struct http_request *req,
 
        if (reply) {
                size_t len;
-               replydata = (char *) base64_encode(wpabuf_head(reply),
-                                                  wpabuf_len(reply), &len);
+               replydata = base64_encode(wpabuf_head(reply), wpabuf_len(reply),
+                                         &len);
        } else
                replydata = NULL;
 
index cf4b7bc6e6ab2cabdc5c3c28532e80ecd31f9d4a..55971fcc6187104b035518685879b4c8eee81e70 100644 (file)
@@ -296,7 +296,7 @@ static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
 {
        struct wpa_config_blob *blob;
        char buf[256], *pos;
-       unsigned char *encoded = NULL, *nencoded;
+       char *encoded = NULL, *nencoded;
        int end = 0;
        size_t encoded_len = 0, len;
 
@@ -1098,7 +1098,7 @@ static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
 #ifndef CONFIG_NO_CONFIG_BLOBS
 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
 {
-       unsigned char *encoded;
+       char *encoded;
 
        encoded = base64_encode(blob->data, blob->len, NULL);
        if (encoded == NULL)
index 524724f19735d990664b719a7c6d888f61b2bd9d..53d75853bdf66da773a62886cedd325cc7168ee7 100644 (file)
@@ -439,7 +439,7 @@ static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result,
 static void eapol_test_write_cert(FILE *f, const char *subject,
                                  const struct wpabuf *cert)
 {
-       unsigned char *encoded;
+       char *encoded;
 
        encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
        if (encoded == NULL)
index cb236df18d86b5556031e1fa2e674cf1a8fbacb1..47a1d0175de599fc868c82bceef9c3db676da090 100644 (file)
@@ -340,7 +340,7 @@ int hs20_get_icon(struct wpa_supplicant *wpa_s, const u8 *bssid,
 {
        struct icon_entry *icon;
        size_t out_size;
-       unsigned char *b64;
+       char *b64;
        size_t b64_size;
        int reply_size;