]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
gnutls_pem_base64_encode2: do raw base64 when msg is NULL
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Thu, 6 Apr 2017 16:09:14 +0000 (18:09 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 7 Apr 2017 08:55:17 +0000 (10:55 +0200)
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
lib/x509/output.c
lib/x509_b64.c

index e006db233c1ae383b37fa83bbefba5593fb2f5de..e071c14031364409e031a8497b7689607dedcb80 100644 (file)
@@ -30,7 +30,6 @@
 #include <x509_int.h>
 #include <num.h>
 #include "errors.h"
-#include <extras/randomart.h>
 #include "extensions.h"
 #include "ip.h"
 
index f60d4d08d096fdc1bfa376a080d4de322b24d8ea..e5a741a868c8c10a74cdd946d8d7027d7709b361 100644 (file)
@@ -53,23 +53,25 @@ _gnutls_fbase64_encode(const char *msg, const uint8_t * data,
        char top[80];
        char bottom[80];
        size_t size, max, bytes;
-       int pos, top_len, bottom_len;
+       int pos, top_len = 0, bottom_len = 0;
 
-       if (msg == NULL || strlen(msg) > 50) {
-               gnutls_assert();
-               return GNUTLS_E_BASE64_ENCODING_ERROR;
-       }
+       if (msg != NULL) {
+               if (strlen(msg) > 50) {
+                       gnutls_assert();
+                       return GNUTLS_E_BASE64_ENCODING_ERROR;
+               }
 
-       _gnutls_str_cpy(top, sizeof(top), "-----BEGIN ");
-       _gnutls_str_cat(top, sizeof(top), msg);
-       _gnutls_str_cat(top, sizeof(top), "-----\n");
+               _gnutls_str_cpy(top, sizeof(top), "-----BEGIN ");
+               _gnutls_str_cat(top, sizeof(top), msg);
+               _gnutls_str_cat(top, sizeof(top), "-----\n");
 
-       _gnutls_str_cpy(bottom, sizeof(bottom), "-----END ");
-       _gnutls_str_cat(bottom, sizeof(bottom), msg);
-       _gnutls_str_cat(bottom, sizeof(bottom), "-----\n");
+               _gnutls_str_cpy(bottom, sizeof(bottom), "-----END ");
+               _gnutls_str_cat(bottom, sizeof(bottom), msg);
+               _gnutls_str_cat(bottom, sizeof(bottom), "-----\n");
 
-       top_len = strlen(top);
-       bottom_len = strlen(bottom);
+               top_len = strlen(top);
+               bottom_len = strlen(bottom);
+       }
 
        max = B64FSIZE(top_len + bottom_len, data_size);
 
@@ -102,9 +104,13 @@ _gnutls_fbase64_encode(const char *msg, const uint8_t * data,
 
                memcpy(ptr, tmpres, size);
                ptr += size;
-               *ptr++ = '\n';
-
-               pos += size + 1;
+               pos += size;
+               if (msg != NULL) {
+                       *ptr++ = '\n';
+                       pos++;
+               } else {
+                       bytes--;
+               }
        }
 
        INCR(bytes, bottom_len, max);
@@ -118,7 +124,7 @@ _gnutls_fbase64_encode(const char *msg, const uint8_t * data,
 
 /**
  * gnutls_pem_base64_encode:
- * @msg: is a message to be put in the header
+ * @msg: is a message to be put in the header (may be %NULL)
  * @data: contain the raw data
  * @result: the place where base64 data will be copied
  * @result_size: holds the size of the result
@@ -126,9 +132,12 @@ _gnutls_fbase64_encode(const char *msg, const uint8_t * data,
  * This function will convert the given data to printable data, using
  * the base64 encoding. This is the encoding used in PEM messages.
  *
- * The output string will be null terminated, although the size will
+ * The output string will be null terminated, although the output size will
  * not include the terminating null.
  *
+ * Since GnuTLS 3.6.0 this function when provided a %NULL msg will
+ * provide a raw base64 output of the input data.
+ *
  * Returns: On success %GNUTLS_E_SUCCESS (0) is returned,
  *   %GNUTLS_E_SHORT_MEMORY_BUFFER is returned if the buffer given is
  *   not long enough, or 0 on success.
@@ -159,7 +168,7 @@ gnutls_pem_base64_encode(const char *msg, const gnutls_datum_t * data,
 
 /**
  * gnutls_pem_base64_encode2:
- * @msg: is a message to be put in the encoded header
+ * @msg: is a message to be put in the encoded header (may be %NULL)
  * @data: contains the raw data
  * @result: will hold the newly allocated encoded data
  *
@@ -174,6 +183,9 @@ gnutls_pem_base64_encode(const char *msg, const gnutls_datum_t * data,
  * under the name gnutls_pem_base64_encode_alloc(). There is
  * compatibility macro pointing to this function.
  *
+ * Since GnuTLS 3.6.0 this function when provided a %NULL msg will
+ * provide a raw base64 output of the input data.
+ *
  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
  *   an error code is returned.
  *