1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* egg-secure-memory.h - library for allocating memory that is non-pageable
3 
4    Copyright (C) 2007 Stefan Walter
5 
6    The Gnome Keyring Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    The Gnome Keyring Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    see <http://www.gnu.org/licenses/>.
19 
20    Author: Stef Walter <stef@memberwebs.com>
21 */
22 
23 #ifndef EGG_SECURE_MEMORY_H
24 #define EGG_SECURE_MEMORY_H
25 
26 #include <glib.h>
27 #include <stdlib.h>
28 
29 /* -------------------------------------------------------------------
30  * Low Level Secure Memory
31  *
32  * IMPORTANT: This is pure vanila standard C, no glib. We need this
33  * because certain consumers of this protocol need to be built
34  * without linking in any special libraries. ie: the PKCS#11 module.
35  *
36  * Thread locking
37  *
38  * In order to use these functions in a module the following functions
39  * must be defined somewhere, and provide appropriate locking for
40  * secure memory between threads:
41  */
42 
43 typedef struct {
44 	void       (* lock)        (void);
45 	void       (* unlock)      (void);
46 	void *     (* fallback)    (void *pointer,
47 	                            size_t length);
48 	void *        pool_data;
49 	const char *  pool_version;
50 } egg_secure_glob;
51 
52 #define EGG_SECURE_POOL_VER_STR             "1.0"
53 #define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
54 
55 #define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
56 	egg_secure_glob EGG_SECURE_GLOBALS = { \
57 		lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
58 
59 #define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
60 	static GMutex memory_mutex = { NULL, }; \
61 	static void egg_memory_lock (void) \
62 		{ g_mutex_lock (&memory_mutex); } \
63 	static void egg_memory_unlock (void) \
64 		{ g_mutex_unlock (&memory_mutex); } \
65 	EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
66 
67 extern egg_secure_glob EGG_SECURE_GLOBALS;
68 
69 /*
70  * Main functionality
71  *
72  * Allocations return NULL on failure.
73  */
74 
75 #define EGG_SECURE_USE_FALLBACK     0x0001
76 
77 #define EGG_SECURE_DECLARE(tag) \
78 	static inline void* egg_secure_alloc (size_t length) { \
79 		return egg_secure_alloc_full (G_STRINGIFY (tag), length, EGG_SECURE_USE_FALLBACK); \
80 	} \
81 	static inline void* egg_secure_realloc (void *p, size_t length) { \
82 		return egg_secure_realloc_full (G_STRINGIFY (tag), p, length, EGG_SECURE_USE_FALLBACK); \
83 	} \
84 	static inline void* egg_secure_strdup (const char *str) { \
85 		return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
86 	} \
87 	static inline void* egg_secure_strndup (const char *str, size_t length) { \
88 		return egg_secure_strndup_full (G_STRINGIFY (tag), str, length, EGG_SECURE_USE_FALLBACK); \
89 	}
90 
91 void*  egg_secure_alloc_full   (const char *tag, size_t length, int options);
92 
93 void*  egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
94 
95 void   egg_secure_free         (void* p);
96 
97 void   egg_secure_free_full    (void* p, int fallback);
98 
99 void   egg_secure_clear        (void *p, size_t length);
100 
101 int    egg_secure_check        (const void* p);
102 
103 void   egg_secure_validate     (void);
104 
105 char*  egg_secure_strdup_full  (const char *tag, const char *str, int options);
106 
107 char*  egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
108 
109 void   egg_secure_strclear     (char *str);
110 
111 void   egg_secure_strfree      (char *str);
112 
113 typedef struct {
114 	const char *tag;
115 	size_t request_length;
116 	size_t block_length;
117 } egg_secure_rec;
118 
119 egg_secure_rec *   egg_secure_records    (unsigned int *count);
120 
121 #endif /* EGG_SECURE_MEMORY_H */
122