2011-06-12 20:55:02 +00:00
|
|
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
|
|
|
|
/* egg-secure-memory.h - library for allocating memory that is non-pageable
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
Copyright (C) 2007 Stefan Walter
|
|
|
|
|
|
|
|
The Gnome Keyring Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Gnome Keyring Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with the Gnome Library; see the file COPYING.LIB. If not,
|
|
|
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
Author: Stef Walter <stef@memberwebs.com>
|
|
|
|
*/
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
#ifndef EGG_SECURE_MEMORY_H
|
|
|
|
#define EGG_SECURE_MEMORY_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
2012-10-27 09:58:13 +00:00
|
|
|
* Low Level Secure Memory
|
|
|
|
*
|
|
|
|
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
|
|
|
* because certain consumers of this protocol need to be built
|
2011-06-12 20:55:02 +00:00
|
|
|
* without linking in any special libraries. ie: the PKCS#11 module.
|
2012-10-27 09:58:13 +00:00
|
|
|
*
|
2011-06-12 20:55:02 +00:00
|
|
|
* Thread locking
|
2012-10-27 09:58:13 +00:00
|
|
|
*
|
2011-06-12 20:55:02 +00:00
|
|
|
* In order to use these functions in a module the following functions
|
2012-10-27 09:58:13 +00:00
|
|
|
* must be defined somewhere, and provide appropriate locking for
|
2011-06-12 20:55:02 +00:00
|
|
|
* secure memory between threads:
|
|
|
|
*/
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
typedef struct {
|
|
|
|
void (* lock) (void);
|
|
|
|
void (* unlock) (void);
|
|
|
|
void * (* fallback) (void *pointer,
|
|
|
|
size_t length);
|
|
|
|
void * pool_data;
|
|
|
|
const char * pool_version;
|
|
|
|
} egg_secure_glob;
|
|
|
|
|
|
|
|
#define EGG_SECURE_POOL_VER_STR "1.0"
|
|
|
|
#define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
|
|
|
|
|
|
|
|
#define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
|
|
|
|
egg_secure_glob EGG_SECURE_GLOBALS = { \
|
|
|
|
lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
|
|
|
|
|
|
|
|
#define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
|
2013-01-25 18:06:11 +00:00
|
|
|
static GMutex memory_mutex = { NULL, }; \
|
2012-10-27 09:58:13 +00:00
|
|
|
static void egg_memory_lock (void) \
|
2013-01-25 18:06:11 +00:00
|
|
|
{ g_mutex_lock (&memory_mutex); } \
|
2012-10-27 09:58:13 +00:00
|
|
|
static void egg_memory_unlock (void) \
|
2013-01-25 18:06:11 +00:00
|
|
|
{ g_mutex_unlock (&memory_mutex); } \
|
2012-10-27 09:58:13 +00:00
|
|
|
EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
extern egg_secure_glob EGG_SECURE_GLOBALS;
|
|
|
|
|
|
|
|
/*
|
2011-06-12 20:55:02 +00:00
|
|
|
* Main functionality
|
2012-10-27 09:58:13 +00:00
|
|
|
*
|
2011-06-12 20:55:02 +00:00
|
|
|
* Allocations return NULL on failure.
|
2012-10-27 09:58:13 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
#define EGG_SECURE_USE_FALLBACK 0x0001
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
#define EGG_SECURE_DECLARE(tag) \
|
|
|
|
static inline void* egg_secure_alloc (size_t length) { \
|
|
|
|
return egg_secure_alloc_full (G_STRINGIFY (tag), length, EGG_SECURE_USE_FALLBACK); \
|
|
|
|
} \
|
|
|
|
static inline void* egg_secure_realloc (void *p, size_t length) { \
|
|
|
|
return egg_secure_realloc_full (G_STRINGIFY (tag), p, length, EGG_SECURE_USE_FALLBACK); \
|
|
|
|
} \
|
|
|
|
static inline void* egg_secure_strdup (const char *str) { \
|
|
|
|
return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
|
2012-03-25 10:17:03 +00:00
|
|
|
} \
|
|
|
|
static inline void* egg_secure_strndup (const char *str, size_t length) { \
|
|
|
|
return egg_secure_strndup_full (G_STRINGIFY (tag), str, length, EGG_SECURE_USE_FALLBACK); \
|
2011-09-25 06:22:36 +00:00
|
|
|
}
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
void* egg_secure_alloc_full (const char *tag, size_t length, int options);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
void* egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
void egg_secure_free (void* p);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
void egg_secure_free_full (void* p, int fallback);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
void egg_secure_clear (void *p, size_t length);
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
int egg_secure_check (const void* p);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
void egg_secure_validate (void);
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
char* egg_secure_strdup_full (const char *tag, const char *str, int options);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-03-25 10:17:03 +00:00
|
|
|
char* egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
void egg_secure_strclear (char *str);
|
|
|
|
|
|
|
|
void egg_secure_strfree (char *str);
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *tag;
|
|
|
|
size_t request_length;
|
|
|
|
size_t block_length;
|
|
|
|
} egg_secure_rec;
|
|
|
|
|
|
|
|
egg_secure_rec * egg_secure_records (unsigned int *count);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#endif /* EGG_SECURE_MEMORY_H */
|