41
41
/* Set a default cipher algorithm and a digest algorithm.
42
42
Even if AES and SHA-256 are not 'MUST' in the latest
43
43
OpenPGP draft, AES seems to be a good choice. */
44
#define DEFAULT_CIPHER_ALGO GCRY_CIPHER_AES
45
#define DEFAULT_DIGEST_ALGO GCRY_MD_SHA256
47
/* The site of the secure memory which is allocated in gcrypt. */
48
#define SECMEM_SIZE 16384
51
/* Hooks to custom memory allocation functions. */
52
static void *(*alloc_func) (size_t n) = gcry_xmalloc;
53
static void *(*alloc_secure_func) (size_t n) = gcry_malloc_secure;
54
static void *(*realloc_func) (void *p, size_t n) = gcry_realloc;
55
static void *(*calloc_func) (size_t m, size_t n) = gcry_calloc;
56
static void (*free_func) (void *) = gcry_free;
57
static int malloc_hooks = 0;
58
static int secmem_init = 0;
60
/* Global settings for the logging. */
61
static cdk_log_fnc_t log_handler = NULL;
62
static void *log_handler_value = NULL;
63
static int log_level = CDK_LOG_NONE;
68
* @ec: the error number
70
* Return an error text for the given id.
79
case CDK_EOF: return "End Of File";
80
case CDK_Success: return "No error";
81
case CDK_General_Error: return "General error";
82
case CDK_File_Error: return strerror (errno);
83
case CDK_Bad_Sig: return "Bad signature";
84
case CDK_Inv_Packet: return "Invalid packet";
85
case CDK_Inv_Algo: return "Invalid algorithm";
86
case CDK_Not_Implemented: return "This is not implemented yet";
87
case CDK_Armor_Error: return "ASCII armor error";
88
case CDK_Armor_CRC_Error: return "ASCII armored damaged (CRC error)";
89
case CDK_MPI_Error: return "Invalid or missformed MPI";
90
case CDK_Inv_Value: return "Invalid parameter or value";
91
case CDK_Error_No_Key: return "No key available or not found";
92
case CDK_Chksum_Error: return "Check for key does not match";
93
case CDK_Time_Conflict: return "Time conflict";
94
case CDK_Zlib_Error: return "ZLIB error";
95
case CDK_Weak_Key: return "Weak key was detected";
96
case CDK_Out_Of_Core: return "Out of core!!";
97
case CDK_Wrong_Seckey: return "Wrong secret key";
98
case CDK_Wrong_Format: return "Data has wrong format";
99
case CDK_Bad_MDC: return "Manipulated MDC detected";
100
case CDK_Inv_Mode: return "Invalid mode";
101
case CDK_Error_No_Keyring: return "No keyring available";
102
case CDK_Inv_Packet_Ver: return "Invalid version for packet";
103
case CDK_Too_Short: return "Buffer or object is too short";
104
case CDK_Unusable_Key: return "Unusable public key";
105
case CDK_No_Data: return "No data";
106
case CDK_No_Passphrase: return "No passphrase supplied";
107
case CDK_Network_Error: return "A network error occurred";
108
default: sprintf (buf, "ec=%d", ec); return buf;
115
out_of_core (size_t n)
117
fprintf (stderr, "\n ** fatal error: out of memory (%d bytes) **\n", n);
122
* cdk_set_malloc_hooks:
123
* @new_alloc_func: malloc replacement
124
* @new_alloc_secure_func: secure malloc replacement
125
* @new_realloc_func: realloc replacement
126
* @new_calloc_func: calloc replacement
127
* @new_free_func: free replacement
129
* Set private memory hooks for the library.
132
cdk_set_malloc_hooks (void *(*new_alloc_func) (size_t n),
133
void *(*new_alloc_secure_func) (size_t n),
134
void *(*new_realloc_func) (void *p, size_t n),
135
void *(*new_calloc_func) (size_t m, size_t n),
136
void (*new_free_func) (void *))
138
alloc_func = new_alloc_func;
139
alloc_secure_func = new_alloc_secure_func;
140
realloc_func = new_realloc_func;
141
calloc_func = new_calloc_func;
142
free_func = new_free_func;
148
* cdk_malloc_hook_initialized:
150
* Return if the malloc hooks are already initialized.
153
cdk_malloc_hook_initialized (void)
160
cdk_malloc (size_t size)
162
void *p = alloc_func (size);
171
* @n: amount of elements
172
* @m: size of one element
174
* Safe wrapper around the c-function calloc.
177
cdk_calloc (size_t n, size_t m)
179
void * p = calloc_func (n, m);
186
/* Things which need to be done after the secure memory initialisation. */
188
_secmem_finish (void)
190
gcry_control (GCRYCTL_DROP_PRIVS);
194
/* Initialize the secure memory. */
196
_secmem_init (size_t size)
198
if (secmem_init == 1)
200
if (size >= SECMEM_SIZE)
203
/* Check if no other library has already initialized gcrypt. */
204
if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
206
_cdk_log_debug ("init: libgcrypt initialize.\n");
207
gcry_control (GCRYCTL_INIT_SECMEM, size, 0);
208
gcry_control (GCRYCTL_USE_SECURE_RNDPOOL);
209
gcry_control (GCRYCTL_DISABLE_SECMEM_WARN);
210
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
216
/* Things which needs to be done to deinit the secure memory. */
220
gcry_control (GCRYCTL_TERM_SECMEM);
225
/* The Windows system needs to startup the Winsock interface first
226
before we can use any socket related function. */
231
static int initialized = 0;
236
if (WSAStartup (0x202, &wsdata))
237
_cdk_log_debug ("winsock init failed.\n");
243
deinit_sockets (void)
248
void init_sockets (void) {}
249
void deinit_sockets (void) {}
256
* Prepare the internal structures of the library.
257
* This function should be called before any other CDK function.
260
cdk_lib_startup (void)
262
_secmem_init (SECMEM_SIZE);
271
* Shutdown the library and free all internal and globally used
272
* memory and structures. This function should be called in the
273
* exit handler of the calling program.
276
cdk_lib_shutdown (void)
284
* @size: how much bytes should be allocated.
285
* @clear: shall the buffer cleared after the allocation?
287
* Allocated the requested amount of bytes in 'secure' memory.
290
cdk_salloc (size_t size, int clear)
295
_secmem_init (SECMEM_SIZE);
297
p = alloc_secure_func (size);
307
cdk_realloc (void *ptr, size_t size)
309
void * p = realloc_func (ptr, size);
317
cdk_strdup (const char * ptr)
319
char * p = cdk_malloc (strlen (ptr) + 1);
327
cdk_free (void * ptr)
334
/* Internal logging routine. */
336
_cdk_logv (int level, const char *fmt, va_list arg_ptr)
340
log_handler (log_handler_value, level, fmt, arg_ptr);
343
if (level == CDK_LOG_NONE)
345
if (level == CDK_LOG_DEBUG)
346
fputs ("DBG: ", stderr);
347
vfprintf (stderr, fmt, arg_ptr);
353
* cdk_set_log_handler:
354
* @logfnc: the function pointer
355
* @opaque: a private values for the function
357
* Set a custom handler for logging.
360
cdk_set_log_handler (cdk_log_fnc_t logfnc, void *opaque)
362
log_handler = logfnc;
363
log_handler_value = opaque;
371
* Set the verbosity level.
374
cdk_set_log_level (int level)
380
/* Return the current log level of the lib. */
382
_cdk_get_log_level (void)
389
_cdk_log_info (const char *fmt, ...)
393
if (log_level == CDK_LOG_NONE)
396
_cdk_logv (CDK_LOG_INFO, fmt, arg);
402
_cdk_log_debug (const char *fmt, ...)
406
if (log_level < CDK_LOG_DEBUG)
409
_cdk_logv (CDK_LOG_DEBUG, fmt, arg);
44
#define DEFAULT_DIGEST_ALGO GNUTLS_DIG_SHA256
414
46
/* Use the passphrase callback in the handle HD or
415
47
return NULL if there is no valid callback. */
417
49
_cdk_passphrase_get (cdk_ctx_t hd, const char *prompt)
419
51
if (!hd || !hd->passphrase_cb)