aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/tls/x509_b64.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/tls/x509_b64.c')
-rw-r--r--src/daemon/https/tls/x509_b64.c233
1 files changed, 24 insertions, 209 deletions
diff --git a/src/daemon/https/tls/x509_b64.c b/src/daemon/https/tls/x509_b64.c
index b19e301f..abe3fb40 100644
--- a/src/daemon/https/tls/x509_b64.c
+++ b/src/daemon/https/tls/x509_b64.c
@@ -58,6 +58,17 @@ static const uint8_t asciitable[128] = {
58 0xff, 0xff 58 0xff, 0xff
59}; 59};
60 60
61#define INCR(what, size) \
62 do { \
63 what+=size; \
64 if (what > ret) { \
65 MHD_gnutls_assert(); \
66 MHD_gnutls_free( (*result)); *result = NULL; \
67 return GNUTLS_E_INTERNAL_ERROR; \
68 } \
69 } while(0)
70
71
61inline static int 72inline static int
62encode (char *result, const uint8_t * data, int left) 73encode (char *result, const uint8_t * data, int left)
63{ 74{
@@ -139,48 +150,6 @@ decode (uint8_t * result, const opaque * data)
139} 150}
140 151
141/* encodes data and puts the result into result (locally allocated) 152/* encodes data and puts the result into result (locally allocated)
142 * The result_size is the return value
143 */
144int
145MHD__gnutls_base64_encode (const uint8_t * data, size_t data_size,
146 uint8_t ** result)
147{
148 unsigned int i, j;
149 int ret, tmp;
150 char tmpres[4];
151
152 ret = B64SIZE (data_size);
153
154 (*result) = MHD_gnutls_malloc (ret + 1);
155 if ((*result) == NULL)
156 return GNUTLS_E_MEMORY_ERROR;
157
158 for (i = j = 0; i < data_size; i += 3, j += 4)
159 {
160 tmp = encode (tmpres, &data[i], data_size - i);
161 if (tmp == -1)
162 {
163 MHD_gnutls_free ((*result));
164 return GNUTLS_E_MEMORY_ERROR;
165 }
166 memcpy (&(*result)[j], tmpres, tmp);
167 }
168 (*result)[ret] = 0; /* null terminated */
169
170 return ret;
171}
172
173#define INCR(what, size) \
174 do { \
175 what+=size; \
176 if (what > ret) { \
177 MHD_gnutls_assert(); \
178 MHD_gnutls_free( (*result)); *result = NULL; \
179 return GNUTLS_E_INTERNAL_ERROR; \
180 } \
181 } while(0)
182
183/* encodes data and puts the result into result (locally allocated)
184 * The result_size (including the null terminator) is the return value. 153 * The result_size (including the null terminator) is the return value.
185 */ 154 */
186int 155int
@@ -204,16 +173,16 @@ MHD__gnutls_fbase64_encode (const char *msg, const uint8_t * data,
204 memset (bottom, 0, sizeof (bottom)); 173 memset (bottom, 0, sizeof (bottom));
205 memset (top, 0, sizeof (top)); 174 memset (top, 0, sizeof (top));
206 175
207 strcat (top, "-----BEGIN "); /* Flawfinder: ignore */ 176 strcat ((char*) top, "-----BEGIN "); /* Flawfinder: ignore */
208 strcat (top, msg); /* Flawfinder: ignore */ 177 strcat ((char*)top, msg); /* Flawfinder: ignore */
209 strcat (top, "-----"); /* Flawfinder: ignore */ 178 strcat ((char*)top, "-----"); /* Flawfinder: ignore */
210 179
211 strcat (bottom, "\n-----END "); /* Flawfinder: ignore */ 180 strcat ((char*)bottom, "\n-----END "); /* Flawfinder: ignore */
212 strcat (bottom, msg); /* Flawfinder: ignore */ 181 strcat ((char*)bottom, msg); /* Flawfinder: ignore */
213 strcat (bottom, "-----\n"); /* Flawfinder: ignore */ 182 strcat ((char*)bottom, "-----\n"); /* Flawfinder: ignore */
214 183
215 top_len = strlen (top); 184 top_len = strlen ((char*)top);
216 bottom_len = strlen (bottom); 185 bottom_len = strlen ((char*)bottom);
217 186
218 ret = B64FSIZE (msglen, data_size); 187 ret = B64FSIZE (msglen, data_size);
219 188
@@ -228,7 +197,7 @@ MHD__gnutls_fbase64_encode (const char *msg, const uint8_t * data,
228 INCR (bytes, top_len); 197 INCR (bytes, top_len);
229 pos = top_len; 198 pos = top_len;
230 199
231 strcpy (*result, top); /* Flawfinder: ignore */ 200 strcpy ((char*)*result,(char*) top); /* Flawfinder: ignore */
232 201
233 for (i = j = 0; i < data_size; i += 3, j += 4) 202 for (i = j = 0; i < data_size; i += 3, j += 4)
234 { 203 {
@@ -286,82 +255,6 @@ MHD__gnutls_fbase64_encode (const char *msg, const uint8_t * data,
286 return ret + 1; 255 return ret + 1;
287} 256}
288 257
289/**
290 * MHD_gtls_pem_base64_encode - This function will convert raw data to Base64 encoded
291 * @msg: is a message to be put in the header
292 * @data: contain the raw data
293 * @result: the place where base64 data will be copied
294 * @result_size: holds the size of the result
295 *
296 * This function will convert the given data to printable data, using the base64
297 * encoding. This is the encoding used in PEM messages. If the provided
298 * buffer is not long enough GNUTLS_E_SHORT_MEMORY_BUFFER is returned.
299 *
300 * The output string will be null terminated, although the size will not include
301 * the terminating null.
302 *
303 **/
304int
305MHD_gtls_pem_base64_encode (const char *msg, const MHD_gnutls_datum_t * data,
306 char *result, size_t * result_size)
307{
308 opaque *ret;
309 int size;
310
311 size = MHD__gnutls_fbase64_encode (msg, data->data, data->size, &ret);
312 if (size < 0)
313 return size;
314
315 if (result == NULL || *result_size < (unsigned) size)
316 {
317 MHD_gnutls_free (ret);
318 *result_size = size;
319 return GNUTLS_E_SHORT_MEMORY_BUFFER;
320 }
321 else
322 {
323 memcpy (result, ret, size);
324 MHD_gnutls_free (ret);
325 *result_size = size - 1;
326 }
327
328 return 0;
329}
330
331/**
332 * MHD_gtls_pem_base64_encode_alloc - This function will convert raw data to Base64 encoded
333 * @msg: is a message to be put in the encoded header
334 * @data: contains the raw data
335 * @result: will hold the newly allocated encoded data
336 *
337 * This function will convert the given data to printable data, using the base64
338 * encoding. This is the encoding used in PEM messages. This function will
339 * allocate the required memory to hold the encoded data.
340 *
341 * You should use MHD_gnutls_free() to free the returned data.
342 *
343 **/
344int
345MHD_gtls_pem_base64_encode_alloc (const char *msg,
346 const MHD_gnutls_datum_t * data,
347 MHD_gnutls_datum_t * result)
348{
349 opaque *ret;
350 int size;
351
352 if (result == NULL)
353 return GNUTLS_E_INVALID_REQUEST;
354
355 size = MHD__gnutls_fbase64_encode (msg, data->data, data->size, &ret);
356 if (size < 0)
357 return size;
358
359 result->data = ret;
360 result->size = size - 1;
361 return 0;
362}
363
364
365/* decodes data and puts the result into result (locally allocated) 258/* decodes data and puts the result into result (locally allocated)
366 * The result_size is the return value 259 * The result_size is the return value
367 */ 260 */
@@ -440,7 +333,7 @@ MHD__gnutls_fbase64_decode (const char *header, const opaque * data,
440 if (header != NULL) 333 if (header != NULL)
441 MHD_gtls_str_cat (pem_header, sizeof (pem_header), header); 334 MHD_gtls_str_cat (pem_header, sizeof (pem_header), header);
442 335
443 rdata = MHD_memmem (data, data_size, pem_header, strlen (pem_header)); 336 rdata = memmem (data, data_size, pem_header, strlen (pem_header));
444 337
445 if (rdata == NULL) 338 if (rdata == NULL)
446 { 339 {
@@ -457,11 +350,11 @@ MHD__gnutls_fbase64_decode (const char *header, const opaque * data,
457 return GNUTLS_E_BASE64_DECODING_ERROR; 350 return GNUTLS_E_BASE64_DECODING_ERROR;
458 } 351 }
459 352
460 kdata = MHD_memmem (rdata, data_size, ENDSTR, sizeof (ENDSTR) - 1); 353 kdata = memmem (rdata, data_size, ENDSTR, sizeof (ENDSTR) - 1);
461 /* allow CR as well. 354 /* allow CR as well.
462 */ 355 */
463 if (kdata == NULL) 356 if (kdata == NULL)
464 kdata = MHD_memmem (rdata, data_size, ENDSTR2, sizeof (ENDSTR2) - 1); 357 kdata = memmem (rdata, data_size, ENDSTR2, sizeof (ENDSTR2) - 1);
465 358
466 if (kdata == NULL) 359 if (kdata == NULL)
467 { 360 {
@@ -476,7 +369,7 @@ MHD__gnutls_fbase64_decode (const char *header, const opaque * data,
476 369
477 /* position is now after the ---BEGIN--- headers */ 370 /* position is now after the ---BEGIN--- headers */
478 371
479 kdata = MHD_memmem (rdata, data_size, bottom, strlen (bottom)); 372 kdata = memmem (rdata, data_size, bottom, strlen (bottom));
480 if (kdata == NULL) 373 if (kdata == NULL)
481 { 374 {
482 MHD_gnutls_assert (); 375 MHD_gnutls_assert ();
@@ -519,81 +412,3 @@ MHD__gnutls_fbase64_decode (const char *header, const opaque * data,
519 return ret; 412 return ret;
520} 413}
521 414
522/**
523 * MHD_gtls_pem_base64_decode - This function will decode base64 encoded data
524 * @header: A null terminated string with the PEM header (eg. CERTIFICATE)
525 * @b64_data: contain the encoded data
526 * @result: the place where decoded data will be copied
527 * @result_size: holds the size of the result
528 *
529 * This function will decode the given encoded data. If the header given
530 * is non null this function will search for "-----BEGIN header" and decode
531 * only this part. Otherwise it will decode the first PEM packet found.
532 *
533 * Returns GNUTLS_E_SHORT_MEMORY_BUFFER if the buffer given is not long enough,
534 * or 0 on success.
535 **/
536int
537MHD_gtls_pem_base64_decode (const char *header,
538 const MHD_gnutls_datum_t * b64_data,
539 unsigned char *result, size_t * result_size)
540{
541 opaque *ret;
542 int size;
543
544 size =
545 MHD__gnutls_fbase64_decode (header, b64_data->data, b64_data->size, &ret);
546 if (size < 0)
547 return size;
548
549 if (result == NULL || *result_size < (unsigned) size)
550 {
551 MHD_gnutls_free (ret);
552 *result_size = size;
553 return GNUTLS_E_SHORT_MEMORY_BUFFER;
554 }
555 else
556 {
557 memcpy (result, ret, size);
558 MHD_gnutls_free (ret);
559 *result_size = size;
560 }
561
562 return 0;
563}
564
565/**
566 * MHD_gtls_pem_base64_decode_alloc - This function will decode base64 encoded data
567 * @header: The PEM header (eg. CERTIFICATE)
568 * @b64_data: contains the encoded data
569 * @result: the place where decoded data lie
570 *
571 * This function will decode the given encoded data. The decoded data
572 * will be allocated, and stored into result.
573 * If the header given is non null this function will search for
574 * "-----BEGIN header" and decode only this part. Otherwise it will decode the
575 * first PEM packet found.
576 *
577 * You should use MHD_gnutls_free() to free the returned data.
578 *
579 **/
580int
581MHD_gtls_pem_base64_decode_alloc (const char *header,
582 const MHD_gnutls_datum_t * b64_data,
583 MHD_gnutls_datum_t * result)
584{
585 opaque *ret;
586 int size;
587
588 if (result == NULL)
589 return GNUTLS_E_INVALID_REQUEST;
590
591 size =
592 MHD__gnutls_fbase64_decode (header, b64_data->data, b64_data->size, &ret);
593 if (size < 0)
594 return size;
595
596 result->data = ret;
597 result->size = size;
598 return 0;
599}