From cfb7b5aed687c04d7b6ed82cf734bf08fb4d190f Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 31 Jul 2017 09:08:24 +0200 Subject: base64: introduced new functions for base64 encoding Signed-off-by: Nikos Mavrogiannopoulos --- lib/includes/gnutls/gnutls.h.in | 5 +++ lib/libgnutls.map | 2 ++ lib/x509_b64.c | 67 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/includes/gnutls/gnutls.h.in b/lib/includes/gnutls/gnutls.h.in index da8db487a0..e5ba7c6439 100644 --- a/lib/includes/gnutls/gnutls.h.in +++ b/lib/includes/gnutls/gnutls.h.in @@ -2427,6 +2427,11 @@ int gnutls_pem_base64_decode2(const char *header, const gnutls_datum_t * b64_data, gnutls_datum_t * result); +int gnutls_base64_encode2(const gnutls_datum_t * data, + gnutls_datum_t * result); +int gnutls_base64_decode2(const gnutls_datum_t * b64_data, + gnutls_datum_t * result); + #define gnutls_pem_base64_encode_alloc gnutls_pem_base64_encode2 #define gnutls_pem_base64_decode_alloc gnutls_pem_base64_decode2 diff --git a/lib/libgnutls.map b/lib/libgnutls.map index 4ed21f8fa1..2568b9aefb 100644 --- a/lib/libgnutls.map +++ b/lib/libgnutls.map @@ -1172,6 +1172,8 @@ GNUTLS_3_4 gnutls_group_get; gnutls_priority_group_list; gnutls_pkcs11_token_check_mechanism; + gnutls_base64_encode2; + gnutls_base64_decode2; local: *; }; diff --git a/lib/x509_b64.c b/lib/x509_b64.c index 340724c4d7..2f229feeb3 100644 --- a/lib/x509_b64.c +++ b/lib/x509_b64.c @@ -261,6 +261,10 @@ _gnutls_base64_decode(const uint8_t * data, size_t data_size, base64_decode_init(&ctx); size = BASE64_DECODE_LENGTH(data_size); + if (size == 0) { + ret = gnutls_assert_val(GNUTLS_E_PARSING_ERROR); + goto cleanup; + } result->data = gnutls_malloc(size); if (result->data == NULL) { @@ -430,7 +434,7 @@ gnutls_pem_base64_decode(const char *header, * gnutls_pem_base64_decode2: * @header: The PEM header (eg. CERTIFICATE) * @b64_data: contains the encoded data - * @result: the place where decoded data lie + * @result: the location of decoded data * * This function will decode the given encoded data. The decoded data * will be allocated, and stored into result. If the header given is @@ -471,3 +475,64 @@ gnutls_pem_base64_decode2(const char *header, return 0; } + +/** + * gnutls_base64_decode2: + * @base64: contains the encoded data + * @result: the location of decoded data + * + * This function will decode the given base64 encoded data. The decoded data + * will be allocated, and stored into result. + * + * You should use gnutls_free() to free the returned data. + * + * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise + * an error code is returned. + * + * Since: 3.6.0 + **/ +int +gnutls_base64_decode2(const gnutls_datum_t *base64, + gnutls_datum_t *result) +{ + int ret; + + ret = _gnutls_base64_decode(base64->data, base64->size, result); + if (ret < 0) { + return gnutls_assert_val(ret); + } + + return 0; +} + +/** + * gnutls_base64_encode2: + * @data: contains the raw data + * @result: will hold the newly allocated encoded data + * + * This function will convert the given data to printable data, using + * the base64 encoding. This function will allocate the required + * memory to hold the encoded data. + * + * You should use gnutls_free() to free the returned data. + * + * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise + * an error code is returned. + * + * Since: 3.6.0 + **/ +int +gnutls_base64_encode2(const gnutls_datum_t *data, + gnutls_datum_t *result) +{ + int ret; + + if (result == NULL) + return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); + + ret = _gnutls_fbase64_encode(NULL, data->data, data->size, result); + if (ret < 0) + return gnutls_assert_val(ret); + + return 0; +} -- cgit v1.2.1