The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / chunk.c
index 811a975..6f12c9b 100644 (file)
@@ -1,10 +1,3 @@
-/**
- * @file chunk.c
- *
- * @brief Pointer/lenght abstraction and its functions.
- *
- */
-
 /*
  * Copyright (C) 2005-2006 Martin Willi
  * Copyright (C) 2005 Jan Hutter
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
+ *
+ * $Id$
  */
 
 #include <stdio.h>
+#include <sys/stat.h>
 
 #include "chunk.h"
 
+#include <debug.h>
 #include <printf_hook.h>
+#include <utils/randomizer.h>
 
 /**
  * Empty chunk.
@@ -35,13 +33,22 @@ chunk_t chunk_empty = { NULL, 0 };
 /**
  * Described in header.
  */
-chunk_t chunk_clone(chunk_t chunk)
+chunk_t chunk_create(u_char *ptr, size_t len)
+{
+       chunk_t chunk = {ptr, len};
+       return chunk;
+}
+
+/**
+ * Described in header.
+ */
+chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk)
 {
        chunk_t clone = chunk_empty;
        
        if (chunk.ptr && chunk.len > 0)
        {
-               clone.ptr = malloc(chunk.len);
+               clone.ptr = ptr;
                clone.len = chunk.len;
                memcpy(clone.ptr, chunk.ptr, chunk.len);
        }
@@ -52,45 +59,66 @@ chunk_t chunk_clone(chunk_t chunk)
 /**
  * Decribed in header.
  */
-chunk_t chunk_cat(const char* mode, ...)
+size_t chunk_length(const char* mode, ...)
 {
-       chunk_t construct;
        va_list chunks;
-       u_char *pos;
-       int i;
-       int count = strlen(mode);
-
-       /* sum up lengths of individual chunks */
+       size_t length = 0;
+       
        va_start(chunks, mode);
-       construct.len = 0;
-       for (i = 0; i < count; i++)
+       while (TRUE)
        {
-               chunk_t ch = va_arg(chunks, chunk_t);
-               construct.len += ch.len;
+               switch (*mode++)
+               {
+                       case 'm':
+                       case 'c':
+                       {
+                               chunk_t ch = va_arg(chunks, chunk_t);
+                               length += ch.len;
+                               continue;
+                       }
+                       default:
+                               break;
+               }
+               break;
        }
        va_end(chunks);
+       return length;
+}
 
-       /* allocate needed memory for construct */
-       construct.ptr = malloc(construct.len);
-       pos = construct.ptr;
-
-       /* copy or move the chunks */
+/**
+ * Decribed in header.
+ */
+chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...)
+{
+       va_list chunks;
+       chunk_t construct = chunk_create(ptr, 0);
+       
        va_start(chunks, mode);
-       for (i = 0; i < count; i++)
+       while (TRUE)
        {
-               chunk_t ch = va_arg(chunks, chunk_t);
+               bool free_chunk = FALSE;
                switch (*mode++)
                {
                        case 'm':
-                               memcpy(pos, ch.ptr, ch.len); 
-                               pos += ch.len;
-                               free(ch.ptr);
-                               break;
+                       {
+                               free_chunk = TRUE;
+                       }
                        case 'c':
+                       {
+                               chunk_t ch = va_arg(chunks, chunk_t);
+                               memcpy(ptr, ch.ptr, ch.len); 
+                               ptr += ch.len;
+                               construct.len += ch.len;
+                               if (free_chunk)
+                               {
+                                       free(ch.ptr);
+                               }
+                               continue;
+                       }
                        default:
-                               memcpy(pos, ch.ptr, ch.len); 
-                               pos += ch.len;
+                               break;
                }
+               break;
        }
        va_end(chunks);
        
@@ -98,123 +126,247 @@ chunk_t chunk_cat(const char* mode, ...)
 }
 
 /**
- * Described in header.
+ * Decribed in header.
  */
-void chunk_free(chunk_t *chunk)
+void chunk_split(chunk_t chunk, const char *mode, ...)
 {
-       free(chunk->ptr);
-       chunk->ptr = NULL;
-       chunk->len = 0;
+       va_list chunks;
+       size_t len;
+       chunk_t *ch;
+       
+       va_start(chunks, mode);
+       while (TRUE)
+       {
+               if (*mode == '\0')
+               {
+                       break;
+               }
+               len = va_arg(chunks, size_t);
+               ch = va_arg(chunks, chunk_t*);
+               /* a null chunk means skip len bytes */
+               if (ch == NULL)
+               {
+                       chunk = chunk_skip(chunk, len);
+                       continue;
+               }
+               switch (*mode++)
+               {
+                       case 'm':
+                       {
+                               ch->len = min(chunk.len, len);
+                               if (ch->len)
+                               {
+                                       ch->ptr = chunk.ptr;
+                               }
+                               else
+                               {
+                                       ch->ptr = NULL;
+                               }
+                               chunk = chunk_skip(chunk, ch->len);
+                               continue;
+                       }
+                       case 'a':
+                       {
+                               ch->len = min(chunk.len, len);
+                               if (ch->len)
+                               {
+                                       ch->ptr = malloc(ch->len);
+                                       memcpy(ch->ptr, chunk.ptr, ch->len);
+                               }
+                               else
+                               {
+                                       ch->ptr = NULL;
+                               }
+                               chunk = chunk_skip(chunk, ch->len);
+                               continue;
+                       }
+                       case 'c':
+                       {
+                               ch->len = min(ch->len, chunk.len);
+                               ch->len = min(ch->len, len);
+                               if (ch->len)
+                               {
+                                       memcpy(ch->ptr, chunk.ptr, ch->len);
+                               }
+                               else
+                               {
+                                       ch->ptr = NULL;
+                               }
+                               chunk = chunk_skip(chunk, ch->len);
+                               continue;
+                       }
+                       default:
+                               break;
+               }
+               break;
+       }
+       va_end(chunks);
 }
 
 /**
  * Described in header.
  */
-chunk_t chunk_alloc(size_t bytes)
+bool chunk_write(chunk_t chunk, const char *path, const char *label, mode_t mask, bool force)
 {
-       chunk_t new_chunk;
-       new_chunk.ptr = malloc(bytes);
-       new_chunk.len = bytes;
-       return new_chunk;
+       mode_t oldmask;
+       FILE *fd;
+
+       if (!force)
+       {
+               fd = fopen(path, "r");
+               if (fd)
+               {
+                       fclose(fd);
+                       DBG1("  %s file '%s' already exists", label, path);
+                       return FALSE;
+               }
+       }
+
+       /* set umask */
+       oldmask = umask(mask);
+
+       fd = fopen(path, "w");
+
+       if (fd)
+       {
+               fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd);
+               fclose(fd);
+               DBG1("  written %s file '%s' (%u bytes)", label, path, chunk.len);
+               umask(oldmask);
+               return TRUE;
+       }
+       else
+       {
+               DBG1("  could not open %s file '%s' for writing", label, path);
+               umask(oldmask);
+               return FALSE;
+       }
 }
 
+/** hex conversion digits */
+static char hexdig_upper[] = "0123456789ABCDEF";
+static char hexdig_lower[] = "0123456789abcdef";
+
 /**
  * Described in header.
  */
-bool chunk_equals(chunk_t a, chunk_t b)
+char *chunk_to_hex(chunk_t chunk, bool uppercase)
 {
-       return a.ptr != NULL  && b.ptr != NULL &&
-                       a.len == b.len && memeq(a.ptr, b.ptr, a.len);
+       int i;
+       char *str;
+       char *hexdig = hexdig_lower;
+       
+       if (uppercase)
+       {
+               hexdig = hexdig_upper;
+       }
+       
+       str = malloc(chunk.len * 2 + 1);
+       str[chunk.len * 2] = '\0';
+       
+       for (i = 0; i < chunk.len; i ++)
+       {
+               str[i*2]   = hexdig[(chunk.ptr[i] >> 4) & 0xF];
+               str[i*2+1] = hexdig[(chunk.ptr[i]     ) & 0xF];
+       }
+       return str;
 }
 
 /**
  * Described in header.
  */
-bool chunk_equals_or_null(chunk_t a, chunk_t b)
+void chunk_free(chunk_t *chunk)
 {
-       if (a.ptr == NULL || b.ptr == NULL)
-               return TRUE;
-       return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
+       free(chunk->ptr);
+       chunk->ptr = NULL;
+       chunk->len = 0;
 }
 
 /**
- * Number of bytes per line to dump raw data
+ * Described in header.
  */
-#define BYTES_PER_LINE 16
+void chunk_free_randomized(chunk_t *chunk)
+{
+       if (chunk->ptr)
+       {
+               if (chunk->len > 0)
+               {
+                       randomizer_t *randomizer = randomizer_create();
+
+                       randomizer->get_pseudo_random_bytes(randomizer,
+                                                                                               chunk->len, chunk->ptr);
+                       randomizer->destroy(randomizer);
+               };
+               free(chunk->ptr);
+               chunk->ptr = NULL;
+       }
+       chunk->len = 0;
+}
 
 /**
- * output handler in printf() for byte ranges
+ * Described in header.
  */
-static int print_bytes(FILE *stream, const struct printf_info *info,
-                                          const void *const *args)
+chunk_t chunk_skip(chunk_t chunk, size_t bytes)
 {
-       char *bytes = *((void**)(args[0]));
-       int len = *((size_t*)(args[1]));
-       
-       char buffer[BYTES_PER_LINE * 3];
-       char ascii_buffer[BYTES_PER_LINE + 1];
-       char *buffer_pos = buffer;
-       char *bytes_pos  = bytes;
-       char *bytes_roof = bytes + len;
-       int line_start = 0;
-       int i = 0;
-       int written = 0;
-       
-       written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
-       
-       while (bytes_pos < bytes_roof)
+       if (chunk.len > bytes)
        {
-               static char hexdig[] = "0123456789ABCDEF";
-               
-               *buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
-               *buffer_pos++ = hexdig[ *bytes_pos       & 0xF];
+               chunk.ptr += bytes;
+               chunk.len -= bytes;
+               return chunk;
+       }
+       return chunk_empty;
+}
 
-               ascii_buffer[i++] =
-                               (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
+/**
+ * Described in header.
+ */
+int chunk_compare(chunk_t a, chunk_t b)
+{
+       int compare_len = a.len - b.len;
+       int len = (compare_len < 0)? a.len : b.len;
 
-               if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) 
-               {
-                       int padding = 3 * (BYTES_PER_LINE - i);
-                       int written;
-                       
-                       while (padding--)
-                       {
-                               *buffer_pos++ = ' ';
-                       }
-                       *buffer_pos++ = '\0';
-                       ascii_buffer[i] = '\0';
-                       
-                       written += fprintf(stream, "\n%4d: %s  %s",
-                                                          line_start, buffer, ascii_buffer);
-
-                       
-                       buffer_pos = buffer;
-                       line_start += BYTES_PER_LINE;
-                       i = 0;
-               }
-               else
-               {
-                       *buffer_pos++ = ' ';
-               }
+       if (compare_len != 0 || len == 0)
+       {
+               return compare_len;
        }
-       return written;
+       return memcmp(a.ptr, b.ptr, len);
+};
+
+/**
+ * Described in header.
+ */
+bool chunk_equals(chunk_t a, chunk_t b)
+{
+       return a.ptr != NULL  && b.ptr != NULL &&
+                       a.len == b.len && memeq(a.ptr, b.ptr, a.len);
+}
+
+/**
+ * Described in header.
+ */
+bool chunk_equals_or_null(chunk_t a, chunk_t b)
+{
+       if (a.ptr == NULL || b.ptr == NULL)
+               return TRUE;
+       return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
 }
 
 /**
  * output handler in printf() for chunks
  */
-static int print_chunk(FILE *stream, const struct printf_info *info,
+static int chunk_print(FILE *stream, const struct printf_info *info,
                                           const void *const *args)
 {
        chunk_t *chunk = *((chunk_t**)(args[0]));
        bool first = TRUE;
        chunk_t copy = *chunk;
        int written = 0;
+       printf_hook_functions_t mem = mem_get_printf_hooks();
        
        if (!info->alt)
        {
                const void *new_args[] = {&chunk->ptr, &chunk->len};
-               return print_bytes(stream, info, new_args);
+               return mem.print(stream, info, new_args);
        }
        
        while (copy.len > 0)
@@ -234,10 +386,24 @@ static int print_chunk(FILE *stream, const struct printf_info *info,
 }
 
 /**
- * register printf() handlers
+ * arginfo handler for printf() mem ranges
  */
-static void __attribute__ ((constructor))print_register()
+static int chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
 {
-       register_printf_function(PRINTF_CHUNK, print_chunk, arginfo_ptr);
-       register_printf_function(PRINTF_BYTES, print_bytes, arginfo_ptr_int);
+       if (n > 0)
+       {
+               argtypes[0] = PA_POINTER;
+       }
+       return 1;
 }
+
+/**
+ * return printf hook functions for a chunk
+ */
+printf_hook_functions_t chunk_get_printf_hooks()
+{
+       printf_hook_functions_t hooks = {chunk_print, chunk_arginfo};
+       
+       return hooks;
+}
+