-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt_dl.c
116 lines (102 loc) · 2.45 KB
/
crypt_dl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* crypt_dl.c -- dynamically loaded crypto proxy
*
* Copyright Dean Scarff
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "hope.h"
static void *dl; // dlopen handle
// Pointers to the module's implementations of the crypt.h functions.
static void (*crypt_init_impl)();
static void (*crypt_finish_impl)();
static void (*crypt_load_key_impl)(FILE *);
static uint16_t (*crypt_footprint_impl)();
static size_t (*crypt_sign_length_impl)();
static char *(*crypt_sign_impl)(char *, const char *, size_t);
/* Return non-zero iff all the impl pointers are set. */
static int
have_all()
{
return crypt_init_impl && crypt_finish_impl && crypt_load_key_impl &&
crypt_footprint_impl && crypt_sign_length_impl &&
crypt_sign_impl;
}
/* Set all implementation pointers to NULL. */
static void
reset_all()
{
crypt_init_impl = NULL;
crypt_finish_impl = NULL;
crypt_load_key_impl = NULL;
crypt_footprint_impl = NULL;
crypt_sign_length_impl = NULL;
crypt_sign_impl = NULL;
}
void
crypt_find_module()
{
const char *modules[CRYPT_DL_COUNT];
#ifdef CRYPT_DL_OPENSSL
modules[CRYPT_DL_OPENSSL] = PLUGIN_DIR "/crypt_openssl" LT_MODULE_EXT;
#endif
#ifdef CRYPT_DL_GCRYPT
modules[CRYPT_DL_GCRYPT] = PLUGIN_DIR "/crypt_gcrypt" LT_MODULE_EXT;
#endif
for (int i = 0; i < CRYPT_DL_COUNT; ++i) {
dl = dlopen(modules[i], RTLD_LAZY | RTLD_LOCAL);
if (NULL == dl)
continue;
crypt_init_impl = dlsym(dl, "crypt_init");
crypt_finish_impl = dlsym(dl, "crypt_finish");
crypt_load_key_impl = dlsym(dl, "crypt_load_key");
crypt_footprint_impl = dlsym(dl, "crypt_footprint");
crypt_sign_length_impl = dlsym(dl, "crypt_sign_length");
crypt_sign_impl = dlsym(dl, "crypt_sign");
if (!have_all()) {
reset_all();
dlclose(dl);
}
}
hope(have_all(), "no useable cryptography library found");
}
// See crypt.h
void
crypt_init()
{
crypt_find_module();
(*crypt_init_impl)();
}
void
crypt_finish()
{
(*crypt_finish_impl)();
dlclose(dl);
}
void
crypt_load_key(FILE *privkey)
{
(*crypt_load_key_impl)(privkey);
}
uint16_t
crypt_footprint()
{
return (*crypt_footprint_impl)();
}
size_t
crypt_sign_length()
{
return (*crypt_sign_length_impl)();
}
char *
crypt_sign(char *dst, const char *src, size_t length)
{
return (*crypt_sign_impl)(dst, src, length);
}