summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2003-10-30 10:08:43 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2003-10-30 10:08:43 +0000
commit22fe46e3e82f46d9d7fe6527d12f41b1ef5db69b (patch)
treee76de41464c9779b683f2ca1c0f4277cf4afb18d
parentcd6df99b20c085ca063162542d4b5ba9f8eb3f9e (diff)
downloadgnutls-22fe46e3e82f46d9d7fe6527d12f41b1ef5db69b.tar.gz
Made the B64FSIZE to return an accurate value.
-rw-r--r--lib/x509/common.c2
-rw-r--r--lib/x509_b64.c48
-rw-r--r--lib/x509_b64.h7
3 files changed, 44 insertions, 13 deletions
diff --git a/lib/x509/common.c b/lib/x509/common.c
index 4daf9e02b5..eb64ec686c 100644
--- a/lib/x509/common.c
+++ b/lib/x509/common.c
@@ -626,7 +626,7 @@ int _gnutls_x509_export_int( ASN1_TYPE asn1_data,
gnutls_assert();
if (result == ASN1_MEM_ERROR) {
_gnutls_debug_log("Length required for der coding: %d\n", len);
- *output_data_size = B64FSIZE(strlen(pem_header),len);
+ *output_data_size = B64FSIZE(strlen(pem_header),len) + 1;
}
gnutls_afree(tmp);
return _gnutls_asn2err(result);
diff --git a/lib/x509_b64.c b/lib/x509_b64.c
index f6316618c4..912414ab78 100644
--- a/lib/x509_b64.c
+++ b/lib/x509_b64.c
@@ -162,8 +162,16 @@ int _gnutls_base64_encode(const uint8 * data, size_t data_size, uint8 ** result)
return ret;
}
+#define INCR(what, size) \
+ what+=size; \
+ if (what > ret) { \
+ gnutls_assert(); \
+ gnutls_free( (*result)); *result = NULL; \
+ return GNUTLS_E_INTERNAL_ERROR; \
+ }
+
/* encodes data and puts the result into result (locally allocated)
- * The result_size is the return value
+ * The result_size (including the null terminator) is the return value.
*/
int _gnutls_fbase64_encode(const char *msg, const uint8 * data, int data_size,
uint8 ** result)
@@ -173,11 +181,13 @@ int _gnutls_fbase64_encode(const char *msg, const uint8 * data, int data_size,
uint8 *ptr;
uint8 top[80];
uint8 bottom[80];
- int pos;
+ int pos, bytes, top_len, bottom_len;
size_t msglen = strlen(msg);
- if (msglen > 50)
+ if (msglen > 50) {
+ gnutls_assert();
return GNUTLS_E_BASE64_ENCODING_ERROR;
+ }
memset(bottom, 0, sizeof(bottom));
memset(top, 0, sizeof(top));
@@ -190,50 +200,70 @@ int _gnutls_fbase64_encode(const char *msg, const uint8 * data, int data_size,
strcat(bottom, msg); /* Flawfinder: ignore */
strcat(bottom, "-----\n"); /* Flawfinder: ignore */
+ top_len = strlen(top);
+ bottom_len = strlen(bottom);
+
ret = B64FSIZE( msglen, data_size);
(*result) = gnutls_calloc(1, ret + 1);
- if ((*result) == NULL)
+ if ((*result) == NULL) {
+ gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
+ }
+
+ bytes = pos = 0;
+ INCR( bytes, top_len);
+ pos = top_len;
strcpy(*result, top); /* Flawfinder: ignore */
- pos = strlen(top);
for (i = j = 0; i < data_size; i += 3, j += 4) {
+
tmp = encode(tmpres, &data[i], data_size - i);
if (tmp == -1) {
+ gnutls_assert();
gnutls_free( (*result)); *result = NULL;
return GNUTLS_E_BASE64_ENCODING_ERROR;
}
+
+ INCR(bytes, 4);
ptr = &(*result)[j + pos];
if ((j) % 64 == 0) {
+ INCR(bytes, 1);
pos++;
*ptr++ = '\n';
}
*ptr++ = tmpres[0];
if ((j + 1) % 64 == 0) {
- *ptr++ = '\n';
+ INCR(bytes, 1);
pos++;
+ *ptr++ = '\n';
}
*ptr++ = tmpres[1];
if ((j + 2) % 64 == 0) {
+ INCR(bytes, 1);
pos++;
*ptr++ = '\n';
}
*ptr++ = tmpres[2];
if ((j + 3) % 64 == 0) {
- *ptr++ = '\n';
+ INCR(bytes, 1);
pos++;
+ *ptr++ = '\n';
}
*ptr++ = tmpres[3];
}
- strcat(*result, bottom); /* Flawfinder: ignore */
- return strlen(*result) + 1;
+ INCR( bytes, bottom_len);
+
+ memcpy( &(*result)[ bytes-bottom_len], bottom, bottom_len);
+ (*result)[ bytes] = 0;
+
+ return ret + 1;
}
/**
diff --git a/lib/x509_b64.h b/lib/x509_b64.h
index 60b0ed1fb1..752095c688 100644
--- a/lib/x509_b64.h
+++ b/lib/x509_b64.h
@@ -5,15 +5,16 @@ int _gnutls_base64_decode(const uint8 * data, size_t data_size, uint8 ** result)
int _gnutls_fbase64_decode( const opaque* header, const uint8 * data, size_t data_size,
uint8 ** result);
-#define B64SIZE( data_size) ((data_size%3==0)?((data_size*4)/3):(4+((data_size*4)/3)))
+#define B64SIZE( data_size) ((data_size%3==0)?((data_size*4)/3):(4+((data_size/3)*4)))
/* The size for B64 encoding + newlines plus header
*/
#define HEADSIZE( hsize) \
- sizeof("-----BEGIN")-1+sizeof("-----\n")-1+ \
+ sizeof("-----BEGIN ")-1+sizeof("-----")-1+ \
sizeof("\n-----END ")-1+sizeof("-----\n")-1+hsize+hsize
#define B64FSIZE( hsize, dsize) \
(B64SIZE(dsize) + HEADSIZE(hsize) + /*newlines*/ \
- B64SIZE(dsize)/64 + (B64SIZE(dsize) % 64 > 0 ? 1 : 0))
+ B64SIZE(dsize)/64 + (((B64SIZE(dsize) % 64) > 0) ? 1 : 0))
+