Clean up base64_{encode,decode} pointer types

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>
master
Jouni Malinen 5 years ago committed by Jouni Malinen
parent b22608423b
commit 8e5e36a184

@ -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);

@ -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;

@ -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;

@ -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);

@ -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");

@ -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");

@ -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;

@ -12,18 +12,16 @@
#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);
}

@ -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);

@ -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);

@ -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);

@ -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;

@ -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);
}

@ -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 */

@ -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;

@ -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;

@ -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)

@ -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)

@ -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;

Loading…
Cancel
Save