-static int decode_key(const char *key_file, const char *header_str,
- const char *footer_str, unsigned char **result)
-{
- int ret, ret2, i, j;
- void *map;
- size_t map_size, key_size, blob_size;
- unsigned char *blob = NULL;
- char *begin, *footer, *key;
-
- ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
- if (ret < 0)
- return ret;
- ret = -E_KEY_MARKER;
- if (strncmp(map, header_str, strlen(header_str)))
- goto unmap;
- footer = strstr(map, footer_str);
- ret = -E_KEY_MARKER;
- if (!footer)
- goto unmap;
- begin = map + strlen(header_str);
- /* skip whitespace at the beginning */
- for (; begin < footer; begin++) {
- if (para_isspace(*begin))
- continue;
- break;
- }
- ret = -E_KEY_MARKER;
- if (begin >= footer)
- goto unmap;
-
- key_size = footer - begin;
- key = para_malloc(key_size + 1);
- for (i = 0, j = 0; begin + i < footer; i++) {
- if (para_isspace(begin[i]))
- continue;
- key[j++] = begin[i];
- }
- key[j] = '\0';
- //PARA_CRIT_LOG("key: %s\n", key);
- blob_size = key_size * 2;
- blob = para_malloc(blob_size);
- ret = base64_decode(key, blob, blob_size);
- free(key);
- if (ret < 0)
- goto free_unmap;
- goto unmap;
-free_unmap:
- free(blob);
- blob = NULL;
-unmap:
- ret2 = para_munmap(map, map_size);
- if (ret >= 0 && ret2 < 0)
- ret = ret2;
- if (ret < 0) {
- free(blob);
- blob = NULL;
- }
- *result = blob;
- return ret;
-}
-