Clean up base64_url_{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>
This commit is contained in:
Jouni Malinen 2019-11-27 15:30:53 +02:00 committed by Jouni Malinen
parent a4255a207b
commit b22608423b
4 changed files with 26 additions and 38 deletions

View file

@ -4679,9 +4679,9 @@ static int dpp_build_jwk(struct wpabuf *buf, const char *name, EVP_PKEY *key,
if (!pub) if (!pub)
goto fail; goto fail;
pos = wpabuf_head(pub); pos = wpabuf_head(pub);
x = (char *) base64_url_encode(pos, curve->prime_len, NULL); x = base64_url_encode(pos, curve->prime_len, NULL);
pos += curve->prime_len; pos += curve->prime_len;
y = (char *) base64_url_encode(pos, curve->prime_len, NULL); y = base64_url_encode(pos, curve->prime_len, NULL);
if (!x || !y) if (!x || !y)
goto fail; goto fail;
@ -4851,11 +4851,9 @@ skip_groups:
os_snprintf(jws_prot_hdr, sizeof(jws_prot_hdr), os_snprintf(jws_prot_hdr, sizeof(jws_prot_hdr),
"{\"typ\":\"dppCon\",\"kid\":\"%s\",\"alg\":\"%s\"}", "{\"typ\":\"dppCon\",\"kid\":\"%s\",\"alg\":\"%s\"}",
auth->conf->kid, curve->jws_alg); auth->conf->kid, curve->jws_alg);
signed1 = (char *) base64_url_encode((unsigned char *) jws_prot_hdr, signed1 = base64_url_encode(jws_prot_hdr, os_strlen(jws_prot_hdr),
os_strlen(jws_prot_hdr),
&signed1_len); &signed1_len);
signed2 = (char *) base64_url_encode(wpabuf_head(dppcon), signed2 = base64_url_encode(wpabuf_head(dppcon), wpabuf_len(dppcon),
wpabuf_len(dppcon),
&signed2_len); &signed2_len);
if (!signed1 || !signed2) if (!signed1 || !signed2)
goto fail; goto fail;
@ -4906,8 +4904,7 @@ skip_groups:
signature_len = 2 * curve->prime_len; signature_len = 2 * curve->prime_len;
wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (raw r,s)", wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (raw r,s)",
signature, signature_len); signature, signature_len);
signed3 = (char *) base64_url_encode(signature, signature_len, signed3 = base64_url_encode(signature, signature_len, &signed3_len);
&signed3_len);
if (!signed3) if (!signed3)
goto fail; goto fail;
@ -5819,8 +5816,7 @@ dpp_process_signed_connector(struct dpp_signed_connector_info *info,
ret = DPP_STATUS_INVALID_CONNECTOR; ret = DPP_STATUS_INVALID_CONNECTOR;
goto fail; goto fail;
} }
prot_hdr = base64_url_decode((const unsigned char *) pos, prot_hdr = base64_url_decode(pos, end - pos, &prot_hdr_len);
end - pos, &prot_hdr_len);
if (!prot_hdr) { if (!prot_hdr) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"DPP: Failed to base64url decode signedConnector JWS Protected Header"); "DPP: Failed to base64url decode signedConnector JWS Protected Header");
@ -5852,8 +5848,7 @@ dpp_process_signed_connector(struct dpp_signed_connector_info *info,
goto fail; goto fail;
} }
signed_end = end - 1; signed_end = end - 1;
info->payload = base64_url_decode((const unsigned char *) pos, info->payload = base64_url_decode(pos, end - pos, &info->payload_len);
end - pos, &info->payload_len);
if (!info->payload) { if (!info->payload) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"DPP: Failed to base64url decode signedConnector JWS Payload"); "DPP: Failed to base64url decode signedConnector JWS Payload");
@ -5864,8 +5859,7 @@ dpp_process_signed_connector(struct dpp_signed_connector_info *info,
"DPP: signedConnector - JWS Payload", "DPP: signedConnector - JWS Payload",
info->payload, info->payload_len); info->payload, info->payload_len);
pos = end + 1; pos = end + 1;
signature = base64_url_decode((const unsigned char *) pos, signature = base64_url_decode(pos, os_strlen(pos), &signature_len);
os_strlen(pos), &signature_len);
if (!signature) { if (!signature) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"DPP: Failed to base64url decode signedConnector signature"); "DPP: Failed to base64url decode signedConnector signature");
@ -6605,13 +6599,13 @@ struct wpabuf * dpp_build_conn_status_result(struct dpp_authentication *auth,
return NULL; return NULL;
wpabuf_printf(json, "{\"result\":%d", result); wpabuf_printf(json, "{\"result\":%d", result);
if (ssid) { if (ssid) {
unsigned char *ssid64; char *ssid64;
ssid64 = base64_url_encode(ssid, ssid_len, NULL); ssid64 = base64_url_encode(ssid, ssid_len, NULL);
if (!ssid64) if (!ssid64)
goto fail; goto fail;
wpabuf_put_str(json, ",\"ssid64\":\""); wpabuf_put_str(json, ",\"ssid64\":\"");
wpabuf_put_str(json, (char *) ssid64); wpabuf_put_str(json, ssid64);
os_free(ssid64); os_free(ssid64);
wpabuf_put_str(json, "\""); wpabuf_put_str(json, "\"");
} }
@ -6758,8 +6752,7 @@ dpp_keygen_configurator(const char *curve, const u8 *privkey,
goto fail; goto fail;
} }
conf->kid = (char *) base64_url_encode(kid_hash, sizeof(kid_hash), conf->kid = base64_url_encode(kid_hash, sizeof(kid_hash), NULL);
NULL);
if (!conf->kid) if (!conf->kid)
goto fail; goto fail;
out: out:
@ -7021,8 +7014,7 @@ dpp_peer_intro(struct dpp_introduction *intro, const char *own_connector,
wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the second dot (.)"); wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the second dot (.)");
goto fail; goto fail;
} }
own_conn = base64_url_decode((const unsigned char *) pos, own_conn = base64_url_decode(pos, end - pos, &own_conn_len);
end - pos, &own_conn_len);
if (!own_conn) { if (!own_conn) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"DPP: Failed to base64url decode own signedConnector JWS Payload"); "DPP: Failed to base64url decode own signedConnector JWS Payload");
@ -8756,8 +8748,7 @@ char * dpp_corrupt_connector_signature(const char *connector)
wpa_printf(MSG_DEBUG, "DPP: Original base64url encoded signature: %s", wpa_printf(MSG_DEBUG, "DPP: Original base64url encoded signature: %s",
pos); pos);
signature = base64_url_decode((const unsigned char *) pos, signature = base64_url_decode(pos, os_strlen(pos), &signature_len);
os_strlen(pos), &signature_len);
if (!signature || signature_len == 0) if (!signature || signature_len == 0)
goto fail; goto fail;
wpa_hexdump(MSG_DEBUG, "DPP: Original Connector signature", wpa_hexdump(MSG_DEBUG, "DPP: Original Connector signature",
@ -8765,8 +8756,7 @@ char * dpp_corrupt_connector_signature(const char *connector)
signature[signature_len - 1] ^= 0x01; signature[signature_len - 1] ^= 0x01;
wpa_hexdump(MSG_DEBUG, "DPP: Corrupted Connector signature", wpa_hexdump(MSG_DEBUG, "DPP: Corrupted Connector signature",
signature, signature_len); signature, signature_len);
signed3 = (char *) base64_url_encode(signature, signature_len, signed3 = base64_url_encode(signature, signature_len, &signed3_len);
&signed3_len);
if (!signed3) if (!signed3)
goto fail; goto fail;
os_memcpy(pos, signed3, signed3_len); os_memcpy(pos, signed3, signed3_len);

View file

@ -172,10 +172,10 @@ unsigned char * base64_encode(const unsigned char *src, size_t len,
} }
unsigned char * base64_url_encode(const unsigned char *src, size_t len, char * base64_url_encode(const void *src, size_t len, size_t *out_len)
size_t *out_len)
{ {
return base64_gen_encode(src, len, out_len, base64_url_table, 0); return (char *) base64_gen_encode(src, len, out_len, base64_url_table,
0);
} }
@ -196,8 +196,8 @@ unsigned char * base64_decode(const unsigned char *src, size_t len,
} }
unsigned char * base64_url_decode(const unsigned char *src, size_t len, unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
size_t *out_len)
{ {
return base64_gen_decode(src, len, out_len, base64_url_table); return base64_gen_decode((const unsigned char *) src, len, out_len,
base64_url_table);
} }

View file

@ -13,9 +13,7 @@ unsigned char * base64_encode(const unsigned char *src, size_t len,
size_t *out_len); size_t *out_len);
unsigned char * base64_decode(const unsigned char *src, size_t len, unsigned char * base64_decode(const unsigned char *src, size_t len,
size_t *out_len); size_t *out_len);
unsigned char * base64_url_encode(const unsigned char *src, size_t len, char * base64_url_encode(const void *src, size_t len, size_t *out_len);
size_t *out_len); unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len);
unsigned char * base64_url_decode(const unsigned char *src, size_t len,
size_t *out_len);
#endif /* BASE64_H */ #endif /* BASE64_H */

View file

@ -514,8 +514,8 @@ struct wpabuf * json_get_member_base64url(struct json_token *json,
token = json_get_member(json, name); token = json_get_member(json, name);
if (!token || token->type != JSON_STRING) if (!token || token->type != JSON_STRING)
return NULL; return NULL;
buf = base64_url_decode((const unsigned char *) token->string, buf = base64_url_decode(token->string, os_strlen(token->string),
os_strlen(token->string), &buflen); &buflen);
if (!buf) if (!buf)
return NULL; return NULL;
ret = wpabuf_alloc_ext_data(buf, buflen); ret = wpabuf_alloc_ext_data(buf, buflen);