summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2017-07-31 09:08:24 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2017-08-04 16:53:54 +0200
commitcfb7b5aed687c04d7b6ed82cf734bf08fb4d190f (patch)
tree0f7db7d9fb69d67c60beebdd3da08d0652179f9b
parenta3a8ea816c11e5bbbd1bc0faf9593c73627563e9 (diff)
downloadgnutls-cfb7b5aed687c04d7b6ed82cf734bf08fb4d190f.tar.gz
base64: introduced new functions for base64 encoding
Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
-rw-r--r--lib/includes/gnutls/gnutls.h.in5
-rw-r--r--lib/libgnutls.map2
-rw-r--r--lib/x509_b64.c67
3 files changed, 73 insertions, 1 deletions
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;
+}