]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
base64: introduced new functions for base64 encoding
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Mon, 31 Jul 2017 07:08:24 +0000 (09:08 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 4 Aug 2017 14:53:54 +0000 (16:53 +0200)
Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
lib/includes/gnutls/gnutls.h.in
lib/libgnutls.map
lib/x509_b64.c

index da8db487a0586244b7d6555f2f1e5f8355b5a616..e5ba7c6439ae1d85fc453550ec87d0fb85ca9586 100644 (file)
@@ -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
 
index 4ed21f8fa15682c32bb4a28f1fb545607caa2fef..2568b9aefbd30fde4e15dd155dbad0b26f0d9caf 100644 (file)
@@ -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:
        *;
 };
index 340724c4d7add06af77ca023d6f4f39bf2a1db6e..2f229feeb337f2fc77d02ab22aa53929d4235dd5 100644 (file)
@@ -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;
+}